140 double *i1,
double *i2,
double *o1,
double *o2,
141 double b0,
double b1,
double b2,
double a1,
double a2,
int *clippings,
170 switch (
s->precision) {
203 #define BIQUAD_FILTER(name, type, min, max, need_clipping) \
204 static void biquad_## name (BiquadsContext *s, \
205 const void *input, void *output, int len, \
206 double *in1, double *in2, \
207 double *out1, double *out2, \
208 double b0, double b1, double b2, \
209 double a1, double a2, int *clippings, \
212 const type *ibuf = input; \
213 type *obuf = output; \
218 double wet = s->mix; \
219 double dry = 1. - wet; \
225 for (i = 0; i+1 < len; i++) { \
226 o2 = i2 * b2 + i1 * b1 + ibuf[i] * b0 + o2 * a2 + o1 * a1; \
228 out = o2 * wet + i2 * dry; \
231 } else if (need_clipping && out < min) { \
234 } else if (need_clipping && out > max) { \
241 o1 = i1 * b2 + i2 * b1 + ibuf[i] * b0 + o1 * a2 + o2 * a1; \
243 out = o1 * wet + i1 * dry; \
246 } else if (need_clipping && out < min) { \
249 } else if (need_clipping && out > max) { \
257 double o0 = ibuf[i] * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
262 out = o0 * wet + i1 * dry; \
265 } else if (need_clipping && out < min) { \
268 } else if (need_clipping && out > max) { \
286 #define BIQUAD_DII_FILTER(name, type, min, max, need_clipping) \
287 static void biquad_dii_## name (BiquadsContext *s, \
288 const void *input, void *output, int len, \
289 double *z1, double *z2, \
290 double *unused1, double *unused2, \
291 double b0, double b1, double b2, \
292 double a1, double a2, int *clippings, \
295 const type *ibuf = input; \
296 type *obuf = output; \
299 double wet = s->mix; \
300 double dry = 1. - wet; \
301 double in, out, w0; \
306 for (int i = 0; i < len; i++) { \
308 w0 = in + a1 * w1 + a2 * w2; \
309 out = b0 * w0 + b1 * w1 + b2 * w2; \
312 out = out * wet + in * dry; \
315 } else if (need_clipping && out < min) { \
318 } else if (need_clipping && out > max) { \
334 #define BIQUAD_TDII_FILTER(name, type, min, max, need_clipping) \
335 static void biquad_tdii_## name (BiquadsContext *s, \
336 const void *input, void *output, int len, \
337 double *z1, double *z2, \
338 double *unused1, double *unused2, \
339 double b0, double b1, double b2, \
340 double a1, double a2, int *clippings, \
343 const type *ibuf = input; \
344 type *obuf = output; \
347 double wet = s->mix; \
348 double dry = 1. - wet; \
354 for (int i = 0; i < len; i++) { \
356 out = b0 * in + w1; \
357 w1 = b1 * in + w2 + a1 * out; \
358 w2 = b2 * in + a2 * out; \
359 out = out * wet + in * dry; \
362 } else if (need_clipping && out < min) { \
365 } else if (need_clipping && out > max) { \
381 #define BIQUAD_LATT_FILTER(name, type, min, max, need_clipping) \
382 static void biquad_latt_## name (BiquadsContext *s, \
383 const void *input, void *output, int len, \
384 double *z1, double *z2, \
385 double *unused1, double *unused2, \
386 double v0, double v1, double v2, \
387 double k0, double k1, int *clippings, \
390 const type *ibuf = input; \
391 type *obuf = output; \
394 double wet = s->mix; \
395 double dry = 1. - wet; \
399 for (int i = 0; i < len; i++) { \
414 out = out * wet + in * dry; \
417 } else if (need_clipping && out < min) { \
420 } else if (need_clipping && out > max) { \
438 double k0, k1,
v0, v1, v2;
441 k0 =
s->a1 / (1. + k1);
443 v1 =
s->b1 - v2 *
s->a1;
444 v0 =
s->b0 - v1 * k0 - v2 * k1;
459 double w0 = 2 *
M_PI *
s->frequency /
inlink->sample_rate;
460 double K = tan(w0 / 2.);
463 s->bypass = (((w0 >
M_PI || w0 <= 0.) && reset) || (
s->width <= 0.)) && (
s->filter_type !=
biquad);
469 if ((w0 >
M_PI || w0 <= 0.) && (
s->filter_type !=
biquad))
472 switch (
s->width_type) {
477 alpha = sin(w0) / (2 *
s->frequency /
s->width);
480 alpha = sin(w0) / (2 *
s->frequency / (
s->width * 1000));
483 alpha = sin(w0) * sinh(log(2.) / 2 *
s->width * w0 / sin(w0));
486 alpha = sin(w0) / (2 *
s->width);
489 alpha = sin(w0) / 2 * sqrt((
A + 1 /
A) * (1 /
s->width - 1) + 2);
497 switch (
s->filter_type) {
508 s->a1 = -2 * cos(w0);
511 s->b1 = -2 * cos(w0);
515 beta = sqrt((
A *
A + 1) - (
A - 1) * (
A - 1));
519 double ro = -sin(w0 / 2. - M_PI_4) / sin(w0 / 2. + M_PI_4);
520 double n = (
A + 1) / (
A - 1);
521 double alpha1 =
A == 1. ? 0. : n -
FFSIGN(n) * sqrt(n * n - 1);
522 double beta0 = ((1 +
A) + (1 -
A) * alpha1) * 0.5;
523 double beta1 = ((1 -
A) + (1 +
A) * alpha1) * 0.5;
525 s->a0 = 1 + ro * alpha1;
526 s->a1 = -ro - alpha1;
528 s->b0 = beta0 + ro * beta1;
529 s->b1 = -beta1 - ro * beta0;
532 s->a0 = (
A + 1) + (
A - 1) * cos(w0) + beta *
alpha;
533 s->a1 = -2 * ((
A - 1) + (
A + 1) * cos(w0));
534 s->a2 = (
A + 1) + (
A - 1) * cos(w0) - beta *
alpha;
535 s->b0 =
A * ((
A + 1) - (
A - 1) * cos(w0) + beta *
alpha);
536 s->b1 = 2 *
A * ((
A - 1) - (
A + 1) * cos(w0));
537 s->b2 =
A * ((
A + 1) - (
A - 1) * cos(w0) - beta *
alpha);
541 beta = sqrt((
A *
A + 1) - (
A - 1) * (
A - 1));
545 double ro = sin(w0 / 2. - M_PI_4) / sin(w0 / 2. + M_PI_4);
546 double n = (
A + 1) / (
A - 1);
547 double alpha1 =
A == 1. ? 0. : n -
FFSIGN(n) * sqrt(n * n - 1);
548 double beta0 = ((1 +
A) + (1 -
A) * alpha1) * 0.5;
549 double beta1 = ((1 -
A) + (1 +
A) * alpha1) * 0.5;
551 s->a0 = 1 + ro * alpha1;
554 s->b0 = beta0 + ro * beta1;
555 s->b1 = beta1 + ro * beta0;
558 s->a0 = (
A + 1) - (
A - 1) * cos(w0) + beta *
alpha;
559 s->a1 = 2 * ((
A - 1) - (
A + 1) * cos(w0));
560 s->a2 = (
A + 1) - (
A - 1) * cos(w0) - beta *
alpha;
561 s->b0 =
A * ((
A + 1) + (
A - 1) * cos(w0) + beta *
alpha);
562 s->b1 =-2 *
A * ((
A - 1) + (
A + 1) * cos(w0));
563 s->b2 =
A * ((
A + 1) + (
A - 1) * cos(w0) - beta *
alpha);
569 s->a1 = -2 * cos(w0);
573 s->b2 = -sin(w0) / 2;
576 s->a1 = -2 * cos(w0);
585 s->a1 = -2 * cos(w0);
588 s->b1 = -2 * cos(w0);
601 s->a1 = -2 * cos(w0);
603 s->b0 = (1 - cos(w0)) / 2;
605 s->b2 = (1 - cos(w0)) / 2;
613 s->b0 = (1 -
s->a1) / 2;
618 s->a1 = -2 * cos(w0);
620 s->b0 = (1 + cos(w0)) / 2;
621 s->b1 = -(1 + cos(w0));
622 s->b2 = (1 + cos(w0)) / 2;
629 s->a1 = -(1. - K) / (1. + K);
637 s->a1 = -2 * cos(w0);
640 s->b1 = -2 * cos(w0);
658 if (
s->normalize &&
fabs(
s->b0 +
s->b1 +
s->b2) > 1e-6) {
659 double factor = (
s->a0 +
s->a1 +
s->a2) / (
s->b0 +
s->b1 +
s->b2);
672 switch (
s->transform_type) {
676 s->filter = biquad_s16;
679 s->filter = biquad_s32;
682 s->filter = biquad_flt;
685 s->filter = biquad_dbl;
693 s->filter = biquad_dii_s16;
696 s->filter = biquad_dii_s32;
699 s->filter = biquad_dii_flt;
702 s->filter = biquad_dii_dbl;
710 s->filter = biquad_tdii_s16;
713 s->filter = biquad_tdii_s32;
716 s->filter = biquad_tdii_flt;
719 s->filter = biquad_tdii_dbl;
727 s->filter = biquad_latt_s16;
730 s->filter = biquad_latt_s32;
733 s->filter = biquad_latt_flt;
736 s->filter = biquad_latt_dbl;
747 if (
s->transform_type ==
LATT)
769 const int start = (buf->
channels * jobnr) / nb_jobs;
770 const int end = (buf->
channels * (jobnr+1)) / nb_jobs;
773 for (ch = start; ch < end; ch++) {
782 &
s->cache[ch].i1, &
s->cache[ch].i2, &
s->cache[ch].o1, &
s->cache[ch].o2,
783 s->b0,
s->b1,
s->b2,
s->a1,
s->a2, &
s->cache[ch].clippings,
ctx->is_disabled);
816 for (ch = 0; ch < outlink->
channels; ch++) {
817 if (
s->cache[ch].clippings > 0)
819 ch,
s->cache[ch].clippings);
820 s->cache[ch].clippings = 0;
830 char *res,
int res_len,
int flags)
867 #define OFFSET(x) offsetof(BiquadsContext, x)
868 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
869 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
871 #define DEFINE_BIQUAD_FILTER(name_, description_) \
872 AVFILTER_DEFINE_CLASS(name_); \
873 static av_cold int name_##_init(AVFilterContext *ctx) \
875 BiquadsContext *s = ctx->priv; \
876 s->filter_type = name_; \
880 AVFilter ff_af_##name_ = { \
882 .description = NULL_IF_CONFIG_SMALL(description_), \
883 .priv_size = sizeof(BiquadsContext), \
884 .init = name_##_init, \
886 .query_formats = query_formats, \
888 .outputs = outputs, \
889 .priv_class = &name_##_class, \
890 .process_command = process_command, \
891 .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, \
894 #if CONFIG_EQUALIZER_FILTER
895 static const AVOption equalizer_options[] = {
921 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
933 #if CONFIG_BASS_FILTER || CONFIG_LOWSHELF_FILTER
934 static const AVOption bass_lowshelf_options[] = {
962 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
972 #if CONFIG_BASS_FILTER
973 #define bass_options bass_lowshelf_options
977 #if CONFIG_LOWSHELF_FILTER
978 #define lowshelf_options bass_lowshelf_options
982 #if CONFIG_TREBLE_FILTER || CONFIG_HIGHSHELF_FILTER
983 static const AVOption treble_highshelf_options[] = {
1011 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1021 #if CONFIG_TREBLE_FILTER
1022 #define treble_options treble_highshelf_options
1026 #if CONFIG_HIGHSHELF_FILTER
1027 #define highshelf_options treble_highshelf_options
1031 #if CONFIG_BANDPASS_FILTER
1032 static const AVOption bandpass_options[] = {
1057 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1069 #if CONFIG_BANDREJECT_FILTER
1070 static const AVOption bandreject_options[] = {
1094 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1106 #if CONFIG_LOWPASS_FILTER
1107 static const AVOption lowpass_options[] = {
1133 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1145 #if CONFIG_HIGHPASS_FILTER
1146 static const AVOption highpass_options[] = {
1172 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1184 #if CONFIG_ALLPASS_FILTER
1185 static const AVOption allpass_options[] = {
1211 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},
1223 #if CONFIG_BIQUAD_FILTER
1224 static const AVOption biquad_options[] = {
1243 {
"precision",
"set filtering precision",
OFFSET(precision),
AV_OPT_TYPE_INT, {.i64=-1}, -1, 3,
AF,
"precision"},