+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)
+{
+ AudioFIRContext *s = ctx->priv;
+ float *mag, *phase, min = FLT_MAX, max = FLT_MIN;
+ int prev_ymag = -1, prev_yphase = -1;
+ char text[32];
+ int channel, 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;
+
+ channel = av_clip(s->ir_channel, 0, s->in[1]->channels - 1);
+ for (i = 0; i < s->w; i++) {
+ const float *src = (const float *)s->in[1]->extended_data[channel];
+ double w = i * M_PI / (s->w - 1);
+ double real = 0.;
+ double imag = 0.;
+
+ for (x = 0; x < s->nb_taps; x++) {
+ real += cos(-x * w) * src[x];
+ imag += sin(-x * w) * src[x];
+ }
+
+ mag[i] = 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);
+}
+