FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_firequalizer.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 274 515 53.2%
Branches: 127 323 39.3%

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