FFmpeg
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 #include "avfilter.h"
29 #include "formats.h"
30 #include "libavutil/common.h"
31 #include "libavutil/eval.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/timestamp.h"
38 #include "filters.h"
39 #include "drawutils.h"
40 #include "framesync.h"
41 #include "video.h"
42 #include "vf_overlay.h"
43 
44 typedef struct ThreadData {
45  AVFrame *dst, *src;
46 } ThreadData;
47 
48 static const char *const var_names[] = {
49  "main_w", "W", ///< width of the main video
50  "main_h", "H", ///< height of the main video
51  "overlay_w", "w", ///< width of the overlay video
52  "overlay_h", "h", ///< height of the overlay video
53  "hsub",
54  "vsub",
55  "x",
56  "y",
57  "n", ///< number of frame
58  "t", ///< timestamp expressed in seconds
59  NULL
60 };
61 
62 #define MAIN 0
63 #define OVERLAY 1
64 
65 #define R 0
66 #define G 1
67 #define B 2
68 #define A 3
69 
70 #define Y 0
71 #define U 1
72 #define V 2
73 
74 enum EvalMode {
78 };
79 
81 {
82  OverlayContext *s = ctx->priv;
83 
84  ff_framesync_uninit(&s->fs);
85  av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
86  av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
87 }
88 
89 static inline int normalize_xy(double d, int chroma_sub)
90 {
91  if (isnan(d))
92  return INT_MAX;
93  return (int)d & ~((1 << chroma_sub) - 1);
94 }
95 
97 {
98  OverlayContext *s = ctx->priv;
99 
100  s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
101  s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
102  /* It is necessary if x is expressed from y */
103  s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
104  s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
105  s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
106 }
107 
108 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
109 {
110  int ret;
111  AVExpr *old = NULL;
112 
113  if (*pexpr)
114  old = *pexpr;
115  ret = av_expr_parse(pexpr, expr, var_names,
116  NULL, NULL, NULL, NULL, 0, log_ctx);
117  if (ret < 0) {
118  av_log(log_ctx, AV_LOG_ERROR,
119  "Error when evaluating the expression '%s' for %s\n",
120  expr, option);
121  *pexpr = old;
122  return ret;
123  }
124 
125  av_expr_free(old);
126  return 0;
127 }
128 
129 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
130  char *res, int res_len, int flags)
131 {
132  OverlayContext *s = ctx->priv;
133  int ret;
134 
135  if (!strcmp(cmd, "x"))
136  ret = set_expr(&s->x_pexpr, args, cmd, ctx);
137  else if (!strcmp(cmd, "y"))
138  ret = set_expr(&s->y_pexpr, args, cmd, ctx);
139  else
140  ret = AVERROR(ENOSYS);
141 
142  if (ret < 0)
143  return ret;
144 
145  if (s->eval_mode == EVAL_MODE_INIT) {
146  eval_expr(ctx);
147  av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
148  s->var_values[VAR_X], s->x,
149  s->var_values[VAR_Y], s->y);
150  }
151  return ret;
152 }
153 
154 static const enum AVPixelFormat alpha_pix_fmts[] = {
159 };
160 
162  AVFilterFormatsConfig **cfg_in,
163  AVFilterFormatsConfig **cfg_out)
164 {
165  const OverlayContext *s = ctx->priv;
166 
167  /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
168  static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
172  };
173  static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
175  };
176 
177  static const enum AVPixelFormat main_pix_fmts_yuv420p10[] = {
180  };
181  static const enum AVPixelFormat overlay_pix_fmts_yuv420p10[] = {
183  };
184 
185  static const enum AVPixelFormat main_pix_fmts_yuv422[] = {
187  };
188  static const enum AVPixelFormat overlay_pix_fmts_yuv422[] = {
190  };
191 
192  static const enum AVPixelFormat main_pix_fmts_yuv422p10[] = {
194  };
195  static const enum AVPixelFormat overlay_pix_fmts_yuv422p10[] = {
197  };
198 
199  static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
201  };
202  static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
204  };
205 
206  static const enum AVPixelFormat main_pix_fmts_yuv444p10[] = {
208  };
209  static const enum AVPixelFormat overlay_pix_fmts_yuv444p10[] = {
211  };
212 
213  static const enum AVPixelFormat main_pix_fmts_gbrp[] = {
215  };
216  static const enum AVPixelFormat overlay_pix_fmts_gbrp[] = {
218  };
219 
220  static const enum AVPixelFormat main_pix_fmts_rgb[] = {
225  };
226  static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
230  };
231 
232  const enum AVPixelFormat *main_formats, *overlay_formats;
234  int ret;
235 
236  switch (s->format) {
238  main_formats = main_pix_fmts_yuv420;
239  overlay_formats = overlay_pix_fmts_yuv420;
240  break;
242  main_formats = main_pix_fmts_yuv420p10;
243  overlay_formats = overlay_pix_fmts_yuv420p10;
244  break;
246  main_formats = main_pix_fmts_yuv422;
247  overlay_formats = overlay_pix_fmts_yuv422;
248  break;
250  main_formats = main_pix_fmts_yuv422p10;
251  overlay_formats = overlay_pix_fmts_yuv422p10;
252  break;
254  main_formats = main_pix_fmts_yuv444;
255  overlay_formats = overlay_pix_fmts_yuv444;
256  break;
258  main_formats = main_pix_fmts_yuv444p10;
259  overlay_formats = overlay_pix_fmts_yuv444p10;
260  break;
261  case OVERLAY_FORMAT_RGB:
262  main_formats = main_pix_fmts_rgb;
263  overlay_formats = overlay_pix_fmts_rgb;
264  break;
265  case OVERLAY_FORMAT_GBRP:
266  main_formats = main_pix_fmts_gbrp;
267  overlay_formats = overlay_pix_fmts_gbrp;
268  break;
269  case OVERLAY_FORMAT_AUTO:
270  return ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, alpha_pix_fmts);
271  default:
272  av_assert0(0);
273  }
274 
275  formats = ff_make_format_list(main_formats);
276  if ((ret = ff_formats_ref(formats, &cfg_in[MAIN]->formats)) < 0 ||
277  (ret = ff_formats_ref(formats, &cfg_out[MAIN]->formats)) < 0)
278  return ret;
279 
280  return ff_formats_ref(ff_make_format_list(overlay_formats),
281  &cfg_in[OVERLAY]->formats);
282 }
283 
285 {
286  AVFilterContext *ctx = inlink->dst;
287  OverlayContext *s = inlink->dst->priv;
288  int ret;
289  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
290 
291  av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
292 
293  /* Finish the configuration by evaluating the expressions
294  now when both inputs are configured. */
295  s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
296  s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
297  s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
298  s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
299  s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
300  s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
301  s->var_values[VAR_X] = NAN;
302  s->var_values[VAR_Y] = NAN;
303  s->var_values[VAR_N] = 0;
304  s->var_values[VAR_T] = NAN;
305 
306  if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
307  (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
308  return ret;
309 
310  s->overlay_is_packed_rgb =
311  ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
312  s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
313 
314  if (s->eval_mode == EVAL_MODE_INIT) {
315  eval_expr(ctx);
316  av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
317  s->var_values[VAR_X], s->x,
318  s->var_values[VAR_Y], s->y);
319  }
320 
322  "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
323  ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
324  av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
325  ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
326  av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
327  return 0;
328 }
329 
330 static int config_output(AVFilterLink *outlink)
331 {
332  AVFilterContext *ctx = outlink->src;
333  OverlayContext *s = ctx->priv;
334  int ret;
335 
336  if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
337  return ret;
338 
339  outlink->w = ctx->inputs[MAIN]->w;
340  outlink->h = ctx->inputs[MAIN]->h;
341  outlink->time_base = ctx->inputs[MAIN]->time_base;
342 
343  return ff_framesync_configure(&s->fs);
344 }
345 
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)
349 
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)))
355 
356 #define PTR_ADD(TYPE, ptr, byte_addend) ((TYPE*)((uint8_t*)ptr + (byte_addend)))
357 #define CPTR_ADD(TYPE, ptr, byte_addend) ((const TYPE*)((const uint8_t*)ptr + (byte_addend)))
358 
359 /**
360  * Blend image in src to destination buffer dst at position (x, y).
361  */
362 
364  AVFrame *dst, const AVFrame *src,
365  int main_has_alpha, int x, int y,
366  int is_straight, int jobnr, int nb_jobs)
367 {
368  OverlayContext *s = ctx->priv;
369  int i, imax, j, jmax;
370  const int src_w = src->width;
371  const int src_h = src->height;
372  const int dst_w = dst->width;
373  const int dst_h = dst->height;
374  uint8_t alpha; ///< the amount of overlay to blend on to main
375  const int dr = s->main_rgba_map[R];
376  const int dg = s->main_rgba_map[G];
377  const int db = s->main_rgba_map[B];
378  const int da = s->main_rgba_map[A];
379  const int dstep = s->main_pix_step[0];
380  const int sr = s->overlay_rgba_map[R];
381  const int sg = s->overlay_rgba_map[G];
382  const int sb = s->overlay_rgba_map[B];
383  const int sa = s->overlay_rgba_map[A];
384  const int sstep = s->overlay_pix_step[0];
385  int slice_start, slice_end;
386  uint8_t *S, *sp, *d, *dp;
387 
388  i = FFMAX(-y, 0);
389  imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h);
390 
391  slice_start = i + (imax * jobnr) / nb_jobs;
392  slice_end = i + (imax * (jobnr+1)) / nb_jobs;
393 
394  sp = src->data[0] + (slice_start) * src->linesize[0];
395  dp = dst->data[0] + (y + slice_start) * dst->linesize[0];
396 
397  for (i = slice_start; i < slice_end; i++) {
398  j = FFMAX(-x, 0);
399  S = sp + j * sstep;
400  d = dp + (x+j) * dstep;
401 
402  for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
403  alpha = S[sa];
404 
405  // if the main channel has an alpha channel, alpha has to be calculated
406  // to create an un-premultiplied (straight) alpha value
407  if (main_has_alpha && alpha != 0 && alpha != 255) {
408  uint8_t alpha_d = d[da];
409  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
410  }
411 
412  switch (alpha) {
413  case 0:
414  break;
415  case 255:
416  d[dr] = S[sr];
417  d[dg] = S[sg];
418  d[db] = S[sb];
419  break;
420  default:
421  // main_value = main_value * (1 - alpha) + overlay_value * alpha
422  // since alpha is in the range 0-255, the result must divided by 255
423  d[dr] = is_straight ? FAST_DIV255(d[dr] * (255 - alpha) + S[sr] * alpha) :
424  FFMIN(FAST_DIV255(d[dr] * (255 - alpha)) + S[sr], 255);
425  d[dg] = is_straight ? FAST_DIV255(d[dg] * (255 - alpha) + S[sg] * alpha) :
426  FFMIN(FAST_DIV255(d[dg] * (255 - alpha)) + S[sg], 255);
427  d[db] = is_straight ? FAST_DIV255(d[db] * (255 - alpha) + S[sb] * alpha) :
428  FFMIN(FAST_DIV255(d[db] * (255 - alpha)) + S[sb], 255);
429  }
430  if (main_has_alpha) {
431  switch (alpha) {
432  case 0:
433  break;
434  case 255:
435  d[da] = S[sa];
436  break;
437  default:
438  // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
439  d[da] += FAST_DIV255((255 - d[da]) * S[sa]);
440  }
441  }
442  d += dstep;
443  S += sstep;
444  }
445  dp += dst->linesize[0];
446  sp += src->linesize[0];
447  }
448 }
449 
450 #define DEFINE_BLEND_PLANE(depth, T, nbits) \
451 static av_always_inline void blend_plane_##depth##_##nbits##bits(AVFilterContext *ctx, \
452  AVFrame *dst, const AVFrame *src, \
453  int src_w, int src_h, \
454  int dst_w, int dst_h, \
455  int i, int hsub, int vsub, \
456  int x, int y, \
457  int main_has_alpha, \
458  int dst_plane, \
459  int dst_offset, \
460  int dst_step, \
461  int straight, \
462  int yuv, \
463  int jobnr, \
464  int nb_jobs) \
465 { \
466  OverlayContext *octx = ctx->priv; \
467  int src_wp = AV_CEIL_RSHIFT(src_w, hsub); \
468  int src_hp = AV_CEIL_RSHIFT(src_h, vsub); \
469  int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub); \
470  int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub); \
471  int yp = y>>vsub; \
472  int xp = x>>hsub; \
473  const T max = (1 << nbits) - 1; \
474  const T mid = (1 << (nbits - 1)); \
475  \
476  const int jmin = FFMAX(-yp, 0), jmax = FFMIN3(-yp + dst_hp, FFMIN(src_hp, dst_hp), yp + src_hp); \
477  const int kmin = FFMAX(-xp, 0), kmax = FFMIN(-xp + dst_wp, src_wp); \
478  const int slice_start = jmin + (jmax * jobnr) / nb_jobs; \
479  const int slice_end = jmin + (jmax * (jobnr + 1)) / nb_jobs; \
480  \
481  const uint8_t *sp = src->data[i] + (slice_start) * src->linesize[i]; \
482  uint8_t *dp = dst->data[dst_plane] \
483  + (yp + slice_start) * dst->linesize[dst_plane] \
484  + dst_offset; \
485  const uint8_t *ap = src->data[3] + (slice_start << vsub) * src->linesize[3]; \
486  const uint8_t *dap = main_has_alpha ? dst->data[3] + ((yp + slice_start) << vsub) * dst->linesize[3] : NULL; \
487  \
488  for (int j = slice_start; j < slice_end; ++j) { \
489  int k = kmin; \
490  const T *s = (const T *)sp + k; \
491  const T *a = (const T *)ap + (k << hsub); \
492  const T *da = main_has_alpha ? (T *)dap + ((xp + k) << hsub) : NULL; \
493  T *d = (T *)(dp + (xp + k) * dst_step); \
494  \
495  if (nbits == 8 && ((vsub && j+1 < src_hp) || !vsub) && octx->blend_row[i]) { \
496  int c = octx->blend_row[i]((uint8_t*)d, (uint8_t*)da, (uint8_t*)s, \
497  (uint8_t*)a, kmax - k, src->linesize[3]); \
498  \
499  s += c; \
500  d = PTR_ADD(T, d, dst_step * c); \
501  if (main_has_alpha) \
502  da += (1 << hsub) * c; \
503  a += (1 << hsub) * c; \
504  k += c; \
505  } \
506  for (; k < kmax; k++) { \
507  int alpha_v, alpha_h, alpha; \
508  \
509  /* average alpha for color components, improve quality */ \
510  if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
511  const T *next_line = CPTR_ADD(T, a, src->linesize[3]); \
512  alpha = (a[0] + next_line[0] + \
513  a[1] + next_line[1]) >> 2; \
514  } else if (hsub || vsub) { \
515  alpha_h = hsub && k+1 < src_wp ? \
516  (a[0] + a[1]) >> 1 : a[0]; \
517  alpha_v = vsub && j+1 < src_hp ? \
518  (a[0] + *CPTR_ADD(T, a, src->linesize[3])) >> 1 : a[0]; \
519  alpha = (alpha_v + alpha_h) >> 1; \
520  } else \
521  alpha = a[0]; \
522  /* if the main channel has an alpha channel, alpha has to be calculated */ \
523  /* to create an un-premultiplied (straight) alpha value */ \
524  if (main_has_alpha && alpha != 0 && alpha != max) { \
525  /* average alpha for color components, improve quality */ \
526  uint8_t alpha_d; \
527  if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
528  const T *next_line = CPTR_ADD(T, da, dst->linesize[3]); \
529  alpha_d = (da[0] + next_line[0] + \
530  da[1] + next_line[1]) >> 2; \
531  } else if (hsub || vsub) { \
532  alpha_h = hsub && k+1 < src_wp ? \
533  (da[0] + da[1]) >> 1 : da[0]; \
534  alpha_v = vsub && j+1 < src_hp ? \
535  (da[0] + *CPTR_ADD(T, da, dst->linesize[3])) >> 1 : da[0]; \
536  alpha_d = (alpha_v + alpha_h) >> 1; \
537  } else \
538  alpha_d = da[0]; \
539  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
540  } \
541  if (straight) { \
542  if (nbits > 8) \
543  *d = (*d * (max - alpha) + *s * alpha) / max; \
544  else \
545  *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha); \
546  } else { \
547  if (nbits > 8) { \
548  if (i && yuv) \
549  *d = av_clip((*d * (max - alpha) + *s * alpha) / max + *s - mid, -mid, mid) + mid; \
550  else \
551  *d = av_clip_uintp2((*d * (max - alpha) + *s * alpha) / max + *s - (16<<(nbits-8)),\
552  nbits);\
553  } else { \
554  if (i && yuv) \
555  *d = av_clip(FAST_DIV255((*d - mid) * (max - alpha)) + *s - mid, -mid, mid) + mid; \
556  else \
557  *d = av_clip_uint8(FAST_DIV255(*d * (255 - alpha)) + *s - 16); \
558  } \
559  } \
560  s++; \
561  d = PTR_ADD(T, d, dst_step); \
562  if (main_has_alpha) \
563  da += 1 << hsub; \
564  a += 1 << hsub; \
565  } \
566  dp += dst->linesize[dst_plane]; \
567  sp += src->linesize[i]; \
568  ap += (1 << vsub) * src->linesize[3]; \
569  if (main_has_alpha) \
570  dap += (1 << vsub) * dst->linesize[3]; \
571  } \
572 }
573 DEFINE_BLEND_PLANE(8, uint8_t, 8)
574 DEFINE_BLEND_PLANE(16, uint16_t, 10)
575 
576 #define DEFINE_ALPHA_COMPOSITE(depth, T, nbits) \
577 static inline void alpha_composite_##depth##_##nbits##bits(const AVFrame *src, const AVFrame *dst, \
578  int src_w, int src_h, \
579  int dst_w, int dst_h, \
580  int x, int y, \
581  int jobnr, int nb_jobs) \
582 { \
583  T alpha; /* the amount of overlay to blend on to main */ \
584  const T max = (1 << nbits) - 1; \
585  \
586  const int imin = FFMAX(-y, 0), imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h); \
587  const int jmin = FFMAX(-x, 0), jmax = FFMIN(-x + dst_w, src_w); \
588  const int slice_start = imin + ( imax * jobnr) / nb_jobs; \
589  const int slice_end = imin + ((imax * (jobnr + 1)) / nb_jobs); \
590  \
591  const uint8_t *sa = src->data[3] + (slice_start) * src->linesize[3]; \
592  uint8_t *da = dst->data[3] + (y + slice_start) * dst->linesize[3]; \
593  \
594  for (int i = slice_start; i < slice_end; ++i) { \
595  const T *s = (const T *)sa + jmin; \
596  T *d = (T *)da + x + jmin; \
597  \
598  for (int j = jmin; j < jmax; ++j) { \
599  alpha = *s; \
600  if (alpha != 0 && alpha != max) { \
601  uint8_t alpha_d = *d; \
602  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
603  } \
604  if (alpha == max) \
605  *d = *s; \
606  else if (alpha > 0) { \
607  /* apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha */ \
608  if (nbits > 8) \
609  *d += (max - *d) * *s / max; \
610  else \
611  *d += FAST_DIV255((max - *d) * *s); \
612  } \
613  d += 1; \
614  s += 1; \
615  } \
616  da += dst->linesize[3]; \
617  sa += src->linesize[3]; \
618  } \
619 }
620 DEFINE_ALPHA_COMPOSITE(8, uint8_t, 8)
621 DEFINE_ALPHA_COMPOSITE(16, uint16_t, 10)
622 
623 #define DEFINE_BLEND_SLICE_YUV(depth, nbits) \
624 static av_always_inline void blend_slice_yuv_##depth##_##nbits##bits(AVFilterContext *ctx, \
625  AVFrame *dst, const AVFrame *src, \
626  int hsub, int vsub, \
627  int main_has_alpha, \
628  int x, int y, \
629  int is_straight, \
630  int jobnr, int nb_jobs) \
631 { \
632  OverlayContext *s = ctx->priv; \
633  const int src_w = src->width; \
634  const int src_h = src->height; \
635  const int dst_w = dst->width; \
636  const int dst_h = dst->height; \
637  \
638  blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, \
639  x, y, main_has_alpha, s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, \
640  s->main_desc->comp[0].step, is_straight, 1, jobnr, nb_jobs); \
641  blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, \
642  x, y, main_has_alpha, s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, \
643  s->main_desc->comp[1].step, is_straight, 1, jobnr, nb_jobs); \
644  blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, \
645  x, y, main_has_alpha, s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, \
646  s->main_desc->comp[2].step, is_straight, 1, jobnr, nb_jobs); \
647  \
648  if (main_has_alpha) \
649  alpha_composite_##depth##_##nbits##bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, \
650  jobnr, nb_jobs); \
651 }
654 
656  AVFrame *dst, const AVFrame *src,
657  int hsub, int vsub,
658  int main_has_alpha,
659  int x, int y,
660  int is_straight,
661  int jobnr,
662  int nb_jobs)
663 {
664  OverlayContext *s = ctx->priv;
665  const int src_w = src->width;
666  const int src_h = src->height;
667  const int dst_w = dst->width;
668  const int dst_h = dst->height;
669 
670  blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha,
671  s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, is_straight, 0,
672  jobnr, nb_jobs);
673  blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha,
674  s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, is_straight, 0,
675  jobnr, nb_jobs);
676  blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha,
677  s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, is_straight, 0,
678  jobnr, nb_jobs);
679 
680  if (main_has_alpha)
681  alpha_composite_8_8bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, jobnr, nb_jobs);
682 }
683 
684 #define DEFINE_BLEND_SLICE_PLANAR_FMT(format_, blend_slice_fn_suffix_, hsub_, vsub_, main_has_alpha_, direct_) \
685 static int blend_slice_##format_(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
686 { \
687  OverlayContext *s = ctx->priv; \
688  ThreadData *td = arg; \
689  blend_slice_##blend_slice_fn_suffix_(ctx, td->dst, td->src, \
690  hsub_, vsub_, main_has_alpha_, \
691  s->x, s->y, direct_, \
692  jobnr, nb_jobs); \
693  return 0; \
694 }
695 
696 // FMT FN H V A D
697 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv420, yuv_8_8bits, 1, 1, 0, 1)
698 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva420, yuv_8_8bits, 1, 1, 1, 1)
699 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv420p10, yuv_16_10bits, 1, 1, 0, 1)
700 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva420p10, yuv_16_10bits, 1, 1, 1, 1)
701 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv422p10, yuv_16_10bits, 1, 0, 0, 1)
702 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva422p10, yuv_16_10bits, 1, 0, 1, 1)
703 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv422, yuv_8_8bits, 1, 0, 0, 1)
704 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva422, yuv_8_8bits, 1, 0, 1, 1)
705 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv444, yuv_8_8bits, 0, 0, 0, 1)
706 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva444, yuv_8_8bits, 0, 0, 1, 1)
707 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv444p10, yuv_16_10bits, 0, 0, 0, 1)
708 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva444p10, yuv_16_10bits, 0, 0, 1, 1)
709 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrp, planar_rgb, 0, 0, 0, 1)
710 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrap, planar_rgb, 0, 0, 1, 1)
711 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv420_pm, yuv_8_8bits, 1, 1, 0, 0)
712 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva420_pm, yuv_8_8bits, 1, 1, 1, 0)
713 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv422_pm, yuv_8_8bits, 1, 0, 0, 0)
714 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva422_pm, yuv_8_8bits, 1, 0, 1, 0)
715 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv444_pm, yuv_8_8bits, 0, 0, 0, 0)
716 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva444_pm, yuv_8_8bits, 0, 0, 1, 0)
717 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrp_pm, planar_rgb, 0, 0, 0, 0)
718 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrap_pm, planar_rgb, 0, 0, 1, 0)
719 
720 #define DEFINE_BLEND_SLICE_PACKED_FMT(format_, blend_slice_fn_suffix_, main_has_alpha_, direct_) \
721 static int blend_slice_##format_(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
722 { \
723  OverlayContext *s = ctx->priv; \
724  ThreadData *td = arg; \
725  blend_slice_packed_##blend_slice_fn_suffix_(ctx, td->dst, td->src, \
726  main_has_alpha_, \
727  s->x, s->y, direct_, \
728  jobnr, nb_jobs); \
729  return 0; \
730 }
731 
732 // FMT FN A D
735 DEFINE_BLEND_SLICE_PACKED_FMT(rgb_pm, rgb, 0, 0)
736 DEFINE_BLEND_SLICE_PACKED_FMT(rgba_pm, rgb, 1, 0)
737 
739 {
740  OverlayContext *s = inlink->dst->priv;
741  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
742 
743  av_image_fill_max_pixsteps(s->main_pix_step, NULL, pix_desc);
744 
745  s->hsub = pix_desc->log2_chroma_w;
746  s->vsub = pix_desc->log2_chroma_h;
747 
748  s->main_desc = pix_desc;
749 
750  s->main_is_packed_rgb =
751  ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
752  s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
753  switch (s->format) {
755  s->blend_slice = s->main_has_alpha ? blend_slice_yuva420 : blend_slice_yuv420;
756  break;
758  s->blend_slice = s->main_has_alpha ? blend_slice_yuva420p10 : blend_slice_yuv420p10;
759  break;
761  s->blend_slice = s->main_has_alpha ? blend_slice_yuva422 : blend_slice_yuv422;
762  break;
764  s->blend_slice = s->main_has_alpha ? blend_slice_yuva422p10 : blend_slice_yuv422p10;
765  break;
767  s->blend_slice = s->main_has_alpha ? blend_slice_yuva444 : blend_slice_yuv444;
768  break;
770  s->blend_slice = s->main_has_alpha ? blend_slice_yuva444p10 : blend_slice_yuv444p10;
771  break;
772  case OVERLAY_FORMAT_RGB:
773  s->blend_slice = s->main_has_alpha ? blend_slice_rgba : blend_slice_rgb;
774  break;
775  case OVERLAY_FORMAT_GBRP:
776  s->blend_slice = s->main_has_alpha ? blend_slice_gbrap : blend_slice_gbrp;
777  break;
778  case OVERLAY_FORMAT_AUTO:
779  switch (inlink->format) {
780  case AV_PIX_FMT_YUVA420P:
781  s->blend_slice = blend_slice_yuva420;
782  break;
784  s->blend_slice = blend_slice_yuva420p10;
785  break;
786  case AV_PIX_FMT_YUVA422P:
787  s->blend_slice = blend_slice_yuva422;
788  break;
790  s->blend_slice = blend_slice_yuva422p10;
791  break;
792  case AV_PIX_FMT_YUVA444P:
793  s->blend_slice = blend_slice_yuva444;
794  break;
796  s->blend_slice = blend_slice_yuva444p10;
797  break;
798  case AV_PIX_FMT_ARGB:
799  case AV_PIX_FMT_RGBA:
800  case AV_PIX_FMT_BGRA:
801  case AV_PIX_FMT_ABGR:
802  s->blend_slice = blend_slice_rgba;
803  break;
804  case AV_PIX_FMT_GBRAP:
805  s->blend_slice = blend_slice_gbrap;
806  break;
807  default:
808  av_assert0(0);
809  break;
810  }
811  break;
812  }
813 
814  if (!s->alpha_format)
815  goto end;
816 
817  switch (s->format) {
819  s->blend_slice = s->main_has_alpha ? blend_slice_yuva420_pm : blend_slice_yuv420_pm;
820  break;
822  s->blend_slice = s->main_has_alpha ? blend_slice_yuva422_pm : blend_slice_yuv422_pm;
823  break;
825  s->blend_slice = s->main_has_alpha ? blend_slice_yuva444_pm : blend_slice_yuv444_pm;
826  break;
827  case OVERLAY_FORMAT_RGB:
828  s->blend_slice = s->main_has_alpha ? blend_slice_rgba_pm : blend_slice_rgb_pm;
829  break;
830  case OVERLAY_FORMAT_GBRP:
831  s->blend_slice = s->main_has_alpha ? blend_slice_gbrap_pm : blend_slice_gbrp_pm;
832  break;
833  case OVERLAY_FORMAT_AUTO:
834  switch (inlink->format) {
835  case AV_PIX_FMT_YUVA420P:
836  s->blend_slice = blend_slice_yuva420_pm;
837  break;
838  case AV_PIX_FMT_YUVA422P:
839  s->blend_slice = blend_slice_yuva422_pm;
840  break;
841  case AV_PIX_FMT_YUVA444P:
842  s->blend_slice = blend_slice_yuva444_pm;
843  break;
844  case AV_PIX_FMT_ARGB:
845  case AV_PIX_FMT_RGBA:
846  case AV_PIX_FMT_BGRA:
847  case AV_PIX_FMT_ABGR:
848  s->blend_slice = blend_slice_rgba_pm;
849  break;
850  case AV_PIX_FMT_GBRAP:
851  s->blend_slice = blend_slice_gbrap_pm;
852  break;
853  default:
854  av_assert0(0);
855  break;
856  }
857  break;
858  }
859 
860 end:
861 #if ARCH_X86
862  ff_overlay_init_x86(s, s->format, inlink->format,
863  s->alpha_format, s->main_has_alpha);
864 #endif
865 
866  return 0;
867 }
868 
869 static int do_blend(FFFrameSync *fs)
870 {
871  AVFilterContext *ctx = fs->parent;
872  AVFrame *mainpic, *second;
873  OverlayContext *s = ctx->priv;
874  AVFilterLink *inlink = ctx->inputs[0];
876  int ret;
877 
878  ret = ff_framesync_dualinput_get_writable(fs, &mainpic, &second);
879  if (ret < 0)
880  return ret;
881  if (!second)
882  return ff_filter_frame(ctx->outputs[0], mainpic);
883 
884  if (s->eval_mode == EVAL_MODE_FRAME) {
885 
886  s->var_values[VAR_N] = inl->frame_count_out;
887  s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
888  NAN : mainpic->pts * av_q2d(inlink->time_base);
889 
890  s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = second->width;
891  s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = second->height;
892  s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = mainpic->width;
893  s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = mainpic->height;
894 
895  eval_expr(ctx);
896  av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f x:%f xi:%d y:%f yi:%d\n",
897  s->var_values[VAR_N], s->var_values[VAR_T],
898  s->var_values[VAR_X], s->x,
899  s->var_values[VAR_Y], s->y);
900  }
901 
902  if (s->x < mainpic->width && s->x + second->width >= 0 &&
903  s->y < mainpic->height && s->y + second->height >= 0) {
904  ThreadData td;
905 
906  td.dst = mainpic;
907  td.src = second;
908  ff_filter_execute(ctx, s->blend_slice, &td, NULL, FFMIN(FFMAX(1, FFMIN3(s->y + second->height, FFMIN(second->height, mainpic->height), mainpic->height - s->y)),
910  }
911  return ff_filter_frame(ctx->outputs[0], mainpic);
912 }
913 
915 {
916  OverlayContext *s = ctx->priv;
917 
918  s->fs.on_event = do_blend;
919  return 0;
920 }
921 
923 {
924  OverlayContext *s = ctx->priv;
925  return ff_framesync_activate(&s->fs);
926 }
927 
928 #define OFFSET(x) offsetof(OverlayContext, x)
929 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
930 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
931 
932 static const AVOption overlay_options[] = {
933  { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, TFLAGS },
934  { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, TFLAGS },
935  { "eof_action", "Action to take when encountering EOF from secondary input ",
936  OFFSET(fs.opt_eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
937  EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, .unit = "eof_action" },
938  { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, .unit = "eof_action" },
939  { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, .unit = "eof_action" },
940  { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, .unit = "eof_action" },
941  { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, .unit = "eval" },
942  { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
943  { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
944  { "shortest", "force termination when the shortest input terminates", OFFSET(fs.opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
945  { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, .unit = "format" },
946  { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
947  { "yuv420p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420P10}, .flags = FLAGS, .unit = "format" },
948  { "yuv422", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422}, .flags = FLAGS, .unit = "format" },
949  { "yuv422p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422P10}, .flags = FLAGS, .unit = "format" },
950  { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
951  { "yuv444p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444P10}, .flags = FLAGS, .unit = "format" },
952  { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
953  { "gbrp", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_GBRP}, .flags = FLAGS, .unit = "format" },
954  { "auto", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_AUTO}, .flags = FLAGS, .unit = "format" },
955  { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(fs.opt_repeatlast), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
956  { "alpha", "alpha format", OFFSET(alpha_format), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, .unit = "alpha_format" },
957  { "straight", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, .flags = FLAGS, .unit = "alpha_format" },
958  { "premultiplied", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, .flags = FLAGS, .unit = "alpha_format" },
959  { NULL }
960 };
961 
963 
965  {
966  .name = "main",
967  .type = AVMEDIA_TYPE_VIDEO,
968  .config_props = config_input_main,
969  },
970  {
971  .name = "overlay",
972  .type = AVMEDIA_TYPE_VIDEO,
973  .config_props = config_input_overlay,
974  },
975 };
976 
978  {
979  .name = "default",
980  .type = AVMEDIA_TYPE_VIDEO,
981  .config_props = config_output,
982  },
983 };
984 
986  .p.name = "overlay",
987  .p.description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
988  .p.priv_class = &overlay_class,
991  .preinit = overlay_framesync_preinit,
992  .init = init,
993  .uninit = uninit,
994  .priv_size = sizeof(OverlayContext),
995  .activate = activate,
1000 };
flags
const SwsFlags flags[]
Definition: swscale.c:61
formats
formats
Definition: signature.h:47
VAR_MAIN_H
@ VAR_MAIN_H
Definition: vf_drawtext.c:129
ff_framesync_configure
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:137
VAR_Y
@ VAR_Y
Definition: vf_blend.c:54
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
blend_slice_packed_rgb
static av_always_inline void blend_slice_packed_rgb(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int main_has_alpha, int x, int y, int is_straight, int jobnr, int nb_jobs)
Blend image in src to destination buffer dst at position (x, y).
Definition: vf_overlay.c:363
VAR_X
@ VAR_X
Definition: vf_blend.c:54
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
opt.h
VAR_OH
@ VAR_OH
Definition: scale_eval.c:46
set_expr
static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
Definition: vf_overlay.c:108
OVERLAY
#define OVERLAY
Definition: vf_overlay.c:63
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:435
ff_framesync_uninit
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:301
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3341
avfilter_vf_overlay_outputs
static const AVFilterPad avfilter_vf_overlay_outputs[]
Definition: vf_overlay.c:977
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:263
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:421
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:523
AVFrame::width
int width
Definition: frame.h:493
do_blend
static int do_blend(FFFrameSync *fs)
Definition: vf_overlay.c:869
AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:573
AVOption
AVOption.
Definition: opt.h:429
EOF_ACTION_ENDALL
@ EOF_ACTION_ENDALL
Definition: framesync.h:28
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: vf_overlay.c:161
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:528
VAR_HSUB
@ VAR_HSUB
Definition: boxblur.c:41
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:76
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
mathematics.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:215
FFFrameSync
Frame sync structure.
Definition: framesync.h:168
video.h
VAR_MAIN_W
@ VAR_MAIN_W
Definition: vf_drawtext.c:130
AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:574
hsub
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:74
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
av_expr_parse
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:710
var_names
static const char *const var_names[]
Definition: vf_overlay.c:48
S
#define S(s, c, i)
Definition: flacdsp_template.c:46
VAR_T
@ VAR_T
Definition: aeval.c:53
rgb
Definition: rpzaenc.c:60
VAR_VSUB
@ VAR_VSUB
Definition: boxblur.c:42
OVERLAY_FORMAT_RGB
@ OVERLAY_FORMAT_RGB
Definition: vf_overlay.h:48
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition: mpeg12dec.c:1687
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:212
DEFINE_ALPHA_COMPOSITE
#define DEFINE_ALPHA_COMPOSITE(depth, T, nbits)
Definition: vf_overlay.c:576
FAST_DIV255
#define FAST_DIV255(x)
Definition: vf_overlay.c:348
OVERLAY_FORMAT_YUV422P10
@ OVERLAY_FORMAT_YUV422P10
Definition: vf_overlay.h:45
av_expr_free
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:358
R
#define R
Definition: vf_overlay.c:65
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:39
DEFINE_BLEND_SLICE_PACKED_FMT
#define DEFINE_BLEND_SLICE_PACKED_FMT(format_, blend_slice_fn_suffix_, main_has_alpha_, direct_)
Definition: vf_overlay.c:720
ff_overlay_init_x86
void ff_overlay_init_x86(OverlayContext *s, int format, int pix_format, int alpha_format, int main_has_alpha)
Definition: vf_overlay_init.c:35
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:531
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
config_input_main
static int config_input_main(AVFilterLink *inlink)
Definition: vf_overlay.c:738
av_cold
#define av_cold
Definition: attributes.h:90
FFFilter
Definition: filters.h:266
EVAL_MODE_FRAME
@ EVAL_MODE_FRAME
Definition: vf_overlay.c:76
AV_PIX_FMT_YUVJ422P
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:86
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:108
OVERLAY_FORMAT_YUV422
@ OVERLAY_FORMAT_YUV422
Definition: vf_overlay.h:44
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:678
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
filters.h
VAR_MW
@ VAR_MW
Definition: vf_overlay.h:28
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
ctx
AVFormatContext * ctx
Definition: movenc.c:49
av_expr_eval
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:792
UNPREMULTIPLY_ALPHA
#define UNPREMULTIPLY_ALPHA(x, y)
Definition: vf_overlay.c:354
AVExpr
Definition: eval.c:158
AVPixFmtDescriptor::log2_chroma_w
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
ff_fmt_is_in
int ff_fmt_is_in(int fmt, const int *fmts)
Tell if an integer is contained in the provided -1-terminated list of integers.
Definition: formats.c:406
vf_overlay.h
B
#define B
Definition: vf_overlay.c:67
VAR_N
@ VAR_N
Definition: noise.c:48
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
eval_expr
static void eval_expr(AVFilterContext *ctx)
Definition: vf_overlay.c:96
EOF_ACTION_PASS
@ EOF_ACTION_PASS
Definition: framesync.h:29
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:264
NAN
#define NAN
Definition: mathematics.h:115
AV_PIX_FMT_YUVJ444P
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:87
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
option
option
Definition: libkvazaar.c:314
ThreadData::dst
AVFrame * dst
Definition: vf_blend.c:58
config_input_overlay
static int config_input_overlay(AVFilterLink *inlink)
Definition: vf_overlay.c:284
NULL
#define NULL
Definition: coverity.c:32
format
New swscale design to change SwsGraph is what coordinates multiple passes These can include cascaded scaling error diffusion and so on Or we could have separate passes for the vertical and horizontal scaling In between each SwsPass lies a fully allocated image buffer Graph passes may have different levels of e g we can have a single threaded error diffusion pass following a multi threaded scaling pass SwsGraph is internally recreated whenever the image format
Definition: swscale-v2.txt:14
EVAL_MODE_NB
@ EVAL_MODE_NB
Definition: vf_overlay.c:77
fs
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:200
ThreadData::src
const uint8_t * src
Definition: vf_bm3d.c:54
isnan
#define isnan(x)
Definition: libm.h:342
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_overlay.c:80
AV_PIX_FMT_YUVJ420P
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:85
DEFINE_BLEND_PLANE
#define DEFINE_BLEND_PLANE(depth, T, nbits)
Definition: vf_overlay.c:450
OverlayContext
Definition: vf_overlay.h:54
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:529
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:101
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:121
avfilter_vf_overlay_inputs
static const AVFilterPad avfilter_vf_overlay_inputs[]
Definition: vf_overlay.c:964
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:198
TFLAGS
#define TFLAGS
Definition: vf_overlay.c:930
eval.h
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
A
#define A
Definition: vf_overlay.c:68
overlay_options
static const AVOption overlay_options[]
Definition: vf_overlay.c:932
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
ff_framesync_init_dualinput
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
Definition: framesync.c:372
OVERLAY_FORMAT_NB
@ OVERLAY_FORMAT_NB
Definition: vf_overlay.h:51
OVERLAY_FORMAT_YUV420P10
@ OVERLAY_FORMAT_YUV420P10
Definition: vf_overlay.h:43
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
OVERLAY_FORMAT_YUV420
@ OVERLAY_FORMAT_YUV420
Definition: vf_overlay.h:42
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:247
VAR_MH
@ VAR_MH
Definition: vf_overlay.h:29
config_output
static int config_output(AVFilterLink *outlink)
Definition: vf_overlay.c:330
ff_vf_overlay
const FFFilter ff_vf_overlay
Definition: vf_overlay.c:985
OVERLAY_FORMAT_YUV444P10
@ OVERLAY_FORMAT_YUV444P10
Definition: vf_overlay.h:47
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:174
AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:575
OVERLAY_FORMAT_AUTO
@ OVERLAY_FORMAT_AUTO
Definition: vf_overlay.h:50
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:99
DEFINE_BLEND_SLICE_PLANAR_FMT
#define DEFINE_BLEND_SLICE_PLANAR_FMT(format_, blend_slice_fn_suffix_, hsub_, vsub_, main_has_alpha_, direct_)
Definition: vf_overlay.c:684
FLAGS
#define FLAGS
Definition: vf_overlay.c:929
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
blend_slice_planar_rgb
static av_always_inline void blend_slice_planar_rgb(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int hsub, int vsub, int main_has_alpha, int x, int y, int is_straight, int jobnr, int nb_jobs)
Definition: vf_overlay.c:655
FFMIN3
#define FFMIN3(a, b, c)
Definition: macros.h:50
common.h
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:840
ThreadData
Used for passing data between threads.
Definition: dsddec.c:71
EvalMode
EvalMode
Definition: af_volume.h:39
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:240
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_PIX_FMT_NV21
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
Definition: pixfmt.h:97
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:45
VAR_OVERLAY_H
@ VAR_OVERLAY_H
Definition: vf_overlay.h:31
VAR_OW
@ VAR_OW
Definition: scale_eval.c:45
normalize_xy
static int normalize_xy(double d, int chroma_sub)
Definition: vf_overlay.c:89
slice_start
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition: dec.c:842
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_overlay.c:129
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
MAIN
#define MAIN
Definition: vf_overlay.c:62
EOF_ACTION_REPEAT
@ EOF_ACTION_REPEAT
Definition: framesync.h:27
AVFrame::height
int height
Definition: frame.h:493
ff_set_common_formats_from_list2
int ff_set_common_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *fmts)
Definition: formats.c:1016
framesync.h
ff_filter_execute
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: avfilter.c:1686
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
activate
static int activate(AVFilterContext *ctx)
Definition: vf_overlay.c:922
G
#define G
Definition: vf_overlay.c:66
av_image_fill_max_pixsteps
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:35
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVFilterContext
An instance of a filter.
Definition: avfilter.h:269
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:165
OVERLAY_FORMAT_GBRP
@ OVERLAY_FORMAT_GBRP
Definition: vf_overlay.h:49
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:162
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
FFFilter::p
AVFilter p
The public AVFilter.
Definition: filters.h:270
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
OVERLAY_FORMAT_YUV444
@ OVERLAY_FORMAT_YUV444
Definition: vf_overlay.h:46
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FRAMESYNC_DEFINE_CLASS
FRAMESYNC_DEFINE_CLASS(overlay, OverlayContext, fs)
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
EVAL_MODE_INIT
@ EVAL_MODE_INIT
Definition: vf_overlay.c:75
ff_fill_rgba_map
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:80
DEFINE_BLEND_SLICE_YUV
#define DEFINE_BLEND_SLICE_YUV(depth, nbits)
Definition: vf_overlay.c:623
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition: avfilter.h:200
imgutils.h
timestamp.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_framesync_activate
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition: framesync.c:352
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276
ff_framesync_dualinput_get_writable
int ff_framesync_dualinput_get_writable(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
Same as ff_framesync_dualinput_get(), but make sure that f0 is writable.
Definition: framesync.c:410
drawutils.h
alpha_pix_fmts
static enum AVPixelFormat alpha_pix_fmts[]
Definition: vf_overlay.c:154
OFFSET
#define OFFSET(x)
Definition: vf_overlay.c:928
VAR_OVERLAY_W
@ VAR_OVERLAY_W
Definition: vf_overlay.h:30
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
AVPixFmtDescriptor::log2_chroma_h
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
src
#define src
Definition: vp8dsp.c:248
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:173
av_get_pix_fmt_name
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:3261
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_overlay.c:914