FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_asupercut.c
Date: 2024-07-16 12:46:59
Exec Total Coverage
Lines: 0 139 0.0%
Functions: 0 8 0.0%
Branches: 0 65 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2005 Boðaç Topaktaþ
3 * Copyright (c) 2020 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/ffmath.h"
24 #include "libavutil/opt.h"
25 #include "avfilter.h"
26 #include "audio.h"
27
28 typedef struct BiquadCoeffs {
29 double a1, a2;
30 double b0, b1, b2;
31 } BiquadCoeffs;
32
33 typedef struct ASuperCutContext {
34 const AVClass *class;
35
36 double cutoff;
37 double level;
38 double qfactor;
39 int order;
40
41 int filter_count;
42 int bypass;
43
44 BiquadCoeffs coeffs[10];
45
46 AVFrame *w;
47
48 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
49 } ASuperCutContext;
50
51 static const enum AVSampleFormat sample_fmts[] = {
52 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE
53 };
54
55 static void calc_q_factors(int n, double *q)
56 {
57 for (int i = 0; i < n / 2; i++)
58 q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
59 }
60
61 static int get_coeffs(AVFilterContext *ctx)
62 {
63 ASuperCutContext *s = ctx->priv;
64 AVFilterLink *inlink = ctx->inputs[0];
65 double w0 = s->cutoff / inlink->sample_rate;
66 double K = tan(M_PI * w0);
67 double q[10];
68
69 s->bypass = w0 >= 0.5;
70 if (s->bypass)
71 return 0;
72
73 if (!strcmp(ctx->filter->name, "asubcut")) {
74 s->filter_count = s->order / 2 + (s->order & 1);
75
76 calc_q_factors(s->order, q);
77
78 if (s->order & 1) {
79 BiquadCoeffs *coeffs = &s->coeffs[0];
80 double omega = 2. * tan(M_PI * w0);
81
82 coeffs->b0 = 2. / (2. + omega);
83 coeffs->b1 = -coeffs->b0;
84 coeffs->b2 = 0.;
85 coeffs->a1 = -(omega - 2.) / (2. + omega);
86 coeffs->a2 = 0.;
87 }
88
89 for (int b = (s->order & 1); b < s->filter_count; b++) {
90 BiquadCoeffs *coeffs = &s->coeffs[b];
91 const int idx = b - (s->order & 1);
92 double norm = 1.0 / (1.0 + K / q[idx] + K * K);
93
94 coeffs->b0 = norm;
95 coeffs->b1 = -2.0 * coeffs->b0;
96 coeffs->b2 = coeffs->b0;
97 coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
98 coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm;
99 }
100 } else if (!strcmp(ctx->filter->name, "asupercut")) {
101 s->filter_count = s->order / 2 + (s->order & 1);
102
103 calc_q_factors(s->order, q);
104
105 if (s->order & 1) {
106 BiquadCoeffs *coeffs = &s->coeffs[0];
107 double omega = 2. * tan(M_PI * w0);
108
109 coeffs->b0 = omega / (2. + omega);
110 coeffs->b1 = coeffs->b0;
111 coeffs->b2 = 0.;
112 coeffs->a1 = -(omega - 2.) / (2. + omega);
113 coeffs->a2 = 0.;
114 }
115
116 for (int b = (s->order & 1); b < s->filter_count; b++) {
117 BiquadCoeffs *coeffs = &s->coeffs[b];
118 const int idx = b - (s->order & 1);
119 double norm = 1.0 / (1.0 + K / q[idx] + K * K);
120
121 coeffs->b0 = K * K * norm;
122 coeffs->b1 = 2.0 * coeffs->b0;
123 coeffs->b2 = coeffs->b0;
124 coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
125 coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm;
126 }
127 } else if (!strcmp(ctx->filter->name, "asuperpass")) {
128 double alpha, beta, gamma, theta;
129 double theta_0 = 2. * M_PI * (s->cutoff / inlink->sample_rate);
130 double d_E;
131
132 s->filter_count = s->order / 2;
133 d_E = (2. * tan(theta_0 / (2. * s->qfactor))) / sin(theta_0);
134
135 for (int b = 0; b < s->filter_count; b += 2) {
136 double D = 2. * sin(((b + 1) * M_PI) / (2. * s->filter_count));
137 double A = (1. + pow((d_E / 2.), 2)) / (D * d_E / 2.);
138 double d = sqrt((d_E * D) / (A + sqrt(A * A - 1.)));
139 double B = D * (d_E / 2.) / d;
140 double W = B + sqrt(B * B - 1.);
141
142 for (int j = 0; j < 2; j++) {
143 BiquadCoeffs *coeffs = &s->coeffs[b + j];
144
145 if (j == 1)
146 theta = 2. * atan(tan(theta_0 / 2.) / W);
147 else
148 theta = 2. * atan(W * tan(theta_0 / 2.));
149
150 beta = 0.5 * ((1. - (d / 2.) * sin(theta)) / (1. + (d / 2.) * sin(theta)));
151 gamma = (0.5 + beta) * cos(theta);
152 alpha = 0.5 * (0.5 - beta) * sqrt(1. + pow((W - (1. / W)) / d, 2.));
153
154 coeffs->a1 = 2. * gamma;
155 coeffs->a2 = -2. * beta;
156 coeffs->b0 = 2. * alpha;
157 coeffs->b1 = 0.;
158 coeffs->b2 = -2. * alpha;
159 }
160 }
161 } else if (!strcmp(ctx->filter->name, "asuperstop")) {
162 double alpha, beta, gamma, theta;
163 double theta_0 = 2. * M_PI * (s->cutoff / inlink->sample_rate);
164 double d_E;
165
166 s->filter_count = s->order / 2;
167 d_E = (2. * tan(theta_0 / (2. * s->qfactor))) / sin(theta_0);
168
169 for (int b = 0; b < s->filter_count; b += 2) {
170 double D = 2. * sin(((b + 1) * M_PI) / (2. * s->filter_count));
171 double A = (1. + pow((d_E / 2.), 2)) / (D * d_E / 2.);
172 double d = sqrt((d_E * D) / (A + sqrt(A * A - 1.)));
173 double B = D * (d_E / 2.) / d;
174 double W = B + sqrt(B * B - 1.);
175
176 for (int j = 0; j < 2; j++) {
177 BiquadCoeffs *coeffs = &s->coeffs[b + j];
178
179 if (j == 1)
180 theta = 2. * atan(tan(theta_0 / 2.) / W);
181 else
182 theta = 2. * atan(W * tan(theta_0 / 2.));
183
184 beta = 0.5 * ((1. - (d / 2.) * sin(theta)) / (1. + (d / 2.) * sin(theta)));
185 gamma = (0.5 + beta) * cos(theta);
186 alpha = 0.5 * (0.5 + beta) * ((1. - cos(theta)) / (1. - cos(theta_0)));
187
188 coeffs->a1 = 2. * gamma;
189 coeffs->a2 = -2. * beta;
190 coeffs->b0 = 2. * alpha;
191 coeffs->b1 = -4. * alpha * cos(theta_0);
192 coeffs->b2 = 2. * alpha;
193 }
194 }
195 }
196
197 return 0;
198 }
199
200 typedef struct ThreadData {
201 AVFrame *in, *out;
202 } ThreadData;
203
204 #define FILTER(name, type) \
205 static int filter_channels_## name(AVFilterContext *ctx, void *arg, \
206 int jobnr, int nb_jobs) \
207 { \
208 ASuperCutContext *s = ctx->priv; \
209 ThreadData *td = arg; \
210 AVFrame *out = td->out; \
211 AVFrame *in = td->in; \
212 const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs; \
213 const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs; \
214 const double level = s->level; \
215 \
216 for (int ch = start; ch < end; ch++) { \
217 const type *src = (const type *)in->extended_data[ch]; \
218 type *dst = (type *)out->extended_data[ch]; \
219 \
220 for (int b = 0; b < s->filter_count; b++) { \
221 BiquadCoeffs *coeffs = &s->coeffs[b]; \
222 const type a1 = coeffs->a1; \
223 const type a2 = coeffs->a2; \
224 const type b0 = coeffs->b0; \
225 const type b1 = coeffs->b1; \
226 const type b2 = coeffs->b2; \
227 type *w = ((type *)s->w->extended_data[ch]) + b * 2; \
228 \
229 for (int n = 0; n < in->nb_samples; n++) { \
230 type sin = b ? dst[n] : src[n] * level; \
231 type sout = sin * b0 + w[0]; \
232 \
233 w[0] = b1 * sin + w[1] + a1 * sout; \
234 w[1] = b2 * sin + a2 * sout; \
235 \
236 dst[n] = sout; \
237 } \
238 } \
239 } \
240 \
241 return 0; \
242 }
243
244 FILTER(fltp, float)
245 FILTER(dblp, double)
246
247 static int config_input(AVFilterLink *inlink)
248 {
249 AVFilterContext *ctx = inlink->dst;
250 ASuperCutContext *s = ctx->priv;
251
252 switch (inlink->format) {
253 case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
254 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
255 }
256
257 s->w = ff_get_audio_buffer(inlink, 2 * 10);
258 if (!s->w)
259 return AVERROR(ENOMEM);
260
261 return get_coeffs(ctx);
262 }
263
264 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
265 {
266 AVFilterContext *ctx = inlink->dst;
267 ASuperCutContext *s = ctx->priv;
268 AVFilterLink *outlink = ctx->outputs[0];
269 ThreadData td;
270 AVFrame *out;
271
272 if (s->bypass)
273 return ff_filter_frame(outlink, in);
274
275 if (av_frame_is_writable(in)) {
276 out = in;
277 } else {
278 out = ff_get_audio_buffer(outlink, in->nb_samples);
279 if (!out) {
280 av_frame_free(&in);
281 return AVERROR(ENOMEM);
282 }
283 av_frame_copy_props(out, in);
284 }
285
286 td.in = in; td.out = out;
287 ff_filter_execute(ctx, s->filter_channels, &td, NULL,
288 FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
289
290 if (out != in)
291 av_frame_free(&in);
292 return ff_filter_frame(outlink, out);
293 }
294
295 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
296 char *res, int res_len, int flags)
297 {
298 int ret;
299
300 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
301 if (ret < 0)
302 return ret;
303
304 return get_coeffs(ctx);
305 }
306
307 static av_cold void uninit(AVFilterContext *ctx)
308 {
309 ASuperCutContext *s = ctx->priv;
310
311 av_frame_free(&s->w);
312 }
313
314 #define OFFSET(x) offsetof(ASuperCutContext, x)
315 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
316
317 static const AVOption asupercut_options[] = {
318 { "cutoff", "set cutoff frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=20000}, 20000, 192000, FLAGS },
319 { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=10}, 3, 20, FLAGS },
320 { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 1., FLAGS },
321 { NULL }
322 };
323
324 AVFILTER_DEFINE_CLASS(asupercut);
325
326 static const AVFilterPad inputs[] = {
327 {
328 .name = "default",
329 .type = AVMEDIA_TYPE_AUDIO,
330 .filter_frame = filter_frame,
331 .config_props = config_input,
332 },
333 };
334
335 const AVFilter ff_af_asupercut = {
336 .name = "asupercut",
337 .description = NULL_IF_CONFIG_SMALL("Cut super frequencies."),
338 .priv_size = sizeof(ASuperCutContext),
339 .priv_class = &asupercut_class,
340 .uninit = uninit,
341 FILTER_INPUTS(inputs),
342 FILTER_OUTPUTS(ff_audio_default_filterpad),
343 FILTER_SAMPLEFMTS_ARRAY(sample_fmts),
344 .process_command = process_command,
345 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
346 AVFILTER_FLAG_SLICE_THREADS,
347 };
348
349 static const AVOption asubcut_options[] = {
350 { "cutoff", "set cutoff frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 2, 200, FLAGS },
351 { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=10}, 3, 20, FLAGS },
352 { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 1., FLAGS },
353 { NULL }
354 };
355
356 AVFILTER_DEFINE_CLASS(asubcut);
357
358 const AVFilter ff_af_asubcut = {
359 .name = "asubcut",
360 .description = NULL_IF_CONFIG_SMALL("Cut subwoofer frequencies."),
361 .priv_size = sizeof(ASuperCutContext),
362 .priv_class = &asubcut_class,
363 .uninit = uninit,
364 FILTER_INPUTS(inputs),
365 FILTER_OUTPUTS(ff_audio_default_filterpad),
366 FILTER_SAMPLEFMTS_ARRAY(sample_fmts),
367 .process_command = process_command,
368 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
369 AVFILTER_FLAG_SLICE_THREADS,
370 };
371
372 static const AVOption asuperpass_asuperstop_options[] = {
373 { "centerf","set center frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 999999, FLAGS },
374 { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=4}, 4, 20, FLAGS },
375 { "qfactor","set Q-factor", OFFSET(qfactor),AV_OPT_TYPE_DOUBLE, {.dbl=1.},0.01, 100., FLAGS },
376 { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 2., FLAGS },
377 { NULL }
378 };
379
380 AVFILTER_DEFINE_CLASS_EXT(asuperpass_asuperstop, "asuperpass/asuperstop",
381 asuperpass_asuperstop_options);
382
383 const AVFilter ff_af_asuperpass = {
384 .name = "asuperpass",
385 .description = NULL_IF_CONFIG_SMALL("Apply high order Butterworth band-pass filter."),
386 .priv_class = &asuperpass_asuperstop_class,
387 .priv_size = sizeof(ASuperCutContext),
388 .uninit = uninit,
389 FILTER_INPUTS(inputs),
390 FILTER_OUTPUTS(ff_audio_default_filterpad),
391 FILTER_SAMPLEFMTS_ARRAY(sample_fmts),
392 .process_command = process_command,
393 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
394 AVFILTER_FLAG_SLICE_THREADS,
395 };
396
397 const AVFilter ff_af_asuperstop = {
398 .name = "asuperstop",
399 .description = NULL_IF_CONFIG_SMALL("Apply high order Butterworth band-stop filter."),
400 .priv_class = &asuperpass_asuperstop_class,
401 .priv_size = sizeof(ASuperCutContext),
402 .uninit = uninit,
403 FILTER_INPUTS(inputs),
404 FILTER_OUTPUTS(ff_audio_default_filterpad),
405 FILTER_SAMPLEFMTS_ARRAY(sample_fmts),
406 .process_command = process_command,
407 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
408 AVFILTER_FLAG_SLICE_THREADS,
409 };
410