[FFmpeg-devel] [PATCH] amerge audio filter [WIP]

Nicolas George nicolas.george at normalesup.org
Sun Nov 6 21:32:55 CET 2011


Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 libavfilter/Makefile     |    1 +
 libavfilter/af_amerge.c  |  177 ++++++++++++++++++++++++++++++++++++++++++++++
 libavfilter/allfilters.c |    1 +
 3 files changed, 179 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/af_amerge.c


This is early work in progress, just in case someone would happen to work on
the same thing at the same time, and because I will not be able to come back
on it for a few days.

The following command works for me:

./ffmpeg_g -f lavfi -i 'amovie=1.wav [b] ; amovie=2.mp3 [a] ;
  [a] [b] amerge, pan=5.1:1:0:0:0:0:0:0:1:0:0:0:0:0:0:0:0:1:0:0:0:0:0:0:1' \
  -f alsa surround51

(On the other hand, amovie segfaults on Ogg Vorbis files.)

Regards,

-- 
  Nicolas George



diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index aeb5575..3185666 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -25,6 +25,7 @@ OBJS-$(CONFIG_AVCODEC)                       += avcodec.o
 
 OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
 OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
+OBJS-$(CONFIG_AMERGE_FILTER)                 += af_amerge.o
 OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
 OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
diff --git a/libavfilter/af_amerge.c b/libavfilter/af_amerge.c
new file mode 100644
index 0000000..37a6a8d
--- /dev/null
+++ b/libavfilter/af_amerge.c
@@ -0,0 +1,177 @@
+/*
+ * 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
+ * Audio merging 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"
+
+struct amerge_context {
+    AVFilterBufferRef *inbuf[2];
+    int route[SWR_CH_MAX];
+    int nb_channels_in[2];
+    int inpos[2];
+};
+
+static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
+{
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    struct amerge_context *am = ctx->priv;
+    int64_t inlayout[2], outlayout;
+    const int packing_fmts[] = { AVFILTER_PACKED, -1};
+    AVFilterFormats *formats;
+    int i;
+
+    for (i = 0; i < 2; i++) {
+        if (!ctx->inputs[i]->in_chlayouts ||
+            !ctx->inputs[i]->in_chlayouts->format_count) {
+            av_log(ctx, AV_LOG_ERROR,
+                   "no channel layout for input %d\n", i + 1);
+            return AVERROR(EINVAL);
+        }
+        inlayout[i] = ctx->inputs[i]->in_chlayouts->formats[0];
+        if (ctx->inputs[i]->in_chlayouts->format_count > 1) {
+            char buf[256];
+            av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
+            av_log(ctx, AV_LOG_INFO, "using \"%s\" for input %d\n", buf, i + 1);
+        }
+        am->nb_channels_in[i] = av_get_channel_layout_nb_channels(inlayout[i]);
+    }
+    if (inlayout[0] & inlayout[1]) {
+        av_log(ctx, AV_LOG_WARNING,
+               "inputs overlap: output layout will be meaningless\n");
+        for (i = 0; i < am->nb_channels_in[0]; i++)
+            am->route[i] = i;
+        for (i = 0; i < am->nb_channels_in[1]; i++)
+            am->route[i + am->nb_channels_in[0]] = i;
+        outlayout = av_get_default_channel_layout(am->nb_channels_in[0] + am->nb_channels_in[1]);
+        if (!outlayout)
+            outlayout = ((int64_t)1 << (am->nb_channels_in[0] + am->nb_channels_in[1])) - 1;
+    } else {
+        /* TODO */
+        return AVERROR(EINVAL);
+    }
+    avfilter_set_common_sample_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO));
+    avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
+    for (i = 0; i < 2; i++) {
+        formats = NULL;
+        avfilter_add_format(&formats, inlayout[i]);
+        avfilter_formats_ref(formats, &ctx->inputs[i]->out_chlayouts);
+    }
+    formats = NULL;
+    avfilter_add_format(&formats, outlayout);
+    avfilter_formats_ref(formats, &ctx->outputs[0]->in_chlayouts);
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    return 0;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    struct amerge_context *am = ctx->priv;
+    int i;
+
+    for (i = 0; i < 2; i++)
+        if (!am->inbuf[i])
+            avfilter_request_frame(ctx->inputs[i]);
+    return 0;
+}
+
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+{
+    AVFilterContext *ctx = inlink->dst;
+    struct amerge_context *am = ctx->priv;
+    int in_no = inlink == ctx->inputs[1];
+    int nb_samples, i, s, c, *route;
+    AVFilterBufferRef *outsamples;
+    int16_t *ins[2], *outs;
+
+    am->inbuf[in_no] = avfilter_ref_buffer(insamples, AV_PERM_READ);
+    am->inpos[in_no] = 0;
+    if (!am->inbuf[!in_no])
+        return;
+    nb_samples = FFMIN(am->inbuf[0]->audio->nb_samples - am->inpos[0],
+                       am->inbuf[1]->audio->nb_samples - am->inpos[1]);
+    outsamples = avfilter_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE,
+                                           nb_samples);
+    outs = (int16_t *)outsamples->data[0];
+    for (i = 0; i < 2; i++)
+        ins[i] = (int16_t *)am->inbuf[i]->data[0] +
+                 am->nb_channels_in[i] * am->inpos[i];
+    for (s = 0; s < nb_samples; s++) {
+        route = am->route;
+        for (i = 0; i < 2; i++) {
+            for (c = 0; c < am->nb_channels_in[i]; c++)
+                *(outs++) = ins[i][*(route++)];
+            ins[i] += am->nb_channels_in[i];
+        }
+    }
+    for (i = 0; i < 2; i++) {
+        am->inpos[i] += nb_samples;
+        if (am->inpos[i] == am->inbuf[i]->audio->nb_samples) {
+            avfilter_unref_buffer(am->inbuf[i]);
+            am->inbuf[i] = 0;
+        }
+    }
+    avfilter_filter_samples(ctx->outputs[0], outsamples);
+}
+
+AVFilter avfilter_af_amerge = {
+    .name          = "amerge",
+    .description   = NULL_IF_CONFIG_SMALL("Merge two audio streams into a single multi-channel stream"),
+    .priv_size     = sizeof(struct amerge_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             = "default",
+          .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 d36ede7..daee7d9 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -36,6 +36,7 @@ void avfilter_register_all(void)
 
     REGISTER_FILTER (ACONVERT,    aconvert,    af);
     REGISTER_FILTER (AFORMAT,     aformat,     af);
+    REGISTER_FILTER (AMERGE,      amerge,     af);
     REGISTER_FILTER (ANULL,       anull,       af);
     REGISTER_FILTER (ARESAMPLE,   aresample,   af);
     REGISTER_FILTER (ASHOWINFO,   ashowinfo,   af);
-- 
1.7.7.1



More information about the ffmpeg-devel mailing list