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 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/bprint.h"
27 #include "libavutil/opt.h"
28 #include "libswresample/swresample.h" // only for SWR_CH_MAX
31 #include "bufferqueue.h"
37 int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
40 struct FFBufQueue queue;
41 int nb_ch; /**< number of channels for the input */
47 #define OFFSET(x) offsetof(AMergeContext, x)
49 static const AVOption amerge_options[] = {
50 { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
51 AV_OPT_TYPE_INT, { .dbl = 2 }, 2, SWR_CH_MAX },
55 static const AVClass amerge_class = {
56 .class_name = "AMergeContext",
57 .item_name = av_default_item_name,
58 .option = amerge_options,
61 static av_cold void uninit(AVFilterContext *ctx)
63 AMergeContext *am = ctx->priv;
66 for (i = 0; i < am->nb_inputs; i++)
67 ff_bufqueue_discard_all(&am->in[i].queue);
71 static int query_formats(AVFilterContext *ctx)
73 AMergeContext *am = ctx->priv;
74 int64_t inlayout[SWR_CH_MAX], outlayout = 0;
75 AVFilterFormats *formats;
76 AVFilterChannelLayouts *layouts;
77 int i, overlap = 0, nb_ch = 0;
79 for (i = 0; i < am->nb_inputs; i++) {
80 if (!ctx->inputs[i]->in_channel_layouts ||
81 !ctx->inputs[i]->in_channel_layouts->nb_channel_layouts) {
82 av_log(ctx, AV_LOG_ERROR,
83 "No channel layout for input %d\n", i + 1);
84 return AVERROR(EINVAL);
86 inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
87 if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
89 av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
90 av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
92 am->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
93 if (outlayout & inlayout[i])
95 outlayout |= inlayout[i];
96 nb_ch += am->in[i].nb_ch;
98 if (nb_ch > SWR_CH_MAX) {
99 av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
100 return AVERROR(EINVAL);
103 av_log(ctx, AV_LOG_WARNING,
104 "Inputs overlap: output layout will be meaningless\n");
105 for (i = 0; i < nb_ch; i++)
107 outlayout = av_get_default_channel_layout(nb_ch);
109 outlayout = ((int64_t)1 << nb_ch) - 1;
111 int *route[SWR_CH_MAX];
112 int c, out_ch_number = 0;
114 route[0] = am->route;
115 for (i = 1; i < am->nb_inputs; i++)
116 route[i] = route[i - 1] + am->in[i - 1].nb_ch;
117 for (c = 0; c < 64; c++)
118 for (i = 0; i < am->nb_inputs; i++)
119 if ((inlayout[i] >> c) & 1)
120 *(route[i]++) = out_ch_number++;
122 formats = avfilter_make_format_list(ff_packed_sample_fmts_array);
123 avfilter_set_common_sample_formats(ctx, formats);
124 for (i = 0; i < am->nb_inputs; i++) {
126 ff_add_channel_layout(&layouts, inlayout[i]);
127 ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
130 ff_add_channel_layout(&layouts, outlayout);
131 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
132 ff_set_common_samplerates(ctx, ff_all_samplerates());
136 static int config_output(AVFilterLink *outlink)
138 AVFilterContext *ctx = outlink->src;
139 AMergeContext *am = ctx->priv;
143 for (i = 1; i < am->nb_inputs; i++) {
144 if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
145 av_log(ctx, AV_LOG_ERROR,
146 "Inputs must have the same sample rate "
147 "(%"PRIi64" for in%d vs %"PRIi64")\n",
148 ctx->inputs[i]->sample_rate, i, ctx->inputs[0]->sample_rate);
149 return AVERROR(EINVAL);
152 am->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
153 outlink->sample_rate = ctx->inputs[0]->sample_rate;
154 outlink->time_base = ctx->inputs[0]->time_base;
156 av_bprint_init(&bp, 0, 1);
157 for (i = 0; i < am->nb_inputs; i++) {
158 av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
159 av_bprint_channel_layout(&bp, -1, ctx->inputs[i]->channel_layout);
161 av_bprintf(&bp, " -> out:");
162 av_bprint_channel_layout(&bp, -1, ctx->outputs[0]->channel_layout);
163 av_log(ctx, AV_LOG_INFO, "%s\n", bp.str);
168 static int request_frame(AVFilterLink *outlink)
170 AVFilterContext *ctx = outlink->src;
171 AMergeContext *am = ctx->priv;
174 for (i = 0; i < am->nb_inputs; i++)
175 if (!am->in[i].nb_samples)
176 if ((ret = avfilter_request_frame(ctx->inputs[i])) < 0)
182 * Copy samples from several input streams to one output stream.
183 * @param nb_inputs number of inputs
184 * @param in inputs; used only for the nb_ch field;
185 * @param route routing values;
186 * input channel i goes to output channel route[i];
187 * i < in[0].nb_ch are the channels from the first output;
188 * i >= in[0].nb_ch are the channels from the second output
189 * @param ins pointer to the samples of each inputs, in packed format;
190 * will be left at the end of the copied samples
191 * @param outs pointer to the samples of the output, in packet format;
192 * must point to a buffer big enough;
193 * will be left at the end of the copied samples
194 * @param ns number of samples to copy
195 * @param bps bytes per sample
197 static inline void copy_samples(int nb_inputs, struct amerge_input in[],
198 int *route, uint8_t *ins[],
199 uint8_t **outs, int ns, int bps)
204 for (i = 0; i < nb_inputs; i++)
205 nb_ch += in[i].nb_ch;
208 for (i = 0; i < nb_inputs; i++) {
209 for (c = 0; c < in[i].nb_ch; c++) {
210 memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
214 *outs += nb_ch * bps;
218 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
220 AVFilterContext *ctx = inlink->dst;
221 AMergeContext *am = ctx->priv;
222 AVFilterLink *const outlink = ctx->outputs[0];
224 int nb_samples, ns, i;
225 AVFilterBufferRef *outbuf, *inbuf[SWR_CH_MAX];
226 uint8_t *ins[SWR_CH_MAX], *outs;
228 for (input_number = 0; input_number < am->nb_inputs; input_number++)
229 if (inlink == ctx->inputs[input_number])
231 av_assert1(input_number < am->nb_inputs);
232 ff_bufqueue_add(ctx, &am->in[input_number].queue, insamples);
233 am->in[input_number].nb_samples += insamples->audio->nb_samples;
234 nb_samples = am->in[0].nb_samples;
235 for (i = 1; i < am->nb_inputs; i++)
236 nb_samples = FFMIN(nb_samples, am->in[i].nb_samples);
240 outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE, nb_samples);
241 outs = outbuf->data[0];
242 for (i = 0; i < am->nb_inputs; i++) {
243 inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
244 ins[i] = inbuf[i]->data[0] +
245 am->in[i].pos * am->in[i].nb_ch * am->bps;
247 outbuf->pts = inbuf[0]->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
249 av_rescale_q(am->in[0].pos,
250 (AVRational){ 1, ctx->inputs[0]->sample_rate },
251 ctx->outputs[0]->time_base);
253 avfilter_copy_buffer_ref_props(outbuf, inbuf[0]);
254 outbuf->audio->nb_samples = nb_samples;
255 outbuf->audio->channel_layout = outlink->channel_layout;
259 for (i = 0; i < am->nb_inputs; i++)
260 ns = FFMIN(ns, inbuf[i]->audio->nb_samples - am->in[i].pos);
261 /* Unroll the most common sample formats: speed +~350% for the loop,
262 +~13% overall (including two common decoders) */
265 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 1);
268 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 2);
271 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 4);
274 copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, am->bps);
279 for (i = 0; i < am->nb_inputs; i++) {
280 am->in[i].nb_samples -= ns;
282 if (am->in[i].pos == inbuf[i]->audio->nb_samples) {
284 avfilter_unref_buffer(inbuf[i]);
285 ff_bufqueue_get(&am->in[i].queue);
286 inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
287 ins[i] = inbuf[i] ? inbuf[i]->data[0] : NULL;
291 ff_filter_samples(ctx->outputs[0], outbuf);
294 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
296 AMergeContext *am = ctx->priv;
300 am->class = &amerge_class;
301 av_opt_set_defaults(am);
302 ret = av_set_options_string(am, args, "=", ":");
304 av_log(ctx, AV_LOG_ERROR, "Error parsing options: '%s'\n", args);
307 am->in = av_calloc(am->nb_inputs, sizeof(*am->in));
309 return AVERROR(ENOMEM);
310 for (i = 0; i < am->nb_inputs; i++) {
313 .type = AVMEDIA_TYPE_AUDIO,
314 .filter_samples = filter_samples,
315 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
317 snprintf(name, sizeof(name), "in%d", i);
318 avfilter_insert_inpad(ctx, i, &pad);
323 AVFilter avfilter_af_amerge = {
325 .description = NULL_IF_CONFIG_SMALL("Merge two audio streams into "
326 "a single multi-channel stream."),
327 .priv_size = sizeof(AMergeContext),
330 .query_formats = query_formats,
332 .inputs = (const AVFilterPad[]) { { .name = NULL } },
333 .outputs = (const AVFilterPad[]) {
335 .type = AVMEDIA_TYPE_AUDIO,
336 .config_props = config_output,
337 .request_frame = request_frame, },