diff mbox series

[FFmpeg-devel] avfilter: add FIR equalizer coefficients source filter

Message ID CAPYw7P79XzdycPfV3Rr5EMKV4-E2ig+bHmNOKCo+6tEE1FP51g@mail.gmail.com
State New
Headers show
Series [FFmpeg-devel] avfilter: add FIR equalizer coefficients source filter | expand

Checks

Context Check Description
yinshiyou/configure_loongarch64 warning Failed to apply patch

Commit Message

Paul B Mahol Jan. 15, 2023, 9:48 a.m. UTC
Patch attached
diff mbox series

Patch

From 5e22b9bc08f7015853cc93a07e165155601b87fe Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@gmail.com>
Date: Fri, 13 Jan 2023 13:32:26 +0100
Subject: [PATCH] avfilter: add FIR equalizer coefficients source filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 doc/filters.texi           |  61 +++++++++
 libavfilter/Makefile       |   1 +
 libavfilter/allfilters.c   |   1 +
 libavfilter/asrc_afirsrc.c | 247 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 308 insertions(+), 2 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index be70a2396b..c86f44d294 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -7546,6 +7546,67 @@  Specifies the channel layout, and can be a string representing a channel layout.
 The default value of @var{channel_layout} is "stereo".
 @end table
 
+@section afireqsrc
+
+Generate a FIR equalizer coefficients.
+
+The resulting stream can be used with @ref{afir} filter for filtering the audio signal.
+
+The filter accepts the following options:
+
+@table @option
+@item preset, p
+Set equalizer preset.
+Default preset is @code{flat}.
+
+Available presets are:
+@table @samp
+@item custom
+@item flat
+@item acoustic
+@item bass
+@item beats
+@item classic
+@item clear
+@item deep bass
+@item dubstep
+@item electronic
+@item hard-style
+@item hip-hop
+@item jazz
+@item metal
+@item movie
+@item pop
+@item r&b
+@item rock
+@item vocal booster
+@end table
+
+@item gains, g
+Set custom gains for each band. Only used if the preset option is set to @code{custom}.
+Gains are separated by white spaces and each gain is set in dBFS.
+Default is @code{0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0}.
+
+@item bands, b
+Set the custom bands from where custon equalizer gains are set.
+This must be in strictly increasing order. Only used if the preset option is set to @code{custom}.
+Bands are separated by white spaces and each band represent frequency in Hz.
+Default is @code{25 40 63 100 160 250 400 630 1000 1600 2500 4000 6300 10000 16000 24000}.
+
+@item taps, t
+Set number of filter coefficents in output audio stream.
+Default value is @code{4096}.
+
+@item sample_rate, r
+Set sample rate of output audio stream, default is @code{44100}.
+
+@item nb_samples, n
+Set number of samples per each frame in output audio stream. Default is @code{1024}.
+
+@item interp, i
+Set interpolation method for FIR equalizer coefficients. Can be @code{linear} or @code{cubic}.
+@end table
+
 @section afirsrc
 
 Generate a FIR coefficients using frequency sampling method.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index a9c835474e..285ef74068 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -174,6 +174,7 @@  OBJS-$(CONFIG_VOLUMEDETECT_FILTER)           += af_volumedetect.o
 
 OBJS-$(CONFIG_AEVALSRC_FILTER)               += aeval.o
 OBJS-$(CONFIG_AFDELAYSRC_FILTER)             += asrc_afdelaysrc.o
+OBJS-$(CONFIG_AFIREQSRC_FILTER)              += asrc_afirsrc.o
 OBJS-$(CONFIG_AFIRSRC_FILTER)                += asrc_afirsrc.o
 OBJS-$(CONFIG_ANOISESRC_FILTER)              += asrc_anoisesrc.o
 OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 1c729090e3..c1faafca4a 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -162,6 +162,7 @@  extern const AVFilter ff_af_volumedetect;
 
 extern const AVFilter ff_asrc_aevalsrc;
 extern const AVFilter ff_asrc_afdelaysrc;
