30 #define RDFT_BITS_MIN 4
31 #define RDFT_BITS_MAX 16
55 #define NB_GAIN_ENTRY_MAX 4096
114 #define OFFSET(x) offsetof(FIREqualizerContext, x)
115 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
116 #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
160 s->analysis_rdft =
s->analysis_irdft =
s->rdft =
s->irdft =
NULL;
162 s->cepstrum_rdft =
NULL;
163 s->cepstrum_irdft =
NULL;
186 if (nsamples <= s->nsamples_max) {
187 float *buf = conv_buf + idx->buf_idx *
s->rdft_len;
188 float *obuf = conv_buf + !idx->buf_idx *
s->rdft_len + idx->overlap_idx;
189 int center =
s->fir_len/2;
192 memset(buf, 0, center *
sizeof(*
data));
193 memcpy(buf + center,
data, nsamples *
sizeof(*
data));
194 memset(buf + center + nsamples, 0, (
s->rdft_len - nsamples - center) *
sizeof(*
data));
197 buf[0] *= kernel_buf[0];
198 buf[1] *= kernel_buf[
s->rdft_len/2];
199 for (k = 1; k <
s->rdft_len/2; k++) {
200 buf[2*k] *= kernel_buf[k];
201 buf[2*k+1] *= kernel_buf[k];
205 for (k = 0; k <
s->rdft_len - idx->overlap_idx; k++)
207 memcpy(
data, buf, nsamples *
sizeof(*
data));
208 idx->buf_idx = !idx->buf_idx;
209 idx->overlap_idx = nsamples;
211 while (nsamples >
s->nsamples_max * 2) {
213 data +=
s->nsamples_max;
214 nsamples -=
s->nsamples_max;
222 float *av_restrict conv_buf,
OverlapIndex *av_restrict idx,
223 float *av_restrict
data,
int nsamples)
225 if (nsamples <= s->nsamples_max) {
226 float *buf = conv_buf + idx->buf_idx *
s->rdft_len;
227 float *obuf = conv_buf + !idx->buf_idx *
s->rdft_len + idx->overlap_idx;
230 memcpy(buf,
data, nsamples *
sizeof(*
data));
231 memset(buf + nsamples, 0, (
s->rdft_len - nsamples) *
sizeof(*
data));
234 buf[0] *= kernel_buf[0];
235 buf[1] *= kernel_buf[1];
236 for (k = 2; k <
s->rdft_len; k += 2) {
238 re = buf[k] * kernel_buf[k] - buf[k+1] * kernel_buf[k+1];
239 im = buf[k] * kernel_buf[k+1] + buf[k+1] * kernel_buf[k];
245 for (k = 0; k <
s->rdft_len - idx->overlap_idx; k++)
247 memcpy(
data, buf, nsamples *
sizeof(*
data));
248 idx->buf_idx = !idx->buf_idx;
249 idx->overlap_idx = nsamples;
251 while (nsamples >
s->nsamples_max * 2) {
253 data +=
s->nsamples_max;
254 nsamples -=
s->nsamples_max;
262 OverlapIndex *av_restrict idx,
float *av_restrict data0,
float *av_restrict data1,
int nsamples)
264 if (nsamples <= s->nsamples_max) {
265 FFTComplex *buf = conv_buf + idx->buf_idx *
s->rdft_len;
266 FFTComplex *obuf = conv_buf + !idx->buf_idx *
s->rdft_len + idx->overlap_idx;
267 int center =
s->fir_len/2;
271 memset(buf, 0, center *
sizeof(*buf));
272 for (k = 0; k < nsamples; k++) {
273 buf[center+k].
re = data0[k];
274 buf[center+k].
im = data1[k];
276 memset(buf + center + nsamples, 0, (
s->rdft_len - nsamples - center) *
sizeof(*buf));
283 buf[0].
re = 0.5f * kernel_buf[0] * buf[0].
im;
284 buf[0].
im = 0.5f * kernel_buf[0] *
tmp;
285 for (k = 1; k <
s->rdft_len/2; k++) {
286 int m =
s->rdft_len - k;
288 buf[k].
re = 0.5f * kernel_buf[k] * buf[k].
im;
289 buf[k].
im = 0.5f * kernel_buf[k] *
tmp;
291 buf[m].
re = 0.5f * kernel_buf[k] * buf[m].
im;
292 buf[m].
im = 0.5f * kernel_buf[k] *
tmp;
295 buf[k].
re = 0.5f * kernel_buf[k] * buf[k].
im;
296 buf[k].
im = 0.5f * kernel_buf[k] *
tmp;
301 for (k = 0; k <
s->rdft_len - idx->overlap_idx; k++) {
302 buf[k].
re += obuf[k].
re;
303 buf[k].
im += obuf[k].
im;
307 for (k = 0; k < nsamples; k++) {
308 data0[k] = buf[k].
im;
309 data1[k] = buf[k].
re;
311 idx->buf_idx = !idx->buf_idx;
312 idx->overlap_idx = nsamples;
314 while (nsamples >
s->nsamples_max * 2) {
316 data0 +=
s->nsamples_max;
317 data1 +=
s->nsamples_max;
318 nsamples -=
s->nsamples_max;
321 fast_convolute2(
s, kernel_buf, conv_buf, idx, data0 + nsamples/2, data1 + nsamples/2, nsamples - nsamples/2);
328 int rate =
ctx->inputs[0]->sample_rate;
332 int center =
s->fir_len / 2;
333 double delay =
s->zero_phase ? 0.0 : (
double) center / rate;
337 s->analysis_buf[0] *=
s->rdft_len/2;
338 for (x = 1; x <= center; x++) {
339 s->analysis_buf[x] *=
s->rdft_len/2;
340 s->analysis_buf[
s->analysis_rdft_len - x] *=
s->rdft_len/2;
343 for (x = 0; x <
s->fir_len; x++)
344 s->analysis_buf[x] *=
s->rdft_len/2;
350 fprintf(
fp,
"# time[%d] (time amplitude)\n", ch);
353 for (x = center; x > 0; x--)
354 fprintf(
fp,
"%15.10f %15.10f\n", delay - (
double) x / rate, (
double)
s->analysis_buf[
s->analysis_rdft_len - x]);
356 for (x = 0; x <= center; x++)
357 fprintf(
fp,
"%15.10f %15.10f\n", delay + (
double)x / rate , (
double)
s->analysis_buf[x]);
359 for (x = 0; x <
s->fir_len; x++)
360 fprintf(
fp,
"%15.10f %15.10f\n", (
double)x / rate, (
double)
s->analysis_buf[x]);
365 fprintf(
fp,
"\n\n# freq[%d] (frequency desired_gain actual_gain)\n", ch);
367 for (x = 0; x <=
s->analysis_rdft_len/2; x++) {
368 int i = (x ==
s->analysis_rdft_len/2) ? 1 : 2 * x;
369 vx = (
double)x * rate /
s->analysis_rdft_len;
373 yb =
s->min_phase && (
i > 1) ? hypotf(
s->analysis_buf[
i],
s->analysis_buf[
i+1]) :
s->analysis_buf[
i];
377 ya = 20.0 * log10(
fabs(ya));
378 yb = 20.0 * log10(
fabs(yb));
380 fprintf(
fp,
"%17.10f %17.10f %17.10f\n", vx, ya, yb);
391 s->gain_entry_err =
AVERROR(EINVAL);
397 s->gain_entry_err =
AVERROR(EINVAL);
401 if (
s->nb_gain_entry > 0 && freq <= s->gain_entry_tbl[
s->nb_gain_entry - 1].freq) {
403 s->gain_entry_err =
AVERROR(EINVAL);
407 s->gain_entry_tbl[
s->nb_gain_entry].freq = freq;
408 s->gain_entry_tbl[
s->nb_gain_entry].gain = gain;
415 const double *freq =
key;
418 if (*freq < entry[0].freq)
420 if (*freq > entry[1].freq)
435 if (!
s->nb_gain_entry)
438 if (freq <= s->gain_entry_tbl[0].freq)
439 return s->gain_entry_tbl[0].gain;
441 if (freq >=
s->gain_entry_tbl[
s->nb_gain_entry-1].freq)
442 return s->gain_entry_tbl[
s->nb_gain_entry-1].gain;
444 res = bsearch(&freq, &
s->gain_entry_tbl,
s->nb_gain_entry - 1,
sizeof(*res),
gain_entry_compare);
448 d0 = freq - res[0].
freq;
449 d1 = res[1].
freq - freq;
452 return (d0 * res[1].gain + d1 * res[0].gain) /
d;
467 double m0, m1, m2, msum, unit;
469 if (!
s->nb_gain_entry)
472 if (freq <= s->gain_entry_tbl[0].freq)
473 return s->gain_entry_tbl[0].gain;
475 if (freq >=
s->gain_entry_tbl[
s->nb_gain_entry-1].freq)
476 return s->gain_entry_tbl[
s->nb_gain_entry-1].gain;
478 res = bsearch(&freq, &
s->gain_entry_tbl,
s->nb_gain_entry - 1,
sizeof(*res),
gain_entry_compare);
482 m0 = res !=
s->gain_entry_tbl ?
483 unit * (res[0].
gain - res[-1].
gain) / (res[0].freq - res[-1].freq) : 0;
485 m2 = res !=
s->gain_entry_tbl +
s->nb_gain_entry - 2 ?
486 unit * (res[2].
gain - res[1].
gain) / (res[2].freq - res[1].freq) : 0;
489 m0 = msum > 0 ? (
fabs(m0) * m1 +
fabs(m1) * m0) / msum : 0;
491 m1 = msum > 0 ? (
fabs(m1) * m2 +
fabs(m2) * m1) / msum : 0;
495 b = 3 * res[1].
gain - m1 - 2 *
c - 3 *
d;
498 x = (freq - res[0].
freq) / unit;
502 return a * x3 +
b * x2 +
c * x +
d;
527 int k, cepstrum_len =
s->cepstrum_len, rdft_len =
s->rdft_len;
528 double norm = 2.0 / cepstrum_len;
529 double minval = 1e-7 / rdft_len;
531 memset(
s->cepstrum_buf, 0, cepstrum_len *
sizeof(*
s->cepstrum_buf));
532 memcpy(
s->cepstrum_buf, rdft_buf, rdft_len/2 *
sizeof(*rdft_buf));
533 memcpy(
s->cepstrum_buf + cepstrum_len - rdft_len/2, rdft_buf + rdft_len/2, rdft_len/2 *
sizeof(*rdft_buf));
537 s->cepstrum_buf[0] = log(
FFMAX(
s->cepstrum_buf[0], minval));
538 s->cepstrum_buf[1] = log(
FFMAX(
s->cepstrum_buf[1], minval));
540 for (k = 2; k < cepstrum_len; k += 2) {
541 s->cepstrum_buf[k] = log(
FFMAX(
s->cepstrum_buf[k], minval));
542 s->cepstrum_buf[k+1] = 0;
547 memset(
s->cepstrum_buf + cepstrum_len/2 + 1, 0, (cepstrum_len/2 - 1) *
sizeof(*
s->cepstrum_buf));
548 for (k = 1; k < cepstrum_len/2; k++)
549 s->cepstrum_buf[k] *= 2;
553 s->cepstrum_buf[0] =
exp(
s->cepstrum_buf[0] * norm) * norm;
554 s->cepstrum_buf[1] =
exp(
s->cepstrum_buf[1] * norm) * norm;
555 for (k = 2; k < cepstrum_len; k += 2) {
556 double mag =
exp(
s->cepstrum_buf[k] * norm) * norm;
557 double ph =
s->cepstrum_buf[k+1] * norm;
558 s->cepstrum_buf[k] = mag * cos(ph);
559 s->cepstrum_buf[k+1] = mag * sin(ph);
563 memset(rdft_buf, 0,
s->rdft_len *
sizeof(*rdft_buf));
564 memcpy(rdft_buf,
s->cepstrum_buf,
s->fir_len *
sizeof(*rdft_buf));
567 memset(
s->analysis_buf, 0,
s->analysis_rdft_len *
sizeof(*
s->analysis_buf));
568 memcpy(
s->analysis_buf,
s->cepstrum_buf,
s->fir_len *
sizeof(*
s->analysis_buf));
577 const char *gain_entry_func_names[] = {
"entry",
NULL };
578 const char *gain_func_names[] = {
"gain_interpolate",
"cubic_interpolate",
NULL };
583 int ret, k, center, ch;
586 FILE *dump_fp =
NULL;
588 s->nb_gain_entry = 0;
589 s->gain_entry_err = 0;
593 gain_entry_func_names, gain_entry_funcs,
ctx, 0,
ctx);
596 if (
s->gain_entry_err < 0)
597 return s->gain_entry_err;
607 if (
s->dumpfile && (!
s->dump_buf || !
s->analysis_rdft || !(dump_fp =
avpriv_fopen_utf8(
s->dumpfile,
"w"))))
612 inlink->ch_layout.u.mask : 0;
614 for (ch = 0; ch <
inlink->ch_layout.nb_channels; ch++) {
615 float *rdft_buf =
s->kernel_tmp_buf + ch *
s->rdft_len;
623 s->analysis_buf[0] = ylog ? pow(10.0, 0.05 *
result) :
result;
629 s->analysis_buf[1] = ylog ? pow(10.0, 0.05 *
result) :
result;
631 for (k = 1; k <
s->analysis_rdft_len/2; k++) {
637 s->analysis_buf[2*k+1] = 0.0;
641 memcpy(
s->dump_buf,
s->analysis_buf,
s->analysis_rdft_len *
sizeof(*
s->analysis_buf));
644 center =
s->fir_len / 2;
646 for (k = 0; k <= center; k++) {
647 double u = k * (
M_PI/center);
654 win = 0.5 + 0.5 * cos(
u);
657 win = 0.53836 + 0.46164 * cos(
u);
660 win = 0.42 + 0.5 * cos(
u) + 0.08 * cos(2*
u);
663 win = 0.40897 + 0.5 * cos(
u) + 0.09103 * cos(2*
u);
666 win = 0.4243801 + 0.4973406 * cos(
u) + 0.0782793 * cos(2*
u);
669 win = 0.355768 + 0.487396 * cos(
u) + 0.144232 * cos(2*
u) + 0.012604 * cos(3*
u);
672 win = 0.3635819 + 0.4891775 * cos(
u) + 0.1365995 * cos(2*
u) + 0.0106411 * cos(3*
u);
675 win = 0.35875 + 0.48829 * cos(
u) + 0.14128 * cos(2*
u) + 0.01168 * cos(3*
u);
678 win = (
u <= 0.5 *
M_PI) ? 1.0 : (0.5 + 0.5 * cos(2*
u -
M_PI));
683 s->analysis_buf[k] *= (2.0/
s->analysis_rdft_len) * (2.0/
s->rdft_len) *
win;
685 s->analysis_buf[
s->analysis_rdft_len - k] =
s->analysis_buf[k];
688 memset(
s->analysis_buf + center + 1, 0, (
s->analysis_rdft_len -
s->fir_len) *
sizeof(*
s->analysis_buf));
689 memcpy(rdft_buf,
s->analysis_buf,
s->rdft_len/2 *
sizeof(*
s->analysis_buf));
690 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));
695 for (k = 0; k <
s->rdft_len; k++) {
696 if (
isnan(rdft_buf[k]) ||
isinf(rdft_buf[k])) {
706 rdft_buf[
s->rdft_len-1] = rdft_buf[1];
707 for (k = 0; k <
s->rdft_len/2; k++)
708 rdft_buf[k] = rdft_buf[2*k];
709 rdft_buf[
s->rdft_len/2] = rdft_buf[
s->rdft_len-1];
719 memcpy(
s->kernel_buf,
s->kernel_tmp_buf, (
s->multi ?
inlink->ch_layout.nb_channels : 1) *
s->rdft_len *
sizeof(*
s->kernel_buf));
726 #define SELECT_GAIN(s) (s->gain_cmd ? s->gain_cmd : s->gain)
727 #define SELECT_GAIN_ENTRY(s) (s->gain_entry_cmd ? s->gain_entry_cmd : s->gain_entry)
738 s->frame_nsamples_max = 0;
740 s->fir_len =
FFMAX(2 * (
int)(
inlink->sample_rate *
s->delay) + 1, 3);
741 s->remaining =
s->fir_len - 1;
744 s->rdft_len = 1 << rdft_bits;
745 s->nsamples_max =
s->rdft_len -
s->fir_len + 1;
746 if (
s->nsamples_max * 2 >=
s->fir_len)
758 if (
s->fft2 && !
s->multi &&
inlink->ch_layout.nb_channels > 1 && !(
s->fft_ctx =
av_fft_init(rdft_bits, 0)))
762 int cepstrum_bits = rdft_bits + 2;
771 if (!
s->cepstrum_rdft || !
s->cepstrum_irdft)
774 s->cepstrum_len = 1 << cepstrum_bits;
776 if (!
s->cepstrum_buf)
781 s->analysis_rdft_len = 1 << rdft_bits;
782 if (
inlink->sample_rate <=
s->accuracy *
s->analysis_rdft_len)
800 s->kernel_tmp_buf =
av_malloc_array(
s->rdft_len * (
s->multi ?
inlink->ch_layout.nb_channels : 1),
sizeof(*
s->kernel_tmp_buf));
802 s->conv_buf =
av_calloc(2 *
s->rdft_len *
inlink->ch_layout.nb_channels,
sizeof(*
s->conv_buf));
804 if (!
s->analysis_buf || !
s->kernel_tmp_buf || !
s->kernel_buf || !
s->conv_buf || !
s->conv_idx)
807 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",
808 inlink->sample_rate,
inlink->ch_layout.nb_channels,
s->analysis_rdft_len,
s->rdft_len,
s->fir_len,
s->nsamples_max);
823 for (ch = 0; ch + 1 <
inlink->ch_layout.nb_channels &&
s->fft_ctx; ch += 2) {
825 s->conv_idx + ch, (
float *)
frame->extended_data[ch],
826 (
float *)
frame->extended_data[ch+1],
frame->nb_samples);
829 for ( ; ch <
inlink->ch_layout.nb_channels; ch++) {
831 s->conv_buf + 2 * ch *
s->rdft_len,
s->conv_idx + ch,
832 (
float *)
frame->extended_data[ch],
frame->nb_samples);
835 for (ch = 0; ch <
inlink->ch_layout.nb_channels; ch++) {
837 s->conv_buf + 2 * ch *
s->rdft_len,
s->conv_idx + ch,
838 (
float *)
frame->extended_data[ch],
frame->nb_samples);
845 if (
s->zero_phase && !
s->min_phase)
848 s->frame_nsamples_max =
FFMAX(
s->frame_nsamples_max,
frame->nb_samples);
867 s->remaining -=
frame->nb_samples;
875 char *res,
int res_len,
int flags)
880 if (!strcmp(cmd,
"gain")) {
895 s->gain_cmd = gain_cmd;
899 }
else if (!strcmp(cmd,
"gain_entry")) {
900 char *gain_entry_cmd;
914 s->gain_entry_cmd = gain_entry_cmd;
942 .
name =
"firequalizer",
950 .priv_class = &firequalizer_class,