Browse code

avfilter: add firequalizer filter

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Muhammad Faiz <mfcc64@gmail.com>

Muhammad Faiz authored on 2016/02/17 03:02:22
Showing 8 changed files
... ...
@@ -6,6 +6,7 @@ version <next>:
6 6
 - fieldhint filter
7 7
 - loop video filter and aloop audio filter
8 8
 - Bob Weaver deinterlacing filter
9
+- firequalizer filter
9 10
 
10 11
 
11 12
 version 3.0:
... ...
@@ -353,6 +353,7 @@ Filters:
353 353
   af_biquads.c                          Paul B Mahol
354 354
   af_chorus.c                           Paul B Mahol
355 355
   af_compand.c                          Paul B Mahol
356
+  af_firequalizer.c                     Muhammad Faiz
356 357
   af_ladspa.c                           Paul B Mahol
357 358
   af_pan.c                              Nicolas George
358 359
   af_sidechaincompress.c                Paul B Mahol
... ...
@@ -2861,6 +2861,8 @@ eq_filter_deps="gpl"
2861 2861
 fftfilt_filter_deps="avcodec"
2862 2862
 fftfilt_filter_select="rdft"
2863 2863
 find_rect_filter_deps="avcodec avformat gpl"
2864
+firequalizer_filter_deps="avcodec"
2865
+firequalizer_filter_select="rdft"
2864 2866
 flite_filter_deps="libflite"
2865 2867
 frei0r_filter_deps="frei0r dlopen"
2866 2868
 frei0r_src_filter_deps="frei0r dlopen"
... ...
@@ -2366,6 +2366,115 @@ Sets the difference coefficient (default: 2.5). 0.0 means mono sound
2366 2366
 Enable clipping. By default is enabled.
2367 2367
 @end table
2368 2368
 
2369
+@section firequalizer
2370
+Apply FIR Equalization using arbitrary frequency response.
2371
+
2372
+The filter accepts the following option:
2373
+
2374
+@table @option
2375
+@item gain
2376
+Set gain curve equation (in dB). The expression can contain variables:
2377
+@table @option
2378
+@item f
2379
+the evaluated frequency
2380
+@item sr
2381
+sample rate
2382
+@item ch
2383
+channel number, set to 0 when multichannels evaluation is disabled
2384
+@item chid
2385
+channel id, see libavutil/channel_layout.h, set to the first channel id when
2386
+multichannels evaluation is disabled
2387
+@item chs
2388
+number of channels
2389
+@item chlayout
2390
+channel_layout, see libavutil/channel_layout.h
2391
+
2392
+@end table
2393
+and functions:
2394
+@table @option
2395
+@item gain_interpolate(f)
2396
+interpolate gain on frequency f based on gain_entry
2397
+@end table
2398
+This option is also available as command. Default is @code{gain_interpolate(f)}.
2399
+
2400
+@item gain_entry
2401
+Set gain entry for gain_interpolate function. The expression can
2402
+contain functions:
2403
+@table @option
2404
+@item entry(f, g)
2405
+store gain entry at frequency f with value g
2406
+@end table
2407
+This option is also available as command.
2408
+
2409
+@item delay
2410
+Set filter delay in seconds. Higher value means more accurate.
2411
+Default is @code{0.01}.
2412
+
2413
+@item accuracy
2414
+Set filter accuracy in Hz. Lower value means more accurate.
2415
+Default is @code{5}.
2416
+
2417
+@item wfunc
2418
+Set window function. Acceptable values are:
2419
+@table @option
2420
+@item rectangular
2421
+rectangular window, useful when gain curve is already smooth
2422
+@item hann
2423
+hann window (default)
2424
+@item hamming
2425
+hamming window
2426
+@item blackman
2427
+blackman window
2428
+@item nuttall3
2429
+3-terms continuous 1st derivative nuttall window
2430
+@item mnuttall3
2431
+minimum 3-terms discontinuous nuttall window
2432
+@item nuttall
2433
+4-terms continuous 1st derivative nuttall window
2434
+@item bnuttall
2435
+minimum 4-terms discontinuous nuttall (blackman-nuttall) window
2436
+@item bharris
2437
+blackman-harris window
2438
+@end table
2439
+
2440
+@item fixed
2441
+If enabled, use fixed number of audio samples. This improves speed when
2442
+filtering with large delay. Default is disabled.
2443
+
2444
+@item multi
2445
+Enable multichannels evaluation on gain. Default is disabled.
2446
+@end table
2447
+
2448
+@subsection Examples
2449
+@itemize
2450
+@item
2451
+lowpass at 1000 Hz:
2452
+@example
2453
+firequalizer=gain='if(lt(f,1000), 0, -INF)'
2454
+@end example
2455
+@item
2456
+lowpass at 1000 Hz with gain_entry:
2457
+@example
2458
+firequalizer=gain_entry='entry(1000,0); entry(1001, -INF)'
2459
+@end example
2460
+@item
2461
+custom equalization:
2462
+@example
2463
+firequalizer=gain_entry='entry(100,0); entry(400, -4); entry(1000, -6); entry(2000, 0)'
2464
+@end example
2465
+@item
2466
+higher delay:
2467
+@example
2468
+firequalizer=delay=0.1:fixed=on
2469
+@end example
2470
+@item
2471
+lowpass on left channel, highpass on right channel:
2472
+@example
2473
+firequalizer=gain='if(eq(chid,1), gain_interpolate(f), if(eq(chid,2), gain_interpolate(1e6+f), 0))'
2474
+:gain_entry='entry(1000, 0); entry(1001,-INF); entry(1e6+1000,0)':multi=on
2475
+@end example
2476
+@end itemize
2477
+
2369 2478
 @section flanger
