FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_aiir.c
Date: 2026-09-25 02:02:46
Exec Total Coverage
Lines: 0 750 0.0%
Functions: 0 47 0.0%
Branches: 0 567 0.0%

Line Branch Exec Source
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
21 #include <float.h>
22
23 #include "libavutil/avstring.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/xga_font_data.h"
28 #include "audio.h"
29 #include "avfilter.h"
30 #include "filters.h"
31 #include "formats.h"
32 #include "video.h"
33
34 typedef struct ThreadData {
35 AVFrame *in, *out;
36 } ThreadData;
37
38 typedef struct Pair {
39 int a, b;
40 } Pair;
41
42 typedef struct BiquadContext {
43 double a[3];
44 double b[3];
45 double w1, w2;
46 } BiquadContext;
47
48 typedef struct IIRChannel {
49 int nb_ab[2];
50 double *ab[2];
51 double g;
52 double *cache[2];
53 double fir;
54 BiquadContext *biquads;
55 int clippings;
56 } IIRChannel;
57
58 typedef struct AudioIIRContext {
59 const AVClass *class;
60 char *a_str, *b_str, *g_str;
61 double dry_gain, wet_gain;
62 double mix;
63 int normalize;
64 int format;
65 int process;
66 int precision;
67 int response;
68 int w, h;
69 int ir_channel;
70 AVRational rate;
71
72 AVFrame *video;
73
74 IIRChannel *iir;
75 int channels;
76 enum AVSampleFormat sample_format;
77
78 int (*iir_channel)(AVFilterContext *ctx, void *arg, int ch, int nb_jobs);
79 } AudioIIRContext;
80
81 ✗ static int query_formats(const AVFilterContext *ctx,
82 AVFilterFormatsConfig **cfg_in,
83 AVFilterFormatsConfig **cfg_out)
84 {
85 ✗ const AudioIIRContext *s = ctx->priv;
86 AVFilterFormats *formats;
87 ✗ enum AVSampleFormat sample_fmts[] = {
88 AV_SAMPLE_FMT_DBLP,
89 AV_SAMPLE_FMT_NONE
90 };
91 static const enum AVPixelFormat pix_fmts[] = {
92 AV_PIX_FMT_RGB0,
93 AV_PIX_FMT_NONE
94 };
95 int ret;
96
97 ✗ if (s->response) {
98 ✗ formats = ff_make_pixel_format_list(pix_fmts);
99 ✗ if ((ret = ff_formats_ref(formats, &cfg_out[1]->formats)) < 0)
100 ✗ return ret;
101 }
102
103 ✗ sample_fmts[0] = s->sample_format;
104 ✗ ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts);
105 ✗ if (ret < 0)
106 ✗ return ret;
107
108 ✗ return 0;
109 }
110
111 #define IIR_CH(name, type, min, max, need_clipping) \
112 static int iir_ch_## name(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) \
113 { \
114 AudioIIRContext *s = ctx->priv; \
115 const double ig = s->dry_gain; \
116 const double og = s->wet_gain; \
117 const double mix = s->mix; \
118 ThreadData *td = arg; \
119 AVFrame *in = td->in, *out = td->out; \
120 const type *src = (const type *)in->extended_data[ch]; \
121 double *oc = (double *)s->iir[ch].cache[0]; \
122 double *ic = (double *)s->iir[ch].cache[1]; \
123 const int nb_a = s->iir[ch].nb_ab[0]; \
124 const int nb_b = s->iir[ch].nb_ab[1]; \
125 const double *a = s->iir[ch].ab[0]; \
126 const double *b = s->iir[ch].ab[1]; \
127 const double g = s->iir[ch].g; \
128 int *clippings = &s->iir[ch].clippings; \
129 type *dst = (type *)out->extended_data[ch]; \
130 int n; \
131 \
132 for (n = 0; n < in->nb_samples; n++) { \
133 double sample = 0.; \
134 int x; \
135 \
136 memmove(&ic[1], &ic[0], (nb_b - 1) * sizeof(*ic)); \
137 memmove(&oc[1], &oc[0], (nb_a - 1) * sizeof(*oc)); \
138 ic[0] = src[n] * ig; \
139 for (x = 0; x < nb_b; x++) \
140 sample += b[x] * ic[x]; \
141 \
142 for (x = 1; x < nb_a; x++) \
143 sample -= a[x] * oc[x]; \
144 \
145 oc[0] = sample; \
146 sample *= og * g; \
147 sample = sample * mix + ic[0] * (1. - mix); \
148 if (need_clipping && sample < min) { \
149 (*clippings)++; \
150 dst[n] = min; \
151 } else if (need_clipping && sample > max) { \
152 (*clippings)++; \
153 dst[n] = max; \
154 } else { \
155 dst[n] = sample; \
156 } \
157 } \
158 \
159 return 0; \
160 }
161
162 ✗ IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
163 ✗ IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
164 ✗ IIR_CH(fltp, float, -1., 1., 0)
165 ✗ IIR_CH(dblp, double, -1., 1., 0)
166
167 #define SERIAL_IIR_CH(name, type, min, max, need_clipping) \
168 static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, \
169 int ch, int nb_jobs) \
170 { \
171 AudioIIRContext *s = ctx->priv; \
172 const double ig = s->dry_gain; \
173 const double og = s->wet_gain; \
174 const double mix = s->mix; \
175 const double imix = 1. - mix; \
176 ThreadData *td = arg; \
177 AVFrame *in = td->in, *out = td->out; \
178 const type *src = (const type *)in->extended_data[ch]; \
179 type *dst = (type *)out->extended_data[ch]; \
180 IIRChannel *iir = &s->iir[ch]; \
181 const double g = iir->g; \
182 int *clippings = &iir->clippings; \
183 int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; \
184 int n, i; \
185 \
186 for (i = nb_biquads - 1; i >= 0; i--) { \
187 const double a1 = -iir->biquads[i].a[1]; \
188 const double a2 = -iir->biquads[i].a[2]; \
189 const double b0 = iir->biquads[i].b[0]; \
190 const double b1 = iir->biquads[i].b[1]; \
191 const double b2 = iir->biquads[i].b[2]; \
192 double w1 = iir->biquads[i].w1; \
193 double w2 = iir->biquads[i].w2; \
194 \
195 for (n = 0; n < in->nb_samples; n++) { \
196 double i0 = ig * (i ? dst[n] : src[n]); \
197 double o0 = i0 * b0 + w1; \
198 \
199 w1 = b1 * i0 + w2 + a1 * o0; \
200 w2 = b2 * i0 + a2 * o0; \
201 o0 *= og * g; \
202 \
203 o0 = o0 * mix + imix * i0; \
204 if (need_clipping && o0 < min) { \
205 (*clippings)++; \
206 dst[n] = min; \
207 } else if (need_clipping && o0 > max) { \
208 (*clippings)++; \
209 dst[n] = max; \
210 } else { \
211 dst[n] = o0; \
212 } \
213 } \
214 iir->biquads[i].w1 = w1; \
215 iir->biquads[i].w2 = w2; \
216 } \
217 \
218 return 0; \
219 }
220
221 ✗ SERIAL_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
222 ✗ SERIAL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
223 ✗ SERIAL_IIR_CH(fltp, float, -1., 1., 0)
224 ✗ SERIAL_IIR_CH(dblp, double, -1., 1., 0)
225
226 #define PARALLEL_IIR_CH(name, type, min, max, need_clipping) \
227 static int iir_ch_parallel_## name(AVFilterContext *ctx, void *arg, \
228 int ch, int nb_jobs) \
229 { \
230 AudioIIRContext *s = ctx->priv; \
231 const double ig = s->dry_gain; \
232 const double og = s->wet_gain; \
233 const double mix = s->mix; \
234 const double imix = 1. - mix; \
235 ThreadData *td = arg; \
236 AVFrame *in = td->in, *out = td->out; \
237 const type *src = (const type *)in->extended_data[ch]; \
238 type *dst = (type *)out->extended_data[ch]; \
239 IIRChannel *iir = &s->iir[ch]; \
240 const double g = iir->g; \
241 const double fir = iir->fir; \
242 int *clippings = &iir->clippings; \
243 int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; \
244 int n, i; \
245 \
246 for (i = 0; i < nb_biquads; i++) { \
247 const double a1 = -iir->biquads[i].a[1]; \
248 const double a2 = -iir->biquads[i].a[2]; \
249 const double b1 = iir->biquads[i].b[1]; \
250 const double b2 = iir->biquads[i].b[2]; \
251 double w1 = iir->biquads[i].w1; \
252 double w2 = iir->biquads[i].w2; \
253 \
254 for (n = 0; n < in->nb_samples; n++) { \
255 double i0 = ig * src[n]; \
256 double o0 = w1; \
257 \
258 w1 = b1 * i0 + w2 + a1 * o0; \
259 w2 = b2 * i0 + a2 * o0; \
260 o0 *= og * g; \
261 o0 += dst[n]; \
262 \
263 if (need_clipping && o0 < min) { \
264 (*clippings)++; \
265 dst[n] = min; \
266 } else if (need_clipping && o0 > max) { \
267 (*clippings)++; \
268 dst[n] = max; \
269 } else { \
270 dst[n] = o0; \
271 } \
272 } \
273 iir->biquads[i].w1 = w1; \
274 iir->biquads[i].w2 = w2; \
275 } \
276 \
277 for (n = 0; n < in->nb_samples; n++) { \
278 dst[n] += fir * src[n]; \
279 dst[n] = dst[n] * mix + imix * src[n]; \
280 } \
281 \
282 return 0; \
283 }
284
285 ✗ PARALLEL_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
286 ✗ PARALLEL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
287 ✗ PARALLEL_IIR_CH(fltp, float, -1., 1., 0)
288 ✗ PARALLEL_IIR_CH(dblp, double, -1., 1., 0)
289
290 #define LATTICE_IIR_CH(name, type, min, max, need_clipping) \
291 static int iir_ch_lattice_## name(AVFilterContext *ctx, void *arg, \
292 int ch, int nb_jobs) \
293 { \
294 AudioIIRContext *s = ctx->priv; \
295 const double ig = s->dry_gain; \
296 const double og = s->wet_gain; \
297 const double mix = s->mix; \
298 ThreadData *td = arg; \
299 AVFrame *in = td->in, *out = td->out; \
300 const type *src = (const type *)in->extended_data[ch]; \
301 double n0, n1, p0, *x = (double *)s->iir[ch].cache[0]; \
302 const int nb_stages = s->iir[ch].nb_ab[1]; \
303 const double *v = s->iir[ch].ab[0]; \
304 const double *k = s->iir[ch].ab[1]; \
305 const double g = s->iir[ch].g; \
306 int *clippings = &s->iir[ch].clippings; \
307 type *dst = (type *)out->extended_data[ch]; \
308 int n; \
309 \
310 for (n = 0; n < in->nb_samples; n++) { \
311 const double in = src[n] * ig; \
312 double out = 0.; \
313 \
314 n1 = in; \
315 for (int i = nb_stages - 1; i >= 0; i--) { \
316 n0 = n1 - k[i] * x[i]; \
317 p0 = n0 * k[i] + x[i]; \
318 out += p0 * v[i+1]; \
319 x[i] = p0; \
320 n1 = n0; \
321 } \
322 \
323 out += n1 * v[0]; \
324 memmove(&x[1], &x[0], nb_stages * sizeof(*x)); \
325 x[0] = n1; \
326 out *= og * g; \
327 out = out * mix + in * (1. - mix); \
328 if (need_clipping && out < min) { \
329 (*clippings)++; \
330 dst[n] = min; \
331 } else if (need_clipping && out > max) { \
332 (*clippings)++; \
333 dst[n] = max; \
334 } else { \
335 dst[n] = out; \
336 } \
337 } \
338 \
339 return 0; \
340 }
341
342 ✗ LATTICE_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
343 ✗ LATTICE_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
344 ✗ LATTICE_IIR_CH(fltp, float, -1., 1., 0)
345 ✗ LATTICE_IIR_CH(dblp, double, -1., 1., 0)
346
347 ✗ static void count_coefficients(char *item_str, int *nb_items)
348 {
349 char *p;
350
351 ✗ if (!item_str)
352 ✗ return;
353
354 ✗ *nb_items = 1;
355 ✗ for (p = item_str; *p && *p != '|'; p++) {
356 ✗ if (*p == ' ')
357 ✗ (*nb_items)++;
358 }
359 }
360
361 ✗ static int read_gains(AVFilterContext *ctx, char *item_str, int nb_items)
362 {
363 ✗ AudioIIRContext *s = ctx->priv;
364 ✗ char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
365 int i;
366
367 ✗ p = old_str = av_strdup(item_str);
368 ✗ if (!p)
369 ✗ return AVERROR(ENOMEM);
370 ✗ for (i = 0; i < nb_items; i++) {
371 ✗ if (!(arg = av_strtok(p, "|", &saveptr)))
372 ✗ arg = prev_arg;
373
374 ✗ if (!arg) {
375 ✗ av_freep(&old_str);
376 ✗ return AVERROR(EINVAL);
377 }
378
379 ✗ p = NULL;
380 ✗ if (av_sscanf(arg, "%lf", &s->iir[i].g) != 1 || !isfinite(s->iir[i].g)) {
381 ✗ av_log(ctx, AV_LOG_ERROR, "Invalid gains supplied: %s\n", arg);
382 ✗ av_freep(&old_str);
383 ✗ return AVERROR(EINVAL);
384 }
385
386 ✗ prev_arg = arg;
387 }
388
389 ✗ av_freep(&old_str);
390
391 ✗ return 0;
392 }
393
394 ✗ static int read_tf_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst)
395 {
396 ✗ char *p, *arg, *old_str, *saveptr = NULL;
397 int i;
398
399 ✗ p = old_str = av_strdup(item_str);
400 ✗ if (!p)
401 ✗ return AVERROR(ENOMEM);
402 ✗ for (i = 0; i < nb_items; i++) {
403 ✗ if (!(arg = av_strtok(p, " ", &saveptr)))
404 ✗ break;
405
406 ✗ p = NULL;
407 ✗ if (av_sscanf(arg, "%lf", &dst[i]) != 1 || !isfinite(dst[i])) {
408 ✗ av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
409 ✗ av_freep(&old_str);
410 ✗ return AVERROR(EINVAL);
411 }
412 }
413
414 ✗ av_freep(&old_str);
415
416 ✗ return 0;
417 }
418
419 ✗ static int read_zp_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst, const char *format)
420 {
421 ✗ char *p, *arg, *old_str, *saveptr = NULL;
422 int i;
423
424 ✗ p = old_str = av_strdup(item_str);
425 ✗ if (!p)
426 ✗ return AVERROR(ENOMEM);
427 ✗ for (i = 0; i < nb_items; i++) {
428 ✗ if (!(arg = av_strtok(p, " ", &saveptr)))
429 ✗ break;
430
431 ✗ p = NULL;
432 ✗ if (av_sscanf(arg, format, &dst[i*2], &dst[i*2+1]) != 2 ||
433 ✗ !isfinite(dst[i*2]) || !isfinite(dst[i*2+1])) {
434 ✗ av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
435 ✗ av_freep(&old_str);
436 ✗ return AVERROR(EINVAL);
437 }
438 }
439
440 ✗ av_freep(&old_str);
441
442 ✗ return 0;
443 }
444
445 static const char *const format[] = { "%lf", "%lf %lfi", "%lf %lfr", "%lf %lfd", "%lf %lfi" };
446
447 ✗ static int read_channels(AVFilterContext *ctx, int channels, uint8_t *item_str, int ab)
448 {
449 ✗ AudioIIRContext *s = ctx->priv;
450 ✗ char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
451 int i, ret;
452
453 ✗ p = old_str = av_strdup(item_str);
454 ✗ if (!p)
455 ✗ return AVERROR(ENOMEM);
456 ✗ for (i = 0; i < channels; i++) {
457 ✗ IIRChannel *iir = &s->iir[i];
458
459 ✗ if (!(arg = av_strtok(p, "|", &saveptr)))
460 ✗ arg = prev_arg;
461
462 ✗ if (!arg) {
463 ✗ av_freep(&old_str);
464 ✗ return AVERROR(EINVAL);
465 }
466
467 ✗ count_coefficients(arg, &iir->nb_ab[ab]);
468
469 ✗ p = NULL;
470 ✗ iir->cache[ab] = av_calloc(iir->nb_ab[ab] + 1, sizeof(double));
471 ✗ iir->ab[ab] = av_calloc(iir->nb_ab[ab] * (!!s->format + 1), sizeof(double));
472 ✗ if (!iir->ab[ab] || !iir->cache[ab]) {
473 ✗ av_freep(&old_str);
474 ✗ return AVERROR(ENOMEM);
475 }
476
477 ✗ if (s->format > 0) {
478 ✗ ret = read_zp_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab], format[s->format]);
479 } else {
480 ✗ ret = read_tf_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab]);
481 }
482 ✗ if (ret < 0) {
483 ✗ av_freep(&old_str);
484 ✗ return ret;
485 }
486 ✗ prev_arg = arg;
487 }
488
489 ✗ av_freep(&old_str);
490
491 ✗ return 0;
492 }
493
494 ✗ static void cmul(double re, double im, double re2, double im2, double *RE, double *IM)
495 {
496 ✗ *RE = re * re2 - im * im2;
497 ✗ *IM = re * im2 + re2 * im;
498 ✗ }
499
500 ✗ static int expand(AVFilterContext *ctx, double *pz, int n, double *coefs)
501 {
502 ✗ coefs[2 * n] = 1.0;
503
504 ✗ for (int i = 1; i <= n; i++) {
505 ✗ for (int j = n - i; j < n; j++) {
506 double re, im;
507
508 ✗ cmul(coefs[2 * (j + 1)], coefs[2 * (j + 1) + 1],
509 ✗ pz[2 * (i - 1)], pz[2 * (i - 1) + 1], &re, &im);
510
511 ✗ coefs[2 * j] -= re;
512 ✗ coefs[2 * j + 1] -= im;
513 }
514 }
515
516 ✗ for (int i = 0; i < n + 1; i++) {
517 ✗ if (fabs(coefs[2 * i + 1]) > FLT_EPSILON) {
518 ✗ av_log(ctx, AV_LOG_ERROR, "coefs: %f of z^%d is not real; poles/zeros are not complex conjugates.\n",
519 ✗ coefs[2 * i + 1], i);
520 ✗ return AVERROR(EINVAL);
521 }
522 }
523
524 ✗ return 0;
525 }
526
527 ✗ static void normalize_coeffs(AVFilterContext *ctx, int ch)
528 {
529 ✗ AudioIIRContext *s = ctx->priv;
530 ✗ IIRChannel *iir = &s->iir[ch];
531 ✗ double sum_den = 0.;
532
533 ✗ if (!s->normalize)
534 ✗ return;
535
536 ✗ for (int i = 0; i < iir->nb_ab[1]; i++) {
537 ✗ sum_den += iir->ab[1][i];
538 }
539
540 ✗ if (sum_den > 1e-6) {
541 ✗ double factor, sum_num = 0.;
542
543 ✗ for (int i = 0; i < iir->nb_ab[0]; i++) {
544 ✗ sum_num += iir->ab[0][i];
545 }
546
547 ✗ factor = sum_num / sum_den;
548
549 ✗ for (int i = 0; i < iir->nb_ab[1]; i++) {
550 ✗ iir->ab[1][i] *= factor;
551 }
552 }
553 }
554
555 ✗ static int convert_zp2tf(AVFilterContext *ctx, int channels)
556 {
557 ✗ AudioIIRContext *s = ctx->priv;
558 ✗ int ch, i, j, ret = 0;
559
560 ✗ for (ch = 0; ch < channels; ch++) {
561 ✗ IIRChannel *iir = &s->iir[ch];
562 double *topc, *botc;
563
564 ✗ topc = av_calloc((iir->nb_ab[1] + 1) * 2, sizeof(*topc));
565 ✗ botc = av_calloc((iir->nb_ab[0] + 1) * 2, sizeof(*botc));
566 ✗ if (!topc || !botc) {
567 ✗ ret = AVERROR(ENOMEM);
568 ✗ goto fail;
569 }
570
571 ✗ ret = expand(ctx, iir->ab[0], iir->nb_ab[0], botc);
572 ✗ if (ret < 0) {
573 ✗ goto fail;
574 }
575
576 ✗ ret = expand(ctx, iir->ab[1], iir->nb_ab[1], topc);
577 ✗ if (ret < 0) {
578 ✗ goto fail;
579 }
580
581 ✗ for (j = 0, i = iir->nb_ab[1]; i >= 0; j++, i--) {
582 ✗ iir->ab[1][j] = topc[2 * i];
583 }
584 ✗ iir->nb_ab[1]++;
585
586 ✗ for (j = 0, i = iir->nb_ab[0]; i >= 0; j++, i--) {
587 ✗ iir->ab[0][j] = botc[2 * i];
588 }
589 ✗ iir->nb_ab[0]++;
590
591 ✗ normalize_coeffs(ctx, ch);
592
593 ✗ fail:
594 ✗ av_free(topc);
595 ✗ av_free(botc);
596 ✗ if (ret < 0)
597 ✗ break;
598 }
599
600 ✗ return ret;
601 }
602
603 ✗ static int decompose_zp2biquads(AVFilterContext *ctx, int channels)
604 {
605 ✗ AudioIIRContext *s = ctx->priv;
606 int ch, ret;
607
608 ✗ for (ch = 0; ch < channels; ch++) {
609 ✗ IIRChannel *iir = &s->iir[ch];
610 ✗ int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2;
611 ✗ int current_biquad = 0;
612
613 ✗ iir->biquads = av_calloc(nb_biquads, sizeof(BiquadContext));
614 ✗ if (!iir->biquads)
615 ✗ return AVERROR(ENOMEM);
616
617 ✗ while (nb_biquads--) {
618 ✗ Pair outmost_pole = { -1, -1 };
619 ✗ Pair nearest_zero = { -1, -1 };
620 ✗ double zeros[4] = { 0 };
621 ✗ double poles[4] = { 0 };
622 ✗ double b[6] = { 0 };
623 ✗ double a[6] = { 0 };
624 ✗ double min_distance = DBL_MAX;
625 ✗ double max_mag = 0;
626 double factor;
627 int i;
628
629 ✗ for (i = 0; i < iir->nb_ab[0]; i++) {
630 double mag;
631
632 ✗ if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
633 ✗ continue;
634 ✗ mag = hypot(iir->ab[0][2 * i], iir->ab[0][2 * i + 1]);
635
636 ✗ if (mag > max_mag) {
637 ✗ max_mag = mag;
638 ✗ outmost_pole.a = i;
639 }
640 }
641
642 ✗ for (i = 0; i < iir->nb_ab[0]; i++) {
643 ✗ if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
644 ✗ continue;
645
646 ✗ if (iir->ab[0][2 * i ] == iir->ab[0][2 * outmost_pole.a ] &&
647 ✗ iir->ab[0][2 * i + 1] == -iir->ab[0][2 * outmost_pole.a + 1]) {
648 ✗ outmost_pole.b = i;
649 ✗ break;
650 }
651 }
652
653 ✗ av_log(ctx, AV_LOG_VERBOSE, "outmost_pole is %d.%d\n", outmost_pole.a, outmost_pole.b);
654
655 ✗ if (outmost_pole.a < 0 || outmost_pole.b < 0)
656 ✗ return AVERROR(EINVAL);
657
658 ✗ for (i = 0; i < iir->nb_ab[1]; i++) {
659 double distance;
660
661 ✗ if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
662 ✗ continue;
663 ✗ distance = hypot(iir->ab[0][2 * outmost_pole.a ] - iir->ab[1][2 * i ],
664 ✗ iir->ab[0][2 * outmost_pole.a + 1] - iir->ab[1][2 * i + 1]);
665
666 ✗ if (distance < min_distance) {
667 ✗ min_distance = distance;
668 ✗ nearest_zero.a = i;
669 }
670 }
671
672 ✗ for (i = 0; i < iir->nb_ab[1]; i++) {
673 ✗ if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
674 ✗ continue;
675
676 ✗ if (iir->ab[1][2 * i ] == iir->ab[1][2 * nearest_zero.a ] &&
677 ✗ iir->ab[1][2 * i + 1] == -iir->ab[1][2 * nearest_zero.a + 1]) {
678 ✗ nearest_zero.b = i;
679 ✗ break;
680 }
681 }
682
683 ✗ av_log(ctx, AV_LOG_VERBOSE, "nearest_zero is %d.%d\n", nearest_zero.a, nearest_zero.b);
684
685 ✗ if (nearest_zero.a < 0 || nearest_zero.b < 0)
686 ✗ return AVERROR(EINVAL);
687
688 ✗ poles[0] = iir->ab[0][2 * outmost_pole.a ];
689 ✗ poles[1] = iir->ab[0][2 * outmost_pole.a + 1];
690
691 ✗ zeros[0] = iir->ab[1][2 * nearest_zero.a ];
692 ✗ zeros[1] = iir->ab[1][2 * nearest_zero.a + 1];
693
694 ✗ if (nearest_zero.a == nearest_zero.b && outmost_pole.a == outmost_pole.b) {
695 ✗ zeros[2] = 0;
696 ✗ zeros[3] = 0;
697
698 ✗ poles[2] = 0;
699 ✗ poles[3] = 0;
700 } else {
701 ✗ poles[2] = iir->ab[0][2 * outmost_pole.b ];
702 ✗ poles[3] = iir->ab[0][2 * outmost_pole.b + 1];
703
704 ✗ zeros[2] = iir->ab[1][2 * nearest_zero.b ];
705 ✗ zeros[3] = iir->ab[1][2 * nearest_zero.b + 1];
706 }
707
708 ✗ ret = expand(ctx, zeros, 2, b);
709 ✗ if (ret < 0)
710 ✗ return ret;
711
712 ✗ ret = expand(ctx, poles, 2, a);
713 ✗ if (ret < 0)
714 ✗ return ret;
715
716 ✗ iir->ab[0][2 * outmost_pole.a] = iir->ab[0][2 * outmost_pole.a + 1] = NAN;
717 ✗ iir->ab[0][2 * outmost_pole.b] = iir->ab[0][2 * outmost_pole.b + 1] = NAN;
718 ✗ iir->ab[1][2 * nearest_zero.a] = iir->ab[1][2 * nearest_zero.a + 1] = NAN;
719 ✗ iir->ab[1][2 * nearest_zero.b] = iir->ab[1][2 * nearest_zero.b + 1] = NAN;
720
721 ✗ iir->biquads[current_biquad].a[0] = 1.;
722 ✗ iir->biquads[current_biquad].a[1] = a[2] / a[4];
723 ✗ iir->biquads[current_biquad].a[2] = a[0] / a[4];
724 ✗ iir->biquads[current_biquad].b[0] = b[4] / a[4];
725 ✗ iir->biquads[current_biquad].b[1] = b[2] / a[4];
726 ✗ iir->biquads[current_biquad].b[2] = b[0] / a[4];
727
728 ✗ if (s->normalize &&
729 ✗ fabs(iir->biquads[current_biquad].b[0] +
730 ✗ iir->biquads[current_biquad].b[1] +
731 ✗ iir->biquads[current_biquad].b[2]) > 1e-6) {
732 ✗ factor = (iir->biquads[current_biquad].a[0] +
733 ✗ iir->biquads[current_biquad].a[1] +
734 ✗ iir->biquads[current_biquad].a[2]) /
735 ✗ (iir->biquads[current_biquad].b[0] +
736 ✗ iir->biquads[current_biquad].b[1] +
737 ✗ iir->biquads[current_biquad].b[2]);
738
739 ✗ av_log(ctx, AV_LOG_VERBOSE, "factor=%f\n", factor);
740
741 ✗ iir->biquads[current_biquad].b[0] *= factor;
742 ✗ iir->biquads[current_biquad].b[1] *= factor;
743 ✗ iir->biquads[current_biquad].b[2] *= factor;
744 }
745
746 ✗ iir->biquads[current_biquad].b[0] *= (current_biquad ? 1.0 : iir->g);
747 ✗ iir->biquads[current_biquad].b[1] *= (current_biquad ? 1.0 : iir->g);
748 ✗ iir->biquads[current_biquad].b[2] *= (current_biquad ? 1.0 : iir->g);
749
750 ✗ av_log(ctx, AV_LOG_VERBOSE, "a=%f %f %f:b=%f %f %f\n",
751 ✗ iir->biquads[current_biquad].a[0],
752 ✗ iir->biquads[current_biquad].a[1],
753 ✗ iir->biquads[current_biquad].a[2],
754 ✗ iir->biquads[current_biquad].b[0],
755 ✗ iir->biquads[current_biquad].b[1],
756 ✗ iir->biquads[current_biquad].b[2]);
757
758 ✗ current_biquad++;
759 }
760 }
761
762 ✗ return 0;
763 }
764
765 ✗ static void biquad_process(double *x, double *y, int length,
766 double b0, double b1, double b2,
767 double a1, double a2)
768 {
769 ✗ double w1 = 0., w2 = 0.;
770
771 ✗ a1 = -a1;
772 ✗ a2 = -a2;
773
774 ✗ for (int n = 0; n < length; n++) {
775 ✗ double out, in = x[n];
776
777 ✗ y[n] = out = in * b0 + w1;
778 ✗ w1 = b1 * in + w2 + a1 * out;
779 ✗ w2 = b2 * in + a2 * out;
780 }
781 ✗ }
782
783 ✗ static void solve(double *matrix, double *vector, int n, double *y, double *x, double *lu)
784 {
785 ✗ double sum = 0.;
786
787 ✗ for (int i = 0; i < n; i++) {
788 ✗ for (int j = i; j < n; j++) {
789 ✗ sum = 0.;
790 ✗ for (int k = 0; k < i; k++)
791 ✗ sum += lu[i * n + k] * lu[k * n + j];
792 ✗ lu[i * n + j] = matrix[j * n + i] - sum;
793 }
794 ✗ for (int j = i + 1; j < n; j++) {
795 ✗ sum = 0.;
796 ✗ for (int k = 0; k < i; k++)
797 ✗ sum += lu[j * n + k] * lu[k * n + i];
798 ✗ lu[j * n + i] = (1. / lu[i * n + i]) * (matrix[i * n + j] - sum);
799 }
800 }
801
802 ✗ for (int i = 0; i < n; i++) {
803 ✗ sum = 0.;
804 ✗ for (int k = 0; k < i; k++)
805 ✗ sum += lu[i * n + k] * y[k];
806 ✗ y[i] = vector[i] - sum;
807 }
808
809 ✗ for (int i = n - 1; i >= 0; i--) {
810 ✗ sum = 0.;
811 ✗ for (int k = i + 1; k < n; k++)
812 ✗ sum += lu[i * n + k] * x[k];
813 ✗ x[i] = (1 / lu[i * n + i]) * (y[i] - sum);
814 }
815 ✗ }
816
817 ✗ static int convert_serial2parallel(AVFilterContext *ctx, int channels)
818 {
819 ✗ AudioIIRContext *s = ctx->priv;
820
821 ✗ for (int ch = 0; ch < channels; ch++) {
822 ✗ IIRChannel *iir = &s->iir[ch];
823 ✗ int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2;
824 ✗ int length = nb_biquads * 2 + 1;
825 ✗ double *impulse = av_calloc(length, sizeof(*impulse));
826 ✗ double *y = av_calloc(length, sizeof(*y));
827 ✗ double *resp = av_calloc(length, sizeof(*resp));
828 ✗ double *M = av_calloc((length - 1) * nb_biquads, 2 * 2 * sizeof(*M));
829 double *W;
830
831 ✗ if (!impulse || !y || !resp || !M) {
832 ✗ av_free(impulse);
833 ✗ av_free(y);
834 ✗ av_free(resp);
835 ✗ av_free(M);
836 ✗ return AVERROR(ENOMEM);
837 }
838 ✗ W = M + (length - 1) * 2 * nb_biquads;
839
840 ✗ impulse[0] = 1.;
841
842 ✗ for (int n = 0; n < nb_biquads; n++) {
843 ✗ BiquadContext *biquad = &iir->biquads[n];
844
845 ✗ biquad_process(n ? y : impulse, y, length,
846 biquad->b[0], biquad->b[1], biquad->b[2],
847 biquad->a[1], biquad->a[2]);
848 }
849
850 ✗ for (int n = 0; n < nb_biquads; n++) {
851 ✗ BiquadContext *biquad = &iir->biquads[n];
852
853 ✗ biquad_process(impulse, resp, length - 1,
854 1., 0., 0., biquad->a[1], biquad->a[2]);
855
856 ✗ memcpy(M + n * 2 * (length - 1), resp, sizeof(*resp) * (length - 1));
857 ✗ memcpy(M + n * 2 * (length - 1) + length, resp, sizeof(*resp) * (length - 2));
858 ✗ memset(resp, 0, length * sizeof(*resp));
859 }
860
861 ✗ solve(M, &y[1], length - 1, &impulse[1], resp, W);
862
863 ✗ iir->fir = y[0];
864
865 ✗ for (int n = 0; n < nb_biquads; n++) {
866 ✗ BiquadContext *biquad = &iir->biquads[n];
867
868 ✗ biquad->b[0] = 0.;
869 ✗ biquad->b[1] = resp[n * 2 + 0];
870 ✗ biquad->b[2] = resp[n * 2 + 1];
871 }
872
873 ✗ av_free(impulse);
874 ✗ av_free(y);
875 ✗ av_free(resp);
876 ✗ av_free(M);
877 }
878
879 ✗ return 0;
880 }
881
882 ✗ static void convert_pr2zp(AVFilterContext *ctx, int channels)
883 {
884 ✗ AudioIIRContext *s = ctx->priv;
885 int ch;
886
887 ✗ for (ch = 0; ch < channels; ch++) {
888 ✗ IIRChannel *iir = &s->iir[ch];
889 int n;
890
891 ✗ for (n = 0; n < iir->nb_ab[0]; n++) {
892 ✗ double r = iir->ab[0][2*n];
893 ✗ double angle = iir->ab[0][2*n+1];
894
895 ✗ iir->ab[0][2*n] = r * cos(angle);
896 ✗ iir->ab[0][2*n+1] = r * sin(angle);
897 }
898
899 ✗ for (n = 0; n < iir->nb_ab[1]; n++) {
900 ✗ double r = iir->ab[1][2*n];
901 ✗ double angle = iir->ab[1][2*n+1];
902
903 ✗ iir->ab[1][2*n] = r * cos(angle);
904 ✗ iir->ab[1][2*n+1] = r * sin(angle);
905 }
906 }
907 ✗ }
908
909 ✗ static void convert_sp2zp(AVFilterContext *ctx, int channels)
910 {
911 ✗ AudioIIRContext *s = ctx->priv;
912 int ch;
913
914 ✗ for (ch = 0; ch < channels; ch++) {
915 ✗ IIRChannel *iir = &s->iir[ch];
916 int n;
917
918 ✗ for (n = 0; n < iir->nb_ab[0]; n++) {
919 ✗ double sr = iir->ab[0][2*n];
920 ✗ double si = iir->ab[0][2*n+1];
921
922 ✗ iir->ab[0][2*n] = exp(sr) * cos(si);
923 ✗ iir->ab[0][2*n+1] = exp(sr) * sin(si);
924 }
925
926 ✗ for (n = 0; n < iir->nb_ab[1]; n++) {
927 ✗ double sr = iir->ab[1][2*n];
928 ✗ double si = iir->ab[1][2*n+1];
929
930 ✗ iir->ab[1][2*n] = exp(sr) * cos(si);
931 ✗ iir->ab[1][2*n+1] = exp(sr) * sin(si);
932 }
933 }
934 ✗ }
935
936 ✗ static double fact(double i)
937 {
938 ✗ if (i <= 0.)
939 ✗ return 1.;
940 ✗ return i * fact(i - 1.);
941 }
942
943 ✗ static double coef_sf2zf(double *a, int N, int n)
944 {
945 ✗ double z = 0.;
946
947 ✗ for (int i = 0; i <= N; i++) {
948 ✗ double acc = 0.;
949
950 ✗ for (int k = FFMAX(n - N + i, 0); k <= FFMIN(i, n); k++) {
951 ✗ acc += ((fact(i) * fact(N - i)) /
952 ✗ (fact(k) * fact(i - k) * fact(n - k) * fact(N - i - n + k))) *
953 ✗ ((k & 1) ? -1. : 1.);
954 }
955
956 ✗ z += a[i] * pow(2., i) * acc;
957 }
958
959 ✗ return z;
960 }
961
962 ✗ static void convert_sf2tf(AVFilterContext *ctx, int channels)
963 {
964 ✗ AudioIIRContext *s = ctx->priv;
965 int ch;
966
967 ✗ for (ch = 0; ch < channels; ch++) {
968 ✗ IIRChannel *iir = &s->iir[ch];
969 ✗ double *temp0 = av_calloc(iir->nb_ab[0], sizeof(*temp0));
970 ✗ double *temp1 = av_calloc(iir->nb_ab[1], sizeof(*temp1));
971
972 ✗ if (!temp0 || !temp1)
973 ✗ goto next;
974
975 ✗ memcpy(temp0, iir->ab[0], iir->nb_ab[0] * sizeof(*temp0));
976 ✗ memcpy(temp1, iir->ab[1], iir->nb_ab[1] * sizeof(*temp1));
977
978 ✗ for (int n = 0; n < iir->nb_ab[0]; n++)
979 ✗ iir->ab[0][n] = coef_sf2zf(temp0, iir->nb_ab[0] - 1, n);
980
981 ✗ for (int n = 0; n < iir->nb_ab[1]; n++)
982 ✗ iir->ab[1][n] = coef_sf2zf(temp1, iir->nb_ab[1] - 1, n);
983
984 ✗ next:
985 ✗ av_free(temp0);
986 ✗ av_free(temp1);
987 }
988 ✗ }
989
990 ✗ static void convert_pd2zp(AVFilterContext *ctx, int channels)
991 {
992 ✗ AudioIIRContext *s = ctx->priv;
993 int ch;
994
995 ✗ for (ch = 0; ch < channels; ch++) {
996 ✗ IIRChannel *iir = &s->iir[ch];
997 int n;
998
999 ✗ for (n = 0; n < iir->nb_ab[0]; n++) {
1000 ✗ double r = iir->ab[0][2*n];
1001 ✗ double angle = M_PI*iir->ab[0][2*n+1]/180.;
1002
1003 ✗ iir->ab[0][2*n] = r * cos(angle);
1004 ✗ iir->ab[0][2*n+1] = r * sin(angle);
1005 }
1006
1007 ✗ for (n = 0; n < iir->nb_ab[1]; n++) {
1008 ✗ double r = iir->ab[1][2*n];
1009 ✗ double angle = M_PI*iir->ab[1][2*n+1]/180.;
1010
1011 ✗ iir->ab[1][2*n] = r * cos(angle);
1012 ✗ iir->ab[1][2*n+1] = r * sin(angle);
1013 }
1014 }
1015 ✗ }
1016
1017 ✗ static void check_stability(AVFilterContext *ctx, int channels)
1018 {
1019 ✗ AudioIIRContext *s = ctx->priv;
1020 int ch;
1021
1022 ✗ for (ch = 0; ch < channels; ch++) {
1023 ✗ IIRChannel *iir = &s->iir[ch];
1024
1025 ✗ for (int n = 0; n < iir->nb_ab[0]; n++) {
1026 ✗ double pr = hypot(iir->ab[0][2*n], iir->ab[0][2*n+1]);
1027
1028 ✗ if (pr >= 1.) {
1029 ✗ av_log(ctx, AV_LOG_WARNING, "pole %d at channel %d is unstable\n", n, ch);
1030 ✗ break;
1031 }
1032 }
1033 }
1034 ✗ }
1035
1036 ✗ static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint32_t color)
1037 {
1038 const uint8_t *font;
1039 int font_height;
1040 int i;
1041
1042 ✗ font = avpriv_cga_font_get(), font_height = 8;
1043
1044 ✗ for (i = 0; txt[i]; i++) {
1045 int char_y, mask;
1046
1047 ✗ uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
1048 ✗ for (char_y = 0; char_y < font_height; char_y++) {
1049 ✗ for (mask = 0x80; mask; mask >>= 1) {
1050 ✗ if (font[txt[i] * font_height + char_y] & mask)
1051 ✗ AV_WL32(p, color);
1052 ✗ p += 4;
1053 }
1054 ✗ p += pic->linesize[0] - 8 * 4;
1055 }
1056 }
1057 ✗ }
1058
1059 ✗ static void draw_line(AVFrame *out, int x0, int y0, int x1, int y1, uint32_t color)
1060 {
1061 ✗ int dx = FFABS(x1-x0);
1062 ✗ int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
1063 ✗ int err = (dx>dy ? dx : -dy) / 2, e2;
1064
1065 for (;;) {
1066 ✗ AV_WL32(out->data[0] + y0 * out->linesize[0] + x0 * 4, color);
1067
1068 ✗ if (x0 == x1 && y0 == y1)
1069 ✗ break;
1070
1071 ✗ e2 = err;
1072
1073 ✗ if (e2 >-dx) {
1074 ✗ err -= dy;
1075 ✗ x0--;
1076 }
1077
1078 ✗ if (e2 < dy) {
1079 ✗ err += dx;
1080 ✗ y0 += sy;
1081 }
1082 }
1083 ✗ }
1084
1085 ✗ static double distance(double x0, double x1, double y0, double y1)
1086 {
1087 ✗ return hypot(x0 - x1, y0 - y1);
1088 }
1089
1090 ✗ static void get_response(int channel, int format, double w,
1091 const double *b, const double *a,
1092 int nb_b, int nb_a, double *magnitude, double *phase)
1093 {
1094 double realz, realp;
1095 double imagz, imagp;
1096 double real, imag;
1097 double div;
1098
1099 ✗ if (format == 0) {
1100 ✗ realz = 0., realp = 0.;
1101 ✗ imagz = 0., imagp = 0.;
1102 ✗ for (int x = 0; x < nb_a; x++) {
1103 ✗ realz += cos(-x * w) * a[x];
1104 ✗ imagz += sin(-x * w) * a[x];
1105 }
1106
1107 ✗ for (int x = 0; x < nb_b; x++) {
1108 ✗ realp += cos(-x * w) * b[x];
1109 ✗ imagp += sin(-x * w) * b[x];
1110 }
1111
1112 ✗ div = realp * realp + imagp * imagp;
1113 ✗ real = (realz * realp + imagz * imagp) / div;
1114 ✗ imag = (imagz * realp - imagp * realz) / div;
1115
1116 ✗ *magnitude = hypot(real, imag);
1117 ✗ *phase = atan2(imag, real);
1118 } else {
1119 ✗ double p = 1., z = 1.;
1120 ✗ double acc = 0.;
1121
1122 ✗ for (int x = 0; x < nb_a; x++) {
1123 ✗ z *= distance(cos(w), a[2 * x], sin(w), a[2 * x + 1]);
1124 ✗ acc += atan2(sin(w) - a[2 * x + 1], cos(w) - a[2 * x]);
1125 }
1126
1127 ✗ for (int x = 0; x < nb_b; x++) {
1128 ✗ p *= distance(cos(w), b[2 * x], sin(w), b[2 * x + 1]);
1129 ✗ acc -= atan2(sin(w) - b[2 * x + 1], cos(w) - b[2 * x]);
1130 }
1131
1132 ✗ *magnitude = z / p;
1133 ✗ *phase = acc;
1134 }
1135 ✗ }
1136
1137 ✗ static void draw_response(AVFilterContext *ctx, AVFrame *out, int sample_rate)
1138 {
1139 ✗ AudioIIRContext *s = ctx->priv;
1140 ✗ double *mag, *phase, *temp, *delay, min = DBL_MAX, max = -DBL_MAX;
1141 ✗ double min_delay = DBL_MAX, max_delay = -DBL_MAX, min_phase, max_phase;
1142 ✗ int prev_ymag = -1, prev_yphase = -1, prev_ydelay = -1;
1143 char text[32];
1144 int ch, i;
1145
1146 ✗ memset(out->data[0], 0, s->h * out->linesize[0]);
1147
1148 ✗ phase = av_malloc_array(s->w, sizeof(*phase));
1149 ✗ temp = av_malloc_array(s->w, sizeof(*temp));
1150 ✗ mag = av_malloc_array(s->w, sizeof(*mag));
1151 ✗ delay = av_malloc_array(s->w, sizeof(*delay));
1152 ✗ if (!mag || !phase || !delay || !temp)
1153 ✗ goto end;
1154
1155 ✗ ch = av_clip(s->ir_channel, 0, s->channels - 1);
1156 ✗ for (i = 0; i < s->w; i++) {
1157 ✗ const double *b = s->iir[ch].ab[0];
1158 ✗ const double *a = s->iir[ch].ab[1];
1159 ✗ const int nb_b = s->iir[ch].nb_ab[0];
1160 ✗ const int nb_a = s->iir[ch].nb_ab[1];
1161 ✗ double w = i * M_PI / (s->w - 1);
1162 double m, p;
1163
1164 ✗ get_response(ch, s->format, w, b, a, nb_b, nb_a, &m, &p);
1165
1166 ✗ mag[i] = s->iir[ch].g * m;
1167 ✗ phase[i] = p;
1168 ✗ min = fmin(min, mag[i]);
1169 ✗ max = fmax(max, mag[i]);
1170 }
1171
1172 ✗ temp[0] = 0.;
1173 ✗ for (i = 0; i < s->w - 1; i++) {
1174 ✗ double d = phase[i] - phase[i + 1];
1175 ✗ temp[i + 1] = ceil(fabs(d) / (2. * M_PI)) * 2. * M_PI * ((d > M_PI) - (d < -M_PI));
1176 }
1177
1178 ✗ min_phase = phase[0];
1179 ✗ max_phase = phase[0];
1180 ✗ for (i = 1; i < s->w; i++) {
1181 ✗ temp[i] += temp[i - 1];
1182 ✗ phase[i] += temp[i];
1183 ✗ min_phase = fmin(min_phase, phase[i]);
1184 ✗ max_phase = fmax(max_phase, phase[i]);
1185 }
1186
1187 ✗ for (i = 0; i < s->w - 1; i++) {
1188 ✗ double div = s->w / (double)sample_rate;
1189
1190 ✗ delay[i + 1] = -(phase[i] - phase[i + 1]) / div;
1191 ✗ min_delay = fmin(min_delay, delay[i + 1]);
1192 ✗ max_delay = fmax(max_delay, delay[i + 1]);
1193 }
1194 ✗ delay[0] = delay[1];
1195
1196 ✗ for (i = 0; i < s->w; i++) {
1197 ✗ int ymag = mag[i] / max * (s->h - 1);
1198 ✗ int ydelay = (delay[i] - min_delay) / (max_delay - min_delay) * (s->h - 1);
1199 ✗ int yphase = (phase[i] - min_phase) / (max_phase - min_phase) * (s->h - 1);
1200
1201 ✗ ymag = s->h - 1 - av_clip(ymag, 0, s->h - 1);
1202 ✗ yphase = s->h - 1 - av_clip(yphase, 0, s->h - 1);
1203 ✗ ydelay = s->h - 1 - av_clip(ydelay, 0, s->h - 1);
1204
1205 ✗ if (prev_ymag < 0)
1206 ✗ prev_ymag = ymag;
1207 ✗ if (prev_yphase < 0)
1208 ✗ prev_yphase = yphase;
1209 ✗ if (prev_ydelay < 0)
1210 ✗ prev_ydelay = ydelay;
1211
1212 ✗ draw_line(out, i, ymag, FFMAX(i - 1, 0), prev_ymag, 0xFFFF00FF);
1213 ✗ draw_line(out, i, yphase, FFMAX(i - 1, 0), prev_yphase, 0xFF00FF00);
1214 ✗ draw_line(out, i, ydelay, FFMAX(i - 1, 0), prev_ydelay, 0xFF00FFFF);
1215
1216 ✗ prev_ymag = ymag;
1217 ✗ prev_yphase = yphase;
1218 ✗ prev_ydelay = ydelay;
1219 }
1220
1221 ✗ if (s->w > 400 && s->h > 100) {
1222 ✗ drawtext(out, 2, 2, "Max Magnitude:", 0xDDDDDDDD);
1223 ✗ snprintf(text, sizeof(text), "%.2f", max);
1224 ✗ drawtext(out, 15 * 8 + 2, 2, text, 0xDDDDDDDD);
1225
1226 ✗ drawtext(out, 2, 12, "Min Magnitude:", 0xDDDDDDDD);
1227 ✗ snprintf(text, sizeof(text), "%.2f", min);
1228 ✗ drawtext(out, 15 * 8 + 2, 12, text, 0xDDDDDDDD);
1229
1230 ✗ drawtext(out, 2, 22, "Max Phase:", 0xDDDDDDDD);
1231 ✗ snprintf(text, sizeof(text), "%.2f", max_phase);
1232 ✗ drawtext(out, 15 * 8 + 2, 22, text, 0xDDDDDDDD);
1233
1234 ✗ drawtext(out, 2, 32, "Min Phase:", 0xDDDDDDDD);
1235 ✗ snprintf(text, sizeof(text), "%.2f", min_phase);
1236 ✗ drawtext(out, 15 * 8 + 2, 32, text, 0xDDDDDDDD);
1237
1238 ✗ drawtext(out, 2, 42, "Max Delay:", 0xDDDDDDDD);
1239 ✗ snprintf(text, sizeof(text), "%.2f", max_delay);
1240 ✗ drawtext(out, 11 * 8 + 2, 42, text, 0xDDDDDDDD);
1241
1242 ✗ drawtext(out, 2, 52, "Min Delay:", 0xDDDDDDDD);
1243 ✗ snprintf(text, sizeof(text), "%.2f", min_delay);
1244 ✗ drawtext(out, 11 * 8 + 2, 52, text, 0xDDDDDDDD);
1245 }
1246
1247 ✗ end:
1248 ✗ av_free(delay);
1249 ✗ av_free(temp);
1250 ✗ av_free(phase);
1251 ✗ av_free(mag);
1252 ✗ }
1253
1254 ✗ static int config_output(AVFilterLink *outlink)
1255 {
1256 ✗ AVFilterContext *ctx = outlink->src;
1257 ✗ AudioIIRContext *s = ctx->priv;
1258 ✗ AVFilterLink *inlink = ctx->inputs[0];
1259 int ch, ret, i;
1260
1261 ✗ s->channels = inlink->ch_layout.nb_channels;
1262 ✗ s->iir = av_calloc(s->channels, sizeof(*s->iir));
1263 ✗ if (!s->iir)
1264 ✗ return AVERROR(ENOMEM);
1265
1266 ✗ ret = read_gains(ctx, s->g_str, inlink->ch_layout.nb_channels);
1267 ✗ if (ret < 0)
1268 ✗ return ret;
1269
1270 ✗ ret = read_channels(ctx, inlink->ch_layout.nb_channels, s->a_str, 0);
1271 ✗ if (ret < 0)
1272 ✗ return ret;
1273
1274 ✗ ret = read_channels(ctx, inlink->ch_layout.nb_channels, s->b_str, 1);
1275 ✗ if (ret < 0)
1276 ✗ return ret;
1277
1278 ✗ if (s->format == -1) {
1279 ✗ convert_sf2tf(ctx, inlink->ch_layout.nb_channels);
1280 ✗ s->format = 0;
1281 ✗ } else if (s->format == 2) {
1282 ✗ convert_pr2zp(ctx, inlink->ch_layout.nb_channels);
1283 ✗ } else if (s->format == 3) {
1284 ✗ convert_pd2zp(ctx, inlink->ch_layout.nb_channels);
1285 ✗ } else if (s->format == 4) {
1286 ✗ convert_sp2zp(ctx, inlink->ch_layout.nb_channels);
1287 }
1288 ✗ if (s->format > 0) {
1289 ✗ check_stability(ctx, inlink->ch_layout.nb_channels);
1290 }
1291
1292 ✗ av_frame_free(&s->video);
1293 ✗ if (s->response) {
1294 ✗ s->video = ff_get_video_buffer(ctx->outputs[1], s->w, s->h);
1295 ✗ if (!s->video)
1296 ✗ return AVERROR(ENOMEM);
1297
1298 ✗ draw_response(ctx, s->video, inlink->sample_rate);
1299 }
1300
1301 ✗ if (s->format == 0)
1302 ✗ av_log(ctx, AV_LOG_WARNING, "transfer function coefficients format is not recommended for too high number of zeros/poles.\n");
1303
1304 ✗ if (s->format > 0 && s->process == 0) {
1305 ✗ av_log(ctx, AV_LOG_WARNING, "Direct processing is not recommended for zp coefficients format.\n");
1306
1307 ✗ ret = convert_zp2tf(ctx, inlink->ch_layout.nb_channels);
1308 ✗ if (ret < 0)
1309 ✗ return ret;
1310 ✗ } else if (s->format == -2 && s->process > 0) {
1311 ✗ av_log(ctx, AV_LOG_ERROR, "Only direct processing is implemented for lattice-ladder function.\n");
1312 ✗ return AVERROR_PATCHWELCOME;
1313 ✗ } else if (s->format <= 0 && s->process == 1) {
1314 ✗ av_log(ctx, AV_LOG_ERROR, "Serial processing is not implemented for transfer function.\n");
1315 ✗ return AVERROR_PATCHWELCOME;
1316 ✗ } else if (s->format <= 0 && s->process == 2) {
1317 ✗ av_log(ctx, AV_LOG_ERROR, "Parallel processing is not implemented for transfer function.\n");
1318 ✗ return AVERROR_PATCHWELCOME;
1319 ✗ } else if (s->format > 0 && s->process == 1) {
1320 ✗ ret = decompose_zp2biquads(ctx, inlink->ch_layout.nb_channels);
1321 ✗ if (ret < 0)
1322 ✗ return ret;
1323 ✗ } else if (s->format > 0 && s->process == 2) {
1324 ✗ if (s->precision > 1)
1325 ✗ av_log(ctx, AV_LOG_WARNING, "Parallel processing is not recommended for fixed-point precisions.\n");
1326 ✗ ret = decompose_zp2biquads(ctx, inlink->ch_layout.nb_channels);
1327 ✗ if (ret < 0)
1328 ✗ return ret;
1329 ✗ ret = convert_serial2parallel(ctx, inlink->ch_layout.nb_channels);
1330 ✗ if (ret < 0)
1331 ✗ return ret;
1332 }
1333
1334 ✗ for (ch = 0; s->format == -2 && ch < inlink->ch_layout.nb_channels; ch++) {
1335 ✗ IIRChannel *iir = &s->iir[ch];
1336
1337 ✗ if (iir->nb_ab[0] != iir->nb_ab[1] + 1) {
1338 ✗ av_log(ctx, AV_LOG_ERROR, "Number of ladder coefficients must be one more than number of reflection coefficients.\n");
1339 ✗ return AVERROR(EINVAL);
1340 }
1341 }
1342
1343 ✗ for (ch = 0; s->format == 0 && ch < inlink->ch_layout.nb_channels; ch++) {
1344 ✗ IIRChannel *iir = &s->iir[ch];
1345
1346 ✗ for (i = 1; i < iir->nb_ab[0]; i++) {
1347 ✗ iir->ab[0][i] /= iir->ab[0][0];
1348 }
1349
1350 ✗ iir->ab[0][0] = 1.0;
1351 ✗ for (i = 0; i < iir->nb_ab[1]; i++) {
1352 ✗ iir->ab[1][i] *= iir->g;
1353 }
1354
1355 ✗ normalize_coeffs(ctx, ch);
1356 }
1357
1358 ✗ switch (inlink->format) {
1359 ✗ case AV_SAMPLE_FMT_DBLP: s->iir_channel = s->process == 2 ? iir_ch_parallel_dblp : s->process == 1 ? iir_ch_serial_dblp : iir_ch_dblp; break;
1360 ✗ case AV_SAMPLE_FMT_FLTP: s->iir_channel = s->process == 2 ? iir_ch_parallel_fltp : s->process == 1 ? iir_ch_serial_fltp : iir_ch_fltp; break;
1361 ✗ case AV_SAMPLE_FMT_S32P: s->iir_channel = s->process == 2 ? iir_ch_parallel_s32p : s->process == 1 ? iir_ch_serial_s32p : iir_ch_s32p; break;
1362 ✗ case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 2 ? iir_ch_parallel_s16p : s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break;
1363 }
1364
1365 ✗ if (s->format == -2) {
1366 ✗ switch (inlink->format) {
1367 ✗ case AV_SAMPLE_FMT_DBLP: s->iir_channel = iir_ch_lattice_dblp; break;
1368 ✗ case AV_SAMPLE_FMT_FLTP: s->iir_channel = iir_ch_lattice_fltp; break;
1369 ✗ case AV_SAMPLE_FMT_S32P: s->iir_channel = iir_ch_lattice_s32p; break;
1370 ✗ case AV_SAMPLE_FMT_S16P: s->iir_channel = iir_ch_lattice_s16p; break;
1371 }
1372 }
1373
1374 ✗ return 0;
1375 }
1376
1377 ✗ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
1378 {
1379 ✗ AVFilterContext *ctx = inlink->dst;
1380 ✗ AudioIIRContext *s = ctx->priv;
1381 ✗ AVFilterLink *outlink = ctx->outputs[0];
1382 ThreadData td;
1383 AVFrame *out;
1384 int ch, ret;
1385
1386 ✗ if (av_frame_is_writable(in) && s->process != 2) {
1387 ✗ out = in;
1388 } else {
1389 ✗ out = ff_get_audio_buffer(outlink, in->nb_samples);
1390 ✗ if (!out) {
1391 ✗ av_frame_free(&in);
1392 ✗ return AVERROR(ENOMEM);
1393 }
1394 ✗ av_frame_copy_props(out, in);
1395 }
1396
1397 ✗ td.in = in;
1398 ✗ td.out = out;
1399 ✗ ff_filter_execute(ctx, s->iir_channel, &td, NULL, outlink->ch_layout.nb_channels);
1400
1401 ✗ for (ch = 0; ch < outlink->ch_layout.nb_channels; ch++) {
1402 ✗ if (s->iir[ch].clippings > 0)
1403 ✗ av_log(ctx, AV_LOG_WARNING, "Channel %d clipping %d times. Please reduce gain.\n",
1404 ✗ ch, s->iir[ch].clippings);
1405 ✗ s->iir[ch].clippings = 0;
1406 }
1407
1408 ✗ if (in != out)
1409 ✗ av_frame_free(&in);
1410
1411 ✗ if (s->response) {
1412 ✗ AVFilterLink *outlink = ctx->outputs[1];
1413 ✗ int64_t old_pts = s->video->pts;
1414 ✗ int64_t new_pts = av_rescale_q(out->pts, ctx->inputs[0]->time_base, outlink->time_base);
1415
1416 ✗ if (new_pts > old_pts) {
1417 AVFrame *clone;
1418
1419 ✗ s->video->pts = new_pts;
1420 ✗ clone = av_frame_clone(s->video);
1421 ✗ if (!clone)
1422 ✗ return AVERROR(ENOMEM);
1423 ✗ ret = ff_filter_frame(outlink, clone);
1424 ✗ if (ret < 0)
1425 ✗ return ret;
1426 }
1427 }
1428
1429 ✗ return ff_filter_frame(outlink, out);
1430 }
1431
1432 ✗ static int config_video(AVFilterLink *outlink)
1433 {
1434 ✗ FilterLink *l = ff_filter_link(outlink);
1435 ✗ AVFilterContext *ctx = outlink->src;
1436 ✗ AudioIIRContext *s = ctx->priv;
1437
1438 ✗ outlink->sample_aspect_ratio = (AVRational){1,1};
1439 ✗ outlink->w = s->w;
1440 ✗ outlink->h = s->h;
1441 ✗ l->frame_rate = s->rate;
1442 ✗ outlink->time_base = av_inv_q(l->frame_rate);
1443
1444 ✗ return 0;
1445 }
1446
1447 ✗ static av_cold int init(AVFilterContext *ctx)
1448 {
1449 ✗ AudioIIRContext *s = ctx->priv;
1450 AVFilterPad pad, vpad;
1451 int ret;
1452
1453 ✗ if (!s->a_str || !s->b_str || !s->g_str) {
1454 ✗ av_log(ctx, AV_LOG_ERROR, "Valid coefficients are mandatory.\n");
1455 ✗ return AVERROR(EINVAL);
1456 }
1457
1458 ✗ switch (s->precision) {
1459 ✗ case 0: s->sample_format = AV_SAMPLE_FMT_DBLP; break;
1460 ✗ case 1: s->sample_format = AV_SAMPLE_FMT_FLTP; break;
1461 ✗ case 2: s->sample_format = AV_SAMPLE_FMT_S32P; break;
1462 ✗ case 3: s->sample_format = AV_SAMPLE_FMT_S16P; break;
1463 ✗ default: return AVERROR_BUG;
1464 }
1465
1466 ✗ pad = (AVFilterPad){
1467 .name = "default",
1468 .type = AVMEDIA_TYPE_AUDIO,
1469 .config_props = config_output,
1470 };
1471
1472 ✗ ret = ff_append_outpad(ctx, &pad);
1473 ✗ if (ret < 0)
1474 ✗ return ret;
1475
1476 ✗ if (s->response) {
1477 ✗ vpad = (AVFilterPad){
1478 .name = "filter_response",
1479 .type = AVMEDIA_TYPE_VIDEO,
1480 .config_props = config_video,
1481 };
1482
1483 ✗ ret = ff_append_outpad(ctx, &vpad);
1484 ✗ if (ret < 0)
1485 ✗ return ret;
1486 }
1487
1488 ✗ return 0;
1489 }
1490
1491 ✗ static av_cold void uninit(AVFilterContext *ctx)
1492 {
1493 ✗ AudioIIRContext *s = ctx->priv;
1494 int ch;
1495
1496 ✗ if (s->iir) {
1497 ✗ for (ch = 0; ch < s->channels; ch++) {
1498 ✗ IIRChannel *iir = &s->iir[ch];
1499 ✗ av_freep(&iir->ab[0]);
1500 ✗ av_freep(&iir->ab[1]);
1501 ✗ av_freep(&iir->cache[0]);
1502 ✗ av_freep(&iir->cache[1]);
1503 ✗ av_freep(&iir->biquads);
1504 }
1505 }
1506 ✗ av_freep(&s->iir);
1507
1508 ✗ av_frame_free(&s->video);
1509 ✗ }
1510
1511 static const AVFilterPad inputs[] = {
1512 {
1513 .name = "default",
1514 .type = AVMEDIA_TYPE_AUDIO,
1515 .filter_frame = filter_frame,
1516 },
1517 };
1518
1519 #define OFFSET(x) offsetof(AudioIIRContext, x)
1520 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1521 #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1522
1523 static const AVOption aiir_options[] = {
1524 { "zeros", "set B/numerator/zeros/reflection coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
1525 { "z", "set B/numerator/zeros/reflection coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
1526 { "poles", "set A/denominator/poles/ladder coefficients", OFFSET(a_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
1527 { "p", "set A/denominator/poles/ladder coefficients", OFFSET(a_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
1528 { "gains", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
1529 { "k", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
1530 { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
1531 { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
1532 { "format", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, -2, 4, AF, .unit = "format" },
1533 { "f", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, -2, 4, AF, .unit = "format" },
1534 { "ll", "lattice-ladder function", 0, AV_OPT_TYPE_CONST, {.i64=-2}, 0, 0, AF, .unit = "format" },
1535 { "sf", "analog transfer function", 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, AF, .unit = "format" },
1536 { "tf", "digital transfer function", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "format" },
1537 { "zp", "Z-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "format" },
1538 { "pr", "Z-plane zeros/poles (polar radians)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, .unit = "format" },
1539 { "pd", "Z-plane zeros/poles (polar degrees)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, .unit = "format" },
1540 { "sp", "S-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, .unit = "format" },
1541 { "process", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, AF, .unit = "process" },
1542 { "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, AF, .unit = "process" },
1543 { "d", "direct", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "process" },
1544 { "s", "serial", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "process" },
1545 { "p", "parallel", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, .unit = "process" },
1546 { "precision", "set filtering precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, .unit = "precision" },
1547 { "e", "set precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, .unit = "precision" },
1548 { "dbl", "double-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "precision" },
1549 { "flt", "single-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "precision" },
1550 { "i32", "32-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, .unit = "precision" },
1551 { "i16", "16-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, .unit = "precision" },
1552 { "normalize", "normalize coefficients", OFFSET(normalize),AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
1553 { "n", "normalize coefficients", OFFSET(normalize),AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
1554 { "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
1555 { "response", "show IR frequency response", OFFSET(response), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
1556 { "channel", "set IR channel to display frequency response", OFFSET(ir_channel), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, VF },
1557 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, VF },
1558 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT32_MAX, VF },
1559 { NULL },
1560 };
1561
1562 AVFILTER_DEFINE_CLASS(aiir);
1563
1564 const FFFilter ff_af_aiir = {
1565 .p.name = "aiir",
1566 .p.description = NULL_IF_CONFIG_SMALL("Apply Infinite Impulse Response filter with supplied coefficients."),
1567 .p.priv_class = &aiir_class,
1568 .p.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
1569 AVFILTER_FLAG_SLICE_THREADS,
1570 .priv_size = sizeof(AudioIIRContext),
1571 .init = init,
1572 .uninit = uninit,
1573 FILTER_INPUTS(inputs),
1574 FILTER_QUERY_FUNC2(query_formats),
1575 };
1576