You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

982 lines
35KB

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