[FFmpeg-devel] [PATCH] Port biquads filters from SoX

Stefano Sabatini stefasab at gmail.com
Thu Jan 31 01:54:21 CET 2013


On date Wednesday 2013-01-30 20:17:40 +0000, Paul B Mahol encoded:
> Adds allpass, bandpass, bandreject, bass, biquad,
> equalizer, highpass, lowpass and treble filter.
> 
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>  doc/filters.texi         | 298 ++++++++++++++++++++++++
>  libavfilter/Makefile     |   9 +
>  libavfilter/af_biquads.c | 589 +++++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/allfilters.c |   9 +
>  4 files changed, 905 insertions(+)
>  create mode 100644 libavfilter/af_biquads.c
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 21e2cff..f553992 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -282,6 +282,304 @@ aconvert=u8:auto
>  @end example
>  @end itemize
>  
> + at section allpass
> +
> +Apply a two-pole all-pass filter with central frequency (in Hz)
> + at var{frequency}, and filter-width @var{width}.
> +An all-pass filter changes the audio's frequency to phase relationship
> +without changing its frequency to amplitude relationship.
> +
> +The filter accepts parameters as a list of @var{key}=@var{value}
> +pairs, separated by ":".
> +
> +A description of the accepted parameters follows.
> +
> + at table @option
> + at item frequency, f
> +A frequency in Hz.

Nit: Set frequency in Hz.

> +
> + at item width_type
> +Set method to specify band-width of filter.
> + at table @option
> + at item @var{Hz}
> + at code{h}
> + at item @var{Q-Factor}
> + at code{q}
> + at item @var{octave}
> + at code{o}
> + at item @var{slope}
> + at code{s}
> + at end table

Here it seems you inverted name and description.

> +
> + at item width, w
> +Used to specify the band-width of a filter in width_type units.

Specify the band-width... for grammar consistency.

> + at end table
> +
> + at section highpass
> +
> +Apply a high-pass filter with 3dB point frequency.
> +The filter can be either single-pole, or double-pole (the default).
> +The filter roll off at 6dB per pole per octave (20dB per pole per decade).
> +
> +The filter accepts parameters as a list of @var{key}=@var{value}
> +pairs, separated by ":".
> +
> +A description of the accepted parameters follows.
> +
> + at table @option

> + at item frequency, f
> +A frequency in Hz. Default is 3000.

Set a frequency ...

> +
> + at item poles, p
> +Number of poles. Default is 2.

Set number of poles ...

> +
> + at item width_type
> +Set method to specify band-width of filter.
> + at table @option
> + at item @var{Hz}
> + at code{h}
> + at item @var{Q-Factor}
> + at code{q}
> + at item @var{octave}
> + at code{o}
> + at item @var{slope}
> + at code{s}
> + at end table

Ditto

> +
> + at item width, w
> +Applies only to double-pole filter.

Set width ... Apply only ...

> +The default is 0.707 Q and gives a Butterworth response.
> + at end table
> +
> + at section lowpass
> +
> +Apply a low-pass filter with 3dB point frequency.
> +The filter can be either single-pole or double-pole (the default).
> +The filter roll off at 6dB per pole per octave (20dB per pole per decade).
> +
> +The filter accepts parameters as a list of @var{key}=@var{value}
> +pairs, separated by ":".
> +
> +A description of the accepted parameters follows.
> +
> + at table @option

> + at item frequency, f
> +A frequency in Hz. Default is 500.
> +
> + at item poles, p
> +Number of poles. Default is 2.

Ditto

> +
> + at item width_type
> +Set method to specify band-width of filter.
> + at table @option
> + at item @var{Hz}
> + at code{h}
> + at item @var{Q-Factor}
> + at code{q}
> + at item @var{octave}
> + at code{o}
> + at item @var{slope}
> + at code{s}
> + at end table

Same

> +
> + at item width, w
> +Applies only to double-pole filter.

Set width ... Apply only ...

> +The default is 0.707 Q and gives a Butterworth response.
> + at end table
> +
> + at section bass
> +
> +Boost or cut the bass (lower) frequencies of the audio using a two-pole
> +shelving filter with a response similar to that of a standard
> +hi-fi's tone-controls. This is also known as shelving equalisation (EQ).
> +
> +The filter accepts parameters as a list of @var{key}=@var{value}
> +pairs, separated by ":".
> +
> +A description of the accepted parameters follows.
> +
> + at table @option
> + at item gain, g
> +Gives the gain at 0 Hz. Its useful range is about -20

Give

> +(for a large cut) to +20 (for a large boost).
> +Beware of Clipping when using a positive gain.

