[FFmpeg-devel] [PATCH 2/2] lavfi: add channelremap audio filter.
Clément Bœsch
ubitux at gmail.com
Wed Jan 11 13:17:57 CET 2012
From: Clément Bœsch <clement.boesch at smartjog.com>
---
doc/filters.texi | 32 ++++++++
libavfilter/Makefile | 1 +
libavfilter/af_channelremap.c | 160 +++++++++++++++++++++++++++++++++++++++++
libavfilter/allfilters.c | 1 +
4 files changed, 194 insertions(+), 0 deletions(-)
create mode 100644 libavfilter/af_channelremap.c
diff --git a/doc/filters.texi b/doc/filters.texi
index d0075f2..3bd0861 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -299,6 +299,38 @@ amovie=file.ogg [a] ; amovie=file.mp3 [b] ;
[a2] [b2] amerge
@end example
+ at section channelremap
+
+Remap the channels of an audio stream using channel indexes.
+
+This filter allows to re-order, copy, remove or insert muted channels in an
+audio stream. It accepts a list of channel indexes separated by ":". Using "-1"
+as channel index means it is a muted channel.
+
+For example, if you have a 5.1 source and want a stereo audio stream by
+dropping the extra channels:
+ at example
+channelremap=0:1
+ at end example
+
+Given the same source, you can also switch front left and front right channels
+and keep the input channel layout:
+ at example
+channelremap=1:0:2:3:4:5
+ at end example
+
+If the input is a stereo audio stream, you can mute the front left channel (and
+still keep the stereo channel layout) with:
+ at example
+channelremap=-1:1
+ at end example
+
+Still with a stereo audio stream input, you can copy the right channel in both
+front left and right:
+ at example
+channelremap=0:0
+ at end example
+
@section earwax
Make audio easier to listen to on headphones.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index d4b71d2..0cd54a7 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -32,6 +32,7 @@ OBJS-$(CONFIG_ARESAMPLE_FILTER) += af_aresample.o
OBJS-$(CONFIG_ASHOWINFO_FILTER) += af_ashowinfo.o
OBJS-$(CONFIG_ASPLIT_FILTER) += af_asplit.o
OBJS-$(CONFIG_ASTREAMSYNC_FILTER) += af_astreamsync.o
+OBJS-$(CONFIG_CHANNELREMAP_FILTER) += af_channelremap.o
OBJS-$(CONFIG_EARWAX_FILTER) += af_earwax.o
OBJS-$(CONFIG_PAN_FILTER) += af_pan.o
OBJS-$(CONFIG_SILENCEDETECT_FILTER) += af_silencedetect.o
diff --git a/libavfilter/af_channelremap.c b/libavfilter/af_channelremap.c
new file mode 100644
index 0000000..d3d553b
--- /dev/null
+++ b/libavfilter/af_channelremap.c
@@ -0,0 +1,160 @@
+/*
+ * Copyright (C) 2011 Smartjog S.A.S, Clément Bœsch <clement.boesch at smartjog.com>
+ *
+ * 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 channel remapping filter
+ */
+
+#include "libavutil/avstring.h"
+#include "libavutil/opt.h"
+#include "libswresample/swresample.h"
+#include "avfilter.h"
+
+typedef struct {
+ int ch[SWR_CH_MAX];
+ int nb_channels;
+ uint64_t channel_layout;
+ struct SwrContext *swr;
+ int sample_rate;
+} ChannelRemapContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
+{
+ int i;
+ ChannelRemapContext *remap = ctx->priv;
+ char *args = av_strdup(args0);
+ char *arg = args;
+
+ for (;;) {
+ remap->ch[remap->nb_channels++] = strtol(arg, &arg, 10);
+ if (!*arg)
+ break;
+ arg++;
+ }
+ remap->channel_layout = av_get_default_channel_layout(remap->nb_channels);
+
+ av_log(ctx, AV_LOG_INFO, "channels:");
+ for (i = 0; i < remap->nb_channels; i++)
+ if (remap->ch[i] < 0) av_log(ctx, AV_LOG_INFO, " M");
+ else av_log(ctx, AV_LOG_INFO, " %d", remap->ch[i]);
+ av_log(ctx, AV_LOG_INFO, "\n");
+
+ av_freep(&args);
+ return 0;
+}
+
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+{
+ int n = insamples->audio->nb_samples;
+ ChannelRemapContext *remap = inlink->dst->priv;
+ AVFilterLink * const outlink = inlink->dst->outputs[0];
+ AVFilterBufferRef *outsamples = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, n);
+ AVFilterBufferRefAudioProps *in = insamples->audio;
+ AVFilterBufferRefAudioProps *out = outsamples->audio;
+
+ if (!remap->sample_rate || remap->sample_rate != in->sample_rate) {
+ remap->sample_rate = in->sample_rate;
+ remap->swr = swr_alloc_set_opts(remap->swr,
+ out->channel_layout, outsamples->format, remap->sample_rate,
+ in ->channel_layout, insamples ->format, remap->sample_rate,
+ 0, 0);
+ if (!remap->swr)
+ return;
+ av_opt_set_int(remap->swr, "icl", remap->channel_layout, 0);
+ av_opt_set_int(remap->swr, "uch", remap->nb_channels, 0);
+ swr_set_channel_mapping(remap->swr, remap->ch);
+ if (swr_init(remap->swr) < 0)
+ return;
+ }
+
+ swr_convert(remap->swr, outsamples->data, n, insamples->data, n);
+
+ avfilter_filter_samples(outlink, outsamples);
+ avfilter_unref_buffer(insamples);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ ChannelRemapContext *remap = ctx->priv;
+ AVFilterLink *inlink = ctx->inputs[0];
+ AVFilterLink *outlink = ctx->outputs[0];
+ AVFilterFormats *formats;
+
+ // libswr supports any sample and packing formats
+ avfilter_set_common_sample_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO));
+ avfilter_set_common_packing_formats(ctx, avfilter_make_all_packing_formats());
+
+ // inlink supports any channel layout
+ formats = avfilter_make_all_channel_layouts();
+ avfilter_formats_ref(formats, &inlink->out_chlayouts);
+
+ // outlink supports only requested output channel layout
+ formats = NULL;
+ avfilter_add_format(&formats, remap->channel_layout);
+ avfilter_formats_ref(formats, &outlink->in_chlayouts);
+ return 0;
+}
+
+static int config_props(AVFilterLink *link)
+{
+ AVFilterContext *ctx = link->dst;
+ ChannelRemapContext *remap = ctx->priv;
+ int i, in_nb_ch = av_get_channel_layout_nb_channels(link->channel_layout);
+
+ for (i = 0; i < remap->nb_channels; i++) {
+ if (remap->ch[i] >= in_nb_ch) {
+ av_log(ctx, AV_LOG_ERROR,
+ "Invalid channel index %d (input has %d channels, and indexes start at 0)\n",
+ remap->ch[i], in_nb_ch);
+ return AVERROR(EINVAL);
+ }
+ }
+ return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ ChannelRemapContext *remap = ctx->priv;
+ swr_free(&remap->swr);
+}
+
+AVFilter avfilter_af_channelremap = {
+ .name = "channelremap",
+ .description = NULL_IF_CONFIG_SMALL("Remap the channels of an audio stream"),
+ .priv_size = sizeof(ChannelRemapContext),
+ .query_formats = query_formats,
+ .init = init,
+ .uninit = uninit,
+
+ .inputs = (const AVFilterPad[]) {
+ { .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .filter_samples = filter_samples,
+ .config_props = config_props,
+ .min_perms = AV_PERM_READ|AV_PERM_WRITE, },
+ { .name = NULL }
+ },
+ .outputs = (const AVFilterPad[]) {
+ { .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO, },
+ { .name = NULL }
+ },
+};
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 8286d4d..e6c7b83 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -42,6 +42,7 @@ void avfilter_register_all(void)
REGISTER_FILTER (ASHOWINFO, ashowinfo, af);
REGISTER_FILTER (ASPLIT, asplit, af);
REGISTER_FILTER (ASTREAMSYNC, astreamsync, af);
+ REGISTER_FILTER (CHANNELREMAP,channelremap,af);
REGISTER_FILTER (EARWAX, earwax, af);
REGISTER_FILTER (PAN, pan, af);
REGISTER_FILTER (SILENCEDETECT, silencedetect, af);
--
1.7.7.3
More information about the ffmpeg-devel
mailing list