FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_pan.c
Date: 2022-12-06 04:36:11
Exec Total Coverage
Lines: 163 213 76.5%
Functions: 8 8 100.0%
Branches: 93 126 73.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2002 Anders Johansson <ajh@atri.curtin.edu.au>
3 * Copyright (c) 2011 Clément Bœsch <u pkh me>
4 * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Audio panning filter (channels mixing)
26 * Original code written by Anders Johansson for MPlayer,
27 * reimplemented for FFmpeg.
28 */
29
30 #include <stdio.h>
31 #include "libavutil/avstring.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/opt.h"
34 #include "libswresample/swresample.h"
35 #include "audio.h"
36 #include "avfilter.h"
37 #include "formats.h"
38 #include "internal.h"
39
40 #define MAX_CHANNELS 64
41
42 typedef struct PanContext {
43 const AVClass *class;
44 char *args;
45 AVChannelLayout out_channel_layout;
46 double gain[MAX_CHANNELS][MAX_CHANNELS];
47 int64_t need_renorm;
48 int need_renumber;
49 int nb_output_channels;
50
51 int pure_gains;
52 /* channel mapping specific */
53 int channel_map[MAX_CHANNELS];
54 struct SwrContext *swr;
55 } PanContext;
56
57 270 static void skip_spaces(char **arg)
58 {
59 270 int len = 0;
60
61 270 sscanf(*arg, " %n", &len);
62 270 *arg += len;
63 270 }
64
65 135 static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
66 {
67 char buf[8];
68 135 int len, channel_id = 0;
69
70 135 skip_spaces(arg);
71 /* try to parse a channel name, e.g. "FL" */
72
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 117 times.
135 if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
73 18 channel_id = av_channel_from_string(buf);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (channel_id < 0)
75 return channel_id;
76
77 18 *rchannel = channel_id;
78 18 *rnamed = 1;
79 18 *arg += len;
80 18 return 0;
81 }
82 /* try to parse a channel number, e.g. "c2" */
83
1/2
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
117 if (sscanf(*arg, "c%d%n", &channel_id, &len) &&
84
2/4
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 117 times.
✗ Branch 3 not taken.
117 channel_id >= 0 && channel_id < MAX_CHANNELS) {
85 117 *rchannel = channel_id;
86 117 *rnamed = 0;
87 117 *arg += len;
88 117 return 0;
89 }
90 return AVERROR(EINVAL);
91 }
92
93 32 static av_cold int init(AVFilterContext *ctx)
94 {
95 32 PanContext *const pan = ctx->priv;
96 32 char *arg, *arg0, *tokenizer, *args = av_strdup(pan->args);
97 32 int out_ch_id, in_ch_id, len, named, ret, sign = 1;
98 32 int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
99 32 int used_out_ch[MAX_CHANNELS] = {0};
100 double gain;
101
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!pan->args) {
103 av_log(ctx, AV_LOG_ERROR,
104 "pan filter needs a channel layout and a set "
105 "of channel definitions as parameter\n");
106 return AVERROR(EINVAL);
107 }
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!args)
109 return AVERROR(ENOMEM);
110 32 arg = av_strtok(args, "|", &tokenizer);
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!arg) {
112 av_log(ctx, AV_LOG_ERROR, "Channel layout not specified\n");
113 ret = AVERROR(EINVAL);
114 goto fail;
115 }
116 32 ret = ff_parse_channel_layout(&pan->out_channel_layout,
117 &pan->nb_output_channels, arg, ctx);
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
119 goto fail;
120
121 /* parse channel specifications */
122
2/2
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 32 times.
87 while ((arg = arg0 = av_strtok(NULL, "|", &tokenizer))) {
123 55 int used_in_ch[MAX_CHANNELS] = {0};
124 /* channel name */
125
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
55 if (parse_channel_name(&arg, &out_ch_id, &named)) {
126 av_log(ctx, AV_LOG_ERROR,
127 "Expected out channel name, got \"%.8s\"\n", arg);
128 ret = AVERROR(EINVAL);
129 goto fail;
130 }
131
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 48 times.
55 if (named) {
132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if ((out_ch_id = av_channel_layout_index_from_channel(&pan->out_channel_layout, out_ch_id)) < 0) {
133 av_log(ctx, AV_LOG_ERROR,
134 "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
135 ret = AVERROR(EINVAL);
136 goto fail;
137 }
138 }
139
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 55 times.
55 if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
140 av_log(ctx, AV_LOG_ERROR,
141 "Invalid out channel name \"%.8s\"\n", arg0);
142 ret = AVERROR(EINVAL);
143 goto fail;
144 }
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (used_out_ch[out_ch_id]) {
146 av_log(ctx, AV_LOG_ERROR,
147 "Can not reference out channel %d twice\n", out_ch_id);
148 ret = AVERROR(EINVAL);
149 goto fail;
150 }
151 55 used_out_ch[out_ch_id] = 1;
152 55 skip_spaces(&arg);
153
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 4 times.
55 if (*arg == '=') {
154 51 arg++;
155
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 } else if (*arg == '<') {
156 4 pan->need_renorm |= (int64_t)1 << out_ch_id;
157 4 arg++;
158 } else {
159 av_log(ctx, AV_LOG_ERROR,
160 "Syntax error after channel name in \"%.8s\"\n", arg0);
161 ret = AVERROR(EINVAL);
162 goto fail;
163 }
164 /* gains */
165 55 sign = 1;
166 while (1) {
167 80 gain = 1;
168
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 62 times.
80 if (sscanf(arg, "%lf%n *%n", &gain, &len, &len))
169 18 arg += len;
170
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
80 if (parse_channel_name(&arg, &in_ch_id, &named)){
171 av_log(ctx, AV_LOG_ERROR,
172 "Expected in channel name, got \"%.8s\"\n", arg);
173 ret = AVERROR(EINVAL);
174 goto fail;
175 }
176 80 nb_in_channels[named]++;
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (nb_in_channels[!named]) {
178 av_log(ctx, AV_LOG_ERROR,
179 "Can not mix named and numbered channels\n");
180 ret = AVERROR(EINVAL);
181 goto fail;
182 }
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (used_in_ch[in_ch_id]) {
184 av_log(ctx, AV_LOG_ERROR,
185 "Can not reference in channel %d twice\n", in_ch_id);
186 ret = AVERROR(EINVAL);
187 goto fail;
188 }
189 80 used_in_ch[in_ch_id] = 1;
190 80 pan->gain[out_ch_id][in_ch_id] = sign * gain;
191 80 skip_spaces(&arg);
192
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 25 times.
80 if (!*arg)
193 55 break;
194
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 16 times.
25 if (*arg == '-') {
195 9 sign = -1;
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (*arg != '+') {
197 av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
198 ret = AVERROR(EINVAL);
199 goto fail;
200 } else {
201 16 sign = 1;
202 }
203 25 arg++;
204 }
205 }
206 32 pan->need_renumber = !!nb_in_channels[1];
207
208 32 ret = 0;
209 32 fail:
210 32 av_free(args);
211 32 return ret;
212 }
213
214 24 static int are_gains_pure(const PanContext *pan)
215 {
216 int i, j;
217
218
2/2
✓ Branch 0 taken 1032 times.
✓ Branch 1 taken 16 times.
1048 for (i = 0; i < MAX_CHANNELS; i++) {
219 1032 int nb_gain = 0;
220
221
2/2
✓ Branch 0 taken 65552 times.
✓ Branch 1 taken 1024 times.
66576 for (j = 0; j < MAX_CHANNELS; j++) {
222 65552 double gain = pan->gain[i][j];
223
224 /* channel mapping is effective only if 0% or 100% of a channel is
225 * selected... */
226
4/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 65518 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 27 times.
65552 if (gain != 0. && gain != 1.)
227 7 return 0;
228 /* ...and if the output channel is only composed of one input */
229
4/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 65518 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 26 times.
65545 if (gain && nb_gain++)
230 1 return 0;
231 }
232 }
233 16 return 1;
234 }
235
236 24 static int query_formats(AVFilterContext *ctx)
237 {
238 24 PanContext *pan = ctx->priv;
239 24 AVFilterLink *inlink = ctx->inputs[0];
240 24 AVFilterLink *outlink = ctx->outputs[0];
241 AVFilterChannelLayouts *layouts;
242 int ret;
243
244 24 pan->pure_gains = are_gains_pure(pan);
245 /* libswr supports any sample and packing formats */
246
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
24 if ((ret = ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO))) < 0)
247 return ret;
248
249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if ((ret = ff_set_common_all_samplerates(ctx)) < 0)
250 return ret;
251
252 // inlink supports any channel layout
253 24 layouts = ff_all_channel_counts();
254
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
255 return ret;
256
257 // outlink supports only requested output channel layout
258 24 layouts = NULL;
259
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if ((ret = ff_add_channel_layout(&layouts, &pan->out_channel_layout)) < 0)
260 return ret;
261 24 return ff_channel_layouts_ref(layouts, &outlink->incfg.channel_layouts);
262 }
263
264 24 static int config_props(AVFilterLink *link)
265 {
266 24 AVFilterContext *ctx = link->dst;
267 24 PanContext *pan = ctx->priv;
268 char buf[1024], *cur;
269 int i, j, k, r, ret;
270 double t;
271
272
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 21 times.
24 if (pan->need_renumber) {
273 // input channels were given by their name: renumber them
274
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 3 times.
195 for (i = j = 0; i < MAX_CHANNELS; i++) {
275
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 183 times.
192 if (av_channel_layout_index_from_channel(&link->ch_layout, i) >= 0) {
276
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 9 times.
25 for (k = 0; k < pan->nb_output_channels; k++)
277 16 pan->gain[k][j] = pan->gain[k][i];
278 9 j++;
279 }
280 }
281 }
282
283 // sanity check; can't be done in query_formats since the inlink
284 // channel layout is unknown at that time
285
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (link->ch_layout.nb_channels > MAX_CHANNELS ||
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 pan->nb_output_channels > MAX_CHANNELS) {
287 av_log(ctx, AV_LOG_ERROR,
288 "af_pan supports a maximum of %d channels. "
289 "Feel free to ask for a higher limit.\n", MAX_CHANNELS);
290 return AVERROR_PATCHWELCOME;
291 }
292
293 // init libswresample context
294 24 ret = swr_alloc_set_opts2(&pan->swr,
295 24 &pan->out_channel_layout, link->format, link->sample_rate,
296 24 &link->ch_layout, link->format, link->sample_rate,
297 0, ctx);
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
299 return AVERROR(ENOMEM);
300
301 // gains are pure, init the channel mapping
302
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 if (pan->pure_gains) {
303
304 // get channel map from the pure gains
305
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 16 times.
37 for (i = 0; i < pan->nb_output_channels; i++) {
306 21 int ch_id = -1;
307
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 9 times.
36 for (j = 0; j < link->ch_layout.nb_channels; j++) {
308
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 15 times.
27 if (pan->gain[i][j]) {
309 12 ch_id = j;
310 12 break;
311 }
312 }
313 21 pan->channel_map[i] = ch_id;
314 }
315
316 16 av_opt_set_int(pan->swr, "uch", pan->nb_output_channels, 0);
317 16 swr_set_channel_mapping(pan->swr, pan->channel_map);
318 } else {
319 // renormalize
320
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 8 times.
35 for (i = 0; i < pan->nb_output_channels; i++) {
321
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 4 times.
27 if (!((pan->need_renorm >> i) & 1))
322 23 continue;
323 4 t = 0;
324
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (j = 0; j < link->ch_layout.nb_channels; j++)
325 12 t += fabs(pan->gain[i][j]);
326
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (t > -1E-5 && t < 1E-5) {
327 // t is almost 0 but not exactly, this is probably a mistake
328 if (t)
329 av_log(ctx, AV_LOG_WARNING,
330 "Degenerate coefficients while renormalizing\n");
331 continue;
332 }
333
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (j = 0; j < link->ch_layout.nb_channels; j++)
334 12 pan->gain[i][j] /= t;
335 }
336 8 swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
337 }
338
339 24 r = swr_init(pan->swr);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (r < 0)
341 return r;
342
343 // summary
344
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
72 for (i = 0; i < pan->nb_output_channels; i++) {
345 48 cur = buf;
346
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 48 times.
223 for (j = 0; j < link->ch_layout.nb_channels; j++) {
347
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 48 times.
175 r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
348 j ? " + " : "", pan->gain[i][j], j);
349
1/2
✓ Branch 0 taken 175 times.
✗ Branch 1 not taken.
175 cur += FFMIN(buf + sizeof(buf) - cur, r);
350 }
351 48 av_log(ctx, AV_LOG_VERBOSE, "o%d = %s\n", i, buf);
352 }
353 // add channel mapping summary if possible
354
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 if (pan->pure_gains) {
355 16 av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
356
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 16 times.
37 for (i = 0; i < pan->nb_output_channels; i++)
357
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 12 times.
21 if (pan->channel_map[i] < 0)
358 9 av_log(ctx, AV_LOG_INFO, " M");
359 else
360 12 av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
361 16 av_log(ctx, AV_LOG_INFO, "\n");
362 16 return 0;
363 }
364 8 return 0;
365 }
366
367 3116 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
368 {
369 int ret;
370 3116 int n = insamples->nb_samples;
371 3116 AVFilterLink *const outlink = inlink->dst->outputs[0];
372 3116 AVFrame *outsamples = ff_get_audio_buffer(outlink, n);
373 3116 PanContext *pan = inlink->dst->priv;
374
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3116 times.
3116 if (!outsamples) {
376 av_frame_free(&insamples);
377 return AVERROR(ENOMEM);
378 }
379 3116 swr_convert(pan->swr, outsamples->extended_data, n,
380 3116 (void *)insamples->extended_data, n);
381 3116 av_frame_copy_props(outsamples, insamples);
382 #if FF_API_OLD_CHANNEL_LAYOUT
383 FF_DISABLE_DEPRECATION_WARNINGS
384 3116 outsamples->channel_layout = outlink->channel_layout;
385 3116 outsamples->channels = outlink->ch_layout.nb_channels;
386 FF_ENABLE_DEPRECATION_WARNINGS
387 #endif
388
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3116 times.
3116 if ((ret = av_channel_layout_copy(&outsamples->ch_layout, &outlink->ch_layout)) < 0)
389 return ret;
390
391 3116 ret = ff_filter_frame(outlink, outsamples);
392 3116 av_frame_free(&insamples);
393 3116 return ret;
394 }
395
396 32 static av_cold void uninit(AVFilterContext *ctx)
397 {
398 32 PanContext *pan = ctx->priv;
399 32 swr_free(&pan->swr);
400 32 }
401
402 #define OFFSET(x) offsetof(PanContext, x)
403
404 static const AVOption pan_options[] = {
405 { "args", NULL, OFFSET(args), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
406 { NULL }
407 };
408
409 AVFILTER_DEFINE_CLASS(pan);
410
411 static const AVFilterPad pan_inputs[] = {
412 {
413 .name = "default",
414 .type = AVMEDIA_TYPE_AUDIO,
415 .config_props = config_props,
416 .filter_frame = filter_frame,
417 },
418 };
419
420 static const AVFilterPad pan_outputs[] = {
421 {
422 .name = "default",
423 .type = AVMEDIA_TYPE_AUDIO,
424 },
425 };
426
427 const AVFilter ff_af_pan = {
428 .name = "pan",
429 .description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
430 .priv_size = sizeof(PanContext),
431 .priv_class = &pan_class,
432 .init = init,
433 .uninit = uninit,
434 FILTER_INPUTS(pan_inputs),
435 FILTER_OUTPUTS(pan_outputs),
436 FILTER_QUERY_FUNC(query_formats),
437 };
438