_c_lipping?

> +
> + at item frequency, f
> +Sets the filter's central frequency and so can be used

Set 

> +to extend or reduce the frequency range to be boosted or cut.
> +The default value is @code{100} Hz.
> +
> + at item width_type
> +Set method to specify band-width of filter.

> + at table @option
> + at item @var{Hz}
> + at code{h}
> + at item @var{Q-Factor}
> + at code{q}
> + at item @var{octave}
> + at code{o}
> + at item @var{slope}
> + at code{s}
> + at end table

Ditto.

> +
> + at item width, w
> +Determines how steep is the filter's shelf transition.

Determine...

> + at end table
> +
> + at section treble
> +
> +Boost or cut treble (upper) frequencies of the audio using a two-pole
> +shelving filter with a response similar to that of a standard
> +hi-fi's tone-controls. This is also known as shelving equalisation (EQ).
> +
> +The filter accepts parameters as a list of @var{key}=@var{value}
> +pairs, separated by ":".
> +
> +A description of the accepted parameters follows.
> +
> + at table @option
> + at item gain, g
> +Gives the gain at whichever is the lower of ~22 kHz and the

Give

> +Nyquist frequency. Its useful range is about -20 (for a large cut)
> +to +20 (for a large boost). Beware of clipping when using a positive gain.
> +
> + at item frequency, f
> +Sets the filter's central frequency and so can be used

Set

> +to extend or reduce the frequency range to be boosted or cut.
> +The default value is @code{3000} Hz.
> +
> + at item width_type
> +Set method to specify band-width of filter.
> + at table @option
> + at item @var{Hz}
> + at code{h}
> + at item @var{Q-Factor}
> + at code{q}
> + at item @var{octave}
> + at code{o}
> + at item @var{slope}
> + at code{s}
> + at end table

Ditto


> +
> + at item width, w
> +Determines how steep is the filter's shelf transition.

Determine

> + at end table
> +
> + at section bandpass
> +
> +Apply a two-pole Butterworth band-pass filter with central
> +frequency @var{frequency}, and (3dB-point) band-width width.
> +The @var{csg} option selects a constant skirt gain (peak gain = Q)
> +instead of the default: constant 0dB peak gain.
> +The filter roll off at 6dB per octave (20dB per decade).
> +
> +The filter accepts parameters as a list of @var{key}=@var{value}
> +pairs, separated by ":".
> +
> +A description of the accepted parameters follows.
> +
> + at table @option
> + at item frequency, f
> +Sets the filter's central frequency. Default is @code{3000}.

Set

> +
> + at item csg
> +Use constant skirt gain. Defaults to disabled.

... skirt gain if set to 1. Default to 0.

> +
> + at item width_type
> +Set method to specify band-width of filter.
> + at table @option
> + at item @var{Hz}
> + at code{h}
> + at item @var{Q-Factor}
> + at code{q}
> + at item @var{octave}
> + at code{o}
> + at item @var{slope}
> + at code{s}
> + at end table
> +

> + at item width, w
> +Used to specify the band-width of a filter in Hz.

Specify the ...

> + at end table
> +
> + at section bandreject
> +
> +Apply a two-pole Butterworth band-reject filter with central
> +frequency @var{frequency}, and (3dB-point) band-width @var{width}.
> +The filter roll off at 6dB per octave (20dB per decade).
> +
> +The filter accepts parameters as a list of @var{key}=@var{value}
> +pairs, separated by ":".
> +
> +A description of the accepted parameters follows.
> +
> + at table @option
> + at item frequency, f
> +Sets the filter's central frequency. Default is @code{3000}.

Set

> +
> + at item width_type
> +Set method to specify band-width of filter.
> + at table @option
> + at item @var{Hz}
> + at code{h}
> + at item @var{Q-Factor}
> + at code{q}
> + at item @var{octave}
> + at code{o}
> + at item @var{slope}
> + at code{s}
> + at end table

Ditto

> +
> + at item width, w
> +Used to specify the band-width of a filter in Hz.

Specify

> + at end table
> +
> + at section biquad
> +
> +Apply a biquad IIR filter with the given coefficients.
> +Where @var{b0}, @var{b1}, @var{b2} and @var{a0}, @var{a1}, @var{a2}
> +are the numerator and denominator coefficients respectively.
> +
> + at section equalizer
> +
> +Apply a two-pole peaking equalisation (EQ) filter. With this
> +filter, the signal-level at and around a selected frequency can
> +be increased or decreased, whilst (unlike bandpass and bandreject
> +filters) that at all other frequencies is unchanged.
> +
> +In order to produce complex equalisation curves, this filter can
> +be given several times, each with a different central frequency.
> +
> +The filter accepts parameters as a list of @var{key}=@var{value}
> +pairs, separated by ":".
> +
> +A description of the accepted parameters follows.
> +
> + at table @option
> + at item frequency, f
> +Gives the filter's central frequency in Hz.

