]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_amerge.c
avfilter/audio: Remove unused array, move used-only-once array
[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 Lesser 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 "libavutil/avstring.h"
27 #include "libavutil/bprint.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/opt.h"
30 #include "avfilter.h"
31 #include "filters.h"
32 #include "audio.h"
33 #include "internal.h"
34
35 #define SWR_CH_MAX 64
36
37 typedef struct AMergeContext {
38     const AVClass *class;
39     int nb_inputs;
40     int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
41     int bps;
42     struct amerge_input {
43         int nb_ch;         /**< number of channels for the input */
44     } *in;
45 } AMergeContext;
46
47 #define OFFSET(x) offsetof(AMergeContext, x)
48 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49
50 static const AVOption amerge_options[] = {
51     { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
52       AV_OPT_TYPE_INT, { .i64 = 2 }, 1, SWR_CH_MAX, FLAGS },
53     { NULL }
54 };
55
56 AVFILTER_DEFINE_CLASS(amerge);
57
58 static av_cold void uninit(AVFilterContext *ctx)
59 {
60     AMergeContext *s = ctx->priv;
61     int i;
62
63     for (i = 0; i < s->nb_inputs; i++) {
64         if (ctx->input_pads)
65             av_freep(&ctx->input_pads[i].name);
66     }
67     av_freep(&s->in);
68 }
69
70 static int query_formats(AVFilterContext *ctx)
71 {
72     static const enum AVSampleFormat packed_sample_fmts[] = {
73         AV_SAMPLE_FMT_U8,
74         AV_SAMPLE_FMT_S16,
75         AV_SAMPLE_FMT_S32,
76         AV_SAMPLE_FMT_FLT,
77         AV_SAMPLE_FMT_DBL,
78         AV_SAMPLE_FMT_NONE
79     };
80     AMergeContext *s = ctx->priv;
81     int64_t inlayout[SWR_CH_MAX], outlayout = 0;
82     AVFilterFormats *formats;
83     AVFilterChannelLayouts *layouts;
84     int i, ret, overlap = 0, nb_ch = 0;
85
86     for (i = 0; i < s->nb_inputs; i++) {
87         if (!ctx->inputs[i]->in_channel_layouts ||
88             !ctx->inputs[i]->in_channel_layouts->nb_channel_layouts) {
89             av_log(ctx, AV_LOG_WARNING,
90                    "No channel layout for input %d\n", i + 1);
91             return AVERROR(EAGAIN);
92         }
93         inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
94         if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
95             char buf[256];
96             av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
97             av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
98         }
99         s->in[i].nb_ch = FF_LAYOUT2COUNT(inlayout[i]);
100         if (s->in[i].nb_ch) {
101             overlap++;
102         } else {
103             s->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
104             if (outlayout & inlayout[i])
105                 overlap++;
106             outlayout |= inlayout[i];
107         }
108         nb_ch += s->in[i].nb_ch;
109     }
110     if (nb_ch > SWR_CH_MAX) {
111         av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
112         return AVERROR(EINVAL);
113     }
114     if (overlap) {
115         av_log(ctx, AV_LOG_WARNING,
116                "Input channel layouts overlap: "
117                "output layout will be determined by the number of distinct input channels\n");
118         for (i = 0; i < nb_ch; i++)
119             s->route[i] = i;
120         outlayout = av_get_default_channel_layout(nb_ch);
121         if (!outlayout && nb_ch)
122             outlayout = 0xFFFFFFFFFFFFFFFFULL >> (64 - nb_ch);
123     } else {
124         int *route[SWR_CH_MAX];
125         int c, out_ch_number = 0;
126
127         route[0] = s->route;
128         for (i = 1; i < s->nb_inputs; i++)
129             route[i] = route[i - 1] + s->in[i - 1].nb_ch;
130         for (c = 0; c < 64; c++)
131             for (i = 0; i < s->nb_inputs; i++)
132                 if ((inlayout[i] >> c) & 1)
133                     *(route[i]++) = out_ch_number++;
134     }
135     formats = ff_make_format_list(packed_sample_fmts);
136     if ((ret = ff_set_common_formats(ctx, formats)) < 0)
137         return ret;
138     for (i = 0; i < s->nb_inputs; i++) {
139         layouts = NULL;
140         if ((ret = ff_add_channel_layout(&layouts, inlayout[i])) < 0)
141             return ret;
142         if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
143             return ret;
144     }
145     layouts = NULL;
146     if ((ret = ff_add_channel_layout(&layouts, outlayout)) < 0)
147         return ret;
148     if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
149         return ret;
150
151     return ff_set_common_samplerates(ctx, ff_all_samplerates());
152 }
153
154 static int config_output(AVFilterLink *outlink)
155 {
156     AVFilterContext *ctx = outlink->src;
157     AMergeContext *s = ctx->priv;
158     AVBPrint bp;
159     int i;
160
161     for (i = 1; i < s->nb_inputs; i++) {
162         if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
163             av_log(ctx, AV_LOG_ERROR,
164                    "Inputs must have the same sample rate "
165                    "%d for in%d vs %d\n",
166                    ctx->inputs[i]->sample_rate, i, ctx->inputs[0]->sample_rate);
167             return AVERROR(EINVAL);
168         }
169     }
170     s->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
171     outlink->sample_rate = ctx->inputs[0]->sample_rate;
172     outlink->time_base   = ctx->inputs[0]->time_base;
173
174     av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
175     for (i = 0; i < s->nb_inputs; i++) {
176         av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
177         av_bprint_channel_layout(&bp, -1, ctx->inputs[i]->channel_layout);
178     }
179     av_bprintf(&bp, " -> out:");
180     av_bprint_channel_layout(&bp, -1, ctx->outputs[0]->channel_layout);
181     av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
182
183     return 0;
184 }
185
186 /**
187  * Copy samples from several input streams to one output stream.
188  * @param nb_inputs number of inputs
189  * @param in        inputs; used only for the nb_ch field;
190  * @param route     routing values;
191  *                  input channel i goes to output channel route[i];
192  *                  i <  in[0].nb_ch are the channels from the first output;
193  *                  i >= in[0].nb_ch are the channels from the second output
194  * @param ins       pointer to the samples of each inputs, in packed format;
195  *                  will be left at the end of the copied samples
196  * @param outs      pointer to the samples of the output, in packet format;
197  *                  must point to a buffer big enough;
198  *                  will be left at the end of the copied samples
199  * @param ns        number of samples to copy
200  * @param bps       bytes per sample
201  */
202 static inline void copy_samples(int nb_inputs, struct amerge_input in[],
203                                 int *route, uint8_t *ins[],
204                                 uint8_t **outs, int ns, int bps)
205 {
206     int *route_cur;
207     int i, c, nb_ch = 0;
208
209     for (i = 0; i < nb_inputs; i++)
210         nb_ch += in[i].nb_ch;
211     while (ns--) {
212         route_cur = route;
213         for (i = 0; i < nb_inputs; i++) {
214             for (c = 0; c < in[i].nb_ch; c++) {
215                 memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
216                 ins[i] += bps;
217             }
218         }
219         *outs += nb_ch * bps;
220     }
221 }
222
223 static void free_frames(int nb_inputs, AVFrame **input_frames)
224 {
225     int i;
226     for (i = 0; i < nb_inputs; i++)
227         av_frame_free(&input_frames[i]);
228 }
229
230 static int try_push_frame(AVFilterContext *ctx, int nb_samples)
231 {
232     AMergeContext *s = ctx->priv;
233     AVFilterLink *outlink = ctx->outputs[0];
234     int i, ret;
235     AVFrame *outbuf, *inbuf[SWR_CH_MAX] = { NULL };
236     uint8_t *outs, *ins[SWR_CH_MAX];
237
238     for (i = 0; i < ctx->nb_inputs; i++) {
239         ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &inbuf[i]);
240         if (ret < 0) {
241             free_frames(i, inbuf);
242             return ret;
243         }
244         ins[i] = inbuf[i]->data[0];
245     }
246
247     outbuf = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
248     if (!outbuf) {
249         free_frames(s->nb_inputs, inbuf);
250         return AVERROR(ENOMEM);
251     }
252
253     outs = outbuf->data[0];
254     outbuf->pts = inbuf[0]->pts;
255
256     outbuf->nb_samples     = nb_samples;
257     outbuf->channel_layout = outlink->channel_layout;
258     outbuf->channels       = outlink->channels;
259
260     while (nb_samples) {
261         /* Unroll the most common sample formats: speed +~350% for the loop,
262            +~13% overall (including two common decoders) */
263         switch (s->bps) {
264             case 1:
265                 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 1);
266                 break;
267             case 2:
268                 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 2);
269                 break;
270             case 4:
271                 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 4);
272                 break;
273             default:
274                 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, s->bps);
275                 break;
276         }
277
278         nb_samples = 0;
279     }
280
281     free_frames(s->nb_inputs, inbuf);
282     return ff_filter_frame(ctx->outputs[0], outbuf);
283 }
284
285 static int activate(AVFilterContext *ctx)
286 {
287     int i, status;
288     int ret, nb_samples;
289     int64_t pts;
290
291     FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
292
293     nb_samples = ff_inlink_queued_samples(ctx->inputs[0]);
294     for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) {
295         nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[i]), nb_samples);
296     }
297
298     if (nb_samples) {
299         ret = try_push_frame(ctx, nb_samples);
300         if (ret < 0)
301             return ret;
302     }
303
304     for (i = 0; i < ctx->nb_inputs; i++) {
305         if (ff_inlink_queued_samples(ctx->inputs[i]))
306             continue;
307
308         if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
309             ff_outlink_set_status(ctx->outputs[0], status, pts);
310             return 0;
311         } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
312             ff_inlink_request_frame(ctx->inputs[i]);
313             return 0;
314         }
315     }
316
317     return 0;
318 }
319
320 static av_cold int init(AVFilterContext *ctx)
321 {
322     AMergeContext *s = ctx->priv;
323     int i, ret;
324
325     s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
326     if (!s->in)
327         return AVERROR(ENOMEM);
328     for (i = 0; i < s->nb_inputs; i++) {
329         char *name = av_asprintf("in%d", i);
330         AVFilterPad pad = {
331             .name             = name,
332             .type             = AVMEDIA_TYPE_AUDIO,
333         };
334         if (!name)
335             return AVERROR(ENOMEM);
336         if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
337             av_freep(&pad.name);
338             return ret;
339         }
340     }
341     return 0;
342 }
343
344 static const AVFilterPad amerge_outputs[] = {
345     {
346         .name          = "default",
347         .type          = AVMEDIA_TYPE_AUDIO,
348         .config_props  = config_output,
349     },
350     { NULL }
351 };
352
353 AVFilter ff_af_amerge = {
354     .name          = "amerge",
355     .description   = NULL_IF_CONFIG_SMALL("Merge two or more audio streams into "
356                                           "a single multi-channel stream."),
357     .priv_size     = sizeof(AMergeContext),
358     .init          = init,
359     .uninit        = uninit,
360     .query_formats = query_formats,
361     .activate      = activate,
362     .inputs        = NULL,
363     .outputs       = amerge_outputs,
364     .priv_class    = &amerge_class,
365     .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
366 };