]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_overlay.c
lavfi: move color filter to testsrc, factorize
[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 /* #define DEBUG */
29
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "libavutil/eval.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/timestamp.h"
39 #include "internal.h"
40 #include "bufferqueue.h"
41 #include "drawutils.h"
42 #include "video.h"
43
44 static const char *const var_names[] = {
45     "main_w",    "W", ///< width  of the main    video
46     "main_h",    "H", ///< height of the main    video
47     "overlay_w", "w", ///< width  of the overlay video
48     "overlay_h", "h", ///< height of the overlay video
49     NULL
50 };
51
52 enum var_name {
53     VAR_MAIN_W,    VAR_MW,
54     VAR_MAIN_H,    VAR_MH,
55     VAR_OVERLAY_W, VAR_OW,
56     VAR_OVERLAY_H, VAR_OH,
57     VAR_VARS_NB
58 };
59
60 #define MAIN    0
61 #define OVERLAY 1
62
63 #define R 0
64 #define G 1
65 #define B 2
66 #define A 3
67
68 #define Y 0
69 #define U 1
70 #define V 2
71
72 typedef struct {
73     const AVClass *class;
74     int x, y;                   ///< position of overlayed picture
75
76     int allow_packed_rgb;
77     uint8_t frame_requested;
78     uint8_t overlay_eof;
79     uint8_t main_is_packed_rgb;
80     uint8_t main_rgba_map[4];
81     uint8_t main_has_alpha;
82     uint8_t overlay_is_packed_rgb;
83     uint8_t overlay_rgba_map[4];
84     uint8_t overlay_has_alpha;
85
86     AVFilterBufferRef *overpicref;
87     struct FFBufQueue queue_main;
88     struct FFBufQueue queue_over;
89
90     int main_pix_step[4];       ///< steps per pixel for each plane of the main output
91     int overlay_pix_step[4];    ///< steps per pixel for each plane of the overlay
92     int hsub, vsub;             ///< chroma subsampling values
93
94     char *x_expr, *y_expr;
95 } OverlayContext;
96
97 #define OFFSET(x) offsetof(OverlayContext, x)
98
99 static const AVOption overlay_options[] = {
100     { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX },
101     { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX },
102     {"rgb", "force packed RGB in input and output", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
103     {NULL},
104 };
105
106 AVFILTER_DEFINE_CLASS(overlay);
107
108 static av_cold int init(AVFilterContext *ctx, const char *args)
109 {
110     OverlayContext *over = ctx->priv;
111     char *args1 = av_strdup(args);
112     char *expr, *bufptr = NULL;
113     int ret = 0;
114
115     over->class = &overlay_class;
116     av_opt_set_defaults(over);
117
118     if (expr = av_strtok(args1, ":", &bufptr)) {
119         av_free(over->x_expr);
120         if (!(over->x_expr = av_strdup(expr))) {
121             ret = AVERROR(ENOMEM);
122             goto end;
123         }
124     }
125     if (expr = av_strtok(NULL, ":", &bufptr)) {
126         av_free(over->y_expr);
127         if (!(over->y_expr = av_strdup(expr))) {
128             ret = AVERROR(ENOMEM);
129             goto end;
130         }
131     }
132
133     if (bufptr && (ret = av_set_options_string(over, bufptr, "=", ":")) < 0)
134         goto end;
135
136 end:
137     av_free(args1);
138     return ret;
139 }
140
141 static av_cold void uninit(AVFilterContext *ctx)
142 {
143     OverlayContext *over = ctx->priv;
144
145     av_freep(&over->x_expr);
146     av_freep(&over->y_expr);
147
148     avfilter_unref_bufferp(&over->overpicref);
149     ff_bufqueue_discard_all(&over->queue_main);
150     ff_bufqueue_discard_all(&over->queue_over);
151 }
152
153 static int query_formats(AVFilterContext *ctx)
154 {
155     OverlayContext *over = ctx->priv;
156
157     /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
158     const enum PixelFormat main_pix_fmts_yuv[] = { PIX_FMT_YUV420P,  PIX_FMT_NONE };
159     const enum PixelFormat overlay_pix_fmts_yuv[] = { PIX_FMT_YUVA420P, PIX_FMT_NONE };
160     const enum PixelFormat main_pix_fmts_rgb[] = {
161         PIX_FMT_ARGB,  PIX_FMT_RGBA,
162         PIX_FMT_ABGR,  PIX_FMT_BGRA,
163         PIX_FMT_RGB24, PIX_FMT_BGR24,
164         PIX_FMT_NONE
165     };
166     const enum PixelFormat overlay_pix_fmts_rgb[] = {
167         PIX_FMT_ARGB,  PIX_FMT_RGBA,
168         PIX_FMT_ABGR,  PIX_FMT_BGRA,
169         PIX_FMT_NONE
170     };
171
172     AVFilterFormats *main_formats;
173     AVFilterFormats *overlay_formats;
174
175     if (over->allow_packed_rgb) {
176         main_formats    = ff_make_format_list(main_pix_fmts_rgb);
177         overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
178     } else {
179         main_formats    = ff_make_format_list(main_pix_fmts_yuv);
180         overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv);
181     }
182
183     ff_formats_ref(main_formats,    &ctx->inputs [MAIN   ]->out_formats);
184     ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
185     ff_formats_ref(main_formats,    &ctx->outputs[MAIN   ]->in_formats );
186
187     return 0;
188 }
189
190 static const enum PixelFormat alpha_pix_fmts[] = {
191     PIX_FMT_YUVA420P, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_RGBA,
192     PIX_FMT_BGRA, PIX_FMT_NONE
193 };
194
195 static int config_input_main(AVFilterLink *inlink)
196 {
197     OverlayContext *over = inlink->dst->priv;
198     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
199
200     av_image_fill_max_pixsteps(over->main_pix_step,    NULL, pix_desc);
201
202     over->hsub = pix_desc->log2_chroma_w;
203     over->vsub = pix_desc->log2_chroma_h;
204
205     over->main_is_packed_rgb =
206         ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
207     over->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
208     return 0;
209 }
210
211 static int config_input_overlay(AVFilterLink *inlink)
212 {
213     AVFilterContext *ctx  = inlink->dst;
214     OverlayContext  *over = inlink->dst->priv;
215     char *expr;
216     double var_values[VAR_VARS_NB], res;
217     int ret;
218     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
219
220     av_image_fill_max_pixsteps(over->overlay_pix_step, NULL, pix_desc);
221
222     /* Finish the configuration by evaluating the expressions
223        now when both inputs are configured. */
224     var_values[VAR_MAIN_W   ] = var_values[VAR_MW] = ctx->inputs[MAIN   ]->w;
225     var_values[VAR_MAIN_H   ] = var_values[VAR_MH] = ctx->inputs[MAIN   ]->h;
226     var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
227     var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
228
229     if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
230                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
231         goto fail;
232     over->x = res;
233     if ((ret = av_expr_parse_and_eval(&res, (expr = over->y_expr), var_names, var_values,
234                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)))
235         goto fail;
236     over->y = res;
237     /* x may depend on y */
238     if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
239                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
240         goto fail;
241     over->x = res;
242
243     over->overlay_is_packed_rgb =
244         ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
245     over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
246
247     av_log(ctx, AV_LOG_VERBOSE,
248            "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
249            ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
250            av_pix_fmt_descriptors[ctx->inputs[MAIN]->format].name,
251            over->x, over->y,
252            ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
253            av_pix_fmt_descriptors[ctx->inputs[OVERLAY]->format].name);
254
255     if (over->x < 0 || over->y < 0 ||
256         over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
257         over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
258         av_log(ctx, AV_LOG_ERROR,
259                "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
260                over->x, over->y,
261                (int)(over->x + var_values[VAR_OVERLAY_W]),
262                (int)(over->y + var_values[VAR_OVERLAY_H]),
263                (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
264         return AVERROR(EINVAL);
265     }
266     return 0;
267
268 fail:
269     av_log(NULL, AV_LOG_ERROR,
270            "Error when evaluating the expression '%s'\n", expr);
271     return ret;
272 }
273
274 static int config_output(AVFilterLink *outlink)
275 {
276     AVFilterContext *ctx = outlink->src;
277     int exact;
278     // common timebase computation:
279     AVRational tb1 = ctx->inputs[MAIN   ]->time_base;
280     AVRational tb2 = ctx->inputs[OVERLAY]->time_base;
281     AVRational *tb = &ctx->outputs[0]->time_base;
282     exact = av_reduce(&tb->num, &tb->den,
283                       av_gcd((int64_t)tb1.num * tb2.den,
284                              (int64_t)tb2.num * tb1.den),
285                       (int64_t)tb1.den * tb2.den, INT_MAX);
286     av_log(ctx, AV_LOG_VERBOSE,
287            "main_tb:%d/%d overlay_tb:%d/%d -> tb:%d/%d exact:%d\n",
288            tb1.num, tb1.den, tb2.num, tb2.den, tb->num, tb->den, exact);
289     if (!exact)
290         av_log(ctx, AV_LOG_WARNING,
291                "Timestamp conversion inexact, timestamp information loss may occurr\n");
292
293     outlink->w = ctx->inputs[MAIN]->w;
294     outlink->h = ctx->inputs[MAIN]->h;
295
296     return 0;
297 }
298
299 static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
300 {
301     return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
302 }
303
304 // divide by 255 and round to nearest
305 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
306 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
307
308 static void blend_slice(AVFilterContext *ctx,
309                         AVFilterBufferRef *dst, AVFilterBufferRef *src,
310                         int x, int y, int w, int h,
311                         int slice_y, int slice_w, int slice_h)
312 {
313     OverlayContext *over = ctx->priv;
314     int i, j, k;
315     int width, height;
316     int overlay_end_y = y+h;
317     int slice_end_y = slice_y+slice_h;
318     int end_y, start_y;
319
320     width = FFMIN(slice_w - x, w);
321     end_y = FFMIN(slice_end_y, overlay_end_y);
322     start_y = FFMAX(y, slice_y);
323     height = end_y - start_y;
324
325     if (over->main_is_packed_rgb) {
326         uint8_t *dp = dst->data[0] + x * over->main_pix_step[0] +
327                       start_y * dst->linesize[0];
328         uint8_t *sp = src->data[0];
329         uint8_t alpha;          ///< the amount of overlay to blend on to main
330         const int dr = over->main_rgba_map[R];
331         const int dg = over->main_rgba_map[G];
332         const int db = over->main_rgba_map[B];
333         const int da = over->main_rgba_map[A];
334         const int dstep = over->main_pix_step[0];
335         const int sr = over->overlay_rgba_map[R];
336         const int sg = over->overlay_rgba_map[G];
337         const int sb = over->overlay_rgba_map[B];
338         const int sa = over->overlay_rgba_map[A];
339         const int sstep = over->overlay_pix_step[0];
340         const int main_has_alpha = over->main_has_alpha;
341         if (slice_y > y)
342             sp += (slice_y - y) * src->linesize[0];
343         for (i = 0; i < height; i++) {
344             uint8_t *d = dp, *s = sp;
345             for (j = 0; j < width; j++) {
346                 alpha = s[sa];
347
348                 // if the main channel has an alpha channel, alpha has to be calculated
349                 // to create an un-premultiplied (straight) alpha value
350                 if (main_has_alpha && alpha != 0 && alpha != 255) {
351                     // apply the general equation:
352                     // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
353                     alpha =
354                         // the next line is a faster version of: 255 * 255 * alpha
355                         ( (alpha << 16) - (alpha << 9) + alpha )
356                         /
357                         // the next line is a faster version of: 255 * (alpha + d[da])
358                         ( ((alpha + d[da]) << 8 ) - (alpha + d[da])
359                           - d[da] * alpha );
360                 }
361
362                 switch (alpha) {
363                 case 0:
364                     break;
365                 case 255:
366                     d[dr] = s[sr];
367                     d[dg] = s[sg];
368                     d[db] = s[sb];
369                     break;
370                 default:
371                     // main_value = main_value * (1 - alpha) + overlay_value * alpha
372                     // since alpha is in the range 0-255, the result must divided by 255
373                     d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
374                     d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
375                     d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
376                 }
377                 if (main_has_alpha) {
378                     switch (alpha) {
379                     case 0:
380                         break;
381                     case 255:
382                         d[da] = s[sa];
383                         break;
384                     default:
385                         // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
386                         d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
387                     }
388                 }
389                 d += dstep;
390                 s += sstep;
391             }
392             dp += dst->linesize[0];
393             sp += src->linesize[0];
394         }
395     } else {
396         for (i = 0; i < 3; i++) {
397             int hsub = i ? over->hsub : 0;
398             int vsub = i ? over->vsub : 0;
399             uint8_t *dp = dst->data[i] + (x >> hsub) +
400                 (start_y >> vsub) * dst->linesize[i];
401             uint8_t *sp = src->data[i];
402             uint8_t *ap = src->data[3];
403             int wp = FFALIGN(width, 1<<hsub) >> hsub;
404             int hp = FFALIGN(height, 1<<vsub) >> vsub;
405             if (slice_y > y) {
406                 sp += ((slice_y - y) >> vsub) * src->linesize[i];
407                 ap += (slice_y - y) * src->linesize[3];
408             }
409             for (j = 0; j < hp; j++) {
410                 uint8_t *d = dp, *s = sp, *a = ap;
411                 for (k = 0; k < wp; k++) {
412                     // average alpha for color components, improve quality
413                     int alpha_v, alpha_h, alpha;
414                     if (hsub && vsub && j+1 < hp && k+1 < wp) {
415                         alpha = (a[0] + a[src->linesize[3]] +
416                                  a[1] + a[src->linesize[3]+1]) >> 2;
417                     } else if (hsub || vsub) {
418                         alpha_h = hsub && k+1 < wp ?
419                             (a[0] + a[1]) >> 1 : a[0];
420                         alpha_v = vsub && j+1 < hp ?
421                             (a[0] + a[src->linesize[3]]) >> 1 : a[0];
422                         alpha = (alpha_v + alpha_h) >> 1;
423                     } else
424                         alpha = a[0];
425                     *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
426                     s++;
427                     d++;
428                     a += 1 << hsub;
429                 }
430                 dp += dst->linesize[i];
431                 sp += src->linesize[i];
432                 ap += (1 << vsub) * src->linesize[3];
433             }
434         }
435     }
436 }
437
438 static int try_start_frame(AVFilterContext *ctx, AVFilterBufferRef *mainpic)
439 {
440     OverlayContext *over = ctx->priv;
441     AVFilterLink *outlink = ctx->outputs[0];
442     AVFilterBufferRef *next_overpic, *outpicref;
443
444     /* Discard obsolete overlay frames: if there is a next frame with pts is
445      * before the main frame, we can drop the current overlay. */
446     while (1) {
447         next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
448         if (!next_overpic || next_overpic->pts > mainpic->pts)
449             break;
450         ff_bufqueue_get(&over->queue_over);
451         avfilter_unref_buffer(over->overpicref);
452         over->overpicref = next_overpic;
453     }
454     /* If there is no next frame and no EOF and the overlay frame is before
455      * the main frame, we can not know yet if it will be superseded. */
456     if (!over->queue_over.available && !over->overlay_eof &&
457         (!over->overpicref || over->overpicref->pts < mainpic->pts))
458         return AVERROR(EAGAIN);
459     /* At this point, we know that the current overlay frame extends to the
460      * time of the main frame. */
461     outlink->out_buf = outpicref = avfilter_ref_buffer(mainpic, ~0);
462
463     av_dlog(ctx, "main_pts:%s main_pts_time:%s",
464             av_ts2str(outpicref->pts), av_ts2timestr(outpicref->pts, &outlink->time_base));
465     if (over->overpicref)
466         av_dlog(ctx, " over_pts:%s over_pts_time:%s",
467                 av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &outlink->time_base));
468     av_dlog(ctx, "\n");
469
470     ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(outpicref, ~0));
471     over->frame_requested = 0;
472     return 0;
473 }
474
475 static int try_start_next_frame(AVFilterContext *ctx)
476 {
477     OverlayContext *over = ctx->priv;
478     AVFilterBufferRef *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
479     if (!next_mainpic || try_start_frame(ctx, next_mainpic) < 0)
480         return AVERROR(EAGAIN);
481     avfilter_unref_buffer(ff_bufqueue_get(&over->queue_main));
482     return 0;
483 }
484
485 static int try_push_frame(AVFilterContext *ctx)
486 {
487     OverlayContext *over = ctx->priv;
488     AVFilterLink *outlink = ctx->outputs[0];
489     AVFilterBufferRef *outpicref = outlink->out_buf;
490
491     if (try_start_next_frame(ctx) < 0)
492         return AVERROR(EAGAIN);
493     outpicref = outlink->out_buf;
494     if (over->overpicref)
495         blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
496                     over->overpicref->video->w, over->overpicref->video->h,
497                     0, outpicref->video->w, outpicref->video->h);
498     ff_draw_slice(outlink, 0, outpicref->video->h, +1);
499     ff_end_frame(outlink);
500     return 0;
501 }
502
503 static void flush_frames(AVFilterContext *ctx)
504 {
505     while (!try_push_frame(ctx));
506 }
507
508 static int start_frame_main(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
509 {
510     AVFilterContext *ctx = inlink->dst;
511     OverlayContext *over = ctx->priv;
512
513     flush_frames(ctx);
514     inpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[MAIN]->time_base,
515                                  ctx->outputs[0]->time_base);
516     if (try_start_frame(ctx, inpicref) < 0) {
517         ff_bufqueue_add(ctx, &over->queue_main, inpicref);
518         av_assert1(inpicref == inlink->cur_buf);
519         inlink->cur_buf = NULL;
520     }
521     return 0;
522 }
523
524 static int draw_slice_main(AVFilterLink *inlink, int y, int h, int slice_dir)
525 {
526     AVFilterContext *ctx = inlink->dst;
527     OverlayContext *over = ctx->priv;
528     AVFilterLink *outlink = ctx->outputs[0];
529     AVFilterBufferRef *outpicref = outlink->out_buf;
530
531     if (!outpicref)
532         return 0;
533     if (over->overpicref &&
534         y + h > over->y && y < over->y + over->overpicref->video->h) {
535         blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
536                     over->overpicref->video->w, over->overpicref->video->h,
537                     y, outpicref->video->w, h);
538     }
539     return ff_draw_slice(outlink, y, h, slice_dir);
540 }
541
542 static int end_frame_main(AVFilterLink *inlink)
543 {
544     AVFilterContext *ctx = inlink->dst;
545     AVFilterLink *outlink = ctx->outputs[0];
546     AVFilterBufferRef *outpicref = outlink->out_buf;
547     flush_frames(ctx);
548
549     if (!outpicref)
550         return 0;
551     return ff_end_frame(ctx->outputs[0]);
552 }
553
554 static int start_frame_over(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
555 {
556     return 0;
557 }
558
559 static int end_frame_over(AVFilterLink *inlink)
560 {
561     AVFilterContext *ctx = inlink->dst;
562     OverlayContext *over = ctx->priv;
563     AVFilterBufferRef *inpicref = inlink->cur_buf;
564     inlink->cur_buf = NULL;
565
566     flush_frames(ctx);
567     inpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[OVERLAY]->time_base,
568                                  ctx->outputs[0]->time_base);
569     ff_bufqueue_add(ctx, &over->queue_over, inpicref);
570     return try_push_frame(ctx);
571 }
572
573 static int request_frame(AVFilterLink *outlink)
574 {
575     AVFilterContext *ctx = outlink->src;
576     OverlayContext *over = ctx->priv;
577     int input, ret;
578
579     if (!try_push_frame(ctx))
580         return 0;
581     over->frame_requested = 1;
582     while (over->frame_requested) {
583         /* TODO if we had a frame duration, we could guess more accurately */
584         input = !over->overlay_eof && (over->queue_main.available ||
585                                        over->queue_over.available < 2) ?
586                 OVERLAY : MAIN;
587         ret = ff_request_frame(ctx->inputs[input]);
588         /* EOF on main is reported immediately */
589         if (ret == AVERROR_EOF && input == OVERLAY) {
590             over->overlay_eof = 1;
591             if (!try_start_next_frame(ctx))
592                 return 0;
593             ret = 0; /* continue requesting frames on main */
594         }
595         if (ret < 0)
596             return ret;
597     }
598     return 0;
599 }
600
601 static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
602 {
603     return 0;
604 }
605
606 AVFilter avfilter_vf_overlay = {
607     .name      = "overlay",
608     .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
609
610     .init      = init,
611     .uninit    = uninit,
612
613     .priv_size = sizeof(OverlayContext),
614
615     .query_formats = query_formats,
616
617     .inputs    = (const AVFilterPad[]) {{ .name            = "main",
618                                           .type            = AVMEDIA_TYPE_VIDEO,
619                                           .get_video_buffer= get_video_buffer,
620                                           .config_props    = config_input_main,
621                                           .start_frame     = start_frame_main,
622                                           .draw_slice      = draw_slice_main,
623                                           .end_frame       = end_frame_main,
624                                           .min_perms       = AV_PERM_READ,
625                                           .rej_perms       = AV_PERM_REUSE2|AV_PERM_PRESERVE, },
626                                         { .name            = "overlay",
627                                           .type            = AVMEDIA_TYPE_VIDEO,
628                                           .config_props    = config_input_overlay,
629                                           .start_frame     = start_frame_over,
630                                           .draw_slice      = null_draw_slice,
631                                           .end_frame       = end_frame_over,
632                                           .min_perms       = AV_PERM_READ,
633                                           .rej_perms       = AV_PERM_REUSE2, },
634                                         { .name = NULL}},
635     .outputs   = (const AVFilterPad[]) {{ .name            = "default",
636                                           .type            = AVMEDIA_TYPE_VIDEO,
637                                           .config_props    = config_output,
638                                           .request_frame   = request_frame, },
639                                         { .name = NULL}},
640 };