Browse code

avfilter: add agate filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2015/09/17 18:38:23
Showing 8 changed files
... ...
@@ -10,6 +10,7 @@ version <next>:
10 10
 - stereotools filter
11 11
 - rubberband filter
12 12
 - tremolo filter
13
+- agate filter
13 14
 
14 15
 
15 16
 version 2.8:
... ...
@@ -641,6 +641,57 @@ Force the output to either unsigned 8-bit or signed 16-bit stereo
641 641
 aformat=sample_fmts=u8|s16:channel_layouts=stereo
642 642
 @end example
643 643
 
644
+@section agate
645
+
646
+A gate is mainly used to reduce lower parts of a signal. This kind of signal
647
+processing reduces disturbing noise between useful signals.
648
+
649
+Gating is done by detecting the volume below a chosen level @var{threshold}
650
+and divide it by the factor set with @var{ratio}. The bottom of the noise
651
+floor is set via @var{range}. Because an exact manipulation of the signal
652
+would cause distortion of the waveform the reduction can be levelled over
653
+time. This is done by setting @var{attack} and @var{release}.
654
+
655
+@var{attack} determines how long the signal has to fall below the threshold
656
+before any reduction will occur and @var{release} sets the time the signal
657
+has to raise above the threshold to reduce the reduction again.
658
+Shorter signals than the chosen attack time will be left untouched.
659
+
660
+@table @option
661
+@item level_in
662
+Set input level before filtering.
663
+
664
+@item range
665
+Set the level of gain reduction when the signal is below the threshold.
666
+
667
+@item threshold
668
+If a signal rises above this level the gain reduction is released.
669
+
670
+@item ratio
671
+Set a ratio about which the signal is reduced.
672
+
673
+@item attack
674
+Amount of milliseconds the signal has to rise above the threshold before gain
675
+reduction stops.
676
+
677
+@item release
678
+Amount of milliseconds the signal has to fall below the threshold before the
679
+reduction is increased again.
680
+
681
+@item makeup
682
+Set amount of amplification of signal after processing.
683
+
684
+@item knee
685
+Curve the sharp knee around the threshold to enter gain reduction more softly.
686
+
687
+@item detection
688
+Choose if exact signal should be taken for detection or an RMS like one.
689
+
690
+@item link
691
+Choose if the average level between all channels or the louder channel affects
692
+the reduction.
693
+@end table
694
+
644 695
 @section alimiter
645 696
 
646 697
 The limiter prevents input signal from raising over a desired threshold.
... ...
@@ -29,6 +29,7 @@ OBJS-$(CONFIG_AECHO_FILTER)                  += af_aecho.o
29 29
 OBJS-$(CONFIG_AEVAL_FILTER)                  += aeval.o
30 30
 OBJS-$(CONFIG_AFADE_FILTER)                  += af_afade.o
31 31
 OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
32
+OBJS-$(CONFIG_AGATE_FILTER)                  += af_agate.o
32 33
 OBJS-$(CONFIG_AINTERLEAVE_FILTER)            += f_interleave.o
33 34
 OBJS-$(CONFIG_ALIMITER_FILTER)               += af_alimiter.o
34 35
 OBJS-$(CONFIG_ALLPASS_FILTER)                += af_biquads.o
35 36
new file mode 100644
... ...
@@ -0,0 +1,237 @@
0
+/*
1
+ * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Damien Zammit
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/channel_layout.h"
21
+#include "libavutil/opt.h"
22
+#include "avfilter.h"
23
+#include "audio.h"
24
+#include "formats.h"
25
+#include "hermite.h"
26
+
27
+typedef struct AudioGateContext {
28
+    const AVClass *class;
29
+
30
+    double level_in;
31
+    double attack;
32
+    double release;
33
+    double threshold;
34
+    double ratio;
35
+    double knee;
36
+    double makeup;
37
+    double range;
38
+    int link;
39
+    int detection;
40
+
41
+    double thres;
42
+    double knee_start;
43
+    double lin_knee_stop;
44
+    double knee_stop;
45
+    double lin_slope;
46
+    double attack_coeff;
47
+    double release_coeff;
48
+} AudioGateContext;
49
+
50
+#define OFFSET(x) offsetof(AudioGateContext, x)
51
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52
+
53
+static const AVOption agate_options[] = {
54
+    { "level_in",  "set input level",        OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1},           0.015625,   64, A },
55
+    { "range",     "set max gain reduction", OFFSET(range),     AV_OPT_TYPE_DOUBLE, {.dbl=0.06125},     0, 1, A },
56
+    { "threshold", "set threshold",          OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125},       0, 1, A },
57
+    { "ratio",     "set ratio",              OFFSET(ratio),     AV_OPT_TYPE_DOUBLE, {.dbl=2},           1,  9000, A },
58
+    { "attack",    "set attack",             OFFSET(attack),    AV_OPT_TYPE_DOUBLE, {.dbl=20},          0.01, 9000, A },
59
+    { "release",   "set release",            OFFSET(release),   AV_OPT_TYPE_DOUBLE, {.dbl=250},         0.01, 9000, A },
60
+    { "makeup",    "set makeup gain",        OFFSET(makeup),    AV_OPT_TYPE_DOUBLE, {.dbl=1},           1,   64, A },
61
+    { "knee",      "set knee",               OFFSET(knee),      AV_OPT_TYPE_DOUBLE, {.dbl=2.828427125}, 1,    8, A },
62
+    { "detection", "set detection",          OFFSET(detection), AV_OPT_TYPE_INT,    {.i64=0},           0,    1, A, "detection" },
63
+    {   "peak",    0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=0},           0,    0, A, "detection" },
64
+    {   "rms",     0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=1},           0,    0, A, "detection" },
65
+    { "link",      "set link",               OFFSET(link),      AV_OPT_TYPE_INT,    {.i64=0},           0,    1, A, "link" },
66
+    {   "average", 0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=0},           0,    0, A, "link" },
67
+    {   "maximum", 0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=1},           0,    0, A, "link" },
68
+    { NULL }
69
+};
70
+
71
+AVFILTER_DEFINE_CLASS(agate);
72
+
73
+static int query_formats(AVFilterContext *ctx)
74
+{
75
+    AVFilterFormats *formats = NULL;
76
+    AVFilterChannelLayouts *layouts;
77
+    int ret;
78
+
79
+    ff_add_format(&formats, AV_SAMPLE_FMT_DBL);
80
+    ret = ff_set_common_formats(ctx, formats);
81
+    if (ret < 0)
82
+        return ret;
83
+
84
+    layouts = ff_all_channel_counts();
85
+    if (!layouts)
86
+        return AVERROR(ENOMEM);
87
+    ret = ff_set_common_channel_layouts(ctx, layouts);
88
+    if (ret < 0)
89
+        return ret;
90
+
91
+    formats = ff_all_samplerates();
92
+    if (!formats)
93
+        return AVERROR(ENOMEM);
94
+
95
+    return ff_set_common_samplerates(ctx, formats);
96
+}
97
+
98
+static int config_input(AVFilterLink *inlink)
99
+{
100
+    AVFilterContext *ctx = inlink->dst;
101
+    AudioGateContext *s = ctx->priv;
102
+    double lin_threshold = s->threshold;
103
+    double lin_knee_sqrt = sqrt(s->knee);
104
+    double lin_knee_start;
105
+
106
+    if (s->detection)
107
+        lin_threshold *= lin_threshold;
108
+
109
+    s->attack_coeff  = FFMIN(1., 1. / (s->attack * inlink->sample_rate / 4000.));
110
+    s->release_coeff = FFMIN(1., 1. / (s->release * inlink->sample_rate / 4000.));
111
+    s->lin_knee_stop = lin_threshold * lin_knee_sqrt;
112
+    lin_knee_start = lin_threshold / lin_knee_sqrt;
113
+    s->thres = log(lin_threshold);
114
+    s->knee_start = log(lin_knee_start);
115
+    s->knee_stop = log(s->lin_knee_stop);
116
+
117
+    return 0;
118
+}
119
+
120
+// A fake infinity value (because real infinity may break some hosts)
121
+#define FAKE_INFINITY (65536.0 * 65536.0)
122
+
123
+// Check for infinity (with appropriate-ish tolerance)
124
+#define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
125
+
126
+static double output_gain(double lin_slope, double ratio, double thres,
127
+                          double knee, double knee_start, double knee_stop,
128
+                          double lin_knee_stop, double range)
129
+{
130
+    if (lin_slope < lin_knee_stop) {
131
+        double slope = log(lin_slope);
132
+        double tratio = ratio;
133
+        double gain = 0.;
134
+        double delta = 0.;
135
+
136
+        if (IS_FAKE_INFINITY(ratio))
137
+            tratio = 1000.;
138
+        gain = (slope - thres) * tratio + thres;
139
+        delta = tratio;
140
+
141
+        if (knee > 1. && slope > knee_start) {
142
+            gain = hermite_interpolation(slope, knee_start, knee_stop, ((knee_start - thres) * tratio  + thres), knee_stop, delta, 1.);
143
+        }
144
+        return FFMAX(range, exp(gain - slope));
145
+    }
146
+
147
+    return 1.;
148
+}
149
+
150
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
151
+{
152
+    AVFilterContext *ctx = inlink->dst;
153
+    AVFilterLink *outlink = ctx->outputs[0];
154
+    AudioGateContext *s = ctx->priv;
155
+    const double *src = (const double *)in->data[0];
156
+    const double makeup = s->makeup;
157
+    const double attack_coeff = s->attack_coeff;
158
+    const double release_coeff = s->release_coeff;
159
+    const double level_in = s->level_in;
160
+    AVFrame *out = NULL;
161
+    double *dst;
162
+    int n, c;
163
+
164
+    if (av_frame_is_writable(in)) {
165
+        out = in;
166
+    } else {
167
+        AVFrame *out = ff_get_audio_buffer(inlink, in->nb_samples);
168
+        if (!out) {
169
+            av_frame_free(&in);
170
+            return AVERROR(ENOMEM);
171
+        }
172
+        av_frame_copy_props(out, in);
173
+    }
174
+    dst = (double *)out->data[0];
175
+
176
+    for (n = 0; n < in->nb_samples; n++, src += inlink->channels, dst += inlink->channels) {
177
+        double abs_sample = FFABS(src[0]), gain = 1.0;
178
+
179
+        for (c = 0; c < inlink->channels; c++)
180
+            dst[c] = src[c] * level_in;
181
+
182
+        if (s->link == 1) {
183
+            for (c = 1; c < inlink->channels; c++)
184
+                abs_sample = FFMAX(FFABS(src[c]), abs_sample);
185
+        } else {
186
+            for (c = 1; c < inlink->channels; c++)
187
+                abs_sample += FFABS(src[c]);
188
+
189
+            abs_sample /= inlink->channels;
190
+        }
191
+
192
+        if (s->detection)
193
+            abs_sample *= abs_sample;
194
+
195
+        s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? attack_coeff : release_coeff);
196
+        if (s->lin_slope > 0.0)
197
+            gain = output_gain(s->lin_slope, s->ratio, s->thres,
198
+                               s->knee, s->knee_start, s->knee_stop,
199
+                               s->lin_knee_stop, s->range);
200
+
201
+        for (c = 0; c < inlink->channels; c++)
202
+            dst[c] *= gain * makeup;
203
+    }
204
+
205
+    if (out != in)
206
+        av_frame_free(&in);
207
+    return ff_filter_frame(outlink, out);
208
+}
209
+
210
+static const AVFilterPad inputs[] = {
211
+    {
212
+        .name         = "default",
213
+        .type         = AVMEDIA_TYPE_AUDIO,
214
+        .filter_frame = filter_frame,
215
+        .config_props = config_input,
216
+    },
217
+    { NULL }
218
+};
219
+
220
+static const AVFilterPad outputs[] = {
221
+    {
222
+        .name = "default",
223
+        .type = AVMEDIA_TYPE_AUDIO,
224
+    },
225
+    { NULL }
226
+};
227
+
228
+AVFilter ff_af_agate = {
229
+    .name           = "agate",
230
+    .description    = NULL_IF_CONFIG_SMALL("Audio gate."),
231
+    .query_formats  = query_formats,
232
+    .priv_size      = sizeof(AudioGateContext),
233
+    .priv_class     = &agate_class,
234
+    .inputs         = inputs,
235
+    .outputs        = outputs,
236
+};
... ...
@@ -32,6 +32,7 @@
32 32
 #include "audio.h"
33 33
 #include "avfilter.h"
34 34
 #include "formats.h"
35
+#include "hermite.h"
35 36
 #include "internal.h"
36 37
 
37 38
 typedef struct SidechainCompressContext {
... ...
@@ -90,29 +91,6 @@ static av_cold int init(AVFilterContext *ctx)
90 90
     return 0;
91 91
 }
92 92
 
93
-static inline double hermite_interpolation(double x, double x0, double x1,
94
-                                           double p0, double p1,
95
-                                           double m0, double m1)
96
-{
97
-    double width = x1 - x0;
98
-    double t = (x - x0) / width;
99
-    double t2, t3;
100
-    double ct0, ct1, ct2, ct3;
101
-
102
-    m0 *= width;
103
-    m1 *= width;
104
-
105
-    t2 = t*t;
106
-    t3 = t2*t;
107
-    ct0 = p0;
108
-    ct1 = m0;
109
-
110
-    ct2 = -3 * p0 - 2 * m0 + 3 * p1 - m1;
111
-    ct3 = 2 * p0 + m0  - 2 * p1 + m1;
112
-
113
-    return ct3 * t3 + ct2 * t2 + ct1 * t + ct0;
114
-}
115
-
116 93
 // A fake infinity value (because real infinity may break some hosts)
117 94
 #define FAKE_INFINITY (65536.0 * 65536.0)
118 95
 
... ...
@@ -51,6 +51,7 @@ void avfilter_register_all(void)
51 51
     REGISTER_FILTER(AEVAL,          aeval,          af);
52 52
     REGISTER_FILTER(AFADE,          afade,          af);
53 53
     REGISTER_FILTER(AFORMAT,        aformat,        af);
54
+    REGISTER_FILTER(AGATE,          agate,          af);
54 55
     REGISTER_FILTER(AINTERLEAVE,    ainterleave,    af);
55 56
     REGISTER_FILTER(ALIMITER,       alimiter,       af);
56 57
     REGISTER_FILTER(ALLPASS,        allpass,        af);
57 58
new file mode 100644
... ...
@@ -0,0 +1,40 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+inline double hermite_interpolation(double x, double x0, double x1,
19
+                                    double p0, double p1,
20
+                                    double m0, double m1)
21
+{
22
+    double width = x1 - x0;
23
+    double t = (x - x0) / width;
24
+    double t2, t3;
25
+    double ct0, ct1, ct2, ct3;
26
+
27
+    m0 *= width;
28
+    m1 *= width;
29
+
30
+    t2 = t*t;
31
+    t3 = t2*t;
32
+    ct0 = p0;
33
+    ct1 = m0;
34
+
35
+    ct2 = -3 * p0 - 2 * m0 + 3 * p1 - m1;
36
+    ct3 = 2 * p0 + m0  - 2 * p1 + m1;
37
+
38
+    return ct3 * t3 + ct2 * t2 + ct1 * t + ct0;
39
+}
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR   7
33
+#define LIBAVFILTER_VERSION_MINOR   8
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \