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"
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
45 typedef struct JoinContext {
50 char *channel_layout_str;
51 uint64_t channel_layout;
57 * Temporary storage for input frames, until we get one on each input.
59 AVFrame **input_frames;
62 * Temporary storage for buffer references, for assembling the output frame.
64 AVBufferRef **buffers;
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 },
80 AVFILTER_DEFINE_CLASS(join);
82 static int parse_maps(AVFilterContext *ctx)
84 JoinContext *s = ctx->priv;
90 uint64_t in_channel = 0, out_channel = 0;
91 int input_idx, out_ch_idx, in_ch_idx;
93 next = strchr(cur, separator);
97 /* split the map into input and output parts */
98 if (!(sep = strchr(cur, '-'))) {
99 av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
101 return AVERROR(EINVAL);
105 #define PARSE_CHANNEL(str, var, inout) \
106 if (!(var = av_get_channel_layout(str))) { \
107 av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
108 return AVERROR(EINVAL); \
110 if (av_get_channel_layout_nb_channels(var) != 1) { \
111 av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
112 inout " channel.\n"); \
113 return AVERROR(EINVAL); \
116 /* parse output channel */
117 PARSE_CHANNEL(sep, out_channel, "output");
118 if (!(out_channel & s->channel_layout)) {
119 av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
120 "requested channel layout.\n", sep);
121 return AVERROR(EINVAL);
124 out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
126 if (s->channels[out_ch_idx].input >= 0) {
127 av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
129 return AVERROR(EINVAL);
132 /* parse input channel */
133 input_idx = strtol(cur, &cur, 0);
134 if (input_idx < 0 || input_idx >= s->inputs) {
135 av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
137 return AVERROR(EINVAL);
143 in_ch_idx = strtol(cur, &p, 0);
145 /* channel specifier is not a number,
146 * try to parse as channel name */
147 PARSE_CHANNEL(cur, in_channel, "input");
150 s->channels[out_ch_idx].input = input_idx;
152 s->channels[out_ch_idx].in_channel = in_channel;
154 s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
161 static av_cold int join_init(AVFilterContext *ctx)
163 JoinContext *s = ctx->priv;
166 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
167 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
168 s->channel_layout_str);
169 return AVERROR(EINVAL);
172 s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
173 s->channels = av_mallocz_array(s->nb_channels, sizeof(*s->channels));
174 s->buffers = av_mallocz_array(s->nb_channels, sizeof(*s->buffers));
175 s->input_frames = av_mallocz_array(s->inputs, sizeof(*s->input_frames));
176 if (!s->channels || !s->buffers|| !s->input_frames)
177 return AVERROR(ENOMEM);
179 for (i = 0; i < s->nb_channels; i++) {
180 s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
181 s->channels[i].input = -1;
184 if ((ret = parse_maps(ctx)) < 0)
187 for (i = 0; i < s->inputs; i++) {
189 AVFilterPad pad = { 0 };
191 snprintf(name, sizeof(name), "input%d", i);
192 pad.type = AVMEDIA_TYPE_AUDIO;
193 pad.name = av_strdup(name);
195 return AVERROR(ENOMEM);
197 if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
206 static av_cold void join_uninit(AVFilterContext *ctx)
208 JoinContext *s = ctx->priv;
211 for (i = 0; i < ctx->nb_inputs; i++) {
212 av_freep(&ctx->input_pads[i].name);
213 av_frame_free(&s->input_frames[i]);
216 av_freep(&s->channels);
217 av_freep(&s->buffers);
218 av_freep(&s->input_frames);
221 static int join_query_formats(AVFilterContext *ctx)
223 JoinContext *s = ctx->priv;
224 AVFilterChannelLayouts *layouts = NULL;
227 if ((ret = ff_add_channel_layout(&layouts, s->channel_layout)) < 0 ||
228 (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
231 for (i = 0; i < ctx->nb_inputs; i++) {
232 layouts = ff_all_channel_layouts();
233 if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
237 if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
238 (ret = ff_set_common_samplerates(ctx, ff_all_samplerates())) < 0)
244 static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
249 for (i = 0; i < ctx->nb_inputs; i++) {
250 AVFilterLink *link = ctx->inputs[i];
252 if (ch->out_channel & link->channel_layout &&
253 !(ch->out_channel & inputs[i])) {
255 ch->in_channel = ch->out_channel;
256 inputs[i] |= ch->out_channel;
262 static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
267 for (i = 0; i < ctx->nb_inputs; i++) {
268 AVFilterLink *link = ctx->inputs[i];
270 if ((inputs[i] & link->channel_layout) != link->channel_layout) {
271 uint64_t unused = link->channel_layout & ~inputs[i];
274 ch->in_channel = av_channel_layout_extract_channel(unused, 0);
275 inputs[i] |= ch->in_channel;
281 static int join_config_output(AVFilterLink *outlink)
283 AVFilterContext *ctx = outlink->src;
284 JoinContext *s = ctx->priv;
285 uint64_t *inputs; // nth element tracks which channels are used from nth input
288 /* initialize inputs to user-specified mappings */
289 if (!(inputs = av_mallocz_array(ctx->nb_inputs, sizeof(*inputs))))
290 return AVERROR(ENOMEM);
291 for (i = 0; i < s->nb_channels; i++) {
292 ChannelMap *ch = &s->channels[i];
293 AVFilterLink *inlink;
298 inlink = ctx->inputs[ch->input];
301 ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
304 if (!(ch->in_channel & inlink->channel_layout)) {
305 av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
306 "input stream #%d.\n", av_get_channel_name(ch->in_channel),
308 ret = AVERROR(EINVAL);
312 inputs[ch->input] |= ch->in_channel;
315 /* guess channel maps when not explicitly defined */
316 /* first try unused matching channels */
317 for (i = 0; i < s->nb_channels; i++) {
318 ChannelMap *ch = &s->channels[i];
321 guess_map_matching(ctx, ch, inputs);
324 /* if the above failed, try to find _any_ unused input channel */
325 for (i = 0; i < s->nb_channels; i++) {
326 ChannelMap *ch = &s->channels[i];
329 guess_map_any(ctx, ch, inputs);
332 av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
333 "output channel '%s'.\n",
334 av_get_channel_name(ch->out_channel));
338 ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
343 av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
344 for (i = 0; i < s->nb_channels; i++) {
345 ChannelMap *ch = &s->channels[i];
346 av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
347 av_get_channel_name(ch->in_channel),
348 av_get_channel_name(ch->out_channel));
350 av_log(ctx, AV_LOG_VERBOSE, "\n");
352 for (i = 0; i < ctx->nb_inputs; i++) {
354 av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
363 static int try_push_frame(AVFilterContext *ctx)
365 AVFilterLink *outlink = ctx->outputs[0];
366 JoinContext *s = ctx->priv;
368 int linesize = INT_MAX;
369 int nb_samples = INT_MAX;
373 for (i = 0; i < ctx->nb_inputs; i++) {
374 if (!s->input_frames[i])
376 nb_samples = FFMIN(nb_samples, s->input_frames[i]->nb_samples);
381 /* setup the output frame */
382 frame = av_frame_alloc();
384 return AVERROR(ENOMEM);
385 if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
386 frame->extended_data = av_mallocz_array(s->nb_channels,
387 sizeof(*frame->extended_data));
388 if (!frame->extended_data) {
389 ret = AVERROR(ENOMEM);
394 /* copy the data pointers */
395 for (i = 0; i < s->nb_channels; i++) {
396 ChannelMap *ch = &s->channels[i];
397 AVFrame *cur = s->input_frames[ch->input];
400 frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
401 linesize = FFMIN(linesize, cur->linesize[0]);
403 /* add the buffer where this plan is stored to the list if it's
404 * not already there */
405 buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
407 ret = AVERROR(EINVAL);
410 for (j = 0; j < nb_buffers; j++)
411 if (s->buffers[j]->buffer == buf->buffer)
414 s->buffers[nb_buffers++] = buf;
417 /* create references to the buffers we copied to output */
418 if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
419 frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
420 frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
421 sizeof(*frame->extended_buf));
422 if (!frame->extended_buf) {
423 frame->nb_extended_buf = 0;
424 ret = AVERROR(ENOMEM);
428 for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
429 frame->buf[i] = av_buffer_ref(s->buffers[i]);
430 if (!frame->buf[i]) {
431 ret = AVERROR(ENOMEM);
435 for (i = 0; i < frame->nb_extended_buf; i++) {
436 frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
437 FF_ARRAY_ELEMS(frame->buf)]);
438 if (!frame->extended_buf[i]) {
439 ret = AVERROR(ENOMEM);
444 frame->nb_samples = nb_samples;
445 frame->channel_layout = outlink->channel_layout;
446 frame->channels = outlink->channels;
447 frame->sample_rate = outlink->sample_rate;
448 frame->format = outlink->format;
449 frame->pts = s->input_frames[0]->pts;
450 frame->linesize[0] = linesize;
451 if (frame->data != frame->extended_data) {
452 memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
453 FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
456 ret = ff_filter_frame(outlink, frame);
458 for (i = 0; i < ctx->nb_inputs; i++)
459 av_frame_free(&s->input_frames[i]);
464 av_frame_free(&frame);
468 static int activate(AVFilterContext *ctx)
470 JoinContext *s = ctx->priv;
475 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
477 if (!s->input_frames[0]) {
478 ret = ff_inlink_consume_frame(ctx->inputs[0], &s->input_frames[0]);
481 } else if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
482 ff_outlink_set_status(ctx->outputs[0], status, pts);
485 if (ff_outlink_frame_wanted(ctx->outputs[0]) && !s->input_frames[0]) {
486 ff_inlink_request_frame(ctx->inputs[0]);
490 if (!s->input_frames[0]) {
495 nb_samples = s->input_frames[0]->nb_samples;
497 for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) {
498 if (s->input_frames[i])
501 if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
502 ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->input_frames[i]);
505 } else if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
506 ff_outlink_set_status(ctx->outputs[0], status, pts);
510 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
511 ff_inlink_request_frame(ctx->inputs[i]);
517 return try_push_frame(ctx);
520 static const AVFilterPad avfilter_af_join_outputs[] = {
523 .type = AVMEDIA_TYPE_AUDIO,
524 .config_props = join_config_output,
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 .activate = activate,
538 .query_formats = join_query_formats,
540 .outputs = avfilter_af_join_outputs,
541 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,