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