[FFmpeg-devel] [PATCH] lavfi/showspectrum: display multiple channels in separate row

Rudolf Polzer divverent at xonotic.org
Fri Feb 1 16:33:17 CET 2013


The showspectrum filter gets multiple channel (any count) support. Most
ideas here are from durandal_1707.

Screenshots (temporary URL):
http://rm.sudo.rm-f.org/~xonotic/temp/ffmpeg-showspectrum/screenshots/

New options:

- combined=1: old ffplay-like combined view of all channels
- intensity=1: sox-like intensity spectrum
- logscale=1: sox-like logarithmic scale
- saturation=<float>: saturation modifier

Signed-off-by: Rudolf Polzer <divverent at xonotic.org>
---
 Changelog                      |   1 +
 doc/filters.texi               |   4 +
 libavfilter/avf_showspectrum.c | 215 ++++++++++++++++++++++++++++++++++-------
 3 files changed, 186 insertions(+), 34 deletions(-)

diff --git a/Changelog b/Changelog
index c509c4c..8d9befc 100644
--- a/Changelog
+++ b/Changelog
@@ -10,6 +10,7 @@ version <next>:
 - EVRC decoder
 - audio fade filter
 - filtering audio with unknown channel layout
+- showspectrum now with separate channel display and multichannel (configurable)
 
 
 version 1.1:
diff --git a/doc/filters.texi b/doc/filters.texi
index 21e2cff..73674cb 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -5709,6 +5709,10 @@ Specify the video size for the output. Default value is @code{640x480}.
 @item slide
 Specify if the spectrum should slide along the window. Default value is
 @code{0}.
+ at item compat
+Specify if the old mode, where up to 2 channels are displayed in same row,
+should be used. Default value is
+ at code{0}.
 @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 977fca9..41f7a8e 100644
--- a/libavfilter/avf_showspectrum.c
+++ b/libavfilter/avf_showspectrum.c
@@ -37,14 +37,20 @@ typedef struct {
     int w, h;
     AVFilterBufferRef *outpicref;
     int req_fullfilled;
+    int nb_display_channels;
     int sliding;                ///< 1 if sliding mode, 0 otherwise
+    int combined;               ///< use combined channel display mode
+    int intensity_colors;       ///< use intensity-based coloring
+    int logscale;               ///< use logarithmic 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<<rdft_bits)
-    FFTSample *rdft_data;       ///< bins holder for each (displayed) channels
+    FFTSample **rdft_data;      ///< bins holder for each (displayed) channels
     int filled;                 ///< number of samples (per channel) filled in current rdft_buffer
     int consumed;               ///< number of samples (per channel) consumed from the input frame
     float *window_func_lut;     ///< Window function LUT
+    float *combine_buffer;      ///< color combining buffer (3 * h items)
 } ShowSpectrumContext;
 
 #define OFFSET(x) offsetof(ShowSpectrumContext, x)
@@ -54,11 +60,30 @@ static const AVOption showspectrum_options[] = {
     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0, FLAGS },
     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0, FLAGS },
     { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
+    { "combined", "set combined mode", OFFSET(combined), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
+    { "intensity", "set intensity based coloring", OFFSET(intensity_colors), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
+    { "logscale", "set logarithmic scale", OFFSET(logscale), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
+    { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
     { NULL },
 };
 
 AVFILTER_DEFINE_CLASS(showspectrum);
 
+typedef struct {
+    float a, y, u, v;
+} intensity_color_table_item;
+static const intensity_color_table_item intensity_color_table[] =
+{
+    { 0, 0, 0, 0 },
+    { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
+    { 0.3, .1857228179456802, .1772436246393981, .1747555484041475 },
+    { 0.6, .2818498058365613, -.1593064119945782, .4713207455460892 },
+    { 0.73, .6583062117554781, -.3716070802232764, .2435275933125293 },
+    { 0.78, 0.763185357582429, -.4307467689263783, .1686649662231043 },
+    { 0.91, .9533636363636364, -.2045454545454546, .03313636363636363 },
+    { 1, 1, 0, 0 }
+};
+
 static av_cold int init(AVFilterContext *ctx, const char *args)
 {
     ShowSpectrumContext *showspectrum = ctx->priv;
@@ -76,8 +101,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 +119,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 +149,22 @@ 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->combined ? outlink->h : outlink->h / inlink->channels;
+
     /* RDFT window size (precision) according to the requested output frame height */
-    for (rdft_bits = 1; 1<<rdft_bits < 2*outlink->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 +174,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);
+        if (av_size_mult(sizeof(**showspectrum->rdft_data),
+                         win_size, &rdft_size) < 0)
             return AVERROR(EINVAL);
-        showspectrum->rdft_data = av_malloc(rdft_size);
+        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 +218,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 +262,160 @@ 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->combined ? outlink->h : outlink->h / showspectrum->nb_display_channels;
+
         /* 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;
-
-            if (showspectrum->sliding) {
-                memmove(p, p + 3, (outlink->w - 1) * 3);
-                p += (outlink->w - 1) * 3;
+            showspectrum->combine_buffer[3*y] = 0;
+            showspectrum->combine_buffer[3*y+1] = 127.5;
+            showspectrum->combine_buffer[3*y+2] = 127.5;
+        }
+
+        for (ch = 0; ch < showspectrum->nb_display_channels; ch++) {
+            float yf, uf, vf;
+
+            /* decide color range */
+            if (showspectrum->combined) {
+                // reduce range by channel count
+                yf = 256.0f / showspectrum->nb_display_channels;
+                if (showspectrum->intensity_colors) {
+                    uf = yf;
+                    vf = yf;
+                } else {
+                    /* adjust saturation for mixed UV coloring */
+                    /* this factor is correct for infinite channels, an approximation otherwise */
+                    uf = yf * M_PI;
+                    vf = yf * M_PI;
+                }
             } else {
-                p += showspectrum->xpos * 3;
+                // full range
+                yf = 256.0f;
+                uf = 256.0f;
+                vf = 256.0f;
+            }
+
+            /* decide channel colors */
+            if (!showspectrum->intensity_colors) {
+                if (showspectrum->nb_display_channels >= 2) {
+                    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->combined ? y : ch * h + y;
+                float *out = &showspectrum->combine_buffer[3 * row];
+
+                /* get magnitude */
+                float a = w * MAGNITUDE(y, ch);
+
+                /* apply scale */
+                if (showspectrum->logscale)
+                    a = 1 - log(FFMAX(FFMIN(1, a), 1e-6)) / log(1e-6); // zero = -120dBFS
+                else
+                    a = sqrt(a);
+
+                if (showspectrum->intensity_colors) {
+                    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;
+                }
             }
+        }
 
-            a = FFMIN(a, 255);
-            b = FFMIN(b, 255);
-            p[0] = a;
-            p[1] = b;
-            p[2] = (a + b) / 2;
+        /* 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 },
-- 
1.8.1.1



More information about the ffmpeg-devel mailing list