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"
30 #include "libswresample/swresample.h" // only for SWR_CH_MAX
33 #include "bufferqueue.h"
39 int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
42 struct FFBufQueue queue;
43 int nb_ch; /**< number of channels for the input */
49 #define OFFSET(x) offsetof(AMergeContext, x)
50 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52 static const AVOption amerge_options[] = {
53 { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
54 AV_OPT_TYPE_INT, { .i64 = 2 }, 2, SWR_CH_MAX, FLAGS },
58 AVFILTER_DEFINE_CLASS(amerge);
60 static av_cold void uninit(AVFilterContext *ctx)
62 AMergeContext *am = ctx->priv;
65 for (i = 0; i < am->nb_inputs; i++) {
67 ff_bufqueue_discard_all(&am->in[i].queue);
69 av_freep(&ctx->input_pads[i].name);
74 static int query_formats(AVFilterContext *ctx)
76 AMergeContext *am = ctx->priv;
77 int64_t inlayout[SWR_CH_MAX], outlayout = 0;
78 AVFilterFormats *formats;
79 AVFilterChannelLayouts *layouts;
80 int i, overlap = 0, nb_ch = 0;
82 for (i = 0; i < am->nb_inputs; i++) {
83 if (!ctx->inputs[i]->in_channel_layouts ||
84 !ctx->inputs[i]->in_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]->in_channel_layouts->channel_layouts[0];
90 if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
92 av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
93 av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
95 am->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
96 if (outlayout & inlayout[i])
98 outlayout |= inlayout[i];
99 nb_ch += am->in[i].nb_ch;
101 if (nb_ch > SWR_CH_MAX) {
102 av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
103 return AVERROR(EINVAL);
106 av_log(ctx, AV_LOG_WARNING,
107 "Input channel layouts overlap: "
108 "output layout will be determined by the number of distinct input channels\n");
109 for (i = 0; i < nb_ch; i++)
111 outlayout = av_get_default_channel_layout(nb_ch);
113 outlayout = ((int64_t)1 << nb_ch) - 1;
115 int *route[SWR_CH_MAX];
116 int c, out_ch_number = 0;
118 route[0] = am->route;
119 for (i = 1; i < am->nb_inputs; i++)
120 route[i] = route[i - 1] + am->in[i - 1].nb_ch;
121 for (c = 0; c < 64; c++)
122 for (i = 0; i < am->nb_inputs; i++)
123 if ((inlayout[i] >> c) & 1)
124 *(route[i]++) = out_ch_number++;
126 formats = ff_make_format_list(ff_packed_sample_fmts_array);
127 ff_set_common_formats(ctx, formats);
128 for (i = 0; i < am->nb_inputs; i++) {
130 ff_add_channel_layout(&layouts, inlayout[i]);
131 ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
134 ff_add_channel_layout(&layouts, outlayout);
135 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
136 ff_set_common_samplerates(ctx, ff_all_samplerates());
140 static int config_output(AVFilterLink *outlink)
142 AVFilterContext *ctx = outlink->src;
143 AMergeContext *am = ctx->priv;
147 for (i = 1; i < am->nb_inputs; i++) {
148 if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
149 av_log(ctx, AV_LOG_ERROR,
150 "Inputs must have the same sample rate "
151 "%d for in%d vs %d\n",
152 ctx->inputs[i]->sample_rate, i, ctx->inputs[0]->sample_rate);
153 return AVERROR(EINVAL);
156 am->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
157 outlink->sample_rate = ctx->inputs[0]->sample_rate;
158 outlink->time_base = ctx->inputs[0]->time_base;
160 av_bprint_init(&bp, 0, 1);
161 for (i = 0; i < am->nb_inputs; i++) {
162 av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
163 av_bprint_channel_layout(&bp, -1, ctx->inputs[i]->channel_layout);
165 av_bprintf(&bp, " -> out:");
166 av_bprint_channel_layout(&bp, -1, ctx->outputs[0]->channel_layout);
167 av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
172 static int request_frame(AVFilterLink *outlink)
174 AVFilterContext *ctx = outlink->src;
175 AMergeContext *am = ctx->priv;
178 for (i = 0; i < am->nb_inputs; i++)
179 if (!am->in[i].nb_samples)
180 if ((ret = ff_request_frame(ctx->inputs[i])) < 0)
186 * Copy samples from several input streams to one output stream.
187 * @param nb_inputs number of inputs
188 * @param in inputs; used only for the nb_ch field;
189 * @param route routing values;
190 * input channel i goes to output channel route[i];
191 * i < in[0].nb_ch are the channels from the first output;
192 * i >= in[0].nb_ch are the channels from the second output
193 * @param ins pointer to the samples of each inputs, in packed format;
194 * will be left at the end of the copied samples
195 * @param outs pointer to the samples of the output, in packet format;
196 * must point to a buffer big enough;
197 * will be left at the end of the copied samples
198 * @param ns number of samples to copy
199 * @param bps bytes per sample
201 static inline void copy_samples(int nb_inputs, struct amerge_input in[],
202 int *route, uint8_t *ins[],
203 uint8_t **outs, int ns, int bps)
208 for (i = 0; i < nb_inputs; i++)
209 nb_ch += in[i].nb_ch;
212 for (i = 0; i < nb_inputs; i++) {
213 for (c = 0; c < in[i].nb_ch; c++) {
214 memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
218 *outs += nb_ch * bps;
222 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
224 AVFilterContext *ctx = inlink->dst;
225 AMergeContext *am = ctx->priv;
226 AVFilterLink *const outlink = ctx->outputs[0];
228 int nb_samples, ns, i;
229 AVFrame *outbuf, *inbuf[SWR_CH_MAX];
230 uint8_t *ins[SWR_CH_MAX], *outs;
232 for (input_number = 0; input_number < am->nb_inputs; input_number++)
233 if (inlink == ctx->inputs[input_number])
235 av_assert1(input_number < am->nb_inputs);
236 if (ff_bufqueue_is_full(&am->in[input_number].queue)) {
237 av_frame_free(&insamples);
238 return AVERROR(ENOMEM);
240 ff_bufqueue_add(ctx, &am->in[input_number].queue, av_frame_clone(insamples));
241 am->in[input_number].nb_samples += insamples->nb_samples;
242 av_frame_free(&insamples);
243 nb_samples = am->in[0].nb_samples;
244 for (i = 1; i < am->nb_inputs; i++)
245 nb_samples = FFMIN(nb_samples, am->in[i].nb_samples);
249 outbuf = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
251 return AVERROR(ENOMEM);
252 outs = outbuf->data[0];
253 for (i = 0; i < am->nb_inputs; i++) {
254 inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
255 ins[i] = inbuf[i]->data[0] +
256 am->in[i].pos * am->in[i].nb_ch * am->bps;
258 av_frame_copy_props(outbuf, inbuf[0]);
259 outbuf->pts = inbuf[0]->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
261 av_rescale_q(am->in[0].pos,
262 (AVRational){ 1, ctx->inputs[0]->sample_rate },
263 ctx->outputs[0]->time_base);
265 outbuf->nb_samples = nb_samples;
266 outbuf->channel_layout = outlink->channel_layout;
267 av_frame_set_channels(outbuf, outlink->channels);
271 for (i = 0; i < am->nb_inputs; i++)
272 ns = FFMIN(ns, inbuf[i]->nb_samples - am->in[i].pos);
273 /* Unroll the most common sample formats: speed +~350% for the loop,
274 +~13% overall (including two common decoders) */
277 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 1);
280 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 2);
283 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 4);
286 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, am->bps);
291 for (i = 0; i < am->nb_inputs; i++) {
292 am->in[i].nb_samples -= ns;
294 if (am->in[i].pos == inbuf[i]->nb_samples) {
296 av_frame_free(&inbuf[i]);
297 ff_bufqueue_get(&am->in[i].queue);
298 inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
299 ins[i] = inbuf[i] ? inbuf[i]->data[0] : NULL;
303 return ff_filter_frame(ctx->outputs[0], outbuf);
306 static av_cold int init(AVFilterContext *ctx)
308 AMergeContext *am = ctx->priv;
311 am->in = av_calloc(am->nb_inputs, sizeof(*am->in));
313 return AVERROR(ENOMEM);
314 for (i = 0; i < am->nb_inputs; i++) {
315 char *name = av_asprintf("in%d", i);
318 .type = AVMEDIA_TYPE_AUDIO,
319 .filter_frame = filter_frame,
322 return AVERROR(ENOMEM);
323 ff_insert_inpad(ctx, i, &pad);
328 static const AVFilterPad amerge_outputs[] = {
331 .type = AVMEDIA_TYPE_AUDIO,
332 .config_props = config_output,
333 .request_frame = request_frame,
338 AVFilter avfilter_af_amerge = {
340 .description = NULL_IF_CONFIG_SMALL("Merge two or more audio streams into "
341 "a single multi-channel stream."),
342 .priv_size = sizeof(AMergeContext),
345 .query_formats = query_formats,
347 .outputs = amerge_outputs,
348 .priv_class = &amerge_class,
349 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,