2 * Copyright (c) 2012 Google, Inc.
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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 static const AVOption options[] = {
71 { "map", "A comma-separated list of input channel numbers in output order.",
72 OFFSET(mapping_str), AV_OPT_TYPE_STRING, .flags = A },
73 { "channel_layout", "Output channel layout.",
74 OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
78 static const AVClass channelmap_class = {
79 .class_name = "channel map filter",
80 .item_name = av_default_item_name,
82 .version = LIBAVUTIL_VERSION_INT,
85 static char* split(char *message, char delim) {
86 char *next = strchr(message, delim);
92 static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
94 char *next = split(*map, delim);
97 if (!next && delim == '-')
98 return AVERROR(EINVAL);
100 sscanf(*map, "%d%n", ch, &n);
102 return AVERROR(EINVAL);
103 if (*ch < 0 || *ch > max_ch)
104 return AVERROR(EINVAL);
109 static int get_channel(char **map, uint64_t *ch, char delim)
111 char *next = split(*map, delim);
112 if (!next && delim == '-')
113 return AVERROR(EINVAL);
114 *ch = av_get_channel_layout(*map);
115 if (av_get_channel_layout_nb_channels(*ch) != 1)
116 return AVERROR(EINVAL);
121 static av_cold int channelmap_init(AVFilterContext *ctx)
123 ChannelMapContext *s = ctx->priv;
124 char *mapping, separator = '|';
127 enum MappingMode mode;
128 uint64_t out_ch_mask = 0;
131 mapping = s->mapping_str;
136 char *dash = strchr(mapping, '-');
137 if (!dash) { // short mapping
138 if (av_isdigit(*mapping))
142 } else if (av_isdigit(*mapping)) {
143 if (av_isdigit(*(dash+1)))
144 mode = MAP_PAIR_INT_INT;
146 mode = MAP_PAIR_INT_STR;
148 if (av_isdigit(*(dash+1)))
149 mode = MAP_PAIR_STR_INT;
151 mode = MAP_PAIR_STR_STR;
153 #if FF_API_OLD_FILTER_OPTS
154 if (strchr(mapping, ',')) {
155 av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
156 "'|' to separate the mappings.\n");
162 if (mode != MAP_NONE) {
165 while ((sep = strchr(sep, separator))) {
166 if (*++sep) // Allow trailing comma
171 if (map_entries > MAX_CH) {
172 av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
173 return AVERROR(EINVAL);
176 for (i = 0; i < map_entries; i++) {
177 int in_ch_idx = -1, out_ch_idx = -1;
178 uint64_t in_ch = 0, out_ch = 0;
179 static const char err[] = "Failed to parse channel map\n";
182 if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
183 av_log(ctx, AV_LOG_ERROR, err);
184 return AVERROR(EINVAL);
186 s->map[i].in_channel_idx = in_ch_idx;
187 s->map[i].out_channel_idx = i;
190 if (get_channel(&mapping, &in_ch, separator) < 0) {
191 av_log(ctx, AV_LOG_ERROR, err);
192 return AVERROR(EINVAL);
194 s->map[i].in_channel = in_ch;
195 s->map[i].out_channel_idx = i;
197 case MAP_PAIR_INT_INT:
198 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
199 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
200 av_log(ctx, AV_LOG_ERROR, err);
201 return AVERROR(EINVAL);
203 s->map[i].in_channel_idx = in_ch_idx;
204 s->map[i].out_channel_idx = out_ch_idx;
206 case MAP_PAIR_INT_STR:
207 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
208 get_channel(&mapping, &out_ch, separator) < 0 ||
209 out_ch & out_ch_mask) {
210 av_log(ctx, AV_LOG_ERROR, err);
211 return AVERROR(EINVAL);
213 s->map[i].in_channel_idx = in_ch_idx;
214 s->map[i].out_channel = out_ch;
215 out_ch_mask |= out_ch;
217 case MAP_PAIR_STR_INT:
218 if (get_channel(&mapping, &in_ch, '-') < 0 ||
219 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
220 av_log(ctx, AV_LOG_ERROR, err);
221 return AVERROR(EINVAL);
223 s->map[i].in_channel = in_ch;
224 s->map[i].out_channel_idx = out_ch_idx;
226 case MAP_PAIR_STR_STR:
227 if (get_channel(&mapping, &in_ch, '-') < 0 ||
228 get_channel(&mapping, &out_ch, separator) < 0 ||
229 out_ch & out_ch_mask) {
230 av_log(ctx, AV_LOG_ERROR, err);
231 return AVERROR(EINVAL);
233 s->map[i].in_channel = in_ch;
234 s->map[i].out_channel = out_ch;
235 out_ch_mask |= out_ch;
240 s->nch = map_entries;
241 s->output_layout = out_ch_mask ? out_ch_mask :
242 av_get_default_channel_layout(map_entries);
244 if (s->channel_layout_str) {
246 if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
247 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
248 s->channel_layout_str);
249 return AVERROR(EINVAL);
251 if (mode == MAP_NONE) {
253 s->nch = av_get_channel_layout_nb_channels(fmt);
254 for (i = 0; i < s->nch; i++) {
255 s->map[i].in_channel_idx = i;
256 s->map[i].out_channel_idx = i;
258 } else if (out_ch_mask && out_ch_mask != fmt) {
259 av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
260 av_log(ctx, AV_LOG_ERROR,
261 "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
262 s->channel_layout_str, buf);
263 return AVERROR(EINVAL);
264 } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
265 av_log(ctx, AV_LOG_ERROR,
266 "Output channel layout %s does not match the number of channels mapped %d.\n",
267 s->channel_layout_str, s->nch);
268 return AVERROR(EINVAL);
270 s->output_layout = fmt;
272 if (!s->output_layout) {
273 av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and "
274 "cannot be guessed from the maps.\n");
275 return AVERROR(EINVAL);
278 if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
279 for (i = 0; i < s->nch; i++) {
280 s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
281 s->output_layout, s->map[i].out_channel);
288 static int channelmap_query_formats(AVFilterContext *ctx)
290 ChannelMapContext *s = ctx->priv;
291 AVFilterChannelLayouts *channel_layouts = NULL;
293 ff_add_channel_layout(&channel_layouts, s->output_layout);
295 ff_set_common_formats(ctx, ff_planar_sample_fmts());
296 ff_set_common_samplerates(ctx, ff_all_samplerates());
297 ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts);
298 ff_channel_layouts_ref(channel_layouts, &ctx->outputs[0]->in_channel_layouts);
303 static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
305 AVFilterContext *ctx = inlink->dst;
306 AVFilterLink *outlink = ctx->outputs[0];
307 const ChannelMapContext *s = ctx->priv;
308 const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
309 const int nch_out = s->nch;
311 uint8_t *source_planes[MAX_CH];
313 memcpy(source_planes, buf->extended_data,
314 nch_in * sizeof(source_planes[0]));
316 if (nch_out > nch_in) {
317 if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
318 uint8_t **new_extended_data =
319 av_mallocz(nch_out * sizeof(*buf->extended_data));
320 if (!new_extended_data) {
322 return AVERROR(ENOMEM);
324 if (buf->extended_data == buf->data) {
325 buf->extended_data = new_extended_data;
327 av_free(buf->extended_data);
328 buf->extended_data = new_extended_data;
330 } else if (buf->extended_data != buf->data) {
331 av_free(buf->extended_data);
332 buf->extended_data = buf->data;
336 for (ch = 0; ch < nch_out; ch++) {
337 buf->extended_data[s->map[ch].out_channel_idx] =
338 source_planes[s->map[ch].in_channel_idx];
341 if (buf->data != buf->extended_data)
342 memcpy(buf->data, buf->extended_data,
343 FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
345 buf->channel_layout = outlink->channel_layout;
347 return ff_filter_frame(outlink, buf);
350 static int channelmap_config_input(AVFilterLink *inlink)
352 AVFilterContext *ctx = inlink->dst;
353 ChannelMapContext *s = ctx->priv;
354 int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
356 const char *channel_name;
357 char layout_name[256];
359 for (i = 0; i < s->nch; i++) {
360 struct ChannelMap *m = &s->map[i];
362 if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
363 m->in_channel_idx = av_get_channel_layout_channel_index(
364 inlink->channel_layout, m->in_channel);
367 if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
368 av_get_channel_layout_string(layout_name, sizeof(layout_name),
369 0, inlink->channel_layout);
371 channel_name = av_get_channel_name(m->in_channel);
372 av_log(ctx, AV_LOG_ERROR,
373 "input channel '%s' not available from input layout '%s'\n",
374 channel_name, layout_name);
376 av_log(ctx, AV_LOG_ERROR,
377 "input channel #%d not available from input layout '%s'\n",
378 m->in_channel_idx, layout_name);
380 err = AVERROR(EINVAL);
387 static const AVFilterPad avfilter_af_channelmap_inputs[] = {
390 .type = AVMEDIA_TYPE_AUDIO,
391 .filter_frame = channelmap_filter_frame,
392 .config_props = channelmap_config_input
397 static const AVFilterPad avfilter_af_channelmap_outputs[] = {
400 .type = AVMEDIA_TYPE_AUDIO
405 AVFilter ff_af_channelmap = {
406 .name = "channelmap",
407 .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
408 .init = channelmap_init,
409 .query_formats = channelmap_query_formats,
410 .priv_size = sizeof(ChannelMapContext),
411 .priv_class = &channelmap_class,
413 .inputs = avfilter_af_channelmap_inputs,
414 .outputs = avfilter_af_channelmap_outputs,