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;
108 while (cur && *cur) {
109 char *sep, *next, *p;
110 uint64_t in_channel = 0, out_channel = 0;
111 int input_idx, out_ch_idx, in_ch_idx;
113 next = strchr(cur, ',');
117 /* split the map into input and output parts */
118 if (!(sep = strchr(cur, '-'))) {
119 av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
121 return AVERROR(EINVAL);
125 #define PARSE_CHANNEL(str, var, inout) \
126 if (!(var = av_get_channel_layout(str))) { \
127 av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
128 return AVERROR(EINVAL); \
130 if (av_get_channel_layout_nb_channels(var) != 1) { \
131 av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
132 inout " channel.\n"); \
133 return AVERROR(EINVAL); \
136 /* parse output channel */
137 PARSE_CHANNEL(sep, out_channel, "output");
138 if (!(out_channel & s->channel_layout)) {
139 av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
140 "requested channel layout.\n", sep);
141 return AVERROR(EINVAL);
144 out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
146 if (s->channels[out_ch_idx].input >= 0) {
147 av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
149 return AVERROR(EINVAL);
152 /* parse input channel */
153 input_idx = strtol(cur, &cur, 0);
154 if (input_idx < 0 || input_idx >= s->inputs) {
155 av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
157 return AVERROR(EINVAL);
163 in_ch_idx = strtol(cur, &p, 0);
165 /* channel specifier is not a number,
166 * try to parse as channel name */
167 PARSE_CHANNEL(cur, in_channel, "input");
170 s->channels[out_ch_idx].input = input_idx;
172 s->channels[out_ch_idx].in_channel = in_channel;
174 s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
181 static int join_init(AVFilterContext *ctx, const char *args)
183 JoinContext *s = ctx->priv;
186 s->class = &join_class;
187 av_opt_set_defaults(s);
188 if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
191 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
192 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
193 s->channel_layout_str);
194 ret = AVERROR(EINVAL);
198 s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
199 s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
200 s->buffers = av_mallocz(sizeof(*s->buffers) * s->nb_channels);
201 s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
202 if (!s->channels || !s->buffers|| !s->input_frames) {
203 ret = AVERROR(ENOMEM);
207 for (i = 0; i < s->nb_channels; i++) {
208 s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
209 s->channels[i].input = -1;
212 if ((ret = parse_maps(ctx)) < 0)
215 for (i = 0; i < s->inputs; i++) {
217 AVFilterPad pad = { 0 };
219 snprintf(name, sizeof(name), "input%d", i);
220 pad.type = AVMEDIA_TYPE_AUDIO;
221 pad.name = av_strdup(name);
222 pad.filter_frame = filter_frame;
226 ff_insert_inpad(ctx, i, &pad);
234 static void join_uninit(AVFilterContext *ctx)
236 JoinContext *s = ctx->priv;
239 for (i = 0; i < ctx->nb_inputs; i++) {
240 av_freep(&ctx->input_pads[i].name);
241 av_frame_free(&s->input_frames[i]);
244 av_freep(&s->channels);
245 av_freep(&s->buffers);
246 av_freep(&s->input_frames);
249 static int join_query_formats(AVFilterContext *ctx)
251 JoinContext *s = ctx->priv;
252 AVFilterChannelLayouts *layouts = NULL;
255 ff_add_channel_layout(&layouts, s->channel_layout);
256 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
258 for (i = 0; i < ctx->nb_inputs; i++)
259 ff_channel_layouts_ref(ff_all_channel_layouts(),
260 &ctx->inputs[i]->out_channel_layouts);
262 ff_set_common_formats (ctx, ff_planar_sample_fmts());
263 ff_set_common_samplerates(ctx, ff_all_samplerates());
268 static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
273 for (i = 0; i < ctx->nb_inputs; i++) {
274 AVFilterLink *link = ctx->inputs[i];
276 if (ch->out_channel & link->channel_layout &&
277 !(ch->out_channel & inputs[i])) {
279 ch->in_channel = ch->out_channel;
280 inputs[i] |= ch->out_channel;
286 static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
291 for (i = 0; i < ctx->nb_inputs; i++) {
292 AVFilterLink *link = ctx->inputs[i];
294 if ((inputs[i] & link->channel_layout) != link->channel_layout) {
295 uint64_t unused = link->channel_layout & ~inputs[i];
298 ch->in_channel = av_channel_layout_extract_channel(unused, 0);
299 inputs[i] |= ch->in_channel;
305 static int join_config_output(AVFilterLink *outlink)
307 AVFilterContext *ctx = outlink->src;
308 JoinContext *s = ctx->priv;
309 uint64_t *inputs; // nth element tracks which channels are used from nth input
312 /* initialize inputs to user-specified mappings */
313 if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
314 return AVERROR(ENOMEM);
315 for (i = 0; i < s->nb_channels; i++) {
316 ChannelMap *ch = &s->channels[i];
317 AVFilterLink *inlink;
322 inlink = ctx->inputs[ch->input];
325 ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
328 if (!(ch->in_channel & inlink->channel_layout)) {
329 av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
330 "input stream #%d.\n", av_get_channel_name(ch->in_channel),
332 ret = AVERROR(EINVAL);
336 inputs[ch->input] |= ch->in_channel;
339 /* guess channel maps when not explicitly defined */
340 /* first try unused matching channels */
341 for (i = 0; i < s->nb_channels; i++) {
342 ChannelMap *ch = &s->channels[i];
345 guess_map_matching(ctx, ch, inputs);
348 /* if the above failed, try to find _any_ unused input channel */
349 for (i = 0; i < s->nb_channels; i++) {
350 ChannelMap *ch = &s->channels[i];
353 guess_map_any(ctx, ch, inputs);
356 av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
357 "output channel '%s'.\n",
358 av_get_channel_name(ch->out_channel));
362 ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
367 av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
368 for (i = 0; i < s->nb_channels; i++) {
369 ChannelMap *ch = &s->channels[i];
370 av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
371 av_get_channel_name(ch->in_channel),
372 av_get_channel_name(ch->out_channel));
374 av_log(ctx, AV_LOG_VERBOSE, "\n");
376 for (i = 0; i < ctx->nb_inputs; i++) {
378 av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
387 static int join_request_frame(AVFilterLink *outlink)
389 AVFilterContext *ctx = outlink->src;
390 JoinContext *s = ctx->priv;
392 int linesize = INT_MAX;
397 /* get a frame on each input */
398 for (i = 0; i < ctx->nb_inputs; i++) {
399 AVFilterLink *inlink = ctx->inputs[i];
401 if (!s->input_frames[i] &&
402 (ret = ff_request_frame(inlink)) < 0)
405 /* request the same number of samples on all inputs */
407 nb_samples = s->input_frames[0]->nb_samples;
409 for (j = 1; !i && j < ctx->nb_inputs; j++)
410 ctx->inputs[j]->request_samples = nb_samples;
414 /* setup the output frame */
415 frame = av_frame_alloc();
417 return AVERROR(ENOMEM);
418 if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
419 frame->extended_data = av_mallocz(s->nb_channels *
420 sizeof(*frame->extended_data));
421 if (!frame->extended_data) {
422 ret = AVERROR(ENOMEM);
427 /* copy the data pointers */
428 for (i = 0; i < s->nb_channels; i++) {
429 ChannelMap *ch = &s->channels[i];
430 AVFrame *cur = s->input_frames[ch->input];
433 frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
434 linesize = FFMIN(linesize, cur->linesize[0]);
436 /* add the buffer where this plan is stored to the list if it's
437 * not already there */
438 buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
440 ret = AVERROR(EINVAL);
443 for (j = 0; j < nb_buffers; j++)
444 if (s->buffers[j]->buffer == buf->buffer)
447 s->buffers[nb_buffers++] = buf;
450 /* create references to the buffers we copied to output */
451 if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
452 frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
453 frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
454 frame->nb_extended_buf);
455 if (!frame->extended_buf) {
456 frame->nb_extended_buf = 0;
457 ret = AVERROR(ENOMEM);
461 for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
462 frame->buf[i] = av_buffer_ref(s->buffers[i]);
463 if (!frame->buf[i]) {
464 ret = AVERROR(ENOMEM);
468 for (i = 0; i < frame->nb_extended_buf; i++) {
469 frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
470 FF_ARRAY_ELEMS(frame->buf)]);
471 if (!frame->extended_buf[i]) {
472 ret = AVERROR(ENOMEM);
477 frame->nb_samples = nb_samples;
478 frame->channel_layout = outlink->channel_layout;
479 av_frame_set_channels(frame, outlink->channels);
480 frame->format = outlink->format;
481 frame->sample_rate = outlink->sample_rate;
482 frame->pts = s->input_frames[0]->pts;
483 frame->linesize[0] = linesize;
484 if (frame->data != frame->extended_data) {
485 memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
486 FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
489 ret = ff_filter_frame(outlink, frame);
491 for (i = 0; i < ctx->nb_inputs; i++)
492 av_frame_free(&s->input_frames[i]);
497 av_frame_free(&frame);
501 static const AVFilterPad avfilter_af_join_outputs[] = {
504 .type = AVMEDIA_TYPE_AUDIO,
505 .config_props = join_config_output,
506 .request_frame = join_request_frame,
511 AVFilter avfilter_af_join = {
513 .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
514 "multi-channel output"),
515 .priv_size = sizeof(JoinContext),
518 .uninit = join_uninit,
519 .query_formats = join_query_formats,
522 .outputs = avfilter_af_join_outputs,
523 .priv_class = &join_class,