FFmpeg  4.4.9
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 "filters.h"
30 #include "formats.h"
31 #include "libavutil/common.h"
32 #include "libavutil/eval.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/timestamp.h"
39 #include "internal.h"
40 #include "drawutils.h"
41 #include "framesync.h"
42 #include "video.h"
43 #include "vf_overlay.h"
44 
45 typedef struct ThreadData {
46  AVFrame *dst, *src;
47 } ThreadData;
48 
49 static const char *const var_names[] = {
50  "main_w", "W", ///< width of the main video
51  "main_h", "H", ///< height of the main video
52  "overlay_w", "w", ///< width of the overlay video
53  "overlay_h", "h", ///< height of the overlay video
54  "hsub",
55  "vsub",
56  "x",
57  "y",
58  "n", ///< number of frame
59  "pos", ///< position in the file
60  "t", ///< timestamp expressed in seconds
61  NULL
62 };
63 
64 #define MAIN 0
65 #define OVERLAY 1
66 
67 #define R 0
68 #define G 1
69 #define B 2
70 #define A 3
71 
72 #define Y 0
73 #define U 1
74 #define V 2
75 
76 enum EvalMode {
80 };
81 
83 {
84  OverlayContext *s = ctx->priv;
85 
86  ff_framesync_uninit(&s->fs);
87  av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
88  av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
89 }
90 
91 static inline int normalize_xy(double d, int chroma_sub)
92 {
93  if (isnan(d))
94  return INT_MAX;
95  return (int)d & ~((1 << chroma_sub) - 1);
96 }
97 
99 {
100  OverlayContext *s = ctx->priv;
101 
102  s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
103  s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
104  /* It is necessary if x is expressed from y */
105  s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
106  s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
107  s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
108 }
109 
110 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
111 {
112  int ret;
113  AVExpr *old = NULL;
114 
115  if (*pexpr)
116  old = *pexpr;
117  ret = av_expr_parse(pexpr, expr, var_names,
118  NULL, NULL, NULL, NULL, 0, log_ctx);
119  if (ret < 0) {
120  av_log(log_ctx, AV_LOG_ERROR,
121  "Error when evaluating the expression '%s' for %s\n",
122  expr, option);
123  *pexpr = old;
124  return ret;
125  }
126 
127  av_expr_free(old);
128  return 0;
129 }
130 
131 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
132  char *res, int res_len, int flags)
133 {
134  OverlayContext *s = ctx->priv;
135  int ret;
136 
137  if (!strcmp(cmd, "x"))
138  ret = set_expr(&s->x_pexpr, args, cmd, ctx);
139  else if (!strcmp(cmd, "y"))
140  ret = set_expr(&s->y_pexpr, args, cmd, ctx);
141  else
142  ret = AVERROR(ENOSYS);
143 
144  if (ret < 0)
145  return ret;
146 
147  if (s->eval_mode == EVAL_MODE_INIT) {
148  eval_expr(ctx);
149  av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
150  s->var_values[VAR_X], s->x,
151  s->var_values[VAR_Y], s->y);
152  }
153  return ret;
154 }
155 
156 static const enum AVPixelFormat alpha_pix_fmts[] = {
161 };
162 
164 {
165  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_gbrp[] = {
208  };
209  static const enum AVPixelFormat overlay_pix_fmts_gbrp[] = {
211  };
212 
213  static const enum AVPixelFormat main_pix_fmts_rgb[] = {
218  };
219  static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
223  };
224 
225  const enum AVPixelFormat *main_formats, *overlay_formats;
227  int ret;
228 
229  switch (s->format) {
231  main_formats = main_pix_fmts_yuv420;
232  overlay_formats = overlay_pix_fmts_yuv420;
233  break;
235  main_formats = main_pix_fmts_yuv420p10;
236  overlay_formats = overlay_pix_fmts_yuv420p10;
237  break;
239  main_formats = main_pix_fmts_yuv422;
240  overlay_formats = overlay_pix_fmts_yuv422;
241  break;
243  main_formats = main_pix_fmts_yuv422p10;
244  overlay_formats = overlay_pix_fmts_yuv422p10;
245  break;
247  main_formats = main_pix_fmts_yuv444;
248  overlay_formats = overlay_pix_fmts_yuv444;
249  break;
250  case OVERLAY_FORMAT_RGB:
251  main_formats = main_pix_fmts_rgb;
252  overlay_formats = overlay_pix_fmts_rgb;
253  break;
254  case OVERLAY_FORMAT_GBRP:
255  main_formats = main_pix_fmts_gbrp;
256  overlay_formats = overlay_pix_fmts_gbrp;
257  break;
258  case OVERLAY_FORMAT_AUTO:
260  default:
261  av_assert0(0);
262  }
263 
264  formats = ff_make_format_list(main_formats);
265  if ((ret = ff_formats_ref(formats, &ctx->inputs[MAIN]->outcfg.formats)) < 0 ||
266  (ret = ff_formats_ref(formats, &ctx->outputs[MAIN]->incfg.formats)) < 0)
267  return ret;
268 
269  return ff_formats_ref(ff_make_format_list(overlay_formats),
270  &ctx->inputs[OVERLAY]->outcfg.formats);
271 }
272 
274 {
275  AVFilterContext *ctx = inlink->dst;
276  OverlayContext *s = inlink->dst->priv;
277  int ret;
278  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
279 
280  av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
281 
282  /* Finish the configuration by evaluating the expressions
283  now when both inputs are configured. */
284  s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
285  s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
286  s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
287  s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
288  s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
289  s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
290  s->var_values[VAR_X] = NAN;
291  s->var_values[VAR_Y] = NAN;
292  s->var_values[VAR_N] = 0;
293  s->var_values[VAR_T] = NAN;
294  s->var_values[VAR_POS] = NAN;
295 
296  if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
297  (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
298  return ret;
299 
300  s->overlay_is_packed_rgb =
301  ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
302  s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
303 
304  if (s->eval_mode == EVAL_MODE_INIT) {
305  eval_expr(ctx);
306  av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
307  s->var_values[VAR_X], s->x,
308  s->var_values[VAR_Y], s->y);
309  }
310 
312  "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
313  ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
314  av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
315  ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
316  av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
317  return 0;
318 }
319 
320 static int config_output(AVFilterLink *outlink)
321 {
322  AVFilterContext *ctx = outlink->src;
323  OverlayContext *s = ctx->priv;
324  int ret;
325 
326  if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
327  return ret;
328 
329  outlink->w = ctx->inputs[MAIN]->w;
330  outlink->h = ctx->inputs[MAIN]->h;
331  outlink->time_base = ctx->inputs[MAIN]->time_base;
332 
333  return ff_framesync_configure(&s->fs);
334 }
335 
336 // divide by 255 and round to nearest
337 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
338 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
339 
340 // calculate the unpremultiplied alpha, applying the general equation:
341 // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
342 // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
343 // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
344 #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
345 
346 #define PTR_ADD(TYPE, ptr, byte_addend) ((TYPE*)((uint8_t*)ptr + (byte_addend)))
347 #define CPTR_ADD(TYPE, ptr, byte_addend) ((const TYPE*)((const uint8_t*)ptr + (byte_addend)))
348 
349 /**
350  * Blend image in src to destination buffer dst at position (x, y).
351  */
352 
354  AVFrame *dst, const AVFrame *src,
355  int main_has_alpha, int x, int y,
356  int is_straight, int jobnr, int nb_jobs)
357 {
358  OverlayContext *s = ctx->priv;
359  int i, imax, j, jmax;
360  const int src_w = src->width;
361  const int src_h = src->height;
362  const int dst_w = dst->width;
363  const int dst_h = dst->height;
364  uint8_t alpha; ///< the amount of overlay to blend on to main
365  const int dr = s->main_rgba_map[R];
366  const int dg = s->main_rgba_map[G];
367  const int db = s->main_rgba_map[B];
368  const int da = s->main_rgba_map[A];
369  const int dstep = s->main_pix_step[0];
370  const int sr = s->overlay_rgba_map[R];
371  const int sg = s->overlay_rgba_map[G];
372  const int sb = s->overlay_rgba_map[B];
373  const int sa = s->overlay_rgba_map[A];
374  const int sstep = s->overlay_pix_step[0];
375  int slice_start, slice_end;
376  uint8_t *S, *sp, *d, *dp;
377 
378  i = FFMAX(-y, 0);
379  imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h);
380 
381  slice_start = i + ff_slice_pos(imax, jobnr, nb_jobs);
382  slice_end = i + ff_slice_pos(imax, jobnr + 1, nb_jobs);
383 
384  sp = src->data[0] + (slice_start) * src->linesize[0];
385  dp = dst->data[0] + (y + slice_start) * dst->linesize[0];
386 
387  for (i = slice_start; i < slice_end; i++) {
388  j = FFMAX(-x, 0);
389  S = sp + j * sstep;
390  d = dp + (x+j) * dstep;
391 
392  for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
393  alpha = S[sa];
394 
395  // if the main channel has an alpha channel, alpha has to be calculated
396  // to create an un-premultiplied (straight) alpha value
397  if (main_has_alpha && alpha != 0 && alpha != 255) {
398  uint8_t alpha_d = d[da];
399  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
400  }
401 
402  switch (alpha) {
403  case 0:
404  break;
405  case 255:
406  d[dr] = S[sr];
407  d[dg] = S[sg];
408  d[db] = S[sb];
409  break;
410  default:
411  // main_value = main_value * (1 - alpha) + overlay_value * alpha
412  // since alpha is in the range 0-255, the result must divided by 255
413  d[dr] = is_straight ? FAST_DIV255(d[dr] * (255 - alpha) + S[sr] * alpha) :
414  FFMIN(FAST_DIV255(d[dr] * (255 - alpha)) + S[sr], 255);
415  d[dg] = is_straight ? FAST_DIV255(d[dg] * (255 - alpha) + S[sg] * alpha) :
416  FFMIN(FAST_DIV255(d[dg] * (255 - alpha)) + S[sg], 255);
417  d[db] = is_straight ? FAST_DIV255(d[db] * (255 - alpha) + S[sb] * alpha) :
418  FFMIN(FAST_DIV255(d[db] * (255 - alpha)) + S[sb], 255);
419  }
420  if (main_has_alpha) {
421  switch (alpha) {
422  case 0:
423  break;
424  case 255:
425  d[da] = S[sa];
426  break;
427  default:
428  // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
429  d[da] += FAST_DIV255((255 - d[da]) * S[sa]);
430  }
431  }
432  d += dstep;
433  S += sstep;
434  }
435  dp += dst->linesize[0];
436  sp += src->linesize[0];
437  }
438 }
439 
440 #define DEFINE_BLEND_PLANE(depth, nbits) \
441 static av_always_inline void blend_plane_##depth##_##nbits##bits(AVFilterContext *ctx, \
442  AVFrame *dst, const AVFrame *src, \
443  int src_w, int src_h, \
444  int dst_w, int dst_h, \
445  int i, int hsub, int vsub, \
446  int x, int y, \
447  int main_has_alpha, \
448  int dst_plane, \
449  int dst_offset, \
450  int dst_step, \
451  int straight, \
452  int yuv, \
453  int jobnr, \
454  int nb_jobs) \
455 { \
456  OverlayContext *octx = ctx->priv; \
457  int src_wp = AV_CEIL_RSHIFT(src_w, hsub); \
458  int src_hp = AV_CEIL_RSHIFT(src_h, vsub); \
459  int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub); \
460  int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub); \
461  int yp = y>>vsub; \
462  int xp = x>>hsub; \
463  uint##depth##_t *s, *sp, *d, *dp, *dap, *a, *ap; \
464  int jmax, j, k, kmax; \
465  int slice_start, slice_end; \
466  const uint##depth##_t max = (1 << nbits) - 1; \
467  const uint##depth##_t mid = (1 << (nbits -1)) ; \
468  int bytes = depth / 8; \
469  \
470  dst_step /= bytes; \
471  j = FFMAX(-yp, 0); \
472  jmax = FFMIN3(-yp + dst_hp, FFMIN(src_hp, dst_hp), yp + src_hp); \
473  \
474  slice_start = j + ff_slice_pos(jmax, jobnr, nb_jobs); \
475  slice_end = j + ff_slice_pos(jmax, jobnr + 1, nb_jobs); \
476  \
477  sp = (uint##depth##_t *)(src->data[i] + (slice_start) * src->linesize[i]); \
478  dp = (uint##depth##_t *)(dst->data[dst_plane] \
479  + (yp + slice_start) * dst->linesize[dst_plane] \
480  + dst_offset); \
481  ap = (uint##depth##_t *)(src->data[3] + (slice_start << vsub) * src->linesize[3]); \
482  if (main_has_alpha) \
483  dap = (uint##depth##_t *)(dst->data[3] + ((yp + slice_start) << vsub) * dst->linesize[3]); \
484  \
485  for (j = slice_start; j < slice_end; j++) { \
486  k = FFMAX(-xp, 0); \
487  d = dp + (xp+k) * dst_step; \
488  s = sp + k; \
489  a = ap + (k<<hsub); \
490  uint##depth##_t *da = main_has_alpha ? dap + ((xp+k) << hsub) : NULL; \
491  kmax = FFMIN(-xp + dst_wp, src_wp); \
492  \
493  if (nbits == 8 && ((vsub && j+1 < src_hp) || !vsub) && octx->blend_row[i]) { \
494  int c = octx->blend_row[i]((uint8_t*)d, (uint8_t*)da, (uint8_t*)s, \
495  (uint8_t*)a, kmax - k, src->linesize[3]); \
496  \
497  s += c; \
498  d += dst_step * c; \
499  if (main_has_alpha) \
500  da += (1 << hsub) * c; \
501  a += (1 << hsub) * c; \
502  k += c; \
503  } \
504  for (; k < kmax; k++) { \
505  int alpha_v, alpha_h, alpha; \
506  \
507  /* average alpha for color components, improve quality */ \
508  if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
509  const uint##depth##_t *next_line = CPTR_ADD(uint##depth##_t, a, src->linesize[3]); \
510  alpha = (a[0] + next_line[0] + \
511  a[1] + next_line[1]) >> 2; \
512  } else if (hsub || vsub) { \
513  alpha_h = hsub && k+1 < src_wp ? \
514  (a[0] + a[1]) >> 1 : a[0]; \
515  alpha_v = vsub && j+1 < src_hp ? \
516  (a[0] + *CPTR_ADD(uint##depth##_t, a, src->linesize[3])) >> 1 : a[0]; \
517  alpha = (alpha_v + alpha_h) >> 1; \
518  } else \
519  alpha = a[0]; \
520  /* if the main channel has an alpha channel, alpha has to be calculated */ \
521  /* to create an un-premultiplied (straight) alpha value */ \
522  if (main_has_alpha && alpha != 0 && alpha != max) { \
523  /* average alpha for color components, improve quality */ \
524  uint8_t alpha_d; \
525  if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
526  const uint##depth##_t *next_line = CPTR_ADD(uint##depth##_t, da, dst->linesize[3]); \
527  alpha_d = (da[0] + next_line[0] + \
528  da[1] + next_line[1]) >> 2; \
529  } else if (hsub || vsub) { \
530  alpha_h = hsub && k+1 < src_wp ? \
531  (da[0] + da[1]) >> 1 : da[0]; \
532  alpha_v = vsub && j+1 < src_hp ? \
533  (da[0] + *CPTR_ADD(uint##depth##_t, da, dst->linesize[3])) >> 1 : da[0]; \
534  alpha_d = (alpha_v + alpha_h) >> 1; \
535  } else \
536  alpha_d = da[0]; \
537  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
538  } \
539  if (straight) { \
540  if (nbits > 8) \
541  *d = (*d * (max - alpha) + *s * alpha) / max; \
542  else \
543  *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha); \
544  } else { \
545  if (nbits > 8) { \
546  if (i && yuv) \
547  *d = av_clip((*d * (max - alpha) + *s * alpha) / max + *s - mid, -mid, mid) + mid; \
548  else \
549  *d = FFMIN((*d * (max - alpha) + *s * alpha) / max + *s, max); \
550  } else { \
551  if (i && yuv) \
552  *d = av_clip(FAST_DIV255((*d - mid) * (max - alpha)) + *s - mid, -mid, mid) + mid; \
553  else \
554  *d = FFMIN(FAST_DIV255(*d * (max - alpha)) + *s, max); \
555  } \
556  } \
557  s++; \
558  d += dst_step; \
559  if (main_has_alpha) \
560  da += 1 << hsub; \
561  a += 1 << hsub; \
562  } \
563  dp += dst->linesize[dst_plane] / bytes; \
564  sp += src->linesize[i] / bytes; \
565  ap += (1 << vsub) * src->linesize[3] / bytes; \
566  if (main_has_alpha) \
567  dap += (1 << vsub) * dst->linesize[3] / bytes; \
568  } \
569 }
570 DEFINE_BLEND_PLANE(8, 8)
571 DEFINE_BLEND_PLANE(16, 10)
572 
573 #define DEFINE_ALPHA_COMPOSITE(depth, nbits) \
574 static inline void alpha_composite_##depth##_##nbits##bits(const AVFrame *src, const AVFrame *dst, \
575  int src_w, int src_h, \
576  int dst_w, int dst_h, \
577  int x, int y, \
578  int jobnr, int nb_jobs) \
579 { \
580  uint##depth##_t alpha; /* the amount of overlay to blend on to main */ \
581  uint##depth##_t *s, *sa, *d, *da; \
582  int i, imax, j, jmax; \
583  int slice_start, slice_end; \
584  const uint##depth##_t max = (1 << nbits) - 1; \
585  int bytes = depth / 8; \
586  \
587  imax = FFMIN(-y + dst_h, src_h); \
588  slice_start = ff_slice_pos(imax, jobnr, nb_jobs); \
589  slice_end = (ff_slice_pos(imax, jobnr + 1, nb_jobs)); \
590  \
591  i = FFMAX(-y, 0); \
592  sa = (uint##depth##_t *)(src->data[3] + (i + slice_start) * src->linesize[3]); \
593  da = (uint##depth##_t *)(dst->data[3] + (y + i + slice_start) * dst->linesize[3]); \
594  \
595  for (i = i + slice_start; i < slice_end; i++) { \
596  j = FFMAX(-x, 0); \
597  s = sa + j; \
598  d = da + x+j; \
599  \
600  for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) { \
601  alpha = *s; \
602  if (alpha != 0 && alpha != max) { \
603  uint8_t alpha_d = *d; \
604  alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
605  } \
606  if (alpha == max) \
607  *d = *s; \
608  else if (alpha > 0) { \
609  /* apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha */ \
610  if (nbits > 8) \
611  *d += (max - *d) * *s / max; \
612  else \
613  *d += FAST_DIV255((max - *d) * *s); \
614  } \
615  d += 1; \
616  s += 1; \
617  } \
618  da += dst->linesize[3] / bytes; \
619  sa += src->linesize[3] / bytes; \
620  } \
621 }
624 
625 #define DEFINE_BLEND_SLICE_YUV(depth, nbits) \
626 static av_always_inline void blend_slice_yuv_##depth##_##nbits##bits(AVFilterContext *ctx, \
627  AVFrame *dst, const AVFrame *src, \
628  int hsub, int vsub, \
629  int main_has_alpha, \
630  int x, int y, \
631  int is_straight, \
632  int jobnr, int nb_jobs) \
633 { \
634  OverlayContext *s = ctx->priv; \
635  const int src_w = src->width; \
636  const int src_h = src->height; \
637  const int dst_w = dst->width; \
638  const int dst_h = dst->height; \
639  \
640  blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, \
641  x, y, main_has_alpha, s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, \
642  s->main_desc->comp[0].step, is_straight, 1, jobnr, nb_jobs); \
643  blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, \
644  x, y, main_has_alpha, s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, \
645  s->main_desc->comp[1].step, is_straight, 1, jobnr, nb_jobs); \
646  blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, \
647  x, y, main_has_alpha, s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, \
648  s->main_desc->comp[2].step, is_straight, 1, jobnr, nb_jobs); \
649  \
650  if (main_has_alpha) \
651  alpha_composite_##depth##_##nbits##bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, \
652  jobnr, nb_jobs); \
653 }
656 
658  AVFrame *dst, const AVFrame *src,
659  int hsub, int vsub,
660  int main_has_alpha,
661  int x, int y,
662  int is_straight,
663  int jobnr,
664  int nb_jobs)
665 {
666  OverlayContext *s = ctx->priv;
667  const int src_w = src->width;
668  const int src_h = src->height;
669  const int dst_w = dst->width;
670  const int dst_h = dst->height;
671 
672  blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha,
673  s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, is_straight, 0,
674  jobnr, nb_jobs);
675  blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha,
676  s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, is_straight, 0,
677  jobnr, nb_jobs);
678  blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha,
679  s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, is_straight, 0,
680  jobnr, nb_jobs);
681 
682  if (main_has_alpha)
683  alpha_composite_8_8bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, jobnr, nb_jobs);
684 }
685 
686 static int blend_slice_yuv420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
687 {
688  OverlayContext *s = ctx->priv;
689  ThreadData *td = arg;
690  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 1, jobnr, nb_jobs);
691  return 0;
692 }
693 
694 static int blend_slice_yuva420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
695 {
696  OverlayContext *s = ctx->priv;
697  ThreadData *td = arg;
698  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 1, jobnr, nb_jobs);
699  return 0;
700 }
701 
702 static int blend_slice_yuv420p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
703 {
704  OverlayContext *s = ctx->priv;
705  ThreadData *td = arg;
706  blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 1, jobnr, nb_jobs);
707  return 0;
708 }
709 
710 static int blend_slice_yuva420p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
711 {
712  OverlayContext *s = ctx->priv;
713  ThreadData *td = arg;
714  blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 1, jobnr, nb_jobs);
715  return 0;
716 }
717 
718 static int blend_slice_yuv422p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
719 {
720  OverlayContext *s = ctx->priv;
721  ThreadData *td = arg;
722  blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
723  return 0;
724 }
725 
726 static int blend_slice_yuva422p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
727 {
728  OverlayContext *s = ctx->priv;
729  ThreadData *td = arg;
730  blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
731  return 0;
732 }
733 
734 static int blend_slice_yuv422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
735 {
736  OverlayContext *s = ctx->priv;
737  ThreadData *td = arg;
738  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
739  return 0;
740 }
741 
742 static int blend_slice_yuva422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
743 {
744  OverlayContext *s = ctx->priv;
745  ThreadData *td = arg;
746  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
747  return 0;
748 }
749 
750 static int blend_slice_yuv444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
751 {
752  OverlayContext *s = ctx->priv;
753  ThreadData *td = arg;
754  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
755  return 0;
756 }
757 
758 static int blend_slice_yuva444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
759 {
760  OverlayContext *s = ctx->priv;
761  ThreadData *td = arg;
762  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
763  return 0;
764 }
765 
766 static int blend_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
767 {
768  OverlayContext *s = ctx->priv;
769  ThreadData *td = arg;
770  blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
771  return 0;
772 }
773 
774 static int blend_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
775 {
776  OverlayContext *s = ctx->priv;
777  ThreadData *td = arg;
778  blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
779  return 0;
780 }
781 
782 static int blend_slice_yuv420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
783 {
784  OverlayContext *s = ctx->priv;
785  ThreadData *td = arg;
786  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 0, jobnr, nb_jobs);
787  return 0;
788 }
789 
790 static int blend_slice_yuva420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
791 {
792  OverlayContext *s = ctx->priv;
793  ThreadData *td = arg;
794  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 0, jobnr, nb_jobs);
795  return 0;
796 }
797 
798 static int blend_slice_yuv422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
799 {
800  OverlayContext *s = ctx->priv;
801  ThreadData *td = arg;
802  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
803  return 0;
804 }
805 
806 static int blend_slice_yuva422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
807 {
808  OverlayContext *s = ctx->priv;
809  ThreadData *td = arg;
810  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
811  return 0;
812 }
813 
814 static int blend_slice_yuv444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
815 {
816  OverlayContext *s = ctx->priv;
817  ThreadData *td = arg;
818  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
819  return 0;
820 }
821 
822 static int blend_slice_yuva444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
823 {
824  OverlayContext *s = ctx->priv;
825  ThreadData *td = arg;
826  blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
827  return 0;
828 }
829 
830 static int blend_slice_gbrp_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
831 {
832  OverlayContext *s = ctx->priv;
833  ThreadData *td = arg;
834  blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
835  return 0;
836 }
837 
838 static int blend_slice_gbrap_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
839 {
840  OverlayContext *s = ctx->priv;
841  ThreadData *td = arg;
842  blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
843  return 0;
844 }
845 
846 static int blend_slice_rgb(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
847 {
848  OverlayContext *s = ctx->priv;
849  ThreadData *td = arg;
850  blend_slice_packed_rgb(ctx, td->dst, td->src, 0, s->x, s->y, 1, jobnr, nb_jobs);
851  return 0;
852 }
853 
854 static int blend_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
855 {
856  OverlayContext *s = ctx->priv;
857  ThreadData *td = arg;
858  blend_slice_packed_rgb(ctx, td->dst, td->src, 1, s->x, s->y, 1, jobnr, nb_jobs);
859  return 0;
860 }
861 
862 static int blend_slice_rgb_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
863 {
864  OverlayContext *s = ctx->priv;
865  ThreadData *td = arg;
866  blend_slice_packed_rgb(ctx, td->dst, td->src, 0, s->x, s->y, 0, jobnr, nb_jobs);
867  return 0;
868 }
869 
870 static int blend_slice_rgba_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
871 {
872  OverlayContext *s = ctx->priv;
873  ThreadData *td = arg;
874  blend_slice_packed_rgb(ctx, td->dst, td->src, 1, s->x, s->y, 0, jobnr, nb_jobs);
875  return 0;
876 }
877 
878 static int config_input_main(AVFilterLink *inlink)
879 {
880  OverlayContext *s = inlink->dst->priv;
881  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
882 
883  av_image_fill_max_pixsteps(s->main_pix_step, NULL, pix_desc);
884 
885  s->hsub = pix_desc->log2_chroma_w;
886  s->vsub = pix_desc->log2_chroma_h;
887 
888  s->main_desc = pix_desc;
889 
890  s->main_is_packed_rgb =
891  ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
892  s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
893  switch (s->format) {
895  s->blend_slice = s->main_has_alpha ? blend_slice_yuva420 : blend_slice_yuv420;
896  break;
898  s->blend_slice = s->main_has_alpha ? blend_slice_yuva420p10 : blend_slice_yuv420p10;
899  break;
901  s->blend_slice = s->main_has_alpha ? blend_slice_yuva422 : blend_slice_yuv422;
902  break;
904  s->blend_slice = s->main_has_alpha ? blend_slice_yuva422p10 : blend_slice_yuv422p10;
905  break;
907  s->blend_slice = s->main_has_alpha ? blend_slice_yuva444 : blend_slice_yuv444;
908  break;
909  case OVERLAY_FORMAT_RGB:
910  s->blend_slice = s->main_has_alpha ? blend_slice_rgba : blend_slice_rgb;
911  break;
912  case OVERLAY_FORMAT_GBRP:
913  s->blend_slice = s->main_has_alpha ? blend_slice_gbrap : blend_slice_gbrp;
914  break;
915  case OVERLAY_FORMAT_AUTO:
916  switch (inlink->format) {
917  case AV_PIX_FMT_YUVA420P:
918  s->blend_slice = blend_slice_yuva420;
919  break;
921  s->blend_slice = blend_slice_yuva420p10;
922  break;
923  case AV_PIX_FMT_YUVA422P:
924  s->blend_slice = blend_slice_yuva422;
925  break;
927  s->blend_slice = blend_slice_yuva422p10;
928  break;
929  case AV_PIX_FMT_YUVA444P:
930  s->blend_slice = blend_slice_yuva444;
931  break;
932  case AV_PIX_FMT_ARGB:
933  case AV_PIX_FMT_RGBA:
934  case AV_PIX_FMT_BGRA:
935  case AV_PIX_FMT_ABGR:
936  s->blend_slice = blend_slice_rgba;
937  break;
938  case AV_PIX_FMT_GBRAP:
939  s->blend_slice = blend_slice_gbrap;
940  break;
941  default:
942  av_assert0(0);
943  break;
944  }
945  break;
946  }
947 
948  if (!s->alpha_format)
949  goto end;
950 
951  switch (s->format) {
953  s->blend_slice = s->main_has_alpha ? blend_slice_yuva420_pm : blend_slice_yuv420_pm;
954  break;
956  s->blend_slice = s->main_has_alpha ? blend_slice_yuva422_pm : blend_slice_yuv422_pm;
957  break;
959  s->blend_slice = s->main_has_alpha ? blend_slice_yuva444_pm : blend_slice_yuv444_pm;
960  break;
961  case OVERLAY_FORMAT_RGB:
962  s->blend_slice = s->main_has_alpha ? blend_slice_rgba_pm : blend_slice_rgb_pm;
963  break;
964  case OVERLAY_FORMAT_GBRP:
965  s->blend_slice = s->main_has_alpha ? blend_slice_gbrap_pm : blend_slice_gbrp_pm;
966  break;
967  case OVERLAY_FORMAT_AUTO:
968  switch (inlink->format) {
969  case AV_PIX_FMT_YUVA420P:
970  s->blend_slice = blend_slice_yuva420_pm;
971  break;
972  case AV_PIX_FMT_YUVA422P:
973  s->blend_slice = blend_slice_yuva422_pm;
974  break;
975  case AV_PIX_FMT_YUVA444P:
976  s->blend_slice = blend_slice_yuva444_pm;
977  break;
978  case AV_PIX_FMT_ARGB:
979  case AV_PIX_FMT_RGBA:
980  case AV_PIX_FMT_BGRA:
981  case AV_PIX_FMT_ABGR:
982  s->blend_slice = blend_slice_rgba_pm;
983  break;
984  case AV_PIX_FMT_GBRAP:
985  s->blend_slice = blend_slice_gbrap_pm;
986  break;
987  default:
988  av_assert0(0);
989  break;
990  }
991  break;
992  }
993 
994 end:
995  if (ARCH_X86)
996  ff_overlay_init_x86(s, s->format, inlink->format,
997  s->alpha_format, s->main_has_alpha);
998 
999  return 0;
1000 }
1001 
1003 {
1004  AVFilterContext *ctx = fs->parent;
1005  AVFrame *mainpic, *second;
1006  OverlayContext *s = ctx->priv;
1007  AVFilterLink *inlink = ctx->inputs[0];
1008  int ret;
1009 
1010  ret = ff_framesync_dualinput_get_writable(fs, &mainpic, &second);
1011  if (ret < 0)
1012  return ret;
1013  if (!second)
1014  return ff_filter_frame(ctx->outputs[0], mainpic);
1015 
1016  if (s->eval_mode == EVAL_MODE_FRAME) {
1017  int64_t pos = mainpic->pkt_pos;
1018 
1019  s->var_values[VAR_N] = inlink->frame_count_out;
1020  s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
1021  NAN : mainpic->pts * av_q2d(inlink->time_base);
1022  s->var_values[VAR_POS] = pos == -1 ? NAN : pos;
1023 
1024  s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = second->width;
1025  s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = second->height;
1026  s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = mainpic->width;
1027  s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = mainpic->height;
1028 
1029  eval_expr(ctx);
1030  av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
1031  s->var_values[VAR_N], s->var_values[VAR_T], s->var_values[VAR_POS],
1032  s->var_values[VAR_X], s->x,
1033  s->var_values[VAR_Y], s->y);
1034  }
1035 
1036  if (s->x < mainpic->width && s->x + second->width >= 0 &&
1037  s->y < mainpic->height && s->y + second->height >= 0) {
1038  ThreadData td;
1039 
1040  td.dst = mainpic;
1041  td.src = second;
1042  ctx->internal->execute(ctx, s->blend_slice, &td, NULL, FFMIN(FFMAX(1, FFMIN3(s->y + second->height, FFMIN(second->height, mainpic->height), mainpic->height - s->y)),
1044  }
1045  return ff_filter_frame(ctx->outputs[0], mainpic);
1046 }
1047 
1049 {
1050  OverlayContext *s = ctx->priv;
1051 
1052  s->fs.on_event = do_blend;
1053  return 0;
1054 }
1055 
1057 {
1058  OverlayContext *s = ctx->priv;
1059  return ff_framesync_activate(&s->fs);
1060 }
1061 
1062 #define OFFSET(x) offsetof(OverlayContext, x)
1063 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1064 
1065 static const AVOption overlay_options[] = {
1066  { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS },
1067  { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS },
1068  { "eof_action", "Action to take when encountering EOF from secondary input ",
1069  OFFSET(fs.opt_eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
1070  EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, "eof_action" },
1071  { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, "eof_action" },
1072  { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, "eof_action" },
1073  { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, "eof_action" },
1074  { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
1075  { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
1076  { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
1077  { "shortest", "force termination when the shortest input terminates", OFFSET(fs.opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1078  { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
1079  { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
1080  { "yuv420p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420P10}, .flags = FLAGS, .unit = "format" },
1081  { "yuv422", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422}, .flags = FLAGS, .unit = "format" },
1082  { "yuv422p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422P10}, .flags = FLAGS, .unit = "format" },
1083  { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
1084  { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
1085  { "gbrp", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_GBRP}, .flags = FLAGS, .unit = "format" },
1086  { "auto", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_AUTO}, .flags = FLAGS, .unit = "format" },
1087  { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(fs.opt_repeatlast), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
1088  { "alpha", "alpha format", OFFSET(alpha_format), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "alpha_format" },
1089  { "straight", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, .flags = FLAGS, .unit = "alpha_format" },
1090  { "premultiplied", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, .flags = FLAGS, .unit = "alpha_format" },
1091  { NULL }
1092 };
1093 
1095 
1097  {
1098  .name = "main",
1099  .type = AVMEDIA_TYPE_VIDEO,
1100  .config_props = config_input_main,
1101  },
1102  {
1103  .name = "overlay",
1104  .type = AVMEDIA_TYPE_VIDEO,
1105  .config_props = config_input_overlay,
1106  },
1107  { NULL }
1108 };
1109 
1111  {
1112  .name = "default",
1113  .type = AVMEDIA_TYPE_VIDEO,
1114  .config_props = config_output,
1115  },
1116  { NULL }
1117 };
1118 
1120  .name = "overlay",
1121  .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
1122  .preinit = overlay_framesync_preinit,
1123  .init = init,
1124  .uninit = uninit,
1125  .priv_size = sizeof(OverlayContext),
1126  .priv_class = &overlay_class,
1128  .activate = activate,
1134  .flags_internal = FF_FILTER_FLAG_DYNAMIC_FRAME_SIZE,
1135 };
@ VAR_T
Definition: aeval.c:51
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static const char *const format[]
Definition: af_aiir.c:457
EvalMode
Definition: af_volume.h:39
#define av_always_inline
Definition: attributes.h:45
#define av_cold
Definition: attributes.h:88
uint8_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:805
Main libavfilter public API header.
@ VAR_VSUB
Definition: boxblur.c:41
@ VAR_HSUB
Definition: boxblur.c:40
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
common internal and external API header
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
#define FFMIN3(a, b, c)
Definition: common.h:106
#define ARCH_X86
Definition: config.h:39
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:35
misc drawing utilities
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:339
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:781
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:700
simple arithmetic expression evaluator
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition: filters.h:282
#define S(s, c, i)
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition: formats.c:466
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:257
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:124
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition: framesync.c:341
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
Definition: framesync.c:361
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:290
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:399
@ EOF_ACTION_PASS
Definition: framesync.h:29
@ EOF_ACTION_ENDALL
Definition: framesync.h:28
@ EOF_ACTION_REPEAT
Definition: framesync.h:27
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
#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:134
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
static const int16_t alpha[]
Definition: ilbcdata.h:55
misc image utilities
int i
Definition: input.c:407
const char * arg
Definition: jacosubdec.c:67
#define FF_FILTER_FLAG_DYNAMIC_FRAME_SIZE
The filter handles input frames whose size differs from the one its input link was configured with.
Definition: internal.h:345
@ VAR_X
Definition: vf_blend.c:53
@ VAR_Y
Definition: vf_blend.c:53
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
option
Definition: libkvazaar.c:325
#define isnan(x)
Definition: libm.h:340
#define NAN
Definition: mathematics.h:64
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2035
AVOptions.
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:2489
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:436
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:437
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ 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:89
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
Definition: pixfmt.h:90
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
@ 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:79
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
@ 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:80
@ 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:78
#define sp
Definition: regdef.h:63
#define td
Definition: regdef.h:70
@ VAR_OH
Definition: scale_eval.c:47
@ VAR_OW
Definition: scale_eval.c:46
@ VAR_POS
Definition: setts_bsf.c:53
@ VAR_N
Definition: setts_bsf.c:51
formats
Definition: signature.h:48
unsigned int pos
Definition: spdifenc.c:412
Definition: eval.c:159
An instance of a filter.
Definition: avfilter.h:341
void * priv
private data for use by the filter
Definition: avfilter.h:356
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int64_t pkt_pos
reordered pos from the last AVPacket that has been input into the decoder
Definition: frame.h:589
int height
Definition: frame.h:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
Frame sync structure.
Definition: framesync.h:146
Used for passing data between threads.
Definition: dsddec.c:67
const uint8_t * src
Definition: vf_bm3d.c:56
AVFrame * dst
Definition: vf_blend.c:57
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
AVFormatContext * ctx
Definition: movenc.c:48
timestamp utils, mostly useful for debugging/logging purposes
@ VAR_MAIN_H
Definition: vf_drawtext.c:117
@ VAR_MAIN_W
Definition: vf_drawtext.c:118
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:76
static const AVOption overlay_options[]
Definition: vf_overlay.c:1065
#define B
Definition: vf_overlay.c:69
static int blend_slice_gbrap_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:838
static int blend_slice_yuva422p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:726
#define DEFINE_BLEND_SLICE_YUV(depth, nbits)
Definition: vf_overlay.c:625
static int blend_slice_yuv444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:814
static int config_input_overlay(AVFilterLink *inlink)
Definition: vf_overlay.c:273
#define MAIN
Definition: vf_overlay.c:64
static int blend_slice_rgba_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:870
#define FAST_DIV255(x)
Definition: vf_overlay.c:338
static int blend_slice_yuv420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:686
static int blend_slice_yuva444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:758
static int blend_slice_yuva444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:822
static int query_formats(AVFilterContext *ctx)
Definition: vf_overlay.c:163
#define R
Definition: vf_overlay.c:67
#define UNPREMULTIPLY_ALPHA(x, y)
Definition: vf_overlay.c:344
static int blend_slice_yuv420p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:702
static int blend_slice_gbrp_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:830
AVFilter ff_vf_overlay
Definition: vf_overlay.c:1119
static int do_blend(FFFrameSync *fs)
Definition: vf_overlay.c:1002
#define FLAGS
Definition: vf_overlay.c:1063
static int blend_slice_yuv422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:798
static int blend_slice_rgb_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:862
static int blend_slice_rgb(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:846
static int blend_slice_yuv422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:734
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:353
#define A
Definition: vf_overlay.c:70
static const AVFilterPad avfilter_vf_overlay_inputs[]
Definition: vf_overlay.c:1096
static int blend_slice_yuva422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:742
static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
Definition: vf_overlay.c:110
@ EVAL_MODE_NB
Definition: vf_overlay.c:79
@ EVAL_MODE_FRAME
Definition: vf_overlay.c:78
@ EVAL_MODE_INIT
Definition: vf_overlay.c:77
static enum AVPixelFormat alpha_pix_fmts[]
Definition: vf_overlay.c:156
static int config_input_main(AVFilterLink *inlink)
Definition: vf_overlay.c:878
FRAMESYNC_DEFINE_CLASS(overlay, OverlayContext, fs)
static int blend_slice_yuva420p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:710
static int blend_slice_yuva420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:694
static const char *const var_names[]
Definition: vf_overlay.c:49
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_overlay.c:131
static int activate(AVFilterContext *ctx)
Definition: vf_overlay.c:1056
#define DEFINE_BLEND_PLANE(depth, nbits)
Definition: vf_overlay.c:440
static av_cold int init(AVFilterContext *ctx)
Definition: vf_overlay.c:1048
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_overlay.c:82
#define OVERLAY
Definition: vf_overlay.c:65
static int blend_slice_yuva420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:790
static int blend_slice_yuv420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:782
static const AVFilterPad avfilter_vf_overlay_outputs[]
Definition: vf_overlay.c:1110
#define OFFSET(x)
Definition: vf_overlay.c:1062
static int blend_slice_yuv444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:750
static int config_output(AVFilterLink *outlink)
Definition: vf_overlay.c:320
#define DEFINE_ALPHA_COMPOSITE(depth, nbits)
Definition: vf_overlay.c:573
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:657
static int blend_slice_yuv422p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:718
static int blend_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:854
static void eval_expr(AVFilterContext *ctx)
Definition: vf_overlay.c:98
static int blend_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:774
#define G
Definition: vf_overlay.c:68
static int blend_slice_yuva422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:806
static int normalize_xy(double d, int chroma_sub)
Definition: vf_overlay.c:91
static int blend_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_overlay.c:766
@ VAR_OVERLAY_H
Definition: vf_overlay.h:31
@ VAR_MW
Definition: vf_overlay.h:28
@ VAR_MH
Definition: vf_overlay.h:29
@ VAR_OVERLAY_W
Definition: vf_overlay.h:30
@ OVERLAY_FORMAT_YUV420
Definition: vf_overlay.h:43
@ OVERLAY_FORMAT_YUV444
Definition: vf_overlay.h:47
@ OVERLAY_FORMAT_NB
Definition: vf_overlay.h:51
@ OVERLAY_FORMAT_YUV422
Definition: vf_overlay.h:45
@ OVERLAY_FORMAT_YUV420P10
Definition: vf_overlay.h:44
@ OVERLAY_FORMAT_RGB
Definition: vf_overlay.h:48
@ OVERLAY_FORMAT_AUTO
Definition: vf_overlay.h:50
@ OVERLAY_FORMAT_YUV422P10
Definition: vf_overlay.h:46
@ OVERLAY_FORMAT_GBRP
Definition: vf_overlay.h:49
void ff_overlay_init_x86(OverlayContext *s, int format, int pix_format, int alpha_format, int main_has_alpha)