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 "libswresample/swresample.h" // only for SWR_CH_MAX
34 int nb_in_ch[2]; /**< number of channels for each input */
35 int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
38 AVFilterBufferRef *buf[QUEUE_SIZE];
39 int nb_buf, nb_samples, pos;
43 static av_cold void uninit(AVFilterContext *ctx)
45 AMergeContext *am = ctx->priv;
48 for (i = 0; i < 2; i++)
49 for (j = 0; j < am->queue[i].nb_buf; j++)
50 avfilter_unref_buffer(am->queue[i].buf[j]);
53 static int query_formats(AVFilterContext *ctx)
55 AMergeContext *am = ctx->priv;
56 int64_t inlayout[2], outlayout;
57 const int packing_fmts[] = { AVFILTER_PACKED, -1 };
58 AVFilterFormats *formats;
59 AVFilterChannelLayouts *layouts;
62 for (i = 0; i < 2; i++) {
63 if (!ctx->inputs[i]->in_channel_layouts ||
64 !ctx->inputs[i]->in_channel_layouts->nb_channel_layouts) {
65 av_log(ctx, AV_LOG_ERROR,
66 "No channel layout for input %d\n", i + 1);
67 return AVERROR(EINVAL);
69 inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
70 if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
72 av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
73 av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
75 am->nb_in_ch[i] = av_get_channel_layout_nb_channels(inlayout[i]);
77 if (am->nb_in_ch[0] + am->nb_in_ch[1] > SWR_CH_MAX) {
78 av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
79 return AVERROR(EINVAL);
81 if (inlayout[0] & inlayout[1]) {
82 av_log(ctx, AV_LOG_WARNING,
83 "Inputs overlap: output layout will be meaningless\n");
84 for (i = 0; i < am->nb_in_ch[0] + am->nb_in_ch[1]; i++)
86 outlayout = av_get_default_channel_layout(am->nb_in_ch[0] +
89 outlayout = ((int64_t)1 << (am->nb_in_ch[0] + am->nb_in_ch[1])) - 1;
91 int *route[2] = { am->route, am->route + am->nb_in_ch[0] };
92 int c, out_ch_number = 0;
94 outlayout = inlayout[0] | inlayout[1];
95 for (c = 0; c < 64; c++)
96 for (i = 0; i < 2; i++)
97 if ((inlayout[i] >> c) & 1)
98 *(route[i]++) = out_ch_number++;
100 formats = avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO);
101 avfilter_set_common_sample_formats(ctx, formats);
102 formats = avfilter_make_format_list(packing_fmts);
103 avfilter_set_common_packing_formats(ctx, formats);
104 for (i = 0; i < 2; i++) {
106 ff_add_channel_layout(&layouts, inlayout[i]);
107 ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
110 ff_add_channel_layout(&layouts, outlayout);
111 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
115 static int config_output(AVFilterLink *outlink)
117 AVFilterContext *ctx = outlink->src;
118 AMergeContext *am = ctx->priv;
123 if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
124 av_log(ctx, AV_LOG_ERROR,
125 "Inputs must have the same sample rate "
126 "(%"PRIi64" vs %"PRIi64")\n",
127 ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
128 return AVERROR(EINVAL);
130 am->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
131 outlink->sample_rate = ctx->inputs[0]->sample_rate;
132 outlink->time_base = ctx->inputs[0]->time_base;
133 for (i = 0; i < 3; i++) {
134 layout = (i < 2 ? ctx->inputs[i] : ctx->outputs[0])->channel_layout;
135 av_get_channel_layout_string(name[i], sizeof(name[i]), -1, layout);
137 av_log(ctx, AV_LOG_INFO,
138 "in1:%s + in2:%s -> out:%s\n", name[0], name[1], name[2]);
142 static int request_frame(AVFilterLink *outlink)
144 AVFilterContext *ctx = outlink->src;
145 AMergeContext *am = ctx->priv;
148 for (i = 0; i < 2; i++)
149 if (!am->queue[i].nb_samples)
150 if ((ret = avfilter_request_frame(ctx->inputs[i])) < 0)
156 * Copy samples from two input streams to one output stream.
157 * @param nb_in_ch number of channels in each input stream
158 * @param route routing values;
159 * input channel i goes to output channel route[i];
160 * i < nb_in_ch[0] are the channels from the first output;
161 * i >= nb_in_ch[0] are the channels from the second output
162 * @param ins pointer to the samples of each inputs, in packed format;
163 * will be left at the end of the copied samples
164 * @param outs pointer to the samples of the output, in packet format;
165 * must point to a buffer big enough;
166 * will be left at the end of the copied samples
167 * @param ns number of samples to copy
168 * @param bps bytes per sample
170 static inline void copy_samples(int nb_in_ch[2], int *route, uint8_t *ins[2],
171 uint8_t **outs, int ns, int bps)
178 for (i = 0; i < 2; i++) {
179 for (c = 0; c < nb_in_ch[i]; c++) {
180 memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
184 *outs += (nb_in_ch[0] + nb_in_ch[1]) * bps;
188 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
190 AVFilterContext *ctx = inlink->dst;
191 AMergeContext *am = ctx->priv;
192 AVFilterLink *const outlink = ctx->outputs[0];
193 int input_number = inlink == ctx->inputs[1];
194 struct amerge_queue *inq = &am->queue[input_number];
195 int nb_samples, ns, i;
196 AVFilterBufferRef *outbuf, **inbuf[2];
197 uint8_t *ins[2], *outs;
199 if (inq->nb_buf == QUEUE_SIZE) {
200 av_log(ctx, AV_LOG_ERROR, "Packet queue overflow; dropped\n");
201 avfilter_unref_buffer(insamples);
204 inq->buf[inq->nb_buf++] = avfilter_ref_buffer(insamples, AV_PERM_READ |
206 inq->nb_samples += insamples->audio->nb_samples;
207 avfilter_unref_buffer(insamples);
208 if (!am->queue[!input_number].nb_samples)
211 nb_samples = FFMIN(am->queue[0].nb_samples,
212 am->queue[1].nb_samples);
213 outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE,
215 outs = outbuf->data[0];
216 for (i = 0; i < 2; i++) {
217 inbuf[i] = am->queue[i].buf;
218 ins[i] = (*inbuf[i])->data[0] +
219 am->queue[i].pos * am->nb_in_ch[i] * am->bps;
222 avfilter_copy_buffer_ref_props(outbuf, *inbuf[0]);
223 outbuf->audio->nb_samples = nb_samples;
224 outbuf->audio->channel_layout = outlink->channel_layout;
225 outbuf->audio->planar = outlink->planar;
229 for (i = 0; i < 2; i++)
230 ns = FFMIN(ns, (*inbuf[i])->audio->nb_samples - am->queue[i].pos);
231 /* Unroll the most common sample formats: speed +~350% for the loop,
232 +~13% overall (including two common decoders) */
235 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 1);
238 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 2);
241 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 4);
244 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, am->bps);
249 for (i = 0; i < 2; i++) {
250 am->queue[i].nb_samples -= ns;
251 am->queue[i].pos += ns;
252 if (am->queue[i].pos == (*inbuf[i])->audio->nb_samples) {
253 am->queue[i].pos = 0;
254 avfilter_unref_buffer(*inbuf[i]);
257 ins[i] = *inbuf[i] ? (*inbuf[i])->data[0] : NULL;
261 for (i = 0; i < 2; i++) {
262 int nbufused = inbuf[i] - am->queue[i].buf;
264 am->queue[i].nb_buf -= nbufused;
265 memmove(am->queue[i].buf, inbuf[i],
266 am->queue[i].nb_buf * sizeof(**inbuf));
269 ff_filter_samples(ctx->outputs[0], outbuf);
272 AVFilter avfilter_af_amerge = {
274 .description = NULL_IF_CONFIG_SMALL("Merge two audio streams into "
275 "a single multi-channel stream."),
276 .priv_size = sizeof(AMergeContext),
278 .query_formats = query_formats,
280 .inputs = (const AVFilterPad[]) {
282 .type = AVMEDIA_TYPE_AUDIO,
283 .filter_samples = filter_samples,
284 .min_perms = AV_PERM_READ, },
286 .type = AVMEDIA_TYPE_AUDIO,
287 .filter_samples = filter_samples,
288 .min_perms = AV_PERM_READ, },
291 .outputs = (const AVFilterPad[]) {
293 .type = AVMEDIA_TYPE_AUDIO,
294 .config_props = config_output,
295 .request_frame = request_frame, },