Give

> +
> + at item width_type
> +Set method to specify band-width of filter.
> + at table @option
> + at item @var{Hz}
> + at code{h}
> + at item @var{Q-Factor}
> + at code{q}
> + at item @var{octave}
> + at code{o}
> + at item @var{slope}
> + at code{s}
> + at end table

Ditto

> +

> + at item width, w
> +The band-width.

Set the band-width?

> +
> + at item gain, g
> +The required gain or attenuation in dB.

Set the required ...

> +Beware of clipping when using a positive gain.
> + at end table
> +
>  @section afade
>  
>  Apply fade-in/out effect to input audio.
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 5835a7e..938b183 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -53,6 +53,7 @@ OBJS-$(CONFIG_SWSCALE)                       += lswsutils.o
>  OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
>  OBJS-$(CONFIG_AFADE_FILTER)                  += af_afade.o
>  OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
> +OBJS-$(CONFIG_ALLPASS_FILTER)                += af_biquads.o
>  OBJS-$(CONFIG_AMERGE_FILTER)                 += af_amerge.o
>  OBJS-$(CONFIG_AMIX_FILTER)                   += af_amix.o
>  OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
> @@ -68,14 +69,22 @@ OBJS-$(CONFIG_ASPLIT_FILTER)                 += split.o
>  OBJS-$(CONFIG_ASTREAMSYNC_FILTER)            += af_astreamsync.o
>  OBJS-$(CONFIG_ASYNCTS_FILTER)                += af_asyncts.o
>  OBJS-$(CONFIG_ATEMPO_FILTER)                 += af_atempo.o
> +OBJS-$(CONFIG_BANDPASS_FILTER)               += af_biquads.o
> +OBJS-$(CONFIG_BANDREJECT_FILTER)             += af_biquads.o
> +OBJS-$(CONFIG_BASS_FILTER)                   += af_biquads.o
> +OBJS-$(CONFIG_BIQUAD_FILTER)                 += af_biquads.o
>  OBJS-$(CONFIG_CHANNELMAP_FILTER)             += af_channelmap.o
>  OBJS-$(CONFIG_CHANNELSPLIT_FILTER)           += af_channelsplit.o
>  OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
>  OBJS-$(CONFIG_EBUR128_FILTER)                += f_ebur128.o
> +OBJS-$(CONFIG_EQUALIZER_FILTER)              += af_biquads.o
> +OBJS-$(CONFIG_HIGHPASS_FILTER)               += af_biquads.o
>  OBJS-$(CONFIG_JOIN_FILTER)                   += af_join.o
> +OBJS-$(CONFIG_LOWPASS_FILTER)                += af_biquads.o
>  OBJS-$(CONFIG_PAN_FILTER)                    += af_pan.o
>  OBJS-$(CONFIG_RESAMPLE_FILTER)               += af_resample.o
>  OBJS-$(CONFIG_SILENCEDETECT_FILTER)          += af_silencedetect.o
> +OBJS-$(CONFIG_TREBLE_FILTER)                 += af_biquads.o
>  OBJS-$(CONFIG_VOLUME_FILTER)                 += af_volume.o
>  OBJS-$(CONFIG_VOLUMEDETECT_FILTER)           += af_volumedetect.o
>  
> diff --git a/libavfilter/af_biquads.c b/libavfilter/af_biquads.c
> new file mode 100644
> index 0000000..fd4244b
> --- /dev/null
> +++ b/libavfilter/af_biquads.c
> @@ -0,0 +1,589 @@
> +/*
> + * Copyright (c) 2013 Paul B Mahol
> + * Copyright (c) 2006-2008 Rob Sykes <robs at 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 at audioimagination.com>
> + *   see http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
> + *
> + * 1-pole filters based on code (c) 2000 Chris Bagwell <cbagwell at 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/opt.h"
> +#include "libavutil/avassert.h"
> +#include "audio.h"
> +#include "avfilter.h"
> +#include "internal.h"
> +
> +enum FilterType {
> +    biquad,
> +    equalizer,
> +    bass,
> +    treble,
> +    band,
> +    bandpass,
> +    bandreject,
> +    allpass,
> +    highpass,
> +    lowpass,
> +};
> +
> +enum WidthType {
> +    NONE,
> +    HZ,
> +    OCTAVE,
> +    QFACTOR,
> +    SLOPE,
> +};
> +
> +typedef struct ChanCache {
> +    double i1, i2;
> +    double o1, o2;
> +} ChanCache;
> +
> +typedef struct {
> +    const AVClass *class;
> +
> +    enum FilterType filter_type;
> +    enum WidthType width_type;
> +    int poles;
> +    int csg;
> +
> +    double gain;
> +    double frequency;
> +    double width;
> +
> +    double a0, a1, a2;
> +    double b0, b1, b2;
> +
> +    ChanCache *cache;
> +
> +    void (*filter)(const void *ibuf, void *obuf, int len,
> +                   double *i1, double *i2, double *o1, double *o2,
> +                   double b0, double b1, double b2, double a1, double a2);
> +} BiquadsContext;
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> +    BiquadsContext *p = ctx->priv;
> +    int ret;
> +
> +    av_opt_set_defaults(p);
> +
> +    if ((ret = av_set_options_string(p, args, "=", ":")) < 0)
> +        return ret;
> +
> +    if (p->filter_type != biquad) {
> +        if (p->frequency <= 0 || p->width <= 0) {
> +            av_log(ctx, AV_LOG_ERROR, "frequency and/or width <= 0\n");
> +            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
> +    };
> +
> +    layouts = ff_all_channel_layouts();
> +    if (!layouts)
> +        return AVERROR(ENOMEM);
> +    ff_set_common_channel_layouts(ctx, layouts);
> +
> +    formats = ff_make_format_list(sample_fmts);
> +    if (!formats)
> +        return AVERROR(ENOMEM);
> +    ff_set_common_formats(ctx, formats);
> +
> +    formats = ff_all_samplerates();
> +    if (!formats)
> +        return AVERROR(ENOMEM);
> +    ff_set_common_samplerates(ctx, formats);
> +
> +    return 0;
> +}
> +
> +#define BIQUAD_FILTER(name, type, min, max)                                   \
> +static void biquad_## name (const void *input, void *output, int len,         \
> +                            double *i1, double *i2, double *o1, double *o2,   \
> +                            double b0, double b1, double b2,                  \
> +                            double a1, double a2)                             \
> +{                                                                             \
> +    const type *ibuf = input;                                                 \
> +    type *obuf = output;                                                      \
> +    int i;                                                                    \
> +                                                                              \
> +    for (i = 0; i < len; i++) {                                               \
> +        double o0 = ibuf[i] * b0 + *i1 * b1 + *i2 * b2 - *o1 * a1 - *o2 * a2; \
> +        *i2 = *i1;                                                            \
> +        *i1 = ibuf[i];                                                        \
> +        *o2 = *o1;                                                            \
> +        *o1 = o0;                                                             \
> +        if (o0 < min) {                                                       \
> +            av_log(NULL, AV_LOG_WARNING, "clipping\n");                       \
> +            obuf[i] = min;                                                    \
> +        } else if (o0 > max) {                                                \
> +            av_log(NULL, AV_LOG_WARNING, "clipping\n");                       \
> +            obuf[i] = max;                                                    \
> +        } else {                                                              \
> +            obuf[i] = o0;                                                     \
> +        }                                                                     \
> +    }                                                                         \
> +}
> +
> +BIQUAD_FILTER(s16, int16_t, INT16_MIN, INT16_MAX)
> +BIQUAD_FILTER(s32, int32_t, INT32_MIN, INT32_MAX)
> +BIQUAD_FILTER(flt, float,   -1., 1.)
> +BIQUAD_FILTER(dbl, double,  -1., 1.)
> +
> +static int config_output(AVFilterLink *outlink)
> +{
> +    AVFilterContext *ctx    = outlink->src;
> +    BiquadsContext *p       = ctx->priv;
> +    AVFilterLink *inlink    = ctx->inputs[0];
> +    double A = exp(p->gain / 40 * log(10.));
> +    double w0 = 2 * M_PI * p->frequency / inlink->sample_rate;
> +    double alpha;
> +
> +    if (w0 > M_PI) {
> +        av_log(ctx, AV_LOG_ERROR,
> +               "frequency %f must be less than half the sample-rate %d\n",
> +               p->frequency, inlink->sample_rate);

Nit: Invalid frequency %f. Frequency must be ...

[...]

No more nits from me. Feel free to push if you tested it enough and
there are no people willing to review the processing code.

Thanks.
-- 
FFmpeg = Faithless Fast Mournful Patchable Encoding/decoding Gladiator


More information about the ffmpeg-devel mailing list