summaryrefslogtreecommitdiff
path: root/libavfilter
diff options
context:
space:
mode:
authorPaul B Mahol <onemda@gmail.com>2015-09-17 09:38:23 +0000
committerPaul B Mahol <onemda@gmail.com>2015-09-22 22:07:36 +0200
commited4257de2d74ce5e5ae77ae96d58c58f1bbaeacd (patch)
tree0f2f957ccf4856bcbc1d1a6c1553f28a6f1dad1f /libavfilter
parent31623e9d1ea960035cee59839e016397a559dab3 (diff)
avfilter: add agate filter
Signed-off-by: Paul B Mahol <onemda@gmail.com>
Diffstat (limited to 'libavfilter')
-rw-r--r--libavfilter/Makefile1
-rw-r--r--libavfilter/af_agate.c237
-rw-r--r--libavfilter/af_sidechaincompress.c24
-rw-r--r--libavfilter/allfilters.c1
-rw-r--r--libavfilter/hermite.h40
-rw-r--r--libavfilter/version.h2
6 files changed, 281 insertions, 24 deletions
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 8db5da9bc6..be177db4e1 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -29,6 +29,7 @@ OBJS-$(CONFIG_AECHO_FILTER) += af_aecho.o
OBJS-$(CONFIG_AEVAL_FILTER) += aeval.o
OBJS-$(CONFIG_AFADE_FILTER) += af_afade.o
OBJS-$(CONFIG_AFORMAT_FILTER) += af_aformat.o
+OBJS-$(CONFIG_AGATE_FILTER) += af_agate.o
OBJS-$(CONFIG_AINTERLEAVE_FILTER) += f_interleave.o
OBJS-$(CONFIG_ALIMITER_FILTER) += af_alimiter.o
OBJS-$(CONFIG_ALLPASS_FILTER) += af_biquads.o
diff --git a/libavfilter/af_agate.c b/libavfilter/af_agate.c
new file mode 100644
index 0000000000..46ee226235
--- /dev/null
+++ b/libavfilter/af_agate.c
@@ -0,0 +1,237 @@
+/*
+ * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Damien Zammit
+ *
+ * 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/channel_layout.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "audio.h"
+#include "formats.h"
+#include "hermite.h"
+
+typedef struct AudioGateContext {
+ const AVClass *class;
+
+ double level_in;
+ double attack;
+ double release;
+ double threshold;
+ double ratio;
+ double knee;
+ double makeup;
+ double range;
+ int link;
+ int detection;
+
+ double thres;
+ double knee_start;
+ double lin_knee_stop;
+ double knee_stop;
+ double lin_slope;
+ double attack_coeff;
+ double release_coeff;
+} AudioGateContext;
+
+#define OFFSET(x) offsetof(AudioGateContext, x)
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption agate_options[] = {
+ { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
+ { "range", "set max gain reduction", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=0.06125}, 0, 1, A },
+ { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0, 1, A },
+ { "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 9000, A },
+ { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 9000, A },
+ { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A },
+ { "makeup", "set makeup gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 64, A },
+ { "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.828427125}, 1, 8, A },
+ { "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, "detection" },
+ { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "detection" },
+ { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "detection" },
+ { "link", "set link", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, "link" },
+ { "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "link" },
+ { "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "link" },
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(agate);
+
+static int query_formats(AVFilterContext *ctx)
+{
+ AVFilterFormats *formats = NULL;
+ AVFilterChannelLayouts *layouts;
+ int ret;
+
+ ff_add_format(&formats, AV_SAMPLE_FMT_DBL);
+ ret = ff_set_common_formats(ctx, formats);
+ if (ret < 0)
+ return 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_all_samplerates();
+ if (!formats)
+ return AVERROR(ENOMEM);
+
+ return ff_set_common_samplerates(ctx, formats);
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ AudioGateContext *s = ctx->priv;
+ double lin_threshold = s->threshold;
+ double lin_knee_sqrt = sqrt(s->knee);
+ double lin_knee_start;
+
+ if (s->detection)
+ lin_threshold *= lin_threshold;
+
+ s->attack_coeff = FFMIN(1., 1. / (s->attack * inlink->sample_rate / 4000.));
+ s->release_coeff = FFMIN(1., 1. / (s->release * inlink->sample_rate / 4000.));
+ s->lin_knee_stop = lin_threshold * lin_knee_sqrt;
+ lin_knee_start = lin_threshold / lin_knee_sqrt;
+ s->thres = log(lin_threshold);
+ s->knee_start = log(lin_knee_start);
+ s->knee_stop = log(s->lin_knee_stop);
+
+ return 0;
+}
+
+// A fake infinity value (because real infinity may break some hosts)
+#define FAKE_INFINITY (65536.0 * 65536.0)
+
+// Check for infinity (with appropriate-ish tolerance)
+#define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
+
+static double output_gain(double lin_slope, double ratio, double thres,
+ double knee, double knee_start, double knee_stop,
+ double lin_knee_stop, double range)
+{
+ if (lin_slope < lin_knee_stop) {
+ double slope = log(lin_slope);
+ double tratio = ratio;
+ double gain = 0.;
+ double delta = 0.;
+
+ if (IS_FAKE_INFINITY(ratio))
+ tratio = 1000.;
+ gain = (slope - thres) * tratio + thres;
+ delta = tratio;
+
+ if (knee > 1. && slope > knee_start) {
+ gain = hermite_interpolation(slope, knee_start, knee_stop, ((knee_start - thres) * tratio + thres), knee_stop, delta, 1.);
+ }
+ return FFMAX(range, exp(gain - slope));
+ }
+
+ return 1.;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+ AVFilterContext *ctx = inlink->dst;
+ AVFilterLink *outlink = ctx->outputs[0];
+ AudioGateContext *s = ctx->priv;
+ const double *src = (const double *)in->data[0];
+ const double makeup = s->makeup;
+ const double attack_coeff = s->attack_coeff;
+ const double release_coeff = s->release_coeff;
+ const double level_in = s->level_in;
+ AVFrame *out = NULL;
+ double *dst;
+ int n, c;
+
+ if (av_frame_is_writable(in)) {
+ out = in;
+ } else {
+ AVFrame *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 (n = 0; n < in->nb_samples; n++, src += inlink->channels, dst += inlink->channels) {
+ double abs_sample = FFABS(src[0]), gain = 1.0;
+
+ for (c = 0; c < inlink->channels; c++)
+ dst[c] = src[c] * level_in;
+
+ if (s->link == 1) {
+ for (c = 1; c < inlink->channels; c++)
+ abs_sample = FFMAX(FFABS(src[c]), abs_sample);
+ } else {
+ for (c = 1; c < inlink->channels; c++)
+ abs_sample += FFABS(src[c]);
+
+ abs_sample /= inlink->channels;
+ }
+
+ if (s->detection)
+ abs_sample *= abs_sample;
+
+ s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? attack_coeff : release_coeff);
+ if (s->lin_slope > 0.0)
+ gain = output_gain(s->lin_slope, s->ratio, s->thres,
+ s->knee, s->knee_start, s->knee_stop,
+ s->lin_knee_stop, s->range);
+
+ for (c = 0; c < inlink->channels; c++)
+ dst[c] *= gain * makeup;
+ }
+
+ if (out != in)
+ av_frame_free(&in);
+ return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .filter_frame = filter_frame,
+ .config_props = config_input,
+ },
+ { NULL }
+};
+
+static const AVFilterPad outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO,
+ },
+ { NULL }
+};
+
+AVFilter ff_af_agate = {
+ .name = "agate",
+ .description = NULL_IF_CONFIG_SMALL("Audio gate."),
+ .query_formats = query_formats,
+ .priv_size = sizeof(AudioGateContext),
+ .priv_class = &agate_class,
+ .inputs = inputs,
+ .outputs = outputs,
+};
diff --git a/libavfilter/af_sidechaincompress.c b/libavfilter/af_sidechaincompress.c
index 40ffca6dc3..b8a81fc0bb 100644
--- a/libavfilter/af_sidechaincompress.c
+++ b/libavfilter/af_sidechaincompress.c
@@ -32,6 +32,7 @@
#include "audio.h"
#include "avfilter.h"
#include "formats.h"
+#include "hermite.h"
#include "internal.h"
typedef struct SidechainCompressContext {
@@ -90,29 +91,6 @@ static av_cold int init(AVFilterContext *ctx)
return 0;
}
-static inline double hermite_interpolation(double x, double x0, double x1,
- double p0, double p1,
- double m0, double m1)
-{
- double width = x1 - x0;
- double t = (x - x0) / width;
- double t2, t3;
- double ct0, ct1, ct2, ct3;
-
- m0 *= width;
- m1 *= width;
-
- t2 = t*t;
- t3 = t2*t;
- ct0 = p0;
- ct1 = m0;
-
- ct2 = -3 * p0 - 2 * m0 + 3 * p1 - m1;
- ct3 = 2 * p0 + m0 - 2 * p1 + m1;
-
- return ct3 * t3 + ct2 * t2 + ct1 * t + ct0;
-}
-
// A fake infinity value (because real infinity may break some hosts)
#define FAKE_INFINITY (65536.0 * 65536.0)
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 292ecde289..7c93c1dcda 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -51,6 +51,7 @@ void avfilter_register_all(void)
REGISTER_FILTER(AEVAL, aeval, af);
REGISTER_FILTER(AFADE, afade, af);
REGISTER_FILTER(AFORMAT, aformat, af);
+ REGISTER_FILTER(AGATE, agate, af);
REGISTER_FILTER(AINTERLEAVE, ainterleave, af);
REGISTER_FILTER(ALIMITER, alimiter, af);
REGISTER_FILTER(ALLPASS, allpass, af);
diff --git a/libavfilter/hermite.h b/libavfilter/hermite.h
new file mode 100644
index 0000000000..6142391b2e
--- /dev/null
+++ b/libavfilter/hermite.h
@@ -0,0 +1,40 @@
+/*
+ * 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
+ */
+
+inline double hermite_interpolation(double x, double x0, double x1,
+ double p0, double p1,
+ double m0, double m1)
+{
+ double width = x1 - x0;
+ double t = (x - x0) / width;
+ double t2, t3;
+ double ct0, ct1, ct2, ct3;
+
+ m0 *= width;
+ m1 *= width;
+
+ t2 = t*t;
+ t3 = t2*t;
+ ct0 = p0;
+ ct1 = m0;
+
+ ct2 = -3 * p0 - 2 * m0 + 3 * p1 - m1;
+ ct3 = 2 * p0 + m0 - 2 * p1 + m1;
+
+ return ct3 * t3 + ct2 * t2 + ct1 * t + ct0;
+}
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 96503770e1..1426cf8100 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,7 +30,7 @@
#include "libavutil/version.h"
#define LIBAVFILTER_VERSION_MAJOR 6
-#define LIBAVFILTER_VERSION_MINOR 7
+#define LIBAVFILTER_VERSION_MINOR 8
#define LIBAVFILTER_VERSION_MICRO 100
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \