lavfi/avcodec.c: fix copyright
[ffmpeg.git] / libavfilter / vf_overlay.c
index 6fa10ae..8227d35 100644 (file)
@@ -25,6 +25,8 @@
  * overlay one video on top of another
  */
 
+/* #define DEBUG */
+
 #include "avfilter.h"
 #include "libavutil/eval.h"
 #include "libavutil/avstring.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/timestamp.h"
 #include "internal.h"
 #include "drawutils.h"
 
-static const char *var_names[] = {
+static const char *const var_names[] = {
     "main_w",    "W", ///< width  of the main    video
     "main_h",    "H", ///< height of the main    video
     "overlay_w", "w", ///< width  of the overlay video
@@ -75,7 +78,7 @@ typedef struct {
     uint8_t overlay_rgba_map[4];
     uint8_t overlay_has_alpha;
 
-    AVFilterBufferRef *overpicref;
+    AVFilterBufferRef *overpicref, *overpicref_next;
 
     int main_pix_step[4];       ///< steps per pixel for each plane of the main output
     int overlay_pix_step[4];    ///< steps per pixel for each plane of the overlay
@@ -115,12 +118,14 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
     av_opt_set_defaults(over);
 
     if (expr = av_strtok(args1, ":", &bufptr)) {
+        av_free(over->x_expr);
         if (!(over->x_expr = av_strdup(expr))) {
             ret = AVERROR(ENOMEM);
             goto end;
         }
     }
     if (expr = av_strtok(NULL, ":", &bufptr)) {
+        av_free(over->y_expr);
         if (!(over->y_expr = av_strdup(expr))) {
             ret = AVERROR(ENOMEM);
             goto end;
@@ -144,6 +149,8 @@ static av_cold void uninit(AVFilterContext *ctx)
 
     if (over->overpicref)
         avfilter_unref_buffer(over->overpicref);
+    if (over->overpicref_next)
+        avfilter_unref_buffer(over->overpicref_next);
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -183,7 +190,7 @@ static int query_formats(AVFilterContext *ctx)
     return 0;
 }
 
-static enum PixelFormat alpha_pix_fmts[] = {
+static const enum PixelFormat alpha_pix_fmts[] = {
     PIX_FMT_YUVA420P, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_RGBA,
     PIX_FMT_BGRA, PIX_FMT_NONE
 };
@@ -302,22 +309,31 @@ static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
     AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
     AVFilterContext *ctx = inlink->dst;
     OverlayContext *over = ctx->priv;
+    av_unused AVFilterLink *outlink = ctx->outputs[0];
 
     inlink->dst->outputs[0]->out_buf = outpicref;
     outpicref->pts = av_rescale_q(outpicref->pts, ctx->inputs[MAIN]->time_base,
                                   ctx->outputs[0]->time_base);
 
     if (!over->overpicref || over->overpicref->pts < outpicref->pts) {
-        AVFilterBufferRef *old = over->overpicref;
-        over->overpicref = NULL;
-        avfilter_request_frame(ctx->inputs[OVERLAY]);
-        if (over->overpicref) {
-            if (old)
-                avfilter_unref_buffer(old);
-        } else
-            over->overpicref = old;
+        if (!over->overpicref_next)
+            avfilter_request_frame(ctx->inputs[OVERLAY]);
+
+        if (over->overpicref && over->overpicref_next &&
+            over->overpicref_next->pts <= outpicref->pts) {
+            avfilter_unref_buffer(over->overpicref);
+            over->overpicref = over->overpicref_next;
+            over->overpicref_next = NULL;
+        }
     }
 
+    av_dlog(ctx, "main_pts:%s main_pts_time:%s",
+            av_ts2str(outpicref->pts), av_ts2timestr(outpicref->pts, &outlink->time_base));
+    if (over->overpicref)
+        av_dlog(ctx, " over_pts:%s over_pts_time:%s",
+                av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &outlink->time_base));
+    av_dlog(ctx, "\n");
+
     avfilter_start_frame(inlink->dst->outputs[0], outpicref);
 }
 
@@ -326,9 +342,11 @@ static void start_frame_overlay(AVFilterLink *inlink, AVFilterBufferRef *inpicre
     AVFilterContext *ctx = inlink->dst;
     OverlayContext *over = ctx->priv;
 
-    over->overpicref = inpicref;
-    over->overpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[OVERLAY]->time_base,
-                                         ctx->outputs[0]->time_base);
+    inpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[OVERLAY]->time_base,
+                                 ctx->outputs[0]->time_base);
+
+    if (!over->overpicref) over->overpicref      = inpicref;
+    else                   over->overpicref_next = inpicref;
 }
 
 // divide by 255 and round to nearest
@@ -492,6 +510,18 @@ static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) {
 
 static void null_end_frame(AVFilterLink *inlink) { }
 
+static int poll_frame(AVFilterLink *link)
+{
+    AVFilterContext   *s = link->src;
+    OverlayContext *over = s->priv;
+    int ret = avfilter_poll_frame(s->inputs[OVERLAY]);
+
+    if (ret == AVERROR_EOF)
+        ret = !!over->overpicref;
+
+    return ret && avfilter_poll_frame(s->inputs[MAIN]);
+}
+
 AVFilter avfilter_vf_overlay = {
     .name      = "overlay",
     .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
@@ -503,7 +533,7 @@ AVFilter avfilter_vf_overlay = {
 
     .query_formats = query_formats,
 
-    .inputs    = (AVFilterPad[]) {{ .name            = "main",
+    .inputs    = (const AVFilterPad[]) {{ .name      = "main",
                                     .type            = AVMEDIA_TYPE_VIDEO,
                                     .start_frame     = start_frame,
                                     .get_video_buffer= get_video_buffer,
@@ -521,8 +551,9 @@ AVFilter avfilter_vf_overlay = {
                                     .min_perms       = AV_PERM_READ,
                                     .rej_perms       = AV_PERM_REUSE2, },
                                   { .name = NULL}},
-    .outputs   = (AVFilterPad[]) {{ .name            = "default",
+    .outputs   = (const AVFilterPad[]) {{ .name      = "default",
                                     .type            = AVMEDIA_TYPE_VIDEO,
-                                    .config_props    = config_output, },
+                                    .config_props    = config_output,
+                                    .poll_frame      = poll_frame },
                                   { .name = NULL}},
 };