FFmpeg  4.4.9
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 typedef struct ChannelMapContext {
58  const AVClass *class;
59  char *mapping_str;
61  uint64_t output_layout;
62  struct ChannelMap *map;
63  int nch;
64  enum MappingMode mode;
65 
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 
81 
83 {
84  ChannelMapContext *s = ctx->priv;
85  av_freep(&s->map);
86  av_freep(&s->source_planes);
87 }
88 
89 static char* split(char *message, char delim) {
90  char *next = strchr(message, delim);
91  if (next)
92  *next++ = '\0';
93  return next;
94 }
95 
96 static int get_channel_idx(char **map, int *ch, char delim)
97 {
98  char *next;
99  int len;
100  int n = 0;
101  if (!*map)
102  return AVERROR(EINVAL);
103  next = split(*map, delim);
104  if (!next && delim == '-')
105  return AVERROR(EINVAL);
106  len = strlen(*map);
107  sscanf(*map, "%d%n", ch, &n);
108  if (n != len)
109  return AVERROR(EINVAL);
110  if (*ch < 0)
111  return AVERROR(EINVAL);
112  *map = next;
113  return 0;
114 }
115 
116 static int get_channel(char **map, uint64_t *ch, char delim)
117 {
118  char *next = split(*map, delim);
119  if (!next && delim == '-')
120  return AVERROR(EINVAL);
121  *ch = av_get_channel_layout(*map);
122  if (av_get_channel_layout_nb_channels(*ch) != 1)
123  return AVERROR(EINVAL);
124  *map = next;
125  return 0;
126 }
127 
128 static int check_idx_and_id(AVFilterContext *ctx, int channel_idx, uint64_t channel, uint64_t ch_layout, int nb_channels, const char *io)
129 {
130  char layout_name[256];
131 
132  if (channel_idx < 0 || channel_idx >= nb_channels) {
133  av_get_channel_layout_string(layout_name, sizeof(layout_name),
134  nb_channels, ch_layout);
135  if (channel) {
137  "%sput channel '%s' not available from %sput layout '%s'\n",
138  io, av_get_channel_name(channel), io, layout_name);
139  } else {
141  "%sput channel #%d not available from %sput layout '%s'\n",
142  io, channel_idx, io, layout_name);
143  }
144  return AVERROR(EINVAL);
145  }
146 
147  return 0;
148 }
149 
151 {
152  ChannelMapContext *s = ctx->priv;
153  char *mapping, separator = '|';
154  int map_entries = 0;
155  char buf[256];
156  enum MappingMode mode;
157  uint64_t out_ch_mask = 0;
158  uint64_t presence_mask;
159  int i;
160 
161  mapping = s->mapping_str;
162 
163  if (!mapping) {
164  mode = MAP_NONE;
165  } else {
166  char *dash = strchr(mapping, '-');
167  if (!dash) { // short mapping
168  if (av_isdigit(*mapping))
169  mode = MAP_ONE_INT;
170  else
171  mode = MAP_ONE_STR;
172  } else if (av_isdigit(*mapping)) {
173  if (av_isdigit(*(dash+1)))
175  else
177  } else {
178  if (av_isdigit(*(dash+1)))
180  else
182  }
183  }
184 
185  if (mode != MAP_NONE) {
186  char *sep = mapping;
187  map_entries = 1;
188  while ((sep = strchr(sep, separator))) {
189  if (*++sep) // Allow trailing comma
190  map_entries++;
191  }
192 
193  s->map = av_calloc(map_entries, sizeof(*s->map));
194  if (!s->map)
195  return AVERROR(ENOMEM);
196  }
197 
198  for (i = 0; i < map_entries; i++) {
199  int in_ch_idx = -1, out_ch_idx = -1;
200  uint64_t in_ch = 0, out_ch = 0;
201  static const char err[] = "Failed to parse channel map\n";
202 
203  s->map[i].in_channel_idx = -1;
204  s->map[i].out_channel_idx = -1;
205 
206  switch (mode) {
207  case MAP_ONE_INT:
208  if (get_channel_idx(&mapping, &in_ch_idx, separator) < 0) {
209  av_log(ctx, AV_LOG_ERROR, err);
210  return AVERROR(EINVAL);
211  }
212  s->map[i].in_channel_idx = in_ch_idx;
213  s->map[i].out_channel_idx = i;
214  break;
215  case MAP_ONE_STR:
216  if (get_channel(&mapping, &in_ch, separator) < 0) {
217  av_log(ctx, AV_LOG_ERROR, err);
218  return AVERROR(EINVAL);
219  }
220  s->map[i].in_channel = in_ch;
221  s->map[i].out_channel_idx = i;
222  break;
223  case MAP_PAIR_INT_INT:
224  if (get_channel_idx(&mapping, &in_ch_idx, '-') < 0 ||
225  get_channel_idx(&mapping, &out_ch_idx, separator) < 0) {
226  av_log(ctx, AV_LOG_ERROR, err);
227  return AVERROR(EINVAL);
228  }
229  s->map[i].in_channel_idx = in_ch_idx;
230  s->map[i].out_channel_idx = out_ch_idx;
231  break;
232  case MAP_PAIR_INT_STR:
233  if (get_channel_idx(&mapping, &in_ch_idx, '-') < 0 ||
234  get_channel(&mapping, &out_ch, separator) < 0) {
235  av_log(ctx, AV_LOG_ERROR, err);
236  return AVERROR(EINVAL);
237  }
238  s->map[i].in_channel_idx = in_ch_idx;
239  s->map[i].out_channel = out_ch;
240  out_ch_mask |= out_ch;
241  break;
242  case MAP_PAIR_STR_INT:
243  if (get_channel(&mapping, &in_ch, '-') < 0 ||
244  get_channel_idx(&mapping, &out_ch_idx, separator) < 0) {
245  av_log(ctx, AV_LOG_ERROR, err);
246  return AVERROR(EINVAL);
247  }
248  s->map[i].in_channel = in_ch;
249  s->map[i].out_channel_idx = out_ch_idx;
250  break;
251  case MAP_PAIR_STR_STR:
252  if (get_channel(&mapping, &in_ch, '-') < 0 ||
253  get_channel(&mapping, &out_ch, separator) < 0) {
254  av_log(ctx, AV_LOG_ERROR, err);
255  return AVERROR(EINVAL);
256  }
257  s->map[i].in_channel = in_ch;
258  s->map[i].out_channel = out_ch;
259  out_ch_mask |= out_ch;
260  break;
261  }
262  }
263  s->mode = mode;
264  s->nch = map_entries;
265  s->output_layout = out_ch_mask ? out_ch_mask :
266  av_get_default_channel_layout(map_entries);
267 
268  if (s->channel_layout_str) {
269  uint64_t fmt;
270  if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
271  av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
272  s->channel_layout_str);
273  return AVERROR(EINVAL);
274  }
275  if (mode == MAP_NONE) {
276  int i;
278 
279  s->map = av_calloc(s->nch, sizeof(*s->map));
280  if (!s->map)
281  return AVERROR(ENOMEM);
282 
283  for (i = 0; i < s->nch; i++) {
284  s->map[i].in_channel_idx = i;
285  s->map[i].out_channel_idx = i;
286  }
287  } else if (out_ch_mask && out_ch_mask != fmt) {
288  av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
290  "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
291  s->channel_layout_str, buf);
292  return AVERROR(EINVAL);
293  } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
295  "Output channel layout %s does not match the number of channels mapped %d.\n",
296  s->channel_layout_str, s->nch);
297  return AVERROR(EINVAL);
298  }
299  s->output_layout = fmt;
300  }
301  if (!s->output_layout) {
302  av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and "
303  "cannot be guessed from the maps.\n");
304  return AVERROR(EINVAL);
305  }
306 
308  for (i = 0; i < s->nch; i++) {
309  s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
310  s->output_layout, s->map[i].out_channel);
311  }
312  }
313 
314  presence_mask = 0;
315  for (i = 0; i < s->nch; i++) {
316  uint64_t idx_mask;
317  int ret = check_idx_and_id(ctx, s->map[i].out_channel_idx, s->map[i].out_channel, s->output_layout, av_get_channel_layout_nb_channels(s->output_layout), "out");
318  if (ret < 0)
319  return ret;
320  idx_mask = (1ULL << s->map[i].out_channel_idx);
321  if (presence_mask & idx_mask) {
322  char layout_name[256];
323  av_get_channel_layout_string(layout_name, sizeof(layout_name), 0, s->output_layout);
324  av_log(ctx, AV_LOG_ERROR, "Mapping %d assigns channel #%d twice in output layout '%s'.\n",
325  i + 1, s->map[i].out_channel_idx, layout_name);
326  return AVERROR(EINVAL);
327  }
328  presence_mask |= idx_mask;
329  }
330 
331  return 0;
332 }
333 
335 {
336  ChannelMapContext *s = ctx->priv;
338  int ret;
339 
340  if ((ret = ff_set_common_formats (ctx, ff_planar_sample_fmts())) < 0 ||
341  (ret = ff_set_common_samplerates (ctx , ff_all_samplerates() )) < 0 ||
342  (ret = ff_add_channel_layout(&channel_layouts, s->output_layout)) < 0 ||
344  &ctx->outputs[0]->incfg.channel_layouts)) < 0)
345  return ret;
346 
348  &ctx->inputs[0]->outcfg.channel_layouts);
349 }
350 
352 {
353  AVFilterContext *ctx = inlink->dst;
354  AVFilterLink *outlink = ctx->outputs[0];
355  const ChannelMapContext *s = ctx->priv;
356  const int nch_in = inlink->channels;
357  const int nch_out = s->nch;
358  int ch;
359 
360  memcpy(s->source_planes, buf->extended_data,
361  nch_in * sizeof(s->source_planes[0]));
362 
363  if (nch_out > nch_in) {
364  if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
365  uint8_t **new_extended_data =
366  av_mallocz_array(nch_out, sizeof(*buf->extended_data));
367  if (!new_extended_data) {
368  av_frame_free(&buf);
369  return AVERROR(ENOMEM);
370  }
371  if (buf->extended_data == buf->data) {
372  buf->extended_data = new_extended_data;
373  } else {
374  av_free(buf->extended_data);
375  buf->extended_data = new_extended_data;
376  }
377  } else if (buf->extended_data != buf->data) {
378  av_free(buf->extended_data);
379  buf->extended_data = buf->data;
380  }
381  }
382 
383  for (ch = 0; ch < nch_out; ch++) {
384  buf->extended_data[s->map[ch].out_channel_idx] =
385  s->source_planes[s->map[ch].in_channel_idx];
386  }
387 
388  if (buf->data != buf->extended_data)
389  memcpy(buf->data, buf->extended_data,
390  FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
391 
392  buf->channel_layout = outlink->channel_layout;
393  buf->channels = outlink->channels;
394 
395  return ff_filter_frame(outlink, buf);
396 }
397 
399 {
400  AVFilterContext *ctx = inlink->dst;
401  ChannelMapContext *s = ctx->priv;
402  int i, err = 0;
403 
404  for (i = 0; i < s->nch; i++) {
405  struct ChannelMap *m = &s->map[i];
406 
407  if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR || s->mode == MAP_ONE_STR) {
409  inlink->channel_layout, m->in_channel);
410  }
411 
412  if (check_idx_and_id(ctx, m->in_channel_idx, m->in_channel, inlink->channel_layout, inlink->channels, "in") < 0)
413  err = AVERROR(EINVAL);
414  }
415 
416  av_freep(&s->source_planes);
417  s->source_planes = av_calloc(inlink->channels,
418  sizeof(*s->source_planes));
419  if (!s->source_planes)
420  return AVERROR(ENOMEM);
421 
422  return err;
423 }
424 
426  {
427  .name = "default",
428  .type = AVMEDIA_TYPE_AUDIO,
429  .filter_frame = channelmap_filter_frame,
430  .config_props = channelmap_config_input,
431  .needs_writable = 1,
432  },
433  { NULL }
434 };
435 
437  {
438  .name = "default",
439  .type = AVMEDIA_TYPE_AUDIO
440  },
441  { NULL }
442 };
443 
445  .name = "channelmap",
446  .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
447  .init = channelmap_init,
448  .uninit = channelmap_uninit,
449  .query_formats = channelmap_query_formats,
450  .priv_size = sizeof(ChannelMapContext),
451  .priv_class = &channelmap_class,
454 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
AVFILTER_DEFINE_CLASS(channelmap)
static int channelmap_config_input(AVFilterLink *inlink)
static const AVFilterPad avfilter_af_channelmap_outputs[]
#define F
Definition: af_channelmap.c:71
MappingMode
Definition: af_channelmap.c:47
@ MAP_ONE_STR
Definition: af_channelmap.c:50
@ MAP_PAIR_STR_INT
Definition: af_channelmap.c:53
@ MAP_PAIR_STR_STR
Definition: af_channelmap.c:54
@ MAP_ONE_INT
Definition: af_channelmap.c:49
@ MAP_PAIR_INT_STR
Definition: af_channelmap.c:52
@ MAP_NONE
Definition: af_channelmap.c:48
@ MAP_PAIR_INT_INT
Definition: af_channelmap.c:51
static const AVFilterPad avfilter_af_channelmap_inputs[]
static int check_idx_and_id(AVFilterContext *ctx, int channel_idx, uint64_t channel, uint64_t ch_layout, int nb_channels, const char *io)
static int get_channel_idx(char **map, int *ch, char delim)
Definition: af_channelmap.c:96
#define A
Definition: af_channelmap.c:70
static av_cold int channelmap_init(AVFilterContext *ctx)
AVFilter ff_af_channelmap
#define OFFSET(x)
Definition: af_channelmap.c:69
static const AVOption channelmap_options[]
Definition: af_channelmap.c:72
static char * split(char *message, char delim)
Definition: af_channelmap.c:89
static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
static int get_channel(char **map, uint64_t *ch, char delim)
static int channelmap_query_formats(AVFilterContext *ctx)
static void channelmap_uninit(AVFilterContext *ctx)
Definition: af_channelmap.c:82
#define av_cold
Definition: attributes.h:88
uint8_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
Main libavfilter public API header.
#define s(width, name)
Definition: cbs_vp9.c:257
int nb_channels
audio channel layout utility functions
common internal and external API header
#define FFMIN(a, b)
Definition: common.h:105
#define NULL
Definition: coverity.c:32
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:114
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:436
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:338
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:408
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:587
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:575
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:461
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:421
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
int av_get_channel_layout_channel_index(uint64_t channel_layout, uint64_t channel)
Get the index of a channel in channel_layout.
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
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.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
const char * av_get_channel_name(uint64_t channel)
Get the name of a given channel.
#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:203
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.h:211
const VDPAUPixFmtMap * map
int i
Definition: input.c:407
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVOptions.
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
Definition: log.h:67
A list of supported channel layouts.
Definition: formats.h:86
An instance of a filter.
Definition: avfilter.h:341
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int channels
number of audio channels, only used for audio.
Definition: frame.h:624
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:495
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:365
AVOption.
Definition: opt.h:248
enum MappingMode mode
Definition: af_channelmap.c:64
struct ChannelMap * map
Definition: af_channelmap.c:62
uint64_t output_layout
Definition: af_channelmap.c:61
uint8_t ** source_planes
Definition: af_channelmap.c:66
char * channel_layout_str
Definition: af_channelmap.c:60
int channel_idx
Definition: opus.h:149
uint64_t out_channel
layout describing the output channel
Definition: af_channelmap.c:42
int in_channel_idx
index of in_channel in the input stream data
Definition: af_channelmap.c:43
int out_channel_idx
Definition: af_channelmap.c:44
uint64_t in_channel
layout describing the input channel
Definition: af_channelmap.c:41
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
AVFormatContext * ctx
Definition: movenc.c:48
int len