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.

1264 lines
43KB

  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 i1, i2;
  39. double o1, o2;
  40. } BiquadContext;
  41. typedef struct IIRChannel {
  42. int nb_ab[2];
  43. double *ab[2];
  44. double g;
  45. double *cache[2];
  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->in_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, int ch, int nb_jobs) \
  162. { \
  163. AudioIIRContext *s = ctx->priv; \
  164. const double ig = s->dry_gain; \
  165. const double og = s->wet_gain; \
  166. const double mix = s->mix; \
  167. ThreadData *td = arg; \
  168. AVFrame *in = td->in, *out = td->out; \
  169. const type *src = (const type *)in->extended_data[ch]; \
  170. type *dst = (type *)out->extended_data[ch]; \
  171. IIRChannel *iir = &s->iir[ch]; \
  172. const double g = iir->g; \
  173. int *clippings = &iir->clippings; \
  174. int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; \
  175. int n, i; \
  176. \
  177. for (i = 0; i < nb_biquads; i++) { \
  178. const double a1 = -iir->biquads[i].a[1]; \
  179. const double a2 = -iir->biquads[i].a[2]; \
  180. const double b0 = iir->biquads[i].b[0]; \
  181. const double b1 = iir->biquads[i].b[1]; \
  182. const double b2 = iir->biquads[i].b[2]; \
  183. double i1 = iir->biquads[i].i1; \
  184. double i2 = iir->biquads[i].i2; \
  185. double o1 = iir->biquads[i].o1; \
  186. double o2 = iir->biquads[i].o2; \
  187. \
  188. for (n = 0; n < in->nb_samples; n++) { \
  189. double sample = ig * (i ? dst[n] : src[n]); \
  190. double o0 = sample * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
  191. \
  192. i2 = i1; \
  193. i1 = src[n]; \
  194. o2 = o1; \
  195. o1 = o0; \
  196. o0 *= og * g; \
  197. \
  198. o0 = o0 * mix + (1. - mix) * sample; \
  199. if (need_clipping && o0 < min) { \
  200. (*clippings)++; \
  201. dst[n] = min; \
  202. } else if (need_clipping && o0 > max) { \
  203. (*clippings)++; \
  204. dst[n] = max; \
  205. } else { \
  206. dst[n] = o0; \
  207. } \
  208. } \
  209. iir->biquads[i].i1 = i1; \
  210. iir->biquads[i].i2 = i2; \
  211. iir->biquads[i].o1 = o1; \
  212. iir->biquads[i].o2 = o2; \
  213. } \
  214. \
  215. return 0; \
  216. }
  217. SERIAL_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
  218. SERIAL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
  219. SERIAL_IIR_CH(fltp, float, -1., 1., 0)
  220. SERIAL_IIR_CH(dblp, double, -1., 1., 0)
  221. static void count_coefficients(char *item_str, int *nb_items)
  222. {
  223. char *p;
  224. if (!item_str)
  225. return;
  226. *nb_items = 1;
  227. for (p = item_str; *p && *p != '|'; p++) {
  228. if (*p == ' ')
  229. (*nb_items)++;
  230. }
  231. }
  232. static int read_gains(AVFilterContext *ctx, char *item_str, int nb_items)
  233. {
  234. AudioIIRContext *s = ctx->priv;
  235. char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
  236. int i;
  237. p = old_str = av_strdup(item_str);
  238. if (!p)
  239. return AVERROR(ENOMEM);
  240. for (i = 0; i < nb_items; i++) {
  241. if (!(arg = av_strtok(p, "|", &saveptr)))
  242. arg = prev_arg;
  243. if (!arg) {
  244. av_freep(&old_str);
  245. return AVERROR(EINVAL);
  246. }
  247. p = NULL;
  248. if (sscanf(arg, "%lf", &s->iir[i].g) != 1) {
  249. av_log(ctx, AV_LOG_ERROR, "Invalid gains supplied: %s\n", arg);
  250. av_freep(&old_str);
  251. return AVERROR(EINVAL);
  252. }
  253. prev_arg = arg;
  254. }
  255. av_freep(&old_str);
  256. return 0;
  257. }
  258. static int read_tf_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst)
  259. {
  260. char *p, *arg, *old_str, *saveptr = NULL;
  261. int i;
  262. p = old_str = av_strdup(item_str);
  263. if (!p)
  264. return AVERROR(ENOMEM);
  265. for (i = 0; i < nb_items; i++) {
  266. if (!(arg = av_strtok(p, " ", &saveptr)))
  267. break;
  268. p = NULL;
  269. if (sscanf(arg, "%lf", &dst[i]) != 1) {
  270. av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
  271. av_freep(&old_str);
  272. return AVERROR(EINVAL);
  273. }
  274. }
  275. av_freep(&old_str);
  276. return 0;
  277. }
  278. static int read_zp_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst, const char *format)
  279. {
  280. char *p, *arg, *old_str, *saveptr = NULL;
  281. int i;
  282. p = old_str = av_strdup(item_str);
  283. if (!p)
  284. return AVERROR(ENOMEM);
  285. for (i = 0; i < nb_items; i++) {
  286. if (!(arg = av_strtok(p, " ", &saveptr)))
  287. break;
  288. p = NULL;
  289. if (sscanf(arg, format, &dst[i*2], &dst[i*2+1]) != 2) {
  290. av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
  291. av_freep(&old_str);
  292. return AVERROR(EINVAL);
  293. }
  294. }
  295. av_freep(&old_str);
  296. return 0;
  297. }
  298. static const char *format[] = { "%lf", "%lf %lfi", "%lf %lfr", "%lf %lfd" };
  299. static int read_channels(AVFilterContext *ctx, int channels, uint8_t *item_str, int ab)
  300. {
  301. AudioIIRContext *s = ctx->priv;
  302. char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
  303. int i, ret;
  304. p = old_str = av_strdup(item_str);
  305. if (!p)
  306. return AVERROR(ENOMEM);
  307. for (i = 0; i < channels; i++) {
  308. IIRChannel *iir = &s->iir[i];
  309. if (!(arg = av_strtok(p, "|", &saveptr)))
  310. arg = prev_arg;
  311. if (!arg) {
  312. av_freep(&old_str);
  313. return AVERROR(EINVAL);
  314. }
  315. count_coefficients(arg, &iir->nb_ab[ab]);
  316. p = NULL;
  317. iir->cache[ab] = av_calloc(iir->nb_ab[ab] + 1, sizeof(double));
  318. iir->ab[ab] = av_calloc(iir->nb_ab[ab] * (!!s->format + 1), sizeof(double));
  319. if (!iir->ab[ab] || !iir->cache[ab]) {
  320. av_freep(&old_str);
  321. return AVERROR(ENOMEM);
  322. }
  323. if (s->format) {
  324. ret = read_zp_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab], format[s->format]);
  325. } else {
  326. ret = read_tf_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab]);
  327. }
  328. if (ret < 0) {
  329. av_freep(&old_str);
  330. return ret;
  331. }
  332. prev_arg = arg;
  333. }
  334. av_freep(&old_str);
  335. return 0;
  336. }
  337. static void multiply(double wre, double wim, int npz, double *coeffs)
  338. {
  339. double nwre = -wre, nwim = -wim;
  340. double cre, cim;
  341. int i;
  342. for (i = npz; i >= 1; i--) {
  343. cre = coeffs[2 * i + 0];
  344. cim = coeffs[2 * i + 1];
  345. coeffs[2 * i + 0] = (nwre * cre - nwim * cim) + coeffs[2 * (i - 1) + 0];
  346. coeffs[2 * i + 1] = (nwre * cim + nwim * cre) + coeffs[2 * (i - 1) + 1];
  347. }
  348. cre = coeffs[0];
  349. cim = coeffs[1];
  350. coeffs[0] = nwre * cre - nwim * cim;
  351. coeffs[1] = nwre * cim + nwim * cre;
  352. }
  353. static int expand(AVFilterContext *ctx, double *pz, int nb, double *coeffs)
  354. {
  355. int i;
  356. coeffs[0] = 1.0;
  357. coeffs[1] = 0.0;
  358. for (i = 0; i < nb; i++) {
  359. coeffs[2 * (i + 1) ] = 0.0;
  360. coeffs[2 * (i + 1) + 1] = 0.0;
  361. }
  362. for (i = 0; i < nb; i++)
  363. multiply(pz[2 * i], pz[2 * i + 1], nb, coeffs);
  364. for (i = 0; i < nb + 1; i++) {
  365. if (fabs(coeffs[2 * i + 1]) > FLT_EPSILON) {
  366. av_log(ctx, AV_LOG_ERROR, "coeff: %f of z^%d is not real; poles/zeros are not complex conjugates.\n",
  367. coeffs[2 * i + 1], i);
  368. return AVERROR(EINVAL);
  369. }
  370. }
  371. return 0;
  372. }
  373. static void normalize_coeffs(AVFilterContext *ctx, int ch)
  374. {
  375. AudioIIRContext *s = ctx->priv;
  376. IIRChannel *iir = &s->iir[ch];
  377. double sum_den = 0.;
  378. if (!s->normalize)
  379. return;
  380. for (int i = 0; i < iir->nb_ab[1]; i++) {
  381. sum_den += iir->ab[1][i];
  382. }
  383. if (sum_den > 1e-6) {
  384. double factor, sum_num = 0.;
  385. for (int i = 0; i < iir->nb_ab[0]; i++) {
  386. sum_num += iir->ab[0][i];
  387. }
  388. factor = sum_num / sum_den;
  389. for (int i = 0; i < iir->nb_ab[1]; i++) {
  390. iir->ab[1][i] *= factor;
  391. }
  392. }
  393. }
  394. static int convert_zp2tf(AVFilterContext *ctx, int channels)
  395. {
  396. AudioIIRContext *s = ctx->priv;
  397. int ch, i, j, ret = 0;
  398. for (ch = 0; ch < channels; ch++) {
  399. IIRChannel *iir = &s->iir[ch];
  400. double *topc, *botc;
  401. topc = av_calloc((iir->nb_ab[0] + 1) * 2, sizeof(*topc));
  402. botc = av_calloc((iir->nb_ab[1] + 1) * 2, sizeof(*botc));
  403. if (!topc || !botc) {
  404. ret = AVERROR(ENOMEM);
  405. goto fail;
  406. }
  407. ret = expand(ctx, iir->ab[0], iir->nb_ab[0], botc);
  408. if (ret < 0) {
  409. goto fail;
  410. }
  411. ret = expand(ctx, iir->ab[1], iir->nb_ab[1], topc);
  412. if (ret < 0) {
  413. goto fail;
  414. }
  415. for (j = 0, i = iir->nb_ab[1]; i >= 0; j++, i--) {
  416. iir->ab[1][j] = topc[2 * i];
  417. }
  418. iir->nb_ab[1]++;
  419. for (j = 0, i = iir->nb_ab[0]; i >= 0; j++, i--) {
  420. iir->ab[0][j] = botc[2 * i];
  421. }
  422. iir->nb_ab[0]++;
  423. normalize_coeffs(ctx, ch);
  424. fail:
  425. av_free(topc);
  426. av_free(botc);
  427. if (ret < 0)
  428. break;
  429. }
  430. return ret;
  431. }
  432. static int decompose_zp2biquads(AVFilterContext *ctx, int channels)
  433. {
  434. AudioIIRContext *s = ctx->priv;
  435. int ch, ret;
  436. for (ch = 0; ch < channels; ch++) {
  437. IIRChannel *iir = &s->iir[ch];
  438. int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2;
  439. int current_biquad = 0;
  440. iir->biquads = av_calloc(nb_biquads, sizeof(BiquadContext));
  441. if (!iir->biquads)
  442. return AVERROR(ENOMEM);
  443. while (nb_biquads--) {
  444. Pair outmost_pole = { -1, -1 };
  445. Pair nearest_zero = { -1, -1 };
  446. double zeros[4] = { 0 };
  447. double poles[4] = { 0 };
  448. double b[6] = { 0 };
  449. double a[6] = { 0 };
  450. double min_distance = DBL_MAX;
  451. double max_mag = 0;
  452. double factor;
  453. int i;
  454. for (i = 0; i < iir->nb_ab[0]; i++) {
  455. double mag;
  456. if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
  457. continue;
  458. mag = hypot(iir->ab[0][2 * i], iir->ab[0][2 * i + 1]);
  459. if (mag > max_mag) {
  460. max_mag = mag;
  461. outmost_pole.a = i;
  462. }
  463. }
  464. for (i = 0; i < iir->nb_ab[0]; i++) {
  465. if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
  466. continue;
  467. if (iir->ab[0][2 * i ] == iir->ab[0][2 * outmost_pole.a ] &&
  468. iir->ab[0][2 * i + 1] == -iir->ab[0][2 * outmost_pole.a + 1]) {
  469. outmost_pole.b = i;
  470. break;
  471. }
  472. }
  473. av_log(ctx, AV_LOG_VERBOSE, "outmost_pole is %d.%d\n", outmost_pole.a, outmost_pole.b);
  474. if (outmost_pole.a < 0 || outmost_pole.b < 0)
  475. return AVERROR(EINVAL);
  476. for (i = 0; i < iir->nb_ab[1]; i++) {
  477. double distance;
  478. if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
  479. continue;
  480. distance = hypot(iir->ab[0][2 * outmost_pole.a ] - iir->ab[1][2 * i ],
  481. iir->ab[0][2 * outmost_pole.a + 1] - iir->ab[1][2 * i + 1]);
  482. if (distance < min_distance) {
  483. min_distance = distance;
  484. nearest_zero.a = i;
  485. }
  486. }
  487. for (i = 0; i < iir->nb_ab[1]; i++) {
  488. if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
  489. continue;
  490. if (iir->ab[1][2 * i ] == iir->ab[1][2 * nearest_zero.a ] &&
  491. iir->ab[1][2 * i + 1] == -iir->ab[1][2 * nearest_zero.a + 1]) {
  492. nearest_zero.b = i;
  493. break;
  494. }
  495. }
  496. av_log(ctx, AV_LOG_VERBOSE, "nearest_zero is %d.%d\n", nearest_zero.a, nearest_zero.b);
  497. if (nearest_zero.a < 0 || nearest_zero.b < 0)
  498. return AVERROR(EINVAL);
  499. poles[0] = iir->ab[0][2 * outmost_pole.a ];
  500. poles[1] = iir->ab[0][2 * outmost_pole.a + 1];
  501. zeros[0] = iir->ab[1][2 * nearest_zero.a ];
  502. zeros[1] = iir->ab[1][2 * nearest_zero.a + 1];
  503. if (nearest_zero.a == nearest_zero.b && outmost_pole.a == outmost_pole.b) {
  504. zeros[2] = 0;
  505. zeros[3] = 0;
  506. poles[2] = 0;
  507. poles[3] = 0;
  508. } else {
  509. poles[2] = iir->ab[0][2 * outmost_pole.b ];
  510. poles[3] = iir->ab[0][2 * outmost_pole.b + 1];
  511. zeros[2] = iir->ab[1][2 * nearest_zero.b ];
  512. zeros[3] = iir->ab[1][2 * nearest_zero.b + 1];
  513. }
  514. ret = expand(ctx, zeros, 2, b);
  515. if (ret < 0)
  516. return ret;
  517. ret = expand(ctx, poles, 2, a);
  518. if (ret < 0)
  519. return ret;
  520. iir->ab[0][2 * outmost_pole.a] = iir->ab[0][2 * outmost_pole.a + 1] = NAN;
  521. iir->ab[0][2 * outmost_pole.b] = iir->ab[0][2 * outmost_pole.b + 1] = NAN;
  522. iir->ab[1][2 * nearest_zero.a] = iir->ab[1][2 * nearest_zero.a + 1] = NAN;
  523. iir->ab[1][2 * nearest_zero.b] = iir->ab[1][2 * nearest_zero.b + 1] = NAN;
  524. iir->biquads[current_biquad].a[0] = 1.;
  525. iir->biquads[current_biquad].a[1] = a[2] / a[4];
  526. iir->biquads[current_biquad].a[2] = a[0] / a[4];
  527. iir->biquads[current_biquad].b[0] = b[4] / a[4];
  528. iir->biquads[current_biquad].b[1] = b[2] / a[4];
  529. iir->biquads[current_biquad].b[2] = b[0] / a[4];
  530. if (s->normalize &&
  531. fabs(iir->biquads[current_biquad].b[0] +
  532. iir->biquads[current_biquad].b[1] +
  533. iir->biquads[current_biquad].b[2]) > 1e-6) {
  534. factor = (iir->biquads[current_biquad].a[0] +
  535. iir->biquads[current_biquad].a[1] +
  536. iir->biquads[current_biquad].a[2]) /
  537. (iir->biquads[current_biquad].b[0] +
  538. iir->biquads[current_biquad].b[1] +
  539. iir->biquads[current_biquad].b[2]);
  540. av_log(ctx, AV_LOG_VERBOSE, "factor=%f\n", factor);
  541. iir->biquads[current_biquad].b[0] *= factor;
  542. iir->biquads[current_biquad].b[1] *= factor;
  543. iir->biquads[current_biquad].b[2] *= factor;
  544. }
  545. iir->biquads[current_biquad].b[0] *= (current_biquad ? 1.0 : iir->g);
  546. iir->biquads[current_biquad].b[1] *= (current_biquad ? 1.0 : iir->g);
  547. iir->biquads[current_biquad].b[2] *= (current_biquad ? 1.0 : iir->g);
  548. av_log(ctx, AV_LOG_VERBOSE, "a=%f %f %f:b=%f %f %f\n",
  549. iir->biquads[current_biquad].a[0],
  550. iir->biquads[current_biquad].a[1],
  551. iir->biquads[current_biquad].a[2],
  552. iir->biquads[current_biquad].b[0],
  553. iir->biquads[current_biquad].b[1],
  554. iir->biquads[current_biquad].b[2]);
  555. current_biquad++;
  556. }
  557. }
  558. return 0;
  559. }
  560. static void convert_pr2zp(AVFilterContext *ctx, int channels)
  561. {
  562. AudioIIRContext *s = ctx->priv;
  563. int ch;
  564. for (ch = 0; ch < channels; ch++) {
  565. IIRChannel *iir = &s->iir[ch];
  566. int n;
  567. for (n = 0; n < iir->nb_ab[0]; n++) {
  568. double r = iir->ab[0][2*n];
  569. double angle = iir->ab[0][2*n+1];
  570. iir->ab[0][2*n] = r * cos(angle);
  571. iir->ab[0][2*n+1] = r * sin(angle);
  572. }
  573. for (n = 0; n < iir->nb_ab[1]; n++) {
  574. double r = iir->ab[1][2*n];
  575. double angle = iir->ab[1][2*n+1];
  576. iir->ab[1][2*n] = r * cos(angle);
  577. iir->ab[1][2*n+1] = r * sin(angle);
  578. }
  579. }
  580. }
  581. static void convert_pd2zp(AVFilterContext *ctx, int channels)
  582. {
  583. AudioIIRContext *s = ctx->priv;
  584. int ch;
  585. for (ch = 0; ch < channels; ch++) {
  586. IIRChannel *iir = &s->iir[ch];
  587. int n;
  588. for (n = 0; n < iir->nb_ab[0]; n++) {
  589. double r = iir->ab[0][2*n];
  590. double angle = M_PI*iir->ab[0][2*n+1]/180.;
  591. iir->ab[0][2*n] = r * cos(angle);
  592. iir->ab[0][2*n+1] = r * sin(angle);
  593. }
  594. for (n = 0; n < iir->nb_ab[1]; n++) {
  595. double r = iir->ab[1][2*n];
  596. double angle = M_PI*iir->ab[1][2*n+1]/180.;
  597. iir->ab[1][2*n] = r * cos(angle);
  598. iir->ab[1][2*n+1] = r * sin(angle);
  599. }
  600. }
  601. }
  602. static void check_stability(AVFilterContext *ctx, int channels)
  603. {
  604. AudioIIRContext *s = ctx->priv;
  605. int ch;
  606. for (ch = 0; ch < channels; ch++) {
  607. IIRChannel *iir = &s->iir[ch];
  608. for (int n = 0; n < iir->nb_ab[0]; n++) {
  609. double pr = hypot(iir->ab[0][2*n], iir->ab[0][2*n+1]);
  610. if (pr >= 1.) {
  611. av_log(ctx, AV_LOG_WARNING, "pole %d at channel %d is unstable\n", n, ch);
  612. break;
  613. }
  614. }
  615. }
  616. }
  617. static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint32_t color)
  618. {
  619. const uint8_t *font;
  620. int font_height;
  621. int i;
  622. font = avpriv_cga_font, font_height = 8;
  623. for (i = 0; txt[i]; i++) {
  624. int char_y, mask;
  625. uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
  626. for (char_y = 0; char_y < font_height; char_y++) {
  627. for (mask = 0x80; mask; mask >>= 1) {
  628. if (font[txt[i] * font_height + char_y] & mask)
  629. AV_WL32(p, color);
  630. p += 4;
  631. }
  632. p += pic->linesize[0] - 8 * 4;
  633. }
  634. }
  635. }
  636. static void draw_line(AVFrame *out, int x0, int y0, int x1, int y1, uint32_t color)
  637. {
  638. int dx = FFABS(x1-x0);
  639. int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
  640. int err = (dx>dy ? dx : -dy) / 2, e2;
  641. for (;;) {
  642. AV_WL32(out->data[0] + y0 * out->linesize[0] + x0 * 4, color);
  643. if (x0 == x1 && y0 == y1)
  644. break;
  645. e2 = err;
  646. if (e2 >-dx) {
  647. err -= dy;
  648. x0--;
  649. }
  650. if (e2 < dy) {
  651. err += dx;
  652. y0 += sy;
  653. }
  654. }
  655. }
  656. static void get_response(int channel, int format, double w,
  657. const double *b, const double *a,
  658. int nb_b, int nb_a, double *r, double *i)
  659. {
  660. double realz, realp;
  661. double imagz, imagp;
  662. double real, imag;
  663. double div;
  664. if (format == 0) {
  665. realz = 0., realp = 0.;
  666. imagz = 0., imagp = 0.;
  667. for (int x = 0; x < nb_a; x++) {
  668. realz += cos(-x * w) * a[x];
  669. imagz += sin(-x * w) * a[x];
  670. }
  671. for (int x = 0; x < nb_b; x++) {
  672. realp += cos(-x * w) * b[x];
  673. imagp += sin(-x * w) * b[x];
  674. }
  675. div = realp * realp + imagp * imagp;
  676. real = (realz * realp + imagz * imagp) / div;
  677. imag = (imagz * realp - imagp * realz) / div;
  678. } else {
  679. real = 1;
  680. imag = 0;
  681. for (int x = 0; x < nb_a; x++) {
  682. double ore, oim, re, im;
  683. re = cos(w) - a[2 * x];
  684. im = sin(w) - a[2 * x + 1];
  685. ore = real;
  686. oim = imag;
  687. real = ore * re - oim * im;
  688. imag = ore * im + oim * re;
  689. }
  690. for (int x = 0; x < nb_b; x++) {
  691. double ore, oim, re, im;
  692. re = cos(w) - b[2 * x];
  693. im = sin(w) - b[2 * x + 1];
  694. ore = real;
  695. oim = imag;
  696. div = re * re + im * im;
  697. real = (ore * re + oim * im) / div;
  698. imag = (oim * re - ore * im) / div;
  699. }
  700. }
  701. *r = real;
  702. *i = imag;
  703. }
  704. static void draw_response(AVFilterContext *ctx, AVFrame *out, int sample_rate)
  705. {
  706. AudioIIRContext *s = ctx->priv;
  707. double *mag, *phase, *temp, *delay, min = DBL_MAX, max = -DBL_MAX;
  708. double min_delay = DBL_MAX, max_delay = -DBL_MAX, min_phase, max_phase;
  709. int prev_ymag = -1, prev_yphase = -1, prev_ydelay = -1;
  710. char text[32];
  711. int ch, i;
  712. memset(out->data[0], 0, s->h * out->linesize[0]);
  713. phase = av_malloc_array(s->w, sizeof(*phase));
  714. temp = av_malloc_array(s->w, sizeof(*temp));
  715. mag = av_malloc_array(s->w, sizeof(*mag));
  716. delay = av_malloc_array(s->w, sizeof(*delay));
  717. if (!mag || !phase || !delay || !temp)
  718. goto end;
  719. ch = av_clip(s->ir_channel, 0, s->channels - 1);
  720. for (i = 0; i < s->w; i++) {
  721. const double *b = s->iir[ch].ab[0];
  722. const double *a = s->iir[ch].ab[1];
  723. const int nb_b = s->iir[ch].nb_ab[0];
  724. const int nb_a = s->iir[ch].nb_ab[1];
  725. double w = i * M_PI / (s->w - 1);
  726. double real, imag;
  727. get_response(ch, s->format, w, b, a, nb_b, nb_a, &real, &imag);
  728. mag[i] = s->iir[ch].g * hypot(real, imag);
  729. phase[i] = atan2(imag, real);
  730. min = fmin(min, mag[i]);
  731. max = fmax(max, mag[i]);
  732. }
  733. temp[0] = 0.;
  734. for (i = 0; i < s->w - 1; i++) {
  735. double d = phase[i] - phase[i + 1];
  736. temp[i + 1] = ceil(fabs(d) / (2. * M_PI)) * 2. * M_PI * ((d > M_PI) - (d < -M_PI));
  737. }
  738. min_phase = phase[0];
  739. max_phase = phase[0];
  740. for (i = 1; i < s->w; i++) {
  741. temp[i] += temp[i - 1];
  742. phase[i] += temp[i];
  743. min_phase = fmin(min_phase, phase[i]);
  744. max_phase = fmax(max_phase, phase[i]);
  745. }
  746. for (i = 0; i < s->w - 1; i++) {
  747. double div = s->w / (double)sample_rate;
  748. delay[i + 1] = -(phase[i] - phase[i + 1]) / div;
  749. min_delay = fmin(min_delay, delay[i + 1]);
  750. max_delay = fmax(max_delay, delay[i + 1]);
  751. }
  752. delay[0] = delay[1];
  753. for (i = 0; i < s->w; i++) {
  754. int ymag = mag[i] / max * (s->h - 1);
  755. int ydelay = (delay[i] - min_delay) / (max_delay - min_delay) * (s->h - 1);
  756. int yphase = (phase[i] - min_phase) / (max_phase - min_phase) * (s->h - 1);
  757. ymag = s->h - 1 - av_clip(ymag, 0, s->h - 1);
  758. yphase = s->h - 1 - av_clip(yphase, 0, s->h - 1);
  759. ydelay = s->h - 1 - av_clip(ydelay, 0, s->h - 1);
  760. if (prev_ymag < 0)
  761. prev_ymag = ymag;
  762. if (prev_yphase < 0)
  763. prev_yphase = yphase;
  764. if (prev_ydelay < 0)
  765. prev_ydelay = ydelay;
  766. draw_line(out, i, ymag, FFMAX(i - 1, 0), prev_ymag, 0xFFFF00FF);
  767. draw_line(out, i, yphase, FFMAX(i - 1, 0), prev_yphase, 0xFF00FF00);
  768. draw_line(out, i, ydelay, FFMAX(i - 1, 0), prev_ydelay, 0xFF00FFFF);
  769. prev_ymag = ymag;
  770. prev_yphase = yphase;
  771. prev_ydelay = ydelay;
  772. }
  773. if (s->w > 400 && s->h > 100) {
  774. drawtext(out, 2, 2, "Max Magnitude:", 0xDDDDDDDD);
  775. snprintf(text, sizeof(text), "%.2f", max);
  776. drawtext(out, 15 * 8 + 2, 2, text, 0xDDDDDDDD);
  777. drawtext(out, 2, 12, "Min Magnitude:", 0xDDDDDDDD);
  778. snprintf(text, sizeof(text), "%.2f", min);
  779. drawtext(out, 15 * 8 + 2, 12, text, 0xDDDDDDDD);
  780. drawtext(out, 2, 22, "Max Phase:", 0xDDDDDDDD);
  781. snprintf(text, sizeof(text), "%.2f", max_phase);
  782. drawtext(out, 15 * 8 + 2, 22, text, 0xDDDDDDDD);
  783. drawtext(out, 2, 32, "Min Phase:", 0xDDDDDDDD);
  784. snprintf(text, sizeof(text), "%.2f", min_phase);
  785. drawtext(out, 15 * 8 + 2, 32, text, 0xDDDDDDDD);
  786. drawtext(out, 2, 42, "Max Delay:", 0xDDDDDDDD);
  787. snprintf(text, sizeof(text), "%.2f", max_delay);
  788. drawtext(out, 11 * 8 + 2, 42, text, 0xDDDDDDDD);
  789. drawtext(out, 2, 52, "Min Delay:", 0xDDDDDDDD);
  790. snprintf(text, sizeof(text), "%.2f", min_delay);
  791. drawtext(out, 11 * 8 + 2, 52, text, 0xDDDDDDDD);
  792. }
  793. end:
  794. av_free(delay);
  795. av_free(temp);
  796. av_free(phase);
  797. av_free(mag);
  798. }
  799. static int config_output(AVFilterLink *outlink)
  800. {
  801. AVFilterContext *ctx = outlink->src;
  802. AudioIIRContext *s = ctx->priv;
  803. AVFilterLink *inlink = ctx->inputs[0];
  804. int ch, ret, i;
  805. s->channels = inlink->channels;
  806. s->iir = av_calloc(s->channels, sizeof(*s->iir));
  807. if (!s->iir)
  808. return AVERROR(ENOMEM);
  809. ret = read_gains(ctx, s->g_str, inlink->channels);
  810. if (ret < 0)
  811. return ret;
  812. ret = read_channels(ctx, inlink->channels, s->a_str, 0);
  813. if (ret < 0)
  814. return ret;
  815. ret = read_channels(ctx, inlink->channels, s->b_str, 1);
  816. if (ret < 0)
  817. return ret;
  818. if (s->format == 2) {
  819. convert_pr2zp(ctx, inlink->channels);
  820. } else if (s->format == 3) {
  821. convert_pd2zp(ctx, inlink->channels);
  822. }
  823. if (s->format > 0) {
  824. check_stability(ctx, inlink->channels);
  825. }
  826. if (s->format == 0)
  827. av_log(ctx, AV_LOG_WARNING, "tf coefficients format is not recommended for too high number of zeros/poles.\n");
  828. if (s->format > 0 && s->process == 0) {
  829. av_log(ctx, AV_LOG_WARNING, "Direct processsing is not recommended for zp coefficients format.\n");
  830. ret = convert_zp2tf(ctx, inlink->channels);
  831. if (ret < 0)
  832. return ret;
  833. } else if (s->format == 0 && s->process == 1) {
  834. av_log(ctx, AV_LOG_ERROR, "Serial cascading is not implemented for transfer function.\n");
  835. return AVERROR_PATCHWELCOME;
  836. } else if (s->format > 0 && s->process == 1) {
  837. if (inlink->format == AV_SAMPLE_FMT_S16P)
  838. av_log(ctx, AV_LOG_WARNING, "Serial cascading is not recommended for i16 precision.\n");
  839. ret = decompose_zp2biquads(ctx, inlink->channels);
  840. if (ret < 0)
  841. return ret;
  842. }
  843. for (ch = 0; s->format == 0 && ch < inlink->channels; ch++) {
  844. IIRChannel *iir = &s->iir[ch];
  845. for (i = 1; i < iir->nb_ab[0]; i++) {
  846. iir->ab[0][i] /= iir->ab[0][0];
  847. }
  848. iir->ab[0][0] = 1.0;
  849. for (i = 0; i < iir->nb_ab[1]; i++) {
  850. iir->ab[1][i] *= iir->g;
  851. }
  852. normalize_coeffs(ctx, ch);
  853. }
  854. switch (inlink->format) {
  855. case AV_SAMPLE_FMT_DBLP: s->iir_channel = s->process == 1 ? iir_ch_serial_dblp : iir_ch_dblp; break;
  856. case AV_SAMPLE_FMT_FLTP: s->iir_channel = s->process == 1 ? iir_ch_serial_fltp : iir_ch_fltp; break;
  857. case AV_SAMPLE_FMT_S32P: s->iir_channel = s->process == 1 ? iir_ch_serial_s32p : iir_ch_s32p; break;
  858. case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break;
  859. }
  860. av_frame_free(&s->video);
  861. if (s->response) {
  862. s->video = ff_get_video_buffer(ctx->outputs[1], s->w, s->h);
  863. if (!s->video)
  864. return AVERROR(ENOMEM);
  865. draw_response(ctx, s->video, inlink->sample_rate);
  866. }
  867. return 0;
  868. }
  869. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  870. {
  871. AVFilterContext *ctx = inlink->dst;
  872. AudioIIRContext *s = ctx->priv;
  873. AVFilterLink *outlink = ctx->outputs[0];
  874. ThreadData td;
  875. AVFrame *out;
  876. int ch, ret;
  877. if (av_frame_is_writable(in)) {
  878. out = in;
  879. } else {
  880. out = ff_get_audio_buffer(outlink, in->nb_samples);
  881. if (!out) {
  882. av_frame_free(&in);
  883. return AVERROR(ENOMEM);
  884. }
  885. av_frame_copy_props(out, in);
  886. }
  887. td.in = in;
  888. td.out = out;
  889. ctx->internal->execute(ctx, s->iir_channel, &td, NULL, outlink->channels);
  890. for (ch = 0; ch < outlink->channels; ch++) {
  891. if (s->iir[ch].clippings > 0)
  892. av_log(ctx, AV_LOG_WARNING, "Channel %d clipping %d times. Please reduce gain.\n",
  893. ch, s->iir[ch].clippings);
  894. s->iir[ch].clippings = 0;
  895. }
  896. if (in != out)
  897. av_frame_free(&in);
  898. if (s->response) {
  899. AVFilterLink *outlink = ctx->outputs[1];
  900. int64_t old_pts = s->video->pts;
  901. int64_t new_pts = av_rescale_q(out->pts, ctx->inputs[0]->time_base, outlink->time_base);
  902. if (new_pts > old_pts) {
  903. AVFrame *clone;
  904. s->video->pts = new_pts;
  905. clone = av_frame_clone(s->video);
  906. if (!clone)
  907. return AVERROR(ENOMEM);
  908. ret = ff_filter_frame(outlink, clone);
  909. if (ret < 0)
  910. return ret;
  911. }
  912. }
  913. return ff_filter_frame(outlink, out);
  914. }
  915. static int config_video(AVFilterLink *outlink)
  916. {
  917. AVFilterContext *ctx = outlink->src;
  918. AudioIIRContext *s = ctx->priv;
  919. outlink->sample_aspect_ratio = (AVRational){1,1};
  920. outlink->w = s->w;
  921. outlink->h = s->h;
  922. outlink->frame_rate = s->rate;
  923. outlink->time_base = av_inv_q(outlink->frame_rate);
  924. return 0;
  925. }
  926. static av_cold int init(AVFilterContext *ctx)
  927. {
  928. AudioIIRContext *s = ctx->priv;
  929. AVFilterPad pad, vpad;
  930. int ret;
  931. if (!s->a_str || !s->b_str || !s->g_str) {
  932. av_log(ctx, AV_LOG_ERROR, "Valid coefficients are mandatory.\n");
  933. return AVERROR(EINVAL);
  934. }
  935. switch (s->precision) {
  936. case 0: s->sample_format = AV_SAMPLE_FMT_DBLP; break;
  937. case 1: s->sample_format = AV_SAMPLE_FMT_FLTP; break;
  938. case 2: s->sample_format = AV_SAMPLE_FMT_S32P; break;
  939. case 3: s->sample_format = AV_SAMPLE_FMT_S16P; break;
  940. default: return AVERROR_BUG;
  941. }
  942. pad = (AVFilterPad){
  943. .name = av_strdup("default"),
  944. .type = AVMEDIA_TYPE_AUDIO,
  945. .config_props = config_output,
  946. };
  947. if (!pad.name)
  948. return AVERROR(ENOMEM);
  949. if (s->response) {
  950. vpad = (AVFilterPad){
  951. .name = av_strdup("filter_response"),
  952. .type = AVMEDIA_TYPE_VIDEO,
  953. .config_props = config_video,
  954. };
  955. if (!vpad.name)
  956. return AVERROR(ENOMEM);
  957. }
  958. ret = ff_insert_outpad(ctx, 0, &pad);
  959. if (ret < 0)
  960. return ret;
  961. if (s->response) {
  962. ret = ff_insert_outpad(ctx, 1, &vpad);
  963. if (ret < 0)
  964. return ret;
  965. }
  966. return 0;
  967. }
  968. static av_cold void uninit(AVFilterContext *ctx)
  969. {
  970. AudioIIRContext *s = ctx->priv;
  971. int ch;
  972. if (s->iir) {
  973. for (ch = 0; ch < s->channels; ch++) {
  974. IIRChannel *iir = &s->iir[ch];
  975. av_freep(&iir->ab[0]);
  976. av_freep(&iir->ab[1]);
  977. av_freep(&iir->cache[0]);
  978. av_freep(&iir->cache[1]);
  979. av_freep(&iir->biquads);
  980. }
  981. }
  982. av_freep(&s->iir);
  983. av_freep(&ctx->output_pads[0].name);
  984. if (s->response)
  985. av_freep(&ctx->output_pads[1].name);
  986. av_frame_free(&s->video);
  987. }
  988. static const AVFilterPad inputs[] = {
  989. {
  990. .name = "default",
  991. .type = AVMEDIA_TYPE_AUDIO,
  992. .filter_frame = filter_frame,
  993. },
  994. { NULL }
  995. };
  996. #define OFFSET(x) offsetof(AudioIIRContext, x)
  997. #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  998. #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  999. static const AVOption aiir_options[] = {
  1000. { "zeros", "set B/numerator/zeros coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  1001. { "z", "set B/numerator/zeros coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  1002. { "poles", "set A/denominator/poles coefficients", OFFSET(a_str),AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  1003. { "p", "set A/denominator/poles coefficients", OFFSET(a_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  1004. { "gains", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
  1005. { "k", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
  1006. { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
  1007. { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
  1008. { "format", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, 0, 3, AF, "format" },
  1009. { "f", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, 0, 3, AF, "format" },
  1010. { "tf", "transfer function", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "format" },
  1011. { "zp", "Z-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "format" },
  1012. { "pr", "Z-plane zeros/poles (polar radians)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "format" },
  1013. { "pd", "Z-plane zeros/poles (polar degrees)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "format" },
  1014. { "process", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" },
  1015. { "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" },
  1016. { "d", "direct", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "process" },
  1017. { "s", "serial cascading", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "process" },
  1018. { "precision", "set filtering precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
  1019. { "e", "set precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
  1020. { "dbl", "double-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" },
  1021. { "flt", "single-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "precision" },
  1022. { "i32", "32-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "precision" },
  1023. { "i16", "16-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "precision" },
  1024. { "normalize", "normalize coefficients", OFFSET(normalize),AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
  1025. { "n", "normalize coefficients", OFFSET(normalize),AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
  1026. { "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
  1027. { "response", "show IR frequency response", OFFSET(response), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
  1028. { "channel", "set IR channel to display frequency response", OFFSET(ir_channel), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, VF },
  1029. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, VF },
  1030. { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT32_MAX, VF },
  1031. { NULL },
  1032. };
  1033. AVFILTER_DEFINE_CLASS(aiir);
  1034. AVFilter ff_af_aiir = {
  1035. .name = "aiir",
  1036. .description = NULL_IF_CONFIG_SMALL("Apply Infinite Impulse Response filter with supplied coefficients."),
  1037. .priv_size = sizeof(AudioIIRContext),
  1038. .priv_class = &aiir_class,
  1039. .init = init,
  1040. .uninit = uninit,
  1041. .query_formats = query_formats,
  1042. .inputs = inputs,
  1043. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
  1044. AVFILTER_FLAG_SLICE_THREADS,
  1045. };