diff mbox

[FFmpeg-devel,2/2] avfilter/af_join: switch to activate

Message ID 20171027161939.3625-2-onemda@gmail.com
State New
Headers show

Commit Message

Paul B Mahol Oct. 27, 2017, 4:19 p.m. UTC
Fixes #6780.

Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavfilter/af_join.c | 101 +++++++++++++++++++++++++++-----------------------
 1 file changed, 54 insertions(+), 47 deletions(-)
diff mbox

Patch

diff --git a/libavfilter/af_join.c b/libavfilter/af_join.c
index 33df685691..030bb8e549 100644
--- a/libavfilter/af_join.c
+++ b/libavfilter/af_join.c
@@ -32,6 +32,7 @@ 
 #include "audio.h"
 #include "avfilter.h"
 #include "formats.h"
+#include "filters.h"
 #include "internal.h"
 
 typedef struct ChannelMap {
@@ -78,34 +79,6 @@  static const AVOption join_options[] = {
 
 AVFILTER_DEFINE_CLASS(join);
 
-static int try_push_frame(AVFilterContext *ctx);
-
-static int filter_frame(AVFilterLink *link, AVFrame *frame)
-{
-    AVFilterContext *ctx = link->dst;
-    JoinContext       *s = ctx->priv;
-    int i, j;
-
-    for (i = 0; i < ctx->nb_inputs; i++)
-        if (link == ctx->inputs[i])
-            break;
-    av_assert0(i < ctx->nb_inputs);
-    av_assert0(!s->input_frames[i]);
-    s->input_frames[i] = frame;
-
-    /* request the same number of samples on all inputs */
-    /* FIXME that means a frame arriving asynchronously on a different input
-       will not have the requested number of samples */
-    if (i == 0) {
-        int nb_samples = s->input_frames[0]->nb_samples;
-
-        for (j = 1; !i && j < ctx->nb_inputs; j++)
-            ctx->inputs[j]->request_samples = nb_samples;
-    }
-
-    return try_push_frame(ctx);
-}
-
 static int parse_maps(AVFilterContext *ctx)
 {
     JoinContext *s = ctx->priv;
@@ -220,9 +193,6 @@  static av_cold int join_init(AVFilterContext *ctx)
         pad.name           = av_strdup(name);
         if (!pad.name)
             return AVERROR(ENOMEM);
-        pad.filter_frame   = filter_frame;
-
-        pad.needs_fifo = 1;
 
         if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
             av_freep(&pad.name);
@@ -390,21 +360,6 @@  fail:
     return ret;
 }
 
-static int join_request_frame(AVFilterLink *outlink)
-{
-    AVFilterContext *ctx = outlink->src;
-    JoinContext *s       = ctx->priv;
-    int i;
-
-    /* get a frame on each input */
-    for (i = 0; i < ctx->nb_inputs; i++) {
-        AVFilterLink *inlink = ctx->inputs[i];
-        if (!s->input_frames[i])
-            return ff_request_frame(inlink);
-    }
-    return 0;
-}
-
 static int try_push_frame(AVFilterContext *ctx)
 {
     AVFilterLink *outlink = ctx->outputs[0];
@@ -420,6 +375,8 @@  static int try_push_frame(AVFilterContext *ctx)
             return 0;
         nb_samples = FFMIN(nb_samples, s->input_frames[i]->nb_samples);
     }
+    if (!nb_samples)
+        return 0;
 
     /* setup the output frame */
     frame = av_frame_alloc();
@@ -508,12 +465,61 @@  fail:
     return ret;
 }
 
+static int activate(AVFilterContext *ctx)
+{
+    JoinContext *s = ctx->priv;
+    int i, ret, status;
+    int nb_samples = 0;
+    int64_t pts;
+
+    if (!s->input_frames[0]) {
+        ret = ff_inlink_consume_frame(ctx->inputs[0], &s->input_frames[0]);
+        if (ret < 0) {
+            return ret;
+        } else if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
+            ff_outlink_set_status(ctx->outputs[0], status, pts);
+            return 0;
+        } else {
+            if (ff_outlink_frame_wanted(ctx->outputs[0]) && !s->input_frames[0]) {
+                ff_inlink_request_frame(ctx->inputs[0]);
+                return 0;
+            }
+        }
+    }
+
+    nb_samples = s->input_frames[0]->nb_samples;
+
+    for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) {
+        if (s->input_frames[i])
+            continue;
+
+        if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
+            ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->input_frames[i]);
+            if (ret < 0) {
+                return ret;
+            } else if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
+                ff_outlink_set_status(ctx->outputs[0], status, pts);
+                return 0;
+            }
+        } else {
+            if (ff_outlink_frame_wanted(ctx->outputs[0])) {
+                ff_inlink_request_frame(ctx->inputs[i]);
+                return 0;
+            }
+        }
+    }
+
+    if (i == ctx->nb_inputs)
+        ret = try_push_frame(ctx);
+
+    return ret;
+}
+
 static const AVFilterPad avfilter_af_join_outputs[] = {
     {
         .name          = "default",
         .type          = AVMEDIA_TYPE_AUDIO,
         .config_props  = join_config_output,
-        .request_frame = join_request_frame,
     },
     { NULL }
 };
@@ -526,6 +532,7 @@  AVFilter ff_af_join = {
     .priv_class     = &join_class,
     .init           = join_init,
     .uninit         = join_uninit,
+    .activate       = activate,
     .query_formats  = join_query_formats,
     .inputs         = NULL,
     .outputs        = avfilter_af_join_outputs,