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.

850 lines
30KB

  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/opt.h"
  24. #include "audio.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. typedef struct ThreadData {
  28. AVFrame *in, *out;
  29. } ThreadData;
  30. typedef struct Pair {
  31. int a, b;
  32. } Pair;
  33. typedef struct BiquadContext {
  34. double a0, a1, a2;
  35. double b0, b1, b2;
  36. double i1, i2;
  37. double o1, o2;
  38. } BiquadContext;
  39. typedef struct IIRChannel {
  40. int nb_ab[2];
  41. double *ab[2];
  42. double g;
  43. double *cache[2];
  44. BiquadContext *biquads;
  45. int clippings;
  46. } IIRChannel;
  47. typedef struct AudioIIRContext {
  48. const AVClass *class;
  49. char *a_str, *b_str, *g_str;
  50. double dry_gain, wet_gain;
  51. int format;
  52. int process;
  53. int precision;
  54. IIRChannel *iir;
  55. int channels;
  56. enum AVSampleFormat sample_format;
  57. int (*iir_channel)(AVFilterContext *ctx, void *arg, int ch, int nb_jobs);
  58. } AudioIIRContext;
  59. static int query_formats(AVFilterContext *ctx)
  60. {
  61. AudioIIRContext *s = ctx->priv;
  62. AVFilterFormats *formats;
  63. AVFilterChannelLayouts *layouts;
  64. enum AVSampleFormat sample_fmts[] = {
  65. AV_SAMPLE_FMT_DBLP,
  66. AV_SAMPLE_FMT_NONE
  67. };
  68. int ret;
  69. layouts = ff_all_channel_counts();
  70. if (!layouts)
  71. return AVERROR(ENOMEM);
  72. ret = ff_set_common_channel_layouts(ctx, layouts);
  73. if (ret < 0)
  74. return ret;
  75. sample_fmts[0] = s->sample_format;
  76. formats = ff_make_format_list(sample_fmts);
  77. if (!formats)
  78. return AVERROR(ENOMEM);
  79. ret = ff_set_common_formats(ctx, formats);
  80. if (ret < 0)
  81. return ret;
  82. formats = ff_all_samplerates();
  83. if (!formats)
  84. return AVERROR(ENOMEM);
  85. return ff_set_common_samplerates(ctx, formats);
  86. }
  87. #define IIR_CH(name, type, min, max, need_clipping) \
  88. static int iir_ch_## name(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) \
  89. { \
  90. AudioIIRContext *s = ctx->priv; \
  91. const double ig = s->dry_gain; \
  92. const double og = s->wet_gain; \
  93. ThreadData *td = arg; \
  94. AVFrame *in = td->in, *out = td->out; \
  95. const type *src = (const type *)in->extended_data[ch]; \
  96. double *ic = (double *)s->iir[ch].cache[0]; \
  97. double *oc = (double *)s->iir[ch].cache[1]; \
  98. const int nb_a = s->iir[ch].nb_ab[0]; \
  99. const int nb_b = s->iir[ch].nb_ab[1]; \
  100. const double *a = s->iir[ch].ab[0]; \
  101. const double *b = s->iir[ch].ab[1]; \
  102. int *clippings = &s->iir[ch].clippings; \
  103. type *dst = (type *)out->extended_data[ch]; \
  104. int n; \
  105. \
  106. for (n = 0; n < in->nb_samples; n++) { \
  107. double sample = 0.; \
  108. int x; \
  109. \
  110. memmove(&ic[1], &ic[0], (nb_b - 1) * sizeof(*ic)); \
  111. memmove(&oc[1], &oc[0], (nb_a - 1) * sizeof(*oc)); \
  112. ic[0] = src[n] * ig; \
  113. for (x = 0; x < nb_b; x++) \
  114. sample += b[x] * ic[x]; \
  115. \
  116. for (x = 1; x < nb_a; x++) \
  117. sample -= a[x] * oc[x]; \
  118. \
  119. oc[0] = sample; \
  120. sample *= og; \
  121. if (need_clipping && sample < min) { \
  122. (*clippings)++; \
  123. dst[n] = min; \
  124. } else if (need_clipping && sample > max) { \
  125. (*clippings)++; \
  126. dst[n] = max; \
  127. } else { \
  128. dst[n] = sample; \
  129. } \
  130. } \
  131. \
  132. return 0; \
  133. }
  134. IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
  135. IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
  136. IIR_CH(fltp, float, -1., 1., 0)
  137. IIR_CH(dblp, double, -1., 1., 0)
  138. #define SERIAL_IIR_CH(name, type, min, max, need_clipping) \
  139. static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) \
  140. { \
  141. AudioIIRContext *s = ctx->priv; \
  142. const double ig = s->dry_gain; \
  143. const double og = s->wet_gain; \
  144. ThreadData *td = arg; \
  145. AVFrame *in = td->in, *out = td->out; \
  146. const type *src = (const type *)in->extended_data[ch]; \
  147. type *dst = (type *)out->extended_data[ch]; \
  148. IIRChannel *iir = &s->iir[ch]; \
  149. int *clippings = &iir->clippings; \
  150. int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; \
  151. int n, i; \
  152. \
  153. for (i = 0; i < nb_biquads; i++) { \
  154. const double a1 = -iir->biquads[i].a1; \
  155. const double a2 = -iir->biquads[i].a2; \
  156. const double b0 = iir->biquads[i].b0; \
  157. const double b1 = iir->biquads[i].b1; \
  158. const double b2 = iir->biquads[i].b2; \
  159. double i1 = iir->biquads[i].i1; \
  160. double i2 = iir->biquads[i].i2; \
  161. double o1 = iir->biquads[i].o1; \
  162. double o2 = iir->biquads[i].o2; \
  163. \
  164. for (n = 0; n < in->nb_samples; n++) { \
  165. double sample = ig * (i ? dst[n] : src[n]); \
  166. double o0 = sample * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
  167. \
  168. i2 = i1; \
  169. i1 = src[n]; \
  170. o2 = o1; \
  171. o1 = o0; \
  172. o0 *= og; \
  173. \
  174. if (need_clipping && o0 < min) { \
  175. (*clippings)++; \
  176. dst[n] = min; \
  177. } else if (need_clipping && o0 > max) { \
  178. (*clippings)++; \
  179. dst[n] = max; \
  180. } else { \
  181. dst[n] = o0; \
  182. } \
  183. } \
  184. iir->biquads[i].i1 = i1; \
  185. iir->biquads[i].i2 = i2; \
  186. iir->biquads[i].o1 = o1; \
  187. iir->biquads[i].o2 = o2; \
  188. } \
  189. \
  190. return 0; \
  191. }
  192. SERIAL_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
  193. SERIAL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
  194. SERIAL_IIR_CH(fltp, float, -1., 1., 0)
  195. SERIAL_IIR_CH(dblp, double, -1., 1., 0)
  196. static void count_coefficients(char *item_str, int *nb_items)
  197. {
  198. char *p;
  199. if (!item_str)
  200. return;
  201. *nb_items = 1;
  202. for (p = item_str; *p && *p != '|'; p++) {
  203. if (*p == ' ')
  204. (*nb_items)++;
  205. }
  206. }
  207. static int read_gains(AVFilterContext *ctx, char *item_str, int nb_items)
  208. {
  209. AudioIIRContext *s = ctx->priv;
  210. char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
  211. int i;
  212. p = old_str = av_strdup(item_str);
  213. if (!p)
  214. return AVERROR(ENOMEM);
  215. for (i = 0; i < nb_items; i++) {
  216. if (!(arg = av_strtok(p, "|", &saveptr)))
  217. arg = prev_arg;
  218. if (!arg) {
  219. av_freep(&old_str);
  220. return AVERROR(EINVAL);
  221. }
  222. p = NULL;
  223. if (sscanf(arg, "%lf", &s->iir[i].g) != 1) {
  224. av_log(ctx, AV_LOG_ERROR, "Invalid gains supplied: %s\n", arg);
  225. av_freep(&old_str);
  226. return AVERROR(EINVAL);
  227. }
  228. prev_arg = arg;
  229. }
  230. av_freep(&old_str);
  231. return 0;
  232. }
  233. static int read_tf_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst)
  234. {
  235. char *p, *arg, *old_str, *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. break;
  243. p = NULL;
  244. if (sscanf(arg, "%lf", &dst[i]) != 1) {
  245. av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
  246. av_freep(&old_str);
  247. return AVERROR(EINVAL);
  248. }
  249. }
  250. av_freep(&old_str);
  251. return 0;
  252. }
  253. static int read_zp_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst, const char *format)
  254. {
  255. char *p, *arg, *old_str, *saveptr = NULL;
  256. int i;
  257. p = old_str = av_strdup(item_str);
  258. if (!p)
  259. return AVERROR(ENOMEM);
  260. for (i = 0; i < nb_items; i++) {
  261. if (!(arg = av_strtok(p, " ", &saveptr)))
  262. break;
  263. p = NULL;
  264. if (sscanf(arg, format, &dst[i*2], &dst[i*2+1]) != 2) {
  265. av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
  266. av_freep(&old_str);
  267. return AVERROR(EINVAL);
  268. }
  269. }
  270. av_freep(&old_str);
  271. return 0;
  272. }
  273. static const char *format[] = { "%lf", "%lf %lfi", "%lf %lfr", "%lf %lfd" };
  274. static int read_channels(AVFilterContext *ctx, int channels, uint8_t *item_str, int ab)
  275. {
  276. AudioIIRContext *s = ctx->priv;
  277. char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
  278. int i, ret;
  279. p = old_str = av_strdup(item_str);
  280. if (!p)
  281. return AVERROR(ENOMEM);
  282. for (i = 0; i < channels; i++) {
  283. IIRChannel *iir = &s->iir[i];
  284. if (!(arg = av_strtok(p, "|", &saveptr)))
  285. arg = prev_arg;
  286. if (!arg) {
  287. av_freep(&old_str);
  288. return AVERROR(EINVAL);
  289. }
  290. count_coefficients(arg, &iir->nb_ab[ab]);
  291. p = NULL;
  292. iir->cache[ab] = av_calloc(iir->nb_ab[ab] + 1, sizeof(double));
  293. iir->ab[ab] = av_calloc(iir->nb_ab[ab] * (!!s->format + 1), sizeof(double));
  294. if (!iir->ab[ab] || !iir->cache[ab]) {
  295. av_freep(&old_str);
  296. return AVERROR(ENOMEM);
  297. }
  298. if (s->format) {
  299. ret = read_zp_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab], format[s->format]);
  300. } else {
  301. ret = read_tf_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab]);
  302. }
  303. if (ret < 0) {
  304. av_freep(&old_str);
  305. return ret;
  306. }
  307. prev_arg = arg;
  308. }
  309. av_freep(&old_str);
  310. return 0;
  311. }
  312. static void multiply(double wre, double wim, int npz, double *coeffs)
  313. {
  314. double nwre = -wre, nwim = -wim;
  315. double cre, cim;
  316. int i;
  317. for (i = npz; i >= 1; i--) {
  318. cre = coeffs[2 * i + 0];
  319. cim = coeffs[2 * i + 1];
  320. coeffs[2 * i + 0] = (nwre * cre - nwim * cim) + coeffs[2 * (i - 1) + 0];
  321. coeffs[2 * i + 1] = (nwre * cim + nwim * cre) + coeffs[2 * (i - 1) + 1];
  322. }
  323. cre = coeffs[0];
  324. cim = coeffs[1];
  325. coeffs[0] = nwre * cre - nwim * cim;
  326. coeffs[1] = nwre * cim + nwim * cre;
  327. }
  328. static int expand(AVFilterContext *ctx, double *pz, int nb, double *coeffs)
  329. {
  330. int i;
  331. coeffs[0] = 1.0;
  332. coeffs[1] = 0.0;
  333. for (i = 0; i < nb; i++) {
  334. coeffs[2 * (i + 1) ] = 0.0;
  335. coeffs[2 * (i + 1) + 1] = 0.0;
  336. }
  337. for (i = 0; i < nb; i++)
  338. multiply(pz[2 * i], pz[2 * i + 1], nb, coeffs);
  339. for (i = 0; i < nb + 1; i++) {
  340. if (fabs(coeffs[2 * i + 1]) > FLT_EPSILON) {
  341. av_log(ctx, AV_LOG_ERROR, "coeff: %lf of z^%d is not real; poles/zeros are not complex conjugates.\n",
  342. coeffs[2 * i + 1], i);
  343. return AVERROR(EINVAL);
  344. }
  345. }
  346. return 0;
  347. }
  348. static int convert_zp2tf(AVFilterContext *ctx, int channels)
  349. {
  350. AudioIIRContext *s = ctx->priv;
  351. int ch, i, j, ret = 0;
  352. for (ch = 0; ch < channels; ch++) {
  353. IIRChannel *iir = &s->iir[ch];
  354. double *topc, *botc;
  355. topc = av_calloc((iir->nb_ab[0] + 1) * 2, sizeof(*topc));
  356. botc = av_calloc((iir->nb_ab[1] + 1) * 2, sizeof(*botc));
  357. if (!topc || !botc) {
  358. ret = AVERROR(ENOMEM);
  359. goto fail;
  360. }
  361. ret = expand(ctx, iir->ab[0], iir->nb_ab[0], botc);
  362. if (ret < 0) {
  363. goto fail;
  364. }
  365. ret = expand(ctx, iir->ab[1], iir->nb_ab[1], topc);
  366. if (ret < 0) {
  367. goto fail;
  368. }
  369. for (j = 0, i = iir->nb_ab[1]; i >= 0; j++, i--) {
  370. iir->ab[1][j] = topc[2 * i];
  371. }
  372. iir->nb_ab[1]++;
  373. for (j = 0, i = iir->nb_ab[0]; i >= 0; j++, i--) {
  374. iir->ab[0][j] = botc[2 * i];
  375. }
  376. iir->nb_ab[0]++;
  377. fail:
  378. av_free(topc);
  379. av_free(botc);
  380. if (ret < 0)
  381. break;
  382. }
  383. return ret;
  384. }
  385. static int decompose_zp2biquads(AVFilterContext *ctx, int channels)
  386. {
  387. AudioIIRContext *s = ctx->priv;
  388. int ch, ret;
  389. for (ch = 0; ch < channels; ch++) {
  390. IIRChannel *iir = &s->iir[ch];
  391. int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2;
  392. int current_biquad = 0;
  393. iir->biquads = av_calloc(nb_biquads, sizeof(BiquadContext));
  394. if (!iir->biquads)
  395. return AVERROR(ENOMEM);
  396. while (nb_biquads--) {
  397. Pair outmost_pole = { -1, -1 };
  398. Pair nearest_zero = { -1, -1 };
  399. double zeros[4] = { 0 };
  400. double poles[4] = { 0 };
  401. double b[6] = { 0 };
  402. double a[6] = { 0 };
  403. double min_distance = DBL_MAX;
  404. double max_mag = 0;
  405. int i;
  406. for (i = 0; i < iir->nb_ab[0]; i++) {
  407. double mag;
  408. if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
  409. continue;
  410. mag = hypot(iir->ab[0][2 * i], iir->ab[0][2 * i + 1]);
  411. if (mag > max_mag) {
  412. max_mag = mag;
  413. outmost_pole.a = i;
  414. }
  415. }
  416. for (i = 0; i < iir->nb_ab[1]; i++) {
  417. if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
  418. continue;
  419. if (iir->ab[0][2 * i ] == iir->ab[0][2 * outmost_pole.a ] &&
  420. iir->ab[0][2 * i + 1] == -iir->ab[0][2 * outmost_pole.a + 1]) {
  421. outmost_pole.b = i;
  422. break;
  423. }
  424. }
  425. av_log(ctx, AV_LOG_VERBOSE, "outmost_pole is %d.%d\n", outmost_pole.a, outmost_pole.b);
  426. if (outmost_pole.a < 0 || outmost_pole.b < 0)
  427. return AVERROR(EINVAL);
  428. for (i = 0; i < iir->nb_ab[1]; i++) {
  429. double distance;
  430. if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
  431. continue;
  432. distance = hypot(iir->ab[0][2 * outmost_pole.a ] - iir->ab[1][2 * i ],
  433. iir->ab[0][2 * outmost_pole.a + 1] - iir->ab[1][2 * i + 1]);
  434. if (distance < min_distance) {
  435. min_distance = distance;
  436. nearest_zero.a = i;
  437. }
  438. }
  439. for (i = 0; i < iir->nb_ab[1]; i++) {
  440. if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
  441. continue;
  442. if (iir->ab[1][2 * i ] == iir->ab[1][2 * nearest_zero.a ] &&
  443. iir->ab[1][2 * i + 1] == -iir->ab[1][2 * nearest_zero.a + 1]) {
  444. nearest_zero.b = i;
  445. break;
  446. }
  447. }
  448. av_log(ctx, AV_LOG_VERBOSE, "nearest_zero is %d.%d\n", nearest_zero.a, nearest_zero.b);
  449. if (nearest_zero.a < 0 || nearest_zero.b < 0)
  450. return AVERROR(EINVAL);
  451. poles[0] = iir->ab[0][2 * outmost_pole.a ];
  452. poles[1] = iir->ab[0][2 * outmost_pole.a + 1];
  453. zeros[0] = iir->ab[1][2 * nearest_zero.a ];
  454. zeros[1] = iir->ab[1][2 * nearest_zero.a + 1];
  455. if (nearest_zero.a == nearest_zero.b && outmost_pole.a == outmost_pole.b) {
  456. zeros[2] = 0;
  457. zeros[3] = 0;
  458. poles[2] = 0;
  459. poles[3] = 0;
  460. } else {
  461. poles[2] = iir->ab[0][2 * outmost_pole.b ];
  462. poles[3] = iir->ab[0][2 * outmost_pole.b + 1];
  463. zeros[2] = iir->ab[1][2 * nearest_zero.b ];
  464. zeros[3] = iir->ab[1][2 * nearest_zero.b + 1];
  465. }
  466. ret = expand(ctx, zeros, 2, b);
  467. if (ret < 0)
  468. return ret;
  469. ret = expand(ctx, poles, 2, a);
  470. if (ret < 0)
  471. return ret;
  472. iir->ab[0][2 * outmost_pole.a] = iir->ab[0][2 * outmost_pole.a + 1] = NAN;
  473. iir->ab[0][2 * outmost_pole.b] = iir->ab[0][2 * outmost_pole.b + 1] = NAN;
  474. iir->ab[1][2 * nearest_zero.a] = iir->ab[1][2 * nearest_zero.a + 1] = NAN;
  475. iir->ab[1][2 * nearest_zero.b] = iir->ab[1][2 * nearest_zero.b + 1] = NAN;
  476. iir->biquads[current_biquad].a0 = 1.0;
  477. iir->biquads[current_biquad].a1 = a[2] / a[4];
  478. iir->biquads[current_biquad].a2 = a[0] / a[4];
  479. iir->biquads[current_biquad].b0 = b[4] / a[4] * (current_biquad ? 1.0 : iir->g);
  480. iir->biquads[current_biquad].b1 = b[2] / a[4] * (current_biquad ? 1.0 : iir->g);
  481. iir->biquads[current_biquad].b2 = b[0] / a[4] * (current_biquad ? 1.0 : iir->g);
  482. av_log(ctx, AV_LOG_VERBOSE, "a=%lf %lf %lf:b=%lf %lf %lf\n",
  483. iir->biquads[current_biquad].a0,
  484. iir->biquads[current_biquad].a1,
  485. iir->biquads[current_biquad].a2,
  486. iir->biquads[current_biquad].b0,
  487. iir->biquads[current_biquad].b1,
  488. iir->biquads[current_biquad].b2);
  489. current_biquad++;
  490. }
  491. }
  492. return 0;
  493. }
  494. static void convert_pr2zp(AVFilterContext *ctx, int channels)
  495. {
  496. AudioIIRContext *s = ctx->priv;
  497. int ch;
  498. for (ch = 0; ch < channels; ch++) {
  499. IIRChannel *iir = &s->iir[ch];
  500. int n;
  501. for (n = 0; n < iir->nb_ab[0]; n++) {
  502. double r = iir->ab[0][2*n];
  503. double angle = iir->ab[0][2*n+1];
  504. iir->ab[0][2*n] = r * cos(angle);
  505. iir->ab[0][2*n+1] = r * sin(angle);
  506. }
  507. for (n = 0; n < iir->nb_ab[1]; n++) {
  508. double r = iir->ab[1][2*n];
  509. double angle = iir->ab[1][2*n+1];
  510. iir->ab[1][2*n] = r * cos(angle);
  511. iir->ab[1][2*n+1] = r * sin(angle);
  512. }
  513. }
  514. }
  515. static void convert_pd2zp(AVFilterContext *ctx, int channels)
  516. {
  517. AudioIIRContext *s = ctx->priv;
  518. int ch;
  519. for (ch = 0; ch < channels; ch++) {
  520. IIRChannel *iir = &s->iir[ch];
  521. int n;
  522. for (n = 0; n < iir->nb_ab[0]; n++) {
  523. double r = iir->ab[0][2*n];
  524. double angle = M_PI*iir->ab[0][2*n+1]/180.;
  525. iir->ab[0][2*n] = r * cos(angle);
  526. iir->ab[0][2*n+1] = r * sin(angle);
  527. }
  528. for (n = 0; n < iir->nb_ab[1]; n++) {
  529. double r = iir->ab[1][2*n];
  530. double angle = M_PI*iir->ab[1][2*n+1]/180.;
  531. iir->ab[1][2*n] = r * cos(angle);
  532. iir->ab[1][2*n+1] = r * sin(angle);
  533. }
  534. }
  535. }
  536. static int config_output(AVFilterLink *outlink)
  537. {
  538. AVFilterContext *ctx = outlink->src;
  539. AudioIIRContext *s = ctx->priv;
  540. AVFilterLink *inlink = ctx->inputs[0];
  541. int ch, ret, i;
  542. s->channels = inlink->channels;
  543. s->iir = av_calloc(s->channels, sizeof(*s->iir));
  544. if (!s->iir)
  545. return AVERROR(ENOMEM);
  546. ret = read_gains(ctx, s->g_str, inlink->channels);
  547. if (ret < 0)
  548. return ret;
  549. ret = read_channels(ctx, inlink->channels, s->a_str, 0);
  550. if (ret < 0)
  551. return ret;
  552. ret = read_channels(ctx, inlink->channels, s->b_str, 1);
  553. if (ret < 0)
  554. return ret;
  555. if (s->format == 2) {
  556. convert_pr2zp(ctx, inlink->channels);
  557. } else if (s->format == 3) {
  558. convert_pd2zp(ctx, inlink->channels);
  559. }
  560. if (s->format == 0)
  561. av_log(ctx, AV_LOG_WARNING, "tf coefficients format is not recommended for too high number of zeros/poles.\n");
  562. if (s->format > 0 && s->process == 0) {
  563. av_log(ctx, AV_LOG_WARNING, "Direct processsing is not recommended for zp coefficients format.\n");
  564. ret = convert_zp2tf(ctx, inlink->channels);
  565. if (ret < 0)
  566. return ret;
  567. } else if (s->format == 0 && s->process == 1) {
  568. av_log(ctx, AV_LOG_ERROR, "Serial cascading is not implemented for transfer function.\n");
  569. return AVERROR_PATCHWELCOME;
  570. } else if (s->format > 0 && s->process == 1) {
  571. if (inlink->format == AV_SAMPLE_FMT_S16P)
  572. av_log(ctx, AV_LOG_WARNING, "Serial cascading is not recommended for i16 precision.\n");
  573. ret = decompose_zp2biquads(ctx, inlink->channels);
  574. if (ret < 0)
  575. return ret;
  576. }
  577. for (ch = 0; ch < inlink->channels; ch++) {
  578. IIRChannel *iir = &s->iir[ch];
  579. for (i = 1; i < iir->nb_ab[0]; i++) {
  580. iir->ab[0][i] /= iir->ab[0][0];
  581. }
  582. for (i = 0; i < iir->nb_ab[1]; i++) {
  583. iir->ab[1][i] *= iir->g / iir->ab[0][0];
  584. }
  585. }
  586. switch (inlink->format) {
  587. case AV_SAMPLE_FMT_DBLP: s->iir_channel = s->process == 1 ? iir_ch_serial_dblp : iir_ch_dblp; break;
  588. case AV_SAMPLE_FMT_FLTP: s->iir_channel = s->process == 1 ? iir_ch_serial_fltp : iir_ch_fltp; break;
  589. case AV_SAMPLE_FMT_S32P: s->iir_channel = s->process == 1 ? iir_ch_serial_s32p : iir_ch_s32p; break;
  590. case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break;
  591. }
  592. return 0;
  593. }
  594. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  595. {
  596. AVFilterContext *ctx = inlink->dst;
  597. AudioIIRContext *s = ctx->priv;
  598. AVFilterLink *outlink = ctx->outputs[0];
  599. ThreadData td;
  600. AVFrame *out;
  601. int ch;
  602. if (av_frame_is_writable(in)) {
  603. out = in;
  604. } else {
  605. out = ff_get_audio_buffer(outlink, in->nb_samples);
  606. if (!out) {
  607. av_frame_free(&in);
  608. return AVERROR(ENOMEM);
  609. }
  610. av_frame_copy_props(out, in);
  611. }
  612. td.in = in;
  613. td.out = out;
  614. ctx->internal->execute(ctx, s->iir_channel, &td, NULL, outlink->channels);
  615. for (ch = 0; ch < outlink->channels; ch++) {
  616. if (s->iir[ch].clippings > 0)
  617. av_log(ctx, AV_LOG_WARNING, "Channel %d clipping %d times. Please reduce gain.\n",
  618. ch, s->iir[ch].clippings);
  619. s->iir[ch].clippings = 0;
  620. }
  621. if (in != out)
  622. av_frame_free(&in);
  623. return ff_filter_frame(outlink, out);
  624. }
  625. static av_cold int init(AVFilterContext *ctx)
  626. {
  627. AudioIIRContext *s = ctx->priv;
  628. if (!s->a_str || !s->b_str || !s->g_str) {
  629. av_log(ctx, AV_LOG_ERROR, "Valid coefficients are mandatory.\n");
  630. return AVERROR(EINVAL);
  631. }
  632. switch (s->precision) {
  633. case 0: s->sample_format = AV_SAMPLE_FMT_DBLP; break;
  634. case 1: s->sample_format = AV_SAMPLE_FMT_FLTP; break;
  635. case 2: s->sample_format = AV_SAMPLE_FMT_S32P; break;
  636. case 3: s->sample_format = AV_SAMPLE_FMT_S16P; break;
  637. default: return AVERROR_BUG;
  638. }
  639. return 0;
  640. }
  641. static av_cold void uninit(AVFilterContext *ctx)
  642. {
  643. AudioIIRContext *s = ctx->priv;
  644. int ch;
  645. if (s->iir) {
  646. for (ch = 0; ch < s->channels; ch++) {
  647. IIRChannel *iir = &s->iir[ch];
  648. av_freep(&iir->ab[0]);
  649. av_freep(&iir->ab[1]);
  650. av_freep(&iir->cache[0]);
  651. av_freep(&iir->cache[1]);
  652. av_freep(&iir->biquads);
  653. }
  654. }
  655. av_freep(&s->iir);
  656. }
  657. static const AVFilterPad inputs[] = {
  658. {
  659. .name = "default",
  660. .type = AVMEDIA_TYPE_AUDIO,
  661. .filter_frame = filter_frame,
  662. },
  663. { NULL }
  664. };
  665. static const AVFilterPad outputs[] = {
  666. {
  667. .name = "default",
  668. .type = AVMEDIA_TYPE_AUDIO,
  669. .config_props = config_output,
  670. },
  671. { NULL }
  672. };
  673. #define OFFSET(x) offsetof(AudioIIRContext, x)
  674. #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  675. static const AVOption aiir_options[] = {
  676. { "z", "set B/numerator/zeros coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  677. { "p", "set A/denominator/poles coefficients", OFFSET(a_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  678. { "k", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
  679. { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
  680. { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
  681. { "f", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, 0, 3, AF, "format" },
  682. { "tf", "transfer function", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "format" },
  683. { "zp", "Z-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "format" },
  684. { "pr", "Z-plane zeros/poles (polar radians)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "format" },
  685. { "pd", "Z-plane zeros/poles (polar degrees)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "format" },
  686. { "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" },
  687. { "d", "direct", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "process" },
  688. { "s", "serial cascading", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "process" },
  689. { "e", "set precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
  690. { "dbl", "double-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" },
  691. { "flt", "single-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "precision" },
  692. { "i32", "32-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "precision" },
  693. { "i16", "16-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "precision" },
  694. { NULL },
  695. };
  696. AVFILTER_DEFINE_CLASS(aiir);
  697. AVFilter ff_af_aiir = {
  698. .name = "aiir",
  699. .description = NULL_IF_CONFIG_SMALL("Apply Infinite Impulse Response filter with supplied coefficients."),
  700. .priv_size = sizeof(AudioIIRContext),
  701. .priv_class = &aiir_class,
  702. .init = init,
  703. .uninit = uninit,
  704. .query_formats = query_formats,
  705. .inputs = inputs,
  706. .outputs = outputs,
  707. .flags = AVFILTER_FLAG_SLICE_THREADS,
  708. };