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