]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_biquads.c
lavfi/biquads: remove pointless casts
[ffmpeg] / 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 "libavutil/opt.h"
66 #include "libavutil/avassert.h"
67 #include "audio.h"
68 #include "avfilter.h"
69 #include "internal.h"
70
71 enum FilterType {
72     biquad,
73     equalizer,
74     bass,
75     treble,
76     band,
77     bandpass,
78     bandreject,
79     allpass,
80     highpass,
81     lowpass,
82 };
83
84 enum WidthType {
85     NONE,
86     HZ,
87     OCTAVE,
88     QFACTOR,
89     SLOPE,
90 };
91
92 typedef struct ChanCache {
93     double i1, i2;
94     double o1, o2;
95 } ChanCache;
96
97 typedef struct {
98     const AVClass *class;
99
100     enum FilterType filter_type;
101     enum WidthType width_type;
102     int poles;
103     int csg;
104
105     double gain;
106     double frequency;
107     double width;
108
109     double a0, a1, a2;
110     double b0, b1, b2;
111
112     ChanCache *cache;
113
114     void (*filter)(const void *ibuf, void *obuf, int len,
115                    double *i1, double *i2, double *o1, double *o2,
116                    double b0, double b1, double b2, double a1, double a2);
117 } BiquadsContext;
118
119 static av_cold int init(AVFilterContext *ctx, const char *args)
120 {
121     BiquadsContext *p = ctx->priv;
122     int ret;
123
124     av_opt_set_defaults(p);
125
126     if ((ret = av_set_options_string(p, args, "=", ":")) < 0)
127         return ret;
128
129     if (p->filter_type != biquad) {
130         if (p->frequency <= 0 || p->width <= 0) {
131             av_log(ctx, AV_LOG_ERROR, "Invalid frequency %f and/or width %f <= 0\n",
132                    p->frequency, p->width);
133             return AVERROR(EINVAL);
134         }
135     }
136
137     return 0;
138 }
139
140 static int query_formats(AVFilterContext *ctx)
141 {
142     AVFilterFormats *formats;
143     AVFilterChannelLayouts *layouts;
144     static const enum AVSampleFormat sample_fmts[] = {
145         AV_SAMPLE_FMT_S16P,
146         AV_SAMPLE_FMT_S32P,
147         AV_SAMPLE_FMT_FLTP,
148         AV_SAMPLE_FMT_DBLP,
149         AV_SAMPLE_FMT_NONE
150     };
151
152     layouts = ff_all_channel_layouts();
153     if (!layouts)
154         return AVERROR(ENOMEM);
155     ff_set_common_channel_layouts(ctx, layouts);
156
157     formats = ff_make_format_list(sample_fmts);
158     if (!formats)
159         return AVERROR(ENOMEM);
160     ff_set_common_formats(ctx, formats);
161
162     formats = ff_all_samplerates();
163     if (!formats)
164         return AVERROR(ENOMEM);
165     ff_set_common_samplerates(ctx, formats);
166
167     return 0;
168 }
169
170 #define BIQUAD_FILTER(name, type, min, max)                                   \
171 static void biquad_## name (const void *input, void *output, int len,         \
172                             double *in1, double *in2,                         \
173                             double *out1, double *out2,                       \
174                             double b0, double b1, double b2,                  \
175                             double a1, double a2)                             \
176 {                                                                             \
177     const type *ibuf = input;                                                 \
178     type *obuf = output;                                                      \
179     double i1 = *in1;                                                         \
180     double i2 = *in2;                                                         \
181     double o1 = *out1;                                                        \
182     double o2 = *out2;                                                        \
183     int i;                                                                    \
184                                                                               \
185     for (i = 0; i < len; i++) {                                               \
186         double o0 = ibuf[i] * b0 + i1 * b1 + i2 * b2 - o1 * a1 - o2 * a2;     \
187         i2 = i1;                                                              \
188         i1 = ibuf[i];                                                         \
189         o2 = o1;                                                              \
190         o1 = o0;                                                              \
191         if (o0 < min) {                                                       \
192             av_log(NULL, AV_LOG_WARNING, "clipping\n");                       \
193             obuf[i] = min;                                                    \
194         } else if (o0 > max) {                                                \
195             av_log(NULL, AV_LOG_WARNING, "clipping\n");                       \
196             obuf[i] = max;                                                    \
197         } else {                                                              \
198             obuf[i] = o0;                                                     \
199         }                                                                     \
200     }                                                                         \
201     *in1  = i1;                                                               \
202     *in2  = i2;                                                               \
203     *out1 = o1;                                                               \
204     *out2 = o2;                                                               \
205 }
206
207 BIQUAD_FILTER(s16, int16_t, INT16_MIN, INT16_MAX)
208 BIQUAD_FILTER(s32, int32_t, INT32_MIN, INT32_MAX)
209 BIQUAD_FILTER(flt, float,   -1., 1.)
210 BIQUAD_FILTER(dbl, double,  -1., 1.)
211
212 static int config_output(AVFilterLink *outlink)
213 {
214     AVFilterContext *ctx    = outlink->src;
215     BiquadsContext *p       = ctx->priv;
216     AVFilterLink *inlink    = ctx->inputs[0];
217     double A = exp(p->gain / 40 * log(10.));
218     double w0 = 2 * M_PI * p->frequency / inlink->sample_rate;
219     double alpha;
220
221     if (w0 > M_PI) {
222         av_log(ctx, AV_LOG_ERROR,
223                "Invalid frequency %f. Frequency must be less than half the sample-rate %d.\n",
224                p->frequency, inlink->sample_rate);
225         return AVERROR(EINVAL);
226     }
227
228     switch (p->width_type) {
229     case NONE:
230         alpha = 0.0;
231         break;
232     case HZ:
233         alpha = sin(w0) / (2 * p->frequency / p->width);
234         break;
235     case OCTAVE:
236         alpha = sin(w0) * sinh(log(2.) / 2 * p->width * w0 / sin(w0));
237         break;
238     case QFACTOR:
239         alpha = sin(w0) / (2 * p->width);
240         break;
241     case SLOPE:
242         alpha = sin(w0) / 2 * sqrt((A + 1 / A) * (1 / p->width - 1) + 2);
243         break;
244     default:
245         av_assert0(0);
246     }
247
248     switch (p->filter_type) {
249     case biquad:
250         break;
251     case equalizer:
252         p->a0 =   1 + alpha / A;
253         p->a1 =  -2 * cos(w0);
254         p->a2 =   1 - alpha / A;
255         p->b0 =   1 + alpha * A;
256         p->b1 =  -2 * cos(w0);
257         p->b2 =   1 - alpha * A;
258         break;
259     case bass:
260         p->a0 =          (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
261         p->a1 =    -2 * ((A - 1) + (A + 1) * cos(w0));
262         p->a2 =          (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
263         p->b0 =     A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
264         p->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
265         p->b2 =     A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
266         break;
267     case treble:
268         p->a0 =          (A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
269         p->a1 =     2 * ((A - 1) - (A + 1) * cos(w0));
270         p->a2 =          (A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
271         p->b0 =     A * ((A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
272         p->b1 =-2 * A * ((A - 1) + (A + 1) * cos(w0));
273         p->b2 =     A * ((A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
274         break;
275     case bandpass:
276         if (p->csg) {
277             p->a0 =  1 + alpha;
278             p->a1 = -2 * cos(w0);
279             p->a2 =  1 - alpha;
280             p->b0 =  sin(w0) / 2;
281             p->b1 =  0;
282             p->b2 = -sin(w0) / 2;
283         } else {
284             p->a0 =  1 + alpha;
285             p->a1 = -2 * cos(w0);
286             p->a2 =  1 - alpha;
287             p->b0 =  alpha;
288             p->b1 =  0;
289             p->b2 = -alpha;
290         }
291         break;
292     case bandreject:
293         p->a0 =  1 + alpha;
294         p->a1 = -2 * cos(w0);
295         p->a2 =  1 - alpha;
296         p->b0 =  1;
297         p->b1 = -2 * cos(w0);
298         p->b2 =  1;
299         break;
300     case lowpass:
301         if (p->poles == 1) {
302             p->a0 = 1;
303             p->a1 = -exp(-w0);
304             p->a2 = 0;
305             p->b0 = 1 + p->a1;
306             p->b1 = 0;
307             p->b2 = 0;
308         } else {
309             p->a0 =  1 + alpha;
310             p->a1 = -2 * cos(w0);
311             p->a2 =  1 - alpha;
312             p->b0 = (1 - cos(w0)) / 2;
313             p->b1 =  1 - cos(w0);
314             p->b2 = (1 - cos(w0)) / 2;
315         }
316         break;
317     case highpass:
318         if (p->poles == 1) {
319             p->a0 = 1;
320             p->a1 = -exp(-w0);
321             p->a2 = 0;
322             p->b0 = (1 - p->a1) / 2;
323             p->b1 = -p->b0;
324             p->b2 = 0;
325         } else {
326             p->a0 =   1 + alpha;
327             p->a1 =  -2 * cos(w0);
328             p->a2 =   1 - alpha;
329             p->b0 =  (1 + cos(w0)) / 2;
330             p->b1 = -(1 + cos(w0));
331             p->b2 =  (1 + cos(w0)) / 2;
332         }
333         break;
334     case allpass:
335         p->a0 =  1 + alpha;
336         p->a1 = -2 * cos(w0);
337         p->a2 =  1 - alpha;
338         p->b0 =  1 - alpha;
339         p->b1 = -2 * cos(w0);
340         p->b2 =  1 + alpha;
341         break;
342     default:
343         av_assert0(0);
344     }
345
346     p->a1 /= p->a0;
347     p->a2 /= p->a0;
348     p->b0 /= p->a0;
349     p->b1 /= p->a0;
350     p->b2 /= p->a0;
351
352     p->cache = av_realloc_f(p->cache, sizeof(ChanCache), inlink->channels);
353     if (!p->cache)
354         return AVERROR(ENOMEM);
355
356     switch (inlink->format) {
357     case AV_SAMPLE_FMT_S16P: p->filter = biquad_s16; break;
358     case AV_SAMPLE_FMT_S32P: p->filter = biquad_s32; break;
359     case AV_SAMPLE_FMT_FLTP: p->filter = biquad_flt; break;
360     case AV_SAMPLE_FMT_DBLP: p->filter = biquad_dbl; break;
361     default: av_assert0(0);
362     }
363
364     return 0;
365 }
366
367 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
368 {
369     BiquadsContext *p       = inlink->dst->priv;
370     AVFilterLink *outlink   = inlink->dst->outputs[0];
371     AVFilterBufferRef *out_buf;
372     int nb_samples = buf->audio->nb_samples;
373     int ch;
374
375     if (buf->perms & AV_PERM_WRITE) {
376         out_buf = buf;
377     } else {
378         out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
379         if (!out_buf)
380             return AVERROR(ENOMEM);
381         out_buf->pts = buf->pts;
382     }
383
384     for (ch = 0; ch < buf->audio->channels; ch++)
385         p->filter(buf->extended_data[ch],
386                   out_buf->extended_data[ch], nb_samples,
387                   &p->cache[ch].i1, &p->cache[ch].i2,
388                   &p->cache[ch].o1, &p->cache[ch].o2,
389                   p->b0, p->b1, p->b2, p->a1, p->a2);
390
391     if (buf != out_buf)
392         avfilter_unref_buffer(buf);
393
394     return ff_filter_frame(outlink, out_buf);
395 }
396
397 static av_cold void uninit(AVFilterContext *ctx)
398 {
399     BiquadsContext *p = ctx->priv;
400
401     av_freep(&p->cache);
402     av_opt_free(p);
403 }
404
405 static const AVFilterPad inputs[] = {
406     {
407         .name         = "default",
408         .type         = AVMEDIA_TYPE_AUDIO,
409         .filter_frame = filter_frame,
410     },
411     { NULL }
412 };
413
414 static const AVFilterPad outputs[] = {
415     {
416         .name         = "default",
417         .type         = AVMEDIA_TYPE_AUDIO,
418         .config_props = config_output,
419     },
420     { NULL }
421 };
422
423 #define OFFSET(x) offsetof(BiquadsContext, x)
424 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
425
426 #define DEFINE_BIQUAD_FILTER(name_, description_)                       \
427 AVFILTER_DEFINE_CLASS(name_);                                           \
428 static av_cold int name_##_init(AVFilterContext *ctx, const char *args) \
429 {                                                                       \
430     BiquadsContext *p = ctx->priv;                                      \
431     p->class = &name_##_class;                                          \
432     p->filter_type = name_;                                             \
433     return init(ctx, args);                                             \
434 }                                                                       \
435                                                          \
436 AVFilter avfilter_af_##name_ = {                         \
437     .name          = #name_,                             \
438     .description   = NULL_IF_CONFIG_SMALL(description_), \
439     .priv_size     = sizeof(BiquadsContext),             \
440     .init          = name_##_init,                       \
441     .uninit        = uninit,                             \
442     .query_formats = query_formats,                      \
443     .inputs        = inputs,                             \
444     .outputs       = outputs,                            \
445     .priv_class    = &name_##_class,                     \
446 }
447
448 #if CONFIG_EQUALIZER_FILTER
449 static const AVOption equalizer_options[] = {
450     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
451     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
452     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
453     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
454     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
455     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
456     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
457     {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
458     {"w",     "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
459     {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
460     {"g",    "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
461     {NULL},
462 };
463
464 DEFINE_BIQUAD_FILTER(equalizer, "Apply two-pole peaking equalization (EQ) filter.");
465 #endif  /* CONFIG_EQUALIZER_FILTER */
466 #if CONFIG_BASS_FILTER
467 static const AVOption bass_options[] = {
468     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
469     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
470     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
471     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
472     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
473     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
474     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
475     {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
476     {"w",     "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
477     {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
478     {"g",    "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
479     {NULL},
480 };
481
482 DEFINE_BIQUAD_FILTER(bass, "Boost or cut lower frequencies.");
483 #endif  /* CONFIG_BASS_FILTER */
484 #if CONFIG_TREBLE_FILTER
485 static const AVOption treble_options[] = {
486     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
487     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
488     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
489     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
490     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
491     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
492     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
493     {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
494     {"w",     "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
495     {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
496     {"g",    "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
497     {NULL},
498 };
499
500 DEFINE_BIQUAD_FILTER(treble, "Boost or cut upper frequencies.");
501 #endif  /* CONFIG_TREBLE_FILTER */
502 #if CONFIG_BANDPASS_FILTER
503 static const AVOption bandpass_options[] = {
504     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
505     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
506     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
507     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
508     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
509     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
510     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
511     {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
512     {"w",     "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
513     {"csg",   "use constant skirt gain", OFFSET(csg), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
514     {NULL},
515 };
516
517 DEFINE_BIQUAD_FILTER(bandpass, "Apply a two-pole Butterworth band-pass filter.");
518 #endif  /* CONFIG_BANDPASS_FILTER */
519 #if CONFIG_BANDREJECT_FILTER
520 static const AVOption bandreject_options[] = {
521     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
522     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
523     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
524     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
525     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
526     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
527     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
528     {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
529     {"w",     "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
530     {NULL},
531 };
532
533 DEFINE_BIQUAD_FILTER(bandreject, "Apply a two-pole Butterworth band-reject filter.");
534 #endif  /* CONFIG_BANDREJECT_FILTER */
535 #if CONFIG_LOWPASS_FILTER
536 static const AVOption lowpass_options[] = {
537     {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
538     {"f",         "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
539     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
540     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
541     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
542     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
543     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
544     {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
545     {"w",     "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
546     {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
547     {"p",     "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
548     {NULL},
549 };
550
551 DEFINE_BIQUAD_FILTER(lowpass, "Apply a low-pass filter with 3dB point frequency.");
552 #endif  /* CONFIG_LOWPASS_FILTER */
553 #if CONFIG_HIGHPASS_FILTER
554 static const AVOption highpass_options[] = {
555     {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
556     {"f",         "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
557     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
558     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
559     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
560     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
561     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
562     {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
563     {"w",     "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
564     {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
565     {"p",     "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
566     {NULL},
567 };
568
569 DEFINE_BIQUAD_FILTER(highpass, "Apply a high-pass filter with 3dB point frequency.");
570 #endif  /* CONFIG_HIGHPASS_FILTER */
571 #if CONFIG_ALLPASS_FILTER
572 static const AVOption allpass_options[] = {
573     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
574     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
575     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=HZ}, HZ, SLOPE, FLAGS, "width_type"},
576     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
577     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
578     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
579     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
580     {"width", "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
581     {"w",     "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
582     {NULL},
583 };
584
585 DEFINE_BIQUAD_FILTER(allpass, "Apply a two-pole all-pass filter.");
586 #endif  /* CONFIG_ALLPASS_FILTER */
587 #if CONFIG_BIQUAD_FILTER
588 static const AVOption biquad_options[] = {
589     {"a0", NULL, OFFSET(a0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
590     {"a1", NULL, OFFSET(a1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
591     {"a2", NULL, OFFSET(a2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
592     {"b0", NULL, OFFSET(b0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
593     {"b1", NULL, OFFSET(b1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
594     {"b2", NULL, OFFSET(b2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
595     {NULL},
596 };
597
598 DEFINE_BIQUAD_FILTER(biquad, "Apply a biquad IIR filter with the given coefficients.");
599 #endif  /* CONFIG_BIQUAD_FILTER */