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.

1090 lines
36KB

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