[FFmpeg-cvslog] avfilter: add aexciter audio filter

Paul B Mahol git at videolan.org
Wed Feb 10 20:26:51 EET 2021


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Sat Feb  6 17:31:00 2021 +0100| [579e4e57a2c4ab8d98bf2e18413dc73ce02353f9] | committer: Paul B Mahol

avfilter: add aexciter audio filter

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=579e4e57a2c4ab8d98bf2e18413dc73ce02353f9
---

 Changelog                 |   1 +
 doc/filters.texi          |  56 ++++++++
 libavfilter/Makefile      |   1 +
 libavfilter/af_aexciter.c | 317 ++++++++++++++++++++++++++++++++++++++++++++++
 libavfilter/allfilters.c  |   1 +
 libavfilter/version.h     |   2 +-
 6 files changed, 377 insertions(+), 1 deletion(-)

diff --git a/Changelog b/Changelog
index c1a7385c81..e8e084998b 100644
--- a/Changelog
+++ b/Changelog
@@ -69,6 +69,7 @@ version <next>:
 - xbm_pipe demuxer
 - colorize filter
 - CRI parser
+- aexciter audio filter
 
 
 version 4.3:
diff --git a/doc/filters.texi b/doc/filters.texi
index dc42927f6a..85052aa18d 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -1003,6 +1003,62 @@ aeval=val(0)|-val(1)
 @end example
 @end itemize
 
+ at section aexciter
+
+An exciter is used to produce high sound that is not present in the
+original signal. This is done by creating harmonic distortions of the
+signal which are restricted in range and added to the original signal.
+An Exciter raises the upper end of an audio signal without simply raising
+the higher frequencies like an equalizer would do to create a more
+"crisp" or "brilliant" sound.
+
+The filter accepts the following options:
+
+ at table @option
+ at item level_in
+Set input level prior processing of signal.
+Allowed range is from 0 to 64.
+Default value is 1.
+
+ at item level_out
+Set output level after processing of signal.
+Allowed range is from 0 to 64.
+Default value is 1.
+
+ at item amount
+Set the amount of harmonics added to original signal.
+Allowed range is from 0 to 64.
+Default value is 1.
+
+ at item drive
+Set the amount of newly created harmonics.
+Allowed range is from 0.1 to 10.
+Default value is 8.5.
+
+ at item blend
+Set the octave of newly created harmonics.
+Allowed range is from -10 to 10.
+Default value is 0.
+
+ at item freq
+Set the lower frequency limit of producing harmonics in Hz.
+Allowed range is from 2000 to 12000 Hz.
+Default is 7500 Hz.
+
+ at item ceil
+Set the upper frequency limit of producing harmonics.
+Allowed range is from 9999 to 20000 Hz.
+If value is lower than 10000 Hz no limit is applied.
+
+ at item listen
+Mute the original signal and output only added harmonics.
+By default is disabled.
+ at end table
+
+ at subsection Commands
+
+This filter supports the all above options as @ref{commands}.
+
 @anchor{afade}
 @section afade
 
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 3ec28df411..607a09287a 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -46,6 +46,7 @@ OBJS-$(CONFIG_ADERIVATIVE_FILTER)            += af_aderivative.o
 OBJS-$(CONFIG_AECHO_FILTER)                  += af_aecho.o
 OBJS-$(CONFIG_AEMPHASIS_FILTER)              += af_aemphasis.o
 OBJS-$(CONFIG_AEVAL_FILTER)                  += aeval.o
+OBJS-$(CONFIG_AEXCITER_FILTER)               += af_aexciter.o
 OBJS-$(CONFIG_AFADE_FILTER)                  += af_afade.o
 OBJS-$(CONFIG_AFFTDN_FILTER)                 += af_afftdn.o
 OBJS-$(CONFIG_AFFTFILT_FILTER)               += af_afftfilt.o
