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/opt.h"
40 #include "libavutil/timestamp.h"
42 #include "bufferqueue.h"
43 #include "drawutils.h"
46 static const char *const var_names[] = {
47 "main_w", "W", ///< width of the main video
48 "main_h", "H", ///< height of the main video
49 "overlay_w", "w", ///< width of the overlay video
50 "overlay_h", "h", ///< height of the overlay video
55 "n", ///< number of frame
56 "pos", ///< position in the file
57 "t", ///< timestamp expressed in seconds
64 VAR_OVERLAY_W, VAR_OW,
65 VAR_OVERLAY_H, VAR_OH,
90 int x, y; ///< position of overlayed picture
91 int enable; ///< tells if blending is enabled
94 uint8_t frame_requested;
96 uint8_t main_is_packed_rgb;
97 uint8_t main_rgba_map[4];
98 uint8_t main_has_alpha;
99 uint8_t overlay_is_packed_rgb;
100 uint8_t overlay_rgba_map[4];
101 uint8_t overlay_has_alpha;
102 enum OverlayFormat { OVERLAY_FORMAT_YUV420, OVERLAY_FORMAT_YUV444, OVERLAY_FORMAT_RGB, OVERLAY_FORMAT_NB} format;
103 enum EvalMode { EVAL_MODE_INIT, EVAL_MODE_FRAME, EVAL_MODE_NB } eval_mode;
106 struct FFBufQueue queue_main;
107 struct FFBufQueue queue_over;
109 int main_pix_step[4]; ///< steps per pixel for each plane of the main output
110 int overlay_pix_step[4]; ///< steps per pixel for each plane of the overlay
111 int hsub, vsub; ///< chroma subsampling values
112 int shortest; ///< terminate stream when the shortest input terminates
113 int repeatlast; ///< repeat last overlay frame
115 double var_values[VAR_VARS_NB];
116 char *x_expr, *y_expr;
117 AVExpr *x_pexpr, *y_pexpr;
120 static av_cold int init(AVFilterContext *ctx)
122 OverlayContext *over = ctx->priv;
124 if (over->allow_packed_rgb) {
125 av_log(ctx, AV_LOG_WARNING,
126 "The rgb option is deprecated and is overriding the format option, use format instead\n");
127 over->format = OVERLAY_FORMAT_RGB;
132 static av_cold void uninit(AVFilterContext *ctx)
134 OverlayContext *over = ctx->priv;
136 av_frame_free(&over->overpicref);
137 ff_bufqueue_discard_all(&over->queue_main);
138 ff_bufqueue_discard_all(&over->queue_over);
139 av_expr_free(over->x_pexpr); over->x_pexpr = NULL;
140 av_expr_free(over->y_pexpr); over->y_pexpr = NULL;
143 static inline int normalize_xy(double d, int chroma_sub)
147 return (int)d & ~((1 << chroma_sub) - 1);
150 static void eval_expr(AVFilterContext *ctx)
152 OverlayContext *over = ctx->priv;
155 over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
156 over->var_values[VAR_Y] = av_expr_eval(over->y_pexpr, over->var_values, NULL);
157 over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
158 over->x = normalize_xy(over->var_values[VAR_X], over->hsub);
159 over->y = normalize_xy(over->var_values[VAR_Y], over->vsub);
162 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
169 ret = av_expr_parse(pexpr, expr, var_names,
170 NULL, NULL, NULL, NULL, 0, log_ctx);
172 av_log(log_ctx, AV_LOG_ERROR,
173 "Error when evaluating the expression '%s' for %s\n",
183 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
184 char *res, int res_len, int flags)
186 OverlayContext *over = ctx->priv;
189 if (!strcmp(cmd, "x"))
190 ret = set_expr(&over->x_pexpr, args, cmd, ctx);
191 else if (!strcmp(cmd, "y"))
192 ret = set_expr(&over->y_pexpr, args, cmd, ctx);
194 ret = AVERROR(ENOSYS);
199 if (over->eval_mode == EVAL_MODE_INIT) {
201 av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
202 over->var_values[VAR_X], over->x,
203 over->var_values[VAR_Y], over->y);
208 static int query_formats(AVFilterContext *ctx)
210 OverlayContext *over = ctx->priv;
212 /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
213 static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
214 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
216 static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
217 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
220 static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
221 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
223 static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
224 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
227 static const enum AVPixelFormat main_pix_fmts_rgb[] = {
228 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
229 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
230 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
233 static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
234 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
235 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
239 AVFilterFormats *main_formats;
240 AVFilterFormats *overlay_formats;
242 switch (over->format) {
243 case OVERLAY_FORMAT_YUV420:
244 main_formats = ff_make_format_list(main_pix_fmts_yuv420);
245 overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420);
247 case OVERLAY_FORMAT_YUV444:
248 main_formats = ff_make_format_list(main_pix_fmts_yuv444);
249 overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444);
251 case OVERLAY_FORMAT_RGB:
252 main_formats = ff_make_format_list(main_pix_fmts_rgb);
253 overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
259 ff_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
260 ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
261 ff_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
266 static const enum AVPixelFormat alpha_pix_fmts[] = {
267 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA444P,
268 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
269 AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE
272 static int config_input_main(AVFilterLink *inlink)
274 OverlayContext *over = inlink->dst->priv;
275 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
277 av_image_fill_max_pixsteps(over->main_pix_step, NULL, pix_desc);
279 over->hsub = pix_desc->log2_chroma_w;
280 over->vsub = pix_desc->log2_chroma_h;
282 over->main_is_packed_rgb =
283 ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
284 over->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
288 static int config_input_overlay(AVFilterLink *inlink)
290 AVFilterContext *ctx = inlink->dst;
291 OverlayContext *over = inlink->dst->priv;
293 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
295 av_image_fill_max_pixsteps(over->overlay_pix_step, NULL, pix_desc);
297 /* Finish the configuration by evaluating the expressions
298 now when both inputs are configured. */
299 over->var_values[VAR_MAIN_W ] = over->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
300 over->var_values[VAR_MAIN_H ] = over->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
301 over->var_values[VAR_OVERLAY_W] = over->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
302 over->var_values[VAR_OVERLAY_H] = over->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
303 over->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
304 over->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
305 over->var_values[VAR_X] = NAN;
306 over->var_values[VAR_Y] = NAN;
307 over->var_values[VAR_N] = 0;
308 over->var_values[VAR_T] = NAN;
309 over->var_values[VAR_POS] = NAN;
311 if ((ret = set_expr(&over->x_pexpr, over->x_expr, "x", ctx)) < 0 ||
312 (ret = set_expr(&over->y_pexpr, over->y_expr, "y", ctx)) < 0)
315 over->overlay_is_packed_rgb =
316 ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
317 over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
319 if (over->eval_mode == EVAL_MODE_INIT) {
321 av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
322 over->var_values[VAR_X], over->x,
323 over->var_values[VAR_Y], over->y);
326 av_log(ctx, AV_LOG_VERBOSE,
327 "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
328 ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
329 av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
330 ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
331 av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
335 static int config_output(AVFilterLink *outlink)
337 AVFilterContext *ctx = outlink->src;
339 outlink->w = ctx->inputs[MAIN]->w;
340 outlink->h = ctx->inputs[MAIN]->h;
341 outlink->time_base = ctx->inputs[MAIN]->time_base;
346 // divide by 255 and round to nearest
347 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
348 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
350 // calculate the unpremultiplied alpha, applying the general equation:
351 // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
352 // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
353 // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
354 #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
357 * Blend image in src to destination buffer dst at position (x, y).
359 static void blend_image(AVFilterContext *ctx,
360 AVFrame *dst, AVFrame *src,
363 OverlayContext *over = ctx->priv;
364 int i, imax, j, jmax, k, kmax;
365 const int src_w = src->width;
366 const int src_h = src->height;
367 const int dst_w = dst->width;
368 const int dst_h = dst->height;
370 if (x >= dst_w || x+dst_w < 0 ||
371 y >= dst_h || y+dst_h < 0)
372 return; /* no intersection */
374 if (over->main_is_packed_rgb) {
375 uint8_t alpha; ///< the amount of overlay to blend on to main
376 const int dr = over->main_rgba_map[R];
377 const int dg = over->main_rgba_map[G];
378 const int db = over->main_rgba_map[B];
379 const int da = over->main_rgba_map[A];
380 const int dstep = over->main_pix_step[0];
381 const int sr = over->overlay_rgba_map[R];
382 const int sg = over->overlay_rgba_map[G];
383 const int sb = over->overlay_rgba_map[B];
384 const int sa = over->overlay_rgba_map[A];
385 const int sstep = over->overlay_pix_step[0];
386 const int main_has_alpha = over->main_has_alpha;
387 uint8_t *s, *sp, *d, *dp;
390 sp = src->data[0] + i * src->linesize[0];
391 dp = dst->data[0] + (y+i) * dst->linesize[0];
393 for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
396 d = dp + (x+j) * dstep;
398 for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
401 // if the main channel has an alpha channel, alpha has to be calculated
402 // to create an un-premultiplied (straight) alpha value
403 if (main_has_alpha && alpha != 0 && alpha != 255) {
404 uint8_t alpha_d = d[da];
405 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
417 // main_value = main_value * (1 - alpha) + overlay_value * alpha
418 // since alpha is in the range 0-255, the result must divided by 255
419 d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
420 d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
421 d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
423 if (main_has_alpha) {
431 // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
432 d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
438 dp += dst->linesize[0];
439 sp += src->linesize[0];
442 const int main_has_alpha = over->main_has_alpha;
443 if (main_has_alpha) {
444 uint8_t alpha; ///< the amount of overlay to blend on to main
445 uint8_t *s, *sa, *d, *da;
448 sa = src->data[3] + i * src->linesize[3];
449 da = dst->data[3] + (y+i) * dst->linesize[3];
451 for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
456 for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
458 if (alpha != 0 && alpha != 255) {
459 uint8_t alpha_d = *d;
460 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
469 // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
470 *d += FAST_DIV255((255 - *d) * *s);
475 da += dst->linesize[3];
476 sa += src->linesize[3];
479 for (i = 0; i < 3; i++) {
480 int hsub = i ? over->hsub : 0;
481 int vsub = i ? over->vsub : 0;
482 int src_wp = FFALIGN(src_w, 1<<hsub) >> hsub;
483 int src_hp = FFALIGN(src_h, 1<<vsub) >> vsub;
484 int dst_wp = FFALIGN(dst_w, 1<<hsub) >> hsub;
485 int dst_hp = FFALIGN(dst_h, 1<<vsub) >> vsub;
488 uint8_t *s, *sp, *d, *dp, *a, *ap;
491 sp = src->data[i] + j * src->linesize[i];
492 dp = dst->data[i] + (yp+j) * dst->linesize[i];
493 ap = src->data[3] + (j<<vsub) * src->linesize[3];
495 for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
501 for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
502 int alpha_v, alpha_h, alpha;
504 // average alpha for color components, improve quality
505 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
506 alpha = (a[0] + a[src->linesize[3]] +
507 a[1] + a[src->linesize[3]+1]) >> 2;
508 } else if (hsub || vsub) {
509 alpha_h = hsub && k+1 < src_wp ?
510 (a[0] + a[1]) >> 1 : a[0];
511 alpha_v = vsub && j+1 < src_hp ?
512 (a[0] + a[src->linesize[3]]) >> 1 : a[0];
513 alpha = (alpha_v + alpha_h) >> 1;
516 // if the main channel has an alpha channel, alpha has to be calculated
517 // to create an un-premultiplied (straight) alpha value
518 if (main_has_alpha && alpha != 0 && alpha != 255) {
519 // average alpha for color components, improve quality
521 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
522 alpha_d = (d[0] + d[src->linesize[3]] +
523 d[1] + d[src->linesize[3]+1]) >> 2;
524 } else if (hsub || vsub) {
525 alpha_h = hsub && k+1 < src_wp ?
526 (d[0] + d[1]) >> 1 : d[0];
527 alpha_v = vsub && j+1 < src_hp ?
528 (d[0] + d[src->linesize[3]]) >> 1 : d[0];
529 alpha_d = (alpha_v + alpha_h) >> 1;
532 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
534 *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
539 dp += dst->linesize[i];
540 sp += src->linesize[i];
541 ap += (1 << vsub) * src->linesize[3];
547 static int try_filter_frame(AVFilterContext *ctx, AVFrame *mainpic)
549 OverlayContext *over = ctx->priv;
550 AVFilterLink *inlink = ctx->inputs[0];
551 AVFrame *next_overpic;
554 /* Discard obsolete overlay frames: if there is a next overlay frame with pts
555 * before the main frame, we can drop the current overlay. */
557 next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
558 if (!next_overpic && over->overlay_eof && !over->repeatlast) {
559 av_frame_free(&over->overpicref);
562 if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
563 mainpic->pts , ctx->inputs[MAIN]->time_base) > 0)
565 ff_bufqueue_get(&over->queue_over);
566 av_frame_free(&over->overpicref);
567 over->overpicref = next_overpic;
570 /* If there is no next frame and no EOF and the overlay frame is before
571 * the main frame, we can not know yet if it will be superseded. */
572 if (!over->queue_over.available && !over->overlay_eof &&
573 (!over->overpicref || av_compare_ts(over->overpicref->pts, ctx->inputs[OVERLAY]->time_base,
574 mainpic->pts , ctx->inputs[MAIN]->time_base) < 0))
575 return AVERROR(EAGAIN);
577 /* At this point, we know that the current overlay frame extends to the
578 * time of the main frame. */
579 av_dlog(ctx, "main_pts:%s main_pts_time:%s",
580 av_ts2str(mainpic->pts), av_ts2timestr(mainpic->pts, &ctx->inputs[MAIN]->time_base));
581 if (over->overpicref)
582 av_dlog(ctx, " over_pts:%s over_pts_time:%s",
583 av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &ctx->inputs[OVERLAY]->time_base));
586 if (over->overpicref) {
587 if (over->eval_mode == EVAL_MODE_FRAME) {
588 int64_t pos = av_frame_get_pkt_pos(mainpic);
590 over->var_values[VAR_N] = inlink->frame_count;
591 over->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
592 NAN : mainpic->pts * av_q2d(inlink->time_base);
593 over->var_values[VAR_POS] = pos == -1 ? NAN : pos;
596 av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
597 over->var_values[VAR_N], over->var_values[VAR_T], over->var_values[VAR_POS],
598 over->var_values[VAR_X], over->x,
599 over->var_values[VAR_Y], over->y);
602 blend_image(ctx, mainpic, over->overpicref, over->x, over->y);
605 ret = ff_filter_frame(ctx->outputs[0], mainpic);
606 av_assert1(ret != AVERROR(EAGAIN));
607 over->frame_requested = 0;
611 static int try_filter_next_frame(AVFilterContext *ctx)
613 OverlayContext *over = ctx->priv;
614 AVFrame *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
618 return AVERROR(EAGAIN);
619 if ((ret = try_filter_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
621 ff_bufqueue_get(&over->queue_main);
625 static int flush_frames(AVFilterContext *ctx)
629 while (!(ret = try_filter_next_frame(ctx)));
630 return ret == AVERROR(EAGAIN) ? 0 : ret;
633 static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
635 AVFilterContext *ctx = inlink->dst;
636 OverlayContext *over = ctx->priv;
639 if ((ret = flush_frames(ctx)) < 0)
641 if ((ret = try_filter_frame(ctx, inpicref)) < 0) {
642 if (ret != AVERROR(EAGAIN))
644 ff_bufqueue_add(ctx, &over->queue_main, inpicref);
647 if (!over->overpicref)
654 static int filter_frame_over(AVFilterLink *inlink, AVFrame *inpicref)
656 AVFilterContext *ctx = inlink->dst;
657 OverlayContext *over = ctx->priv;
660 if ((ret = flush_frames(ctx)) < 0)
662 ff_bufqueue_add(ctx, &over->queue_over, inpicref);
663 ret = try_filter_next_frame(ctx);
664 return ret == AVERROR(EAGAIN) ? 0 : ret;
667 #define DEF_FILTER_FRAME(name, mode, enable_value) \
668 static int filter_frame_##name##_##mode(AVFilterLink *inlink, AVFrame *frame) \
670 AVFilterContext *ctx = inlink->dst; \
671 OverlayContext *over = ctx->priv; \
672 over->enable = enable_value; \
673 return filter_frame_##name(inlink, frame); \
676 DEF_FILTER_FRAME(main, enabled, 1);
677 DEF_FILTER_FRAME(main, disabled, 0);
678 DEF_FILTER_FRAME(over, enabled, 1);
679 DEF_FILTER_FRAME(over, disabled, 0);
681 static int request_frame(AVFilterLink *outlink)
683 AVFilterContext *ctx = outlink->src;
684 OverlayContext *over = ctx->priv;
687 if (!try_filter_next_frame(ctx))
689 over->frame_requested = 1;
690 while (over->frame_requested) {
691 /* TODO if we had a frame duration, we could guess more accurately */
692 input = !over->overlay_eof && (over->queue_main.available ||
693 over->queue_over.available < 2) ?
695 ret = ff_request_frame(ctx->inputs[input]);
696 /* EOF on main is reported immediately */
697 if (ret == AVERROR_EOF && input == OVERLAY) {
698 over->overlay_eof = 1;
701 if ((ret = try_filter_next_frame(ctx)) != AVERROR(EAGAIN))
703 ret = 0; /* continue requesting frames on main */
711 #define OFFSET(x) offsetof(OverlayContext, x)
712 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
714 static const AVOption overlay_options[] = {
715 { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
716 { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
717 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
718 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
719 { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
720 { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
721 { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
722 { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
723 { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
724 { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
725 { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
726 { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(repeatlast), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
730 AVFILTER_DEFINE_CLASS(overlay);
732 static const AVFilterPad avfilter_vf_overlay_inputs[] = {
735 .type = AVMEDIA_TYPE_VIDEO,
736 .get_video_buffer = ff_null_get_video_buffer,
737 .config_props = config_input_main,
738 .filter_frame = filter_frame_main_enabled,
739 .passthrough_filter_frame = filter_frame_main_disabled,
744 .type = AVMEDIA_TYPE_VIDEO,
745 .config_props = config_input_overlay,
746 .filter_frame = filter_frame_over_enabled,
747 .passthrough_filter_frame = filter_frame_over_disabled,
752 static const AVFilterPad avfilter_vf_overlay_outputs[] = {
755 .type = AVMEDIA_TYPE_VIDEO,
756 .config_props = config_output,
757 .request_frame = request_frame,
762 AVFilter avfilter_vf_overlay = {
764 .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
769 .priv_size = sizeof(OverlayContext),
770 .priv_class = &overlay_class,
772 .query_formats = query_formats,
773 .process_command = process_command,
775 .inputs = avfilter_vf_overlay_inputs,
776 .outputs = avfilter_vf_overlay_outputs,
777 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE,