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.

643 lines
22KB

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