From 58b049f2fa4f192b00baadb5f1f32ca366f936ea Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Wed, 16 May 2012 09:19:46 +0200 Subject: lavfi: support automatically inserting the fifo filter when needed. This breaks libavfilter ABI. --- doc/APIchanges | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'doc') diff --git a/doc/APIchanges b/doc/APIchanges index b84c7ae85c..990e36e5c0 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -4,7 +4,7 @@ since the last major version increase. The last version increases were: libavcodec: 2012-01-27 libavdevice: 2011-04-18 -libavfilter: 2011-04-18 +libavfilter: 2012-06-22 libavformat: 2012-01-27 libavresample: 2012-04-24 libswscale: 2011-06-20 -- cgit v1.2.3 From dc07fb6f7b8fe0f7febdd89e21d23ac50ae920ec Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Mon, 28 May 2012 07:28:58 +0200 Subject: lavfi: add join audio filter. It joins multiple input streams into one multi-channel output. --- Changelog | 1 + doc/filters.texi | 37 ++++ libavfilter/Makefile | 1 + libavfilter/af_join.c | 500 +++++++++++++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 5 files changed, 540 insertions(+) create mode 100644 libavfilter/af_join.c (limited to 'doc') diff --git a/Changelog b/Changelog index 8ee9b8f30a..03287673e8 100644 --- a/Changelog +++ b/Changelog @@ -28,6 +28,7 @@ version : - RTMPT protocol support - iLBC encoding/decoding via libilbc - Microsoft Screen 1 decoder +- join audio filter version 0.8: diff --git a/doc/filters.texi b/doc/filters.texi index f17dad8051..e40f8798cc 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -232,6 +232,43 @@ front_center.wav -map '[LFE]' lfe.wav -map '[SL]' side_left.wav -map '[SR]' side_right.wav @end example +@section join +Join multiple input streams into one multi-channel stream. + +The filter accepts the following named parameters: +@table @option + +@item inputs +Number of input streams. Defaults to 2. + +@item channel_layout +Desired output channel layout. Defaults to stereo. + +@item map +Map channels from inputs to output. The argument is a comma-separated list of +mappings, each in the @code{@var{input_idx}.@var{in_channel}-@var{out_channel}} +form. @var{input_idx} is the 0-based index of the input stream. @var{in_channel} +can be either the name of the input channel (e.g. FR for front left) or its +index in the specified input stream. @var{out_channel} is the name of the output +channel. +@end table + +The filter will attempt to guess the mappings when those are not specified +explicitly. It does so by first trying to find an unused matching input channel +and if that fails it picks the first unused input channel. + +E.g. to join 3 inputs (with properly set channel layouts) +@example +avconv -i INPUT1 -i INPUT2 -i INPUT3 -filter_complex join=inputs=3 OUTPUT +@end example + +To build a 5.1 output from 6 single-channel streams: +@example +avconv -i fl -i fr -i fc -i sl -i sr -i lfe -filter_complex +'join=inputs=6:channel_layout=5.1:map=0.0-FL\,1.0-FR\,2.0-FC\,3.0-SL\,4.0-SR\,5.0-LFE' +out +@end example + @section resample Convert the audio sample format, sample rate and channel layout. This filter is not meant to be used directly, it is inserted automatically by libavfilter diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 377956e6af..5fa85cebe4 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -32,6 +32,7 @@ OBJS-$(CONFIG_ANULL_FILTER) += af_anull.o OBJS-$(CONFIG_ASPLIT_FILTER) += split.o OBJS-$(CONFIG_ASYNCTS_FILTER) += af_asyncts.o OBJS-$(CONFIG_CHANNELSPLIT_FILTER) += af_channelsplit.o +OBJS-$(CONFIG_JOIN_FILTER) += af_join.o OBJS-$(CONFIG_RESAMPLE_FILTER) += af_resample.o OBJS-$(CONFIG_ANULLSRC_FILTER) += asrc_anullsrc.o diff --git a/libavfilter/af_join.c b/libavfilter/af_join.c new file mode 100644 index 0000000000..ab5e9055b3 --- /dev/null +++ b/libavfilter/af_join.c @@ -0,0 +1,500 @@ +/* + * + * This file is part of Libav. + * + * Libav 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. + * + * Libav 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Audio join filter + * + * Join multiple audio inputs as different channels in + * a single output + */ + +#include "libavutil/audioconvert.h" +#include "libavutil/avassert.h" +#include "libavutil/opt.h" + +#include "audio.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" + +typedef struct ChannelMap { + int input; ///< input stream index + int in_channel_idx; ///< index of in_channel in the input stream data + uint64_t in_channel; ///< layout describing the input channel + uint64_t out_channel; ///< layout describing the output channel +} ChannelMap; + +typedef struct JoinContext { + const AVClass *class; + + int inputs; + char *map; + char *channel_layout_str; + uint64_t channel_layout; + + int nb_channels; + ChannelMap *channels; + + /** + * Temporary storage for input frames, until we get one on each input. + */ + AVFilterBufferRef **input_frames; + + /** + * Temporary storage for data pointers, for assembling the output buffer. + */ + uint8_t **data; +} JoinContext; + +/** + * To avoid copying the data from input buffers, this filter creates + * a custom output buffer that stores references to all inputs and + * unrefs them on free. + */ +typedef struct JoinBufferPriv { + AVFilterBufferRef **in_buffers; + int nb_in_buffers; +} JoinBufferPriv; + +#define OFFSET(x) offsetof(JoinContext, x) +#define A AV_OPT_FLAG_AUDIO_PARAM +static const AVOption join_options[] = { + { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { 2 }, 1, INT_MAX, A }, + { "channel_layout", "Channel layout of the " + "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A }, + { "map", "A comma-separated list of channels maps in the format " + "'input_stream.input_channel-output_channel.", + OFFSET(map), AV_OPT_TYPE_STRING, .flags = A }, + { NULL }, +}; + +static const AVClass join_class = { + .class_name = "join filter", + .item_name = av_default_item_name, + .option = join_options, + .version = LIBAVUTIL_VERSION_INT, +}; + +static void filter_samples(AVFilterLink *link, AVFilterBufferRef *buf) +{ + AVFilterContext *ctx = link->dst; + JoinContext *s = ctx->priv; + int i; + + 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] = buf; +} + +static int parse_maps(AVFilterContext *ctx) +{ + JoinContext *s = ctx->priv; + char *cur = s->map; + + while (cur && *cur) { + char *sep, *next, *p; + uint64_t in_channel = 0, out_channel = 0; + int input_idx, out_ch_idx, in_ch_idx; + + next = strchr(cur, ','); + if (next) + *next++ = 0; + + /* split the map into input and output parts */ + if (!(sep = strchr(cur, '-'))) { + av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel " + "map '%s'\n", cur); + return AVERROR(EINVAL); + } + *sep++ = 0; + +#define PARSE_CHANNEL(str, var, inout) \ + if (!(var = av_get_channel_layout(str))) { \ + av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\ + return AVERROR(EINVAL); \ + } \ + if (av_get_channel_layout_nb_channels(var) != 1) { \ + av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \ + inout " channel.\n"); \ + return AVERROR(EINVAL); \ + } + + /* parse output channel */ + PARSE_CHANNEL(sep, out_channel, "output"); + if (!(out_channel & s->channel_layout)) { + av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in " + "requested channel layout.\n", sep); + return AVERROR(EINVAL); + } + + out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout, + out_channel); + if (s->channels[out_ch_idx].input >= 0) { + av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel " + "'%s'.\n", sep); + return AVERROR(EINVAL); + } + + /* parse input channel */ + input_idx = strtol(cur, &cur, 0); + if (input_idx < 0 || input_idx >= s->inputs) { + av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n", + input_idx); + return AVERROR(EINVAL); + } + + if (*cur) + cur++; + + in_ch_idx = strtol(cur, &p, 0); + if (p == cur) { + /* channel specifier is not a number, + * try to parse as channel name */ + PARSE_CHANNEL(cur, in_channel, "input"); + } + + s->channels[out_ch_idx].input = input_idx; + if (in_channel) + s->channels[out_ch_idx].in_channel = in_channel; + else + s->channels[out_ch_idx].in_channel_idx = in_ch_idx; + + cur = next; + } + return 0; +} + +static int join_init(AVFilterContext *ctx, const char *args, void *opaque) +{ + JoinContext *s = ctx->priv; + int ret, i; + + s->class = &join_class; + av_opt_set_defaults(s); + if ((ret = av_set_options_string(s, args, "=", ":")) < 0) { + av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args); + return ret; + } + + if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) { + av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n", + s->channel_layout_str); + ret = AVERROR(EINVAL); + goto fail; + } + + s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout); + s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels); + s->data = av_mallocz(sizeof(*s->data) * s->nb_channels); + s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs); + if (!s->channels || !s->data || !s->input_frames) { + ret = AVERROR(ENOMEM); + goto fail; + } + + for (i = 0; i < s->nb_channels; i++) { + s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i); + s->channels[i].input = -1; + } + + if ((ret = parse_maps(ctx)) < 0) + goto fail; + + for (i = 0; i < s->inputs; i++) { + char name[32]; + AVFilterPad pad = { 0 }; + + snprintf(name, sizeof(name), "input%d", i); + pad.type = AVMEDIA_TYPE_AUDIO; + pad.name = av_strdup(name); + pad.filter_samples = filter_samples; + + pad.needs_fifo = 1; + + ff_insert_inpad(ctx, i, &pad); + } + +fail: + av_opt_free(s); + return ret; +} + +static void join_uninit(AVFilterContext *ctx) +{ + JoinContext *s = ctx->priv; + int i; + + for (i = 0; i < ctx->nb_inputs; i++) { + av_freep(&ctx->input_pads[i].name); + avfilter_unref_buffer(s->input_frames[i]); + } + + av_freep(&s->channels); + av_freep(&s->data); + av_freep(&s->input_frames); +} + +static int join_query_formats(AVFilterContext *ctx) +{ + JoinContext *s = ctx->priv; + AVFilterChannelLayouts *layouts = NULL; + int i; + + ff_add_channel_layout(&layouts, s->channel_layout); + ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts); + + for (i = 0; i < ctx->nb_inputs; i++) + ff_channel_layouts_ref(ff_all_channel_layouts(), + &ctx->inputs[i]->out_channel_layouts); + + ff_set_common_formats (ctx, ff_planar_sample_fmts()); + ff_set_common_samplerates(ctx, ff_all_samplerates()); + + return 0; +} + +static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch, + uint64_t *inputs) +{ + int i; + + for (i = 0; i < ctx->nb_inputs; i++) { + AVFilterLink *link = ctx->inputs[i]; + + if (ch->out_channel & link->channel_layout && + !(ch->out_channel & inputs[i])) { + ch->input = i; + ch->in_channel = ch->out_channel; + inputs[i] |= ch->out_channel; + return; + } + } +} + +static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch, + uint64_t *inputs) +{ + int i; + + for (i = 0; i < ctx->nb_inputs; i++) { + AVFilterLink *link = ctx->inputs[i]; + + if ((inputs[i] & link->channel_layout) != link->channel_layout) { + uint64_t unused = link->channel_layout & ~inputs[i]; + + ch->input = i; + ch->in_channel = av_channel_layout_extract_channel(unused, 0); + inputs[i] |= ch->in_channel; + return; + } + } +} + +static int join_config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + JoinContext *s = ctx->priv; + uint64_t *inputs; // nth element tracks which channels are used from nth input + int i, ret = 0; + + /* initialize inputs to user-specified mappings */ + if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs))) + return AVERROR(ENOMEM); + for (i = 0; i < s->nb_channels; i++) { + ChannelMap *ch = &s->channels[i]; + AVFilterLink *inlink; + + if (ch->input < 0) + continue; + + inlink = ctx->inputs[ch->input]; + + if (!ch->in_channel) + ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout, + ch->in_channel_idx); + + if (!(ch->in_channel & inlink->channel_layout)) { + av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in " + "input stream #%d.\n", av_get_channel_name(ch->in_channel), + ch->input); + ret = AVERROR(EINVAL); + goto fail; + } + + inputs[ch->input] |= ch->in_channel; + } + + /* guess channel maps when not explicitly defined */ + /* first try unused matching channels */ + for (i = 0; i < s->nb_channels; i++) { + ChannelMap *ch = &s->channels[i]; + + if (ch->input < 0) + guess_map_matching(ctx, ch, inputs); + } + + /* if the above failed, try to find _any_ unused input channel */ + for (i = 0; i < s->nb_channels; i++) { + ChannelMap *ch = &s->channels[i]; + + if (ch->input < 0) + guess_map_any(ctx, ch, inputs); + + if (ch->input < 0) { + av_log(ctx, AV_LOG_ERROR, "Could not find input channel for " + "output channel '%s'.\n", + av_get_channel_name(ch->out_channel)); + goto fail; + } + + ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout, + ch->in_channel); + } + + /* print mappings */ + av_log(ctx, AV_LOG_VERBOSE, "mappings: "); + for (i = 0; i < s->nb_channels; i++) { + ChannelMap *ch = &s->channels[i]; + av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input, + av_get_channel_name(ch->in_channel), + av_get_channel_name(ch->out_channel)); + } + av_log(ctx, AV_LOG_VERBOSE, "\n"); + + for (i = 0; i < ctx->nb_inputs; i++) { + if (!inputs[i]) + av_log(ctx, AV_LOG_WARNING, "No channels are used from input " + "stream %d.\n", i); + } + +fail: + av_freep(&inputs); + return ret; +} + +static void join_free_buffer(AVFilterBuffer *buf) +{ + JoinBufferPriv *priv = buf->priv; + + if (priv) { + int i; + + for (i = 0; i < priv->nb_in_buffers; i++) + avfilter_unref_buffer(priv->in_buffers[i]); + + av_freep(&priv->in_buffers); + av_freep(&buf->priv); + } + + if (buf->extended_data != buf->data) + av_freep(&buf->extended_data); + av_freep(&buf); +} + +static int join_request_frame(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + JoinContext *s = ctx->priv; + AVFilterBufferRef *buf; + JoinBufferPriv *priv; + int linesize = INT_MAX; + int perms = ~0; + int nb_samples; + int i, j, ret; + + /* get a frame on each input */ + for (i = 0; i < ctx->nb_inputs; i++) { + AVFilterLink *inlink = ctx->inputs[i]; + + if (!s->input_frames[i] && + (ret = ff_request_frame(inlink)) < 0) + return ret; + + /* request the same number of samples on all inputs */ + if (i == 0) { + nb_samples = s->input_frames[0]->audio->nb_samples; + + for (j = 1; !i && j < ctx->nb_inputs; j++) + ctx->inputs[j]->request_samples = nb_samples; + } + } + + for (i = 0; i < s->nb_channels; i++) { + ChannelMap *ch = &s->channels[i]; + AVFilterBufferRef *cur_buf = s->input_frames[ch->input]; + + s->data[i] = cur_buf->extended_data[ch->in_channel_idx]; + linesize = FFMIN(linesize, cur_buf->linesize[0]); + perms &= cur_buf->perms; + } + + buf = avfilter_get_audio_buffer_ref_from_arrays(s->data, linesize, perms, + nb_samples, outlink->format, + outlink->channel_layout); + if (!buf) + return AVERROR(ENOMEM); + + buf->buf->free = join_free_buffer; + buf->pts = s->input_frames[0]->pts; + + if (!(priv = av_mallocz(sizeof(*priv)))) + goto fail; + if (!(priv->in_buffers = av_mallocz(sizeof(*priv->in_buffers) * ctx->nb_inputs))) + goto fail; + + for (i = 0; i < ctx->nb_inputs; i++) + priv->in_buffers[i] = s->input_frames[i]; + priv->nb_in_buffers = ctx->nb_inputs; + buf->buf->priv = priv; + + ff_filter_samples(outlink, buf); + + memset(s->input_frames, 0, sizeof(*s->input_frames) * ctx->nb_inputs); + + return 0; + +fail: + avfilter_unref_buffer(buf); + if (priv) + av_freep(&priv->in_buffers); + av_freep(&priv); + return AVERROR(ENOMEM); +} + +AVFilter avfilter_af_join = { + .name = "join", + .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into " + "multi-channel output"), + .priv_size = sizeof(JoinContext), + + .init = join_init, + .uninit = join_uninit, + .query_formats = join_query_formats, + + .inputs = (const AVFilterPad[]){{ NULL }}, + .outputs = (const AVFilterPad[]){{ .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = join_config_output, + .request_frame = join_request_frame, }, + { NULL }}, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 5a249c08c8..786c2dea7b 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -41,6 +41,7 @@ void avfilter_register_all(void) REGISTER_FILTER (ASPLIT, asplit, af); REGISTER_FILTER (ASYNCTS, asyncts, af); REGISTER_FILTER (CHANNELSPLIT,channelsplit,af); + REGISTER_FILTER (JOIN, join, af); REGISTER_FILTER (RESAMPLE, resample, af); REGISTER_FILTER (ANULLSRC, anullsrc, asrc); -- cgit v1.2.3 From 41e637e44902246efa17babf26e9be8a4a519e6f Mon Sep 17 00:00:00 2001 From: Alex Converse Date: Fri, 8 Jun 2012 18:42:53 -0700 Subject: lavfi: Add the af_channelmap audio channel mapping filter. Inspired by MPlayer's af_channels filter and SoX's remix effect. --- Changelog | 1 + doc/filters.texi | 33 ++++ libavfilter/Makefile | 1 + libavfilter/af_channelmap.c | 402 ++++++++++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 5 files changed, 438 insertions(+) create mode 100644 libavfilter/af_channelmap.c (limited to 'doc') diff --git a/Changelog b/Changelog index 03287673e8..95f79bcd6e 100644 --- a/Changelog +++ b/Changelog @@ -29,6 +29,7 @@ version : - iLBC encoding/decoding via libilbc - Microsoft Screen 1 decoder - join audio filter +- audio channel mapping filter version 0.8: diff --git a/doc/filters.texi b/doc/filters.texi index e40f8798cc..218cd600ee 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -232,6 +232,39 @@ front_center.wav -map '[LFE]' lfe.wav -map '[SL]' side_left.wav -map '[SR]' side_right.wav @end example +@section channelmap +Remap input channels to new locations. + +This filter accepts the following named parameters: +@table @option +@item channel_layout +Channel layout of the output stream. + +@item map +Map channels from input to output. The argument is a comma-separated list of +mappings, each in the @code{@var{in_channel}-@var{out_channel}} or +@var{in_channel} form. @var{in_channel} can be either the name of the input +channel (e.g. FL for front left) or its index in the input channel layout. +@var{out_channel} is the name of the output channel or its index in the output +channel layout. If @var{out_channel} is not given then it is implicitly an +index, starting with zero and increasing by one for each mapping. +@end table + +If no mapping is present, the filter will implicitly map input channels to +output channels preserving index. + +For example, assuming a 5.1+downmix input MOV file +@example +avconv -i in.mov -filter 'channelmap=map=DL-FL\,DR-FR' out.wav +@end example +will create an output WAV file tagged as stereo from the downmix channels of +the input. + +To fix a 5.1 WAV improperly encoded in AAC's native channel order +@example +avconv -i in.wav -filter 'channelmap=1\,2\,0\,5\,3\,4:channel_layout=5.1' out.wav +@end example + @section join Join multiple input streams into one multi-channel stream. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 5fa85cebe4..c77d450d7e 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -31,6 +31,7 @@ OBJS-$(CONFIG_AMIX_FILTER) += af_amix.o OBJS-$(CONFIG_ANULL_FILTER) += af_anull.o OBJS-$(CONFIG_ASPLIT_FILTER) += split.o OBJS-$(CONFIG_ASYNCTS_FILTER) += af_asyncts.o +OBJS-$(CONFIG_CHANNELMAP_FILTER) += af_channelmap.o OBJS-$(CONFIG_CHANNELSPLIT_FILTER) += af_channelsplit.o OBJS-$(CONFIG_JOIN_FILTER) += af_join.o OBJS-$(CONFIG_RESAMPLE_FILTER) += af_resample.o diff --git a/libavfilter/af_channelmap.c b/libavfilter/af_channelmap.c new file mode 100644 index 0000000000..14c2c0cb5a --- /dev/null +++ b/libavfilter/af_channelmap.c @@ -0,0 +1,402 @@ +/* + * Copyright (c) 2012 Google, Inc. + * + * This file is part of Libav. + * + * Libav 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. + * + * Libav 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * audio channel mapping filter + */ + +#include + +#include "libavutil/audioconvert.h" +#include "libavutil/avstring.h" +#include "libavutil/mathematics.h" +#include "libavutil/opt.h" +#include "libavutil/samplefmt.h" + +#include "audio.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" + +struct ChannelMap { + uint64_t in_channel; + uint64_t out_channel; + int in_channel_idx; + int out_channel_idx; +}; + +enum MappingMode { + MAP_NONE, + MAP_ONE_INT, + MAP_ONE_STR, + MAP_PAIR_INT_INT, + MAP_PAIR_INT_STR, + MAP_PAIR_STR_INT, + MAP_PAIR_STR_STR +}; + +#define MAX_CH 64 +typedef struct ChannelMapContext { + const AVClass *class; + AVFilterChannelLayouts *channel_layouts; + char *mapping_str; + char *channel_layout_str; + uint64_t output_layout; + struct ChannelMap map[MAX_CH]; + int nch; + enum MappingMode mode; +} ChannelMapContext; + +#define OFFSET(x) offsetof(ChannelMapContext, x) +#define A AV_OPT_FLAG_AUDIO_PARAM +static const AVOption options[] = { + { "map", "A comma-separated list of input channel numbers in output order.", + OFFSET(mapping_str), AV_OPT_TYPE_STRING, .flags = A }, + { "channel_layout", "Output channel layout.", + OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A }, + { NULL }, +}; + +static const AVClass channelmap_class = { + .class_name = "channel map filter", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +static char* split(char *message, char delim) { + char *next = strchr(message, delim); + if (next) + *next++ = '\0'; + return next; +} + +static int get_channel_idx(char **map, int *ch, char delim, int max_ch) +{ + char *next = split(*map, delim); + int len; + int n = 0; + if (!next && delim == '-') + return AVERROR(EINVAL); + len = strlen(*map); + sscanf(*map, "%d%n", ch, &n); + if (n != len) + return AVERROR(EINVAL); + if (*ch < 0 || *ch > max_ch) + return AVERROR(EINVAL); + *map = next; + return 0; +} + +static int get_channel(char **map, uint64_t *ch, char delim) +{ + char *next = split(*map, delim); + if (!next && delim == '-') + return AVERROR(EINVAL); + *ch = av_get_channel_layout(*map); + if (av_get_channel_layout_nb_channels(*ch) != 1) + return AVERROR(EINVAL); + *map = next; + return 0; +} + +static av_cold int channelmap_init(AVFilterContext *ctx, const char *args, + void *opaque) +{ + ChannelMapContext *s = ctx->priv; + int ret; + char *mapping; + enum mode; + int map_entries = 0; + char buf[256]; + enum MappingMode mode; + uint64_t out_ch_mask = 0; + int i; + + if (!args) { + av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n"); + return AVERROR(EINVAL); + } + + s->class = &channelmap_class; + av_opt_set_defaults(s); + + if ((ret = av_set_options_string(s, args, "=", ":")) < 0) { + av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args); + return ret; + } + + mapping = s->mapping_str; + + if (!mapping) { + mode = MAP_NONE; + } else { + char *dash = strchr(mapping, '-'); + if (!dash) { // short mapping + if (isdigit(*mapping)) + mode = MAP_ONE_INT; + else + mode = MAP_ONE_STR; + } else if (isdigit(*mapping)) { + if (isdigit(*(dash+1))) + mode = MAP_PAIR_INT_INT; + else + mode = MAP_PAIR_INT_STR; + } else { + if (isdigit(*(dash+1))) + mode = MAP_PAIR_STR_INT; + else + mode = MAP_PAIR_STR_STR; + } + } + + if (mode != MAP_NONE) { + char *comma = mapping; + map_entries = 1; + while ((comma = strchr(comma, ','))) { + if (*++comma) // Allow trailing comma + map_entries++; + } + } + + if (map_entries > MAX_CH) { + av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries); + ret = AVERROR(EINVAL); + goto fail; + } + + for (i = 0; i < map_entries; i++) { + int in_ch_idx = -1, out_ch_idx = -1; + uint64_t in_ch = 0, out_ch = 0; + static const char err[] = "Failed to parse channel map\n"; + switch (mode) { + case MAP_ONE_INT: + if (get_channel_idx(&mapping, &in_ch_idx, ',', MAX_CH) < 0) { + ret = AVERROR(EINVAL); + av_log(ctx, AV_LOG_ERROR, err); + goto fail; + } + s->map[i].in_channel_idx = in_ch_idx; + s->map[i].out_channel_idx = i; + break; + case MAP_ONE_STR: + if (!get_channel(&mapping, &in_ch, ',')) { + av_log(ctx, AV_LOG_ERROR, err); + ret = AVERROR(EINVAL); + goto fail; + } + s->map[i].in_channel = in_ch; + s->map[i].out_channel_idx = i; + break; + case MAP_PAIR_INT_INT: + if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 || + get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) { + av_log(ctx, AV_LOG_ERROR, err); + ret = AVERROR(EINVAL); + goto fail; + } + s->map[i].in_channel_idx = in_ch_idx; + s->map[i].out_channel_idx = out_ch_idx; + break; + case MAP_PAIR_INT_STR: + if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 || + get_channel(&mapping, &out_ch, ',') < 0 || + out_ch & out_ch_mask) { + av_log(ctx, AV_LOG_ERROR, err); + ret = AVERROR(EINVAL); + goto fail; + } + s->map[i].in_channel_idx = in_ch_idx; + s->map[i].out_channel = out_ch; + out_ch_mask |= out_ch; + break; + case MAP_PAIR_STR_INT: + if (get_channel(&mapping, &in_ch, '-') < 0 || + get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) { + av_log(ctx, AV_LOG_ERROR, err); + ret = AVERROR(EINVAL); + goto fail; + } + s->map[i].in_channel = in_ch; + s->map[i].out_channel_idx = out_ch_idx; + break; + case MAP_PAIR_STR_STR: + if (get_channel(&mapping, &in_ch, '-') < 0 || + get_channel(&mapping, &out_ch, ',') < 0 || + out_ch & out_ch_mask) { + av_log(ctx, AV_LOG_ERROR, err); + ret = AVERROR(EINVAL); + goto fail; + } + s->map[i].in_channel = in_ch; + s->map[i].out_channel = out_ch; + out_ch_mask |= out_ch; + break; + } + } + s->mode = mode; + s->nch = map_entries; + s->output_layout = out_ch_mask ? out_ch_mask : + av_get_default_channel_layout(map_entries); + + if (s->channel_layout_str) { + uint64_t fmt; + if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) { + av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n", + s->channel_layout_str); + ret = AVERROR(EINVAL); + goto fail; + } + if (mode == MAP_NONE) { + int i; + s->nch = av_get_channel_layout_nb_channels(fmt); + for (i = 0; i < s->nch; i++) { + s->map[i].in_channel_idx = i; + s->map[i].out_channel_idx = i; + } + } else if (out_ch_mask && out_ch_mask != fmt) { + av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask); + av_log(ctx, AV_LOG_ERROR, + "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n", + s->channel_layout_str, buf); + ret = AVERROR(EINVAL); + goto fail; + } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) { + av_log(ctx, AV_LOG_ERROR, + "Output channel layout %s does not match the number of channels mapped %d.\n", + s->channel_layout_str, s->nch); + ret = AVERROR(EINVAL); + goto fail; + } + s->output_layout = fmt; + } + ff_add_channel_layout(&s->channel_layouts, s->output_layout); + + if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) { + for (i = 0; i < s->nch; i++) { + s->map[i].out_channel_idx = av_get_channel_layout_channel_index( + s->output_layout, s->map[i].out_channel); + } + } + +fail: + av_opt_free(s); + return ret; +} + +static int channelmap_query_formats(AVFilterContext *ctx) +{ + ChannelMapContext *s = ctx->priv; + + ff_set_common_formats(ctx, ff_planar_sample_fmts()); + ff_set_common_samplerates(ctx, ff_all_samplerates()); + ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts); + ff_channel_layouts_ref(s->channel_layouts, &ctx->outputs[0]->in_channel_layouts); + + return 0; +} + +static void channelmap_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + const ChannelMapContext *s = ctx->priv; + const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout); + const int nch_out = s->nch; + int ch; + uint8_t *source_planes[MAX_CH]; + + memcpy(source_planes, buf->extended_data, + nch_in * sizeof(source_planes[0])); + + if (nch_out > nch_in) { + if (nch_out > FF_ARRAY_ELEMS(buf->data)) { + uint8_t **new_extended_data = + av_mallocz(nch_out * sizeof(*buf->extended_data)); + if (!new_extended_data) + return; + if (buf->extended_data == buf->data) { + buf->extended_data = new_extended_data; + } else { + buf->extended_data = new_extended_data; + av_free(buf->extended_data); + } + } else if (buf->extended_data != buf->data) { + av_free(buf->extended_data); + buf->extended_data = buf->data; + } + } + + for (ch = 0; ch < nch_out; ch++) { + buf->extended_data[s->map[ch].out_channel_idx] = + source_planes[s->map[ch].in_channel_idx]; + } + + if (buf->data != buf->extended_data) + memcpy(buf->data, buf->extended_data, + FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0])); + + ff_filter_samples(outlink, buf); +} + +static int channelmap_config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + ChannelMapContext *s = ctx->priv; + int i, err = 0; + const char *channel_name; + char layout_name[256]; + + if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) { + for (i = 0; i < s->nch; i++) { + s->map[i].in_channel_idx = av_get_channel_layout_channel_index( + inlink->channel_layout, s->map[i].in_channel); + if (s->map[i].in_channel_idx < 0) { + channel_name = av_get_channel_name(s->map[i].in_channel); + av_get_channel_layout_string(layout_name, sizeof(layout_name), + 0, inlink->channel_layout); + av_log(ctx, AV_LOG_ERROR, + "input channel '%s' not available from input layout '%s'\n", + channel_name, layout_name); + err = AVERROR(EINVAL); + } + } + } + + return err; +} + +AVFilter avfilter_af_channelmap = { + .name = "channelmap", + .description = NULL_IF_CONFIG_SMALL("Remap audio channels."), + .init = channelmap_init, + .query_formats = channelmap_query_formats, + .priv_size = sizeof(ChannelMapContext), + + .inputs = (AVFilterPad[]) {{ .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_samples = channelmap_filter_samples, + .config_props = channelmap_config_input }, + { .name = NULL }}, + .outputs = (AVFilterPad[]) {{ .name = "default", + .type = AVMEDIA_TYPE_AUDIO }, + { .name = NULL }}, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 786c2dea7b..9c22b4fe9e 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -40,6 +40,7 @@ void avfilter_register_all(void) REGISTER_FILTER (ANULL, anull, af); REGISTER_FILTER (ASPLIT, asplit, af); REGISTER_FILTER (ASYNCTS, asyncts, af); + REGISTER_FILTER (CHANNELMAP, channelmap, af); REGISTER_FILTER (CHANNELSPLIT,channelsplit,af); REGISTER_FILTER (JOIN, join, af); REGISTER_FILTER (RESAMPLE, resample, af); -- cgit v1.2.3