29 #define RDFT_BITS_MIN 4
30 #define RDFT_BITS_MAX 16
54 #define NB_GAIN_ENTRY_MAX 4096
113 #define OFFSET(x) offsetof(FIREqualizerContext, x)
114 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
115 #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
159 s->analysis_rdft =
s->analysis_irdft =
s->rdft =
s->irdft =
NULL;
161 s->cepstrum_rdft =
NULL;
162 s->cepstrum_irdft =
NULL;
215 if (nsamples <= s->nsamples_max) {
216 float *buf = conv_buf + idx->buf_idx *
s->rdft_len;
217 float *obuf = conv_buf + !idx->buf_idx *
s->rdft_len + idx->overlap_idx;
218 int center =
s->fir_len/2;
221 memset(buf, 0, center *
sizeof(*
data));
222 memcpy(buf + center,
data, nsamples *
sizeof(*
data));
223 memset(buf + center + nsamples, 0, (
s->rdft_len - nsamples - center) *
sizeof(*
data));
226 buf[0] *= kernel_buf[0];
227 buf[1] *= kernel_buf[
s->rdft_len/2];
228 for (k = 1; k <
s->rdft_len/2; k++) {
229 buf[2*k] *= kernel_buf[k];
230 buf[2*k+1] *= kernel_buf[k];
234 for (k = 0; k <
s->rdft_len - idx->overlap_idx; k++)
236 memcpy(
data, buf, nsamples *
sizeof(*
data));
237 idx->buf_idx = !idx->buf_idx;
238 idx->overlap_idx = nsamples;
240 while (nsamples >
s->nsamples_max * 2) {
242 data +=
s->nsamples_max;
243 nsamples -=
s->nsamples_max;
251 float *av_restrict conv_buf,
OverlapIndex *av_restrict idx,
252 float *av_restrict
data,
int nsamples)
254 if (nsamples <= s->nsamples_max) {
255 float *buf = conv_buf + idx->buf_idx *
s->rdft_len;
256 float *obuf = conv_buf + !idx->buf_idx *
s->rdft_len + idx->overlap_idx;
259 memcpy(buf,
data, nsamples *
sizeof(*
data));
260 memset(buf + nsamples, 0, (
s->rdft_len - nsamples) *
sizeof(*
data));
263 buf[0] *= kernel_buf[0];
264 buf[1] *= kernel_buf[1];
265 for (k = 2; k <
s->rdft_len; k += 2) {
267 re = buf[k] * kernel_buf[k] - buf[k+1] * kernel_buf[k+1];
268 im = buf[k] * kernel_buf[k+1] + buf[k+1] * kernel_buf[k];
274 for (k = 0; k <
s->rdft_len - idx->overlap_idx; k++)
276 memcpy(
data, buf, nsamples *
sizeof(*
data));
277 idx->buf_idx = !idx->buf_idx;
278 idx->overlap_idx = nsamples;
280 while (nsamples >
s->nsamples_max * 2) {
282 data +=
s->nsamples_max;
283 nsamples -=
s->nsamples_max;
291 OverlapIndex *av_restrict idx,
float *av_restrict data0,
float *av_restrict data1,
int nsamples)
293 if (nsamples <= s->nsamples_max) {
294 FFTComplex *buf = conv_buf + idx->buf_idx *
s->rdft_len;
295 FFTComplex *obuf = conv_buf + !idx->buf_idx *
s->rdft_len + idx->overlap_idx;
296 int center =
s->fir_len/2;
300 memset(buf, 0, center *
sizeof(*buf));
301 for (k = 0; k < nsamples; k++) {
302 buf[center+k].
re = data0[k];
303 buf[center+k].
im = data1[k];
305 memset(buf + center + nsamples, 0, (
s->rdft_len - nsamples - center) *
sizeof(*buf));
312 buf[0].
re = 0.5f * kernel_buf[0] * buf[0].
im;
313 buf[0].
im = 0.5f * kernel_buf[0] *
tmp;
314 for (k = 1; k <
s->rdft_len/2; k++) {
315 int m =
s->rdft_len - k;
317 buf[k].
re = 0.5f * kernel_buf[k] * buf[k].
im;
318 buf[k].
im = 0.5f * kernel_buf[k] *
tmp;
320 buf[m].
re = 0.5f * kernel_buf[k] * buf[m].
im;
321 buf[m].
im = 0.5f * kernel_buf[k] *
tmp;
324 buf[k].
re = 0.5f * kernel_buf[k] * buf[k].
im;
325 buf[k].
im = 0.5f * kernel_buf[k] *
tmp;
330 for (k = 0; k <
s->rdft_len - idx->overlap_idx; k++) {
331 buf[k].
re += obuf[k].
re;
332 buf[k].
im += obuf[k].
im;
336 for (k = 0; k < nsamples; k++) {
337 data0[k] = buf[k].
im;
338 data1[k] = buf[k].
re;
340 idx->buf_idx = !idx->buf_idx;
341 idx->overlap_idx = nsamples;
343 while (nsamples >
s->nsamples_max * 2) {
345 data0 +=
s->nsamples_max;
346 data1 +=
s->nsamples_max;
347 nsamples -=
s->nsamples_max;
350 fast_convolute2(
s, kernel_buf, conv_buf, idx, data0 + nsamples/2, data1 + nsamples/2, nsamples - nsamples/2);
357 int rate =
ctx->inputs[0]->sample_rate;
361 int center =
s->fir_len / 2;
362 double delay =
s->zero_phase ? 0.0 : (double) center / rate;
366 s->analysis_buf[0] *=
s->rdft_len/2;
367 for (x = 1; x <= center; x++) {
368 s->analysis_buf[x] *=
s->rdft_len/2;
369 s->analysis_buf[
s->analysis_rdft_len - x] *=
s->rdft_len/2;
372 for (x = 0; x <
s->fir_len; x++)
373 s->analysis_buf[x] *=
s->rdft_len/2;
379 fprintf(
fp,
"# time[%d] (time amplitude)\n", ch);
382 for (x = center; x > 0; x--)
383 fprintf(
fp,
"%15.10f %15.10f\n", delay - (
double) x / rate, (
double)
s->analysis_buf[
s->analysis_rdft_len - x]);
385 for (x = 0; x <= center; x++)
386 fprintf(
fp,
"%15.10f %15.10f\n", delay + (
double)x / rate , (
double)
s->analysis_buf[x]);
388 for (x = 0; x <
s->fir_len; x++)
389 fprintf(
fp,
"%15.10f %15.10f\n", (
double)x / rate, (
double)
s->analysis_buf[x]);
394 fprintf(
fp,
"\n\n# freq[%d] (frequency desired_gain actual_gain)\n", ch);
396 for (x = 0; x <=
s->analysis_rdft_len/2; x++) {
397 int i = (x ==
s->analysis_rdft_len/2) ? 1 : 2 * x;
398 vx = (double)x * rate /
s->analysis_rdft_len;
402 yb =
s->min_phase && (
i > 1) ? hypotf(
s->analysis_buf[
i],
s->analysis_buf[
i+1]) :
s->analysis_buf[
i];
406 ya = 20.0 * log10(
fabs(ya));
407 yb = 20.0 * log10(
fabs(yb));
409 fprintf(
fp,
"%17.10f %17.10f %17.10f\n", vx, ya, yb);
420 s->gain_entry_err =
AVERROR(EINVAL);
426 s->gain_entry_err =
AVERROR(EINVAL);
430 if (
s->nb_gain_entry > 0 && freq <= s->gain_entry_tbl[
s->nb_gain_entry - 1].freq) {
432 s->gain_entry_err =
AVERROR(EINVAL);
436 s->gain_entry_tbl[
s->nb_gain_entry].freq = freq;
437 s->gain_entry_tbl[
s->nb_gain_entry].gain = gain;
444 const double *freq =
key;
447 if (*freq < entry[0].freq)
449 if (*freq > entry[1].freq)
464 if (!
s->nb_gain_entry)
467 if (freq <= s->gain_entry_tbl[0].freq)
468 return s->gain_entry_tbl[0].gain;
470 if (freq >=
s->gain_entry_tbl[
s->nb_gain_entry-1].freq)
471 return s->gain_entry_tbl[
s->nb_gain_entry-1].gain;
473 res = bsearch(&freq, &
s->gain_entry_tbl,
s->nb_gain_entry - 1,
sizeof(*res),
gain_entry_compare);
477 d0 = freq - res[0].
freq;
478 d1 = res[1].
freq - freq;
481 return (d0 * res[1].gain + d1 * res[0].gain) / d;
496 double m0, m1, m2, msum, unit;
498 if (!
s->nb_gain_entry)
501 if (freq <= s->gain_entry_tbl[0].freq)
502 return s->gain_entry_tbl[0].gain;
504 if (freq >=
s->gain_entry_tbl[
s->nb_gain_entry-1].freq)
505 return s->gain_entry_tbl[
s->nb_gain_entry-1].gain;
507 res = bsearch(&freq, &
s->gain_entry_tbl,
s->nb_gain_entry - 1,
sizeof(*res),
gain_entry_compare);
511 m0 = res !=
s->gain_entry_tbl ?
512 unit * (res[0].
gain - res[-1].
gain) / (res[0].freq - res[-1].freq) : 0;
514 m2 = res !=
s->gain_entry_tbl +
s->nb_gain_entry - 2 ?
515 unit * (res[2].
gain - res[1].
gain) / (res[2].freq - res[1].freq) : 0;
518 m0 = msum > 0 ? (
fabs(m0) * m1 +
fabs(m1) * m0) / msum : 0;
520 m1 = msum > 0 ? (
fabs(m1) * m2 +
fabs(m2) * m1) / msum : 0;
524 b = 3 * res[1].
gain - m1 - 2 *
c - 3 * d;
527 x = (freq - res[0].
freq) / unit;
531 return a * x3 +
b * x2 +
c * x + d;
556 int k, cepstrum_len =
s->cepstrum_len, rdft_len =
s->rdft_len;
557 double norm = 2.0 / cepstrum_len;
558 double minval = 1e-7 / rdft_len;
560 memset(
s->cepstrum_buf, 0, cepstrum_len *
sizeof(*
s->cepstrum_buf));
561 memcpy(
s->cepstrum_buf, rdft_buf, rdft_len/2 *
sizeof(*rdft_buf));
562 memcpy(
s->cepstrum_buf + cepstrum_len - rdft_len/2, rdft_buf + rdft_len/2, rdft_len/2 *
sizeof(*rdft_buf));
566 s->cepstrum_buf[0] = log(
FFMAX(
s->cepstrum_buf[0], minval));
567 s->cepstrum_buf[1] = log(
FFMAX(
s->cepstrum_buf[1], minval));
569 for (k = 2; k < cepstrum_len; k += 2) {
570 s->cepstrum_buf[k] = log(
FFMAX(
s->cepstrum_buf[k], minval));
571 s->cepstrum_buf[k+1] = 0;
576 memset(
s->cepstrum_buf + cepstrum_len/2 + 1, 0, (cepstrum_len/2 - 1) *
sizeof(*
s->cepstrum_buf));
577 for (k = 1; k < cepstrum_len/2; k++)
578 s->cepstrum_buf[k] *= 2;
582 s->cepstrum_buf[0] =
exp(
s->cepstrum_buf[0] * norm) * norm;
583 s->cepstrum_buf[1] =
exp(
s->cepstrum_buf[1] * norm) * norm;
584 for (k = 2; k < cepstrum_len; k += 2) {
585 double mag =
exp(
s->cepstrum_buf[k] * norm) * norm;
586 double ph =
s->cepstrum_buf[k+1] * norm;
587 s->cepstrum_buf[k] = mag * cos(ph);
588 s->cepstrum_buf[k+1] = mag * sin(ph);
592 memset(rdft_buf, 0,
s->rdft_len *
sizeof(*rdft_buf));
593 memcpy(rdft_buf,
s->cepstrum_buf,
s->fir_len *
sizeof(*rdft_buf));
596 memset(
s->analysis_buf, 0,
s->analysis_rdft_len *
sizeof(*
s->analysis_buf));
597 memcpy(
s->analysis_buf,
s->cepstrum_buf,
s->fir_len *
sizeof(*
s->analysis_buf));
606 const char *gain_entry_func_names[] = {
"entry",
NULL };
607 const char *gain_func_names[] = {
"gain_interpolate",
"cubic_interpolate",
NULL };
608 double (*gain_entry_funcs[])(
void *, double, double) = {
entry_func,
NULL };
612 int ret, k, center, ch;
615 FILE *dump_fp =
NULL;
617 s->nb_gain_entry = 0;
618 s->gain_entry_err = 0;
622 gain_entry_func_names, gain_entry_funcs,
ctx, 0,
ctx);
625 if (
s->gain_entry_err < 0)
626 return s->gain_entry_err;
636 if (
s->dumpfile && (!
s->dump_buf || !
s->analysis_rdft || !(dump_fp = fopen(
s->dumpfile,
"w"))))
642 for (ch = 0; ch <
inlink->channels; ch++) {
643 float *rdft_buf =
s->kernel_tmp_buf + ch *
s->rdft_len;
651 s->analysis_buf[0] = ylog ? pow(10.0, 0.05 *
result) :
result;
657 s->analysis_buf[1] = ylog ? pow(10.0, 0.05 *
result) :
result;
659 for (k = 1; k <
s->analysis_rdft_len/2; k++) {
660 vars[
VAR_F] = k * ((double)
inlink->sample_rate /(
double)
s->analysis_rdft_len);
665 s->analysis_buf[2*k+1] = 0.0;
669 memcpy(
s->dump_buf,
s->analysis_buf,
s->analysis_rdft_len *
sizeof(*
s->analysis_buf));
672 center =
s->fir_len / 2;
674 for (k = 0; k <= center; k++) {
675 double u = k * (
M_PI/center);
682 win = 0.5 + 0.5 * cos(
u);
685 win = 0.53836 + 0.46164 * cos(
u);
688 win = 0.42 + 0.5 * cos(
u) + 0.08 * cos(2*
u);
691 win = 0.40897 + 0.5 * cos(
u) + 0.09103 * cos(2*
u);
694 win = 0.4243801 + 0.4973406 * cos(
u) + 0.0782793 * cos(2*
u);
697 win = 0.355768 + 0.487396 * cos(
u) + 0.144232 * cos(2*
u) + 0.012604 * cos(3*
u);
700 win = 0.3635819 + 0.4891775 * cos(
u) + 0.1365995 * cos(2*
u) + 0.0106411 * cos(3*
u);
703 win = 0.35875 + 0.48829 * cos(
u) + 0.14128 * cos(2*
u) + 0.01168 * cos(3*
u);
706 win = (
u <= 0.5 *
M_PI) ? 1.0 : (0.5 + 0.5 * cos(2*
u -
M_PI));
711 s->analysis_buf[k] *= (2.0/
s->analysis_rdft_len) * (2.0/
s->rdft_len) *
win;
713 s->analysis_buf[
s->analysis_rdft_len - k] =
s->analysis_buf[k];
716 memset(
s->analysis_buf + center + 1, 0, (
s->analysis_rdft_len -
s->fir_len) *
sizeof(*
s->analysis_buf));
717 memcpy(rdft_buf,
s->analysis_buf,
s->rdft_len/2 *
sizeof(*
s->analysis_buf));
718 memcpy(rdft_buf +
s->rdft_len/2,
s->analysis_buf +
s->analysis_rdft_len -
s->rdft_len/2,
s->rdft_len/2 *
sizeof(*
s->analysis_buf));
723 for (k = 0; k <
s->rdft_len; k++) {
724 if (
isnan(rdft_buf[k]) ||
isinf(rdft_buf[k])) {
734 rdft_buf[
s->rdft_len-1] = rdft_buf[1];
735 for (k = 0; k <
s->rdft_len/2; k++)
736 rdft_buf[k] = rdft_buf[2*k];
737 rdft_buf[
s->rdft_len/2] = rdft_buf[
s->rdft_len-1];
747 memcpy(
s->kernel_buf,
s->kernel_tmp_buf, (
s->multi ?
inlink->channels : 1) *
s->rdft_len *
sizeof(*
s->kernel_buf));
754 #define SELECT_GAIN(s) (s->gain_cmd ? s->gain_cmd : s->gain)
755 #define SELECT_GAIN_ENTRY(s) (s->gain_entry_cmd ? s->gain_entry_cmd : s->gain_entry)
766 s->frame_nsamples_max = 0;
768 s->fir_len =
FFMAX(2 * (
int)(
inlink->sample_rate *
s->delay) + 1, 3);
769 s->remaining =
s->fir_len - 1;
772 s->rdft_len = 1 << rdft_bits;
773 s->nsamples_max =
s->rdft_len -
s->fir_len + 1;
774 if (
s->nsamples_max * 2 >=
s->fir_len)
790 int cepstrum_bits = rdft_bits + 2;
799 if (!
s->cepstrum_rdft || !
s->cepstrum_irdft)
802 s->cepstrum_len = 1 << cepstrum_bits;
804 if (!
s->cepstrum_buf)
809 s->analysis_rdft_len = 1 << rdft_bits;
810 if (
inlink->sample_rate <=
s->accuracy *
s->analysis_rdft_len)
832 if (!
s->analysis_buf || !
s->kernel_tmp_buf || !
s->kernel_buf || !
s->conv_buf || !
s->conv_idx)
835 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",
836 inlink->sample_rate,
inlink->channels,
s->analysis_rdft_len,
s->rdft_len,
s->fir_len,
s->nsamples_max);
851 for (ch = 0; ch + 1 <
inlink->channels &&
s->fft_ctx; ch += 2) {
853 s->conv_idx + ch, (
float *)
frame->extended_data[ch],
854 (
float *)
frame->extended_data[ch+1],
frame->nb_samples);
857 for ( ; ch <
inlink->channels; ch++) {
859 s->conv_buf + 2 * ch *
s->rdft_len,
s->conv_idx + ch,
860 (
float *)
frame->extended_data[ch],
frame->nb_samples);
863 for (ch = 0; ch <
inlink->channels; ch++) {
865 s->conv_buf + 2 * ch *
s->rdft_len,
s->conv_idx + ch,
866 (
float *)
frame->extended_data[ch],
frame->nb_samples);
873 if (
s->zero_phase && !
s->min_phase)
876 s->frame_nsamples_max =
FFMAX(
s->frame_nsamples_max,
frame->nb_samples);
895 s->remaining -=
frame->nb_samples;
903 char *res,
int res_len,
int flags)
908 if (!strcmp(cmd,
"gain")) {
923 s->gain_cmd = gain_cmd;
927 }
else if (!strcmp(cmd,
"gain_entry")) {
928 char *gain_entry_cmd;
942 s->gain_entry_cmd = gain_entry_cmd;
972 .
name =
"firequalizer",
980 .priv_class = &firequalizer_class,