FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_overlay.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  * Copyright (c) 2010 Baptiste Coudurier
4  * Copyright (c) 2007 Bobby Bingham
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 /**
24  * @file
25  * overlay one video on top of another
26  */
27 
28 /* #define DEBUG */
29 
30 #include "avfilter.h"
31 #include "formats.h"
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"
41 #include "internal.h"
42 #include "bufferqueue.h"
43 #include "drawutils.h"
44 #include "video.h"
45 
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
51  "hsub",
52  "vsub",
53  "x",
54  "y",
55  "n", ///< number of frame
56  "pos", ///< position in the file
57  "t", ///< timestamp expressed in seconds
58  NULL
59 };
60 
61 enum var_name {
74 };
75 
76 #define MAIN 0
77 #define OVERLAY 1
78 
79 #define R 0
80 #define G 1
81 #define B 2
82 #define A 3
83 
84 #define Y 0
85 #define U 1
86 #define V 2
87 
88 typedef struct {
89  const AVClass *class;
90  int x, y; ///< position of overlayed picture
91 
96  uint8_t main_rgba_map[4];
99  uint8_t overlay_rgba_map[4];
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;
103 
105  struct FFBufQueue queue_main;
106  struct FFBufQueue queue_over;
107 
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
112  int repeatlast; ///< repeat last overlay frame
113 
114  double var_values[VAR_VARS_NB];
115  char *x_expr, *y_expr;
116  AVExpr *x_pexpr, *y_pexpr;
118 
119 static av_cold int init(AVFilterContext *ctx)
120 {
121  OverlayContext *s = ctx->priv;
122 
123  if (s->allow_packed_rgb) {
124  av_log(ctx, AV_LOG_WARNING,
125  "The rgb option is deprecated and is overriding the format option, use format instead\n");
126  s->format = OVERLAY_FORMAT_RGB;
127  }
128  return 0;
129 }
130 
131 static av_cold void uninit(AVFilterContext *ctx)
132 {
133  OverlayContext *s = ctx->priv;
134 
138  av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
139  av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
140 }
141 
142 static inline int normalize_xy(double d, int chroma_sub)
143 {
144  if (isnan(d))
145  return INT_MAX;
146  return (int)d & ~((1 << chroma_sub) - 1);
147 }
148 
149 static void eval_expr(AVFilterContext *ctx)
150 {
151  OverlayContext *s = ctx->priv;
152 
156  s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
157  s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
158 }
159 
160 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
161 {
162  int ret;
163  AVExpr *old = NULL;
164 
165  if (*pexpr)
166  old = *pexpr;
167  ret = av_expr_parse(pexpr, expr, var_names,
168  NULL, NULL, NULL, NULL, 0, log_ctx);
169  if (ret < 0) {
170  av_log(log_ctx, AV_LOG_ERROR,
171  "Error when evaluating the expression '%s' for %s\n",
172  expr, option);
173  *pexpr = old;
174  return ret;
175  }
176 
177  av_expr_free(old);
178  return 0;
179 }
180 
181 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
182  char *res, int res_len, int flags)
183 {
184  OverlayContext *s = ctx->priv;
185  int ret;
186 
187  if (!strcmp(cmd, "x"))
188  ret = set_expr(&s->x_pexpr, args, cmd, ctx);
189  else if (!strcmp(cmd, "y"))
190  ret = set_expr(&s->y_pexpr, args, cmd, ctx);
191  else
192  ret = AVERROR(ENOSYS);
193 
194  if (ret < 0)
195  return ret;
196 
197  if (s->eval_mode == EVAL_MODE_INIT) {
198  eval_expr(ctx);
199  av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
200  s->var_values[VAR_X], s->x,
201  s->var_values[VAR_Y], s->y);
202  }
203  return ret;
204 }
205 
207 {
208  OverlayContext *s = ctx->priv;
209 
210  /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
211  static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
213  };
214  static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
216  };
217 
218  static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
220  };
221  static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
223  };
224 
225  static const enum AVPixelFormat main_pix_fmts_rgb[] = {
230  };
231  static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
235  };
236 
237  AVFilterFormats *main_formats;
238  AVFilterFormats *overlay_formats;
239 
240  switch (s->format) {
241  case OVERLAY_FORMAT_YUV420:
242  main_formats = ff_make_format_list(main_pix_fmts_yuv420);
243  overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420);
244  break;
245  case OVERLAY_FORMAT_YUV444:
246  main_formats = ff_make_format_list(main_pix_fmts_yuv444);
247  overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444);
248  break;
249  case OVERLAY_FORMAT_RGB:
250  main_formats = ff_make_format_list(main_pix_fmts_rgb);
251  overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
252  break;
253  default:
254  av_assert0(0);
255  }
256 
257  ff_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
258  ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
259  ff_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
260 
261  return 0;
262 }
263 
264 static const enum AVPixelFormat alpha_pix_fmts[] = {
268 };
269 
270 static int config_input_main(AVFilterLink *inlink)
271 {
272  OverlayContext *s = inlink->dst->priv;
273  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
274 
276 
277  s->hsub = pix_desc->log2_chroma_w;
278  s->vsub = pix_desc->log2_chroma_h;
279 
280  s->main_is_packed_rgb =
281  ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
283  return 0;
284 }
285 
287 {
288  AVFilterContext *ctx = inlink->dst;
289  OverlayContext *s = inlink->dst->priv;
290  int ret;
291  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
292 
294 
295  /* Finish the configuration by evaluating the expressions
296  now when both inputs are configured. */
297  s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
298  s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
301  s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
302  s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
303  s->var_values[VAR_X] = NAN;
304  s->var_values[VAR_Y] = NAN;
305  s->var_values[VAR_N] = 0;
306  s->var_values[VAR_T] = NAN;
307  s->var_values[VAR_POS] = NAN;
308 
309  if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
310  (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
311  return ret;
312 
314  ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
316 
317  if (s->eval_mode == EVAL_MODE_INIT) {
318  eval_expr(ctx);
319  av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
320  s->var_values[VAR_X], s->x,
321  s->var_values[VAR_Y], s->y);
322  }
323 
324  av_log(ctx, AV_LOG_VERBOSE,
325  "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
326  ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
328  ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
330  return 0;
331 }
332 
333 static int config_output(AVFilterLink *outlink)
334 {
335  AVFilterContext *ctx = outlink->src;
336 
337  outlink->w = ctx->inputs[MAIN]->w;
338  outlink->h = ctx->inputs[MAIN]->h;
339  outlink->time_base = ctx->inputs[MAIN]->time_base;
340 
341  return 0;
342 }
343 
344 // divide by 255 and round to nearest
345 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
346 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
347 
348 // calculate the unpremultiplied alpha, applying the general equation:
349 // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
350 // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
351 // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
352 #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
353 
354 /**
355  * Blend image in src to destination buffer dst at position (x, y).
356  */
357 static void blend_image(AVFilterContext *ctx,
358  AVFrame *dst, AVFrame *src,
359  int x, int y)
360 {
361  OverlayContext *s = ctx->priv;
362  int i, imax, j, jmax, k, kmax;
363  const int src_w = src->width;
364  const int src_h = src->height;
365  const int dst_w = dst->width;
366  const int dst_h = dst->height;
367 
368  if (x >= dst_w || x+dst_w < 0 ||
369  y >= dst_h || y+dst_h < 0)
370  return; /* no intersection */
371 
372  if (s->main_is_packed_rgb) {
373  uint8_t alpha; ///< the amount of overlay to blend on to main
374  const int dr = s->main_rgba_map[R];
375  const int dg = s->main_rgba_map[G];
376  const int db = s->main_rgba_map[B];
377  const int da = s->main_rgba_map[A];
378  const int dstep = s->main_pix_step[0];
379  const int sr = s->overlay_rgba_map[R];
380  const int sg = s->overlay_rgba_map[G];
381  const int sb = s->overlay_rgba_map[B];
382  const int sa = s->overlay_rgba_map[A];
383  const int sstep = s->overlay_pix_step[0];
384  const int main_has_alpha = s->main_has_alpha;
385  uint8_t *s, *sp, *d, *dp;
386 
387  i = FFMAX(-y, 0);
388  sp = src->data[0] + i * src->linesize[0];
389  dp = dst->data[0] + (y+i) * dst->linesize[0];
390 
391  for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
392  j = FFMAX(-x, 0);
393  s = sp + j * sstep;
394  d = dp + (x+j) * dstep;
395 
396  for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
397  alpha = s[sa];
398 
399  // if the main channel has an alpha channel, alpha has to be calculated
400  // to create an un-premultiplied (straight) alpha value
401  if (main_has_alpha && alpha != 0 && alpha != 255) {
402  uint8_t alpha_d = d[da];
403  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
404  }
405 
406  switch (alpha) {
407  case 0:
408  break;
409  case 255:
410  d[dr] = s[sr];
411  d[dg] = s[sg];
412  d[db] = s[sb];
413  break;
414  default:
415  // main_value = main_value * (1 - alpha) + overlay_value * alpha
416  // since alpha is in the range 0-255, the result must divided by 255
417  d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
418  d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
419  d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
420  }
421  if (main_has_alpha) {
422  switch (alpha) {
423  case 0:
424  break;
425  case 255:
426  d[da] = s[sa];
427  break;
428  default:
429  // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
430  d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
431  }
432  }
433  d += dstep;
434  s += sstep;
435  }
436  dp += dst->linesize[0];
437  sp += src->linesize[0];
438  }
439  } else {
440  const int main_has_alpha = s->main_has_alpha;
441  if (main_has_alpha) {
442  uint8_t alpha; ///< the amount of overlay to blend on to main
443  uint8_t *s, *sa, *d, *da;
444 
445  i = FFMAX(-y, 0);
446  sa = src->data[3] + i * src->linesize[3];
447  da = dst->data[3] + (y+i) * dst->linesize[3];
448 
449  for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
450  j = FFMAX(-x, 0);
451  s = sa + j;
452  d = da + x+j;
453 
454  for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
455  alpha = *s;
456  if (alpha != 0 && alpha != 255) {
457  uint8_t alpha_d = *d;
458  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
459  }
460  switch (alpha) {
461  case 0:
462  break;
463  case 255:
464  *d = *s;
465  break;
466  default:
467  // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
468  *d += FAST_DIV255((255 - *d) * *s);
469  }
470  d += 1;
471  s += 1;
472  }
473  da += dst->linesize[3];
474  sa += src->linesize[3];
475  }
476  }
477  for (i = 0; i < 3; i++) {
478  int hsub = i ? s->hsub : 0;
479  int vsub = i ? s->vsub : 0;
480  int src_wp = FF_CEIL_RSHIFT(src_w, hsub);
481  int src_hp = FF_CEIL_RSHIFT(src_h, vsub);
482  int dst_wp = FF_CEIL_RSHIFT(dst_w, hsub);
483  int dst_hp = FF_CEIL_RSHIFT(dst_h, vsub);
484  int yp = y>>vsub;
485  int xp = x>>hsub;
486  uint8_t *s, *sp, *d, *dp, *a, *ap;
487 
488  j = FFMAX(-yp, 0);
489  sp = src->data[i] + j * src->linesize[i];
490  dp = dst->data[i] + (yp+j) * dst->linesize[i];
491  ap = src->data[3] + (j<<vsub) * src->linesize[3];
492 
493  for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
494  k = FFMAX(-xp, 0);
495  d = dp + xp+k;
496  s = sp + k;
497  a = ap + (k<<hsub);
498 
499  for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
500  int alpha_v, alpha_h, alpha;
501 
502  // average alpha for color components, improve quality
503  if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
504  alpha = (a[0] + a[src->linesize[3]] +
505  a[1] + a[src->linesize[3]+1]) >> 2;
506  } else if (hsub || vsub) {
507  alpha_h = hsub && k+1 < src_wp ?
508  (a[0] + a[1]) >> 1 : a[0];
509  alpha_v = vsub && j+1 < src_hp ?
510  (a[0] + a[src->linesize[3]]) >> 1 : a[0];
511  alpha = (alpha_v + alpha_h) >> 1;
512  } else
513  alpha = a[0];
514  // if the main channel has an alpha channel, alpha has to be calculated
515  // to create an un-premultiplied (straight) alpha value
516  if (main_has_alpha && alpha != 0 && alpha != 255) {
517  // average alpha for color components, improve quality
518  uint8_t alpha_d;
519  if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
520  alpha_d = (d[0] + d[src->linesize[3]] +
521  d[1] + d[src->linesize[3]+1]) >> 2;
522  } else if (hsub || vsub) {
523  alpha_h = hsub && k+1 < src_wp ?
524  (d[0] + d[1]) >> 1 : d[0];
525  alpha_v = vsub && j+1 < src_hp ?
526  (d[0] + d[src->linesize[3]]) >> 1 : d[0];
527  alpha_d = (alpha_v + alpha_h) >> 1;
528  } else
529  alpha_d = d[0];
530  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
531  }
532  *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
533  s++;
534  d++;
535  a += 1 << hsub;
536  }
537  dp += dst->linesize[i];
538  sp += src->linesize[i];
539  ap += (1 << vsub) * src->linesize[3];
540  }
541  }
542  }
543 }
544 
545 static int try_filter_frame(AVFilterContext *ctx, AVFrame *mainpic)
546 {
547  OverlayContext *s = ctx->priv;
548  AVFilterLink *inlink = ctx->inputs[0];
549  AVFrame *next_overpic;
550  int ret;
551 
552  /* Discard obsolete overlay frames: if there is a next overlay frame with pts
553  * before the main frame, we can drop the current overlay. */
554  while (1) {
555  next_overpic = ff_bufqueue_peek(&s->queue_over, 0);
556  if (!next_overpic && s->overlay_eof && !s->repeatlast) {
558  break;
559  }
560  if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
561  mainpic->pts , ctx->inputs[MAIN]->time_base) > 0)
562  break;
565  s->overpicref = next_overpic;
566  }
567 
568  /* If there is no next frame and no EOF and the overlay frame is before
569  * the main frame, we can not know yet if it will be superseded. */
570  if (!s->queue_over.available && !s->overlay_eof &&
572  mainpic->pts , ctx->inputs[MAIN]->time_base) < 0))
573  return AVERROR(EAGAIN);
574 
575  /* At this point, we know that the current overlay frame extends to the
576  * time of the main frame. */
577  av_dlog(ctx, "main_pts:%s main_pts_time:%s",
578  av_ts2str(mainpic->pts), av_ts2timestr(mainpic->pts, &ctx->inputs[MAIN]->time_base));
579  if (s->overpicref)
580  av_dlog(ctx, " over_pts:%s over_pts_time:%s",
582  av_dlog(ctx, "\n");
583 
584  if (s->overpicref) {
585  if (s->eval_mode == EVAL_MODE_FRAME) {
586  int64_t pos = av_frame_get_pkt_pos(mainpic);
587 
588  s->var_values[VAR_N] = inlink->frame_count;
589  s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
590  NAN : mainpic->pts * av_q2d(inlink->time_base);
591  s->var_values[VAR_POS] = pos == -1 ? NAN : pos;
592 
593  eval_expr(ctx);
594  av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
596  s->var_values[VAR_X], s->x,
597  s->var_values[VAR_Y], s->y);
598  }
599  if (!ctx->is_disabled)
600  blend_image(ctx, mainpic, s->overpicref, s->x, s->y);
601 
602  }
603  ret = ff_filter_frame(ctx->outputs[0], mainpic);
604  av_assert1(ret != AVERROR(EAGAIN));
605  s->frame_requested = 0;
606  return ret;
607 }
608 
610 {
611  OverlayContext *s = ctx->priv;
612  AVFrame *next_mainpic = ff_bufqueue_peek(&s->queue_main, 0);
613  int ret;
614 
615  if (!next_mainpic)
616  return AVERROR(EAGAIN);
617  if ((ret = try_filter_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
618  return ret;
620  return ret;
621 }
622 
624 {
625  int ret;
626 
627  while (!(ret = try_filter_next_frame(ctx)));
628  return ret == AVERROR(EAGAIN) ? 0 : ret;
629 }
630 
631 static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
632 {
633  AVFilterContext *ctx = inlink->dst;
634  OverlayContext *s = ctx->priv;
635  int ret;
636 
637  if ((ret = flush_frames(ctx)) < 0)
638  return ret;
639  if ((ret = try_filter_frame(ctx, inpicref)) < 0) {
640  if (ret != AVERROR(EAGAIN))
641  return ret;
642  ff_bufqueue_add(ctx, &s->queue_main, inpicref);
643  }
644 
645  if (!s->overpicref)
646  return 0;
647  flush_frames(ctx);
648 
649  return 0;
650 }
651 
652 static int filter_frame_over(AVFilterLink *inlink, AVFrame *inpicref)
653 {
654  AVFilterContext *ctx = inlink->dst;
655  OverlayContext *s = ctx->priv;
656  int ret;
657 
658  if ((ret = flush_frames(ctx)) < 0)
659  return ret;
660  ff_bufqueue_add(ctx, &s->queue_over, inpicref);
661  ret = try_filter_next_frame(ctx);
662  return ret == AVERROR(EAGAIN) ? 0 : ret;
663 }
664 
665 static int request_frame(AVFilterLink *outlink)
666 {
667  AVFilterContext *ctx = outlink->src;
668  OverlayContext *s = ctx->priv;
669  int input, ret;
670 
671  if (!try_filter_next_frame(ctx))
672  return 0;
673  s->frame_requested = 1;
674  while (s->frame_requested) {
675  /* TODO if we had a frame duration, we could guess more accurately */
676  input = !s->overlay_eof && (s->queue_main.available ||
677  s->queue_over.available < 2) ?
678  OVERLAY : MAIN;
679  ret = ff_request_frame(ctx->inputs[input]);
680  /* EOF on main is reported immediately */
681  if (ret == AVERROR_EOF && input == OVERLAY) {
682  s->overlay_eof = 1;
683  if (s->shortest)
684  return ret;
685  if ((ret = try_filter_next_frame(ctx)) != AVERROR(EAGAIN))
686  return ret;
687  ret = 0; /* continue requesting frames on main */
688  }
689  if (ret < 0)
690  return ret;
691  }
692  return 0;
693 }
694 
695 #define OFFSET(x) offsetof(OverlayContext, x)
696 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
697 
698 static const AVOption overlay_options[] = {
699  { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
700  { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
701  { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
702  { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
703  { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
704  { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
705  { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
706  { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
707  { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
708  { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
709  { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
710  { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(repeatlast), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
711  { NULL }
712 };
713 
714 AVFILTER_DEFINE_CLASS(overlay);
715 
717  {
718  .name = "main",
719  .type = AVMEDIA_TYPE_VIDEO,
720  .get_video_buffer = ff_null_get_video_buffer,
721  .config_props = config_input_main,
722  .filter_frame = filter_frame_main,
723  .needs_writable = 1,
724  },
725  {
726  .name = "overlay",
727  .type = AVMEDIA_TYPE_VIDEO,
728  .config_props = config_input_overlay,
729  .filter_frame = filter_frame_over,
730  },
731  { NULL }
732 };
733 
735  {
736  .name = "default",
737  .type = AVMEDIA_TYPE_VIDEO,
738  .config_props = config_output,
739  .request_frame = request_frame,
740  },
741  { NULL }
742 };
743 
745  .name = "overlay",
746  .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
747 
748  .init = init,
749  .uninit = uninit,
750 
751  .priv_size = sizeof(OverlayContext),
752  .priv_class = &overlay_class,
753 
756 
757  .inputs = avfilter_vf_overlay_inputs,
758  .outputs = avfilter_vf_overlay_outputs,
760 };