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.

1592 lines
58KB

  1. /*
  2. * Copyright (c) 2018 Paul B Mahol
  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 <float.h>
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/xga_font_data.h"
  26. #include "audio.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. typedef struct ThreadData {
  30. AVFrame *in, *out;
  31. } ThreadData;
  32. typedef struct Pair {
  33. int a, b;
  34. } Pair;
  35. typedef struct BiquadContext {
  36. double a[3];
  37. double b[3];
  38. double w1, w2;
  39. } BiquadContext;
  40. typedef struct IIRChannel {
  41. int nb_ab[2];
  42. double *ab[2];
  43. double g;
  44. double *cache[2];
  45. double fir;
  46. BiquadContext *biquads;
  47. int clippings;
  48. } IIRChannel;
  49. typedef struct AudioIIRContext {
  50. const AVClass *class;
  51. char *a_str, *b_str, *g_str;
  52. double dry_gain, wet_gain;
  53. double mix;
  54. int normalize;
  55. int format;
  56. int process;
  57. int precision;
  58. int response;
  59. int w, h;
  60. int ir_channel;
  61. AVRational rate;
  62. AVFrame *video;
  63. IIRChannel *iir;
  64. int channels;
  65. enum AVSampleFormat sample_format;
  66. int (*iir_channel)(AVFilterContext *ctx, void *arg, int ch, int nb_jobs);
  67. } AudioIIRContext;
  68. static int query_formats(AVFilterContext *ctx)
  69. {
  70. AudioIIRContext *s = ctx->priv;
  71. AVFilterFormats *formats;
  72. AVFilterChannelLayouts *layouts;
  73. enum AVSampleFormat sample_fmts[] = {
  74. AV_SAMPLE_FMT_DBLP,
  75. AV_SAMPLE_FMT_NONE
  76. };
  77. static const enum AVPixelFormat pix_fmts[] = {
  78. AV_PIX_FMT_RGB0,
  79. AV_PIX_FMT_NONE
  80. };
  81. int ret;
  82. if (s->response) {
  83. AVFilterLink *videolink = ctx->outputs[1];
  84. formats = ff_make_format_list(pix_fmts);
  85. if ((ret = ff_formats_ref(formats, &videolink->incfg.formats)) < 0)
  86. return ret;
  87. }
  88. layouts = ff_all_channel_counts();
  89. if (!layouts)
  90. return AVERROR(ENOMEM);
  91. ret = ff_set_common_channel_layouts(ctx, layouts);
  92. if (ret < 0)
  93. return ret;
  94. sample_fmts[0] = s->sample_format;
  95. formats = ff_make_format_list(sample_fmts);
  96. if (!formats)
  97. return AVERROR(ENOMEM);
  98. ret = ff_set_common_formats(ctx, formats);
  99. if (ret < 0)
  100. return ret;
  101. formats = ff_all_samplerates();
  102. if (!formats)
  103. return AVERROR(ENOMEM);
  104. return ff_set_common_samplerates(ctx, formats);
  105. }
  106. #define IIR_CH(name, type, min, max, need_clipping) \
  107. static int iir_ch_## name(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) \
  108. { \
  109. AudioIIRContext *s = ctx->priv; \
  110. const double ig = s->dry_gain; \
  111. const double og = s->wet_gain; \
  112. const double mix = s->mix; \
  113. ThreadData *td = arg; \
  114. AVFrame *in = td->in, *out = td->out; \
  115. const type *src = (const type *)in->extended_data[ch]; \
  116. double *oc = (double *)s->iir[ch].cache[0]; \
  117. double *ic = (double *)s->iir[ch].cache[1]; \
  118. const int nb_a = s->iir[ch].nb_ab[0]; \
  119. const int nb_b = s->iir[ch].nb_ab[1]; \
  120. const double *a = s->iir[ch].ab[0]; \
  121. const double *b = s->iir[ch].ab[1]; \
  122. const double g = s->iir[ch].g; \
  123. int *clippings = &s->iir[ch].clippings; \
  124. type *dst = (type *)out->extended_data[ch]; \
  125. int n; \
  126. \
  127. for (n = 0; n < in->nb_samples; n++) { \
  128. double sample = 0.; \
  129. int x; \
  130. \
  131. memmove(&ic[1], &ic[0], (nb_b - 1) * sizeof(*ic)); \
  132. memmove(&oc[1], &oc[0], (nb_a - 1) * sizeof(*oc)); \
  133. ic[0] = src[n] * ig; \
  134. for (x = 0; x < nb_b; x++) \
  135. sample += b[x] * ic[x]; \
  136. \
  137. for (x = 1; x < nb_a; x++) \
  138. sample -= a[x] * oc[x]; \
  139. \
  140. oc[0] = sample; \
  141. sample *= og * g; \
  142. sample = sample * mix + ic[0] * (1. - mix); \
  143. if (need_clipping && sample < min) { \
  144. (*clippings)++; \
  145. dst[n] = min; \
  146. } else if (need_clipping && sample > max) { \
  147. (*clippings)++; \
  148. dst[n] = max; \
  149. } else { \
  150. dst[n] = sample; \
  151. } \
  152. } \
  153. \
  154. return 0; \
  155. }
  156. IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
  157. IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
  158. IIR_CH(fltp, float, -1., 1., 0)
  159. IIR_CH(dblp, double, -1., 1., 0)
  160. #define SERIAL_IIR_CH(name, type, min, max, need_clipping) \
  161. static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, \
  162. int ch, int nb_jobs) \
  163. { \
  164. AudioIIRContext *s = ctx->priv; \
  165. const double ig = s->dry_gain; \
  166. const double og = s->wet_gain; \
  167. const double mix = s->mix; \
  168. const double imix = 1. - mix; \
  169. ThreadData *td = arg; \
  170. AVFrame *in = td->in, *out = td->out; \
  171. const type *src = (const type *)in->extended_data[ch]; \
  172. type *dst = (type *)out->extended_data[ch]; \
  173. IIRChannel *iir = &s->iir[ch]; \
  174. const double g = iir->g; \
  175. int *clippings = &iir->clippings; \
  176. int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; \
  177. int n, i; \
  178. \
  179. for (i = nb_biquads - 1; i >= 0; i--) { \
  180. const double a1 = -iir->biquads[i].a[1]; \
  181. const double a2 = -iir->biquads[i].a[2]; \
  182. const double b0 = iir->biquads[i].b[0]; \
  183. const double b1 = iir->biquads[i].b[1]; \
  184. const double b2 = iir->biquads[i].b[2]; \
  185. double w1 = iir->biquads[i].w1; \
  186. double w2 = iir->biquads[i].w2; \
  187. \
  188. for (n = 0; n < in->nb_samples; n++) { \
  189. double i0 = ig * (i ? dst[n] : src[n]); \
  190. double o0 = i0 * b0 + w1; \
  191. \
  192. w1 = b1 * i0 + w2 + a1 * o0; \
  193. w2 = b2 * i0 + a2 * o0; \
  194. o0 *= og * g; \
  195. \
  196. o0 = o0 * mix + imix * i0; \
  197. if (need_clipping && o0 < min) { \
  198. (*clippings)++; \
  199. dst[n] = min; \
  200. } else if (need_clipping && o0 > max) { \
  201. (*clippings)++; \
  202. dst[n] = max; \
  203. } else { \
  204. dst[n] = o0; \
  205. } \
  206. } \
  207. iir->biquads[i].w1 = w1; \
  208. iir->biquads[i].w2 = w2; \
  209. } \
  210. \
  211. return 0; \
  212. }
  213. SERIAL_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
  214. SERIAL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
  215. SERIAL_IIR_CH(fltp, float, -1., 1., 0)
  216. SERIAL_IIR_CH(dblp, double, -1., 1., 0)
  217. #define PARALLEL_IIR_CH(name, type, min, max, need_clipping) \
  218. static int iir_ch_parallel_## name(AVFilterContext *ctx, void *arg, \
  219. int ch, int nb_jobs) \
  220. { \
  221. AudioIIRContext *s = ctx->priv; \
  222. const double ig = s->dry_gain; \
  223. const double og = s->wet_gain; \
  224. const double mix = s->mix; \
  225. const double imix = 1. - mix; \
  226. ThreadData *td = arg; \
  227. AVFrame *in = td->in, *out = td->out; \
  228. const type *src = (const type *)in->extended_data[ch]; \
  229. type *dst = (type *)out->extended_data[ch]; \
  230. IIRChannel *iir = &s->iir[ch]; \
  231. const double g = iir->g; \
  232. const double fir = iir->fir; \
  233. int *clippings = &iir->clippings; \
  234. int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; \
  235. int n, i; \
  236. \
  237. for (i = 0; i < nb_biquads; i++) { \
  238. const double a1 = -iir->biquads[i].a[1]; \
  239. const double a2 = -iir->biquads[i].a[2]; \
  240. const double b1 = iir->biquads[i].b[1]; \
  241. const double b2 = iir->biquads[i].b[2]; \
  242. double w1 = iir->biquads[i].w1; \
  243. double w2 = iir->biquads[i].w2; \
  244. \
  245. for (n = 0; n < in->nb_samples; n++) { \
  246. double i0 = ig * src[n]; \
  247. double o0 = w1; \
  248. \
  249. w1 = b1 * i0 + w2 + a1 * o0; \
  250. w2 = b2 * i0 + a2 * o0; \
  251. o0 *= og * g; \
  252. o0 += dst[n]; \
  253. \
  254. if (need_clipping && o0 < min) { \
  255. (*clippings)++; \
  256. dst[n] = min; \
  257. } else if (need_clipping && o0 > max) { \
  258. (*clippings)++; \
  259. dst[n] = max; \
  260. } else { \
  261. dst[n] = o0; \
  262. } \
  263. } \
  264. iir->biquads[i].w1 = w1; \
  265. iir->biquads[i].w2 = w2; \
  266. } \
  267. \
  268. for (n = 0; n < in->nb_samples; n++) { \
  269. dst[n] += fir * src[n]; \
  270. dst[n] = dst[n] * mix + imix * src[n]; \
  271. } \
  272. \
  273. return 0; \
  274. }
  275. PARALLEL_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
  276. PARALLEL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
  277. PARALLEL_IIR_CH(fltp, float, -1., 1., 0)
  278. PARALLEL_IIR_CH(dblp, double, -1., 1., 0)
  279. #define LATTICE_IIR_CH(name, type, min, max, need_clipping) \
  280. static int iir_ch_lattice_## name(AVFilterContext *ctx, void *arg, \
  281. int ch, int nb_jobs) \
  282. { \
  283. AudioIIRContext *s = ctx->priv; \
  284. const double ig = s->dry_gain; \
  285. const double og = s->wet_gain; \
  286. const double mix = s->mix; \
  287. ThreadData *td = arg; \
  288. AVFrame *in = td->in, *out = td->out; \
  289. const type *src = (const type *)in->extended_data[ch]; \
  290. double n0, n1, p0, *x = (double *)s->iir[ch].cache[0]; \
  291. const int nb_stages = s->iir[ch].nb_ab[1]; \
  292. const double *v = s->iir[ch].ab[0]; \
  293. const double *k = s->iir[ch].ab[1]; \
  294. const double g = s->iir[ch].g; \
  295. int *clippings = &s->iir[ch].clippings; \
  296. type *dst = (type *)out->extended_data[ch]; \
  297. int n; \
  298. \
  299. for (n = 0; n < in->nb_samples; n++) { \
  300. const double in = src[n] * ig; \
  301. double out = 0.; \
  302. \
  303. n1 = in; \
  304. for (int i = nb_stages - 1; i >= 0; i--) { \
  305. n0 = n1 - k[i] * x[i]; \
  306. p0 = n0 * k[i] + x[i]; \
  307. out += p0 * v[i+1]; \
  308. x[i] = p0; \
  309. n1 = n0; \
  310. } \
  311. \
  312. out += n1 * v[0]; \
  313. memmove(&x[1], &x[0], nb_stages * sizeof(*x)); \
  314. x[0] = n1; \
  315. out *= og * g; \
  316. out = out * mix + in * (1. - mix); \
  317. if (need_clipping && out < min) { \
  318. (*clippings)++; \
  319. dst[n] = min; \
  320. } else if (need_clipping && out > max) { \
  321. (*clippings)++; \
  322. dst[n] = max; \
  323. } else { \
  324. dst[n] = out; \
  325. } \
  326. } \
  327. \
  328. return 0; \
  329. }
  330. LATTICE_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
  331. LATTICE_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
  332. LATTICE_IIR_CH(fltp, float, -1., 1., 0)
  333. LATTICE_IIR_CH(dblp, double, -1., 1., 0)
  334. static void count_coefficients(char *item_str, int *nb_items)
  335. {
  336. char *p;
  337. if (!item_str)
  338. return;
  339. *nb_items = 1;
  340. for (p = item_str; *p && *p != '|'; p++) {
  341. if (*p == ' ')
  342. (*nb_items)++;
  343. }
  344. }
  345. static int read_gains(AVFilterContext *ctx, char *item_str, int nb_items)
  346. {
  347. AudioIIRContext *s = ctx->priv;
  348. char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
  349. int i;
  350. p = old_str = av_strdup(item_str);
  351. if (!p)
  352. return AVERROR(ENOMEM);
  353. for (i = 0; i < nb_items; i++) {
  354. if (!(arg = av_strtok(p, "|", &saveptr)))
  355. arg = prev_arg;
  356. if (!arg) {
  357. av_freep(&old_str);
  358. return AVERROR(EINVAL);
  359. }
  360. p = NULL;
  361. if (av_sscanf(arg, "%lf", &s->iir[i].g) != 1) {
  362. av_log(ctx, AV_LOG_ERROR, "Invalid gains supplied: %s\n", arg);
  363. av_freep(&old_str);
  364. return AVERROR(EINVAL);
  365. }
  366. prev_arg = arg;
  367. }
  368. av_freep(&old_str);
  369. return 0;
  370. }
  371. static int read_tf_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst)
  372. {
  373. char *p, *arg, *old_str, *saveptr = NULL;
  374. int i;
  375. p = old_str = av_strdup(item_str);
  376. if (!p)
  377. return AVERROR(ENOMEM);
  378. for (i = 0; i < nb_items; i++) {
  379. if (!(arg = av_strtok(p, " ", &saveptr)))
  380. break;
  381. p = NULL;
  382. if (av_sscanf(arg, "%lf", &dst[i]) != 1) {
  383. av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
  384. av_freep(&old_str);
  385. return AVERROR(EINVAL);
  386. }
  387. }
  388. av_freep(&old_str);
  389. return 0;
  390. }
  391. static int read_zp_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst, const char *format)
  392. {
  393. char *p, *arg, *old_str, *saveptr = NULL;
  394. int i;
  395. p = old_str = av_strdup(item_str);
  396. if (!p)
  397. return AVERROR(ENOMEM);
  398. for (i = 0; i < nb_items; i++) {
  399. if (!(arg = av_strtok(p, " ", &saveptr)))
  400. break;
  401. p = NULL;
  402. if (av_sscanf(arg, format, &dst[i*2], &dst[i*2+1]) != 2) {
  403. av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
  404. av_freep(&old_str);
  405. return AVERROR(EINVAL);
  406. }
  407. }
  408. av_freep(&old_str);
  409. return 0;
  410. }
  411. static const char *const format[] = { "%lf", "%lf %lfi", "%lf %lfr", "%lf %lfd", "%lf %lfi" };
  412. static int read_channels(AVFilterContext *ctx, int channels, uint8_t *item_str, int ab)
  413. {
  414. AudioIIRContext *s = ctx->priv;
  415. char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
  416. int i, ret;
  417. p = old_str = av_strdup(item_str);
  418. if (!p)
  419. return AVERROR(ENOMEM);
  420. for (i = 0; i < channels; i++) {
  421. IIRChannel *iir = &s->iir[i];
  422. if (!(arg = av_strtok(p, "|", &saveptr)))
  423. arg = prev_arg;
  424. if (!arg) {
  425. av_freep(&old_str);
  426. return AVERROR(EINVAL);
  427. }
  428. count_coefficients(arg, &iir->nb_ab[ab]);
  429. p = NULL;
  430. iir->cache[ab] = av_calloc(iir->nb_ab[ab] + 1, sizeof(double));
  431. iir->ab[ab] = av_calloc(iir->nb_ab[ab] * (!!s->format + 1), sizeof(double));
  432. if (!iir->ab[ab] || !iir->cache[ab]) {
  433. av_freep(&old_str);
  434. return AVERROR(ENOMEM);
  435. }
  436. if (s->format > 0) {
  437. ret = read_zp_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab], format[s->format]);
  438. } else {
  439. ret = read_tf_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab]);
  440. }
  441. if (ret < 0) {
  442. av_freep(&old_str);
  443. return ret;
  444. }
  445. prev_arg = arg;
  446. }
  447. av_freep(&old_str);
  448. return 0;
  449. }
  450. static void cmul(double re, double im, double re2, double im2, double *RE, double *IM)
  451. {
  452. *RE = re * re2 - im * im2;
  453. *IM = re * im2 + re2 * im;
  454. }
  455. static int expand(AVFilterContext *ctx, double *pz, int n, double *coefs)
  456. {
  457. coefs[2 * n] = 1.0;
  458. for (int i = 1; i <= n; i++) {
  459. for (int j = n - i; j < n; j++) {
  460. double re, im;
  461. cmul(coefs[2 * (j + 1)], coefs[2 * (j + 1) + 1],
  462. pz[2 * (i - 1)], pz[2 * (i - 1) + 1], &re, &im);
  463. coefs[2 * j] -= re;
  464. coefs[2 * j + 1] -= im;
  465. }
  466. }
  467. for (int i = 0; i < n + 1; i++) {
  468. if (fabs(coefs[2 * i + 1]) > FLT_EPSILON) {
  469. av_log(ctx, AV_LOG_ERROR, "coefs: %f of z^%d is not real; poles/zeros are not complex conjugates.\n",
  470. coefs[2 * i + 1], i);
  471. return AVERROR(EINVAL);
  472. }
  473. }
  474. return 0;
  475. }
  476. static void normalize_coeffs(AVFilterContext *ctx, int ch)
  477. {
  478. AudioIIRContext *s = ctx->priv;
  479. IIRChannel *iir = &s->iir[ch];
  480. double sum_den = 0.;
  481. if (!s->normalize)
  482. return;
  483. for (int i = 0; i < iir->nb_ab[1]; i++) {
  484. sum_den += iir->ab[1][i];
  485. }
  486. if (sum_den > 1e-6) {
  487. double factor, sum_num = 0.;
  488. for (int i = 0; i < iir->nb_ab[0]; i++) {
  489. sum_num += iir->ab[0][i];
  490. }
  491. factor = sum_num / sum_den;
  492. for (int i = 0; i < iir->nb_ab[1]; i++) {
  493. iir->ab[1][i] *= factor;
  494. }
  495. }
  496. }
  497. static int convert_zp2tf(AVFilterContext *ctx, int channels)
  498. {
  499. AudioIIRContext *s = ctx->priv;
  500. int ch, i, j, ret = 0;
  501. for (ch = 0; ch < channels; ch++) {
  502. IIRChannel *iir = &s->iir[ch];
  503. double *topc, *botc;
  504. topc = av_calloc((iir->nb_ab[1] + 1) * 2, sizeof(*topc));
  505. botc = av_calloc((iir->nb_ab[0] + 1) * 2, sizeof(*botc));
  506. if (!topc || !botc) {
  507. ret = AVERROR(ENOMEM);
  508. goto fail;
  509. }
  510. ret = expand(ctx, iir->ab[0], iir->nb_ab[0], botc);
  511. if (ret < 0) {
  512. goto fail;
  513. }
  514. ret = expand(ctx, iir->ab[1], iir->nb_ab[1], topc);
  515. if (ret < 0) {
  516. goto fail;
  517. }
  518. for (j = 0, i = iir->nb_ab[1]; i >= 0; j++, i--) {
  519. iir->ab[1][j] = topc[2 * i];
  520. }
  521. iir->nb_ab[1]++;
  522. for (j = 0, i = iir->nb_ab[0]; i >= 0; j++, i--) {
  523. iir->ab[0][j] = botc[2 * i];
  524. }
  525. iir->nb_ab[0]++;
  526. normalize_coeffs(ctx, ch);
  527. fail:
  528. av_free(topc);
  529. av_free(botc);
  530. if (ret < 0)
  531. break;
  532. }
  533. return ret;
  534. }
  535. static int decompose_zp2biquads(AVFilterContext *ctx, int channels)
  536. {
  537. AudioIIRContext *s = ctx->priv;
  538. int ch, ret;
  539. for (ch = 0; ch < channels; ch++) {
  540. IIRChannel *iir = &s->iir[ch];
  541. int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2;
  542. int current_biquad = 0;
  543. iir->biquads = av_calloc(nb_biquads, sizeof(BiquadContext));
  544. if (!iir->biquads)
  545. return AVERROR(ENOMEM);
  546. while (nb_biquads--) {
  547. Pair outmost_pole = { -1, -1 };
  548. Pair nearest_zero = { -1, -1 };
  549. double zeros[4] = { 0 };
  550. double poles[4] = { 0 };
  551. double b[6] = { 0 };
  552. double a[6] = { 0 };
  553. double min_distance = DBL_MAX;
  554. double max_mag = 0;
  555. double factor;
  556. int i;
  557. for (i = 0; i < iir->nb_ab[0]; i++) {
  558. double mag;
  559. if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
  560. continue;
  561. mag = hypot(iir->ab[0][2 * i], iir->ab[0][2 * i + 1]);
  562. if (mag > max_mag) {
  563. max_mag = mag;
  564. outmost_pole.a = i;
  565. }
  566. }
  567. for (i = 0; i < iir->nb_ab[0]; i++) {
  568. if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
  569. continue;
  570. if (iir->ab[0][2 * i ] == iir->ab[0][2 * outmost_pole.a ] &&
  571. iir->ab[0][2 * i + 1] == -iir->ab[0][2 * outmost_pole.a + 1]) {
  572. outmost_pole.b = i;
  573. break;
  574. }
  575. }
  576. av_log(ctx, AV_LOG_VERBOSE, "outmost_pole is %d.%d\n", outmost_pole.a, outmost_pole.b);
  577. if (outmost_pole.a < 0 || outmost_pole.b < 0)
  578. return AVERROR(EINVAL);
  579. for (i = 0; i < iir->nb_ab[1]; i++) {
  580. double distance;
  581. if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
  582. continue;
  583. distance = hypot(iir->ab[0][2 * outmost_pole.a ] - iir->ab[1][2 * i ],
  584. iir->ab[0][2 * outmost_pole.a + 1] - iir->ab[1][2 * i + 1]);
  585. if (distance < min_distance) {
  586. min_distance = distance;
  587. nearest_zero.a = i;
  588. }
  589. }
  590. for (i = 0; i < iir->nb_ab[1]; i++) {
  591. if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
  592. continue;
  593. if (iir->ab[1][2 * i ] == iir->ab[1][2 * nearest_zero.a ] &&
  594. iir->ab[1][2 * i + 1] == -iir->ab[1][2 * nearest_zero.a + 1]) {
  595. nearest_zero.b = i;
  596. break;
  597. }
  598. }
  599. av_log(ctx, AV_LOG_VERBOSE, "nearest_zero is %d.%d\n", nearest_zero.a, nearest_zero.b);
  600. if (nearest_zero.a < 0 || nearest_zero.b < 0)
  601. return AVERROR(EINVAL);
  602. poles[0] = iir->ab[0][2 * outmost_pole.a ];
  603. poles[1] = iir->ab[0][2 * outmost_pole.a + 1];
  604. zeros[0] = iir->ab[1][2 * nearest_zero.a ];
  605. zeros[1] = iir->ab[1][2 * nearest_zero.a + 1];
  606. if (nearest_zero.a == nearest_zero.b && outmost_pole.a == outmost_pole.b) {
  607. zeros[2] = 0;
  608. zeros[3] = 0;
  609. poles[2] = 0;
  610. poles[3] = 0;
  611. } else {
  612. poles[2] = iir->ab[0][2 * outmost_pole.b ];
  613. poles[3] = iir->ab[0][2 * outmost_pole.b + 1];
  614. zeros[2] = iir->ab[1][2 * nearest_zero.b ];
  615. zeros[3] = iir->ab[1][2 * nearest_zero.b + 1];
  616. }
  617. ret = expand(ctx, zeros, 2, b);
  618. if (ret < 0)
  619. return ret;
  620. ret = expand(ctx, poles, 2, a);
  621. if (ret < 0)
  622. return ret;
  623. iir->ab[0][2 * outmost_pole.a] = iir->ab[0][2 * outmost_pole.a + 1] = NAN;
  624. iir->ab[0][2 * outmost_pole.b] = iir->ab[0][2 * outmost_pole.b + 1] = NAN;
  625. iir->ab[1][2 * nearest_zero.a] = iir->ab[1][2 * nearest_zero.a + 1] = NAN;
  626. iir->ab[1][2 * nearest_zero.b] = iir->ab[1][2 * nearest_zero.b + 1] = NAN;
  627. iir->biquads[current_biquad].a[0] = 1.;
  628. iir->biquads[current_biquad].a[1] = a[2] / a[4];
  629. iir->biquads[current_biquad].a[2] = a[0] / a[4];
  630. iir->biquads[current_biquad].b[0] = b[4] / a[4];
  631. iir->biquads[current_biquad].b[1] = b[2] / a[4];
  632. iir->biquads[current_biquad].b[2] = b[0] / a[4];
  633. if (s->normalize &&
  634. fabs(iir->biquads[current_biquad].b[0] +
  635. iir->biquads[current_biquad].b[1] +
  636. iir->biquads[current_biquad].b[2]) > 1e-6) {
  637. factor = (iir->biquads[current_biquad].a[0] +
  638. iir->biquads[current_biquad].a[1] +
  639. iir->biquads[current_biquad].a[2]) /
  640. (iir->biquads[current_biquad].b[0] +
  641. iir->biquads[current_biquad].b[1] +
  642. iir->biquads[current_biquad].b[2]);
  643. av_log(ctx, AV_LOG_VERBOSE, "factor=%f\n", factor);
  644. iir->biquads[current_biquad].b[0] *= factor;
  645. iir->biquads[current_biquad].b[1] *= factor;
  646. iir->biquads[current_biquad].b[2] *= factor;
  647. }
  648. iir->biquads[current_biquad].b[0] *= (current_biquad ? 1.0 : iir->g);
  649. iir->biquads[current_biquad].b[1] *= (current_biquad ? 1.0 : iir->g);
  650. iir->biquads[current_biquad].b[2] *= (current_biquad ? 1.0 : iir->g);
  651. av_log(ctx, AV_LOG_VERBOSE, "a=%f %f %f:b=%f %f %f\n",
  652. iir->biquads[current_biquad].a[0],
  653. iir->biquads[current_biquad].a[1],
  654. iir->biquads[current_biquad].a[2],
  655. iir->biquads[current_biquad].b[0],
  656. iir->biquads[current_biquad].b[1],
  657. iir->biquads[current_biquad].b[2]);
  658. current_biquad++;
  659. }
  660. }
  661. return 0;
  662. }
  663. static void biquad_process(double *x, double *y, int length,
  664. double b0, double b1, double b2,
  665. double a1, double a2)
  666. {
  667. double w1 = 0., w2 = 0.;
  668. a1 = -a1;
  669. a2 = -a2;
  670. for (int n = 0; n < length; n++) {
  671. double out, in = x[n];
  672. y[n] = out = in * b0 + w1;
  673. w1 = b1 * in + w2 + a1 * out;
  674. w2 = b2 * in + a2 * out;
  675. }
  676. }
  677. static void solve(double *matrix, double *vector, int n, double *y, double *x, double *lu)
  678. {
  679. double sum = 0.;
  680. for (int i = 0; i < n; i++) {
  681. for (int j = i; j < n; j++) {
  682. sum = 0.;
  683. for (int k = 0; k < i; k++)
  684. sum += lu[i * n + k] * lu[k * n + j];
  685. lu[i * n + j] = matrix[j * n + i] - sum;
  686. }
  687. for (int j = i + 1; j < n; j++) {
  688. sum = 0.;
  689. for (int k = 0; k < i; k++)
  690. sum += lu[j * n + k] * lu[k * n + i];
  691. lu[j * n + i] = (1. / lu[i * n + i]) * (matrix[i * n + j] - sum);
  692. }
  693. }
  694. for (int i = 0; i < n; i++) {
  695. sum = 0.;
  696. for (int k = 0; k < i; k++)
  697. sum += lu[i * n + k] * y[k];
  698. y[i] = vector[i] - sum;
  699. }
  700. for (int i = n - 1; i >= 0; i--) {
  701. sum = 0.;
  702. for (int k = i + 1; k < n; k++)
  703. sum += lu[i * n + k] * x[k];
  704. x[i] = (1 / lu[i * n + i]) * (y[i] - sum);
  705. }
  706. }
  707. static int convert_serial2parallel(AVFilterContext *ctx, int channels)
  708. {
  709. AudioIIRContext *s = ctx->priv;
  710. int ret = 0;
  711. for (int ch = 0; ch < channels; ch++) {
  712. IIRChannel *iir = &s->iir[ch];
  713. int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2;
  714. int length = nb_biquads * 2 + 1;
  715. double *impulse = av_calloc(length, sizeof(*impulse));
  716. double *y = av_calloc(length, sizeof(*y));
  717. double *resp = av_calloc(length, sizeof(*resp));
  718. double *M = av_calloc((length - 1) * 2 * nb_biquads, sizeof(*M));
  719. double *W = av_calloc((length - 1) * 2 * nb_biquads, sizeof(*W));
  720. if (!impulse || !y || !resp || !M) {
  721. av_free(impulse);
  722. av_free(y);
  723. av_free(resp);
  724. av_free(M);
  725. av_free(W);
  726. return AVERROR(ENOMEM);
  727. }
  728. impulse[0] = 1.;
  729. for (int n = 0; n < nb_biquads; n++) {
  730. BiquadContext *biquad = &iir->biquads[n];
  731. biquad_process(n ? y : impulse, y, length,
  732. biquad->b[0], biquad->b[1], biquad->b[2],
  733. biquad->a[1], biquad->a[2]);
  734. }
  735. for (int n = 0; n < nb_biquads; n++) {
  736. BiquadContext *biquad = &iir->biquads[n];
  737. biquad_process(impulse, resp, length - 1,
  738. 1., 0., 0., biquad->a[1], biquad->a[2]);
  739. memcpy(M + n * 2 * (length - 1), resp, sizeof(*resp) * (length - 1));
  740. memcpy(M + n * 2 * (length - 1) + length, resp, sizeof(*resp) * (length - 2));
  741. memset(resp, 0, length * sizeof(*resp));
  742. }
  743. solve(M, &y[1], length - 1, &impulse[1], resp, W);
  744. iir->fir = y[0];
  745. for (int n = 0; n < nb_biquads; n++) {
  746. BiquadContext *biquad = &iir->biquads[n];
  747. biquad->b[0] = 0.;
  748. biquad->b[1] = resp[n * 2 + 0];
  749. biquad->b[2] = resp[n * 2 + 1];
  750. }
  751. av_free(impulse);
  752. av_free(y);
  753. av_free(resp);
  754. av_free(M);
  755. av_free(W);
  756. if (ret < 0)
  757. return ret;
  758. }
  759. return 0;
  760. }
  761. static void convert_pr2zp(AVFilterContext *ctx, int channels)
  762. {
  763. AudioIIRContext *s = ctx->priv;
  764. int ch;
  765. for (ch = 0; ch < channels; ch++) {
  766. IIRChannel *iir = &s->iir[ch];
  767. int n;
  768. for (n = 0; n < iir->nb_ab[0]; n++) {
  769. double r = iir->ab[0][2*n];
  770. double angle = iir->ab[0][2*n+1];
  771. iir->ab[0][2*n] = r * cos(angle);
  772. iir->ab[0][2*n+1] = r * sin(angle);
  773. }
  774. for (n = 0; n < iir->nb_ab[1]; n++) {
  775. double r = iir->ab[1][2*n];
  776. double angle = iir->ab[1][2*n+1];
  777. iir->ab[1][2*n] = r * cos(angle);
  778. iir->ab[1][2*n+1] = r * sin(angle);
  779. }
  780. }
  781. }
  782. static void convert_sp2zp(AVFilterContext *ctx, int channels)
  783. {
  784. AudioIIRContext *s = ctx->priv;
  785. int ch;
  786. for (ch = 0; ch < channels; ch++) {
  787. IIRChannel *iir = &s->iir[ch];
  788. int n;
  789. for (n = 0; n < iir->nb_ab[0]; n++) {
  790. double sr = iir->ab[0][2*n];
  791. double si = iir->ab[0][2*n+1];
  792. iir->ab[0][2*n] = exp(sr) * cos(si);
  793. iir->ab[0][2*n+1] = exp(sr) * sin(si);
  794. }
  795. for (n = 0; n < iir->nb_ab[1]; n++) {
  796. double sr = iir->ab[1][2*n];
  797. double si = iir->ab[1][2*n+1];
  798. iir->ab[1][2*n] = exp(sr) * cos(si);
  799. iir->ab[1][2*n+1] = exp(sr) * sin(si);
  800. }
  801. }
  802. }
  803. static double fact(double i)
  804. {
  805. if (i <= 0.)
  806. return 1.;
  807. return i * fact(i - 1.);
  808. }
  809. static double coef_sf2zf(double *a, int N, int n)
  810. {
  811. double z = 0.;
  812. for (int i = 0; i <= N; i++) {
  813. double acc = 0.;
  814. for (int k = FFMAX(n - N + i, 0); k <= FFMIN(i, n); k++) {
  815. acc += ((fact(i) * fact(N - i)) /
  816. (fact(k) * fact(i - k) * fact(n - k) * fact(N - i - n + k))) *
  817. ((k & 1) ? -1. : 1.);;
  818. }
  819. z += a[i] * pow(2., i) * acc;
  820. }
  821. return z;
  822. }
  823. static void convert_sf2tf(AVFilterContext *ctx, int channels)
  824. {
  825. AudioIIRContext *s = ctx->priv;
  826. int ch;
  827. for (ch = 0; ch < channels; ch++) {
  828. IIRChannel *iir = &s->iir[ch];
  829. double *temp0 = av_calloc(iir->nb_ab[0], sizeof(*temp0));
  830. double *temp1 = av_calloc(iir->nb_ab[1], sizeof(*temp1));
  831. if (!temp0 || !temp1)
  832. goto next;
  833. memcpy(temp0, iir->ab[0], iir->nb_ab[0] * sizeof(*temp0));
  834. memcpy(temp1, iir->ab[1], iir->nb_ab[1] * sizeof(*temp1));
  835. for (int n = 0; n < iir->nb_ab[0]; n++)
  836. iir->ab[0][n] = coef_sf2zf(temp0, iir->nb_ab[0] - 1, n);
  837. for (int n = 0; n < iir->nb_ab[1]; n++)
  838. iir->ab[1][n] = coef_sf2zf(temp1, iir->nb_ab[1] - 1, n);
  839. next:
  840. av_free(temp0);
  841. av_free(temp1);
  842. }
  843. }
  844. static void convert_pd2zp(AVFilterContext *ctx, int channels)
  845. {
  846. AudioIIRContext *s = ctx->priv;
  847. int ch;
  848. for (ch = 0; ch < channels; ch++) {
  849. IIRChannel *iir = &s->iir[ch];
  850. int n;
  851. for (n = 0; n < iir->nb_ab[0]; n++) {
  852. double r = iir->ab[0][2*n];
  853. double angle = M_PI*iir->ab[0][2*n+1]/180.;
  854. iir->ab[0][2*n] = r * cos(angle);
  855. iir->ab[0][2*n+1] = r * sin(angle);
  856. }
  857. for (n = 0; n < iir->nb_ab[1]; n++) {
  858. double r = iir->ab[1][2*n];
  859. double angle = M_PI*iir->ab[1][2*n+1]/180.;
  860. iir->ab[1][2*n] = r * cos(angle);
  861. iir->ab[1][2*n+1] = r * sin(angle);
  862. }
  863. }
  864. }
  865. static void check_stability(AVFilterContext *ctx, int channels)
  866. {
  867. AudioIIRContext *s = ctx->priv;
  868. int ch;
  869. for (ch = 0; ch < channels; ch++) {
  870. IIRChannel *iir = &s->iir[ch];
  871. for (int n = 0; n < iir->nb_ab[0]; n++) {
  872. double pr = hypot(iir->ab[0][2*n], iir->ab[0][2*n+1]);
  873. if (pr >= 1.) {
  874. av_log(ctx, AV_LOG_WARNING, "pole %d at channel %d is unstable\n", n, ch);
  875. break;
  876. }
  877. }
  878. }
  879. }
  880. static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint32_t color)
  881. {
  882. const uint8_t *font;
  883. int font_height;
  884. int i;
  885. font = avpriv_cga_font, font_height = 8;
  886. for (i = 0; txt[i]; i++) {
  887. int char_y, mask;
  888. uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
  889. for (char_y = 0; char_y < font_height; char_y++) {
  890. for (mask = 0x80; mask; mask >>= 1) {
  891. if (font[txt[i] * font_height + char_y] & mask)
  892. AV_WL32(p, color);
  893. p += 4;
  894. }
  895. p += pic->linesize[0] - 8 * 4;
  896. }
  897. }
  898. }
  899. static void draw_line(AVFrame *out, int x0, int y0, int x1, int y1, uint32_t color)
  900. {
  901. int dx = FFABS(x1-x0);
  902. int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
  903. int err = (dx>dy ? dx : -dy) / 2, e2;
  904. for (;;) {
  905. AV_WL32(out->data[0] + y0 * out->linesize[0] + x0 * 4, color);
  906. if (x0 == x1 && y0 == y1)
  907. break;
  908. e2 = err;
  909. if (e2 >-dx) {
  910. err -= dy;
  911. x0--;
  912. }
  913. if (e2 < dy) {
  914. err += dx;
  915. y0 += sy;
  916. }
  917. }
  918. }
  919. static double distance(double x0, double x1, double y0, double y1)
  920. {
  921. return hypot(x0 - x1, y0 - y1);
  922. }
  923. static void get_response(int channel, int format, double w,
  924. const double *b, const double *a,
  925. int nb_b, int nb_a, double *magnitude, double *phase)
  926. {
  927. double realz, realp;
  928. double imagz, imagp;
  929. double real, imag;
  930. double div;
  931. if (format == 0) {
  932. realz = 0., realp = 0.;
  933. imagz = 0., imagp = 0.;
  934. for (int x = 0; x < nb_a; x++) {
  935. realz += cos(-x * w) * a[x];
  936. imagz += sin(-x * w) * a[x];
  937. }
  938. for (int x = 0; x < nb_b; x++) {
  939. realp += cos(-x * w) * b[x];
  940. imagp += sin(-x * w) * b[x];
  941. }
  942. div = realp * realp + imagp * imagp;
  943. real = (realz * realp + imagz * imagp) / div;
  944. imag = (imagz * realp - imagp * realz) / div;
  945. *magnitude = hypot(real, imag);
  946. *phase = atan2(imag, real);
  947. } else {
  948. double p = 1., z = 1.;
  949. double acc = 0.;
  950. for (int x = 0; x < nb_a; x++) {
  951. z *= distance(cos(w), a[2 * x], sin(w), a[2 * x + 1]);
  952. acc += atan2(sin(w) - a[2 * x + 1], cos(w) - a[2 * x]);
  953. }
  954. for (int x = 0; x < nb_b; x++) {
  955. p *= distance(cos(w), b[2 * x], sin(w), b[2 * x + 1]);
  956. acc -= atan2(sin(w) - b[2 * x + 1], cos(w) - b[2 * x]);
  957. }
  958. *magnitude = z / p;
  959. *phase = acc;
  960. }
  961. }
  962. static void draw_response(AVFilterContext *ctx, AVFrame *out, int sample_rate)
  963. {
  964. AudioIIRContext *s = ctx->priv;
  965. double *mag, *phase, *temp, *delay, min = DBL_MAX, max = -DBL_MAX;
  966. double min_delay = DBL_MAX, max_delay = -DBL_MAX, min_phase, max_phase;
  967. int prev_ymag = -1, prev_yphase = -1, prev_ydelay = -1;
  968. char text[32];
  969. int ch, i;
  970. memset(out->data[0], 0, s->h * out->linesize[0]);
  971. phase = av_malloc_array(s->w, sizeof(*phase));
  972. temp = av_malloc_array(s->w, sizeof(*temp));
  973. mag = av_malloc_array(s->w, sizeof(*mag));
  974. delay = av_malloc_array(s->w, sizeof(*delay));
  975. if (!mag || !phase || !delay || !temp)
  976. goto end;
  977. ch = av_clip(s->ir_channel, 0, s->channels - 1);
  978. for (i = 0; i < s->w; i++) {
  979. const double *b = s->iir[ch].ab[0];
  980. const double *a = s->iir[ch].ab[1];
  981. const int nb_b = s->iir[ch].nb_ab[0];
  982. const int nb_a = s->iir[ch].nb_ab[1];
  983. double w = i * M_PI / (s->w - 1);
  984. double m, p;
  985. get_response(ch, s->format, w, b, a, nb_b, nb_a, &m, &p);
  986. mag[i] = s->iir[ch].g * m;
  987. phase[i] = p;
  988. min = fmin(min, mag[i]);
  989. max = fmax(max, mag[i]);
  990. }
  991. temp[0] = 0.;
  992. for (i = 0; i < s->w - 1; i++) {
  993. double d = phase[i] - phase[i + 1];
  994. temp[i + 1] = ceil(fabs(d) / (2. * M_PI)) * 2. * M_PI * ((d > M_PI) - (d < -M_PI));
  995. }
  996. min_phase = phase[0];
  997. max_phase = phase[0];
  998. for (i = 1; i < s->w; i++) {
  999. temp[i] += temp[i - 1];
  1000. phase[i] += temp[i];
  1001. min_phase = fmin(min_phase, phase[i]);
  1002. max_phase = fmax(max_phase, phase[i]);
  1003. }
  1004. for (i = 0; i < s->w - 1; i++) {
  1005. double div = s->w / (double)sample_rate;
  1006. delay[i + 1] = -(phase[i] - phase[i + 1]) / div;
  1007. min_delay = fmin(min_delay, delay[i + 1]);
  1008. max_delay = fmax(max_delay, delay[i + 1]);
  1009. }
  1010. delay[0] = delay[1];
  1011. for (i = 0; i < s->w; i++) {
  1012. int ymag = mag[i] / max * (s->h - 1);
  1013. int ydelay = (delay[i] - min_delay) / (max_delay - min_delay) * (s->h - 1);
  1014. int yphase = (phase[i] - min_phase) / (max_phase - min_phase) * (s->h - 1);
  1015. ymag = s->h - 1 - av_clip(ymag, 0, s->h - 1);
  1016. yphase = s->h - 1 - av_clip(yphase, 0, s->h - 1);
  1017. ydelay = s->h - 1 - av_clip(ydelay, 0, s->h - 1);
  1018. if (prev_ymag < 0)
  1019. prev_ymag = ymag;
  1020. if (prev_yphase < 0)
  1021. prev_yphase = yphase;
  1022. if (prev_ydelay < 0)
  1023. prev_ydelay = ydelay;
  1024. draw_line(out, i, ymag, FFMAX(i - 1, 0), prev_ymag, 0xFFFF00FF);
  1025. draw_line(out, i, yphase, FFMAX(i - 1, 0), prev_yphase, 0xFF00FF00);
  1026. draw_line(out, i, ydelay, FFMAX(i - 1, 0), prev_ydelay, 0xFF00FFFF);
  1027. prev_ymag = ymag;
  1028. prev_yphase = yphase;
  1029. prev_ydelay = ydelay;
  1030. }
  1031. if (s->w > 400 && s->h > 100) {
  1032. drawtext(out, 2, 2, "Max Magnitude:", 0xDDDDDDDD);
  1033. snprintf(text, sizeof(text), "%.2f", max);
  1034. drawtext(out, 15 * 8 + 2, 2, text, 0xDDDDDDDD);
  1035. drawtext(out, 2, 12, "Min Magnitude:", 0xDDDDDDDD);
  1036. snprintf(text, sizeof(text), "%.2f", min);
  1037. drawtext(out, 15 * 8 + 2, 12, text, 0xDDDDDDDD);
  1038. drawtext(out, 2, 22, "Max Phase:", 0xDDDDDDDD);
  1039. snprintf(text, sizeof(text), "%.2f", max_phase);
  1040. drawtext(out, 15 * 8 + 2, 22, text, 0xDDDDDDDD);
  1041. drawtext(out, 2, 32, "Min Phase:", 0xDDDDDDDD);
  1042. snprintf(text, sizeof(text), "%.2f", min_phase);
  1043. drawtext(out, 15 * 8 + 2, 32, text, 0xDDDDDDDD);
  1044. drawtext(out, 2, 42, "Max Delay:", 0xDDDDDDDD);
  1045. snprintf(text, sizeof(text), "%.2f", max_delay);
  1046. drawtext(out, 11 * 8 + 2, 42, text, 0xDDDDDDDD);
  1047. drawtext(out, 2, 52, "Min Delay:", 0xDDDDDDDD);
  1048. snprintf(text, sizeof(text), "%.2f", min_delay);
  1049. drawtext(out, 11 * 8 + 2, 52, text, 0xDDDDDDDD);
  1050. }
  1051. end:
  1052. av_free(delay);
  1053. av_free(temp);
  1054. av_free(phase);
  1055. av_free(mag);
  1056. }
  1057. static int config_output(AVFilterLink *outlink)
  1058. {
  1059. AVFilterContext *ctx = outlink->src;
  1060. AudioIIRContext *s = ctx->priv;
  1061. AVFilterLink *inlink = ctx->inputs[0];
  1062. int ch, ret, i;
  1063. s->channels = inlink->channels;
  1064. s->iir = av_calloc(s->channels, sizeof(*s->iir));
  1065. if (!s->iir)
  1066. return AVERROR(ENOMEM);
  1067. ret = read_gains(ctx, s->g_str, inlink->channels);
  1068. if (ret < 0)
  1069. return ret;
  1070. ret = read_channels(ctx, inlink->channels, s->a_str, 0);
  1071. if (ret < 0)
  1072. return ret;
  1073. ret = read_channels(ctx, inlink->channels, s->b_str, 1);
  1074. if (ret < 0)
  1075. return ret;
  1076. if (s->format == -1) {
  1077. convert_sf2tf(ctx, inlink->channels);
  1078. s->format = 0;
  1079. } else if (s->format == 2) {
  1080. convert_pr2zp(ctx, inlink->channels);
  1081. } else if (s->format == 3) {
  1082. convert_pd2zp(ctx, inlink->channels);
  1083. } else if (s->format == 4) {
  1084. convert_sp2zp(ctx, inlink->channels);
  1085. }
  1086. if (s->format > 0) {
  1087. check_stability(ctx, inlink->channels);
  1088. }
  1089. av_frame_free(&s->video);
  1090. if (s->response) {
  1091. s->video = ff_get_video_buffer(ctx->outputs[1], s->w, s->h);
  1092. if (!s->video)
  1093. return AVERROR(ENOMEM);
  1094. draw_response(ctx, s->video, inlink->sample_rate);
  1095. }
  1096. if (s->format == 0)
  1097. av_log(ctx, AV_LOG_WARNING, "transfer function coefficients format is not recommended for too high number of zeros/poles.\n");
  1098. if (s->format > 0 && s->process == 0) {
  1099. av_log(ctx, AV_LOG_WARNING, "Direct processsing is not recommended for zp coefficients format.\n");
  1100. ret = convert_zp2tf(ctx, inlink->channels);
  1101. if (ret < 0)
  1102. return ret;
  1103. } else if (s->format == -2 && s->process > 0) {
  1104. av_log(ctx, AV_LOG_ERROR, "Only direct processing is implemented for lattice-ladder function.\n");
  1105. return AVERROR_PATCHWELCOME;
  1106. } else if (s->format <= 0 && s->process == 1) {
  1107. av_log(ctx, AV_LOG_ERROR, "Serial processing is not implemented for transfer function.\n");
  1108. return AVERROR_PATCHWELCOME;
  1109. } else if (s->format <= 0 && s->process == 2) {
  1110. av_log(ctx, AV_LOG_ERROR, "Parallel processing is not implemented for transfer function.\n");
  1111. return AVERROR_PATCHWELCOME;
  1112. } else if (s->format > 0 && s->process == 1) {
  1113. ret = decompose_zp2biquads(ctx, inlink->channels);
  1114. if (ret < 0)
  1115. return ret;
  1116. } else if (s->format > 0 && s->process == 2) {
  1117. if (s->precision > 1)
  1118. av_log(ctx, AV_LOG_WARNING, "Parallel processing is not recommended for fixed-point precisions.\n");
  1119. ret = decompose_zp2biquads(ctx, inlink->channels);
  1120. if (ret < 0)
  1121. return ret;
  1122. ret = convert_serial2parallel(ctx, inlink->channels);
  1123. if (ret < 0)
  1124. return ret;
  1125. }
  1126. for (ch = 0; s->format == -2 && ch < inlink->channels; ch++) {
  1127. IIRChannel *iir = &s->iir[ch];
  1128. if (iir->nb_ab[0] != iir->nb_ab[1] + 1) {
  1129. av_log(ctx, AV_LOG_ERROR, "Number of ladder coefficients must be one more than number of reflection coefficients.\n");
  1130. return AVERROR(EINVAL);
  1131. }
  1132. }
  1133. for (ch = 0; s->format == 0 && ch < inlink->channels; ch++) {
  1134. IIRChannel *iir = &s->iir[ch];
  1135. for (i = 1; i < iir->nb_ab[0]; i++) {
  1136. iir->ab[0][i] /= iir->ab[0][0];
  1137. }
  1138. iir->ab[0][0] = 1.0;
  1139. for (i = 0; i < iir->nb_ab[1]; i++) {
  1140. iir->ab[1][i] *= iir->g;
  1141. }
  1142. normalize_coeffs(ctx, ch);
  1143. }
  1144. switch (inlink->format) {
  1145. case AV_SAMPLE_FMT_DBLP: s->iir_channel = s->process == 2 ? iir_ch_parallel_dblp : s->process == 1 ? iir_ch_serial_dblp : iir_ch_dblp; break;
  1146. case AV_SAMPLE_FMT_FLTP: s->iir_channel = s->process == 2 ? iir_ch_parallel_fltp : s->process == 1 ? iir_ch_serial_fltp : iir_ch_fltp; break;
  1147. case AV_SAMPLE_FMT_S32P: s->iir_channel = s->process == 2 ? iir_ch_parallel_s32p : s->process == 1 ? iir_ch_serial_s32p : iir_ch_s32p; break;
  1148. case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 2 ? iir_ch_parallel_s16p : s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break;
  1149. }
  1150. if (s->format == -2) {
  1151. switch (inlink->format) {
  1152. case AV_SAMPLE_FMT_DBLP: s->iir_channel = iir_ch_lattice_dblp; break;
  1153. case AV_SAMPLE_FMT_FLTP: s->iir_channel = iir_ch_lattice_fltp; break;
  1154. case AV_SAMPLE_FMT_S32P: s->iir_channel = iir_ch_lattice_s32p; break;
  1155. case AV_SAMPLE_FMT_S16P: s->iir_channel = iir_ch_lattice_s16p; break;
  1156. }
  1157. }
  1158. return 0;
  1159. }
  1160. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  1161. {
  1162. AVFilterContext *ctx = inlink->dst;
  1163. AudioIIRContext *s = ctx->priv;
  1164. AVFilterLink *outlink = ctx->outputs[0];
  1165. ThreadData td;
  1166. AVFrame *out;
  1167. int ch, ret;
  1168. if (av_frame_is_writable(in) && s->process != 2) {
  1169. out = in;
  1170. } else {
  1171. out = ff_get_audio_buffer(outlink, in->nb_samples);
  1172. if (!out) {
  1173. av_frame_free(&in);
  1174. return AVERROR(ENOMEM);
  1175. }
  1176. av_frame_copy_props(out, in);
  1177. }
  1178. td.in = in;
  1179. td.out = out;
  1180. ctx->internal->execute(ctx, s->iir_channel, &td, NULL, outlink->channels);
  1181. for (ch = 0; ch < outlink->channels; ch++) {
  1182. if (s->iir[ch].clippings > 0)
  1183. av_log(ctx, AV_LOG_WARNING, "Channel %d clipping %d times. Please reduce gain.\n",
  1184. ch, s->iir[ch].clippings);
  1185. s->iir[ch].clippings = 0;
  1186. }
  1187. if (in != out)
  1188. av_frame_free(&in);
  1189. if (s->response) {
  1190. AVFilterLink *outlink = ctx->outputs[1];
  1191. int64_t old_pts = s->video->pts;
  1192. int64_t new_pts = av_rescale_q(out->pts, ctx->inputs[0]->time_base, outlink->time_base);
  1193. if (new_pts > old_pts) {
  1194. AVFrame *clone;
  1195. s->video->pts = new_pts;
  1196. clone = av_frame_clone(s->video);
  1197. if (!clone)
  1198. return AVERROR(ENOMEM);
  1199. ret = ff_filter_frame(outlink, clone);
  1200. if (ret < 0)
  1201. return ret;
  1202. }
  1203. }
  1204. return ff_filter_frame(outlink, out);
  1205. }
  1206. static int config_video(AVFilterLink *outlink)
  1207. {
  1208. AVFilterContext *ctx = outlink->src;
  1209. AudioIIRContext *s = ctx->priv;
  1210. outlink->sample_aspect_ratio = (AVRational){1,1};
  1211. outlink->w = s->w;
  1212. outlink->h = s->h;
  1213. outlink->frame_rate = s->rate;
  1214. outlink->time_base = av_inv_q(outlink->frame_rate);
  1215. return 0;
  1216. }
  1217. static av_cold int init(AVFilterContext *ctx)
  1218. {
  1219. AudioIIRContext *s = ctx->priv;
  1220. AVFilterPad pad, vpad;
  1221. int ret;
  1222. if (!s->a_str || !s->b_str || !s->g_str) {
  1223. av_log(ctx, AV_LOG_ERROR, "Valid coefficients are mandatory.\n");
  1224. return AVERROR(EINVAL);
  1225. }
  1226. switch (s->precision) {
  1227. case 0: s->sample_format = AV_SAMPLE_FMT_DBLP; break;
  1228. case 1: s->sample_format = AV_SAMPLE_FMT_FLTP; break;
  1229. case 2: s->sample_format = AV_SAMPLE_FMT_S32P; break;
  1230. case 3: s->sample_format = AV_SAMPLE_FMT_S16P; break;
  1231. default: return AVERROR_BUG;
  1232. }
  1233. pad = (AVFilterPad){
  1234. .name = "default",
  1235. .type = AVMEDIA_TYPE_AUDIO,
  1236. .config_props = config_output,
  1237. };
  1238. ret = ff_insert_outpad(ctx, 0, &pad);
  1239. if (ret < 0)
  1240. return ret;
  1241. if (s->response) {
  1242. vpad = (AVFilterPad){
  1243. .name = "filter_response",
  1244. .type = AVMEDIA_TYPE_VIDEO,
  1245. .config_props = config_video,
  1246. };
  1247. ret = ff_insert_outpad(ctx, 1, &vpad);
  1248. if (ret < 0)
  1249. return ret;
  1250. }
  1251. return 0;
  1252. }
  1253. static av_cold void uninit(AVFilterContext *ctx)
  1254. {
  1255. AudioIIRContext *s = ctx->priv;
  1256. int ch;
  1257. if (s->iir) {
  1258. for (ch = 0; ch < s->channels; ch++) {
  1259. IIRChannel *iir = &s->iir[ch];
  1260. av_freep(&iir->ab[0]);
  1261. av_freep(&iir->ab[1]);
  1262. av_freep(&iir->cache[0]);
  1263. av_freep(&iir->cache[1]);
  1264. av_freep(&iir->biquads);
  1265. }
  1266. }
  1267. av_freep(&s->iir);
  1268. av_frame_free(&s->video);
  1269. }
  1270. static const AVFilterPad inputs[] = {
  1271. {
  1272. .name = "default",
  1273. .type = AVMEDIA_TYPE_AUDIO,
  1274. .filter_frame = filter_frame,
  1275. },
  1276. { NULL }
  1277. };
  1278. #define OFFSET(x) offsetof(AudioIIRContext, x)
  1279. #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  1280. #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  1281. static const AVOption aiir_options[] = {
  1282. { "zeros", "set B/numerator/zeros/reflection coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  1283. { "z", "set B/numerator/zeros/reflection coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  1284. { "poles", "set A/denominator/poles/ladder coefficients", OFFSET(a_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  1285. { "p", "set A/denominator/poles/ladder coefficients", OFFSET(a_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  1286. { "gains", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
  1287. { "k", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
  1288. { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
  1289. { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
  1290. { "format", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, -2, 4, AF, "format" },
  1291. { "f", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, -2, 4, AF, "format" },
  1292. { "ll", "lattice-ladder function", 0, AV_OPT_TYPE_CONST, {.i64=-2}, 0, 0, AF, "format" },
  1293. { "sf", "analog transfer function", 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, AF, "format" },
  1294. { "tf", "digital transfer function", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "format" },
  1295. { "zp", "Z-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "format" },
  1296. { "pr", "Z-plane zeros/poles (polar radians)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "format" },
  1297. { "pd", "Z-plane zeros/poles (polar degrees)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "format" },
  1298. { "sp", "S-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "format" },
  1299. { "process", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, AF, "process" },
  1300. { "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, AF, "process" },
  1301. { "d", "direct", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "process" },
  1302. { "s", "serial", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "process" },
  1303. { "p", "parallel", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "process" },
  1304. { "precision", "set filtering precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
  1305. { "e", "set precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
  1306. { "dbl", "double-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" },
  1307. { "flt", "single-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "precision" },
  1308. { "i32", "32-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "precision" },
  1309. { "i16", "16-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "precision" },
  1310. { "normalize", "normalize coefficients", OFFSET(normalize),AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
  1311. { "n", "normalize coefficients", OFFSET(normalize),AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
  1312. { "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
  1313. { "response", "show IR frequency response", OFFSET(response), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
  1314. { "channel", "set IR channel to display frequency response", OFFSET(ir_channel), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, VF },
  1315. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, VF },
  1316. { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT32_MAX, VF },
  1317. { NULL },
  1318. };
  1319. AVFILTER_DEFINE_CLASS(aiir);
  1320. AVFilter ff_af_aiir = {
  1321. .name = "aiir",
  1322. .description = NULL_IF_CONFIG_SMALL("Apply Infinite Impulse Response filter with supplied coefficients."),
  1323. .priv_size = sizeof(AudioIIRContext),
  1324. .priv_class = &aiir_class,
  1325. .init = init,
  1326. .uninit = uninit,
  1327. .query_formats = query_formats,
  1328. .inputs = inputs,
  1329. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
  1330. AVFILTER_FLAG_SLICE_THREADS,
  1331. };