2 * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Audio merging filter
26 #include "libavutil/avstring.h"
27 #include "libavutil/bprint.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/opt.h"
38 typedef struct AMergeContext {
41 int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
44 int nb_ch; /**< number of channels for the input */
48 #define OFFSET(x) offsetof(AMergeContext, x)
49 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 static const AVOption amerge_options[] = {
52 { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
53 AV_OPT_TYPE_INT, { .i64 = 2 }, 1, SWR_CH_MAX, FLAGS },
57 AVFILTER_DEFINE_CLASS(amerge);
59 static av_cold void uninit(AVFilterContext *ctx)
61 AMergeContext *s = ctx->priv;
66 static int query_formats(AVFilterContext *ctx)
68 static const enum AVSampleFormat packed_sample_fmts[] = {
76 AMergeContext *s = ctx->priv;
77 AVChannelLayout *inlayout[SWR_CH_MAX] = { NULL }, outlayout = { 0 };
79 AVFilterChannelLayouts *layouts;
80 int i, ret, overlap = 0, nb_ch = 0;
82 for (i = 0; i < s->nb_inputs; i++) {
83 if (!ctx->inputs[i]->incfg.channel_layouts ||
84 !ctx->inputs[i]->incfg.channel_layouts->nb_channel_layouts) {
85 av_log(ctx, AV_LOG_WARNING,
86 "No channel layout for input %d\n", i + 1);
87 return AVERROR(EAGAIN);
89 inlayout[i] = &ctx->inputs[i]->incfg.channel_layouts->channel_layouts[0];
90 if (ctx->inputs[i]->incfg.channel_layouts->nb_channel_layouts > 1) {
92 av_channel_layout_describe(inlayout[i], buf, sizeof(buf));
93 av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
95 s->in[i].nb_ch = FF_LAYOUT2COUNT(inlayout[i]);
99 s->in[i].nb_ch = inlayout[i]->nb_channels;
100 if (av_channel_layout_subset(inlayout[i], outmask))
102 outmask |= inlayout[i]->order == AV_CHANNEL_ORDER_NATIVE ?
103 inlayout[i]->u.mask : 0;
105 nb_ch += s->in[i].nb_ch;
107 if (nb_ch > SWR_CH_MAX) {
108 av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
109 return AVERROR(EINVAL);
112 av_log(ctx, AV_LOG_WARNING,
113 "Input channel layouts overlap: "
114 "output layout will be determined by the number of distinct input channels\n");
115 for (i = 0; i < nb_ch; i++)
117 av_channel_layout_default(&outlayout, nb_ch);
118 if (!KNOWN(&outlayout) && nb_ch)
119 av_channel_layout_from_mask(&outlayout, 0xFFFFFFFFFFFFFFFFULL >> (64 - nb_ch));
121 int *route[SWR_CH_MAX];
122 int c, out_ch_number = 0;
124 av_channel_layout_from_mask(&outlayout, outmask);
126 for (i = 1; i < s->nb_inputs; i++)
127 route[i] = route[i - 1] + s->in[i - 1].nb_ch;
128 for (c = 0; c < 64; c++)
129 for (i = 0; i < s->nb_inputs; i++)
130 if (av_channel_layout_index_from_channel(inlayout[i], c) >= 0)
131 *(route[i]++) = out_ch_number++;
133 if ((ret = ff_set_common_formats_from_list(ctx, packed_sample_fmts)) < 0)
135 for (i = 0; i < s->nb_inputs; i++) {
137 if ((ret = ff_add_channel_layout(&layouts, inlayout[i])) < 0)
139 if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->outcfg.channel_layouts)) < 0)
143 if ((ret = ff_add_channel_layout(&layouts, &outlayout)) < 0)
145 if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->incfg.channel_layouts)) < 0)
148 return ff_set_common_all_samplerates(ctx);
151 static int config_output(AVFilterLink *outlink)
153 AVFilterContext *ctx = outlink->src;
154 AMergeContext *s = ctx->priv;
159 s->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
160 outlink->time_base = ctx->inputs[0]->time_base;
162 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
163 for (i = 0; i < s->nb_inputs; i++) {
164 av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
165 av_channel_layout_describe(&ctx->inputs[i]->ch_layout, buf, sizeof(buf));
166 av_bprintf(&bp, "%s", buf);
168 av_bprintf(&bp, " -> out:");
169 av_channel_layout_describe(&ctx->outputs[0]->ch_layout, buf, sizeof(buf));
170 av_bprintf(&bp, "%s", buf);
171 av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
177 * Copy samples from several input streams to one output stream.
178 * @param nb_inputs number of inputs
179 * @param in inputs; used only for the nb_ch field;
180 * @param route routing values;
181 * input channel i goes to output channel route[i];
182 * i < in[0].nb_ch are the channels from the first output;
183 * i >= in[0].nb_ch are the channels from the second output
184 * @param ins pointer to the samples of each inputs, in packed format;
185 * will be left at the end of the copied samples
186 * @param outs pointer to the samples of the output, in packet format;
187 * must point to a buffer big enough;
188 * will be left at the end of the copied samples
189 * @param ns number of samples to copy
190 * @param bps bytes per sample
192 static inline void copy_samples(int nb_inputs, struct amerge_input in[],
193 int *route, uint8_t *ins[],
194 uint8_t **outs, int ns, int bps)
199 for (i = 0; i < nb_inputs; i++)
200 nb_ch += in[i].nb_ch;
203 for (i = 0; i < nb_inputs; i++) {
204 for (c = 0; c < in[i].nb_ch; c++) {
205 memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
209 *outs += nb_ch * bps;
213 static void free_frames(int nb_inputs, AVFrame **input_frames)
216 for (i = 0; i < nb_inputs; i++)
217 av_frame_free(&input_frames[i]);
220 static int try_push_frame(AVFilterContext *ctx, int nb_samples)
222 AMergeContext *s = ctx->priv;
223 AVFilterLink *outlink = ctx->outputs[0];
225 AVFrame *outbuf, *inbuf[SWR_CH_MAX] = { NULL };
226 uint8_t *outs, *ins[SWR_CH_MAX];
228 for (i = 0; i < ctx->nb_inputs; i++) {
229 ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &inbuf[i]);
231 free_frames(i, inbuf);
234 ins[i] = inbuf[i]->data[0];
237 outbuf = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
239 free_frames(s->nb_inputs, inbuf);
240 return AVERROR(ENOMEM);
243 outs = outbuf->data[0];
244 outbuf->pts = inbuf[0]->pts;
246 outbuf->nb_samples = nb_samples;
247 outbuf->duration = av_rescale_q(outbuf->nb_samples,
248 av_make_q(1, outlink->sample_rate),
251 if ((ret = av_channel_layout_copy(&outbuf->ch_layout, &outlink->ch_layout)) < 0)
253 #if FF_API_OLD_CHANNEL_LAYOUT
254 FF_DISABLE_DEPRECATION_WARNINGS
255 outbuf->channel_layout = outlink->channel_layout;
256 outbuf->channels = outlink->ch_layout.nb_channels;
257 FF_ENABLE_DEPRECATION_WARNINGS
261 /* Unroll the most common sample formats: speed +~350% for the loop,
262 +~13% overall (including two common decoders) */
265 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 1);
268 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 2);
271 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 4);
274 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, s->bps);
281 free_frames(s->nb_inputs, inbuf);
282 return ff_filter_frame(ctx->outputs[0], outbuf);
285 static int activate(AVFilterContext *ctx)
291 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
293 nb_samples = ff_inlink_queued_samples(ctx->inputs[0]);
294 for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) {
295 nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[i]), nb_samples);
299 ret = try_push_frame(ctx, nb_samples);
304 for (i = 0; i < ctx->nb_inputs; i++) {
305 if (ff_inlink_queued_samples(ctx->inputs[i]))
308 if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
309 ff_outlink_set_status(ctx->outputs[0], status, pts);
311 } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
312 ff_inlink_request_frame(ctx->inputs[i]);
320 static av_cold int init(AVFilterContext *ctx)
322 AMergeContext *s = ctx->priv;
325 s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
327 return AVERROR(ENOMEM);
328 for (i = 0; i < s->nb_inputs; i++) {
329 char *name = av_asprintf("in%d", i);
332 .type = AVMEDIA_TYPE_AUDIO,
335 return AVERROR(ENOMEM);
336 if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0)
342 static const AVFilterPad amerge_outputs[] = {
345 .type = AVMEDIA_TYPE_AUDIO,
346 .config_props = config_output,
350 const AVFilter ff_af_amerge = {
352 .description = NULL_IF_CONFIG_SMALL("Merge two or more audio streams into "
353 "a single multi-channel stream."),
354 .priv_size = sizeof(AMergeContext),
357 .activate = activate,
359 FILTER_OUTPUTS(amerge_outputs),
360 FILTER_QUERY_FUNC(query_formats),
361 .priv_class = &amerge_class,
362 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,