2 * This file is part of Libav.
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.
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.
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
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 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 },
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,
85 static int filter_frame(AVFilterLink *link, AVFrame *frame)
87 AVFilterContext *ctx = link->dst;
88 JoinContext *s = ctx->priv;
91 for (i = 0; i < ctx->nb_inputs; i++)
92 if (link == ctx->inputs[i])
94 av_assert0(i < ctx->nb_inputs);
95 av_assert0(!s->input_frames[i]);
96 s->input_frames[i] = frame;
101 static int parse_maps(AVFilterContext *ctx)
103 JoinContext *s = ctx->priv;
104 char separator = '|';
107 #if FF_API_OLD_FILTER_OPTS
108 if (cur && strchr(cur, ',')) {
109 av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "
110 "separate the mappings.\n");
115 while (cur && *cur) {
116 char *sep, *next, *p;
117 uint64_t in_channel = 0, out_channel = 0;
118 int input_idx, out_ch_idx, in_ch_idx;
120 next = strchr(cur, separator);
124 /* split the map into input and output parts */
125 if (!(sep = strchr(cur, '-'))) {
126 av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
128 return AVERROR(EINVAL);
132 #define PARSE_CHANNEL(str, var, inout) \
133 if (!(var = av_get_channel_layout(str))) { \
134 av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
135 return AVERROR(EINVAL); \
137 if (av_get_channel_layout_nb_channels(var) != 1) { \
138 av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
139 inout " channel.\n"); \
140 return AVERROR(EINVAL); \
143 /* parse output channel */
144 PARSE_CHANNEL(sep, out_channel, "output");
145 if (!(out_channel & s->channel_layout)) {
146 av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
147 "requested channel layout.\n", sep);
148 return AVERROR(EINVAL);
151 out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
153 if (s->channels[out_ch_idx].input >= 0) {
154 av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
156 return AVERROR(EINVAL);
159 /* parse input channel */
160 input_idx = strtol(cur, &cur, 0);
161 if (input_idx < 0 || input_idx >= s->inputs) {
162 av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
164 return AVERROR(EINVAL);
170 in_ch_idx = strtol(cur, &p, 0);
172 /* channel specifier is not a number,
173 * try to parse as channel name */
174 PARSE_CHANNEL(cur, in_channel, "input");
177 s->channels[out_ch_idx].input = input_idx;
179 s->channels[out_ch_idx].in_channel = in_channel;
181 s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
188 static av_cold int join_init(AVFilterContext *ctx)
190 JoinContext *s = ctx->priv;
193 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
194 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
195 s->channel_layout_str);
196 ret = AVERROR(EINVAL);
200 s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
201 s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
202 s->buffers = av_mallocz(sizeof(*s->buffers) * s->nb_channels);
203 s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
204 if (!s->channels || !s->buffers|| !s->input_frames) {
205 ret = AVERROR(ENOMEM);
209 for (i = 0; i < s->nb_channels; i++) {
210 s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
211 s->channels[i].input = -1;
214 if ((ret = parse_maps(ctx)) < 0)
217 for (i = 0; i < s->inputs; i++) {
219 AVFilterPad pad = { 0 };
221 snprintf(name, sizeof(name), "input%d", i);
222 pad.type = AVMEDIA_TYPE_AUDIO;
223 pad.name = av_strdup(name);
224 pad.filter_frame = filter_frame;
228 ff_insert_inpad(ctx, i, &pad);
236 static av_cold void join_uninit(AVFilterContext *ctx)
238 JoinContext *s = ctx->priv;
241 for (i = 0; i < ctx->nb_inputs; i++) {
242 av_freep(&ctx->input_pads[i].name);
243 av_frame_free(&s->input_frames[i]);
246 av_freep(&s->channels);
247 av_freep(&s->buffers);
248 av_freep(&s->input_frames);
251 static int join_query_formats(AVFilterContext *ctx)
253 JoinContext *s = ctx->priv;
254 AVFilterChannelLayouts *layouts = NULL;
257 ff_add_channel_layout(&layouts, s->channel_layout);
258 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
260 for (i = 0; i < ctx->nb_inputs; i++)
261 ff_channel_layouts_ref(ff_all_channel_layouts(),
262 &ctx->inputs[i]->out_channel_layouts);
264 ff_set_common_formats (ctx, ff_planar_sample_fmts());
265 ff_set_common_samplerates(ctx, ff_all_samplerates());
270 static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
275 for (i = 0; i < ctx->nb_inputs; i++) {
276 AVFilterLink *link = ctx->inputs[i];
278 if (ch->out_channel & link->channel_layout &&
279 !(ch->out_channel & inputs[i])) {
281 ch->in_channel = ch->out_channel;
282 inputs[i] |= ch->out_channel;
288 static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
293 for (i = 0; i < ctx->nb_inputs; i++) {
294 AVFilterLink *link = ctx->inputs[i];
296 if ((inputs[i] & link->channel_layout) != link->channel_layout) {
297 uint64_t unused = link->channel_layout & ~inputs[i];
300 ch->in_channel = av_channel_layout_extract_channel(unused, 0);
301 inputs[i] |= ch->in_channel;
307 static int join_config_output(AVFilterLink *outlink)
309 AVFilterContext *ctx = outlink->src;
310 JoinContext *s = ctx->priv;
311 uint64_t *inputs; // nth element tracks which channels are used from nth input
314 /* initialize inputs to user-specified mappings */
315 if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
316 return AVERROR(ENOMEM);
317 for (i = 0; i < s->nb_channels; i++) {
318 ChannelMap *ch = &s->channels[i];
319 AVFilterLink *inlink;
324 inlink = ctx->inputs[ch->input];
327 ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
330 if (!(ch->in_channel & inlink->channel_layout)) {
331 av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
332 "input stream #%d.\n", av_get_channel_name(ch->in_channel),
334 ret = AVERROR(EINVAL);
338 inputs[ch->input] |= ch->in_channel;
341 /* guess channel maps when not explicitly defined */
342 /* first try unused matching channels */
343 for (i = 0; i < s->nb_channels; i++) {
344 ChannelMap *ch = &s->channels[i];
347 guess_map_matching(ctx, ch, inputs);
350 /* if the above failed, try to find _any_ unused input channel */
351 for (i = 0; i < s->nb_channels; i++) {
352 ChannelMap *ch = &s->channels[i];
355 guess_map_any(ctx, ch, inputs);
358 av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
359 "output channel '%s'.\n",
360 av_get_channel_name(ch->out_channel));
364 ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
369 av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
370 for (i = 0; i < s->nb_channels; i++) {
371 ChannelMap *ch = &s->channels[i];
372 av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
373 av_get_channel_name(ch->in_channel),
374 av_get_channel_name(ch->out_channel));
376 av_log(ctx, AV_LOG_VERBOSE, "\n");
378 for (i = 0; i < ctx->nb_inputs; i++) {
380 av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
389 static int join_request_frame(AVFilterLink *outlink)
391 AVFilterContext *ctx = outlink->src;
392 JoinContext *s = ctx->priv;
394 int linesize = INT_MAX;
399 /* get a frame on each input */
400 for (i = 0; i < ctx->nb_inputs; i++) {
401 AVFilterLink *inlink = ctx->inputs[i];
403 if (!s->input_frames[i] &&
404 (ret = ff_request_frame(inlink)) < 0)
407 /* request the same number of samples on all inputs */
409 nb_samples = s->input_frames[0]->nb_samples;
411 for (j = 1; !i && j < ctx->nb_inputs; j++)
412 ctx->inputs[j]->request_samples = nb_samples;
416 /* setup the output frame */
417 frame = av_frame_alloc();
419 return AVERROR(ENOMEM);
420 if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
421 frame->extended_data = av_mallocz(s->nb_channels *
422 sizeof(*frame->extended_data));
423 if (!frame->extended_data) {
424 ret = AVERROR(ENOMEM);
429 /* copy the data pointers */
430 for (i = 0; i < s->nb_channels; i++) {
431 ChannelMap *ch = &s->channels[i];
432 AVFrame *cur = s->input_frames[ch->input];
435 frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
436 linesize = FFMIN(linesize, cur->linesize[0]);
438 /* add the buffer where this plan is stored to the list if it's
439 * not already there */
440 buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
442 ret = AVERROR(EINVAL);
445 for (j = 0; j < nb_buffers; j++)
446 if (s->buffers[j]->buffer == buf->buffer)
449 s->buffers[nb_buffers++] = buf;
452 /* create references to the buffers we copied to output */
453 if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
454 frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
455 frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
456 frame->nb_extended_buf);
457 if (!frame->extended_buf) {
458 frame->nb_extended_buf = 0;
459 ret = AVERROR(ENOMEM);
463 for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
464 frame->buf[i] = av_buffer_ref(s->buffers[i]);
465 if (!frame->buf[i]) {
466 ret = AVERROR(ENOMEM);
470 for (i = 0; i < frame->nb_extended_buf; i++) {
471 frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
472 FF_ARRAY_ELEMS(frame->buf)]);
473 if (!frame->extended_buf[i]) {
474 ret = AVERROR(ENOMEM);
479 frame->nb_samples = nb_samples;
480 frame->channel_layout = outlink->channel_layout;
481 frame->sample_rate = outlink->sample_rate;
482 frame->format = outlink->format;
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 ff_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,