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 {
61 char *channel_layout_str;
62 uint64_t output_layout;
63 struct ChannelMap map[MAX_CH];
65 enum MappingMode mode;
68 #define OFFSET(x) offsetof(ChannelMapContext, x)
69 #define A AV_OPT_FLAG_AUDIO_PARAM
70 #define F AV_OPT_FLAG_FILTERING_PARAM
71 static const AVOption channelmap_options[] = {
72 { "map", "A comma-separated list of input channel numbers in output order.",
73 OFFSET(mapping_str), AV_OPT_TYPE_STRING, .flags = A|F },
74 { "channel_layout", "Output channel layout.",
75 OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A|F },
79 AVFILTER_DEFINE_CLASS(channelmap);
81 static char* split(char *message, char delim) {
82 char *next = strchr(message, delim);
88 static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
94 return AVERROR(EINVAL);
95 next = split(*map, delim);
96 if (!next && delim == '-')
97 return AVERROR(EINVAL);
99 sscanf(*map, "%d%n", ch, &n);
101 return AVERROR(EINVAL);
102 if (*ch < 0 || *ch > max_ch)
103 return AVERROR(EINVAL);
108 static int get_channel(char **map, uint64_t *ch, char delim)
110 char *next = split(*map, delim);
111 if (!next && delim == '-')
112 return AVERROR(EINVAL);
113 *ch = av_get_channel_layout(*map);
114 if (av_get_channel_layout_nb_channels(*ch) != 1)
115 return AVERROR(EINVAL);
120 static av_cold int channelmap_init(AVFilterContext *ctx)
122 ChannelMapContext *s = ctx->priv;
123 char *mapping, separator = '|';
126 enum MappingMode mode;
127 uint64_t out_ch_mask = 0;
130 mapping = s->mapping_str;
135 char *dash = strchr(mapping, '-');
136 if (!dash) { // short mapping
137 if (av_isdigit(*mapping))
141 } else if (av_isdigit(*mapping)) {
142 if (av_isdigit(*(dash+1)))
143 mode = MAP_PAIR_INT_INT;
145 mode = MAP_PAIR_INT_STR;
147 if (av_isdigit(*(dash+1)))
148 mode = MAP_PAIR_STR_INT;
150 mode = MAP_PAIR_STR_STR;
154 if (mode != MAP_NONE) {
157 while ((sep = strchr(sep, separator))) {
158 if (*++sep) // Allow trailing comma
163 if (map_entries > MAX_CH) {
164 av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
165 return AVERROR(EINVAL);
168 for (i = 0; i < map_entries; i++) {
169 int in_ch_idx = -1, out_ch_idx = -1;
170 uint64_t in_ch = 0, out_ch = 0;
171 static const char err[] = "Failed to parse channel map\n";
174 if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
175 av_log(ctx, AV_LOG_ERROR, err);
176 return AVERROR(EINVAL);
178 s->map[i].in_channel_idx = in_ch_idx;
179 s->map[i].out_channel_idx = i;
182 if (get_channel(&mapping, &in_ch, separator) < 0) {
183 av_log(ctx, AV_LOG_ERROR, err);
184 return AVERROR(EINVAL);
186 s->map[i].in_channel = in_ch;
187 s->map[i].out_channel_idx = i;
189 case MAP_PAIR_INT_INT:
190 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
191 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
192 av_log(ctx, AV_LOG_ERROR, err);
193 return AVERROR(EINVAL);
195 s->map[i].in_channel_idx = in_ch_idx;
196 s->map[i].out_channel_idx = out_ch_idx;
198 case MAP_PAIR_INT_STR:
199 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
200 get_channel(&mapping, &out_ch, separator) < 0 ||
201 out_ch & out_ch_mask) {
202 av_log(ctx, AV_LOG_ERROR, err);
203 return AVERROR(EINVAL);
205 s->map[i].in_channel_idx = in_ch_idx;
206 s->map[i].out_channel = out_ch;
207 out_ch_mask |= out_ch;
209 case MAP_PAIR_STR_INT:
210 if (get_channel(&mapping, &in_ch, '-') < 0 ||
211 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
212 av_log(ctx, AV_LOG_ERROR, err);
213 return AVERROR(EINVAL);
215 s->map[i].in_channel = in_ch;
216 s->map[i].out_channel_idx = out_ch_idx;
218 case MAP_PAIR_STR_STR:
219 if (get_channel(&mapping, &in_ch, '-') < 0 ||
220 get_channel(&mapping, &out_ch, separator) < 0 ||
221 out_ch & out_ch_mask) {
222 av_log(ctx, AV_LOG_ERROR, err);
223 return AVERROR(EINVAL);
225 s->map[i].in_channel = in_ch;
226 s->map[i].out_channel = out_ch;
227 out_ch_mask |= out_ch;
232 s->nch = map_entries;
233 s->output_layout = out_ch_mask ? out_ch_mask :
234 av_get_default_channel_layout(map_entries);
236 if (s->channel_layout_str) {
238 if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
239 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
240 s->channel_layout_str);
241 return AVERROR(EINVAL);
243 if (mode == MAP_NONE) {
245 s->nch = av_get_channel_layout_nb_channels(fmt);
246 for (i = 0; i < s->nch; i++) {
247 s->map[i].in_channel_idx = i;
248 s->map[i].out_channel_idx = i;
250 } else if (out_ch_mask && out_ch_mask != fmt) {
251 av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
252 av_log(ctx, AV_LOG_ERROR,
253 "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
254 s->channel_layout_str, buf);
255 return AVERROR(EINVAL);
256 } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
257 av_log(ctx, AV_LOG_ERROR,
258 "Output channel layout %s does not match the number of channels mapped %d.\n",
259 s->channel_layout_str, s->nch);
260 return AVERROR(EINVAL);
262 s->output_layout = fmt;
264 if (!s->output_layout) {
265 av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and "
266 "cannot be guessed from the maps.\n");
267 return AVERROR(EINVAL);
270 if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
271 for (i = 0; i < s->nch; i++) {
272 s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
273 s->output_layout, s->map[i].out_channel);
280 static int channelmap_query_formats(AVFilterContext *ctx)
282 ChannelMapContext *s = ctx->priv;
283 AVFilterChannelLayouts *layouts;
284 AVFilterChannelLayouts *channel_layouts = NULL;
287 layouts = ff_all_channel_counts();
289 ret = AVERROR(ENOMEM);
292 if ((ret = ff_add_channel_layout (&channel_layouts, s->output_layout )) < 0 ||
293 (ret = ff_set_common_formats (ctx , ff_planar_sample_fmts() )) < 0 ||
294 (ret = ff_set_common_samplerates (ctx , ff_all_samplerates() )) < 0 ||
295 (ret = ff_channel_layouts_ref (layouts , &ctx->inputs[0]->out_channel_layouts)) < 0 ||
296 (ret = ff_channel_layouts_ref (channel_layouts , &ctx->outputs[0]->in_channel_layouts)) < 0)
302 av_freep(&layouts->channel_layouts);
307 static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
309 AVFilterContext *ctx = inlink->dst;
310 AVFilterLink *outlink = ctx->outputs[0];
311 const ChannelMapContext *s = ctx->priv;
312 const int nch_in = inlink->channels;
313 const int nch_out = s->nch;
315 uint8_t *source_planes[MAX_CH];
317 memcpy(source_planes, buf->extended_data,
318 nch_in * sizeof(source_planes[0]));
320 if (nch_out > nch_in) {
321 if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
322 uint8_t **new_extended_data =
323 av_mallocz_array(nch_out, sizeof(*buf->extended_data));
324 if (!new_extended_data) {
326 return AVERROR(ENOMEM);
328 if (buf->extended_data == buf->data) {
329 buf->extended_data = new_extended_data;
331 av_free(buf->extended_data);
332 buf->extended_data = new_extended_data;
334 } else if (buf->extended_data != buf->data) {
335 av_free(buf->extended_data);
336 buf->extended_data = buf->data;
340 for (ch = 0; ch < nch_out; ch++) {
341 buf->extended_data[s->map[ch].out_channel_idx] =
342 source_planes[s->map[ch].in_channel_idx];
345 if (buf->data != buf->extended_data)
346 memcpy(buf->data, buf->extended_data,
347 FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
349 buf->channel_layout = outlink->channel_layout;
350 buf->channels = outlink->channels;
352 return ff_filter_frame(outlink, buf);
355 static int channelmap_config_input(AVFilterLink *inlink)
357 AVFilterContext *ctx = inlink->dst;
358 ChannelMapContext *s = ctx->priv;
359 int nb_channels = inlink->channels;
361 const char *channel_name;
362 char layout_name[256];
364 for (i = 0; i < s->nch; i++) {
365 struct ChannelMap *m = &s->map[i];
367 if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
368 m->in_channel_idx = av_get_channel_layout_channel_index(
369 inlink->channel_layout, m->in_channel);
372 if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
373 av_get_channel_layout_string(layout_name, sizeof(layout_name),
374 nb_channels, inlink->channel_layout);
376 channel_name = av_get_channel_name(m->in_channel);
377 av_log(ctx, AV_LOG_ERROR,
378 "input channel '%s' not available from input layout '%s'\n",
379 channel_name, layout_name);
381 av_log(ctx, AV_LOG_ERROR,
382 "input channel #%d not available from input layout '%s'\n",
383 m->in_channel_idx, layout_name);
385 err = AVERROR(EINVAL);
392 static const AVFilterPad avfilter_af_channelmap_inputs[] = {
395 .type = AVMEDIA_TYPE_AUDIO,
396 .filter_frame = channelmap_filter_frame,
397 .config_props = channelmap_config_input,
403 static const AVFilterPad avfilter_af_channelmap_outputs[] = {
406 .type = AVMEDIA_TYPE_AUDIO
411 AVFilter ff_af_channelmap = {
412 .name = "channelmap",
413 .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
414 .init = channelmap_init,
415 .query_formats = channelmap_query_formats,
416 .priv_size = sizeof(ChannelMapContext),
417 .priv_class = &channelmap_class,
418 .inputs = avfilter_af_channelmap_inputs,
419 .outputs = avfilter_af_channelmap_outputs,