FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_stereotools.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 96 195 49.2%
Functions: 4 5 80.0%
Branches: 24 114 21.1%

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