FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_channelmap.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Google, Inc.
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 /**
22  * @file
23  * audio channel mapping filter
24  */
25 
26 #include <ctype.h>
27 
28 #include "libavutil/avstring.h"
30 #include "libavutil/common.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/samplefmt.h"
34 
35 #include "audio.h"
36 #include "avfilter.h"
37 #include "formats.h"
38 #include "internal.h"
39 
40 struct ChannelMap {
41  uint64_t in_channel;
42  uint64_t out_channel;
45 };
46 
55 };
56 
57 #define MAX_CH 64
58 typedef struct ChannelMapContext {
59  const AVClass *class;
61  char *mapping_str;
63  uint64_t output_layout;
65  int nch;
68 
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 },
77  { NULL },
78 };
79 
80 static const AVClass channelmap_class = {
81  .class_name = "channel map filter",
82  .item_name = av_default_item_name,
83  .option = options,
84  .version = LIBAVUTIL_VERSION_INT,
85 };
86 
87 static char* split(char *message, char delim) {
88  char *next = strchr(message, delim);
89  if (next)
90  *next++ = '\0';
91  return next;
92 }
93 
94 static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
95 {
96  char *next = split(*map, delim);
97  int len;
98  int n = 0;
99  if (!next && delim == '-')
100  return AVERROR(EINVAL);
101  len = strlen(*map);
102  sscanf(*map, "%d%n", ch, &n);
103  if (n != len)
104  return AVERROR(EINVAL);
105  if (*ch < 0 || *ch > max_ch)
106  return AVERROR(EINVAL);
107  *map = next;
108  return 0;
109 }
110 
111 static int get_channel(char **map, uint64_t *ch, char delim)
112 {
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);
119  *map = next;
120  return 0;
121 }
122 
123 static av_cold int channelmap_init(AVFilterContext *ctx, const char *args)
124 {
125  ChannelMapContext *s = ctx->priv;
126  int ret;
127  char *mapping;
128  int map_entries = 0;
129  char buf[256];
130  enum MappingMode mode;
131  uint64_t out_ch_mask = 0;
132  int i;
133 
134  if (!args) {
135  av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
136  return AVERROR(EINVAL);
137  }
138 
139  s->class = &channelmap_class;
141 
142  if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
143  return ret;
144 
145  mapping = s->mapping_str;
146 
147  if (!mapping) {
148  mode = MAP_NONE;
149  } else {
150  char *dash = strchr(mapping, '-');
151  if (!dash) { // short mapping
152  if (isdigit(*mapping))
153  mode = MAP_ONE_INT;
154  else
155  mode = MAP_ONE_STR;
156  } else if (isdigit(*mapping)) {
157  if (isdigit(*(dash+1)))
158  mode = MAP_PAIR_INT_INT;
159  else
160  mode = MAP_PAIR_INT_STR;
161  } else {
162  if (isdigit(*(dash+1)))
163  mode = MAP_PAIR_STR_INT;
164  else
165  mode = MAP_PAIR_STR_STR;
166  }
167  }
168 
169  if (mode != MAP_NONE) {
170  char *comma = mapping;
171  map_entries = 1;
172  while ((comma = strchr(comma, ','))) {
173  if (*++comma) // Allow trailing comma
174  map_entries++;
175  }
176  }
177 
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);
181  goto fail;
182  }
183 
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";
188  switch (mode) {
189  case MAP_ONE_INT:
190  if (get_channel_idx(&mapping, &in_ch_idx, ',', MAX_CH) < 0) {
191  ret = AVERROR(EINVAL);
192  av_log(ctx, AV_LOG_ERROR, err);
193  goto fail;
194  }
195  s->map[i].in_channel_idx = in_ch_idx;
196  s->map[i].out_channel_idx = i;
197  break;
198  case MAP_ONE_STR:
199  if (!get_channel(&mapping, &in_ch, ',')) {
200  av_log(ctx, AV_LOG_ERROR, err);
201  ret = AVERROR(EINVAL);
202  goto fail;
203  }
204  s->map[i].in_channel = in_ch;
205  s->map[i].out_channel_idx = i;
206  break;
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);
212  goto fail;
213  }
214  s->map[i].in_channel_idx = in_ch_idx;
215  s->map[i].out_channel_idx = out_ch_idx;
216  break;
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);
223  goto fail;
224  }
225  s->map[i].in_channel_idx = in_ch_idx;
226  s->map[i].out_channel = out_ch;
227  out_ch_mask |= out_ch;
228  break;
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);
234  goto fail;
235  }
236  s->map[i].in_channel = in_ch;
237  s->map[i].out_channel_idx = out_ch_idx;
238  break;
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);
245  goto fail;
246  }
247  s->map[i].in_channel = in_ch;
248  s->map[i].out_channel = out_ch;
249  out_ch_mask |= out_ch;
250  break;
251  }
252  }
253  s->mode = mode;
254  s->nch = map_entries;
255  s->output_layout = out_ch_mask ? out_ch_mask :
256  av_get_default_channel_layout(map_entries);
257 
258  if (s->channel_layout_str) {
259  uint64_t fmt;
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);
264  goto fail;
265  }
266  if (mode == MAP_NONE) {
267  int i;
269  for (i = 0; i < s->nch; i++) {
270  s->map[i].in_channel_idx = i;
271  s->map[i].out_channel_idx = i;
272  }
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);
279  goto fail;
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);
285  goto fail;
286  }
287  s->output_layout = fmt;
288  }
290 
291  if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
292  for (i = 0; i < s->nch; i++) {
294  s->output_layout, s->map[i].out_channel);
295  }
296  }
297 
298 fail:
299  av_opt_free(s);
300  return ret;
301 }
302 
304 {
305  ChannelMapContext *s = ctx->priv;
306 
311 
312  return 0;
313 }
314 
316 {
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;
322  int ch;
323  uint8_t *source_planes[MAX_CH];
324 
325  memcpy(source_planes, buf->extended_data,
326  nch_in * sizeof(source_planes[0]));
327 
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) {
334  return AVERROR(ENOMEM);
335  }
336  if (buf->extended_data == buf->data) {
337  buf->extended_data = new_extended_data;
338  } else {
339  av_free(buf->extended_data);
340  buf->extended_data = new_extended_data;
341  }
342  } else if (buf->extended_data != buf->data) {
343  av_free(buf->extended_data);
344  buf->extended_data = buf->data;
345  }
346  }
347 
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];
351  }
352 
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]));
356 
357  return ff_filter_frame(outlink, buf);
358 }
359 
361 {
362  AVFilterContext *ctx = inlink->dst;
363  ChannelMapContext *s = ctx->priv;
364  int i, err = 0;
365  const char *channel_name;
366  char layout_name[256];
367 
368  if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
369  for (i = 0; i < s->nch; i++) {
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);
380  }
381  }
382  }
383 
384  return err;
385 }
386 
388  {
389  .name = "default",
390  .type = AVMEDIA_TYPE_AUDIO,
391  .min_perms = AV_PERM_READ | AV_PERM_WRITE,
392  .filter_frame = channelmap_filter_frame,
393  .config_props = channelmap_config_input
394  },
395  { NULL }
396 };
397 
399  {
400  .name = "default",
401  .type = AVMEDIA_TYPE_AUDIO
402  },
403  { NULL }
404 };
405 
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),
412 
413  .inputs = avfilter_af_channelmap_inputs,
414  .outputs = avfilter_af_channelmap_outputs,
415  .priv_class = &channelmap_class,
416 };