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 AVFilterFormats *formats;
58 AVFilterChannelLayouts *layouts;
61 for (i = 0; i < 2; i++) {
62 if (!ctx->inputs[i]->in_channel_layouts ||
63 !ctx->inputs[i]->in_channel_layouts->nb_channel_layouts) {
64 av_log(ctx, AV_LOG_ERROR,
65 "No channel layout for input %d\n", i + 1);
66 return AVERROR(EINVAL);
68 inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
69 if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
71 av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
72 av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
74 am->nb_in_ch[i] = av_get_channel_layout_nb_channels(inlayout[i]);
76 if (am->nb_in_ch[0] + am->nb_in_ch[1] > SWR_CH_MAX) {
77 av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
78 return AVERROR(EINVAL);
80 if (inlayout[0] & inlayout[1]) {
81 av_log(ctx, AV_LOG_WARNING,
82 "Inputs overlap: output layout will be meaningless\n");
83 for (i = 0; i < am->nb_in_ch[0] + am->nb_in_ch[1]; i++)
85 outlayout = av_get_default_channel_layout(am->nb_in_ch[0] +
88 outlayout = ((int64_t)1 << (am->nb_in_ch[0] + am->nb_in_ch[1])) - 1;
90 int *route[2] = { am->route, am->route + am->nb_in_ch[0] };
91 int c, out_ch_number = 0;
93 outlayout = inlayout[0] | inlayout[1];
94 for (c = 0; c < 64; c++)
95 for (i = 0; i < 2; i++)
96 if ((inlayout[i] >> c) & 1)
97 *(route[i]++) = out_ch_number++;
99 formats = avfilter_make_format_list(ff_packed_sample_fmts);
100 avfilter_set_common_sample_formats(ctx, formats);
101 for (i = 0; i < 2; i++) {
103 ff_add_channel_layout(&layouts, inlayout[i]);
104 ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
107 ff_add_channel_layout(&layouts, outlayout);
108 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
112 static int config_output(AVFilterLink *outlink)
114 AVFilterContext *ctx = outlink->src;
115 AMergeContext *am = ctx->priv;
120 if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
121 av_log(ctx, AV_LOG_ERROR,
122 "Inputs must have the same sample rate "
123 "(%"PRIi64" vs %"PRIi64")\n",
124 ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
125 return AVERROR(EINVAL);
127 am->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
128 outlink->sample_rate = ctx->inputs[0]->sample_rate;
129 outlink->time_base = ctx->inputs[0]->time_base;
130 for (i = 0; i < 3; i++) {
131 layout = (i < 2 ? ctx->inputs[i] : ctx->outputs[0])->channel_layout;
132 av_get_channel_layout_string(name[i], sizeof(name[i]), -1, layout);
134 av_log(ctx, AV_LOG_INFO,
135 "in1:%s + in2:%s -> out:%s\n", name[0], name[1], name[2]);
139 static int request_frame(AVFilterLink *outlink)
141 AVFilterContext *ctx = outlink->src;
142 AMergeContext *am = ctx->priv;
145 for (i = 0; i < 2; i++)
146 if (!am->queue[i].nb_samples)
147 if ((ret = avfilter_request_frame(ctx->inputs[i])) < 0)
153 * Copy samples from two input streams to one output stream.
154 * @param nb_in_ch number of channels in each input stream
155 * @param route routing values;
156 * input channel i goes to output channel route[i];
157 * i < nb_in_ch[0] are the channels from the first output;
158 * i >= nb_in_ch[0] are the channels from the second output
159 * @param ins pointer to the samples of each inputs, in packed format;
160 * will be left at the end of the copied samples
161 * @param outs pointer to the samples of the output, in packet format;
162 * must point to a buffer big enough;
163 * will be left at the end of the copied samples
164 * @param ns number of samples to copy
165 * @param bps bytes per sample
167 static inline void copy_samples(int nb_in_ch[2], int *route, uint8_t *ins[2],
168 uint8_t **outs, int ns, int bps)
175 for (i = 0; i < 2; i++) {
176 for (c = 0; c < nb_in_ch[i]; c++) {
177 memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
181 *outs += (nb_in_ch[0] + nb_in_ch[1]) * bps;
185 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
187 AVFilterContext *ctx = inlink->dst;
188 AMergeContext *am = ctx->priv;
189 AVFilterLink *const outlink = ctx->outputs[0];
190 int input_number = inlink == ctx->inputs[1];
191 struct amerge_queue *inq = &am->queue[input_number];
192 int nb_samples, ns, i;
193 AVFilterBufferRef *outbuf, **inbuf[2];
194 uint8_t *ins[2], *outs;
196 if (inq->nb_buf == QUEUE_SIZE) {
197 av_log(ctx, AV_LOG_ERROR, "Packet queue overflow; dropped\n");
198 avfilter_unref_buffer(insamples);
201 inq->buf[inq->nb_buf++] = avfilter_ref_buffer(insamples, AV_PERM_READ |
203 inq->nb_samples += insamples->audio->nb_samples;
204 avfilter_unref_buffer(insamples);
205 if (!am->queue[!input_number].nb_samples)
208 nb_samples = FFMIN(am->queue[0].nb_samples,
209 am->queue[1].nb_samples);
210 outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE,
212 outs = outbuf->data[0];
213 for (i = 0; i < 2; i++) {
214 inbuf[i] = am->queue[i].buf;
215 ins[i] = (*inbuf[i])->data[0] +
216 am->queue[i].pos * am->nb_in_ch[i] * am->bps;
219 avfilter_copy_buffer_ref_props(outbuf, *inbuf[0]);
220 outbuf->audio->nb_samples = nb_samples;
221 outbuf->audio->channel_layout = outlink->channel_layout;
225 for (i = 0; i < 2; i++)
226 ns = FFMIN(ns, (*inbuf[i])->audio->nb_samples - am->queue[i].pos);
227 /* Unroll the most common sample formats: speed +~350% for the loop,
228 +~13% overall (including two common decoders) */
231 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 1);
234 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 2);
237 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 4);
240 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, am->bps);
245 for (i = 0; i < 2; i++) {
246 am->queue[i].nb_samples -= ns;
247 am->queue[i].pos += ns;
248 if (am->queue[i].pos == (*inbuf[i])->audio->nb_samples) {
249 am->queue[i].pos = 0;
250 avfilter_unref_buffer(*inbuf[i]);
253 ins[i] = *inbuf[i] ? (*inbuf[i])->data[0] : NULL;
257 for (i = 0; i < 2; i++) {
258 int nbufused = inbuf[i] - am->queue[i].buf;
260 am->queue[i].nb_buf -= nbufused;
261 memmove(am->queue[i].buf, inbuf[i],
262 am->queue[i].nb_buf * sizeof(**inbuf));
265 ff_filter_samples(ctx->outputs[0], outbuf);
268 AVFilter avfilter_af_amerge = {
270 .description = NULL_IF_CONFIG_SMALL("Merge two audio streams into "
271 "a single multi-channel stream."),
272 .priv_size = sizeof(AMergeContext),
274 .query_formats = query_formats,
276 .inputs = (const AVFilterPad[]) {
278 .type = AVMEDIA_TYPE_AUDIO,
279 .filter_samples = filter_samples,
280 .min_perms = AV_PERM_READ, },
282 .type = AVMEDIA_TYPE_AUDIO,
283 .filter_samples = filter_samples,
284 .min_perms = AV_PERM_READ, },
287 .outputs = (const AVFilterPad[]) {
289 .type = AVMEDIA_TYPE_AUDIO,
290 .config_props = config_output,
291 .request_frame = request_frame, },