00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028
00029
00030 #include "avfilter.h"
00031 #include "formats.h"
00032 #include "libavutil/common.h"
00033 #include "libavutil/eval.h"
00034 #include "libavutil/avstring.h"
00035 #include "libavutil/opt.h"
00036 #include "libavutil/pixdesc.h"
00037 #include "libavutil/imgutils.h"
00038 #include "libavutil/mathematics.h"
00039 #include "libavutil/timestamp.h"
00040 #include "internal.h"
00041 #include "bufferqueue.h"
00042 #include "drawutils.h"
00043 #include "video.h"
00044
00045 static const char *const var_names[] = {
00046 "main_w", "W",
00047 "main_h", "H",
00048 "overlay_w", "w",
00049 "overlay_h", "h",
00050 NULL
00051 };
00052
00053 enum var_name {
00054 VAR_MAIN_W, VAR_MW,
00055 VAR_MAIN_H, VAR_MH,
00056 VAR_OVERLAY_W, VAR_OW,
00057 VAR_OVERLAY_H, VAR_OH,
00058 VAR_VARS_NB
00059 };
00060
00061 #define MAIN 0
00062 #define OVERLAY 1
00063
00064 #define R 0
00065 #define G 1
00066 #define B 2
00067 #define A 3
00068
00069 #define Y 0
00070 #define U 1
00071 #define V 2
00072
00073 typedef struct {
00074 const AVClass *class;
00075 int x, y;
00076
00077 int allow_packed_rgb;
00078 uint8_t frame_requested;
00079 uint8_t overlay_eof;
00080 uint8_t main_is_packed_rgb;
00081 uint8_t main_rgba_map[4];
00082 uint8_t main_has_alpha;
00083 uint8_t overlay_is_packed_rgb;
00084 uint8_t overlay_rgba_map[4];
00085 uint8_t overlay_has_alpha;
00086
00087 AVFilterBufferRef *overpicref;
00088 struct FFBufQueue queue_main;
00089 struct FFBufQueue queue_over;
00090
00091 int main_pix_step[4];
00092 int overlay_pix_step[4];
00093 int hsub, vsub;
00094
00095 char *x_expr, *y_expr;
00096 } OverlayContext;
00097
00098 #define OFFSET(x) offsetof(OverlayContext, x)
00099 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
00100
00101 static const AVOption overlay_options[] = {
00102 { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
00103 { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
00104 {"rgb", "force packed RGB in input and output", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
00105 {NULL},
00106 };
00107
00108 AVFILTER_DEFINE_CLASS(overlay);
00109
00110 static av_cold int init(AVFilterContext *ctx, const char *args)
00111 {
00112 OverlayContext *over = ctx->priv;
00113 char *args1 = av_strdup(args);
00114 char *expr, *bufptr = NULL;
00115 int ret = 0;
00116
00117 over->class = &overlay_class;
00118 av_opt_set_defaults(over);
00119
00120 if (expr = av_strtok(args1, ":", &bufptr)) {
00121 av_free(over->x_expr);
00122 if (!(over->x_expr = av_strdup(expr))) {
00123 ret = AVERROR(ENOMEM);
00124 goto end;
00125 }
00126 }
00127 if (expr = av_strtok(NULL, ":", &bufptr)) {
00128 av_free(over->y_expr);
00129 if (!(over->y_expr = av_strdup(expr))) {
00130 ret = AVERROR(ENOMEM);
00131 goto end;
00132 }
00133 }
00134
00135 if (bufptr && (ret = av_set_options_string(over, bufptr, "=", ":")) < 0)
00136 goto end;
00137
00138 end:
00139 av_free(args1);
00140 return ret;
00141 }
00142
00143 static av_cold void uninit(AVFilterContext *ctx)
00144 {
00145 OverlayContext *over = ctx->priv;
00146
00147 av_freep(&over->x_expr);
00148 av_freep(&over->y_expr);
00149
00150 avfilter_unref_bufferp(&over->overpicref);
00151 ff_bufqueue_discard_all(&over->queue_main);
00152 ff_bufqueue_discard_all(&over->queue_over);
00153 }
00154
00155 static int query_formats(AVFilterContext *ctx)
00156 {
00157 OverlayContext *over = ctx->priv;
00158
00159
00160 const enum AVPixelFormat main_pix_fmts_yuv[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE };
00161 const enum AVPixelFormat overlay_pix_fmts_yuv[] = { AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE };
00162 const enum AVPixelFormat main_pix_fmts_rgb[] = {
00163 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
00164 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
00165 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
00166 AV_PIX_FMT_NONE
00167 };
00168 const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
00169 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
00170 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
00171 AV_PIX_FMT_NONE
00172 };
00173
00174 AVFilterFormats *main_formats;
00175 AVFilterFormats *overlay_formats;
00176
00177 if (over->allow_packed_rgb) {
00178 main_formats = ff_make_format_list(main_pix_fmts_rgb);
00179 overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
00180 } else {
00181 main_formats = ff_make_format_list(main_pix_fmts_yuv);
00182 overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv);
00183 }
00184
00185 ff_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
00186 ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
00187 ff_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
00188
00189 return 0;
00190 }
00191
00192 static const enum AVPixelFormat alpha_pix_fmts[] = {
00193 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
00194 AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE
00195 };
00196
00197 static int config_input_main(AVFilterLink *inlink)
00198 {
00199 OverlayContext *over = inlink->dst->priv;
00200 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
00201
00202 av_image_fill_max_pixsteps(over->main_pix_step, NULL, pix_desc);
00203
00204 over->hsub = pix_desc->log2_chroma_w;
00205 over->vsub = pix_desc->log2_chroma_h;
00206
00207 over->main_is_packed_rgb =
00208 ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
00209 over->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
00210 return 0;
00211 }
00212
00213 static int config_input_overlay(AVFilterLink *inlink)
00214 {
00215 AVFilterContext *ctx = inlink->dst;
00216 OverlayContext *over = inlink->dst->priv;
00217 char *expr;
00218 double var_values[VAR_VARS_NB], res;
00219 int ret;
00220 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
00221
00222 av_image_fill_max_pixsteps(over->overlay_pix_step, NULL, pix_desc);
00223
00224
00225
00226 var_values[VAR_MAIN_W ] = var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
00227 var_values[VAR_MAIN_H ] = var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
00228 var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
00229 var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
00230
00231 if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
00232 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00233 goto fail;
00234 over->x = res;
00235 if ((ret = av_expr_parse_and_eval(&res, (expr = over->y_expr), var_names, var_values,
00236 NULL, NULL, NULL, NULL, NULL, 0, ctx)))
00237 goto fail;
00238 over->y = res;
00239
00240 if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
00241 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00242 goto fail;
00243 over->x = res;
00244
00245 over->overlay_is_packed_rgb =
00246 ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
00247 over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
00248
00249 av_log(ctx, AV_LOG_VERBOSE,
00250 "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
00251 ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
00252 av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
00253 over->x, over->y,
00254 ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
00255 av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
00256
00257 if (over->x < 0 || over->y < 0 ||
00258 over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
00259 over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
00260 av_log(ctx, AV_LOG_ERROR,
00261 "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
00262 over->x, over->y,
00263 (int)(over->x + var_values[VAR_OVERLAY_W]),
00264 (int)(over->y + var_values[VAR_OVERLAY_H]),
00265 (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
00266 return AVERROR(EINVAL);
00267 }
00268 return 0;
00269
00270 fail:
00271 av_log(NULL, AV_LOG_ERROR,
00272 "Error when evaluating the expression '%s'\n", expr);
00273 return ret;
00274 }
00275
00276 static int config_output(AVFilterLink *outlink)
00277 {
00278 AVFilterContext *ctx = outlink->src;
00279
00280 outlink->w = ctx->inputs[MAIN]->w;
00281 outlink->h = ctx->inputs[MAIN]->h;
00282 outlink->time_base = ctx->inputs[MAIN]->time_base;
00283
00284 return 0;
00285 }
00286
00287
00288
00289 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
00290
00291
00292
00293
00294
00295 #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
00296
00297 static void blend_slice(AVFilterContext *ctx,
00298 AVFilterBufferRef *dst, AVFilterBufferRef *src,
00299 int x, int y, int w, int h,
00300 int slice_y, int slice_w, int slice_h)
00301 {
00302 OverlayContext *over = ctx->priv;
00303 int i, j, k;
00304 int width, height;
00305 int overlay_end_y = y+h;
00306 int slice_end_y = slice_y+slice_h;
00307 int end_y, start_y;
00308
00309 width = FFMIN(slice_w - x, w);
00310 end_y = FFMIN(slice_end_y, overlay_end_y);
00311 start_y = FFMAX(y, slice_y);
00312 height = end_y - start_y;
00313
00314 if (over->main_is_packed_rgb) {
00315 uint8_t *dp = dst->data[0] + x * over->main_pix_step[0] +
00316 start_y * dst->linesize[0];
00317 uint8_t *sp = src->data[0];
00318 uint8_t alpha;
00319 const int dr = over->main_rgba_map[R];
00320 const int dg = over->main_rgba_map[G];
00321 const int db = over->main_rgba_map[B];
00322 const int da = over->main_rgba_map[A];
00323 const int dstep = over->main_pix_step[0];
00324 const int sr = over->overlay_rgba_map[R];
00325 const int sg = over->overlay_rgba_map[G];
00326 const int sb = over->overlay_rgba_map[B];
00327 const int sa = over->overlay_rgba_map[A];
00328 const int sstep = over->overlay_pix_step[0];
00329 const int main_has_alpha = over->main_has_alpha;
00330 if (slice_y > y)
00331 sp += (slice_y - y) * src->linesize[0];
00332 for (i = 0; i < height; i++) {
00333 uint8_t *d = dp, *s = sp;
00334 for (j = 0; j < width; j++) {
00335 alpha = s[sa];
00336
00337
00338
00339 if (main_has_alpha && alpha != 0 && alpha != 255) {
00340 uint8_t alpha_d = d[da];
00341 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
00342 }
00343
00344 switch (alpha) {
00345 case 0:
00346 break;
00347 case 255:
00348 d[dr] = s[sr];
00349 d[dg] = s[sg];
00350 d[db] = s[sb];
00351 break;
00352 default:
00353
00354
00355 d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
00356 d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
00357 d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
00358 }
00359 if (main_has_alpha) {
00360 switch (alpha) {
00361 case 0:
00362 break;
00363 case 255:
00364 d[da] = s[sa];
00365 break;
00366 default:
00367
00368 d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
00369 }
00370 }
00371 d += dstep;
00372 s += sstep;
00373 }
00374 dp += dst->linesize[0];
00375 sp += src->linesize[0];
00376 }
00377 } else {
00378 const int main_has_alpha = over->main_has_alpha;
00379 if (main_has_alpha) {
00380 uint8_t *da = dst->data[3] + x * over->main_pix_step[3] +
00381 start_y * dst->linesize[3];
00382 uint8_t *sa = src->data[3];
00383 uint8_t alpha;
00384 if (slice_y > y)
00385 sa += (slice_y - y) * src->linesize[3];
00386 for (i = 0; i < height; i++) {
00387 uint8_t *d = da, *s = sa;
00388 for (j = 0; j < width; j++) {
00389 alpha = *s;
00390 if (alpha != 0 && alpha != 255) {
00391 uint8_t alpha_d = *d;
00392 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
00393 }
00394 switch (alpha) {
00395 case 0:
00396 break;
00397 case 255:
00398 *d = *s;
00399 break;
00400 default:
00401
00402 *d += FAST_DIV255((255 - *d) * *s);
00403 }
00404 d += 1;
00405 s += 1;
00406 }
00407 da += dst->linesize[3];
00408 sa += src->linesize[3];
00409 }
00410 }
00411 for (i = 0; i < 3; i++) {
00412 int hsub = i ? over->hsub : 0;
00413 int vsub = i ? over->vsub : 0;
00414 uint8_t *dp = dst->data[i] + (x >> hsub) +
00415 (start_y >> vsub) * dst->linesize[i];
00416 uint8_t *sp = src->data[i];
00417 uint8_t *ap = src->data[3];
00418 int wp = FFALIGN(width, 1<<hsub) >> hsub;
00419 int hp = FFALIGN(height, 1<<vsub) >> vsub;
00420 if (slice_y > y) {
00421 sp += ((slice_y - y) >> vsub) * src->linesize[i];
00422 ap += (slice_y - y) * src->linesize[3];
00423 }
00424 for (j = 0; j < hp; j++) {
00425 uint8_t *d = dp, *s = sp, *a = ap;
00426 for (k = 0; k < wp; k++) {
00427
00428 int alpha_v, alpha_h, alpha;
00429 if (hsub && vsub && j+1 < hp && k+1 < wp) {
00430 alpha = (a[0] + a[src->linesize[3]] +
00431 a[1] + a[src->linesize[3]+1]) >> 2;
00432 } else if (hsub || vsub) {
00433 alpha_h = hsub && k+1 < wp ?
00434 (a[0] + a[1]) >> 1 : a[0];
00435 alpha_v = vsub && j+1 < hp ?
00436 (a[0] + a[src->linesize[3]]) >> 1 : a[0];
00437 alpha = (alpha_v + alpha_h) >> 1;
00438 } else
00439 alpha = a[0];
00440
00441
00442 if (main_has_alpha && alpha != 0 && alpha != 255) {
00443
00444 uint8_t alpha_d;
00445 if (hsub && vsub && j+1 < hp && k+1 < wp) {
00446 alpha_d = (d[0] + d[src->linesize[3]] +
00447 d[1] + d[src->linesize[3]+1]) >> 2;
00448 } else if (hsub || vsub) {
00449 alpha_h = hsub && k+1 < wp ?
00450 (d[0] + d[1]) >> 1 : d[0];
00451 alpha_v = vsub && j+1 < hp ?
00452 (d[0] + d[src->linesize[3]]) >> 1 : d[0];
00453 alpha_d = (alpha_v + alpha_h) >> 1;
00454 } else
00455 alpha_d = d[0];
00456 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
00457 }
00458 *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
00459 s++;
00460 d++;
00461 a += 1 << hsub;
00462 }
00463 dp += dst->linesize[i];
00464 sp += src->linesize[i];
00465 ap += (1 << vsub) * src->linesize[3];
00466 }
00467 }
00468 }
00469 }
00470
00471 static int try_start_frame(AVFilterContext *ctx, AVFilterBufferRef *mainpic)
00472 {
00473 OverlayContext *over = ctx->priv;
00474 AVFilterLink *outlink = ctx->outputs[0];
00475 AVFilterBufferRef *next_overpic, *outpicref;
00476 int ret;
00477
00478
00479
00480 while (1) {
00481 next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
00482 if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
00483 mainpic->pts , ctx->inputs[MAIN]->time_base) > 0)
00484 break;
00485 ff_bufqueue_get(&over->queue_over);
00486 avfilter_unref_buffer(over->overpicref);
00487 over->overpicref = next_overpic;
00488 }
00489
00490
00491
00492 if (!over->queue_over.available && !over->overlay_eof &&
00493 (!over->overpicref || av_compare_ts(over->overpicref->pts, ctx->inputs[OVERLAY]->time_base,
00494 mainpic->pts , ctx->inputs[MAIN]->time_base) < 0))
00495 return AVERROR(EAGAIN);
00496
00497
00498
00499 outlink->out_buf = outpicref = avfilter_ref_buffer(mainpic, ~0);
00500
00501 av_dlog(ctx, "main_pts:%s main_pts_time:%s",
00502 av_ts2str(outpicref->pts), av_ts2timestr(outpicref->pts, &outlink->time_base));
00503 if (over->overpicref)
00504 av_dlog(ctx, " over_pts:%s over_pts_time:%s",
00505 av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &outlink->time_base));
00506 av_dlog(ctx, "\n");
00507
00508 ret = ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(outpicref, ~0));
00509 over->frame_requested = 0;
00510 return ret;
00511 }
00512
00513 static int try_start_next_frame(AVFilterContext *ctx)
00514 {
00515 OverlayContext *over = ctx->priv;
00516 AVFilterBufferRef *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
00517 int ret;
00518
00519 if (!next_mainpic)
00520 return AVERROR(EAGAIN);
00521 if ((ret = try_start_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
00522 return ret;
00523 avfilter_unref_buffer(ff_bufqueue_get(&over->queue_main));
00524 return ret;
00525 }
00526
00527 static int try_push_frame(AVFilterContext *ctx)
00528 {
00529 OverlayContext *over = ctx->priv;
00530 AVFilterLink *outlink = ctx->outputs[0];
00531 AVFilterBufferRef *outpicref;
00532 int ret;
00533
00534 if ((ret = try_start_next_frame(ctx)) < 0)
00535 return ret;
00536 outpicref = outlink->out_buf;
00537 if (over->overpicref)
00538 blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
00539 over->overpicref->video->w, over->overpicref->video->h,
00540 0, outpicref->video->w, outpicref->video->h);
00541 if ((ret = ff_draw_slice(outlink, 0, outpicref->video->h, +1)) < 0 ||
00542 (ret = ff_end_frame(outlink)) < 0)
00543 return ret;
00544 return 0;
00545 }
00546
00547 static int flush_frames(AVFilterContext *ctx)
00548 {
00549 int ret;
00550
00551 while (!(ret = try_push_frame(ctx)));
00552 return ret == AVERROR(EAGAIN) ? 0 : ret;
00553 }
00554
00555 static int start_frame_main(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00556 {
00557 AVFilterContext *ctx = inlink->dst;
00558 OverlayContext *over = ctx->priv;
00559 int ret;
00560
00561 if ((ret = flush_frames(ctx)) < 0)
00562 return ret;
00563 if ((ret = try_start_frame(ctx, inpicref)) < 0) {
00564 if (ret != AVERROR(EAGAIN))
00565 return ret;
00566 ff_bufqueue_add(ctx, &over->queue_main, inpicref);
00567 av_assert1(inpicref == inlink->cur_buf);
00568 inlink->cur_buf = NULL;
00569 }
00570 return 0;
00571 }
00572
00573 static int draw_slice_main(AVFilterLink *inlink, int y, int h, int slice_dir)
00574 {
00575 AVFilterContext *ctx = inlink->dst;
00576 OverlayContext *over = ctx->priv;
00577 AVFilterLink *outlink = ctx->outputs[0];
00578 AVFilterBufferRef *outpicref = outlink->out_buf;
00579
00580 if (!outpicref)
00581 return 0;
00582 if (over->overpicref &&
00583 y + h > over->y && y < over->y + over->overpicref->video->h) {
00584 blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
00585 over->overpicref->video->w, over->overpicref->video->h,
00586 y, outpicref->video->w, h);
00587 }
00588 return ff_draw_slice(outlink, y, h, slice_dir);
00589 }
00590
00591 static int end_frame_main(AVFilterLink *inlink)
00592 {
00593 AVFilterContext *ctx = inlink->dst;
00594 AVFilterLink *outlink = ctx->outputs[0];
00595 AVFilterBufferRef *outpicref = outlink->out_buf;
00596 flush_frames(ctx);
00597
00598 if (!outpicref)
00599 return 0;
00600 return ff_end_frame(ctx->outputs[0]);
00601 }
00602
00603 static int start_frame_over(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00604 {
00605 return 0;
00606 }
00607
00608 static int end_frame_over(AVFilterLink *inlink)
00609 {
00610 AVFilterContext *ctx = inlink->dst;
00611 OverlayContext *over = ctx->priv;
00612 AVFilterBufferRef *inpicref = inlink->cur_buf;
00613 int ret;
00614
00615 inlink->cur_buf = NULL;
00616
00617 if ((ret = flush_frames(ctx)) < 0)
00618 return ret;
00619 ff_bufqueue_add(ctx, &over->queue_over, inpicref);
00620 ret = try_push_frame(ctx);
00621 return ret == AVERROR(EAGAIN) ? 0 : ret;
00622 }
00623
00624 static int request_frame(AVFilterLink *outlink)
00625 {
00626 AVFilterContext *ctx = outlink->src;
00627 OverlayContext *over = ctx->priv;
00628 int input, ret;
00629
00630 if (!try_push_frame(ctx))
00631 return 0;
00632 over->frame_requested = 1;
00633 while (over->frame_requested) {
00634
00635 input = !over->overlay_eof && (over->queue_main.available ||
00636 over->queue_over.available < 2) ?
00637 OVERLAY : MAIN;
00638 ret = ff_request_frame(ctx->inputs[input]);
00639
00640 if (ret == AVERROR_EOF && input == OVERLAY) {
00641 over->overlay_eof = 1;
00642 if ((ret = try_start_next_frame(ctx)) != AVERROR(EAGAIN))
00643 return ret;
00644 ret = 0;
00645 }
00646 if (ret < 0)
00647 return ret;
00648 }
00649 return 0;
00650 }
00651
00652 static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00653 {
00654 return 0;
00655 }
00656
00657 static const AVFilterPad avfilter_vf_overlay_inputs[] = {
00658 {
00659 .name = "main",
00660 .type = AVMEDIA_TYPE_VIDEO,
00661 .get_video_buffer = ff_null_get_video_buffer,
00662 .config_props = config_input_main,
00663 .start_frame = start_frame_main,
00664 .draw_slice = draw_slice_main,
00665 .end_frame = end_frame_main,
00666 .min_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE,
00667 },
00668 {
00669 .name = "overlay",
00670 .type = AVMEDIA_TYPE_VIDEO,
00671 .config_props = config_input_overlay,
00672 .start_frame = start_frame_over,
00673 .draw_slice = null_draw_slice,
00674 .end_frame = end_frame_over,
00675 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
00676 },
00677 { NULL }
00678 };
00679
00680 static const AVFilterPad avfilter_vf_overlay_outputs[] = {
00681 {
00682 .name = "default",
00683 .type = AVMEDIA_TYPE_VIDEO,
00684 .rej_perms = AV_PERM_WRITE,
00685 .config_props = config_output,
00686 .request_frame = request_frame,
00687 },
00688 { NULL }
00689 };
00690
00691 AVFilter avfilter_vf_overlay = {
00692 .name = "overlay",
00693 .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
00694
00695 .init = init,
00696 .uninit = uninit,
00697
00698 .priv_size = sizeof(OverlayContext),
00699
00700 .query_formats = query_formats,
00701
00702 .inputs = avfilter_vf_overlay_inputs,
00703 .outputs = avfilter_vf_overlay_outputs,
00704 .priv_class = &overlay_class,
00705 };