diff --git a/libavfilter/af_aexciter.c b/libavfilter/af_aexciter.c
new file mode 100644
index 0000000000..f09c99984c
--- /dev/null
+++ b/libavfilter/af_aexciter.c
@@ -0,0 +1,317 @@
+/*
+ * Copyright (c) Markus Schmidt and Christian Holschuh
+ *
+ * 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 "avfilter.h"
+#include "internal.h"
+#include "audio.h"
+
+typedef struct ChannelParams {
+    double blend_old, drive_old;
+    double rdrive, rbdr, kpa, kpb, kna, knb, ap,
+           an, imr, kc, srct, sq, pwrq;
+    double prev_med, prev_out;
+
+    double hp[5], lp[5];
+    double hw[4][2], lw[2][2];
+} ChannelParams;
+
+typedef struct AExciterContext {
+    const AVClass *class;
+
+    double level_in;
+    double level_out;
+    double amount;
+    double drive;
+    double blend;
+    double freq;
+    double ceil;
+    int listen;
+
+    ChannelParams *cp;
+} AExciterContext;
+
+#define OFFSET(x) offsetof(AExciterContext, x)
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
+
+static const AVOption aexciter_options[] = {
+    { "level_in",  "set level in",    OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1},           0, 64, A },
+    { "level_out", "set level out",   OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1},           0, 64, A },
+    { "amount", "set amount",         OFFSET(amount),    AV_OPT_TYPE_DOUBLE, {.dbl=1},           0, 64, A },
+    { "drive", "set harmonics",       OFFSET(drive),     AV_OPT_TYPE_DOUBLE, {.dbl=8.5},       0.1, 10, A },
+    { "blend", "set blend harmonics", OFFSET(blend),     AV_OPT_TYPE_DOUBLE, {.dbl=0},         -10, 10, A },
+    { "freq", "set scope",            OFFSET(freq),      AV_OPT_TYPE_DOUBLE, {.dbl=7500},  2000, 12000, A },
+    { "ceil", "set ceiling",          OFFSET(ceil),      AV_OPT_TYPE_DOUBLE, {.dbl=9999},  9999, 20000, A },
+    { "listen", "enable listen mode", OFFSET(listen),    AV_OPT_TYPE_BOOL,   {.i64=0},        0,     1, A },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(aexciter);
+
+static inline double M(double x)
+{
+    return (fabs(x) > 0.00000001) ? x : 0.0;
+}
+
+static inline double D(double x)
+{
+    x = fabs(x);
+
+    return (x > 0.00000001) ? sqrt(x) : 0.0;
+}
+
+static void set_params(ChannelParams *p,
+                       double blend, double drive,
+                       double srate, double freq,
+                       double ceil)
+{
+    double a0, a1, a2, b0, b1, b2, w0, alpha;
+
+    p->rdrive = 12.0 / drive;
+    p->rbdr = p->rdrive / (10.5 - blend) * 780.0 / 33.0;
+    p->kpa = D(2.0 * (p->rdrive*p->rdrive) - 1.0) + 1.0;
+    p->kpb = (2.0 - p->kpa) / 2.0;
+    p->ap = ((p->rdrive*p->rdrive) - p->kpa + 1.0) / 2.0;
+    p->kc = p->kpa / D(2.0 * D(2.0 * (p->rdrive*p->rdrive) - 1.0) - 2.0 * p->rdrive*p->rdrive);
+
+    p->srct = (0.1 * srate) / (0.1 * srate + 1.0);
+    p->sq = p->kc*p->kc + 1.0;
+    p->knb = -1.0 * p->rbdr / D(p->sq);
+    p->kna = 2.0 * p->kc * p->rbdr / D(p->sq);
+    p->an = p->rbdr*p->rbdr / p->sq;
+    p->imr = 2.0 * p->knb + D(2.0 * p->kna + 4.0 * p->an - 1.0);
+    p->pwrq = 2.0 / (p->imr + 1.0);
+
+    w0 = 2 * M_PI * freq / srate;
+    alpha = sin(w0) / (2. * 0.707);
+    a0 =   1 + alpha;
+    a1 =  -2 * cos(w0);
+    a2 =   1 - alpha;
+    b0 =  (1 + cos(w0)) / 2;
+    b1 = -(1 + cos(w0));
+    b2 =  (1 + cos(w0)) / 2;
+
+    p->hp[0] =-a1 / a0;
+    p->hp[1] =-a2 / a0;
+    p->hp[2] = b0 / a0;
+    p->hp[3] = b1 / a0;
+    p->hp[4] = b2 / a0;
+
+    w0 = 2 * M_PI * ceil / srate;
+    alpha = sin(w0) / (2. * 0.707);
+    a0 =  1 + alpha;
+    a1 = -2 * cos(w0);
+    a2 =  1 - alpha;
+    b0 = (1 - cos(w0)) / 2;
+    b1 =  1 - cos(w0);
+    b2 = (1 - cos(w0)) / 2;
+
+    p->lp[0] =-a1 / a0;
+    p->lp[1] =-a2 / a0;
+    p->lp[2] = b0 / a0;
+    p->lp[3] = b1 / a0;
+    p->lp[4] = b2 / a0;
+}
+
+static double bprocess(double in, const double *const c,
+                       double *w1, double *w2)
+{
+    double out = c[2] * in + *w1;
+
+    *w1 = c[3] * in + *w2 + c[0] * out;
+    *w2 = c[4] * in + c[1] * out;
+
+    return out;
+}
+
+static double distortion_process(AExciterContext *s, ChannelParams *p, double in)
+{
+    double proc = in, med;
+
+    proc = bprocess(proc, p->hp, &p->hw[0][0], &p->hw[0][1]);
+    proc = bprocess(proc, p->hp, &p->hw[1][0], &p->hw[1][1]);
+
+    if (proc >= 0.0) {
+        med = (D(p->ap + proc * (p->kpa - proc)) + p->kpb) * p->pwrq;
+    } else {
+        med = (D(p->an - proc * (p->kna + proc)) + p->knb) * p->pwrq * -1.0;
+    }
+
+    proc = p->srct * (med - p->prev_med + p->prev_out);
+    p->prev_med = M(med);
+    p->prev_out = M(proc);
+
+    proc = bprocess(proc, p->hp, &p->hw[2][0], &p->hw[2][1]);
+    proc = bprocess(proc, p->hp, &p->hw[3][0], &p->hw[3][1]);
+
+    if (s->ceil >= 10000.) {
+        proc = bprocess(proc, p->lp, &p->lw[0][0], &p->lw[0][1]);
+        proc = bprocess(proc, p->lp, &p->lw[1][0], &p->lw[1][1]);
+    }
+
+    return proc;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AExciterContext *s = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    AVFrame *out;
+    const double *src = (const double *)in->data[0];
+    const double level_in = s->level_in;
+    const double level_out = s->level_out;
+    const double amount = s->amount;
+    const double listen = 1.0 - s->listen;
+    double *dst;
+
+    if (av_frame_is_writable(in)) {
+        out = in;
+    } else {
+        out = ff_get_audio_buffer(inlink, in->nb_samples);
+        if (!out) {
+            av_frame_free(&in);
+            return AVERROR(ENOMEM);
+        }
+        av_frame_copy_props(out, in);
+    }
+
+    dst = (double *)out->data[0];
+    for (int n = 0; n < in->nb_samples; n++) {
+        for (int c = 0; c < inlink->channels; c++) {
+            double sample = src[c] * level_in;
+
+            sample = distortion_process(s, &s->cp[c], sample);
+            sample = sample * amount + listen * src[c];
+
+            sample *= level_out;
+            if (ctx->is_disabled)
+                dst[c] = src[c];
+            else
+                dst[c] = sample;
+        }
+
+        src += inlink->channels;
+        dst += inlink->channels;
+    }
+
+    if (in != out)
+        av_frame_free(&in);
+
+    return ff_filter_frame(outlink, out);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    AVFilterFormats *formats;
+    AVFilterChannelLayouts *layouts;
+    static const enum AVSampleFormat sample_fmts[] = {
+        AV_SAMPLE_FMT_DBL,
+        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 av_cold void uninit(AVFilterContext *ctx)
+{
+    AExciterContext *s = ctx->priv;
+
+    av_freep(&s->cp);
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AExciterContext *s = ctx->priv;
+
+    if (!s->cp)
+        s->cp = av_calloc(inlink->channels, sizeof(*s->cp));
+    if (!s->cp)
+        return AVERROR(ENOMEM);
+
+    for (int i = 0; i < inlink->channels; i++)
+        set_params(&s->cp[i], s->blend, s->drive, inlink->sample_rate,
+                   s->freq, s->ceil);
+
+    return 0;
+}
+
+static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
+                           char *res, int res_len, int flags)
+{
+    AVFilterLink *inlink = ctx->inputs[0];
+    int ret;
+
+    ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
+    if (ret < 0)
+        return ret;
+
+    return config_input(inlink);
+}
+
+static const AVFilterPad avfilter_af_aexciter_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_AUDIO,
+        .config_props = config_input,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad avfilter_af_aexciter_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_AUDIO,
+    },
+    { NULL }
+};
+
+AVFilter ff_af_aexciter = {
+    .name          = "aexciter",
+    .description   = NULL_IF_CONFIG_SMALL("Enhance high frequency part of audio."),
+    .priv_size     = sizeof(AExciterContext),
+    .priv_class    = &aexciter_class,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = avfilter_af_aexciter_inputs,
+    .outputs       = avfilter_af_aexciter_outputs,
+    .process_command = process_command,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
+};
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 73d859ce5e..a00008b44a 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -39,6 +39,7 @@ extern AVFilter ff_af_aderivative;
 extern AVFilter ff_af_aecho;
 extern AVFilter ff_af_aemphasis;
 extern AVFilter ff_af_aeval;
+extern AVFilter ff_af_aexciter;
 extern AVFilter ff_af_afade;
 extern AVFilter ff_af_afftdn;
 extern AVFilter ff_af_afftfilt;
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 43924194ae..d74eeb3f7b 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/version.h"
 
 #define LIBAVFILTER_VERSION_MAJOR   7
-#define LIBAVFILTER_VERSION_MINOR 103
+#define LIBAVFILTER_VERSION_MINOR 104
 #define LIBAVFILTER_VERSION_MICRO 100
 
 



More information about the ffmpeg-cvslog mailing list