2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2010 Baptiste Coudurier
4 * Copyright (c) 2007 Bobby Bingham
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * overlay one video on top of another
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"
39 #include "drawutils.h"
40 #include "framesync.h"
42 #include "vf_overlay.h"
44 typedef struct ThreadData {
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
57 "n", ///< number of frame
58 "pos", ///< position in the file
59 "t", ///< timestamp expressed in seconds
81 static av_cold void uninit(AVFilterContext *ctx)
83 OverlayContext *s = ctx->priv;
85 ff_framesync_uninit(&s->fs);
86 av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
87 av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
90 static inline int normalize_xy(double d, int chroma_sub)
94 return (int)d & ~((1 << chroma_sub) - 1);
97 static void eval_expr(AVFilterContext *ctx)
99 OverlayContext *s = ctx->priv;
101 s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
102 s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
103 /* It is necessary if x is expressed from y */
104 s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
105 s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
106 s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
109 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
116 ret = av_expr_parse(pexpr, expr, var_names,
117 NULL, NULL, NULL, NULL, 0, log_ctx);
119 av_log(log_ctx, AV_LOG_ERROR,
120 "Error when evaluating the expression '%s' for %s\n",
130 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
131 char *res, int res_len, int flags)
133 OverlayContext *s = ctx->priv;
136 if (!strcmp(cmd, "x"))
137 ret = set_expr(&s->x_pexpr, args, cmd, ctx);
138 else if (!strcmp(cmd, "y"))
139 ret = set_expr(&s->y_pexpr, args, cmd, ctx);
141 ret = AVERROR(ENOSYS);
146 if (s->eval_mode == EVAL_MODE_INIT) {
148 av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
149 s->var_values[VAR_X], s->x,
150 s->var_values[VAR_Y], s->y);
155 static const enum AVPixelFormat alpha_pix_fmts[] = {
156 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
157 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10,
158 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
159 AV_PIX_FMT_BGRA, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
162 static int query_formats(AVFilterContext *ctx)
164 OverlayContext *s = ctx->priv;
166 /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
167 static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
168 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVA420P,
169 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
172 static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
173 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
176 static const enum AVPixelFormat main_pix_fmts_yuv420p10[] = {
177 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUVA420P10,
180 static const enum AVPixelFormat overlay_pix_fmts_yuv420p10[] = {
181 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_NONE
184 static const enum AVPixelFormat main_pix_fmts_yuv422[] = {
185 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
187 static const enum AVPixelFormat overlay_pix_fmts_yuv422[] = {
188 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
191 static const enum AVPixelFormat main_pix_fmts_yuv422p10[] = {
192 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_NONE
194 static const enum AVPixelFormat overlay_pix_fmts_yuv422p10[] = {
195 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_NONE
198 static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
199 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
201 static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
202 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
205 static const enum AVPixelFormat main_pix_fmts_gbrp[] = {
206 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
208 static const enum AVPixelFormat overlay_pix_fmts_gbrp[] = {
209 AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
212 static const enum AVPixelFormat main_pix_fmts_rgb[] = {
213 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
214 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
215 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
218 static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
219 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
220 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
224 const enum AVPixelFormat *main_formats, *overlay_formats;
225 AVFilterFormats *formats;
229 case OVERLAY_FORMAT_YUV420:
230 main_formats = main_pix_fmts_yuv420;
231 overlay_formats = overlay_pix_fmts_yuv420;
233 case OVERLAY_FORMAT_YUV420P10:
234 main_formats = main_pix_fmts_yuv420p10;
235 overlay_formats = overlay_pix_fmts_yuv420p10;
237 case OVERLAY_FORMAT_YUV422:
238 main_formats = main_pix_fmts_yuv422;
239 overlay_formats = overlay_pix_fmts_yuv422;
241 case OVERLAY_FORMAT_YUV422P10:
242 main_formats = main_pix_fmts_yuv422p10;
243 overlay_formats = overlay_pix_fmts_yuv422p10;
245 case OVERLAY_FORMAT_YUV444:
246 main_formats = main_pix_fmts_yuv444;
247 overlay_formats = overlay_pix_fmts_yuv444;
249 case OVERLAY_FORMAT_RGB:
250 main_formats = main_pix_fmts_rgb;
251 overlay_formats = overlay_pix_fmts_rgb;
253 case OVERLAY_FORMAT_GBRP:
254 main_formats = main_pix_fmts_gbrp;
255 overlay_formats = overlay_pix_fmts_gbrp;
257 case OVERLAY_FORMAT_AUTO:
258 return ff_set_common_formats(ctx, ff_make_format_list(alpha_pix_fmts));
263 formats = ff_make_format_list(main_formats);
264 if ((ret = ff_formats_ref(formats, &ctx->inputs[MAIN]->outcfg.formats)) < 0 ||
265 (ret = ff_formats_ref(formats, &ctx->outputs[MAIN]->incfg.formats)) < 0)
268 return ff_formats_ref(ff_make_format_list(overlay_formats),
269 &ctx->inputs[OVERLAY]->outcfg.formats);
272 static int config_input_overlay(AVFilterLink *inlink)
274 AVFilterContext *ctx = inlink->dst;
275 OverlayContext *s = inlink->dst->priv;
277 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
279 av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
281 /* Finish the configuration by evaluating the expressions
282 now when both inputs are configured. */
283 s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
284 s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
285 s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
286 s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
287 s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
288 s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
289 s->var_values[VAR_X] = NAN;
290 s->var_values[VAR_Y] = NAN;
291 s->var_values[VAR_N] = 0;
292 s->var_values[VAR_T] = NAN;
293 s->var_values[VAR_POS] = NAN;
295 if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
296 (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
299 s->overlay_is_packed_rgb =
300 ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
301 s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
303 if (s->eval_mode == EVAL_MODE_INIT) {
305 av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
306 s->var_values[VAR_X], s->x,
307 s->var_values[VAR_Y], s->y);
310 av_log(ctx, AV_LOG_VERBOSE,
311 "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
312 ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
313 av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
314 ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
315 av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
319 static int config_output(AVFilterLink *outlink)
321 AVFilterContext *ctx = outlink->src;
322 OverlayContext *s = ctx->priv;
325 if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
328 outlink->w = ctx->inputs[MAIN]->w;
329 outlink->h = ctx->inputs[MAIN]->h;
330 outlink->time_base = ctx->inputs[MAIN]->time_base;
332 return ff_framesync_configure(&s->fs);
335 // divide by 255 and round to nearest
336 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
337 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
339 // calculate the unpremultiplied alpha, applying the general equation:
340 // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
341 // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
342 // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
343 #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
346 * Blend image in src to destination buffer dst at position (x, y).
349 static av_always_inline void blend_slice_packed_rgb(AVFilterContext *ctx,
350 AVFrame *dst, const AVFrame *src,
351 int main_has_alpha, int x, int y,
352 int is_straight, int jobnr, int nb_jobs)
354 OverlayContext *s = ctx->priv;
355 int i, imax, j, jmax;
356 const int src_w = src->width;
357 const int src_h = src->height;
358 const int dst_w = dst->width;
359 const int dst_h = dst->height;
360 uint8_t alpha; ///< the amount of overlay to blend on to main
361 const int dr = s->main_rgba_map[R];
362 const int dg = s->main_rgba_map[G];
363 const int db = s->main_rgba_map[B];
364 const int da = s->main_rgba_map[A];
365 const int dstep = s->main_pix_step[0];
366 const int sr = s->overlay_rgba_map[R];
367 const int sg = s->overlay_rgba_map[G];
368 const int sb = s->overlay_rgba_map[B];
369 const int sa = s->overlay_rgba_map[A];
370 const int sstep = s->overlay_pix_step[0];
371 int slice_start, slice_end;
372 uint8_t *S, *sp, *d, *dp;
375 imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h);
377 slice_start = i + (imax * jobnr) / nb_jobs;
378 slice_end = i + (imax * (jobnr+1)) / nb_jobs;
380 sp = src->data[0] + (slice_start) * src->linesize[0];
381 dp = dst->data[0] + (y + slice_start) * dst->linesize[0];
383 for (i = slice_start; i < slice_end; i++) {
386 d = dp + (x+j) * dstep;
388 for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
391 // if the main channel has an alpha channel, alpha has to be calculated
392 // to create an un-premultiplied (straight) alpha value
393 if (main_has_alpha && alpha != 0 && alpha != 255) {
394 uint8_t alpha_d = d[da];
395 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
407 // main_value = main_value * (1 - alpha) + overlay_value * alpha
408 // since alpha is in the range 0-255, the result must divided by 255
409 d[dr] = is_straight ? FAST_DIV255(d[dr] * (255 - alpha) + S[sr] * alpha) :
410 FFMIN(FAST_DIV255(d[dr] * (255 - alpha)) + S[sr], 255);
411 d[dg] = is_straight ? FAST_DIV255(d[dg] * (255 - alpha) + S[sg] * alpha) :
412 FFMIN(FAST_DIV255(d[dg] * (255 - alpha)) + S[sg], 255);
413 d[db] = is_straight ? FAST_DIV255(d[db] * (255 - alpha) + S[sb] * alpha) :
414 FFMIN(FAST_DIV255(d[db] * (255 - alpha)) + S[sb], 255);
416 if (main_has_alpha) {
424 // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
425 d[da] += FAST_DIV255((255 - d[da]) * S[sa]);
431 dp += dst->linesize[0];
432 sp += src->linesize[0];
436 #define DEFINE_BLEND_PLANE(depth, nbits) \
437 static av_always_inline void blend_plane_##depth##_##nbits##bits(AVFilterContext *ctx, \
438 AVFrame *dst, const AVFrame *src, \
439 int src_w, int src_h, \
440 int dst_w, int dst_h, \
441 int i, int hsub, int vsub, \
443 int main_has_alpha, \
452 OverlayContext *octx = ctx->priv; \
453 int src_wp = AV_CEIL_RSHIFT(src_w, hsub); \
454 int src_hp = AV_CEIL_RSHIFT(src_h, vsub); \
455 int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub); \
456 int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub); \
459 uint##depth##_t *s, *sp, *d, *dp, *dap, *a, *da, *ap; \
460 int jmax, j, k, kmax; \
461 int slice_start, slice_end; \
462 const uint##depth##_t max = (1 << nbits) - 1; \
463 const uint##depth##_t mid = (1 << (nbits -1)) ; \
464 int bytes = depth / 8; \
468 jmax = FFMIN3(-yp + dst_hp, FFMIN(src_hp, dst_hp), yp + src_hp); \
470 slice_start = j + (jmax * jobnr) / nb_jobs; \
471 slice_end = j + (jmax * (jobnr+1)) / nb_jobs; \
473 sp = (uint##depth##_t *)(src->data[i] + (slice_start) * src->linesize[i]); \
474 dp = (uint##depth##_t *)(dst->data[dst_plane] \
475 + (yp + slice_start) * dst->linesize[dst_plane] \
477 ap = (uint##depth##_t *)(src->data[3] + (slice_start << vsub) * src->linesize[3]); \
478 dap = (uint##depth##_t *)(dst->data[3] + ((yp + slice_start) << vsub) * dst->linesize[3]); \
480 for (j = slice_start; j < slice_end; j++) { \
482 d = dp + (xp+k) * dst_step; \
484 a = ap + (k<<hsub); \
485 da = dap + ((xp+k) << hsub); \
486 kmax = FFMIN(-xp + dst_wp, src_wp); \
488 if (nbits == 8 && ((vsub && j+1 < src_hp) || !vsub) && octx->blend_row[i]) { \
489 int c = octx->blend_row[i]((uint8_t*)d, (uint8_t*)da, (uint8_t*)s, \
490 (uint8_t*)a, kmax - k, src->linesize[3]); \
494 da += (1 << hsub) * c; \
495 a += (1 << hsub) * c; \
498 for (; k < kmax; k++) { \
499 int alpha_v, alpha_h, alpha; \
501 /* average alpha for color components, improve quality */ \
502 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
503 alpha = (a[0] + a[src->linesize[3]] + \
504 a[1] + a[src->linesize[3]+1]) >> 2; \
505 } else if (hsub || vsub) { \
506 alpha_h = hsub && k+1 < src_wp ? \
507 (a[0] + a[1]) >> 1 : a[0]; \
508 alpha_v = vsub && j+1 < src_hp ? \
509 (a[0] + a[src->linesize[3]]) >> 1 : a[0]; \
510 alpha = (alpha_v + alpha_h) >> 1; \
513 /* if the main channel has an alpha channel, alpha has to be calculated */ \
514 /* to create an un-premultiplied (straight) alpha value */ \
515 if (main_has_alpha && alpha != 0 && alpha != max) { \
516 /* average alpha for color components, improve quality */ \
518 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
519 alpha_d = (da[0] + da[dst->linesize[3]] + \
520 da[1] + da[dst->linesize[3]+1]) >> 2; \
521 } else if (hsub || vsub) { \
522 alpha_h = hsub && k+1 < src_wp ? \
523 (da[0] + da[1]) >> 1 : da[0]; \
524 alpha_v = vsub && j+1 < src_hp ? \
525 (da[0] + da[dst->linesize[3]]) >> 1 : da[0]; \
526 alpha_d = (alpha_v + alpha_h) >> 1; \
529 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
533 *d = (*d * (max - alpha) + *s * alpha) / max; \
535 *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha); \
539 *d = av_clip((*d * (max - alpha) + *s * alpha) / max + *s - mid, -mid, mid) + mid; \
541 *d = FFMIN((*d * (max - alpha) + *s * alpha) / max + *s, max); \
544 *d = av_clip(FAST_DIV255((*d - mid) * (max - alpha)) + *s - mid, -mid, mid) + mid; \
546 *d = FFMIN(FAST_DIV255(*d * (max - alpha)) + *s, max); \
554 dp += dst->linesize[dst_plane] / bytes; \
555 sp += src->linesize[i] / bytes; \
556 ap += (1 << vsub) * src->linesize[3] / bytes; \
557 dap += (1 << vsub) * dst->linesize[3] / bytes; \
560 DEFINE_BLEND_PLANE(8, 8)
561 DEFINE_BLEND_PLANE(16, 10)
563 #define DEFINE_ALPHA_COMPOSITE(depth, nbits) \
564 static inline void alpha_composite_##depth##_##nbits##bits(const AVFrame *src, const AVFrame *dst, \
565 int src_w, int src_h, \
566 int dst_w, int dst_h, \
568 int jobnr, int nb_jobs) \
570 uint##depth##_t alpha; /* the amount of overlay to blend on to main */ \
571 uint##depth##_t *s, *sa, *d, *da; \
572 int i, imax, j, jmax; \
573 int slice_start, slice_end; \
574 const uint##depth##_t max = (1 << nbits) - 1; \
575 int bytes = depth / 8; \
577 imax = FFMIN(-y + dst_h, src_h); \
578 slice_start = (imax * jobnr) / nb_jobs; \
579 slice_end = ((imax * (jobnr+1)) / nb_jobs); \
582 sa = (uint##depth##_t *)(src->data[3] + (i + slice_start) * src->linesize[3]); \
583 da = (uint##depth##_t *)(dst->data[3] + (y + i + slice_start) * dst->linesize[3]); \
585 for (i = i + slice_start; i < slice_end; i++) { \
590 for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) { \
592 if (alpha != 0 && alpha != max) { \
593 uint8_t alpha_d = *d; \
594 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
598 else if (alpha > 0) { \
599 /* apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha */ \
601 *d += (max - *d) * *s / max; \
603 *d += FAST_DIV255((max - *d) * *s); \
608 da += dst->linesize[3] / bytes; \
609 sa += src->linesize[3] / bytes; \
612 DEFINE_ALPHA_COMPOSITE(8, 8)
613 DEFINE_ALPHA_COMPOSITE(16, 10)
615 #define DEFINE_BLEND_SLICE_YUV(depth, nbits) \
616 static av_always_inline void blend_slice_yuv_##depth##_##nbits##bits(AVFilterContext *ctx, \
617 AVFrame *dst, const AVFrame *src, \
618 int hsub, int vsub, \
619 int main_has_alpha, \
622 int jobnr, int nb_jobs) \
624 OverlayContext *s = ctx->priv; \
625 const int src_w = src->width; \
626 const int src_h = src->height; \
627 const int dst_w = dst->width; \
628 const int dst_h = dst->height; \
630 blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, \
631 x, y, main_has_alpha, s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, \
632 s->main_desc->comp[0].step, is_straight, 1, jobnr, nb_jobs); \
633 blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, \
634 x, y, main_has_alpha, s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, \
635 s->main_desc->comp[1].step, is_straight, 1, jobnr, nb_jobs); \
636 blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, \
637 x, y, main_has_alpha, s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, \
638 s->main_desc->comp[2].step, is_straight, 1, jobnr, nb_jobs); \
640 if (main_has_alpha) \
641 alpha_composite_##depth##_##nbits##bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, \
644 DEFINE_BLEND_SLICE_YUV(8, 8)
645 DEFINE_BLEND_SLICE_YUV(16, 10)
647 static av_always_inline void blend_slice_planar_rgb(AVFilterContext *ctx,
648 AVFrame *dst, const AVFrame *src,
656 OverlayContext *s = ctx->priv;
657 const int src_w = src->width;
658 const int src_h = src->height;
659 const int dst_w = dst->width;
660 const int dst_h = dst->height;
662 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha,
663 s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, is_straight, 0,
665 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha,
666 s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, is_straight, 0,
668 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha,
669 s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, is_straight, 0,
673 alpha_composite_8_8bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, jobnr, nb_jobs);
676 static int blend_slice_yuv420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
678 OverlayContext *s = ctx->priv;
679 ThreadData *td = arg;
680 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 1, jobnr, nb_jobs);
684 static int blend_slice_yuva420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
686 OverlayContext *s = ctx->priv;
687 ThreadData *td = arg;
688 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 1, jobnr, nb_jobs);
692 static int blend_slice_yuv420p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
694 OverlayContext *s = ctx->priv;
695 ThreadData *td = arg;
696 blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 1, jobnr, nb_jobs);
700 static int blend_slice_yuva420p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
702 OverlayContext *s = ctx->priv;
703 ThreadData *td = arg;
704 blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 1, jobnr, nb_jobs);
708 static int blend_slice_yuv422p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
710 OverlayContext *s = ctx->priv;
711 ThreadData *td = arg;
712 blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
716 static int blend_slice_yuva422p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
718 OverlayContext *s = ctx->priv;
719 ThreadData *td = arg;
720 blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
724 static int blend_slice_yuv422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
726 OverlayContext *s = ctx->priv;
727 ThreadData *td = arg;
728 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
732 static int blend_slice_yuva422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
734 OverlayContext *s = ctx->priv;
735 ThreadData *td = arg;
736 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
740 static int blend_slice_yuv444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
742 OverlayContext *s = ctx->priv;
743 ThreadData *td = arg;
744 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
748 static int blend_slice_yuva444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
750 OverlayContext *s = ctx->priv;
751 ThreadData *td = arg;
752 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
756 static int blend_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
758 OverlayContext *s = ctx->priv;
759 ThreadData *td = arg;
760 blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
764 static int blend_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
766 OverlayContext *s = ctx->priv;
767 ThreadData *td = arg;
768 blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
772 static int blend_slice_yuv420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
774 OverlayContext *s = ctx->priv;
775 ThreadData *td = arg;
776 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 0, jobnr, nb_jobs);
780 static int blend_slice_yuva420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
782 OverlayContext *s = ctx->priv;
783 ThreadData *td = arg;
784 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 0, jobnr, nb_jobs);
788 static int blend_slice_yuv422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
790 OverlayContext *s = ctx->priv;
791 ThreadData *td = arg;
792 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
796 static int blend_slice_yuva422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
798 OverlayContext *s = ctx->priv;
799 ThreadData *td = arg;
800 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
804 static int blend_slice_yuv444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
806 OverlayContext *s = ctx->priv;
807 ThreadData *td = arg;
808 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
812 static int blend_slice_yuva444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
814 OverlayContext *s = ctx->priv;
815 ThreadData *td = arg;
816 blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
820 static int blend_slice_gbrp_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
822 OverlayContext *s = ctx->priv;
823 ThreadData *td = arg;
824 blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
828 static int blend_slice_gbrap_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
830 OverlayContext *s = ctx->priv;
831 ThreadData *td = arg;
832 blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
836 static int blend_slice_rgb(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
838 OverlayContext *s = ctx->priv;
839 ThreadData *td = arg;
840 blend_slice_packed_rgb(ctx, td->dst, td->src, 0, s->x, s->y, 1, jobnr, nb_jobs);
844 static int blend_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
846 OverlayContext *s = ctx->priv;
847 ThreadData *td = arg;
848 blend_slice_packed_rgb(ctx, td->dst, td->src, 1, s->x, s->y, 1, jobnr, nb_jobs);
852 static int blend_slice_rgb_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
854 OverlayContext *s = ctx->priv;
855 ThreadData *td = arg;
856 blend_slice_packed_rgb(ctx, td->dst, td->src, 0, s->x, s->y, 0, jobnr, nb_jobs);
860 static int blend_slice_rgba_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
862 OverlayContext *s = ctx->priv;
863 ThreadData *td = arg;
864 blend_slice_packed_rgb(ctx, td->dst, td->src, 1, s->x, s->y, 0, jobnr, nb_jobs);
868 static int config_input_main(AVFilterLink *inlink)
870 OverlayContext *s = inlink->dst->priv;
871 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
873 av_image_fill_max_pixsteps(s->main_pix_step, NULL, pix_desc);
875 s->hsub = pix_desc->log2_chroma_w;
876 s->vsub = pix_desc->log2_chroma_h;
878 s->main_desc = pix_desc;
880 s->main_is_packed_rgb =
881 ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
882 s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
884 case OVERLAY_FORMAT_YUV420:
885 s->blend_slice = s->main_has_alpha ? blend_slice_yuva420 : blend_slice_yuv420;
887 case OVERLAY_FORMAT_YUV420P10:
888 s->blend_slice = s->main_has_alpha ? blend_slice_yuva420p10 : blend_slice_yuv420p10;
890 case OVERLAY_FORMAT_YUV422:
891 s->blend_slice = s->main_has_alpha ? blend_slice_yuva422 : blend_slice_yuv422;
893 case OVERLAY_FORMAT_YUV422P10:
894 s->blend_slice = s->main_has_alpha ? blend_slice_yuva422p10 : blend_slice_yuv422p10;
896 case OVERLAY_FORMAT_YUV444:
897 s->blend_slice = s->main_has_alpha ? blend_slice_yuva444 : blend_slice_yuv444;
899 case OVERLAY_FORMAT_RGB:
900 s->blend_slice = s->main_has_alpha ? blend_slice_rgba : blend_slice_rgb;
902 case OVERLAY_FORMAT_GBRP:
903 s->blend_slice = s->main_has_alpha ? blend_slice_gbrap : blend_slice_gbrp;
905 case OVERLAY_FORMAT_AUTO:
906 switch (inlink->format) {
907 case AV_PIX_FMT_YUVA420P:
908 s->blend_slice = blend_slice_yuva420;
910 case AV_PIX_FMT_YUVA420P10:
911 s->blend_slice = blend_slice_yuva420p10;
913 case AV_PIX_FMT_YUVA422P:
914 s->blend_slice = blend_slice_yuva422;
916 case AV_PIX_FMT_YUVA422P10:
917 s->blend_slice = blend_slice_yuva422p10;
919 case AV_PIX_FMT_YUVA444P:
920 s->blend_slice = blend_slice_yuva444;
922 case AV_PIX_FMT_ARGB:
923 case AV_PIX_FMT_RGBA:
924 case AV_PIX_FMT_BGRA:
925 case AV_PIX_FMT_ABGR:
926 s->blend_slice = blend_slice_rgba;
928 case AV_PIX_FMT_GBRAP:
929 s->blend_slice = blend_slice_gbrap;
938 if (!s->alpha_format)
942 case OVERLAY_FORMAT_YUV420:
943 s->blend_slice = s->main_has_alpha ? blend_slice_yuva420_pm : blend_slice_yuv420_pm;
945 case OVERLAY_FORMAT_YUV422:
946 s->blend_slice = s->main_has_alpha ? blend_slice_yuva422_pm : blend_slice_yuv422_pm;
948 case OVERLAY_FORMAT_YUV444:
949 s->blend_slice = s->main_has_alpha ? blend_slice_yuva444_pm : blend_slice_yuv444_pm;
951 case OVERLAY_FORMAT_RGB:
952 s->blend_slice = s->main_has_alpha ? blend_slice_rgba_pm : blend_slice_rgb_pm;
954 case OVERLAY_FORMAT_GBRP:
955 s->blend_slice = s->main_has_alpha ? blend_slice_gbrap_pm : blend_slice_gbrp_pm;
957 case OVERLAY_FORMAT_AUTO:
958 switch (inlink->format) {
959 case AV_PIX_FMT_YUVA420P:
960 s->blend_slice = blend_slice_yuva420_pm;
962 case AV_PIX_FMT_YUVA422P:
963 s->blend_slice = blend_slice_yuva422_pm;
965 case AV_PIX_FMT_YUVA444P:
966 s->blend_slice = blend_slice_yuva444_pm;
968 case AV_PIX_FMT_ARGB:
969 case AV_PIX_FMT_RGBA:
970 case AV_PIX_FMT_BGRA:
971 case AV_PIX_FMT_ABGR:
972 s->blend_slice = blend_slice_rgba_pm;
974 case AV_PIX_FMT_GBRAP:
975 s->blend_slice = blend_slice_gbrap_pm;
986 ff_overlay_init_x86(s, s->format, inlink->format,
987 s->alpha_format, s->main_has_alpha);
992 static int do_blend(FFFrameSync *fs)
994 AVFilterContext *ctx = fs->parent;
995 AVFrame *mainpic, *second;
996 OverlayContext *s = ctx->priv;
997 AVFilterLink *inlink = ctx->inputs[0];
1000 ret = ff_framesync_dualinput_get_writable(fs, &mainpic, &second);
1004 return ff_filter_frame(ctx->outputs[0], mainpic);
1006 if (s->eval_mode == EVAL_MODE_FRAME) {
1007 int64_t pos = mainpic->pkt_pos;
1009 s->var_values[VAR_N] = inlink->frame_count_out;
1010 s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
1011 NAN : mainpic->pts * av_q2d(inlink->time_base);
1012 s->var_values[VAR_POS] = pos == -1 ? NAN : pos;
1014 s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = second->width;
1015 s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = second->height;
1016 s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = mainpic->width;
1017 s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = mainpic->height;
1020 av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
1021 s->var_values[VAR_N], s->var_values[VAR_T], s->var_values[VAR_POS],
1022 s->var_values[VAR_X], s->x,
1023 s->var_values[VAR_Y], s->y);
1026 if (s->x < mainpic->width && s->x + second->width >= 0 &&
1027 s->y < mainpic->height && s->y + second->height >= 0) {
1032 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)),
1033 ff_filter_get_nb_threads(ctx)));
1035 return ff_filter_frame(ctx->outputs[0], mainpic);
1038 static av_cold int init(AVFilterContext *ctx)
1040 OverlayContext *s = ctx->priv;
1042 s->fs.on_event = do_blend;
1046 static int activate(AVFilterContext *ctx)
1048 OverlayContext *s = ctx->priv;
1049 return ff_framesync_activate(&s->fs);
1052 #define OFFSET(x) offsetof(OverlayContext, x)
1053 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1055 static const AVOption overlay_options[] = {
1056 { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS },
1057 { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS },
1058 { "eof_action", "Action to take when encountering EOF from secondary input ",
1059 OFFSET(fs.opt_eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
1060 EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, "eof_action" },
1061 { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, "eof_action" },
1062 { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, "eof_action" },
1063 { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, "eof_action" },
1064 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
1065 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
1066 { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
1067 { "shortest", "force termination when the shortest input terminates", OFFSET(fs.opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1068 { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
1069 { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
1070 { "yuv420p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420P10}, .flags = FLAGS, .unit = "format" },
1071 { "yuv422", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422}, .flags = FLAGS, .unit = "format" },
1072 { "yuv422p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422P10}, .flags = FLAGS, .unit = "format" },
1073 { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
1074 { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
1075 { "gbrp", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_GBRP}, .flags = FLAGS, .unit = "format" },
1076 { "auto", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_AUTO}, .flags = FLAGS, .unit = "format" },
1077 { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(fs.opt_repeatlast), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
1078 { "alpha", "alpha format", OFFSET(alpha_format), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "alpha_format" },
1079 { "straight", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, .flags = FLAGS, .unit = "alpha_format" },
1080 { "premultiplied", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, .flags = FLAGS, .unit = "alpha_format" },
1084 FRAMESYNC_DEFINE_CLASS(overlay, OverlayContext, fs);
1086 static const AVFilterPad avfilter_vf_overlay_inputs[] = {
1089 .type = AVMEDIA_TYPE_VIDEO,
1090 .config_props = config_input_main,
1094 .type = AVMEDIA_TYPE_VIDEO,
1095 .config_props = config_input_overlay,
1100 static const AVFilterPad avfilter_vf_overlay_outputs[] = {
1103 .type = AVMEDIA_TYPE_VIDEO,
1104 .config_props = config_output,
1109 const AVFilter ff_vf_overlay = {
1111 .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
1112 .preinit = overlay_framesync_preinit,
1115 .priv_size = sizeof(OverlayContext),
1116 .priv_class = &overlay_class,
1117 .query_formats = query_formats,
1118 .activate = activate,
1119 .process_command = process_command,
1120 .inputs = avfilter_vf_overlay_inputs,
1121 .outputs = avfilter_vf_overlay_outputs,
1122 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
1123 AVFILTER_FLAG_SLICE_THREADS,