FFmpeg coverage


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