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