Browse code

avfilter: add superequalizer filter

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

Paul B Mahol authored on 2017/06/16 21:07:01
Showing 6 changed files
... ...
@@ -20,6 +20,7 @@ version <next>:
20 20
 - sofalizer filter switched to libmysofa
21 21
 - Gremlin Digital Video demuxer and decoder
22 22
 - headphone audio filter
23
+- superequalizer audio filter
23 24
 
24 25
 version 3.3:
25 26
 - CrystalHD decoder moved to new decode API
... ...
@@ -3835,6 +3835,49 @@ channels. Default is 0.3.
3835 3835
 Set level of input signal of original channel. Default is 0.8.
3836 3836
 @end table
3837 3837
 
3838
+@section superequalizer
3839
+Apply 18th band equalizer.
3840
+
3841
+The filter accpets the following options:
3842
+@table @option
3843
+@item 1b
3844
+Set 65Hz band gain.
3845
+@item 2b
3846
+Set 92Hz band gain.
3847
+@item 3b
3848
+Set 131Hz band gain.
3849
+@item 4b
3850
+Set 185Hz band gain.
3851
+@item 5b
3852
+Set 262Hz band gain.
3853
+@item 6b
3854
+Set 370Hz band gain.
3855
+@item 7b
3856
+Set 523Hz band gain.
3857
+@item 8b
3858
+Set 740Hz band gain.
3859
+@item 9b
3860
+Set 1047Hz band gain.
3861
+@item 10b
3862
+Set 1480Hz band gain.
3863
+@item 11b
3864
+Set 2093Hz band gain.
3865
+@item 12b
3866
+Set 2960Hz band gain.
3867
+@item 13b
3868
+Set 4186Hz band gain.
3869
+@item 14b
3870
+Set 5920Hz band gain.
3871
+@item 15b
3872
+Set 8372Hz band gain.
3873
+@item 16b
3874
+Set 11840Hz band gain.
3875
+@item 17b
3876
+Set 16744Hz band gain.
3877
+@item 18b
3878
+Set 20000Hz band gain.
3879
+@end table
3880
+
3838 3881
 @section surround
3839 3882
 Apply audio surround upmix filter.
3840 3883
 
... ...
@@ -109,6 +109,7 @@ OBJS-$(CONFIG_SILENCEREMOVE_FILTER)          += af_silenceremove.o
109 109
 OBJS-$(CONFIG_SOFALIZER_FILTER)              += af_sofalizer.o
110 110
 OBJS-$(CONFIG_STEREOTOOLS_FILTER)            += af_stereotools.o
111 111
 OBJS-$(CONFIG_STEREOWIDEN_FILTER)            += af_stereowiden.o
112
+OBJS-$(CONFIG_SUPEREQUALIZER_FILTER)         += af_superequalizer.o
112 113
 OBJS-$(CONFIG_SURROUND_FILTER)               += af_surround.o
113 114
 OBJS-$(CONFIG_TREBLE_FILTER)                 += af_biquads.o
114 115
 OBJS-$(CONFIG_TREMOLO_FILTER)                += af_tremolo.o
