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 int 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;
111 static int parse_maps(AVFilterContext *ctx)
113 JoinContext *s = ctx->priv;
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, ',');
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 int join_init(AVFilterContext *ctx, const char *args)
191 JoinContext *s = ctx->priv;
194 s->class = &join_class;
195 av_opt_set_defaults(s);
196 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
197 av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
201 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
202 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
203 s->channel_layout_str);
204 ret = AVERROR(EINVAL);
208 s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
209 s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
210 s->data = av_mallocz(sizeof(*s->data) * s->nb_channels);
211 s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
212 if (!s->channels || !s->data || !s->input_frames) {
213 ret = AVERROR(ENOMEM);
217 for (i = 0; i < s->nb_channels; i++) {
218 s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
219 s->channels[i].input = -1;
222 if ((ret = parse_maps(ctx)) < 0)
225 for (i = 0; i < s->inputs; i++) {
227 AVFilterPad pad = { 0 };
229 snprintf(name, sizeof(name), "input%d", i);
230 pad.type = AVMEDIA_TYPE_AUDIO;
231 pad.name = av_strdup(name);
232 pad.filter_samples = filter_samples;
236 ff_insert_inpad(ctx, i, &pad);
244 static void join_uninit(AVFilterContext *ctx)
246 JoinContext *s = ctx->priv;
249 for (i = 0; i < ctx->nb_inputs; i++) {
250 av_freep(&ctx->input_pads[i].name);
251 avfilter_unref_buffer(s->input_frames[i]);
254 av_freep(&s->channels);
256 av_freep(&s->input_frames);
259 static int join_query_formats(AVFilterContext *ctx)
261 JoinContext *s = ctx->priv;
262 AVFilterChannelLayouts *layouts = NULL;
265 ff_add_channel_layout(&layouts, s->channel_layout);
266 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
268 for (i = 0; i < ctx->nb_inputs; i++)
269 ff_channel_layouts_ref(ff_all_channel_layouts(),
270 &ctx->inputs[i]->out_channel_layouts);
272 ff_set_common_formats (ctx, ff_planar_sample_fmts());
273 ff_set_common_samplerates(ctx, ff_all_samplerates());
278 static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
283 for (i = 0; i < ctx->nb_inputs; i++) {
284 AVFilterLink *link = ctx->inputs[i];
286 if (ch->out_channel & link->channel_layout &&
287 !(ch->out_channel & inputs[i])) {
289 ch->in_channel = ch->out_channel;
290 inputs[i] |= ch->out_channel;
296 static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
301 for (i = 0; i < ctx->nb_inputs; i++) {
302 AVFilterLink *link = ctx->inputs[i];
304 if ((inputs[i] & link->channel_layout) != link->channel_layout) {
305 uint64_t unused = link->channel_layout & ~inputs[i];
308 ch->in_channel = av_channel_layout_extract_channel(unused, 0);
309 inputs[i] |= ch->in_channel;
315 static int join_config_output(AVFilterLink *outlink)
317 AVFilterContext *ctx = outlink->src;
318 JoinContext *s = ctx->priv;
319 uint64_t *inputs; // nth element tracks which channels are used from nth input
322 /* initialize inputs to user-specified mappings */
323 if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
324 return AVERROR(ENOMEM);
325 for (i = 0; i < s->nb_channels; i++) {
326 ChannelMap *ch = &s->channels[i];
327 AVFilterLink *inlink;
332 inlink = ctx->inputs[ch->input];
335 ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
338 if (!(ch->in_channel & inlink->channel_layout)) {
339 av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
340 "input stream #%d.\n", av_get_channel_name(ch->in_channel),
342 ret = AVERROR(EINVAL);
346 inputs[ch->input] |= ch->in_channel;
349 /* guess channel maps when not explicitly defined */
350 /* first try unused matching channels */
351 for (i = 0; i < s->nb_channels; i++) {
352 ChannelMap *ch = &s->channels[i];
355 guess_map_matching(ctx, ch, inputs);
358 /* if the above failed, try to find _any_ unused input channel */
359 for (i = 0; i < s->nb_channels; i++) {
360 ChannelMap *ch = &s->channels[i];
363 guess_map_any(ctx, ch, inputs);
366 av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
367 "output channel '%s'.\n",
368 av_get_channel_name(ch->out_channel));
372 ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
377 av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
378 for (i = 0; i < s->nb_channels; i++) {
379 ChannelMap *ch = &s->channels[i];
380 av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
381 av_get_channel_name(ch->in_channel),
382 av_get_channel_name(ch->out_channel));
384 av_log(ctx, AV_LOG_VERBOSE, "\n");
386 for (i = 0; i < ctx->nb_inputs; i++) {
388 av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
397 static void join_free_buffer(AVFilterBuffer *buf)
399 JoinBufferPriv *priv = buf->priv;
404 for (i = 0; i < priv->nb_in_buffers; i++)
405 avfilter_unref_buffer(priv->in_buffers[i]);
407 av_freep(&priv->in_buffers);
408 av_freep(&buf->priv);
411 if (buf->extended_data != buf->data)
412 av_freep(&buf->extended_data);
416 static int join_request_frame(AVFilterLink *outlink)
418 AVFilterContext *ctx = outlink->src;
419 JoinContext *s = ctx->priv;
420 AVFilterBufferRef *buf;
421 JoinBufferPriv *priv;
422 int linesize = INT_MAX;
427 /* get a frame on each input */
428 for (i = 0; i < ctx->nb_inputs; i++) {
429 AVFilterLink *inlink = ctx->inputs[i];
431 if (!s->input_frames[i] &&
432 (ret = ff_request_frame(inlink)) < 0)
435 /* request the same number of samples on all inputs */
437 nb_samples = s->input_frames[0]->audio->nb_samples;
439 for (j = 1; !i && j < ctx->nb_inputs; j++)
440 ctx->inputs[j]->request_samples = nb_samples;
444 for (i = 0; i < s->nb_channels; i++) {
445 ChannelMap *ch = &s->channels[i];
446 AVFilterBufferRef *cur_buf = s->input_frames[ch->input];
448 s->data[i] = cur_buf->extended_data[ch->in_channel_idx];
449 linesize = FFMIN(linesize, cur_buf->linesize[0]);
450 perms &= cur_buf->perms;
453 av_assert0(nb_samples > 0);
454 buf = avfilter_get_audio_buffer_ref_from_arrays(s->data, linesize, perms,
455 nb_samples, outlink->format,
456 outlink->channel_layout);
458 return AVERROR(ENOMEM);
460 buf->buf->free = join_free_buffer;
461 buf->pts = s->input_frames[0]->pts;
463 if (!(priv = av_mallocz(sizeof(*priv))))
465 if (!(priv->in_buffers = av_mallocz(sizeof(*priv->in_buffers) * ctx->nb_inputs)))
468 for (i = 0; i < ctx->nb_inputs; i++)
469 priv->in_buffers[i] = s->input_frames[i];
470 priv->nb_in_buffers = ctx->nb_inputs;
471 buf->buf->priv = priv;
473 ret = ff_filter_samples(outlink, buf);
475 memset(s->input_frames, 0, sizeof(*s->input_frames) * ctx->nb_inputs);
480 avfilter_unref_buffer(buf);
482 av_freep(&priv->in_buffers);
484 return AVERROR(ENOMEM);
487 AVFilter avfilter_af_join = {
489 .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
490 "multi-channel output"),
491 .priv_size = sizeof(JoinContext),
494 .uninit = join_uninit,
495 .query_formats = join_query_formats,
497 .inputs = (const AVFilterPad[]){{ NULL }},
498 .outputs = (const AVFilterPad[]){{ .name = "default",
499 .type = AVMEDIA_TYPE_AUDIO,
500 .config_props = join_config_output,
501 .request_frame = join_request_frame, },