• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2002 Naoki Shibata
3  * Copyright (c) 2017 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/opt.h"
23 
24 #include "libavcodec/avfft.h"
25 
26 #include "audio.h"
27 #include "avfilter.h"
28 #include "filters.h"
29 #include "internal.h"
30 
31 #define NBANDS 17
32 #define M 15
33 
34 typedef struct EqParameter {
35     float lower, upper, gain;
36 } EqParameter;
37 
38 typedef struct SuperEqualizerContext {
39     const AVClass *class;
40 
41     EqParameter params[NBANDS + 1];
42 
43     float gains[NBANDS + 1];
44 
45     float fact[M + 1];
46     float aa;
47     float iza;
48     float *ires, *irest;
49     float *fsamples;
50     int winlen, tabsize;
51 
52     AVFrame *in, *out;
53     RDFTContext *rdft, *irdft;
54 } SuperEqualizerContext;
55 
56 static const float bands[] = {
57     65.406392, 92.498606, 130.81278, 184.99721, 261.62557, 369.99442, 523.25113, 739.9884, 1046.5023,
58     1479.9768, 2093.0045, 2959.9536, 4186.0091, 5919.9072, 8372.0181, 11839.814, 16744.036
59 };
60 
izero(SuperEqualizerContext * s,float x)61 static float izero(SuperEqualizerContext *s, float x)
62 {
63     float ret = 1;
64     int m;
65 
66     for (m = 1; m <= M; m++) {
67         float t;
68 
69         t = pow(x / 2, m) / s->fact[m];
70         ret += t*t;
71     }
72 
73     return ret;
74 }
75 
hn_lpf(int n,float f,float fs)76 static float hn_lpf(int n, float f, float fs)
77 {
78     float t = 1 / fs;
79     float omega = 2 * M_PI * f;
80 
81     if (n * omega * t == 0)
82         return 2 * f * t;
83     return 2 * f * t * sinf(n * omega * t) / (n * omega * t);
84 }
85 
hn_imp(int n)86 static float hn_imp(int n)
87 {
88     return n == 0 ? 1.f : 0.f;
89 }
90 
hn(int n,EqParameter * param,float fs)91 static float hn(int n, EqParameter *param, float fs)
92 {
93     float ret, lhn;
94     int i;
95 
96     lhn = hn_lpf(n, param[0].upper, fs);
97     ret = param[0].gain*lhn;
98 
99     for (i = 1; i < NBANDS + 1 && param[i].upper < fs / 2; i++) {
100         float lhn2 = hn_lpf(n, param[i].upper, fs);
101         ret += param[i].gain * (lhn2 - lhn);
102         lhn = lhn2;
103     }
104 
105     ret += param[i].gain * (hn_imp(n) - lhn);
106 
107     return ret;
108 }
109 
alpha(float a)110 static float alpha(float a)
111 {
112     if (a <= 21)
113         return 0;
114     if (a <= 50)
115         return .5842f * pow(a - 21, 0.4f) + 0.07886f * (a - 21);
116     return .1102f * (a - 8.7f);
117 }
118 
win(SuperEqualizerContext * s,float n,int N)119 static float win(SuperEqualizerContext *s, float n, int N)
120 {
121     return izero(s, alpha(s->aa) * sqrtf(1 - 4 * n * n / ((N - 1) * (N - 1)))) / s->iza;
122 }
123 
process_param(float * bc,EqParameter * param,float fs)124 static void process_param(float *bc, EqParameter *param, float fs)
125 {
126     int i;
127 
128     for (i = 0; i <= NBANDS; i++) {
129         param[i].lower = i == 0 ? 0 : bands[i - 1];
130         param[i].upper = i == NBANDS ? fs : bands[i];
131         param[i].gain  = bc[i];
132     }
133 }
134 
equ_init(SuperEqualizerContext * s,int wb)135 static int equ_init(SuperEqualizerContext *s, int wb)
136 {
137     int i,j;
138 
139     s->rdft  = av_rdft_init(wb, DFT_R2C);
140     s->irdft = av_rdft_init(wb, IDFT_C2R);
141     if (!s->rdft || !s->irdft)
142         return AVERROR(ENOMEM);
143 
144     s->aa = 96;
145     s->winlen = (1 << (wb-1))-1;
146     s->tabsize  = 1 << wb;
147 
148     s->ires     = av_calloc(s->tabsize, sizeof(float));
149     s->irest    = av_calloc(s->tabsize, sizeof(float));
150     s->fsamples = av_calloc(s->tabsize, sizeof(float));
151 
152     for (i = 0; i <= M; i++) {
153         s->fact[i] = 1;
154         for (j = 1; j <= i; j++)
155             s->fact[i] *= j;
156     }
157 
158     s->iza = izero(s, alpha(s->aa));
159 
160     return 0;
161 }
162 
make_fir(SuperEqualizerContext * s,float * lbc,float * rbc,EqParameter * param,float fs)163 static void make_fir(SuperEqualizerContext *s, float *lbc, float *rbc, EqParameter *param, float fs)
164 {
165     const int winlen = s->winlen;
166     const int tabsize = s->tabsize;
167     float *nires;
168     int i;
169 
170     if (fs <= 0)
171         return;
172 
173     process_param(lbc, param, fs);
174     for (i = 0; i < winlen; i++)
175         s->irest[i] = hn(i - winlen / 2, param, fs) * win(s, i - winlen / 2, winlen);
176     for (; i < tabsize; i++)
177         s->irest[i] = 0;
178 
179     av_rdft_calc(s->rdft, s->irest);
180     nires = s->ires;
181     for (i = 0; i < tabsize; i++)
182         nires[i] = s->irest[i];
183 }
184 
filter_frame(AVFilterLink * inlink,AVFrame * in)185 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
186 {
187     AVFilterContext *ctx = inlink->dst;
188     SuperEqualizerContext *s = ctx->priv;
189     AVFilterLink *outlink = ctx->outputs[0];
190     const float *ires = s->ires;
191     float *fsamples = s->fsamples;
192     int ch, i;
193 
194     AVFrame *out = ff_get_audio_buffer(outlink, s->winlen);
195     float *src, *dst, *ptr;
196 
197     if (!out) {
198         av_frame_free(&in);
199         return AVERROR(ENOMEM);
200     }
201 
202     for (ch = 0; ch < in->channels; ch++) {
203         ptr = (float *)out->extended_data[ch];
204         dst = (float *)s->out->extended_data[ch];
205         src = (float *)in->extended_data[ch];
206 
207         for (i = 0; i < in->nb_samples; i++)
208             fsamples[i] = src[i];
209         for (; i < s->tabsize; i++)
210             fsamples[i] = 0;
211 
212         av_rdft_calc(s->rdft, fsamples);
213 
214         fsamples[0] = ires[0] * fsamples[0];
215         fsamples[1] = ires[1] * fsamples[1];
216         for (i = 1; i < s->tabsize / 2; i++) {
217             float re, im;
218 
219             re = ires[i*2  ] * fsamples[i*2] - ires[i*2+1] * fsamples[i*2+1];
220             im = ires[i*2+1] * fsamples[i*2] + ires[i*2  ] * fsamples[i*2+1];
221 
222             fsamples[i*2  ] = re;
223             fsamples[i*2+1] = im;
224         }
225 
226         av_rdft_calc(s->irdft, fsamples);
227 
228         for (i = 0; i < s->winlen; i++)
229             dst[i] += fsamples[i] / s->tabsize * 2;
230         for (i = s->winlen; i < s->tabsize; i++)
231             dst[i]  = fsamples[i] / s->tabsize * 2;
232         for (i = 0; i < s->winlen; i++)
233             ptr[i] = dst[i];
234         for (i = 0; i < s->winlen; i++)
235             dst[i] = dst[i+s->winlen];
236     }
237 
238     out->pts = in->pts;
239     av_frame_free(&in);
240 
241     return ff_filter_frame(outlink, out);
242 }
243 
activate(AVFilterContext * ctx)244 static int activate(AVFilterContext *ctx)
245 {
246     AVFilterLink *inlink = ctx->inputs[0];
247     AVFilterLink *outlink = ctx->outputs[0];
248     SuperEqualizerContext *s = ctx->priv;
249     AVFrame *in = NULL;
250     int ret;
251 
252     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
253 
254     ret = ff_inlink_consume_samples(inlink, s->winlen, s->winlen, &in);
255     if (ret < 0)
256         return ret;
257     if (ret > 0)
258         return filter_frame(inlink, in);
259 
260     FF_FILTER_FORWARD_STATUS(inlink, outlink);
261     FF_FILTER_FORWARD_WANTED(outlink, inlink);
262 
263     return FFERROR_NOT_READY;
264 }
265 
init(AVFilterContext * ctx)266 static av_cold int init(AVFilterContext *ctx)
267 {
268     SuperEqualizerContext *s = ctx->priv;
269 
270     return equ_init(s, 14);
271 }
272 
query_formats(AVFilterContext * ctx)273 static int query_formats(AVFilterContext *ctx)
274 {
275     AVFilterFormats *formats;
276     AVFilterChannelLayouts *layouts;
277     static const enum AVSampleFormat sample_fmts[] = {
278         AV_SAMPLE_FMT_FLTP,
279         AV_SAMPLE_FMT_NONE
280     };
281     int ret;
282 
283     layouts = ff_all_channel_counts();
284     if (!layouts)
285         return AVERROR(ENOMEM);
286     ret = ff_set_common_channel_layouts(ctx, layouts);
287     if (ret < 0)
288         return ret;
289 
290     formats = ff_make_format_list(sample_fmts);
291     if ((ret = ff_set_common_formats(ctx, formats)) < 0)
292         return ret;
293 
294     formats = ff_all_samplerates();
295     return ff_set_common_samplerates(ctx, formats);
296 }
297 
config_input(AVFilterLink * inlink)298 static int config_input(AVFilterLink *inlink)
299 {
300     AVFilterContext *ctx = inlink->dst;
301     SuperEqualizerContext *s = ctx->priv;
302 
303     s->out = ff_get_audio_buffer(inlink, s->tabsize);
304     if (!s->out)
305         return AVERROR(ENOMEM);
306 
307     return 0;
308 }
309 
config_output(AVFilterLink * outlink)310 static int config_output(AVFilterLink *outlink)
311 {
312     AVFilterContext *ctx = outlink->src;
313     SuperEqualizerContext *s = ctx->priv;
314 
315     make_fir(s, s->gains, s->gains, s->params, outlink->sample_rate);
316 
317     return 0;
318 }
319 
uninit(AVFilterContext * ctx)320 static av_cold void uninit(AVFilterContext *ctx)
321 {
322     SuperEqualizerContext *s = ctx->priv;
323 
324     av_frame_free(&s->out);
325     av_freep(&s->irest);
326     av_freep(&s->ires);
327     av_freep(&s->fsamples);
328     av_rdft_end(s->rdft);
329     av_rdft_end(s->irdft);
330 }
331 
332 static const AVFilterPad superequalizer_inputs[] = {
333     {
334         .name         = "default",
335         .type         = AVMEDIA_TYPE_AUDIO,
336         .config_props = config_input,
337     },
338     { NULL }
339 };
340 
341 static const AVFilterPad superequalizer_outputs[] = {
342     {
343         .name         = "default",
344         .type         = AVMEDIA_TYPE_AUDIO,
345         .config_props = config_output,
346     },
347     { NULL }
348 };
349 
350 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
351 #define OFFSET(x) offsetof(SuperEqualizerContext, x)
352 
353 static const AVOption superequalizer_options[] = {
354     {  "1b", "set 65Hz band gain",    OFFSET(gains [0]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
355     {  "2b", "set 92Hz band gain",    OFFSET(gains [1]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
356     {  "3b", "set 131Hz band gain",   OFFSET(gains [2]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
357     {  "4b", "set 185Hz band gain",   OFFSET(gains [3]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
358     {  "5b", "set 262Hz band gain",   OFFSET(gains [4]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
359     {  "6b", "set 370Hz band gain",   OFFSET(gains [5]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
360     {  "7b", "set 523Hz band gain",   OFFSET(gains [6]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
361     {  "8b", "set 740Hz band gain",   OFFSET(gains [7]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
362     {  "9b", "set 1047Hz band gain",  OFFSET(gains [8]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
363     { "10b", "set 1480Hz band gain",  OFFSET(gains [9]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
364     { "11b", "set 2093Hz band gain",  OFFSET(gains[10]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
365     { "12b", "set 2960Hz band gain",  OFFSET(gains[11]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
366     { "13b", "set 4186Hz band gain",  OFFSET(gains[12]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
367     { "14b", "set 5920Hz band gain",  OFFSET(gains[13]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
368     { "15b", "set 8372Hz band gain",  OFFSET(gains[14]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
369     { "16b", "set 11840Hz band gain", OFFSET(gains[15]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
370     { "17b", "set 16744Hz band gain", OFFSET(gains[16]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
371     { "18b", "set 20000Hz band gain", OFFSET(gains[17]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
372     { NULL }
373 };
374 
375 AVFILTER_DEFINE_CLASS(superequalizer);
376 
377 AVFilter ff_af_superequalizer = {
378     .name          = "superequalizer",
379     .description   = NULL_IF_CONFIG_SMALL("Apply 18 band equalization filter."),
380     .priv_size     = sizeof(SuperEqualizerContext),
381     .priv_class    = &superequalizer_class,
382     .query_formats = query_formats,
383     .init          = init,
384     .activate      = activate,
385     .uninit        = uninit,
386     .inputs        = superequalizer_inputs,
387     .outputs       = superequalizer_outputs,
388 };
389