FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_amerge.c
Date: 2024-04-18 20:30:25
Exec Total Coverage
Lines: 120 158 75.9%
Functions: 8 8 100.0%
Branches: 71 106 67.0%

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