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 channelmap_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 AVFILTER_DEFINE_CLASS(channelmap);
81 
82 static char* split(char *message, char delim) {
83  char *next = strchr(message, delim);
84  if (next)
85  *next++ = '\0';
86  return next;
87 }
88 
89 static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
90 {
91  char *next;
92  int len;
93  int n = 0;
94  if (!*map)
95  return AVERROR(EINVAL);
96  next = split(*map, delim);
97  if (!next && delim == '-')
98  return AVERROR(EINVAL);
99  len = strlen(*map);
100  sscanf(*map, "%d%n", ch, &n);
101  if (n != len)
102  return AVERROR(EINVAL);
103  if (*ch < 0 || *ch > max_ch)
104  return AVERROR(EINVAL);
105  *map = next;
106  return 0;
107 }
108 
109 static int get_channel(char **map, uint64_t *ch, char delim)
110 {
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);
117  *map = next;
118  return 0;
119 }
120 
122 {
123  ChannelMapContext *s = ctx->priv;
124  char *mapping, separator = '|';
125  int map_entries = 0;
126  char buf[256];
127  enum MappingMode mode;
128  uint64_t out_ch_mask = 0;
129  int i;
130 
131  mapping = s->mapping_str;
132 
133  if (!mapping) {
134  mode = MAP_NONE;
135  } else {
136  char *dash = strchr(mapping, '-');
137  if (!dash) { // short mapping
138  if (av_isdigit(*mapping))
139  mode = MAP_ONE_INT;
140  else
141  mode = MAP_ONE_STR;
142  } else if (av_isdigit(*mapping)) {
143  if (av_isdigit(*(dash+1)))
144  mode = MAP_PAIR_INT_INT;
145  else
146  mode = MAP_PAIR_INT_STR;
147  } else {
148  if (av_isdigit(*(dash+1)))
149  mode = MAP_PAIR_STR_INT;
150  else
151  mode = MAP_PAIR_STR_STR;
152  }
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");
157  separator = ',';
158  }
159 #endif
160  }
161 
162  if (mode != MAP_NONE) {
163  char *sep = mapping;
164  map_entries = 1;
165  while ((sep = strchr(sep, separator))) {
166  if (*++sep) // Allow trailing comma
167  map_entries++;
168  }
169  }
170 
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);
174  }
175 
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";
180  switch (mode) {
181  case MAP_ONE_INT:
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);
185  }
186  s->map[i].in_channel_idx = in_ch_idx;
187  s->map[i].out_channel_idx = i;
188  break;
189  case MAP_ONE_STR:
190  if (get_channel(&mapping, &in_ch, separator) < 0) {
191  av_log(ctx, AV_LOG_ERROR, err);
192  return AVERROR(EINVAL);
193  }
194  s->map[i].in_channel = in_ch;
195  s->map[i].out_channel_idx = i;
196  break;
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);
202  }
203  s->map[i].in_channel_idx = in_ch_idx;
204  s->map[i].out_channel_idx = out_ch_idx;
205  break;
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);
212  }
213  s->map[i].in_channel_idx = in_ch_idx;
214  s->map[i].out_channel = out_ch;
215  out_ch_mask |= out_ch;
216  break;
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);
222  }
223  s->map[i].in_channel = in_ch;
224  s->map[i].out_channel_idx = out_ch_idx;
225  break;
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);
232  }
233  s->map[i].in_channel = in_ch;
234  s->map[i].out_channel = out_ch;
235  out_ch_mask |= out_ch;
236  break;
237  }
238  }
239  s->mode = mode;
240  s->nch = map_entries;
241  s->output_layout = out_ch_mask ? out_ch_mask :
242  av_get_default_channel_layout(map_entries);
243 
244  if (s->channel_layout_str) {
245  uint64_t fmt;
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);
250  }
251  if (mode == MAP_NONE) {
252  int i;
254  for (i = 0; i < s->nch; i++) {
255  s->map[i].in_channel_idx = i;
256  s->map[i].out_channel_idx = i;
257  }
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);
269  }
270  s->output_layout = fmt;
271  }
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);
276  }
277 
279 
280  if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
281  for (i = 0; i < s->nch; i++) {
283  s->output_layout, s->map[i].out_channel);
284  }
285  }
286 
287  return 0;
288 }
289 
291 {
292  ChannelMapContext *s = ctx->priv;
294 
297 
298  layouts = ff_all_channel_layouts();
299  if (!layouts)
300  return AVERROR(ENOMEM);
301 
304 
305  return 0;
306 }
307 
309 {
310  AVFilterContext *ctx = inlink->dst;
311  AVFilterLink *outlink = ctx->outputs[0];
312  const ChannelMapContext *s = ctx->priv;
313  const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
314  const int nch_out = s->nch;
315  int ch;
316  uint8_t *source_planes[MAX_CH];
317 
318  memcpy(source_planes, buf->extended_data,
319  nch_in * sizeof(source_planes[0]));
320 
321  if (nch_out > nch_in) {
322  if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
323  uint8_t **new_extended_data =
324  av_mallocz_array(nch_out, sizeof(*buf->extended_data));
325  if (!new_extended_data) {
326  av_frame_free(&buf);
327  return AVERROR(ENOMEM);
328  }
329  if (buf->extended_data == buf->data) {
330  buf->extended_data = new_extended_data;
331  } else {
332  av_free(buf->extended_data);
333  buf->extended_data = new_extended_data;
334  }
335  } else if (buf->extended_data != buf->data) {
336  av_free(buf->extended_data);
337  buf->extended_data = buf->data;
338  }
339  }
340 
341  for (ch = 0; ch < nch_out; ch++) {
342  buf->extended_data[s->map[ch].out_channel_idx] =
343  source_planes[s->map[ch].in_channel_idx];
344  }
345 
346  if (buf->data != buf->extended_data)
347  memcpy(buf->data, buf->extended_data,
348  FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
349 
350  buf->channel_layout = outlink->channel_layout;
351  av_frame_set_channels(buf, outlink->channels);
352 
353  return ff_filter_frame(outlink, buf);
354 }
355 
357 {
358  AVFilterContext *ctx = inlink->dst;
359  ChannelMapContext *s = ctx->priv;
361  int i, err = 0;
362  const char *channel_name;
363  char layout_name[256];
364 
365  for (i = 0; i < s->nch; i++) {
366  struct ChannelMap *m = &s->map[i];
367 
368  if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
370  inlink->channel_layout, m->in_channel);
371  }
372 
373  if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
374  av_get_channel_layout_string(layout_name, sizeof(layout_name),
375  0, inlink->channel_layout);
376  if (m->in_channel) {
377  channel_name = av_get_channel_name(m->in_channel);
378  av_log(ctx, AV_LOG_ERROR,
379  "input channel '%s' not available from input layout '%s'\n",
380  channel_name, layout_name);
381  } else {
382  av_log(ctx, AV_LOG_ERROR,
383  "input channel #%d not available from input layout '%s'\n",
384  m->in_channel_idx, layout_name);
385  }
386  err = AVERROR(EINVAL);
387  }
388  }
389 
390  return err;
391 }
392 
394  {
395  .name = "default",
396  .type = AVMEDIA_TYPE_AUDIO,
397  .filter_frame = channelmap_filter_frame,
398  .config_props = channelmap_config_input,
399  .needs_writable = 1,
400  },
401  { NULL }
402 };
403 
405  {
406  .name = "default",
407  .type = AVMEDIA_TYPE_AUDIO
408  },
409  { NULL }
410 };
411 
413  .name = "channelmap",
414  .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
415  .init = channelmap_init,
416  .query_formats = channelmap_query_formats,
417  .priv_size = sizeof(ChannelMapContext),
418  .priv_class = &channelmap_class,
419  .inputs = avfilter_af_channelmap_inputs,
420  .outputs = avfilter_af_channelmap_outputs,
421 };
void av_frame_set_channels(AVFrame *frame, int val)
#define NULL
Definition: coverity.c:32
int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.c:320
AVFILTER_DEFINE_CLASS(channelmap)
const char * s
Definition: avisynth_c.h:631
uint64_t in_channel
layout describing the input channel
Definition: af_channelmap.c:41
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
const char * fmt
Definition: avisynth_c.h:632
#define OFFSET(x)
Definition: af_channelmap.c:69
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
AVFilter ff_af_channelmap
uint64_t output_layout
Definition: af_channelmap.c:63
static const AVOption channelmap_options[]
Definition: af_channelmap.c:72
#define FF_ARRAY_ELEMS(a)
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
const char * name
Pad name.
Definition: internal.h:67
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:417
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1145
uint8_t
#define av_cold
Definition: attributes.h:74
mode
Definition: f_perms.c:27
AVOptions.
enum MappingMode mode
Definition: af_channelmap.c:66
static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
char * channel_layout_str
Definition: af_channelmap.c:62
static int channelmap_query_formats(AVFilterContext *ctx)
#define av_log(a,...)
unsigned m
Definition: audioconvert.c:187
A filter pad used for either input or output.
Definition: internal.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:542
AVFilterChannelLayouts * channel_layouts
Definition: af_channelmap.c:60
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:329
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
#define A
Definition: af_channelmap.c:70
static char * split(char *message, char delim)
Definition: af_channelmap.c:82
MappingMode
Definition: af_channelmap.c:47
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:427
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:66
int n
Definition: avisynth_c.h:547
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:367
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:385
int in_channel_idx
index of in_channel in the input stream data
Definition: af_channelmap.c:43
A list of supported channel layouts.
Definition: formats.h:85
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
void * buf
Definition: avisynth_c.h:553
struct ChannelMap map[MAX_CH]
Definition: af_channelmap.c:64
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
uint64_t out_channel
layout describing the output channel
Definition: af_channelmap.c:42
const char * name
Filter name.
Definition: avfilter.h:474
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
int av_get_channel_layout_channel_index(uint64_t channel_layout, uint64_t channel)
Get the index of a channel in channel_layout.
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:379
static int channelmap_config_input(AVFilterLink *inlink)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
static const AVFilterPad avfilter_af_channelmap_inputs[]
static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
Definition: af_channelmap.c:89
#define MAX_CH
Definition: af_channelmap.c:57
common internal and external API header
#define F
Definition: af_channelmap.c:71
#define av_free(p)
const char * av_get_channel_name(uint64_t channel)
Get the name of a given channel.
int len
static int get_channel(char **map, uint64_t *ch, char delim)
An instance of a filter.
Definition: avfilter.h:633
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
static av_cold int channelmap_init(AVFilterContext *ctx)
static const AVFilterPad avfilter_af_channelmap_outputs[]
int nb_channels
int out_channel_idx
Definition: af_channelmap.c:44
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:530