h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / af_channelmap.c
CommitLineData
41e637e4
AC
1/*
2 * Copyright (c) 2012 Google, Inc.
3 *
4 * This file is part of Libav.
5 *
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.
10 *
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.
15 *
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
19 */
20
21/**
22 * @file
23 * audio channel mapping filter
24 */
25
26#include <ctype.h>
27
41e637e4 28#include "libavutil/avstring.h"
a903f8f0 29#include "libavutil/channel_layout.h"
1d9c2dc8 30#include "libavutil/common.h"
41e637e4
AC
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
40struct ChannelMap {
41 uint64_t in_channel;
42 uint64_t out_channel;
43 int in_channel_idx;
44 int out_channel_idx;
45};
46
47enum MappingMode {
48 MAP_NONE,
49 MAP_ONE_INT,
50 MAP_ONE_STR,
51 MAP_PAIR_INT_INT,
52 MAP_PAIR_INT_STR,
53 MAP_PAIR_STR_INT,
54 MAP_PAIR_STR_STR
55};
56
57#define MAX_CH 64
58typedef struct ChannelMapContext {
59 const AVClass *class;
41e637e4
AC
60 char *mapping_str;
61 char *channel_layout_str;
62 uint64_t output_layout;
63 struct ChannelMap map[MAX_CH];
64 int nch;
65 enum MappingMode mode;
66} ChannelMapContext;
67
68#define OFFSET(x) offsetof(ChannelMapContext, x)
69#define A AV_OPT_FLAG_AUDIO_PARAM
70static 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 },
75 { NULL },
76};
77
78static const AVClass channelmap_class = {
79 .class_name = "channel map filter",
80 .item_name = av_default_item_name,
81 .option = options,
82 .version = LIBAVUTIL_VERSION_INT,
83};
84
85static char* split(char *message, char delim) {
86 char *next = strchr(message, delim);
87 if (next)
88 *next++ = '\0';
89 return next;
90}
91
92static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
93{
94 char *next = split(*map, delim);
95 int len;
96 int n = 0;
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
109static 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
c43a7eca 121static av_cold int channelmap_init(AVFilterContext *ctx)
41e637e4
AC
122{
123 ChannelMapContext *s = ctx->priv;
ba8efac9 124 char *mapping, separator = '|';
41e637e4
AC
125 int map_entries = 0;
126 char buf[256];
127 enum MappingMode mode;
128 uint64_t out_ch_mask = 0;
129 int i;
130
41e637e4
AC
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
efa7f420 138 if (av_isdigit(*mapping))
41e637e4
AC
139 mode = MAP_ONE_INT;
140 else
141 mode = MAP_ONE_STR;
efa7f420
RD
142 } else if (av_isdigit(*mapping)) {
143 if (av_isdigit(*(dash+1)))
41e637e4
AC
144 mode = MAP_PAIR_INT_INT;
145 else
146 mode = MAP_PAIR_INT_STR;
147 } else {
efa7f420 148 if (av_isdigit(*(dash+1)))
41e637e4
AC
149 mode = MAP_PAIR_STR_INT;
150 else
151 mode = MAP_PAIR_STR_STR;
152 }
153 }
154
155 if (mode != MAP_NONE) {
ba8efac9 156 char *sep = mapping;
41e637e4 157 map_entries = 1;
ba8efac9
AK
158 while ((sep = strchr(sep, separator))) {
159 if (*++sep) // Allow trailing comma
41e637e4
AC
160 map_entries++;
161 }
162 }
163
164 if (map_entries > MAX_CH) {
165 av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
949c603e 166 return AVERROR(EINVAL);
41e637e4
AC
167 }
168
169 for (i = 0; i < map_entries; i++) {
170 int in_ch_idx = -1, out_ch_idx = -1;
171 uint64_t in_ch = 0, out_ch = 0;
172 static const char err[] = "Failed to parse channel map\n";
173 switch (mode) {
174 case MAP_ONE_INT:
ba8efac9 175 if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
41e637e4 176 av_log(ctx, AV_LOG_ERROR, err);
949c603e 177 return AVERROR(EINVAL);
41e637e4
AC
178 }
179 s->map[i].in_channel_idx = in_ch_idx;
180 s->map[i].out_channel_idx = i;
181 break;
182 case MAP_ONE_STR:
e8436126 183 if (get_channel(&mapping, &in_ch, separator) < 0) {
41e637e4 184 av_log(ctx, AV_LOG_ERROR, err);
949c603e 185 return AVERROR(EINVAL);
41e637e4
AC
186 }
187 s->map[i].in_channel = in_ch;
188 s->map[i].out_channel_idx = i;
189 break;
190 case MAP_PAIR_INT_INT:
191 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
ba8efac9 192 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
41e637e4 193 av_log(ctx, AV_LOG_ERROR, err);
949c603e 194 return AVERROR(EINVAL);
41e637e4
AC
195 }
196 s->map[i].in_channel_idx = in_ch_idx;
197 s->map[i].out_channel_idx = out_ch_idx;
198 break;
199 case MAP_PAIR_INT_STR:
200 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
ba8efac9 201 get_channel(&mapping, &out_ch, separator) < 0 ||
41e637e4
AC
202 out_ch & out_ch_mask) {
203 av_log(ctx, AV_LOG_ERROR, err);
949c603e 204 return AVERROR(EINVAL);
41e637e4
AC
205 }
206 s->map[i].in_channel_idx = in_ch_idx;
207 s->map[i].out_channel = out_ch;
208 out_ch_mask |= out_ch;
209 break;
210 case MAP_PAIR_STR_INT:
211 if (get_channel(&mapping, &in_ch, '-') < 0 ||
ba8efac9 212 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
41e637e4 213 av_log(ctx, AV_LOG_ERROR, err);
949c603e 214 return AVERROR(EINVAL);
41e637e4
AC
215 }
216 s->map[i].in_channel = in_ch;
217 s->map[i].out_channel_idx = out_ch_idx;
218 break;
219 case MAP_PAIR_STR_STR:
220 if (get_channel(&mapping, &in_ch, '-') < 0 ||
ba8efac9 221 get_channel(&mapping, &out_ch, separator) < 0 ||
41e637e4
AC
222 out_ch & out_ch_mask) {
223 av_log(ctx, AV_LOG_ERROR, err);
949c603e 224 return AVERROR(EINVAL);
41e637e4
AC
225 }
226 s->map[i].in_channel = in_ch;
227 s->map[i].out_channel = out_ch;
228 out_ch_mask |= out_ch;
229 break;
230 }
231 }
232 s->mode = mode;
233 s->nch = map_entries;
234 s->output_layout = out_ch_mask ? out_ch_mask :
235 av_get_default_channel_layout(map_entries);
236
237 if (s->channel_layout_str) {
238 uint64_t fmt;
239 if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
240 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
241 s->channel_layout_str);
949c603e 242 return AVERROR(EINVAL);
41e637e4
AC
243 }
244 if (mode == MAP_NONE) {
245 int i;
246 s->nch = av_get_channel_layout_nb_channels(fmt);
247 for (i = 0; i < s->nch; i++) {
248 s->map[i].in_channel_idx = i;
249 s->map[i].out_channel_idx = i;
250 }
251 } else if (out_ch_mask && out_ch_mask != fmt) {
252 av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
253 av_log(ctx, AV_LOG_ERROR,
254 "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
255 s->channel_layout_str, buf);
949c603e 256 return AVERROR(EINVAL);
41e637e4
AC
257 } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
258 av_log(ctx, AV_LOG_ERROR,
259 "Output channel layout %s does not match the number of channels mapped %d.\n",
260 s->channel_layout_str, s->nch);
949c603e 261 return AVERROR(EINVAL);
41e637e4
AC
262 }
263 s->output_layout = fmt;
264 }
8aaab111
AK
265 if (!s->output_layout) {
266 av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and "
267 "cannot be guessed from the maps.\n");
268 return AVERROR(EINVAL);
269 }
270
41e637e4
AC
271 if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
272 for (i = 0; i < s->nch; i++) {
273 s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
274 s->output_layout, s->map[i].out_channel);
275 }
276 }
277
949c603e 278 return 0;
41e637e4
AC
279}
280
281static int channelmap_query_formats(AVFilterContext *ctx)
282{
283 ChannelMapContext *s = ctx->priv;
22ecfcd4
AK
284 AVFilterChannelLayouts *channel_layouts = NULL;
285
286 ff_add_channel_layout(&channel_layouts, s->output_layout);
41e637e4
AC
287
288 ff_set_common_formats(ctx, ff_planar_sample_fmts());
289 ff_set_common_samplerates(ctx, ff_all_samplerates());
290 ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts);
22ecfcd4 291 ff_channel_layouts_ref(channel_layouts, &ctx->outputs[0]->in_channel_layouts);
41e637e4
AC
292
293 return 0;
294}
295
7e350379 296static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
41e637e4
AC
297{
298 AVFilterContext *ctx = inlink->dst;
299 AVFilterLink *outlink = ctx->outputs[0];
300 const ChannelMapContext *s = ctx->priv;
301 const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
302 const int nch_out = s->nch;
303 int ch;
304 uint8_t *source_planes[MAX_CH];
305
306 memcpy(source_planes, buf->extended_data,
307 nch_in * sizeof(source_planes[0]));
308
309 if (nch_out > nch_in) {
310 if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
311 uint8_t **new_extended_data =
312 av_mallocz(nch_out * sizeof(*buf->extended_data));
cd991462 313 if (!new_extended_data) {
7e350379 314 av_frame_free(&buf);
cd991462
AK
315 return AVERROR(ENOMEM);
316 }
41e637e4
AC
317 if (buf->extended_data == buf->data) {
318 buf->extended_data = new_extended_data;
319 } else {
41e637e4 320 av_free(buf->extended_data);
1afd7a11 321 buf->extended_data = new_extended_data;
41e637e4
AC
322 }
323 } else if (buf->extended_data != buf->data) {
324 av_free(buf->extended_data);
325 buf->extended_data = buf->data;
326 }
327 }
328
329 for (ch = 0; ch < nch_out; ch++) {
330 buf->extended_data[s->map[ch].out_channel_idx] =
331 source_planes[s->map[ch].in_channel_idx];
332 }
333
334 if (buf->data != buf->extended_data)
335 memcpy(buf->data, buf->extended_data,
336 FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
337
f0e95948
LB
338 buf->channel_layout = outlink->channel_layout;
339
565e4993 340 return ff_filter_frame(outlink, buf);
41e637e4
AC
341}
342
343static int channelmap_config_input(AVFilterLink *inlink)
344{
345 AVFilterContext *ctx = inlink->dst;
346 ChannelMapContext *s = ctx->priv;
aafed117 347 int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
41e637e4
AC
348 int i, err = 0;
349 const char *channel_name;
350 char layout_name[256];
351
aafed117 352 for (i = 0; i < s->nch; i++) {
c0771a1a
AK
353 struct ChannelMap *m = &s->map[i];
354
aafed117 355 if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
c0771a1a
AK
356 m->in_channel_idx = av_get_channel_layout_channel_index(
357 inlink->channel_layout, m->in_channel);
aafed117
AK
358 }
359
c0771a1a 360 if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
aafed117
AK
361 av_get_channel_layout_string(layout_name, sizeof(layout_name),
362 0, inlink->channel_layout);
c0771a1a
AK
363 if (m->in_channel) {
364 channel_name = av_get_channel_name(m->in_channel);
41e637e4
AC
365 av_log(ctx, AV_LOG_ERROR,
366 "input channel '%s' not available from input layout '%s'\n",
367 channel_name, layout_name);
aafed117
AK
368 } else {
369 av_log(ctx, AV_LOG_ERROR,
370 "input channel #%d not available from input layout '%s'\n",
c0771a1a 371 m->in_channel_idx, layout_name);
41e637e4 372 }
aafed117 373 err = AVERROR(EINVAL);
41e637e4
AC
374 }
375 }
376
377 return err;
378}
379
568c70e7
MR
380static const AVFilterPad avfilter_af_channelmap_inputs[] = {
381 {
382 .name = "default",
383 .type = AVMEDIA_TYPE_AUDIO,
565e4993 384 .filter_frame = channelmap_filter_frame,
568c70e7
MR
385 .config_props = channelmap_config_input
386 },
387 { NULL }
388};
389
390static const AVFilterPad avfilter_af_channelmap_outputs[] = {
391 {
392 .name = "default",
393 .type = AVMEDIA_TYPE_AUDIO
394 },
395 { NULL }
396};
397
cd43ca04 398AVFilter ff_af_channelmap = {
41e637e4
AC
399 .name = "channelmap",
400 .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
401 .init = channelmap_init,
402 .query_formats = channelmap_query_formats,
403 .priv_size = sizeof(ChannelMapContext),
ba8efac9 404 .priv_class = &channelmap_class,
41e637e4 405
568c70e7
MR
406 .inputs = avfilter_af_channelmap_inputs,
407 .outputs = avfilter_af_channelmap_outputs,
41e637e4 408};