Browse code

avfilter: add high-order parametric multiband equalizer filter

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

Paul B Mahol authored on 2015/12/20 17:50:35
Showing 7 changed files
... ...
@@ -47,6 +47,7 @@ version <next>:
47 47
 - DXVA2-accelerated VP9 decoding
48 48
 - SOFAlizer: virtual binaural acoustics filter
49 49
 - VAAPI VP9 hwaccel
50
+- audio high-order multiband parametric equalizer
50 51
 
51 52
 
52 53
 version 2.8:
... ...
@@ -2841,6 +2841,7 @@ unix_protocol_select="network"
2841 2841
 
2842 2842
 # filters
2843 2843
 amovie_filter_deps="avcodec avformat"
2844
+anequalizer_filter_deps="cabs cexp"
2844 2845
 aresample_filter_deps="swresample"
2845 2846
 ass_filter_deps="libass"
2846 2847
 asyncts_filter_deps="avresample"
... ...
@@ -992,6 +992,100 @@ stream ends. The default value is 2 seconds.
992 992
 
993 993
 @end table
994 994
 
995
+@section anequalizer
996
+
997
+High-order parametric multiband equalizer for each channel.
998
+
999
+It accepts the following parameters:
1000
+@table @option
1001
+@item params
1002
+
1003
+This option string is in format:
1004
+"c@var{chn} f=@var{cf} w=@var{w} g=@var{g} t=@var{f} | ..."
1005
+Each equalizer band is separated by '|'.
1006
+
1007
+@table @option
1008
+@item chn
1009
+Set channel number to which equalization will be applied.
1010
+If input doesn't have that channel the entry is ignored.
1011
+
1012
+@item cf
1013
+Set central frequency for band.
1014
+If input doesn't have that frequency the entry is ignored.
1015
+
1016
+@item w
1017
+Set band width in hertz.
1018
+
1019
+@item g
1020
+Set band gain in dB.
1021
+
1022
+@item f
1023
+Set filter type for band, optional, can be:
1024
+
1025
+@table @samp
1026
+@item 0
1027
+Butterworth, this is default.
1028
+
1029
+@item 1
1030
+Chebyshev type 1.
1031
+
1032
+@item 2
1033
+Chebyshev type 2.
1034
+@end table
1035
+@end table
1036
+
1037
+@item curves
1038
+With this option activated frequency response of anequalizer is displayed
1039
+in video stream.
1040
+
1041
+@item size
1042
+Set video stream size. Only useful if curves option is activated.
1043
+
1044
+@item mgain
1045
+Set max gain that will be displayed. Only useful if curves option is activated.
1046
+Setting this to reasonable value allows to display gain which is derived from
1047
+neighbour bands which are too close to each other and thus produce higher gain
1048
+when both are activated.
1049
+
1050
+@item fscale
1051
+Set frequency scale used to draw frequency response in video output.
1052
+Can be linear or logarithmic. Default is logarithmic.
1053
+
1054
+@item colors
1055
+Set color for each channel curve which is going to be displayed in video stream.
1056
+This is list of color names separated by space or by '|'.
1057
+Unrecognised or missing colors will be replaced by white color.
1058
+@end table
1059
+
1060
+@subsection Examples
1061
+
1062
+@itemize
1063
+@item
1064
+Lower gain by 10 of central frequency 200Hz and width 100 Hz
1065
+for first 2 channels using Chebyshev type 1 filter:
1066
+@example
1067
+anequalizer=c0 f=200 w=100 g=-10 t=1|c1 f=200 w=100 g=-10 t=1
1068
+@end example
1069
+@end itemize
1070
+
1071
+@subsection Commands
1072
+
1073
+This filter supports the following commands:
1074
+@table @option
1075
+@item change
1076
+Alter existing filter parameters.
1077
+Syntax for the commands is : "@var{fN}|f=@var{freq}|w=@var{width}|g=@var{gain}"
1078
+
1079
+@var{fN} is existing filter number, starting from 0, if no such filter is available
1080
+error is returned.
1081
+@var{freq} set new frequency parameter.
1082
+@var{width} set new width parameter in herz.
1083
+@var{gain} set new gain parameter in dB.
1084
+
1085
+Full filter invocation with asendcmd may look like this:
1086
+asendcmd=c='4.0 anequalizer change 0|f=200|w=50|g=1',anequalizer=...
1087
+@end table
1088
+
995 1089
 @section anull
996 1090
 
997 1091
 Pass the audio source unchanged to the output.
... ...
@@ -29,6 +29,7 @@ OBJS-$(CONFIG_ACROSSFADE_FILTER)             += af_afade.o
29 29
 OBJS-$(CONFIG_ADELAY_FILTER)                 += af_adelay.o
30 30
 OBJS-$(CONFIG_AECHO_FILTER)                  += af_aecho.o
31 31
 OBJS-$(CONFIG_AEMPHASIS_FILTER)              += af_aemphasis.o
32
+OBJS-$(CONFIG_ANEQUALIZER_FILTER)            += af_anequalizer.o
32 33
 OBJS-$(CONFIG_AEVAL_FILTER)                  += aeval.o
33 34
 OBJS-$(CONFIG_AFADE_FILTER)                  += af_afade.o
34 35
 OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
35 36
new file mode 100644
... ...
@@ -0,0 +1,751 @@
0
+/*
1
+ * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
2
+ * Copyright (c) 2015 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 <complex.h>
22
+
23
+#include "libavutil/intreadwrite.h"
24
+#include "libavutil/avstring.h"
25
+#include "libavutil/opt.h"
26
+#include "libavutil/parseutils.h"
27
+#include "avfilter.h"
28
+#include "internal.h"
29
+#include "audio.h"
30
+
31
+#define FILTER_ORDER 4
32
+
33
+enum FilterType {
34
+    BUTTERWORTH,
35
+    CHEBYSHEV1,
36
+    CHEBYSHEV2,
37
+    NB_TYPES
38
+};
39
+
40
+typedef struct FoSection {
41
+    double a0, a1, a2, a3, a4;
42
+    double b0, b1, b2, b3, b4;
43
+
44
+    double num[4];
45
+    double denum[4];
46
+} FoSection;
47
+
48
+typedef struct EqualizatorFilter {
49
+    int ignore;
50
+    int channel;
51
+    int type;
52
+
53
+    double freq;
54
+    double gain;
55
+    double width;
56
+
57
+    FoSection section[2];
58
+} EqualizatorFilter;
59
+
60
+typedef struct AudioNEqualizerContext {
61
+    const AVClass *class;
62
+    char *args;
63
+    char *colors;
64
+    int draw_curves;
65
+    int w, h;
66
+
67
+    double mag;
68
+    int fscale;
69
+    int nb_filters;
70
+    int nb_allocated;
71
+    EqualizatorFilter *filters;
72
+    AVFrame *video;
73
+} AudioNEqualizerContext;
74
+
75
+#define OFFSET(x) offsetof(AudioNEqualizerContext, x)
76
+#define A AV_OPT_FLAG_AUDIO_PARAM
77
+#define V AV_OPT_FLAG_VIDEO_PARAM
78
+#define F AV_OPT_FLAG_FILTERING_PARAM
79
+
80
+static const AVOption anequalizer_options[] = {
81
+    { "params", NULL,                             OFFSET(args),        AV_OPT_TYPE_STRING,     {.str=""}, 0, 0, A|F },
82
+    { "curves", "draw frequency response curves", OFFSET(draw_curves), AV_OPT_TYPE_BOOL,       {.i64=0}, 0, 1, V|F },
83
+    { "size",   "set video size",                 OFFSET(w),           AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, V|F },
84
+    { "mgain",  "set max gain",                   OFFSET(mag),         AV_OPT_TYPE_DOUBLE,     {.dbl=60}, -900, 900, V|F },
85
+    { "fscale", "set frequency scale",            OFFSET(fscale),      AV_OPT_TYPE_INT,        {.i64=1}, 0, 1, V|F, "fscale" },
86
+        { "lin",  "linear",                       0,                   AV_OPT_TYPE_CONST,      {.i64=0}, 0, 0, V|F, "fscale" },
87
+        { "log",  "logarithmic",                  0,                   AV_OPT_TYPE_CONST,      {.i64=1}, 0, 0, V|F, "fscale" },
88
+    { "colors", "set channels curves colors",     OFFSET(colors),      AV_OPT_TYPE_STRING,     {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, V|F },
89
+    { NULL }
90
+};
91
+
92
+AVFILTER_DEFINE_CLASS(anequalizer);
93
+
94
+static void draw_curves(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *out)
95
+{
96
+    AudioNEqualizerContext *s = ctx->priv;
97
+    char *colors, *color, *saveptr = NULL;
98
+    int ch, i, n;
99
+
100
+    colors = av_strdup(s->colors);
101
+    if (!colors)
102
+        return;
103
+
104
+    memset(out->data[0], 0, s->h * out->linesize[0]);
105
+
106
+    for (ch = 0; ch < inlink->channels; ch++) {
107
+        uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
108
+        int prev_v = -1;
109
+        double f;
110
+
111
+        color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
112
+        if (color)
113
+            av_parse_color(fg, color, -1, ctx);
114
+
115
+        for (f = 0; f < s->w; f++) {
116
+            double complex z;
117
+            double complex H = 1;
118
+            double w;
119
+            int v, y, x;
120
+
121
+            w = M_PI * (s->fscale ? pow(s->w - 1, f / s->w) : f) / (s->w - 1);
122
+            z = 1. / cexp(I * w);
123
+
124
+            for (n = 0; n < s->nb_filters; n++) {
125
+                if (s->filters[n].channel != ch ||
126
+                    s->filters[n].ignore)
127
+                    continue;
128
+
129
+                for (i = 0; i < FILTER_ORDER / 2; i++) {
130
+                    FoSection *S = &s->filters[n].section[i];
131
+
132
+                    H *= (((((S->b4 * z + S->b3) * z + S->b2) * z + S->b1) * z + S->b0) /
133
+                          ((((S->a4 * z + S->a3) * z + S->a2) * z + S->a1) * z + S->a0));
134
+                }
135
+            }
136
+
137
+            v = av_clip((1. + -20 * log10(cabs(H)) / s->mag) * s->h / 2, 0, s->h - 1);
138
+            x = lrint(f);
139
+            if (prev_v == -1)
140
+                prev_v = v;
141
+            if (v <= prev_v) {
142
+                for (y = v; y <= prev_v; y++)
143
+                    AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
144
+            } else {
145
+                for (y = prev_v; y <= v; y++)
146
+                    AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
147
+            }
148
+
149
+            prev_v = v;
150
+        }
151
+    }
152
+
153
+    av_free(colors);
154
+}
155
+
156
+static int config_video(AVFilterLink *outlink)
157
+{
158
+    AVFilterContext *ctx = outlink->src;
159
+    AudioNEqualizerContext *s = ctx->priv;
160
+    AVFilterLink *inlink = ctx->inputs[0];
161
+    AVFrame *out;
162
+
163
+    outlink->w = s->w;
164
+    outlink->h = s->h;
165
+
166
+    av_frame_free(&s->video);
167
+    s->video = out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
168
+    if (!out)
169
+        return AVERROR(ENOMEM);
170
+    outlink->sample_aspect_ratio = (AVRational){1,1};
171
+
172
+    draw_curves(ctx, inlink, out);
173
+
174
+    return 0;
175
+}
176
+
177
+static av_cold int init(AVFilterContext *ctx)
178
+{
179
+    AudioNEqualizerContext *s = ctx->priv;
180
+    AVFilterPad pad, vpad;
181
+
182
+    pad = (AVFilterPad){
183
+        .name         = av_strdup("out0"),
184
+        .type         = AVMEDIA_TYPE_AUDIO,
185
+    };
186
+
187
+    if (!pad.name)
188
+        return AVERROR(ENOMEM);
189
+
190
+    if (s->draw_curves) {
191
+        vpad = (AVFilterPad){
192
+            .name         = av_strdup("out1"),
193
+            .type         = AVMEDIA_TYPE_VIDEO,
194
+            .config_props = config_video,
195
+        };
196
+        if (!vpad.name)
197
+            return AVERROR(ENOMEM);
198
+    }
199
+
200
+    ff_insert_outpad(ctx, 0, &pad);
201
+
202
+    if (s->draw_curves)
203
+        ff_insert_outpad(ctx, 1, &vpad);
204
+
205
+    return 0;
206
+}
207
+
208
+static int query_formats(AVFilterContext *ctx)
209
+{
210
+    AVFilterLink *inlink = ctx->inputs[0];
211
+    AVFilterLink *outlink = ctx->outputs[0];
212
+    AudioNEqualizerContext *s = ctx->priv;
213
+    AVFilterFormats *formats;
214
+    AVFilterChannelLayouts *layouts;
215
+    static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
216
+    static const enum AVSampleFormat sample_fmts[] = {
217
+        AV_SAMPLE_FMT_DBLP,
218
+        AV_SAMPLE_FMT_NONE
219
+    };
220
+    int ret;
221
+
222
+    if (s->draw_curves) {
223
+        AVFilterLink *videolink = ctx->outputs[1];
224
+        formats = ff_make_format_list(pix_fmts);
225
+        if ((ret = ff_formats_ref(formats, &videolink->in_formats)) < 0)
226
+            return ret;
227
+    }
228
+
229
+    formats = ff_make_format_list(sample_fmts);
230
+    if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0 ||
231
+        (ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
232
+        return ret;
233
+
234
+    layouts = ff_all_channel_counts();
235
+    if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0 ||
236
+        (ret = ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts)) < 0)
237
+        return ret;
238
+
239
+    formats = ff_all_samplerates();
240
+    if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0 ||
241
+        (ret = ff_formats_ref(formats, &outlink->in_samplerates)) < 0)
242
+        return ret;
243
+
244
+    return 0;
245
+}
246
+
247
+static av_cold void uninit(AVFilterContext *ctx)
248
+{
249
+    AudioNEqualizerContext *s = ctx->priv;
250
+
251
+    av_freep(&ctx->output_pads[0].name);
252
+    if (s->draw_curves)
253
+        av_freep(&ctx->output_pads[1].name);
254
+    av_frame_free(&s->video);
255
+    av_freep(&s->filters);
256
+    s->nb_filters = 0;
257
+    s->nb_allocated = 0;
258
+}
259
+
260
+static void butterworth_fo_section(FoSection *S, double beta,
261
+                                   double si, double g, double g0,
262
+                                   double D, double c0)
263
+{
264
+    if (c0 == 1 || c0 == -1) {
265
+        S->b0 = (g*g*beta*beta + 2*g*g0*si*beta + g0*g0)/D;
266
+        S->b1 = 2*c0*(g*g*beta*beta - g0*g0)/D;
267
+        S->b2 = (g*g*beta*beta - 2*g0*g*beta*si + g0*g0)/D;
268
+        S->b3 = 0;
269
+        S->b4 = 0;
270
+
271
+        S->a0 = 1;
272
+        S->a1 = 2*c0*(beta*beta - 1)/D;
273
+        S->a2 = (beta*beta - 2*beta*si + 1)/D;
274
+        S->a3 = 0;
275
+        S->a4 = 0;
276
+    } else {
277
+        S->b0 = (g*g*beta*beta + 2*g*g0*si*beta + g0*g0)/D;
278
+        S->b1 = -4*c0*(g0*g0 + g*g0*si*beta)/D;
279
+        S->b2 = 2*(g0*g0*(1 + 2*c0*c0) - g*g*beta*beta)/D;
280
+        S->b3 = -4*c0*(g0*g0 - g*g0*si*beta)/D;
281
+        S->b4 = (g*g*beta*beta - 2*g*g0*si*beta + g0*g0)/D;
282
+
283
+        S->a0 = 1;
284
+        S->a1 = -4*c0*(1 + si*beta)/D;
285
+        S->a2 = 2*(1 + 2*c0*c0 - beta*beta)/D;
286
+        S->a3 = -4*c0*(1 - si*beta)/D;
287
+        S->a4 = (beta*beta - 2*si*beta + 1)/D;
288
+    }
289
+}
290
+
291
+static void butterworth_bp_filter(EqualizatorFilter *f,
292
+                                  int N, double w0, double wb,
293
+                                  double G, double Gb, double G0)
294
+{
295
+    double g, c0, g0, beta;
296
+    double epsilon;
297
+    int r =  N % 2;
298
+    int L = (N - r) / 2;
299
+    int i;
300
+
301
+    if (G == 0 && G0 == 0) {
302
+        f->section[0].a0 = 1;
303
+        f->section[0].b0 = 1;
304
+        f->section[1].a0 = 1;
305
+        f->section[1].b0 = 1;
306
+        return;
307
+    }
308
+
309
+    G  = exp10( G/20);
310
+    Gb = exp10(Gb/20);
311
+    G0 = exp10(G0/20);
312
+
313
+    epsilon = sqrt((G * G - Gb * Gb) / (Gb * Gb - G0 * G0));
314
+    g  = pow(G,  1.0 / N);
315
+    g0 = pow(G0, 1.0 / N);
316
+    beta = pow(epsilon, -1.0 / N) * tan(wb/2);
317
+    c0 = cos(w0);
318
+
319
+    for (i = 1; i <= L; i++) {
320
+        double ui = (2.0 * i - 1) / N;
321
+        double si = sin(M_PI * ui / 2.0);
322
+        double Di = beta * beta + 2 * si * beta + 1;
323
+
324
+        butterworth_fo_section(&f->section[i - 1], beta, si, g, g0, Di, c0);
325
+    }
326
+}
327
+
328
+static void chebyshev1_fo_section(FoSection *S, double a,
329
+                                  double c, double tetta_b,
330
+                                  double g0, double si, double b,
331
+                                  double D, double c0)
332
+{
333
+    if (c0 == 1 || c0 == -1) {
334
+        S->b0 = (tetta_b*tetta_b*(b*b+g0*g0*c*c) + 2*g0*b*si*tetta_b*tetta_b + g0*g0)/D;
335
+        S->b1 = 2*c0*(tetta_b*tetta_b*(b*b+g0*g0*c*c) - g0*g0)/D;
336
+        S->b2 = (tetta_b*tetta_b*(b*b+g0*g0*c*c) - 2*g0*b*si*tetta_b + g0*g0)/D;
337
+        S->b3 = 0;
338
+        S->b4 = 0;
339
+
340
+        S->a0 = 1;
341
+        S->a1 = 2*c0*(tetta_b*tetta_b*(a*a+c*c) - 1)/D;
342
+        S->a2 = (tetta_b*tetta_b*(a*a+c*c) - 2*a*si*tetta_b + 1)/D;
343
+        S->a3 = 0;
344
+        S->a4 = 0;
345
+    } else {
346
+        S->b0 = ((b*b + g0*g0*c*c)*tetta_b*tetta_b + 2*g0*b*si*tetta_b + g0*g0)/D;
347
+        S->b1 = -4*c0*(g0*g0 + g0*b*si*tetta_b)/D;
348
+        S->b2 = 2*(g0*g0*(1 + 2*c0*c0) - (b*b + g0*g0*c*c)*tetta_b*tetta_b)/D;
349
+        S->b3 = -4*c0*(g0*g0 - g0*b*si*tetta_b)/D;
350
+        S->b4 = ((b*b + g0*g0*c*c)*tetta_b*tetta_b - 2*g0*b*si*tetta_b + g0*g0)/D;
351
+
352
+        S->a0 = 1;
353
+        S->a1 = -4*c0*(1 + a*si*tetta_b)/D;
354
+        S->a2 = 2*(1 + 2*c0*c0 - (a*a + c*c)*tetta_b*tetta_b)/D;
355
+        S->a3 = -4*c0*(1 - a*si*tetta_b)/D;
356
+        S->a4 = ((a*a + c*c)*tetta_b*tetta_b - 2*a*si*tetta_b + 1)/D;
357
+    }
358
+}
359
+
360
+static void chebyshev1_bp_filter(EqualizatorFilter *f,
361
+                                 int N, double w0, double wb,
362
+                                 double G, double Gb, double G0)
363
+{
364
+    double a, b, c0, g0, alfa, beta, tetta_b;
365
+    double epsilon;
366
+    int r =  N % 2;
367
+    int L = (N - r) / 2;
368
+    int i;
369
+
370
+    if (G == 0 && G0 == 0) {
371
+        f->section[0].a0 = 1;
372
+        f->section[0].b0 = 1;
373
+        f->section[1].a0 = 1;
374
+        f->section[1].b0 = 1;
375
+        return;
376
+    }
377
+
378
+    G  = exp10( G/20);
379
+    Gb = exp10(Gb/20);
380
+    G0 = exp10(G0/20);
381
+
382
+    epsilon = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0));
383
+    g0 = pow(G0,1.0/N);
384
+    alfa = pow(1.0/epsilon    + sqrt(1 + pow(epsilon,-2.0)), 1.0/N);
385
+    beta = pow(G/epsilon + Gb * sqrt(1 + pow(epsilon,-2.0)), 1.0/N);
386
+    a = 0.5 * (alfa - 1.0/alfa);
387
+    b = 0.5 * (beta - g0*g0*(1/beta));
388
+    tetta_b = tan(wb/2);
389
+    c0 = cos(w0);
390
+
391
+    for (i = 1; i <= L; i++) {
392
+        double ui = (2.0*i-1.0)/N;
393
+        double ci = cos(M_PI*ui/2.0);
394
+        double si = sin(M_PI*ui/2.0);
395
+        double Di = (a*a + ci*ci)*tetta_b*tetta_b + 2.0*a*si*tetta_b + 1;
396
+
397
+        chebyshev1_fo_section(&f->section[i - 1], a, ci, tetta_b, g0, si, b, Di, c0);
398
+    }
399
+}
400
+
401
+static void chebyshev2_fo_section(FoSection *S, double a,
402
+                                  double c, double tetta_b,
403
+                                  double g, double si, double b,
404
+                                  double D, double c0)
405
+{
406
+    if (c0 == 1 || c0 == -1) {
407
+        S->b0 = (g*g*tetta_b*tetta_b + 2*tetta_b*g*b*si + b*b + g*g*c*c)/D;
408
+        S->b1 = 2*c0*(g*g*tetta_b*tetta_b - b*b - g*g*c*c)/D;
409
+        S->b2 = (g*g*tetta_b*tetta_b - 2*tetta_b*g*b*si + b*b + g*g*c*c)/D;
410
+        S->b3 = 0;
411
+        S->b4 = 0;
412
+
413
+        S->a0 = 1;
414
+        S->a1 = 2*c0*(tetta_b*tetta_b - a*a - c*c)/D;
415
+        S->a2 = (tetta_b*tetta_b - 2*tetta_b*a*si + a*a + c*c)/D;
416
+        S->a3 = 0;
417
+        S->a4 = 0;
418
+    } else {
419
+        S->b0 = (g*g*tetta_b*tetta_b + 2*g*b*si*tetta_b + b*b + g*g*c*c)/D;
420
+        S->b1 = -4*c0*(b*b + g*g*c*c + g*b*si*tetta_b)/D;
421
+        S->b2 = 2*((b*b + g*g*c*c)*(1 + 2*c0*c0) - g*g*tetta_b*tetta_b)/D;
422
+        S->b3 = -4*c0*(b*b + g*g*c*c - g*b*si*tetta_b)/D;
423
+        S->b4 = (g*g*tetta_b*tetta_b - 2*g*b*si*tetta_b + b*b + g*g*c*c)/D;
424
+
425
+        S->a0 = 1;
426
+        S->a1 = -4*c0*(a*a + c*c + a*si*tetta_b)/D;
427
+        S->a2 = 2*((a*a + c*c)*(1 + 2*c0*c0) - tetta_b*tetta_b)/D;
428
+        S->a3 = -4*c0*(a*a + c*c - a*si*tetta_b)/D;
429
+        S->a4 = (tetta_b*tetta_b - 2*a*si*tetta_b + a*a + c*c)/D;
430
+    }
431
+}
432
+
433
+static void chebyshev2_bp_filter(EqualizatorFilter *f,
434
+                                 int N, double w0, double wb,
435
+                                 double G, double Gb, double G0)
436
+{
437
+    double a, b, c0, tetta_b;
438
+    double epsilon, g, eu, ew;
439
+    int r =  N % 2;
440
+    int L = (N - r) / 2;
441
+    int i;
442
+
443
+    if (G == 0 && G0 == 0) {
444
+        f->section[0].a0 = 1;
445
+        f->section[0].b0 = 1;
446
+        f->section[1].a0 = 1;
447
+        f->section[1].b0 = 1;
448
+        return;
449
+    }
450
+
451
+    G  = exp10( G/20);
452
+    Gb = exp10(Gb/20);
453
+    G0 = exp10(G0/20);
454
+
455
+    epsilon = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0));
456
+    g  = pow(G, 1.0 / N);
457
+    eu = pow(epsilon + sqrt(1 + epsilon*epsilon), 1.0/N);
458
+    ew = pow(G0*epsilon + Gb*sqrt(1 + epsilon*epsilon), 1.0/N);
459
+    a = (eu - 1.0/eu)/2.0;
460
+    b = (ew - g*g/ew)/2.0;
461
+    tetta_b = tan(wb/2);
462
+    c0 = cos(w0);
463
+
464
+    for (i = 1; i <= L; i++) {
465
+        double ui = (2.0 * i - 1.0)/N;
466
+        double ci = cos(M_PI * ui / 2.0);
467
+        double si = sin(M_PI * ui / 2.0);
468
+        double Di = tetta_b*tetta_b + 2*a*si*tetta_b + a*a + ci*ci;
469
+
470
+        chebyshev2_fo_section(&f->section[i - 1], a, ci, tetta_b, g, si, b, Di, c0);
471
+    }
472
+}
473
+
474
+static double butterworth_compute_bw_gain_db(double gain)
475
+{
476
+    double bw_gain = 0;
477
+
478
+    if (gain <= -6)
479
+        bw_gain = gain + 3;
480
+    else if(gain > -6 && gain < 6)
481
+        bw_gain = gain * 0.5;
482
+    else if(gain >= 6)
483
+        bw_gain = gain - 3;
484
+
485
+    return bw_gain;
486
+}
487
+
488
+static double chebyshev1_compute_bw_gain_db(double gain)
489
+{
490
+    double bw_gain = 0;
491
+
492
+    if (gain <= -6)
493
+        bw_gain = gain + 1;
494
+    else if(gain > -6 && gain < 6)
495
+        bw_gain = gain * 0.9;
496
+    else if(gain >= 6)
497
+        bw_gain = gain - 1;
498
+
499
+    return bw_gain;
500
+}
501
+
502
+static double chebyshev2_compute_bw_gain_db(double gain)
503
+{
504
+    double bw_gain = 0;
505
+
506
+    if (gain <= -6)
507
+        bw_gain = -3;
508
+    else if(gain > -6 && gain < 6)
509
+        bw_gain = gain * 0.3;
510
+    else if(gain >= 6)
511
+        bw_gain = 3;
512
+
513
+    return bw_gain;
514
+}
515
+
516
+static inline double hz_2_rad(double x, double fs)
517
+{
518
+    return 2 * M_PI * x / fs;
519
+}
520
+
521
+static void equalizer(EqualizatorFilter *f, double sample_rate)
522
+{
523
+    double w0 = hz_2_rad(f->freq,  sample_rate);
524
+    double wb = hz_2_rad(f->width, sample_rate);
525
+    double bw_gain;
526
+
527
+    switch (f->type) {
528
+    case BUTTERWORTH:
529
+        bw_gain = butterworth_compute_bw_gain_db(f->gain);
530
+        butterworth_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0);
531
+        break;
532
+    case CHEBYSHEV1:
533
+        bw_gain = chebyshev1_compute_bw_gain_db(f->gain);
534
+        chebyshev1_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0);
535
+        break;
536
+    case CHEBYSHEV2:
537
+        bw_gain = chebyshev2_compute_bw_gain_db(f->gain);
538
+        chebyshev2_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0);
539
+        break;
540
+    }
541
+
542
+}
543
+
544
+static int add_filter(AudioNEqualizerContext *s, AVFilterLink *inlink)
545
+{
546
+    equalizer(&s->filters[s->nb_filters], inlink->sample_rate);
547
+    if (s->nb_filters >= s->nb_allocated) {
548
+        EqualizatorFilter *filters;
549
+
550
+        filters = av_calloc(s->nb_allocated, 2 * sizeof(*s->filters));
551
+        if (!filters)
552
+            return AVERROR(ENOMEM);
553
+        memcpy(filters, s->filters, sizeof(*s->filters) * s->nb_allocated);
554
+        av_free(s->filters);
555
+        s->filters = filters;
556
+        s->nb_allocated *= 2;
557
+    }
558
+    s->nb_filters++;
559
+
560
+    return 0;
561
+}
562
+
563
+static int config_input(AVFilterLink *inlink)
564
+{
565
+    AVFilterContext *ctx = inlink->dst;
566
+    AudioNEqualizerContext *s = ctx->priv;
567
+    char *args = av_strdup(s->args);
568
+    char *saveptr = NULL;
569
+    int ret = 0;
570
+
571
+    if (!args)
572
+        return AVERROR(ENOMEM);
573
+
574
+    s->nb_allocated = 32 * inlink->channels;
575
+    s->filters = av_calloc(inlink->channels, 32 * sizeof(*s->filters));
576
+    if (!s->filters) {
577
+        s->nb_allocated = 0;
578
+        return AVERROR(ENOMEM);
579
+    }
580
+
581
+    while (1) {
582
+        char *arg = av_strtok(s->nb_filters == 0 ? args : NULL, "|", &saveptr);
583
+
584
+        if (!arg)
585
+            break;
586
+
587
+        s->filters[s->nb_filters].type = 0;
588
+        if (sscanf(arg, "c%d f=%lf w=%lf g=%lf t=%d", &s->filters[s->nb_filters].channel,
589
+                                                     &s->filters[s->nb_filters].freq,
590
+                                                     &s->filters[s->nb_filters].width,
591
+                                                     &s->filters[s->nb_filters].gain,
592
+                                                     &s->filters[s->nb_filters].type) != 5 &&
593
+            sscanf(arg, "c%d f=%lf w=%lf g=%lf", &s->filters[s->nb_filters].channel,
594
+                                                &s->filters[s->nb_filters].freq,
595
+                                                &s->filters[s->nb_filters].width,
596
+                                                &s->filters[s->nb_filters].gain) != 4 ) {
597
+            av_free(args);
598
+            return AVERROR(EINVAL);
599
+        }
600
+
601
+        if (s->filters[s->nb_filters].freq < 0 ||
602
+            s->filters[s->nb_filters].freq > inlink->sample_rate / 2)
603
+            s->filters[s->nb_filters].ignore = 1;
604
+
605
+        if (s->filters[s->nb_filters].channel < 0 ||
606
+            s->filters[s->nb_filters].channel >= inlink->channels)
607
+            s->filters[s->nb_filters].ignore = 1;
608
+
609
+        av_clip(s->filters[s->nb_filters].type, 0, NB_TYPES - 1);
610
+        ret = add_filter(s, inlink);
611
+        if (ret < 0)
612
+            break;
613
+    }
614
+
615
+    av_free(args);
616
+
617
+    return ret;
618
+}
619
+
620
+static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
621
+                           char *res, int res_len, int flags)
622
+{
623
+    AudioNEqualizerContext *s = ctx->priv;
624
+    AVFilterLink *inlink = ctx->inputs[0];
625
+    int ret = AVERROR(ENOSYS);
626
+
627
+    if (!strcmp(cmd, "change")) {
628
+        double freq, width, gain;
629
+        int filter;
630
+
631
+        if (sscanf(args, "%d|f=%lf|w=%lf|g=%lf", &filter, &freq, &width, &gain) != 4)
632
+            return AVERROR(EINVAL);
633
+
634
+        if (filter < 0 || filter >= s->nb_filters)
635
+            return AVERROR(EINVAL);
636
+
637
+        if (freq < 0 || freq > inlink->sample_rate / 2)
638
+            return AVERROR(EINVAL);
639
+
640
+        s->filters[filter].freq  = freq;
641
+        s->filters[filter].width = width;
642
+        s->filters[filter].gain  = gain;
643
+        equalizer(&s->filters[filter], inlink->sample_rate);
644
+        if (s->draw_curves)
645
+            draw_curves(ctx, inlink, s->video);
646
+
647
+        ret = 0;
648
+    }
649
+
650
+    return ret;
651
+}
652
+
653
+static inline double section_process(FoSection *S, double in)
654
+{
655
+    double out;
656
+
657
+    out = S->b0 * in;
658
+    out+= S->b1 * S->num[0] - S->denum[0] * S->a1;
659
+    out+= S->b2 * S->num[1] - S->denum[1] * S->a2;
660
+    out+= S->b3 * S->num[2] - S->denum[2] * S->a3;
661
+    out+= S->b4 * S->num[3] - S->denum[3] * S->a4;
662
+
663
+    S->num[3] = S->num[2];
664
+    S->num[2] = S->num[1];
665
+    S->num[1] = S->num[0];
666
+    S->num[0] = in;
667
+
668
+    S->denum[3] = S->denum[2];
669
+    S->denum[2] = S->denum[1];
670
+    S->denum[1] = S->denum[0];
671
+    S->denum[0] = out;
672
+
673
+    return out;
674
+}
675
+
676
+static double process_sample(FoSection *s1, double in)
677
+{
678
+    double p0 = in, p1;
679
+    int i;
680
+
681
+    for (i = 0; i < FILTER_ORDER / 2; i++) {
682
+        p1 = section_process(&s1[i], p0);
683
+        p0 = p1;
684
+    }
685
+
686
+    return p1;
687
+}
688
+
689
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
690
+{
691
+    AVFilterContext *ctx = inlink->dst;
692
+    AudioNEqualizerContext *s = ctx->priv;
693
+    AVFilterLink *outlink = ctx->outputs[0];
694
+    double *bptr;
695
+    int i, n;
696
+
697
+    for (i = 0; i < s->nb_filters; i++) {
698
+        EqualizatorFilter *f = &s->filters[i];
699
+
700
+        if (f->gain == 0. || f->ignore)
701
+            continue;
702
+
703
+        bptr = (double *)buf->extended_data[f->channel];
704
+        for (n = 0; n < buf->nb_samples; n++) {
705
+            double sample = bptr[n];
706
+
707
+            sample  = process_sample(f->section, sample);
708
+            bptr[n] = sample;
709
+        }
710
+    }
711
+
712
+    if (s->draw_curves) {
713
+        const int64_t pts = buf->pts +
714
+            av_rescale_q(buf->nb_samples, (AVRational){ 1, inlink->sample_rate },
715
+                         outlink->time_base);
716
+        int ret;
717
+
718
+        s->video->pts = pts;
719
+        ret = ff_filter_frame(ctx->outputs[1], av_frame_clone(s->video));
720
+        if (ret < 0)
721
+            return ret;
722
+    }
723
+
724
+    return ff_filter_frame(outlink, buf);
725
+}
726
+
727
+static const AVFilterPad inputs[] = {
728
+    {
729
+        .name           = "default",
730
+        .type           = AVMEDIA_TYPE_AUDIO,
731
+        .config_props   = config_input,
732
+        .filter_frame   = filter_frame,
733
+        .needs_writable = 1,
734
+    },
735
+    { NULL }
736
+};
737
+
738
+AVFilter ff_af_anequalizer = {
739
+    .name          = "anequalizer",
740
+    .description   = NULL_IF_CONFIG_SMALL("Apply high-order audio parametric multi band equalizer."),
741
+    .priv_size     = sizeof(AudioNEqualizerContext),
742
+    .priv_class    = &anequalizer_class,
743
+    .init          = init,
744
+    .uninit        = uninit,
745
+    .query_formats = query_formats,
746
+    .inputs        = inputs,
747
+    .outputs       = NULL,
748
+    .flags         = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
749
+    .process_command = process_command,
750
+};
... ...
@@ -59,6 +59,7 @@ void avfilter_register_all(void)
59 59
     REGISTER_FILTER(ALLPASS,        allpass,        af);
60 60
     REGISTER_FILTER(AMERGE,         amerge,         af);
61 61
     REGISTER_FILTER(AMIX,           amix,           af);
62
+    REGISTER_FILTER(ANEQUALIZER,    anequalizer,    af);
62 63
     REGISTER_FILTER(ANULL,          anull,          af);
63 64
     REGISTER_FILTER(APAD,           apad,           af);
64 65
     REGISTER_FILTER(APERMS,         aperms,         af);
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  21
34
-#define LIBAVFILTER_VERSION_MICRO 101
33
+#define LIBAVFILTER_VERSION_MINOR  22
34
+#define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \