[FFmpeg-devel] [PATCH] lavfi: add arandorder filter

Nicolas George nicolas.george at normalesup.org
Wed Dec 14 21:45:14 CET 2011


This filter has two inputs, two outputs and just copies the samples from one
to the other. But it does it in a random order, possibly buffering several
buffers of samples in the middle.

The purpose is to help stress-testing filters with several inputs.

Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---


I do not know whether it deserves to be committed in the public repository,
but I had to write it, so I should make it available to other developers if
they happen to need it.

Regards,

-- 
  Nicolas George


 libavfilter/Makefile        |    1 +
 libavfilter/af_arandorder.c |  155 +++++++++++++++++++++++++++++++++++++++++++
 libavfilter/allfilters.c    |    1 +
 3 files changed, 157 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/af_arandorder.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index a6fd18f..a04fd15 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -26,6 +26,7 @@ OBJS-$(CONFIG_AVCODEC)                       += avcodec.o
 OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
 OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
 OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
+OBJS-$(CONFIG_ARANDORDER_FILTER)             += af_arandorder.o
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
 OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
 OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
diff --git a/libavfilter/af_arandorder.c b/libavfilter/af_arandorder.c
new file mode 100644
index 0000000..573f387
--- /dev/null
+++ b/libavfilter/af_arandorder.c
@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2011 Nicolas George <nicolas.george at normalesup.org>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Random reordering filter
+ */
+
+#include <stdlib.h>
+#include "libavcodec/avcodec.h"
+#include "libavutil/avstring.h"
+#include "libswresample/swresample.h" // only for SWR_CH_MAX
+#include "avfilter.h"
+#include "internal.h"
+
+#define QUEUE_SIZE 16
+
+struct arandorder_context {
+    unsigned rand_state;
+    struct {
+        AVFilterBufferRef *buf[QUEUE_SIZE];
+        unsigned t, n;
+    } queue[2];
+    int req[2];
+};
+
+static inline int next_out(struct arandorder_context *ar)
+{
+    return !!(ar->rand_state & 0x80000000);
+}
+
+static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
+{
+    struct arandorder_context *ar = ctx->priv;
+
+    ar->rand_state = MKTAG('R','A','N','D') * 42;
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    avfilter_set_common_sample_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO));
+    avfilter_set_common_packing_formats(ctx, avfilter_make_all_packing_formats());
+    avfilter_set_common_channel_layouts(ctx, avfilter_make_all_channel_layouts());
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    return 0;
+}
+
+static void send_next(AVFilterContext *ctx)
+{
+    struct arandorder_context *ar = ctx->priv;
+    int id;
+
+    while (1) {
+        id = next_out(ar);
+        if (!ar->queue[id].n)
+            break;
+        avfilter_filter_samples(ctx->outputs[id], ar->queue[id].buf[ar->queue[id].t]);
+        ar->queue[id].n--;
+        ar->queue[id].t = (ar->queue[id].t + 1) % QUEUE_SIZE;
+        ar->rand_state = ar->rand_state * 1664525 + 1013904223;
+        if (ar->req[id])
+            ar->req[id]--;
+    }
+    for (id = 0; id < 2; id++) {
+        if (ar->queue[id].n == QUEUE_SIZE) {
+            avfilter_filter_samples(ctx->outputs[id], ar->queue[id].buf[ar->queue[id].t]);
+            ar->queue[id].n--;
+            ar->queue[id].t = (ar->queue[id].t + 1) % QUEUE_SIZE;
+            if (ar->req[id])
+                ar->req[id]--;
+        }
+    }
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    struct arandorder_context *ar = ctx->priv;
+    int id = outlink == ctx->outputs[1];
+    int i;
+
+    if (id == next_out(ar) && ar->queue[id].n) {
+        send_next(ctx);
+        return 0;
+    }
+    ar->req[id]++;
+    while (ar->req[id])
+        for (i = 0; i < 2; i++)
+            if (!ar->queue[i].n)
+                avfilter_request_frame(ctx->inputs[i]);
+    return 0;
+}
+
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+{
+    AVFilterContext *ctx = inlink->dst;
+    struct arandorder_context *ar = ctx->priv;
+    int id = inlink == ctx->inputs[1];
+
+    ar->queue[id].buf[(ar->queue[id].t + ar->queue[id].n++) % QUEUE_SIZE] = insamples;
+    send_next(ctx);
+}
+
+AVFilter avfilter_af_arandorder = {
+    .name          = "arandorder",
+    .description   = NULL_IF_CONFIG_SMALL("Copy two streams of audio data in a random order"),
+    .priv_size     = sizeof(struct arandorder_context),
+    .init          = init,
+    .query_formats = query_formats,
+
+    .inputs    = (const AVFilterPad[]) {
+        { .name             = "in1",
+          .type             = AVMEDIA_TYPE_AUDIO,
+          .filter_samples   = filter_samples,
+          .min_perms        = AV_PERM_READ, },
+        { .name             = "in2",
+          .type             = AVMEDIA_TYPE_AUDIO,
+          .filter_samples   = filter_samples,
+          .min_perms        = AV_PERM_READ, },
+        { .name = NULL}
+    },
+    .outputs   = (const AVFilterPad[]) {
+        { .name             = "out1",
+          .type             = AVMEDIA_TYPE_AUDIO,
+          .config_props     = config_output,
+          .request_frame    = request_frame, },
+        { .name             = "out2",
+          .type             = AVMEDIA_TYPE_AUDIO,
+          .config_props     = config_output,
+          .request_frame    = request_frame, },
+        { .name = NULL}
+    },
+};
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index fb50a3a..94cfa40 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -37,6 +37,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (ACONVERT,    aconvert,    af);
     REGISTER_FILTER (AFORMAT,     aformat,     af);
     REGISTER_FILTER (ANULL,       anull,       af);
+    REGISTER_FILTER (ARANDORDER,  arandorder,  af);
     REGISTER_FILTER (ARESAMPLE,   aresample,   af);
     REGISTER_FILTER (ASHOWINFO,   ashowinfo,   af);
     REGISTER_FILTER (EARWAX,      earwax,      af);
-- 
1.7.7.3



More information about the ffmpeg-devel mailing list