3 * This file is part of Libav.
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.
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.
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
24 * Join multiple audio inputs as different channels in
28 #include "libavutil/avassert.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #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 static const AVOption join_options[] = {
70 { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A },
71 { "channel_layout", "Channel layout of the "
72 "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A },
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 },
79 static const AVClass join_class = {
80 .class_name = "join filter",
81 .item_name = av_default_item_name,
82 .option = join_options,
83 .version = LIBAVUTIL_VERSION_INT,
86 static int filter_frame(AVFilterLink *link, AVFrame *frame)
88 AVFilterContext *ctx = link->dst;
89 JoinContext *s = ctx->priv;
92 for (i = 0; i < ctx->nb_inputs; i++)
93 if (link == ctx->inputs[i])
95 av_assert0(i < ctx->nb_inputs);
96 av_assert0(!s->input_frames[i]);
97 s->input_frames[i] = frame;
102 static int parse_maps(AVFilterContext *ctx)
104 JoinContext *s = ctx->priv;
105 char separator = '|';
108 #if FF_API_OLD_FILTER_OPTS
109 if (cur && strchr(cur, ',')) {
110 av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "
111 "separate the mappings.\n");
116 while (cur && *cur) {
117 char *sep, *next, *p;
118 uint64_t in_channel = 0, out_channel = 0;
119 int input_idx, out_ch_idx, in_ch_idx;
121 next = strchr(cur, separator);
125 /* split the map into input and output parts */
126 if (!(sep = strchr(cur, '-'))) {
127 av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
129 return AVERROR(EINVAL);
133 #define PARSE_CHANNEL(str, var, inout) \
134 if (!(var = av_get_channel_layout(str))) { \
135 av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
136 return AVERROR(EINVAL); \
138 if (av_get_channel_layout_nb_channels(var) != 1) { \
139 av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
140 inout " channel.\n"); \
141 return AVERROR(EINVAL); \
144 /* parse output channel */
145 PARSE_CHANNEL(sep, out_channel, "output");
146 if (!(out_channel & s->channel_layout)) {
147 av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
148 "requested channel layout.\n", sep);
149 return AVERROR(EINVAL);
152 out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
154 if (s->channels[out_ch_idx].input >= 0) {
155 av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
157 return AVERROR(EINVAL);
160 /* parse input channel */
161 input_idx = strtol(cur, &cur, 0);
162 if (input_idx < 0 || input_idx >= s->inputs) {
163 av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
165 return AVERROR(EINVAL);
171 in_ch_idx = strtol(cur, &p, 0);
173 /* channel specifier is not a number,
174 * try to parse as channel name */
175 PARSE_CHANNEL(cur, in_channel, "input");
178 s->channels[out_ch_idx].input = input_idx;
180 s->channels[out_ch_idx].in_channel = in_channel;
182 s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
189 static av_cold int join_init(AVFilterContext *ctx)
191 JoinContext *s = ctx->priv;
194 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
195 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
196 s->channel_layout_str);
197 ret = AVERROR(EINVAL);
201 s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
202 s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
203 s->buffers = av_mallocz(sizeof(*s->buffers) * s->nb_channels);
204 s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
205 if (!s->channels || !s->buffers|| !s->input_frames) {
206 ret = AVERROR(ENOMEM);
210 for (i = 0; i < s->nb_channels; i++) {
211 s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
212 s->channels[i].input = -1;
215 if ((ret = parse_maps(ctx)) < 0)
218 for (i = 0; i < s->inputs; i++) {
220 AVFilterPad pad = { 0 };
222 snprintf(name, sizeof(name), "input%d", i);
223 pad.type = AVMEDIA_TYPE_AUDIO;
224 pad.name = av_strdup(name);
225 pad.filter_frame = filter_frame;
229 ff_insert_inpad(ctx, i, &pad);
237 static av_cold void join_uninit(AVFilterContext *ctx)
239 JoinContext *s = ctx->priv;
242 for (i = 0; i < ctx->nb_inputs; i++) {
243 av_freep(&ctx->input_pads[i].name);
244 av_frame_free(&s->input_frames[i]);
247 av_freep(&s->channels);
248 av_freep(&s->buffers);
249 av_freep(&s->input_frames);
252 static int join_query_formats(AVFilterContext *ctx)
254 JoinContext *s = ctx->priv;
255 AVFilterChannelLayouts *layouts = NULL;
258 ff_add_channel_layout(&layouts, s->channel_layout);
259 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
261 for (i = 0; i < ctx->nb_inputs; i++)
262 ff_channel_layouts_ref(ff_all_channel_layouts(),
263 &ctx->inputs[i]->out_channel_layouts);
265 ff_set_common_formats (ctx, ff_planar_sample_fmts());
266 ff_set_common_samplerates(ctx, ff_all_samplerates());
271 static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
276 for (i = 0; i < ctx->nb_inputs; i++) {
277 AVFilterLink *link = ctx->inputs[i];
279 if (ch->out_channel & link->channel_layout &&
280 !(ch->out_channel & inputs[i])) {
282 ch->in_channel = ch->out_channel;
283 inputs[i] |= ch->out_channel;
289 static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
294 for (i = 0; i < ctx->nb_inputs; i++) {
295 AVFilterLink *link = ctx->inputs[i];
297 if ((inputs[i] & link->channel_layout) != link->channel_layout) {
298 uint64_t unused = link->channel_layout & ~inputs[i];
301 ch->in_channel = av_channel_layout_extract_channel(unused, 0);
302 inputs[i] |= ch->in_channel;
308 static int join_config_output(AVFilterLink *outlink)
310 AVFilterContext *ctx = outlink->src;
311 JoinContext *s = ctx->priv;
312 uint64_t *inputs; // nth element tracks which channels are used from nth input
315 /* initialize inputs to user-specified mappings */
316 if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
317 return AVERROR(ENOMEM);
318 for (i = 0; i < s->nb_channels; i++) {
319 ChannelMap *ch = &s->channels[i];
320 AVFilterLink *inlink;
325 inlink = ctx->inputs[ch->input];
328 ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
331 if (!(ch->in_channel & inlink->channel_layout)) {
332 av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
333 "input stream #%d.\n", av_get_channel_name(ch->in_channel),
335 ret = AVERROR(EINVAL);
339 inputs[ch->input] |= ch->in_channel;
342 /* guess channel maps when not explicitly defined */
343 /* first try unused matching channels */
344 for (i = 0; i < s->nb_channels; i++) {
345 ChannelMap *ch = &s->channels[i];
348 guess_map_matching(ctx, ch, inputs);
351 /* if the above failed, try to find _any_ unused input channel */
352 for (i = 0; i < s->nb_channels; i++) {
353 ChannelMap *ch = &s->channels[i];
356 guess_map_any(ctx, ch, inputs);
359 av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
360 "output channel '%s'.\n",
361 av_get_channel_name(ch->out_channel));
365 ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
370 av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
371 for (i = 0; i < s->nb_channels; i++) {
372 ChannelMap *ch = &s->channels[i];
373 av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
374 av_get_channel_name(ch->in_channel),
375 av_get_channel_name(ch->out_channel));
377 av_log(ctx, AV_LOG_VERBOSE, "\n");
379 for (i = 0; i < ctx->nb_inputs; i++) {
381 av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
390 static int join_request_frame(AVFilterLink *outlink)
392 AVFilterContext *ctx = outlink->src;
393 JoinContext *s = ctx->priv;
395 int linesize = INT_MAX;
400 /* get a frame on each input */
401 for (i = 0; i < ctx->nb_inputs; i++) {
402 AVFilterLink *inlink = ctx->inputs[i];
404 if (!s->input_frames[i] &&
405 (ret = ff_request_frame(inlink)) < 0)
408 /* request the same number of samples on all inputs */
410 nb_samples = s->input_frames[0]->nb_samples;
412 for (j = 1; !i && j < ctx->nb_inputs; j++)
413 ctx->inputs[j]->request_samples = nb_samples;
417 /* setup the output frame */
418 frame = av_frame_alloc();
420 return AVERROR(ENOMEM);
421 if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
422 frame->extended_data = av_mallocz(s->nb_channels *
423 sizeof(*frame->extended_data));
424 if (!frame->extended_data) {
425 ret = AVERROR(ENOMEM);
430 /* copy the data pointers */
431 for (i = 0; i < s->nb_channels; i++) {
432 ChannelMap *ch = &s->channels[i];
433 AVFrame *cur = s->input_frames[ch->input];
436 frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
437 linesize = FFMIN(linesize, cur->linesize[0]);
439 /* add the buffer where this plan is stored to the list if it's
440 * not already there */
441 buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
443 ret = AVERROR(EINVAL);
446 for (j = 0; j < nb_buffers; j++)
447 if (s->buffers[j]->buffer == buf->buffer)
450 s->buffers[nb_buffers++] = buf;
453 /* create references to the buffers we copied to output */
454 if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
455 frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
456 frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
457 frame->nb_extended_buf);
458 if (!frame->extended_buf) {
459 frame->nb_extended_buf = 0;
460 ret = AVERROR(ENOMEM);
464 for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
465 frame->buf[i] = av_buffer_ref(s->buffers[i]);
466 if (!frame->buf[i]) {
467 ret = AVERROR(ENOMEM);
471 for (i = 0; i < frame->nb_extended_buf; i++) {
472 frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
473 FF_ARRAY_ELEMS(frame->buf)]);
474 if (!frame->extended_buf[i]) {
475 ret = AVERROR(ENOMEM);
480 frame->nb_samples = nb_samples;
481 frame->channel_layout = outlink->channel_layout;
482 frame->sample_rate = outlink->sample_rate;
483 frame->pts = s->input_frames[0]->pts;
484 frame->linesize[0] = linesize;
485 if (frame->data != frame->extended_data) {
486 memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
487 FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
490 ret = ff_filter_frame(outlink, frame);
492 for (i = 0; i < ctx->nb_inputs; i++)
493 av_frame_free(&s->input_frames[i]);
498 av_frame_free(&frame);
502 static const AVFilterPad avfilter_af_join_outputs[] = {
505 .type = AVMEDIA_TYPE_AUDIO,
506 .config_props = join_config_output,
507 .request_frame = join_request_frame,
512 AVFilter avfilter_af_join = {
514 .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
515 "multi-channel output"),
516 .priv_size = sizeof(JoinContext),
517 .priv_class = &join_class,
520 .uninit = join_uninit,
521 .query_formats = join_query_formats,
524 .outputs = avfilter_af_join_outputs,
526 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,