Imported Upstream version 5.1.2
[platform/upstream/ffmpeg.git] / libavfilter / af_biquads.c
1 /*
2  * Copyright (c) 2013 Paul B Mahol
3  * Copyright (c) 2006-2008 Rob Sykes <robs@users.sourceforge.net>
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 /*
23  * 2-pole filters designed by Robert Bristow-Johnson <rbj@audioimagination.com>
24  *   see http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
25  *
26  * 1-pole filters based on code (c) 2000 Chris Bagwell <cbagwell@sprynet.com>
27  *   Algorithms: Recursive single pole low/high pass filter
28  *   Reference: The Scientist and Engineer's Guide to Digital Signal Processing
29  *
30  *   low-pass: output[N] = input[N] * A + output[N-1] * B
31  *     X = exp(-2.0 * pi * Fc)
32  *     A = 1 - X
33  *     B = X
34  *     Fc = cutoff freq / sample rate
35  *
36  *     Mimics an RC low-pass filter:
37  *
38  *     ---/\/\/\/\----------->
39  *                   |
40  *                  --- C
41  *                  ---
42  *                   |
43  *                   |
44  *                   V
45  *
46  *   high-pass: output[N] = A0 * input[N] + A1 * input[N-1] + B1 * output[N-1]
47  *     X  = exp(-2.0 * pi * Fc)
48  *     A0 = (1 + X) / 2
49  *     A1 = -(1 + X) / 2
50  *     B1 = X
51  *     Fc = cutoff freq / sample rate
52  *
53  *     Mimics an RC high-pass filter:
54  *
55  *         || C
56  *     ----||--------->
57  *         ||    |
58  *               <
59  *               > R
60  *               <
61  *               |
62  *               V
63  */
64
65 #include "config_components.h"
66
67 #include "libavutil/avassert.h"
68 #include "libavutil/channel_layout.h"
69 #include "libavutil/ffmath.h"
70 #include "libavutil/opt.h"
71 #include "audio.h"
72 #include "avfilter.h"
73 #include "filters.h"
74 #include "internal.h"
75
76 enum FilterType {
77     biquad,
78     equalizer,
79     bass,
80     treble,
81     bandpass,
82     bandreject,
83     allpass,
84     highpass,
85     lowpass,
86     lowshelf,
87     highshelf,
88     tiltshelf,
89 };
90
91 enum WidthType {
92     NONE,
93     HERTZ,
94     OCTAVE,
95     QFACTOR,
96     SLOPE,
97     KHERTZ,
98     NB_WTYPE,
99 };
100
101 enum TransformType {
102     DI,
103     DII,
104     TDI,
105     TDII,
106     LATT,
107     SVF,
108     ZDF,
109     NB_TTYPE,
110 };
111
112 typedef struct ChanCache {
113     double i1, i2;
114     double o1, o2;
115     double ri1, ri2;
116     double ro1, ro2;
117     int clippings;
118 } ChanCache;
119
120 typedef struct BiquadsContext {
121     const AVClass *class;
122
123     enum FilterType filter_type;
124     int width_type;
125     int poles;
126     int csg;
127     int transform_type;
128     int precision;
129     int block_samples;
130
131     int bypass;
132
133     double gain;
134     double frequency;
135     double width;
136     double mix;
137     char *ch_layout_str;
138     AVChannelLayout ch_layout;
139     int normalize;
140     int order;
141
142     double a0, a1, a2;
143     double b0, b1, b2;
144
145     double oa0, oa1, oa2;
146     double ob0, ob1, ob2;
147
148     AVFrame *block[3];
149
150     ChanCache *cache;
151     int block_align;
152
153     int64_t pts;
154     int nb_samples;
155
156     void (*filter)(struct BiquadsContext *s, const void *ibuf, void *obuf, int len,
157                    double *i1, double *i2, double *o1, double *o2,
158                    double b0, double b1, double b2, double a0, double a1, double a2, int *clippings,
159                    int disabled);
160 } BiquadsContext;
161
162 static int query_formats(AVFilterContext *ctx)
163 {
164     BiquadsContext *s = ctx->priv;
165     static const enum AVSampleFormat auto_sample_fmts[] = {
166         AV_SAMPLE_FMT_S16P,
167         AV_SAMPLE_FMT_S32P,
168         AV_SAMPLE_FMT_FLTP,
169         AV_SAMPLE_FMT_DBLP,
170         AV_SAMPLE_FMT_NONE
171     };
172     enum AVSampleFormat sample_fmts[] = {
173         AV_SAMPLE_FMT_S16P,
174         AV_SAMPLE_FMT_NONE
175     };
176     const enum AVSampleFormat *sample_fmts_list = sample_fmts;
177     int ret = ff_set_common_all_channel_counts(ctx);
178     if (ret < 0)
179         return ret;
180
181     switch (s->precision) {
182     case 0:
183         sample_fmts[0] = AV_SAMPLE_FMT_S16P;
184         break;
185     case 1:
186         sample_fmts[0] = AV_SAMPLE_FMT_S32P;
187         break;
188     case 2:
189         sample_fmts[0] = AV_SAMPLE_FMT_FLTP;
190         break;
191     case 3:
192         sample_fmts[0] = AV_SAMPLE_FMT_DBLP;
193         break;
194     default:
195         sample_fmts_list = auto_sample_fmts;
196         break;
197     }
198     ret = ff_set_common_formats_from_list(ctx, sample_fmts_list);
199     if (ret < 0)
200         return ret;
201
202     return ff_set_common_all_samplerates(ctx);
203 }
204
205 #define BIQUAD_FILTER(name, type, min, max, need_clipping)                    \
206 static void biquad_## name (BiquadsContext *s,                                \
207                             const void *input, void *output, int len,         \
208                             double *in1, double *in2,                         \
209                             double *out1, double *out2,                       \
210                             double b0, double b1, double b2,                  \
211                             double a0, double a1, double a2, int *clippings,  \
212                             int disabled)                                     \
213 {                                                                             \
214     const type *ibuf = input;                                                 \
215     type *obuf = output;                                                      \
216     double i1 = *in1;                                                         \
217     double i2 = *in2;                                                         \
218     double o1 = *out1;                                                        \
219     double o2 = *out2;                                                        \
220     double wet = s->mix;                                                      \
221     double dry = 1. - wet;                                                    \
222     double out;                                                               \
223     int i;                                                                    \
224     a1 = -a1;                                                                 \
225     a2 = -a2;                                                                 \
226                                                                               \
227     for (i = 0; i+1 < len; i++) {                                             \
228         o2 = i2 * b2 + i1 * b1 + ibuf[i] * b0 + o2 * a2 + o1 * a1;            \
229         i2 = ibuf[i];                                                         \
230         out = o2 * wet + i2 * dry;                                            \
231         if (disabled) {                                                       \
232             obuf[i] = i2;                                                     \
233         } else if (need_clipping && out < min) {                              \
234             (*clippings)++;                                                   \
235             obuf[i] = min;                                                    \
236         } else if (need_clipping && out > max) {                              \
237             (*clippings)++;                                                   \
238             obuf[i] = max;                                                    \
239         } else {                                                              \
240             obuf[i] = out;                                                    \
241         }                                                                     \
242         i++;                                                                  \
243         o1 = i1 * b2 + i2 * b1 + ibuf[i] * b0 + o1 * a2 + o2 * a1;            \
244         i1 = ibuf[i];                                                         \
245         out = o1 * wet + i1 * dry;                                            \
246         if (disabled) {                                                       \
247             obuf[i] = i1;                                                     \
248         } else if (need_clipping && out < min) {                              \
249             (*clippings)++;                                                   \
250             obuf[i] = min;                                                    \
251         } else if (need_clipping && out > max) {                              \
252             (*clippings)++;                                                   \
253             obuf[i] = max;                                                    \
254         } else {                                                              \
255             obuf[i] = out;                                                    \
256         }                                                                     \
257     }                                                                         \
258     if (i < len) {                                                            \
259         double o0 = ibuf[i] * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2;     \
260         i2 = i1;                                                              \
261         i1 = ibuf[i];                                                         \
262         o2 = o1;                                                              \
263         o1 = o0;                                                              \
264         out = o0 * wet + i1 * dry;                                            \
265         if (disabled) {                                                       \
266             obuf[i] = i1;                                                     \
267         } else if (need_clipping && out < min) {                              \
268             (*clippings)++;                                                   \
269             obuf[i] = min;                                                    \
270         } else if (need_clipping && out > max) {                              \
271             (*clippings)++;                                                   \
272             obuf[i] = max;                                                    \
273         } else {                                                              \
274             obuf[i] = out;                                                    \
275         }                                                                     \
276     }                                                                         \
277     *in1  = i1;                                                               \
278     *in2  = i2;                                                               \
279     *out1 = o1;                                                               \
280     *out2 = o2;                                                               \
281 }
282
283 BIQUAD_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
284 BIQUAD_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
285 BIQUAD_FILTER(flt, float,   -1., 1., 0)
286 BIQUAD_FILTER(dbl, double,  -1., 1., 0)
287
288 #define BIQUAD_DII_FILTER(name, type, min, max, need_clipping)                \
289 static void biquad_dii_## name (BiquadsContext *s,                            \
290                             const void *input, void *output, int len,         \
291                             double *z1, double *z2,                           \
292                             double *unused1, double *unused2,                 \
293                             double b0, double b1, double b2,                  \
294                             double a0, double a1, double a2, int *clippings,  \
295                             int disabled)                                     \
296 {                                                                             \
297     const type *ibuf = input;                                                 \
298     type *obuf = output;                                                      \
299     double w1 = *z1;                                                          \
300     double w2 = *z2;                                                          \
301     double wet = s->mix;                                                      \
302     double dry = 1. - wet;                                                    \
303     double in, out, w0;                                                       \
304                                                                               \
305     a1 = -a1;                                                                 \
306     a2 = -a2;                                                                 \
307                                                                               \
308     for (int i = 0; i < len; i++) {                                           \
309         in = ibuf[i];                                                         \
310         w0 = in + a1 * w1 + a2 * w2;                                          \
311         out = b0 * w0 + b1 * w1 + b2 * w2;                                    \
312         w2 = w1;                                                              \
313         w1 = w0;                                                              \
314         out = out * wet + in * dry;                                           \
315         if (disabled) {                                                       \
316             obuf[i] = in;                                                     \
317         } else if (need_clipping && out < min) {                              \
318             (*clippings)++;                                                   \
319             obuf[i] = min;                                                    \
320         } else if (need_clipping && out > max) {                              \
321             (*clippings)++;                                                   \
322             obuf[i] = max;                                                    \
323         } else {                                                              \
324             obuf[i] = out;                                                    \
325         }                                                                     \
326     }                                                                         \
327     *z1 = w1;                                                                 \
328     *z2 = w2;                                                                 \
329 }
330
331 BIQUAD_DII_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
332 BIQUAD_DII_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
333 BIQUAD_DII_FILTER(flt, float,   -1., 1., 0)
334 BIQUAD_DII_FILTER(dbl, double,  -1., 1., 0)
335
336 #define BIQUAD_TDI_FILTER(name, type, min, max, need_clipping)                \
337 static void biquad_tdi_## name (BiquadsContext *s,                            \
338                             const void *input, void *output, int len,         \
339                             double *z1, double *z2,                           \
340                             double *z3, double *z4,                           \
341                             double b0, double b1, double b2,                  \
342                             double a0, double a1, double a2, int *clippings,  \
343                             int disabled)                                     \
344 {                                                                             \
345     const type *ibuf = input;                                                 \
346     type *obuf = output;                                                      \
347     double s1 = *z1;                                                          \
348     double s2 = *z2;                                                          \
349     double s3 = *z3;                                                          \
350     double s4 = *z4;                                                          \
351     double wet = s->mix;                                                      \
352     double dry = 1. - wet;                                                    \
353     double in, out;                                                           \
354                                                                               \
355     a1 = -a1;                                                                 \
356     a2 = -a2;                                                                 \
357                                                                               \
358     for (int i = 0; i < len; i++) {                                           \
359         double t1, t2, t3, t4;                                                \
360         in = ibuf[i] + s1;                                                    \
361         t1 = in * a1 + s2;                                                    \
362         t2 = in * a2;                                                         \
363         t3 = in * b1 + s4;                                                    \
364         t4 = in * b2;                                                         \
365         out = b0 * in + s3;                                                   \
366         out = out * wet + in * dry;                                           \
367         s1 = t1; s2 = t2; s3 = t3; s4 = t4;                                   \
368         if (disabled) {                                                       \
369             obuf[i] = in;                                                     \
370         } else if (need_clipping && out < min) {                              \
371             (*clippings)++;                                                   \
372             obuf[i] = min;                                                    \
373         } else if (need_clipping && out > max) {                              \
374             (*clippings)++;                                                   \
375             obuf[i] = max;                                                    \
376         } else {                                                              \
377             obuf[i] = out;                                                    \
378         }                                                                     \
379     }                                                                         \
380                                                                               \
381     *z1 = s1;                                                                 \
382     *z2 = s2;                                                                 \
383     *z3 = s3;                                                                 \
384     *z4 = s4;                                                                 \
385 }
386
387 BIQUAD_TDI_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
388 BIQUAD_TDI_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
389 BIQUAD_TDI_FILTER(flt, float,   -1., 1., 0)
390 BIQUAD_TDI_FILTER(dbl, double,  -1., 1., 0)
391
392 #define BIQUAD_TDII_FILTER(name, type, min, max, need_clipping)               \
393 static void biquad_tdii_## name (BiquadsContext *s,                           \
394                             const void *input, void *output, int len,         \
395                             double *z1, double *z2,                           \
396                             double *unused1, double *unused2,                 \
397                             double b0, double b1, double b2,                  \
398                             double a0, double a1, double a2, int *clippings,  \
399                             int disabled)                                     \
400 {                                                                             \
401     const type *ibuf = input;                                                 \
402     type *obuf = output;                                                      \
403     double w1 = *z1;                                                          \
404     double w2 = *z2;                                                          \
405     double wet = s->mix;                                                      \
406     double dry = 1. - wet;                                                    \
407     double in, out;                                                           \
408                                                                               \
409     a1 = -a1;                                                                 \
410     a2 = -a2;                                                                 \
411                                                                               \
412     for (int i = 0; i < len; i++) {                                           \
413         in = ibuf[i];                                                         \
414         out = b0 * in + w1;                                                   \
415         w1 = b1 * in + w2 + a1 * out;                                         \
416         w2 = b2 * in + a2 * out;                                              \
417         out = out * wet + in * dry;                                           \
418         if (disabled) {                                                       \
419             obuf[i] = in;                                                     \
420         } else if (need_clipping && out < min) {                              \
421             (*clippings)++;                                                   \
422             obuf[i] = min;                                                    \
423         } else if (need_clipping && out > max) {                              \
424             (*clippings)++;                                                   \
425             obuf[i] = max;                                                    \
426         } else {                                                              \
427             obuf[i] = out;                                                    \
428         }                                                                     \
429     }                                                                         \
430     *z1 = w1;                                                                 \
431     *z2 = w2;                                                                 \
432 }
433
434 BIQUAD_TDII_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
435 BIQUAD_TDII_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
436 BIQUAD_TDII_FILTER(flt, float,   -1., 1., 0)
437 BIQUAD_TDII_FILTER(dbl, double,  -1., 1., 0)
438
439 #define BIQUAD_LATT_FILTER(name, type, min, max, need_clipping)               \
440 static void biquad_latt_## name (BiquadsContext *s,                           \
441                            const void *input, void *output, int len,          \
442                            double *z1, double *z2,                            \
443                            double *unused1, double *unused2,                  \
444                            double v0, double v1, double v2,                   \
445                            double unused, double k0, double k1,               \
446                            int *clippings,                                    \
447                            int disabled)                                      \
448 {                                                                             \
449     const type *ibuf = input;                                                 \
450     type *obuf = output;                                                      \
451     double s0 = *z1;                                                          \
452     double s1 = *z2;                                                          \
453     double wet = s->mix;                                                      \
454     double dry = 1. - wet;                                                    \
455     double in, out;                                                           \
456     double t0, t1;                                                            \
457                                                                               \
458     for (int i = 0; i < len; i++) {                                           \
459         out  = 0.;                                                            \
460         in   = ibuf[i];                                                       \
461         t0   = in - k1 * s0;                                                  \
462         t1   = t0 * k1 + s0;                                                  \
463         out += t1 * v2;                                                       \
464                                                                               \
465         t0    = t0 - k0 * s1;                                                 \
466         t1    = t0 * k0 + s1;                                                 \
467         out  += t1 * v1;                                                      \
468                                                                               \
469         out  += t0 * v0;                                                      \
470         s0    = t1;                                                           \
471         s1    = t0;                                                           \
472                                                                               \
473         out = out * wet + in * dry;                                           \
474         if (disabled) {                                                       \
475             obuf[i] = in;                                                     \
476         } else if (need_clipping && out < min) {                              \
477             (*clippings)++;                                                   \
478             obuf[i] = min;                                                    \
479         } else if (need_clipping && out > max) {                              \
480             (*clippings)++;                                                   \
481             obuf[i] = max;                                                    \
482         } else {                                                              \
483             obuf[i] = out;                                                    \
484         }                                                                     \
485     }                                                                         \
486     *z1 = s0;                                                                 \
487     *z2 = s1;                                                                 \
488 }
489
490 BIQUAD_LATT_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
491 BIQUAD_LATT_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
492 BIQUAD_LATT_FILTER(flt, float,   -1., 1., 0)
493 BIQUAD_LATT_FILTER(dbl, double,  -1., 1., 0)
494
495 #define BIQUAD_SVF_FILTER(name, type, min, max, need_clipping)                \
496 static void biquad_svf_## name (BiquadsContext *s,                            \
497                            const void *input, void *output, int len,          \
498                            double *y0, double *y1,                            \
499                            double *unused1, double *unused2,                  \
500                            double b0, double b1, double b2,                   \
501                            double a0, double a1, double a2, int *clippings,   \
502                            int disabled)                                      \
503 {                                                                             \
504     const type *ibuf = input;                                                 \
505     type *obuf = output;                                                      \
506     double s0 = *y0;                                                          \
507     double s1 = *y1;                                                          \
508     double wet = s->mix;                                                      \
509     double dry = 1. - wet;                                                    \
510     double in, out;                                                           \
511     double t0, t1;                                                            \
512                                                                               \
513     for (int i = 0; i < len; i++) {                                           \
514         in   = ibuf[i];                                                       \
515         out  = b2 * in + s0;                                                  \
516         t0   = b0 * in + a1 * s0 + s1;                                        \
517         t1   = b1 * in + a2 * s0;                                             \
518         s0   = t0;                                                            \
519         s1   = t1;                                                            \
520                                                                               \
521         out = out * wet + in * dry;                                           \
522         if (disabled) {                                                       \
523             obuf[i] = in;                                                     \
524         } else if (need_clipping && out < min) {                              \
525             (*clippings)++;                                                   \
526             obuf[i] = min;                                                    \
527         } else if (need_clipping && out > max) {                              \
528             (*clippings)++;                                                   \
529             obuf[i] = max;                                                    \
530         } else {                                                              \
531             obuf[i] = out;                                                    \
532         }                                                                     \
533     }                                                                         \
534     *y0 = s0;                                                                 \
535     *y1 = s1;                                                                 \
536 }
537
538 BIQUAD_SVF_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
539 BIQUAD_SVF_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
540 BIQUAD_SVF_FILTER(flt, float,   -1., 1., 0)
541 BIQUAD_SVF_FILTER(dbl, double,  -1., 1., 0)
542
543 #define BIQUAD_ZDF_FILTER(name, type, min, max, need_clipping)                \
544 static void biquad_zdf_## name (BiquadsContext *s,                            \
545                            const void *input, void *output, int len,          \
546                            double *y0, double *y1,                            \
547                            double *unused1, double *unused2,                  \
548                            double m0, double m1, double m2,                   \
549                            double a0, double a1, double a2, int *clippings,   \
550                            int disabled)                                      \
551 {                                                                             \
552     const type *ibuf = input;                                                 \
553     type *obuf = output;                                                      \
554     double b0 = *y0;                                                          \
555     double b1 = *y1;                                                          \
556     double wet = s->mix;                                                      \
557     double dry = 1. - wet;                                                    \
558     double out;                                                               \
559                                                                               \
560     for (int i = 0; i < len; i++) {                                           \
561         const double in = ibuf[i];                                            \
562         const double v0 = in;                                                 \
563         const double v3 = v0 - b1;                                            \
564         const double v1 = a0 * b0 + a1 * v3;                                  \
565         const double v2 = b1 + a1 * b0 + a2 * v3;                             \
566                                                                               \
567         b0 = 2. * v1 - b0;                                                    \
568         b1 = 2. * v2 - b1;                                                    \
569                                                                               \
570         out = m0 * v0 + m1 * v1 + m2 * v2;                                    \
571         out = out * wet + in * dry;                                           \
572         if (disabled) {                                                       \
573             obuf[i] = in;                                                     \
574         } else if (need_clipping && out < min) {                              \
575             (*clippings)++;                                                   \
576             obuf[i] = min;                                                    \
577         } else if (need_clipping && out > max) {                              \
578             (*clippings)++;                                                   \
579             obuf[i] = max;                                                    \
580         } else {                                                              \
581             obuf[i] = out;                                                    \
582         }                                                                     \
583     }                                                                         \
584     *y0 = b0;                                                                 \
585     *y1 = b1;                                                                 \
586 }
587
588 BIQUAD_ZDF_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
589 BIQUAD_ZDF_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
590 BIQUAD_ZDF_FILTER(flt, float,   -1., 1., 0)
591 BIQUAD_ZDF_FILTER(dbl, double,  -1., 1., 0)
592
593 static void convert_dir2latt(BiquadsContext *s)
594 {
595     double k0, k1, v0, v1, v2;
596
597     k1 = s->a2;
598     k0 = s->a1 / (1. + k1);
599     v2 = s->b2;
600     v1 = s->b1 - v2 * s->a1;
601     v0 = s->b0 - v1 * k0 - v2 * k1;
602
603     s->a1 = k0;
604     s->a2 = k1;
605     s->b0 = v0;
606     s->b1 = v1;
607     s->b2 = v2;
608 }
609
610 static void convert_dir2svf(BiquadsContext *s)
611 {
612     double a[2];
613     double b[3];
614
615     a[0] = -s->a1;
616     a[1] = -s->a2;
617     b[0] = s->b1 - s->a1 * s->b0;
618     b[1] = s->b2 - s->a2 * s->b0;
619     b[2] = s->b0;
620
621     s->a1 = a[0];
622     s->a2 = a[1];
623     s->b0 = b[0];
624     s->b1 = b[1];
625     s->b2 = b[2];
626 }
627
628 static double convert_width2qfactor(double width,
629                                     double frequency,
630                                     double gain,
631                                     double sample_rate,
632                                     int width_type)
633 {
634     double w0 = 2. * M_PI * frequency / sample_rate;
635     double A = ff_exp10(gain / 40.);
636     double ret;
637
638     switch (width_type) {
639     case NONE:
640     case QFACTOR:
641         ret = width;
642         break;
643     case HERTZ:
644         ret = frequency / width;
645         break;
646     case KHERTZ:
647         ret = frequency / (width * 1000.);
648         break;
649     case OCTAVE:
650         ret = 1. / (2. * sinh(log(2.) / 2. * width * w0 / sin(w0)));
651         break;
652     case SLOPE:
653         ret = 1. / sqrt((A + 1. / A) * (1. / width - 1.) + 2.);
654         break;
655     default:
656         av_assert0(0);
657         break;
658     }
659
660     return ret;
661 }
662
663 static void convert_dir2zdf(BiquadsContext *s, int sample_rate)
664 {
665     double Q = convert_width2qfactor(s->width, s->frequency, s->gain, sample_rate, s->width_type);
666     double g, k, A;
667     double a[3];
668     double m[3];
669
670     switch (s->filter_type) {
671     case biquad:
672         a[0] = s->oa0;
673         a[1] = s->oa1;
674         a[2] = s->oa2;
675         m[0] = s->ob0;
676         m[1] = s->ob1;
677         m[2] = s->ob2;
678         break;
679     case equalizer:
680         A = ff_exp10(s->gain / 40.);
681         g = tan(M_PI * s->frequency / sample_rate);
682         k = 1. / (Q * A);
683         a[0] = 1. / (1. + g * (g + k));
684         a[1] = g * a[0];
685         a[2] = g * a[1];
686         m[0] = 1.;
687         m[1] = k * (A * A - 1.);
688         m[2] = 0.;
689         break;
690     case bass:
691     case lowshelf:
692         A = ff_exp10(s->gain / 40.);
693         g = tan(M_PI * s->frequency / sample_rate) / sqrt(A);
694         k = 1. / Q;
695         a[0] = 1. / (1. + g * (g + k));
696         a[1] = g * a[0];
697         a[2] = g * a[1];
698         m[0] = 1.;
699         m[1] = k * (A - 1.);
700         m[2] = A * A - 1.;
701         break;
702     case tiltshelf:
703         A = ff_exp10(s->gain / 20.);
704         g = tan(M_PI * s->frequency / sample_rate) / sqrt(A);
705         k = 1. / Q;
706         a[0] = 1. / (1. + g * (g + k));
707         a[1] = g * a[0];
708         a[2] = g * a[1];
709         m[0] = 1./ A;
710         m[1] = k * (A - 1.) / A;
711         m[2] = (A * A - 1.) / A;
712         break;
713     case treble:
714     case highshelf:
715         A = ff_exp10(s->gain / 40.);
716         g = tan(M_PI * s->frequency / sample_rate) / sqrt(A);
717         k = 1. / Q;
718         a[0] = 1. / (1. + g * (g + k));
719         a[1] = g * a[0];
720         a[2] = g * a[1];
721         m[0] = A * A;
722         m[1] = k * (1. - A) * A;
723         m[2] = 1. - A * A;
724         break;
725     case bandpass:
726         g = tan(M_PI * s->frequency / sample_rate);
727         k = 1. / Q;
728         a[0] = 1. / (1. + g * (g + k));
729         a[1] = g * a[0];
730         a[2] = g * a[1];
731         m[0] = 0.;
732         m[1] = s->csg ? 1. : 2.;
733         m[2] = 0.;
734         break;
735     case bandreject:
736         g = tan(M_PI * s->frequency / sample_rate);
737         k = 1. / Q;
738         a[0] = 1. / (1. + g * (g + k));
739         a[1] = g * a[0];
740         a[2] = g * a[1];
741         m[0] =  1.;
742         m[1] = -k;
743         m[2] =  0.;
744         break;
745     case lowpass:
746         g = tan(M_PI * s->frequency / sample_rate);
747         k = 1. / Q;
748         a[0] = 1. / (1. + g * (g + k));
749         a[1] = g * a[0];
750         a[2] = g * a[1];
751         m[0] = 0.;
752         m[1] = 0.;
753         m[2] = 1.;
754         break;
755     case highpass:
756         g = tan(M_PI * s->frequency / sample_rate);
757         k = 1. / Q;
758         a[0] = 1. / (1. + g * (g + k));
759         a[1] = g * a[0];
760         a[2] = g * a[1];
761         m[0] =  1.;
762         m[1] = -k;
763         m[2] = -1.;
764         break;
765     case allpass:
766         g = tan(M_PI * s->frequency / sample_rate);
767         k = 1. / Q;
768         a[0] = 1. / (1. + g * (g + k));
769         a[1] = g * a[0];
770         a[2] = g * a[1];
771         m[0] =  1.;
772         m[1] = -2. * k;
773         m[2] =  0.;
774         break;
775     default:
776         av_assert0(0);
777     }
778
779     s->a0 = a[0];
780     s->a1 = a[1];
781     s->a2 = a[2];
782     s->b0 = m[0];
783     s->b1 = m[1];
784     s->b2 = m[2];
785 }
786
787 static int config_filter(AVFilterLink *outlink, int reset)
788 {
789     AVFilterContext *ctx    = outlink->src;
790     BiquadsContext *s       = ctx->priv;
791     AVFilterLink *inlink    = ctx->inputs[0];
792     double gain = s->gain * ((s->filter_type == tiltshelf) + 1.);
793     double A = ff_exp10(gain / 40);
794     double w0 = 2 * M_PI * s->frequency / inlink->sample_rate;
795     double K = tan(w0 / 2.);
796     double alpha, beta;
797
798     s->bypass = (((w0 > M_PI || w0 <= 0.) && reset) || (s->width <= 0.)) && (s->filter_type != biquad);
799     if (s->bypass) {
800         av_log(ctx, AV_LOG_WARNING, "Invalid frequency and/or width!\n");
801         return 0;
802     }
803
804     if ((w0 > M_PI || w0 <= 0.) && (s->filter_type != biquad))
805         return AVERROR(EINVAL);
806
807     switch (s->width_type) {
808     case NONE:
809         alpha = 0.0;
810         break;
811     case HERTZ:
812         alpha = sin(w0) / (2 * s->frequency / s->width);
813         break;
814     case KHERTZ:
815         alpha = sin(w0) / (2 * s->frequency / (s->width * 1000));
816         break;
817     case OCTAVE:
818         alpha = sin(w0) * sinh(log(2.) / 2 * s->width * w0 / sin(w0));
819         break;
820     case QFACTOR:
821         alpha = sin(w0) / (2 * s->width);
822         break;
823     case SLOPE:
824         alpha = sin(w0) / 2 * sqrt((A + 1 / A) * (1 / s->width - 1) + 2);
825         break;
826     default:
827         av_assert0(0);
828     }
829
830     beta = 2 * sqrt(A);
831
832     switch (s->filter_type) {
833     case biquad:
834         s->a0 = s->oa0;
835         s->a1 = s->oa1;
836         s->a2 = s->oa2;
837         s->b0 = s->ob0;
838         s->b1 = s->ob1;
839         s->b2 = s->ob2;
840         break;
841     case equalizer:
842         s->a0 =   1 + alpha / A;
843         s->a1 =  -2 * cos(w0);
844         s->a2 =   1 - alpha / A;
845         s->b0 =   1 + alpha * A;
846         s->b1 =  -2 * cos(w0);
847         s->b2 =   1 - alpha * A;
848         break;
849     case bass:
850         beta = sqrt((A * A + 1) - (A - 1) * (A - 1));
851     case tiltshelf:
852     case lowshelf:
853         if (s->poles == 1) {
854             double A = ff_exp10(gain / 20);
855             double ro = -sin(w0 / 2. - M_PI_4) / sin(w0 / 2. + M_PI_4);
856             double n = (A + 1) / (A - 1);
857             double alpha1 = A == 1. ? 0. : n - FFSIGN(n) * sqrt(n * n - 1);
858             double beta0 = ((1 + A) + (1 - A) * alpha1) * 0.5;
859             double beta1 = ((1 - A) + (1 + A) * alpha1) * 0.5;
860
861             s->a0 = 1 + ro * alpha1;
862             s->a1 = -ro - alpha1;
863             s->a2 = 0;
864             s->b0 = beta0 + ro * beta1;
865             s->b1 = -beta1 - ro * beta0;
866             s->b2 = 0;
867         } else {
868             s->a0 =          (A + 1) + (A - 1) * cos(w0) + beta * alpha;
869             s->a1 =    -2 * ((A - 1) + (A + 1) * cos(w0));
870             s->a2 =          (A + 1) + (A - 1) * cos(w0) - beta * alpha;
871             s->b0 =     A * ((A + 1) - (A - 1) * cos(w0) + beta * alpha);
872             s->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
873             s->b2 =     A * ((A + 1) - (A - 1) * cos(w0) - beta * alpha);
874         }
875         break;
876     case treble:
877         beta = sqrt((A * A + 1) - (A - 1) * (A - 1));
878     case highshelf:
879         if (s->poles == 1) {
880             double A = ff_exp10(gain / 20);
881             double ro = sin(w0 / 2. - M_PI_4) / sin(w0 / 2. + M_PI_4);
882             double n = (A + 1) / (A - 1);
883             double alpha1 = A == 1. ? 0. : n - FFSIGN(n) * sqrt(n * n - 1);
884             double beta0 = ((1 + A) + (1 - A) * alpha1) * 0.5;
885             double beta1 = ((1 - A) + (1 + A) * alpha1) * 0.5;
886
887             s->a0 = 1 + ro * alpha1;
888             s->a1 = ro + alpha1;
889             s->a2 = 0;
890             s->b0 = beta0 + ro * beta1;
891             s->b1 = beta1 + ro * beta0;
892             s->b2 = 0;
893         } else {
894             s->a0 =          (A + 1) - (A - 1) * cos(w0) + beta * alpha;
895             s->a1 =     2 * ((A - 1) - (A + 1) * cos(w0));
896             s->a2 =          (A + 1) - (A - 1) * cos(w0) - beta * alpha;
897             s->b0 =     A * ((A + 1) + (A - 1) * cos(w0) + beta * alpha);
898             s->b1 =-2 * A * ((A - 1) + (A + 1) * cos(w0));
899             s->b2 =     A * ((A + 1) + (A - 1) * cos(w0) - beta * alpha);
900         }
901         break;
902     case bandpass:
903         if (s->csg) {
904             s->a0 =  1 + alpha;
905             s->a1 = -2 * cos(w0);
906             s->a2 =  1 - alpha;
907             s->b0 =  sin(w0) / 2;
908             s->b1 =  0;
909             s->b2 = -sin(w0) / 2;
910         } else {
911             s->a0 =  1 + alpha;
912             s->a1 = -2 * cos(w0);
913             s->a2 =  1 - alpha;
914             s->b0 =  alpha;
915             s->b1 =  0;
916             s->b2 = -alpha;
917         }
918         break;
919     case bandreject:
920         s->a0 =  1 + alpha;
921         s->a1 = -2 * cos(w0);
922         s->a2 =  1 - alpha;
923         s->b0 =  1;
924         s->b1 = -2 * cos(w0);
925         s->b2 =  1;
926         break;
927     case lowpass:
928         if (s->poles == 1) {
929             s->a0 = 1;
930             s->a1 = -exp(-w0);
931             s->a2 = 0;
932             s->b0 = 1 + s->a1;
933             s->b1 = 0;
934             s->b2 = 0;
935         } else {
936             s->a0 =  1 + alpha;
937             s->a1 = -2 * cos(w0);
938             s->a2 =  1 - alpha;
939             s->b0 = (1 - cos(w0)) / 2;
940             s->b1 =  1 - cos(w0);
941             s->b2 = (1 - cos(w0)) / 2;
942         }
943         break;
944     case highpass:
945         if (s->poles == 1) {
946             s->a0 = 1;
947             s->a1 = -exp(-w0);
948             s->a2 = 0;
949             s->b0 = (1 - s->a1) / 2;
950             s->b1 = -s->b0;
951             s->b2 = 0;
952         } else {
953             s->a0 =   1 + alpha;
954             s->a1 =  -2 * cos(w0);
955             s->a2 =   1 - alpha;
956             s->b0 =  (1 + cos(w0)) / 2;
957             s->b1 = -(1 + cos(w0));
958             s->b2 =  (1 + cos(w0)) / 2;
959         }
960         break;
961     case allpass:
962         switch (s->order) {
963         case 1:
964             s->a0 = 1.;
965             s->a1 = -(1. - K) / (1. + K);
966             s->a2 = 0.;
967             s->b0 = s->a1;
968             s->b1 = s->a0;
969             s->b2 = 0.;
970             break;
971         case 2:
972             s->a0 =  1 + alpha;
973             s->a1 = -2 * cos(w0);
974             s->a2 =  1 - alpha;
975             s->b0 =  1 - alpha;
976             s->b1 = -2 * cos(w0);
977             s->b2 =  1 + alpha;
978         break;
979         }
980         break;
981     default:
982         av_assert0(0);
983     }
984
985     av_log(ctx, AV_LOG_VERBOSE, "a=%f %f %f:b=%f %f %f\n", s->a0, s->a1, s->a2, s->b0, s->b1, s->b2);
986
987     s->a1 /= s->a0;
988     s->a2 /= s->a0;
989     s->b0 /= s->a0;
990     s->b1 /= s->a0;
991     s->b2 /= s->a0;
992     s->a0 /= s->a0;
993
994     if (s->normalize && fabs(s->b0 + s->b1 + s->b2) > 1e-6) {
995         double factor = (s->a0 + s->a1 + s->a2) / (s->b0 + s->b1 + s->b2);
996
997         s->b0 *= factor;
998         s->b1 *= factor;
999         s->b2 *= factor;
1000     }
1001
1002     switch (s->filter_type) {
1003     case tiltshelf:
1004         s->b0 /= A;
1005         s->b1 /= A;
1006         s->b2 /= A;
1007         break;
1008     }
1009
1010     s->cache = av_realloc_f(s->cache, sizeof(ChanCache), inlink->ch_layout.nb_channels);
1011     if (!s->cache)
1012         return AVERROR(ENOMEM);
1013     if (reset)
1014         memset(s->cache, 0, sizeof(ChanCache) * inlink->ch_layout.nb_channels);
1015
1016     if (reset && s->block_samples > 0) {
1017         for (int i = 0; i < 3; i++) {
1018             s->block[i] = ff_get_audio_buffer(outlink, s->block_samples * 2);
1019             if (!s->block[i])
1020                 return AVERROR(ENOMEM);
1021             av_samples_set_silence(s->block[i]->extended_data, 0, s->block_samples * 2,
1022                                    s->block[i]->ch_layout.nb_channels, s->block[i]->format);
1023         }
1024     }
1025
1026     switch (s->transform_type) {
1027     case DI:
1028         switch (inlink->format) {
1029         case AV_SAMPLE_FMT_S16P:
1030             s->filter = biquad_s16;
1031             break;
1032         case AV_SAMPLE_FMT_S32P:
1033             s->filter = biquad_s32;
1034             break;
1035         case AV_SAMPLE_FMT_FLTP:
1036             s->filter = biquad_flt;
1037             break;
1038         case AV_SAMPLE_FMT_DBLP:
1039             s->filter = biquad_dbl;
1040             break;
1041         default: av_assert0(0);
1042         }
1043         break;
1044     case DII:
1045         switch (inlink->format) {
1046         case AV_SAMPLE_FMT_S16P:
1047             s->filter = biquad_dii_s16;
1048             break;
1049         case AV_SAMPLE_FMT_S32P:
1050             s->filter = biquad_dii_s32;
1051             break;
1052         case AV_SAMPLE_FMT_FLTP:
1053             s->filter = biquad_dii_flt;
1054             break;
1055         case AV_SAMPLE_FMT_DBLP:
1056             s->filter = biquad_dii_dbl;
1057             break;
1058         default: av_assert0(0);
1059         }
1060         break;
1061     case TDI:
1062         switch (inlink->format) {
1063         case AV_SAMPLE_FMT_S16P:
1064             s->filter = biquad_tdi_s16;
1065             break;
1066         case AV_SAMPLE_FMT_S32P:
1067             s->filter = biquad_tdi_s32;
1068             break;
1069         case AV_SAMPLE_FMT_FLTP:
1070             s->filter = biquad_tdi_flt;
1071             break;
1072         case AV_SAMPLE_FMT_DBLP:
1073             s->filter = biquad_tdi_dbl;
1074             break;
1075         default: av_assert0(0);
1076         }
1077         break;
1078     case TDII:
1079         switch (inlink->format) {
1080         case AV_SAMPLE_FMT_S16P:
1081             s->filter = biquad_tdii_s16;
1082             break;
1083         case AV_SAMPLE_FMT_S32P:
1084             s->filter = biquad_tdii_s32;
1085             break;
1086         case AV_SAMPLE_FMT_FLTP:
1087             s->filter = biquad_tdii_flt;
1088             break;
1089         case AV_SAMPLE_FMT_DBLP:
1090             s->filter = biquad_tdii_dbl;
1091             break;
1092         default: av_assert0(0);
1093         }
1094         break;
1095     case LATT:
1096         switch (inlink->format) {
1097         case AV_SAMPLE_FMT_S16P:
1098             s->filter = biquad_latt_s16;
1099             break;
1100         case AV_SAMPLE_FMT_S32P:
1101             s->filter = biquad_latt_s32;
1102             break;
1103         case AV_SAMPLE_FMT_FLTP:
1104             s->filter = biquad_latt_flt;
1105             break;
1106         case AV_SAMPLE_FMT_DBLP:
1107             s->filter = biquad_latt_dbl;
1108             break;
1109         default: av_assert0(0);
1110         }
1111         break;
1112     case SVF:
1113         switch (inlink->format) {
1114         case AV_SAMPLE_FMT_S16P:
1115             s->filter = biquad_svf_s16;
1116             break;
1117         case AV_SAMPLE_FMT_S32P:
1118             s->filter = biquad_svf_s32;
1119             break;
1120         case AV_SAMPLE_FMT_FLTP:
1121             s->filter = biquad_svf_flt;
1122             break;
1123         case AV_SAMPLE_FMT_DBLP:
1124             s->filter = biquad_svf_dbl;
1125             break;
1126         default: av_assert0(0);
1127         }
1128         break;
1129     case ZDF:
1130         switch (inlink->format) {
1131         case AV_SAMPLE_FMT_S16P:
1132             s->filter = biquad_zdf_s16;
1133             break;
1134         case AV_SAMPLE_FMT_S32P:
1135             s->filter = biquad_zdf_s32;
1136             break;
1137         case AV_SAMPLE_FMT_FLTP:
1138             s->filter = biquad_zdf_flt;
1139             break;
1140         case AV_SAMPLE_FMT_DBLP:
1141             s->filter = biquad_zdf_dbl;
1142             break;
1143         default: av_assert0(0);
1144         }
1145         break;
1146     default:
1147         av_assert0(0);
1148      }
1149
1150      s->block_align = av_get_bytes_per_sample(inlink->format);
1151
1152      if (s->transform_type == LATT)
1153          convert_dir2latt(s);
1154      else if (s->transform_type == SVF)
1155          convert_dir2svf(s);
1156      else if (s->transform_type == ZDF)
1157          convert_dir2zdf(s, inlink->sample_rate);
1158
1159     return 0;
1160 }
1161
1162 static int config_output(AVFilterLink *outlink)
1163 {
1164     return config_filter(outlink, 1);
1165 }
1166
1167 typedef struct ThreadData {
1168     AVFrame *in, *out;
1169     int eof;
1170 } ThreadData;
1171
1172 static void reverse_samples(AVFrame *out, AVFrame *in, int p,
1173                             int oo, int io, int nb_samples)
1174 {
1175     switch (out->format) {
1176     case AV_SAMPLE_FMT_S16P: {
1177         const int16_t *src = ((const int16_t *)in->extended_data[p]) + io;
1178         int16_t *dst = ((int16_t *)out->extended_data[p]) + oo;
1179         for (int i = 0, j = nb_samples - 1; i < nb_samples; i++, j--)
1180             dst[i] = src[j];
1181     }
1182         break;
1183     case AV_SAMPLE_FMT_S32P: {
1184         const int32_t *src = ((const int32_t *)in->extended_data[p]) + io;
1185         int32_t *dst = ((int32_t *)out->extended_data[p]) + oo;
1186         for (int i = 0, j = nb_samples - 1; i < nb_samples; i++, j--)
1187             dst[i] = src[j];
1188     }
1189         break;
1190     case AV_SAMPLE_FMT_FLTP: {
1191         const float *src = ((const float *)in->extended_data[p]) + io;
1192         float *dst = ((float *)out->extended_data[p]) + oo;
1193         for (int i = 0, j = nb_samples - 1; i < nb_samples; i++, j--)
1194             dst[i] = src[j];
1195     }
1196         break;
1197     case AV_SAMPLE_FMT_DBLP: {
1198         const double *src = ((const double *)in->extended_data[p]) + io;
1199         double *dst = ((double *)out->extended_data[p]) + oo;
1200         for (int i = 0, j = nb_samples - 1; i < nb_samples; i++, j--)
1201             dst[i] = src[j];
1202     }
1203         break;
1204     }
1205 }
1206
1207 static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1208 {
1209     AVFilterLink *inlink = ctx->inputs[0];
1210     ThreadData *td = arg;
1211     AVFrame *buf = td->in;
1212     AVFrame *out_buf = td->out;
1213     BiquadsContext *s = ctx->priv;
1214     const int start = (buf->ch_layout.nb_channels * jobnr) / nb_jobs;
1215     const int end = (buf->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
1216     int ch;
1217
1218     for (ch = start; ch < end; ch++) {
1219         enum AVChannel channel = av_channel_layout_channel_from_index(&inlink->ch_layout, ch);
1220
1221         if (av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0) {
1222             if (buf != out_buf)
1223                 memcpy(out_buf->extended_data[ch], buf->extended_data[ch],
1224                        buf->nb_samples * s->block_align);
1225             continue;
1226         }
1227
1228         if (!s->block_samples) {
1229             s->filter(s, buf->extended_data[ch], out_buf->extended_data[ch], buf->nb_samples,
1230                       &s->cache[ch].i1, &s->cache[ch].i2, &s->cache[ch].o1, &s->cache[ch].o2,
1231                       s->b0, s->b1, s->b2, s->a0, s->a1, s->a2, &s->cache[ch].clippings, ctx->is_disabled);
1232         } else if (td->eof) {
1233             memcpy(out_buf->extended_data[ch], s->block[1]->extended_data[ch] + s->block_align * s->block_samples,
1234                    s->nb_samples * s->block_align);
1235         } else {
1236             memcpy(s->block[0]->extended_data[ch] + s->block_align * s->block_samples, buf->extended_data[ch],
1237                    buf->nb_samples * s->block_align);
1238             memset(s->block[0]->extended_data[ch] + s->block_align * (s->block_samples + buf->nb_samples),
1239                    0, (s->block_samples - buf->nb_samples) * s->block_align);
1240             s->filter(s, s->block[0]->extended_data[ch], s->block[1]->extended_data[ch], s->block_samples,
1241                       &s->cache[ch].i1, &s->cache[ch].i2, &s->cache[ch].o1, &s->cache[ch].o2,
1242                       s->b0, s->b1, s->b2, s->a0, s->a1, s->a2, &s->cache[ch].clippings, ctx->is_disabled);
1243             s->cache[ch].ri1 = s->cache[ch].i1;
1244             s->cache[ch].ri2 = s->cache[ch].i2;
1245             s->cache[ch].ro1 = s->cache[ch].o1;
1246             s->cache[ch].ro2 = s->cache[ch].o2;
1247             s->filter(s, s->block[0]->extended_data[ch] + s->block_samples * s->block_align,
1248                       s->block[1]->extended_data[ch] + s->block_samples * s->block_align,
1249                       s->block_samples,
1250                       &s->cache[ch].ri1, &s->cache[ch].ri2, &s->cache[ch].ro1, &s->cache[ch].ro2,
1251                       s->b0, s->b1, s->b2, s->a0, s->a1, s->a2, &s->cache[ch].clippings, ctx->is_disabled);
1252             reverse_samples(s->block[2], s->block[1], ch, 0, 0, 2 * s->block_samples);
1253             s->cache[ch].ri1 = 0.;
1254             s->cache[ch].ri2 = 0.;
1255             s->cache[ch].ro1 = 0.;
1256             s->cache[ch].ro2 = 0.;
1257             s->filter(s, s->block[2]->extended_data[ch], s->block[2]->extended_data[ch], 2 * s->block_samples,
1258                       &s->cache[ch].ri1, &s->cache[ch].ri2, &s->cache[ch].ro1, &s->cache[ch].ro2,
1259                       s->b0, s->b1, s->b2, s->a0, s->a1, s->a2, &s->cache[ch].clippings, ctx->is_disabled);
1260             reverse_samples(s->block[1], s->block[2], ch, 0, 0, 2 * s->block_samples);
1261             memcpy(out_buf->extended_data[ch], s->block[1]->extended_data[ch],
1262                    s->block_samples * s->block_align);
1263             memmove(s->block[0]->extended_data[ch], s->block[0]->extended_data[ch] + s->block_align * s->block_samples,
1264                     s->block_samples * s->block_align);
1265         }
1266     }
1267
1268     return 0;
1269 }
1270
1271 static int filter_frame(AVFilterLink *inlink, AVFrame *buf, int eof)
1272 {
1273     AVFilterContext  *ctx = inlink->dst;
1274     BiquadsContext *s     = ctx->priv;
1275     AVFilterLink *outlink = ctx->outputs[0];
1276     AVFrame *out_buf;
1277     ThreadData td;
1278     int ch, ret, drop = 0;
1279
1280     if (s->bypass)
1281         return ff_filter_frame(outlink, buf);
1282
1283     ret = av_channel_layout_copy(&s->ch_layout, &inlink->ch_layout);
1284     if (ret < 0) {
1285         av_frame_free(&buf);
1286         return ret;
1287     }
1288     if (strcmp(s->ch_layout_str, "all"))
1289         av_channel_layout_from_string(&s->ch_layout,
1290                                       s->ch_layout_str);
1291
1292     if (av_frame_is_writable(buf) && s->block_samples == 0) {
1293         out_buf = buf;
1294     } else {
1295         out_buf = ff_get_audio_buffer(outlink, s->block_samples > 0 ? s->block_samples : buf->nb_samples);
1296         if (!out_buf) {
1297             av_frame_free(&buf);
1298             return AVERROR(ENOMEM);
1299         }
1300         av_frame_copy_props(out_buf, buf);
1301     }
1302
1303     if (s->block_samples > 0 && s->pts == AV_NOPTS_VALUE)
1304         drop = 1;
1305     td.in = buf;
1306     td.out = out_buf;
1307     td.eof = eof;
1308     ff_filter_execute(ctx, filter_channel, &td, NULL,
1309                       FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
1310
1311     for (ch = 0; ch < outlink->ch_layout.nb_channels; ch++) {
1312         if (s->cache[ch].clippings > 0)
1313             av_log(ctx, AV_LOG_WARNING, "Channel %d clipping %d times. Please reduce gain.\n",
1314                    ch, s->cache[ch].clippings);
1315         s->cache[ch].clippings = 0;
1316     }
1317
1318     if (s->block_samples > 0) {
1319         int nb_samples = buf->nb_samples;
1320         int64_t pts = buf->pts;
1321
1322         out_buf->pts = s->pts;
1323         out_buf->nb_samples = s->nb_samples;
1324         s->pts = pts;
1325         s->nb_samples = nb_samples;
1326     }
1327
1328     if (buf != out_buf)
1329         av_frame_free(&buf);
1330
1331     if (!drop)
1332         return ff_filter_frame(outlink, out_buf);
1333     else {
1334         av_frame_free(&out_buf);
1335         ff_filter_set_ready(ctx, 10);
1336         return 0;
1337     }
1338 }
1339
1340 static int activate(AVFilterContext *ctx)
1341 {
1342     AVFilterLink *inlink = ctx->inputs[0];
1343     AVFilterLink *outlink = ctx->outputs[0];
1344     BiquadsContext *s = ctx->priv;
1345     AVFrame *in = NULL;
1346     int64_t pts;
1347     int status;
1348     int ret;
1349
1350     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
1351
1352     if (s->block_samples > 0) {
1353         ret = ff_inlink_consume_samples(inlink, s->block_samples, s->block_samples, &in);
1354     } else {
1355         ret = ff_inlink_consume_frame(inlink, &in);
1356     }
1357     if (ret < 0)
1358         return ret;
1359     if (ret > 0)
1360         return filter_frame(inlink, in, 0);
1361
1362     if (s->block_samples > 0 && ff_inlink_queued_samples(inlink) >= s->block_samples) {
1363         ff_filter_set_ready(ctx, 10);
1364         return 0;
1365     }
1366
1367     if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
1368         if (s->block_samples > 0) {
1369             AVFrame *in = ff_get_audio_buffer(outlink, s->block_samples);
1370             if (!in)
1371                 return AVERROR(ENOMEM);
1372
1373             ret = filter_frame(inlink, in, 1);
1374         }
1375
1376         ff_outlink_set_status(outlink, status, pts);
1377
1378         return ret;
1379     }
1380
1381     FF_FILTER_FORWARD_WANTED(outlink, inlink);
1382
1383     return FFERROR_NOT_READY;
1384 }
1385
1386 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
1387                            char *res, int res_len, int flags)
1388 {
1389     AVFilterLink *outlink = ctx->outputs[0];
1390     int ret;
1391
1392     ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
1393     if (ret < 0)
1394         return ret;
1395
1396     return config_filter(outlink, 0);
1397 }
1398
1399 static av_cold void uninit(AVFilterContext *ctx)
1400 {
1401     BiquadsContext *s = ctx->priv;
1402
1403     for (int i = 0; i < 3; i++)
1404         av_frame_free(&s->block[i]);
1405     av_freep(&s->cache);
1406     av_channel_layout_uninit(&s->ch_layout);
1407 }
1408
1409 static const AVFilterPad inputs[] = {
1410     {
1411         .name         = "default",
1412         .type         = AVMEDIA_TYPE_AUDIO,
1413     },
1414 };
1415
1416 static const AVFilterPad outputs[] = {
1417     {
1418         .name         = "default",
1419         .type         = AVMEDIA_TYPE_AUDIO,
1420         .config_props = config_output,
1421     },
1422 };
1423
1424 #define OFFSET(x) offsetof(BiquadsContext, x)
1425 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
1426 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1427
1428 #define DEFINE_BIQUAD_FILTER_2(name_, description_, priv_class_)        \
1429 static av_cold int name_##_init(AVFilterContext *ctx)                   \
1430 {                                                                       \
1431     BiquadsContext *s = ctx->priv;                                      \
1432     s->filter_type = name_;                                             \
1433     s->pts = AV_NOPTS_VALUE;                                            \
1434     return 0;                                                           \
1435 }                                                                       \
1436                                                          \
1437 const AVFilter ff_af_##name_ = {                         \
1438     .name          = #name_,                             \
1439     .description   = NULL_IF_CONFIG_SMALL(description_), \
1440     .priv_class    = &priv_class_##_class,               \
1441     .priv_size     = sizeof(BiquadsContext),             \
1442     .init          = name_##_init,                       \
1443     .activate      = activate,                           \
1444     .uninit        = uninit,                             \
1445     FILTER_INPUTS(inputs),                               \
1446     FILTER_OUTPUTS(outputs),                             \
1447     FILTER_QUERY_FUNC(query_formats),                    \
1448     .process_command = process_command,                  \
1449     .flags         = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, \
1450 }
1451
1452 #define DEFINE_BIQUAD_FILTER(name, description)                         \
1453     AVFILTER_DEFINE_CLASS(name);                                        \
1454     DEFINE_BIQUAD_FILTER_2(name, description, name)
1455
1456 #define WIDTH_OPTION(x)                                                                       \
1457     {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=x}, 0, 99999, FLAGS}, \
1458     {"w",     "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=x}, 0, 99999, FLAGS}
1459
1460 #define WIDTH_TYPE_OPTION(x)                                                                                                        \
1461     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=x}, HERTZ, NB_WTYPE-1, FLAGS, "width_type"}, \
1462     {"t",          "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=x}, HERTZ, NB_WTYPE-1, FLAGS, "width_type"}, \
1463     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},                                                     \
1464     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},                                             \
1465     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},                                                \
1466     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},                                                  \
1467     {"k", "kHz", 0, AV_OPT_TYPE_CONST, {.i64=KHERTZ}, 0, 0, FLAGS, "width_type"}
1468
1469 #define MIX_CHANNELS_NORMALIZE_OPTION(x, y, z)                                                                \
1470     {"mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=x}, 0, 1, FLAGS},                               \
1471     {"m",   "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=x}, 0, 1, FLAGS},                               \
1472     {"channels", "set channels to filter", OFFSET(ch_layout_str), AV_OPT_TYPE_STRING, {.str=y}, 0, 0, FLAGS}, \
1473     {"c",        "set channels to filter", OFFSET(ch_layout_str), AV_OPT_TYPE_STRING, {.str=y}, 0, 0, FLAGS}, \
1474     {"normalize", "normalize coefficients", OFFSET(normalize), AV_OPT_TYPE_BOOL, {.i64=z}, 0, 1, FLAGS},      \
1475     {"n",         "normalize coefficients", OFFSET(normalize), AV_OPT_TYPE_BOOL, {.i64=z}, 0, 1, FLAGS}
1476
1477 #define TRANSFORM_OPTION(x)                                                                                                      \
1478     {"transform", "set transform type", OFFSET(transform_type), AV_OPT_TYPE_INT, {.i64=x}, 0, NB_TTYPE-1, AF, "transform_type"}, \
1479     {"a",         "set transform type", OFFSET(transform_type), AV_OPT_TYPE_INT, {.i64=x}, 0, NB_TTYPE-1, AF, "transform_type"}, \
1480     {"di",   "direct form I",  0, AV_OPT_TYPE_CONST, {.i64=DI}, 0, 0, AF, "transform_type"},                                     \
1481     {"dii",  "direct form II", 0, AV_OPT_TYPE_CONST, {.i64=DII}, 0, 0, AF, "transform_type"},                                    \
1482     {"tdi",  "transposed direct form I",  0, AV_OPT_TYPE_CONST, {.i64=TDI},  0, 0, AF, "transform_type"},                        \
1483     {"tdii", "transposed direct form II", 0, AV_OPT_TYPE_CONST, {.i64=TDII}, 0, 0, AF, "transform_type"},                        \
1484     {"latt", "lattice-ladder form", 0, AV_OPT_TYPE_CONST, {.i64=LATT}, 0, 0, AF, "transform_type"},                              \
1485     {"svf",  "state variable filter form", 0, AV_OPT_TYPE_CONST, {.i64=SVF}, 0, 0, AF, "transform_type"},                        \
1486     {"zdf",  "zero-delay filter form", 0, AV_OPT_TYPE_CONST, {.i64=ZDF}, 0, 0, AF, "transform_type"}
1487
1488 #define PRECISION_OPTION(x)                                                                                           \
1489     {"precision", "set filtering precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=x}, -1, 3, AF, "precision"},   \
1490     {"r",         "set filtering precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=x}, -1, 3, AF, "precision"},   \
1491     {"auto", "automatic",            0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, AF, "precision"},                         \
1492     {"s16", "signed 16-bit",         0, AV_OPT_TYPE_CONST, {.i64=0},  0, 0, AF, "precision"},                         \
1493     {"s32", "signed 32-bit",         0, AV_OPT_TYPE_CONST, {.i64=1},  0, 0, AF, "precision"},                         \
1494     {"f32", "floating-point single", 0, AV_OPT_TYPE_CONST, {.i64=2},  0, 0, AF, "precision"},                         \
1495     {"f64", "floating-point double", 0, AV_OPT_TYPE_CONST, {.i64=3},  0, 0, AF, "precision"}
1496
1497 #define BLOCKSIZE_OPTION(x)                                                                              \
1498     {"blocksize", "set the block size", OFFSET(block_samples), AV_OPT_TYPE_INT, {.i64=x}, 0, 32768, AF}, \
1499     {"b",         "set the block size", OFFSET(block_samples), AV_OPT_TYPE_INT, {.i64=x}, 0, 32768, AF}
1500
1501 #if CONFIG_EQUALIZER_FILTER
1502 static const AVOption equalizer_options[] = {
1503     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
1504     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
1505     WIDTH_TYPE_OPTION(QFACTOR),
1506     WIDTH_OPTION(1.0),
1507     {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1508     {"g",    "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1509     MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1510     TRANSFORM_OPTION(DI),
1511     PRECISION_OPTION(-1),
1512     BLOCKSIZE_OPTION(0),
1513     {NULL}
1514 };
1515
1516 DEFINE_BIQUAD_FILTER(equalizer, "Apply two-pole peaking equalization (EQ) filter.");
1517 #endif  /* CONFIG_EQUALIZER_FILTER */
1518 #if CONFIG_BASS_FILTER || CONFIG_LOWSHELF_FILTER
1519 static const AVOption bass_lowshelf_options[] = {
1520     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
1521     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
1522     WIDTH_TYPE_OPTION(QFACTOR),
1523     WIDTH_OPTION(0.5),
1524     {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1525     {"g",    "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1526     {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1527     {"p",     "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1528     MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1529     TRANSFORM_OPTION(DI),
1530     PRECISION_OPTION(-1),
1531     BLOCKSIZE_OPTION(0),
1532     {NULL}
1533 };
1534
1535 AVFILTER_DEFINE_CLASS_EXT(bass_lowshelf, "bass/lowshelf", bass_lowshelf_options);
1536 #if CONFIG_BASS_FILTER
1537 DEFINE_BIQUAD_FILTER_2(bass, "Boost or cut lower frequencies.", bass_lowshelf);
1538 #endif  /* CONFIG_BASS_FILTER */
1539
1540 #if CONFIG_LOWSHELF_FILTER
1541 DEFINE_BIQUAD_FILTER_2(lowshelf, "Apply a low shelf filter.", bass_lowshelf);
1542 #endif  /* CONFIG_LOWSHELF_FILTER */
1543 #endif  /* CONFIG_BASS_FILTER || CONFIG LOWSHELF_FILTER */
1544 #if CONFIG_TREBLE_FILTER || CONFIG_HIGHSHELF_FILTER || CONFIG_TILTSHELF_FILTER
1545 static const AVOption treble_highshelf_options[] = {
1546     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1547     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1548     WIDTH_TYPE_OPTION(QFACTOR),
1549     WIDTH_OPTION(0.5),
1550     {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1551     {"g",    "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1552     {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1553     {"p",     "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1554     MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1555     TRANSFORM_OPTION(DI),
1556     PRECISION_OPTION(-1),
1557     BLOCKSIZE_OPTION(0),
1558     {NULL}
1559 };
1560
1561 AVFILTER_DEFINE_CLASS_EXT(treble_highshelf, "treble/high/tiltshelf",
1562                           treble_highshelf_options);
1563
1564 #if CONFIG_TREBLE_FILTER
1565 DEFINE_BIQUAD_FILTER_2(treble, "Boost or cut upper frequencies.", treble_highshelf);
1566 #endif  /* CONFIG_TREBLE_FILTER */
1567
1568 #if CONFIG_HIGHSHELF_FILTER
1569 DEFINE_BIQUAD_FILTER_2(highshelf, "Apply a high shelf filter.", treble_highshelf);
1570 #endif  /* CONFIG_HIGHSHELF_FILTER */
1571
1572 #if CONFIG_TILTSHELF_FILTER
1573 DEFINE_BIQUAD_FILTER_2(tiltshelf, "Apply a tilt shelf filter.", treble_highshelf);
1574 #endif
1575 #endif  /* CONFIG_TREBLE_FILTER || CONFIG_HIGHSHELF_FILTER || CONFIG_TILTSHELF_FILTER */
1576
1577 #if CONFIG_BANDPASS_FILTER
1578 static const AVOption bandpass_options[] = {
1579     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1580     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1581     WIDTH_TYPE_OPTION(QFACTOR),
1582     WIDTH_OPTION(0.5),
1583     {"csg",   "use constant skirt gain", OFFSET(csg), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
1584     MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1585     TRANSFORM_OPTION(DI),
1586     PRECISION_OPTION(-1),
1587     BLOCKSIZE_OPTION(0),
1588     {NULL}
1589 };
1590
1591 DEFINE_BIQUAD_FILTER(bandpass, "Apply a two-pole Butterworth band-pass filter.");
1592 #endif  /* CONFIG_BANDPASS_FILTER */
1593 #if CONFIG_BANDREJECT_FILTER
1594 static const AVOption bandreject_options[] = {
1595     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1596     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1597     WIDTH_TYPE_OPTION(QFACTOR),
1598     WIDTH_OPTION(0.5),
1599     MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1600     TRANSFORM_OPTION(DI),
1601     PRECISION_OPTION(-1),
1602     BLOCKSIZE_OPTION(0),
1603     {NULL}
1604 };
1605
1606 DEFINE_BIQUAD_FILTER(bandreject, "Apply a two-pole Butterworth band-reject filter.");
1607 #endif  /* CONFIG_BANDREJECT_FILTER */
1608 #if CONFIG_LOWPASS_FILTER
1609 static const AVOption lowpass_options[] = {
1610     {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
1611     {"f",         "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
1612     WIDTH_TYPE_OPTION(QFACTOR),
1613     WIDTH_OPTION(0.707),
1614     {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1615     {"p",     "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1616     MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1617     TRANSFORM_OPTION(DI),
1618     PRECISION_OPTION(-1),
1619     BLOCKSIZE_OPTION(0),
1620     {NULL}
1621 };
1622
1623 DEFINE_BIQUAD_FILTER(lowpass, "Apply a low-pass filter with 3dB point frequency.");
1624 #endif  /* CONFIG_LOWPASS_FILTER */
1625 #if CONFIG_HIGHPASS_FILTER
1626 static const AVOption highpass_options[] = {
1627     {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1628     {"f",         "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1629     WIDTH_TYPE_OPTION(QFACTOR),
1630     WIDTH_OPTION(0.707),
1631     {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1632     {"p",     "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1633     MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1634     TRANSFORM_OPTION(DI),
1635     PRECISION_OPTION(-1),
1636     BLOCKSIZE_OPTION(0),
1637     {NULL}
1638 };
1639
1640 DEFINE_BIQUAD_FILTER(highpass, "Apply a high-pass filter with 3dB point frequency.");
1641 #endif  /* CONFIG_HIGHPASS_FILTER */
1642 #if CONFIG_ALLPASS_FILTER
1643 static const AVOption allpass_options[] = {
1644     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1645     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1646     WIDTH_TYPE_OPTION(QFACTOR),
1647     WIDTH_OPTION(0.707),
1648     MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1649     {"order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
1650     {"o",     "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
1651     TRANSFORM_OPTION(DI),
1652     PRECISION_OPTION(-1),
1653     {NULL}
1654 };
1655
1656 DEFINE_BIQUAD_FILTER(allpass, "Apply a two-pole all-pass filter.");
1657 #endif  /* CONFIG_ALLPASS_FILTER */
1658 #if CONFIG_BIQUAD_FILTER
1659 static const AVOption biquad_options[] = {
1660     {"a0", NULL, OFFSET(oa0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT32_MIN, INT32_MAX, FLAGS},
1661     {"a1", NULL, OFFSET(oa1), AV_OPT_TYPE_DOUBLE, {.dbl=0}, INT32_MIN, INT32_MAX, FLAGS},
1662     {"a2", NULL, OFFSET(oa2), AV_OPT_TYPE_DOUBLE, {.dbl=0}, INT32_MIN, INT32_MAX, FLAGS},
1663     {"b0", NULL, OFFSET(ob0), AV_OPT_TYPE_DOUBLE, {.dbl=0}, INT32_MIN, INT32_MAX, FLAGS},
1664     {"b1", NULL, OFFSET(ob1), AV_OPT_TYPE_DOUBLE, {.dbl=0}, INT32_MIN, INT32_MAX, FLAGS},
1665     {"b2", NULL, OFFSET(ob2), AV_OPT_TYPE_DOUBLE, {.dbl=0}, INT32_MIN, INT32_MAX, FLAGS},
1666     MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1667     TRANSFORM_OPTION(DI),
1668     PRECISION_OPTION(-1),
1669     BLOCKSIZE_OPTION(0),
1670     {NULL}
1671 };
1672
1673 DEFINE_BIQUAD_FILTER(biquad, "Apply a biquad IIR filter with the given coefficients.");
1674 #endif  /* CONFIG_BIQUAD_FILTER */