FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_amerge.c
Date: 2026-04-28 04:29:18
Exec Total Coverage
Lines: 144 179 80.4%
Functions: 8 8 100.0%
Branches: 84 112 75.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/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/bprint.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 #include "avfilter.h"
33 #include "filters.h"
34 #include "audio.h"
35 #include "formats.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 int layout_mode; /**< the method for determining the output channel layout */
48 } AMergeContext;
49
50 #define OFFSET(x) offsetof(AMergeContext, x)
51 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52
53 enum LayoutModes {
54 LM_LEGACY,
55 LM_RESET,
56 LM_NORMAL,
57 NB_LAYOUTMODES
58 };
59
60 static const AVOption amerge_options[] = {
61 { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
62 AV_OPT_TYPE_INT, { .i64 = 2 }, 1, SWR_CH_MAX, FLAGS },
63 { "layout_mode", "method used to determine the output channel layout", OFFSET(layout_mode),
64 AV_OPT_TYPE_INT, { .i64 = LM_LEGACY }, 0, NB_LAYOUTMODES - 1, FLAGS, .unit = "layout_mode"},
65 { "legacy", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = LM_LEGACY }, 0, 0, FLAGS, .unit = "layout_mode" },
66 { "reset", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = LM_RESET }, 0, 0, FLAGS, .unit = "layout_mode" },
67 { "normal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = LM_NORMAL }, 0, 0, FLAGS, .unit = "layout_mode" },
68 { NULL }
69 };
70
71 AVFILTER_DEFINE_CLASS(amerge);
72
73 8 static av_cold void uninit(AVFilterContext *ctx)
74 {
75 8 AMergeContext *s = ctx->priv;
76
77 8 av_freep(&s->in);
78 8 }
79
80 #define INLAYOUT(ctx, i) (&(ctx)->inputs[i]->incfg.channel_layouts->channel_layouts[0])
81
82 8 static int query_formats(AVFilterContext *ctx)
83 {
84 static const enum AVSampleFormat packed_sample_fmts[] = {
85 AV_SAMPLE_FMT_U8,
86 AV_SAMPLE_FMT_S16,
87 AV_SAMPLE_FMT_S32,
88 AV_SAMPLE_FMT_FLT,
89 AV_SAMPLE_FMT_DBL,
90 AV_SAMPLE_FMT_NONE
91 };
92 8 AMergeContext *s = ctx->priv;
93 8 AVChannelLayout outlayout = { 0 };
94 8 uint64_t outmask = 0;
95 AVFilterChannelLayouts *layouts;
96 8 int i, ret, nb_ch = 0;
97 8 int native_layout_routes[SWR_CH_MAX] = { 0 };
98
99
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 4 times.
18 for (i = 0; i < s->nb_inputs; i++) {
100
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
14 if (!ctx->inputs[i]->incfg.channel_layouts ||
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 !ctx->inputs[i]->incfg.channel_layouts->nb_channel_layouts) {
102 4 av_log(ctx, AV_LOG_WARNING,
103 "No channel layout for input %d\n", i + 1);
104 4 return AVERROR(EAGAIN);
105 }
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ctx->inputs[i]->incfg.channel_layouts->nb_channel_layouts > 1) {
107 char buf[256];
108 av_channel_layout_describe(INLAYOUT(ctx, i), buf, sizeof(buf));
109 av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
110 }
111 10 s->in[i].nb_ch = INLAYOUT(ctx, i)->nb_channels;
112 10 nb_ch += s->in[i].nb_ch;
113 }
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (nb_ch > SWR_CH_MAX) {
115 av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
116 return AVERROR(EINVAL);
117 }
118 4 ret = av_channel_layout_custom_init(&outlayout, nb_ch);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
120 return ret;
121
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
14 for (int i = 0, ch_idx = 0; i < s->nb_inputs; i++) {
122
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 10 times.
22 for (int j = 0; j < s->in[i].nb_ch; j++) {
123 12 enum AVChannel id = av_channel_layout_channel_from_index(INLAYOUT(ctx, i), j);
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (INLAYOUT(ctx, i)->order == AV_CHANNEL_ORDER_CUSTOM)
125 outlayout.u.map[ch_idx] = INLAYOUT(ctx, i)->u.map[j];
126 else
127
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
12 outlayout.u.map[ch_idx].id = (id == AV_CHAN_NONE ? AV_CHAN_UNKNOWN : id);
128
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
12 if (id >= 0 && id < 64) {
129 10 outmask |= (1ULL << id);
130 10 native_layout_routes[id] = ch_idx;
131 }
132 12 s->route[ch_idx] = ch_idx;
133 12 ch_idx++;
134 }
135 }
136
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
4 switch (s->layout_mode) {
137 2 case LM_LEGACY:
138 2 av_channel_layout_uninit(&outlayout);
139
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (av_popcount64(outmask) != nb_ch) {
140 1 av_log(ctx, AV_LOG_WARNING,
141 "Input channel layouts overlap: "
142 "output layout will be determined by the number of distinct input channels\n");
143 1 av_channel_layout_default(&outlayout, nb_ch);
144
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)
145 av_channel_layout_from_mask(&outlayout, 0xFFFFFFFFFFFFFFFFULL >> (64 - nb_ch));
146 } else {
147
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 1 times.
65 for (int c = 0, ch_idx = 0; c < 64; c++)
148
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 62 times.
64 if ((1ULL << c) & outmask)
149 2 s->route[native_layout_routes[c]] = ch_idx++;
150 1 av_channel_layout_from_mask(&outlayout, outmask);
151 }
152 2 break;
153 1 case LM_RESET:
154 1 av_channel_layout_uninit(&outlayout);
155 1 outlayout.order = AV_CHANNEL_ORDER_UNSPEC;
156 1 outlayout.nb_channels = nb_ch;
157 1 break;
158 1 case LM_NORMAL:
159 1 ret = av_channel_layout_retype(&outlayout, 0, AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL);
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
161 goto out;
162 1 break;
163 default:
164 av_unreachable("Invalid layout_mode");
165 }
166
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if ((ret = ff_set_sample_formats_from_list(ctx, packed_sample_fmts)) < 0)
167 goto out;
168
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
14 for (i = 0; i < s->nb_inputs; i++) {
169 10 layouts = NULL;
170
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_add_channel_layout(&layouts, INLAYOUT(ctx, i))) < 0)
171 goto out;
172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->outcfg.channel_layouts)) < 0)
173 goto out;
174 }
175 4 layouts = NULL;
176
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if ((ret = ff_add_channel_layout(&layouts, &outlayout)) < 0)
177 goto out;
178
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->incfg.channel_layouts)) < 0)
179 goto out;
180
181 4 ret = ff_set_common_all_samplerates(ctx);
182 4 out:
183 4 av_channel_layout_uninit(&outlayout);
184 4 return ret;
185 }
186
187 4 static int config_output(AVFilterLink *outlink)
188 {
189 4 AVFilterContext *ctx = outlink->src;
190 4 AMergeContext *s = ctx->priv;
191 AVBPrint bp;
192 int i;
193
194 4 s->bps = av_get_bytes_per_sample(outlink->format);
195 4 outlink->time_base = ctx->inputs[0]->time_base;
196
197 4 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
198
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
14 for (i = 0; i < s->nb_inputs; i++) {
199
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
10 av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
200 10 av_channel_layout_describe_bprint(&ctx->inputs[i]->ch_layout, &bp);
201 }
202 4 av_bprintf(&bp, " -> out:");
203 4 av_channel_layout_describe_bprint(&outlink->ch_layout, &bp);
204 4 av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
205
206 4 return 0;
207 }
208
209 /**
210 * Copy samples from several input streams to one output stream.
211 * @param nb_inputs number of inputs
212 * @param in inputs; used only for the nb_ch field;
213 * @param route routing values;
214 * input channel i goes to output channel route[i];
215 * i < in[0].nb_ch are the channels from the first output;
216 * i >= in[0].nb_ch are the channels from the second output
217 * @param ins pointer to the samples of each inputs, in packed format;
218 * will be left at the end of the copied samples
219 * @param outs pointer to the samples of the output, in packet format;
220 * must point to a buffer big enough;
221 * will be left at the end of the copied samples
222 * @param ns number of samples to copy
223 * @param bps bytes per sample
224 */
225 248 static inline void copy_samples(int nb_inputs, struct amerge_input in[],
226 int *route, uint8_t *ins[],
227 uint8_t **outs, int ns, int bps)
228 {
229 int *route_cur;
230 248 int i, c, nb_ch = 0;
231
232
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 248 times.
864 for (i = 0; i < nb_inputs; i++)
233 616 nb_ch += in[i].nb_ch;
234
2/2
✓ Branch 0 taken 1010958 times.
✓ Branch 1 taken 248 times.
1011206 while (ns--) {
235 1010958 route_cur = route;
236
2/2
✓ Branch 0 taken 2507016 times.
✓ Branch 1 taken 1010958 times.
3517974 for (i = 0; i < nb_inputs; i++) {
237
2/2
✓ Branch 0 taken 2992116 times.
✓ Branch 1 taken 2507016 times.
5499132 for (c = 0; c < in[i].nb_ch; c++) {
238 2992116 memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
239 2992116 ins[i] += bps;
240 }
241 }
242 1010958 *outs += nb_ch * bps;
243 }
244 248 }
245
246 248 static void free_frames(int nb_inputs, AVFrame **input_frames)
247 {
248 int i;
249
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 248 times.
864 for (i = 0; i < nb_inputs; i++)
250 616 av_frame_free(&input_frames[i]);
251 248 }
252
253 248 static int try_push_frame(AVFilterContext *ctx, int nb_samples)
254 {
255 248 AMergeContext *s = ctx->priv;
256 248 AVFilterLink *outlink = ctx->outputs[0];
257 int i, ret;
258 248 AVFrame *outbuf, *inbuf[SWR_CH_MAX] = { NULL };
259 uint8_t *outs, *ins[SWR_CH_MAX];
260
261
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 248 times.
864 for (i = 0; i < ctx->nb_inputs; i++) {
262 616 ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &inbuf[i]);
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 if (ret < 0) {
264 free_frames(i, inbuf);
265 return ret;
266 }
267 616 ins[i] = inbuf[i]->data[0];
268 }
269
270 248 outbuf = ff_get_audio_buffer(outlink, nb_samples);
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 if (!outbuf) {
272 free_frames(s->nb_inputs, inbuf);
273 return AVERROR(ENOMEM);
274 }
275
276 248 outs = outbuf->data[0];
277 248 outbuf->pts = inbuf[0]->pts;
278
279 248 outbuf->nb_samples = nb_samples;
280 248 outbuf->duration = av_rescale_q(outbuf->nb_samples,
281 av_make_q(1, outlink->sample_rate),
282 outlink->time_base);
283
284
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 248 times.
248 if ((ret = av_channel_layout_copy(&outbuf->ch_layout, &outlink->ch_layout)) < 0) {
285 free_frames(s->nb_inputs, inbuf);
286 av_frame_free(&outbuf);
287 return ret;
288 }
289
290
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 248 times.
496 while (nb_samples) {
291 /* Unroll the most common sample formats: speed +~350% for the loop,
292 +~13% overall (including two common decoders) */
293
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
248 switch (s->bps) {
294 case 1:
295 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 1);
296 break;
297 248 case 2:
298 248 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 2);
299 248 break;
300 case 4:
301 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 4);
302 break;
303 default:
304 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, s->bps);
305 break;
306 }
307
308 248 nb_samples = 0;
309 }
310
311 248 free_frames(s->nb_inputs, inbuf);
312 248 return ff_filter_frame(outlink, outbuf);
313 }
314
315 1444 static int activate(AVFilterContext *ctx)
316 {
317 int i, status;
318 int ret, nb_samples;
319 int64_t pts;
320
321
4/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1440 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 4 times.
1454 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
322
323 1440 nb_samples = ff_inlink_queued_samples(ctx->inputs[0]);
324
4/4
✓ Branch 0 taken 2160 times.
✓ Branch 1 taken 679 times.
✓ Branch 2 taken 1399 times.
✓ Branch 3 taken 761 times.
2839 for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) {
325
2/2
✓ Branch 1 taken 1201 times.
✓ Branch 2 taken 198 times.
1399 nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[i]), nb_samples);
326 }
327
328
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 1192 times.
1440 if (nb_samples) {
329 248 ret = try_push_frame(ctx, nb_samples);
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 if (ret < 0)
331 return ret;
332 }
333
334
2/2
✓ Branch 0 taken 2890 times.
✓ Branch 1 taken 347 times.
3237 for (i = 0; i < ctx->nb_inputs; i++) {
335
2/2
✓ Branch 1 taken 1281 times.
✓ Branch 2 taken 1609 times.
2890 if (ff_inlink_queued_samples(ctx->inputs[i]))
336 1281 continue;
337
338
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1607 times.
1609 if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
339 2 ff_outlink_set_status(ctx->outputs[0], status, pts);
340 2 return 0;
341
2/2
✓ Branch 1 taken 1091 times.
✓ Branch 2 taken 516 times.
1607 } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
342 1091 ff_inlink_request_frame(ctx->inputs[i]);
343 1091 return 0;
344 }
345 }
346
347 347 return 0;
348 }
349
350 8 static av_cold int init(AVFilterContext *ctx)
351 {
352 8 AMergeContext *s = ctx->priv;
353 int i, ret;
354
355 8 s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!s->in)
357 return AVERROR(ENOMEM);
358
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8 times.
28 for (i = 0; i < s->nb_inputs; i++) {
359 20 char *name = av_asprintf("in%d", i);
360 20 AVFilterPad pad = {
361 .name = name,
362 .type = AVMEDIA_TYPE_AUDIO,
363 };
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!name)
365 return AVERROR(ENOMEM);
366
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0)
367 return ret;
368 }
369 8 return 0;
370 }
371
372 static const AVFilterPad amerge_outputs[] = {
373 {
374 .name = "default",
375 .type = AVMEDIA_TYPE_AUDIO,
376 .config_props = config_output,
377 },
378 };
379
380 const FFFilter ff_af_amerge = {
381 .p.name = "amerge",
382 .p.description = NULL_IF_CONFIG_SMALL("Merge two or more audio streams into "
383 "a single multi-channel stream."),
384 .p.inputs = NULL,
385 .p.priv_class = &amerge_class,
386 .p.flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
387 .priv_size = sizeof(AMergeContext),
388 .init = init,
389 .uninit = uninit,
390 .activate = activate,
391 FILTER_OUTPUTS(amerge_outputs),
392 FILTER_QUERY_FUNC(query_formats),
393 };
394