Browse code

lavfi: Add the af_channelmap audio channel mapping filter.

Inspired by MPlayer's af_channels filter and SoX's remix effect.

Alex Converse authored on 2012/06/09 10:42:53
Showing 5 changed files
... ...
@@ -29,6 +29,7 @@ version <next>:
29 29
 - iLBC encoding/decoding via libilbc
30 30
 - Microsoft Screen 1 decoder
31 31
 - join audio filter
32
+- audio channel mapping filter
32 33
 
33 34
 
34 35
 version 0.8:
... ...
@@ -232,6 +232,39 @@ front_center.wav -map '[LFE]' lfe.wav -map '[SL]' side_left.wav -map '[SR]'
232 232
 side_right.wav
233 233
 @end example
234 234
 
235
+@section channelmap
236
+Remap input channels to new locations.
237
+
238
+This filter accepts the following named parameters:
239
+@table @option
240
+@item channel_layout
241
+Channel layout of the output stream.
242
+
243
+@item map
244
+Map channels from input to output. The argument is a comma-separated list of
245
+mappings, each in the @code{@var{in_channel}-@var{out_channel}} or
246
+@var{in_channel} form. @var{in_channel} can be either the name of the input
247
+channel (e.g. FL for front left) or its index in the input channel layout.
248
+@var{out_channel} is the name of the output channel or its index in the output
249
+channel layout. If @var{out_channel} is not given then it is implicitly an
250
+index, starting with zero and increasing by one for each mapping.
251
+@end table
252
+
253
+If no mapping is present, the filter will implicitly map input channels to
254
+output channels preserving index.
255
+
256
+For example, assuming a 5.1+downmix input MOV file
257
+@example
258
+avconv -i in.mov -filter 'channelmap=map=DL-FL\,DR-FR' out.wav
259
+@end example
260
+will create an output WAV file tagged as stereo from the downmix channels of
261
+the input.
262
+
263
+To fix a 5.1 WAV improperly encoded in AAC's native channel order
264
+@example
265
+avconv -i in.wav -filter 'channelmap=1\,2\,0\,5\,3\,4:channel_layout=5.1' out.wav
266
+@end example
267
+
235 268
 @section join
236 269
 Join multiple input streams into one multi-channel stream.
237 270
 
... ...
@@ -31,6 +31,7 @@ OBJS-$(CONFIG_AMIX_FILTER)                   += af_amix.o
31 31
 OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
32 32
 OBJS-$(CONFIG_ASPLIT_FILTER)                 += split.o
33 33
 OBJS-$(CONFIG_ASYNCTS_FILTER)                += af_asyncts.o
34
+OBJS-$(CONFIG_CHANNELMAP_FILTER)             += af_channelmap.o
34 35
 OBJS-$(CONFIG_CHANNELSPLIT_FILTER)           += af_channelsplit.o
35 36
 OBJS-$(CONFIG_JOIN_FILTER)                   += af_join.o
36 37
 OBJS-$(CONFIG_RESAMPLE_FILTER)               += af_resample.o
