+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;
+}
+