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)
125 ChannelMapContext *s = ctx->priv;
127 char *mapping, separator = '|';
130 enum MappingMode mode;
131 uint64_t out_ch_mask = 0;
134 mapping = s->mapping_str;
139 char *dash = strchr(mapping, '-');
140 if (!dash) { // short mapping
141 if (av_isdigit(*mapping))
145 } else if (av_isdigit(*mapping)) {
146 if (av_isdigit(*(dash+1)))
147 mode = MAP_PAIR_INT_INT;
149 mode = MAP_PAIR_INT_STR;
151 if (av_isdigit(*(dash+1)))
152 mode = MAP_PAIR_STR_INT;
154 mode = MAP_PAIR_STR_STR;
156 #if FF_API_OLD_FILTER_OPTS
157 if (strchr(mapping, ',')) {
158 av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
159 "'|' to separate the mappings.\n");
165 if (mode != MAP_NONE) {
168 while ((sep = strchr(sep, separator))) {
169 if (*++sep) // Allow trailing comma
174 if (map_entries > MAX_CH) {
175 av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
176 ret = AVERROR(EINVAL);
180 for (i = 0; i < map_entries; i++) {
181 int in_ch_idx = -1, out_ch_idx = -1;
182 uint64_t in_ch = 0, out_ch = 0;
183 static const char err[] = "Failed to parse channel map\n";
186 if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
187 ret = AVERROR(EINVAL);
188 av_log(ctx, AV_LOG_ERROR, err);
191 s->map[i].in_channel_idx = in_ch_idx;
192 s->map[i].out_channel_idx = i;
195 if (!get_channel(&mapping, &in_ch, separator)) {
196 av_log(ctx, AV_LOG_ERROR, err);
197 ret = AVERROR(EINVAL);
200 s->map[i].in_channel = in_ch;
201 s->map[i].out_channel_idx = i;
203 case MAP_PAIR_INT_INT:
204 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
205 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
206 av_log(ctx, AV_LOG_ERROR, err);
207 ret = AVERROR(EINVAL);
210 s->map[i].in_channel_idx = in_ch_idx;
211 s->map[i].out_channel_idx = out_ch_idx;
213 case MAP_PAIR_INT_STR:
214 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
215 get_channel(&mapping, &out_ch, separator) < 0 ||
216 out_ch & out_ch_mask) {
217 av_log(ctx, AV_LOG_ERROR, err);
218 ret = AVERROR(EINVAL);
221 s->map[i].in_channel_idx = in_ch_idx;
222 s->map[i].out_channel = out_ch;
223 out_ch_mask |= out_ch;
225 case MAP_PAIR_STR_INT:
226 if (get_channel(&mapping, &in_ch, '-') < 0 ||
227 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
228 av_log(ctx, AV_LOG_ERROR, err);
229 ret = AVERROR(EINVAL);
232 s->map[i].in_channel = in_ch;
233 s->map[i].out_channel_idx = out_ch_idx;
235 case MAP_PAIR_STR_STR:
236 if (get_channel(&mapping, &in_ch, '-') < 0 ||
237 get_channel(&mapping, &out_ch, separator) < 0 ||
238 out_ch & out_ch_mask) {
239 av_log(ctx, AV_LOG_ERROR, err);
240 ret = AVERROR(EINVAL);
243 s->map[i].in_channel = in_ch;
244 s->map[i].out_channel = out_ch;
245 out_ch_mask |= out_ch;
250 s->nch = map_entries;
251 s->output_layout = out_ch_mask ? out_ch_mask :
252 av_get_default_channel_layout(map_entries);
254 if (s->channel_layout_str) {
256 if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
257 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
258 s->channel_layout_str);
259 ret = AVERROR(EINVAL);
262 if (mode == MAP_NONE) {
264 s->nch = av_get_channel_layout_nb_channels(fmt);
265 for (i = 0; i < s->nch; i++) {
266 s->map[i].in_channel_idx = i;
267 s->map[i].out_channel_idx = i;
269 } else if (out_ch_mask && out_ch_mask != fmt) {
270 av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
271 av_log(ctx, AV_LOG_ERROR,
272 "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
273 s->channel_layout_str, buf);
274 ret = AVERROR(EINVAL);
276 } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
277 av_log(ctx, AV_LOG_ERROR,
278 "Output channel layout %s does not match the number of channels mapped %d.\n",
279 s->channel_layout_str, s->nch);
280 ret = AVERROR(EINVAL);
283 s->output_layout = fmt;
285 ff_add_channel_layout(&s->channel_layouts, s->output_layout);
287 if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
288 for (i = 0; i < s->nch; i++) {
289 s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
290 s->output_layout, s->map[i].out_channel);
299 static int channelmap_query_formats(AVFilterContext *ctx)
301 ChannelMapContext *s = ctx->priv;
303 ff_set_common_formats(ctx, ff_planar_sample_fmts());
304 ff_set_common_samplerates(ctx, ff_all_samplerates());
305 ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts);
306 ff_channel_layouts_ref(s->channel_layouts, &ctx->outputs[0]->in_channel_layouts);
311 static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
313 AVFilterContext *ctx = inlink->dst;
314 AVFilterLink *outlink = ctx->outputs[0];
315 const ChannelMapContext *s = ctx->priv;
316 const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
317 const int nch_out = s->nch;
319 uint8_t *source_planes[MAX_CH];
321 memcpy(source_planes, buf->extended_data,
322 nch_in * sizeof(source_planes[0]));
324 if (nch_out > nch_in) {
325 if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
326 uint8_t **new_extended_data =
327 av_mallocz(nch_out * sizeof(*buf->extended_data));
328 if (!new_extended_data) {
330 return AVERROR(ENOMEM);
332 if (buf->extended_data == buf->data) {
333 buf->extended_data = new_extended_data;
335 av_free(buf->extended_data);
336 buf->extended_data = new_extended_data;
338 } else if (buf->extended_data != buf->data) {
339 av_free(buf->extended_data);
340 buf->extended_data = buf->data;
344 for (ch = 0; ch < nch_out; ch++) {
345 buf->extended_data[s->map[ch].out_channel_idx] =
346 source_planes[s->map[ch].in_channel_idx];
349 if (buf->data != buf->extended_data)
350 memcpy(buf->data, buf->extended_data,
351 FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
353 return ff_filter_frame(outlink, buf);
356 static int channelmap_config_input(AVFilterLink *inlink)
358 AVFilterContext *ctx = inlink->dst;
359 ChannelMapContext *s = ctx->priv;
361 const char *channel_name;
362 char layout_name[256];
364 if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
365 for (i = 0; i < s->nch; i++) {
366 s->map[i].in_channel_idx = av_get_channel_layout_channel_index(
367 inlink->channel_layout, s->map[i].in_channel);
368 if (s->map[i].in_channel_idx < 0) {
369 channel_name = av_get_channel_name(s->map[i].in_channel);
370 av_get_channel_layout_string(layout_name, sizeof(layout_name),
371 0, inlink->channel_layout);
372 av_log(ctx, AV_LOG_ERROR,
373 "input channel '%s' not available from input layout '%s'\n",
374 channel_name, layout_name);
375 err = AVERROR(EINVAL);
383 static const AVFilterPad avfilter_af_channelmap_inputs[] = {
386 .type = AVMEDIA_TYPE_AUDIO,
387 .filter_frame = channelmap_filter_frame,
388 .config_props = channelmap_config_input,
394 static const AVFilterPad avfilter_af_channelmap_outputs[] = {
397 .type = AVMEDIA_TYPE_AUDIO
402 AVFilter avfilter_af_channelmap = {
403 .name = "channelmap",
404 .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
405 .init = channelmap_init,
406 .query_formats = channelmap_query_formats,
407 .priv_size = sizeof(ChannelMapContext),
408 .priv_class = &channelmap_class,
410 .inputs = avfilter_af_channelmap_inputs,
411 .outputs = avfilter_af_channelmap_outputs,