115 116
new file mode 100644
... ...
@@ -0,0 +1,368 @@
0
+/*
1
+ * Copyright (c) 2002 Naoki Shibata
2
+ * Copyright (c) 2017 Paul B Mahol
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/opt.h"
22
+
23
+#include "libavcodec/avfft.h"
24
+
25
+#include "audio.h"
26
+#include "avfilter.h"
27
+#include "internal.h"
28
+
29
+#define NBANDS 17
30
+#define M 15
31
+
32
+typedef struct EqParameter {
33
+    float lower, upper, gain;
34
+} EqParameter;
35
+
36
+typedef struct SuperEqualizerContext {
37
+    const AVClass *class;
38
+
39
+    EqParameter params[NBANDS + 1];
40
+
41
+    float gains[NBANDS + 1];
42
+
43
+    float fact[M + 1];
44
+    float aa;
45
+    float iza;
46
+    float *ires, *irest;
47
+    float *fsamples;
48
+    int winlen, tabsize;
49
+
50
+    AVFrame *in, *out;
51
+    RDFTContext *rdft, *irdft;
52
+} SuperEqualizerContext;
53
+
54
+static const float bands[] = {
55
+    65.406392, 92.498606, 130.81278, 184.99721, 261.62557, 369.99442, 523.25113, 739.9884, 1046.5023,
56
+    1479.9768, 2093.0045, 2959.9536, 4186.0091, 5919.9072, 8372.0181, 11839.814, 16744.036
57
+};
58
+
59
+static float izero(SuperEqualizerContext *s, float x)
60
+{
61
+    float ret = 1;
62
+    int m;
63
+
64
+    for (m = 1; m <= M; m++) {
65
+        float t;
66
+
67
+        t = pow(x / 2, m) / s->fact[m];
68
+        ret += t*t;
69
+    }
70
+
71
+    return ret;
72
+}
73
+
74
+static float hn_lpf(int n, float f, float fs)
75
+{
76
+    float t = 1 / fs;
77
+    float omega = 2 * M_PI * f;
78
+
79
+    if (n * omega * t == 0)
80
+        return 2 * f * t;
81
+    return 2 * f * t * sinf(n * omega * t) / (n * omega * t);
82
+}
83
+
84
+static float hn_imp(int n)
85
+{
86
+    return n == 0 ? 1.f : 0.f;
87
+}
88
+
89
+static float hn(int n, EqParameter *param, float fs)
90
+{
91
+    float ret, lhn;
92
+    int i;
93
+
94
+    lhn = hn_lpf(n, param[0].upper, fs);
95
+    ret = param[0].gain*lhn;
96
+
97
+    for (i = 1; i < NBANDS + 1 && param[i].upper < fs / 2; i++) {
98
+        float lhn2 = hn_lpf(n, param[i].upper, fs);
99
+        ret += param[i].gain * (lhn2 - lhn);
100
+        lhn = lhn2;
101
+    }
102
+
103
+    ret += param[i].gain * (hn_imp(n) - lhn);
104
+
105
+    return ret;
106
+}
107
+
108
+static float alpha(float a)
109
+{
110
+    if (a <= 21)
111
+        return 0;
112
+    if (a <= 50)
113
+        return .5842f * pow(a - 21, 0.4f) + 0.07886f * (a - 21);
114
+    return .1102f * (a - 8.7f);
115
+}
116
+
117
+static float win(SuperEqualizerContext *s, float n, int N)
118
+{
119
+    return izero(s, alpha(s->aa) * sqrtf(1 - 4 * n * n / ((N - 1) * (N - 1)))) / s->iza;
120
+}
121
+
122
+static void process_param(float *bc, EqParameter *param, float fs)
123
+{
124
+    int i;
125
+
126
+    for (i = 0; i <= NBANDS; i++) {
127
+        param[i].lower = i == 0 ? 0 : bands[i - 1];
128
+        param[i].upper = i == NBANDS - 1 ? fs : bands[i];
129
+        param[i].gain  = bc[i];
130
+    }
131
+}
132
+
133
+static int equ_init(SuperEqualizerContext *s, int wb)
134
+{
135
+    int i,j;
136
+
137
+    s->rdft  = av_rdft_init(wb, DFT_R2C);
138
+    s->irdft = av_rdft_init(wb, IDFT_C2R);
139
+    if (!s->rdft || !s->irdft)
140
+        return AVERROR(ENOMEM);
141
+
142
+    s->aa = 96;
143
+    s->winlen = (1 << (wb-1))-1;
144
+    s->tabsize  = 1 << wb;
145
+
146
+    s->ires     = av_calloc(s->tabsize, sizeof(float));
147
+    s->irest    = av_calloc(s->tabsize, sizeof(float));
148
+    s->fsamples = av_calloc(s->tabsize, sizeof(float));
149
+
150
+    for (i = 0; i <= M; i++) {
151
+        s->fact[i] = 1;
152
+        for (j = 1; j <= i; j++)
153
+            s->fact[i] *= j;
154
+    }
155
+
156
+    s->iza = izero(s, alpha(s->aa));
157
+
158
+    return 0;
159
+}
160
+
161
+static void make_fir(SuperEqualizerContext *s, float *lbc, float *rbc, EqParameter *param, float fs)
162
+{
163
+    const int winlen = s->winlen;
164
+    const int tabsize = s->tabsize;
165
+    float *nires;
166
+    int i;
167
+
168
+    if (fs <= 0)
169
+        return;
170
+
171
+    process_param(lbc, param, fs);
172
+    for (i = 0; i < winlen; i++)
173
+        s->irest[i] = hn(i - winlen / 2, param, fs) * win(s, i - winlen / 2, winlen);
174
+    for (; i < tabsize; i++)
175
+        s->irest[i] = 0;
176
+
177
+    av_rdft_calc(s->rdft, s->irest);
178
+    nires = s->ires;
179
+    for (i = 0; i < tabsize; i++)
180
+        nires[i] = s->irest[i];
181
+}
182
+
183
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
184
+{
185
+    AVFilterContext *ctx = inlink->dst;
186
+    SuperEqualizerContext *s = ctx->priv;
187
+    AVFilterLink *outlink = ctx->outputs[0];
188
+    const float *ires = s->ires;
189
+    float *fsamples = s->fsamples;
190
+    int ch, i;
191
+
192
+    AVFrame *out = ff_get_audio_buffer(outlink, s->winlen);
193
+    float *src, *dst, *ptr;
194
+
195
+    if (!out) {
196
+        av_frame_free(&in);
197
+        return AVERROR(ENOMEM);
198
+    }
199
+
200
+    for (ch = 0; ch < in->channels; ch++) {
201
+        ptr = (float *)out->extended_data[ch];
202
+        dst = (float *)s->out->extended_data[ch];
203
+        src = (float *)in->extended_data[ch];
204
+
205
+        for (i = 0; i < s->winlen; i++)
206
+            fsamples[i] = src[i];
207
+        for (; i < s->tabsize; i++)
208
+            fsamples[i] = 0;
209
+
210
+        av_rdft_calc(s->rdft, fsamples);
211
+
212
+        fsamples[0] = ires[0] * fsamples[0];
213
+        fsamples[1] = ires[1] * fsamples[1];
214
+        for (i = 1; i < s->tabsize / 2; i++) {
215
+            float re, im;
216
+
217
+            re = ires[i*2  ] * fsamples[i*2] - ires[i*2+1] * fsamples[i*2+1];
218
+            im = ires[i*2+1] * fsamples[i*2] + ires[i*2  ] * fsamples[i*2+1];
219
+
220
+            fsamples[i*2  ] = re;
221
+            fsamples[i*2+1] = im;
222
+        }
223
+
224
+        av_rdft_calc(s->irdft, fsamples);
225
+
226
+        for (i = 0; i < s->winlen; i++)
227
+            dst[i] += fsamples[i] / s->tabsize * 2;
228
+        for (i = s->winlen; i < s->tabsize; i++)
229
+            dst[i]  = fsamples[i] / s->tabsize * 2;
230
+        for (i = 0; i < s->winlen; i++)
231
+            ptr[i] = dst[i];
232
+        for (i = 0; i < s->winlen; i++)
233
+            dst[i] = dst[i+s->winlen];
234
+    }
235
+
236
+    out->pts = in->pts;
237
+    av_frame_free(&in);
238
+
239
+    return ff_filter_frame(outlink, out);
240
+}
241
+
242
+static av_cold int init(AVFilterContext *ctx)
243
+{
244
+    SuperEqualizerContext *s = ctx->priv;
245
+
246
+    return equ_init(s, 14);
247
+}
248
+
249
+static int query_formats(AVFilterContext *ctx)
250
+{
251
+    AVFilterFormats *formats;
252
+    AVFilterChannelLayouts *layouts;
253
+    static const enum AVSampleFormat sample_fmts[] = {
254
+        AV_SAMPLE_FMT_FLTP,
255
+        AV_SAMPLE_FMT_NONE
256
+    };
257
+    int ret;
258
+
259
+    layouts = ff_all_channel_counts();
260
+    if (!layouts)
261
+        return AVERROR(ENOMEM);
262
+    ret = ff_set_common_channel_layouts(ctx, layouts);
263
+    if (ret < 0)
264
+        return ret;
265
+
266
+    formats = ff_make_format_list(sample_fmts);
267
+    if ((ret = ff_set_common_formats(ctx, formats)) < 0)
268
+        return ret;
269
+
270
+    formats = ff_all_samplerates();
271
+    return ff_set_common_samplerates(ctx, formats);
272
+}
273
+
274
+static int config_input(AVFilterLink *inlink)
275
+{
276
+    AVFilterContext *ctx = inlink->dst;
277
+    SuperEqualizerContext *s = ctx->priv;
278
+
279
+    inlink->partial_buf_size =
280
+    inlink->min_samples =
281
+    inlink->max_samples = s->winlen;
282
+
283
+    s->out = ff_get_audio_buffer(inlink, s->tabsize);
284
+    if (!s->out)
285
+        return AVERROR(ENOMEM);
286
+
287
+    return 0;
288
+}
289
+
290
+static int config_output(AVFilterLink *outlink)
291
+{
292
+    AVFilterContext *ctx = outlink->src;
293
+    SuperEqualizerContext *s = ctx->priv;
294
+
295
+    make_fir(s, s->gains, s->gains, s->params, outlink->sample_rate);
296
+
297
+    return 0;
298
+}
299
+
300
+static av_cold void uninit(AVFilterContext *ctx)
301
+{
302
+    SuperEqualizerContext *s = ctx->priv;
303
+
304
+    av_freep(&s->irest);
305
+    av_freep(&s->ires);
306
+    av_freep(&s->fsamples);
307
+    av_rdft_end(s->rdft);
308
+    av_rdft_end(s->irdft);
309
+}
310
+
311
+static const AVFilterPad superequalizer_inputs[] = {
312
+    {
313
+        .name         = "default",
314
+        .type         = AVMEDIA_TYPE_AUDIO,
315
+        .filter_frame = filter_frame,
316
+        .config_props = config_input,
317
+    },
318
+    { NULL }
319
+};
320
+
321
+static const AVFilterPad superequalizer_outputs[] = {
322
+    {
323
+        .name         = "default",
324
+        .type         = AVMEDIA_TYPE_AUDIO,
325
+        .config_props = config_output,
326
+    },
327
+    { NULL }
328
+};
329
+
330
+#define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
331
+#define OFFSET(x) offsetof(SuperEqualizerContext, x)
332
+
333
+static const AVOption superequalizer_options[] = {
334
+    {  "1b", "set 65Hz band gain",    OFFSET(gains [0]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
335
+    {  "2b", "set 92Hz band gain",    OFFSET(gains [1]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
336
+    {  "3b", "set 131Hz band gain",   OFFSET(gains [2]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
337
+    {  "4b", "set 185Hz band gain",   OFFSET(gains [3]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
338
+    {  "5b", "set 262Hz band gain",   OFFSET(gains [4]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
339
+    {  "6b", "set 370Hz band gain",   OFFSET(gains [5]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
340
+    {  "7b", "set 523Hz band gain",   OFFSET(gains [6]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
341
+    {  "8b", "set 740Hz band gain",   OFFSET(gains [7]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
342
+    {  "9b", "set 1047Hz band gain",  OFFSET(gains [8]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
343
+    { "10b", "set 1480Hz band gain",  OFFSET(gains [9]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
344
+    { "11b", "set 2093Hz band gain",  OFFSET(gains[10]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
345
+    { "12b", "set 2960Hz band gain",  OFFSET(gains[11]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
346
+    { "13b", "set 4186Hz band gain",  OFFSET(gains[12]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
347
+    { "14b", "set 5920Hz band gain",  OFFSET(gains[13]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
348
+    { "15b", "set 8372Hz band gain",  OFFSET(gains[14]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
349
+    { "16b", "set 11840Hz band gain", OFFSET(gains[15]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
350
+    { "17b", "set 16744Hz band gain", OFFSET(gains[16]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
351
+    { "18b", "set 20000Hz band gain", OFFSET(gains[17]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
352
+    { NULL }
353
+};
354
+
355
+AVFILTER_DEFINE_CLASS(superequalizer);
356
+
357
+AVFilter ff_af_superequalizer = {
358
+    .name          = "superequalizer",
359
+    .description   = NULL_IF_CONFIG_SMALL("Apply 18-th band equalization filter."),
360
+    .priv_size     = sizeof(SuperEqualizerContext),
361
+    .priv_class    = &superequalizer_class,
362
+    .query_formats = query_formats,
363
+    .init          = init,
364
+    .uninit        = uninit,
365
+    .inputs        = superequalizer_inputs,
366
+    .outputs       = superequalizer_outputs,
367
+};
... ...
@@ -122,6 +122,7 @@ static void register_all(void)
122 122
     REGISTER_FILTER(SOFALIZER,      sofalizer,      af);
123 123
     REGISTER_FILTER(STEREOTOOLS,    stereotools,    af);
124 124
     REGISTER_FILTER(STEREOWIDEN,    stereowiden,    af);
125
+    REGISTER_FILTER(SUPEREQUALIZER, superequalizer, af);
125 126
     REGISTER_FILTER(SURROUND,       surround,       af);
126 127
     REGISTER_FILTER(TREBLE,         treble,         af);
127 128
     REGISTER_FILTER(TREMOLO,        tremolo,        af);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  92
33
+#define LIBAVFILTER_VERSION_MINOR  93
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \