+static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
+{
+ 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;
+
+ if (o) {
+ for (char_y = font_height - 1; char_y >= 0; char_y--) {
+ uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x;
+ for (mask = 0x80; mask; mask >>= 1) {
+ if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
+ p[char_y] = ~p[char_y];
+ p += pic->linesize[0];
+ }
+ }
+ } else {
+ uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8);
+ 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)
+ *p = ~(*p);
+ p++;
+ }
+ p += pic->linesize[0] - 8;
+ }
+ }
+ }
+}
+
+static void color_range(ShowSpectrumContext *s, int ch,
+ float *yf, float *uf, float *vf)
+{
+ switch (s->mode) {
+ case COMBINED:
+ // reduce range by channel count
+ *yf = 256.0f / s->nb_display_channels;
+ switch (s->color_mode) {
+ case RAINBOW:
+ case MORELAND:
+ case NEBULAE:
+ case FIRE:
+ case FIERY:
+ case FRUIT:
+ case COOL:
+ case GREEN:
+ case VIRIDIS:
+ case PLASMA:
+ case CIVIDIS:
+ case TERRAIN:
+ case MAGMA:
+ case INTENSITY:
+ *uf = *yf;
+ *vf = *yf;
+ break;
+ case CHANNEL:
+ /* adjust saturation for mixed UV coloring */
+ /* this factor is correct for infinite channels, an approximation otherwise */
+ *uf = *yf * M_PI;
+ *vf = *yf * M_PI;
+ break;
+ default:
+ av_assert0(0);
+ }
+ break;
+ case SEPARATE:
+ // full range
+ *yf = 256.0f;
+ *uf = 256.0f;
+ *vf = 256.0f;
+ break;
+ default:
+ av_assert0(0);
+ }
+
+ if (s->color_mode == CHANNEL) {
+ if (s->nb_display_channels > 1) {
+ *uf *= 0.5f * sinf((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
+ *vf *= 0.5f * cosf((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
+ } else {
+ *uf *= 0.5f * sinf(M_PI * s->rotation);
+ *vf *= 0.5f * cosf(M_PI * s->rotation + M_PI_2);
+ }
+ } else {
+ *uf += *uf * sinf(M_PI * s->rotation);
+ *vf += *vf * cosf(M_PI * s->rotation + M_PI_2);
+ }
+
+ *uf *= s->saturation;
+ *vf *= s->saturation;
+}
+
+static void pick_color(ShowSpectrumContext *s,
+ float yf, float uf, float vf,
+ float a, float *out)
+{
+ if (s->color_mode > CHANNEL) {
+ const int cm = s->color_mode;
+ float y, u, v;
+ int i;
+
+ for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
+ if (color_table[cm][i].a >= a)
+ break;
+ // i now is the first item >= the color
+ // now we know to interpolate between item i - 1 and i
+ if (a <= color_table[cm][i - 1].a) {
+ y = color_table[cm][i - 1].y;
+ u = color_table[cm][i - 1].u;
+ v = color_table[cm][i - 1].v;
+ } else if (a >= color_table[cm][i].a) {
+ y = color_table[cm][i].y;
+ u = color_table[cm][i].u;
+ v = color_table[cm][i].v;
+ } else {
+ float start = color_table[cm][i - 1].a;
+ float end = color_table[cm][i].a;
+ float lerpfrac = (a - start) / (end - start);
+ y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
+ + color_table[cm][i].y * lerpfrac;
+ u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
+ + color_table[cm][i].u * lerpfrac;
+ v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
+ + color_table[cm][i].v * lerpfrac;
+ }
+
+ out[0] = y * yf;
+ out[1] = u * uf;
+ out[2] = v * vf;
+ } else {
+ out[0] = a * yf;
+ out[1] = a * uf;
+ out[2] = a * vf;
+ }
+}
+
+static char *get_time(AVFilterContext *ctx, float seconds, int x)
+{
+ char *units;
+
+ if (x == 0)
+ units = av_asprintf("0");
+ else if (log10(seconds) > 6)
+ units = av_asprintf("%.2fh", seconds / (60 * 60));
+ else if (log10(seconds) > 3)
+ units = av_asprintf("%.2fm", seconds / 60);
+ else
+ units = av_asprintf("%.2fs", seconds);
+ return units;
+}
+
+static float log_scale(const float value, const float min, const float max)
+{
+ if (value < min)
+ return min;
+ if (value > max)
+ return max;
+
+ {
+ const float b = logf(max / min) / (max - min);
+ const float a = max / expf(max * b);
+
+ return expf(value * b) * a;
+ }
+}
+
+static float get_log_hz(const int bin, const int num_bins, const float sample_rate)
+{
+ const float max_freq = sample_rate / 2;
+ const float hz_per_bin = max_freq / num_bins;
+ const float freq = hz_per_bin * bin;
+ const float scaled_freq = log_scale(freq + 1, 21, max_freq) - 1;
+
+ return num_bins * scaled_freq / max_freq;
+}
+
+static float inv_log_scale(const float value, const float min, const float max)
+{
+ if (value < min)
+ return min;
+ if (value > max)
+ return max;
+
+ {
+ const float b = logf(max / min) / (max - min);
+ const float a = max / expf(max * b);
+
+ return logf(value / a) / b;
+ }
+}
+
+static float bin_pos(const int bin, const int num_bins, const float sample_rate)
+{
+ const float max_freq = sample_rate / 2;
+ const float hz_per_bin = max_freq / num_bins;
+ const float freq = hz_per_bin * bin;
+ const float scaled_freq = inv_log_scale(freq + 1, 21, max_freq) - 1;
+
+ return num_bins * scaled_freq / max_freq;
+}
+
+static int draw_legend(AVFilterContext *ctx, int samples)
+{
+ ShowSpectrumContext *s = ctx->priv;
+ AVFilterLink *inlink = ctx->inputs[0];
+ AVFilterLink *outlink = ctx->outputs[0];
+ int ch, y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
+ int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL);
+ float spp = samples / (float)sz;
+ char *text;
+ uint8_t *dst;
+ char chlayout_str[128];
+
+ av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), inlink->channels,
+ inlink->channel_layout);
+
+ text = av_asprintf("%d Hz | %s", inlink->sample_rate, chlayout_str);
+ if (!text)
+ return AVERROR(ENOMEM);
+
+ drawtext(s->outpicref, 2, outlink->h - 10, "CREATED BY LIBAVFILTER", 0);
+ drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, outlink->h - 10, text, 0);
+ av_freep(&text);
+ if (s->stop) {
+ text = av_asprintf("Zoom: %d Hz - %d Hz", s->start, s->stop);
+ if (!text)
+ return AVERROR(ENOMEM);
+ drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, 3, text, 0);
+ av_freep(&text);
+ }
+
+ dst = s->outpicref->data[0] + (s->start_y - 1) * s->outpicref->linesize[0] + s->start_x - 1;
+ for (x = 0; x < s->w + 1; x++)
+ dst[x] = 200;
+ dst = s->outpicref->data[0] + (s->start_y + s->h) * s->outpicref->linesize[0] + s->start_x - 1;
+ for (x = 0; x < s->w + 1; x++)
+ dst[x] = 200;
+ for (y = 0; y < s->h + 2; y++) {
+ dst = s->outpicref->data[0] + (y + s->start_y - 1) * s->outpicref->linesize[0];
+ dst[s->start_x - 1] = 200;
+ dst[s->start_x + s->w] = 200;
+ }
+ if (s->orientation == VERTICAL) {
+ int h = s->mode == SEPARATE ? s->h / s->nb_display_channels : s->h;
+ int hh = s->mode == SEPARATE ? -(s->h % s->nb_display_channels) + 1 : 1;
+ for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
+ for (y = 0; y < h; y += 20) {
+ dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
+ dst[s->start_x - 2] = 200;
+ dst[s->start_x + s->w + 1] = 200;
+ }
+ for (y = 0; y < h; y += 40) {
+ dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
+ dst[s->start_x - 3] = 200;
+ dst[s->start_x + s->w + 2] = 200;
+ }
+ dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x;
+ for (x = 0; x < s->w; x+=40)
+ dst[x] = 200;
+ dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x;
+ for (x = 0; x < s->w; x+=80)
+ dst[x] = 200;
+ dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x;
+ for (x = 0; x < s->w; x+=40) {
+ dst[x] = 200;
+ }
+ dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x;
+ for (x = 0; x < s->w; x+=80) {
+ dst[x] = 200;
+ }
+ for (y = 0; y < h; y += 40) {
+ float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
+ float bin = s->fscale == F_LINEAR ? y : get_log_hz(y, h, inlink->sample_rate);
+ float hertz = s->start + bin * range / (float)(1 << (int)ceil(log2(h)));
+ char *units;
+
+ if (hertz == 0)
+ units = av_asprintf("DC");
+ else
+ units = av_asprintf("%.2f", hertz);
+ if (!units)
+ return AVERROR(ENOMEM);
+
+ drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4 - hh, units, 0);
+ av_free(units);
+ }
+ }
+
+ for (x = 0; x < s->w && s->single_pic; x+=80) {
+ float seconds = x * spp / inlink->sample_rate;
+ char *units = get_time(ctx, seconds, x);
+ if (!units)
+ return AVERROR(ENOMEM);
+
+ drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->h + s->start_y + 6, units, 0);
+ drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->start_y - 12, units, 0);
+ av_free(units);
+ }
+
+ drawtext(s->outpicref, outlink->w / 2 - 4 * 4, outlink->h - s->start_y / 2, "TIME", 0);
+ drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 14 * 4, "FREQUENCY (Hz)", 1);
+ } else {
+ int w = s->mode == SEPARATE ? s->w / s->nb_display_channels : s->w;
+ for (y = 0; y < s->h; y += 20) {
+ dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
+ dst[s->start_x - 2] = 200;
+ dst[s->start_x + s->w + 1] = 200;
+ }
+ for (y = 0; y < s->h; y += 40) {
+ dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
+ dst[s->start_x - 3] = 200;
+ dst[s->start_x + s->w + 2] = 200;
+ }
+ for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
+ dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
+ for (x = 0; x < w; x+=40)
+ dst[x] = 200;
+ dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x + w * ch;
+ for (x = 0; x < w; x+=80)
+ dst[x] = 200;
+ dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x + w * ch;
+ for (x = 0; x < w; x+=40) {
+ dst[x] = 200;
+ }
+ dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
+ for (x = 0; x < w; x+=80) {
+ dst[x] = 200;
+ }
+ for (x = 0; x < w - 79; x += 80) {
+ float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
+ float bin = s->fscale == F_LINEAR ? x : get_log_hz(x, w, inlink->sample_rate);
+ float hertz = s->start + bin * range / (float)(1 << (int)ceil(log2(w)));
+ char *units;
+
+ if (hertz == 0)
+ units = av_asprintf("DC");
+ else
+ units = av_asprintf("%.2f", hertz);
+ if (!units)
+ return AVERROR(ENOMEM);
+
+ drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->start_y - 12, units, 0);
+ drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->h + s->start_y + 6, units, 0);
+ av_free(units);
+ }
+ }
+ for (y = 0; y < s->h && s->single_pic; y+=40) {
+ float seconds = y * spp / inlink->sample_rate;
+ char *units = get_time(ctx, seconds, x);
+ if (!units)
+ return AVERROR(ENOMEM);
+
+ drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0);
+ av_free(units);
+ }
+ drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 4 * 4, "TIME", 1);
+ drawtext(s->outpicref, outlink->w / 2 - 14 * 4, outlink->h - s->start_y / 2, "FREQUENCY (Hz)", 0);
+ }
+
+ for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) {
+ int h = multi ? s->h / s->nb_display_channels : s->h;
+
+ for (y = 0; y < h; y++) {
+ float out[3] = { 0., 127.5, 127.5};
+ int chn;
+
+ for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) {
+ float yf, uf, vf;
+ int channel = (multi) ? s->nb_display_channels - ch - 1 : chn;
+ float lout[3];
+
+ color_range(s, channel, &yf, &uf, &vf);
+ pick_color(s, yf, uf, vf, y / (float)h, lout);
+ out[0] += lout[0];
+ out[1] += lout[1];
+ out[2] += lout[2];
+ }
+ memset(s->outpicref->data[0]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[0] + s->w + s->start_x + 20, av_clip_uint8(out[0]), 10);
+ memset(s->outpicref->data[1]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[1] + s->w + s->start_x + 20, av_clip_uint8(out[1]), 10);
+ memset(s->outpicref->data[2]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[2] + s->w + s->start_x + 20, av_clip_uint8(out[2]), 10);
+ }
+
+ for (y = 0; ch == 0 && y < h; y += h / 10) {
+ float value = 120.f * log10f(1.f - y / (float)h);
+ char *text;
+
+ if (value < -120)
+ break;
+ text = av_asprintf("%.0f dB", value);
+ if (!text)
+ continue;
+ drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 5, text, 0);
+ av_free(text);
+ }
+ }
+
+ return 0;
+}
+
+static float get_value(AVFilterContext *ctx, int ch, int y)
+{
+ ShowSpectrumContext *s = ctx->priv;
+ float *magnitudes = s->magnitudes[ch];
+ float *phases = s->phases[ch];
+ float a;
+
+ switch (s->data) {
+ case D_MAGNITUDE:
+ /* get magnitude */
+ a = magnitudes[y];
+ break;
+ case D_PHASE:
+ /* get phase */
+ a = phases[y];
+ break;
+ default:
+ av_assert0(0);
+ }
+
+ /* apply scale */
+ switch (s->scale) {
+ case LINEAR:
+ a = av_clipf(a, 0, 1);
+ break;
+ case SQRT:
+ a = av_clipf(sqrtf(a), 0, 1);
+ break;
+ case CBRT:
+ a = av_clipf(cbrtf(a), 0, 1);
+ break;
+ case FOURTHRT:
+ a = av_clipf(sqrtf(sqrtf(a)), 0, 1);
+ break;
+ case FIFTHRT:
+ a = av_clipf(powf(a, 0.20), 0, 1);
+ break;
+ case LOG:
+ a = 1.f + log10f(av_clipf(a, 1e-6, 1)) / 6.f; // zero = -120dBFS
+ break;
+ default:
+ av_assert0(0);
+ }
+
+ return a;
+}
+
+static int plot_channel_lin(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+ ShowSpectrumContext *s = ctx->priv;
+ const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
+ const int ch = jobnr;
+ float yf, uf, vf;
+ int y;
+
+ /* decide color range */
+ color_range(s, ch, &yf, &uf, &vf);
+
+ /* draw the channel */
+ for (y = 0; y < h; y++) {
+ int row = (s->mode == COMBINED) ? y : ch * h + y;
+ float *out = &s->color_buffer[ch][3 * row];
+ float a = get_value(ctx, ch, y);
+
+ pick_color(s, yf, uf, vf, a, out);
+ }
+
+ return 0;
+}
+
+static int plot_channel_log(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+ ShowSpectrumContext *s = ctx->priv;
+ AVFilterLink *inlink = ctx->inputs[0];
+ const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
+ const int ch = jobnr;
+ float y, yf, uf, vf;
+ int yy = 0;
+
+ /* decide color range */
+ color_range(s, ch, &yf, &uf, &vf);
+
+ /* draw the channel */
+ for (y = 0; y < h && yy < h; yy++) {
+ float pos0 = bin_pos(yy+0, h, inlink->sample_rate);
+ float pos1 = bin_pos(yy+1, h, inlink->sample_rate);
+ float delta = pos1 - pos0;
+ float a0, a1;
+
+ a0 = get_value(ctx, ch, yy+0);
+ a1 = get_value(ctx, ch, FFMIN(yy+1, h-1));
+ for (float j = pos0; j < pos1 && y + j - pos0 < h; j++) {
+ float row = (s->mode == COMBINED) ? y + j - pos0 : ch * h + y + j - pos0;
+ float *out = &s->color_buffer[ch][3 * FFMIN(lrintf(row), h-1)];
+ float lerpfrac = (j - pos0) / delta;
+
+ pick_color(s, yf, uf, vf, lerpfrac * a1 + (1.f-lerpfrac) * a0, out);
+ }
+ y += delta;
+ }
+
+ return 0;
+}
+