1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <float.h>
20 
21 #include "libavutil/opt.h"
22 #include "avfilter.h"
23 #include "audio.h"
24 #include "formats.h"
25 
26 typedef struct AudioDynamicEqualizerContext {
27     const AVClass *class;
28 
29     double threshold;
30     double dfrequency;
31     double dqfactor;
32     double tfrequency;
33     double tqfactor;
34     double ratio;
35     double range;
36     double makeup;
37     double knee;
38     double slew;
39     double attack;
40     double release;
41     double attack_coef;
42     double release_coef;
43     int mode;
44     int type;
45 
46     AVFrame *state;
47 } AudioDynamicEqualizerContext;
48 
config_input(AVFilterLink *inlink)49 static int config_input(AVFilterLink *inlink)
50 {
51     AVFilterContext *ctx = inlink->dst;
52     AudioDynamicEqualizerContext *s = ctx->priv;
53 
54     s->state = ff_get_audio_buffer(inlink, 8);
55     if (!s->state)
56         return AVERROR(ENOMEM);
57 
58     return 0;
59 }
60 
get_svf(double in, double *m, double *a, double *b)61 static double get_svf(double in, double *m, double *a, double *b)
62 {
63     const double v0 = in;
64     const double v3 = v0 - b[1];
65     const double v1 = a[0] * b[0] + a[1] * v3;
66     const double v2 = b[1] + a[1] * b[0] + a[2] * v3;
67 
68     b[0] = 2. * v1 - b[0];
69     b[1] = 2. * v2 - b[1];
70 
71     return m[0] * v0 + m[1] * v1 + m[2] * v2;
72 }
73 
from_dB(double x)74 static inline double from_dB(double x)
75 {
76     return exp(0.05 * x * M_LN10);
77 }
78 
to_dB(double x)79 static inline double to_dB(double x)
80 {
81     return 20. * log10(x);
82 }
83 
sqr(double x)84 static inline double sqr(double x)
85 {
86     return x * x;
87 }
88 
get_gain(double in, double srate, double makeup, double aattack, double iratio, double knee, double range, double thresdb, double slewfactor, double *state, double attack_coeff, double release_coeff, double nc)89 static double get_gain(double in, double srate, double makeup,
90                        double aattack, double iratio, double knee, double range,
91                        double thresdb, double slewfactor, double *state,
92                        double attack_coeff, double release_coeff, double nc)
93 {
94     double width = (6. * knee) + 0.01;
95     double cdb = 0.;
96     double Lgain = 1.;
97     double Lxg, Lxl, Lyg, Lyl, Ly1;
98     double checkwidth = 0.;
99     double slewwidth = 1.8;
100     int attslew = 0;
101 
102     Lyg = 0.;
103     Lxg = to_dB(fabs(in) + DBL_EPSILON);
104 
105     Lyg = Lxg + (iratio - 1.) * sqr(Lxg - thresdb + width * .5) / (2. * width);
106 
107     checkwidth = 2. * fabs(Lxg - thresdb);
108     if (2. * (Lxg - thresdb) < -width) {
109         Lyg = Lxg;
110     } else if (checkwidth <= width) {
111         Lyg = thresdb + (Lxg - thresdb) * iratio;
112         if (checkwidth <= slewwidth) {
113             if (Lyg >= state[2])
114                 attslew = 1;
115         }
116     } else if (2. * (Lxg - thresdb) > width) {
117         Lyg = thresdb + (Lxg - thresdb) * iratio;
118     }
119 
120     attack_coeff = attslew ? aattack : attack_coeff;
121 
122     Lxl = Lxg - Lyg;
123 
124     Ly1 = fmax(Lxl, release_coeff * state[1] +(1. - release_coeff) * Lxl);
125     Lyl = attack_coeff * state[0] + (1. - attack_coeff) * Ly1;
126 
127     cdb = -Lyl;
128     Lgain = from_dB(nc * fmin(cdb - makeup, range));
129 
130     state[0] = Lyl;
131     state[1] = Ly1;
132     state[2] = Lyg;
133 
134     return Lgain;
135 }
136 
137 typedef struct ThreadData {
138     AVFrame *in, *out;
139 } ThreadData;
140 
filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)141 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
142 {
143     AudioDynamicEqualizerContext *s = ctx->priv;
144     ThreadData *td = arg;
145     AVFrame *in = td->in;
146     AVFrame *out = td->out;
147     const double sample_rate = in->sample_rate;
148     const double makeup = s->makeup;
149     const double iratio = 1. / s->ratio;
150     const double range = s->range;
151     const double dfrequency = fmin(s->dfrequency, sample_rate * 0.5);
152     const double tfrequency = fmin(s->tfrequency, sample_rate * 0.5);
153     const double threshold = to_dB(s->threshold + DBL_EPSILON);
154     const double release = s->release_coef;
155     const double attack = s->attack_coef;
156     const double dqfactor = s->dqfactor;
157     const double tqfactor = s->tqfactor;
158     const double fg = tan(M_PI * tfrequency / sample_rate);
159     const double dg = tan(M_PI * dfrequency / sample_rate);
160     const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs;
161     const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
162     const int mode = s->mode;
163     const int type = s->type;
164     const double knee = s->knee;
165     const double slew = s->slew;
166     const double aattack = exp(-1000. / ((s->attack + 2.0 * (slew - 1.)) * sample_rate));
167     const double nc = mode == 0 ? 1. : -1.;
168     double da[3], dm[3];
169 
170     {
171         double k = 1. / dqfactor;
172 
173         da[0] = 1. / (1. + dg * (dg + k));
174         da[1] = dg * da[0];
175         da[2] = dg * da[1];
176 
177         dm[0] = 0.;
178         dm[1] = 1.;
179         dm[2] = 0.;
180     }
181 
182     for (int ch = start; ch < end; ch++) {
183         const double *src = (const double *)in->extended_data[ch];
184         double *dst = (double *)out->extended_data[ch];
185         double *state = (double *)s->state->extended_data[ch];
186 
187         for (int n = 0; n < out->nb_samples; n++) {
188             double detect, gain, v, listen;
189             double fa[3], fm[3];
190             double k, g;
191 
192             detect = listen = get_svf(src[n], dm, da, state);
193             detect = fabs(detect);
194 
195             gain = get_gain(detect, sample_rate, makeup,
196                             aattack, iratio, knee, range, threshold, slew,
197                             &state[4], attack, release, nc);
198 
199             switch (type) {
200             case 0:
201                 k = 1. / (tqfactor * gain);
202 
203                 fa[0] = 1. / (1. + fg * (fg + k));
204                 fa[1] = fg * fa[0];
205                 fa[2] = fg * fa[1];
206 
207                 fm[0] = 1.;
208                 fm[1] = k * (gain * gain - 1.);
209                 fm[2] = 0.;
210                 break;
211             case 1:
212                 k = 1. / tqfactor;
213                 g = fg / sqrt(gain);
214 
215                 fa[0] = 1. / (1. + g * (g + k));
216                 fa[1] = g * fa[0];
217                 fa[2] = g * fa[1];
218 
219                 fm[0] = 1.;
220                 fm[1] = k * (gain - 1.);
221                 fm[2] = gain * gain - 1.;
222                 break;
223             case 2:
224                 k = 1. / tqfactor;
225                 g = fg / sqrt(gain);
226 
227                 fa[0] = 1. / (1. + g * (g + k));
228                 fa[1] = g * fa[0];
229                 fa[2] = g * fa[1];
230 
231                 fm[0] = gain * gain;
232                 fm[1] = k * (1. - gain) * gain;
233                 fm[2] = 1. - gain * gain;
234                 break;
235             }
236 
237             v = get_svf(src[n], fm, fa, &state[2]);
238             v = mode == -1 ? listen : v;
239             dst[n] = ctx->is_disabled ? src[n] : v;
240         }
241     }
242 
243     return 0;
244 }
245 
get_coef(double x, double sr)246 static double get_coef(double x, double sr)
247 {
248     return exp(-1000. / (x * sr));
249 }
250 
filter_frame(AVFilterLink *inlink, AVFrame *in)251 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
252 {
253     AVFilterContext *ctx = inlink->dst;
254     AVFilterLink *outlink = ctx->outputs[0];
255     AudioDynamicEqualizerContext *s = ctx->priv;
256     ThreadData td;
257     AVFrame *out;
258 
259     if (av_frame_is_writable(in)) {
260         out = in;
261     } else {
262         out = ff_get_audio_buffer(outlink, in->nb_samples);
263         if (!out) {
264             av_frame_free(&in);
265             return AVERROR(ENOMEM);
266         }
267         av_frame_copy_props(out, in);
268     }
269 
270     s->attack_coef = get_coef(s->attack, in->sample_rate);
271     s->release_coef = get_coef(s->release, in->sample_rate);
272 
273     td.in = in;
274     td.out = out;
275     ff_filter_execute(ctx, filter_channels, &td, NULL,
276                      FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
277 
278     if (out != in)
279         av_frame_free(&in);
280     return ff_filter_frame(outlink, out);
281 }
282 
uninit(AVFilterContext *ctx)283 static av_cold void uninit(AVFilterContext *ctx)
284 {
285     AudioDynamicEqualizerContext *s = ctx->priv;
286 
287     av_frame_free(&s->state);
288 }
289 
290 #define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x)
291 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
292 
293 static const AVOption adynamicequalizer_options[] = {
294     { "threshold",  "set detection threshold", OFFSET(threshold),  AV_OPT_TYPE_DOUBLE, {.dbl=0},        0, 100,     FLAGS },
295     { "dfrequency", "set detection frequency", OFFSET(dfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000},     2, 1000000, FLAGS },
296     { "dqfactor",   "set detection Q factor",  OFFSET(dqfactor),   AV_OPT_TYPE_DOUBLE, {.dbl=1},    0.001, 1000,    FLAGS },
297     { "tfrequency", "set target frequency",    OFFSET(tfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000},     2, 1000000, FLAGS },
298     { "tqfactor",   "set target Q factor",     OFFSET(tqfactor),   AV_OPT_TYPE_DOUBLE, {.dbl=1},    0.001, 1000,    FLAGS },
299     { "attack",     "set attack duration",     OFFSET(attack),     AV_OPT_TYPE_DOUBLE, {.dbl=20},       1, 2000,    FLAGS },
300     { "release",    "set release duration",    OFFSET(release),    AV_OPT_TYPE_DOUBLE, {.dbl=200},      1, 2000,    FLAGS },
301     { "knee",       "set knee factor",         OFFSET(knee),       AV_OPT_TYPE_DOUBLE, {.dbl=1},        0, 8,       FLAGS },
302     { "ratio",      "set ratio factor",        OFFSET(ratio),      AV_OPT_TYPE_DOUBLE, {.dbl=1},        1, 20,      FLAGS },
303     { "makeup",     "set makeup gain",         OFFSET(makeup),     AV_OPT_TYPE_DOUBLE, {.dbl=0},        0, 30,      FLAGS },
304     { "range",      "set max gain",            OFFSET(range),      AV_OPT_TYPE_DOUBLE, {.dbl=0},        0, 200,     FLAGS },
305     { "slew",       "set slew factor",         OFFSET(slew),       AV_OPT_TYPE_DOUBLE, {.dbl=1},        1, 200,     FLAGS },
306     { "mode",       "set mode",                OFFSET(mode),       AV_OPT_TYPE_INT,    {.i64=0},       -1, 1,       FLAGS, "mode" },
307     {   "listen",   0,                         0,                  AV_OPT_TYPE_CONST,  {.i64=-1},       0, 0,       FLAGS, "mode" },
308     {   "cut",      0,                         0,                  AV_OPT_TYPE_CONST,  {.i64=0},        0, 0,       FLAGS, "mode" },
309     {   "boost",    0,                         0,                  AV_OPT_TYPE_CONST,  {.i64=1},        0, 0,       FLAGS, "mode" },
310     { "tftype",     "set target filter type",  OFFSET(type),       AV_OPT_TYPE_INT,    {.i64=0},        0, 2,       FLAGS, "type" },
311     {   "bell",     0,                         0,                  AV_OPT_TYPE_CONST,  {.i64=0},        0, 0,       FLAGS, "type" },
312     {   "lowshelf", 0,                         0,                  AV_OPT_TYPE_CONST,  {.i64=1},        0, 0,       FLAGS, "type" },
313     {   "highshelf",0,                         0,                  AV_OPT_TYPE_CONST,  {.i64=2},        0, 0,       FLAGS, "type" },
314     { NULL }
315 };
316 
317 AVFILTER_DEFINE_CLASS(adynamicequalizer);
318 
319 static const AVFilterPad inputs[] = {
320     {
321         .name         = "default",
322         .type         = AVMEDIA_TYPE_AUDIO,
323         .filter_frame = filter_frame,
324         .config_props = config_input,
325     },
326 };
327 
328 static const AVFilterPad outputs[] = {
329     {
330         .name = "default",
331         .type = AVMEDIA_TYPE_AUDIO,
332     },
333 };
334 
335 const AVFilter ff_af_adynamicequalizer = {
336     .name            = "adynamicequalizer",
337     .description     = NULL_IF_CONFIG_SMALL("Apply Dynamic Equalization of input audio."),
338     .priv_size       = sizeof(AudioDynamicEqualizerContext),
339     .priv_class      = &adynamicequalizer_class,
340     .uninit          = uninit,
341     FILTER_INPUTS(inputs),
342     FILTER_OUTPUTS(outputs),
343     FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),
344     .flags           = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
345                        AVFILTER_FLAG_SLICE_THREADS,
346     .process_command = ff_filter_process_command,
347 };
348