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/avstring.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #include "libavutil/opt.h"
39 typedef struct ChannelMap {
40 int input; ///< input stream index
41 int in_channel_idx; ///< index of in_channel in the input stream data
42 uint64_t in_channel; ///< layout describing the input channel
43 uint64_t out_channel; ///< layout describing the output channel
46 typedef struct JoinContext {
51 char *channel_layout_str;
52 uint64_t channel_layout;
58 * Temporary storage for input frames, until we get one on each input.
60 AVFrame **input_frames;
63 * Temporary storage for buffer references, for assembling the output frame.
65 AVBufferRef **buffers;
68 #define OFFSET(x) offsetof(JoinContext, x)
69 #define A AV_OPT_FLAG_AUDIO_PARAM
70 #define F AV_OPT_FLAG_FILTERING_PARAM
71 static const AVOption join_options[] = {
72 { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A|F },
73 { "channel_layout", "Channel layout of the "
74 "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A|F },
75 { "map", "A comma-separated list of channels maps in the format "
76 "'input_stream.input_channel-output_channel.",
77 OFFSET(map), AV_OPT_TYPE_STRING, .flags = A|F },
81 AVFILTER_DEFINE_CLASS(join);
83 static int parse_maps(AVFilterContext *ctx)
85 JoinContext *s = ctx->priv;
91 uint64_t in_channel = 0, out_channel = 0;
92 int input_idx, out_ch_idx, in_ch_idx;
94 next = strchr(cur, separator);
98 /* split the map into input and output parts */
99 if (!(sep = strchr(cur, '-'))) {
100 av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
102 return AVERROR(EINVAL);
106 #define PARSE_CHANNEL(str, var, inout) \
107 if (!(var = av_get_channel_layout(str))) { \
108 av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
109 return AVERROR(EINVAL); \
111 if (av_get_channel_layout_nb_channels(var) != 1) { \
112 av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
113 inout " channel.\n"); \
114 return AVERROR(EINVAL); \
117 /* parse output channel */
118 PARSE_CHANNEL(sep, out_channel, "output");
119 if (!(out_channel & s->channel_layout)) {
120 av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
121 "requested channel layout.\n", sep);
122 return AVERROR(EINVAL);
125 out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
127 if (s->channels[out_ch_idx].input >= 0) {
128 av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
130 return AVERROR(EINVAL);
133 /* parse input channel */
134 input_idx = strtol(cur, &cur, 0);
135 if (input_idx < 0 || input_idx >= s->inputs) {
136 av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
138 return AVERROR(EINVAL);
144 in_ch_idx = strtol(cur, &p, 0);
146 /* channel specifier is not a number,
147 * try to parse as channel name */
148 PARSE_CHANNEL(cur, in_channel, "input");
151 s->channels[out_ch_idx].input = input_idx;
153 s->channels[out_ch_idx].in_channel = in_channel;
155 s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
162 static av_cold int join_init(AVFilterContext *ctx)
164 JoinContext *s = ctx->priv;
167 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
168 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
169 s->channel_layout_str);
170 return AVERROR(EINVAL);
173 s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
174 s->channels = av_mallocz_array(s->nb_channels, sizeof(*s->channels));
175 s->buffers = av_mallocz_array(s->nb_channels, sizeof(*s->buffers));
176 s->input_frames = av_mallocz_array(s->inputs, sizeof(*s->input_frames));
177 if (!s->channels || !s->buffers|| !s->input_frames)
178 return AVERROR(ENOMEM);
180 for (i = 0; i < s->nb_channels; i++) {
181 s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
182 s->channels[i].input = -1;
185 if ((ret = parse_maps(ctx)) < 0)
188 for (i = 0; i < s->inputs; i++) {
189 AVFilterPad pad = { 0 };
191 pad.type = AVMEDIA_TYPE_AUDIO;
192 pad.name = av_asprintf("input%d", i);
194 return AVERROR(ENOMEM);
196 if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
205 static av_cold void join_uninit(AVFilterContext *ctx)
207 JoinContext *s = ctx->priv;
210 for (i = 0; i < ctx->nb_inputs; i++) {
211 av_freep(&ctx->input_pads[i].name);
212 av_frame_free(&s->input_frames[i]);
215 av_freep(&s->channels);
216 av_freep(&s->buffers);
217 av_freep(&s->input_frames);
220 static int join_query_formats(AVFilterContext *ctx)
222 JoinContext *s = ctx->priv;
223 AVFilterChannelLayouts *layouts = NULL;
226 if ((ret = ff_add_channel_layout(&layouts, s->channel_layout)) < 0 ||
227 (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
230 for (i = 0; i < ctx->nb_inputs; i++) {
231 layouts = ff_all_channel_layouts();
232 if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
236 if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
237 (ret = ff_set_common_samplerates(ctx, ff_all_samplerates())) < 0)
243 static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
248 for (i = 0; i < ctx->nb_inputs; i++) {
249 AVFilterLink *link = ctx->inputs[i];
251 if (ch->out_channel & link->channel_layout &&
252 !(ch->out_channel & inputs[i])) {
254 ch->in_channel = ch->out_channel;
255 inputs[i] |= ch->out_channel;
261 static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
266 for (i = 0; i < ctx->nb_inputs; i++) {
267 AVFilterLink *link = ctx->inputs[i];
269 if ((inputs[i] & link->channel_layout) != link->channel_layout) {
270 uint64_t unused = link->channel_layout & ~inputs[i];
273 ch->in_channel = av_channel_layout_extract_channel(unused, 0);
274 inputs[i] |= ch->in_channel;
280 static int join_config_output(AVFilterLink *outlink)
282 AVFilterContext *ctx = outlink->src;
283 JoinContext *s = ctx->priv;
284 uint64_t *inputs; // nth element tracks which channels are used from nth input
287 /* initialize inputs to user-specified mappings */
288 if (!(inputs = av_mallocz_array(ctx->nb_inputs, sizeof(*inputs))))
289 return AVERROR(ENOMEM);
290 for (i = 0; i < s->nb_channels; i++) {
291 ChannelMap *ch = &s->channels[i];
292 AVFilterLink *inlink;
297 inlink = ctx->inputs[ch->input];
300 ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
303 if (!(ch->in_channel & inlink->channel_layout)) {
304 av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
305 "input stream #%d.\n", av_get_channel_name(ch->in_channel),
307 ret = AVERROR(EINVAL);
311 inputs[ch->input] |= ch->in_channel;
314 /* guess channel maps when not explicitly defined */
315 /* first try unused matching channels */
316 for (i = 0; i < s->nb_channels; i++) {
317 ChannelMap *ch = &s->channels[i];
320 guess_map_matching(ctx, ch, inputs);
323 /* if the above failed, try to find _any_ unused input channel */
324 for (i = 0; i < s->nb_channels; i++) {
325 ChannelMap *ch = &s->channels[i];
328 guess_map_any(ctx, ch, inputs);
331 av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
332 "output channel '%s'.\n",
333 av_get_channel_name(ch->out_channel));
337 ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
342 av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
343 for (i = 0; i < s->nb_channels; i++) {
344 ChannelMap *ch = &s->channels[i];
345 av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
346 av_get_channel_name(ch->in_channel),
347 av_get_channel_name(ch->out_channel));
349 av_log(ctx, AV_LOG_VERBOSE, "\n");
351 for (i = 0; i < ctx->nb_inputs; i++) {
353 av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
362 static int try_push_frame(AVFilterContext *ctx)
364 AVFilterLink *outlink = ctx->outputs[0];
365 JoinContext *s = ctx->priv;
367 int linesize = INT_MAX;
368 int nb_samples = INT_MAX;
372 for (i = 0; i < ctx->nb_inputs; i++) {
373 if (!s->input_frames[i])
375 nb_samples = FFMIN(nb_samples, s->input_frames[i]->nb_samples);
380 /* setup the output frame */
381 frame = av_frame_alloc();
383 return AVERROR(ENOMEM);
384 if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
385 frame->extended_data = av_mallocz_array(s->nb_channels,
386 sizeof(*frame->extended_data));
387 if (!frame->extended_data) {
388 ret = AVERROR(ENOMEM);
393 /* copy the data pointers */
394 for (i = 0; i < s->nb_channels; i++) {
395 ChannelMap *ch = &s->channels[i];
396 AVFrame *cur = s->input_frames[ch->input];
399 frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
400 linesize = FFMIN(linesize, cur->linesize[0]);
402 /* add the buffer where this plan is stored to the list if it's
403 * not already there */
404 buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
406 ret = AVERROR(EINVAL);
409 for (j = 0; j < nb_buffers; j++)
410 if (s->buffers[j]->buffer == buf->buffer)
413 s->buffers[nb_buffers++] = buf;
416 /* create references to the buffers we copied to output */
417 if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
418 frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
419 frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
420 sizeof(*frame->extended_buf));
421 if (!frame->extended_buf) {
422 frame->nb_extended_buf = 0;
423 ret = AVERROR(ENOMEM);
427 for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
428 frame->buf[i] = av_buffer_ref(s->buffers[i]);
429 if (!frame->buf[i]) {
430 ret = AVERROR(ENOMEM);
434 for (i = 0; i < frame->nb_extended_buf; i++) {
435 frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
436 FF_ARRAY_ELEMS(frame->buf)]);
437 if (!frame->extended_buf[i]) {
438 ret = AVERROR(ENOMEM);
443 frame->nb_samples = nb_samples;
444 frame->channel_layout = outlink->channel_layout;
445 frame->channels = outlink->channels;
446 frame->sample_rate = outlink->sample_rate;
447 frame->format = outlink->format;
448 frame->pts = s->input_frames[0]->pts;
449 frame->linesize[0] = linesize;
450 if (frame->data != frame->extended_data) {
451 memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
452 FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
455 ret = ff_filter_frame(outlink, frame);
457 for (i = 0; i < ctx->nb_inputs; i++)
458 av_frame_free(&s->input_frames[i]);
463 av_frame_free(&frame);
467 static int activate(AVFilterContext *ctx)
469 JoinContext *s = ctx->priv;
474 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
476 if (!s->input_frames[0]) {
477 ret = ff_inlink_consume_frame(ctx->inputs[0], &s->input_frames[0]);
480 } else if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
481 ff_outlink_set_status(ctx->outputs[0], status, pts);
484 if (ff_outlink_frame_wanted(ctx->outputs[0]) && !s->input_frames[0]) {
485 ff_inlink_request_frame(ctx->inputs[0]);
489 if (!s->input_frames[0]) {
494 nb_samples = s->input_frames[0]->nb_samples;
496 for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) {
497 if (s->input_frames[i])
500 if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
501 ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->input_frames[i]);
504 } else if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
505 ff_outlink_set_status(ctx->outputs[0], status, pts);
509 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
510 ff_inlink_request_frame(ctx->inputs[i]);
516 return try_push_frame(ctx);
519 static const AVFilterPad avfilter_af_join_outputs[] = {
522 .type = AVMEDIA_TYPE_AUDIO,
523 .config_props = join_config_output,
528 AVFilter ff_af_join = {
530 .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
531 "multi-channel output."),
532 .priv_size = sizeof(JoinContext),
533 .priv_class = &join_class,
535 .uninit = join_uninit,
536 .activate = activate,
537 .query_formats = join_query_formats,
539 .outputs = avfilter_af_join_outputs,
540 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,