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.

1098 lines
37KB

  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 a0, a1, a2;
  37. double b0, b1, b2;
  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. int format;
  54. int process;
  55. int precision;
  56. int response;
  57. int w, h;
  58. int ir_channel;
  59. AVRational rate;
  60. AVFrame *video;
  61. IIRChannel *iir;
  62. int channels;
  63. enum AVSampleFormat sample_format;
  64. int (*iir_channel)(AVFilterContext *ctx, void *arg, int ch, int nb_jobs);
  65. } AudioIIRContext;
  66. static int query_formats(AVFilterContext *ctx)
  67. {
  68. AudioIIRContext *s = ctx->priv;
  69. AVFilterFormats *formats;
  70. AVFilterChannelLayouts *layouts;
  71. enum AVSampleFormat sample_fmts[] = {
  72. AV_SAMPLE_FMT_DBLP,
  73. AV_SAMPLE_FMT_NONE
  74. };
  75. static const enum AVPixelFormat pix_fmts[] = {
  76. AV_PIX_FMT_RGB0,
  77. AV_PIX_FMT_NONE
  78. };
  79. int ret;
  80. if (s->response) {
  81. AVFilterLink *videolink = ctx->outputs[1];
  82. formats = ff_make_format_list(pix_fmts);
  83. if ((ret = ff_formats_ref(formats, &videolink->in_formats)) < 0)
  84. return ret;
  85. }
  86. layouts = ff_all_channel_counts();
  87. if (!layouts)
  88. return AVERROR(ENOMEM);
  89. ret = ff_set_common_channel_layouts(ctx, layouts);
  90. if (ret < 0)
  91. return ret;
  92. sample_fmts[0] = s->sample_format;
  93. formats = ff_make_format_list(sample_fmts);
  94. if (!formats)
  95. return AVERROR(ENOMEM);
  96. ret = ff_set_common_formats(ctx, formats);
  97. if (ret < 0)
  98. return ret;
  99. formats = ff_all_samplerates();
  100. if (!formats)
  101. return AVERROR(ENOMEM);
  102. return ff_set_common_samplerates(ctx, formats);
  103. }
  104. #define IIR_CH(name, type, min, max, need_clipping) \
  105. static int iir_ch_## name(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) \
  106. { \
  107. AudioIIRContext *s = ctx->priv; \
  108. const double ig = s->dry_gain; \
  109. const double og = s->wet_gain; \
  110. ThreadData *td = arg; \
  111. AVFrame *in = td->in, *out = td->out; \
  112. const type *src = (const type *)in->extended_data[ch]; \
  113. double *ic = (double *)s->iir[ch].cache[0]; \
  114. double *oc = (double *)s->iir[ch].cache[1]; \
  115. const int nb_a = s->iir[ch].nb_ab[0]; \
  116. const int nb_b = s->iir[ch].nb_ab[1]; \
  117. const double *a = s->iir[ch].ab[0]; \
  118. const double *b = s->iir[ch].ab[1]; \
  119. int *clippings = &s->iir[ch].clippings; \
  120. type *dst = (type *)out->extended_data[ch]; \
  121. int n; \
  122. \
  123. for (n = 0; n < in->nb_samples; n++) { \
  124. double sample = 0.; \
  125. int x; \
  126. \
  127. memmove(&ic[1], &ic[0], (nb_b - 1) * sizeof(*ic)); \
  128. memmove(&oc[1], &oc[0], (nb_a - 1) * sizeof(*oc)); \
  129. ic[0] = src[n] * ig; \
  130. for (x = 0; x < nb_b; x++) \
  131. sample += b[x] * ic[x]; \
  132. \
  133. for (x = 1; x < nb_a; x++) \
  134. sample -= a[x] * oc[x]; \
  135. \
  136. oc[0] = sample; \
  137. sample *= og; \
  138. if (need_clipping && sample < min) { \
  139. (*clippings)++; \
  140. dst[n] = min; \
  141. } else if (need_clipping && sample > max) { \
  142. (*clippings)++; \
  143. dst[n] = max; \
  144. } else { \
  145. dst[n] = sample; \
  146. } \
  147. } \
  148. \
  149. return 0; \
  150. }
  151. IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
  152. IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
  153. IIR_CH(fltp, float, -1., 1., 0)
  154. IIR_CH(dblp, double, -1., 1., 0)
  155. #define SERIAL_IIR_CH(name, type, min, max, need_clipping) \
  156. static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) \
  157. { \
  158. AudioIIRContext *s = ctx->priv; \
  159. const double ig = s->dry_gain; \
  160. const double og = s->wet_gain; \
  161. ThreadData *td = arg; \
  162. AVFrame *in = td->in, *out = td->out; \
  163. const type *src = (const type *)in->extended_data[ch]; \
  164. type *dst = (type *)out->extended_data[ch]; \
  165. IIRChannel *iir = &s->iir[ch]; \
  166. int *clippings = &iir->clippings; \
  167. int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; \
  168. int n, i; \
  169. \
  170. for (i = 0; i < nb_biquads; i++) { \
  171. const double a1 = -iir->biquads[i].a1; \
  172. const double a2 = -iir->biquads[i].a2; \
  173. const double b0 = iir->biquads[i].b0; \
  174. const double b1 = iir->biquads[i].b1; \
  175. const double b2 = iir->biquads[i].b2; \
  176. double i1 = iir->biquads[i].i1; \
  177. double i2 = iir->biquads[i].i2; \
  178. double o1 = iir->biquads[i].o1; \
  179. double o2 = iir->biquads[i].o2; \
  180. \
  181. for (n = 0; n < in->nb_samples; n++) { \
  182. double sample = ig * (i ? dst[n] : src[n]); \
  183. double o0 = sample * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
  184. \
  185. i2 = i1; \
  186. i1 = src[n]; \
  187. o2 = o1; \
  188. o1 = o0; \
  189. o0 *= og; \
  190. \
  191. if (need_clipping && o0 < min) { \
  192. (*clippings)++; \
  193. dst[n] = min; \
  194. } else if (need_clipping && o0 > max) { \
  195. (*clippings)++; \
  196. dst[n] = max; \
  197. } else { \
  198. dst[n] = o0; \
  199. } \
  200. } \
  201. iir->biquads[i].i1 = i1; \
  202. iir->biquads[i].i2 = i2; \
  203. iir->biquads[i].o1 = o1; \
  204. iir->biquads[i].o2 = o2; \
  205. } \
  206. \
  207. return 0; \
  208. }
  209. SERIAL_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
  210. SERIAL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
  211. SERIAL_IIR_CH(fltp, float, -1., 1., 0)
  212. SERIAL_IIR_CH(dblp, double, -1., 1., 0)
  213. static void count_coefficients(char *item_str, int *nb_items)
  214. {
  215. char *p;
  216. if (!item_str)
  217. return;
  218. *nb_items = 1;
  219. for (p = item_str; *p && *p != '|'; p++) {
  220. if (*p == ' ')
  221. (*nb_items)++;
  222. }
  223. }
  224. static int read_gains(AVFilterContext *ctx, char *item_str, int nb_items)
  225. {
  226. AudioIIRContext *s = ctx->priv;
  227. char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
  228. int i;
  229. p = old_str = av_strdup(item_str);
  230. if (!p)
  231. return AVERROR(ENOMEM);
  232. for (i = 0; i < nb_items; i++) {
  233. if (!(arg = av_strtok(p, "|", &saveptr)))
  234. arg = prev_arg;
  235. if (!arg) {
  236. av_freep(&old_str);
  237. return AVERROR(EINVAL);
  238. }
  239. p = NULL;
  240. if (sscanf(arg, "%lf", &s->iir[i].g) != 1) {
  241. av_log(ctx, AV_LOG_ERROR, "Invalid gains supplied: %s\n", arg);
  242. av_freep(&old_str);
  243. return AVERROR(EINVAL);
  244. }
  245. prev_arg = arg;
  246. }
  247. av_freep(&old_str);
  248. return 0;
  249. }
  250. static int read_tf_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst)
  251. {
  252. char *p, *arg, *old_str, *saveptr = NULL;
  253. int i;
  254. p = old_str = av_strdup(item_str);
  255. if (!p)
  256. return AVERROR(ENOMEM);
  257. for (i = 0; i < nb_items; i++) {
  258. if (!(arg = av_strtok(p, " ", &saveptr)))
  259. break;
  260. p = NULL;
  261. if (sscanf(arg, "%lf", &dst[i]) != 1) {
  262. av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
  263. av_freep(&old_str);
  264. return AVERROR(EINVAL);
  265. }
  266. }
  267. av_freep(&old_str);
  268. return 0;
  269. }
  270. static int read_zp_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst, const char *format)
  271. {
  272. char *p, *arg, *old_str, *saveptr = NULL;
  273. int i;
  274. p = old_str = av_strdup(item_str);
  275. if (!p)
  276. return AVERROR(ENOMEM);
  277. for (i = 0; i < nb_items; i++) {
  278. if (!(arg = av_strtok(p, " ", &saveptr)))
  279. break;
  280. p = NULL;
  281. if (sscanf(arg, format, &dst[i*2], &dst[i*2+1]) != 2) {
  282. av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
  283. av_freep(&old_str);
  284. return AVERROR(EINVAL);
  285. }
  286. }
  287. av_freep(&old_str);
  288. return 0;
  289. }
  290. static const char *format[] = { "%lf", "%lf %lfi", "%lf %lfr", "%lf %lfd" };
  291. static int read_channels(AVFilterContext *ctx, int channels, uint8_t *item_str, int ab)
  292. {
  293. AudioIIRContext *s = ctx->priv;
  294. char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
  295. int i, ret;
  296. p = old_str = av_strdup(item_str);
  297. if (!p)
  298. return AVERROR(ENOMEM);
  299. for (i = 0; i < channels; i++) {
  300. IIRChannel *iir = &s->iir[i];
  301. if (!(arg = av_strtok(p, "|", &saveptr)))
  302. arg = prev_arg;
  303. if (!arg) {
  304. av_freep(&old_str);
  305. return AVERROR(EINVAL);
  306. }
  307. count_coefficients(arg, &iir->nb_ab[ab]);
  308. p = NULL;
  309. iir->cache[ab] = av_calloc(iir->nb_ab[ab] + 1, sizeof(double));
  310. iir->ab[ab] = av_calloc(iir->nb_ab[ab] * (!!s->format + 1), sizeof(double));
  311. if (!iir->ab[ab] || !iir->cache[ab]) {
  312. av_freep(&old_str);
  313. return AVERROR(ENOMEM);
  314. }
  315. if (s->format) {
  316. ret = read_zp_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab], format[s->format]);
  317. } else {
  318. ret = read_tf_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab]);
  319. }
  320. if (ret < 0) {
  321. av_freep(&old_str);
  322. return ret;
  323. }
  324. prev_arg = arg;
  325. }
  326. av_freep(&old_str);
  327. return 0;
  328. }
  329. static void multiply(double wre, double wim, int npz, double *coeffs)
  330. {
  331. double nwre = -wre, nwim = -wim;
  332. double cre, cim;
  333. int i;
  334. for (i = npz; i >= 1; i--) {
  335. cre = coeffs[2 * i + 0];
  336. cim = coeffs[2 * i + 1];
  337. coeffs[2 * i + 0] = (nwre * cre - nwim * cim) + coeffs[2 * (i - 1) + 0];
  338. coeffs[2 * i + 1] = (nwre * cim + nwim * cre) + coeffs[2 * (i - 1) + 1];
  339. }
  340. cre = coeffs[0];
  341. cim = coeffs[1];
  342. coeffs[0] = nwre * cre - nwim * cim;
  343. coeffs[1] = nwre * cim + nwim * cre;
  344. }
  345. static int expand(AVFilterContext *ctx, double *pz, int nb, double *coeffs)
  346. {
  347. int i;
  348. coeffs[0] = 1.0;
  349. coeffs[1] = 0.0;
  350. for (i = 0; i < nb; i++) {
  351. coeffs[2 * (i + 1) ] = 0.0;
  352. coeffs[2 * (i + 1) + 1] = 0.0;
  353. }
  354. for (i = 0; i < nb; i++)
  355. multiply(pz[2 * i], pz[2 * i + 1], nb, coeffs);
  356. for (i = 0; i < nb + 1; i++) {
  357. if (fabs(coeffs[2 * i + 1]) > FLT_EPSILON) {
  358. av_log(ctx, AV_LOG_ERROR, "coeff: %f of z^%d is not real; poles/zeros are not complex conjugates.\n",
  359. coeffs[2 * i + 1], i);
  360. return AVERROR(EINVAL);
  361. }
  362. }
  363. return 0;
  364. }
  365. static int convert_zp2tf(AVFilterContext *ctx, int channels)
  366. {
  367. AudioIIRContext *s = ctx->priv;
  368. int ch, i, j, ret = 0;
  369. for (ch = 0; ch < channels; ch++) {
  370. IIRChannel *iir = &s->iir[ch];
  371. double *topc, *botc;
  372. topc = av_calloc((iir->nb_ab[0] + 1) * 2, sizeof(*topc));
  373. botc = av_calloc((iir->nb_ab[1] + 1) * 2, sizeof(*botc));
  374. if (!topc || !botc) {
  375. ret = AVERROR(ENOMEM);
  376. goto fail;
  377. }
  378. ret = expand(ctx, iir->ab[0], iir->nb_ab[0], botc);
  379. if (ret < 0) {
  380. goto fail;
  381. }
  382. ret = expand(ctx, iir->ab[1], iir->nb_ab[1], topc);
  383. if (ret < 0) {
  384. goto fail;
  385. }
  386. for (j = 0, i = iir->nb_ab[1]; i >= 0; j++, i--) {
  387. iir->ab[1][j] = topc[2 * i];
  388. }
  389. iir->nb_ab[1]++;
  390. for (j = 0, i = iir->nb_ab[0]; i >= 0; j++, i--) {
  391. iir->ab[0][j] = botc[2 * i];
  392. }
  393. iir->nb_ab[0]++;
  394. fail:
  395. av_free(topc);
  396. av_free(botc);
  397. if (ret < 0)
  398. break;
  399. }
  400. return ret;
  401. }
  402. static int decompose_zp2biquads(AVFilterContext *ctx, int channels)
  403. {
  404. AudioIIRContext *s = ctx->priv;
  405. int ch, ret;
  406. for (ch = 0; ch < channels; ch++) {
  407. IIRChannel *iir = &s->iir[ch];
  408. int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2;
  409. int current_biquad = 0;
  410. iir->biquads = av_calloc(nb_biquads, sizeof(BiquadContext));
  411. if (!iir->biquads)
  412. return AVERROR(ENOMEM);
  413. while (nb_biquads--) {
  414. Pair outmost_pole = { -1, -1 };
  415. Pair nearest_zero = { -1, -1 };
  416. double zeros[4] = { 0 };
  417. double poles[4] = { 0 };
  418. double b[6] = { 0 };
  419. double a[6] = { 0 };
  420. double min_distance = DBL_MAX;
  421. double max_mag = 0;
  422. int i;
  423. for (i = 0; i < iir->nb_ab[0]; i++) {
  424. double mag;
  425. if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
  426. continue;
  427. mag = hypot(iir->ab[0][2 * i], iir->ab[0][2 * i + 1]);
  428. if (mag > max_mag) {
  429. max_mag = mag;
  430. outmost_pole.a = i;
  431. }
  432. }
  433. for (i = 0; i < iir->nb_ab[1]; i++) {
  434. if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
  435. continue;
  436. if (iir->ab[0][2 * i ] == iir->ab[0][2 * outmost_pole.a ] &&
  437. iir->ab[0][2 * i + 1] == -iir->ab[0][2 * outmost_pole.a + 1]) {
  438. outmost_pole.b = i;
  439. break;
  440. }
  441. }
  442. av_log(ctx, AV_LOG_VERBOSE, "outmost_pole is %d.%d\n", outmost_pole.a, outmost_pole.b);
  443. if (outmost_pole.a < 0 || outmost_pole.b < 0)
  444. return AVERROR(EINVAL);
  445. for (i = 0; i < iir->nb_ab[1]; i++) {
  446. double distance;
  447. if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
  448. continue;
  449. distance = hypot(iir->ab[0][2 * outmost_pole.a ] - iir->ab[1][2 * i ],
  450. iir->ab[0][2 * outmost_pole.a + 1] - iir->ab[1][2 * i + 1]);
  451. if (distance < min_distance) {
  452. min_distance = distance;
  453. nearest_zero.a = i;
  454. }
  455. }
  456. for (i = 0; i < iir->nb_ab[1]; i++) {
  457. if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
  458. continue;
  459. if (iir->ab[1][2 * i ] == iir->ab[1][2 * nearest_zero.a ] &&
  460. iir->ab[1][2 * i + 1] == -iir->ab[1][2 * nearest_zero.a + 1]) {
  461. nearest_zero.b = i;
  462. break;
  463. }
  464. }
  465. av_log(ctx, AV_LOG_VERBOSE, "nearest_zero is %d.%d\n", nearest_zero.a, nearest_zero.b);
  466. if (nearest_zero.a < 0 || nearest_zero.b < 0)
  467. return AVERROR(EINVAL);
  468. poles[0] = iir->ab[0][2 * outmost_pole.a ];
  469. poles[1] = iir->ab[0][2 * outmost_pole.a + 1];
  470. zeros[0] = iir->ab[1][2 * nearest_zero.a ];
  471. zeros[1] = iir->ab[1][2 * nearest_zero.a + 1];
  472. if (nearest_zero.a == nearest_zero.b && outmost_pole.a == outmost_pole.b) {
  473. zeros[2] = 0;
  474. zeros[3] = 0;
  475. poles[2] = 0;
  476. poles[3] = 0;
  477. } else {
  478. poles[2] = iir->ab[0][2 * outmost_pole.b ];
  479. poles[3] = iir->ab[0][2 * outmost_pole.b + 1];
  480. zeros[2] = iir->ab[1][2 * nearest_zero.b ];
  481. zeros[3] = iir->ab[1][2 * nearest_zero.b + 1];
  482. }
  483. ret = expand(ctx, zeros, 2, b);
  484. if (ret < 0)
  485. return ret;
  486. ret = expand(ctx, poles, 2, a);
  487. if (ret < 0)
  488. return ret;
  489. iir->ab[0][2 * outmost_pole.a] = iir->ab[0][2 * outmost_pole.a + 1] = NAN;
  490. iir->ab[0][2 * outmost_pole.b] = iir->ab[0][2 * outmost_pole.b + 1] = NAN;
  491. iir->ab[1][2 * nearest_zero.a] = iir->ab[1][2 * nearest_zero.a + 1] = NAN;
  492. iir->ab[1][2 * nearest_zero.b] = iir->ab[1][2 * nearest_zero.b + 1] = NAN;
  493. iir->biquads[current_biquad].a0 = 1.0;
  494. iir->biquads[current_biquad].a1 = a[2] / a[4];
  495. iir->biquads[current_biquad].a2 = a[0] / a[4];
  496. iir->biquads[current_biquad].b0 = b[4] / a[4] * (current_biquad ? 1.0 : iir->g);
  497. iir->biquads[current_biquad].b1 = b[2] / a[4] * (current_biquad ? 1.0 : iir->g);
  498. iir->biquads[current_biquad].b2 = b[0] / a[4] * (current_biquad ? 1.0 : iir->g);
  499. av_log(ctx, AV_LOG_VERBOSE, "a=%f %f %f:b=%f %f %f\n",
  500. iir->biquads[current_biquad].a0,
  501. iir->biquads[current_biquad].a1,
  502. iir->biquads[current_biquad].a2,
  503. iir->biquads[current_biquad].b0,
  504. iir->biquads[current_biquad].b1,
  505. iir->biquads[current_biquad].b2);
  506. current_biquad++;
  507. }
  508. }
  509. return 0;
  510. }
  511. static void convert_pr2zp(AVFilterContext *ctx, int channels)
  512. {
  513. AudioIIRContext *s = ctx->priv;
  514. int ch;
  515. for (ch = 0; ch < channels; ch++) {
  516. IIRChannel *iir = &s->iir[ch];
  517. int n;
  518. for (n = 0; n < iir->nb_ab[0]; n++) {
  519. double r = iir->ab[0][2*n];
  520. double angle = iir->ab[0][2*n+1];
  521. iir->ab[0][2*n] = r * cos(angle);
  522. iir->ab[0][2*n+1] = r * sin(angle);
  523. }
  524. for (n = 0; n < iir->nb_ab[1]; n++) {
  525. double r = iir->ab[1][2*n];
  526. double angle = iir->ab[1][2*n+1];
  527. iir->ab[1][2*n] = r * cos(angle);
  528. iir->ab[1][2*n+1] = r * sin(angle);
  529. }
  530. }
  531. }
  532. static void convert_pd2zp(AVFilterContext *ctx, int channels)
  533. {
  534. AudioIIRContext *s = ctx->priv;
  535. int ch;
  536. for (ch = 0; ch < channels; ch++) {
  537. IIRChannel *iir = &s->iir[ch];
  538. int n;
  539. for (n = 0; n < iir->nb_ab[0]; n++) {
  540. double r = iir->ab[0][2*n];
  541. double angle = M_PI*iir->ab[0][2*n+1]/180.;
  542. iir->ab[0][2*n] = r * cos(angle);
  543. iir->ab[0][2*n+1] = r * sin(angle);
  544. }
  545. for (n = 0; n < iir->nb_ab[1]; n++) {
  546. double r = iir->ab[1][2*n];
  547. double angle = M_PI*iir->ab[1][2*n+1]/180.;
  548. iir->ab[1][2*n] = r * cos(angle);
  549. iir->ab[1][2*n+1] = r * sin(angle);
  550. }
  551. }
  552. }
  553. static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint32_t color)
  554. {
  555. const uint8_t *font;
  556. int font_height;
  557. int i;
  558. font = avpriv_cga_font, font_height = 8;
  559. for (i = 0; txt[i]; i++) {
  560. int char_y, mask;
  561. uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
  562. for (char_y = 0; char_y < font_height; char_y++) {
  563. for (mask = 0x80; mask; mask >>= 1) {
  564. if (font[txt[i] * font_height + char_y] & mask)
  565. AV_WL32(p, color);
  566. p += 4;
  567. }
  568. p += pic->linesize[0] - 8 * 4;
  569. }
  570. }
  571. }
  572. static void draw_line(AVFrame *out, int x0, int y0, int x1, int y1, uint32_t color)
  573. {
  574. int dx = FFABS(x1-x0);
  575. int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
  576. int err = (dx>dy ? dx : -dy) / 2, e2;
  577. for (;;) {
  578. AV_WL32(out->data[0] + y0 * out->linesize[0] + x0 * 4, color);
  579. if (x0 == x1 && y0 == y1)
  580. break;
  581. e2 = err;
  582. if (e2 >-dx) {
  583. err -= dy;
  584. x0--;
  585. }
  586. if (e2 < dy) {
  587. err += dx;
  588. y0 += sy;
  589. }
  590. }
  591. }
  592. static void draw_response(AVFilterContext *ctx, AVFrame *out)
  593. {
  594. AudioIIRContext *s = ctx->priv;
  595. float *mag, *phase, min = FLT_MAX, max = FLT_MIN;
  596. int prev_ymag = -1, prev_yphase = -1;
  597. char text[32];
  598. int ch, i, x;
  599. memset(out->data[0], 0, s->h * out->linesize[0]);
  600. phase = av_malloc_array(s->w, sizeof(*phase));
  601. mag = av_malloc_array(s->w, sizeof(*mag));
  602. if (!mag || !phase)
  603. goto end;
  604. ch = av_clip(s->ir_channel, 0, s->channels - 1);
  605. for (i = 0; i < s->w; i++) {
  606. const double *b = s->iir[ch].ab[0];
  607. const double *a = s->iir[ch].ab[1];
  608. double w = i * M_PI / (s->w - 1);
  609. double realz, realp;
  610. double imagz, imagp;
  611. double real, imag, div;
  612. if (s->format == 0) {
  613. realz = 0., realp = 0.;
  614. imagz = 0., imagp = 0.;
  615. for (x = 0; x < s->iir[ch].nb_ab[1]; x++) {
  616. realz += cos(-x * w) * a[x];
  617. imagz += sin(-x * w) * a[x];
  618. }
  619. for (x = 0; x < s->iir[ch].nb_ab[0]; x++) {
  620. realp += cos(-x * w) * b[x];
  621. imagp += sin(-x * w) * b[x];
  622. }
  623. div = realp * realp + imagp * imagp;
  624. real = (realz * realp + imagz * imagp) / div;
  625. imag = (imagz * realp - imagp * realz) / div;
  626. } else {
  627. real = 1;
  628. imag = 0;
  629. for (x = 0; x < s->iir[ch].nb_ab[1]; x++) {
  630. double ore, oim, re, im;
  631. re = cos(w) - a[2 * x];
  632. im = sin(w) - a[2 * x + 1];
  633. ore = real;
  634. oim = imag;
  635. real = ore * re - oim * im;
  636. imag = ore * im + oim * re;
  637. }
  638. for (x = 0; x < s->iir[ch].nb_ab[0]; x++) {
  639. double ore, oim, re, im;
  640. re = cos(w) - b[2 * x];
  641. im = sin(w) - b[2 * x + 1];
  642. ore = real;
  643. oim = imag;
  644. div = re * re + im * im;
  645. real = (ore * re + oim * im) / div;
  646. imag = (oim * re - ore * im) / div;
  647. }
  648. }
  649. mag[i] = s->iir[ch].g * hypot(real, imag);
  650. phase[i] = atan2(imag, real);
  651. min = fminf(min, mag[i]);
  652. max = fmaxf(max, mag[i]);
  653. }
  654. for (i = 0; i < s->w; i++) {
  655. int ymag = mag[i] / max * (s->h - 1);
  656. int yphase = (0.5 * (1. + phase[i] / M_PI)) * (s->h - 1);
  657. ymag = s->h - 1 - av_clip(ymag, 0, s->h - 1);
  658. yphase = s->h - 1 - av_clip(yphase, 0, s->h - 1);
  659. if (prev_ymag < 0)
  660. prev_ymag = ymag;
  661. if (prev_yphase < 0)
  662. prev_yphase = yphase;
  663. draw_line(out, i, ymag, FFMAX(i - 1, 0), prev_ymag, 0xFFFF00FF);
  664. draw_line(out, i, yphase, FFMAX(i - 1, 0), prev_yphase, 0xFF00FF00);
  665. prev_ymag = ymag;
  666. prev_yphase = yphase;
  667. }
  668. if (s->w > 400 && s->h > 100) {
  669. drawtext(out, 2, 2, "Max Magnitude:", 0xDDDDDDDD);
  670. snprintf(text, sizeof(text), "%.2f", max);
  671. drawtext(out, 15 * 8 + 2, 2, text, 0xDDDDDDDD);
  672. drawtext(out, 2, 12, "Min Magnitude:", 0xDDDDDDDD);
  673. snprintf(text, sizeof(text), "%.2f", min);
  674. drawtext(out, 15 * 8 + 2, 12, text, 0xDDDDDDDD);
  675. }
  676. end:
  677. av_free(phase);
  678. av_free(mag);
  679. }
  680. static int config_output(AVFilterLink *outlink)
  681. {
  682. AVFilterContext *ctx = outlink->src;
  683. AudioIIRContext *s = ctx->priv;
  684. AVFilterLink *inlink = ctx->inputs[0];
  685. int ch, ret, i;
  686. s->channels = inlink->channels;
  687. s->iir = av_calloc(s->channels, sizeof(*s->iir));
  688. if (!s->iir)
  689. return AVERROR(ENOMEM);
  690. ret = read_gains(ctx, s->g_str, inlink->channels);
  691. if (ret < 0)
  692. return ret;
  693. ret = read_channels(ctx, inlink->channels, s->a_str, 0);
  694. if (ret < 0)
  695. return ret;
  696. ret = read_channels(ctx, inlink->channels, s->b_str, 1);
  697. if (ret < 0)
  698. return ret;
  699. if (s->format == 2) {
  700. convert_pr2zp(ctx, inlink->channels);
  701. } else if (s->format == 3) {
  702. convert_pd2zp(ctx, inlink->channels);
  703. }
  704. av_frame_free(&s->video);
  705. if (s->response) {
  706. s->video = ff_get_video_buffer(ctx->outputs[1], s->w, s->h);
  707. if (!s->video)
  708. return AVERROR(ENOMEM);
  709. draw_response(ctx, s->video);
  710. }
  711. if (s->format == 0)
  712. av_log(ctx, AV_LOG_WARNING, "tf coefficients format is not recommended for too high number of zeros/poles.\n");
  713. if (s->format > 0 && s->process == 0) {
  714. av_log(ctx, AV_LOG_WARNING, "Direct processsing is not recommended for zp coefficients format.\n");
  715. ret = convert_zp2tf(ctx, inlink->channels);
  716. if (ret < 0)
  717. return ret;
  718. } else if (s->format == 0 && s->process == 1) {
  719. av_log(ctx, AV_LOG_ERROR, "Serial cascading is not implemented for transfer function.\n");
  720. return AVERROR_PATCHWELCOME;
  721. } else if (s->format > 0 && s->process == 1) {
  722. if (inlink->format == AV_SAMPLE_FMT_S16P)
  723. av_log(ctx, AV_LOG_WARNING, "Serial cascading is not recommended for i16 precision.\n");
  724. ret = decompose_zp2biquads(ctx, inlink->channels);
  725. if (ret < 0)
  726. return ret;
  727. }
  728. for (ch = 0; s->format == 0 && ch < inlink->channels; ch++) {
  729. IIRChannel *iir = &s->iir[ch];
  730. for (i = 1; i < iir->nb_ab[0]; i++) {
  731. iir->ab[0][i] /= iir->ab[0][0];
  732. }
  733. for (i = 0; i < iir->nb_ab[1]; i++) {
  734. iir->ab[1][i] *= iir->g / iir->ab[0][0];
  735. }
  736. }
  737. switch (inlink->format) {
  738. case AV_SAMPLE_FMT_DBLP: s->iir_channel = s->process == 1 ? iir_ch_serial_dblp : iir_ch_dblp; break;
  739. case AV_SAMPLE_FMT_FLTP: s->iir_channel = s->process == 1 ? iir_ch_serial_fltp : iir_ch_fltp; break;
  740. case AV_SAMPLE_FMT_S32P: s->iir_channel = s->process == 1 ? iir_ch_serial_s32p : iir_ch_s32p; break;
  741. case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break;
  742. }
  743. return 0;
  744. }
  745. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  746. {
  747. AVFilterContext *ctx = inlink->dst;
  748. AudioIIRContext *s = ctx->priv;
  749. AVFilterLink *outlink = ctx->outputs[0];
  750. ThreadData td;
  751. AVFrame *out;
  752. int ch, ret;
  753. if (av_frame_is_writable(in)) {
  754. out = in;
  755. } else {
  756. out = ff_get_audio_buffer(outlink, in->nb_samples);
  757. if (!out) {
  758. av_frame_free(&in);
  759. return AVERROR(ENOMEM);
  760. }
  761. av_frame_copy_props(out, in);
  762. }
  763. td.in = in;
  764. td.out = out;
  765. ctx->internal->execute(ctx, s->iir_channel, &td, NULL, outlink->channels);
  766. for (ch = 0; ch < outlink->channels; ch++) {
  767. if (s->iir[ch].clippings > 0)
  768. av_log(ctx, AV_LOG_WARNING, "Channel %d clipping %d times. Please reduce gain.\n",
  769. ch, s->iir[ch].clippings);
  770. s->iir[ch].clippings = 0;
  771. }
  772. if (in != out)
  773. av_frame_free(&in);
  774. if (s->response) {
  775. AVFilterLink *outlink = ctx->outputs[1];
  776. int64_t old_pts = s->video->pts;
  777. int64_t new_pts = av_rescale_q(out->pts, ctx->inputs[0]->time_base, outlink->time_base);
  778. if (new_pts > old_pts) {
  779. s->video->pts = new_pts;
  780. ret = ff_filter_frame(outlink, av_frame_clone(s->video));
  781. if (ret < 0)
  782. return ret;
  783. }
  784. }
  785. return ff_filter_frame(outlink, out);
  786. }
  787. static int config_video(AVFilterLink *outlink)
  788. {
  789. AVFilterContext *ctx = outlink->src;
  790. AudioIIRContext *s = ctx->priv;
  791. outlink->sample_aspect_ratio = (AVRational){1,1};
  792. outlink->w = s->w;
  793. outlink->h = s->h;
  794. outlink->frame_rate = s->rate;
  795. outlink->time_base = av_inv_q(outlink->frame_rate);
  796. return 0;
  797. }
  798. static av_cold int init(AVFilterContext *ctx)
  799. {
  800. AudioIIRContext *s = ctx->priv;
  801. AVFilterPad pad, vpad;
  802. int ret;
  803. if (!s->a_str || !s->b_str || !s->g_str) {
  804. av_log(ctx, AV_LOG_ERROR, "Valid coefficients are mandatory.\n");
  805. return AVERROR(EINVAL);
  806. }
  807. switch (s->precision) {
  808. case 0: s->sample_format = AV_SAMPLE_FMT_DBLP; break;
  809. case 1: s->sample_format = AV_SAMPLE_FMT_FLTP; break;
  810. case 2: s->sample_format = AV_SAMPLE_FMT_S32P; break;
  811. case 3: s->sample_format = AV_SAMPLE_FMT_S16P; break;
  812. default: return AVERROR_BUG;
  813. }
  814. pad = (AVFilterPad){
  815. .name = av_strdup("default"),
  816. .type = AVMEDIA_TYPE_AUDIO,
  817. .config_props = config_output,
  818. };
  819. if (!pad.name)
  820. return AVERROR(ENOMEM);
  821. if (s->response) {
  822. vpad = (AVFilterPad){
  823. .name = av_strdup("filter_response"),
  824. .type = AVMEDIA_TYPE_VIDEO,
  825. .config_props = config_video,
  826. };
  827. if (!vpad.name)
  828. return AVERROR(ENOMEM);
  829. }
  830. ret = ff_insert_outpad(ctx, 0, &pad);
  831. if (ret < 0)
  832. return ret;
  833. if (s->response) {
  834. ret = ff_insert_outpad(ctx, 1, &vpad);
  835. if (ret < 0)
  836. return ret;
  837. }
  838. return 0;
  839. }
  840. static av_cold void uninit(AVFilterContext *ctx)
  841. {
  842. AudioIIRContext *s = ctx->priv;
  843. int ch;
  844. if (s->iir) {
  845. for (ch = 0; ch < s->channels; ch++) {
  846. IIRChannel *iir = &s->iir[ch];
  847. av_freep(&iir->ab[0]);
  848. av_freep(&iir->ab[1]);
  849. av_freep(&iir->cache[0]);
  850. av_freep(&iir->cache[1]);
  851. av_freep(&iir->biquads);
  852. }
  853. }
  854. av_freep(&s->iir);
  855. av_freep(&ctx->output_pads[0].name);
  856. if (s->response)
  857. av_freep(&ctx->output_pads[1].name);
  858. av_frame_free(&s->video);
  859. }
  860. static const AVFilterPad inputs[] = {
  861. {
  862. .name = "default",
  863. .type = AVMEDIA_TYPE_AUDIO,
  864. .filter_frame = filter_frame,
  865. },
  866. { NULL }
  867. };
  868. #define OFFSET(x) offsetof(AudioIIRContext, x)
  869. #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  870. #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  871. static const AVOption aiir_options[] = {
  872. { "z", "set B/numerator/zeros coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  873. { "p", "set A/denominator/poles coefficients", OFFSET(a_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
  874. { "k", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
  875. { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
  876. { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
  877. { "f", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, 0, 3, AF, "format" },
  878. { "tf", "transfer function", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "format" },
  879. { "zp", "Z-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "format" },
  880. { "pr", "Z-plane zeros/poles (polar radians)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "format" },
  881. { "pd", "Z-plane zeros/poles (polar degrees)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "format" },
  882. { "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" },
  883. { "d", "direct", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "process" },
  884. { "s", "serial cascading", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "process" },
  885. { "e", "set precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
  886. { "dbl", "double-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" },
  887. { "flt", "single-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "precision" },
  888. { "i32", "32-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "precision" },
  889. { "i16", "16-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "precision" },
  890. { "response", "show IR frequency response", OFFSET(response), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
  891. { "channel", "set IR channel to display frequency response", OFFSET(ir_channel), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, VF },
  892. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, VF },
  893. { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT32_MAX, VF },
  894. { NULL },
  895. };
  896. AVFILTER_DEFINE_CLASS(aiir);
  897. AVFilter ff_af_aiir = {
  898. .name = "aiir",
  899. .description = NULL_IF_CONFIG_SMALL("Apply Infinite Impulse Response filter with supplied coefficients."),
  900. .priv_size = sizeof(AudioIIRContext),
  901. .priv_class = &aiir_class,
  902. .init = init,
  903. .uninit = uninit,
  904. .query_formats = query_formats,
  905. .inputs = inputs,
  906. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
  907. AVFILTER_FLAG_SLICE_THREADS,
  908. };