]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_amerge.c
Merge commit '755cd4197d53946208e042f095b930dca18d9430'
[ffmpeg] / libavfilter / af_amerge.c
1 /*
2  * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * Audio merging filter
24  */
25
26 #include "libswresample/swresample.h" // only for SWR_CH_MAX
27 #include "avfilter.h"
28 #include "audio.h"
29 #include "internal.h"
30
31 #define QUEUE_SIZE 16
32
33 typedef struct {
34     int nb_in_ch[2];       /**< number of channels for each input */
35     int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
36     int bps;
37     struct amerge_queue {
38         AVFilterBufferRef *buf[QUEUE_SIZE];
39         int nb_buf, nb_samples, pos;
40     } queue[2];
41 } AMergeContext;
42
43 static av_cold void uninit(AVFilterContext *ctx)
44 {
45     AMergeContext *am = ctx->priv;
46     int i, j;
47
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]);
51 }
52
53 static int query_formats(AVFilterContext *ctx)
54 {
55     AMergeContext *am = ctx->priv;
56     int64_t inlayout[2], outlayout;
57     AVFilterFormats *formats;
58     AVFilterChannelLayouts *layouts;
59     int i;
60
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);
67         }
68         inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
69         if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
70             char buf[256];
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);
73         }
74         am->nb_in_ch[i] = av_get_channel_layout_nb_channels(inlayout[i]);
75     }
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);
79     }
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++)
84             am->route[i] = i;
85         outlayout = av_get_default_channel_layout(am->nb_in_ch[0] +
86                                                   am->nb_in_ch[1]);
87         if (!outlayout)
88             outlayout = ((int64_t)1 << (am->nb_in_ch[0] + am->nb_in_ch[1])) - 1;
89     } else {
90         int *route[2] = { am->route, am->route + am->nb_in_ch[0] };
91         int c, out_ch_number = 0;
92
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++;
98     }
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++) {
102         layouts = NULL;
103         ff_add_channel_layout(&layouts, inlayout[i]);
104         ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
105     }
106     layouts = NULL;
107     ff_add_channel_layout(&layouts, outlayout);
108     ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
109     return 0;
110 }
111
112 static int config_output(AVFilterLink *outlink)
113 {
114     AVFilterContext *ctx = outlink->src;
115     AMergeContext *am = ctx->priv;
116     int64_t layout;
117     char name[3][256];
118     int i;
119
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);
126     }
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);
133     }
134     av_log(ctx, AV_LOG_INFO,
135            "in1:%s + in2:%s -> out:%s\n", name[0], name[1], name[2]);
136     return 0;
137 }
138
139 static int request_frame(AVFilterLink *outlink)
140 {
141     AVFilterContext *ctx = outlink->src;
142     AMergeContext *am = ctx->priv;
143     int i, ret;
144
145     for (i = 0; i < 2; i++)
146         if (!am->queue[i].nb_samples)
147             if ((ret = avfilter_request_frame(ctx->inputs[i])) < 0)
148                 return ret;
149     return 0;
150 }
151
152 /**
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
166  */
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)
169 {
170     int *route_cur;
171     int i, c;
172
173     while (ns--) {
174         route_cur = route;
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);
178                 ins[i] += bps;
179             }
180         }
181         *outs += (nb_in_ch[0] + nb_in_ch[1]) * bps;
182     }
183 }
184
185 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
186 {
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;
195
196     if (inq->nb_buf == QUEUE_SIZE) {
197         av_log(ctx, AV_LOG_ERROR, "Packet queue overflow; dropped\n");
198         avfilter_unref_buffer(insamples);
199         return;
200     }
201     inq->buf[inq->nb_buf++] = avfilter_ref_buffer(insamples, AV_PERM_READ |
202                                                              AV_PERM_PRESERVE);
203     inq->nb_samples += insamples->audio->nb_samples;
204     avfilter_unref_buffer(insamples);
205     if (!am->queue[!input_number].nb_samples)
206         return;
207
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,
211                                        nb_samples);
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;
217     }
218
219     avfilter_copy_buffer_ref_props(outbuf, *inbuf[0]);
220     outbuf->audio->nb_samples     = nb_samples;
221     outbuf->audio->channel_layout = outlink->channel_layout;
222
223     while (nb_samples) {
224         ns = nb_samples;
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) */
229         switch (am->bps) {
230             case 1:
231                 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 1);
232                 break;
233             case 2:
234                 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 2);
235                 break;
236             case 4:
237                 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, 4);
238                 break;
239             default:
240                 copy_samples(am->nb_in_ch, am->route, ins, &outs, ns, am->bps);
241                 break;
242         }
243
244         nb_samples -= ns;
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]);
251                 *inbuf[i] = NULL;
252                 inbuf[i]++;
253                 ins[i] = *inbuf[i] ? (*inbuf[i])->data[0] : NULL;
254             }
255         }
256     }
257     for (i = 0; i < 2; i++) {
258         int nbufused = inbuf[i] - am->queue[i].buf;
259         if (nbufused) {
260             am->queue[i].nb_buf -= nbufused;
261             memmove(am->queue[i].buf, inbuf[i],
262                     am->queue[i].nb_buf * sizeof(**inbuf));
263         }
264     }
265     ff_filter_samples(ctx->outputs[0], outbuf);
266 }
267
268 AVFilter avfilter_af_amerge = {
269     .name          = "amerge",
270     .description   = NULL_IF_CONFIG_SMALL("Merge two audio streams into "
271                                           "a single multi-channel stream."),
272     .priv_size     = sizeof(AMergeContext),
273     .uninit        = uninit,
274     .query_formats = query_formats,
275
276     .inputs    = (const AVFilterPad[]) {
277         { .name             = "in1",
278           .type             = AVMEDIA_TYPE_AUDIO,
279           .filter_samples   = filter_samples,
280           .min_perms        = AV_PERM_READ, },
281         { .name             = "in2",
282           .type             = AVMEDIA_TYPE_AUDIO,
283           .filter_samples   = filter_samples,
284           .min_perms        = AV_PERM_READ, },
285         { .name = NULL }
286     },
287     .outputs   = (const AVFilterPad[]) {
288         { .name             = "default",
289           .type             = AVMEDIA_TYPE_AUDIO,
290           .config_props     = config_output,
291           .request_frame    = request_frame, },
292         { .name = NULL }
293     },
294 };