FFmpeg
 All Data Structures Namespaces 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 
124 {
125  ChannelMapContext *s = ctx->priv;
126  char *mapping, separator = '|';
127  int map_entries = 0;
128  char buf[256];
129  enum MappingMode mode;
130  uint64_t out_ch_mask = 0;
131  int i;
132 
133  mapping = s->mapping_str;
134 
135  if (!mapping) {
136  mode = MAP_NONE;
137  } else {
138  char *dash = strchr(mapping, '-');
139  if (!dash) { // short mapping
140  if (av_isdigit(*mapping))
141  mode = MAP_ONE_INT;
142  else
143  mode = MAP_ONE_STR;
144  } else if (av_isdigit(*mapping)) {
145  if (av_isdigit(*(dash+1)))
146  mode = MAP_PAIR_INT_INT;
147  else
148  mode = MAP_PAIR_INT_STR;
149  } else {
150  if (av_isdigit(*(dash+1)))
151  mode = MAP_PAIR_STR_INT;
152  else
153  mode = MAP_PAIR_STR_STR;
154  }
155 #if FF_API_OLD_FILTER_OPTS
156  if (strchr(mapping, ',')) {
157  av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
158  "'|' to separate the mappings.\n");
159  separator = ',';
160  }
161 #endif
162  }
163 
164  if (mode != MAP_NONE) {
165  char *sep = mapping;
166  map_entries = 1;
167  while ((sep = strchr(sep, separator))) {
168  if (*++sep) // Allow trailing comma
169  map_entries++;
170  }
171  }
172 
173  if (map_entries > MAX_CH) {
174  av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
175  return AVERROR(EINVAL);
176  }
177 
178  for (i = 0; i < map_entries; i++) {
179  int in_ch_idx = -1, out_ch_idx = -1;
180  uint64_t in_ch = 0, out_ch = 0;
181  static const char err[] = "Failed to parse channel map\n";
182  switch (mode) {
183  case MAP_ONE_INT:
184  if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
185  av_log(ctx, AV_LOG_ERROR, err);
186  return AVERROR(EINVAL);
187  }
188  s->map[i].in_channel_idx = in_ch_idx;
189  s->map[i].out_channel_idx = i;
190  break;
191  case MAP_ONE_STR:
192  if (!get_channel(&mapping, &in_ch, separator)) {
193  av_log(ctx, AV_LOG_ERROR, err);
194  return AVERROR(EINVAL);
195  }
196  s->map[i].in_channel = in_ch;
197  s->map[i].out_channel_idx = i;
198  break;
199  case MAP_PAIR_INT_INT:
200  if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
201  get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
202  av_log(ctx, AV_LOG_ERROR, err);
203  return AVERROR(EINVAL);
204  }
205  s->map[i].in_channel_idx = in_ch_idx;
206  s->map[i].out_channel_idx = out_ch_idx;
207  break;
208  case MAP_PAIR_INT_STR:
209  if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
210  get_channel(&mapping, &out_ch, separator) < 0 ||
211  out_ch & out_ch_mask) {
212  av_log(ctx, AV_LOG_ERROR, err);
213  return AVERROR(EINVAL);
214  }
215  s->map[i].in_channel_idx = in_ch_idx;
216  s->map[i].out_channel = out_ch;
217  out_ch_mask |= out_ch;
218  break;
219  case MAP_PAIR_STR_INT:
220  if (get_channel(&mapping, &in_ch, '-') < 0 ||
221  get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
222  av_log(ctx, AV_LOG_ERROR, err);
223  return AVERROR(EINVAL);
224  }
225  s->map[i].in_channel = in_ch;
226  s->map[i].out_channel_idx = out_ch_idx;
227  break;
228  case MAP_PAIR_STR_STR:
229  if (get_channel(&mapping, &in_ch, '-') < 0 ||
230  get_channel(&mapping, &out_ch, separator) < 0 ||
231  out_ch & out_ch_mask) {
232  av_log(ctx, AV_LOG_ERROR, err);
233  return AVERROR(EINVAL);
234  }
235  s->map[i].in_channel = in_ch;
236  s->map[i].out_channel = out_ch;
237  out_ch_mask |= out_ch;
238  break;
239  }
240  }
241  s->mode = mode;
242  s->nch = map_entries;
243  s->output_layout = out_ch_mask ? out_ch_mask :
244  av_get_default_channel_layout(map_entries);
245 
246  if (s->channel_layout_str) {
247  uint64_t fmt;
248  if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
249  av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
250  s->channel_layout_str);
251  return AVERROR(EINVAL);
252  }
253  if (mode == MAP_NONE) {
254  int i;
256  for (i = 0; i < s->nch; i++) {
257  s->map[i].in_channel_idx = i;
258  s->map[i].out_channel_idx = i;
259  }
260  } else if (out_ch_mask && out_ch_mask != fmt) {
261  av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
262  av_log(ctx, AV_LOG_ERROR,
263  "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
264  s->channel_layout_str, buf);
265  return AVERROR(EINVAL);
266  } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
267  av_log(ctx, AV_LOG_ERROR,
268  "Output channel layout %s does not match the number of channels mapped %d.\n",
269  s->channel_layout_str, s->nch);
270  return AVERROR(EINVAL);
271  }
272  s->output_layout = fmt;
273  }
274  if (!s->output_layout) {
275  av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and "
276  "cannot be guessed from the maps.\n");
277  return AVERROR(EINVAL);
278  }
279 
281 
282  if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
283  for (i = 0; i < s->nch; i++) {
285  s->output_layout, s->map[i].out_channel);
286  }
287  }
288 
289  return 0;
290 }
291 
293 {
294  ChannelMapContext *s = ctx->priv;
295 
300 
301  return 0;
302 }
303 
305 {
306  AVFilterContext *ctx = inlink->dst;
307  AVFilterLink *outlink = ctx->outputs[0];
308  const ChannelMapContext *s = ctx->priv;
309  const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
310  const int nch_out = s->nch;
311  int ch;
312  uint8_t *source_planes[MAX_CH];
313 
314  memcpy(source_planes, buf->extended_data,
315  nch_in * sizeof(source_planes[0]));
316 
317  if (nch_out > nch_in) {
318  if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
319  uint8_t **new_extended_data =
320  av_mallocz(nch_out * sizeof(*buf->extended_data));
321  if (!new_extended_data) {
322  av_frame_free(&buf);
323  return AVERROR(ENOMEM);
324  }
325  if (buf->extended_data == buf->data) {
326  buf->extended_data = new_extended_data;
327  } else {
328  av_free(buf->extended_data);
329  buf->extended_data = new_extended_data;
330  }
331  } else if (buf->extended_data != buf->data) {
332  av_free(buf->extended_data);
333  buf->extended_data = buf->data;
334  }
335  }
336 
337  for (ch = 0; ch < nch_out; ch++) {
338  buf->extended_data[s->map[ch].out_channel_idx] =
339  source_planes[s->map[ch].in_channel_idx];
340  }
341 
342  if (buf->data != buf->extended_data)
343  memcpy(buf->data, buf->extended_data,
344  FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
345 
346  return ff_filter_frame(outlink, buf);
347 }
348 
350 {
351  AVFilterContext *ctx = inlink->dst;
352  ChannelMapContext *s = ctx->priv;
354  int i, err = 0;
355  const char *channel_name;
356  char layout_name[256];
357 
358  for (i = 0; i < s->nch; i++) {
359  struct ChannelMap *m = &s->map[i];
360 
361  if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
363  inlink->channel_layout, m->in_channel);
364  }
365 
366  if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
367  av_get_channel_layout_string(layout_name, sizeof(layout_name),
368  0, inlink->channel_layout);
369  if (m->in_channel) {
370  channel_name = av_get_channel_name(m->in_channel);
371  av_log(ctx, AV_LOG_ERROR,
372  "input channel '%s' not available from input layout '%s'\n",
373  channel_name, layout_name);
374  } else {
375  av_log(ctx, AV_LOG_ERROR,
376  "input channel #%d not available from input layout '%s'\n",
377  m->in_channel_idx, layout_name);
378  }
379  err = AVERROR(EINVAL);
380  }
381  }
382 
383  return err;
384 }
385 
387  {
388  .name = "default",
389  .type = AVMEDIA_TYPE_AUDIO,
390  .filter_frame = channelmap_filter_frame,
391  .config_props = channelmap_config_input,
392  .needs_writable = 1,
393  },
394  { NULL }
395 };
396 
398  {
399  .name = "default",
400  .type = AVMEDIA_TYPE_AUDIO
401  },
402  { NULL }
403 };
404 
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,
412 
413  .inputs = avfilter_af_channelmap_inputs,
414  .outputs = avfilter_af_channelmap_outputs,
415 };