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.

712 lines
24KB

  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 {
  51. double freq;
  52. double gain;
  53. } GainEntry;
  54. typedef struct {
  55. int buf_idx;
  56. int overlap_idx;
  57. } OverlapIndex;
  58. typedef struct {
  59. const AVClass *class;
  60. RDFTContext *analysis_rdft;
  61. RDFTContext *analysis_irdft;
  62. RDFTContext *rdft;
  63. RDFTContext *irdft;
  64. int analysis_rdft_len;
  65. int rdft_len;
  66. float *analysis_buf;
  67. float *dump_buf;
  68. float *kernel_tmp_buf;
  69. float *kernel_buf;
  70. float *conv_buf;
  71. OverlapIndex *conv_idx;
  72. int fir_len;
  73. int nsamples_max;
  74. int64_t next_pts;
  75. int frame_nsamples_max;
  76. int remaining;
  77. char *gain_cmd;
  78. char *gain_entry_cmd;
  79. const char *gain;
  80. const char *gain_entry;
  81. double delay;
  82. double accuracy;
  83. int wfunc;
  84. int fixed;
  85. int multi;
  86. int zero_phase;
  87. int scale;
  88. char *dumpfile;
  89. int dumpscale;
  90. int nb_gain_entry;
  91. int gain_entry_err;
  92. GainEntry gain_entry_tbl[NB_GAIN_ENTRY_MAX];
  93. } FIREqualizerContext;
  94. #define OFFSET(x) offsetof(FIREqualizerContext, x)
  95. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  96. static const AVOption firequalizer_options[] = {
  97. { "gain", "set gain curve", OFFSET(gain), AV_OPT_TYPE_STRING, { .str = "gain_interpolate(f)" }, 0, 0, FLAGS },
  98. { "gain_entry", "set gain entry", OFFSET(gain_entry), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
  99. { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.0, 1e10, FLAGS },
  100. { "accuracy", "set accuracy", OFFSET(accuracy), AV_OPT_TYPE_DOUBLE, { .dbl = 5.0 }, 0.0, 1e10, FLAGS },
  101. { "wfunc", "set window function", OFFSET(wfunc), AV_OPT_TYPE_INT, { .i64 = WFUNC_HANN }, 0, NB_WFUNC-1, FLAGS, "wfunc" },
  102. { "rectangular", "rectangular window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_RECTANGULAR }, 0, 0, FLAGS, "wfunc" },
  103. { "hann", "hann window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HANN }, 0, 0, FLAGS, "wfunc" },
  104. { "hamming", "hamming window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HAMMING }, 0, 0, FLAGS, "wfunc" },
  105. { "blackman", "blackman window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BLACKMAN }, 0, 0, FLAGS, "wfunc" },
  106. { "nuttall3", "3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL3 }, 0, 0, FLAGS, "wfunc" },
  107. { "mnuttall3", "minimum 3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_MNUTTALL3 }, 0, 0, FLAGS, "wfunc" },
  108. { "nuttall", "nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL }, 0, 0, FLAGS, "wfunc" },
  109. { "bnuttall", "blackman-nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BNUTTALL }, 0, 0, FLAGS, "wfunc" },
  110. { "bharris", "blackman-harris window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BHARRIS }, 0, 0, FLAGS, "wfunc" },
  111. { "tukey", "tukey window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_TUKEY }, 0, 0, FLAGS, "wfunc" },
  112. { "fixed", "set fixed frame samples", OFFSET(fixed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  113. { "multi", "set multi channels mode", OFFSET(multi), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  114. { "zero_phase", "set zero phase mode", OFFSET(zero_phase), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  115. { "scale", "set gain scale", OFFSET(scale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, "scale" },
  116. { "linlin", "linear-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLIN }, 0, 0, FLAGS, "scale" },
  117. { "linlog", "linear-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLOG }, 0, 0, FLAGS, "scale" },
  118. { "loglin", "logarithmic-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLIN }, 0, 0, FLAGS, "scale" },
  119. { "loglog", "logarithmic-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLOG }, 0, 0, FLAGS, "scale" },
  120. { "dumpfile", "set dump file", OFFSET(dumpfile), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
  121. { "dumpscale", "set dump scale", OFFSET(dumpscale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, "scale" },
  122. { NULL }
  123. };
  124. AVFILTER_DEFINE_CLASS(firequalizer);
  125. static void common_uninit(FIREqualizerContext *s)
  126. {
  127. av_rdft_end(s->analysis_rdft);
  128. av_rdft_end(s->analysis_irdft);
  129. av_rdft_end(s->rdft);
  130. av_rdft_end(s->irdft);
  131. s->analysis_rdft = s->analysis_irdft = s->rdft = s->irdft = NULL;
  132. av_freep(&s->analysis_buf);
  133. av_freep(&s->dump_buf);
  134. av_freep(&s->kernel_tmp_buf);
  135. av_freep(&s->kernel_buf);
  136. av_freep(&s->conv_buf);
  137. av_freep(&s->conv_idx);
  138. }
  139. static av_cold void uninit(AVFilterContext *ctx)
  140. {
  141. FIREqualizerContext *s = ctx->priv;
  142. common_uninit(s);
  143. av_freep(&s->gain_cmd);
  144. av_freep(&s->gain_entry_cmd);
  145. }
  146. static int query_formats(AVFilterContext *ctx)
  147. {
  148. AVFilterChannelLayouts *layouts;
  149. AVFilterFormats *formats;
  150. static const enum AVSampleFormat sample_fmts[] = {
  151. AV_SAMPLE_FMT_FLTP,
  152. AV_SAMPLE_FMT_NONE
  153. };
  154. int ret;
  155. layouts = ff_all_channel_counts();
  156. if (!layouts)
  157. return AVERROR(ENOMEM);
  158. ret = ff_set_common_channel_layouts(ctx, layouts);
  159. if (ret < 0)
  160. return ret;
  161. formats = ff_make_format_list(sample_fmts);
  162. if (!formats)
  163. return AVERROR(ENOMEM);
  164. ret = ff_set_common_formats(ctx, formats);
  165. if (ret < 0)
  166. return ret;
  167. formats = ff_all_samplerates();
  168. if (!formats)
  169. return AVERROR(ENOMEM);
  170. return ff_set_common_samplerates(ctx, formats);
  171. }
  172. static void fast_convolute(FIREqualizerContext *s, const float *kernel_buf, float *conv_buf,
  173. OverlapIndex *idx, float *data, int nsamples)
  174. {
  175. if (nsamples <= s->nsamples_max) {
  176. float *buf = conv_buf + idx->buf_idx * s->rdft_len;
  177. float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx;
  178. int k;
  179. memcpy(buf, data, nsamples * sizeof(*data));
  180. memset(buf + nsamples, 0, (s->rdft_len - nsamples) * sizeof(*data));
  181. av_rdft_calc(s->rdft, buf);
  182. buf[0] *= kernel_buf[0];
  183. buf[1] *= kernel_buf[1];
  184. for (k = 2; k < s->rdft_len; k += 2) {
  185. float re, im;
  186. re = buf[k] * kernel_buf[k] - buf[k+1] * kernel_buf[k+1];
  187. im = buf[k] * kernel_buf[k+1] + buf[k+1] * kernel_buf[k];
  188. buf[k] = re;
  189. buf[k+1] = im;
  190. }
  191. av_rdft_calc(s->irdft, buf);
  192. for (k = 0; k < s->rdft_len - idx->overlap_idx; k++)
  193. buf[k] += obuf[k];
  194. memcpy(data, buf, nsamples * sizeof(*data));
  195. idx->buf_idx = !idx->buf_idx;
  196. idx->overlap_idx = nsamples;
  197. } else {
  198. while (nsamples > s->nsamples_max * 2) {
  199. fast_convolute(s, kernel_buf, conv_buf, idx, data, s->nsamples_max);
  200. data += s->nsamples_max;
  201. nsamples -= s->nsamples_max;
  202. }
  203. fast_convolute(s, kernel_buf, conv_buf, idx, data, nsamples/2);
  204. fast_convolute(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2);
  205. }
  206. }
  207. static void dump_fir(AVFilterContext *ctx, FILE *fp, int ch)
  208. {
  209. FIREqualizerContext *s = ctx->priv;
  210. int rate = ctx->inputs[0]->sample_rate;
  211. int xlog = s->dumpscale == SCALE_LOGLIN || s->dumpscale == SCALE_LOGLOG;
  212. int ylog = s->dumpscale == SCALE_LINLOG || s->dumpscale == SCALE_LOGLOG;
  213. int x;
  214. int center = s->fir_len / 2;
  215. double delay = s->zero_phase ? 0.0 : (double) center / rate;
  216. double vx, ya, yb;
  217. s->analysis_buf[0] *= s->rdft_len/2;
  218. for (x = 1; x <= center; x++) {
  219. s->analysis_buf[x] *= s->rdft_len/2;
  220. s->analysis_buf[s->analysis_rdft_len - x] *= s->rdft_len/2;
  221. }
  222. if (ch)
  223. fprintf(fp, "\n\n");
  224. fprintf(fp, "# time[%d] (time amplitude)\n", ch);
  225. for (x = center; x > 0; x--)
  226. fprintf(fp, "%15.10f %15.10f\n", delay - (double) x / rate, (double) s->analysis_buf[s->analysis_rdft_len - x]);
  227. for (x = 0; x <= center; x++)
  228. fprintf(fp, "%15.10f %15.10f\n", delay + (double)x / rate , (double) s->analysis_buf[x]);
  229. av_rdft_calc(s->analysis_rdft, s->analysis_buf);
  230. fprintf(fp, "\n\n# freq[%d] (frequency desired_gain actual_gain)\n", ch);
  231. for (x = 0; x <= s->analysis_rdft_len/2; x++) {
  232. int i = (x == s->analysis_rdft_len/2) ? 1 : 2 * x;
  233. vx = (double)x * rate / s->analysis_rdft_len;
  234. if (xlog)
  235. vx = log2(0.05*vx);
  236. ya = s->dump_buf[i];
  237. yb = s->analysis_buf[i];
  238. if (ylog) {
  239. ya = 20.0 * log10(fabs(ya));
  240. yb = 20.0 * log10(fabs(yb));
  241. }
  242. fprintf(fp, "%17.10f %17.10f %17.10f\n", vx, ya, yb);
  243. }
  244. }
  245. static double entry_func(void *p, double freq, double gain)
  246. {
  247. AVFilterContext *ctx = p;
  248. FIREqualizerContext *s = ctx->priv;
  249. if (s->nb_gain_entry >= NB_GAIN_ENTRY_MAX) {
  250. av_log(ctx, AV_LOG_ERROR, "entry table overflow.\n");
  251. s->gain_entry_err = AVERROR(EINVAL);
  252. return 0;
  253. }
  254. if (isnan(freq)) {
  255. av_log(ctx, AV_LOG_ERROR, "nan frequency (%g, %g).\n", freq, gain);
  256. s->gain_entry_err = AVERROR(EINVAL);
  257. return 0;
  258. }
  259. if (s->nb_gain_entry > 0 && freq <= s->gain_entry_tbl[s->nb_gain_entry - 1].freq) {
  260. av_log(ctx, AV_LOG_ERROR, "unsorted frequency (%g, %g).\n", freq, gain);
  261. s->gain_entry_err = AVERROR(EINVAL);
  262. return 0;
  263. }
  264. s->gain_entry_tbl[s->nb_gain_entry].freq = freq;
  265. s->gain_entry_tbl[s->nb_gain_entry].gain = gain;
  266. s->nb_gain_entry++;
  267. return 0;
  268. }
  269. static int gain_entry_compare(const void *key, const void *memb)
  270. {
  271. const double *freq = key;
  272. const GainEntry *entry = memb;
  273. if (*freq < entry[0].freq)
  274. return -1;
  275. if (*freq > entry[1].freq)
  276. return 1;
  277. return 0;
  278. }
  279. static double gain_interpolate_func(void *p, double freq)
  280. {
  281. AVFilterContext *ctx = p;
  282. FIREqualizerContext *s = ctx->priv;
  283. GainEntry *res;
  284. double d0, d1, d;
  285. if (isnan(freq))
  286. return freq;
  287. if (!s->nb_gain_entry)
  288. return 0;
  289. if (freq <= s->gain_entry_tbl[0].freq)
  290. return s->gain_entry_tbl[0].gain;
  291. if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq)
  292. return s->gain_entry_tbl[s->nb_gain_entry-1].gain;
  293. res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare);
  294. av_assert0(res);
  295. d = res[1].freq - res[0].freq;
  296. d0 = freq - res[0].freq;
  297. d1 = res[1].freq - freq;
  298. if (d0 && d1)
  299. return (d0 * res[1].gain + d1 * res[0].gain) / d;
  300. if (d0)
  301. return res[1].gain;
  302. return res[0].gain;
  303. }
  304. static const char *const var_names[] = {
  305. "f",
  306. "sr",
  307. "ch",
  308. "chid",
  309. "chs",
  310. "chlayout",
  311. NULL
  312. };
  313. enum VarOffset {
  314. VAR_F,
  315. VAR_SR,
  316. VAR_CH,
  317. VAR_CHID,
  318. VAR_CHS,
  319. VAR_CHLAYOUT,
  320. VAR_NB
  321. };
  322. static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *gain_entry)
  323. {
  324. FIREqualizerContext *s = ctx->priv;
  325. AVFilterLink *inlink = ctx->inputs[0];
  326. const char *gain_entry_func_names[] = { "entry", NULL };
  327. const char *gain_func_names[] = { "gain_interpolate", NULL };
  328. double (*gain_entry_funcs[])(void *, double, double) = { entry_func, NULL };
  329. double (*gain_funcs[])(void *, double) = { gain_interpolate_func, NULL };
  330. double vars[VAR_NB];
  331. AVExpr *gain_expr;
  332. int ret, k, center, ch;
  333. int xlog = s->scale == SCALE_LOGLIN || s->scale == SCALE_LOGLOG;
  334. int ylog = s->scale == SCALE_LINLOG || s->scale == SCALE_LOGLOG;
  335. FILE *dump_fp = NULL;
  336. s->nb_gain_entry = 0;
  337. s->gain_entry_err = 0;
  338. if (gain_entry) {
  339. double result = 0.0;
  340. ret = av_expr_parse_and_eval(&result, gain_entry, NULL, NULL, NULL, NULL,
  341. gain_entry_func_names, gain_entry_funcs, ctx, 0, ctx);
  342. if (ret < 0)
  343. return ret;
  344. if (s->gain_entry_err < 0)
  345. return s->gain_entry_err;
  346. }
  347. av_log(ctx, AV_LOG_DEBUG, "nb_gain_entry = %d.\n", s->nb_gain_entry);
  348. ret = av_expr_parse(&gain_expr, gain, var_names,
  349. gain_func_names, gain_funcs, NULL, NULL, 0, ctx);
  350. if (ret < 0)
  351. return ret;
  352. if (s->dumpfile && (!s->dump_buf || !s->analysis_rdft || !(dump_fp = fopen(s->dumpfile, "w"))))
  353. av_log(ctx, AV_LOG_WARNING, "dumping failed.\n");
  354. vars[VAR_CHS] = inlink->channels;
  355. vars[VAR_CHLAYOUT] = inlink->channel_layout;
  356. vars[VAR_SR] = inlink->sample_rate;
  357. for (ch = 0; ch < inlink->channels; ch++) {
  358. float *rdft_buf = s->kernel_tmp_buf + ch * s->rdft_len;
  359. double result;
  360. vars[VAR_CH] = ch;
  361. vars[VAR_CHID] = av_channel_layout_extract_channel(inlink->channel_layout, ch);
  362. vars[VAR_F] = 0.0;
  363. if (xlog)
  364. vars[VAR_F] = log2(0.05 * vars[VAR_F]);
  365. result = av_expr_eval(gain_expr, vars, ctx);
  366. s->analysis_buf[0] = ylog ? pow(10.0, 0.05 * result) : result;
  367. vars[VAR_F] = 0.5 * inlink->sample_rate;
  368. if (xlog)
  369. vars[VAR_F] = log2(0.05 * vars[VAR_F]);
  370. result = av_expr_eval(gain_expr, vars, ctx);
  371. s->analysis_buf[1] = ylog ? pow(10.0, 0.05 * result) : result;
  372. for (k = 1; k < s->analysis_rdft_len/2; k++) {
  373. vars[VAR_F] = k * ((double)inlink->sample_rate /(double)s->analysis_rdft_len);
  374. if (xlog)
  375. vars[VAR_F] = log2(0.05 * vars[VAR_F]);
  376. result = av_expr_eval(gain_expr, vars, ctx);
  377. s->analysis_buf[2*k] = ylog ? pow(10.0, 0.05 * result) : result;
  378. s->analysis_buf[2*k+1] = 0.0;
  379. }
  380. if (s->dump_buf)
  381. memcpy(s->dump_buf, s->analysis_buf, s->analysis_rdft_len * sizeof(*s->analysis_buf));
  382. av_rdft_calc(s->analysis_irdft, s->analysis_buf);
  383. center = s->fir_len / 2;
  384. for (k = 0; k <= center; k++) {
  385. double u = k * (M_PI/center);
  386. double win;
  387. switch (s->wfunc) {
  388. case WFUNC_RECTANGULAR:
  389. win = 1.0;
  390. break;
  391. case WFUNC_HANN:
  392. win = 0.5 + 0.5 * cos(u);
  393. break;
  394. case WFUNC_HAMMING:
  395. win = 0.53836 + 0.46164 * cos(u);
  396. break;
  397. case WFUNC_BLACKMAN:
  398. win = 0.42 + 0.5 * cos(u) + 0.08 * cos(2*u);
  399. break;
  400. case WFUNC_NUTTALL3:
  401. win = 0.40897 + 0.5 * cos(u) + 0.09103 * cos(2*u);
  402. break;
  403. case WFUNC_MNUTTALL3:
  404. win = 0.4243801 + 0.4973406 * cos(u) + 0.0782793 * cos(2*u);
  405. break;
  406. case WFUNC_NUTTALL:
  407. win = 0.355768 + 0.487396 * cos(u) + 0.144232 * cos(2*u) + 0.012604 * cos(3*u);
  408. break;
  409. case WFUNC_BNUTTALL:
  410. win = 0.3635819 + 0.4891775 * cos(u) + 0.1365995 * cos(2*u) + 0.0106411 * cos(3*u);
  411. break;
  412. case WFUNC_BHARRIS:
  413. win = 0.35875 + 0.48829 * cos(u) + 0.14128 * cos(2*u) + 0.01168 * cos(3*u);
  414. break;
  415. case WFUNC_TUKEY:
  416. win = (u <= 0.5 * M_PI) ? 1.0 : (0.5 + 0.5 * cos(2*u - M_PI));
  417. break;
  418. default:
  419. av_assert0(0);
  420. }
  421. s->analysis_buf[k] *= (2.0/s->analysis_rdft_len) * (2.0/s->rdft_len) * win;
  422. if (k)
  423. s->analysis_buf[s->analysis_rdft_len - k] = s->analysis_buf[k];
  424. }
  425. memset(s->analysis_buf + center + 1, 0, (s->analysis_rdft_len - s->fir_len) * sizeof(*s->analysis_buf));
  426. memcpy(rdft_buf, s->analysis_buf + s->analysis_rdft_len - center, center * sizeof(*s->analysis_buf));
  427. memcpy(rdft_buf + center, s->analysis_buf, (s->rdft_len - center) * sizeof(*s->analysis_buf));
  428. av_rdft_calc(s->rdft, rdft_buf);
  429. for (k = 0; k < s->rdft_len; k++) {
  430. if (isnan(rdft_buf[k]) || isinf(rdft_buf[k])) {
  431. av_log(ctx, AV_LOG_ERROR, "filter kernel contains nan or infinity.\n");
  432. av_expr_free(gain_expr);
  433. if (dump_fp)
  434. fclose(dump_fp);
  435. return AVERROR(EINVAL);
  436. }
  437. }
  438. if (dump_fp)
  439. dump_fir(ctx, dump_fp, ch);
  440. if (!s->multi)
  441. break;
  442. }
  443. memcpy(s->kernel_buf, s->kernel_tmp_buf, (s->multi ? inlink->channels : 1) * s->rdft_len * sizeof(*s->kernel_buf));
  444. av_expr_free(gain_expr);
  445. if (dump_fp)
  446. fclose(dump_fp);
  447. return 0;
  448. }
  449. #define SELECT_GAIN(s) (s->gain_cmd ? s->gain_cmd : s->gain)
  450. #define SELECT_GAIN_ENTRY(s) (s->gain_entry_cmd ? s->gain_entry_cmd : s->gain_entry)
  451. static int config_input(AVFilterLink *inlink)
  452. {
  453. AVFilterContext *ctx = inlink->dst;
  454. FIREqualizerContext *s = ctx->priv;
  455. int rdft_bits;
  456. common_uninit(s);
  457. s->next_pts = 0;
  458. s->frame_nsamples_max = 0;
  459. s->fir_len = FFMAX(2 * (int)(inlink->sample_rate * s->delay) + 1, 3);
  460. s->remaining = s->fir_len - 1;
  461. for (rdft_bits = RDFT_BITS_MIN; rdft_bits <= RDFT_BITS_MAX; rdft_bits++) {
  462. s->rdft_len = 1 << rdft_bits;
  463. s->nsamples_max = s->rdft_len - s->fir_len + 1;
  464. if (s->nsamples_max * 2 >= s->fir_len)
  465. break;
  466. }
  467. if (rdft_bits > RDFT_BITS_MAX) {
  468. av_log(ctx, AV_LOG_ERROR, "too large delay, please decrease it.\n");
  469. return AVERROR(EINVAL);
  470. }
  471. if (!(s->rdft = av_rdft_init(rdft_bits, DFT_R2C)) || !(s->irdft = av_rdft_init(rdft_bits, IDFT_C2R)))
  472. return AVERROR(ENOMEM);
  473. for ( ; rdft_bits <= RDFT_BITS_MAX; rdft_bits++) {
  474. s->analysis_rdft_len = 1 << rdft_bits;
  475. if (inlink->sample_rate <= s->accuracy * s->analysis_rdft_len)
  476. break;
  477. }
  478. if (rdft_bits > RDFT_BITS_MAX) {
  479. av_log(ctx, AV_LOG_ERROR, "too small accuracy, please increase it.\n");
  480. return AVERROR(EINVAL);
  481. }
  482. if (!(s->analysis_irdft = av_rdft_init(rdft_bits, IDFT_C2R)))
  483. return AVERROR(ENOMEM);
  484. if (s->dumpfile) {
  485. s->analysis_rdft = av_rdft_init(rdft_bits, DFT_R2C);
  486. s->dump_buf = av_malloc_array(s->analysis_rdft_len, sizeof(*s->dump_buf));
  487. }
  488. s->analysis_buf = av_malloc_array(s->analysis_rdft_len, sizeof(*s->analysis_buf));
  489. s->kernel_tmp_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_tmp_buf));
  490. s->kernel_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_buf));
  491. s->conv_buf = av_calloc(2 * s->rdft_len * inlink->channels, sizeof(*s->conv_buf));
  492. s->conv_idx = av_calloc(inlink->channels, sizeof(*s->conv_idx));
  493. if (!s->analysis_buf || !s->kernel_tmp_buf || !s->kernel_buf || !s->conv_buf || !s->conv_idx)
  494. return AVERROR(ENOMEM);
  495. 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",
  496. inlink->sample_rate, inlink->channels, s->analysis_rdft_len, s->rdft_len, s->fir_len, s->nsamples_max);
  497. if (s->fixed)
  498. inlink->min_samples = inlink->max_samples = inlink->partial_buf_size = s->nsamples_max;
  499. return generate_kernel(ctx, SELECT_GAIN(s), SELECT_GAIN_ENTRY(s));
  500. }
  501. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  502. {
  503. AVFilterContext *ctx = inlink->dst;
  504. FIREqualizerContext *s = ctx->priv;
  505. int ch;
  506. for (ch = 0; ch < inlink->channels; ch++) {
  507. fast_convolute(s, s->kernel_buf + (s->multi ? ch * s->rdft_len : 0),
  508. s->conv_buf + 2 * ch * s->rdft_len, s->conv_idx + ch,
  509. (float *) frame->extended_data[ch], frame->nb_samples);
  510. }
  511. s->next_pts = AV_NOPTS_VALUE;
  512. if (frame->pts != AV_NOPTS_VALUE) {
  513. s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, av_make_q(1, inlink->sample_rate), inlink->time_base);
  514. if (s->zero_phase)
  515. frame->pts -= av_rescale_q(s->fir_len/2, av_make_q(1, inlink->sample_rate), inlink->time_base);
  516. }
  517. s->frame_nsamples_max = FFMAX(s->frame_nsamples_max, frame->nb_samples);
  518. return ff_filter_frame(ctx->outputs[0], frame);
  519. }
  520. static int request_frame(AVFilterLink *outlink)
  521. {
  522. AVFilterContext *ctx = outlink->src;
  523. FIREqualizerContext *s= ctx->priv;
  524. int ret;
  525. ret = ff_request_frame(ctx->inputs[0]);
  526. if (ret == AVERROR_EOF && s->remaining > 0 && s->frame_nsamples_max > 0) {
  527. AVFrame *frame = ff_get_audio_buffer(outlink, FFMIN(s->remaining, s->frame_nsamples_max));
  528. if (!frame)
  529. return AVERROR(ENOMEM);
  530. av_samples_set_silence(frame->extended_data, 0, frame->nb_samples, outlink->channels, frame->format);
  531. frame->pts = s->next_pts;
  532. s->remaining -= frame->nb_samples;
  533. ret = filter_frame(ctx->inputs[0], frame);
  534. }
  535. return ret;
  536. }
  537. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  538. char *res, int res_len, int flags)
  539. {
  540. FIREqualizerContext *s = ctx->priv;
  541. int ret = AVERROR(ENOSYS);
  542. if (!strcmp(cmd, "gain")) {
  543. char *gain_cmd;
  544. if (SELECT_GAIN(s) && !strcmp(SELECT_GAIN(s), args)) {
  545. av_log(ctx, AV_LOG_DEBUG, "equal gain, do not rebuild.\n");
  546. return 0;
  547. }
  548. gain_cmd = av_strdup(args);
  549. if (!gain_cmd)
  550. return AVERROR(ENOMEM);
  551. ret = generate_kernel(ctx, gain_cmd, SELECT_GAIN_ENTRY(s));
  552. if (ret >= 0) {
  553. av_freep(&s->gain_cmd);
  554. s->gain_cmd = gain_cmd;
  555. } else {
  556. av_freep(&gain_cmd);
  557. }
  558. } else if (!strcmp(cmd, "gain_entry")) {
  559. char *gain_entry_cmd;
  560. if (SELECT_GAIN_ENTRY(s) && !strcmp(SELECT_GAIN_ENTRY(s), args)) {
  561. av_log(ctx, AV_LOG_DEBUG, "equal gain_entry, do not rebuild.\n");
  562. return 0;
  563. }
  564. gain_entry_cmd = av_strdup(args);
  565. if (!gain_entry_cmd)
  566. return AVERROR(ENOMEM);
  567. ret = generate_kernel(ctx, SELECT_GAIN(s), gain_entry_cmd);
  568. if (ret >= 0) {
  569. av_freep(&s->gain_entry_cmd);
  570. s->gain_entry_cmd = gain_entry_cmd;
  571. } else {
  572. av_freep(&gain_entry_cmd);
  573. }
  574. }
  575. return ret;
  576. }
  577. static const AVFilterPad firequalizer_inputs[] = {
  578. {
  579. .name = "default",
  580. .config_props = config_input,
  581. .filter_frame = filter_frame,
  582. .type = AVMEDIA_TYPE_AUDIO,
  583. .needs_writable = 1,
  584. },
  585. { NULL }
  586. };
  587. static const AVFilterPad firequalizer_outputs[] = {
  588. {
  589. .name = "default",
  590. .request_frame = request_frame,
  591. .type = AVMEDIA_TYPE_AUDIO,
  592. },
  593. { NULL }
  594. };
  595. AVFilter ff_af_firequalizer = {
  596. .name = "firequalizer",
  597. .description = NULL_IF_CONFIG_SMALL("Finite Impulse Response Equalizer."),
  598. .uninit = uninit,
  599. .query_formats = query_formats,
  600. .process_command = process_command,
  601. .priv_size = sizeof(FIREqualizerContext),
  602. .inputs = firequalizer_inputs,
  603. .outputs = firequalizer_outputs,
  604. .priv_class = &firequalizer_class,
  605. };