FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_superequalizer.c
Date: 2024-04-12 08:31:17
Exec Total Coverage
Lines: 0 147 0.0%
Functions: 0 15 0.0%
Branches: 0 72 0.0%

Line Branch Exec Source
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/mem.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/tx.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, *fsamples_out;
50 int winlen, tabsize;
51
52 AVFrame *in, *out;
53 AVTXContext *rdft, *irdft;
54 av_tx_fn tx_fn, itx_fn;
55 } SuperEqualizerContext;
56
57 static const float bands[] = {
58 65.406392, 92.498606, 130.81278, 184.99721, 261.62557, 369.99442, 523.25113, 739.9884, 1046.5023,
59 1479.9768, 2093.0045, 2959.9536, 4186.0091, 5919.9072, 8372.0181, 11839.814, 16744.036
60 };
61
62 static float izero(SuperEqualizerContext *s, float x)
63 {
64 float ret = 1;
65 int m;
66
67 for (m = 1; m <= M; m++) {
68 float t;
69
70 t = pow(x / 2, m) / s->fact[m];
71 ret += t*t;
72 }
73
74 return ret;
75 }
76
77 static float hn_lpf(int n, float f, float fs)
78 {
79 float t = 1 / fs;
80 float omega = 2 * M_PI * f;
81
82 if (n * omega * t == 0)
83 return 2 * f * t;
84 return 2 * f * t * sinf(n * omega * t) / (n * omega * t);
85 }
86
87 static float hn_imp(int n)
88 {
89 return n == 0 ? 1.f : 0.f;
90 }
91
92 static float hn(int n, EqParameter *param, float fs)
93 {
94 float ret, lhn;
95 int i;
96
97 lhn = hn_lpf(n, param[0].upper, fs);
98 ret = param[0].gain*lhn;
99
100 for (i = 1; i < NBANDS + 1 && param[i].upper < fs / 2; i++) {
101 float lhn2 = hn_lpf(n, param[i].upper, fs);
102 ret += param[i].gain * (lhn2 - lhn);
103 lhn = lhn2;
104 }
105
106 ret += param[i].gain * (hn_imp(n) - lhn);
107
108 return ret;
109 }
110
111 static float alpha(float a)
112 {
113 if (a <= 21)
114 return 0;
115 if (a <= 50)
116 return .5842f * pow(a - 21, 0.4f) + 0.07886f * (a - 21);
117 return .1102f * (a - 8.7f);
118 }
119
120 static float win(SuperEqualizerContext *s, float n, int N)
121 {
122 return izero(s, alpha(s->aa) * sqrtf(1 - 4 * n * n / ((N - 1) * (N - 1)))) / s->iza;
123 }
124
125 static void process_param(float *bc, EqParameter *param, float fs)
126 {
127 int i;
128
129 for (i = 0; i <= NBANDS; i++) {
130 param[i].lower = i == 0 ? 0 : bands[i - 1];
131 param[i].upper = i == NBANDS ? fs : bands[i];
132 param[i].gain = bc[i];
133 }
134 }
135
136 static int equ_init(SuperEqualizerContext *s, int wb)
137 {
138 float scale = 1.f, iscale = 1.f;
139 int i, j, ret;
140
141 ret = av_tx_init(&s->rdft, &s->tx_fn, AV_TX_FLOAT_RDFT, 0, 1 << wb, &scale, 0);
142 if (ret < 0)
143 return ret;
144
145 ret = av_tx_init(&s->irdft, &s->itx_fn, AV_TX_FLOAT_RDFT, 1, 1 << wb, &iscale, 0);
146 if (ret < 0)
147 return ret;
148
149 s->aa = 96;
150 s->winlen = (1 << (wb-1))-1;
151 s->tabsize = 1 << wb;
152
153 s->ires = av_calloc(s->tabsize + 2, sizeof(float));
154 s->irest = av_calloc(s->tabsize, sizeof(float));
155 s->fsamples = av_calloc(s->tabsize, sizeof(float));
156 s->fsamples_out = av_calloc(s->tabsize + 2, sizeof(float));
157 if (!s->ires || !s->irest || !s->fsamples || !s->fsamples_out)
158 return AVERROR(ENOMEM);
159
160 for (i = 0; i <= M; i++) {
161 s->fact[i] = 1;
162 for (j = 1; j <= i; j++)
163 s->fact[i] *= j;
164 }
165
166 s->iza = izero(s, alpha(s->aa));
167
168 return 0;
169 }
170
171 static void make_fir(SuperEqualizerContext *s, float *lbc, float *rbc, EqParameter *param, float fs)
172 {
173 const int winlen = s->winlen;
174 const int tabsize = s->tabsize;
175 int i;
176
177 if (fs <= 0)
178 return;
179
180 process_param(lbc, param, fs);
181 for (i = 0; i < winlen; i++)
182 s->irest[i] = hn(i - winlen / 2, param, fs) * win(s, i - winlen / 2, winlen);
183 for (; i < tabsize; i++)
184 s->irest[i] = 0;
185
186 s->tx_fn(s->rdft, s->ires, s->irest, sizeof(float));
187 }
188
189 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
190 {
191 AVFilterContext *ctx = inlink->dst;
192 SuperEqualizerContext *s = ctx->priv;
193 AVFilterLink *outlink = ctx->outputs[0];
194 const float *ires = s->ires;
195 float *fsamples_out = s->fsamples_out;
196 float *fsamples = s->fsamples;
197 int ch, i;
198
199 AVFrame *out = ff_get_audio_buffer(outlink, in->nb_samples);
200 float *src, *dst, *ptr;
201
202 if (!out) {
203 av_frame_free(&in);
204 return AVERROR(ENOMEM);
205 }
206
207 for (ch = 0; ch < in->ch_layout.nb_channels; ch++) {
208 ptr = (float *)out->extended_data[ch];
209 dst = (float *)s->out->extended_data[ch];
210 src = (float *)in->extended_data[ch];
211
212 for (i = 0; i < in->nb_samples; i++)
213 fsamples[i] = src[i];
214 for (; i < s->tabsize; i++)
215 fsamples[i] = 0;
216
217 s->tx_fn(s->rdft, fsamples_out, fsamples, sizeof(float));
218
219 for (i = 0; i <= s->tabsize / 2; i++) {
220 float re, im;
221
222 re = ires[i*2 ] * fsamples_out[i*2] - ires[i*2+1] * fsamples_out[i*2+1];
223 im = ires[i*2+1] * fsamples_out[i*2] + ires[i*2 ] * fsamples_out[i*2+1];
224
225 fsamples_out[i*2 ] = re;
226 fsamples_out[i*2+1] = im;
227 }
228
229 s->itx_fn(s->irdft, fsamples, fsamples_out, sizeof(AVComplexFloat));
230
231 for (i = 0; i < s->winlen; i++)
232 dst[i] += fsamples[i] / s->tabsize;
233 for (i = s->winlen; i < s->tabsize; i++)
234 dst[i] = fsamples[i] / s->tabsize;
235 for (i = 0; i < out->nb_samples; i++)
236 ptr[i] = dst[i];
237 for (i = 0; i < s->winlen; i++)
238 dst[i] = dst[i+s->winlen];
239 }
240
241 out->pts = in->pts;
242 av_frame_free(&in);
243
244 return ff_filter_frame(outlink, out);
245 }
246
247 static int activate(AVFilterContext *ctx)
248 {
249 AVFilterLink *inlink = ctx->inputs[0];
250 AVFilterLink *outlink = ctx->outputs[0];
251 SuperEqualizerContext *s = ctx->priv;
252 AVFrame *in = NULL;
253 int ret;
254
255 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
256
257 ret = ff_inlink_consume_samples(inlink, s->winlen, s->winlen, &in);
258 if (ret < 0)
259 return ret;
260 if (ret > 0)
261 return filter_frame(inlink, in);
262
263 FF_FILTER_FORWARD_STATUS(inlink, outlink);
264 FF_FILTER_FORWARD_WANTED(outlink, inlink);
265
266 return FFERROR_NOT_READY;
267 }
268
269 static av_cold int init(AVFilterContext *ctx)
270 {
271 SuperEqualizerContext *s = ctx->priv;
272
273 return equ_init(s, 14);
274 }
275
276 static int config_input(AVFilterLink *inlink)
277 {
278 AVFilterContext *ctx = inlink->dst;
279 SuperEqualizerContext *s = ctx->priv;
280
281 s->out = ff_get_audio_buffer(inlink, s->tabsize);
282 if (!s->out)
283 return AVERROR(ENOMEM);
284
285 return 0;
286 }
287
288 static int config_output(AVFilterLink *outlink)
289 {
290 AVFilterContext *ctx = outlink->src;
291 SuperEqualizerContext *s = ctx->priv;
292
293 make_fir(s, s->gains, s->gains, s->params, outlink->sample_rate);
294
295 return 0;
296 }
297
298 static av_cold void uninit(AVFilterContext *ctx)
299 {
300 SuperEqualizerContext *s = ctx->priv;
301
302 av_frame_free(&s->out);
303 av_freep(&s->irest);
304 av_freep(&s->ires);
305 av_freep(&s->fsamples);
306 av_freep(&s->fsamples_out);
307 av_tx_uninit(&s->rdft);
308 av_tx_uninit(&s->irdft);
309 }
310
311 static const AVFilterPad superequalizer_inputs[] = {
312 {
313 .name = "default",
314 .type = AVMEDIA_TYPE_AUDIO,
315 .config_props = config_input,
316 },
317 };
318
319 static const AVFilterPad superequalizer_outputs[] = {
320 {
321 .name = "default",
322 .type = AVMEDIA_TYPE_AUDIO,
323 .config_props = config_output,
324 },
325 };
326
327 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
328 #define OFFSET(x) offsetof(SuperEqualizerContext, x)
329
330 static const AVOption superequalizer_options[] = {
331 { "1b", "set 65Hz band gain", OFFSET(gains [0]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
332 { "2b", "set 92Hz band gain", OFFSET(gains [1]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
333 { "3b", "set 131Hz band gain", OFFSET(gains [2]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
334 { "4b", "set 185Hz band gain", OFFSET(gains [3]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
335 { "5b", "set 262Hz band gain", OFFSET(gains [4]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
336 { "6b", "set 370Hz band gain", OFFSET(gains [5]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
337 { "7b", "set 523Hz band gain", OFFSET(gains [6]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
338 { "8b", "set 740Hz band gain", OFFSET(gains [7]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
339 { "9b", "set 1047Hz band gain", OFFSET(gains [8]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
340 { "10b", "set 1480Hz band gain", OFFSET(gains [9]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
341 { "11b", "set 2093Hz band gain", OFFSET(gains[10]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
342 { "12b", "set 2960Hz band gain", OFFSET(gains[11]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
343 { "13b", "set 4186Hz band gain", OFFSET(gains[12]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
344 { "14b", "set 5920Hz band gain", OFFSET(gains[13]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
345 { "15b", "set 8372Hz band gain", OFFSET(gains[14]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
346 { "16b", "set 11840Hz band gain", OFFSET(gains[15]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
347 { "17b", "set 16744Hz band gain", OFFSET(gains[16]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
348 { "18b", "set 20000Hz band gain", OFFSET(gains[17]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
349 { NULL }
350 };
351
352 AVFILTER_DEFINE_CLASS(superequalizer);
353
354 const AVFilter ff_af_superequalizer = {
355 .name = "superequalizer",
356 .description = NULL_IF_CONFIG_SMALL("Apply 18 band equalization filter."),
357 .priv_size = sizeof(SuperEqualizerContext),
358 .priv_class = &superequalizer_class,
359 .init = init,
360 .activate = activate,
361 .uninit = uninit,
362 FILTER_INPUTS(superequalizer_inputs),
363 FILTER_OUTPUTS(superequalizer_outputs),
364 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP),
365 };
366