[FFmpeg-devel] [PATCH] avfilter/vf_alphamerge: switch to dualinput

Paul B Mahol onemda at gmail.com
Wed Jul 15 15:33:31 CEST 2015


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavfilter/Makefile        |  2 +-
 libavfilter/vf_alphamerge.c | 73 +++++++++++++++++----------------------------
 2 files changed, 29 insertions(+), 46 deletions(-)

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 058b9e9..c504070 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -93,7 +93,7 @@ OBJS-$(CONFIG_ANULLSINK_FILTER)              += asink_anullsink.o
 
 OBJS-$(CONFIG_ASS_FILTER)                    += vf_subtitles.o
 OBJS-$(CONFIG_ALPHAEXTRACT_FILTER)           += vf_extractplanes.o
-OBJS-$(CONFIG_ALPHAMERGE_FILTER)             += vf_alphamerge.o
+OBJS-$(CONFIG_ALPHAMERGE_FILTER)             += vf_alphamerge.o dualinput.o framesync.o
 OBJS-$(CONFIG_BBOX_FILTER)                   += bbox.o vf_bbox.o
 OBJS-$(CONFIG_BLACKDETECT_FILTER)            += vf_blackdetect.o
 OBJS-$(CONFIG_BLACKFRAME_FILTER)             += vf_blackframe.o
diff --git a/libavfilter/vf_alphamerge.c b/libavfilter/vf_alphamerge.c
index 5f0da35..a5fb311 100644
--- a/libavfilter/vf_alphamerge.c
+++ b/libavfilter/vf_alphamerge.c
@@ -27,27 +27,24 @@
 
 #include "libavutil/pixfmt.h"
 #include "avfilter.h"
-#include "bufferqueue.h"
 #include "drawutils.h"
 #include "formats.h"
 #include "internal.h"
+#include "dualinput.h"
 #include "video.h"
 
 enum { Y, U, V, A };
 
 typedef struct {
-    int frame_requested;
+    FFDualInputContext dinput;
     int is_packed_rgb;
     uint8_t rgba_map[4];
-    struct FFBufQueue queue_main;
-    struct FFBufQueue queue_alpha;
 } AlphaMergeContext;
 
 static av_cold void uninit(AVFilterContext *ctx)
 {
-    AlphaMergeContext *merge = ctx->priv;
-    ff_bufqueue_discard_all(&merge->queue_main);
-    ff_bufqueue_discard_all(&merge->queue_alpha);
+    AlphaMergeContext *s = ctx->priv;
+    ff_dualinput_uninit(&s->dinput);
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -77,8 +74,11 @@ static int config_input_main(AVFilterLink *inlink)
 static int config_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
+    AlphaMergeContext *s = ctx->priv;
     AVFilterLink *mainlink = ctx->inputs[0];
     AVFilterLink *alphalink = ctx->inputs[1];
+    int ret;
+
     if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
         av_log(ctx, AV_LOG_ERROR,
                "Input frame sizes do not match (%dx%d vs %dx%d).\n",
@@ -92,12 +92,16 @@ static int config_output(AVFilterLink *outlink)
     outlink->time_base = mainlink->time_base;
     outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
     outlink->frame_rate = mainlink->frame_rate;
+
+    if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0)
+        return ret;
+
     return 0;
 }
 
-static void draw_frame(AVFilterContext *ctx,
-                       AVFrame *main_buf,
-                       AVFrame *alpha_buf)
+static AVFrame *draw_frame(AVFilterContext *ctx,
+                           AVFrame *main_buf,
+                           const AVFrame *alpha_buf)
 {
     AlphaMergeContext *merge = ctx->priv;
     int h = main_buf->height;
@@ -124,50 +128,28 @@ static void draw_frame(AVFilterContext *ctx,
                    FFMIN(main_linesize, alpha_linesize));
         }
     }
+
+    return main_buf;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
+static av_cold int init(AVFilterContext *ctx)
 {
-    AVFilterContext *ctx = inlink->dst;
-    AlphaMergeContext *merge = ctx->priv;
-
-    int ret = 0;
-    int is_alpha = (inlink == ctx->inputs[1]);
-    struct FFBufQueue *queue =
-        (is_alpha ? &merge->queue_alpha : &merge->queue_main);
-    ff_bufqueue_add(ctx, queue, buf);
-
-    do {
-        AVFrame *main_buf, *alpha_buf;
-
-        if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
-            !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
+    AlphaMergeContext *s = ctx->priv;
+    s->dinput.process = draw_frame;
+    return 0;
+}
 
-        main_buf = ff_bufqueue_get(&merge->queue_main);
-        alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
+{
+    AlphaMergeContext *s = inlink->dst->priv;
 
-        merge->frame_requested = 0;
-        draw_frame(ctx, main_buf, alpha_buf);
-        ret = ff_filter_frame(ctx->outputs[0], main_buf);
-        av_frame_free(&alpha_buf);
-    } while (ret >= 0);
-    return ret;
+    return ff_dualinput_filter_frame(&s->dinput, inlink, buf);
 }
 
 static int request_frame(AVFilterLink *outlink)
 {
-    AVFilterContext *ctx = outlink->src;
-    AlphaMergeContext *merge = ctx->priv;
-    int in, ret;
-
-    merge->frame_requested = 1;
-    while (merge->frame_requested) {
-        in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
-        ret = ff_request_frame(ctx->inputs[in]);
-        if (ret < 0)
-            return ret;
-    }
-    return 0;
+    AlphaMergeContext *s = outlink->src->priv;
+    return ff_dualinput_request_frame(&s->dinput, outlink);
 }
 
 static const AVFilterPad alphamerge_inputs[] = {
@@ -199,6 +181,7 @@ AVFilter ff_vf_alphamerge = {
     .name           = "alphamerge",
     .description    = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
                       "input into the alpha channel of the first input."),
+    .init           = init,
     .uninit         = uninit,
     .priv_size      = sizeof(AlphaMergeContext),
     .query_formats  = query_formats,
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list