Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2016 Muhammad Faiz <mfcc64@gmail.com> | ||
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 "libavutil/channel_layout.h" | ||
22 | #include "libavutil/file_open.h" | ||
23 | #include "libavutil/mem.h" | ||
24 | #include "libavutil/opt.h" | ||
25 | #include "libavutil/eval.h" | ||
26 | #include "libavutil/avassert.h" | ||
27 | #include "libavutil/tx.h" | ||
28 | #include "avfilter.h" | ||
29 | #include "filters.h" | ||
30 | #include "audio.h" | ||
31 | |||
32 | #define RDFT_BITS_MIN 4 | ||
33 | #define RDFT_BITS_MAX 16 | ||
34 | |||
35 | enum WindowFunc { | ||
36 | WFUNC_RECTANGULAR, | ||
37 | WFUNC_HANN, | ||
38 | WFUNC_HAMMING, | ||
39 | WFUNC_BLACKMAN, | ||
40 | WFUNC_NUTTALL3, | ||
41 | WFUNC_MNUTTALL3, | ||
42 | WFUNC_NUTTALL, | ||
43 | WFUNC_BNUTTALL, | ||
44 | WFUNC_BHARRIS, | ||
45 | WFUNC_TUKEY, | ||
46 | NB_WFUNC | ||
47 | }; | ||
48 | |||
49 | enum Scale { | ||
50 | SCALE_LINLIN, | ||
51 | SCALE_LINLOG, | ||
52 | SCALE_LOGLIN, | ||
53 | SCALE_LOGLOG, | ||
54 | NB_SCALE | ||
55 | }; | ||
56 | |||
57 | #define NB_GAIN_ENTRY_MAX 4096 | ||
58 | typedef struct GainEntry { | ||
59 | double freq; | ||
60 | double gain; | ||
61 | } GainEntry; | ||
62 | |||
63 | typedef struct OverlapIndex { | ||
64 | int buf_idx; | ||
65 | int overlap_idx; | ||
66 | } OverlapIndex; | ||
67 | |||
68 | typedef struct FIREqualizerContext { | ||
69 | const AVClass *class; | ||
70 | |||
71 | AVTXContext *analysis_rdft; | ||
72 | av_tx_fn analysis_rdft_fn; | ||
73 | AVTXContext *analysis_irdft; | ||
74 | av_tx_fn analysis_irdft_fn; | ||
75 | AVTXContext *rdft; | ||
76 | av_tx_fn rdft_fn; | ||
77 | AVTXContext *irdft; | ||
78 | av_tx_fn irdft_fn; | ||
79 | AVTXContext *fft_ctx; | ||
80 | av_tx_fn fft_fn; | ||
81 | AVTXContext *cepstrum_rdft; | ||
82 | av_tx_fn cepstrum_rdft_fn; | ||
83 | AVTXContext *cepstrum_irdft; | ||
84 | av_tx_fn cepstrum_irdft_fn; | ||
85 | int analysis_rdft_len; | ||
86 | int rdft_len; | ||
87 | int cepstrum_len; | ||
88 | |||
89 | float *analysis_buf; | ||
90 | float *analysis_tbuf; | ||
91 | float *dump_buf; | ||
92 | float *kernel_tmp_buf; | ||
93 | float *kernel_tmp_tbuf; | ||
94 | float *kernel_buf; | ||
95 | float *tx_buf; | ||
96 | float *cepstrum_buf; | ||
97 | float *cepstrum_tbuf; | ||
98 | float *conv_buf; | ||
99 | OverlapIndex *conv_idx; | ||
100 | int fir_len; | ||
101 | int nsamples_max; | ||
102 | int64_t next_pts; | ||
103 | int frame_nsamples_max; | ||
104 | int remaining; | ||
105 | |||
106 | char *gain_cmd; | ||
107 | char *gain_entry_cmd; | ||
108 | const char *gain; | ||
109 | const char *gain_entry; | ||
110 | double delay; | ||
111 | double accuracy; | ||
112 | int wfunc; | ||
113 | int fixed; | ||
114 | int multi; | ||
115 | int zero_phase; | ||
116 | int scale; | ||
117 | char *dumpfile; | ||
118 | int dumpscale; | ||
119 | int fft2; | ||
120 | int min_phase; | ||
121 | |||
122 | int nb_gain_entry; | ||
123 | int gain_entry_err; | ||
124 | GainEntry gain_entry_tbl[NB_GAIN_ENTRY_MAX]; | ||
125 | } FIREqualizerContext; | ||
126 | |||
127 | #define OFFSET(x) offsetof(FIREqualizerContext, x) | ||
128 | #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
129 | #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM | ||
130 | |||
131 | static const AVOption firequalizer_options[] = { | ||
132 | { "gain", "set gain curve", OFFSET(gain), AV_OPT_TYPE_STRING, { .str = "gain_interpolate(f)" }, 0, 0, TFLAGS }, | ||
133 | { "gain_entry", "set gain entry", OFFSET(gain_entry), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, TFLAGS }, | ||
134 | { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.0, 1e10, FLAGS }, | ||
135 | { "accuracy", "set accuracy", OFFSET(accuracy), AV_OPT_TYPE_DOUBLE, { .dbl = 5.0 }, 0.0, 1e10, FLAGS }, | ||
136 | { "wfunc", "set window function", OFFSET(wfunc), AV_OPT_TYPE_INT, { .i64 = WFUNC_HANN }, 0, NB_WFUNC-1, FLAGS, .unit = "wfunc" }, | ||
137 | { "rectangular", "rectangular window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_RECTANGULAR }, 0, 0, FLAGS, .unit = "wfunc" }, | ||
138 | { "hann", "hann window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HANN }, 0, 0, FLAGS, .unit = "wfunc" }, | ||
139 | { "hamming", "hamming window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HAMMING }, 0, 0, FLAGS, .unit = "wfunc" }, | ||
140 | { "blackman", "blackman window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BLACKMAN }, 0, 0, FLAGS, .unit = "wfunc" }, | ||
141 | { "nuttall3", "3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL3 }, 0, 0, FLAGS, .unit = "wfunc" }, | ||
142 | { "mnuttall3", "minimum 3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_MNUTTALL3 }, 0, 0, FLAGS, .unit = "wfunc" }, | ||
143 | { "nuttall", "nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL }, 0, 0, FLAGS, .unit = "wfunc" }, | ||
144 | { "bnuttall", "blackman-nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BNUTTALL }, 0, 0, FLAGS, .unit = "wfunc" }, | ||
145 | { "bharris", "blackman-harris window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BHARRIS }, 0, 0, FLAGS, .unit = "wfunc" }, | ||
146 | { "tukey", "tukey window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_TUKEY }, 0, 0, FLAGS, .unit = "wfunc" }, | ||
147 | { "fixed", "set fixed frame samples", OFFSET(fixed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, | ||
148 | { "multi", "set multi channels mode", OFFSET(multi), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, | ||
149 | { "zero_phase", "set zero phase mode", OFFSET(zero_phase), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, | ||
150 | { "scale", "set gain scale", OFFSET(scale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, .unit = "scale" }, | ||
151 | { "linlin", "linear-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLIN }, 0, 0, FLAGS, .unit = "scale" }, | ||
152 | { "linlog", "linear-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLOG }, 0, 0, FLAGS, .unit = "scale" }, | ||
153 | { "loglin", "logarithmic-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLIN }, 0, 0, FLAGS, .unit = "scale" }, | ||
154 | { "loglog", "logarithmic-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLOG }, 0, 0, FLAGS, .unit = "scale" }, | ||
155 | { "dumpfile", "set dump file", OFFSET(dumpfile), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, | ||
156 | { "dumpscale", "set dump scale", OFFSET(dumpscale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, .unit = "scale" }, | ||
157 | { "fft2", "set 2-channels fft", OFFSET(fft2), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, | ||
158 | { "min_phase", "set minimum phase mode", OFFSET(min_phase), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, | ||
159 | { NULL } | ||
160 | }; | ||
161 | |||
162 | AVFILTER_DEFINE_CLASS(firequalizer); | ||
163 | |||
164 | 15 | static void common_uninit(FIREqualizerContext *s) | |
165 | { | ||
166 | 15 | av_tx_uninit(&s->analysis_rdft); | |
167 | 15 | av_tx_uninit(&s->analysis_irdft); | |
168 | 15 | av_tx_uninit(&s->rdft); | |
169 | 15 | av_tx_uninit(&s->irdft); | |
170 | 15 | av_tx_uninit(&s->fft_ctx); | |
171 | 15 | av_tx_uninit(&s->cepstrum_rdft); | |
172 | 15 | av_tx_uninit(&s->cepstrum_irdft); | |
173 | 15 | s->analysis_rdft = s->analysis_irdft = s->rdft = s->irdft = NULL; | |
174 | 15 | s->fft_ctx = NULL; | |
175 | 15 | s->cepstrum_rdft = NULL; | |
176 | 15 | s->cepstrum_irdft = NULL; | |
177 | |||
178 | 15 | av_freep(&s->analysis_buf); | |
179 | 15 | av_freep(&s->analysis_tbuf); | |
180 | 15 | av_freep(&s->dump_buf); | |
181 | 15 | av_freep(&s->kernel_tmp_buf); | |
182 | 15 | av_freep(&s->kernel_tmp_tbuf); | |
183 | 15 | av_freep(&s->kernel_buf); | |
184 | 15 | av_freep(&s->tx_buf); | |
185 | 15 | av_freep(&s->cepstrum_buf); | |
186 | 15 | av_freep(&s->cepstrum_tbuf); | |
187 | 15 | av_freep(&s->conv_buf); | |
188 | 15 | av_freep(&s->conv_idx); | |
189 | 15 | } | |
190 | |||
191 | 10 | static av_cold void uninit(AVFilterContext *ctx) | |
192 | { | ||
193 | 10 | FIREqualizerContext *s = ctx->priv; | |
194 | |||
195 | 10 | common_uninit(s); | |
196 | 10 | av_freep(&s->gain_cmd); | |
197 | 10 | av_freep(&s->gain_entry_cmd); | |
198 | 10 | } | |
199 | |||
200 | 914 | static void fast_convolute(FIREqualizerContext *restrict s, const float *restrict kernel_buf, float *restrict conv_buf, | |
201 | OverlapIndex *restrict idx, float *restrict data, int nsamples) | ||
202 | { | ||
203 |
2/2✓ Branch 0 taken 760 times.
✓ Branch 1 taken 154 times.
|
914 | if (nsamples <= s->nsamples_max) { |
204 | 760 | float *buf = conv_buf + idx->buf_idx * s->rdft_len; | |
205 | 760 | float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx; | |
206 | 760 | float *tbuf = s->tx_buf; | |
207 | 760 | int center = s->fir_len/2; | |
208 | int k; | ||
209 | |||
210 | 760 | memset(buf, 0, center * sizeof(*data)); | |
211 | 760 | memcpy(buf + center, data, nsamples * sizeof(*data)); | |
212 | 760 | memset(buf + center + nsamples, 0, (s->rdft_len - nsamples - center) * sizeof(*data)); | |
213 | 760 | s->rdft_fn(s->rdft, tbuf, buf, sizeof(float)); | |
214 | |||
215 |
2/2✓ Branch 0 taken 1803000 times.
✓ Branch 1 taken 760 times.
|
1803760 | for (k = 0; k <= s->rdft_len/2; k++) { |
216 | 1803000 | tbuf[2*k] *= kernel_buf[k]; | |
217 | 1803000 | tbuf[2*k+1] *= kernel_buf[k]; | |
218 | } | ||
219 | |||
220 | 760 | s->irdft_fn(s->irdft, buf, tbuf, sizeof(AVComplexFloat)); | |
221 |
2/2✓ Branch 0 taken 1923254 times.
✓ Branch 1 taken 760 times.
|
1924014 | for (k = 0; k < s->rdft_len - idx->overlap_idx; k++) |
222 | 1923254 | buf[k] += obuf[k]; | |
223 | 760 | memcpy(data, buf, nsamples * sizeof(*data)); | |
224 | 760 | idx->buf_idx = !idx->buf_idx; | |
225 | 760 | idx->overlap_idx = nsamples; | |
226 | } else { | ||
227 |
2/2✓ Branch 0 taken 368 times.
✓ Branch 1 taken 154 times.
|
522 | while (nsamples > s->nsamples_max * 2) { |
228 | 368 | fast_convolute(s, kernel_buf, conv_buf, idx, data, s->nsamples_max); | |
229 | 368 | data += s->nsamples_max; | |
230 | 368 | nsamples -= s->nsamples_max; | |
231 | } | ||
232 | 154 | fast_convolute(s, kernel_buf, conv_buf, idx, data, nsamples/2); | |
233 | 154 | fast_convolute(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2); | |
234 | } | ||
235 | 914 | } | |
236 | |||
237 | ✗ | static void fast_convolute_nonlinear(FIREqualizerContext *restrict s, const float *restrict kernel_buf, | |
238 | float *restrict conv_buf, OverlapIndex *restrict idx, | ||
239 | float *restrict data, int nsamples) | ||
240 | { | ||
241 | ✗ | if (nsamples <= s->nsamples_max) { | |
242 | ✗ | float *buf = conv_buf + idx->buf_idx * s->rdft_len; | |
243 | ✗ | float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx; | |
244 | ✗ | float *tbuf = s->tx_buf; | |
245 | int k; | ||
246 | |||
247 | ✗ | memcpy(buf, data, nsamples * sizeof(*data)); | |
248 | ✗ | memset(buf + nsamples, 0, (s->rdft_len - nsamples) * sizeof(*data)); | |
249 | ✗ | s->rdft_fn(s->rdft, tbuf, buf, sizeof(float)); | |
250 | |||
251 | ✗ | for (k = 0; k < s->rdft_len + 2; k += 2) { | |
252 | float re, im; | ||
253 | ✗ | re = tbuf[k] * kernel_buf[k] - tbuf[k+1] * kernel_buf[k+1]; | |
254 | ✗ | im = tbuf[k] * kernel_buf[k+1] + tbuf[k+1] * kernel_buf[k]; | |
255 | ✗ | tbuf[k] = re; | |
256 | ✗ | tbuf[k+1] = im; | |
257 | } | ||
258 | |||
259 | ✗ | s->irdft_fn(s->irdft, buf, tbuf, sizeof(AVComplexFloat)); | |
260 | ✗ | for (k = 0; k < s->rdft_len - idx->overlap_idx; k++) | |
261 | ✗ | buf[k] += obuf[k]; | |
262 | ✗ | memcpy(data, buf, nsamples * sizeof(*data)); | |
263 | ✗ | idx->buf_idx = !idx->buf_idx; | |
264 | ✗ | idx->overlap_idx = nsamples; | |
265 | } else { | ||
266 | ✗ | while (nsamples > s->nsamples_max * 2) { | |
267 | ✗ | fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data, s->nsamples_max); | |
268 | ✗ | data += s->nsamples_max; | |
269 | ✗ | nsamples -= s->nsamples_max; | |
270 | } | ||
271 | ✗ | fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data, nsamples/2); | |
272 | ✗ | fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2); | |
273 | } | ||
274 | ✗ | } | |
275 | |||
276 | 494 | static void fast_convolute2(FIREqualizerContext *restrict s, const float *restrict kernel_buf, AVComplexFloat *restrict conv_buf, | |
277 | OverlapIndex *restrict idx, float *restrict data0, float *restrict data1, int nsamples) | ||
278 | { | ||
279 |
2/2✓ Branch 0 taken 391 times.
✓ Branch 1 taken 103 times.
|
494 | if (nsamples <= s->nsamples_max) { |
280 | 391 | AVComplexFloat *buf = conv_buf + idx->buf_idx * s->rdft_len; | |
281 | 391 | AVComplexFloat *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx; | |
282 | 391 | AVComplexFloat *tbuf = (AVComplexFloat *)s->tx_buf; | |
283 | 391 | int center = s->fir_len/2; | |
284 | int k; | ||
285 | float tmp; | ||
286 | |||
287 | 391 | memset(buf, 0, center * sizeof(*buf)); | |
288 |
2/2✓ Branch 0 taken 547722 times.
✓ Branch 1 taken 391 times.
|
548113 | for (k = 0; k < nsamples; k++) { |
289 | 547722 | buf[center+k].re = data0[k]; | |
290 | 547722 | buf[center+k].im = data1[k]; | |
291 | } | ||
292 | 391 | memset(buf + center + nsamples, 0, (s->rdft_len - nsamples - center) * sizeof(*buf)); | |
293 | 391 | s->fft_fn(s->fft_ctx, tbuf, buf, sizeof(AVComplexFloat)); | |
294 | |||
295 | /* swap re <-> im, do backward fft using forward fft_ctx */ | ||
296 | /* normalize with 0.5f */ | ||
297 | 391 | tmp = tbuf[0].re; | |
298 | 391 | tbuf[0].re = 0.5f * kernel_buf[0] * tbuf[0].im; | |
299 | 391 | tbuf[0].im = 0.5f * kernel_buf[0] * tmp; | |
300 |
2/2✓ Branch 0 taken 805497 times.
✓ Branch 1 taken 391 times.
|
805888 | for (k = 1; k < s->rdft_len/2; k++) { |
301 | 805497 | int m = s->rdft_len - k; | |
302 | 805497 | tmp = tbuf[k].re; | |
303 | 805497 | tbuf[k].re = 0.5f * kernel_buf[k] * tbuf[k].im; | |
304 | 805497 | tbuf[k].im = 0.5f * kernel_buf[k] * tmp; | |
305 | 805497 | tmp = tbuf[m].re; | |
306 | 805497 | tbuf[m].re = 0.5f * kernel_buf[k] * tbuf[m].im; | |
307 | 805497 | tbuf[m].im = 0.5f * kernel_buf[k] * tmp; | |
308 | } | ||
309 | 391 | tmp = tbuf[k].re; | |
310 | 391 | tbuf[k].re = 0.5f * kernel_buf[k] * tbuf[k].im; | |
311 | 391 | tbuf[k].im = 0.5f * kernel_buf[k] * tmp; | |
312 | |||
313 | 391 | s->fft_fn(s->fft_ctx, buf, tbuf, sizeof(AVComplexFloat)); | |
314 | |||
315 |
2/2✓ Branch 0 taken 1065250 times.
✓ Branch 1 taken 391 times.
|
1065641 | for (k = 0; k < s->rdft_len - idx->overlap_idx; k++) { |
316 | 1065250 | buf[k].re += obuf[k].re; | |
317 | 1065250 | buf[k].im += obuf[k].im; | |
318 | } | ||
319 | |||
320 | /* swapped re <-> im */ | ||
321 |
2/2✓ Branch 0 taken 547722 times.
✓ Branch 1 taken 391 times.
|
548113 | for (k = 0; k < nsamples; k++) { |
322 | 547722 | data0[k] = buf[k].im; | |
323 | 547722 | data1[k] = buf[k].re; | |
324 | } | ||
325 | 391 | idx->buf_idx = !idx->buf_idx; | |
326 | 391 | idx->overlap_idx = nsamples; | |
327 | } else { | ||
328 |
2/2✓ Branch 0 taken 182 times.
✓ Branch 1 taken 103 times.
|
285 | while (nsamples > s->nsamples_max * 2) { |
329 | 182 | fast_convolute2(s, kernel_buf, conv_buf, idx, data0, data1, s->nsamples_max); | |
330 | 182 | data0 += s->nsamples_max; | |
331 | 182 | data1 += s->nsamples_max; | |
332 | 182 | nsamples -= s->nsamples_max; | |
333 | } | ||
334 | 103 | fast_convolute2(s, kernel_buf, conv_buf, idx, data0, data1, nsamples/2); | |
335 | 103 | fast_convolute2(s, kernel_buf, conv_buf, idx, data0 + nsamples/2, data1 + nsamples/2, nsamples - nsamples/2); | |
336 | } | ||
337 | 494 | } | |
338 | |||
339 | ✗ | static void dump_fir(AVFilterContext *ctx, FILE *fp, int ch) | |
340 | { | ||
341 | ✗ | FIREqualizerContext *s = ctx->priv; | |
342 | ✗ | int rate = ctx->inputs[0]->sample_rate; | |
343 | ✗ | int xlog = s->dumpscale == SCALE_LOGLIN || s->dumpscale == SCALE_LOGLOG; | |
344 | ✗ | int ylog = s->dumpscale == SCALE_LINLOG || s->dumpscale == SCALE_LOGLOG; | |
345 | int x; | ||
346 | ✗ | int center = s->fir_len / 2; | |
347 | ✗ | double delay = s->zero_phase ? 0.0 : (double) center / rate; | |
348 | double vx, ya, yb; | ||
349 | |||
350 | ✗ | if (!s->min_phase) { | |
351 | ✗ | s->analysis_buf[0] *= s->rdft_len/2; | |
352 | ✗ | for (x = 1; x <= center; x++) { | |
353 | ✗ | s->analysis_buf[x] *= s->rdft_len/2; | |
354 | ✗ | s->analysis_buf[s->analysis_rdft_len - x] *= s->rdft_len/2; | |
355 | } | ||
356 | } else { | ||
357 | ✗ | for (x = 0; x < s->fir_len; x++) | |
358 | ✗ | s->analysis_buf[x] *= s->rdft_len/2; | |
359 | } | ||
360 | |||
361 | ✗ | if (ch) | |
362 | ✗ | fprintf(fp, "\n\n"); | |
363 | |||
364 | ✗ | fprintf(fp, "# time[%d] (time amplitude)\n", ch); | |
365 | |||
366 | ✗ | if (!s->min_phase) { | |
367 | ✗ | for (x = center; x > 0; x--) | |
368 | ✗ | fprintf(fp, "%15.10f %15.10f\n", delay - (double) x / rate, (double) s->analysis_buf[s->analysis_rdft_len - x]); | |
369 | |||
370 | ✗ | for (x = 0; x <= center; x++) | |
371 | ✗ | fprintf(fp, "%15.10f %15.10f\n", delay + (double)x / rate , (double) s->analysis_buf[x]); | |
372 | } else { | ||
373 | ✗ | for (x = 0; x < s->fir_len; x++) | |
374 | ✗ | fprintf(fp, "%15.10f %15.10f\n", (double)x / rate, (double) s->analysis_buf[x]); | |
375 | } | ||
376 | |||
377 | ✗ | s->analysis_rdft_fn(s->analysis_rdft, s->analysis_tbuf, s->analysis_buf, sizeof(float)); | |
378 | |||
379 | ✗ | fprintf(fp, "\n\n# freq[%d] (frequency desired_gain actual_gain)\n", ch); | |
380 | |||
381 | ✗ | for (x = 0; x <= s->analysis_rdft_len/2; x++) { | |
382 | ✗ | int i = 2 * x; | |
383 | ✗ | vx = (double)x * rate / s->analysis_rdft_len; | |
384 | ✗ | if (xlog) | |
385 | ✗ | vx = log2(0.05*vx); | |
386 | ✗ | ya = s->dump_buf[i]; | |
387 | ✗ | yb = s->min_phase ? hypotf(s->analysis_tbuf[i], s->analysis_tbuf[i+1]) : s->analysis_tbuf[i]; | |
388 | ✗ | if (s->min_phase) | |
389 | ✗ | yb = fabs(yb); | |
390 | ✗ | if (ylog) { | |
391 | ✗ | ya = 20.0 * log10(fabs(ya)); | |
392 | ✗ | yb = 20.0 * log10(fabs(yb)); | |
393 | } | ||
394 | ✗ | fprintf(fp, "%17.10f %17.10f %17.10f\n", vx, ya, yb); | |
395 | } | ||
396 | ✗ | } | |
397 | |||
398 | 6 | static double entry_func(void *p, double freq, double gain) | |
399 | { | ||
400 | 6 | AVFilterContext *ctx = p; | |
401 | 6 | FIREqualizerContext *s = ctx->priv; | |
402 | |||
403 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (s->nb_gain_entry >= NB_GAIN_ENTRY_MAX) { |
404 | ✗ | av_log(ctx, AV_LOG_ERROR, "entry table overflow.\n"); | |
405 | ✗ | s->gain_entry_err = AVERROR(EINVAL); | |
406 | ✗ | return 0; | |
407 | } | ||
408 | |||
409 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (isnan(freq)) { |
410 | ✗ | av_log(ctx, AV_LOG_ERROR, "nan frequency (%g, %g).\n", freq, gain); | |
411 | ✗ | s->gain_entry_err = AVERROR(EINVAL); | |
412 | ✗ | return 0; | |
413 | } | ||
414 | |||
415 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
6 | if (s->nb_gain_entry > 0 && freq <= s->gain_entry_tbl[s->nb_gain_entry - 1].freq) { |
416 | ✗ | av_log(ctx, AV_LOG_ERROR, "unsorted frequency (%g, %g).\n", freq, gain); | |
417 | ✗ | s->gain_entry_err = AVERROR(EINVAL); | |
418 | ✗ | return 0; | |
419 | } | ||
420 | |||
421 | 6 | s->gain_entry_tbl[s->nb_gain_entry].freq = freq; | |
422 | 6 | s->gain_entry_tbl[s->nb_gain_entry].gain = gain; | |
423 | 6 | s->nb_gain_entry++; | |
424 | 6 | return 0; | |
425 | } | ||
426 | |||
427 | 9660 | static int gain_entry_compare(const void *key, const void *memb) | |
428 | { | ||
429 | 9660 | const double *freq = key; | |
430 | 9660 | const GainEntry *entry = memb; | |
431 | |||
432 |
2/2✓ Branch 0 taken 2972 times.
✓ Branch 1 taken 6688 times.
|
9660 | if (*freq < entry[0].freq) |
433 | 2972 | return -1; | |
434 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6688 times.
|
6688 | if (*freq > entry[1].freq) |
435 | ✗ | return 1; | |
436 | 6688 | return 0; | |
437 | } | ||
438 | |||
439 | 16386 | static double gain_interpolate_func(void *p, double freq) | |
440 | { | ||
441 | 16386 | AVFilterContext *ctx = p; | |
442 | 16386 | FIREqualizerContext *s = ctx->priv; | |
443 | GainEntry *res; | ||
444 | double d0, d1, d; | ||
445 | |||
446 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16386 times.
|
16386 | if (isnan(freq)) |
447 | ✗ | return freq; | |
448 | |||
449 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16386 times.
|
16386 | if (!s->nb_gain_entry) |
450 | ✗ | return 0; | |
451 | |||
452 |
2/2✓ Branch 0 taken 744 times.
✓ Branch 1 taken 15642 times.
|
16386 | if (freq <= s->gain_entry_tbl[0].freq) |
453 | 744 | return s->gain_entry_tbl[0].gain; | |
454 | |||
455 |
2/2✓ Branch 0 taken 8954 times.
✓ Branch 1 taken 6688 times.
|
15642 | if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq) |
456 | 8954 | return s->gain_entry_tbl[s->nb_gain_entry-1].gain; | |
457 | |||
458 | 6688 | res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare); | |
459 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6688 times.
|
6688 | av_assert0(res); |
460 | |||
461 | 6688 | d = res[1].freq - res[0].freq; | |
462 | 6688 | d0 = freq - res[0].freq; | |
463 | 6688 | d1 = res[1].freq - freq; | |
464 | |||
465 |
2/4✓ Branch 0 taken 6688 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6688 times.
✗ Branch 3 not taken.
|
6688 | if (d0 && d1) |
466 | 6688 | return (d0 * res[1].gain + d1 * res[0].gain) / d; | |
467 | |||
468 | ✗ | if (d0) | |
469 | ✗ | return res[1].gain; | |
470 | |||
471 | ✗ | return res[0].gain; | |
472 | } | ||
473 | |||
474 | ✗ | static double cubic_interpolate_func(void *p, double freq) | |
475 | { | ||
476 | ✗ | AVFilterContext *ctx = p; | |
477 | ✗ | FIREqualizerContext *s = ctx->priv; | |
478 | GainEntry *res; | ||
479 | double x, x2, x3; | ||
480 | double a, b, c, d; | ||
481 | double m0, m1, m2, msum, unit; | ||
482 | |||
483 | ✗ | if (!s->nb_gain_entry) | |
484 | ✗ | return 0; | |
485 | |||
486 | ✗ | if (freq <= s->gain_entry_tbl[0].freq) | |
487 | ✗ | return s->gain_entry_tbl[0].gain; | |
488 | |||
489 | ✗ | if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq) | |
490 | ✗ | return s->gain_entry_tbl[s->nb_gain_entry-1].gain; | |
491 | |||
492 | ✗ | res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare); | |
493 | ✗ | av_assert0(res); | |
494 | |||
495 | ✗ | unit = res[1].freq - res[0].freq; | |
496 | ✗ | m0 = res != s->gain_entry_tbl ? | |
497 | ✗ | unit * (res[0].gain - res[-1].gain) / (res[0].freq - res[-1].freq) : 0; | |
498 | ✗ | m1 = res[1].gain - res[0].gain; | |
499 | ✗ | m2 = res != s->gain_entry_tbl + s->nb_gain_entry - 2 ? | |
500 | ✗ | unit * (res[2].gain - res[1].gain) / (res[2].freq - res[1].freq) : 0; | |
501 | |||
502 | ✗ | msum = fabs(m0) + fabs(m1); | |
503 | ✗ | m0 = msum > 0 ? (fabs(m0) * m1 + fabs(m1) * m0) / msum : 0; | |
504 | ✗ | msum = fabs(m1) + fabs(m2); | |
505 | ✗ | m1 = msum > 0 ? (fabs(m1) * m2 + fabs(m2) * m1) / msum : 0; | |
506 | |||
507 | ✗ | d = res[0].gain; | |
508 | ✗ | c = m0; | |
509 | ✗ | b = 3 * res[1].gain - m1 - 2 * c - 3 * d; | |
510 | ✗ | a = res[1].gain - b - c - d; | |
511 | |||
512 | ✗ | x = (freq - res[0].freq) / unit; | |
513 | ✗ | x2 = x * x; | |
514 | ✗ | x3 = x2 * x; | |
515 | |||
516 | ✗ | return a * x3 + b * x2 + c * x + d; | |
517 | } | ||
518 | |||
519 | static const char *const var_names[] = { | ||
520 | "f", | ||
521 | "sr", | ||
522 | "ch", | ||
523 | "chid", | ||
524 | "chs", | ||
525 | "chlayout", | ||
526 | NULL | ||
527 | }; | ||
528 | |||
529 | enum VarOffset { | ||
530 | VAR_F, | ||
531 | VAR_SR, | ||
532 | VAR_CH, | ||
533 | VAR_CHID, | ||
534 | VAR_CHS, | ||
535 | VAR_CHLAYOUT, | ||
536 | VAR_NB | ||
537 | }; | ||
538 | |||
539 | ✗ | static void generate_min_phase_kernel(FIREqualizerContext *s, float *rdft_buf) | |
540 | { | ||
541 | ✗ | int k, cepstrum_len = s->cepstrum_len, rdft_len = s->rdft_len; | |
542 | ✗ | double norm = 2.0 / cepstrum_len; | |
543 | ✗ | double minval = 1e-7 / rdft_len; | |
544 | |||
545 | ✗ | memset(s->cepstrum_buf, 0, cepstrum_len * sizeof(*s->cepstrum_buf)); | |
546 | ✗ | memset(s->cepstrum_tbuf, 0, (cepstrum_len + 2) * sizeof(*s->cepstrum_tbuf)); | |
547 | ✗ | memcpy(s->cepstrum_buf, rdft_buf, rdft_len/2 * sizeof(*rdft_buf)); | |
548 | ✗ | memcpy(s->cepstrum_buf + cepstrum_len - rdft_len/2, rdft_buf + rdft_len/2, rdft_len/2 * sizeof(*rdft_buf)); | |
549 | |||
550 | ✗ | s->cepstrum_rdft_fn(s->cepstrum_rdft, s->cepstrum_tbuf, s->cepstrum_buf, sizeof(float)); | |
551 | |||
552 | ✗ | for (k = 0; k < cepstrum_len + 2; k += 2) { | |
553 | ✗ | s->cepstrum_tbuf[k] = log(FFMAX(s->cepstrum_tbuf[k], minval)); | |
554 | ✗ | s->cepstrum_tbuf[k+1] = 0; | |
555 | } | ||
556 | |||
557 | ✗ | s->cepstrum_irdft_fn(s->cepstrum_irdft, s->cepstrum_buf, s->cepstrum_tbuf, sizeof(AVComplexFloat)); | |
558 | |||
559 | ✗ | memset(s->cepstrum_buf + cepstrum_len/2 + 1, 0, (cepstrum_len/2 - 1) * sizeof(*s->cepstrum_buf)); | |
560 | ✗ | for (k = 1; k <= cepstrum_len/2; k++) | |
561 | ✗ | s->cepstrum_buf[k] *= 2; | |
562 | |||
563 | ✗ | s->cepstrum_rdft_fn(s->cepstrum_rdft, s->cepstrum_tbuf, s->cepstrum_buf, sizeof(float)); | |
564 | |||
565 | ✗ | for (k = 0; k < cepstrum_len + 2; k += 2) { | |
566 | ✗ | double mag = exp(s->cepstrum_tbuf[k] * norm) * norm; | |
567 | ✗ | double ph = s->cepstrum_tbuf[k+1] * norm; | |
568 | ✗ | s->cepstrum_tbuf[k] = mag * cos(ph); | |
569 | ✗ | s->cepstrum_tbuf[k+1] = mag * sin(ph); | |
570 | } | ||
571 | |||
572 | ✗ | s->cepstrum_irdft_fn(s->cepstrum_irdft, s->cepstrum_buf, s->cepstrum_tbuf, sizeof(AVComplexFloat)); | |
573 | ✗ | memset(rdft_buf, 0, s->rdft_len * sizeof(*rdft_buf)); | |
574 | ✗ | memcpy(rdft_buf, s->cepstrum_buf, s->fir_len * sizeof(*rdft_buf)); | |
575 | |||
576 | ✗ | if (s->dumpfile) { | |
577 | ✗ | memset(s->analysis_buf, 0, (s->analysis_rdft_len + 2) * sizeof(*s->analysis_buf)); | |
578 | ✗ | memcpy(s->analysis_buf, s->cepstrum_buf, s->fir_len * sizeof(*s->analysis_buf)); | |
579 | } | ||
580 | ✗ | } | |
581 | |||
582 | 5 | static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *gain_entry) | |
583 | { | ||
584 | 5 | FIREqualizerContext *s = ctx->priv; | |
585 | 5 | AVFilterLink *inlink = ctx->inputs[0]; | |
586 | 5 | const char *gain_entry_func_names[] = { "entry", NULL }; | |
587 | 5 | const char *gain_func_names[] = { "gain_interpolate", "cubic_interpolate", NULL }; | |
588 | 5 | double (*gain_entry_funcs[])(void *, double, double) = { entry_func, NULL }; | |
589 | 5 | double (*gain_funcs[])(void *, double) = { gain_interpolate_func, cubic_interpolate_func, NULL }; | |
590 | double vars[VAR_NB]; | ||
591 | AVExpr *gain_expr; | ||
592 | int ret, k, center, ch; | ||
593 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | int xlog = s->scale == SCALE_LOGLIN || s->scale == SCALE_LOGLOG; |
594 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
5 | int ylog = s->scale == SCALE_LINLOG || s->scale == SCALE_LOGLOG; |
595 | 5 | FILE *dump_fp = NULL; | |
596 | |||
597 | 5 | s->nb_gain_entry = 0; | |
598 | 5 | s->gain_entry_err = 0; | |
599 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | if (gain_entry) { |
600 | 2 | double result = 0.0; | |
601 | 2 | ret = av_expr_parse_and_eval(&result, gain_entry, NULL, NULL, NULL, NULL, | |
602 | gain_entry_func_names, gain_entry_funcs, ctx, 0, ctx); | ||
603 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
604 | ✗ | return ret; | |
605 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->gain_entry_err < 0) |
606 | ✗ | return s->gain_entry_err; | |
607 | } | ||
608 | |||
609 | 5 | av_log(ctx, AV_LOG_DEBUG, "nb_gain_entry = %d.\n", s->nb_gain_entry); | |
610 | |||
611 | 5 | ret = av_expr_parse(&gain_expr, gain, var_names, | |
612 | gain_func_names, gain_funcs, NULL, NULL, 0, ctx); | ||
613 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (ret < 0) |
614 | ✗ | return ret; | |
615 | |||
616 |
1/8✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
5 | if (s->dumpfile && (!s->dump_buf || !s->analysis_rdft || !(dump_fp = avpriv_fopen_utf8(s->dumpfile, "w")))) |
617 | ✗ | av_log(ctx, AV_LOG_WARNING, "dumping failed.\n"); | |
618 | |||
619 | 5 | vars[VAR_CHS] = inlink->ch_layout.nb_channels; | |
620 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | vars[VAR_CHLAYOUT] = inlink->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? |
621 | 5 | inlink->ch_layout.u.mask : 0; | |
622 | 5 | vars[VAR_SR] = inlink->sample_rate; | |
623 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
|
9 | for (ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { |
624 | 7 | float *rdft_buf = s->kernel_tmp_buf + ch * (s->rdft_len * 2); | |
625 | 7 | float *rdft_tbuf = s->kernel_tmp_tbuf; | |
626 | double result; | ||
627 | 7 | vars[VAR_CH] = ch; | |
628 | 7 | vars[VAR_CHID] = av_channel_layout_channel_from_index(&inlink->ch_layout, ch); | |
629 | |||
630 |
2/2✓ Branch 0 taken 57351 times.
✓ Branch 1 taken 7 times.
|
57358 | for (k = 0; k <= s->analysis_rdft_len/2; k++) { |
631 | 57351 | vars[VAR_F] = k * ((double)inlink->sample_rate /(double)s->analysis_rdft_len); | |
632 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57351 times.
|
57351 | if (xlog) |
633 | ✗ | vars[VAR_F] = log2(0.05 * vars[VAR_F]); | |
634 | 57351 | result = av_expr_eval(gain_expr, vars, ctx); | |
635 |
1/4✓ Branch 0 taken 57351 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
57351 | s->analysis_tbuf[2*k] = ylog ? pow(10.0, 0.05 * result) : s->min_phase ? fabs(result) : result; |
636 | 57351 | s->analysis_tbuf[2*k+1] = 0.0; | |
637 | } | ||
638 | |||
639 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (s->dump_buf) |
640 | ✗ | memcpy(s->dump_buf, s->analysis_tbuf, (s->analysis_rdft_len + 2) * sizeof(*s->analysis_tbuf)); | |
641 | |||
642 | 7 | s->analysis_irdft_fn(s->analysis_irdft, s->analysis_buf, s->analysis_tbuf, sizeof(AVComplexFloat)); | |
643 | 7 | center = s->fir_len / 2; | |
644 | |||
645 |
2/2✓ Branch 0 taken 16324 times.
✓ Branch 1 taken 7 times.
|
16331 | for (k = 0; k <= center; k++) { |
646 | 16324 | double u = k * (M_PI/center); | |
647 | double win; | ||
648 |
2/11✗ Branch 0 not taken.
✓ Branch 1 taken 7502 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 8822 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
16324 | switch (s->wfunc) { |
649 | ✗ | case WFUNC_RECTANGULAR: | |
650 | ✗ | win = 1.0; | |
651 | ✗ | break; | |
652 | 7502 | case WFUNC_HANN: | |
653 | 7502 | win = 0.5 + 0.5 * cos(u); | |
654 | 7502 | break; | |
655 | ✗ | case WFUNC_HAMMING: | |
656 | ✗ | win = 0.53836 + 0.46164 * cos(u); | |
657 | ✗ | break; | |
658 | ✗ | case WFUNC_BLACKMAN: | |
659 | ✗ | win = 0.42 + 0.5 * cos(u) + 0.08 * cos(2*u); | |
660 | ✗ | break; | |
661 | ✗ | case WFUNC_NUTTALL3: | |
662 | ✗ | win = 0.40897 + 0.5 * cos(u) + 0.09103 * cos(2*u); | |
663 | ✗ | break; | |
664 | ✗ | case WFUNC_MNUTTALL3: | |
665 | ✗ | win = 0.4243801 + 0.4973406 * cos(u) + 0.0782793 * cos(2*u); | |
666 | ✗ | break; | |
667 | 8822 | case WFUNC_NUTTALL: | |
668 | 8822 | win = 0.355768 + 0.487396 * cos(u) + 0.144232 * cos(2*u) + 0.012604 * cos(3*u); | |
669 | 8822 | break; | |
670 | ✗ | case WFUNC_BNUTTALL: | |
671 | ✗ | win = 0.3635819 + 0.4891775 * cos(u) + 0.1365995 * cos(2*u) + 0.0106411 * cos(3*u); | |
672 | ✗ | break; | |
673 | ✗ | case WFUNC_BHARRIS: | |
674 | ✗ | win = 0.35875 + 0.48829 * cos(u) + 0.14128 * cos(2*u) + 0.01168 * cos(3*u); | |
675 | ✗ | break; | |
676 | ✗ | case WFUNC_TUKEY: | |
677 | ✗ | win = (u <= 0.5 * M_PI) ? 1.0 : (0.5 + 0.5 * cos(2*u - M_PI)); | |
678 | ✗ | break; | |
679 | ✗ | default: | |
680 | ✗ | av_assert0(0); | |
681 | } | ||
682 | 16324 | s->analysis_buf[k] *= (2.0/s->analysis_rdft_len) * (2.0/s->rdft_len) * win; | |
683 |
2/2✓ Branch 0 taken 16317 times.
✓ Branch 1 taken 7 times.
|
16324 | if (k) |
684 | 16317 | s->analysis_buf[s->analysis_rdft_len - k] = s->analysis_buf[k]; | |
685 | } | ||
686 | |||
687 | 7 | memset(s->analysis_buf + center + 1, 0, (s->analysis_rdft_len - s->fir_len) * sizeof(*s->analysis_buf)); | |
688 | 7 | memcpy(rdft_tbuf, s->analysis_buf, s->rdft_len/2 * sizeof(*s->analysis_buf)); | |
689 | 7 | memcpy(rdft_tbuf + s->rdft_len/2, s->analysis_buf + s->analysis_rdft_len - s->rdft_len/2, s->rdft_len/2 * sizeof(*s->analysis_buf)); | |
690 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (s->min_phase) |
691 | ✗ | generate_min_phase_kernel(s, rdft_tbuf); | |
692 | 7 | s->rdft_fn(s->rdft, rdft_buf, rdft_tbuf, sizeof(float)); | |
693 | |||
694 |
2/2✓ Branch 0 taken 61454 times.
✓ Branch 1 taken 7 times.
|
61461 | for (k = 0; k < s->rdft_len + 2; k++) { |
695 |
2/4✓ Branch 0 taken 61454 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 61454 times.
|
61454 | if (isnan(rdft_buf[k]) || isinf(rdft_buf[k])) { |
696 | ✗ | av_log(ctx, AV_LOG_ERROR, "filter kernel contains nan or infinity.\n"); | |
697 | ✗ | av_expr_free(gain_expr); | |
698 | ✗ | if (dump_fp) | |
699 | ✗ | fclose(dump_fp); | |
700 | ✗ | return AVERROR(EINVAL); | |
701 | } | ||
702 | } | ||
703 | |||
704 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (!s->min_phase) { |
705 |
2/2✓ Branch 0 taken 30727 times.
✓ Branch 1 taken 7 times.
|
30734 | for (k = 0; k <= s->rdft_len/2; k++) |
706 | 30727 | rdft_buf[k] = rdft_buf[2*k]; | |
707 | } | ||
708 | |||
709 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (dump_fp) |
710 | ✗ | dump_fir(ctx, dump_fp, ch); | |
711 | |||
712 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
|
7 | if (!s->multi) |
713 | 3 | break; | |
714 | } | ||
715 | |||
716 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | memcpy(s->kernel_buf, s->kernel_tmp_buf, (s->multi ? inlink->ch_layout.nb_channels : 1) * (s->rdft_len * 2) * sizeof(*s->kernel_buf)); |
717 | 5 | av_expr_free(gain_expr); | |
718 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (dump_fp) |
719 | ✗ | fclose(dump_fp); | |
720 | 5 | return 0; | |
721 | } | ||
722 | |||
723 | #define SELECT_GAIN(s) (s->gain_cmd ? s->gain_cmd : s->gain) | ||
724 | #define SELECT_GAIN_ENTRY(s) (s->gain_entry_cmd ? s->gain_entry_cmd : s->gain_entry) | ||
725 | |||
726 | 5 | static int config_input(AVFilterLink *inlink) | |
727 | { | ||
728 | 5 | FilterLink *l = ff_filter_link(inlink); | |
729 | 5 | AVFilterContext *ctx = inlink->dst; | |
730 | 5 | FIREqualizerContext *s = ctx->priv; | |
731 | 5 | float iscale, scale = 1.f; | |
732 | int rdft_bits, ret; | ||
733 | |||
734 | 5 | common_uninit(s); | |
735 | |||
736 | 5 | s->next_pts = 0; | |
737 | 5 | s->frame_nsamples_max = 0; | |
738 | |||
739 | 5 | s->fir_len = FFMAX(2 * (int)(inlink->sample_rate * s->delay) + 1, 3); | |
740 | 5 | s->remaining = s->fir_len - 1; | |
741 | |||
742 |
1/2✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
|
47 | for (rdft_bits = RDFT_BITS_MIN; rdft_bits <= RDFT_BITS_MAX; rdft_bits++) { |
743 | 47 | s->rdft_len = 1 << rdft_bits; | |
744 | 47 | s->nsamples_max = s->rdft_len - s->fir_len + 1; | |
745 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 42 times.
|
47 | if (s->nsamples_max * 2 >= s->fir_len) |
746 | 5 | break; | |
747 | } | ||
748 | |||
749 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (rdft_bits > RDFT_BITS_MAX) { |
750 | ✗ | av_log(ctx, AV_LOG_ERROR, "too large delay, please decrease it.\n"); | |
751 | ✗ | return AVERROR(EINVAL); | |
752 | } | ||
753 | |||
754 | 5 | iscale = 0.5f; | |
755 |
2/4✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
|
10 | if (((ret = av_tx_init(&s->rdft, &s->rdft_fn, AV_TX_FLOAT_RDFT, 0, 1 << rdft_bits, &scale, 0)) < 0) || |
756 | 5 | ((ret = av_tx_init(&s->irdft, &s->irdft_fn, AV_TX_FLOAT_RDFT, 1, 1 << rdft_bits, &iscale, 0)) < 0)) | |
757 | ✗ | return ret; | |
758 | |||
759 | 5 | scale = 1.f; | |
760 |
5/8✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
|
7 | if (s->fft2 && !s->multi && inlink->ch_layout.nb_channels > 1 && |
761 | 2 | ((ret = av_tx_init(&s->fft_ctx, &s->fft_fn, AV_TX_FLOAT_FFT, 0, 1 << rdft_bits, &scale, 0)) < 0)) | |
762 | ✗ | return ret; | |
763 | |||
764 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (s->min_phase) { |
765 | ✗ | int cepstrum_bits = rdft_bits + 2; | |
766 | ✗ | if (cepstrum_bits > RDFT_BITS_MAX) { | |
767 | ✗ | av_log(ctx, AV_LOG_ERROR, "too large delay, please decrease it.\n"); | |
768 | ✗ | return AVERROR(EINVAL); | |
769 | } | ||
770 | |||
771 | ✗ | cepstrum_bits = FFMIN(RDFT_BITS_MAX, cepstrum_bits + 1); | |
772 | ✗ | scale = 1.f; | |
773 | ✗ | ret = av_tx_init(&s->cepstrum_rdft, &s->cepstrum_rdft_fn, AV_TX_FLOAT_RDFT, 0, 1 << cepstrum_bits, &scale, 0); | |
774 | ✗ | if (ret < 0) | |
775 | ✗ | return ret; | |
776 | |||
777 | ✗ | iscale = 0.5f; | |
778 | ✗ | ret = av_tx_init(&s->cepstrum_irdft, &s->cepstrum_irdft_fn, AV_TX_FLOAT_RDFT, 1, 1 << cepstrum_bits, &iscale, 0); | |
779 | ✗ | if (ret < 0) | |
780 | ✗ | return ret; | |
781 | |||
782 | ✗ | s->cepstrum_len = 1 << cepstrum_bits; | |
783 | ✗ | s->cepstrum_buf = av_malloc_array(s->cepstrum_len, sizeof(*s->cepstrum_buf)); | |
784 | ✗ | if (!s->cepstrum_buf) | |
785 | ✗ | return AVERROR(ENOMEM); | |
786 | ✗ | s->cepstrum_tbuf = av_malloc_array(s->cepstrum_len + 2, sizeof(*s->cepstrum_tbuf)); | |
787 | ✗ | if (!s->cepstrum_tbuf) | |
788 | ✗ | return AVERROR(ENOMEM); | |
789 | } | ||
790 | |||
791 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | for ( ; rdft_bits <= RDFT_BITS_MAX; rdft_bits++) { |
792 | 13 | s->analysis_rdft_len = 1 << rdft_bits; | |
793 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8 times.
|
13 | if (inlink->sample_rate <= s->accuracy * s->analysis_rdft_len) |
794 | 5 | break; | |
795 | } | ||
796 | |||
797 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (rdft_bits > RDFT_BITS_MAX) { |
798 | ✗ | av_log(ctx, AV_LOG_ERROR, "too small accuracy, please increase it.\n"); | |
799 | ✗ | return AVERROR(EINVAL); | |
800 | } | ||
801 | |||
802 | 5 | iscale = 0.5f; | |
803 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
|
5 | if ((ret = av_tx_init(&s->analysis_irdft, &s->analysis_irdft_fn, AV_TX_FLOAT_RDFT, 1, 1 << rdft_bits, &iscale, 0)) < 0) |
804 | ✗ | return ret; | |
805 | |||
806 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (s->dumpfile) { |
807 | ✗ | scale = 1.f; | |
808 | ✗ | if ((ret = av_tx_init(&s->analysis_rdft, &s->analysis_rdft_fn, AV_TX_FLOAT_RDFT, 0, 1 << rdft_bits, &scale, 0)) < 0) | |
809 | ✗ | return ret; | |
810 | ✗ | s->dump_buf = av_malloc_array(s->analysis_rdft_len + 2, sizeof(*s->dump_buf)); | |
811 | } | ||
812 | |||
813 | 5 | s->analysis_buf = av_malloc_array((s->analysis_rdft_len + 2), sizeof(*s->analysis_buf)); | |
814 | 5 | s->analysis_tbuf = av_malloc_array(s->analysis_rdft_len + 2, sizeof(*s->analysis_tbuf)); | |
815 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | s->kernel_tmp_buf = av_malloc_array((s->rdft_len * 2) * (s->multi ? inlink->ch_layout.nb_channels : 1), sizeof(*s->kernel_tmp_buf)); |
816 | 5 | s->kernel_tmp_tbuf = av_malloc_array(s->rdft_len, sizeof(*s->kernel_tmp_tbuf)); | |
817 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | s->kernel_buf = av_malloc_array((s->rdft_len * 2) * (s->multi ? inlink->ch_layout.nb_channels : 1), sizeof(*s->kernel_buf)); |
818 | 5 | s->tx_buf = av_malloc_array(2 * (s->rdft_len + 2), sizeof(*s->kernel_buf)); | |
819 | 5 | s->conv_buf = av_calloc(2 * s->rdft_len * inlink->ch_layout.nb_channels, sizeof(*s->conv_buf)); | |
820 | 5 | s->conv_idx = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->conv_idx)); | |
821 |
8/16✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 5 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 5 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 5 times.
|
5 | if (!s->analysis_buf || !s->analysis_tbuf || !s->kernel_tmp_buf || !s->kernel_buf || !s->conv_buf || !s->conv_idx || !s->kernel_tmp_tbuf || !s->tx_buf) |
822 | ✗ | return AVERROR(ENOMEM); | |
823 | |||
824 | 5 | 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", | |
825 | inlink->sample_rate, inlink->ch_layout.nb_channels, s->analysis_rdft_len, s->rdft_len, s->fir_len, s->nsamples_max); | ||
826 | |||
827 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | if (s->fixed) |
828 | 1 | l->min_samples = l->max_samples = s->nsamples_max; | |
829 | |||
830 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | return generate_kernel(ctx, SELECT_GAIN(s), SELECT_GAIN_ENTRY(s)); |
831 | } | ||
832 | |||
833 | 225 | static int filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
834 | { | ||
835 | 225 | AVFilterContext *ctx = inlink->dst; | |
836 | 225 | FIREqualizerContext *s = ctx->priv; | |
837 | int ch; | ||
838 | |||
839 |
1/2✓ Branch 0 taken 225 times.
✗ Branch 1 not taken.
|
225 | if (!s->min_phase) { |
840 |
4/4✓ Branch 0 taken 225 times.
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 106 times.
✓ Branch 3 taken 119 times.
|
331 | for (ch = 0; ch + 1 < inlink->ch_layout.nb_channels && s->fft_ctx; ch += 2) { |
841 | 106 | fast_convolute2(s, s->kernel_buf, (AVComplexFloat *)(s->conv_buf + 2 * ch * s->rdft_len), | |
842 | 106 | s->conv_idx + ch, (float *) frame->extended_data[ch], | |
843 | 106 | (float *) frame->extended_data[ch+1], frame->nb_samples); | |
844 | } | ||
845 | |||
846 |
2/2✓ Branch 0 taken 238 times.
✓ Branch 1 taken 225 times.
|
463 | for ( ; ch < inlink->ch_layout.nb_channels; ch++) { |
847 | 238 | fast_convolute(s, s->kernel_buf + (s->multi ? ch * (s->rdft_len * 2) : 0), | |
848 | 238 | s->conv_buf + 2 * ch * s->rdft_len, s->conv_idx + ch, | |
849 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 82 times.
|
238 | (float *) frame->extended_data[ch], frame->nb_samples); |
850 | } | ||
851 | } else { | ||
852 | ✗ | for (ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { | |
853 | ✗ | fast_convolute_nonlinear(s, s->kernel_buf + (s->multi ? ch * (s->rdft_len * 2) : 0), | |
854 | ✗ | s->conv_buf + 2 * ch * s->rdft_len, s->conv_idx + ch, | |
855 | ✗ | (float *) frame->extended_data[ch], frame->nb_samples); | |
856 | } | ||
857 | } | ||
858 | |||
859 | 225 | s->next_pts = AV_NOPTS_VALUE; | |
860 |
1/2✓ Branch 0 taken 225 times.
✗ Branch 1 not taken.
|
225 | if (frame->pts != AV_NOPTS_VALUE) { |
861 | 225 | s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, av_make_q(1, inlink->sample_rate), inlink->time_base); | |
862 |
3/4✓ Branch 0 taken 38 times.
✓ Branch 1 taken 187 times.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
|
225 | if (s->zero_phase && !s->min_phase) |
863 | 38 | frame->pts -= av_rescale_q(s->fir_len/2, av_make_q(1, inlink->sample_rate), inlink->time_base); | |
864 | } | ||
865 | 225 | s->frame_nsamples_max = FFMAX(s->frame_nsamples_max, frame->nb_samples); | |
866 | 225 | return ff_filter_frame(ctx->outputs[0], frame); | |
867 | } | ||
868 | |||
869 | 261 | static int request_frame(AVFilterLink *outlink) | |
870 | { | ||
871 | 261 | AVFilterContext *ctx = outlink->src; | |
872 | 261 | FIREqualizerContext *s= ctx->priv; | |
873 | int ret; | ||
874 | |||
875 | 261 | ret = ff_request_frame(ctx->inputs[0]); | |
876 |
5/6✓ Branch 0 taken 12 times.
✓ Branch 1 taken 249 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
|
261 | if (ret == AVERROR_EOF && s->remaining > 0 && s->frame_nsamples_max > 0) { |
877 | 7 | AVFrame *frame = ff_get_audio_buffer(outlink, FFMIN(s->remaining, s->frame_nsamples_max)); | |
878 | |||
879 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (!frame) |
880 | ✗ | return AVERROR(ENOMEM); | |
881 | |||
882 | 7 | av_samples_set_silence(frame->extended_data, 0, frame->nb_samples, outlink->ch_layout.nb_channels, frame->format); | |
883 | 7 | frame->pts = s->next_pts; | |
884 | 7 | s->remaining -= frame->nb_samples; | |
885 | 7 | ret = filter_frame(ctx->inputs[0], frame); | |
886 | } | ||
887 | |||
888 | 261 | return ret; | |
889 | } | ||
890 | |||
891 | ✗ | static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, | |
892 | char *res, int res_len, int flags) | ||
893 | { | ||
894 | ✗ | FIREqualizerContext *s = ctx->priv; | |
895 | ✗ | int ret = AVERROR(ENOSYS); | |
896 | |||
897 | ✗ | if (!strcmp(cmd, "gain")) { | |
898 | char *gain_cmd; | ||
899 | |||
900 | ✗ | if (SELECT_GAIN(s) && !strcmp(SELECT_GAIN(s), args)) { | |
901 | ✗ | av_log(ctx, AV_LOG_DEBUG, "equal gain, do not rebuild.\n"); | |
902 | ✗ | return 0; | |
903 | } | ||
904 | |||
905 | ✗ | gain_cmd = av_strdup(args); | |
906 | ✗ | if (!gain_cmd) | |
907 | ✗ | return AVERROR(ENOMEM); | |
908 | |||
909 | ✗ | ret = generate_kernel(ctx, gain_cmd, SELECT_GAIN_ENTRY(s)); | |
910 | ✗ | if (ret >= 0) { | |
911 | ✗ | av_freep(&s->gain_cmd); | |
912 | ✗ | s->gain_cmd = gain_cmd; | |
913 | } else { | ||
914 | ✗ | av_freep(&gain_cmd); | |
915 | } | ||
916 | ✗ | } else if (!strcmp(cmd, "gain_entry")) { | |
917 | char *gain_entry_cmd; | ||
918 | |||
919 | ✗ | if (SELECT_GAIN_ENTRY(s) && !strcmp(SELECT_GAIN_ENTRY(s), args)) { | |
920 | ✗ | av_log(ctx, AV_LOG_DEBUG, "equal gain_entry, do not rebuild.\n"); | |
921 | ✗ | return 0; | |
922 | } | ||
923 | |||
924 | ✗ | gain_entry_cmd = av_strdup(args); | |
925 | ✗ | if (!gain_entry_cmd) | |
926 | ✗ | return AVERROR(ENOMEM); | |
927 | |||
928 | ✗ | ret = generate_kernel(ctx, SELECT_GAIN(s), gain_entry_cmd); | |
929 | ✗ | if (ret >= 0) { | |
930 | ✗ | av_freep(&s->gain_entry_cmd); | |
931 | ✗ | s->gain_entry_cmd = gain_entry_cmd; | |
932 | } else { | ||
933 | ✗ | av_freep(&gain_entry_cmd); | |
934 | } | ||
935 | } | ||
936 | |||
937 | ✗ | return ret; | |
938 | } | ||
939 | |||
940 | static const AVFilterPad firequalizer_inputs[] = { | ||
941 | { | ||
942 | .name = "default", | ||
943 | .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE, | ||
944 | .config_props = config_input, | ||
945 | .filter_frame = filter_frame, | ||
946 | .type = AVMEDIA_TYPE_AUDIO, | ||
947 | }, | ||
948 | }; | ||
949 | |||
950 | static const AVFilterPad firequalizer_outputs[] = { | ||
951 | { | ||
952 | .name = "default", | ||
953 | .request_frame = request_frame, | ||
954 | .type = AVMEDIA_TYPE_AUDIO, | ||
955 | }, | ||
956 | }; | ||
957 | |||
958 | const AVFilter ff_af_firequalizer = { | ||
959 | .name = "firequalizer", | ||
960 | .description = NULL_IF_CONFIG_SMALL("Finite Impulse Response Equalizer."), | ||
961 | .uninit = uninit, | ||
962 | .process_command = process_command, | ||
963 | .priv_size = sizeof(FIREqualizerContext), | ||
964 | FILTER_INPUTS(firequalizer_inputs), | ||
965 | FILTER_OUTPUTS(firequalizer_outputs), | ||
966 | FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP), | ||
967 | .priv_class = &firequalizer_class, | ||
968 | }; | ||
969 |