2 * Copyright (c) 2012 Google, Inc.
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * audio channel mapping filter
28 #include "libavutil/avstring.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/samplefmt.h"
58 typedef struct ChannelMapContext {
60 AVFilterChannelLayouts *channel_layouts;
62 char *channel_layout_str;
63 uint64_t output_layout;
64 struct ChannelMap map[MAX_CH];
66 enum MappingMode mode;
69 #define OFFSET(x) offsetof(ChannelMapContext, x)
70 #define A AV_OPT_FLAG_AUDIO_PARAM
71 #define F AV_OPT_FLAG_FILTERING_PARAM
72 static const AVOption options[] = {
73 { "map", "A comma-separated list of input channel numbers in output order.",
74 OFFSET(mapping_str), AV_OPT_TYPE_STRING, .flags = A|F },
75 { "channel_layout", "Output channel layout.",
76 OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A|F },
80 static const AVClass channelmap_class = {
81 .class_name = "channel map filter",
82 .item_name = av_default_item_name,
84 .version = LIBAVUTIL_VERSION_INT,
87 static char* split(char *message, char delim) {
88 char *next = strchr(message, delim);
94 static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
96 char *next = split(*map, delim);
99 if (!next && delim == '-')
100 return AVERROR(EINVAL);
102 sscanf(*map, "%d%n", ch, &n);
104 return AVERROR(EINVAL);
105 if (*ch < 0 || *ch > max_ch)
106 return AVERROR(EINVAL);
111 static int get_channel(char **map, uint64_t *ch, char delim)
113 char *next = split(*map, delim);
114 if (!next && delim == '-')
115 return AVERROR(EINVAL);
116 *ch = av_get_channel_layout(*map);
117 if (av_get_channel_layout_nb_channels(*ch) != 1)
118 return AVERROR(EINVAL);
123 static av_cold int channelmap_init(AVFilterContext *ctx, const char *args)
125 ChannelMapContext *s = ctx->priv;
130 enum MappingMode mode;
131 uint64_t out_ch_mask = 0;
135 av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
136 return AVERROR(EINVAL);
139 s->class = &channelmap_class;
140 av_opt_set_defaults(s);
142 if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
145 mapping = s->mapping_str;
150 char *dash = strchr(mapping, '-');
151 if (!dash) { // short mapping
152 if (isdigit(*mapping))
156 } else if (isdigit(*mapping)) {
157 if (isdigit(*(dash+1)))
158 mode = MAP_PAIR_INT_INT;
160 mode = MAP_PAIR_INT_STR;
162 if (isdigit(*(dash+1)))
163 mode = MAP_PAIR_STR_INT;
165 mode = MAP_PAIR_STR_STR;
169 if (mode != MAP_NONE) {
170 char *comma = mapping;
172 while ((comma = strchr(comma, ','))) {
173 if (*++comma) // Allow trailing comma
178 if (map_entries > MAX_CH) {
179 av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
180 ret = AVERROR(EINVAL);
184 for (i = 0; i < map_entries; i++) {
185 int in_ch_idx = -1, out_ch_idx = -1;
186 uint64_t in_ch = 0, out_ch = 0;
187 static const char err[] = "Failed to parse channel map\n";
190 if (get_channel_idx(&mapping, &in_ch_idx, ',', MAX_CH) < 0) {
191 ret = AVERROR(EINVAL);
192 av_log(ctx, AV_LOG_ERROR, err);
195 s->map[i].in_channel_idx = in_ch_idx;
196 s->map[i].out_channel_idx = i;
199 if (!get_channel(&mapping, &in_ch, ',')) {
200 av_log(ctx, AV_LOG_ERROR, err);
201 ret = AVERROR(EINVAL);
204 s->map[i].in_channel = in_ch;
205 s->map[i].out_channel_idx = i;
207 case MAP_PAIR_INT_INT:
208 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
209 get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
210 av_log(ctx, AV_LOG_ERROR, err);
211 ret = AVERROR(EINVAL);
214 s->map[i].in_channel_idx = in_ch_idx;
215 s->map[i].out_channel_idx = out_ch_idx;
217 case MAP_PAIR_INT_STR:
218 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
219 get_channel(&mapping, &out_ch, ',') < 0 ||
220 out_ch & out_ch_mask) {
221 av_log(ctx, AV_LOG_ERROR, err);
222 ret = AVERROR(EINVAL);
225 s->map[i].in_channel_idx = in_ch_idx;
226 s->map[i].out_channel = out_ch;
227 out_ch_mask |= out_ch;
229 case MAP_PAIR_STR_INT:
230 if (get_channel(&mapping, &in_ch, '-') < 0 ||
231 get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
232 av_log(ctx, AV_LOG_ERROR, err);
233 ret = AVERROR(EINVAL);
236 s->map[i].in_channel = in_ch;
237 s->map[i].out_channel_idx = out_ch_idx;
239 case MAP_PAIR_STR_STR:
240 if (get_channel(&mapping, &in_ch, '-') < 0 ||
241 get_channel(&mapping, &out_ch, ',') < 0 ||
242 out_ch & out_ch_mask) {
243 av_log(ctx, AV_LOG_ERROR, err);
244 ret = AVERROR(EINVAL);
247 s->map[i].in_channel = in_ch;
248 s->map[i].out_channel = out_ch;
249 out_ch_mask |= out_ch;
254 s->nch = map_entries;
255 s->output_layout = out_ch_mask ? out_ch_mask :
256 av_get_default_channel_layout(map_entries);
258 if (s->channel_layout_str) {
260 if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
261 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
262 s->channel_layout_str);
263 ret = AVERROR(EINVAL);
266 if (mode == MAP_NONE) {
268 s->nch = av_get_channel_layout_nb_channels(fmt);
269 for (i = 0; i < s->nch; i++) {
270 s->map[i].in_channel_idx = i;
271 s->map[i].out_channel_idx = i;
273 } else if (out_ch_mask && out_ch_mask != fmt) {
274 av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
275 av_log(ctx, AV_LOG_ERROR,
276 "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
277 s->channel_layout_str, buf);
278 ret = AVERROR(EINVAL);
280 } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
281 av_log(ctx, AV_LOG_ERROR,
282 "Output channel layout %s does not match the number of channels mapped %d.\n",
283 s->channel_layout_str, s->nch);
284 ret = AVERROR(EINVAL);
287 s->output_layout = fmt;
289 ff_add_channel_layout(&s->channel_layouts, s->output_layout);
291 if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
292 for (i = 0; i < s->nch; i++) {
293 s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
294 s->output_layout, s->map[i].out_channel);
303 static int channelmap_query_formats(AVFilterContext *ctx)
305 ChannelMapContext *s = ctx->priv;
307 ff_set_common_formats(ctx, ff_planar_sample_fmts());
308 ff_set_common_samplerates(ctx, ff_all_samplerates());
309 ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts);
310 ff_channel_layouts_ref(s->channel_layouts, &ctx->outputs[0]->in_channel_layouts);
315 static int channelmap_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
317 AVFilterContext *ctx = inlink->dst;
318 AVFilterLink *outlink = ctx->outputs[0];
319 const ChannelMapContext *s = ctx->priv;
320 const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
321 const int nch_out = s->nch;
323 uint8_t *source_planes[MAX_CH];
325 memcpy(source_planes, buf->extended_data,
326 nch_in * sizeof(source_planes[0]));
328 if (nch_out > nch_in) {
329 if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
330 uint8_t **new_extended_data =
331 av_mallocz(nch_out * sizeof(*buf->extended_data));
332 if (!new_extended_data) {
333 avfilter_unref_buffer(buf);
334 return AVERROR(ENOMEM);
336 if (buf->extended_data == buf->data) {
337 buf->extended_data = new_extended_data;
339 av_free(buf->extended_data);
340 buf->extended_data = new_extended_data;
342 } else if (buf->extended_data != buf->data) {
343 av_free(buf->extended_data);
344 buf->extended_data = buf->data;
348 for (ch = 0; ch < nch_out; ch++) {
349 buf->extended_data[s->map[ch].out_channel_idx] =
350 source_planes[s->map[ch].in_channel_idx];
353 if (buf->data != buf->extended_data)
354 memcpy(buf->data, buf->extended_data,
355 FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
357 return ff_filter_samples(outlink, buf);
360 static int channelmap_config_input(AVFilterLink *inlink)
362 AVFilterContext *ctx = inlink->dst;
363 ChannelMapContext *s = ctx->priv;
365 const char *channel_name;
366 char layout_name[256];
368 if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
369 for (i = 0; i < s->nch; i++) {
370 s->map[i].in_channel_idx = av_get_channel_layout_channel_index(
371 inlink->channel_layout, s->map[i].in_channel);
372 if (s->map[i].in_channel_idx < 0) {
373 channel_name = av_get_channel_name(s->map[i].in_channel);
374 av_get_channel_layout_string(layout_name, sizeof(layout_name),
375 0, inlink->channel_layout);
376 av_log(ctx, AV_LOG_ERROR,
377 "input channel '%s' not available from input layout '%s'\n",
378 channel_name, layout_name);
379 err = AVERROR(EINVAL);
387 static const AVFilterPad avfilter_af_channelmap_inputs[] = {
390 .type = AVMEDIA_TYPE_AUDIO,
391 .min_perms = AV_PERM_READ | AV_PERM_WRITE,
392 .filter_samples = channelmap_filter_samples,
393 .config_props = channelmap_config_input
398 static const AVFilterPad avfilter_af_channelmap_outputs[] = {
401 .type = AVMEDIA_TYPE_AUDIO
406 AVFilter avfilter_af_channelmap = {
407 .name = "channelmap",
408 .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
409 .init = channelmap_init,
410 .query_formats = channelmap_query_formats,
411 .priv_size = sizeof(ChannelMapContext),
413 .inputs = avfilter_af_channelmap_inputs,
414 .outputs = avfilter_af_channelmap_outputs,
415 .priv_class = &channelmap_class,