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

Paul B Mahol onemda at gmail.com
Mon Feb 4 16:00:53 CET 2013


On 2/3/13, Stefano Sabatini <stefasab at gmail.com> wrote:
> On date Sunday 2013-02-03 18:49:48 +0000, Paul B Mahol encoded:
>> From: Rudolf Polzer <divverent at xonotic.org>
>>
>> The showspectrum filter gets multiple channel (any count) support.
>>
>> Signed-off-by: Rudolf Polzer <divverent at xonotic.org>
>> Signed-off-by: Paul B Mahol <onemda at gmail.com>
>> ---
>>  Changelog                      |   1 +
>>  doc/filters.texi               |  15 +++
>>  libavfilter/avf_showspectrum.c | 245
>> +++++++++++++++++++++++++++++++++++------
>>  3 files changed, 226 insertions(+), 35 deletions(-)
>>
>> diff --git a/Changelog b/Changelog
>> index 01bb42e..5ee9200 100644
>> --- a/Changelog
>> +++ b/Changelog
>> @@ -12,6 +12,7 @@ version <next>:
>>  - 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 fdbe3b7..3268233 100644
>> --- a/doc/filters.texi
>> +++ b/doc/filters.texi
>> @@ -6023,6 +6023,21 @@ 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}.
>> + at item mode
>> +Specify display mode. Can be either @code{combined}: all channels are
>> +displayed in same row, or @code{separate}: all channels are displayed
>
> displayed in the same row
>
>> +in separate row. Default value is @code{combined}.
>
> in separate rows.
>
> You may use a table here and below, for better rendering.
>
>> + at item color
>> +Specify display color mode. Can be either @code{channel}: each channel
>> +is displayed in separate color, or @code{intensity}: each channel is
>
> in a separate color
>
>> +displayed using same color scheme. Default value is @code{channel}.
>
> the same color scheme.
>
>> + at item scale
>> +Specify scale used for calculating intensity color values. Can be either
>> + at code{sqrt} or @code{log}. Default value is @code{sqrt}.
>
>> + at item saturation
>> +Set saturation modifier for displayed colors. Negative values provide
>> +alternative color scheme. @code{0} is no saturation at all.
>> +Default value is @code{1}.
>
> Specify the valid range here (also is this a double or an int?). Also
> it is not clear how the color scheme is selected.
>
>>  @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 d8aed0d..f78189f 100644
>> --- a/libavfilter/avf_showspectrum.c
>> +++ b/libavfilter/avf_showspectrum.c
>> @@ -27,24 +27,36 @@
>>  #include <math.h>
>>
>>  #include "libavcodec/avfft.h"
>> +#include "libavutil/avassert.h"
>>  #include "libavutil/channel_layout.h"
>>  #include "libavutil/opt.h"
>>  #include "avfilter.h"
>>  #include "internal.h"
>>
>
>> +enum ChDisplayMode { COMBINED, SEPARATE, NB_MODES };
>
> ChDisplay is obfuscated, DisplayMode or ChannelDisplayMode would be
> fine.
>
>> +enum ColorMode     { CHANNEL, INTENSITY, NB_CLMODES };
>> +enum DisplayScale  { SQRT, LOG, NB_SCALES };
>> +
>>  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 ChDisplayMode mode;    ///< channel display mode
>> +    enum ColorMode clmode;      ///< display color scheme
>
> nit: color_mode is less obfuscated
>
>> +    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<<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 +66,36 @@ static const AVOption showspectrum_options[] = {
>>      { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str
>> = "640x512"}, 0, 0, FLAGS },
>>      { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str
>> = "640x512"}, 0, 0, FLAGS },
>>      { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT,
>> {.i64 = 0}, 0, 1, FLAGS },
>> +    { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT,
>> {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
>> +    { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED},
>> 0, 0, FLAGS, "mode" },
>> +    { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE},
>> 0, 0, FLAGS, "mode" },
>> +    { "color", "set channel coloring", OFFSET(clmode), AV_OPT_TYPE_INT,
>> {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
>> +    { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST,
>> {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
>> +    { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST,
>> {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
>> +    { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT,
>> {.i64=SQRT}, SQRT, NB_SCALES-1, FLAGS, "scale" },
>> +    { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0,
>> FLAGS, "scale" },
>> +    { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0,
>> FLAGS, "scale" },
>> +    { "saturation", "color saturation multiplier", OFFSET(saturation),
>> AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
>
> Nit: using some empty line to separate unit elements would help
> readability.
>
>>      { 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 },z
>> +    { 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 +113,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 +131,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 };
>
> any special reason for the switch?
>
>>
>>      /* set input audio formats */
>>      formats = ff_make_format_list(sample_fmts);
>> @@ -120,19 +161,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<<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 +187,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 +231,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));
>> +
>
> nit+++: weird indent
>
> [...]
>
> I leave the rest of the code to you and divverent as long as I don't
> think I can find the time to study the math behind it.

Finally applied.


More information about the ffmpeg-devel mailing list