2 * Copyright (c) 2018 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/avassert.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/xga_font_data.h"
32 typedef struct ThreadData {
40 typedef struct BiquadContext {
47 typedef struct IIRChannel {
52 BiquadContext *biquads;
56 typedef struct AudioIIRContext {
58 char *a_str, *b_str, *g_str;
59 double dry_gain, wet_gain;
74 enum AVSampleFormat sample_format;
76 int (*iir_channel)(AVFilterContext *ctx, void *arg, int ch, int nb_jobs);
79 static int query_formats(AVFilterContext *ctx)
81 AudioIIRContext *s = ctx->priv;
82 AVFilterFormats *formats;
83 AVFilterChannelLayouts *layouts;
84 enum AVSampleFormat sample_fmts[] = {
88 static const enum AVPixelFormat pix_fmts[] = {
95 AVFilterLink *videolink = ctx->outputs[1];
97 formats = ff_make_format_list(pix_fmts);
98 if ((ret = ff_formats_ref(formats, &videolink->in_formats)) < 0)
102 layouts = ff_all_channel_counts();
104 return AVERROR(ENOMEM);
105 ret = ff_set_common_channel_layouts(ctx, layouts);
109 sample_fmts[0] = s->sample_format;
110 formats = ff_make_format_list(sample_fmts);
112 return AVERROR(ENOMEM);
113 ret = ff_set_common_formats(ctx, formats);
117 formats = ff_all_samplerates();
119 return AVERROR(ENOMEM);
120 return ff_set_common_samplerates(ctx, formats);
123 #define IIR_CH(name, type, min, max, need_clipping) \
124 static int iir_ch_## name(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) \
126 AudioIIRContext *s = ctx->priv; \
127 const double ig = s->dry_gain; \
128 const double og = s->wet_gain; \
129 const double mix = s->mix; \
130 ThreadData *td = arg; \
131 AVFrame *in = td->in, *out = td->out; \
132 const type *src = (const type *)in->extended_data[ch]; \
133 double *oc = (double *)s->iir[ch].cache[0]; \
134 double *ic = (double *)s->iir[ch].cache[1]; \
135 const int nb_a = s->iir[ch].nb_ab[0]; \
136 const int nb_b = s->iir[ch].nb_ab[1]; \
137 const double *a = s->iir[ch].ab[0]; \
138 const double *b = s->iir[ch].ab[1]; \
139 const double g = s->iir[ch].g; \
140 int *clippings = &s->iir[ch].clippings; \
141 type *dst = (type *)out->extended_data[ch]; \
144 for (n = 0; n < in->nb_samples; n++) { \
145 double sample = 0.; \
148 memmove(&ic[1], &ic[0], (nb_b - 1) * sizeof(*ic)); \
149 memmove(&oc[1], &oc[0], (nb_a - 1) * sizeof(*oc)); \
150 ic[0] = src[n] * ig; \
151 for (x = 0; x < nb_b; x++) \
152 sample += b[x] * ic[x]; \
154 for (x = 1; x < nb_a; x++) \
155 sample -= a[x] * oc[x]; \
159 sample = sample * mix + ic[0] * (1. - mix); \
160 if (need_clipping && sample < min) { \
163 } else if (need_clipping && sample > max) { \
174 IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
175 IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
176 IIR_CH(fltp, float, -1., 1., 0)
177 IIR_CH(dblp, double, -1., 1., 0)
179 #define SERIAL_IIR_CH(name, type, min, max, need_clipping) \
180 static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) \
182 AudioIIRContext *s = ctx->priv; \
183 const double ig = s->dry_gain; \
184 const double og = s->wet_gain; \
185 const double mix = s->mix; \
186 ThreadData *td = arg; \
187 AVFrame *in = td->in, *out = td->out; \
188 const type *src = (const type *)in->extended_data[ch]; \
189 type *dst = (type *)out->extended_data[ch]; \
190 IIRChannel *iir = &s->iir[ch]; \
191 const double g = iir->g; \
192 int *clippings = &iir->clippings; \
193 int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; \
196 for (i = 0; i < nb_biquads; i++) { \
197 const double a1 = -iir->biquads[i].a[1]; \
198 const double a2 = -iir->biquads[i].a[2]; \
199 const double b0 = iir->biquads[i].b[0]; \
200 const double b1 = iir->biquads[i].b[1]; \
201 const double b2 = iir->biquads[i].b[2]; \
202 double i1 = iir->biquads[i].i1; \
203 double i2 = iir->biquads[i].i2; \
204 double o1 = iir->biquads[i].o1; \
205 double o2 = iir->biquads[i].o2; \
207 for (n = 0; n < in->nb_samples; n++) { \
208 double sample = ig * (i ? dst[n] : src[n]); \
209 double o0 = sample * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
217 o0 = o0 * mix + (1. - mix) * sample; \
218 if (need_clipping && o0 < min) { \
221 } else if (need_clipping && o0 > max) { \
228 iir->biquads[i].i1 = i1; \
229 iir->biquads[i].i2 = i2; \
230 iir->biquads[i].o1 = o1; \
231 iir->biquads[i].o2 = o2; \
237 SERIAL_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
238 SERIAL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
239 SERIAL_IIR_CH(fltp, float, -1., 1., 0)
240 SERIAL_IIR_CH(dblp, double, -1., 1., 0)
242 static void count_coefficients(char *item_str, int *nb_items)
250 for (p = item_str; *p && *p != '|'; p++) {
256 static int read_gains(AVFilterContext *ctx, char *item_str, int nb_items)
258 AudioIIRContext *s = ctx->priv;
259 char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
262 p = old_str = av_strdup(item_str);
264 return AVERROR(ENOMEM);
265 for (i = 0; i < nb_items; i++) {
266 if (!(arg = av_strtok(p, "|", &saveptr)))
271 return AVERROR(EINVAL);
275 if (sscanf(arg, "%lf", &s->iir[i].g) != 1) {
276 av_log(ctx, AV_LOG_ERROR, "Invalid gains supplied: %s\n", arg);
278 return AVERROR(EINVAL);
289 static int read_tf_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst)
291 char *p, *arg, *old_str, *saveptr = NULL;
294 p = old_str = av_strdup(item_str);
296 return AVERROR(ENOMEM);
297 for (i = 0; i < nb_items; i++) {
298 if (!(arg = av_strtok(p, " ", &saveptr)))
302 if (sscanf(arg, "%lf", &dst[i]) != 1) {
303 av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
305 return AVERROR(EINVAL);
314 static int read_zp_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst, const char *format)
316 char *p, *arg, *old_str, *saveptr = NULL;
319 p = old_str = av_strdup(item_str);
321 return AVERROR(ENOMEM);
322 for (i = 0; i < nb_items; i++) {
323 if (!(arg = av_strtok(p, " ", &saveptr)))
327 if (sscanf(arg, format, &dst[i*2], &dst[i*2+1]) != 2) {
328 av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
330 return AVERROR(EINVAL);
339 static const char *format[] = { "%lf", "%lf %lfi", "%lf %lfr", "%lf %lfd", "%lf %lfi" };
341 static int read_channels(AVFilterContext *ctx, int channels, uint8_t *item_str, int ab)
343 AudioIIRContext *s = ctx->priv;
344 char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
347 p = old_str = av_strdup(item_str);
349 return AVERROR(ENOMEM);
350 for (i = 0; i < channels; i++) {
351 IIRChannel *iir = &s->iir[i];
353 if (!(arg = av_strtok(p, "|", &saveptr)))
358 return AVERROR(EINVAL);
361 count_coefficients(arg, &iir->nb_ab[ab]);
364 iir->cache[ab] = av_calloc(iir->nb_ab[ab] + 1, sizeof(double));
365 iir->ab[ab] = av_calloc(iir->nb_ab[ab] * (!!s->format + 1), sizeof(double));
366 if (!iir->ab[ab] || !iir->cache[ab]) {
368 return AVERROR(ENOMEM);
372 ret = read_zp_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab], format[s->format]);
374 ret = read_tf_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab]);
388 static void multiply(double wre, double wim, int npz, double *coeffs)
390 double nwre = -wre, nwim = -wim;
394 for (i = npz; i >= 1; i--) {
395 cre = coeffs[2 * i + 0];
396 cim = coeffs[2 * i + 1];
398 coeffs[2 * i + 0] = (nwre * cre - nwim * cim) + coeffs[2 * (i - 1) + 0];
399 coeffs[2 * i + 1] = (nwre * cim + nwim * cre) + coeffs[2 * (i - 1) + 1];
404 coeffs[0] = nwre * cre - nwim * cim;
405 coeffs[1] = nwre * cim + nwim * cre;
408 static int expand(AVFilterContext *ctx, double *pz, int nb, double *coeffs)
415 for (i = 0; i < nb; i++) {
416 coeffs[2 * (i + 1) ] = 0.0;
417 coeffs[2 * (i + 1) + 1] = 0.0;
420 for (i = 0; i < nb; i++)
421 multiply(pz[2 * i], pz[2 * i + 1], nb, coeffs);
423 for (i = 0; i < nb + 1; i++) {
424 if (fabs(coeffs[2 * i + 1]) > FLT_EPSILON) {
425 av_log(ctx, AV_LOG_ERROR, "coeff: %f of z^%d is not real; poles/zeros are not complex conjugates.\n",
426 coeffs[2 * i + 1], i);
427 return AVERROR(EINVAL);
434 static void normalize_coeffs(AVFilterContext *ctx, int ch)
436 AudioIIRContext *s = ctx->priv;
437 IIRChannel *iir = &s->iir[ch];
443 for (int i = 0; i < iir->nb_ab[1]; i++) {
444 sum_den += iir->ab[1][i];
447 if (sum_den > 1e-6) {
448 double factor, sum_num = 0.;
450 for (int i = 0; i < iir->nb_ab[0]; i++) {
451 sum_num += iir->ab[0][i];
454 factor = sum_num / sum_den;
456 for (int i = 0; i < iir->nb_ab[1]; i++) {
457 iir->ab[1][i] *= factor;
462 static int convert_zp2tf(AVFilterContext *ctx, int channels)
464 AudioIIRContext *s = ctx->priv;
465 int ch, i, j, ret = 0;
467 for (ch = 0; ch < channels; ch++) {
468 IIRChannel *iir = &s->iir[ch];
471 topc = av_calloc((iir->nb_ab[0] + 1) * 2, sizeof(*topc));
472 botc = av_calloc((iir->nb_ab[1] + 1) * 2, sizeof(*botc));
473 if (!topc || !botc) {
474 ret = AVERROR(ENOMEM);
478 ret = expand(ctx, iir->ab[0], iir->nb_ab[0], botc);
483 ret = expand(ctx, iir->ab[1], iir->nb_ab[1], topc);
488 for (j = 0, i = iir->nb_ab[1]; i >= 0; j++, i--) {
489 iir->ab[1][j] = topc[2 * i];
493 for (j = 0, i = iir->nb_ab[0]; i >= 0; j++, i--) {
494 iir->ab[0][j] = botc[2 * i];
498 normalize_coeffs(ctx, ch);
510 static int decompose_zp2biquads(AVFilterContext *ctx, int channels)
512 AudioIIRContext *s = ctx->priv;
515 for (ch = 0; ch < channels; ch++) {
516 IIRChannel *iir = &s->iir[ch];
517 int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2;
518 int current_biquad = 0;
520 iir->biquads = av_calloc(nb_biquads, sizeof(BiquadContext));
522 return AVERROR(ENOMEM);
524 while (nb_biquads--) {
525 Pair outmost_pole = { -1, -1 };
526 Pair nearest_zero = { -1, -1 };
527 double zeros[4] = { 0 };
528 double poles[4] = { 0 };
531 double min_distance = DBL_MAX;
536 for (i = 0; i < iir->nb_ab[0]; i++) {
539 if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
541 mag = hypot(iir->ab[0][2 * i], iir->ab[0][2 * i + 1]);
549 for (i = 0; i < iir->nb_ab[0]; i++) {
550 if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
553 if (iir->ab[0][2 * i ] == iir->ab[0][2 * outmost_pole.a ] &&
554 iir->ab[0][2 * i + 1] == -iir->ab[0][2 * outmost_pole.a + 1]) {
560 av_log(ctx, AV_LOG_VERBOSE, "outmost_pole is %d.%d\n", outmost_pole.a, outmost_pole.b);
562 if (outmost_pole.a < 0 || outmost_pole.b < 0)
563 return AVERROR(EINVAL);
565 for (i = 0; i < iir->nb_ab[1]; i++) {
568 if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
570 distance = hypot(iir->ab[0][2 * outmost_pole.a ] - iir->ab[1][2 * i ],
571 iir->ab[0][2 * outmost_pole.a + 1] - iir->ab[1][2 * i + 1]);
573 if (distance < min_distance) {
574 min_distance = distance;
579 for (i = 0; i < iir->nb_ab[1]; i++) {
580 if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
583 if (iir->ab[1][2 * i ] == iir->ab[1][2 * nearest_zero.a ] &&
584 iir->ab[1][2 * i + 1] == -iir->ab[1][2 * nearest_zero.a + 1]) {
590 av_log(ctx, AV_LOG_VERBOSE, "nearest_zero is %d.%d\n", nearest_zero.a, nearest_zero.b);
592 if (nearest_zero.a < 0 || nearest_zero.b < 0)
593 return AVERROR(EINVAL);
595 poles[0] = iir->ab[0][2 * outmost_pole.a ];
596 poles[1] = iir->ab[0][2 * outmost_pole.a + 1];
598 zeros[0] = iir->ab[1][2 * nearest_zero.a ];
599 zeros[1] = iir->ab[1][2 * nearest_zero.a + 1];
601 if (nearest_zero.a == nearest_zero.b && outmost_pole.a == outmost_pole.b) {
608 poles[2] = iir->ab[0][2 * outmost_pole.b ];
609 poles[3] = iir->ab[0][2 * outmost_pole.b + 1];
611 zeros[2] = iir->ab[1][2 * nearest_zero.b ];
612 zeros[3] = iir->ab[1][2 * nearest_zero.b + 1];
615 ret = expand(ctx, zeros, 2, b);
619 ret = expand(ctx, poles, 2, a);
623 iir->ab[0][2 * outmost_pole.a] = iir->ab[0][2 * outmost_pole.a + 1] = NAN;
624 iir->ab[0][2 * outmost_pole.b] = iir->ab[0][2 * outmost_pole.b + 1] = NAN;
625 iir->ab[1][2 * nearest_zero.a] = iir->ab[1][2 * nearest_zero.a + 1] = NAN;
626 iir->ab[1][2 * nearest_zero.b] = iir->ab[1][2 * nearest_zero.b + 1] = NAN;
628 iir->biquads[current_biquad].a[0] = 1.;
629 iir->biquads[current_biquad].a[1] = a[2] / a[4];
630 iir->biquads[current_biquad].a[2] = a[0] / a[4];
631 iir->biquads[current_biquad].b[0] = b[4] / a[4];
632 iir->biquads[current_biquad].b[1] = b[2] / a[4];
633 iir->biquads[current_biquad].b[2] = b[0] / a[4];
636 fabs(iir->biquads[current_biquad].b[0] +
637 iir->biquads[current_biquad].b[1] +
638 iir->biquads[current_biquad].b[2]) > 1e-6) {
639 factor = (iir->biquads[current_biquad].a[0] +
640 iir->biquads[current_biquad].a[1] +
641 iir->biquads[current_biquad].a[2]) /
642 (iir->biquads[current_biquad].b[0] +
643 iir->biquads[current_biquad].b[1] +
644 iir->biquads[current_biquad].b[2]);
646 av_log(ctx, AV_LOG_VERBOSE, "factor=%f\n", factor);
648 iir->biquads[current_biquad].b[0] *= factor;
649 iir->biquads[current_biquad].b[1] *= factor;
650 iir->biquads[current_biquad].b[2] *= factor;
653 iir->biquads[current_biquad].b[0] *= (current_biquad ? 1.0 : iir->g);
654 iir->biquads[current_biquad].b[1] *= (current_biquad ? 1.0 : iir->g);
655 iir->biquads[current_biquad].b[2] *= (current_biquad ? 1.0 : iir->g);
657 av_log(ctx, AV_LOG_VERBOSE, "a=%f %f %f:b=%f %f %f\n",
658 iir->biquads[current_biquad].a[0],
659 iir->biquads[current_biquad].a[1],
660 iir->biquads[current_biquad].a[2],
661 iir->biquads[current_biquad].b[0],
662 iir->biquads[current_biquad].b[1],
663 iir->biquads[current_biquad].b[2]);
672 static void convert_pr2zp(AVFilterContext *ctx, int channels)
674 AudioIIRContext *s = ctx->priv;
677 for (ch = 0; ch < channels; ch++) {
678 IIRChannel *iir = &s->iir[ch];
681 for (n = 0; n < iir->nb_ab[0]; n++) {
682 double r = iir->ab[0][2*n];
683 double angle = iir->ab[0][2*n+1];
685 iir->ab[0][2*n] = r * cos(angle);
686 iir->ab[0][2*n+1] = r * sin(angle);
689 for (n = 0; n < iir->nb_ab[1]; n++) {
690 double r = iir->ab[1][2*n];
691 double angle = iir->ab[1][2*n+1];
693 iir->ab[1][2*n] = r * cos(angle);
694 iir->ab[1][2*n+1] = r * sin(angle);
699 static void convert_sp2zp(AVFilterContext *ctx, int channels)
701 AudioIIRContext *s = ctx->priv;
704 for (ch = 0; ch < channels; ch++) {
705 IIRChannel *iir = &s->iir[ch];
708 for (n = 0; n < iir->nb_ab[0]; n++) {
709 double sr = iir->ab[0][2*n];
710 double si = iir->ab[0][2*n+1];
711 double snr = 1. + sr;
712 double sdr = 1. - sr;
713 double div = sdr * sdr + si * si;
715 iir->ab[0][2*n] = (snr * sdr - si * si) / div;
716 iir->ab[0][2*n+1] = (sdr * si + snr * si) / div;
719 for (n = 0; n < iir->nb_ab[1]; n++) {
720 double sr = iir->ab[1][2*n];
721 double si = iir->ab[1][2*n+1];
722 double snr = 1. + sr;
723 double sdr = 1. - sr;
724 double div = sdr * sdr + si * si;
726 iir->ab[1][2*n] = (snr * sdr - si * si) / div;
727 iir->ab[1][2*n+1] = (sdr * si + snr * si) / div;
732 static void convert_pd2zp(AVFilterContext *ctx, int channels)
734 AudioIIRContext *s = ctx->priv;
737 for (ch = 0; ch < channels; ch++) {
738 IIRChannel *iir = &s->iir[ch];
741 for (n = 0; n < iir->nb_ab[0]; n++) {
742 double r = iir->ab[0][2*n];
743 double angle = M_PI*iir->ab[0][2*n+1]/180.;
745 iir->ab[0][2*n] = r * cos(angle);
746 iir->ab[0][2*n+1] = r * sin(angle);
749 for (n = 0; n < iir->nb_ab[1]; n++) {
750 double r = iir->ab[1][2*n];
751 double angle = M_PI*iir->ab[1][2*n+1]/180.;
753 iir->ab[1][2*n] = r * cos(angle);
754 iir->ab[1][2*n+1] = r * sin(angle);
759 static void check_stability(AVFilterContext *ctx, int channels)
761 AudioIIRContext *s = ctx->priv;
764 for (ch = 0; ch < channels; ch++) {
765 IIRChannel *iir = &s->iir[ch];
767 for (int n = 0; n < iir->nb_ab[0]; n++) {
768 double pr = hypot(iir->ab[0][2*n], iir->ab[0][2*n+1]);
771 av_log(ctx, AV_LOG_WARNING, "pole %d at channel %d is unstable\n", n, ch);
778 static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint32_t color)
784 font = avpriv_cga_font, font_height = 8;
786 for (i = 0; txt[i]; i++) {
789 uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
790 for (char_y = 0; char_y < font_height; char_y++) {
791 for (mask = 0x80; mask; mask >>= 1) {
792 if (font[txt[i] * font_height + char_y] & mask)
796 p += pic->linesize[0] - 8 * 4;
801 static void draw_line(AVFrame *out, int x0, int y0, int x1, int y1, uint32_t color)
803 int dx = FFABS(x1-x0);
804 int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
805 int err = (dx>dy ? dx : -dy) / 2, e2;
808 AV_WL32(out->data[0] + y0 * out->linesize[0] + x0 * 4, color);
810 if (x0 == x1 && y0 == y1)
827 static double distance(double x0, double x1, double y0, double y1)
829 return hypot(x0 - x1, y0 - y1);
832 static void get_response(int channel, int format, double w,
833 const double *b, const double *a,
834 int nb_b, int nb_a, double *magnitude, double *phase)
842 realz = 0., realp = 0.;
843 imagz = 0., imagp = 0.;
844 for (int x = 0; x < nb_a; x++) {
845 realz += cos(-x * w) * a[x];
846 imagz += sin(-x * w) * a[x];
849 for (int x = 0; x < nb_b; x++) {
850 realp += cos(-x * w) * b[x];
851 imagp += sin(-x * w) * b[x];
854 div = realp * realp + imagp * imagp;
855 real = (realz * realp + imagz * imagp) / div;
856 imag = (imagz * realp - imagp * realz) / div;
858 *magnitude = hypot(real, imag);
859 *phase = atan2(imag, real);
861 double p = 1., z = 1.;
864 for (int x = 0; x < nb_a; x++) {
865 z *= distance(cos(w), a[2 * x], sin(w), a[2 * x + 1]);
866 acc += atan2(sin(w) - a[2 * x + 1], cos(w) - a[2 * x]);
869 for (int x = 0; x < nb_b; x++) {
870 p *= distance(cos(w), b[2 * x], sin(w), b[2 * x + 1]);
871 acc -= atan2(sin(w) - b[2 * x + 1], cos(w) - b[2 * x]);
879 static void draw_response(AVFilterContext *ctx, AVFrame *out, int sample_rate)
881 AudioIIRContext *s = ctx->priv;
882 double *mag, *phase, *temp, *delay, min = DBL_MAX, max = -DBL_MAX;
883 double min_delay = DBL_MAX, max_delay = -DBL_MAX, min_phase, max_phase;
884 int prev_ymag = -1, prev_yphase = -1, prev_ydelay = -1;
888 memset(out->data[0], 0, s->h * out->linesize[0]);
890 phase = av_malloc_array(s->w, sizeof(*phase));
891 temp = av_malloc_array(s->w, sizeof(*temp));
892 mag = av_malloc_array(s->w, sizeof(*mag));
893 delay = av_malloc_array(s->w, sizeof(*delay));
894 if (!mag || !phase || !delay || !temp)
897 ch = av_clip(s->ir_channel, 0, s->channels - 1);
898 for (i = 0; i < s->w; i++) {
899 const double *b = s->iir[ch].ab[0];
900 const double *a = s->iir[ch].ab[1];
901 const int nb_b = s->iir[ch].nb_ab[0];
902 const int nb_a = s->iir[ch].nb_ab[1];
903 double w = i * M_PI / (s->w - 1);
906 get_response(ch, s->format, w, b, a, nb_b, nb_a, &m, &p);
908 mag[i] = s->iir[ch].g * m;
910 min = fmin(min, mag[i]);
911 max = fmax(max, mag[i]);
915 for (i = 0; i < s->w - 1; i++) {
916 double d = phase[i] - phase[i + 1];
917 temp[i + 1] = ceil(fabs(d) / (2. * M_PI)) * 2. * M_PI * ((d > M_PI) - (d < -M_PI));
920 min_phase = phase[0];
921 max_phase = phase[0];
922 for (i = 1; i < s->w; i++) {
923 temp[i] += temp[i - 1];
925 min_phase = fmin(min_phase, phase[i]);
926 max_phase = fmax(max_phase, phase[i]);
929 for (i = 0; i < s->w - 1; i++) {
930 double div = s->w / (double)sample_rate;
932 delay[i + 1] = -(phase[i] - phase[i + 1]) / div;
933 min_delay = fmin(min_delay, delay[i + 1]);
934 max_delay = fmax(max_delay, delay[i + 1]);
938 for (i = 0; i < s->w; i++) {
939 int ymag = mag[i] / max * (s->h - 1);
940 int ydelay = (delay[i] - min_delay) / (max_delay - min_delay) * (s->h - 1);
941 int yphase = (phase[i] - min_phase) / (max_phase - min_phase) * (s->h - 1);
943 ymag = s->h - 1 - av_clip(ymag, 0, s->h - 1);
944 yphase = s->h - 1 - av_clip(yphase, 0, s->h - 1);
945 ydelay = s->h - 1 - av_clip(ydelay, 0, s->h - 1);
950 prev_yphase = yphase;
952 prev_ydelay = ydelay;
954 draw_line(out, i, ymag, FFMAX(i - 1, 0), prev_ymag, 0xFFFF00FF);
955 draw_line(out, i, yphase, FFMAX(i - 1, 0), prev_yphase, 0xFF00FF00);
956 draw_line(out, i, ydelay, FFMAX(i - 1, 0), prev_ydelay, 0xFF00FFFF);
959 prev_yphase = yphase;
960 prev_ydelay = ydelay;
963 if (s->w > 400 && s->h > 100) {
964 drawtext(out, 2, 2, "Max Magnitude:", 0xDDDDDDDD);
965 snprintf(text, sizeof(text), "%.2f", max);
966 drawtext(out, 15 * 8 + 2, 2, text, 0xDDDDDDDD);
968 drawtext(out, 2, 12, "Min Magnitude:", 0xDDDDDDDD);
969 snprintf(text, sizeof(text), "%.2f", min);
970 drawtext(out, 15 * 8 + 2, 12, text, 0xDDDDDDDD);
972 drawtext(out, 2, 22, "Max Phase:", 0xDDDDDDDD);
973 snprintf(text, sizeof(text), "%.2f", max_phase);
974 drawtext(out, 15 * 8 + 2, 22, text, 0xDDDDDDDD);
976 drawtext(out, 2, 32, "Min Phase:", 0xDDDDDDDD);
977 snprintf(text, sizeof(text), "%.2f", min_phase);
978 drawtext(out, 15 * 8 + 2, 32, text, 0xDDDDDDDD);
980 drawtext(out, 2, 42, "Max Delay:", 0xDDDDDDDD);
981 snprintf(text, sizeof(text), "%.2f", max_delay);
982 drawtext(out, 11 * 8 + 2, 42, text, 0xDDDDDDDD);
984 drawtext(out, 2, 52, "Min Delay:", 0xDDDDDDDD);
985 snprintf(text, sizeof(text), "%.2f", min_delay);
986 drawtext(out, 11 * 8 + 2, 52, text, 0xDDDDDDDD);
996 static int config_output(AVFilterLink *outlink)
998 AVFilterContext *ctx = outlink->src;
999 AudioIIRContext *s = ctx->priv;
1000 AVFilterLink *inlink = ctx->inputs[0];
1003 s->channels = inlink->channels;
1004 s->iir = av_calloc(s->channels, sizeof(*s->iir));
1006 return AVERROR(ENOMEM);
1008 ret = read_gains(ctx, s->g_str, inlink->channels);
1012 ret = read_channels(ctx, inlink->channels, s->a_str, 0);
1016 ret = read_channels(ctx, inlink->channels, s->b_str, 1);
1020 if (s->format == 2) {
1021 convert_pr2zp(ctx, inlink->channels);
1022 } else if (s->format == 3) {
1023 convert_pd2zp(ctx, inlink->channels);
1024 } else if (s->format == 4) {
1025 convert_sp2zp(ctx, inlink->channels);
1027 if (s->format > 0) {
1028 check_stability(ctx, inlink->channels);
1032 av_log(ctx, AV_LOG_WARNING, "tf coefficients format is not recommended for too high number of zeros/poles.\n");
1034 if (s->format > 0 && s->process == 0) {
1035 av_log(ctx, AV_LOG_WARNING, "Direct processsing is not recommended for zp coefficients format.\n");
1037 ret = convert_zp2tf(ctx, inlink->channels);
1040 } else if (s->format == 0 && s->process == 1) {
1041 av_log(ctx, AV_LOG_ERROR, "Serial cascading is not implemented for transfer function.\n");
1042 return AVERROR_PATCHWELCOME;
1043 } else if (s->format > 0 && s->process == 1) {
1044 if (inlink->format == AV_SAMPLE_FMT_S16P)
1045 av_log(ctx, AV_LOG_WARNING, "Serial cascading is not recommended for i16 precision.\n");
1047 ret = decompose_zp2biquads(ctx, inlink->channels);
1052 for (ch = 0; s->format == 0 && ch < inlink->channels; ch++) {
1053 IIRChannel *iir = &s->iir[ch];
1055 for (i = 1; i < iir->nb_ab[0]; i++) {
1056 iir->ab[0][i] /= iir->ab[0][0];
1059 iir->ab[0][0] = 1.0;
1060 for (i = 0; i < iir->nb_ab[1]; i++) {
1061 iir->ab[1][i] *= iir->g;
1064 normalize_coeffs(ctx, ch);
1067 switch (inlink->format) {
1068 case AV_SAMPLE_FMT_DBLP: s->iir_channel = s->process == 1 ? iir_ch_serial_dblp : iir_ch_dblp; break;
1069 case AV_SAMPLE_FMT_FLTP: s->iir_channel = s->process == 1 ? iir_ch_serial_fltp : iir_ch_fltp; break;
1070 case AV_SAMPLE_FMT_S32P: s->iir_channel = s->process == 1 ? iir_ch_serial_s32p : iir_ch_s32p; break;
1071 case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break;
1074 av_frame_free(&s->video);
1076 s->video = ff_get_video_buffer(ctx->outputs[1], s->w, s->h);
1078 return AVERROR(ENOMEM);
1080 draw_response(ctx, s->video, inlink->sample_rate);
1086 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
1088 AVFilterContext *ctx = inlink->dst;
1089 AudioIIRContext *s = ctx->priv;
1090 AVFilterLink *outlink = ctx->outputs[0];
1095 if (av_frame_is_writable(in)) {
1098 out = ff_get_audio_buffer(outlink, in->nb_samples);
1101 return AVERROR(ENOMEM);
1103 av_frame_copy_props(out, in);
1108 ctx->internal->execute(ctx, s->iir_channel, &td, NULL, outlink->channels);
1110 for (ch = 0; ch < outlink->channels; ch++) {
1111 if (s->iir[ch].clippings > 0)
1112 av_log(ctx, AV_LOG_WARNING, "Channel %d clipping %d times. Please reduce gain.\n",
1113 ch, s->iir[ch].clippings);
1114 s->iir[ch].clippings = 0;
1121 AVFilterLink *outlink = ctx->outputs[1];
1122 int64_t old_pts = s->video->pts;
1123 int64_t new_pts = av_rescale_q(out->pts, ctx->inputs[0]->time_base, outlink->time_base);
1125 if (new_pts > old_pts) {
1128 s->video->pts = new_pts;
1129 clone = av_frame_clone(s->video);
1131 return AVERROR(ENOMEM);
1132 ret = ff_filter_frame(outlink, clone);
1138 return ff_filter_frame(outlink, out);
1141 static int config_video(AVFilterLink *outlink)
1143 AVFilterContext *ctx = outlink->src;
1144 AudioIIRContext *s = ctx->priv;
1146 outlink->sample_aspect_ratio = (AVRational){1,1};
1149 outlink->frame_rate = s->rate;
1150 outlink->time_base = av_inv_q(outlink->frame_rate);
1155 static av_cold int init(AVFilterContext *ctx)
1157 AudioIIRContext *s = ctx->priv;
1158 AVFilterPad pad, vpad;
1161 if (!s->a_str || !s->b_str || !s->g_str) {
1162 av_log(ctx, AV_LOG_ERROR, "Valid coefficients are mandatory.\n");
1163 return AVERROR(EINVAL);
1166 switch (s->precision) {
1167 case 0: s->sample_format = AV_SAMPLE_FMT_DBLP; break;
1168 case 1: s->sample_format = AV_SAMPLE_FMT_FLTP; break;
1169 case 2: s->sample_format = AV_SAMPLE_FMT_S32P; break;
1170 case 3: s->sample_format = AV_SAMPLE_FMT_S16P; break;
1171 default: return AVERROR_BUG;
1174 pad = (AVFilterPad){
1175 .name = av_strdup("default"),
1176 .type = AVMEDIA_TYPE_AUDIO,
1177 .config_props = config_output,
1181 return AVERROR(ENOMEM);
1184 vpad = (AVFilterPad){
1185 .name = av_strdup("filter_response"),
1186 .type = AVMEDIA_TYPE_VIDEO,
1187 .config_props = config_video,
1190 return AVERROR(ENOMEM);
1193 ret = ff_insert_outpad(ctx, 0, &pad);
1198 ret = ff_insert_outpad(ctx, 1, &vpad);
1206 static av_cold void uninit(AVFilterContext *ctx)
1208 AudioIIRContext *s = ctx->priv;
1212 for (ch = 0; ch < s->channels; ch++) {
1213 IIRChannel *iir = &s->iir[ch];
1214 av_freep(&iir->ab[0]);
1215 av_freep(&iir->ab[1]);
1216 av_freep(&iir->cache[0]);
1217 av_freep(&iir->cache[1]);
1218 av_freep(&iir->biquads);
1223 av_freep(&ctx->output_pads[0].name);
1225 av_freep(&ctx->output_pads[1].name);
1226 av_frame_free(&s->video);
1229 static const AVFilterPad inputs[] = {
1232 .type = AVMEDIA_TYPE_AUDIO,
1233 .filter_frame = filter_frame,
1238 #define OFFSET(x) offsetof(AudioIIRContext, x)
1239 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1240 #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1242 static const AVOption aiir_options[] = {
1243 { "zeros", "set B/numerator/zeros coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
1244 { "z", "set B/numerator/zeros coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
1245 { "poles", "set A/denominator/poles coefficients", OFFSET(a_str),AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
1246 { "p", "set A/denominator/poles coefficients", OFFSET(a_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
1247 { "gains", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
1248 { "k", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
1249 { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
1250 { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
1251 { "format", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, 0, 4, AF, "format" },
1252 { "f", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, 0, 4, AF, "format" },
1253 { "tf", "digital transfer function", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "format" },
1254 { "zp", "Z-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "format" },
1255 { "pr", "Z-plane zeros/poles (polar radians)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "format" },
1256 { "pd", "Z-plane zeros/poles (polar degrees)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "format" },
1257 { "sp", "S-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "format" },
1258 { "process", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" },
1259 { "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" },
1260 { "d", "direct", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "process" },
1261 { "s", "serial cascading", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "process" },
1262 { "precision", "set filtering precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
1263 { "e", "set precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
1264 { "dbl", "double-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" },
1265 { "flt", "single-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "precision" },
1266 { "i32", "32-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "precision" },
1267 { "i16", "16-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "precision" },
1268 { "normalize", "normalize coefficients", OFFSET(normalize),AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
1269 { "n", "normalize coefficients", OFFSET(normalize),AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
1270 { "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
1271 { "response", "show IR frequency response", OFFSET(response), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
1272 { "channel", "set IR channel to display frequency response", OFFSET(ir_channel), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, VF },
1273 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, VF },
1274 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT32_MAX, VF },
1278 AVFILTER_DEFINE_CLASS(aiir);
1280 AVFilter ff_af_aiir = {
1282 .description = NULL_IF_CONFIG_SMALL("Apply Infinite Impulse Response filter with supplied coefficients."),
1283 .priv_size = sizeof(AudioIIRContext),
1284 .priv_class = &aiir_class,
1287 .query_formats = query_formats,
1289 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
1290 AVFILTER_FLAG_SLICE_THREADS,