67 #define OFFSET(x) offsetof(AudioFIRSourceContext, x)
68 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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 },
94 if (!(
s->nb_taps & 1)) {
138 static int parse_string(
char *str,
float **items,
int *nb_items,
int *items_size)
153 (*items)[(*nb_items)++] =
av_strtod(tail, &tail);
154 new_items =
av_fast_realloc(*items, items_size, (*nb_items + 2) *
sizeof(
float));
160 }
while (tail && *tail);
167 const float *magnitude,
171 for (
int i = 0;
i < minterp;
i++) {
172 for (
int j = 1; j < m; j++) {
173 const float x =
i / (
float)minterp;
176 const float mg = (x - freq[j-1]) / (freq[j] - freq[j-1]) * (magnitude[j] - magnitude[j-1]) + magnitude[j-1];
177 const float ph = (x - freq[j-1]) / (freq[j] - freq[j-1]) * (phase[j] - phase[j-1]) + phase[j-1];
191 float overlap,
scale = 1.f, compensation;
192 int fft_size, middle,
ret;
194 s->nb_freq =
s->nb_magnitude =
s->nb_phase = 0;
208 if (
s->nb_freq !=
s->nb_magnitude &&
s->nb_freq !=
s->nb_phase &&
s->nb_freq >= 2) {
213 for (
int i = 0;
i <
s->nb_freq;
i++) {
214 if (
i == 0 &&
s->freq[
i] != 0.f) {
219 if (
i ==
s->nb_freq - 1 &&
s->freq[
i] != 1.f) {
224 if (
i &&
s->freq[
i] <
s->freq[
i-1]) {
230 fft_size = 1 << (
av_log2(
s->nb_taps) + 1);
231 s->complexf =
av_calloc(fft_size * 2,
sizeof(*
s->complexf));
249 lininterp(
s->complexf,
s->freq,
s->magnitude,
s->phase,
s->nb_freq, fft_size / 2);
251 s->tx_fn(
s->tx_ctx,
s->complexf + fft_size,
s->complexf,
sizeof(*
s->complexf));
253 compensation = 2.f / fft_size;
254 middle =
s->nb_taps / 2;
256 for (
int i = 0;
i <= middle;
i++) {
257 s->taps[
i] =
s->complexf[fft_size + middle -
i].re * compensation *
s->win[
i];
258 s->taps[middle +
i] =
s->complexf[fft_size +
i].re * compensation *
s->win[middle +
i];
276 nb_samples =
FFMIN(
s->nb_samples,
s->nb_taps -
s->pts);
277 if (nb_samples <= 0) {
285 memcpy(
frame->data[0],
s->taps +
s->pts, nb_samples *
sizeof(
float));
288 s->pts += nb_samples;
310 .priv_class = &afirsrc_class,
313 #define DEFAULT_BANDS "25 40 63 100 160 250 400 630 1000 1600 2500 4000 6300 10000 16000 24000"
321 {
"flat", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
322 {
"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 } },
323 {
"bass", { 10.0, 8.8, 8.5, 6.5, 2.5, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
324 {
"beats", { -5.5, -5.0, -4.5, -4.2, -3.5, -3.0, -1.9, 0, 0, 0, 0, 0, 0, 0, 0 } },
325 {
"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 } },
326 {
"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 } },
327 {
"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 } },
328 {
"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 } },
329 {
"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 } },
330 {
"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 } },
331 {
"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 } },
332 {
"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 } },
333 {
"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 } },
334 {
"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 } },
335 {
"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 } },
336 {
"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 } },
337 {
"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 } },
338 {
"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 } },
371 {
"nb_samples",
"set the number of samples per requested frame",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX,
FLAGS },
372 {
"n",
"set the number of samples per requested frame",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX,
FLAGS },
388 const float *magnitude,
389 int m,
int interp,
int minterp,
392 for (
int i = 0;
i < minterp;
i++) {
393 for (
int j = 0; j < m; j++) {
396 if (x <= freq[j+1]) {
400 const float d = freq[j+1] - freq[j];
401 const float d0 = x - freq[j];
402 const float d1 = freq[j+1] - x;
403 const float g0 = magnitude[j];
404 const float g1 = magnitude[j+1];
407 g = (d0 * g1 + d1 * g0) / d;
419 float m0, m1, m2, msum;
420 const float unit = freq[j+1] - freq[j];
422 m0 = j != 0 ? unit * (magnitude[j] - magnitude[j-1]) / (freq[j] - freq[j-1]) : 0;
423 m1 = magnitude[j+1] - magnitude[j];
424 m2 = j != minterp - 1 ? unit * (magnitude[j+2] - magnitude[j+1]) / (freq[j+2] - freq[j+1]) : 0;
427 m0 = msum > 0.f ? (
fabsf(m0) * m1 +
fabsf(m1) * m0) / msum : 0.
f;
429 m1 = msum > 0.f ? (
fabsf(m1) * m2 +
fabsf(m2) * m1) / msum : 0.
f;
433 b = 3.f * magnitude[j+1] - m1 - 2.f *
c - 3.f * d;
434 a = magnitude[j+1] -
b -
c - d;
436 x1 = (x - freq[j]) / unit;
440 g =
a * x3 +
b * x2 +
c * x1 + d;
446 complexf[minterp * 2 -
i - 1].
re =
g;
447 complexf[minterp * 2 -
i - 1].
im = 0;
459 int fft_size, middle, asize,
ret;
462 s->nb_freq =
s->nb_magnitude = 0;
485 s->magnitude =
av_calloc(
s->nb_magnitude + 1,
sizeof(*
s->magnitude));
488 memcpy(
s->magnitude,
eq_presets[
s->preset].
gains,
sizeof(*
s->magnitude) *
s->nb_magnitude);
491 if (
s->nb_freq !=
s->nb_magnitude ||
s->nb_freq < 2) {
497 s->magnitude[
s->nb_freq] =
s->magnitude[
s->nb_freq-1];
499 fft_size =
s->nb_taps * 2;
502 s->complexf =
av_calloc(asize * 2,
sizeof(*
s->complexf));
517 for (
int i = 0;
i < fft_size;
i++)
521 const float threshold =
powf(10.
f, -100.
f / 20.
f);
522 const float logt = logf(threshold);
529 for (
int i = 0;
i < fft_size;
i++)
530 s->complexf[
i].re =
s->complexf[
i].re < threshold ? logt : logf(
s->complexf[
i].re);
532 s->itx_fn(
s->itx_ctx,
s->complexf + asize,
s->complexf,
sizeof(
float));
533 for (
int i = 0;
i < fft_size;
i++) {
534 s->complexf[
i + asize].re /= fft_size;
535 s->complexf[
i + asize].im /= fft_size;
538 for (
int i = 1;
i <
s->nb_taps;
i++) {
539 s->complexf[asize +
i].re +=
s->complexf[asize + fft_size -
i].re;
540 s->complexf[asize +
i].im -=
s->complexf[asize + fft_size -
i].im;
541 s->complexf[asize + fft_size -
i].re = 0.f;
542 s->complexf[asize + fft_size -
i].im = 0.f;
544 s->complexf[asize +
s->nb_taps - 1].im *= -1.f;
546 s->tx_fn(
s->tx_ctx,
s->complexf,
s->complexf + asize,
sizeof(
float));
548 for (
int i = 0;
i < fft_size;
i++) {
549 float eR =
expf(
s->complexf[
i].re);
551 s->complexf[
i].re = eR *
cosf(
s->complexf[
i].im);
552 s->complexf[
i].im = eR *
sinf(
s->complexf[
i].im);
555 s->itx_fn(
s->itx_ctx,
s->complexf + asize,
s->complexf,
sizeof(
float));
557 for (
int i = 0;
i <
s->nb_taps;
i++)
558 s->taps[
i] =
s->complexf[
i + asize].re / fft_size;
560 s->itx_fn(
s->itx_ctx,
s->complexf + asize,
s->complexf,
sizeof(
float));
562 middle =
s->nb_taps / 2;
563 for (
int i = 0;
i < middle;
i++) {
564 s->taps[middle -
i] =
s->complexf[
i + asize].re / fft_size;
565 s->taps[middle +
i] =
s->complexf[
i + asize].re / fft_size;
591 .priv_class = &afireqsrc_class,