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/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 AVFilterBufferRef **input_frames;
61 * Temporary storage for data pointers, for assembling the output buffer.
67 * To avoid copying the data from input buffers, this filter creates
68 * a custom output buffer that stores references to all inputs and
69 * unrefs them on free.
71 typedef struct JoinBufferPriv {
72 AVFilterBufferRef **in_buffers;
76 #define OFFSET(x) offsetof(JoinContext, x)
77 #define A AV_OPT_FLAG_AUDIO_PARAM
78 static const AVOption join_options[] = {
79 { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { 2 }, 1, INT_MAX, A },
80 { "channel_layout", "Channel layout of the "
81 "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A },
82 { "map", "A comma-separated list of channels maps in the format "
83 "'input_stream.input_channel-output_channel.",
84 OFFSET(map), AV_OPT_TYPE_STRING, .flags = A },
88 static const AVClass join_class = {
89 .class_name = "join filter",
90 .item_name = av_default_item_name,
91 .option = join_options,
92 .version = LIBAVUTIL_VERSION_INT,
95 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *buf)
97 AVFilterContext *ctx = link->dst;
98 JoinContext *s = ctx->priv;
101 for (i = 0; i < ctx->nb_inputs; i++)
102 if (link == ctx->inputs[i])
104 av_assert0(i < ctx->nb_inputs);
105 av_assert0(!s->input_frames[i]);
106 s->input_frames[i] = buf;
109 static int parse_maps(AVFilterContext *ctx)
111 JoinContext *s = ctx->priv;
114 while (cur && *cur) {
115 char *sep, *next, *p;
116 uint64_t in_channel = 0, out_channel = 0;
117 int input_idx, out_ch_idx, in_ch_idx;
119 next = strchr(cur, ',');
123 /* split the map into input and output parts */
124 if (!(sep = strchr(cur, '-'))) {
125 av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
127 return AVERROR(EINVAL);
131 #define PARSE_CHANNEL(str, var, inout) \
132 if (!(var = av_get_channel_layout(str))) { \
133 av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
134 return AVERROR(EINVAL); \
136 if (av_get_channel_layout_nb_channels(var) != 1) { \
137 av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
138 inout " channel.\n"); \
139 return AVERROR(EINVAL); \
142 /* parse output channel */
143 PARSE_CHANNEL(sep, out_channel, "output");
144 if (!(out_channel & s->channel_layout)) {
145 av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
146 "requested channel layout.\n", sep);
147 return AVERROR(EINVAL);
150 out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
152 if (s->channels[out_ch_idx].input >= 0) {
153 av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
155 return AVERROR(EINVAL);
158 /* parse input channel */
159 input_idx = strtol(cur, &cur, 0);
160 if (input_idx < 0 || input_idx >= s->inputs) {
161 av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
163 return AVERROR(EINVAL);
169 in_ch_idx = strtol(cur, &p, 0);
171 /* channel specifier is not a number,
172 * try to parse as channel name */
173 PARSE_CHANNEL(cur, in_channel, "input");
176 s->channels[out_ch_idx].input = input_idx;
178 s->channels[out_ch_idx].in_channel = in_channel;
180 s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
187 static int join_init(AVFilterContext *ctx, const char *args, void *opaque)
189 JoinContext *s = ctx->priv;
192 s->class = &join_class;
193 av_opt_set_defaults(s);
194 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
195 av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
199 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
200 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
201 s->channel_layout_str);
202 ret = AVERROR(EINVAL);
206 s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
207 s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
208 s->data = av_mallocz(sizeof(*s->data) * s->nb_channels);
209 s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
210 if (!s->channels || !s->data || !s->input_frames) {
211 ret = AVERROR(ENOMEM);
215 for (i = 0; i < s->nb_channels; i++) {
216 s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
217 s->channels[i].input = -1;
220 if ((ret = parse_maps(ctx)) < 0)
223 for (i = 0; i < s->inputs; i++) {
225 AVFilterPad pad = { 0 };
227 snprintf(name, sizeof(name), "input%d", i);
228 pad.type = AVMEDIA_TYPE_AUDIO;
229 pad.name = av_strdup(name);
230 pad.filter_samples = filter_samples;
234 ff_insert_inpad(ctx, i, &pad);
242 static void join_uninit(AVFilterContext *ctx)
244 JoinContext *s = ctx->priv;
247 for (i = 0; i < ctx->nb_inputs; i++) {
248 av_freep(&ctx->input_pads[i].name);
249 avfilter_unref_buffer(s->input_frames[i]);
252 av_freep(&s->channels);
254 av_freep(&s->input_frames);
257 static int join_query_formats(AVFilterContext *ctx)
259 JoinContext *s = ctx->priv;
260 AVFilterChannelLayouts *layouts = NULL;
263 ff_add_channel_layout(&layouts, s->channel_layout);
264 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
266 for (i = 0; i < ctx->nb_inputs; i++)
267 ff_channel_layouts_ref(ff_all_channel_layouts(),
268 &ctx->inputs[i]->out_channel_layouts);
270 ff_set_common_formats (ctx, ff_planar_sample_fmts());
271 ff_set_common_samplerates(ctx, ff_all_samplerates());
276 static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
281 for (i = 0; i < ctx->nb_inputs; i++) {
282 AVFilterLink *link = ctx->inputs[i];
284 if (ch->out_channel & link->channel_layout &&
285 !(ch->out_channel & inputs[i])) {
287 ch->in_channel = ch->out_channel;
288 inputs[i] |= ch->out_channel;
294 static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
299 for (i = 0; i < ctx->nb_inputs; i++) {
300 AVFilterLink *link = ctx->inputs[i];
302 if ((inputs[i] & link->channel_layout) != link->channel_layout) {
303 uint64_t unused = link->channel_layout & ~inputs[i];
306 ch->in_channel = av_channel_layout_extract_channel(unused, 0);
307 inputs[i] |= ch->in_channel;
313 static int join_config_output(AVFilterLink *outlink)
315 AVFilterContext *ctx = outlink->src;
316 JoinContext *s = ctx->priv;
317 uint64_t *inputs; // nth element tracks which channels are used from nth input
320 /* initialize inputs to user-specified mappings */
321 if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
322 return AVERROR(ENOMEM);
323 for (i = 0; i < s->nb_channels; i++) {
324 ChannelMap *ch = &s->channels[i];
325 AVFilterLink *inlink;
330 inlink = ctx->inputs[ch->input];
333 ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
336 if (!(ch->in_channel & inlink->channel_layout)) {
337 av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
338 "input stream #%d.\n", av_get_channel_name(ch->in_channel),
340 ret = AVERROR(EINVAL);
344 inputs[ch->input] |= ch->in_channel;
347 /* guess channel maps when not explicitly defined */
348 /* first try unused matching channels */
349 for (i = 0; i < s->nb_channels; i++) {
350 ChannelMap *ch = &s->channels[i];
353 guess_map_matching(ctx, ch, inputs);
356 /* if the above failed, try to find _any_ unused input channel */
357 for (i = 0; i < s->nb_channels; i++) {
358 ChannelMap *ch = &s->channels[i];
361 guess_map_any(ctx, ch, inputs);
364 av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
365 "output channel '%s'.\n",
366 av_get_channel_name(ch->out_channel));
370 ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
375 av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
376 for (i = 0; i < s->nb_channels; i++) {
377 ChannelMap *ch = &s->channels[i];
378 av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
379 av_get_channel_name(ch->in_channel),
380 av_get_channel_name(ch->out_channel));
382 av_log(ctx, AV_LOG_VERBOSE, "\n");
384 for (i = 0; i < ctx->nb_inputs; i++) {
386 av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
395 static void join_free_buffer(AVFilterBuffer *buf)
397 JoinBufferPriv *priv = buf->priv;
402 for (i = 0; i < priv->nb_in_buffers; i++)
403 avfilter_unref_buffer(priv->in_buffers[i]);
405 av_freep(&priv->in_buffers);
406 av_freep(&buf->priv);
409 if (buf->extended_data != buf->data)
410 av_freep(&buf->extended_data);
414 static int join_request_frame(AVFilterLink *outlink)
416 AVFilterContext *ctx = outlink->src;
417 JoinContext *s = ctx->priv;
418 AVFilterBufferRef *buf;
419 JoinBufferPriv *priv;
420 int linesize = INT_MAX;
425 /* get a frame on each input */
426 for (i = 0; i < ctx->nb_inputs; i++) {
427 AVFilterLink *inlink = ctx->inputs[i];
429 if (!s->input_frames[i] &&
430 (ret = ff_request_frame(inlink)) < 0)
433 /* request the same number of samples on all inputs */
435 nb_samples = s->input_frames[0]->audio->nb_samples;
437 for (j = 1; !i && j < ctx->nb_inputs; j++)
438 ctx->inputs[j]->request_samples = nb_samples;
442 for (i = 0; i < s->nb_channels; i++) {
443 ChannelMap *ch = &s->channels[i];
444 AVFilterBufferRef *cur_buf = s->input_frames[ch->input];
446 s->data[i] = cur_buf->extended_data[ch->in_channel_idx];
447 linesize = FFMIN(linesize, cur_buf->linesize[0]);
448 perms &= cur_buf->perms;
451 buf = avfilter_get_audio_buffer_ref_from_arrays(s->data, linesize, perms,
452 nb_samples, outlink->format,
453 outlink->channel_layout);
455 return AVERROR(ENOMEM);
457 buf->buf->free = join_free_buffer;
458 buf->pts = s->input_frames[0]->pts;
460 if (!(priv = av_mallocz(sizeof(*priv))))
462 if (!(priv->in_buffers = av_mallocz(sizeof(*priv->in_buffers) * ctx->nb_inputs)))
465 for (i = 0; i < ctx->nb_inputs; i++)
466 priv->in_buffers[i] = s->input_frames[i];
467 priv->nb_in_buffers = ctx->nb_inputs;
468 buf->buf->priv = priv;
470 ff_filter_samples(outlink, buf);
472 memset(s->input_frames, 0, sizeof(*s->input_frames) * ctx->nb_inputs);
477 avfilter_unref_buffer(buf);
479 av_freep(&priv->in_buffers);
481 return AVERROR(ENOMEM);
484 AVFilter avfilter_af_join = {
486 .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
487 "multi-channel output"),
488 .priv_size = sizeof(JoinContext),
491 .uninit = join_uninit,
492 .query_formats = join_query_formats,
494 .inputs = (const AVFilterPad[]){{ NULL }},
495 .outputs = (const AVFilterPad[]){{ .name = "default",
496 .type = AVMEDIA_TYPE_AUDIO,
497 .config_props = join_config_output,
498 .request_frame = join_request_frame, },