]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_overlay.c
avfilter/vf_overlay: support for 8bit and 10bit overlay with macro-based function
[ffmpeg] / libavfilter / vf_overlay.c
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 "internal.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     "pos",          ///< position in the file
59     "t",            ///< timestamp expressed in seconds
60     NULL
61 };
62
63 #define MAIN    0
64 #define OVERLAY 1
65
66 #define R 0
67 #define G 1
68 #define B 2
69 #define A 3
70
71 #define Y 0
72 #define U 1
73 #define V 2
74
75 enum EvalMode {
76     EVAL_MODE_INIT,
77     EVAL_MODE_FRAME,
78     EVAL_MODE_NB
79 };
80
81 static av_cold void uninit(AVFilterContext *ctx)
82 {
83     OverlayContext *s = ctx->priv;
84
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;
88 }
89
90 static inline int normalize_xy(double d, int chroma_sub)
91 {
92     if (isnan(d))
93         return INT_MAX;
94     return (int)d & ~((1 << chroma_sub) - 1);
95 }
96
97 static void eval_expr(AVFilterContext *ctx)
98 {
99     OverlayContext *s = ctx->priv;
100
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);
107 }
108
109 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
110 {
111     int ret;
112     AVExpr *old = NULL;
113
114     if (*pexpr)
115         old = *pexpr;
116     ret = av_expr_parse(pexpr, expr, var_names,
117                         NULL, NULL, NULL, NULL, 0, log_ctx);
118     if (ret < 0) {
119         av_log(log_ctx, AV_LOG_ERROR,
120                "Error when evaluating the expression '%s' for %s\n",
121                expr, option);
122         *pexpr = old;
123         return ret;
124     }
125
126     av_expr_free(old);
127     return 0;
128 }
129
130 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
131                            char *res, int res_len, int flags)
132 {
133     OverlayContext *s = ctx->priv;
134     int ret;
135
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);
140     else
141         ret = AVERROR(ENOSYS);
142
143     if (ret < 0)
144         return ret;
145
146     if (s->eval_mode == EVAL_MODE_INIT) {
147         eval_expr(ctx);
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);
151     }
152     return ret;
153 }
154
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_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
158     AV_PIX_FMT_BGRA, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
159 };
160
161 static int query_formats(AVFilterContext *ctx)
162 {
163     OverlayContext *s = ctx->priv;
164
165     /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
166     static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
167         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVA420P,
168         AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
169         AV_PIX_FMT_NONE
170     };
171     static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
172         AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
173     };
174
175     static const enum AVPixelFormat main_pix_fmts_yuv422[] = {
176         AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
177     };
178     static const enum AVPixelFormat overlay_pix_fmts_yuv422[] = {
179         AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
180     };
181
182     static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
183         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
184     };
185     static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
186         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
187     };
188
189     static const enum AVPixelFormat main_pix_fmts_gbrp[] = {
190         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
191     };
192     static const enum AVPixelFormat overlay_pix_fmts_gbrp[] = {
193         AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
194     };
195
196     static const enum AVPixelFormat main_pix_fmts_rgb[] = {
197         AV_PIX_FMT_ARGB,  AV_PIX_FMT_RGBA,
198         AV_PIX_FMT_ABGR,  AV_PIX_FMT_BGRA,
199         AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
200         AV_PIX_FMT_NONE
201     };
202     static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
203         AV_PIX_FMT_ARGB,  AV_PIX_FMT_RGBA,
204         AV_PIX_FMT_ABGR,  AV_PIX_FMT_BGRA,
205         AV_PIX_FMT_NONE
206     };
207
208     AVFilterFormats *main_formats = NULL;
209     AVFilterFormats *overlay_formats = NULL;
210     int ret;
211
212     switch (s->format) {
213     case OVERLAY_FORMAT_YUV420:
214         if (!(main_formats    = ff_make_format_list(main_pix_fmts_yuv420)) ||
215             !(overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420))) {
216                 ret = AVERROR(ENOMEM);
217                 goto fail;
218             }
219         break;
220     case OVERLAY_FORMAT_YUV422:
221         if (!(main_formats    = ff_make_format_list(main_pix_fmts_yuv422)) ||
222             !(overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv422))) {
223                 ret = AVERROR(ENOMEM);
224                 goto fail;
225             }
226         break;
227     case OVERLAY_FORMAT_YUV444:
228         if (!(main_formats    = ff_make_format_list(main_pix_fmts_yuv444)) ||
229             !(overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444))) {
230                 ret = AVERROR(ENOMEM);
231                 goto fail;
232             }
233         break;
234     case OVERLAY_FORMAT_RGB:
235         if (!(main_formats    = ff_make_format_list(main_pix_fmts_rgb)) ||
236             !(overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb))) {
237                 ret = AVERROR(ENOMEM);
238                 goto fail;
239             }
240         break;
241     case OVERLAY_FORMAT_GBRP:
242         if (!(main_formats    = ff_make_format_list(main_pix_fmts_gbrp)) ||
243             !(overlay_formats = ff_make_format_list(overlay_pix_fmts_gbrp))) {
244                 ret = AVERROR(ENOMEM);
245                 goto fail;
246             }
247         break;
248     case OVERLAY_FORMAT_AUTO:
249         if (!(main_formats    = ff_make_format_list(alpha_pix_fmts))) {
250                 ret = AVERROR(ENOMEM);
251                 goto fail;
252             }
253         break;
254     default:
255         av_assert0(0);
256     }
257
258     if (s->format == OVERLAY_FORMAT_AUTO) {
259         ret = ff_set_common_formats(ctx, main_formats);
260         if (ret < 0)
261             goto fail;
262     } else {
263         if ((ret = ff_formats_ref(main_formats   , &ctx->inputs[MAIN]->out_formats   )) < 0 ||
264             (ret = ff_formats_ref(overlay_formats, &ctx->inputs[OVERLAY]->out_formats)) < 0 ||
265             (ret = ff_formats_ref(main_formats   , &ctx->outputs[MAIN]->in_formats   )) < 0)
266                 goto fail;
267     }
268
269     return 0;
270 fail:
271     if (main_formats)
272         av_freep(&main_formats->formats);
273     av_freep(&main_formats);
274     if (overlay_formats)
275         av_freep(&overlay_formats->formats);
276     av_freep(&overlay_formats);
277     return ret;
278 }
279
280 static int config_input_overlay(AVFilterLink *inlink)
281 {
282     AVFilterContext *ctx  = inlink->dst;
283     OverlayContext  *s = inlink->dst->priv;
284     int ret;
285     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
286
287     av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
288
289     /* Finish the configuration by evaluating the expressions
290        now when both inputs are configured. */
291     s->var_values[VAR_MAIN_W   ] = s->var_values[VAR_MW] = ctx->inputs[MAIN   ]->w;
292     s->var_values[VAR_MAIN_H   ] = s->var_values[VAR_MH] = ctx->inputs[MAIN   ]->h;
293     s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
294     s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
295     s->var_values[VAR_HSUB]  = 1<<pix_desc->log2_chroma_w;
296     s->var_values[VAR_VSUB]  = 1<<pix_desc->log2_chroma_h;
297     s->var_values[VAR_X]     = NAN;
298     s->var_values[VAR_Y]     = NAN;
299     s->var_values[VAR_N]     = 0;
300     s->var_values[VAR_T]     = NAN;
301     s->var_values[VAR_POS]   = NAN;
302
303     if ((ret = set_expr(&s->x_pexpr,      s->x_expr,      "x",      ctx)) < 0 ||
304         (ret = set_expr(&s->y_pexpr,      s->y_expr,      "y",      ctx)) < 0)
305         return ret;
306
307     s->overlay_is_packed_rgb =
308         ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
309     s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
310
311     if (s->eval_mode == EVAL_MODE_INIT) {
312         eval_expr(ctx);
313         av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
314                s->var_values[VAR_X], s->x,
315                s->var_values[VAR_Y], s->y);
316     }
317
318     av_log(ctx, AV_LOG_VERBOSE,
319            "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
320            ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
321            av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
322            ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
323            av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
324     return 0;
325 }
326
327 static int config_output(AVFilterLink *outlink)
328 {
329     AVFilterContext *ctx = outlink->src;
330     OverlayContext *s = ctx->priv;
331     int ret;
332
333     if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
334         return ret;
335
336     outlink->w = ctx->inputs[MAIN]->w;
337     outlink->h = ctx->inputs[MAIN]->h;
338     outlink->time_base = ctx->inputs[MAIN]->time_base;
339
340     return ff_framesync_configure(&s->fs);
341 }
342
343 // divide by 255 and round to nearest
344 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
345 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
346
347 // calculate the unpremultiplied alpha, applying the general equation:
348 // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
349 // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
350 // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
351 #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
352
353 /**
354  * Blend image in src to destination buffer dst at position (x, y).
355  */
356
357 static av_always_inline void blend_slice_packed_rgb(AVFilterContext *ctx,
358                                    AVFrame *dst, const AVFrame *src,
359                                    int main_has_alpha, int x, int y,
360                                    int is_straight, int jobnr, int nb_jobs)
361 {
362     OverlayContext *s = ctx->priv;
363     int i, imax, j, jmax;
364     const int src_w = src->width;
365     const int src_h = src->height;
366     const int dst_w = dst->width;
367     const int dst_h = dst->height;
368     uint8_t alpha;          ///< the amount of overlay to blend on to main
369     const int dr = s->main_rgba_map[R];
370     const int dg = s->main_rgba_map[G];
371     const int db = s->main_rgba_map[B];
372     const int da = s->main_rgba_map[A];
373     const int dstep = s->main_pix_step[0];
374     const int sr = s->overlay_rgba_map[R];
375     const int sg = s->overlay_rgba_map[G];
376     const int sb = s->overlay_rgba_map[B];
377     const int sa = s->overlay_rgba_map[A];
378     const int sstep = s->overlay_pix_step[0];
379     int slice_start, slice_end;
380     uint8_t *S, *sp, *d, *dp;
381
382     i = FFMAX(-y, 0);
383     imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h);
384
385     slice_start = i + (imax * jobnr) / nb_jobs;
386     slice_end = i + (imax * (jobnr+1)) / nb_jobs;
387
388     sp = src->data[0] + (slice_start)     * src->linesize[0];
389     dp = dst->data[0] + (y + slice_start) * dst->linesize[0];
390
391     for (i = slice_start; i < slice_end; i++) {
392         j = FFMAX(-x, 0);
393         S = sp + j     * sstep;
394         d = dp + (x+j) * dstep;
395
396         for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
397             alpha = S[sa];
398
399             // if the main channel has an alpha channel, alpha has to be calculated
400             // to create an un-premultiplied (straight) alpha value
401             if (main_has_alpha && alpha != 0 && alpha != 255) {
402                 uint8_t alpha_d = d[da];
403                 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
404             }
405
406             switch (alpha) {
407             case 0:
408                 break;
409             case 255:
410                 d[dr] = S[sr];
411                 d[dg] = S[sg];
412                 d[db] = S[sb];
413                 break;
414             default:
415                 // main_value = main_value * (1 - alpha) + overlay_value * alpha
416                 // since alpha is in the range 0-255, the result must divided by 255
417                 d[dr] = is_straight ? FAST_DIV255(d[dr] * (255 - alpha) + S[sr] * alpha) :
418                         FFMIN(FAST_DIV255(d[dr] * (255 - alpha)) + S[sr], 255);
419                 d[dg] = is_straight ? FAST_DIV255(d[dg] * (255 - alpha) + S[sg] * alpha) :
420                         FFMIN(FAST_DIV255(d[dg] * (255 - alpha)) + S[sg], 255);
421                 d[db] = is_straight ? FAST_DIV255(d[db] * (255 - alpha) + S[sb] * alpha) :
422                         FFMIN(FAST_DIV255(d[db] * (255 - alpha)) + S[sb], 255);
423             }
424             if (main_has_alpha) {
425                 switch (alpha) {
426                 case 0:
427                     break;
428                 case 255:
429                     d[da] = S[sa];
430                     break;
431                 default:
432                     // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
433                     d[da] += FAST_DIV255((255 - d[da]) * S[sa]);
434                 }
435             }
436             d += dstep;
437             S += sstep;
438         }
439         dp += dst->linesize[0];
440         sp += src->linesize[0];
441     }
442 }
443
444 #define DEFINE_BLEND_PLANE(depth, nbits)                                                                   \
445 static av_always_inline void blend_plane_##depth##_##nbits##bits(AVFilterContext *ctx,                     \
446                                          AVFrame *dst, const AVFrame *src,                                 \
447                                          int src_w, int src_h,                                             \
448                                          int dst_w, int dst_h,                                             \
449                                          int i, int hsub, int vsub,                                        \
450                                          int x, int y,                                                     \
451                                          int main_has_alpha,                                               \
452                                          int dst_plane,                                                    \
453                                          int dst_offset,                                                   \
454                                          int dst_step,                                                     \
455                                          int straight,                                                     \
456                                          int yuv,                                                          \
457                                          int jobnr,                                                        \
458                                          int nb_jobs)                                                      \
459 {                                                                                                          \
460     OverlayContext *octx = ctx->priv;                                                                      \
461     int src_wp = AV_CEIL_RSHIFT(src_w, hsub);                                                              \
462     int src_hp = AV_CEIL_RSHIFT(src_h, vsub);                                                              \
463     int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub);                                                              \
464     int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub);                                                              \
465     int yp = y>>vsub;                                                                                      \
466     int xp = x>>hsub;                                                                                      \
467     uint##depth##_t *s, *sp, *d, *dp, *dap, *a, *da, *ap;                                                  \
468     int jmax, j, k, kmax;                                                                                  \
469     int slice_start, slice_end;                                                                            \
470     const uint##depth##_t max = (1 << nbits) - 1;                                                          \
471     const uint##depth##_t mid = (1 << (nbits -1)) ;                                                        \
472     int bytes = depth / 8;                                                                                 \
473                                                                                                            \
474     dst_step /= bytes;                                                                                     \
475     j = FFMAX(-yp, 0);                                                                                     \
476     jmax = FFMIN3(-yp + dst_hp, FFMIN(src_hp, dst_hp), yp + src_hp);                                       \
477                                                                                                            \
478     slice_start = j + (jmax * jobnr) / nb_jobs;                                                            \
479     slice_end = j + (jmax * (jobnr+1)) / nb_jobs;                                                          \
480                                                                                                            \
481     sp = (uint##depth##_t *)(src->data[i] + (slice_start) * src->linesize[i]);                             \
482     dp = (uint##depth##_t *)(dst->data[dst_plane]                                                          \
483                       + (yp + slice_start) * dst->linesize[dst_plane]                                      \
484                       + dst_offset);                                                                       \
485     ap = (uint##depth##_t *)(src->data[3] + (slice_start << vsub) * src->linesize[3]);                     \
486     dap = (uint##depth##_t *)(dst->data[3] + ((yp + slice_start) << vsub) * dst->linesize[3]);             \
487                                                                                                            \
488     for (j = slice_start; j < slice_end; j++) {                                                            \
489         k = FFMAX(-xp, 0);                                                                                 \
490         d = dp + (xp+k) * dst_step;                                                                        \
491         s = sp + k;                                                                                        \
492         a = ap + (k<<hsub);                                                                                \
493         da = dap + ((xp+k) << hsub);                                                                       \
494         kmax = FFMIN(-xp + dst_wp, src_wp);                                                                \
495                                                                                                            \
496         if (nbits == 8 && ((vsub && j+1 < src_hp) || !vsub) && octx->blend_row[i]) {                       \
497             int c = octx->blend_row[i]((uint8_t*)d, (uint8_t*)da, (uint8_t*)s,                             \
498                     (uint8_t*)a, kmax - k, src->linesize[3]);                                              \
499                                                                                                            \
500             s += c;                                                                                        \
501             d += dst_step * c;                                                                             \
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                 alpha = (a[0] + a[src->linesize[3]] +                                                      \
512                          a[1] + a[src->linesize[3]+1]) >> 2;                                               \
513             } else if (hsub || vsub) {                                                                     \
514                 alpha_h = hsub && k+1 < src_wp ?                                                           \
515                     (a[0] + a[1]) >> 1 : a[0];                                                             \
516                 alpha_v = vsub && j+1 < src_hp ?                                                           \
517                     (a[0] + a[src->linesize[3]]) >> 1 : a[0];                                              \
518                 alpha = (alpha_v + alpha_h) >> 1;                                                          \
519             } else                                                                                         \
520                 alpha = a[0];                                                                              \
521             /* if the main channel has an alpha channel, alpha has to be calculated */                     \
522             /* to create an un-premultiplied (straight) alpha value */                                     \
523             if (main_has_alpha && alpha != 0 && alpha != max) {                                            \
524                 /* average alpha for color components, improve quality */                                  \
525                 uint8_t alpha_d;                                                                           \
526                 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {                                        \
527                     alpha_d = (da[0] + da[dst->linesize[3]] +                                              \
528                                da[1] + da[dst->linesize[3]+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] + 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             da += 1 << hsub;                                                                               \
560             a += 1 << hsub;                                                                                \
561         }                                                                                                  \
562         dp += dst->linesize[dst_plane] / bytes;                                                            \
563         sp += src->linesize[i] / bytes;                                                                    \
564         ap += (1 << vsub) * src->linesize[3] / bytes;                                                      \
565         dap += (1 << vsub) * dst->linesize[3] / bytes;                                                     \
566     }                                                                                                      \
567 }
568 DEFINE_BLEND_PLANE(8, 8);
569
570 #define DEFINE_ALPHA_COMPOSITE(depth, nbits)                                                               \
571 static inline void alpha_composite_##depth##_##nbits##bits(const AVFrame *src, const AVFrame *dst,         \
572                                    int src_w, int src_h,                                                   \
573                                    int dst_w, int dst_h,                                                   \
574                                    int x, int y,                                                           \
575                                    int jobnr, int nb_jobs)                                                 \
576 {                                                                                                          \
577     uint##depth##_t alpha;          /* the amount of overlay to blend on to main */                        \
578     uint##depth##_t *s, *sa, *d, *da;                                                                      \
579     int i, imax, j, jmax;                                                                                  \
580     int slice_start, slice_end;                                                                            \
581     const uint##depth##_t max = (1 << nbits) - 1;                                                          \
582     int bytes = depth / 8;                                                                                 \
583                                                                                                            \
584     imax = FFMIN(-y + dst_h, src_h);                                                                       \
585     slice_start = (imax * jobnr) / nb_jobs;                                                                \
586     slice_end = ((imax * (jobnr+1)) / nb_jobs);                                                            \
587                                                                                                            \
588     i = FFMAX(-y, 0);                                                                                      \
589     sa = (uint##depth##_t *)(src->data[3] + (i + slice_start) * src->linesize[3]);                         \
590     da = (uint##depth##_t *)(dst->data[3] + (y + i + slice_start) * dst->linesize[3]);                     \
591                                                                                                            \
592     for (i = i + slice_start; i < slice_end; i++) {                                                        \
593         j = FFMAX(-x, 0);                                                                                  \
594         s = sa + j;                                                                                        \
595         d = da + x+j;                                                                                      \
596                                                                                                            \
597         for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {                                             \
598             alpha = *s;                                                                                    \
599             if (alpha != 0 && alpha != max) {                                                              \
600                 uint8_t alpha_d = *d;                                                                      \
601                 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);                                               \
602             }                                                                                              \
603             if (alpha == max)                                                                              \
604                 *d = *s;                                                                                   \
605             else if (alpha > 0) {                                                                          \
606                 /* apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha */                \
607                 if (nbits > 8)                                                                             \
608                     *d += (max - *d) * *s / max;                                                           \
609                 else                                                                                       \
610                     *d += FAST_DIV255((max - *d) * *s);                                                    \
611             }                                                                                              \
612             d += 1;                                                                                        \
613             s += 1;                                                                                        \
614         }                                                                                                  \
615         da += dst->linesize[3] / bytes;                                                                    \
616         sa += src->linesize[3] / bytes;                                                                    \
617     }                                                                                                      \
618 }
619 DEFINE_ALPHA_COMPOSITE(8, 8);
620
621 #define DEFINE_BLEND_SLICE_YUV(depth, nbits)                                                               \
622 static av_always_inline void blend_slice_yuv_##depth##_##nbits##bits(AVFilterContext *ctx,                 \
623                                              AVFrame *dst, const AVFrame *src,                             \
624                                              int hsub, int vsub,                                           \
625                                              int main_has_alpha,                                           \
626                                              int x, int y,                                                 \
627                                              int is_straight,                                              \
628                                              int jobnr, int nb_jobs)                                       \
629 {                                                                                                          \
630     OverlayContext *s = ctx->priv;                                                                         \
631     const int src_w = src->width;                                                                          \
632     const int src_h = src->height;                                                                         \
633     const int dst_w = dst->width;                                                                          \
634     const int dst_h = dst->height;                                                                         \
635                                                                                                            \
636     blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0,       0,          \
637                 x, y, main_has_alpha, s->main_desc->comp[0].plane, s->main_desc->comp[0].offset,           \
638                 s->main_desc->comp[0].step, is_straight, 1, jobnr, nb_jobs);                               \
639     blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub,          \
640                 x, y, main_has_alpha, s->main_desc->comp[1].plane, s->main_desc->comp[1].offset,           \
641                 s->main_desc->comp[1].step, is_straight, 1, jobnr, nb_jobs);                               \
642     blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub,          \
643                 x, y, main_has_alpha, s->main_desc->comp[2].plane, s->main_desc->comp[2].offset,           \
644                 s->main_desc->comp[2].step, is_straight, 1, jobnr, nb_jobs);                               \
645                                                                                                            \
646     if (main_has_alpha)                                                                                    \
647         alpha_composite_##depth##_##nbits##bits(src, dst, src_w, src_h, dst_w, dst_h, x, y,                \
648                                                 jobnr, nb_jobs);                                           \
649 }
650 DEFINE_BLEND_SLICE_YUV(8, 8);
651
652 static av_always_inline void blend_slice_planar_rgb(AVFilterContext *ctx,
653                                                     AVFrame *dst, const AVFrame *src,
654                                                     int hsub, int vsub,
655                                                     int main_has_alpha,
656                                                     int x, int y,
657                                                     int is_straight,
658                                                     int jobnr,
659                                                     int nb_jobs)
660 {
661     OverlayContext *s = ctx->priv;
662     const int src_w = src->width;
663     const int src_h = src->height;
664     const int dst_w = dst->width;
665     const int dst_h = dst->height;
666
667     blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0,   0, x, y, main_has_alpha,
668                 s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, is_straight, 0,
669                 jobnr, nb_jobs);
670     blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha,
671                 s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, is_straight, 0,
672                 jobnr, nb_jobs);
673     blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha,
674                 s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, is_straight, 0,
675                 jobnr, nb_jobs);
676
677     if (main_has_alpha)
678         alpha_composite_8_8bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, jobnr, nb_jobs);
679 }
680
681 static int blend_slice_yuv420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
682 {
683     OverlayContext *s = ctx->priv;
684     ThreadData *td = arg;
685     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 1, jobnr, nb_jobs);
686     return 0;
687 }
688
689 static int blend_slice_yuva420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
690 {
691     OverlayContext *s = ctx->priv;
692     ThreadData *td = arg;
693     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 1, jobnr, nb_jobs);
694     return 0;
695 }
696
697 static int blend_slice_yuv422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
698 {
699     OverlayContext *s = ctx->priv;
700     ThreadData *td = arg;
701     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
702     return 0;
703 }
704
705 static int blend_slice_yuva422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
706 {
707     OverlayContext *s = ctx->priv;
708     ThreadData *td = arg;
709     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
710     return 0;
711 }
712
713 static int blend_slice_yuv444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
714 {
715     OverlayContext *s = ctx->priv;
716     ThreadData *td = arg;
717     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
718     return 0;
719 }
720
721 static int blend_slice_yuva444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
722 {
723     OverlayContext *s = ctx->priv;
724     ThreadData *td = arg;
725     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
726     return 0;
727 }
728
729 static int blend_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
730 {
731     OverlayContext *s = ctx->priv;
732     ThreadData *td = arg;
733     blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
734     return 0;
735 }
736
737 static int blend_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
738 {
739     OverlayContext *s = ctx->priv;
740     ThreadData *td = arg;
741     blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
742     return 0;
743 }
744
745 static int blend_slice_yuv420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
746 {
747     OverlayContext *s = ctx->priv;
748     ThreadData *td = arg;
749     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 0, jobnr, nb_jobs);
750     return 0;
751 }
752
753 static int blend_slice_yuva420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
754 {
755     OverlayContext *s = ctx->priv;
756     ThreadData *td = arg;
757     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 0, jobnr, nb_jobs);
758     return 0;
759 }
760
761 static int blend_slice_yuv422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
762 {
763     OverlayContext *s = ctx->priv;
764     ThreadData *td = arg;
765     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
766     return 0;
767 }
768
769 static int blend_slice_yuva422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
770 {
771     OverlayContext *s = ctx->priv;
772     ThreadData *td = arg;
773     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
774     return 0;
775 }
776
777 static int blend_slice_yuv444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
778 {
779     OverlayContext *s = ctx->priv;
780     ThreadData *td = arg;
781     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
782     return 0;
783 }
784
785 static int blend_slice_yuva444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
786 {
787     OverlayContext *s = ctx->priv;
788     ThreadData *td = arg;
789     blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
790     return 0;
791 }
792
793 static int blend_slice_gbrp_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
794 {
795     OverlayContext *s = ctx->priv;
796     ThreadData *td = arg;
797     blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
798     return 0;
799 }
800
801 static int blend_slice_gbrap_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
802 {
803     OverlayContext *s = ctx->priv;
804     ThreadData *td = arg;
805     blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
806     return 0;
807 }
808
809 static int blend_slice_rgb(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
810 {
811     OverlayContext *s = ctx->priv;
812     ThreadData *td = arg;
813     blend_slice_packed_rgb(ctx, td->dst, td->src, 0, s->x, s->y, 1, jobnr, nb_jobs);
814     return 0;
815 }
816
817 static int blend_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
818 {
819     OverlayContext *s = ctx->priv;
820     ThreadData *td = arg;
821     blend_slice_packed_rgb(ctx, td->dst, td->src, 1, s->x, s->y, 1, jobnr, nb_jobs);
822     return 0;
823 }
824
825 static int blend_slice_rgb_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
826 {
827     OverlayContext *s = ctx->priv;
828     ThreadData *td = arg;
829     blend_slice_packed_rgb(ctx, td->dst, td->src, 0, s->x, s->y, 0, jobnr, nb_jobs);
830     return 0;
831 }
832
833 static int blend_slice_rgba_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
834 {
835     OverlayContext *s = ctx->priv;
836     ThreadData *td = arg;
837     blend_slice_packed_rgb(ctx, td->dst, td->src, 1, s->x, s->y, 0, jobnr, nb_jobs);
838     return 0;
839 }
840
841 static int config_input_main(AVFilterLink *inlink)
842 {
843     OverlayContext *s = inlink->dst->priv;
844     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
845
846     av_image_fill_max_pixsteps(s->main_pix_step,    NULL, pix_desc);
847
848     s->hsub = pix_desc->log2_chroma_w;
849     s->vsub = pix_desc->log2_chroma_h;
850
851     s->main_desc = pix_desc;
852
853     s->main_is_packed_rgb =
854         ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
855     s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
856     switch (s->format) {
857     case OVERLAY_FORMAT_YUV420:
858         s->blend_slice = s->main_has_alpha ? blend_slice_yuva420 : blend_slice_yuv420;
859         break;
860     case OVERLAY_FORMAT_YUV422:
861         s->blend_slice = s->main_has_alpha ? blend_slice_yuva422 : blend_slice_yuv422;
862         break;
863     case OVERLAY_FORMAT_YUV444:
864         s->blend_slice = s->main_has_alpha ? blend_slice_yuva444 : blend_slice_yuv444;
865         break;
866     case OVERLAY_FORMAT_RGB:
867         s->blend_slice = s->main_has_alpha ? blend_slice_rgba : blend_slice_rgb;
868         break;
869     case OVERLAY_FORMAT_GBRP:
870         s->blend_slice = s->main_has_alpha ? blend_slice_gbrap : blend_slice_gbrp;
871         break;
872     case OVERLAY_FORMAT_AUTO:
873         switch (inlink->format) {
874         case AV_PIX_FMT_YUVA420P:
875             s->blend_slice = blend_slice_yuva420;
876             break;
877         case AV_PIX_FMT_YUVA422P:
878             s->blend_slice = blend_slice_yuva422;
879             break;
880         case AV_PIX_FMT_YUVA444P:
881             s->blend_slice = blend_slice_yuva444;
882             break;
883         case AV_PIX_FMT_ARGB:
884         case AV_PIX_FMT_RGBA:
885         case AV_PIX_FMT_BGRA:
886         case AV_PIX_FMT_ABGR:
887             s->blend_slice = blend_slice_rgba;
888             break;
889         case AV_PIX_FMT_GBRAP:
890             s->blend_slice = blend_slice_gbrap;
891             break;
892         default:
893             av_assert0(0);
894             break;
895         }
896         break;
897     }
898
899     if (!s->alpha_format)
900         goto end;
901
902     switch (s->format) {
903     case OVERLAY_FORMAT_YUV420:
904         s->blend_slice = s->main_has_alpha ? blend_slice_yuva420_pm : blend_slice_yuv420_pm;
905         break;
906     case OVERLAY_FORMAT_YUV422:
907         s->blend_slice = s->main_has_alpha ? blend_slice_yuva422_pm : blend_slice_yuv422_pm;
908         break;
909     case OVERLAY_FORMAT_YUV444:
910         s->blend_slice = s->main_has_alpha ? blend_slice_yuva444_pm : blend_slice_yuv444_pm;
911         break;
912     case OVERLAY_FORMAT_RGB:
913         s->blend_slice = s->main_has_alpha ? blend_slice_rgba_pm : blend_slice_rgb_pm;
914         break;
915     case OVERLAY_FORMAT_GBRP:
916         s->blend_slice = s->main_has_alpha ? blend_slice_gbrap_pm : blend_slice_gbrp_pm;
917         break;
918     case OVERLAY_FORMAT_AUTO:
919         switch (inlink->format) {
920         case AV_PIX_FMT_YUVA420P:
921             s->blend_slice = blend_slice_yuva420_pm;
922             break;
923         case AV_PIX_FMT_YUVA422P:
924             s->blend_slice = blend_slice_yuva422_pm;
925             break;
926         case AV_PIX_FMT_YUVA444P:
927             s->blend_slice = blend_slice_yuva444_pm;
928             break;
929         case AV_PIX_FMT_ARGB:
930         case AV_PIX_FMT_RGBA:
931         case AV_PIX_FMT_BGRA:
932         case AV_PIX_FMT_ABGR:
933             s->blend_slice = blend_slice_rgba_pm;
934             break;
935         case AV_PIX_FMT_GBRAP:
936             s->blend_slice = blend_slice_gbrap_pm;
937             break;
938         default:
939             av_assert0(0);
940             break;
941         }
942         break;
943     }
944
945 end:
946     if (ARCH_X86)
947         ff_overlay_init_x86(s, s->format, inlink->format,
948                             s->alpha_format, s->main_has_alpha);
949
950     return 0;
951 }
952
953 static int do_blend(FFFrameSync *fs)
954 {
955     AVFilterContext *ctx = fs->parent;
956     AVFrame *mainpic, *second;
957     OverlayContext *s = ctx->priv;
958     AVFilterLink *inlink = ctx->inputs[0];
959     int ret;
960
961     ret = ff_framesync_dualinput_get_writable(fs, &mainpic, &second);
962     if (ret < 0)
963         return ret;
964     if (!second)
965         return ff_filter_frame(ctx->outputs[0], mainpic);
966
967     if (s->eval_mode == EVAL_MODE_FRAME) {
968         int64_t pos = mainpic->pkt_pos;
969
970         s->var_values[VAR_N] = inlink->frame_count_out;
971         s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
972             NAN : mainpic->pts * av_q2d(inlink->time_base);
973         s->var_values[VAR_POS] = pos == -1 ? NAN : pos;
974
975         s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = second->width;
976         s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = second->height;
977         s->var_values[VAR_MAIN_W   ] = s->var_values[VAR_MW] = mainpic->width;
978         s->var_values[VAR_MAIN_H   ] = s->var_values[VAR_MH] = mainpic->height;
979
980         eval_expr(ctx);
981         av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
982                s->var_values[VAR_N], s->var_values[VAR_T], s->var_values[VAR_POS],
983                s->var_values[VAR_X], s->x,
984                s->var_values[VAR_Y], s->y);
985     }
986
987     if (s->x < mainpic->width  && s->x + second->width  >= 0 &&
988         s->y < mainpic->height && s->y + second->height >= 0) {
989         ThreadData td;
990
991         td.dst = mainpic;
992         td.src = second;
993         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)),
994                                                                      ff_filter_get_nb_threads(ctx)));
995     }
996     return ff_filter_frame(ctx->outputs[0], mainpic);
997 }
998
999 static av_cold int init(AVFilterContext *ctx)
1000 {
1001     OverlayContext *s = ctx->priv;
1002
1003     s->fs.on_event = do_blend;
1004     return 0;
1005 }
1006
1007 static int activate(AVFilterContext *ctx)
1008 {
1009     OverlayContext *s = ctx->priv;
1010     return ff_framesync_activate(&s->fs);
1011 }
1012
1013 #define OFFSET(x) offsetof(OverlayContext, x)
1014 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1015
1016 static const AVOption overlay_options[] = {
1017     { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS },
1018     { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS },
1019     { "eof_action", "Action to take when encountering EOF from secondary input ",
1020         OFFSET(fs.opt_eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
1021         EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, "eof_action" },
1022         { "repeat", "Repeat the previous frame.",   0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, "eof_action" },
1023         { "endall", "End both streams.",            0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, "eof_action" },
1024         { "pass",   "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS },   .flags = FLAGS, "eof_action" },
1025     { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
1026          { "init",  "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT},  .flags = FLAGS, .unit = "eval" },
1027          { "frame", "eval expressions per-frame",                  0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
1028     { "shortest", "force termination when the shortest input terminates", OFFSET(fs.opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1029     { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
1030         { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
1031         { "yuv422", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422}, .flags = FLAGS, .unit = "format" },
1032         { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
1033         { "rgb",    "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB},    .flags = FLAGS, .unit = "format" },
1034         { "gbrp",   "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_GBRP},   .flags = FLAGS, .unit = "format" },
1035         { "auto",   "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_AUTO},   .flags = FLAGS, .unit = "format" },
1036     { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(fs.opt_repeatlast), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
1037     { "alpha", "alpha format", OFFSET(alpha_format), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "alpha_format" },
1038         { "straight",      "", 0, AV_OPT_TYPE_CONST, {.i64=0}, .flags = FLAGS, .unit = "alpha_format" },
1039         { "premultiplied", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, .flags = FLAGS, .unit = "alpha_format" },
1040     { NULL }
1041 };
1042
1043 FRAMESYNC_DEFINE_CLASS(overlay, OverlayContext, fs);
1044
1045 static const AVFilterPad avfilter_vf_overlay_inputs[] = {
1046     {
1047         .name         = "main",
1048         .type         = AVMEDIA_TYPE_VIDEO,
1049         .config_props = config_input_main,
1050     },
1051     {
1052         .name         = "overlay",
1053         .type         = AVMEDIA_TYPE_VIDEO,
1054         .config_props = config_input_overlay,
1055     },
1056     { NULL }
1057 };
1058
1059 static const AVFilterPad avfilter_vf_overlay_outputs[] = {
1060     {
1061         .name          = "default",
1062         .type          = AVMEDIA_TYPE_VIDEO,
1063         .config_props  = config_output,
1064     },
1065     { NULL }
1066 };
1067
1068 AVFilter ff_vf_overlay = {
1069     .name          = "overlay",
1070     .description   = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
1071     .preinit       = overlay_framesync_preinit,
1072     .init          = init,
1073     .uninit        = uninit,
1074     .priv_size     = sizeof(OverlayContext),
1075     .priv_class    = &overlay_class,
1076     .query_formats = query_formats,
1077     .activate      = activate,
1078     .process_command = process_command,
1079     .inputs        = avfilter_vf_overlay_inputs,
1080     .outputs       = avfilter_vf_overlay_outputs,
1081     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
1082                      AVFILTER_FLAG_SLICE_THREADS,
1083 };