2370 2479
 Apply a flanging effect to the audio.
2371 2480
 
... ...
@@ -80,6 +80,7 @@ OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
80 80
 OBJS-$(CONFIG_EBUR128_FILTER)                += f_ebur128.o
81 81
 OBJS-$(CONFIG_EQUALIZER_FILTER)              += af_biquads.o
82 82
 OBJS-$(CONFIG_EXTRASTEREO_FILTER)            += af_extrastereo.o
83
+OBJS-$(CONFIG_FIREQUALIZER_FILTER)           += af_firequalizer.o
83 84
 OBJS-$(CONFIG_FLANGER_FILTER)                += af_flanger.o generate_wave_table.o
84 85
 OBJS-$(CONFIG_HIGHPASS_FILTER)               += af_biquads.o
85 86
 OBJS-$(CONFIG_JOIN_FILTER)                   += af_join.o
86 87
new file mode 100644
... ...
@@ -0,0 +1,592 @@
0
+/*
1
+ * Copyright (c) 2016 Muhammad Faiz <mfcc64@gmail.com>
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/opt.h"
21
+#include "libavutil/eval.h"
22
+#include "libavutil/avassert.h"
23
+#include "libavcodec/avfft.h"
24
+#include "avfilter.h"
25
+#include "internal.h"
26
+#include "audio.h"
27
+
28
+#define RDFT_BITS_MIN 4
29
+#define RDFT_BITS_MAX 16
30
+
31
+enum WindowFunc {
32
+    WFUNC_MIN,
33
+    WFUNC_RECTANGULAR = WFUNC_MIN,
34
+    WFUNC_HANN,
35
+    WFUNC_HAMMING,
36
+    WFUNC_BLACKMAN,
37
+    WFUNC_NUTTALL3,
38
+    WFUNC_MNUTTALL3,
39
+    WFUNC_NUTTALL,
40
+    WFUNC_BNUTTALL,
41
+    WFUNC_BHARRIS,
42
+    WFUNC_MAX = WFUNC_BHARRIS
43
+};
44
+
45
+#define NB_GAIN_ENTRY_MAX 4096
46
+typedef struct {
47
+    double  freq;
48
+    double  gain;
49
+} GainEntry;
50
+
51
+typedef struct {
52
+    int buf_idx;
53
+    int overlap_idx;
54
+} OverlapIndex;
55
+
56
+typedef struct {
57
+    const AVClass *class;
58
+
59
+    RDFTContext   *analysis_irdft;
60
+    RDFTContext   *rdft;
61
+    RDFTContext   *irdft;
62
+    int           analysis_rdft_len;
63
+    int           rdft_len;
64
+
65
+    float         *analysis_buf;
66
+    float         *kernel_tmp_buf;
67
+    float         *kernel_buf;
68
+    float         *conv_buf;
69
+    OverlapIndex  *conv_idx;
70
+    int           fir_len;
71
+    int           nsamples_max;
72
+    int64_t       next_pts;
73
+    int           frame_nsamples_max;
74
+    int           remaining;
75
+
76
+    char          *gain_cmd;
77
+    char          *gain_entry_cmd;
78
+    const char    *gain;
79
+    const char    *gain_entry;
80
+    double        delay;
81
+    double        accuracy;
82
+    int           wfunc;
83
+    int           fixed;
84
+    int           multi;
85
+
86
+    int           nb_gain_entry;
87
+    int           gain_entry_err;
88
+    GainEntry     gain_entry_tbl[NB_GAIN_ENTRY_MAX];
89
+} FIREqualizerContext;
90
+
91
+#define OFFSET(x) offsetof(FIREqualizerContext, x)
92
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
93
+
94
+static const AVOption firequalizer_options[] = {
95
+    { "gain", "set gain curve", OFFSET(gain), AV_OPT_TYPE_STRING, { .str = "gain_interpolate(f)" }, 0, 0, FLAGS },
96
+    { "gain_entry", "set gain entry", OFFSET(gain_entry), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
97
+    { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.0, 1e10, FLAGS },
98
+    { "accuracy", "set accuracy", OFFSET(accuracy), AV_OPT_TYPE_DOUBLE, { .dbl = 5.0 }, 0.0, 1e10, FLAGS },
99
+    { "wfunc", "set window function", OFFSET(wfunc), AV_OPT_TYPE_INT, { .i64 = WFUNC_HANN }, WFUNC_MIN, WFUNC_MAX, FLAGS, "wfunc" },
100
+        { "rectangular", "rectangular window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_RECTANGULAR }, 0, 0, FLAGS, "wfunc" },
101
+        { "hann", "hann window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HANN }, 0, 0, FLAGS, "wfunc" },
102
+        { "hamming", "hamming window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HAMMING }, 0, 0, FLAGS, "wfunc" },
103
+        { "blackman", "blackman window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BLACKMAN }, 0, 0, FLAGS, "wfunc" },
104
+        { "nuttall3", "3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL3 }, 0, 0, FLAGS, "wfunc" },
105
+        { "mnuttall3", "minimum 3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_MNUTTALL3 }, 0, 0, FLAGS, "wfunc" },
106
+        { "nuttall", "nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL }, 0, 0, FLAGS, "wfunc" },
107
+        { "bnuttall", "blackman-nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BNUTTALL }, 0, 0, FLAGS, "wfunc" },
108
+        { "bharris", "blackman-harris window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BHARRIS }, 0, 0, FLAGS, "wfunc" },
109
+    { "fixed", "set fixed frame samples", OFFSET(fixed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
110
+    { "multi", "set multi channels mode", OFFSET(multi), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
111
+    { NULL }
112
+};
113
+
114
+AVFILTER_DEFINE_CLASS(firequalizer);
115
+
116
+static void common_uninit(FIREqualizerContext *s)
117
+{
118
+    av_rdft_end(s->analysis_irdft);
119
+    av_rdft_end(s->rdft);
120
+    av_rdft_end(s->irdft);
121
+    s->analysis_irdft = s->rdft = s->irdft = NULL;
122
+
123
+    av_freep(&s->analysis_buf);
124
+    av_freep(&s->kernel_tmp_buf);
125
+    av_freep(&s->kernel_buf);
126
+    av_freep(&s->conv_buf);
127
+    av_freep(&s->conv_idx);
128
+}
129
+
130
+static av_cold void uninit(AVFilterContext *ctx)
131
+{
132
+    FIREqualizerContext *s = ctx->priv;
133
+
134
+    common_uninit(s);
135
+    av_freep(&s->gain_cmd);
136
+    av_freep(&s->gain_entry_cmd);
137
+}
138
+
139
+static int query_formats(AVFilterContext *ctx)
140
+{
141
+    AVFilterChannelLayouts *layouts;
142
+    AVFilterFormats *formats;
143
+    static const enum AVSampleFormat sample_fmts[] = {
144
+        AV_SAMPLE_FMT_FLTP,
145
+        AV_SAMPLE_FMT_NONE
146
+    };
147
+    int ret;
148
+
149
+    layouts = ff_all_channel_counts();
150
+    if (!layouts)
151
+        return AVERROR(ENOMEM);
152
+    ret = ff_set_common_channel_layouts(ctx, layouts);
153
+    if (ret < 0)
154
+        return ret;
155
+
156
+    formats = ff_make_format_list(sample_fmts);
157
+    if (!formats)
158
+        return AVERROR(ENOMEM);
159
+    ret = ff_set_common_formats(ctx, formats);
160
+    if (ret < 0)
161
+        return ret;
162
+
163
+    formats = ff_all_samplerates();
164
+    if (!formats)
165
+        return AVERROR(ENOMEM);
166
+    return ff_set_common_samplerates(ctx, formats);
167
+}
168
+
169
+static void fast_convolute(FIREqualizerContext *s, const float *kernel_buf, float *conv_buf,
170
+                           OverlapIndex *idx, float *data, int nsamples)
171
+{
172
+    if (nsamples <= s->nsamples_max) {
173
+        float *buf = conv_buf + idx->buf_idx * s->rdft_len;
174
+        float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx;
175
+        int k;
176
+
177
+        memcpy(buf, data, nsamples * sizeof(*data));
178
+        memset(buf + nsamples, 0, (s->rdft_len - nsamples) * sizeof(*data));
179
+        av_rdft_calc(s->rdft, buf);
180
+
181
+        buf[0] *= kernel_buf[0];
182
+        buf[1] *= kernel_buf[1];
183
+        for (k = 2; k < s->rdft_len; k += 2) {
184
+            float re, im;
185
+            re = buf[k] * kernel_buf[k] - buf[k+1] * kernel_buf[k+1];
186
+            im = buf[k] * kernel_buf[k+1] + buf[k+1] * kernel_buf[k];
187
+            buf[k] = re;
188
+            buf[k+1] = im;
189
+        }
190
+
191
+        av_rdft_calc(s->irdft, buf);
192
+        for (k = 0; k < s->rdft_len - idx->overlap_idx; k++)
193
+            buf[k] += obuf[k];
194
+        memcpy(data, buf, nsamples * sizeof(*data));
195
+        idx->buf_idx = !idx->buf_idx;
196
+        idx->overlap_idx = nsamples;
197
+    } else {
198
+        while (nsamples > s->nsamples_max * 2) {
199
+            fast_convolute(s, kernel_buf, conv_buf, idx, data, s->nsamples_max);
200
+            data += s->nsamples_max;
201
+            nsamples -= s->nsamples_max;
202
+        }
203
+        fast_convolute(s, kernel_buf, conv_buf, idx, data, nsamples/2);
204
+        fast_convolute(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2);
205
+    }
206
+}
207
+
208
+static double entry_func(void *p, double freq, double gain)
209
+{
210
+    AVFilterContext *ctx = p;
211
+    FIREqualizerContext *s = ctx->priv;
212
+
213
+    if (s->nb_gain_entry >= NB_GAIN_ENTRY_MAX) {
214
+        av_log(ctx, AV_LOG_ERROR, "entry table overflow.\n");
215
+        s->gain_entry_err = AVERROR(EINVAL);
216
+        return 0;
217
+    }
218
+
219
+    if (isnan(freq)) {
220
+        av_log(ctx, AV_LOG_ERROR, "nan frequency (%g, %g).\n", freq, gain);
221
+        s->gain_entry_err = AVERROR(EINVAL);
222
+        return 0;
223
+    }
224
+
225
+    if (s->nb_gain_entry > 0 && freq <= s->gain_entry_tbl[s->nb_gain_entry - 1].freq) {
226
+        av_log(ctx, AV_LOG_ERROR, "unsorted frequency (%g, %g).\n", freq, gain);
227
+        s->gain_entry_err = AVERROR(EINVAL);
228
+        return 0;
229
+    }
230
+
231
+    s->gain_entry_tbl[s->nb_gain_entry].freq = freq;
232
+    s->gain_entry_tbl[s->nb_gain_entry].gain = gain;
233
+    s->nb_gain_entry++;
234
+    return 0;
235
+}
236
+
237
+static int gain_entry_compare(const void *key, const void *memb)
238
+{
239
+    const double *freq = key;
240
+    const GainEntry *entry = memb;
241
+
242
+    if (*freq < entry[0].freq)
243
+        return -1;
244
+    if (*freq > entry[1].freq)
245
+        return 1;
246
+    return 0;
247
+}
248
+
249
+static double gain_interpolate_func(void *p, double freq)
250
+{
251
+    AVFilterContext *ctx = p;
252
+    FIREqualizerContext *s = ctx->priv;
253
+    GainEntry *res;
254
+    double d0, d1, d;
255
+
256
+    if (isnan(freq))
257
+        return freq;
258
+
259
+    if (!s->nb_gain_entry)
260
+        return 0;
261
+
262
+    if (freq <= s->gain_entry_tbl[0].freq)
263
+        return s->gain_entry_tbl[0].gain;
264
+
265
+    if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq)
266
+        return s->gain_entry_tbl[s->nb_gain_entry-1].gain;
267
+
268
+    res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare);
269
+    av_assert0(res);
270
+
271
+    d  = res[1].freq - res[0].freq;
272
+    d0 = freq - res[0].freq;
273
+    d1 = res[1].freq - freq;
274
+
275
+    if (d0 && d1)
276
+        return (d0 * res[1].gain + d1 * res[0].gain) / d;
277
+
278
+    if (d0)
279
+        return res[1].gain;
280
+
281
+    return res[0].gain;
282
+}
283
+
284
+static const char *const var_names[] = {
285
+    "f",
286
+    "sr",
287
+    "ch",
288
+    "chid",
289
+    "chs",
290
+    "chlayout",
291
+    NULL
292
+};
293
+
294
+enum VarOffset {
295
+    VAR_F,
296
+    VAR_SR,
297
+    VAR_CH,
298
+    VAR_CHID,
299
+    VAR_CHS,
300
+    VAR_CHLAYOUT,
301
+    VAR_NB
302
+};
303
+
304
+static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *gain_entry)
305
+{
306
+    FIREqualizerContext *s = ctx->priv;
307
+    AVFilterLink *inlink = ctx->inputs[0];
308
+    const char *gain_entry_func_names[] = { "entry", NULL };
309
+    const char *gain_func_names[] = { "gain_interpolate", NULL };
310
+    double (*gain_entry_funcs[])(void *, double, double) = { entry_func, NULL };
311
+    double (*gain_funcs[])(void *, double) = { gain_interpolate_func, NULL };
312
+    double vars[VAR_NB];
313
+    AVExpr *gain_expr;
314
+    int ret, k, center, ch;
315
+
316
+    s->nb_gain_entry = 0;
317
+    s->gain_entry_err = 0;
318
+    if (gain_entry) {
319
+        double result = 0.0;
320
+        ret = av_expr_parse_and_eval(&result, gain_entry, NULL, NULL, NULL, NULL,
321
+                                     gain_entry_func_names, gain_entry_funcs, ctx, 0, ctx);
322
+        if (ret < 0)
323
+            return ret;
324
+        if (s->gain_entry_err < 0)
325
+            return s->gain_entry_err;
326
+    }
327
+
328
+    av_log(ctx, AV_LOG_DEBUG, "nb_gain_entry = %d.\n", s->nb_gain_entry);
329
+
330
+    ret = av_expr_parse(&gain_expr, gain, var_names,
331
+                        gain_func_names, gain_funcs, NULL, NULL, 0, ctx);
332
+    if (ret < 0)
333
+        return ret;
334
+
335
+    vars[VAR_CHS] = inlink->channels;
336
+    vars[VAR_CHLAYOUT] = inlink->channel_layout;
337
+    vars[VAR_SR] = inlink->sample_rate;
338
+    for (ch = 0; ch < inlink->channels; ch++) {
339
+        vars[VAR_CH] = ch;
340
+        vars[VAR_CHID] = av_channel_layout_extract_channel(inlink->channel_layout, ch);
341
+        vars[VAR_F] = 0.0;
342
+        s->analysis_buf[0] = pow(10.0, 0.05 * av_expr_eval(gain_expr, vars, ctx));
343
+        vars[VAR_F] = 0.5 * inlink->sample_rate;
344
+        s->analysis_buf[1] = pow(10.0, 0.05 * av_expr_eval(gain_expr, vars, ctx));
345
+
346
+        for (k = 1; k < s->analysis_rdft_len/2; k++) {
347
+            vars[VAR_F] = k * ((double)inlink->sample_rate /(double)s->analysis_rdft_len);
348
+            s->analysis_buf[2*k] = pow(10.0, 0.05 * av_expr_eval(gain_expr, vars, ctx));
349
+            s->analysis_buf[2*k+1] = 0.0;
350
+        }
351
+
352
+        av_rdft_calc(s->analysis_irdft, s->analysis_buf);
353
+        center = s->fir_len / 2;
354
+
355
+        for (k = 0; k <= center; k++) {
356
+            double u = k * (M_PI/center);
357
+            double win;
358
+            switch (s->wfunc) {
359
+            case WFUNC_RECTANGULAR:
360
+                win = 1.0;
361
+                break;
362
+            case WFUNC_HANN:
363
+                win = 0.5 + 0.5 * cos(u);
364
+                break;
365
+            case WFUNC_HAMMING:
366
+                win = 0.53836 + 0.46164 * cos(u);
367
+                break;
368
+            case WFUNC_BLACKMAN:
369
+                win = 0.48 + 0.5 * cos(u) + 0.02 * cos(2*u);
370
+                break;
371
+            case WFUNC_NUTTALL3:
372
+                win = 0.40897 + 0.5 * cos(u) + 0.09103 * cos(2*u);
373
+                break;
374
+            case WFUNC_MNUTTALL3:
375
+                win = 0.4243801 + 0.4973406 * cos(u) + 0.0782793 * cos(2*u);
376
+                break;
377
+            case WFUNC_NUTTALL:
378
+                win = 0.355768 + 0.487396 * cos(u) + 0.144232 * cos(2*u) + 0.012604 * cos(3*u);
379
+                break;
380
+            case WFUNC_BNUTTALL:
381
+                win = 0.3635819 + 0.4891775 * cos(u) + 0.1365995 * cos(2*u) + 0.0106411 * cos(3*u);
382
+                break;
383
+            case WFUNC_BHARRIS:
384
+                win = 0.35875 + 0.48829 * cos(u) + 0.14128 * cos(2*u) + 0.01168 * cos(3*u);
385
+                break;
386
+            default:
387
+                av_assert0(0);
388
+            }
389
+            s->analysis_buf[k] *= (2.0/s->analysis_rdft_len) * (2.0/s->rdft_len) * win;
390
+        }
391
+
392
+        for (k = 0; k < center - k; k++) {
393
+            float tmp = s->analysis_buf[k];
394
+            s->analysis_buf[k] = s->analysis_buf[center - k];
395
+            s->analysis_buf[center - k] = tmp;
396
+        }
397
+
398
+        for (k = 1; k <= center; k++)
399
+            s->analysis_buf[center + k] = s->analysis_buf[center - k];
400
+
401
+        memset(s->analysis_buf + s->fir_len, 0, (s->rdft_len - s->fir_len) * sizeof(*s->analysis_buf));
402
+        av_rdft_calc(s->rdft, s->analysis_buf);
403
+
404
+        for (k = 0; k < s->rdft_len; k++) {
405
+            if (isnan(s->analysis_buf[k]) || isinf(s->analysis_buf[k])) {
406
+                av_log(ctx, AV_LOG_ERROR, "filter kernel contains nan or infinity.\n");
407
+                av_expr_free(gain_expr);
408
+                return AVERROR(EINVAL);
409
+            }
410
+        }
411
+
412
+        memcpy(s->kernel_tmp_buf + ch * s->rdft_len, s->analysis_buf, s->rdft_len * sizeof(*s->analysis_buf));
413
+        if (!s->multi)
414
+            break;
415
+    }
416
+
417
+    memcpy(s->kernel_buf, s->kernel_tmp_buf, (s->multi ? inlink->channels : 1) * s->rdft_len * sizeof(*s->kernel_buf));
418
+    av_expr_free(gain_expr);
419
+    return 0;
420
+}
421
+
422
+static int config_input(AVFilterLink *inlink)
423
+{
424
+    AVFilterContext *ctx = inlink->dst;
425
+    FIREqualizerContext *s = ctx->priv;
426
+    int rdft_bits;
427
+
428
+    common_uninit(s);
429
+
430
+    s->next_pts = 0;
431
+    s->frame_nsamples_max = 0;
432
+
433
+    s->fir_len = FFMAX(2 * (int)(inlink->sample_rate * s->delay) + 1, 3);
434
+    s->remaining = s->fir_len - 1;
435
+
436
+    for (rdft_bits = RDFT_BITS_MIN; rdft_bits <= RDFT_BITS_MAX; rdft_bits++) {
437
+        s->rdft_len = 1 << rdft_bits;
438
+        s->nsamples_max = s->rdft_len - s->fir_len + 1;
439
+        if (s->nsamples_max * 2 >= s->fir_len)
440
+            break;
441
+    }
442
+
443
+    if (rdft_bits > RDFT_BITS_MAX) {
444
+        av_log(ctx, AV_LOG_ERROR, "too large delay, please decrease it.\n");
445
+        return AVERROR(EINVAL);
446
+    }
447
+
448
+    if (!(s->rdft = av_rdft_init(rdft_bits, DFT_R2C)) || !(s->irdft = av_rdft_init(rdft_bits, IDFT_C2R)))
449
+        return AVERROR(ENOMEM);
450
+
451
+    for ( ; rdft_bits <= RDFT_BITS_MAX; rdft_bits++) {
452
+        s->analysis_rdft_len = 1 << rdft_bits;
453
+        if (inlink->sample_rate <= s->accuracy * s->analysis_rdft_len)
454
+            break;
455
+    }
456
+
457
+    if (rdft_bits > RDFT_BITS_MAX) {
458
+        av_log(ctx, AV_LOG_ERROR, "too small accuracy, please increase it.\n");
459
+        return AVERROR(EINVAL);
460
+    }
461
+
462
+    if (!(s->analysis_irdft = av_rdft_init(rdft_bits, IDFT_C2R)))
463
+        return AVERROR(ENOMEM);
464
+
465
+    s->analysis_buf = av_malloc_array(s->analysis_rdft_len, sizeof(*s->analysis_buf));
466
+    s->kernel_tmp_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_tmp_buf));
467
+    s->kernel_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_buf));
468
+    s->conv_buf   = av_calloc(2 * s->rdft_len * inlink->channels, sizeof(*s->conv_buf));
469
+    s->conv_idx   = av_calloc(inlink->channels, sizeof(*s->conv_idx));
470
+    if (!s->analysis_buf || !s->kernel_tmp_buf || !s->kernel_buf || !s->conv_buf || !s->conv_idx)
471
+        return AVERROR(ENOMEM);
472
+
473
+    av_log(ctx, AV_LOG_DEBUG, "sample_rate = %d, channels = %d, analysis_rdft_len = %d, rdft_len = %d, fir_len = %d, nsamples_max = %d.\n",
474
+           inlink->sample_rate, inlink->channels, s->analysis_rdft_len, s->rdft_len, s->fir_len, s->nsamples_max);
475
+
476
+    if (s->fixed)
477
+        inlink->min_samples = inlink->max_samples = inlink->partial_buf_size = s->nsamples_max;
478
+
479
+    return generate_kernel(ctx, s->gain_cmd ? s->gain_cmd : s->gain,
480
+                           s->gain_entry_cmd ? s->gain_entry_cmd : s->gain_entry);
481
+}
482
+
483
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
484
+{
485
+    AVFilterContext *ctx = inlink->dst;
486
+    FIREqualizerContext *s = ctx->priv;
487
+    int ch;
488
+
489
+    for (ch = 0; ch < inlink->channels; ch++) {
490
+        fast_convolute(s, s->kernel_buf + (s->multi ? ch * s->rdft_len : 0),
491
+                       s->conv_buf + 2 * ch * s->rdft_len, s->conv_idx + ch,
492
+                       (float *) frame->extended_data[ch], frame->nb_samples);
493
+    }
494
+
495
+    s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, av_make_q(1, inlink->sample_rate), inlink->time_base);
496
+    s->frame_nsamples_max = FFMAX(s->frame_nsamples_max, frame->nb_samples);
497
+    return ff_filter_frame(ctx->outputs[0], frame);
498
+}
499
+
500
+static int request_frame(AVFilterLink *outlink)
501
+{
502
+    AVFilterContext *ctx = outlink->src;
503
+    FIREqualizerContext *s= ctx->priv;
504
+    int ret;
505
+
506
+    ret = ff_request_frame(ctx->inputs[0]);
507
+    if (ret == AVERROR_EOF && s->remaining > 0 && s->frame_nsamples_max > 0) {
508
+        AVFrame *frame = ff_get_audio_buffer(outlink, FFMIN(s->remaining, s->frame_nsamples_max));
509
+
510
+        if (!frame)
511
+            return AVERROR(ENOMEM);
512
+
513
+        av_samples_set_silence(frame->extended_data, 0, frame->nb_samples, outlink->channels, frame->format);
514
+        frame->pts = s->next_pts;
515
+        s->remaining -= frame->nb_samples;
516
+        ret = filter_frame(ctx->inputs[0], frame);
517
+    }
518
+
519
+    return ret;
520
+}
521
+
522
+static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
523
+                           char *res, int res_len, int flags)
524
+{
525
+    FIREqualizerContext *s = ctx->priv;
526
+    int ret = AVERROR(ENOSYS);
527
+
528
+    if (!strcmp(cmd, "gain")) {
529
+        char *gain_cmd;
530
+
531
+        gain_cmd = av_strdup(args);
532
+        if (!gain_cmd)
533
+            return AVERROR(ENOMEM);
534
+
535
+        ret = generate_kernel(ctx, gain_cmd, s->gain_entry_cmd ? s->gain_entry_cmd : s->gain_entry);
536
+        if (ret >= 0) {
537
+            av_freep(&s->gain_cmd);
538
+            s->gain_cmd = gain_cmd;
539
+        } else {
540
+            av_freep(&gain_cmd);
541
+        }
542
+    } else if (!strcmp(cmd, "gain_entry")) {
543
+        char *gain_entry_cmd;
544
+
545
+        gain_entry_cmd = av_strdup(args);
546
+        if (!gain_entry_cmd)
547
+            return AVERROR(ENOMEM);
548
+
549
+        ret = generate_kernel(ctx, s->gain_cmd ? s->gain_cmd : s->gain, gain_entry_cmd);
550
+        if (ret >= 0) {
551
+            av_freep(&s->gain_entry_cmd);
552
+            s->gain_entry_cmd = gain_entry_cmd;
553
+        } else {
554
+            av_freep(&gain_entry_cmd);
555
+        }
556
+    }
557
+
558
+    return ret;
559
+}
560
+
561
+static const AVFilterPad firequalizer_inputs[] = {
562
+    {
563
+        .name           = "default",
564
+        .config_props   = config_input,
565
+        .filter_frame   = filter_frame,
566
+        .type           = AVMEDIA_TYPE_AUDIO,
567
+        .needs_writable = 1,
568
+    },
569
+    { NULL }
570
+};
571
+
572
+static const AVFilterPad firequalizer_outputs[] = {
573
+    {
574
+        .name           = "default",
575
+        .request_frame  = request_frame,
576
+        .type           = AVMEDIA_TYPE_AUDIO,
577
+    },
578
+    { NULL }
579
+};
580
+
581
+AVFilter ff_af_firequalizer = {
582
+    .name               = "firequalizer",
583
+    .description        = NULL_IF_CONFIG_SMALL("Finite Impulse Response Equalizer"),
584
+    .uninit             = uninit,
585
+    .query_formats      = query_formats,
586
+    .process_command    = process_command,
587
+    .priv_size          = sizeof(FIREqualizerContext),
588
+    .inputs             = firequalizer_inputs,
589
+    .outputs            = firequalizer_outputs,
590
+    .priv_class         = &firequalizer_class,
591
+};
... ...
@@ -101,6 +101,7 @@ void avfilter_register_all(void)
101 101
     REGISTER_FILTER(EBUR128,        ebur128,        af);
102 102
     REGISTER_FILTER(EQUALIZER,      equalizer,      af);
103 103
     REGISTER_FILTER(EXTRASTEREO,    extrastereo,    af);
104
+    REGISTER_FILTER(FIREQUALIZER,   firequalizer,   af);
104 105
     REGISTER_FILTER(FLANGER,        flanger,        af);
105 106
     REGISTER_FILTER(HIGHPASS,       highpass,       af);
106 107
     REGISTER_FILTER(JOIN,           join,           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  34
33
+#define LIBAVFILTER_VERSION_MINOR  35
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \