libavfilter/af_biquads.c
b70ea49c
 /*
  * Copyright (c) 2013 Paul B Mahol
  * Copyright (c) 2006-2008 Rob Sykes <robs@users.sourceforge.net>
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /*
  * 2-pole filters designed by Robert Bristow-Johnson <rbj@audioimagination.com>
  *   see http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
  *
  * 1-pole filters based on code (c) 2000 Chris Bagwell <cbagwell@sprynet.com>
  *   Algorithms: Recursive single pole low/high pass filter
  *   Reference: The Scientist and Engineer's Guide to Digital Signal Processing
  *
  *   low-pass: output[N] = input[N] * A + output[N-1] * B
  *     X = exp(-2.0 * pi * Fc)
  *     A = 1 - X
  *     B = X
  *     Fc = cutoff freq / sample rate
  *
  *     Mimics an RC low-pass filter:
  *
  *     ---/\/\/\/\----------->
  *                   |
  *                  --- C
  *                  ---
  *                   |
  *                   |
  *                   V
  *
  *   high-pass: output[N] = A0 * input[N] + A1 * input[N-1] + B1 * output[N-1]
  *     X  = exp(-2.0 * pi * Fc)
  *     A0 = (1 + X) / 2
  *     A1 = -(1 + X) / 2
  *     B1 = X
  *     Fc = cutoff freq / sample rate
  *
  *     Mimics an RC high-pass filter:
  *
  *         || C
  *     ----||--------->
  *         ||    |
  *               <
  *               > R
  *               <
  *               |
  *               V
  */
 
 #include "libavutil/avassert.h"
b211607b
 #include "libavutil/opt.h"
b70ea49c
 #include "audio.h"
 #include "avfilter.h"
 #include "internal.h"
 
 enum FilterType {
     biquad,
     equalizer,
     bass,
     treble,
     band,
     bandpass,
     bandreject,
     allpass,
     highpass,
     lowpass,
 };
 
 enum WidthType {
     NONE,
33f5d70d
     HERTZ,
b70ea49c
     OCTAVE,
     QFACTOR,
     SLOPE,
 };
 
 typedef struct ChanCache {
     double i1, i2;
     double o1, o2;
 } ChanCache;
 
3689b58a
 typedef struct BiquadsContext {
b70ea49c
     const AVClass *class;
 
     enum FilterType filter_type;
e8c1eb09
     int width_type;
b70ea49c
     int poles;
     int csg;
 
     double gain;
     double frequency;
     double width;
9d08c7bd
     uint64_t channels;
b70ea49c
 
     double a0, a1, a2;
     double b0, b1, b2;
 
     ChanCache *cache;
3689b58a
     int clippings;
9d08c7bd
     int block_align;
b70ea49c
 
3689b58a
     void (*filter)(struct BiquadsContext *s, const void *ibuf, void *obuf, int len,
b70ea49c
                    double *i1, double *i2, double *o1, double *o2,
                    double b0, double b1, double b2, double a1, double a2);
 } BiquadsContext;
 
fd6228e6
 static av_cold int init(AVFilterContext *ctx)
b70ea49c
 {
6121968a
     BiquadsContext *s = ctx->priv;
b70ea49c
 
6121968a
     if (s->filter_type != biquad) {
         if (s->frequency <= 0 || s->width <= 0) {
b70ea49c
             av_log(ctx, AV_LOG_ERROR, "Invalid frequency %f and/or width %f <= 0\n",
6121968a
                    s->frequency, s->width);
b70ea49c
             return AVERROR(EINVAL);
         }
     }
 
     return 0;
 }
 
 static int query_formats(AVFilterContext *ctx)
 {
     AVFilterFormats *formats;
     AVFilterChannelLayouts *layouts;
     static const enum AVSampleFormat sample_fmts[] = {
         AV_SAMPLE_FMT_S16P,
         AV_SAMPLE_FMT_S32P,
         AV_SAMPLE_FMT_FLTP,
         AV_SAMPLE_FMT_DBLP,
         AV_SAMPLE_FMT_NONE
     };
a0854c08
     int ret;
b70ea49c
 
494b7924
     layouts = ff_all_channel_counts();
b70ea49c
     if (!layouts)
         return AVERROR(ENOMEM);
a0854c08
     ret = ff_set_common_channel_layouts(ctx, layouts);
     if (ret < 0)
         return ret;
b70ea49c
 
     formats = ff_make_format_list(sample_fmts);
     if (!formats)
         return AVERROR(ENOMEM);
a0854c08
     ret = ff_set_common_formats(ctx, formats);
     if (ret < 0)
         return ret;
b70ea49c
 
     formats = ff_all_samplerates();
     if (!formats)
         return AVERROR(ENOMEM);
a0854c08
     return ff_set_common_samplerates(ctx, formats);
b70ea49c
 }
 
ce385c86
 #define BIQUAD_FILTER(name, type, min, max, need_clipping)                    \
3689b58a
 static void biquad_## name (BiquadsContext *s,                                \
e6690ce0
                             const void *input, void *output, int len,         \
b70ea49c
                             double *in1, double *in2,                         \
                             double *out1, double *out2,                       \
                             double b0, double b1, double b2,                  \
                             double a1, double a2)                             \
 {                                                                             \
     const type *ibuf = input;                                                 \
     type *obuf = output;                                                      \
     double i1 = *in1;                                                         \
     double i2 = *in2;                                                         \
     double o1 = *out1;                                                        \
     double o2 = *out2;                                                        \
     int i;                                                                    \
a00c4b4d
     a1 = -a1;                                                                 \
     a2 = -a2;                                                                 \
b70ea49c
                                                                               \
9f956611
     for (i = 0; i+1 < len; i++) {                                             \
a00c4b4d
         o2 = i2 * b2 + i1 * b1 + ibuf[i] * b0 + o2 * a2 + o1 * a1;            \
9f956611
         i2 = ibuf[i];                                                         \
ce385c86
         if (need_clipping && o2 < min) {                                      \
3689b58a
             s->clippings++;                                                   \
9f956611
             obuf[i] = min;                                                    \
ce385c86
         } else if (need_clipping && o2 > max) {                               \
3689b58a
             s->clippings++;                                                   \
9f956611
             obuf[i] = max;                                                    \
         } else {                                                              \
             obuf[i] = o2;                                                     \
         }                                                                     \
         i++;                                                                  \
a00c4b4d
         o1 = i1 * b2 + i2 * b1 + ibuf[i] * b0 + o1 * a2 + o2 * a1;            \
9f956611
         i1 = ibuf[i];                                                         \
ce385c86
         if (need_clipping && o1 < min) {                                      \
3689b58a
             s->clippings++;                                                   \
9f956611
             obuf[i] = min;                                                    \
ce385c86
         } else if (need_clipping && o1 > max) {                               \
3689b58a
             s->clippings++;                                                   \
9f956611
             obuf[i] = max;                                                    \
         } else {                                                              \
             obuf[i] = o1;                                                     \
         }                                                                     \
     }                                                                         \
     if (i < len) {                                                            \
a00c4b4d
         double o0 = ibuf[i] * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2;     \
b70ea49c
         i2 = i1;                                                              \
         i1 = ibuf[i];                                                         \
         o2 = o1;                                                              \
         o1 = o0;                                                              \
ce385c86
         if (need_clipping && o0 < min) {                                      \
3689b58a
             s->clippings++;                                                   \
b70ea49c
             obuf[i] = min;                                                    \
ce385c86
         } else if (need_clipping && o0 > max) {                               \
3689b58a
             s->clippings++;                                                   \
b70ea49c
             obuf[i] = max;                                                    \
         } else {                                                              \
             obuf[i] = o0;                                                     \
         }                                                                     \
     }                                                                         \
     *in1  = i1;                                                               \
     *in2  = i2;                                                               \
     *out1 = o1;                                                               \
     *out2 = o2;                                                               \
 }
 
ce385c86
 BIQUAD_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
 BIQUAD_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
 BIQUAD_FILTER(flt, float,   -1., 1., 0)
 BIQUAD_FILTER(dbl, double,  -1., 1., 0)
b70ea49c
 
 static int config_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx    = outlink->src;
6121968a
     BiquadsContext *s       = ctx->priv;
b70ea49c
     AVFilterLink *inlink    = ctx->inputs[0];
6121968a
     double A = exp(s->gain / 40 * log(10.));
     double w0 = 2 * M_PI * s->frequency / inlink->sample_rate;
b70ea49c
     double alpha;
 
     if (w0 > M_PI) {
         av_log(ctx, AV_LOG_ERROR,
                "Invalid frequency %f. Frequency must be less than half the sample-rate %d.\n",
6121968a
                s->frequency, inlink->sample_rate);
b70ea49c
         return AVERROR(EINVAL);
     }
 
6121968a
     switch (s->width_type) {
b70ea49c
     case NONE:
         alpha = 0.0;
         break;
33f5d70d
     case HERTZ:
6121968a
         alpha = sin(w0) / (2 * s->frequency / s->width);
b70ea49c
         break;
     case OCTAVE:
6121968a
         alpha = sin(w0) * sinh(log(2.) / 2 * s->width * w0 / sin(w0));
b70ea49c
         break;
     case QFACTOR:
6121968a
         alpha = sin(w0) / (2 * s->width);
b70ea49c
         break;
     case SLOPE:
6121968a
         alpha = sin(w0) / 2 * sqrt((A + 1 / A) * (1 / s->width - 1) + 2);
b70ea49c
         break;
     default:
         av_assert0(0);
     }
 
6121968a
     switch (s->filter_type) {
b70ea49c
     case biquad:
         break;
     case equalizer:
6121968a
         s->a0 =   1 + alpha / A;
         s->a1 =  -2 * cos(w0);
         s->a2 =   1 - alpha / A;
         s->b0 =   1 + alpha * A;
         s->b1 =  -2 * cos(w0);
         s->b2 =   1 - alpha * A;
b70ea49c
         break;
     case bass:
6121968a
         s->a0 =          (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
         s->a1 =    -2 * ((A - 1) + (A + 1) * cos(w0));
         s->a2 =          (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
         s->b0 =     A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
         s->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
         s->b2 =     A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
b70ea49c
         break;
     case treble:
6121968a
         s->a0 =          (A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
         s->a1 =     2 * ((A - 1) - (A + 1) * cos(w0));
         s->a2 =          (A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
         s->b0 =     A * ((A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
         s->b1 =-2 * A * ((A - 1) + (A + 1) * cos(w0));
         s->b2 =     A * ((A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
b70ea49c
         break;
     case bandpass:
6121968a
         if (s->csg) {
             s->a0 =  1 + alpha;
             s->a1 = -2 * cos(w0);
             s->a2 =  1 - alpha;
             s->b0 =  sin(w0) / 2;
             s->b1 =  0;
             s->b2 = -sin(w0) / 2;
b70ea49c
         } else {
6121968a
             s->a0 =  1 + alpha;
             s->a1 = -2 * cos(w0);
             s->a2 =  1 - alpha;
             s->b0 =  alpha;
             s->b1 =  0;
             s->b2 = -alpha;
b70ea49c
         }
         break;
     case bandreject:
6121968a
         s->a0 =  1 + alpha;
         s->a1 = -2 * cos(w0);
         s->a2 =  1 - alpha;
         s->b0 =  1;
         s->b1 = -2 * cos(w0);
         s->b2 =  1;
b70ea49c
         break;
     case lowpass:
6121968a
         if (s->poles == 1) {
             s->a0 = 1;
             s->a1 = -exp(-w0);
             s->a2 = 0;
             s->b0 = 1 + s->a1;
             s->b1 = 0;
             s->b2 = 0;
b70ea49c
         } else {
6121968a
             s->a0 =  1 + alpha;
             s->a1 = -2 * cos(w0);
             s->a2 =  1 - alpha;
             s->b0 = (1 - cos(w0)) / 2;
             s->b1 =  1 - cos(w0);
             s->b2 = (1 - cos(w0)) / 2;
b70ea49c
         }
         break;
     case highpass:
6121968a
         if (s->poles == 1) {
             s->a0 = 1;
             s->a1 = -exp(-w0);
             s->a2 = 0;
             s->b0 = (1 - s->a1) / 2;
             s->b1 = -s->b0;
             s->b2 = 0;
b70ea49c
         } else {
6121968a
             s->a0 =   1 + alpha;
             s->a1 =  -2 * cos(w0);
             s->a2 =   1 - alpha;
             s->b0 =  (1 + cos(w0)) / 2;
             s->b1 = -(1 + cos(w0));
             s->b2 =  (1 + cos(w0)) / 2;
b70ea49c
         }
         break;
     case allpass:
6121968a
         s->a0 =  1 + alpha;
         s->a1 = -2 * cos(w0);
         s->a2 =  1 - alpha;
         s->b0 =  1 - alpha;
         s->b1 = -2 * cos(w0);
         s->b2 =  1 + alpha;
b70ea49c
         break;
     default:
         av_assert0(0);
     }
 
6121968a
     s->a1 /= s->a0;
     s->a2 /= s->a0;
     s->b0 /= s->a0;
     s->b1 /= s->a0;
     s->b2 /= s->a0;
b70ea49c
 
6121968a
     s->cache = av_realloc_f(s->cache, sizeof(ChanCache), inlink->channels);
     if (!s->cache)
b70ea49c
         return AVERROR(ENOMEM);
6121968a
     memset(s->cache, 0, sizeof(ChanCache) * inlink->channels);
b70ea49c
 
     switch (inlink->format) {
6121968a
     case AV_SAMPLE_FMT_S16P: s->filter = biquad_s16; break;
     case AV_SAMPLE_FMT_S32P: s->filter = biquad_s32; break;
     case AV_SAMPLE_FMT_FLTP: s->filter = biquad_flt; break;
     case AV_SAMPLE_FMT_DBLP: s->filter = biquad_dbl; break;
b70ea49c
     default: av_assert0(0);
     }
 
9d08c7bd
     s->block_align = av_get_bytes_per_sample(inlink->format);
 
b70ea49c
     return 0;
 }
 
a05a44e2
 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
b70ea49c
 {
e6690ce0
     AVFilterContext  *ctx = inlink->dst;
     BiquadsContext *s     = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
a05a44e2
     AVFrame *out_buf;
     int nb_samples = buf->nb_samples;
b70ea49c
     int ch;
 
a05a44e2
     if (av_frame_is_writable(buf)) {
b70ea49c
         out_buf = buf;
     } else {
a05a44e2
         out_buf = ff_get_audio_buffer(inlink, nb_samples);
142894d7
         if (!out_buf) {
             av_frame_free(&buf);
b70ea49c
             return AVERROR(ENOMEM);
142894d7
         }
6fcb4ae1
         av_frame_copy_props(out_buf, buf);
b70ea49c
     }
 
9d08c7bd
     for (ch = 0; ch < buf->channels; ch++) {
         if (!((av_channel_layout_extract_channel(inlink->channel_layout, ch) & s->channels))) {
             if (buf != out_buf)
                 memcpy(out_buf->extended_data[ch], buf->extended_data[ch], nb_samples * s->block_align);
             continue;
         }
3689b58a
         s->filter(s, buf->extended_data[ch],
402ea625
                   out_buf->extended_data[ch], nb_samples,
6121968a
                   &s->cache[ch].i1, &s->cache[ch].i2,
                   &s->cache[ch].o1, &s->cache[ch].o2,
                   s->b0, s->b1, s->b2, s->a1, s->a2);
9d08c7bd
     }
b70ea49c
 
3689b58a
     if (s->clippings > 0)
         av_log(ctx, AV_LOG_WARNING, "clipping %d times. Please reduce gain.\n", s->clippings);
a9b33b5a
     s->clippings = 0;
3689b58a
 
b70ea49c
     if (buf != out_buf)
a05a44e2
         av_frame_free(&buf);
b70ea49c
 
     return ff_filter_frame(outlink, out_buf);
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
 {
6121968a
     BiquadsContext *s = ctx->priv;
b70ea49c
 
6121968a
     av_freep(&s->cache);
b70ea49c
 }
 
 static const AVFilterPad inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .filter_frame = filter_frame,
     },
     { NULL }
 };
 
 static const AVFilterPad outputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .config_props = config_output,
     },
     { NULL }
 };
 
 #define OFFSET(x) offsetof(BiquadsContext, x)
 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 
 #define DEFINE_BIQUAD_FILTER(name_, description_)                       \
 AVFILTER_DEFINE_CLASS(name_);                                           \
fd6228e6
 static av_cold int name_##_init(AVFilterContext *ctx) \
b70ea49c
 {                                                                       \
6121968a
     BiquadsContext *s = ctx->priv;                                      \
     s->class = &name_##_class;                                          \
     s->filter_type = name_;                                             \
fd6228e6
     return init(ctx);                                             \
b70ea49c
 }                                                                       \
                                                          \
325f6e0a
 AVFilter ff_af_##name_ = {                         \
b70ea49c
     .name          = #name_,                             \
     .description   = NULL_IF_CONFIG_SMALL(description_), \
     .priv_size     = sizeof(BiquadsContext),             \
     .init          = name_##_init,                       \
     .uninit        = uninit,                             \
     .query_formats = query_formats,                      \
     .inputs        = inputs,                             \
     .outputs       = outputs,                            \
     .priv_class    = &name_##_class,                     \
 }
 
 #if CONFIG_EQUALIZER_FILTER
 static const AVOption equalizer_options[] = {
     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
33f5d70d
     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
a23a56e7
     {"t",          "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
33f5d70d
     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
b70ea49c
     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
     {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
     {"w",     "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
     {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
     {"g",    "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
9d08c7bd
     {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
     {"c",        "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
b211607b
     {NULL}
b70ea49c
 };
 
 DEFINE_BIQUAD_FILTER(equalizer, "Apply two-pole peaking equalization (EQ) filter.");
 #endif  /* CONFIG_EQUALIZER_FILTER */
 #if CONFIG_BASS_FILTER
 static const AVOption bass_options[] = {
     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
33f5d70d
     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
a23a56e7
     {"t",          "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
33f5d70d
     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
b70ea49c
     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
     {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
     {"w",     "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
     {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
     {"g",    "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
9d08c7bd
     {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
     {"c",        "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
b211607b
     {NULL}
b70ea49c
 };
 
 DEFINE_BIQUAD_FILTER(bass, "Boost or cut lower frequencies.");
 #endif  /* CONFIG_BASS_FILTER */
 #if CONFIG_TREBLE_FILTER
 static const AVOption treble_options[] = {
     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
33f5d70d
     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
a23a56e7
     {"t",          "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
33f5d70d
     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
b70ea49c
     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
     {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
     {"w",     "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
     {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
     {"g",    "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
9d08c7bd
     {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
     {"c",        "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
b211607b
     {NULL}
b70ea49c
 };
 
 DEFINE_BIQUAD_FILTER(treble, "Boost or cut upper frequencies.");
 #endif  /* CONFIG_TREBLE_FILTER */
 #if CONFIG_BANDPASS_FILTER
 static const AVOption bandpass_options[] = {
     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
33f5d70d
     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
a23a56e7
     {"t",          "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
33f5d70d
     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
b70ea49c
     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
     {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
     {"w",     "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
1e2c23f0
     {"csg",   "use constant skirt gain", OFFSET(csg), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
9d08c7bd
     {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
     {"c",        "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
b211607b
     {NULL}
b70ea49c
 };
 
 DEFINE_BIQUAD_FILTER(bandpass, "Apply a two-pole Butterworth band-pass filter.");
 #endif  /* CONFIG_BANDPASS_FILTER */
 #if CONFIG_BANDREJECT_FILTER
 static const AVOption bandreject_options[] = {
     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
33f5d70d
     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
a23a56e7
     {"t",          "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
33f5d70d
     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
b70ea49c
     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
     {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
     {"w",     "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
9d08c7bd
     {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
     {"c",        "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
b211607b
     {NULL}
b70ea49c
 };
 
 DEFINE_BIQUAD_FILTER(bandreject, "Apply a two-pole Butterworth band-reject filter.");
 #endif  /* CONFIG_BANDREJECT_FILTER */
 #if CONFIG_LOWPASS_FILTER
 static const AVOption lowpass_options[] = {
     {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
     {"f",         "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
33f5d70d
     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
a23a56e7
     {"t",          "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
33f5d70d
     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
b70ea49c
     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
     {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
     {"w",     "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
     {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
     {"p",     "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
9d08c7bd
     {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
     {"c",        "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
b211607b
     {NULL}
b70ea49c
 };
 
 DEFINE_BIQUAD_FILTER(lowpass, "Apply a low-pass filter with 3dB point frequency.");
 #endif  /* CONFIG_LOWPASS_FILTER */
 #if CONFIG_HIGHPASS_FILTER
 static const AVOption highpass_options[] = {
     {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
     {"f",         "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
33f5d70d
     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
a23a56e7
     {"t",          "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
33f5d70d
     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
b70ea49c
     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
     {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
     {"w",     "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
     {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
     {"p",     "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
9d08c7bd
     {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
     {"c",        "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
b211607b
     {NULL}
b70ea49c
 };
 
 DEFINE_BIQUAD_FILTER(highpass, "Apply a high-pass filter with 3dB point frequency.");
 #endif  /* CONFIG_HIGHPASS_FILTER */
 #if CONFIG_ALLPASS_FILTER
 static const AVOption allpass_options[] = {
     {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
     {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
33f5d70d
     {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=HERTZ}, HERTZ, SLOPE, FLAGS, "width_type"},
a23a56e7
     {"t",          "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=HERTZ}, HERTZ, SLOPE, FLAGS, "width_type"},
33f5d70d
     {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
b70ea49c
     {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
     {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
     {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
     {"width", "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
     {"w",     "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
9d08c7bd
     {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
     {"c",        "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
b211607b
     {NULL}
b70ea49c
 };
 
 DEFINE_BIQUAD_FILTER(allpass, "Apply a two-pole all-pass filter.");
 #endif  /* CONFIG_ALLPASS_FILTER */
 #if CONFIG_BIQUAD_FILTER
 static const AVOption biquad_options[] = {
705b607d
     {"a0", NULL, OFFSET(a0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
     {"a1", NULL, OFFSET(a1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
     {"a2", NULL, OFFSET(a2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
     {"b0", NULL, OFFSET(b0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
     {"b1", NULL, OFFSET(b1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
     {"b2", NULL, OFFSET(b2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
9d08c7bd
     {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
     {"c",        "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
b211607b
     {NULL}
b70ea49c
 };
 
 DEFINE_BIQUAD_FILTER(biquad, "Apply a biquad IIR filter with the given coefficients.");
 #endif  /* CONFIG_BIQUAD_FILTER */