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 #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 static const AVClass join_class = {
81 .class_name = "join filter",
82 .item_name = av_default_item_name,
83 .option = join_options,
84 .version = LIBAVUTIL_VERSION_INT,
87 static int filter_frame(AVFilterLink *link, AVFrame *frame)
89 AVFilterContext *ctx = link->dst;
90 JoinContext *s = ctx->priv;
93 for (i = 0; i < ctx->nb_inputs; i++)
94 if (link == ctx->inputs[i])
96 av_assert0(i < ctx->nb_inputs);
97 av_assert0(!s->input_frames[i]);
98 s->input_frames[i] = frame;
103 static int parse_maps(AVFilterContext *ctx)
105 JoinContext *s = ctx->priv;
106 char separator = '|';
109 #if FF_API_OLD_FILTER_OPTS
110 if (cur && strchr(cur, ',')) {
111 av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "
112 "separate the mappings.\n");
117 while (cur && *cur) {
118 char *sep, *next, *p;
119 uint64_t in_channel = 0, out_channel = 0;
120 int input_idx, out_ch_idx, in_ch_idx;
122 next = strchr(cur, separator);
126 /* split the map into input and output parts */
127 if (!(sep = strchr(cur, '-'))) {
128 av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
130 return AVERROR(EINVAL);
134 #define PARSE_CHANNEL(str, var, inout) \
135 if (!(var = av_get_channel_layout(str))) { \
136 av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
137 return AVERROR(EINVAL); \
139 if (av_get_channel_layout_nb_channels(var) != 1) { \
140 av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
141 inout " channel.\n"); \
142 return AVERROR(EINVAL); \
145 /* parse output channel */
146 PARSE_CHANNEL(sep, out_channel, "output");
147 if (!(out_channel & s->channel_layout)) {
148 av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
149 "requested channel layout.\n", sep);
150 return AVERROR(EINVAL);
153 out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
155 if (s->channels[out_ch_idx].input >= 0) {
156 av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
158 return AVERROR(EINVAL);
161 /* parse input channel */
162 input_idx = strtol(cur, &cur, 0);
163 if (input_idx < 0 || input_idx >= s->inputs) {
164 av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
166 return AVERROR(EINVAL);
172 in_ch_idx = strtol(cur, &p, 0);
174 /* channel specifier is not a number,
175 * try to parse as channel name */
176 PARSE_CHANNEL(cur, in_channel, "input");
179 s->channels[out_ch_idx].input = input_idx;
181 s->channels[out_ch_idx].in_channel = in_channel;
183 s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
190 static int join_init(AVFilterContext *ctx)
192 JoinContext *s = ctx->priv;
195 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
196 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
197 s->channel_layout_str);
198 ret = AVERROR(EINVAL);
202 s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
203 s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
204 s->buffers = av_mallocz(sizeof(*s->buffers) * s->nb_channels);
205 s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
206 if (!s->channels || !s->buffers|| !s->input_frames) {
207 ret = AVERROR(ENOMEM);
211 for (i = 0; i < s->nb_channels; i++) {
212 s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
213 s->channels[i].input = -1;
216 if ((ret = parse_maps(ctx)) < 0)
219 for (i = 0; i < s->inputs; i++) {
221 AVFilterPad pad = { 0 };
223 snprintf(name, sizeof(name), "input%d", i);
224 pad.type = AVMEDIA_TYPE_AUDIO;
225 pad.name = av_strdup(name);
226 pad.filter_frame = filter_frame;
230 ff_insert_inpad(ctx, i, &pad);
238 static void join_uninit(AVFilterContext *ctx)
240 JoinContext *s = ctx->priv;
243 for (i = 0; i < ctx->nb_inputs; i++) {
244 av_freep(&ctx->input_pads[i].name);
245 av_frame_free(&s->input_frames[i]);
248 av_freep(&s->channels);
249 av_freep(&s->buffers);
250 av_freep(&s->input_frames);
253 static int join_query_formats(AVFilterContext *ctx)
255 JoinContext *s = ctx->priv;
256 AVFilterChannelLayouts *layouts = NULL;
259 ff_add_channel_layout(&layouts, s->channel_layout);
260 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
262 for (i = 0; i < ctx->nb_inputs; i++)
263 ff_channel_layouts_ref(ff_all_channel_layouts(),
264 &ctx->inputs[i]->out_channel_layouts);
266 ff_set_common_formats (ctx, ff_planar_sample_fmts());
267 ff_set_common_samplerates(ctx, ff_all_samplerates());
272 static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
277 for (i = 0; i < ctx->nb_inputs; i++) {
278 AVFilterLink *link = ctx->inputs[i];
280 if (ch->out_channel & link->channel_layout &&
281 !(ch->out_channel & inputs[i])) {
283 ch->in_channel = ch->out_channel;
284 inputs[i] |= ch->out_channel;
290 static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
295 for (i = 0; i < ctx->nb_inputs; i++) {
296 AVFilterLink *link = ctx->inputs[i];
298 if ((inputs[i] & link->channel_layout) != link->channel_layout) {
299 uint64_t unused = link->channel_layout & ~inputs[i];
302 ch->in_channel = av_channel_layout_extract_channel(unused, 0);
303 inputs[i] |= ch->in_channel;
309 static int join_config_output(AVFilterLink *outlink)
311 AVFilterContext *ctx = outlink->src;
312 JoinContext *s = ctx->priv;
313 uint64_t *inputs; // nth element tracks which channels are used from nth input
316 /* initialize inputs to user-specified mappings */
317 if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
318 return AVERROR(ENOMEM);
319 for (i = 0; i < s->nb_channels; i++) {
320 ChannelMap *ch = &s->channels[i];
321 AVFilterLink *inlink;
326 inlink = ctx->inputs[ch->input];
329 ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
332 if (!(ch->in_channel & inlink->channel_layout)) {
333 av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
334 "input stream #%d.\n", av_get_channel_name(ch->in_channel),
336 ret = AVERROR(EINVAL);
340 inputs[ch->input] |= ch->in_channel;
343 /* guess channel maps when not explicitly defined */
344 /* first try unused matching channels */
345 for (i = 0; i < s->nb_channels; i++) {
346 ChannelMap *ch = &s->channels[i];
349 guess_map_matching(ctx, ch, inputs);
352 /* if the above failed, try to find _any_ unused input channel */
353 for (i = 0; i < s->nb_channels; i++) {
354 ChannelMap *ch = &s->channels[i];
357 guess_map_any(ctx, ch, inputs);
360 av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
361 "output channel '%s'.\n",
362 av_get_channel_name(ch->out_channel));
366 ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
371 av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
372 for (i = 0; i < s->nb_channels; i++) {
373 ChannelMap *ch = &s->channels[i];
374 av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
375 av_get_channel_name(ch->in_channel),
376 av_get_channel_name(ch->out_channel));
378 av_log(ctx, AV_LOG_VERBOSE, "\n");
380 for (i = 0; i < ctx->nb_inputs; i++) {
382 av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
391 static int join_request_frame(AVFilterLink *outlink)
393 AVFilterContext *ctx = outlink->src;
394 JoinContext *s = ctx->priv;
396 int linesize = INT_MAX;
401 /* get a frame on each input */
402 for (i = 0; i < ctx->nb_inputs; i++) {
403 AVFilterLink *inlink = ctx->inputs[i];
405 if (!s->input_frames[i] &&
406 (ret = ff_request_frame(inlink)) < 0)
409 /* request the same number of samples on all inputs */
411 nb_samples = s->input_frames[0]->nb_samples;
413 for (j = 1; !i && j < ctx->nb_inputs; j++)
414 ctx->inputs[j]->request_samples = nb_samples;
418 /* setup the output frame */
419 frame = av_frame_alloc();
421 return AVERROR(ENOMEM);
422 if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
423 frame->extended_data = av_mallocz(s->nb_channels *
424 sizeof(*frame->extended_data));
425 if (!frame->extended_data) {
426 ret = AVERROR(ENOMEM);
431 /* copy the data pointers */
432 for (i = 0; i < s->nb_channels; i++) {
433 ChannelMap *ch = &s->channels[i];
434 AVFrame *cur = s->input_frames[ch->input];
437 frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
438 linesize = FFMIN(linesize, cur->linesize[0]);
440 /* add the buffer where this plan is stored to the list if it's
441 * not already there */
442 buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
444 ret = AVERROR(EINVAL);
447 for (j = 0; j < nb_buffers; j++)
448 if (s->buffers[j]->buffer == buf->buffer)
451 s->buffers[nb_buffers++] = buf;
454 /* create references to the buffers we copied to output */
455 if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
456 frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
457 frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
458 frame->nb_extended_buf);
459 if (!frame->extended_buf) {
460 frame->nb_extended_buf = 0;
461 ret = AVERROR(ENOMEM);
465 for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
466 frame->buf[i] = av_buffer_ref(s->buffers[i]);
467 if (!frame->buf[i]) {
468 ret = AVERROR(ENOMEM);
472 for (i = 0; i < frame->nb_extended_buf; i++) {
473 frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
474 FF_ARRAY_ELEMS(frame->buf)]);
475 if (!frame->extended_buf[i]) {
476 ret = AVERROR(ENOMEM);
481 frame->nb_samples = nb_samples;
482 frame->channel_layout = outlink->channel_layout;
483 av_frame_set_channels(frame, outlink->channels);
484 frame->format = outlink->format;
485 frame->sample_rate = outlink->sample_rate;
486 frame->pts = s->input_frames[0]->pts;
487 frame->linesize[0] = linesize;
488 if (frame->data != frame->extended_data) {
489 memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
490 FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
493 ret = ff_filter_frame(outlink, frame);
495 for (i = 0; i < ctx->nb_inputs; i++)
496 av_frame_free(&s->input_frames[i]);
501 av_frame_free(&frame);
505 static const AVFilterPad avfilter_af_join_outputs[] = {
508 .type = AVMEDIA_TYPE_AUDIO,
509 .config_props = join_config_output,
510 .request_frame = join_request_frame,
515 AVFilter avfilter_af_join = {
517 .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
518 "multi-channel output."),
519 .priv_size = sizeof(JoinContext),
520 .priv_class = &join_class,
523 .uninit = join_uninit,
524 .query_formats = join_query_formats,
527 .outputs = avfilter_af_join_outputs,
529 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,