+static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint32_t color)
+{
+ const uint8_t *font;
+ int font_height;
+ int i;
+
+ font = avpriv_cga_font, font_height = 8;
+
+ for (i = 0; txt[i]; i++) {
+ int char_y, mask;
+
+ uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
+ for (char_y = 0; char_y < font_height; char_y++) {
+ for (mask = 0x80; mask; mask >>= 1) {
+ if (font[txt[i] * font_height + char_y] & mask)
+ AV_WL32(p, color);
+ p += 4;
+ }
+ p += pic->linesize[0] - 8 * 4;
+ }
+ }
+}
+
+static void draw_line(AVFrame *out, int x0, int y0, int x1, int y1, uint32_t color)
+{
+ int dx = FFABS(x1-x0);
+ int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
+ int err = (dx>dy ? dx : -dy) / 2, e2;
+
+ for (;;) {
+ AV_WL32(out->data[0] + y0 * out->linesize[0] + x0 * 4, color);
+
+ if (x0 == x1 && y0 == y1)
+ break;
+
+ e2 = err;
+
+ if (e2 >-dx) {
+ err -= dy;
+ x0--;
+ }
+
+ if (e2 < dy) {
+ err += dx;
+ y0 += sy;
+ }
+ }
+}
+
+static void draw_response(AVFilterContext *ctx, AVFrame *out)
+{
+ AudioIIRContext *s = ctx->priv;
+ float *mag, *phase, min = FLT_MAX, max = FLT_MIN;
+ int prev_ymag = -1, prev_yphase = -1;
+ char text[32];
+ int ch, i, x;
+
+ memset(out->data[0], 0, s->h * out->linesize[0]);
+
+ phase = av_malloc_array(s->w, sizeof(*phase));
+ mag = av_malloc_array(s->w, sizeof(*mag));
+ if (!mag || !phase)
+ goto end;
+
+ ch = av_clip(s->ir_channel, 0, s->channels - 1);
+ for (i = 0; i < s->w; i++) {
+ const double *b = s->iir[ch].ab[0];
+ const double *a = s->iir[ch].ab[1];
+ double w = i * M_PI / (s->w - 1);
+ double realz, realp;
+ double imagz, imagp;
+ double real, imag, div;
+
+ if (s->format == 0) {
+ realz = 0., realp = 0.;
+ imagz = 0., imagp = 0.;
+ for (x = 0; x < s->iir[ch].nb_ab[1]; x++) {
+ realz += cos(-x * w) * a[x];
+ imagz += sin(-x * w) * a[x];
+ }
+
+ for (x = 0; x < s->iir[ch].nb_ab[0]; x++) {
+ realp += cos(-x * w) * b[x];
+ imagp += sin(-x * w) * b[x];
+ }
+
+ div = realp * realp + imagp * imagp;
+ real = (realz * realp + imagz * imagp) / div;
+ imag = (imagz * realp - imagp * realz) / div;
+ } else {
+ real = 1;
+ imag = 0;
+ for (x = 0; x < s->iir[ch].nb_ab[1]; x++) {
+ double ore, oim, re, im;
+
+ re = cos(w) - a[2 * x];
+ im = sin(w) - a[2 * x + 1];
+
+ ore = real;
+ oim = imag;
+
+ real = ore * re - oim * im;
+ imag = ore * im + oim * re;
+ }
+
+ for (x = 0; x < s->iir[ch].nb_ab[0]; x++) {
+ double ore, oim, re, im;
+
+ re = cos(w) - b[2 * x];
+ im = sin(w) - b[2 * x + 1];
+
+ ore = real;
+ oim = imag;
+ div = re * re + im * im;
+
+ real = (ore * re + oim * im) / div;
+ imag = (oim * re - ore * im) / div;
+ }
+ }
+
+ mag[i] = s->iir[ch].g * hypot(real, imag);
+ phase[i] = atan2(imag, real);
+ min = fminf(min, mag[i]);
+ max = fmaxf(max, mag[i]);
+ }
+
+ for (i = 0; i < s->w; i++) {
+ int ymag = mag[i] / max * (s->h - 1);
+ int yphase = (0.5 * (1. + phase[i] / M_PI)) * (s->h - 1);
+
+ ymag = s->h - 1 - av_clip(ymag, 0, s->h - 1);
+ yphase = s->h - 1 - av_clip(yphase, 0, s->h - 1);
+
+ if (prev_ymag < 0)
+ prev_ymag = ymag;
+ if (prev_yphase < 0)
+ prev_yphase = yphase;
+
+ draw_line(out, i, ymag, FFMAX(i - 1, 0), prev_ymag, 0xFFFF00FF);
+ draw_line(out, i, yphase, FFMAX(i - 1, 0), prev_yphase, 0xFF00FF00);
+
+ prev_ymag = ymag;
+ prev_yphase = yphase;
+ }
+
+ if (s->w > 400 && s->h > 100) {
+ drawtext(out, 2, 2, "Max Magnitude:", 0xDDDDDDDD);
+ snprintf(text, sizeof(text), "%.2f", max);
+ drawtext(out, 15 * 8 + 2, 2, text, 0xDDDDDDDD);
+
+ drawtext(out, 2, 12, "Min Magnitude:", 0xDDDDDDDD);
+ snprintf(text, sizeof(text), "%.2f", min);
+ drawtext(out, 15 * 8 + 2, 12, text, 0xDDDDDDDD);
+ }
+
+end:
+ av_free(phase);
+ av_free(mag);
+}
+