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