]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_blend.c
gxf: Factorize code in get_sindex()
[ffmpeg] / libavfilter / vf_blend.c
1 /*
2  * Copyright (c) 2013 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/imgutils.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixfmt.h"
25 #include "avfilter.h"
26 #include "bufferqueue.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30
31 #define TOP    0
32 #define BOTTOM 1
33
34 enum BlendMode {
35     BLEND_UNSET = -1,
36     BLEND_NORMAL,
37     BLEND_ADDITION,
38     BLEND_AND,
39     BLEND_AVERAGE,
40     BLEND_BURN,
41     BLEND_DARKEN,
42     BLEND_DIFFERENCE,
43     BLEND_DIVIDE,
44     BLEND_DODGE,
45     BLEND_EXCLUSION,
46     BLEND_HARDLIGHT,
47     BLEND_LIGHTEN,
48     BLEND_MULTIPLY,
49     BLEND_NEGATION,
50     BLEND_OR,
51     BLEND_OVERLAY,
52     BLEND_PHOENIX,
53     BLEND_PINLIGHT,
54     BLEND_REFLECT,
55     BLEND_SCREEN,
56     BLEND_SOFTLIGHT,
57     BLEND_SUBTRACT,
58     BLEND_VIVIDLIGHT,
59     BLEND_XOR,
60     BLEND_NB
61 };
62
63 static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "SW",   "SH",   "T",   "N",   "A",   "B",   "TOP",   "BOTTOM",        NULL };
64 enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_SW, VAR_SH, VAR_T, VAR_N, VAR_A, VAR_B, VAR_TOP, VAR_BOTTOM, VAR_VARS_NB };
65
66 typedef struct FilterParams {
67     enum BlendMode mode;
68     double opacity;
69     AVExpr *e;
70     char *expr_str;
71     void (*blend)(const uint8_t *top, int top_linesize,
72                   const uint8_t *bottom, int bottom_linesize,
73                   uint8_t *dst, int dst_linesize,
74                   int width, int start, int end,
75                   struct FilterParams *param, double *values);
76 } FilterParams;
77
78 typedef struct ThreadData {
79     AVFrame *top, *bottom, *dst;
80     AVFilterLink *inlink;
81     int plane;
82     int w, h;
83     FilterParams *param;
84 } ThreadData;
85
86 typedef struct {
87     const AVClass *class;
88     struct FFBufQueue queue_top;
89     struct FFBufQueue queue_bottom;
90     int hsub, vsub;             ///< chroma subsampling values
91     int nb_planes;
92     int frame_requested;
93     char *all_expr;
94     enum BlendMode all_mode;
95     double all_opacity;
96
97     FilterParams params[4];
98 } BlendContext;
99
100 #define OFFSET(x) offsetof(BlendContext, x)
101 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
102
103 static const AVOption blend_options[] = {
104     { "c0_mode", "set component #0 blend mode", OFFSET(params[0].mode), AV_OPT_TYPE_INT, {.i64=0}, 0, BLEND_NB-1, FLAGS, "mode"},
105     { "c1_mode", "set component #1 blend mode", OFFSET(params[1].mode), AV_OPT_TYPE_INT, {.i64=0}, 0, BLEND_NB-1, FLAGS, "mode"},
106     { "c2_mode", "set component #2 blend mode", OFFSET(params[2].mode), AV_OPT_TYPE_INT, {.i64=0}, 0, BLEND_NB-1, FLAGS, "mode"},
107     { "c3_mode", "set component #3 blend mode", OFFSET(params[3].mode), AV_OPT_TYPE_INT, {.i64=0}, 0, BLEND_NB-1, FLAGS, "mode"},
108     { "all_mode", "set blend mode for all components", OFFSET(all_mode), AV_OPT_TYPE_INT, {.i64=-1},-1, BLEND_NB-1, FLAGS, "mode"},
109     { "addition",   "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_ADDITION},   0, 0, FLAGS, "mode" },
110     { "and",        "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_AND},        0, 0, FLAGS, "mode" },
111     { "average",    "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_AVERAGE},    0, 0, FLAGS, "mode" },
112     { "burn",       "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_BURN},       0, 0, FLAGS, "mode" },
113     { "darken",     "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_DARKEN},     0, 0, FLAGS, "mode" },
114     { "difference", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_DIFFERENCE}, 0, 0, FLAGS, "mode" },
115     { "divide",     "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_DIVIDE},     0, 0, FLAGS, "mode" },
116     { "dodge",      "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_DODGE},      0, 0, FLAGS, "mode" },
117     { "exclusion",  "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_EXCLUSION},  0, 0, FLAGS, "mode" },
118     { "hardlight",  "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_HARDLIGHT},  0, 0, FLAGS, "mode" },
119     { "lighten",    "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_LIGHTEN},    0, 0, FLAGS, "mode" },
120     { "multiply",   "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_MULTIPLY},   0, 0, FLAGS, "mode" },
121     { "negation",   "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_NEGATION},   0, 0, FLAGS, "mode" },
122     { "normal",     "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_NORMAL},     0, 0, FLAGS, "mode" },
123     { "or",         "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_OR},         0, 0, FLAGS, "mode" },
124     { "overlay",    "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_OVERLAY},    0, 0, FLAGS, "mode" },
125     { "phoenix",    "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_PHOENIX},    0, 0, FLAGS, "mode" },
126     { "pinlight",   "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_PINLIGHT},   0, 0, FLAGS, "mode" },
127     { "reflect",    "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_REFLECT},    0, 0, FLAGS, "mode" },
128     { "screen",     "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_SCREEN},     0, 0, FLAGS, "mode" },
129     { "softlight",  "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_SOFTLIGHT},  0, 0, FLAGS, "mode" },
130     { "subtract",   "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_SUBTRACT},   0, 0, FLAGS, "mode" },
131     { "vividlight", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_VIVIDLIGHT}, 0, 0, FLAGS, "mode" },
132     { "xor",        "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_XOR},        0, 0, FLAGS, "mode" },
133     { "c0_expr",  "set color component #0 expression", OFFSET(params[0].expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
134     { "c1_expr",  "set color component #1 expression", OFFSET(params[1].expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
135     { "c2_expr",  "set color component #2 expression", OFFSET(params[2].expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
136     { "c3_expr",  "set color component #3 expression", OFFSET(params[3].expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
137     { "all_expr", "set expression for all color components", OFFSET(all_expr), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
138     { "c0_opacity",  "set color component #0 opacity", OFFSET(params[0].opacity), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
139     { "c1_opacity",  "set color component #1 opacity", OFFSET(params[1].opacity), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
140     { "c2_opacity",  "set color component #2 opacity", OFFSET(params[2].opacity), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
141     { "c3_opacity",  "set color component #3 opacity", OFFSET(params[3].opacity), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
142     { "all_opacity", "set opacity for all color components", OFFSET(all_opacity), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS},
143     { NULL },
144 };
145
146 AVFILTER_DEFINE_CLASS(blend);
147
148 static void blend_normal(const uint8_t *top, int top_linesize,
149                          const uint8_t *bottom, int bottom_linesize,
150                          uint8_t *dst, int dst_linesize,
151                          int width, int start, int end,
152                          FilterParams *param, double *values)
153 {
154     av_image_copy_plane(dst, dst_linesize, top, top_linesize, width, end - start);
155 }
156
157 #define DEFINE_BLEND(name, expr)                                      \
158 static void blend_## name(const uint8_t *top, int top_linesize,       \
159                           const uint8_t *bottom, int bottom_linesize, \
160                           uint8_t *dst, int dst_linesize,             \
161                           int width, int start, int end,              \
162                           FilterParams *param, double *values)        \
163 {                                                                     \
164     double opacity = param->opacity;                                  \
165     int i, j;                                                         \
166                                                                       \
167     for (i = start; i < end; i++) {                                   \
168         for (j = 0; j < width; j++) {                                 \
169             dst[j] = top[j] + ((expr) - top[j]) * opacity;            \
170         }                                                             \
171         dst    += dst_linesize;                                       \
172         top    += top_linesize;                                       \
173         bottom += bottom_linesize;                                    \
174     }                                                                 \
175 }
176
177 #define A top[j]
178 #define B bottom[j]
179
180 #define MULTIPLY(x, a, b) (x * ((a * b) / 255))
181 #define SCREEN(x, a, b)   (255 - x * ((255 - a) * (255 - b) / 255))
182 #define BURN(a, b)        ((a == 0) ? a : FFMAX(0, 255 - ((255 - b) << 8) / a))
183 #define DODGE(a, b)       ((a == 255) ? a : FFMIN(255, ((b << 8) / (255 - a))))
184
185 DEFINE_BLEND(addition,   FFMIN(255, A + B))
186 DEFINE_BLEND(average,    (A + B) / 2)
187 DEFINE_BLEND(subtract,   FFMAX(0, A - B))
188 DEFINE_BLEND(multiply,   MULTIPLY(1, A, B))
189 DEFINE_BLEND(negation,   255 - FFABS(255 - A - B))
190 DEFINE_BLEND(difference, FFABS(A - B))
191 DEFINE_BLEND(screen,     SCREEN(1, A, B))
192 DEFINE_BLEND(overlay,    (A < 128) ? MULTIPLY(2, A, B) : SCREEN(2, A, B))
193 DEFINE_BLEND(hardlight,  (B < 128) ? MULTIPLY(2, B, A) : SCREEN(2, B, A))
194 DEFINE_BLEND(darken,     FFMIN(A, B))
195 DEFINE_BLEND(lighten,    FFMAX(A, B))
196 DEFINE_BLEND(divide,     ((float)A / ((float)B) * 255))
197 DEFINE_BLEND(dodge,      DODGE(A, B))
198 DEFINE_BLEND(burn,       BURN(A, B))
199 DEFINE_BLEND(softlight,  (A > 127) ? B + (255 - B) * (A - 127.5) / 127.5 * (0.5 - FFABS(B - 127.5) / 255): B - B * ((127.5 - A) / 127.5) * (0.5 - FFABS(B - 127.5)/255))
200 DEFINE_BLEND(exclusion,  A + B - 2 * A * B / 255)
201 DEFINE_BLEND(pinlight,   (B < 128) ? FFMIN(A, 2 * B) : FFMAX(A, 2 * (B - 128)))
202 DEFINE_BLEND(phoenix,    FFMIN(A, B) - FFMAX(A, B) + 255)
203 DEFINE_BLEND(reflect,    (B == 255) ? B : FFMIN(255, (A * A / (255 - B))))
204 DEFINE_BLEND(and,        A & B)
205 DEFINE_BLEND(or,         A | B)
206 DEFINE_BLEND(xor,        A ^ B)
207 DEFINE_BLEND(vividlight, (B < 128) ? BURN(A, 2 * B) : DODGE(A, 2 * (B - 128)))
208
209 static void blend_expr(const uint8_t *top, int top_linesize,
210                        const uint8_t *bottom, int bottom_linesize,
211                        uint8_t *dst, int dst_linesize,
212                        int width, int start, int end,
213                        FilterParams *param, double *values)
214 {
215     AVExpr *e = param->e;
216     int y, x;
217
218     for (y = start; y < end; y++) {
219         values[VAR_Y] = y;
220         for (x = 0; x < width; x++) {
221             values[VAR_X]      = x;
222             values[VAR_TOP]    = values[VAR_A] = top[x];
223             values[VAR_BOTTOM] = values[VAR_B] = bottom[x];
224             dst[x] = av_expr_eval(e, values, NULL);
225         }
226         dst    += dst_linesize;
227         top    += top_linesize;
228         bottom += bottom_linesize;
229     }
230 }
231
232 static av_cold int init(AVFilterContext *ctx)
233 {
234     BlendContext *b = ctx->priv;
235     int ret, plane;
236
237     for (plane = 0; plane < FF_ARRAY_ELEMS(b->params); plane++) {
238         FilterParams *param = &b->params[plane];
239
240         if (b->all_mode >= 0)
241             param->mode = b->all_mode;
242         if (b->all_opacity < 1)
243             param->opacity = b->all_opacity;
244
245         switch (param->mode) {
246         case BLEND_ADDITION:   param->blend = blend_addition;   break;
247         case BLEND_AND:        param->blend = blend_and;        break;
248         case BLEND_AVERAGE:    param->blend = blend_average;    break;
249         case BLEND_BURN:       param->blend = blend_burn;       break;
250         case BLEND_DARKEN:     param->blend = blend_darken;     break;
251         case BLEND_DIFFERENCE: param->blend = blend_difference; break;
252         case BLEND_DIVIDE:     param->blend = blend_divide;     break;
253         case BLEND_DODGE:      param->blend = blend_dodge;      break;
254         case BLEND_EXCLUSION:  param->blend = blend_exclusion;  break;
255         case BLEND_HARDLIGHT:  param->blend = blend_hardlight;  break;
256         case BLEND_LIGHTEN:    param->blend = blend_lighten;    break;
257         case BLEND_MULTIPLY:   param->blend = blend_multiply;   break;
258         case BLEND_NEGATION:   param->blend = blend_negation;   break;
259         case BLEND_NORMAL:     param->blend = blend_normal;     break;
260         case BLEND_OR:         param->blend = blend_or;         break;
261         case BLEND_OVERLAY:    param->blend = blend_overlay;    break;
262         case BLEND_PHOENIX:    param->blend = blend_phoenix;    break;
263         case BLEND_PINLIGHT:   param->blend = blend_pinlight;   break;
264         case BLEND_REFLECT:    param->blend = blend_reflect;    break;
265         case BLEND_SCREEN:     param->blend = blend_screen;     break;
266         case BLEND_SOFTLIGHT:  param->blend = blend_softlight;  break;
267         case BLEND_SUBTRACT:   param->blend = blend_subtract;   break;
268         case BLEND_VIVIDLIGHT: param->blend = blend_vividlight; break;
269         case BLEND_XOR:        param->blend = blend_xor;        break;
270         }
271
272         if (b->all_expr && !param->expr_str) {
273             param->expr_str = av_strdup(b->all_expr);
274             if (!param->expr_str)
275                 return AVERROR(ENOMEM);
276         }
277         if (param->expr_str) {
278             ret = av_expr_parse(&param->e, param->expr_str, var_names,
279                                 NULL, NULL, NULL, NULL, 0, ctx);
280             if (ret < 0)
281                 return ret;
282             param->blend = blend_expr;
283         }
284     }
285
286     return 0;
287 }
288
289 static int query_formats(AVFilterContext *ctx)
290 {
291     static const enum AVPixelFormat pix_fmts[] = {
292         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
293         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
294         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
295         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
296     };
297
298     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
299     return 0;
300 }
301
302 static int config_output(AVFilterLink *outlink)
303 {
304     AVFilterContext *ctx = outlink->src;
305     AVFilterLink *toplink = ctx->inputs[TOP];
306     AVFilterLink *bottomlink = ctx->inputs[BOTTOM];
307     BlendContext *b = ctx->priv;
308     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(toplink->format);
309
310     if (toplink->format != bottomlink->format) {
311         av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
312         return AVERROR(EINVAL);
313     }
314     if (toplink->w                       != bottomlink->w ||
315         toplink->h                       != bottomlink->h ||
316         toplink->sample_aspect_ratio.num != bottomlink->sample_aspect_ratio.num ||
317         toplink->sample_aspect_ratio.den != bottomlink->sample_aspect_ratio.den) {
318         av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
319                "(size %dx%d, SAR %d:%d) do not match the corresponding "
320                "second input link %s parameters (%dx%d, SAR %d:%d)\n",
321                ctx->input_pads[TOP].name, toplink->w, toplink->h,
322                toplink->sample_aspect_ratio.num,
323                toplink->sample_aspect_ratio.den,
324                ctx->input_pads[BOTTOM].name, bottomlink->w, bottomlink->h,
325                bottomlink->sample_aspect_ratio.num,
326                bottomlink->sample_aspect_ratio.den);
327         return AVERROR(EINVAL);
328     }
329
330     outlink->w = toplink->w;
331     outlink->h = toplink->h;
332     outlink->time_base = toplink->time_base;
333     outlink->sample_aspect_ratio = toplink->sample_aspect_ratio;
334     outlink->frame_rate = toplink->frame_rate;
335
336     b->hsub = pix_desc->log2_chroma_w;
337     b->vsub = pix_desc->log2_chroma_h;
338     b->nb_planes = av_pix_fmt_count_planes(toplink->format);
339
340     return 0;
341 }
342
343 static av_cold void uninit(AVFilterContext *ctx)
344 {
345     BlendContext *b = ctx->priv;
346     int i;
347
348     ff_bufqueue_discard_all(&b->queue_top);
349     ff_bufqueue_discard_all(&b->queue_bottom);
350
351     for (i = 0; i < FF_ARRAY_ELEMS(b->params); i++)
352         av_expr_free(b->params[i].e);
353 }
354
355 static int request_frame(AVFilterLink *outlink)
356 {
357     AVFilterContext *ctx = outlink->src;
358     BlendContext *b = ctx->priv;
359     int in, ret;
360
361     b->frame_requested = 1;
362     while (b->frame_requested) {
363         in = ff_bufqueue_peek(&b->queue_top, 0) ? BOTTOM : TOP;
364         ret = ff_request_frame(ctx->inputs[in]);
365         if (ret < 0)
366             return ret;
367     }
368     return 0;
369 }
370
371 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
372 {
373     ThreadData *td = arg;
374     int slice_start = (td->h *  jobnr   ) / nb_jobs;
375     int slice_end   = (td->h * (jobnr+1)) / nb_jobs;
376     const uint8_t *top    = td->top->data[td->plane];
377     const uint8_t *bottom = td->bottom->data[td->plane];
378     uint8_t *dst    = td->dst->data[td->plane];
379     double values[VAR_VARS_NB];
380
381     values[VAR_N]  = td->inlink->frame_count;
382     values[VAR_T]  = td->dst->pts == AV_NOPTS_VALUE ? NAN : td->dst->pts * av_q2d(td->inlink->time_base);
383     values[VAR_W]  = td->w;
384     values[VAR_H]  = td->h;
385     values[VAR_SW] = td->w / (double)td->dst->width;
386     values[VAR_SH] = td->h / (double)td->dst->height;
387
388     td->param->blend(top + slice_start * td->top->linesize[td->plane],
389                      td->top->linesize[td->plane],
390                      bottom + slice_start * td->bottom->linesize[td->plane],
391                      td->bottom->linesize[td->plane],
392                      dst + slice_start * td->dst->linesize[td->plane],
393                      td->dst->linesize[td->plane],
394                      td->w, slice_start, slice_end, td->param, &values[0]);
395     return 0;
396 }
397
398 static void blend_frame(AVFilterContext *ctx,
399                         AVFrame *top_buf,
400                         AVFrame *bottom_buf,
401                         AVFrame *dst_buf)
402 {
403     BlendContext *b = ctx->priv;
404     AVFilterLink *inlink = ctx->inputs[0];
405     int plane;
406
407     for (plane = 0; plane < b->nb_planes; plane++) {
408         int hsub = plane == 1 || plane == 2 ? b->hsub : 0;
409         int vsub = plane == 1 || plane == 2 ? b->vsub : 0;
410         int outw = FF_CEIL_RSHIFT(dst_buf->width,  hsub);
411         int outh = FF_CEIL_RSHIFT(dst_buf->height, vsub);
412         FilterParams *param = &b->params[plane];
413         ThreadData td = { .top = top_buf, .bottom = bottom_buf, .dst = dst_buf,
414                           .w = outw, .h = outh, .param = param, .plane = plane,
415                           .inlink = inlink };
416
417         ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outh, ctx->graph->nb_threads));
418     }
419 }
420
421 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
422 {
423     AVFilterContext *ctx = inlink->dst;
424     AVFilterLink *outlink = ctx->outputs[0];
425     BlendContext *b = ctx->priv;
426
427     int ret = 0;
428     int is_bottom = (inlink == ctx->inputs[BOTTOM]);
429     struct FFBufQueue *queue =
430         (is_bottom ? &b->queue_bottom : &b->queue_top);
431     ff_bufqueue_add(ctx, queue, buf);
432
433     while (1) {
434         AVFrame *top_buf, *bottom_buf, *out_buf;
435
436         if (!ff_bufqueue_peek(&b->queue_top, 0) ||
437             !ff_bufqueue_peek(&b->queue_bottom, 0)) break;
438
439         top_buf = ff_bufqueue_get(&b->queue_top);
440         bottom_buf = ff_bufqueue_get(&b->queue_bottom);
441
442         if (!ctx->is_disabled) {
443             out_buf = ff_get_video_buffer(outlink, outlink->w, outlink->h);
444             if (!out_buf)
445                 return AVERROR(ENOMEM);
446             av_frame_copy_props(out_buf, top_buf);
447             blend_frame(ctx, top_buf, bottom_buf, out_buf);
448         } else {
449             out_buf = av_frame_clone(top_buf);
450             if (!out_buf)
451                 return AVERROR(ENOMEM);
452         }
453
454         b->frame_requested = 0;
455         ret = ff_filter_frame(outlink, out_buf);
456         av_frame_free(&top_buf);
457         av_frame_free(&bottom_buf);
458     }
459     return ret;
460 }
461
462 static const AVFilterPad blend_inputs[] = {
463     {
464         .name             = "top",
465         .type             = AVMEDIA_TYPE_VIDEO,
466         .filter_frame     = filter_frame,
467     },{
468         .name             = "bottom",
469         .type             = AVMEDIA_TYPE_VIDEO,
470         .filter_frame     = filter_frame,
471     },
472     { NULL }
473 };
474
475 static const AVFilterPad blend_outputs[] = {
476     {
477         .name          = "default",
478         .type          = AVMEDIA_TYPE_VIDEO,
479         .config_props  = config_output,
480         .request_frame = request_frame,
481     },
482     { NULL }
483 };
484
485 AVFilter avfilter_vf_blend = {
486     .name          = "blend",
487     .description   = NULL_IF_CONFIG_SMALL("Blend two video frames into each other."),
488     .init          = init,
489     .uninit        = uninit,
490     .priv_size     = sizeof(BlendContext),
491     .query_formats = query_formats,
492     .inputs        = blend_inputs,
493     .outputs       = blend_outputs,
494     .priv_class    = &blend_class,
495     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
496 };