+ 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;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+ AVFilterContext *ctx = outlink->src;
+ AVFilterLink *inlink = ctx->inputs[0];
+ ShowSpectrumContext *s = ctx->priv;
+ int i, fft_bits, h, w;
+ float overlap;
+
+ switch (s->fscale) {
+ case F_LINEAR: s->plot_channel = plot_channel_lin; break;
+ case F_LOG: s->plot_channel = plot_channel_log; break;
+ default: return AVERROR_BUG;
+ }
+
+ s->stop = FFMIN(s->stop, inlink->sample_rate / 2);
+ if (s->stop && s->stop <= s->start) {
+ av_log(ctx, AV_LOG_ERROR, "Stop frequency should be greater than start.\n");
+ return AVERROR(EINVAL);
+ }
+
+ if (!strcmp(ctx->filter->name, "showspectrumpic"))
+ s->single_pic = 1;
+
+ outlink->w = s->w;
+ outlink->h = s->h;
+ outlink->sample_aspect_ratio = (AVRational){1,1};
+
+ if (s->legend) {
+ s->start_x = (log10(inlink->sample_rate) + 1) * 25;
+ s->start_y = 64;
+ outlink->w += s->start_x * 2;
+ outlink->h += s->start_y * 2;
+ }
+
+ h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? s->h : s->h / inlink->channels;
+ w = (s->mode == COMBINED || s->orientation == VERTICAL) ? s->w : s->w / inlink->channels;
+ s->channel_height = h;
+ s->channel_width = w;
+
+ if (s->orientation == VERTICAL) {
+ /* FFT window size (precision) according to the requested output frame height */
+ for (fft_bits = 1; 1 << fft_bits < 2 * h; fft_bits++);
+ } else {
+ /* FFT window size (precision) according to the requested output frame width */
+ for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++);
+ }
+
+ s->win_size = 1 << fft_bits;
+ s->buf_size = s->win_size << !!s->stop;
+
+ if (!s->fft) {
+ s->fft = av_calloc(inlink->channels, sizeof(*s->fft));
+ if (!s->fft)
+ return AVERROR(ENOMEM);
+ }
+
+ if (s->stop) {
+ if (!s->ifft) {
+ s->ifft = av_calloc(inlink->channels, sizeof(*s->ifft));
+ if (!s->ifft)
+ return AVERROR(ENOMEM);
+ }
+ }
+
+ /* (re-)configuration if the video output changed (or first init) */
+ if (fft_bits != s->fft_bits) {
+ AVFrame *outpicref;
+
+ s->fft_bits = fft_bits;
+
+ /* FFT buffers: x2 for each (display) channel buffer.
+ * Note: we use free and malloc instead of a realloc-like function to
+ * make sure the buffer is aligned in memory for the FFT functions. */
+ for (i = 0; i < s->nb_display_channels; i++) {
+ if (s->stop) {
+ av_fft_end(s->ifft[i]);
+ av_freep(&s->fft_scratch[i]);
+ }
+ av_fft_end(s->fft[i]);
+ av_freep(&s->fft_data[i]);
+ }
+ av_freep(&s->fft_data);
+
+ s->nb_display_channels = inlink->channels;
+ for (i = 0; i < s->nb_display_channels; i++) {
+ s->fft[i] = av_fft_init(fft_bits + !!s->stop, 0);
+ if (s->stop) {
+ s->ifft[i] = av_fft_init(fft_bits + !!s->stop, 1);
+ if (!s->ifft[i]) {
+ av_log(ctx, AV_LOG_ERROR, "Unable to create Inverse FFT context. "
+ "The window size might be too high.\n");
+ return AVERROR(EINVAL);
+ }
+ }
+ if (!s->fft[i]) {
+ av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
+ "The window size might be too high.\n");
+ return AVERROR(EINVAL);
+ }
+ }
+
+ s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
+ if (!s->magnitudes)
+ return AVERROR(ENOMEM);
+ for (i = 0; i < s->nb_display_channels; i++) {
+ s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
+ if (!s->magnitudes[i])
+ return AVERROR(ENOMEM);
+ }
+
+ s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases));
+ if (!s->phases)
+ return AVERROR(ENOMEM);
+ for (i = 0; i < s->nb_display_channels; i++) {
+ s->phases[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->phases));
+ if (!s->phases[i])
+ return AVERROR(ENOMEM);
+ }
+
+ av_freep(&s->color_buffer);
+ s->color_buffer = av_calloc(s->nb_display_channels, sizeof(*s->color_buffer));
+ if (!s->color_buffer)
+ return AVERROR(ENOMEM);
+ for (i = 0; i < s->nb_display_channels; i++) {
+ s->color_buffer[i] = av_calloc(s->orientation == VERTICAL ? s->h * 3 : s->w * 3, sizeof(**s->color_buffer));
+ if (!s->color_buffer[i])
+ return AVERROR(ENOMEM);
+ }
+
+ s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
+ if (!s->fft_data)
+ return AVERROR(ENOMEM);
+ s->fft_scratch = av_calloc(s->nb_display_channels, sizeof(*s->fft_scratch));
+ if (!s->fft_scratch)
+ return AVERROR(ENOMEM);
+ for (i = 0; i < s->nb_display_channels; i++) {
+ s->fft_data[i] = av_calloc(s->buf_size, sizeof(**s->fft_data));
+ if (!s->fft_data[i])
+ return AVERROR(ENOMEM);
+
+ s->fft_scratch[i] = av_calloc(s->buf_size, sizeof(**s->fft_scratch));
+ if (!s->fft_scratch[i])
+ return AVERROR(ENOMEM);
+ }
+
+ /* pre-calc windowing function */
+ s->window_func_lut =
+ av_realloc_f(s->window_func_lut, s->win_size,
+ sizeof(*s->window_func_lut));
+ if (!s->window_func_lut)
+ return AVERROR(ENOMEM);
+ generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
+ if (s->overlap == 1)
+ s->overlap = overlap;
+ s->hop_size = (1.f - s->overlap) * s->win_size;
+ if (s->hop_size < 1) {
+ av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
+ return AVERROR(EINVAL);
+ }
+
+ for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
+ s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
+ }
+ s->win_scale = 1.f / sqrtf(s->win_scale);
+
+ /* prepare the initial picref buffer (black frame) */
+ av_frame_free(&s->outpicref);
+ s->outpicref = outpicref =
+ ff_get_video_buffer(outlink, outlink->w, outlink->h);
+ if (!outpicref)
+ return AVERROR(ENOMEM);
+ outpicref->sample_aspect_ratio = (AVRational){1,1};
+ for (i = 0; i < outlink->h; i++) {
+ memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
+ memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
+ memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
+ }
+ outpicref->color_range = AVCOL_RANGE_JPEG;
+
+ if (!s->single_pic && s->legend)
+ draw_legend(ctx, 0);
+ }
+
+ if ((s->orientation == VERTICAL && s->xpos >= s->w) ||
+ (s->orientation == HORIZONTAL && s->xpos >= s->h))
+ s->xpos = 0;
+
+ s->auto_frame_rate = av_make_q(inlink->sample_rate, s->hop_size);
+ if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
+ s->auto_frame_rate.den *= s->w;
+ if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
+ s->auto_frame_rate.den *= s->h;
+ if (!s->single_pic && strcmp(s->rate_str, "auto")) {
+ int ret = av_parse_video_rate(&s->frame_rate, s->rate_str);
+ if (ret < 0)
+ return ret;