[FFmpeg-devel] [PATCH] avfilter: add firequalizer filter
Paul B Mahol
onemda at gmail.com
Tue Feb 16 12:48:02 CET 2016
On 2/16/16, Muhammad Faiz <mfcc64 at gmail.com> wrote:
> patch attached
>
> thank's
>
>
> ---
> Changelog | 1 +
> MAINTAINERS | 1 +
> configure | 2 +
> doc/filters.texi | 109 ++++++++
> libavfilter/Makefile | 1 +
> libavfilter/af_firequalizer.c | 592 ++++++++++++++++++++++++++++++++++++++++++
> libavfilter/allfilters.c | 1 +
> libavfilter/version.h | 2 +-
> 8 files changed, 708 insertions(+), 1 deletion(-)
> create mode 100644 libavfilter/af_firequalizer.c
>
> diff --git a/Changelog b/Changelog
> index 96a9955..1794164 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -2,6 +2,7 @@ Entries are sorted chronologically from oldest to youngest within each release,
> releases are sorted from youngest to oldest.
>
> version <next>:
> +- firequalizer filter
>
Interesting.
>
> version 3.0:
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e57150d..9f7baf0 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -353,6 +353,7 @@ Filters:
> af_biquads.c Paul B Mahol
> af_chorus.c Paul B Mahol
> af_compand.c Paul B Mahol
> + af_firequalizer.c Muhammad Faiz
> af_ladspa.c Paul B Mahol
> af_pan.c Nicolas George
> af_sidechaincompress.c Paul B Mahol
> diff --git a/configure b/configure
> index 2148f11..b775cb9 100755
> --- a/configure
> +++ b/configure
> @@ -2857,6 +2857,8 @@ eq_filter_deps="gpl"
> fftfilt_filter_deps="avcodec"
> fftfilt_filter_select="rdft"
> find_rect_filter_deps="avcodec avformat gpl"
> +firequalizer_filter_deps="avcodec"
> +firequalizer_filter_select="rdft"
> flite_filter_deps="libflite"
> frei0r_filter_deps="frei0r dlopen"
> frei0r_src_filter_deps="frei0r dlopen"
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 68f54f1..67506dc 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -2366,6 +2366,115 @@ Sets the difference coefficient (default: 2.5). 0.0 means mono sound
> Enable clipping. By default is enabled.
> @end table
>
> + at section firequalizer
> +Apply FIR Equalization using arbitrary frequency response.
> +
> +The filter accepts the following option:
> +
> + at table @option
> + at item gain
> +Set gain curve equation (in dB). The expression can contain variables:
> + at table @option
> + at item f
> +the evaluated frequency
> + at item sr
> +sample rate
> + at item ch
> +channel number, set to 0 when multichannels evaluation is disabled
> + at item chid
> +channel id, see libavutil/channel_layout.h, set to the first channel id when
> +multichannels evaluation is disabled
> + at item chs
> +number of channels
> + at item chlayout
> +channel_layout, see libavutil/channel_layout.h
> +
> + at end table
> +and functions:
> + at table @option
> + at item gain_interpolate(f)
> +interpote gain on frequency f based on gain_entry
> + at end table
> +This option is also available as command. Default is @code{gain_interpolate(f)}.
> +
> + at item gain_entry
> +Set gain entry for gain_interpolate function. The expression can
> +contain functions:
> + at table @option
> + at item entry(f, g)
> +store gain entry at frequency f with value g
> + at end table
> +This option is also available as command.
> +
> + at item delay
> +Set filter delay in seconds. Higher value means more accurate.
> +Default is @code{0.01}.
> +
> + at item accuracy
> +Set filter accuracy in Hz. Lower value means more accurate.
> +Default is @code{5}.
> +
> + at item wfunc
> +Set window function. Acceptable values are:
> + at table @option
> + at item rectangular
> +rectangular window, useful when gain curve is already smooth
> + at item hann
> +hann window (default)
> + at item hamming
> +hamming window
> + at item blackman
> +blackman window
> + at item nuttall3
> +3-terms continuous 1st derivative nuttall window
> + at item mnuttall3
> +minimum 3-terms discontinuous nuttall window
> + at item nuttall
> +4-terms continuous 1st derivative nuttall window
> + at item bnuttall
> +minimum 4-terms discontinuous nuttall (blackman-nuttall) window
> + at item bharris
> +blackman-harris window
> + at end table
> +
> + at item fixed
> +If enabled, use fixed number of audio samples. This improves speed when
> +filtering with large delay. Default is disabled.
> +
> + at item multi
> +Enable multichannels evaluation on gain. Default is disabled.
> + at end table
> +
> + at subsection Examples
> + at itemize
> + at item
> +lowpass at 1000 Hz:
> + at example
> +firequalizer=gain='if(lt(f,1000), 0, -INF)'
> + at end example
> + at item
> +lowpass at 1000 Hz with gain_entry:
> + at example
> +firequalizer=gain_entry='entry(1000,0); entry(1001, -INF)'
> + at end example
> + at item
> +custom equalization:
> + at example
> +firequalizer=gain_entry='entry(100,0); entry(400, -4); entry(1000, -6); entry(2000, 0)'
> + at end example
> + at item
> +higher delay:
> + at example
> +firequalizer=delay=0.1:fixed=on
> + at end example
> + at item
> +lowpass on left channel, highpass on right channel:
> + at example
> +firequalizer=gain='if(eq(chid,1), gain_interpolate(f), if(eq(chid,2), gain_interpolate(1e6+f), 0))'
> +:gain_entry='entry(1000, 0); entry(1001,-INF); entry(1e6+1000,0)':multi=on
> + at end example
> + at end itemize
> +
> @section flanger
> Apply a flanging effect to the audio.
>
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 8916588..5f74b6a 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -79,6 +79,7 @@ OBJS-$(CONFIG_EARWAX_FILTER) += af_earwax.o
> OBJS-$(CONFIG_EBUR128_FILTER) += f_ebur128.o
> OBJS-$(CONFIG_EQUALIZER_FILTER) += af_biquads.o
> OBJS-$(CONFIG_EXTRASTEREO_FILTER) += af_extrastereo.o
> +OBJS-$(CONFIG_FIREQUALIZER_FILTER) += af_firequalizer.o
> OBJS-$(CONFIG_FLANGER_FILTER) += af_flanger.o generate_wave_table.o
> OBJS-$(CONFIG_HIGHPASS_FILTER) += af_biquads.o
> OBJS-$(CONFIG_JOIN_FILTER) += af_join.o
> diff --git a/libavfilter/af_firequalizer.c b/libavfilter/af_firequalizer.c
> new file mode 100644
> index 0000000..4d3007c
> --- /dev/null
> +++ b/libavfilter/af_firequalizer.c
> @@ -0,0 +1,592 @@
> +/*
> + * Copyright (c) 2016 Muhammad Faiz <mfcc64 at gmail.com>
> + *
> + * 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
> + */
> +
> +#include "libavutil/opt.h"
> +#include "libavutil/eval.h"
> +#include "libavutil/avassert.h"
> +#include "libavcodec/avfft.h"
> +#include "avfilter.h"
> +#include "internal.h"
> +#include "audio.h"
> +
> +#define RDFT_BITS_MIN 4
> +#define RDFT_BITS_MAX 16
> +
> +enum WindowFunc {
> + WFUNC_MIN,
> + WFUNC_RECTANGULAR = WFUNC_MIN,
> + WFUNC_HANN,
> + WFUNC_HAMMING,
> + WFUNC_BLACKMAN,
> + WFUNC_NUTTALL3,
> + WFUNC_MNUTTALL3,
> + WFUNC_NUTTALL,
> + WFUNC_BNUTTALL,
> + WFUNC_BHARRIS,
> + WFUNC_MAX = WFUNC_BHARRIS
> +};
> +
> +#define NB_GAIN_ENTRY_MAX 4096
> +typedef struct {
> + double freq;
> + double gain;
> +} GainEntry;
> +
> +typedef struct {
> + int buf_idx;
> + int overlap_idx;
> +} OverlapIndex;
> +
> +typedef struct {
> + const AVClass *class;
> +
> + RDFTContext *analysis_irdft;
> + RDFTContext *rdft;
> + RDFTContext *irdft;
> + int analysis_rdft_len;
> + int rdft_len;
> +
> + float *analysis_buf;
> + float *kernel_tmp_buf;
> + float *kernel_buf;
> + float *conv_buf;
> + OverlapIndex *conv_idx;
> + int fir_len;
> + int nsamples_max;
> + int64_t next_pts;
> + int frame_nsamples_max;
> + int remaining;
> +
> + char *gain_cmd;
> + char *gain_entry_cmd;
> + const char *gain;
> + const char *gain_entry;
> + double delay;
> + double accuracy;
> + int wfunc;
> + int fixed;
> + int multi;
> +
> + int nb_gain_entry;
> + int gain_entry_err;
> + GainEntry gain_entry_tbl[NB_GAIN_ENTRY_MAX];
> +} FIREqualizerContext;
> +
> +#define OFFSET(x) offsetof(FIREqualizerContext, x)
> +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
> +
> +static const AVOption firequalizer_options[] = {
> + { "gain", "set gain curve", OFFSET(gain), AV_OPT_TYPE_STRING, { .str = "gain_interpolate(f)" }, 0, 0, FLAGS },
> + { "gain_entry", "set gain entry", OFFSET(gain_entry), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
> + { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.0, 1e10, FLAGS },
> + { "accuracy", "set accuracy", OFFSET(accuracy), AV_OPT_TYPE_DOUBLE, { .dbl = 5.0 }, 0.0, 1e10, FLAGS },
> + { "wfunc", "set window function", OFFSET(wfunc), AV_OPT_TYPE_INT, { .i64 = WFUNC_HANN }, WFUNC_MIN, WFUNC_MAX, FLAGS, "wfunc" },
> + { "rectangular", "rectangular window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_RECTANGULAR }, 0, 0, FLAGS, "wfunc" },
> + { "hann", "hann window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HANN }, 0, 0, FLAGS, "wfunc" },
> + { "hamming", "hamming window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HAMMING }, 0, 0, FLAGS, "wfunc" },
> + { "blackman", "blackman window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BLACKMAN }, 0, 0, FLAGS, "wfunc" },
> + { "nuttall3", "3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL3 }, 0, 0, FLAGS, "wfunc" },
> + { "mnuttall3", "minimum 3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_MNUTTALL3 }, 0, 0, FLAGS, "wfunc" },
> + { "nuttall", "nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL }, 0, 0, FLAGS, "wfunc" },
> + { "bnuttall", "blackman-nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BNUTTALL }, 0, 0, FLAGS, "wfunc" },
> + { "bharris", "blackman-harris window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BHARRIS }, 0, 0, FLAGS, "wfunc" },
> + { "fixed", "set fixed frame samples", OFFSET(fixed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
> + { "multi", "set multi channels mode", OFFSET(multi), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
> + { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(firequalizer);
> +
> +static void common_uninit(FIREqualizerContext *s)
> +{
> + av_rdft_end(s->analysis_irdft);
> + av_rdft_end(s->rdft);
> + av_rdft_end(s->irdft);
> + s->analysis_irdft = s->rdft = s->irdft = NULL;
> +
> + av_freep(&s->analysis_buf);
> + av_freep(&s->kernel_tmp_buf);
> + av_freep(&s->kernel_buf);
> + av_freep(&s->conv_buf);
> + av_freep(&s->conv_idx);
> +}
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> + FIREqualizerContext *s = ctx->priv;
> +
> + common_uninit(s);
> + av_freep(&s->gain_cmd);
> + av_freep(&s->gain_entry_cmd);
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> + AVFilterChannelLayouts *layouts;
> + AVFilterFormats *formats;
> + static const enum AVSampleFormat sample_fmts[] = {
> + AV_SAMPLE_FMT_FLTP,
> + AV_SAMPLE_FMT_NONE
> + };
> + int ret;
> +
> + layouts = ff_all_channel_counts();
> + if (!layouts)
> + return AVERROR(ENOMEM);
> + ret = ff_set_common_channel_layouts(ctx, layouts);
> + if (ret < 0)
> + return ret;
> +
> + formats = ff_make_format_list(sample_fmts);
> + if (!formats)
> + return AVERROR(ENOMEM);
> + ret = ff_set_common_formats(ctx, formats);
> + if (ret < 0)
> + return ret;
> +
> + formats = ff_all_samplerates();
> + if (!formats)
> + return AVERROR(ENOMEM);
> + return ff_set_common_samplerates(ctx, formats);
> +}
> +
> +static void fast_convolute(FIREqualizerContext *s, const float *kernel_buf, float *conv_buf,
> + OverlapIndex *idx, float *data, int nsamples)
> +{
> + if (nsamples <= s->nsamples_max) {
> + float *buf = conv_buf + idx->buf_idx * s->rdft_len;
> + float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx;
> + int k;
> +
> + memcpy(buf, data, nsamples * sizeof(*data));
> + memset(buf + nsamples, 0, (s->rdft_len - nsamples) * sizeof(*data));
> + av_rdft_calc(s->rdft, buf);
> +
> + buf[0] *= kernel_buf[0];
> + buf[1] *= kernel_buf[1];
> + for (k = 2; k < s->rdft_len; k += 2) {
> + float re, im;
> + re = buf[k] * kernel_buf[k] - buf[k+1] * kernel_buf[k+1];
> + im = buf[k] * kernel_buf[k+1] + buf[k+1] * kernel_buf[k];
> + buf[k] = re;
> + buf[k+1] = im;
> + }
> +
> + av_rdft_calc(s->irdft, buf);
> + for (k = 0; k < s->rdft_len - idx->overlap_idx; k++)
> + buf[k] += obuf[k];
> + memcpy(data, buf, nsamples * sizeof(*data));
> + idx->buf_idx = !idx->buf_idx;
> + idx->overlap_idx = nsamples;
> + } else {
> + while (nsamples > s->nsamples_max * 2) {
> + fast_convolute(s, kernel_buf, conv_buf, idx, data, s->nsamples_max);
> + data += s->nsamples_max;
> + nsamples -= s->nsamples_max;
> + }
> + fast_convolute(s, kernel_buf, conv_buf, idx, data, nsamples/2);
> + fast_convolute(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2);
> + }
> +}
> +
> +static double entry_func(void *p, double freq, double gain)
> +{
> + AVFilterContext *ctx = p;
> + FIREqualizerContext *s = ctx->priv;
> +
> + if (s->nb_gain_entry >= NB_GAIN_ENTRY_MAX) {
> + av_log(ctx, AV_LOG_ERROR, "entry table overflow.\n");
> + s->gain_entry_err = AVERROR(EINVAL);
> + return 0;
> + }
> +
> + if (isnan(freq)) {
> + av_log(ctx, AV_LOG_ERROR, "nan frequency (%g, %g).\n", freq, gain);
> + s->gain_entry_err = AVERROR(EINVAL);
> + return 0;
> + }
> +
> + if (s->nb_gain_entry > 0 && freq <= s->gain_entry_tbl[s->nb_gain_entry - 1].freq) {
> + av_log(ctx, AV_LOG_ERROR, "unsorted frequency (%g, %g).\n", freq, gain);
> + s->gain_entry_err = AVERROR(EINVAL);
> + return 0;
> + }
> +
> + s->gain_entry_tbl[s->nb_gain_entry].freq = freq;
> + s->gain_entry_tbl[s->nb_gain_entry].gain = gain;
> + s->nb_gain_entry++;
> + return 0;
> +}
> +
> +static int gain_entry_compare(const void *key, const void *memb)
> +{
> + const double *freq = key;
> + const GainEntry *entry = memb;
> +
> + if (*freq < entry[0].freq)
> + return -1;
> + if (*freq > entry[1].freq)
> + return 1;
> + return 0;
> +}
> +
> +static double gain_interpolate_func(void *p, double freq)
> +{
> + AVFilterContext *ctx = p;
> + FIREqualizerContext *s = ctx->priv;
> + GainEntry *res;
> + double d0, d1, d;
> +
> + if (isnan(freq))
> + return freq;
> +
> + if (!s->nb_gain_entry)
> + return 0;
> +
> + if (freq <= s->gain_entry_tbl[0].freq)
> + return s->gain_entry_tbl[0].gain;
> +
> + if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq)
> + return s->gain_entry_tbl[s->nb_gain_entry-1].gain;
> +
> + res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare);
> + av_assert0(res);
> +
> + d = res[1].freq - res[0].freq;
> + d0 = freq - res[0].freq;
> + d1 = res[1].freq - freq;
> +
> + if (d0 && d1)
> + return (d0 * res[1].gain + d1 * res[0].gain) / d;
> +
> + if (d0)
> + return res[1].gain;
> +
> + return res[0].gain;
> +}
> +
> +static const char *const var_names[] = {
> + "f",
> + "sr",
> + "ch",
> + "chid",
> + "chs",
> + "chlayout",
> + NULL
> +};
> +
> +enum VarOffset {
> + VAR_F,
> + VAR_SR,
> + VAR_CH,
> + VAR_CHID,
> + VAR_CHS,
> + VAR_CHLAYOUT,
> + VAR_NB
> +};
> +
> +static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *gain_entry)
> +{
> + FIREqualizerContext *s = ctx->priv;
> + AVFilterLink *inlink = ctx->inputs[0];
> + const char *gain_entry_func_names[] = { "entry", NULL };
> + const char *gain_func_names[] = { "gain_interpolate", NULL };
> + double (*gain_entry_funcs[])(void *, double, double) = { entry_func, NULL };
> + double (*gain_funcs[])(void *, double) = { gain_interpolate_func, NULL };
> + double vars[VAR_NB];
> + AVExpr *gain_expr;
> + int ret, k, center, ch;
> +
> + s->nb_gain_entry = 0;
> + s->gain_entry_err = 0;
> + if (gain_entry) {
> + double result = 0.0;
> + ret = av_expr_parse_and_eval(&result, gain_entry, NULL, NULL, NULL, NULL,
> + gain_entry_func_names, gain_entry_funcs, ctx, 0, ctx);
> + if (ret < 0)
> + return ret;
> + if (s->gain_entry_err < 0)
> + return s->gain_entry_err;
> + }
> +
> + av_log(ctx, AV_LOG_DEBUG, "nb_gain_entry = %d.\n", s->nb_gain_entry);
> +
> + ret = av_expr_parse(&gain_expr, gain, var_names,
> + gain_func_names, gain_funcs, NULL, NULL, 0, ctx);
> + if (ret < 0)
> + return ret;
> +
> + vars[VAR_CHS] = inlink->channels;
> + vars[VAR_CHLAYOUT] = inlink->channel_layout;
> + vars[VAR_SR] = inlink->sample_rate;
> + for (ch = 0; ch < inlink->channels; ch++) {
> + vars[VAR_CH] = ch;
> + vars[VAR_CHID] = av_channel_layout_extract_channel(inlink->channel_layout, ch);
> + vars[VAR_F] = 0.0;
> + s->analysis_buf[0] = pow(10.0, 0.05 * av_expr_eval(gain_expr, vars, ctx));
> + vars[VAR_F] = 0.5 * inlink->sample_rate;
> + s->analysis_buf[1] = pow(10.0, 0.05 * av_expr_eval(gain_expr, vars, ctx));
> +
> + for (k = 1; k < s->analysis_rdft_len/2; k++) {
> + vars[VAR_F] = k * ((double)inlink->sample_rate /(double)s->analysis_rdft_len);
> + s->analysis_buf[2*k] = pow(10.0, 0.05 * av_expr_eval(gain_expr, vars, ctx));
> + s->analysis_buf[2*k+1] = 0.0;
> + }
> +
> + av_rdft_calc(s->analysis_irdft, s->analysis_buf);
> + center = s->fir_len / 2;
> +
> + for (k = 0; k <= center; k++) {
> + double u = k * (M_PI/center);
> + double win;
> + switch (s->wfunc) {
> + case WFUNC_RECTANGULAR:
> + win = 1.0;
> + break;
> + case WFUNC_HANN:
> + win = 0.5 + 0.5 * cos(u);
> + break;
> + case WFUNC_HAMMING:
> + win = 0.53836 + 0.46164 * cos(u);
> + break;
> + case WFUNC_BLACKMAN:
> + win = 0.48 + 0.5 * cos(u) + 0.02 * cos(2*u);
> + break;
> + case WFUNC_NUTTALL3:
> + win = 0.40897 + 0.5 * cos(u) + 0.09103 * cos(2*u);
> + break;
> + case WFUNC_MNUTTALL3:
> + win = 0.4243801 + 0.4973406 * cos(u) + 0.0782793 * cos(2*u);
> + break;
> + case WFUNC_NUTTALL:
> + win = 0.355768 + 0.487396 * cos(u) + 0.144232 * cos(2*u) + 0.012604 * cos(3*u);
> + break;
> + case WFUNC_BNUTTALL:
> + win = 0.3635819 + 0.4891775 * cos(u) + 0.1365995 * cos(2*u) + 0.0106411 * cos(3*u);
> + break;
> + case WFUNC_BHARRIS:
> + win = 0.35875 + 0.48829 * cos(u) + 0.14128 * cos(2*u) + 0.01168 * cos(3*u);
> + break;
> + default:
> + av_assert0(0);
Wrong indentation, stuff under 'case:' chould be under 'switch'.
Rest looks good so far.
More information about the ffmpeg-devel
mailing list