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/audioconvert.h"
29 #include "libavutil/avassert.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 AVFilterBufferRef **input_frames;
62 * Temporary storage for data pointers, for assembling the output buffer.
68 * To avoid copying the data from input buffers, this filter creates
69 * a custom output buffer that stores references to all inputs and
70 * unrefs them on free.
72 typedef struct JoinBufferPriv {
73 AVFilterBufferRef **in_buffers;
77 #define OFFSET(x) offsetof(JoinContext, x)
78 #define A AV_OPT_FLAG_AUDIO_PARAM
79 static const AVOption join_options[] = {
80 { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A },
81 { "channel_layout", "Channel layout of the "
82 "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A },
83 { "map", "A comma-separated list of channels maps in the format "
84 "'input_stream.input_channel-output_channel.",
85 OFFSET(map), AV_OPT_TYPE_STRING, .flags = A },
89 static const AVClass join_class = {
90 .class_name = "join filter",
91 .item_name = av_default_item_name,
92 .option = join_options,
93 .version = LIBAVUTIL_VERSION_INT,
96 static int filter_samples(AVFilterLink *link, AVFilterBufferRef *buf)
98 AVFilterContext *ctx = link->dst;
99 JoinContext *s = ctx->priv;
102 for (i = 0; i < ctx->nb_inputs; i++)
103 if (link == ctx->inputs[i])
105 av_assert0(i < ctx->nb_inputs);
106 av_assert0(!s->input_frames[i]);
107 s->input_frames[i] = buf;
112 static int parse_maps(AVFilterContext *ctx)
114 JoinContext *s = ctx->priv;
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, ',');
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, const char *args)
192 JoinContext *s = ctx->priv;
195 s->class = &join_class;
196 av_opt_set_defaults(s);
197 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
198 av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
202 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
203 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
204 s->channel_layout_str);
205 ret = AVERROR(EINVAL);
209 s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
210 s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
211 s->data = av_mallocz(sizeof(*s->data) * s->nb_channels);
212 s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
213 if (!s->channels || !s->data || !s->input_frames) {
214 ret = AVERROR(ENOMEM);
218 for (i = 0; i < s->nb_channels; i++) {
219 s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
220 s->channels[i].input = -1;
223 if ((ret = parse_maps(ctx)) < 0)
226 for (i = 0; i < s->inputs; i++) {
228 AVFilterPad pad = { 0 };
230 snprintf(name, sizeof(name), "input%d", i);
231 pad.type = AVMEDIA_TYPE_AUDIO;
232 pad.name = av_strdup(name);
233 pad.filter_samples = filter_samples;
237 ff_insert_inpad(ctx, i, &pad);
245 static void join_uninit(AVFilterContext *ctx)
247 JoinContext *s = ctx->priv;
250 for (i = 0; i < ctx->nb_inputs; i++) {
251 av_freep(&ctx->input_pads[i].name);
252 avfilter_unref_bufferp(&s->input_frames[i]);
255 av_freep(&s->channels);
257 av_freep(&s->input_frames);
260 static int join_query_formats(AVFilterContext *ctx)
262 JoinContext *s = ctx->priv;
263 AVFilterChannelLayouts *layouts = NULL;
266 ff_add_channel_layout(&layouts, s->channel_layout);
267 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
269 for (i = 0; i < ctx->nb_inputs; i++)
270 ff_channel_layouts_ref(ff_all_channel_layouts(),
271 &ctx->inputs[i]->out_channel_layouts);
273 ff_set_common_formats (ctx, ff_planar_sample_fmts());
274 ff_set_common_samplerates(ctx, ff_all_samplerates());
279 static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
284 for (i = 0; i < ctx->nb_inputs; i++) {
285 AVFilterLink *link = ctx->inputs[i];
287 if (ch->out_channel & link->channel_layout &&
288 !(ch->out_channel & inputs[i])) {
290 ch->in_channel = ch->out_channel;
291 inputs[i] |= ch->out_channel;
297 static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
302 for (i = 0; i < ctx->nb_inputs; i++) {
303 AVFilterLink *link = ctx->inputs[i];
305 if ((inputs[i] & link->channel_layout) != link->channel_layout) {
306 uint64_t unused = link->channel_layout & ~inputs[i];
309 ch->in_channel = av_channel_layout_extract_channel(unused, 0);
310 inputs[i] |= ch->in_channel;
316 static int join_config_output(AVFilterLink *outlink)
318 AVFilterContext *ctx = outlink->src;
319 JoinContext *s = ctx->priv;
320 uint64_t *inputs; // nth element tracks which channels are used from nth input
323 /* initialize inputs to user-specified mappings */
324 if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
325 return AVERROR(ENOMEM);
326 for (i = 0; i < s->nb_channels; i++) {
327 ChannelMap *ch = &s->channels[i];
328 AVFilterLink *inlink;
333 inlink = ctx->inputs[ch->input];
336 ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
339 if (!(ch->in_channel & inlink->channel_layout)) {
340 av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
341 "input stream #%d.\n", av_get_channel_name(ch->in_channel),
343 ret = AVERROR(EINVAL);
347 inputs[ch->input] |= ch->in_channel;
350 /* guess channel maps when not explicitly defined */
351 /* first try unused matching channels */
352 for (i = 0; i < s->nb_channels; i++) {
353 ChannelMap *ch = &s->channels[i];
356 guess_map_matching(ctx, ch, inputs);
359 /* if the above failed, try to find _any_ unused input channel */
360 for (i = 0; i < s->nb_channels; i++) {
361 ChannelMap *ch = &s->channels[i];
364 guess_map_any(ctx, ch, inputs);
367 av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
368 "output channel '%s'.\n",
369 av_get_channel_name(ch->out_channel));
373 ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
378 av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
379 for (i = 0; i < s->nb_channels; i++) {
380 ChannelMap *ch = &s->channels[i];
381 av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
382 av_get_channel_name(ch->in_channel),
383 av_get_channel_name(ch->out_channel));
385 av_log(ctx, AV_LOG_VERBOSE, "\n");
387 for (i = 0; i < ctx->nb_inputs; i++) {
389 av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
398 static void join_free_buffer(AVFilterBuffer *buf)
400 JoinBufferPriv *priv = buf->priv;
405 for (i = 0; i < priv->nb_in_buffers; i++)
406 avfilter_unref_bufferp(&priv->in_buffers[i]);
408 av_freep(&priv->in_buffers);
409 av_freep(&buf->priv);
412 if (buf->extended_data != buf->data)
413 av_freep(&buf->extended_data);
417 static int join_request_frame(AVFilterLink *outlink)
419 AVFilterContext *ctx = outlink->src;
420 JoinContext *s = ctx->priv;
421 AVFilterBufferRef *buf;
422 JoinBufferPriv *priv;
423 int linesize = INT_MAX;
428 /* get a frame on each input */
429 for (i = 0; i < ctx->nb_inputs; i++) {
430 AVFilterLink *inlink = ctx->inputs[i];
432 if (!s->input_frames[i] &&
433 (ret = ff_request_frame(inlink)) < 0)
436 /* request the same number of samples on all inputs */
438 nb_samples = s->input_frames[0]->audio->nb_samples;
440 for (j = 1; !i && j < ctx->nb_inputs; j++)
441 ctx->inputs[j]->request_samples = nb_samples;
445 for (i = 0; i < s->nb_channels; i++) {
446 ChannelMap *ch = &s->channels[i];
447 AVFilterBufferRef *cur_buf = s->input_frames[ch->input];
449 s->data[i] = cur_buf->extended_data[ch->in_channel_idx];
450 linesize = FFMIN(linesize, cur_buf->linesize[0]);
451 perms &= cur_buf->perms;
454 av_assert0(nb_samples > 0);
455 buf = avfilter_get_audio_buffer_ref_from_arrays(s->data, linesize, perms,
456 nb_samples, outlink->format,
457 outlink->channel_layout);
459 return AVERROR(ENOMEM);
461 buf->buf->free = join_free_buffer;
462 buf->pts = s->input_frames[0]->pts;
464 if (!(priv = av_mallocz(sizeof(*priv))))
466 if (!(priv->in_buffers = av_mallocz(sizeof(*priv->in_buffers) * ctx->nb_inputs)))
469 for (i = 0; i < ctx->nb_inputs; i++)
470 priv->in_buffers[i] = s->input_frames[i];
471 priv->nb_in_buffers = ctx->nb_inputs;
472 buf->buf->priv = priv;
474 ret = ff_filter_samples(outlink, buf);
476 memset(s->input_frames, 0, sizeof(*s->input_frames) * ctx->nb_inputs);
481 avfilter_unref_buffer(buf);
483 av_freep(&priv->in_buffers);
485 return AVERROR(ENOMEM);
488 AVFilter avfilter_af_join = {
490 .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
491 "multi-channel output"),
492 .priv_size = sizeof(JoinContext),
495 .uninit = join_uninit,
496 .query_formats = join_query_formats,
499 .outputs = (const AVFilterPad[]){{ .name = "default",
500 .type = AVMEDIA_TYPE_AUDIO,
501 .config_props = join_config_output,
502 .request_frame = join_request_frame, },