142 double *i1,
double *i2,
double *o1,
double *o2,
143 double b0,
double b1,
double b2,
double a1,
double a2,
int *clippings,
166 switch (
s->precision) {
180 sample_fmts_list = auto_sample_fmts;
190 #define BIQUAD_FILTER(name, type, min, max, need_clipping) \
191 static void biquad_## name (BiquadsContext *s, \
192 const void *input, void *output, int len, \
193 double *in1, double *in2, \
194 double *out1, double *out2, \
195 double b0, double b1, double b2, \
196 double a1, double a2, int *clippings, \
199 const type *ibuf = input; \
200 type *obuf = output; \
205 double wet = s->mix; \
206 double dry = 1. - wet; \
212 for (i = 0; i+1 < len; i++) { \
213 o2 = i2 * b2 + i1 * b1 + ibuf[i] * b0 + o2 * a2 + o1 * a1; \
215 out = o2 * wet + i2 * dry; \
218 } else if (need_clipping && out < min) { \
221 } else if (need_clipping && out > max) { \
228 o1 = i1 * b2 + i2 * b1 + ibuf[i] * b0 + o1 * a2 + o2 * a1; \
230 out = o1 * wet + i1 * dry; \
233 } else if (need_clipping && out < min) { \
236 } else if (need_clipping && out > max) { \
244 double o0 = ibuf[i] * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
249 out = o0 * wet + i1 * dry; \
252 } else if (need_clipping && out < min) { \
255 } else if (need_clipping && out > max) { \
273 #define BIQUAD_DII_FILTER(name, type, min, max, need_clipping) \
274 static void biquad_dii_## name (BiquadsContext *s, \
275 const void *input, void *output, int len, \
276 double *z1, double *z2, \
277 double *unused1, double *unused2, \
278 double b0, double b1, double b2, \
279 double a1, double a2, int *clippings, \
282 const type *ibuf = input; \
283 type *obuf = output; \
286 double wet = s->mix; \
287 double dry = 1. - wet; \
288 double in, out, w0; \
293 for (int i = 0; i < len; i++) { \
295 w0 = in + a1 * w1 + a2 * w2; \
296 out = b0 * w0 + b1 * w1 + b2 * w2; \
299 out = out * wet + in * dry; \
302 } else if (need_clipping && out < min) { \
305 } else if (need_clipping && out > max) { \
321 #define BIQUAD_TDII_FILTER(name, type, min, max, need_clipping) \
322 static void biquad_tdii_## name (BiquadsContext *s, \
323 const void *input, void *output, int len, \
324 double *z1, double *z2, \
325 double *unused1, double *unused2, \
326 double b0, double b1, double b2, \
327 double a1, double a2, int *clippings, \
330 const type *ibuf = input; \
331 type *obuf = output; \
334 double wet = s->mix; \
335 double dry = 1. - wet; \
341 for (int i = 0; i < len; i++) { \
343 out = b0 * in + w1; \
344 w1 = b1 * in + w2 + a1 * out; \
345 w2 = b2 * in + a2 * out; \
346 out = out * wet + in * dry; \
349 } else if (need_clipping && out < min) { \
352 } else if (need_clipping && out > max) { \
368 #define BIQUAD_LATT_FILTER(name, type, min, max, need_clipping) \
369 static void biquad_latt_## name (BiquadsContext *s, \
370 const void *input, void *output, int len, \
371 double *z1, double *z2, \
372 double *unused1, double *unused2, \
373 double v0, double v1, double v2, \
374 double k0, double k1, int *clippings, \
377 const type *ibuf = input; \
378 type *obuf = output; \
381 double wet = s->mix; \
382 double dry = 1. - wet; \
386 for (int i = 0; i < len; i++) { \
401 out = out * wet + in * dry; \
404 } else if (need_clipping && out < min) { \
407 } else if (need_clipping && out > max) { \
423 #define BIQUAD_SVF_FILTER(name, type, min, max, need_clipping) \
424 static void biquad_svf_## name (BiquadsContext *s, \
425 const void *input, void *output, int len, \
426 double *y0, double *y1, \
427 double *unused1, double *unused2, \
428 double b0, double b1, double b2, \
429 double a1, double a2, int *clippings, \
432 const type *ibuf = input; \
433 type *obuf = output; \
436 double wet = s->mix; \
437 double dry = 1. - wet; \
441 for (int i = 0; i < len; i++) { \
443 out = b2 * in + s0; \
444 t0 = b0 * in + a1 * s0 + s1; \
445 t1 = b1 * in + a2 * s0; \
449 out = out * wet + in * dry; \
452 } else if (need_clipping && out < min) { \
455 } else if (need_clipping && out > max) { \
473 double k0, k1,
v0, v1, v2;
476 k0 =
s->a1 / (1. + k1);
478 v1 =
s->b1 - v2 *
s->a1;
479 v0 =
s->b0 - v1 * k0 - v2 * k1;
495 b[0] =
s->b1 -
s->a1 *
s->b0;
496 b[1] =
s->b2 -
s->a2 *
s->b0;
512 double w0 = 2 *
M_PI *
s->frequency /
inlink->sample_rate;
513 double K = tan(w0 / 2.);
516 s->bypass = (((w0 >
M_PI || w0 <= 0.) && reset) || (
s->width <= 0.)) && (
s->filter_type !=
biquad);
522 if ((w0 >
M_PI || w0 <= 0.) && (
s->filter_type !=
biquad))
525 switch (
s->width_type) {
530 alpha = sin(w0) / (2 *
s->frequency /
s->width);
533 alpha = sin(w0) / (2 *
s->frequency / (
s->width * 1000));
536 alpha = sin(w0) * sinh(log(2.) / 2 *
s->width * w0 / sin(w0));
539 alpha = sin(w0) / (2 *
s->width);
542 alpha = sin(w0) / 2 * sqrt((
A + 1 /
A) * (1 /
s->width - 1) + 2);
550 switch (
s->filter_type) {
561 s->a1 = -2 * cos(w0);
564 s->b1 = -2 * cos(w0);
568 beta = sqrt((
A *
A + 1) - (
A - 1) * (
A - 1));
572 double ro = -sin(w0 / 2. - M_PI_4) / sin(w0 / 2. + M_PI_4);
573 double n = (
A + 1) / (
A - 1);
574 double alpha1 =
A == 1. ? 0. : n -
FFSIGN(n) * sqrt(n * n - 1);
575 double beta0 = ((1 +
A) + (1 -
A) * alpha1) * 0.5;
576 double beta1 = ((1 -
A) + (1 +
A) * alpha1) * 0.5;
578 s->a0 = 1 + ro * alpha1;
579 s->a1 = -ro - alpha1;
581 s->b0 = beta0 + ro * beta1;
582 s->b1 = -beta1 - ro * beta0;
585 s->a0 = (
A + 1) + (
A - 1) * cos(w0) + beta *
alpha;
586 s->a1 = -2 * ((
A - 1) + (
A + 1) * cos(w0));
587 s->a2 = (
A + 1) + (
A - 1) * cos(w0) - beta *
alpha;
588 s->b0 =
A * ((
A + 1) - (
A - 1) * cos(w0) + beta *
alpha);
589 s->b1 = 2 *
A * ((
A - 1) - (
A + 1) * cos(w0));
590 s->b2 =
A * ((
A + 1) - (
A - 1) * cos(w0) - beta *
alpha);
594 beta = sqrt((
A *
A + 1) - (
A - 1) * (
A - 1));
598 double ro = sin(w0 / 2. - M_PI_4) / sin(w0 / 2. + M_PI_4);
599 double n = (
A + 1) / (
A - 1);
600 double alpha1 =
A == 1. ? 0. : n -
FFSIGN(n) * sqrt(n * n - 1);
601 double beta0 = ((1 +
A) + (1 -
A) * alpha1) * 0.5;
602 double beta1 = ((1 -
A) + (1 +
A) * alpha1) * 0.5;
604 s->a0 = 1 + ro * alpha1;
607 s->b0 = beta0 + ro * beta1;
608 s->b1 = beta1 + ro * beta0;
611 s->a0 = (
A + 1) - (
A - 1) * cos(w0) + beta *
alpha;
612 s->a1 = 2 * ((
A - 1) - (
A + 1) * cos(w0));
613 s->a2 = (
A + 1) - (
A - 1) * cos(w0) - beta *
alpha;
614 s->b0 =
A * ((
A + 1) + (
A - 1) * cos(w0) + beta *
alpha);
615 s->b1 =-2 *
A * ((
A - 1) + (
A + 1) * cos(w0));
616 s->b2 =
A * ((
A + 1) + (
A - 1) * cos(w0) - beta *
alpha);
622 s->a1 = -2 * cos(w0);
626 s->b2 = -sin(w0) / 2;
629 s->a1 = -2 * cos(w0);
638 s->a1 = -2 * cos(w0);
641 s->b1 = -2 * cos(w0);
654 s->a1 = -2 * cos(w0);
656 s->b0 = (1 - cos(w0)) / 2;
658 s->b2 = (1 - cos(w0)) / 2;
666 s->b0 = (1 -
s->a1) / 2;
671 s->a1 = -2 * cos(w0);
673 s->b0 = (1 + cos(w0)) / 2;
674 s->b1 = -(1 + cos(w0));
675 s->b2 = (1 + cos(w0)) / 2;
682 s->a1 = -(1. - K) / (1. + K);
690 s->a1 = -2 * cos(w0);
693 s->b1 = -2 * cos(w0);
711 if (
s->normalize &&
fabs(
s->b0 +
s->b1 +
s->b2) > 1e-6) {
712 double factor = (
s->a0 +
s->a1 +
s->a2) / (
s->b0 +
s->b1 +
s->b2);
725 switch (
s->transform_type) {
729 s->filter = biquad_s16;
732 s->filter = biquad_s32;
735 s->filter = biquad_flt;
738 s->filter = biquad_dbl;
746 s->filter = biquad_dii_s16;
749 s->filter = biquad_dii_s32;
752 s->filter = biquad_dii_flt;
755 s->filter = biquad_dii_dbl;
763 s->filter = biquad_tdii_s16;
766 s->filter = biquad_tdii_s32;
769 s->filter = biquad_tdii_flt;
772 s->filter = biquad_tdii_dbl;
780 s->filter = biquad_latt_s16;
783 s->filter = biquad_latt_s32;
786 s->filter = biquad_latt_flt;
789 s->filter = biquad_latt_dbl;
797 s->filter = biquad_svf_s16;
800 s->filter = biquad_svf_s32;
803 s->filter = biquad_svf_flt;
806 s->filter = biquad_svf_dbl;
817 if (
s->transform_type ==
LATT)
819 else if (
s->transform_type ==
SVF)
841 const int start = (buf->
channels * jobnr) / nb_jobs;
842 const int end = (buf->
channels * (jobnr+1)) / nb_jobs;
845 for (ch = start; ch < end; ch++) {
854 &
s->cache[ch].i1, &
s->cache[ch].i2, &
s->cache[ch].o1, &
s->cache[ch].o2,
855 s->b0,
s->b1,
s->b2,
s->a1,
s->a2, &
s->cache[ch].clippings,
ctx->is_disabled);
889 for (ch = 0; ch < outlink->
channels; ch++) {
890 if (
s->cache[ch].clippings > 0)
892 ch,
s->cache[ch].clippings);
893 s->cache[ch].clippings = 0;
903 char *res,
int res_len,
int flags)
938 #define OFFSET(x) offsetof(BiquadsContext, x)
939 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
940 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
942 #define DEFINE_BIQUAD_FILTER_2(name_, description_, priv_class_) \
943 static av_cold int name_##_init(AVFilterContext *ctx) \
945 BiquadsContext *s = ctx->priv; \
946 s->filter_type = name_; \
950 const AVFilter ff_af_##name_ = { \
952 .description = NULL_IF_CONFIG_SMALL(description_), \
953 .priv_class = &priv_class_##_class, \
954 .priv_size = sizeof(BiquadsContext), \
955 .init = name_##_init, \
957 FILTER_INPUTS(inputs), \
958 FILTER_OUTPUTS(outputs), \
959 FILTER_QUERY_FUNC(query_formats), \
960 .process_command = process_command, \
961 .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, \
964 #define DEFINE_BIQUAD_FILTER(name, description) \
965 AVFILTER_DEFINE_CLASS(name); \
966 DEFINE_BIQUAD_FILTER_2(name, description, name)
968 #if CONFIG_EQUALIZER_FILTER
969 static const AVOption equalizer_options[] = {
996 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1008 #if CONFIG_BASS_FILTER || CONFIG_LOWSHELF_FILTER
1009 static const AVOption bass_lowshelf_options[] = {
1038 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1049 #if CONFIG_BASS_FILTER
1053 #if CONFIG_LOWSHELF_FILTER
1057 #if CONFIG_TREBLE_FILTER || CONFIG_HIGHSHELF_FILTER
1058 static const AVOption treble_highshelf_options[] = {
1087 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1098 treble_highshelf_options);
1100 #if CONFIG_TREBLE_FILTER
1104 #if CONFIG_HIGHSHELF_FILTER
1108 #if CONFIG_BANDPASS_FILTER
1109 static const AVOption bandpass_options[] = {
1135 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1147 #if CONFIG_BANDREJECT_FILTER
1148 static const AVOption bandreject_options[] = {
1173 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1185 #if CONFIG_LOWPASS_FILTER
1186 static const AVOption lowpass_options[] = {
1213 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1225 #if CONFIG_HIGHPASS_FILTER
1226 static const AVOption highpass_options[] = {
1253 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1265 #if CONFIG_ALLPASS_FILTER
1266 static const AVOption allpass_options[] = {
1293 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1305 #if CONFIG_BIQUAD_FILTER
1306 static const AVOption biquad_options[] = {
1326 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},