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  double enable; ///< tells if blending is enabled
92 
97  uint8_t main_rgba_map[4];
100  uint8_t overlay_rgba_map[4];
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;
104 
106  struct FFBufQueue queue_main;
107  struct FFBufQueue queue_over;
108 
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
114 
115  double var_values[VAR_VARS_NB];
116  char *x_expr, *y_expr;
117  char *enable_expr;
118  AVExpr *x_pexpr, *y_pexpr, *enable_pexpr;
120 
121 static av_cold int init(AVFilterContext *ctx)
122 {
123  OverlayContext *over = ctx->priv;
124 
125  if (over->allow_packed_rgb) {
126  av_log(ctx, AV_LOG_WARNING,
127  "The rgb option is deprecated and is overriding the format option, use format instead\n");
128  over->format = OVERLAY_FORMAT_RGB;
129  }
130  return 0;
131 }
132 
133 static av_cold void uninit(AVFilterContext *ctx)
134 {
135  OverlayContext *over = ctx->priv;
136 
137  av_frame_free(&over->overpicref);
140  av_expr_free(over->x_pexpr); over->x_pexpr = NULL;
141  av_expr_free(over->y_pexpr); over->y_pexpr = NULL;
142  av_expr_free(over->enable_pexpr); over->enable_pexpr = NULL;
143 }
144 
145 static inline int normalize_xy(double d, int chroma_sub)
146 {
147  if (isnan(d))
148  return INT_MAX;
149  return (int)d & ~((1 << chroma_sub) - 1);
150 }
151 
153 
154 static void eval_expr(AVFilterContext *ctx, enum EvalTarget eval_tgt)
155 {
156  OverlayContext *over = ctx->priv;
157 
158  if (eval_tgt == EVAL_XY || eval_tgt == EVAL_ALL) {
159  over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
160  over->var_values[VAR_Y] = av_expr_eval(over->y_pexpr, over->var_values, NULL);
161  over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
162  over->x = normalize_xy(over->var_values[VAR_X], over->hsub);
163  over->y = normalize_xy(over->var_values[VAR_Y], over->vsub);
164  }
165  if (eval_tgt == EVAL_ENABLE || eval_tgt == EVAL_ALL) {
166  over->enable = av_expr_eval(over->enable_pexpr, over->var_values, NULL);
167  }
168 }
169 
170 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
171 {
172  int ret;
173  AVExpr *old = NULL;
174 
175  if (*pexpr)
176  old = *pexpr;
177  ret = av_expr_parse(pexpr, expr, var_names,
178  NULL, NULL, NULL, NULL, 0, log_ctx);
179  if (ret < 0) {
180  av_log(log_ctx, AV_LOG_ERROR,
181  "Error when evaluating the expression '%s' for %s\n",
182  expr, option);
183  *pexpr = old;
184  return ret;
185  }
186 
187  av_expr_free(old);
188  return 0;
189 }
190 
191 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
192  char *res, int res_len, int flags)
193 {
194  OverlayContext *over = ctx->priv;
195  int ret;
196 
197  if (!strcmp(cmd, "x"))
198  ret = set_expr(&over->x_pexpr, args, cmd, ctx);
199  else if (!strcmp(cmd, "y"))
200  ret = set_expr(&over->y_pexpr, args, cmd, ctx);
201  else if (!strcmp(cmd, "enable"))
202  ret = set_expr(&over->enable_pexpr, args, cmd, ctx);
203  else
204  ret = AVERROR(ENOSYS);
205 
206  if (ret < 0)
207  return ret;
208 
209  if (over->eval_mode == EVAL_MODE_INIT) {
210  eval_expr(ctx, EVAL_ALL);
211  av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d enable:%f\n",
212  over->var_values[VAR_X], over->x,
213  over->var_values[VAR_Y], over->y,
214  over->enable);
215  }
216  return ret;
217 }
218 
220 {
221  OverlayContext *over = ctx->priv;
222 
223  /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
224  static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
226  };
227  static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
228  AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
229  };
230 
231  static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
232  AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
233  };
234  static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
235  AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
236  };
237 
238  static const enum AVPixelFormat main_pix_fmts_rgb[] = {
242  AV_PIX_FMT_NONE
243  };
244  static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
247  AV_PIX_FMT_NONE
248  };
249 
250  AVFilterFormats *main_formats;
251  AVFilterFormats *overlay_formats;
252 
253  switch (over->format) {
254  case OVERLAY_FORMAT_YUV420:
255  main_formats = ff_make_format_list(main_pix_fmts_yuv420);
256  overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420);
257  break;
258  case OVERLAY_FORMAT_YUV444:
259  main_formats = ff_make_format_list(main_pix_fmts_yuv444);
260  overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444);
261  break;
262  case OVERLAY_FORMAT_RGB:
263  main_formats = ff_make_format_list(main_pix_fmts_rgb);
264  overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
265  break;
266  default:
267  av_assert0(0);
268  }
269 
270  ff_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
271  ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
272  ff_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
273 
274  return 0;
275 }
276 
277 static const enum AVPixelFormat alpha_pix_fmts[] = {
281 };
282 
283 static int config_input_main(AVFilterLink *inlink)
284 {
285  OverlayContext *over = inlink->dst->priv;
286  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
287 
289 
290  over->hsub = pix_desc->log2_chroma_w;
291  over->vsub = pix_desc->log2_chroma_h;
292 
293  over->main_is_packed_rgb =
294  ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
296  return 0;
297 }
298 
300 {
301  AVFilterContext *ctx = inlink->dst;
302  OverlayContext *over = inlink->dst->priv;
303  int ret;
304  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
305 
307 
308  /* Finish the configuration by evaluating the expressions
309  now when both inputs are configured. */
310  over->var_values[VAR_MAIN_W ] = over->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
311  over->var_values[VAR_MAIN_H ] = over->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
312  over->var_values[VAR_OVERLAY_W] = over->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
313  over->var_values[VAR_OVERLAY_H] = over->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
314  over->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
315  over->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
316  over->var_values[VAR_X] = NAN;
317  over->var_values[VAR_Y] = NAN;
318  over->var_values[VAR_N] = 0;
319  over->var_values[VAR_T] = NAN;
320  over->var_values[VAR_POS] = NAN;
321 
322  if ((ret = set_expr(&over->x_pexpr, over->x_expr, "x", ctx)) < 0 ||
323  (ret = set_expr(&over->y_pexpr, over->y_expr, "y", ctx)) < 0 ||
324  (ret = set_expr(&over->enable_pexpr, over->enable_expr, "enable", ctx)) < 0)
325  return ret;
326 
327  over->overlay_is_packed_rgb =
328  ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
330 
331  if (over->eval_mode == EVAL_MODE_INIT) {
332  eval_expr(ctx, EVAL_ALL);
333  av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d enable:%f\n",
334  over->var_values[VAR_X], over->x,
335  over->var_values[VAR_Y], over->y,
336  over->enable);
337  }
338 
339  av_log(ctx, AV_LOG_VERBOSE,
340  "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
341  ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
343  ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
345  return 0;
346 }
347 
348 static int config_output(AVFilterLink *outlink)
349 {
350  AVFilterContext *ctx = outlink->src;
351 
352  outlink->w = ctx->inputs[MAIN]->w;
353  outlink->h = ctx->inputs[MAIN]->h;
354  outlink->time_base = ctx->inputs[MAIN]->time_base;
355 
356  return 0;
357 }
358 
359 // divide by 255 and round to nearest
360 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
361 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
362 
363 // calculate the unpremultiplied alpha, applying the general equation:
364 // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
365 // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
366 // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
367 #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
368 
369 /**
370  * Blend image in src to destination buffer dst at position (x, y).
371  */
372 static void blend_image(AVFilterContext *ctx,
373  AVFrame *dst, AVFrame *src,
374  int x, int y)
375 {
376  OverlayContext *over = ctx->priv;
377  int i, imax, j, jmax, k, kmax;
378  const int src_w = src->width;
379  const int src_h = src->height;
380  const int dst_w = dst->width;
381  const int dst_h = dst->height;
382 
383  if (x >= dst_w || x+dst_w < 0 ||
384  y >= dst_h || y+dst_h < 0)
385  return; /* no intersection */
386 
387  if (over->main_is_packed_rgb) {
388  uint8_t alpha; ///< the amount of overlay to blend on to main
389  const int dr = over->main_rgba_map[R];
390  const int dg = over->main_rgba_map[G];
391  const int db = over->main_rgba_map[B];
392  const int da = over->main_rgba_map[A];
393  const int dstep = over->main_pix_step[0];
394  const int sr = over->overlay_rgba_map[R];
395  const int sg = over->overlay_rgba_map[G];
396  const int sb = over->overlay_rgba_map[B];
397  const int sa = over->overlay_rgba_map[A];
398  const int sstep = over->overlay_pix_step[0];
399  const int main_has_alpha = over->main_has_alpha;
400  uint8_t *s, *sp, *d, *dp;
401 
402  i = FFMAX(-y, 0);
403  sp = src->data[0] + i * src->linesize[0];
404  dp = dst->data[0] + (y+i) * dst->linesize[0];
405 
406  for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
407  j = FFMAX(-x, 0);
408  s = sp + j * sstep;
409  d = dp + (x+j) * dstep;
410 
411  for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
412  alpha = s[sa];
413 
414  // if the main channel has an alpha channel, alpha has to be calculated
415  // to create an un-premultiplied (straight) alpha value
416  if (main_has_alpha && alpha != 0 && alpha != 255) {
417  uint8_t alpha_d = d[da];
418  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
419  }
420 
421  switch (alpha) {
422  case 0:
423  break;
424  case 255:
425  d[dr] = s[sr];
426  d[dg] = s[sg];
427  d[db] = s[sb];
428  break;
429  default:
430  // main_value = main_value * (1 - alpha) + overlay_value * alpha
431  // since alpha is in the range 0-255, the result must divided by 255
432  d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
433  d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
434  d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
435  }
436  if (main_has_alpha) {
437  switch (alpha) {
438  case 0:
439  break;
440  case 255:
441  d[da] = s[sa];
442  break;
443  default:
444  // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
445  d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
446  }
447  }
448  d += dstep;
449  s += sstep;
450  }
451  dp += dst->linesize[0];
452  sp += src->linesize[0];
453  }
454  } else {
455  const int main_has_alpha = over->main_has_alpha;
456  if (main_has_alpha) {
457  uint8_t alpha; ///< the amount of overlay to blend on to main
458  uint8_t *s, *sa, *d, *da;
459 
460  i = FFMAX(-y, 0);
461  sa = src->data[3] + i * src->linesize[3];
462  da = dst->data[3] + (y+i) * dst->linesize[3];
463 
464  for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
465  j = FFMAX(-x, 0);
466  s = sa + j;
467  d = da + x+j;
468 
469  for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
470  alpha = *s;
471  if (alpha != 0 && alpha != 255) {
472  uint8_t alpha_d = *d;
473  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
474  }
475  switch (alpha) {
476  case 0:
477  break;
478  case 255:
479  *d = *s;
480  break;
481  default:
482  // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
483  *d += FAST_DIV255((255 - *d) * *s);
484  }
485  d += 1;
486  s += 1;
487  }
488  da += dst->linesize[3];
489  sa += src->linesize[3];
490  }
491  }
492  for (i = 0; i < 3; i++) {
493  int hsub = i ? over->hsub : 0;
494  int vsub = i ? over->vsub : 0;
495  int src_wp = FFALIGN(src_w, 1<<hsub) >> hsub;
496  int src_hp = FFALIGN(src_h, 1<<vsub) >> vsub;
497  int dst_wp = FFALIGN(dst_w, 1<<hsub) >> hsub;
498  int dst_hp = FFALIGN(dst_h, 1<<vsub) >> vsub;
499  int yp = y>>vsub;
500  int xp = x>>hsub;
501  uint8_t *s, *sp, *d, *dp, *a, *ap;
502 
503  j = FFMAX(-yp, 0);
504  sp = src->data[i] + j * src->linesize[i];
505  dp = dst->data[i] + (yp+j) * dst->linesize[i];
506  ap = src->data[3] + (j<<vsub) * src->linesize[3];
507 
508  for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
509  k = FFMAX(-xp, 0);
510  d = dp + xp+k;
511  s = sp + k;
512  a = ap + (k<<hsub);
513 
514  for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
515  int alpha_v, alpha_h, alpha;
516 
517  // average alpha for color components, improve quality
518  if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
519  alpha = (a[0] + a[src->linesize[3]] +
520  a[1] + a[src->linesize[3]+1]) >> 2;
521  } else if (hsub || vsub) {
522  alpha_h = hsub && k+1 < src_wp ?
523  (a[0] + a[1]) >> 1 : a[0];
524  alpha_v = vsub && j+1 < src_hp ?
525  (a[0] + a[src->linesize[3]]) >> 1 : a[0];
526  alpha = (alpha_v + alpha_h) >> 1;
527  } else
528  alpha = a[0];
529  // if the main channel has an alpha channel, alpha has to be calculated
530  // to create an un-premultiplied (straight) alpha value
531  if (main_has_alpha && alpha != 0 && alpha != 255) {
532  // average alpha for color components, improve quality
533  uint8_t alpha_d;
534  if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
535  alpha_d = (d[0] + d[src->linesize[3]] +
536  d[1] + d[src->linesize[3]+1]) >> 2;
537  } else if (hsub || vsub) {
538  alpha_h = hsub && k+1 < src_wp ?
539  (d[0] + d[1]) >> 1 : d[0];
540  alpha_v = vsub && j+1 < src_hp ?
541  (d[0] + d[src->linesize[3]]) >> 1 : d[0];
542  alpha_d = (alpha_v + alpha_h) >> 1;
543  } else
544  alpha_d = d[0];
545  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
546  }
547  *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
548  s++;
549  d++;
550  a += 1 << hsub;
551  }
552  dp += dst->linesize[i];
553  sp += src->linesize[i];
554  ap += (1 << vsub) * src->linesize[3];
555  }
556  }
557  }
558 }
559 
560 static int try_filter_frame(AVFilterContext *ctx, AVFrame *mainpic)
561 {
562  OverlayContext *over = ctx->priv;
563  AVFilterLink *inlink = ctx->inputs[0];
564  AVFrame *next_overpic;
565  int ret;
566 
567  /* Discard obsolete overlay frames: if there is a next overlay frame with pts
568  * before the main frame, we can drop the current overlay. */
569  while (1) {
570  next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
571  if (!next_overpic && over->overlay_eof && !over->repeatlast) {
572  av_frame_free(&over->overpicref);
573  break;
574  }
575  if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
576  mainpic->pts , ctx->inputs[MAIN]->time_base) > 0)
577  break;
578  ff_bufqueue_get(&over->queue_over);
579  av_frame_free(&over->overpicref);
580  over->overpicref = next_overpic;
581  }
582 
583  /* If there is no next frame and no EOF and the overlay frame is before
584  * the main frame, we can not know yet if it will be superseded. */
585  if (!over->queue_over.available && !over->overlay_eof &&
586  (!over->overpicref || av_compare_ts(over->overpicref->pts, ctx->inputs[OVERLAY]->time_base,
587  mainpic->pts , ctx->inputs[MAIN]->time_base) < 0))
588  return AVERROR(EAGAIN);
589 
590  /* At this point, we know that the current overlay frame extends to the
591  * time of the main frame. */
592  av_dlog(ctx, "main_pts:%s main_pts_time:%s",
593  av_ts2str(mainpic->pts), av_ts2timestr(mainpic->pts, &ctx->inputs[MAIN]->time_base));
594  if (over->overpicref)
595  av_dlog(ctx, " over_pts:%s over_pts_time:%s",
597  av_dlog(ctx, "\n");
598 
599  if (over->overpicref) {
600  if (over->eval_mode == EVAL_MODE_FRAME) {
601  int64_t pos = av_frame_get_pkt_pos(mainpic);
602 
603  over->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
604  NAN : mainpic->pts * av_q2d(inlink->time_base);
605  over->var_values[VAR_POS] = pos == -1 ? NAN : pos;
606 
607  eval_expr(ctx, EVAL_ALL);
608  av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d enable:%f\n",
609  over->var_values[VAR_N], over->var_values[VAR_T], over->var_values[VAR_POS],
610  over->var_values[VAR_X], over->x,
611  over->var_values[VAR_Y], over->y,
612  over->enable);
613  }
614  if (over->enable)
615  blend_image(ctx, mainpic, over->overpicref, over->x, over->y);
616 
617  over->var_values[VAR_N] += 1.0;
618  }
619  ret = ff_filter_frame(ctx->outputs[0], mainpic);
620  av_assert1(ret != AVERROR(EAGAIN));
621  over->frame_requested = 0;
622  return ret;
623 }
624 
626 {
627  OverlayContext *over = ctx->priv;
628  AVFrame *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
629  int ret;
630 
631  if (!next_mainpic)
632  return AVERROR(EAGAIN);
633  if ((ret = try_filter_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
634  return ret;
635  ff_bufqueue_get(&over->queue_main);
636  return ret;
637 }
638 
640 {
641  int ret;
642 
643  while (!(ret = try_filter_next_frame(ctx)));
644  return ret == AVERROR(EAGAIN) ? 0 : ret;
645 }
646 
647 static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
648 {
649  AVFilterContext *ctx = inlink->dst;
650  OverlayContext *over = ctx->priv;
651  int ret;
652 
653  if ((ret = flush_frames(ctx)) < 0)
654  return ret;
655  if ((ret = try_filter_frame(ctx, inpicref)) < 0) {
656  if (ret != AVERROR(EAGAIN))
657  return ret;
658  ff_bufqueue_add(ctx, &over->queue_main, inpicref);
659  }
660 
661  if (!over->overpicref)
662  return 0;
663  flush_frames(ctx);
664 
665  return 0;
666 }
667 
668 static int filter_frame_over(AVFilterLink *inlink, AVFrame *inpicref)
669 {
670  AVFilterContext *ctx = inlink->dst;
671  OverlayContext *over = ctx->priv;
672  int ret;
673 
674  if ((ret = flush_frames(ctx)) < 0)
675  return ret;
676  ff_bufqueue_add(ctx, &over->queue_over, inpicref);
677  ret = try_filter_next_frame(ctx);
678  return ret == AVERROR(EAGAIN) ? 0 : ret;
679 }
680 
681 static int request_frame(AVFilterLink *outlink)
682 {
683  AVFilterContext *ctx = outlink->src;
684  OverlayContext *over = ctx->priv;
685  int input, ret;
686 
687  if (!try_filter_next_frame(ctx))
688  return 0;
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) ?
694  OVERLAY : MAIN;
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;
699  if (over->shortest)
700  return ret;
701  if ((ret = try_filter_next_frame(ctx)) != AVERROR(EAGAIN))
702  return ret;
703  ret = 0; /* continue requesting frames on main */
704  }
705  if (ret < 0)
706  return ret;
707  }
708  return 0;
709 }
710 
711 #define OFFSET(x) offsetof(OverlayContext, x)
712 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
713 
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  { "enable", "set expression which enables overlay", OFFSET(enable_expr), AV_OPT_TYPE_STRING, {.str = "1"}, .flags = FLAGS },
718  { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
719  { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
720  { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
721  { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
722  { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
723  { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
724  { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
725  { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
726  { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
727  { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(repeatlast), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
728  { NULL }
729 };
730 
731 AVFILTER_DEFINE_CLASS(overlay);
732 
734  {
735  .name = "main",
736  .type = AVMEDIA_TYPE_VIDEO,
737  .get_video_buffer = ff_null_get_video_buffer,
738  .config_props = config_input_main,
739  .filter_frame = filter_frame_main,
740  .needs_writable = 1,
741  },
742  {
743  .name = "overlay",
744  .type = AVMEDIA_TYPE_VIDEO,
745  .config_props = config_input_overlay,
746  .filter_frame = filter_frame_over,
747  },
748  { NULL }
749 };
750 
752  {
753  .name = "default",
754  .type = AVMEDIA_TYPE_VIDEO,
755  .config_props = config_output,
756  .request_frame = request_frame,
757  },
758  { NULL }
759 };
760 
762  .name = "overlay",
763  .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
764 
765  .init = init,
766  .uninit = uninit,
767 
768  .priv_size = sizeof(OverlayContext),
769  .priv_class = &overlay_class,
770 
773 
774  .inputs = avfilter_vf_overlay_inputs,
775  .outputs = avfilter_vf_overlay_outputs,
776 };
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
Definition: bufferqueue.h:98
const char * s
Definition: avisynth_c.h:668
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
static int request_frame(AVFilterLink *outlink)
Definition: vf_overlay.c:681
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
enum OverlayContext::OverlayFormat format
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:35
static const AVFilterPad avfilter_vf_overlay_inputs[]
Definition: vf_overlay.c:733
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition: eval.c:640
struct FFBufQueue queue_main
Definition: vf_overlay.c:106
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
#define FFALIGN(x, a)
Definition: common.h:63
int allow_packed_rgb
Definition: vf_overlay.c:93
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
Structure holding the queue.
Definition: bufferqueue.h:49
set threshold d
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:32
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVFILTER_DEFINE_CLASS(overlay)
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:105
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
timestamp utils, mostly useful for debugging/logging purposes
static const char *const var_names[]
Definition: vf_overlay.c:46
static av_always_inline av_const int isnan(float x)
Definition: libm.h:96
#define G
Definition: vf_overlay.c:80
double var_values[VAR_VARS_NB]
Definition: vf_overlay.c:115
#define NAN
Definition: math.h:7
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
Definition: eval.c:140
#define R
Definition: vf_overlay.c:79
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:98
#define FAST_DIV255(x)
Definition: vf_overlay.c:361
uint8_t overlay_rgba_map[4]
Definition: vf_overlay.c:100
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
AVExpr * y_pexpr
Definition: vf_overlay.c:118
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define sp
Definition: regdef.h:63
char * enable_expr
Definition: vf_overlay.c:117
#define OVERLAY
Definition: vf_overlay.c:77
static int filter_frame_over(AVFilterLink *inlink, AVFrame *inpicref)
Definition: vf_overlay.c:668
int ff_fmt_is_in(int fmt, const int *fmts)
Tell is a format is contained in the provided list terminated by -1.
Definition: formats.c:254
A filter pad used for either input or output.
enum OverlayContext::EvalMode eval_mode
Discrete Time axis x
uint8_t overlay_eof
Definition: vf_overlay.c:95
static int try_filter_next_frame(AVFilterContext *ctx)
Definition: vf_overlay.c:625
static double alpha(void *priv, double x, double y)
Definition: vf_geq.c:86
int width
width and height of the video frame
Definition: frame.h:122
#define UNPREMULTIPLY_ALPHA(x, y)
Definition: vf_overlay.c:367
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
AVExpr * enable_pexpr
Definition: vf_overlay.c:118
int repeatlast
repeat last overlay frame
Definition: vf_overlay.c:113
uint8_t main_has_alpha
Definition: vf_overlay.c:98
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:72
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:99
void * priv
private data for use by the filter
Definition: avfilter.h:545
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame This method is called when a frame is wanted on an output For an input
static int config_input_overlay(AVFilterLink *inlink)
Definition: vf_overlay.c:299
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
#define FFMAX(a, b)
Definition: common.h:56
int shortest
terminate stream when the shortest input terminates
Definition: vf_overlay.c:112
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:96
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:97
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:135
AVFilter avfilter_vf_overlay
Definition: vf_overlay.c:761
#define AV_LOG_VERBOSE
Definition: log.h:157
static const AVFilterPad avfilter_vf_overlay_outputs[]
Definition: vf_overlay.c:751
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define FFMIN(a, b)
Definition: common.h:58
var_name
uint8_t main_rgba_map[4]
Definition: vf_overlay.c:97
ret
Definition: avfilter.c:821
static void blend_image(AVFilterContext *ctx, AVFrame *dst, AVFrame *src, int x, int y)
Blend image in src to destination buffer dst at position (x, y).
Definition: vf_overlay.c:372
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate.The lists are not just lists
#define B
Definition: vf_overlay.c:81
double enable
tells if blending is enabled
Definition: vf_overlay.c:91
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
uint8_t frame_requested
Definition: vf_overlay.c:94
static av_cold int init(AVFilterContext *ctx)
Definition: vf_overlay.c:121
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
Definition: bufferqueue.h:111
int main_pix_step[4]
steps per pixel for each plane of the main output
Definition: vf_overlay.c:109
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:33
static const AVOption overlay_options[]
Definition: vf_overlay.c:714
for k
AVFrame * overpicref
Definition: vf_overlay.c:105
NULL
Definition: eval.c:55
AVS_Value src
Definition: avisynth_c.h:523
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:432
misc drawing utilities
static void eval_expr(AVFilterContext *ctx, enum EvalTarget eval_tgt)
Definition: vf_overlay.c:154
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:302
unsigned short available
number of available buffers
Definition: bufferqueue.h:52
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_overlay.c:133
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:218
static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
Definition: vf_overlay.c:170
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
AVExpr * x_pexpr
Definition: vf_overlay.c:118
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
#define OFFSET(x)
Definition: vf_overlay.c:711
int y
position of overlayed picture
Definition: vf_overlay.c:90
struct FFBufQueue queue_over
Definition: vf_overlay.c:107
Describe the class of an AVClass context structure.
Definition: log.h:50
EvalTarget
Definition: vf_overlay.c:152
Filter definition.
Definition: avfilter.h:436
uint8_t overlay_has_alpha
Definition: vf_overlay.c:101
#define FLAGS
Definition: vf_overlay.c:712
synthesis window for stochastic i
uint8_t overlay_is_packed_rgb
Definition: vf_overlay.c:99
#define A
Definition: vf_overlay.c:82
const char * name
filter name
Definition: avfilter.h:437
static int query_formats(AVFilterContext *ctx)
Definition: vf_overlay.c:219
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
int overlay_pix_step[4]
steps per pixel for each plane of the overlay
Definition: vf_overlay.c:110
option
Definition: dnxhdenc.c:49
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
static int flags
Definition: cpu.c:23
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
static int normalize_xy(double d, int chroma_sub)
Definition: vf_overlay.c:145
static int config_input_main(AVFilterLink *inlink)
Definition: vf_overlay.c:283
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
common internal and external API header
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
int vsub
chroma subsampling values
Definition: vf_overlay.c:111
function y
Definition: D.m:1
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:50
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:691
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
static int config_output(AVFilterLink *outlink)
Definition: vf_overlay.c:348
A list of supported formats for one end of a filter link.
Definition: formats.h:64
int64_t av_frame_get_pkt_pos(const AVFrame *frame)
uint8_t main_is_packed_rgb
Definition: vf_overlay.c:96
An instance of a filter.
Definition: avfilter.h:524
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_overlay.c:191
int height
Definition: frame.h:122
static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
Definition: vf_overlay.c:647
#define MAIN
Definition: vf_overlay.c:76
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
Definition: bufferqueue.h:71
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:319
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:1700
internal API functions
static int flush_frames(AVFilterContext *ctx)
Definition: vf_overlay.c:639
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
static int try_filter_frame(AVFilterContext *ctx, AVFrame *mainpic)
Definition: vf_overlay.c:560
for(j=16;j >0;--j)
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190
static enum AVPixelFormat alpha_pix_fmts[]
Definition: vf_overlay.c:277
simple arithmetic expression evaluator
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.
Definition: bufferqueue.h:87