2 * This file is part of FFmpeg.
4 * FFmpeg 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.
9 * FFmpeg 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Join multiple audio inputs as different channels in
27 #include "libavutil/avassert.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
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
44 typedef struct JoinContext {
49 char *channel_layout_str;
50 uint64_t channel_layout;
56 * Temporary storage for input frames, until we get one on each input.
58 AVFrame **input_frames;
61 * Temporary storage for buffer references, for assembling the output frame.
63 AVBufferRef **buffers;
66 #define OFFSET(x) offsetof(JoinContext, x)
67 #define A AV_OPT_FLAG_AUDIO_PARAM
68 #define F AV_OPT_FLAG_FILTERING_PARAM
69 static const AVOption join_options[] = {
70 { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A|F },
71 { "channel_layout", "Channel layout of the "
72 "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A|F },
73 { "map", "A comma-separated list of channels maps in the format "
74 "'input_stream.input_channel-output_channel.",
75 OFFSET(map), AV_OPT_TYPE_STRING, .flags = A|F },
79 AVFILTER_DEFINE_CLASS(join);
81 static int try_push_frame(AVFilterContext *ctx);
83 static int filter_frame(AVFilterLink *link, AVFrame *frame)
85 AVFilterContext *ctx = link->dst;
86 JoinContext *s = ctx->priv;
89 for (i = 0; i < ctx->nb_inputs; i++)
90 if (link == ctx->inputs[i])
92 av_assert0(i < ctx->nb_inputs);
93 av_assert0(!s->input_frames[i]);
94 s->input_frames[i] = frame;
96 /* request the same number of samples on all inputs */
97 /* FIXME that means a frame arriving asynchronously on a different input
98 will not have the requested number of samples */
100 int nb_samples = s->input_frames[0]->nb_samples;
102 for (j = 1; !i && j < ctx->nb_inputs; j++)
103 ctx->inputs[j]->request_samples = nb_samples;
106 return try_push_frame(ctx);
109 static int parse_maps(AVFilterContext *ctx)
111 JoinContext *s = ctx->priv;
112 char separator = '|';
115 #if FF_API_OLD_FILTER_OPTS
116 if (cur && strchr(cur, ',')) {
117 av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "
118 "separate the mappings.\n");
123 while (cur && *cur) {
124 char *sep, *next, *p;
125 uint64_t in_channel = 0, out_channel = 0;
126 int input_idx, out_ch_idx, in_ch_idx;
128 next = strchr(cur, separator);
132 /* split the map into input and output parts */
133 if (!(sep = strchr(cur, '-'))) {
134 av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
136 return AVERROR(EINVAL);
140 #define PARSE_CHANNEL(str, var, inout) \
141 if (!(var = av_get_channel_layout(str))) { \
142 av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
143 return AVERROR(EINVAL); \
145 if (av_get_channel_layout_nb_channels(var) != 1) { \
146 av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
147 inout " channel.\n"); \
148 return AVERROR(EINVAL); \
151 /* parse output channel */
152 PARSE_CHANNEL(sep, out_channel, "output");
153 if (!(out_channel & s->channel_layout)) {
154 av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
155 "requested channel layout.\n", sep);
156 return AVERROR(EINVAL);
159 out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
161 if (s->channels[out_ch_idx].input >= 0) {
162 av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
164 return AVERROR(EINVAL);
167 /* parse input channel */
168 input_idx = strtol(cur, &cur, 0);
169 if (input_idx < 0 || input_idx >= s->inputs) {
170 av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
172 return AVERROR(EINVAL);
178 in_ch_idx = strtol(cur, &p, 0);
180 /* channel specifier is not a number,
181 * try to parse as channel name */
182 PARSE_CHANNEL(cur, in_channel, "input");
185 s->channels[out_ch_idx].input = input_idx;
187 s->channels[out_ch_idx].in_channel = in_channel;
189 s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
196 static av_cold int join_init(AVFilterContext *ctx)
198 JoinContext *s = ctx->priv;
201 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
202 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
203 s->channel_layout_str);
204 return AVERROR(EINVAL);
207 s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
208 s->channels = av_mallocz_array(s->nb_channels, sizeof(*s->channels));
209 s->buffers = av_mallocz_array(s->nb_channels, sizeof(*s->buffers));
210 s->input_frames = av_mallocz_array(s->inputs, sizeof(*s->input_frames));
211 if (!s->channels || !s->buffers|| !s->input_frames)
212 return AVERROR(ENOMEM);
214 for (i = 0; i < s->nb_channels; i++) {
215 s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
216 s->channels[i].input = -1;
219 if ((ret = parse_maps(ctx)) < 0)
222 for (i = 0; i < s->inputs; i++) {
224 AVFilterPad pad = { 0 };
226 snprintf(name, sizeof(name), "input%d", i);
227 pad.type = AVMEDIA_TYPE_AUDIO;
228 pad.name = av_strdup(name);
230 return AVERROR(ENOMEM);
231 pad.filter_frame = filter_frame;
235 if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
244 static av_cold void join_uninit(AVFilterContext *ctx)
246 JoinContext *s = ctx->priv;
249 for (i = 0; i < ctx->nb_inputs; i++) {
250 av_freep(&ctx->input_pads[i].name);
251 av_frame_free(&s->input_frames[i]);
254 av_freep(&s->channels);
255 av_freep(&s->buffers);
256 av_freep(&s->input_frames);
259 static int join_query_formats(AVFilterContext *ctx)
261 JoinContext *s = ctx->priv;
262 AVFilterChannelLayouts *layouts = NULL;
265 if ((ret = ff_add_channel_layout(&layouts, s->channel_layout)) < 0 ||
266 (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
269 for (i = 0; i < ctx->nb_inputs; i++) {
270 layouts = ff_all_channel_layouts();
271 if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
275 if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
276 (ret = ff_set_common_samplerates(ctx, ff_all_samplerates())) < 0)
282 static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
287 for (i = 0; i < ctx->nb_inputs; i++) {
288 AVFilterLink *link = ctx->inputs[i];
290 if (ch->out_channel & link->channel_layout &&
291 !(ch->out_channel & inputs[i])) {
293 ch->in_channel = ch->out_channel;
294 inputs[i] |= ch->out_channel;
300 static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
305 for (i = 0; i < ctx->nb_inputs; i++) {
306 AVFilterLink *link = ctx->inputs[i];
308 if ((inputs[i] & link->channel_layout) != link->channel_layout) {
309 uint64_t unused = link->channel_layout & ~inputs[i];
312 ch->in_channel = av_channel_layout_extract_channel(unused, 0);
313 inputs[i] |= ch->in_channel;
319 static int join_config_output(AVFilterLink *outlink)
321 AVFilterContext *ctx = outlink->src;
322 JoinContext *s = ctx->priv;
323 uint64_t *inputs; // nth element tracks which channels are used from nth input
326 /* initialize inputs to user-specified mappings */
327 if (!(inputs = av_mallocz_array(ctx->nb_inputs, sizeof(*inputs))))
328 return AVERROR(ENOMEM);
329 for (i = 0; i < s->nb_channels; i++) {
330 ChannelMap *ch = &s->channels[i];
331 AVFilterLink *inlink;
336 inlink = ctx->inputs[ch->input];
339 ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
342 if (!(ch->in_channel & inlink->channel_layout)) {
343 av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
344 "input stream #%d.\n", av_get_channel_name(ch->in_channel),
346 ret = AVERROR(EINVAL);
350 inputs[ch->input] |= ch->in_channel;
353 /* guess channel maps when not explicitly defined */
354 /* first try unused matching channels */
355 for (i = 0; i < s->nb_channels; i++) {
356 ChannelMap *ch = &s->channels[i];
359 guess_map_matching(ctx, ch, inputs);
362 /* if the above failed, try to find _any_ unused input channel */
363 for (i = 0; i < s->nb_channels; i++) {
364 ChannelMap *ch = &s->channels[i];
367 guess_map_any(ctx, ch, inputs);
370 av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
371 "output channel '%s'.\n",
372 av_get_channel_name(ch->out_channel));
376 ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
381 av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
382 for (i = 0; i < s->nb_channels; i++) {
383 ChannelMap *ch = &s->channels[i];
384 av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
385 av_get_channel_name(ch->in_channel),
386 av_get_channel_name(ch->out_channel));
388 av_log(ctx, AV_LOG_VERBOSE, "\n");
390 for (i = 0; i < ctx->nb_inputs; i++) {
392 av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
401 static int join_request_frame(AVFilterLink *outlink)
403 AVFilterContext *ctx = outlink->src;
404 JoinContext *s = ctx->priv;
407 /* get a frame on each input */
408 for (i = 0; i < ctx->nb_inputs; i++) {
409 AVFilterLink *inlink = ctx->inputs[i];
410 if (!s->input_frames[i])
411 return ff_request_frame(inlink);
416 static int try_push_frame(AVFilterContext *ctx)
418 AVFilterLink *outlink = ctx->outputs[0];
419 JoinContext *s = ctx->priv;
421 int linesize = INT_MAX;
422 int nb_samples = INT_MAX;
426 for (i = 0; i < ctx->nb_inputs; i++) {
427 if (!s->input_frames[i])
429 nb_samples = FFMIN(nb_samples, s->input_frames[i]->nb_samples);
432 /* setup the output frame */
433 frame = av_frame_alloc();
435 return AVERROR(ENOMEM);
436 if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
437 frame->extended_data = av_mallocz_array(s->nb_channels,
438 sizeof(*frame->extended_data));
439 if (!frame->extended_data) {
440 ret = AVERROR(ENOMEM);
445 /* copy the data pointers */
446 for (i = 0; i < s->nb_channels; i++) {
447 ChannelMap *ch = &s->channels[i];
448 AVFrame *cur = s->input_frames[ch->input];
451 frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
452 linesize = FFMIN(linesize, cur->linesize[0]);
454 /* add the buffer where this plan is stored to the list if it's
455 * not already there */
456 buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
458 ret = AVERROR(EINVAL);
461 for (j = 0; j < nb_buffers; j++)
462 if (s->buffers[j]->buffer == buf->buffer)
465 s->buffers[nb_buffers++] = buf;
468 /* create references to the buffers we copied to output */
469 if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
470 frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
471 frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
472 sizeof(*frame->extended_buf));
473 if (!frame->extended_buf) {
474 frame->nb_extended_buf = 0;
475 ret = AVERROR(ENOMEM);
479 for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
480 frame->buf[i] = av_buffer_ref(s->buffers[i]);
481 if (!frame->buf[i]) {
482 ret = AVERROR(ENOMEM);
486 for (i = 0; i < frame->nb_extended_buf; i++) {
487 frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
488 FF_ARRAY_ELEMS(frame->buf)]);
489 if (!frame->extended_buf[i]) {
490 ret = AVERROR(ENOMEM);
495 frame->nb_samples = nb_samples;
496 frame->channel_layout = outlink->channel_layout;
497 frame->channels = outlink->channels;
498 frame->sample_rate = outlink->sample_rate;
499 frame->format = outlink->format;
500 frame->pts = s->input_frames[0]->pts;
501 frame->linesize[0] = linesize;
502 if (frame->data != frame->extended_data) {
503 memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
504 FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
507 ret = ff_filter_frame(outlink, frame);
509 for (i = 0; i < ctx->nb_inputs; i++)
510 av_frame_free(&s->input_frames[i]);
515 av_frame_free(&frame);
519 static const AVFilterPad avfilter_af_join_outputs[] = {
522 .type = AVMEDIA_TYPE_AUDIO,
523 .config_props = join_config_output,
524 .request_frame = join_request_frame,
529 AVFilter ff_af_join = {
531 .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
532 "multi-channel output."),
533 .priv_size = sizeof(JoinContext),
534 .priv_class = &join_class,
536 .uninit = join_uninit,
537 .query_formats = join_query_formats,
539 .outputs = avfilter_af_join_outputs,
540 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,