37 38
new file mode 100644
... ...
@@ -0,0 +1,402 @@
0
+/*
1
+ * Copyright (c) 2012 Google, Inc.
2
+ *
3
+ * This file is part of Libav.
4
+ *
5
+ * Libav is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * Libav is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with Libav; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * audio channel mapping filter
23
+ */
24
+
25
+#include <ctype.h>
26
+
27
+#include "libavutil/audioconvert.h"
28
+#include "libavutil/avstring.h"
29
+#include "libavutil/mathematics.h"
30
+#include "libavutil/opt.h"
31
+#include "libavutil/samplefmt.h"
32
+
33
+#include "audio.h"
34
+#include "avfilter.h"
35
+#include "formats.h"
36
+#include "internal.h"
37
+
38
+struct ChannelMap {
39
+    uint64_t in_channel;
40
+    uint64_t out_channel;
41
+    int in_channel_idx;
42
+    int out_channel_idx;
43
+};
44
+
45
+enum MappingMode {
46
+    MAP_NONE,
47
+    MAP_ONE_INT,
48
+    MAP_ONE_STR,
49
+    MAP_PAIR_INT_INT,
50
+    MAP_PAIR_INT_STR,
51
+    MAP_PAIR_STR_INT,
52
+    MAP_PAIR_STR_STR
53
+};
54
+
55
+#define MAX_CH 64
56
+typedef struct ChannelMapContext {
57
+    const AVClass *class;
58
+    AVFilterChannelLayouts *channel_layouts;
59
+    char *mapping_str;
60
+    char *channel_layout_str;
61
+    uint64_t output_layout;
62
+    struct ChannelMap map[MAX_CH];
63
+    int nch;
64
+    enum MappingMode mode;
65
+} ChannelMapContext;
66
+
67
+#define OFFSET(x) offsetof(ChannelMapContext, x)
68
+#define A AV_OPT_FLAG_AUDIO_PARAM
69
+static const AVOption options[] = {
70
+    { "map", "A comma-separated list of input channel numbers in output order.",
71
+          OFFSET(mapping_str),        AV_OPT_TYPE_STRING, .flags = A },
72
+    { "channel_layout", "Output channel layout.",
73
+          OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
74
+    { NULL },
75
+};
76
+
77
+static const AVClass channelmap_class = {
78
+    .class_name = "channel map filter",
79
+    .item_name  = av_default_item_name,
80
+    .option     = options,
81
+    .version    = LIBAVUTIL_VERSION_INT,
82
+};
83
+
84
+static char* split(char *message, char delim) {
85
+    char *next = strchr(message, delim);
86
+    if (next)
87
+      *next++ = '\0';
88
+    return next;
89
+}
90
+
91
+static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
92
+{
93
+    char *next = split(*map, delim);
94
+    int len;
95
+    int n = 0;
96
+    if (!next && delim == '-')
97
+        return AVERROR(EINVAL);
98
+    len = strlen(*map);
99
+    sscanf(*map, "%d%n", ch, &n);
100
+    if (n != len)
101
+        return AVERROR(EINVAL);
102
+    if (*ch < 0 || *ch > max_ch)
103
+        return AVERROR(EINVAL);
104
+    *map = next;
105
+    return 0;
106
+}
107
+
108
+static int get_channel(char **map, uint64_t *ch, char delim)
109
+{
110
+    char *next = split(*map, delim);
111
+    if (!next && delim == '-')
112
+        return AVERROR(EINVAL);
113
+    *ch = av_get_channel_layout(*map);
114
+    if (av_get_channel_layout_nb_channels(*ch) != 1)
115
+        return AVERROR(EINVAL);
116
+    *map = next;
117
+    return 0;
118
+}
119
+
120
+static av_cold int channelmap_init(AVFilterContext *ctx, const char *args,
121
+                                   void *opaque)
122
+{
123
+    ChannelMapContext *s = ctx->priv;
124
+    int ret;
125
+    char *mapping;
126
+    enum mode;
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
+    if (!args) {
134
+        av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
135
+        return AVERROR(EINVAL);
136
+    }
137
+
138
+    s->class = &channelmap_class;
139
+    av_opt_set_defaults(s);
140
+
141
+    if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
142
+        av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
143
+        return ret;
144
+    }
145
+
146
+    mapping = s->mapping_str;
147
+
148
+    if (!mapping) {
149
+        mode = MAP_NONE;
150
+    } else {
151
+        char *dash = strchr(mapping, '-');
152
+        if (!dash) {  // short mapping
153
+            if (isdigit(*mapping))
154
+                mode = MAP_ONE_INT;
155
+            else
156
+                mode = MAP_ONE_STR;
157
+        } else if (isdigit(*mapping)) {
158
+            if (isdigit(*(dash+1)))
159
+                mode = MAP_PAIR_INT_INT;
160
+            else
161
+                mode = MAP_PAIR_INT_STR;
162
+        } else {
163
+            if (isdigit(*(dash+1)))
164
+                mode = MAP_PAIR_STR_INT;
165
+            else
166
+                mode = MAP_PAIR_STR_STR;
167
+        }
168
+    }
169
+
170
+    if (mode != MAP_NONE) {
171
+        char *comma = mapping;
172
+        map_entries = 1;
173
+        while ((comma = strchr(comma, ','))) {
174
+            if (*++comma)  // Allow trailing comma
175
+                map_entries++;
176
+        }
177
+    }
178
+
179
+    if (map_entries > MAX_CH) {
180
+        av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
181
+        ret = AVERROR(EINVAL);
182
+        goto fail;
183
+    }
184
+
185
+    for (i = 0; i < map_entries; i++) {
186
+        int in_ch_idx = -1, out_ch_idx = -1;
187
+        uint64_t in_ch = 0, out_ch = 0;
188
+        static const char err[] = "Failed to parse channel map\n";
189
+        switch (mode) {
190
+        case MAP_ONE_INT:
191
+            if (get_channel_idx(&mapping, &in_ch_idx, ',', MAX_CH) < 0) {
192
+                ret = AVERROR(EINVAL);
193
+                av_log(ctx, AV_LOG_ERROR, err);
194
+                goto fail;
195
+            }
196
+            s->map[i].in_channel_idx  = in_ch_idx;
197
+            s->map[i].out_channel_idx = i;
198
+            break;
199
+        case MAP_ONE_STR:
200
+            if (!get_channel(&mapping, &in_ch, ',')) {
201
+                av_log(ctx, AV_LOG_ERROR, err);
202
+                ret = AVERROR(EINVAL);
203
+                goto fail;
204
+            }
205
+            s->map[i].in_channel      = in_ch;
206
+            s->map[i].out_channel_idx = i;
207
+            break;
208
+        case MAP_PAIR_INT_INT:
209
+            if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
210
+                get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
211
+                av_log(ctx, AV_LOG_ERROR, err);
212
+                ret = AVERROR(EINVAL);
213
+                goto fail;
214
+            }
215
+            s->map[i].in_channel_idx  = in_ch_idx;
216
+            s->map[i].out_channel_idx = out_ch_idx;
217
+            break;
218
+        case MAP_PAIR_INT_STR:
219
+            if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
220
+                get_channel(&mapping, &out_ch, ',') < 0 ||
221
+                out_ch & out_ch_mask) {
222
+                av_log(ctx, AV_LOG_ERROR, err);
223
+                ret = AVERROR(EINVAL);
224
+                goto fail;
225
+            }
226
+            s->map[i].in_channel_idx  = in_ch_idx;
227
+            s->map[i].out_channel     = out_ch;
228
+            out_ch_mask |= out_ch;
229
+            break;
230
+        case MAP_PAIR_STR_INT:
231
+            if (get_channel(&mapping, &in_ch, '-') < 0 ||
232
+                get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
233
+                av_log(ctx, AV_LOG_ERROR, err);
234
+                ret = AVERROR(EINVAL);
235
+                goto fail;
236
+            }
237
+            s->map[i].in_channel      = in_ch;
238
+            s->map[i].out_channel_idx = out_ch_idx;
239
+            break;
240
+        case MAP_PAIR_STR_STR:
241
+            if (get_channel(&mapping, &in_ch, '-') < 0 ||
242
+                get_channel(&mapping, &out_ch, ',') < 0 ||
243
+                out_ch & out_ch_mask) {
244
+                av_log(ctx, AV_LOG_ERROR, err);
245
+                ret = AVERROR(EINVAL);
246
+                goto fail;
247
+            }
248
+            s->map[i].in_channel = in_ch;
249
+            s->map[i].out_channel = out_ch;
250
+            out_ch_mask |= out_ch;
251
+            break;
252
+        }
253
+    }
254
+    s->mode          = mode;
255
+    s->nch           = map_entries;
256
+    s->output_layout = out_ch_mask ? out_ch_mask :
257
+                       av_get_default_channel_layout(map_entries);
258
+
259
+    if (s->channel_layout_str) {
260
+        uint64_t fmt;
261
+        if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
262
+            av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
263
+                   s->channel_layout_str);
264
+            ret = AVERROR(EINVAL);
265
+            goto fail;
266
+        }
267
+        if (mode == MAP_NONE) {
268
+            int i;
269
+            s->nch = av_get_channel_layout_nb_channels(fmt);
270
+            for (i = 0; i < s->nch; i++) {
271
+                s->map[i].in_channel_idx  = i;
272
+                s->map[i].out_channel_idx = i;
273
+            }
274
+        } else if (out_ch_mask && out_ch_mask != fmt) {
275
+            av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
276
+            av_log(ctx, AV_LOG_ERROR,
277
+                   "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
278
+                   s->channel_layout_str, buf);
279
+            ret = AVERROR(EINVAL);
280
+            goto fail;
281
+        } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
282
+            av_log(ctx, AV_LOG_ERROR,
283
+                   "Output channel layout %s does not match the number of channels mapped %d.\n",
284
+                   s->channel_layout_str, s->nch);
285
+            ret = AVERROR(EINVAL);
286
+            goto fail;
287
+        }
288
+        s->output_layout = fmt;
289
+    }
290
+    ff_add_channel_layout(&s->channel_layouts, s->output_layout);
291
+
292
+    if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
293
+        for (i = 0; i < s->nch; i++) {
294
+            s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
295
+                s->output_layout, s->map[i].out_channel);
296
+        }
297
+    }
298
+
299
+fail:
300
+    av_opt_free(s);
301
+    return ret;
302
+}
303
+
304
+static int channelmap_query_formats(AVFilterContext *ctx)
305
+{
306
+    ChannelMapContext *s = ctx->priv;
307
+
308
+    ff_set_common_formats(ctx, ff_planar_sample_fmts());
309
+    ff_set_common_samplerates(ctx, ff_all_samplerates());
310
+    ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts);
311
+    ff_channel_layouts_ref(s->channel_layouts,       &ctx->outputs[0]->in_channel_layouts);
312
+
313
+    return 0;
314
+}
315
+
316
+static void channelmap_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
317
+{
318
+    AVFilterContext  *ctx = inlink->dst;
319
+    AVFilterLink *outlink = ctx->outputs[0];
320
+    const ChannelMapContext *s = ctx->priv;
321
+    const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
322
+    const int nch_out = s->nch;
323
+    int ch;
324
+    uint8_t *source_planes[MAX_CH];
325
+
326
+    memcpy(source_planes, buf->extended_data,
327
+           nch_in * sizeof(source_planes[0]));
328
+
329
+    if (nch_out > nch_in) {
330
+        if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
331
+            uint8_t **new_extended_data =
332
+                av_mallocz(nch_out * sizeof(*buf->extended_data));
333
+            if (!new_extended_data)
334
+                return;
335
+            if (buf->extended_data == buf->data) {
336
+                buf->extended_data = new_extended_data;
337
+            } else {
338
+                buf->extended_data = new_extended_data;
339
+                av_free(buf->extended_data);
340
+            }
341
+        } else if (buf->extended_data != buf->data) {
342
+            av_free(buf->extended_data);
343
+            buf->extended_data = buf->data;
344
+        }
345
+    }
346
+
347
+    for (ch = 0; ch < nch_out; ch++) {
348
+        buf->extended_data[s->map[ch].out_channel_idx] =
349
+            source_planes[s->map[ch].in_channel_idx];
350
+    }
351
+
352
+    if (buf->data != buf->extended_data)
353
+        memcpy(buf->data, buf->extended_data,
354
+           FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
355
+
356
+    ff_filter_samples(outlink, buf);
357
+}
358
+
359
+static int channelmap_config_input(AVFilterLink *inlink)
360
+{
361
+    AVFilterContext *ctx = inlink->dst;
362
+    ChannelMapContext *s = ctx->priv;
363
+    int i, err = 0;
364
+    const char *channel_name;
365
+    char layout_name[256];
366
+
367
+    if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
368
+        for (i = 0; i < s->nch; i++) {
369
+            s->map[i].in_channel_idx = av_get_channel_layout_channel_index(
370
+                inlink->channel_layout, s->map[i].in_channel);
371
+            if (s->map[i].in_channel_idx < 0) {
372
+                channel_name = av_get_channel_name(s->map[i].in_channel);
373
+                av_get_channel_layout_string(layout_name, sizeof(layout_name),
374
+                                             0, inlink->channel_layout);
375
+                av_log(ctx, AV_LOG_ERROR,
376
+                       "input channel '%s' not available from input layout '%s'\n",
377
+                       channel_name, layout_name);
378
+                err = AVERROR(EINVAL);
379
+            }
380
+        }
381
+    }
382
+
383
+    return err;
384
+}
385
+
386
+AVFilter avfilter_af_channelmap = {
387
+    .name          = "channelmap",
388
+    .description   = NULL_IF_CONFIG_SMALL("Remap audio channels."),
389
+    .init          = channelmap_init,
390
+    .query_formats = channelmap_query_formats,
391
+    .priv_size     = sizeof(ChannelMapContext),
392
+
393
+    .inputs        = (AVFilterPad[]) {{ .name            = "default",
394
+                                        .type            = AVMEDIA_TYPE_AUDIO,
395
+                                        .filter_samples  = channelmap_filter_samples,
396
+                                        .config_props    = channelmap_config_input },
397
+                                      { .name = NULL }},
398
+    .outputs       = (AVFilterPad[]) {{ .name            = "default",
399
+                                        .type            = AVMEDIA_TYPE_AUDIO },
400
+                                      { .name = NULL }},
401
+};
... ...
@@ -40,6 +40,7 @@ void avfilter_register_all(void)
40 40
     REGISTER_FILTER (ANULL,       anull,       af);
41 41
     REGISTER_FILTER (ASPLIT,      asplit,      af);
42 42
     REGISTER_FILTER (ASYNCTS,     asyncts,     af);
43
+    REGISTER_FILTER (CHANNELMAP,  channelmap,  af);
43 44
     REGISTER_FILTER (CHANNELSPLIT,channelsplit,af);
44 45
     REGISTER_FILTER (JOIN,        join,        af);
45 46
     REGISTER_FILTER (RESAMPLE,    resample,    af);