]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_channelmap.c
avutil/pixfmt: Clarify the meaning of the alpha byte in RGB0 and similar formats
[ffmpeg] / libavfilter / af_channelmap.c
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"
29 #include "libavutil/channel_layout.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;
43     int in_channel_idx;
44     int out_channel_idx;
45 };
46
47 enum 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
58 typedef struct ChannelMapContext {
59     const AVClass *class;
60     AVFilterChannelLayouts *channel_layouts;
61     char *mapping_str;
62     char *channel_layout_str;
63     uint64_t output_layout;
64     struct ChannelMap map[MAX_CH];
65     int nch;
66     enum MappingMode mode;
67 } ChannelMapContext;
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 = split(*map, delim);
92     int len;
93     int n = 0;
94     if (!next && delim == '-')
95         return AVERROR(EINVAL);
96     if (!*map)
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)
121 {
122     ChannelMapContext *s = ctx->priv;
123     char *mapping, separator = '|';
124     int map_entries = 0;
125     char buf[256];
126     enum MappingMode mode;
127     uint64_t out_ch_mask = 0;
128     int i;
129
130     mapping = s->mapping_str;
131
132     if (!mapping) {
133         mode = MAP_NONE;
134     } else {
135         char *dash = strchr(mapping, '-');
136         if (!dash) {  // short mapping
137             if (av_isdigit(*mapping))
138                 mode = MAP_ONE_INT;
139             else
140                 mode = MAP_ONE_STR;
141         } else if (av_isdigit(*mapping)) {
142             if (av_isdigit(*(dash+1)))
143                 mode = MAP_PAIR_INT_INT;
144             else
145                 mode = MAP_PAIR_INT_STR;
146         } else {
147             if (av_isdigit(*(dash+1)))
148                 mode = MAP_PAIR_STR_INT;
149             else
150                 mode = MAP_PAIR_STR_STR;
151         }
152 #if FF_API_OLD_FILTER_OPTS
153         if (strchr(mapping, ',')) {
154             av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
155                    "'|' to separate the mappings.\n");
156             separator = ',';
157         }
158 #endif
159     }
160
161     if (mode != MAP_NONE) {
162         char *sep = mapping;
163         map_entries = 1;
164         while ((sep = strchr(sep, separator))) {
165             if (*++sep)  // Allow trailing comma
166                 map_entries++;
167         }
168     }
169
170     if (map_entries > MAX_CH) {
171         av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
172         return AVERROR(EINVAL);
173     }
174
175     for (i = 0; i < map_entries; i++) {
176         int in_ch_idx = -1, out_ch_idx = -1;
177         uint64_t in_ch = 0, out_ch = 0;
178         static const char err[] = "Failed to parse channel map\n";
179         switch (mode) {
180         case MAP_ONE_INT:
181             if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
182                 av_log(ctx, AV_LOG_ERROR, err);
183                 return AVERROR(EINVAL);
184             }
185             s->map[i].in_channel_idx  = in_ch_idx;
186             s->map[i].out_channel_idx = i;
187             break;
188         case MAP_ONE_STR:
189             if (get_channel(&mapping, &in_ch, separator) < 0) {
190                 av_log(ctx, AV_LOG_ERROR, err);
191                 return AVERROR(EINVAL);
192             }
193             s->map[i].in_channel      = in_ch;
194             s->map[i].out_channel_idx = i;
195             break;
196         case MAP_PAIR_INT_INT:
197             if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
198                 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
199                 av_log(ctx, AV_LOG_ERROR, err);
200                 return AVERROR(EINVAL);
201             }
202             s->map[i].in_channel_idx  = in_ch_idx;
203             s->map[i].out_channel_idx = out_ch_idx;
204             break;
205         case MAP_PAIR_INT_STR:
206             if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
207                 get_channel(&mapping, &out_ch, separator) < 0 ||
208                 out_ch & out_ch_mask) {
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     = out_ch;
214             out_ch_mask |= out_ch;
215             break;
216         case MAP_PAIR_STR_INT:
217             if (get_channel(&mapping, &in_ch, '-') < 0 ||
218                 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
219                 av_log(ctx, AV_LOG_ERROR, err);
220                 return AVERROR(EINVAL);
221             }
222             s->map[i].in_channel      = in_ch;
223             s->map[i].out_channel_idx = out_ch_idx;
224             break;
225         case MAP_PAIR_STR_STR:
226             if (get_channel(&mapping, &in_ch, '-') < 0 ||
227                 get_channel(&mapping, &out_ch, separator) < 0 ||
228                 out_ch & out_ch_mask) {
229                 av_log(ctx, AV_LOG_ERROR, err);
230                 return AVERROR(EINVAL);
231             }
232             s->map[i].in_channel = in_ch;
233             s->map[i].out_channel = out_ch;
234             out_ch_mask |= out_ch;
235             break;
236         }
237     }
238     s->mode          = mode;
239     s->nch           = map_entries;
240     s->output_layout = out_ch_mask ? out_ch_mask :
241                        av_get_default_channel_layout(map_entries);
242
243     if (s->channel_layout_str) {
244         uint64_t fmt;
245         if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
246             av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
247                    s->channel_layout_str);
248             return AVERROR(EINVAL);
249         }
250         if (mode == MAP_NONE) {
251             int i;
252             s->nch = av_get_channel_layout_nb_channels(fmt);
253             for (i = 0; i < s->nch; i++) {
254                 s->map[i].in_channel_idx  = i;
255                 s->map[i].out_channel_idx = i;
256             }
257         } else if (out_ch_mask && out_ch_mask != fmt) {
258             av_get_channel_layout_string(buf, sizeof(buf), 0, out_ch_mask);
259             av_log(ctx, AV_LOG_ERROR,
260                    "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
261                    s->channel_layout_str, buf);
262             return AVERROR(EINVAL);
263         } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
264             av_log(ctx, AV_LOG_ERROR,
265                    "Output channel layout %s does not match the number of channels mapped %d.\n",
266                    s->channel_layout_str, s->nch);
267             return AVERROR(EINVAL);
268         }
269         s->output_layout = fmt;
270     }
271     if (!s->output_layout) {
272         av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and "
273                "cannot be guessed from the maps.\n");
274         return AVERROR(EINVAL);
275     }
276
277     ff_add_channel_layout(&s->channel_layouts, s->output_layout);
278
279     if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
280         for (i = 0; i < s->nch; i++) {
281             s->map[i].out_channel_idx = av_get_channel_layout_channel_index(
282                 s->output_layout, s->map[i].out_channel);
283         }
284     }
285
286     return 0;
287 }
288
289 static int channelmap_query_formats(AVFilterContext *ctx)
290 {
291     ChannelMapContext *s = ctx->priv;
292     AVFilterChannelLayouts *layouts;
293
294     ff_set_common_formats(ctx, ff_planar_sample_fmts());
295     ff_set_common_samplerates(ctx, ff_all_samplerates());
296
297     layouts = ff_all_channel_layouts();
298     if (!layouts)
299         return AVERROR(ENOMEM);
300
301     ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
302     ff_channel_layouts_ref(s->channel_layouts,       &ctx->outputs[0]->in_channel_layouts);
303
304     return 0;
305 }
306
307 static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
308 {
309     AVFilterContext  *ctx = inlink->dst;
310     AVFilterLink *outlink = ctx->outputs[0];
311     const ChannelMapContext *s = ctx->priv;
312     const int nch_in = av_get_channel_layout_nb_channels(inlink->channel_layout);
313     const int nch_out = s->nch;
314     int ch;
315     uint8_t *source_planes[MAX_CH];
316
317     memcpy(source_planes, buf->extended_data,
318            nch_in * sizeof(source_planes[0]));
319
320     if (nch_out > nch_in) {
321         if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
322             uint8_t **new_extended_data =
323                 av_mallocz_array(nch_out, sizeof(*buf->extended_data));
324             if (!new_extended_data) {
325                 av_frame_free(&buf);
326                 return AVERROR(ENOMEM);
327             }
328             if (buf->extended_data == buf->data) {
329                 buf->extended_data = new_extended_data;
330             } else {
331                 av_free(buf->extended_data);
332                 buf->extended_data = new_extended_data;
333             }
334         } else if (buf->extended_data != buf->data) {
335             av_free(buf->extended_data);
336             buf->extended_data = buf->data;
337         }
338     }
339
340     for (ch = 0; ch < nch_out; ch++) {
341         buf->extended_data[s->map[ch].out_channel_idx] =
342             source_planes[s->map[ch].in_channel_idx];
343     }
344
345     if (buf->data != buf->extended_data)
346         memcpy(buf->data, buf->extended_data,
347            FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
348
349     buf->channel_layout = outlink->channel_layout;
350
351     return ff_filter_frame(outlink, buf);
352 }
353
354 static int channelmap_config_input(AVFilterLink *inlink)
355 {
356     AVFilterContext *ctx = inlink->dst;
357     ChannelMapContext *s = ctx->priv;
358     int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
359     int i, err = 0;
360     const char *channel_name;
361     char layout_name[256];
362
363     for (i = 0; i < s->nch; i++) {
364         struct ChannelMap *m = &s->map[i];
365
366         if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
367             m->in_channel_idx = av_get_channel_layout_channel_index(
368                 inlink->channel_layout, m->in_channel);
369         }
370
371         if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
372             av_get_channel_layout_string(layout_name, sizeof(layout_name),
373                                          0, inlink->channel_layout);
374             if (m->in_channel) {
375                 channel_name = av_get_channel_name(m->in_channel);
376                 av_log(ctx, AV_LOG_ERROR,
377                        "input channel '%s' not available from input layout '%s'\n",
378                        channel_name, layout_name);
379             } else {
380                 av_log(ctx, AV_LOG_ERROR,
381                        "input channel #%d not available from input layout '%s'\n",
382                        m->in_channel_idx, layout_name);
383             }
384             err = AVERROR(EINVAL);
385         }
386     }
387
388     return err;
389 }
390
391 static const AVFilterPad avfilter_af_channelmap_inputs[] = {
392     {
393         .name           = "default",
394         .type           = AVMEDIA_TYPE_AUDIO,
395         .filter_frame   = channelmap_filter_frame,
396         .config_props   = channelmap_config_input,
397         .needs_writable = 1,
398     },
399     { NULL }
400 };
401
402 static const AVFilterPad avfilter_af_channelmap_outputs[] = {
403     {
404         .name = "default",
405         .type = AVMEDIA_TYPE_AUDIO
406     },
407     { NULL }
408 };
409
410 AVFilter ff_af_channelmap = {
411     .name          = "channelmap",
412     .description   = NULL_IF_CONFIG_SMALL("Remap audio channels."),
413     .init          = channelmap_init,
414     .query_formats = channelmap_query_formats,
415     .priv_size     = sizeof(ChannelMapContext),
416     .priv_class    = &channelmap_class,
417     .inputs        = avfilter_af_channelmap_inputs,
418     .outputs       = avfilter_af_channelmap_outputs,
419 };