2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2010 Baptiste Coudurier
4 * Copyright (c) 2007 Bobby Bingham
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * overlay one video on top of another
32 #include "libavutil/common.h"
33 #include "libavutil/eval.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/timestamp.h"
41 #include "bufferqueue.h"
42 #include "drawutils.h"
45 static const char *const var_names[] = {
46 "main_w", "W", ///< width of the main video
47 "main_h", "H", ///< height of the main video
48 "overlay_w", "w", ///< width of the overlay video
49 "overlay_h", "h", ///< height of the overlay video
54 "n", ///< number of frame
55 "pos", ///< position in the file
56 "t", ///< timestamp expressed in seconds
63 VAR_OVERLAY_W, VAR_OW,
64 VAR_OVERLAY_H, VAR_OH,
89 int x, y; ///< position of overlayed picture
90 double enable; ///< tells if blending is enabled
93 uint8_t frame_requested;
95 uint8_t main_is_packed_rgb;
96 uint8_t main_rgba_map[4];
97 uint8_t main_has_alpha;
98 uint8_t overlay_is_packed_rgb;
99 uint8_t overlay_rgba_map[4];
100 uint8_t overlay_has_alpha;
101 enum OverlayFormat { OVERLAY_FORMAT_YUV420, OVERLAY_FORMAT_YUV444, OVERLAY_FORMAT_RGB, OVERLAY_FORMAT_NB} format;
102 enum EvalMode { EVAL_MODE_INIT, EVAL_MODE_FRAME, EVAL_MODE_NB } eval_mode;
105 struct FFBufQueue queue_main;
106 struct FFBufQueue queue_over;
108 int main_pix_step[4]; ///< steps per pixel for each plane of the main output
109 int overlay_pix_step[4]; ///< steps per pixel for each plane of the overlay
110 int hsub, vsub; ///< chroma subsampling values
111 int shortest; ///< terminate stream when the shortest input terminates
113 double var_values[VAR_VARS_NB];
114 char *x_expr, *y_expr, *enable_expr;
115 AVExpr *x_pexpr, *y_pexpr, *enable_pexpr;
118 #define OFFSET(x) offsetof(OverlayContext, x)
119 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
121 static const AVOption overlay_options[] = {
122 { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
123 { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
124 { "enable", "set expression which enables overlay", OFFSET(enable_expr), AV_OPT_TYPE_STRING, {.str = "1"}, .flags = FLAGS },
126 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
127 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
128 { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
130 { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
131 { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
133 { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
134 { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
135 { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
136 { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
141 AVFILTER_DEFINE_CLASS(overlay);
143 static av_cold int init(AVFilterContext *ctx, const char *args)
145 OverlayContext *over = ctx->priv;
147 if (over->allow_packed_rgb) {
148 av_log(ctx, AV_LOG_WARNING,
149 "The rgb option is deprecated and is overriding the format option, use format instead\n");
150 over->format = OVERLAY_FORMAT_RGB;
155 static av_cold void uninit(AVFilterContext *ctx)
157 OverlayContext *over = ctx->priv;
159 av_frame_free(&over->overpicref);
160 ff_bufqueue_discard_all(&over->queue_main);
161 ff_bufqueue_discard_all(&over->queue_over);
162 av_expr_free(over->x_pexpr); over->x_pexpr = NULL;
163 av_expr_free(over->y_pexpr); over->y_pexpr = NULL;
164 av_expr_free(over->enable_pexpr); over->enable_pexpr = NULL;
167 static inline int normalize_xy(double d, int chroma_sub)
171 return (int)d & ~((1 << chroma_sub) - 1);
174 enum EvalTarget { EVAL_XY, EVAL_ENABLE, EVAL_ALL };
176 static void eval_expr(AVFilterContext *ctx, enum EvalTarget eval_tgt)
178 OverlayContext *over = ctx->priv;
180 if (eval_tgt == EVAL_XY || eval_tgt == EVAL_ALL) {
181 over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
182 over->var_values[VAR_Y] = av_expr_eval(over->y_pexpr, over->var_values, NULL);
183 over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
184 over->x = normalize_xy(over->var_values[VAR_X], over->hsub);
185 over->y = normalize_xy(over->var_values[VAR_Y], over->vsub);
187 if (eval_tgt == EVAL_ENABLE || eval_tgt == EVAL_ALL) {
188 over->enable = av_expr_eval(over->enable_pexpr, over->var_values, NULL);
192 static int set_expr(AVExpr **pexpr, const char *expr, void *log_ctx)
197 av_expr_free(*pexpr);
199 ret = av_expr_parse(pexpr, expr, var_names,
200 NULL, NULL, NULL, NULL, 0, log_ctx);
202 av_log(log_ctx, AV_LOG_ERROR,
203 "Error when evaluating the expression '%s'\n", expr);
207 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
208 char *res, int res_len, int flags)
210 OverlayContext *over = ctx->priv;
213 if (!strcmp(cmd, "x"))
214 ret = set_expr(&over->x_pexpr, args, ctx);
215 else if (!strcmp(cmd, "y"))
216 ret = set_expr(&over->y_pexpr, args, ctx);
217 else if (!strcmp(cmd, "enable"))
218 ret = set_expr(&over->enable_pexpr, args, ctx);
220 ret = AVERROR(ENOSYS);
225 if (over->eval_mode == EVAL_MODE_INIT) {
226 eval_expr(ctx, EVAL_ALL);
227 av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d enable:%f\n",
228 over->var_values[VAR_X], over->x,
229 over->var_values[VAR_Y], over->y,
235 static int query_formats(AVFilterContext *ctx)
237 OverlayContext *over = ctx->priv;
239 /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
240 static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
241 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
243 static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
244 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
247 static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
248 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
250 static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
251 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
254 static const enum AVPixelFormat main_pix_fmts_rgb[] = {
255 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
256 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
257 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
260 static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
261 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
262 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
266 AVFilterFormats *main_formats;
267 AVFilterFormats *overlay_formats;
269 switch (over->format) {
270 case OVERLAY_FORMAT_YUV420:
271 main_formats = ff_make_format_list(main_pix_fmts_yuv420);
272 overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420);
274 case OVERLAY_FORMAT_YUV444:
275 main_formats = ff_make_format_list(main_pix_fmts_yuv444);
276 overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444);
278 case OVERLAY_FORMAT_RGB:
279 main_formats = ff_make_format_list(main_pix_fmts_rgb);
280 overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
286 ff_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
287 ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
288 ff_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
293 static const enum AVPixelFormat alpha_pix_fmts[] = {
294 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA444P,
295 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
296 AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE
299 static int config_input_main(AVFilterLink *inlink)
301 OverlayContext *over = inlink->dst->priv;
302 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
304 av_image_fill_max_pixsteps(over->main_pix_step, NULL, pix_desc);
306 over->hsub = pix_desc->log2_chroma_w;
307 over->vsub = pix_desc->log2_chroma_h;
309 over->main_is_packed_rgb =
310 ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
311 over->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
315 static int config_input_overlay(AVFilterLink *inlink)
317 AVFilterContext *ctx = inlink->dst;
318 OverlayContext *over = inlink->dst->priv;
320 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
322 av_image_fill_max_pixsteps(over->overlay_pix_step, NULL, pix_desc);
324 /* Finish the configuration by evaluating the expressions
325 now when both inputs are configured. */
326 over->var_values[VAR_MAIN_W ] = over->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
327 over->var_values[VAR_MAIN_H ] = over->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
328 over->var_values[VAR_OVERLAY_W] = over->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
329 over->var_values[VAR_OVERLAY_H] = over->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
330 over->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
331 over->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
332 over->var_values[VAR_X] = NAN;
333 over->var_values[VAR_Y] = NAN;
334 over->var_values[VAR_N] = 0;
335 over->var_values[VAR_T] = NAN;
336 over->var_values[VAR_POS] = NAN;
338 if ((ret = set_expr(&over->x_pexpr, over->x_expr, ctx)) < 0 ||
339 (ret = set_expr(&over->y_pexpr, over->y_expr, ctx)) < 0 ||
340 (ret = set_expr(&over->enable_pexpr, over->enable_expr, ctx)) < 0)
343 over->overlay_is_packed_rgb =
344 ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
345 over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
347 if (over->eval_mode == EVAL_MODE_INIT) {
348 eval_expr(ctx, EVAL_ALL);
349 av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d enable:%f\n",
350 over->var_values[VAR_X], over->x,
351 over->var_values[VAR_Y], over->y,
355 av_log(ctx, AV_LOG_VERBOSE,
356 "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
357 ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
358 av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
359 ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
360 av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
364 static int config_output(AVFilterLink *outlink)
366 AVFilterContext *ctx = outlink->src;
368 outlink->w = ctx->inputs[MAIN]->w;
369 outlink->h = ctx->inputs[MAIN]->h;
370 outlink->time_base = ctx->inputs[MAIN]->time_base;
375 // divide by 255 and round to nearest
376 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
377 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
379 // calculate the unpremultiplied alpha, applying the general equation:
380 // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
381 // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
382 // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
383 #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
386 * Blend image in src to destination buffer dst at position (x, y).
388 static void blend_image(AVFilterContext *ctx,
389 AVFrame *dst, AVFrame *src,
392 OverlayContext *over = ctx->priv;
393 int i, imax, j, jmax, k, kmax;
394 const int src_w = src->width;
395 const int src_h = src->height;
396 const int dst_w = dst->width;
397 const int dst_h = dst->height;
399 if (x >= dst_w || x+dst_w < 0 ||
400 y >= dst_h || y+dst_h < 0)
401 return; /* no intersection */
403 if (over->main_is_packed_rgb) {
404 uint8_t alpha; ///< the amount of overlay to blend on to main
405 const int dr = over->main_rgba_map[R];
406 const int dg = over->main_rgba_map[G];
407 const int db = over->main_rgba_map[B];
408 const int da = over->main_rgba_map[A];
409 const int dstep = over->main_pix_step[0];
410 const int sr = over->overlay_rgba_map[R];
411 const int sg = over->overlay_rgba_map[G];
412 const int sb = over->overlay_rgba_map[B];
413 const int sa = over->overlay_rgba_map[A];
414 const int sstep = over->overlay_pix_step[0];
415 const int main_has_alpha = over->main_has_alpha;
416 uint8_t *s, *sp, *d, *dp;
419 sp = src->data[0] + i * src->linesize[0];
420 dp = dst->data[0] + (y+i) * dst->linesize[0];
422 for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
425 d = dp + (x+j) * dstep;
427 for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
430 // if the main channel has an alpha channel, alpha has to be calculated
431 // to create an un-premultiplied (straight) alpha value
432 if (main_has_alpha && alpha != 0 && alpha != 255) {
433 uint8_t alpha_d = d[da];
434 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
446 // main_value = main_value * (1 - alpha) + overlay_value * alpha
447 // since alpha is in the range 0-255, the result must divided by 255
448 d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
449 d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
450 d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
452 if (main_has_alpha) {
460 // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
461 d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
467 dp += dst->linesize[0];
468 sp += src->linesize[0];
471 const int main_has_alpha = over->main_has_alpha;
472 if (main_has_alpha) {
473 uint8_t alpha; ///< the amount of overlay to blend on to main
474 uint8_t *s, *sa, *d, *da;
477 sa = src->data[3] + i * src->linesize[3];
478 da = dst->data[3] + (y+i) * dst->linesize[3];
480 for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
485 for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
487 if (alpha != 0 && alpha != 255) {
488 uint8_t alpha_d = *d;
489 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
498 // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
499 *d += FAST_DIV255((255 - *d) * *s);
504 da += dst->linesize[3];
505 sa += src->linesize[3];
508 for (i = 0; i < 3; i++) {
509 int hsub = i ? over->hsub : 0;
510 int vsub = i ? over->vsub : 0;
511 int src_wp = FFALIGN(src_w, 1<<hsub) >> hsub;
512 int src_hp = FFALIGN(src_h, 1<<vsub) >> vsub;
513 int dst_wp = FFALIGN(dst_w, 1<<hsub) >> hsub;
514 int dst_hp = FFALIGN(dst_h, 1<<vsub) >> vsub;
517 uint8_t *s, *sp, *d, *dp, *a, *ap;
520 sp = src->data[i] + j * src->linesize[i];
521 dp = dst->data[i] + (yp+j) * dst->linesize[i];
522 ap = src->data[3] + (j<<vsub) * src->linesize[3];
524 for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
530 for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
531 int alpha_v, alpha_h, alpha;
533 // average alpha for color components, improve quality
534 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
535 alpha = (a[0] + a[src->linesize[3]] +
536 a[1] + a[src->linesize[3]+1]) >> 2;
537 } else if (hsub || vsub) {
538 alpha_h = hsub && k+1 < src_wp ?
539 (a[0] + a[1]) >> 1 : a[0];
540 alpha_v = vsub && j+1 < src_hp ?
541 (a[0] + a[src->linesize[3]]) >> 1 : a[0];
542 alpha = (alpha_v + alpha_h) >> 1;
545 // if the main channel has an alpha channel, alpha has to be calculated
546 // to create an un-premultiplied (straight) alpha value
547 if (main_has_alpha && alpha != 0 && alpha != 255) {
548 // average alpha for color components, improve quality
550 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
551 alpha_d = (d[0] + d[src->linesize[3]] +
552 d[1] + d[src->linesize[3]+1]) >> 2;
553 } else if (hsub || vsub) {
554 alpha_h = hsub && k+1 < src_wp ?
555 (d[0] + d[1]) >> 1 : d[0];
556 alpha_v = vsub && j+1 < src_hp ?
557 (d[0] + d[src->linesize[3]]) >> 1 : d[0];
558 alpha_d = (alpha_v + alpha_h) >> 1;
561 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
563 *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
568 dp += dst->linesize[i];
569 sp += src->linesize[i];
570 ap += (1 << vsub) * src->linesize[3];
576 static int try_filter_frame(AVFilterContext *ctx, AVFrame *mainpic)
578 OverlayContext *over = ctx->priv;
579 AVFilterLink *inlink = ctx->inputs[0];
580 AVFrame *next_overpic;
583 /* Discard obsolete overlay frames: if there is a next overlay frame with pts
584 * before the main frame, we can drop the current overlay. */
586 next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
587 if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
588 mainpic->pts , ctx->inputs[MAIN]->time_base) > 0)
590 ff_bufqueue_get(&over->queue_over);
591 av_frame_free(&over->overpicref);
592 over->overpicref = next_overpic;
595 /* If there is no next frame and no EOF and the overlay frame is before
596 * the main frame, we can not know yet if it will be superseded. */
597 if (!over->queue_over.available && !over->overlay_eof &&
598 (!over->overpicref || av_compare_ts(over->overpicref->pts, ctx->inputs[OVERLAY]->time_base,
599 mainpic->pts , ctx->inputs[MAIN]->time_base) < 0))
600 return AVERROR(EAGAIN);
602 /* At this point, we know that the current overlay frame extends to the
603 * time of the main frame. */
604 av_dlog(ctx, "main_pts:%s main_pts_time:%s",
605 av_ts2str(mainpic->pts), av_ts2timestr(mainpic->pts, &ctx->inputs[MAIN]->time_base));
606 if (over->overpicref)
607 av_dlog(ctx, " over_pts:%s over_pts_time:%s",
608 av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &ctx->inputs[OVERLAY]->time_base));
611 if (over->overpicref) {
612 if (over->eval_mode == EVAL_MODE_FRAME) {
613 int64_t pos = av_frame_get_pkt_pos(mainpic);
615 over->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
616 NAN : mainpic->pts * av_q2d(inlink->time_base);
617 over->var_values[VAR_POS] = pos == -1 ? NAN : pos;
619 eval_expr(ctx, EVAL_ALL);
620 av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d enable:%f\n",
621 over->var_values[VAR_N], over->var_values[VAR_T], over->var_values[VAR_POS],
622 over->var_values[VAR_X], over->x,
623 over->var_values[VAR_Y], over->y,
627 blend_image(ctx, mainpic, over->overpicref, over->x, over->y);
629 over->var_values[VAR_N] += 1.0;
631 ret = ff_filter_frame(ctx->outputs[0], mainpic);
632 av_assert1(ret != AVERROR(EAGAIN));
633 over->frame_requested = 0;
637 static int try_filter_next_frame(AVFilterContext *ctx)
639 OverlayContext *over = ctx->priv;
640 AVFrame *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
644 return AVERROR(EAGAIN);
645 if ((ret = try_filter_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
647 ff_bufqueue_get(&over->queue_main);
651 static int flush_frames(AVFilterContext *ctx)
655 while (!(ret = try_filter_next_frame(ctx)));
656 return ret == AVERROR(EAGAIN) ? 0 : ret;
659 static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
661 AVFilterContext *ctx = inlink->dst;
662 OverlayContext *over = ctx->priv;
665 if ((ret = flush_frames(ctx)) < 0)
667 if ((ret = try_filter_frame(ctx, inpicref)) < 0) {
668 if (ret != AVERROR(EAGAIN))
670 ff_bufqueue_add(ctx, &over->queue_main, inpicref);
673 if (!over->overpicref)
680 static int filter_frame_over(AVFilterLink *inlink, AVFrame *inpicref)
682 AVFilterContext *ctx = inlink->dst;
683 OverlayContext *over = ctx->priv;
686 if ((ret = flush_frames(ctx)) < 0)
688 ff_bufqueue_add(ctx, &over->queue_over, inpicref);
689 ret = try_filter_next_frame(ctx);
690 return ret == AVERROR(EAGAIN) ? 0 : ret;
693 static int request_frame(AVFilterLink *outlink)
695 AVFilterContext *ctx = outlink->src;
696 OverlayContext *over = ctx->priv;
699 if (!try_filter_next_frame(ctx))
701 over->frame_requested = 1;
702 while (over->frame_requested) {
703 /* TODO if we had a frame duration, we could guess more accurately */
704 input = !over->overlay_eof && (over->queue_main.available ||
705 over->queue_over.available < 2) ?
707 ret = ff_request_frame(ctx->inputs[input]);
708 /* EOF on main is reported immediately */
709 if (ret == AVERROR_EOF && input == OVERLAY) {
710 over->overlay_eof = 1;
713 if ((ret = try_filter_next_frame(ctx)) != AVERROR(EAGAIN))
715 ret = 0; /* continue requesting frames on main */
723 static const AVFilterPad avfilter_vf_overlay_inputs[] = {
726 .type = AVMEDIA_TYPE_VIDEO,
727 .get_video_buffer = ff_null_get_video_buffer,
728 .config_props = config_input_main,
729 .filter_frame = filter_frame_main,
734 .type = AVMEDIA_TYPE_VIDEO,
735 .config_props = config_input_overlay,
736 .filter_frame = filter_frame_over,
741 static const AVFilterPad avfilter_vf_overlay_outputs[] = {
744 .type = AVMEDIA_TYPE_VIDEO,
745 .config_props = config_output,
746 .request_frame = request_frame,
751 static const char *const shorthand[] = { "x", "y", NULL };
753 AVFilter avfilter_vf_overlay = {
755 .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
760 .priv_size = sizeof(OverlayContext),
762 .query_formats = query_formats,
763 .process_command = process_command,
765 .inputs = avfilter_vf_overlay_inputs,
766 .outputs = avfilter_vf_overlay_outputs,
767 .priv_class = &overlay_class,
768 .shorthand = shorthand,