]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_join.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / af_join.c
1 /*
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 join filter
23  *
24  * Join multiple audio inputs as different channels in
25  * a single output
26  */
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #include "libavutil/opt.h"
32
33 #include "audio.h"
34 #include "avfilter.h"
35 #include "formats.h"
36 #include "internal.h"
37
38 typedef struct ChannelMap {
39     int input;                ///< input stream index
40     int       in_channel_idx; ///< index of in_channel in the input stream data
41     uint64_t  in_channel;     ///< layout describing the input channel
42     uint64_t out_channel;     ///< layout describing the output channel
43 } ChannelMap;
44
45 typedef struct JoinContext {
46     const AVClass *class;
47
48     int inputs;
49     char *map;
50     char    *channel_layout_str;
51     uint64_t channel_layout;
52
53     int      nb_channels;
54     ChannelMap *channels;
55
56     /**
57      * Temporary storage for input frames, until we get one on each input.
58      */
59     AVFrame **input_frames;
60
61     /**
62      *  Temporary storage for buffer references, for assembling the output frame.
63      */
64     AVBufferRef **buffers;
65 } JoinContext;
66
67 #define OFFSET(x) offsetof(JoinContext, x)
68 #define A AV_OPT_FLAG_AUDIO_PARAM
69 #define F AV_OPT_FLAG_FILTERING_PARAM
70 static const AVOption join_options[] = {
71     { "inputs",         "Number of input streams.", OFFSET(inputs),             AV_OPT_TYPE_INT,    { .i64 = 2 }, 1, INT_MAX,       A|F },
72     { "channel_layout", "Channel layout of the "
73                         "output stream.",           OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A|F },
74     { "map",            "A comma-separated list of channels maps in the format "
75                         "'input_stream.input_channel-output_channel.",
76                                                     OFFSET(map),                AV_OPT_TYPE_STRING,                 .flags = A|F },
77     { NULL },
78 };
79
80 static const AVClass join_class = {
81     .class_name = "join filter",
82     .item_name  = av_default_item_name,
83     .option     = join_options,
84     .version    = LIBAVUTIL_VERSION_INT,
85 };
86
87 static int filter_frame(AVFilterLink *link, AVFrame *frame)
88 {
89     AVFilterContext *ctx = link->dst;
90     JoinContext       *s = ctx->priv;
91     int i;
92
93     for (i = 0; i < ctx->nb_inputs; i++)
94         if (link == ctx->inputs[i])
95             break;
96     av_assert0(i < ctx->nb_inputs);
97     av_assert0(!s->input_frames[i]);
98     s->input_frames[i] = frame;
99
100     return 0;
101 }
102
103 static int parse_maps(AVFilterContext *ctx)
104 {
105     JoinContext *s = ctx->priv;
106     char *cur      = s->map;
107
108     while (cur && *cur) {
109         char *sep, *next, *p;
110         uint64_t in_channel = 0, out_channel = 0;
111         int input_idx, out_ch_idx, in_ch_idx;
112
113         next = strchr(cur, ',');
114         if (next)
115             *next++ = 0;
116
117         /* split the map into input and output parts */
118         if (!(sep = strchr(cur, '-'))) {
119             av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
120                    "map '%s'\n", cur);
121             return AVERROR(EINVAL);
122         }
123         *sep++ = 0;
124
125 #define PARSE_CHANNEL(str, var, inout)                                         \
126         if (!(var = av_get_channel_layout(str))) {                             \
127             av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
128             return AVERROR(EINVAL);                                            \
129         }                                                                      \
130         if (av_get_channel_layout_nb_channels(var) != 1) {                     \
131             av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one "   \
132                    inout " channel.\n");                                       \
133             return AVERROR(EINVAL);                                            \
134         }
135
136         /* parse output channel */
137         PARSE_CHANNEL(sep, out_channel, "output");
138         if (!(out_channel & s->channel_layout)) {
139             av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
140                    "requested channel layout.\n", sep);
141             return AVERROR(EINVAL);
142         }
143
144         out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
145                                                          out_channel);
146         if (s->channels[out_ch_idx].input >= 0) {
147             av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
148                    "'%s'.\n", sep);
149             return AVERROR(EINVAL);
150         }
151
152         /* parse input channel */
153         input_idx = strtol(cur, &cur, 0);
154         if (input_idx < 0 || input_idx >= s->inputs) {
155             av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
156                    input_idx);
157             return AVERROR(EINVAL);
158         }
159
160         if (*cur)
161             cur++;
162
163         in_ch_idx = strtol(cur, &p, 0);
164         if (p == cur) {
165             /* channel specifier is not a number,
166              * try to parse as channel name */
167             PARSE_CHANNEL(cur, in_channel, "input");
168         }
169
170         s->channels[out_ch_idx].input      = input_idx;
171         if (in_channel)
172             s->channels[out_ch_idx].in_channel = in_channel;
173         else
174             s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
175
176         cur = next;
177     }
178     return 0;
179 }
180
181 static int join_init(AVFilterContext *ctx, const char *args)
182 {
183     JoinContext *s = ctx->priv;
184     int ret, i;
185
186     s->class = &join_class;
187     av_opt_set_defaults(s);
188     if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
189         return ret;
190
191     if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
192         av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
193                s->channel_layout_str);
194         ret = AVERROR(EINVAL);
195         goto fail;
196     }
197
198     s->nb_channels  = av_get_channel_layout_nb_channels(s->channel_layout);
199     s->channels     = av_mallocz(sizeof(*s->channels) * s->nb_channels);
200     s->buffers      = av_mallocz(sizeof(*s->buffers)  * s->nb_channels);
201     s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
202     if (!s->channels || !s->buffers|| !s->input_frames) {
203         ret = AVERROR(ENOMEM);
204         goto fail;
205     }
206
207     for (i = 0; i < s->nb_channels; i++) {
208         s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
209         s->channels[i].input       = -1;
210     }
211
212     if ((ret = parse_maps(ctx)) < 0)
213         goto fail;
214
215     for (i = 0; i < s->inputs; i++) {
216         char name[32];
217         AVFilterPad pad = { 0 };
218
219         snprintf(name, sizeof(name), "input%d", i);
220         pad.type           = AVMEDIA_TYPE_AUDIO;
221         pad.name           = av_strdup(name);
222         pad.filter_frame   = filter_frame;
223
224         pad.needs_fifo = 1;
225
226         ff_insert_inpad(ctx, i, &pad);
227     }
228
229 fail:
230     av_opt_free(s);
231     return ret;
232 }
233
234 static void join_uninit(AVFilterContext *ctx)
235 {
236     JoinContext *s = ctx->priv;
237     int i;
238
239     for (i = 0; i < ctx->nb_inputs; i++) {
240         av_freep(&ctx->input_pads[i].name);
241         av_frame_free(&s->input_frames[i]);
242     }
243
244     av_freep(&s->channels);
245     av_freep(&s->buffers);
246     av_freep(&s->input_frames);
247 }
248
249 static int join_query_formats(AVFilterContext *ctx)
250 {
251     JoinContext *s = ctx->priv;
252     AVFilterChannelLayouts *layouts = NULL;
253     int i;
254
255     ff_add_channel_layout(&layouts, s->channel_layout);
256     ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
257
258     for (i = 0; i < ctx->nb_inputs; i++)
259         ff_channel_layouts_ref(ff_all_channel_layouts(),
260                                &ctx->inputs[i]->out_channel_layouts);
261
262     ff_set_common_formats    (ctx, ff_planar_sample_fmts());
263     ff_set_common_samplerates(ctx, ff_all_samplerates());
264
265     return 0;
266 }
267
268 static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
269                                uint64_t *inputs)
270 {
271     int i;
272
273     for (i = 0; i < ctx->nb_inputs; i++) {
274         AVFilterLink *link = ctx->inputs[i];
275
276         if (ch->out_channel & link->channel_layout &&
277             !(ch->out_channel & inputs[i])) {
278             ch->input      = i;
279             ch->in_channel = ch->out_channel;
280             inputs[i]     |= ch->out_channel;
281             return;
282         }
283     }
284 }
285
286 static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
287                           uint64_t *inputs)
288 {
289     int i;
290
291     for (i = 0; i < ctx->nb_inputs; i++) {
292         AVFilterLink *link = ctx->inputs[i];
293
294         if ((inputs[i] & link->channel_layout) != link->channel_layout) {
295             uint64_t unused = link->channel_layout & ~inputs[i];
296
297             ch->input      = i;
298             ch->in_channel = av_channel_layout_extract_channel(unused, 0);
299             inputs[i]     |= ch->in_channel;
300             return;
301         }
302     }
303 }
304
305 static int join_config_output(AVFilterLink *outlink)
306 {
307     AVFilterContext *ctx = outlink->src;
308     JoinContext       *s = ctx->priv;
309     uint64_t *inputs;   // nth element tracks which channels are used from nth input
310     int i, ret = 0;
311
312     /* initialize inputs to user-specified mappings */
313     if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
314         return AVERROR(ENOMEM);
315     for (i = 0; i < s->nb_channels; i++) {
316         ChannelMap *ch = &s->channels[i];
317         AVFilterLink *inlink;
318
319         if (ch->input < 0)
320             continue;
321
322         inlink = ctx->inputs[ch->input];
323
324         if (!ch->in_channel)
325             ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
326                                                                ch->in_channel_idx);
327
328         if (!(ch->in_channel & inlink->channel_layout)) {
329             av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
330                    "input stream #%d.\n", av_get_channel_name(ch->in_channel),
331                    ch->input);
332             ret = AVERROR(EINVAL);
333             goto fail;
334         }
335
336         inputs[ch->input] |= ch->in_channel;
337     }
338
339     /* guess channel maps when not explicitly defined */
340     /* first try unused matching channels */
341     for (i = 0; i < s->nb_channels; i++) {
342         ChannelMap *ch = &s->channels[i];
343
344         if (ch->input < 0)
345             guess_map_matching(ctx, ch, inputs);
346     }
347
348     /* if the above failed, try to find _any_ unused input channel */
349     for (i = 0; i < s->nb_channels; i++) {
350         ChannelMap *ch = &s->channels[i];
351
352         if (ch->input < 0)
353             guess_map_any(ctx, ch, inputs);
354
355         if (ch->input < 0) {
356             av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
357                    "output channel '%s'.\n",
358                    av_get_channel_name(ch->out_channel));
359             goto fail;
360         }
361
362         ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
363                                                                  ch->in_channel);
364     }
365
366     /* print mappings */
367     av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
368     for (i = 0; i < s->nb_channels; i++) {
369         ChannelMap *ch = &s->channels[i];
370         av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
371                av_get_channel_name(ch->in_channel),
372                av_get_channel_name(ch->out_channel));
373     }
374     av_log(ctx, AV_LOG_VERBOSE, "\n");
375
376     for (i = 0; i < ctx->nb_inputs; i++) {
377         if (!inputs[i])
378             av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
379                    "stream %d.\n", i);
380     }
381
382 fail:
383     av_freep(&inputs);
384     return ret;
385 }
386
387 static int join_request_frame(AVFilterLink *outlink)
388 {
389     AVFilterContext *ctx = outlink->src;
390     JoinContext *s       = ctx->priv;
391     AVFrame *frame;
392     int linesize   = INT_MAX;
393     int nb_samples = 0;
394     int nb_buffers = 0;
395     int i, j, ret;
396
397     /* get a frame on each input */
398     for (i = 0; i < ctx->nb_inputs; i++) {
399         AVFilterLink *inlink = ctx->inputs[i];
400
401         if (!s->input_frames[i] &&
402             (ret = ff_request_frame(inlink)) < 0)
403             return ret;
404
405         /* request the same number of samples on all inputs */
406         if (i == 0) {
407             nb_samples = s->input_frames[0]->nb_samples;
408
409             for (j = 1; !i && j < ctx->nb_inputs; j++)
410                 ctx->inputs[j]->request_samples = nb_samples;
411         }
412     }
413
414     /* setup the output frame */
415     frame = av_frame_alloc();
416     if (!frame)
417         return AVERROR(ENOMEM);
418     if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
419         frame->extended_data = av_mallocz(s->nb_channels *
420                                           sizeof(*frame->extended_data));
421         if (!frame->extended_data) {
422             ret = AVERROR(ENOMEM);
423             goto fail;
424         }
425     }
426
427     /* copy the data pointers */
428     for (i = 0; i < s->nb_channels; i++) {
429         ChannelMap *ch = &s->channels[i];
430         AVFrame *cur   = s->input_frames[ch->input];
431         AVBufferRef *buf;
432
433         frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
434         linesize = FFMIN(linesize, cur->linesize[0]);
435
436         /* add the buffer where this plan is stored to the list if it's
437          * not already there */
438         buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
439         if (!buf) {
440             ret = AVERROR(EINVAL);
441             goto fail;
442         }
443         for (j = 0; j < nb_buffers; j++)
444             if (s->buffers[j]->buffer == buf->buffer)
445                 break;
446         if (j == i)
447             s->buffers[nb_buffers++] = buf;
448     }
449
450     /* create references to the buffers we copied to output */
451     if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
452         frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
453         frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
454                                          frame->nb_extended_buf);
455         if (!frame->extended_buf) {
456             frame->nb_extended_buf = 0;
457             ret = AVERROR(ENOMEM);
458             goto fail;
459         }
460     }
461     for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
462         frame->buf[i] = av_buffer_ref(s->buffers[i]);
463         if (!frame->buf[i]) {
464             ret = AVERROR(ENOMEM);
465             goto fail;
466         }
467     }
468     for (i = 0; i < frame->nb_extended_buf; i++) {
469         frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
470                                                FF_ARRAY_ELEMS(frame->buf)]);
471         if (!frame->extended_buf[i]) {
472             ret = AVERROR(ENOMEM);
473             goto fail;
474         }
475     }
476
477     frame->nb_samples     = nb_samples;
478     frame->channel_layout = outlink->channel_layout;
479     av_frame_set_channels(frame, outlink->channels);
480     frame->format         = outlink->format;
481     frame->sample_rate    = outlink->sample_rate;
482     frame->pts            = s->input_frames[0]->pts;
483     frame->linesize[0]    = linesize;
484     if (frame->data != frame->extended_data) {
485         memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
486                FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
487     }
488
489     ret = ff_filter_frame(outlink, frame);
490
491     for (i = 0; i < ctx->nb_inputs; i++)
492         av_frame_free(&s->input_frames[i]);
493
494     return ret;
495
496 fail:
497     av_frame_free(&frame);
498     return ret;
499 }
500
501 static const AVFilterPad avfilter_af_join_outputs[] = {
502     {
503         .name          = "default",
504         .type          = AVMEDIA_TYPE_AUDIO,
505         .config_props  = join_config_output,
506         .request_frame = join_request_frame,
507     },
508     { NULL }
509 };
510
511 AVFilter avfilter_af_join = {
512     .name           = "join",
513     .description    = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
514                                            "multi-channel output"),
515     .priv_size      = sizeof(JoinContext),
516
517     .init           = join_init,
518     .uninit         = join_uninit,
519     .query_formats  = join_query_formats,
520
521     .inputs  = NULL,
522     .outputs = avfilter_af_join_outputs,
523     .priv_class = &join_class,
524 };