+extern const AVFilter ff_asrc_afireqsrc;
 extern const AVFilter ff_asrc_afirsrc;
 extern const AVFilter ff_asrc_anoisesrc;
 extern const AVFilter ff_asrc_anullsrc;
diff --git a/libavfilter/asrc_afirsrc.c b/libavfilter/asrc_afirsrc.c
index d2ea92c41c..1bc6451565 100644
--- a/libavfilter/asrc_afirsrc.c
+++ b/libavfilter/asrc_afirsrc.c
@@ -18,7 +18,9 @@ 
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/cpu.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/ffmath.h"
 #include "libavutil/eval.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
@@ -38,6 +40,8 @@  typedef struct AudioFIRSourceContext {
     int sample_rate;
     int nb_samples;
     int win_func;
+    int preset;
+    int interp;
 
     AVComplexFloat *complexf;
     float *freq;
@@ -131,7 +135,7 @@  static int parse_string(char *str, float **items, int *nb_items, int *items_size
     float *new_items;
     char *tail;
 
-    new_items = av_fast_realloc(NULL, items_size, 1 * sizeof(float));
+    new_items = av_fast_realloc(NULL, items_size, sizeof(float));
     if (!new_items)
         return AVERROR(ENOMEM);
     *items = new_items;
@@ -142,7 +146,7 @@  static int parse_string(char *str, float **items, int *nb_items, int *items_size
 
     do {
         (*items)[(*nb_items)++] = av_strtod(tail, &tail);
-        new_items = av_fast_realloc(*items, items_size, (*nb_items + 1) * sizeof(float));
+        new_items = av_fast_realloc(*items, items_size, (*nb_items + 2) * sizeof(float));
         if (!new_items)
             return AVERROR(ENOMEM);
         *items = new_items;
@@ -300,3 +304,242 @@  const AVFilter ff_asrc_afirsrc = {
     FILTER_QUERY_FUNC(query_formats),
     .priv_class    = &afirsrc_class,
 };
+
+#define DEFAULT_BANDS "25 40 63 100 160 250 400 630 1000 1600 2500 4000 6300 10000 16000 24000"
+
+typedef struct EqPreset {
+    char name[16];
+    float gains[16];
+} EqPreset;
+
+static const EqPreset eq_presets[] = {
+    { "flat",          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
+    { "acoustic",      { 5.0, 4.5, 4.0, 3.5, 1.5, 1.0, 1.5, 1.5, 2.0, 3.0, 3.5, 4.0, 3.7, 3.0, 3.0 } },
+    { "bass",          { 10.0, 8.8, 8.5, 6.5, 2.5, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
+    { "beats",         { -5.5, -5.0, -4.5, -4.2, -3.5, -3.0, -1.9, 0, 0, 0, 0, 0, 0, 0, 0 } },
+    { "classic",       { -0.3, 0.3, -3.5, -9.0, -1.0, 0.0, 1.8, 2.1, 0.0, 0.0, 0.0, 4.4, 9.0, 9.0, 9.0 } },
+    { "clear",         { 3.5, 5.5, 6.5, 9.5, 8.0, 6.5, 3.5, 2.5, 1.3, 5.0, 7.0, 9.0, 10.0, 11.0, 9.0 } },
+    { "deep bass",     { 12.0, 8.0, 0.0, -6.7, -12.0, -9.0, -3.5, -3.5, -6.1, 0.0, -3.0, -5.0, 0.0, 1.2, 3.0 } },
+    { "dubstep",       { 12.0, 10.0, 0.5, -1.0, -3.0, -5.0, -5.0, -4.8, -4.5, -2.5, -1.0, 0.0, -2.5, -2.5, 0.0 } },
+    { "electronic",    { 4.0, 4.0, 3.5, 1.0, 0.0, -0.5, -2.0, 0.0, 2.0, 0.0, 0.0, 1.0, 3.0, 4.0, 4.5 } },
+    { "hardstyle",     { 6.1, 7.0, 12.0, 6.1, -5.0, -12.0, -2.5, 3.0, 6.5, 0.0, -2.2, -4.5, -6.1, -9.2, -10.0 } },
+    { "hip-hop",       { 4.5, 4.3, 4.0, 2.5, 1.5, 3.0, -1.0, -1.5, -1.5, 1.5, 0.0, -1.0, 0.0, 1.5, 3.0 } },
+    { "jazz",          { 0.0, 0.0, 0.0, 2.0, 4.0, 5.9, -5.9, -4.5, -2.5, 2.5, 1.0, -0.8, -0.8, -0.8, -0.8 } },
+    { "metal",         { 10.5, 10.5, 7.5, 0.0, 2.0, 5.5, 0.0, 0.0, 0.0, 6.1, 0.0, 0.0, 6.1, 10.0, 12.0 } },
+    { "movie",         { 3.0, 3.0, 6.1, 8.5, 9.0, 7.0, 6.1, 6.1, 5.0, 8.0, 3.5, 3.5, 8.0, 10.0, 8.0 } },
+    { "pop",           { 0.0, 0.0, 0.0, 0.0, 0.0, 1.3, 2.0, 2.5, 5.0, -1.5, -2.0, -3.0, -3.0, -3.0, -3.0 } },
+    { "r&b",           { 3.0, 3.0, 7.0, 6.1, 4.5, 1.5, -1.5, -2.0, -1.5, 2.0, 2.5, 3.0, 3.5, 3.8, 4.0 } },
+    { "rock",          { 0.0, 0.0, 0.0, 3.0, 3.0, -10.0, -4.0, -1.0, 0.8, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0 } },
+    { "vocal booster", { -1.5, -2.0, -3.0, -3.0, -0.5, 1.5, 3.5, 3.5, 3.5, 3.0, 2.0, 1.5, 0.0, 0.0, -1.5 } },
+};
+
+static const AVOption afireqsrc_options[] = {
+    { "preset","set equalizer preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=0}, -1, FF_ARRAY_ELEMS(eq_presets)-1, FLAGS, "preset" },
+    { "p",     "set equalizer preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=0}, -1, FF_ARRAY_ELEMS(eq_presets)-1, FLAGS, "preset" },
+    { "custom",            NULL, 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "preset" },
+    { eq_presets[ 0].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 0}, 0, 0, FLAGS, "preset" },
+    { eq_presets[ 1].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 1}, 0, 0, FLAGS, "preset" },
+    { eq_presets[ 2].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 2}, 0, 0, FLAGS, "preset" },
+    { eq_presets[ 3].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 3}, 0, 0, FLAGS, "preset" },
+    { eq_presets[ 4].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 4}, 0, 0, FLAGS, "preset" },
+    { eq_presets[ 5].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 5}, 0, 0, FLAGS, "preset" },
+    { eq_presets[ 6].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 6}, 0, 0, FLAGS, "preset" },
+    { eq_presets[ 7].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 7}, 0, 0, FLAGS, "preset" },
+    { eq_presets[ 8].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 8}, 0, 0, FLAGS, "preset" },
+    { eq_presets[ 9].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 9}, 0, 0, FLAGS, "preset" },
+    { eq_presets[10].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, FLAGS, "preset" },
+    { eq_presets[11].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, FLAGS, "preset" },
+    { eq_presets[12].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, FLAGS, "preset" },
+    { eq_presets[13].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=13}, 0, 0, FLAGS, "preset" },
+    { eq_presets[14].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=14}, 0, 0, FLAGS, "preset" },
+    { eq_presets[15].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=15}, 0, 0, FLAGS, "preset" },
+    { eq_presets[16].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=16}, 0, 0, FLAGS, "preset" },
+    { eq_presets[17].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=17}, 0, 0, FLAGS, "preset" },
+    { "gains", "set gain values per band", OFFSET(magnitude_str), AV_OPT_TYPE_STRING, {.str="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"}, 0, 0, FLAGS },
+    { "g",     "set gain values per band", OFFSET(magnitude_str), AV_OPT_TYPE_STRING, {.str="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"}, 0, 0, FLAGS },
+    { "bands", "set central frequency values per band", OFFSET(freq_points_str), AV_OPT_TYPE_STRING, {.str=DEFAULT_BANDS}, 0, 0, FLAGS },
+    { "b",     "set central frequency values per band", OFFSET(freq_points_str), AV_OPT_TYPE_STRING, {.str=DEFAULT_BANDS}, 0, 0, FLAGS },
+    { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=4096}, 16, UINT16_MAX, FLAGS },
+    { "t",    "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=4096}, 16, UINT16_MAX, FLAGS },
+    { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100},  1, INT_MAX,    FLAGS },
+    { "r",           "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100},  1, INT_MAX,    FLAGS },
+    { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
+    { "n",          "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
+    { "interp","set the interpolation", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "interp" },
+    { "i",     "set the interpolation", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "interp" },
+    { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "interp" },
+    { "cubic",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "interp" },
+    {NULL}
+};
+
+AVFILTER_DEFINE_CLASS(afireqsrc);
+
+static void eq_interp(AVComplexFloat *complexf,
+                      const float *freq,
+                      const float *magnitude,
+                      int m, int interp, int minterp,
+                      const float factor)
+{
+    complexf[0].re = magnitude[0];
+    complexf[0].im = 0;
+
+    for (int i = 1; i < minterp; i++) {
+        for (int j = 0; j < m; j++) {
+            const float x = factor * i;
+
+            if (x <= freq[j+1]) {
+                float g;
+
+                if (interp == 0) {
+                    const float d  = freq[j+1] - freq[j];
+                    const float d0 = x - freq[j];
+                    const float d1 = freq[j+1] - x;
+                    const float g0 = magnitude[j];
+                    const float g1 = magnitude[j+1];
+
+                    if (d0 && d1) {
+                        g = (d0 * g1 + d1 * g0) / d;
+                    } else if (d0) {
+                        g = g1;
+                    } else {
+                        g = g0;
+                    }
+                } else {
+                    if (x <= freq[j]) {
+                        g = magnitude[j];
+                    } else {
+                        float x1, x2, x3;
+                        float a, b, c, d;
+                        float m0, m1, m2, msum;
+                        const float unit = freq[j+1] - freq[j];
+
+                        m0 = j != 0 ? unit * (magnitude[j] - magnitude[j-1]) / (freq[j] - freq[j-1]) : 0;
+                        m1 = magnitude[j+1] - magnitude[j];
+                        m2 = j != minterp - 1 ? unit * (magnitude[j+2] - magnitude[j+1]) / (freq[j+2] - freq[j+1]) : 0;
+
+                        msum = fabsf(m0) + fabsf(m1);
+                        m0 = msum > 0.f ? (fabsf(m0) * m1 + fabsf(m1) * m0) / msum : 0.f;
+                        msum = fabsf(m1) + fabsf(m2);
+                        m1 = msum > 0.f ? (fabsf(m1) * m2 + fabsf(m2) * m1) / msum : 0.f;
+
+                        d = magnitude[j];
+                        c = m0;
+                        b = 3.f * magnitude[j+1] - m1 - 2.f * c - 3.f * d;
+                        a = magnitude[j+1] - b - c - d;
+
+                        x1 = (x - freq[j]) / unit;
+                        x2 = x1 * x1;
+                        x3 = x2 * x1;
+
+                        g = a * x3 + b * x2 + c * x1 + d;
+                    }
+                }
+
+                complexf[i].re = g;
+                complexf[i].im = 0;
+                complexf[minterp * 2 - i].re = g;
+                complexf[minterp * 2 - i].im = 0;
+
+                break;
+            }
+        }
+    }
+}
+
+static av_cold int config_eq_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AudioFIRSourceContext *s = ctx->priv;
+    int fft_size, middle, asize, ret;
+    float scale, factor;
+
+    s->nb_freq = s->nb_magnitude = 0;
+    if (s->preset < 0) {
+        ret = parse_string(s->freq_points_str, &s->freq, &s->nb_freq, &s->freq_size);
+        if (ret < 0)
+            return ret;
+
+        ret = parse_string(s->magnitude_str, &s->magnitude, &s->nb_magnitude, &s->magnitude_size);
+        if (ret < 0)
+            return ret;
+    } else {
+        char *freq_str;
+
+        s->nb_magnitude = FF_ARRAY_ELEMS(eq_presets[s->preset].gains);
+
+        freq_str = av_strdup(DEFAULT_BANDS);
+        if (!freq_str)
+            return AVERROR(ENOMEM);
+
+        ret = parse_string(freq_str, &s->freq, &s->nb_freq, &s->freq_size);
+        av_free(freq_str);
+        if (ret < 0)
+            return ret;
+
+        s->magnitude = av_calloc(s->nb_magnitude, sizeof(*s->magnitude));
+        if (!s->magnitude)
+            return AVERROR(ENOMEM);
+        memcpy(s->magnitude, eq_presets[s->preset].gains, sizeof(*s->magnitude) * s->nb_magnitude);
+    }
+
+    if (s->nb_freq != s->nb_magnitude || s->nb_freq < 2) {
+        av_log(ctx, AV_LOG_ERROR, "Number of bands and gains must be same and >= 2.\n");
+        return AVERROR(EINVAL);
+    }
+
+    s->freq[s->nb_freq] = outlink->sample_rate * 0.5f;
+    s->magnitude[s->nb_freq] = s->magnitude[s->nb_freq-1];
+
+    fft_size = s->nb_taps * 2;
+    factor = FFMIN(outlink->sample_rate * 0.5f, s->freq[s->nb_freq - 1]) / (float)fft_size;
+    asize = FFALIGN(fft_size, av_cpu_max_align());
+    s->complexf = av_calloc(asize * 2, sizeof(*s->complexf));
+    if (!s->complexf)
+        return AVERROR(ENOMEM);
+
+    scale = 1.f;
+    ret = av_tx_init(&s->tx_ctx, &s->tx_fn, AV_TX_FLOAT_FFT, 0, fft_size, &scale, 0);
+    if (ret < 0)
+        return ret;
+
+    s->taps = av_calloc(s->nb_taps, sizeof(*s->taps));
+    if (!s->taps)
+        return AVERROR(ENOMEM);
+
+    eq_interp(s->complexf, s->freq, s->magnitude, s->nb_freq, s->interp, s->nb_taps, factor);
+
+    for (int i = 0; i < asize * 2; i++)
+        s->complexf[i].re = ff_exp10f(s->complexf[i].re / 20.f);
+
+    s->tx_fn(s->tx_ctx, s->complexf + asize, s->complexf, sizeof(float));
+
+    middle = s->nb_taps / 2;
+    for (int i = 0; i < middle; i++) {
+        s->taps[middle - i] = (s->complexf[i + asize].re - s->complexf[i + asize].im) / fft_size;
+        s->taps[middle + i] = (s->complexf[i + asize].re - s->complexf[i + asize].im) / fft_size;
+    }
+
+    s->pts = 0;
+
+    return 0;
+}
+
+static const AVFilterPad afireqsrc_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_AUDIO,
+        .config_props  = config_eq_output,
+    },
+};
+
+const AVFilter ff_asrc_afireqsrc = {
+    .name          = "afireqsrc",
+    .description   = NULL_IF_CONFIG_SMALL("Generate a FIR equalizer coefficients audio stream."),
+    .uninit        = uninit,
+    .activate      = activate,
+    .priv_size     = sizeof(AudioFIRSourceContext),
+    .inputs        = NULL,
+    FILTER_OUTPUTS(afireqsrc_outputs),
+    FILTER_QUERY_FUNC(query_formats),
+    .priv_class    = &afireqsrc_class,
+};
-- 
2.37.2