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