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