1 /*
2 * Copyright (c) 2020 Paul B Mahol
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 License
8 * 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
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
33
36
47
58
62
66
67 #define OFFSET(x) offsetof(AudioFIRSourceContext, x)
68 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
69
81 {
"nb_samples",
"set the number of samples per requested frame",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX,
FLAGS },
82 {
"n",
"set the number of samples per requested frame",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX,
FLAGS },
86 };
87
89
91 {
93
94 if (!(
s->nb_taps & 1)) {
97 }
98
99 return 0;
100 }
101
103 {
105
114 }
115
117 {
124 };
128
132
134 }
135
136 static int parse_string(
char *str,
float **items,
int *nb_items,
int *items_size)
137 {
138 float *new_items;
139 char *tail;
140
142 if (!new_items)
144 *items = new_items;
145
146 tail = str;
147 if (!tail)
149
150 do {
151 (*items)[(*nb_items)++] =
av_strtod(tail, &tail);
152 new_items =
av_fast_realloc(*items, items_size, (*nb_items + 2) *
sizeof(
float));
153 if (!new_items)
155 *items = new_items;
156 if (tail && *tail)
157 tail++;
158 } while (tail && *tail);
159
160 return 0;
161 }
162
164 const float *freq,
165 const float *magnitude,
166 const float *phase,
167 int m, int minterp)
168 {
169 for (
int i = 0;
i < minterp;
i++) {
170 for (int j = 1; j < m; j++) {
171 const float x =
i / (
float)minterp;
172
173 if (x <= freq[j]) {
174 const float mg = (x - freq[j-1]) / (freq[j] - freq[j-1]) * (magnitude[j] - magnitude[j-1]) + magnitude[j-1];
175 const float ph = (x - freq[j-1]) / (freq[j] - freq[j-1]) * (phase[j] - phase[j-1]) + phase[j-1];
176
179 break;
180 }
181 }
182 }
183 }
184
186 {
189 float overlap,
scale = 1.f, compensation;
190 int fft_size, middle,
ret;
191
192 s->nb_freq =
s->nb_magnitude =
s->nb_phase = 0;
193
197
201
205
206 if (
s->nb_freq !=
s->nb_magnitude &&
s->nb_freq !=
s->nb_phase &&
s->nb_freq >= 2) {
209 }
210
211 for (
int i = 0;
i <
s->nb_freq;
i++) {
212 if (
i == 0 &&
s->freq[
i] != 0.f) {
215 }
216
217 if (
i ==
s->nb_freq - 1 &&
s->freq[
i] != 1.f) {
220 }
221
222 if (
i &&
s->freq[
i] <
s->freq[
i-1]) {
225 }
226 }
227
228 fft_size = 1 << (
av_log2(
s->nb_taps) + 1);
229 s->complexf =
av_calloc(fft_size * 2,
sizeof(*
s->complexf));
232
236
240
244
246
247 lininterp(
s->complexf,
s->freq,
s->magnitude,
s->phase,
s->nb_freq, fft_size / 2);
248
249 s->tx_fn(
s->tx_ctx,
s->complexf + fft_size,
s->complexf,
sizeof(*
s->complexf));
250
251 compensation = 2.f / fft_size;
252 middle =
s->nb_taps / 2;
253
254 for (
int i = 0;
i <= middle;
i++) {
255 s->taps[
i] =
s->complexf[fft_size + middle -
i].re * compensation *
s->win[
i];
256 s->taps[middle +
i] =
s->complexf[fft_size +
i].re * compensation *
s->win[middle +
i];
257 }
258
260
261 return 0;
262 }
263
265 {
269 int nb_samples;
270
273
274 nb_samples =
FFMIN(
s->nb_samples,
s->nb_taps -
s->pts);
275 if (nb_samples <= 0) {
277 return 0;
278 }
279
282
283 memcpy(
frame->data[0],
s->taps +
s->pts, nb_samples *
sizeof(
float));
284
286 s->pts += nb_samples;
288 }
289
291 {
295 },
296 };
297
308 .priv_class = &afirsrc_class,
309 };
310
311 #define DEFAULT_BANDS "25 40 63 100 160 250 400 630 1000 1600 2500 4000 6300 10000 16000 24000"
312
317
319 { "flat", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
320 { "acoustic", { 5.0, 4.5, 4.0, 3.5, 1.5, 1.0, 1.5, 1.5, 2.0, 3.0, 3.5, 4.0, 3.7, 3.0, 3.0 } },
321 { "bass", { 10.0, 8.8, 8.5, 6.5, 2.5, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
322 { "beats", { -5.5, -5.0, -4.5, -4.2, -3.5, -3.0, -1.9, 0, 0, 0, 0, 0, 0, 0, 0 } },
323 { "classic", { -0.3, 0.3, -3.5, -9.0, -1.0, 0.0, 1.8, 2.1, 0.0, 0.0, 0.0, 4.4, 9.0, 9.0, 9.0 } },
324 { "clear", { 3.5, 5.5, 6.5, 9.5, 8.0, 6.5, 3.5, 2.5, 1.3, 5.0, 7.0, 9.0, 10.0, 11.0, 9.0 } },
325 { "deep bass", { 12.0, 8.0, 0.0, -6.7, -12.0, -9.0, -3.5, -3.5, -6.1, 0.0, -3.0, -5.0, 0.0, 1.2, 3.0 } },
326 { "dubstep", { 12.0, 10.0, 0.5, -1.0, -3.0, -5.0, -5.0, -4.8, -4.5, -2.5, -1.0, 0.0, -2.5, -2.5, 0.0 } },
327 { "electronic", { 4.0, 4.0, 3.5, 1.0, 0.0, -0.5, -2.0, 0.0, 2.0, 0.0, 0.0, 1.0, 3.0, 4.0, 4.5 } },
328 { "hardstyle", { 6.1, 7.0, 12.0, 6.1, -5.0, -12.0, -2.5, 3.0, 6.5, 0.0, -2.2, -4.5, -6.1, -9.2, -10.0 } },
329 { "hip-hop", { 4.5, 4.3, 4.0, 2.5, 1.5, 3.0, -1.0, -1.5, -1.5, 1.5, 0.0, -1.0, 0.0, 1.5, 3.0 } },
330 { "jazz", { 0.0, 0.0, 0.0, 2.0, 4.0, 5.9, -5.9, -4.5, -2.5, 2.5, 1.0, -0.8, -0.8, -0.8, -0.8 } },
331 { "metal", { 10.5, 10.5, 7.5, 0.0, 2.0, 5.5, 0.0, 0.0, 0.0, 6.1, 0.0, 0.0, 6.1, 10.0, 12.0 } },
332 { "movie", { 3.0, 3.0, 6.1, 8.5, 9.0, 7.0, 6.1, 6.1, 5.0, 8.0, 3.5, 3.5, 8.0, 10.0, 8.0 } },
333 { "pop", { 0.0, 0.0, 0.0, 0.0, 0.0, 1.3, 2.0, 2.5, 5.0, -1.5, -2.0, -3.0, -3.0, -3.0, -3.0 } },
334 { "r&b", { 3.0, 3.0, 7.0, 6.1, 4.5, 1.5, -1.5, -2.0, -1.5, 2.0, 2.5, 3.0, 3.5, 3.8, 4.0 } },
335 { "rock", { 0.0, 0.0, 0.0, 3.0, 3.0, -10.0, -4.0, -1.0, 0.8, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0 } },
336 { "vocal booster", { -1.5, -2.0, -3.0, -3.0, -0.5, 1.5, 3.5, 3.5, 3.5, 3.0, 2.0, 1.5, 0.0, 0.0, -1.5 } },
337 };
338
369 {
"nb_samples",
"set the number of samples per requested frame",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX,
FLAGS },
370 {
"n",
"set the number of samples per requested frame",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX,
FLAGS },
380 };
381
383
385 const float *freq,
386 const float *magnitude,
387 int m,
int interp,
int minterp,
389 {
390 for (
int i = 0;
i < minterp;
i++) {
391 for (int j = 0; j < m; j++) {
393
394 if (x <= freq[j+1]) {
396
398 const float d = freq[j+1] - freq[j];
399 const float d0 = x - freq[j];
400 const float d1 = freq[j+1] - x;
401 const float g0 = magnitude[j];
402 const float g1 = magnitude[j+1];
403
404 if (d0 && d1) {
405 g = (d0 * g1 + d1 * g0) / d;
406 } else if (d0) {
408 } else {
410 }
411 } else {
412 if (x <= freq[j]) {
414 } else {
415 float x1, x2, x3;
417 float m0, m1, m2, msum;
418 const float unit = freq[j+1] - freq[j];
419
420 m0 = j != 0 ? unit * (magnitude[j] - magnitude[j-1]) / (freq[j] - freq[j-1]) : 0;
421 m1 = magnitude[j+1] - magnitude[j];
422 m2 = j != minterp - 1 ? unit * (magnitude[j+2] - magnitude[j+1]) / (freq[j+2] - freq[j+1]) : 0;
423
425 m0 = msum > 0.f ? (
fabsf(m0) * m1 +
fabsf(m1) * m0) / msum : 0.
f;
427 m1 = msum > 0.f ? (
fabsf(m1) * m2 +
fabsf(m2) * m1) / msum : 0.
f;
428
429 d = magnitude[j];
431 b = 3.f * magnitude[j+1] - m1 - 2.f *
c - 3.f * d;
432 a = magnitude[j+1] -
b -
c - d;
433
434 x1 = (x - freq[j]) / unit;
435 x2 = x1 * x1;
436 x3 = x2 * x1;
437
438 g =
a * x3 +
b * x2 +
c * x1 + d;
439 }
440 }
441
444 complexf[minterp * 2 -
i - 1].
re =
g;
445 complexf[minterp * 2 -
i - 1].
im = 0;
446
447 break;
448 }
449 }
450 }
451 }
452
454 {
457 int fft_size, middle, asize,
ret;
459
460 s->nb_freq =
s->nb_magnitude = 0;
465
469 } else {
470 char *freq_str;
471
473
475 if (!freq_str)
477
482
483 s->magnitude =
av_calloc(
s->nb_magnitude + 1,
sizeof(*
s->magnitude));
486 memcpy(
s->magnitude,
eq_presets[
s->preset].
gains,
sizeof(*
s->magnitude) *
s->nb_magnitude);
487 }
488
489 if (
s->nb_freq !=
s->nb_magnitude ||
s->nb_freq < 2) {
492 }
493
495 s->magnitude[
s->nb_freq] =
s->magnitude[
s->nb_freq-1];
496
497 fft_size =
s->nb_taps * 2;
500 s->complexf =
av_calloc(asize * 2,
sizeof(*
s->complexf));
503
508
512
514
515 for (
int i = 0;
i < fft_size;
i++)
517
519 const float threshold =
powf(10.
f, -100.
f / 20.
f);
520 const float logt = logf(threshold);
521
526
527 for (
int i = 0;
i < fft_size;
i++)
528 s->complexf[
i].re =
s->complexf[
i].re < threshold ? logt : logf(
s->complexf[
i].re);
529
530 s->itx_fn(
s->itx_ctx,
s->complexf + asize,
s->complexf,
sizeof(
float));
531 for (
int i = 0;
i < fft_size;
i++) {
532 s->complexf[
i + asize].re /= fft_size;
533 s->complexf[
i + asize].im /= fft_size;
534 }
535
536 for (
int i = 1;
i <
s->nb_taps;
i++) {
537 s->complexf[asize +
i].re +=
s->complexf[asize + fft_size -
i].re;
538 s->complexf[asize +
i].im -=
s->complexf[asize + fft_size -
i].im;
539 s->complexf[asize + fft_size -
i].re = 0.f;
540 s->complexf[asize + fft_size -
i].im = 0.f;
541 }
542 s->complexf[asize +
s->nb_taps - 1].im *= -1.f;
543
544 s->tx_fn(
s->tx_ctx,
s->complexf,
s->complexf + asize,
sizeof(
float));
545
546 for (
int i = 0;
i < fft_size;
i++) {
547 float eR =
expf(
s->complexf[
i].re);
548
549 s->complexf[
i].re = eR *
cosf(
s->complexf[
i].im);
550 s->complexf[
i].im = eR *
sinf(
s->complexf[
i].im);
551 }
552
553 s->itx_fn(
s->itx_ctx,
s->complexf + asize,
s->complexf,
sizeof(
float));
554
555 for (
int i = 0;
i <
s->nb_taps;
i++)
556 s->taps[
i] =
s->complexf[
i + asize].re / fft_size;
557 } else {
558 s->itx_fn(
s->itx_ctx,
s->complexf + asize,
s->complexf,
sizeof(
float));
559
560 middle =
s->nb_taps / 2;
561 for (
int i = 0;
i < middle;
i++) {
562 s->taps[middle -
i] =
s->complexf[
i + asize].re / fft_size;
563 s->taps[middle +
i] =
s->complexf[
i + asize].re / fft_size;
564 }
565 }
566
568
569 return 0;
570 }
571
573 {
577 },
578 };
579
589 .priv_class = &afireqsrc_class,
590 };