FFmpeg coverage


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