From 81f2549ec9ab236727abe5dfe19d5dfd1478c2b2 Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Fri, 1 Feb 2013 16:33:17 +0100 Subject: [PATCH] lavfi/showspectrum: display multiple channels in separate row The showspectrum filter gets multiple channel (any count) support. Signed-off-by: Rudolf Polzer --- Changelog | 1 + doc/filters.texi | 28 ++++ libavfilter/avf_showspectrum.c | 250 ++++++++++++++++++++++++++++----- 3 files changed, 245 insertions(+), 34 deletions(-) diff --git a/Changelog b/Changelog index 01bb42ecc7..5ee9200bca 100644 --- a/Changelog +++ b/Changelog @@ -12,6 +12,7 @@ version : - filtering audio with unknown channel layout - allpass, bass, bandpass, bandreject, biquad, equalizer, highpass, lowpass and treble audio filter +- improved showspectrum filter, with multichannel support and sox-like colors version 1.1: diff --git a/doc/filters.texi b/doc/filters.texi index baa344d93d..37b7a46a9c 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -6055,6 +6055,34 @@ Specify the video size for the output. Default value is @code{640x512}. @item slide Specify if the spectrum should slide along the window. Default value is @code{0}. +@item mode +Specify display mode. Can be either @code{combined}: all channels are +displayed in the same row, or @code{separate}: all channels are displayed +in separate rows. Default value is @code{combined}. +@item color +Specify display color mode. Can be either @code{channel}: each channel +is displayed in a separate color, or @code{intensity}: each channel is +displayed using the same color scheme. Default value is @code{channel}. +@item scale +Specify scale used for calculating intensity color values. + +It accepts the following values: +@table @option +@item{lin} +linear +@item{sqrt} +square root, default +@item{cbrt} +cubic root +@item{log} +logarithmic +@end table + +@item saturation +Set saturation modifier for displayed colors. Negative values provide +alternative color scheme. @code{0} is no saturation at all. +Saturation must be in [-10.0, 10.0] range. +Default value is @code{1}. @end table The usage is very similar to the showwaves filter; see the examples in that diff --git a/libavfilter/avf_showspectrum.c b/libavfilter/avf_showspectrum.c index d8aed0d773..4eb973169a 100644 --- a/libavfilter/avf_showspectrum.c +++ b/libavfilter/avf_showspectrum.c @@ -27,24 +27,36 @@ #include #include "libavcodec/avfft.h" +#include "libavutil/avassert.h" #include "libavutil/channel_layout.h" #include "libavutil/opt.h" #include "avfilter.h" #include "internal.h" +enum DisplayMode { COMBINED, SEPARATE, NB_MODES }; +enum DisplayScale { LINEAR, SQRT, CBRT, LOG, NB_SCALES }; +enum ColorMode { CHANNEL, INTENSITY, NB_CLMODES }; + typedef struct { const AVClass *class; int w, h; AVFilterBufferRef *outpicref; int req_fullfilled; + int nb_display_channels; + int channel_height; int sliding; ///< 1 if sliding mode, 0 otherwise + enum DisplayMode mode; ///< channel display mode + enum ColorMode color_mode; ///< display color scheme + enum DisplayScale scale; + float saturation; ///< color saturation multiplier int xpos; ///< x position (current column) RDFTContext *rdft; ///< Real Discrete Fourier Transform context int rdft_bits; ///< number of bits (RDFT window size = 1<priv; @@ -76,8 +115,12 @@ static av_cold int init(AVFilterContext *ctx, const char *args) static av_cold void uninit(AVFilterContext *ctx) { ShowSpectrumContext *showspectrum = ctx->priv; + int i; + av_freep(&showspectrum->combine_buffer); av_rdft_end(showspectrum->rdft); + for (i = 0; i < showspectrum->nb_display_channels; i++) + av_freep(&showspectrum->rdft_data[i]); av_freep(&showspectrum->rdft_data); av_freep(&showspectrum->window_func_lut); avfilter_unref_bufferp(&showspectrum->outpicref); @@ -90,7 +133,7 @@ static int query_formats(AVFilterContext *ctx) AVFilterLink *inlink = ctx->inputs[0]; AVFilterLink *outlink = ctx->outputs[0]; static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE }; - static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE }; + static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE }; /* set input audio formats */ formats = ff_make_format_list(sample_fmts); @@ -120,19 +163,23 @@ static int query_formats(AVFilterContext *ctx) static int config_output(AVFilterLink *outlink) { AVFilterContext *ctx = outlink->src; + AVFilterLink *inlink = ctx->inputs[0]; ShowSpectrumContext *showspectrum = ctx->priv; - int i, rdft_bits, win_size; + int i, rdft_bits, win_size, h; outlink->w = showspectrum->w; outlink->h = showspectrum->h; + h = (showspectrum->mode == COMBINED) ? outlink->h : outlink->h / inlink->channels; + showspectrum->channel_height = h; + /* RDFT window size (precision) according to the requested output frame height */ - for (rdft_bits = 1; 1<h; rdft_bits++); + for (rdft_bits = 1; 1 << rdft_bits < 2 * h; rdft_bits++); win_size = 1 << rdft_bits; /* (re-)configuration if the video output changed (or first init) */ if (rdft_bits != showspectrum->rdft_bits) { - size_t rdft_size; + size_t rdft_size, rdft_listsize; AVFilterBufferRef *outpicref; av_rdft_end(showspectrum->rdft); @@ -142,12 +189,25 @@ static int config_output(AVFilterLink *outlink) /* RDFT 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 < showspectrum->nb_display_channels; i++) + av_freep(&showspectrum->rdft_data[i]); av_freep(&showspectrum->rdft_data); - if (av_size_mult(sizeof(*showspectrum->rdft_data), 2 * win_size, &rdft_size) < 0) + showspectrum->nb_display_channels = inlink->channels; + + if (av_size_mult(sizeof(*showspectrum->rdft_data), + showspectrum->nb_display_channels, &rdft_listsize) < 0) return AVERROR(EINVAL); - showspectrum->rdft_data = av_malloc(rdft_size); + if (av_size_mult(sizeof(**showspectrum->rdft_data), + win_size, &rdft_size) < 0) + return AVERROR(EINVAL); + showspectrum->rdft_data = av_malloc(rdft_listsize); if (!showspectrum->rdft_data) return AVERROR(ENOMEM); + for (i = 0; i < showspectrum->nb_display_channels; i++) { + showspectrum->rdft_data[i] = av_malloc(rdft_size); + if (!showspectrum->rdft_data[i]) + return AVERROR(ENOMEM); + } showspectrum->filled = 0; /* pre-calc windowing function (hann here) */ @@ -173,6 +233,10 @@ static int config_output(AVFilterLink *outlink) if (showspectrum->xpos >= outlink->w) showspectrum->xpos = 0; + showspectrum->combine_buffer = + av_realloc_f(showspectrum->combine_buffer, outlink->h * 3, + sizeof(*showspectrum->combine_buffer)); + av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d RDFT window size:%d\n", showspectrum->w, showspectrum->h, win_size); return 0; @@ -213,62 +277,180 @@ static int plot_spectrum_column(AVFilterLink *inlink, AVFilterBufferRef *insampl AVFilterLink *outlink = ctx->outputs[0]; ShowSpectrumContext *showspectrum = ctx->priv; AVFilterBufferRef *outpicref = showspectrum->outpicref; - const int nb_channels = av_get_channel_layout_nb_channels(insamples->audio->channel_layout); /* nb_freq contains the power of two superior or equal to the output image * height (or half the RDFT window size) */ const int nb_freq = 1 << (showspectrum->rdft_bits - 1); const int win_size = nb_freq << 1; + const double w = 1. / (sqrt(nb_freq) * 32768.); - int ch, n, y; - FFTSample *data[2]; - const int nb_display_channels = FFMIN(nb_channels, 2); + int ch, plane, n, y; const int start = showspectrum->filled; const int add_samples = FFMIN(win_size - start, nb_samples); /* fill RDFT input with the number of samples available */ - for (ch = 0; ch < nb_display_channels; ch++) { + for (ch = 0; ch < showspectrum->nb_display_channels; ch++) { const int16_t *p = (int16_t *)insamples->extended_data[ch]; p += showspectrum->consumed; - data[ch] = showspectrum->rdft_data + win_size * ch; // select channel buffer for (n = 0; n < add_samples; n++) - data[ch][start + n] = p[n] * showspectrum->window_func_lut[start + n]; + showspectrum->rdft_data[ch][start + n] = p[n] * showspectrum->window_func_lut[start + n]; } showspectrum->filled += add_samples; /* complete RDFT window size? */ if (showspectrum->filled == win_size) { + /* channel height */ + int h = showspectrum->channel_height; + /* run RDFT on each samples set */ - for (ch = 0; ch < nb_display_channels; ch++) - av_rdft_calc(showspectrum->rdft, data[ch]); + for (ch = 0; ch < showspectrum->nb_display_channels; ch++) + av_rdft_calc(showspectrum->rdft, showspectrum->rdft_data[ch]); /* fill a new spectrum column */ -#define RE(ch) data[ch][2*y + 0] -#define IM(ch) data[ch][2*y + 1] -#define MAGNITUDE(re, im) sqrt((re)*(re) + (im)*(im)) +#define RE(y, ch) showspectrum->rdft_data[ch][2 * y + 0] +#define IM(y, ch) showspectrum->rdft_data[ch][2 * y + 1] +#define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch)) + /* initialize buffer for combining to black */ for (y = 0; y < outlink->h; y++) { - // FIXME: bin[0] contains first and last bins - uint8_t *p = outpicref->data[0] + (outlink->h - y - 1) * outpicref->linesize[0]; - const double w = 1. / sqrt(nb_freq); - int a = sqrt(w * MAGNITUDE(RE(0), IM(0))); - int b = nb_display_channels > 1 ? sqrt(w * MAGNITUDE(RE(1), IM(1))) : a; + showspectrum->combine_buffer[3 * y ] = 0; + showspectrum->combine_buffer[3 * y + 1] = 127.5; + showspectrum->combine_buffer[3 * y + 2] = 127.5; + } - if (showspectrum->sliding) { - memmove(p, p + 3, (outlink->w - 1) * 3); - p += (outlink->w - 1) * 3; - } else { - p += showspectrum->xpos * 3; + for (ch = 0; ch < showspectrum->nb_display_channels; ch++) { + float yf, uf, vf; + + /* decide color range */ + switch (showspectrum->mode) { + case COMBINED: + // reduce range by channel count + yf = 256.0f / showspectrum->nb_display_channels; + switch (showspectrum->color_mode) { + 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); } - a = FFMIN(a, 255); - b = FFMIN(b, 255); - p[0] = a; - p[1] = b; - p[2] = (a + b) / 2; + if (showspectrum->color_mode == CHANNEL) { + if (showspectrum->nb_display_channels > 1) { + uf *= 0.5 * sin((2 * M_PI * ch) / showspectrum->nb_display_channels); + vf *= 0.5 * cos((2 * M_PI * ch) / showspectrum->nb_display_channels); + } else { + uf = 0.0f; + vf = 0.0f; + } + } + uf *= showspectrum->saturation; + vf *= showspectrum->saturation; + + /* draw the channel */ + for (y = 0; y < h; y++) { + int row = (showspectrum->mode == COMBINED) ? y : ch * h + y; + float *out = &showspectrum->combine_buffer[3 * row]; + + /* get magnitude */ + float a = w * MAGNITUDE(y, ch); + + /* apply scale */ + switch (showspectrum->scale) { + case LINEAR: + break; + case SQRT: + a = sqrt(a); + break; + case CBRT: + a = cbrt(a); + break; + case LOG: + a = 1 - log(FFMAX(FFMIN(1, a), 1e-6)) / log(1e-6); // zero = -120dBFS + break; + default: + av_assert0(0); + } + + if (showspectrum->color_mode == INTENSITY) { + float y, u, v; + int i; + + for (i = 1; i < sizeof(intensity_color_table) / sizeof(*intensity_color_table) - 1; i++) + if (intensity_color_table[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 <= intensity_color_table[i - 1].a) { + y = intensity_color_table[i - 1].y; + u = intensity_color_table[i - 1].u; + v = intensity_color_table[i - 1].v; + } else if (a >= intensity_color_table[i].a) { + y = intensity_color_table[i].y; + u = intensity_color_table[i].u; + v = intensity_color_table[i].v; + } else { + float start = intensity_color_table[i - 1].a; + float end = intensity_color_table[i].a; + float lerpfrac = (a - start) / (end - start); + y = intensity_color_table[i - 1].y * (1.0f - lerpfrac) + + intensity_color_table[i].y * lerpfrac; + u = intensity_color_table[i - 1].u * (1.0f - lerpfrac) + + intensity_color_table[i].u * lerpfrac; + v = intensity_color_table[i - 1].v * (1.0f - lerpfrac) + + intensity_color_table[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; + } + } } + + /* copy to output */ + if (showspectrum->sliding) { + for (plane = 0; plane < 3; plane++) { + for (y = 0; y < outlink->h; y++) { + uint8_t *p = outpicref->data[plane] + + y * outpicref->linesize[plane]; + memmove(p, p + 1, outlink->w - 1); + } + } + showspectrum->xpos = outlink->w - 1; + } + for (plane = 0; plane < 3; plane++) { + uint8_t *p = outpicref->data[plane] + + (outlink->h - 1) * outpicref->linesize[plane] + + showspectrum->xpos; + for (y = 0; y < outlink->h; y++) { + *p = rint(FFMAX(0, FFMIN(showspectrum->combine_buffer[3 * y + plane], 255))); + p -= outpicref->linesize[plane]; + } + } + outpicref->pts = insamples->pts + av_rescale_q(showspectrum->consumed, (AVRational){ 1, inlink->sample_rate },