FFmpeg
Loading...
Searching...
No Matches
af_asupercut.c
Go to the documentation of this file.
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
23#include "libavutil/ffmath.h"
24#include "libavutil/opt.h"
25#include "avfilter.h"
26#include "audio.h"
27#include "filters.h"
28
29typedef struct BiquadCoeffs {
30 double a1, a2;
31 double b0, b1, b2;
33
34typedef struct ASuperCutContext {
35 const AVClass *class;
36
37 double cutoff;
38 double level;
39 double qfactor;
40 int order;
41
43 int bypass;
44
46
48
49 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
51
55
56static void calc_q_factors(int n, double *q)
57{
58 for (int i = 0; i < n / 2; i++)
59 q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
60}
61
63{
64 ASuperCutContext *s = ctx->priv;
65 AVFilterLink *inlink = ctx->inputs[0];
66 double w0 = s->cutoff / inlink->sample_rate;
67 double K = tan(M_PI * w0);
68 double q[10];
69
70 s->bypass = w0 >= 0.5;
71 if (s->bypass)
72 return 0;
73
74 if (!strcmp(ctx->filter->name, "asubcut")) {
75 s->filter_count = s->order / 2 + (s->order & 1);
76
77 calc_q_factors(s->order, q);
78
79 if (s->order & 1) {
80 BiquadCoeffs *coeffs = &s->coeffs[0];
81 double omega = 2. * tan(M_PI * w0);
82
83 coeffs->b0 = 2. / (2. + omega);
84 coeffs->b1 = -coeffs->b0;
85 coeffs->b2 = 0.;
86 coeffs->a1 = -(omega - 2.) / (2. + omega);
87 coeffs->a2 = 0.;
88 }
89
90 for (int b = (s->order & 1); b < s->filter_count; b++) {
91 BiquadCoeffs *coeffs = &s->coeffs[b];
92 const int idx = b - (s->order & 1);
93 double norm = 1.0 / (1.0 + K / q[idx] + K * K);
94
95 coeffs->b0 = norm;
96 coeffs->b1 = -2.0 * coeffs->b0;
97 coeffs->b2 = coeffs->b0;
98 coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
99 coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm;
100 }
101 } else if (!strcmp(ctx->filter->name, "asupercut")) {
102 s->filter_count = s->order / 2 + (s->order & 1);
103
104 calc_q_factors(s->order, q);
105
106 if (s->order & 1) {
107 BiquadCoeffs *coeffs = &s->coeffs[0];
108 double omega = 2. * tan(M_PI * w0);
109
110 coeffs->b0 = omega / (2. + omega);
111 coeffs->b1 = coeffs->b0;
112 coeffs->b2 = 0.;
113 coeffs->a1 = -(omega - 2.) / (2. + omega);
114 coeffs->a2 = 0.;
115 }
116
117 for (int b = (s->order & 1); b < s->filter_count; b++) {
118 BiquadCoeffs *coeffs = &s->coeffs[b];
119 const int idx = b - (s->order & 1);
120 double norm = 1.0 / (1.0 + K / q[idx] + K * K);
121
122 coeffs->b0 = K * K * norm;
123 coeffs->b1 = 2.0 * coeffs->b0;
124 coeffs->b2 = coeffs->b0;
125 coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
126 coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm;
127 }
128 } else if (!strcmp(ctx->filter->name, "asuperpass")) {
129 double alpha, beta, gamma, theta;
130 double theta_0 = 2. * M_PI * (s->cutoff / inlink->sample_rate);
131 double d_E;
132
133 s->filter_count = s->order / 2;
134 d_E = (2. * tan(theta_0 / (2. * s->qfactor))) / sin(theta_0);
135
136 for (int b = 0; b < s->filter_count; b += 2) {
137 double D = 2. * sin(((b + 1) * M_PI) / (2. * s->filter_count));
138 double A = (1. + pow((d_E / 2.), 2)) / (D * d_E / 2.);
139 double d = sqrt((d_E * D) / (A + sqrt(A * A - 1.)));
140 double B = D * (d_E / 2.) / d;
141 double W = B + sqrt(B * B - 1.);
142
143 for (int j = 0; j < 2; j++) {
144 BiquadCoeffs *coeffs = &s->coeffs[b + j];
145
146 if (j == 1)
147 theta = 2. * atan(tan(theta_0 / 2.) / W);
148 else
149 theta = 2. * atan(W * tan(theta_0 / 2.));
150
151 beta = 0.5 * ((1. - (d / 2.) * sin(theta)) / (1. + (d / 2.) * sin(theta)));
152 gamma = (0.5 + beta) * cos(theta);
153 alpha = 0.5 * (0.5 - beta) * sqrt(1. + pow((W - (1. / W)) / d, 2.));
154
155 coeffs->a1 = 2. * gamma;
156 coeffs->a2 = -2. * beta;
157 coeffs->b0 = 2. * alpha;
158 coeffs->b1 = 0.;
159 coeffs->b2 = -2. * alpha;
160 }
161 }
162 } else if (!strcmp(ctx->filter->name, "asuperstop")) {
163 double alpha, beta, gamma, theta;
164 double theta_0 = 2. * M_PI * (s->cutoff / inlink->sample_rate);
165 double d_E;
166
167 s->filter_count = s->order / 2;
168 d_E = (2. * tan(theta_0 / (2. * s->qfactor))) / sin(theta_0);
169
170 for (int b = 0; b < s->filter_count; b += 2) {
171 double D = 2. * sin(((b + 1) * M_PI) / (2. * s->filter_count));
172 double A = (1. + pow((d_E / 2.), 2)) / (D * d_E / 2.);
173 double d = sqrt((d_E * D) / (A + sqrt(A * A - 1.)));
174 double B = D * (d_E / 2.) / d;
175 double W = B + sqrt(B * B - 1.);
176
177 for (int j = 0; j < 2; j++) {
178 BiquadCoeffs *coeffs = &s->coeffs[b + j];
179
180 if (j == 1)
181 theta = 2. * atan(tan(theta_0 / 2.) / W);
182 else
183 theta = 2. * atan(W * tan(theta_0 / 2.));
184
185 beta = 0.5 * ((1. - (d / 2.) * sin(theta)) / (1. + (d / 2.) * sin(theta)));
186 gamma = (0.5 + beta) * cos(theta);
187 alpha = 0.5 * (0.5 + beta) * ((1. - cos(theta)) / (1. - cos(theta_0)));
188
189 coeffs->a1 = 2. * gamma;
190 coeffs->a2 = -2. * beta;
191 coeffs->b0 = 2. * alpha;
192 coeffs->b1 = -4. * alpha * cos(theta_0);
193 coeffs->b2 = 2. * alpha;
194 }
195 }
196 }
197
198 return 0;
199}
200
201typedef struct ThreadData {
202 AVFrame *in, *out;
203} ThreadData;
204
205#define FILTER(name, type) \
206static int filter_channels_## name(AVFilterContext *ctx, void *arg, \
207 int jobnr, int nb_jobs) \
208{ \
209 ASuperCutContext *s = ctx->priv; \
210 ThreadData *td = arg; \
211 AVFrame *out = td->out; \
212 AVFrame *in = td->in; \
213 const int start = ff_slice_pos(in->ch_layout.nb_channels, jobnr, nb_jobs); \
214 const int end = ff_slice_pos(in->ch_layout.nb_channels, jobnr + 1, nb_jobs); \
215 const double level = s->level; \
216 \
217 for (int ch = start; ch < end; ch++) { \
218 const type *src = (const type *)in->extended_data[ch]; \
219 type *dst = (type *)out->extended_data[ch]; \
220 \
221 for (int b = 0; b < s->filter_count; b++) { \
222 BiquadCoeffs *coeffs = &s->coeffs[b]; \
223 const type a1 = coeffs->a1; \
224 const type a2 = coeffs->a2; \
225 const type b0 = coeffs->b0; \
226 const type b1 = coeffs->b1; \
227 const type b2 = coeffs->b2; \
228 type *w = ((type *)s->w->extended_data[ch]) + b * 2; \
229 \
230 for (int n = 0; n < in->nb_samples; n++) { \
231 type sin = b ? dst[n] : src[n] * level; \
232 type sout = sin * b0 + w[0]; \
233 \
234 w[0] = b1 * sin + w[1] + a1 * sout; \
235 w[1] = b2 * sin + a2 * sout; \
236 \
237 dst[n] = sout; \
238 } \
239 } \
240 } \
241 \
242 return 0; \
243}
244
245FILTER(fltp, float)
246FILTER(dblp, double)
247
248static int config_input(AVFilterLink *inlink)
249{
250 AVFilterContext *ctx = inlink->dst;
251 ASuperCutContext *s = ctx->priv;
252
253 switch (inlink->format) {
254 case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
255 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
256 }
257
258 s->w = ff_get_audio_buffer(inlink, 2 * 10);
259 if (!s->w)
260 return AVERROR(ENOMEM);
261
262 return get_coeffs(ctx);
263}
264
265static int filter_frame(AVFilterLink *inlink, AVFrame *in)
266{
267 AVFilterContext *ctx = inlink->dst;
268 ASuperCutContext *s = ctx->priv;
269 AVFilterLink *outlink = ctx->outputs[0];
270 ThreadData td;
271 AVFrame *out;
272
273 if (s->bypass)
274 return ff_filter_frame(outlink, in);
275
276 if (av_frame_is_writable(in)) {
277 out = in;
278 } else {
279 out = ff_get_audio_buffer(outlink, in->nb_samples);
280 if (!out) {
281 av_frame_free(&in);
282 return AVERROR(ENOMEM);
283 }
285 }
286
287 td.in = in; td.out = out;
288 ff_filter_execute(ctx, s->filter_channels, &td, NULL,
290
291 if (out != in)
292 av_frame_free(&in);
293 return ff_filter_frame(outlink, out);
294}
295
296static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
297 char *res, int res_len, int flags)
298{
299 int ret;
300
301 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
302 if (ret < 0)
303 return ret;
304
305 return get_coeffs(ctx);
306}
307
309{
310 ASuperCutContext *s = ctx->priv;
311
312 av_frame_free(&s->w);
313}
314
315#define OFFSET(x) offsetof(ASuperCutContext, x)
316#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
317
318static const AVOption asupercut_options[] = {
319 { "cutoff", "set cutoff frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=20000}, 20000, 192000, FLAGS },
320 { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=10}, 3, 20, FLAGS },
321 { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 1., FLAGS },
322 { NULL }
323};
324
326
327static const AVFilterPad inputs[] = {
328 {
329 .name = "default",
330 .type = AVMEDIA_TYPE_AUDIO,
331 .filter_frame = filter_frame,
332 .config_props = config_input,
333 },
334};
335
337 .p.name = "asupercut",
338 .p.description = NULL_IF_CONFIG_SMALL("Cut super frequencies."),
339 .p.priv_class = &asupercut_class,
342 .priv_size = sizeof(ASuperCutContext),
343 .uninit = uninit,
347 .process_command = process_command,
348};
349
350static const AVOption asubcut_options[] = {
351 { "cutoff", "set cutoff frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 2, 200, FLAGS },
352 { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=10}, 3, 20, FLAGS },
353 { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 1., FLAGS },
354 { NULL }
355};
356
358
360 .p.name = "asubcut",
361 .p.description = NULL_IF_CONFIG_SMALL("Cut subwoofer frequencies."),
362 .p.priv_class = &asubcut_class,
365 .priv_size = sizeof(ASuperCutContext),
366 .uninit = uninit,
370 .process_command = process_command,
371};
372
374 { "centerf","set center frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 999999, FLAGS },
375 { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=4}, 4, 20, FLAGS },
376 { "qfactor","set Q-factor", OFFSET(qfactor),AV_OPT_TYPE_DOUBLE, {.dbl=1.},0.01, 100., FLAGS },
377 { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 2., FLAGS },
378 { NULL }
379};
380
381AVFILTER_DEFINE_CLASS_EXT(asuperpass_asuperstop, "asuperpass/asuperstop",
383
385 .p.name = "asuperpass",
386 .p.description = NULL_IF_CONFIG_SMALL("Apply high order Butterworth band-pass filter."),
387 .p.priv_class = &asuperpass_asuperstop_class,
390 .priv_size = sizeof(ASuperCutContext),
391 .uninit = uninit,
395 .process_command = process_command,
396};
397
399 .p.name = "asuperstop",
400 .p.description = NULL_IF_CONFIG_SMALL("Apply high order Butterworth band-stop filter."),
401 .p.priv_class = &asuperpass_asuperstop_class,
404 .priv_size = sizeof(ASuperCutContext),
405 .uninit = uninit,
409 .process_command = process_command,
410};
static enum AVSampleFormat sample_fmts[]
Definition adpcmenc.c:933
static const AVFilterPad inputs[]
Definition af_aap.c:299
static int config_input(AVFilterLink *inlink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int get_coeffs(AVFilterContext *ctx)
const FFFilter ff_af_asuperstop
static void calc_q_factors(int n, double *q)
static const AVOption asuperpass_asuperstop_options[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
#define FILTER(name, type)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static const AVOption asubcut_options[]
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
const FFFilter ff_af_asupercut
const FFFilter ff_af_asuperpass
const FFFilter ff_af_asubcut
static const AVOption asupercut_options[]
#define A(x)
Definition vpx_arith.h:28
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
#define D
Definition avdct.c:35
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
internal math functions header
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
#define B
Definition huffyuv.h:42
static const int16_t alpha[]
Definition ilbcdata.h:55
#define b
Definition input.c:43
#define W(a, i, v)
Definition jpegls.h:119
static av_cold void uninit(AVBitStreamFilterContext *ctx)
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_SAMPLEFMTS_ARRAY(array)
Definition filters.h:247
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS_EXT(name, desc, options)
Definition filters.h:470
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define M_PI
Definition mathematics.h:67
AVOptions.
#define K
Definition palette.c:25
BiquadCoeffs coeffs[10]
int(* filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
AVOption.
Definition opt.h:428
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
uint8_t level
Definition svq3.c:208
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49