FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_stereotools.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 98 196 50.0%
Branches: 26 118 22.0%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen
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 GNU
14 * 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 #include "libavutil/channel_layout.h"
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "audio.h"
25 #include "formats.h"
26
27 typedef struct StereoToolsContext {
28 const AVClass *class;
29
30 int softclip;
31 int mute_l;
32 int mute_r;
33 int phase_l;
34 int phase_r;
35 int mode;
36 int bmode_in;
37 int bmode_out;
38 double slev;
39 double sbal;
40 double mlev;
41 double mpan;
42 double phase;
43 double base;
44 double delay;
45 double balance_in;
46 double balance_out;
47 double phase_sin_coef;
48 double phase_cos_coef;
49 double sc_level;
50 double inv_atan_shape;
51 double level_in;
52 double level_out;
53
54 double *buffer;
55 int length;
56 int pos;
57 } StereoToolsContext;
58
59 #define OFFSET(x) offsetof(StereoToolsContext, x)
60 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
61
62 static const AVOption stereotools_options[] = {
63 { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
64 { "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
65 { "balance_in", "set balance in", OFFSET(balance_in), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
66 { "balance_out", "set balance out", OFFSET(balance_out), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
67 { "softclip", "enable softclip", OFFSET(softclip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
68 { "mutel", "mute L", OFFSET(mute_l), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
69 { "muter", "mute R", OFFSET(mute_r), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
70 { "phasel", "phase L", OFFSET(phase_l), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
71 { "phaser", "phase R", OFFSET(phase_r), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
72 { "mode", "set stereo mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 10, A, "mode" },
73 { "lr>lr", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "mode" },
74 { "lr>ms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "mode" },
75 { "ms>lr", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, "mode" },
76 { "lr>ll", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, A, "mode" },
77 { "lr>rr", 0, 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, A, "mode" },
78 { "lr>l+r", 0, 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, A, "mode" },
79 { "lr>rl", 0, 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, A, "mode" },
80 { "ms>ll", 0, 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, A, "mode" },
81 { "ms>rr", 0, 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, A, "mode" },
82 { "ms>rl", 0, 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, A, "mode" },
83 { "lr>l-r", 0, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, A, "mode" },
84 { "slev", "set side level", OFFSET(slev), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
85 { "sbal", "set side balance", OFFSET(sbal), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
86 { "mlev", "set middle level", OFFSET(mlev), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
87 { "mpan", "set middle pan", OFFSET(mpan), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
88 { "base", "set stereo base", OFFSET(base), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
89 { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -20, 20, A },
90 { "sclevel", "set S/C level", OFFSET(sc_level), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 100, A },
91 { "phase", "set stereo phase", OFFSET(phase), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 360, A },
92 { "bmode_in", "set balance in mode", OFFSET(bmode_in), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, A, "bmode" },
93 { "balance", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "bmode" },
94 { "amplitude", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "bmode" },
95 { "power", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, "bmode" },
96 { "bmode_out", "set balance out mode", OFFSET(bmode_out), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, A, "bmode" },
97 { NULL }
98 };
99
100 AVFILTER_DEFINE_CLASS(stereotools);
101
102 1 static int query_formats(AVFilterContext *ctx)
103 {
104 1 AVFilterFormats *formats = NULL;
105 1 AVFilterChannelLayouts *layout = NULL;
106 int ret;
107
108
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
2 if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 ||
109
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 (ret = ff_set_common_formats (ctx , formats )) < 0 ||
110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
2 (ret = ff_add_channel_layout (&layout , &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0 ||
111 1 (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
112 return ret;
113
114 1 return ff_set_common_all_samplerates(ctx);
115 }
116
117 1 static int config_input(AVFilterLink *inlink)
118 {
119 1 AVFilterContext *ctx = inlink->dst;
120 1 StereoToolsContext *s = ctx->priv;
121
122 1 s->length = FFALIGN(inlink->sample_rate / 10, 2);
123
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!s->buffer)
124 1 s->buffer = av_calloc(s->length, sizeof(*s->buffer));
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->buffer)
126 return AVERROR(ENOMEM);
127
128 1 s->inv_atan_shape = 1.0 / atan(s->sc_level);
129 1 s->phase_cos_coef = cos(s->phase / 180 * M_PI);
130 1 s->phase_sin_coef = sin(s->phase / 180 * M_PI);
131
132 1 return 0;
133 }
134
135 20 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
136 {
137 20 AVFilterContext *ctx = inlink->dst;
138 20 AVFilterLink *outlink = ctx->outputs[0];
139 20 StereoToolsContext *s = ctx->priv;
140 20 const double *src = (const double *)in->data[0];
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 const double sb = s->base < 0 ? s->base * 0.5 : s->base;
142 20 const double sbal = 1 + s->sbal;
143 20 const double mpan = 1 + s->mpan;
144 20 const double slev = s->slev;
145 20 const double mlev = s->mlev;
146 20 const double balance_in = s->balance_in;
147 20 const double balance_out = s->balance_out;
148 20 const double level_in = s->level_in;
149 20 const double level_out = s->level_out;
150 20 const double sc_level = s->sc_level;
151 20 const double delay = s->delay;
152 20 const int length = s->length;
153 20 const int mute_l = s->mute_l;
154 20 const int mute_r = s->mute_r;
155 20 const int phase_l = s->phase_l;
156 20 const int phase_r = s->phase_r;
157 20 double *buffer = s->buffer;
158 AVFrame *out;
159 double *dst;
160 20 int nbuf = inlink->sample_rate * (fabs(delay) / 1000.);
161 int n;
162
163 20 nbuf -= nbuf % 2;
164
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 if (av_frame_is_writable(in)) {
165 20 out = in;
166 } else {
167 out = ff_get_audio_buffer(outlink, in->nb_samples);
168 if (!out) {
169 av_frame_free(&in);
170 return AVERROR(ENOMEM);
171 }
172 av_frame_copy_props(out, in);
173 }
174 20 dst = (double *)out->data[0];
175
176
2/2
✓ Branch 0 taken 20480 times.
✓ Branch 1 taken 20 times.
20500 for (n = 0; n < in->nb_samples; n++, src += 2, dst += 2) {
177 20480 double L = src[0], R = src[1], l, r, m, S, gl, gr, gd;
178
179 20480 L *= level_in;
180 20480 R *= level_in;
181
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20480 times.
20480 gl = 1. - FFMAX(0., balance_in);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20480 times.
20480 gr = 1. + FFMIN(0., balance_in);
184
1/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 20480 times.
20480 switch (s->bmode_in) {
185 case 1:
186 gd = gl - gr;
187 gl = 1. + gd;
188 gr = 1. - gd;
189 break;
190 case 2:
191 if (balance_in < 0.) {
192 gr = FFMAX(0.5, gr);
193 gl = 1. / gr;
194 } else if (balance_in > 0.) {
195 gl = FFMAX(0.5, gl);
196 gr = 1. / gl;
197 }
198 break;
199 }
200 20480 L *= gl;
201 20480 R *= gr;
202
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20480 times.
20480 if (s->softclip) {
204 R = s->inv_atan_shape * atan(R * sc_level);
205 L = s->inv_atan_shape * atan(L * sc_level);
206 }
207
208
1/12
✓ Branch 0 taken 20480 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
20480 switch (s->mode) {
209 20480 case 0:
210 20480 m = (L + R) * 0.5;
211 20480 S = (L - R) * 0.5;
212
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20480 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20480 times.
20480 l = m * mlev * FFMIN(1., 2. - mpan) + S * slev * FFMIN(1., 2. - sbal);
213
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20480 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20480 times.
20480 r = m * mlev * FFMIN(1., mpan) - S * slev * FFMIN(1., sbal);
214 20480 L = l;
215 20480 R = r;
216 20480 break;
217 case 1:
218 l = L * FFMIN(1., 2. - sbal);
219 r = R * FFMIN(1., sbal);
220 L = 0.5 * (l + r) * mlev;
221 R = 0.5 * (l - r) * slev;
222 break;
223 case 2:
224 l = L * mlev * FFMIN(1., 2. - mpan) + R * slev * FFMIN(1., 2. - sbal);
225 r = L * mlev * FFMIN(1., mpan) - R * slev * FFMIN(1., sbal);
226 L = l;
227 R = r;
228 break;
229 case 3:
230 R = L;
231 break;
232 case 4:
233 L = R;
234 break;
235 case 5:
236 L = (L + R) * 0.5;
237 R = L;
238 break;
239 case 6:
240 l = L;
241 L = R;
242 R = l;
243 m = (L + R) * 0.5;
244 S = (L - R) * 0.5;
245 l = m * mlev * FFMIN(1., 2. - mpan) + S * slev * FFMIN(1., 2. - sbal);
246 r = m * mlev * FFMIN(1., mpan) - S * slev * FFMIN(1., sbal);
247 L = l;
248 R = r;
249 break;
250 case 7:
251 l = L * mlev * FFMIN(1., 2. - mpan) + R * slev * FFMIN(1., 2. - sbal);
252 L = l;
253 R = l;
254 break;
255 case 8:
256 r = L * mlev * FFMIN(1., mpan) - R * slev * FFMIN(1., sbal);
257 L = r;
258 R = r;
259 break;
260 case 9:
261 l = L * mlev * FFMIN(1., 2. - mpan) + R * slev * FFMIN(1., 2. - sbal);
262 r = L * mlev * FFMIN(1., mpan) - R * slev * FFMIN(1., sbal);
263 L = r;
264 R = l;
265 break;
266 case 10:
267 L = (L - R) * 0.5;
268 R = L;
269 break;
270 }
271
272 20480 L *= 1. - mute_l;
273 20480 R *= 1. - mute_r;
274
275 20480 L *= (2. * (1. - phase_l)) - 1.;
276 20480 R *= (2. * (1. - phase_r)) - 1.;
277
278 20480 buffer[s->pos ] = L;
279 20480 buffer[s->pos+1] = R;
280
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20480 times.
20480 if (delay > 0.) {
282 R = buffer[(s->pos - (int)nbuf + 1 + length) % length];
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20480 times.
20480 } else if (delay < 0.) {
284 L = buffer[(s->pos - (int)nbuf + length) % length];
285 }
286
287 20480 l = L + sb * L - sb * R;
288 20480 r = R + sb * R - sb * L;
289
290 20480 L = l;
291 20480 R = r;
292
293 20480 l = L * s->phase_cos_coef - R * s->phase_sin_coef;
294 20480 r = L * s->phase_sin_coef + R * s->phase_cos_coef;
295
296 20480 L = l;
297 20480 R = r;
298
299 20480 s->pos = (s->pos + 2) % s->length;
300
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20480 times.
20480 gl = 1. - FFMAX(0., balance_out);
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20480 times.
20480 gr = 1. + FFMIN(0., balance_out);
303
1/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 20480 times.
20480 switch (s->bmode_out) {
304 case 1:
305 gd = gl - gr;
306 gl = 1. + gd;
307 gr = 1. - gd;
308 break;
309 case 2:
310 if (balance_out < 0.) {
311 gr = FFMAX(0.5, gr);
312 gl = 1. / gr;
313 } else if (balance_out > 0.) {
314 gl = FFMAX(0.5, gl);
315 gr = 1. / gl;
316 }
317 break;
318 }
319 20480 L *= gl;
320 20480 R *= gr;
321
322
323 20480 L *= level_out;
324 20480 R *= level_out;
325
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20480 times.
20480 if (ctx->is_disabled) {
327 dst[0] = src[0];
328 dst[1] = src[1];
329 } else {
330 20480 dst[0] = L;
331 20480 dst[1] = R;
332 }
333 }
334
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (out != in)
336 av_frame_free(&in);
337 20 return ff_filter_frame(outlink, out);
338 }
339
340 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
341 char *res, int res_len, int flags)
342 {
343 int ret;
344
345 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
346 if (ret < 0)
347 return ret;
348
349 return config_input(ctx->inputs[0]);
350 }
351
352 1 static av_cold void uninit(AVFilterContext *ctx)
353 {
354 1 StereoToolsContext *s = ctx->priv;
355
356 1 av_freep(&s->buffer);
357 1 }
358
359 static const AVFilterPad inputs[] = {
360 {
361 .name = "default",
362 .type = AVMEDIA_TYPE_AUDIO,
363 .filter_frame = filter_frame,
364 .config_props = config_input,
365 },
366 };
367
368 static const AVFilterPad outputs[] = {
369 {
370 .name = "default",
371 .type = AVMEDIA_TYPE_AUDIO,
372 },
373 };
374
375 const AVFilter ff_af_stereotools = {
376 .name = "stereotools",
377 .description = NULL_IF_CONFIG_SMALL("Apply various stereo tools."),
378 .priv_size = sizeof(StereoToolsContext),
379 .priv_class = &stereotools_class,
380 .uninit = uninit,
381 FILTER_INPUTS(inputs),
382 FILTER_OUTPUTS(outputs),
383 FILTER_QUERY_FUNC(query_formats),
384 .process_command = process_command,
385 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
386 };
387