af_aiir.c 39 KB

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