aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter
diff options
context:
space:
mode:
authorPaul B Mahol <onemda@gmail.com>2021-11-25 19:30:32 +0100
committerPaul B Mahol <onemda@gmail.com>2021-12-02 09:39:27 +0100
commitfc9a6866880661b8418c728e6a54515157481875 (patch)
treeddfc331132a3fd53914d961aeff0034412a9bf40 /libavfilter
parent11b11577fe801b6afb7b4f46a5e5df853f7c1557 (diff)
downloadffmpeg-fc9a6866880661b8418c728e6a54515157481875.tar.gz
avfilter: add audio dynamic smooth filter
Diffstat (limited to 'libavfilter')
-rw-r--r--libavfilter/Makefile1
-rw-r--r--libavfilter/af_adynamicsmooth.c142
-rw-r--r--libavfilter/allfilters.c1
3 files changed, 144 insertions, 0 deletions
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 551d13aadc..c8082c4a2f 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -44,6 +44,7 @@ OBJS-$(CONFIG_ADECORRELATE_FILTER) += af_adecorrelate.o
OBJS-$(CONFIG_ADELAY_FILTER) += af_adelay.o
OBJS-$(CONFIG_ADENORM_FILTER) += af_adenorm.o
OBJS-$(CONFIG_ADERIVATIVE_FILTER) += af_aderivative.o
+OBJS-$(CONFIG_ADYNAMICSMOOTH_FILTER) += af_adynamicsmooth.o
OBJS-$(CONFIG_AECHO_FILTER) += af_aecho.o
OBJS-$(CONFIG_AEMPHASIS_FILTER) += af_aemphasis.o
OBJS-$(CONFIG_AEVAL_FILTER) += aeval.o
diff --git a/libavfilter/af_adynamicsmooth.c b/libavfilter/af_adynamicsmooth.c
new file mode 100644
index 0000000000..4e00fecc6a
--- /dev/null
+++ b/libavfilter/af_adynamicsmooth.c
@@ -0,0 +1,142 @@
+/*
+ * 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/ffmath.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "audio.h"
+#include "formats.h"
+
+typedef struct AudioDynamicSmoothContext {
+ const AVClass *class;
+
+ double sensitivity;
+ double basefreq;
+
+ AVFrame *coeffs;
+} AudioDynamicSmoothContext;
+
+static int config_input(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ AudioDynamicSmoothContext *s = ctx->priv;
+
+ s->coeffs = ff_get_audio_buffer(inlink, 3);
+ if (!s->coeffs)
+ return AVERROR(ENOMEM);
+
+ return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+ AVFilterContext *ctx = inlink->dst;
+ AVFilterLink *outlink = ctx->outputs[0];
+ AudioDynamicSmoothContext *s = ctx->priv;
+ const double sensitivity = s->sensitivity;
+ const double wc = s->basefreq / in->sample_rate;
+ AVFrame *out;
+
+ if (av_frame_is_writable(in)) {
+ out = in;
+ } else {
+ out = ff_get_audio_buffer(outlink, in->nb_samples);
+ if (!out) {
+ av_frame_free(&in);
+ return AVERROR(ENOMEM);
+ }
+ av_frame_copy_props(out, in);
+ }
+
+ for (int ch = 0; ch < out->channels; ch++) {
+ const double *src = (const double *)in->extended_data[ch];
+ double *dst = (double *)out->extended_data[ch];
+ double *coeffs = (double *)s->coeffs->extended_data[ch];
+ double low1 = coeffs[0];
+ double low2 = coeffs[1];
+ double inz = coeffs[2];
+
+ for (int n = 0; n < out->nb_samples; n++) {
+ double low1z = low1;
+ double low2z = low2;
+ double bandz = low2z - low1z;
+ double wd = wc + sensitivity * fabs(bandz);
+ double g = fmin(1., wd * (5.9948827 + wd * (-11.969296 + wd * 15.959062)));
+
+ low1 = low1z + g * (0.5 * (src[n] + inz) - low1z);
+ low2 = low2z + g * (0.5 * (low1 + low1z) - low2z);
+ inz = src[n];
+ dst[n] = ctx->is_disabled ? src[n] : low2;
+ }
+
+ coeffs[0] = low1;
+ coeffs[1] = low2;
+ coeffs[2] = inz;
+ }
+
+ if (out != in)
+ av_frame_free(&in);
+ return ff_filter_frame(outlink, out);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ AudioDynamicSmoothContext *s = ctx->priv;
+
+ av_frame_free(&s->coeffs);
+}
+
+#define OFFSET(x) offsetof(AudioDynamicSmoothContext, x)
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
+
+static const AVOption adynamicsmooth_options[] = {
+ { "sensitivity", "set smooth sensitivity", OFFSET(sensitivity), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 1000000, FLAGS },
+ { "basefreq", "set base frequency", OFFSET(basefreq), AV_OPT_TYPE_DOUBLE, {.dbl=22050}, 2, 1000000, FLAGS },
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(adynamicsmooth);
+
+static const AVFilterPad inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .filter_frame = filter_frame,
+ .config_props = config_input,
+ },
+};
+
+static const AVFilterPad outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO,
+ },
+};
+
+const AVFilter ff_af_adynamicsmooth = {
+ .name = "adynamicsmooth",
+ .description = NULL_IF_CONFIG_SMALL("Apply Dynamic Smoothing of input audio."),
+ .priv_size = sizeof(AudioDynamicSmoothContext),
+ .priv_class = &adynamicsmooth_class,
+ .uninit = uninit,
+ FILTER_INPUTS(inputs),
+ FILTER_OUTPUTS(outputs),
+ FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),
+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
+ .process_command = ff_filter_process_command,
+};
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 00c36c3f63..c5c0e9b28b 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -37,6 +37,7 @@ extern const AVFilter ff_af_adecorrelate;
extern const AVFilter ff_af_adelay;
extern const AVFilter ff_af_adenorm;
extern const AVFilter ff_af_aderivative;
+extern const AVFilter ff_af_adynamicsmooth;
extern const AVFilter ff_af_aecho;
extern const AVFilter ff_af_aemphasis;
extern const AVFilter ff_af_aeval;