LCOV - code coverage report
Current view: top level - src/libavfilter - avf_showfreqs.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 246 0.0 %
Date: 2017-07-21 15:41:20 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 Paul B Mahol
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include <float.h>
      22             : #include <math.h>
      23             : 
      24             : #include "libavcodec/avfft.h"
      25             : #include "libavutil/audio_fifo.h"
      26             : #include "libavutil/avassert.h"
      27             : #include "libavutil/avstring.h"
      28             : #include "libavutil/channel_layout.h"
      29             : #include "libavutil/intreadwrite.h"
      30             : #include "libavutil/opt.h"
      31             : #include "libavutil/parseutils.h"
      32             : #include "audio.h"
      33             : #include "video.h"
      34             : #include "avfilter.h"
      35             : #include "internal.h"
      36             : #include "window_func.h"
      37             : 
      38             : enum DisplayMode    { LINE, BAR, DOT, NB_MODES };
      39             : enum ChannelMode    { COMBINED, SEPARATE, NB_CMODES };
      40             : enum FrequencyScale { FS_LINEAR, FS_LOG, FS_RLOG, NB_FSCALES };
      41             : enum AmplitudeScale { AS_LINEAR, AS_SQRT, AS_CBRT, AS_LOG, NB_ASCALES };
      42             : 
      43             : typedef struct ShowFreqsContext {
      44             :     const AVClass *class;
      45             :     int w, h;
      46             :     int mode;
      47             :     int cmode;
      48             :     int fft_bits;
      49             :     int ascale, fscale;
      50             :     int avg;
      51             :     int win_func;
      52             :     FFTContext *fft;
      53             :     FFTComplex **fft_data;
      54             :     float **avg_data;
      55             :     float *window_func_lut;
      56             :     float overlap;
      57             :     float minamp;
      58             :     int hop_size;
      59             :     int nb_channels;
      60             :     int nb_freq;
      61             :     int win_size;
      62             :     float scale;
      63             :     char *colors;
      64             :     AVAudioFifo *fifo;
      65             :     int64_t pts;
      66             : } ShowFreqsContext;
      67             : 
      68             : #define OFFSET(x) offsetof(ShowFreqsContext, x)
      69             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      70             : 
      71             : static const AVOption showfreqs_options[] = {
      72             :     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
      73             :     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
      74             :     { "mode", "set display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=BAR}, 0, NB_MODES-1, FLAGS, "mode" },
      75             :         { "line", "show lines",  0, AV_OPT_TYPE_CONST, {.i64=LINE},   0, 0, FLAGS, "mode" },
      76             :         { "bar",  "show bars",   0, AV_OPT_TYPE_CONST, {.i64=BAR},    0, 0, FLAGS, "mode" },
      77             :         { "dot",  "show dots",   0, AV_OPT_TYPE_CONST, {.i64=DOT},    0, 0, FLAGS, "mode" },
      78             :     { "ascale", "set amplitude scale", OFFSET(ascale), AV_OPT_TYPE_INT, {.i64=AS_LOG}, 0, NB_ASCALES-1, FLAGS, "ascale" },
      79             :         { "lin",  "linear",      0, AV_OPT_TYPE_CONST, {.i64=AS_LINEAR}, 0, 0, FLAGS, "ascale" },
      80             :         { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=AS_SQRT},   0, 0, FLAGS, "ascale" },
      81             :         { "cbrt", "cubic root",  0, AV_OPT_TYPE_CONST, {.i64=AS_CBRT},   0, 0, FLAGS, "ascale" },
      82             :         { "log",  "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=AS_LOG},    0, 0, FLAGS, "ascale" },
      83             :     { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=FS_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
      84             :         { "lin",  "linear",              0, AV_OPT_TYPE_CONST, {.i64=FS_LINEAR}, 0, 0, FLAGS, "fscale" },
      85             :         { "log",  "logarithmic",         0, AV_OPT_TYPE_CONST, {.i64=FS_LOG},    0, 0, FLAGS, "fscale" },
      86             :         { "rlog", "reverse logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_RLOG},   0, 0, FLAGS, "fscale" },
      87             :     { "win_size", "set window size", OFFSET(fft_bits), AV_OPT_TYPE_INT, {.i64=11}, 4, 16, FLAGS, "fft" },
      88             :         { "w16",    0, 0, AV_OPT_TYPE_CONST, {.i64=4},  0, 0, FLAGS, "fft" },
      89             :         { "w32",    0, 0, AV_OPT_TYPE_CONST, {.i64=5},  0, 0, FLAGS, "fft" },
      90             :         { "w64",    0, 0, AV_OPT_TYPE_CONST, {.i64=6},  0, 0, FLAGS, "fft" },
      91             :         { "w128",   0, 0, AV_OPT_TYPE_CONST, {.i64=7},  0, 0, FLAGS, "fft" },
      92             :         { "w256",   0, 0, AV_OPT_TYPE_CONST, {.i64=8},  0, 0, FLAGS, "fft" },
      93             :         { "w512",   0, 0, AV_OPT_TYPE_CONST, {.i64=9},  0, 0, FLAGS, "fft" },
      94             :         { "w1024",  0, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, FLAGS, "fft" },
      95             :         { "w2048",  0, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, FLAGS, "fft" },
      96             :         { "w4096",  0, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, FLAGS, "fft" },
      97             :         { "w8192",  0, 0, AV_OPT_TYPE_CONST, {.i64=13}, 0, 0, FLAGS, "fft" },
      98             :         { "w16384", 0, 0, AV_OPT_TYPE_CONST, {.i64=14}, 0, 0, FLAGS, "fft" },
      99             :         { "w32768", 0, 0, AV_OPT_TYPE_CONST, {.i64=15}, 0, 0, FLAGS, "fft" },
     100             :         { "w65536", 0, 0, AV_OPT_TYPE_CONST, {.i64=16}, 0, 0, FLAGS, "fft" },
     101             :     { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
     102             :         { "rect",     "Rectangular",      0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT},     0, 0, FLAGS, "win_func" },
     103             :         { "bartlett", "Bartlett",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
     104             :         { "hanning",  "Hanning",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING},  0, 0, FLAGS, "win_func" },
     105             :         { "hamming",  "Hamming",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING},  0, 0, FLAGS, "win_func" },
     106             :         { "blackman", "Blackman",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
     107             :         { "welch",    "Welch",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH},    0, 0, FLAGS, "win_func" },
     108             :         { "flattop",  "Flat-top",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP},  0, 0, FLAGS, "win_func" },
     109             :         { "bharris",  "Blackman-Harris",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS},  0, 0, FLAGS, "win_func" },
     110             :         { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
     111             :         { "bhann",    "Bartlett-Hann",    0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN},    0, 0, FLAGS, "win_func" },
     112             :         { "sine",     "Sine",             0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE},     0, 0, FLAGS, "win_func" },
     113             :         { "nuttall",  "Nuttall",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL},  0, 0, FLAGS, "win_func" },
     114             :         { "lanczos",  "Lanczos",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS},  0, 0, FLAGS, "win_func" },
     115             :         { "gauss",    "Gauss",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS},    0, 0, FLAGS, "win_func" },
     116             :         { "tukey",    "Tukey",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY},    0, 0, FLAGS, "win_func" },
     117             :         { "dolph",    "Dolph-Chebyshev",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH},    0, 0, FLAGS, "win_func" },
     118             :         { "cauchy",   "Cauchy",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY},   0, 0, FLAGS, "win_func" },
     119             :         { "parzen",   "Parzen",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN},   0, 0, FLAGS, "win_func" },
     120             :         { "poisson",  "Poisson",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON},  0, 0, FLAGS, "win_func" },
     121             :     { "overlap",  "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=1.}, 0., 1., FLAGS },
     122             :     { "averaging", "set time averaging", OFFSET(avg), AV_OPT_TYPE_INT, {.i64=1}, 0, INT32_MAX, FLAGS },
     123             :     { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
     124             :     { "cmode", "set channel mode", OFFSET(cmode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_CMODES-1, FLAGS, "cmode" },
     125             :         { "combined", "show all channels in same window",  0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "cmode" },
     126             :         { "separate", "show each channel in own window",   0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "cmode" },
     127             :     { "minamp",  "set minimum amplitude", OFFSET(minamp), AV_OPT_TYPE_FLOAT, {.dbl=1e-6}, FLT_MIN, 1e-6, FLAGS },
     128             :     { NULL }
     129             : };
     130             : 
     131             : AVFILTER_DEFINE_CLASS(showfreqs);
     132             : 
     133           0 : static int query_formats(AVFilterContext *ctx)
     134             : {
     135           0 :     AVFilterFormats *formats = NULL;
     136           0 :     AVFilterChannelLayouts *layouts = NULL;
     137           0 :     AVFilterLink *inlink = ctx->inputs[0];
     138           0 :     AVFilterLink *outlink = ctx->outputs[0];
     139             :     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
     140             :     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
     141             :     int ret;
     142             : 
     143             :     /* set input audio formats */
     144           0 :     formats = ff_make_format_list(sample_fmts);
     145           0 :     if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
     146           0 :         return ret;
     147             : 
     148           0 :     layouts = ff_all_channel_layouts();
     149           0 :     if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
     150           0 :         return ret;
     151             : 
     152           0 :     formats = ff_all_samplerates();
     153           0 :     if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
     154           0 :         return ret;
     155             : 
     156             :     /* set output video format */
     157           0 :     formats = ff_make_format_list(pix_fmts);
     158           0 :     if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
     159           0 :         return ret;
     160             : 
     161           0 :     return 0;
     162             : }
     163             : 
     164           0 : static av_cold int init(AVFilterContext *ctx)
     165             : {
     166           0 :     ShowFreqsContext *s = ctx->priv;
     167             : 
     168           0 :     s->pts = AV_NOPTS_VALUE;
     169             : 
     170           0 :     return 0;
     171             : }
     172             : 
     173           0 : static int config_output(AVFilterLink *outlink)
     174             : {
     175           0 :     AVFilterContext *ctx = outlink->src;
     176           0 :     AVFilterLink *inlink = ctx->inputs[0];
     177           0 :     ShowFreqsContext *s = ctx->priv;
     178             :     float overlap;
     179             :     int i;
     180             : 
     181           0 :     s->nb_freq = 1 << (s->fft_bits - 1);
     182           0 :     s->win_size = s->nb_freq << 1;
     183           0 :     av_audio_fifo_free(s->fifo);
     184           0 :     av_fft_end(s->fft);
     185           0 :     s->fft = av_fft_init(s->fft_bits, 0);
     186           0 :     if (!s->fft) {
     187           0 :         av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
     188             :                "The window size might be too high.\n");
     189           0 :         return AVERROR(ENOMEM);
     190             :     }
     191             : 
     192             :     /* FFT buffers: x2 for each (display) channel buffer.
     193             :      * Note: we use free and malloc instead of a realloc-like function to
     194             :      * make sure the buffer is aligned in memory for the FFT functions. */
     195           0 :     for (i = 0; i < s->nb_channels; i++) {
     196           0 :         av_freep(&s->fft_data[i]);
     197           0 :         av_freep(&s->avg_data[i]);
     198             :     }
     199           0 :     av_freep(&s->fft_data);
     200           0 :     av_freep(&s->avg_data);
     201           0 :     s->nb_channels = inlink->channels;
     202             : 
     203           0 :     s->fft_data = av_calloc(s->nb_channels, sizeof(*s->fft_data));
     204           0 :     if (!s->fft_data)
     205           0 :         return AVERROR(ENOMEM);
     206           0 :     s->avg_data = av_calloc(s->nb_channels, sizeof(*s->avg_data));
     207           0 :     if (!s->fft_data)
     208           0 :         return AVERROR(ENOMEM);
     209           0 :     for (i = 0; i < s->nb_channels; i++) {
     210           0 :         s->fft_data[i] = av_calloc(s->win_size, sizeof(**s->fft_data));
     211           0 :         s->avg_data[i] = av_calloc(s->nb_freq, sizeof(**s->avg_data));
     212           0 :         if (!s->fft_data[i] || !s->avg_data[i])
     213           0 :             return AVERROR(ENOMEM);
     214             :     }
     215             : 
     216             :     /* pre-calc windowing function */
     217           0 :     s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size,
     218             :                                       sizeof(*s->window_func_lut));
     219           0 :     if (!s->window_func_lut)
     220           0 :         return AVERROR(ENOMEM);
     221           0 :     ff_generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
     222           0 :     if (s->overlap == 1.)
     223           0 :         s->overlap = overlap;
     224           0 :     s->hop_size = (1. - s->overlap) * s->win_size;
     225           0 :     if (s->hop_size < 1) {
     226           0 :         av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
     227           0 :         return AVERROR(EINVAL);
     228             :     }
     229             : 
     230           0 :     for (s->scale = 0, i = 0; i < s->win_size; i++) {
     231           0 :         s->scale += s->window_func_lut[i] * s->window_func_lut[i];
     232             :     }
     233             : 
     234           0 :     outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
     235           0 :     outlink->sample_aspect_ratio = (AVRational){1,1};
     236           0 :     outlink->w = s->w;
     237           0 :     outlink->h = s->h;
     238             : 
     239           0 :     s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
     240           0 :     if (!s->fifo)
     241           0 :         return AVERROR(ENOMEM);
     242           0 :     return 0;
     243             : }
     244             : 
     245           0 : static inline void draw_dot(AVFrame *out, int x, int y, uint8_t fg[4])
     246             : {
     247             : 
     248           0 :     uint32_t color = AV_RL32(out->data[0] + y * out->linesize[0] + x * 4);
     249             : 
     250           0 :     if ((color & 0xffffff) != 0)
     251           0 :         AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg) | color);
     252             :     else
     253           0 :         AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
     254           0 : }
     255             : 
     256           0 : static int get_sx(ShowFreqsContext *s, int f)
     257             : {
     258           0 :     switch (s->fscale) {
     259           0 :     case FS_LINEAR:
     260           0 :         return (s->w/(float)s->nb_freq)*f;
     261           0 :     case FS_LOG:
     262           0 :         return s->w-pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.));
     263           0 :     case FS_RLOG:
     264           0 :         return pow(s->w, f/(s->nb_freq-1.));
     265             :     }
     266             : 
     267           0 :     return 0;
     268             : }
     269             : 
     270           0 : static float get_bsize(ShowFreqsContext *s, int f)
     271             : {
     272           0 :     switch (s->fscale) {
     273           0 :     case FS_LINEAR:
     274           0 :         return s->w/(float)s->nb_freq;
     275           0 :     case FS_LOG:
     276           0 :         return pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.))-
     277           0 :                pow(s->w, (s->nb_freq-f-2)/(s->nb_freq-1.));
     278           0 :     case FS_RLOG:
     279           0 :         return pow(s->w, (f+1)/(s->nb_freq-1.))-
     280           0 :                pow(s->w,  f   /(s->nb_freq-1.));
     281             :     }
     282             : 
     283           0 :     return 1.;
     284             : }
     285             : 
     286           0 : static inline void plot_freq(ShowFreqsContext *s, int ch,
     287             :                              double a, int f, uint8_t fg[4], int *prev_y,
     288             :                              AVFrame *out, AVFilterLink *outlink)
     289             : {
     290           0 :     const int w = s->w;
     291           0 :     const float min = s->minamp;
     292           0 :     const float avg = s->avg_data[ch][f];
     293           0 :     const float bsize = get_bsize(s, f);
     294           0 :     const int sx = get_sx(s, f);
     295           0 :     int end = outlink->h;
     296             :     int x, y, i;
     297             : 
     298           0 :     switch(s->ascale) {
     299           0 :     case AS_SQRT:
     300           0 :         a = 1.0 - sqrt(a);
     301           0 :         break;
     302           0 :     case AS_CBRT:
     303           0 :         a = 1.0 - cbrt(a);
     304           0 :         break;
     305           0 :     case AS_LOG:
     306           0 :         a = log(av_clipd(a, min, 1)) / log(min);
     307           0 :         break;
     308           0 :     case AS_LINEAR:
     309           0 :         a = 1.0 - a;
     310           0 :         break;
     311             :     }
     312             : 
     313           0 :     switch (s->cmode) {
     314           0 :     case COMBINED:
     315           0 :         y = a * outlink->h - 1;
     316           0 :         break;
     317           0 :     case SEPARATE:
     318           0 :         end = (outlink->h / s->nb_channels) * (ch + 1);
     319           0 :         y = (outlink->h / s->nb_channels) * ch + a * (outlink->h / s->nb_channels) - 1;
     320           0 :         break;
     321           0 :     default:
     322           0 :         av_assert0(0);
     323             :     }
     324           0 :     if (y < 0)
     325           0 :         return;
     326             : 
     327           0 :     switch (s->avg) {
     328           0 :     case 0:
     329           0 :         y = s->avg_data[ch][f] = !outlink->frame_count_in ? y : FFMIN(avg, y);
     330           0 :         break;
     331           0 :     case 1:
     332           0 :         break;
     333           0 :     default:
     334           0 :         s->avg_data[ch][f] = avg + y * (y - avg) / (FFMIN(outlink->frame_count_in + 1, s->avg) * y);
     335           0 :         y = s->avg_data[ch][f];
     336           0 :         break;
     337             :     }
     338             : 
     339           0 :     switch(s->mode) {
     340           0 :     case LINE:
     341           0 :         if (*prev_y == -1) {
     342           0 :             *prev_y = y;
     343             :         }
     344           0 :         if (y <= *prev_y) {
     345           0 :             for (x = sx + 1; x < sx + bsize && x < w; x++)
     346           0 :                 draw_dot(out, x, y, fg);
     347           0 :             for (i = y; i <= *prev_y; i++)
     348           0 :                 draw_dot(out, sx, i, fg);
     349             :         } else {
     350           0 :             for (i = *prev_y; i <= y; i++)
     351           0 :                 draw_dot(out, sx, i, fg);
     352           0 :             for (x = sx + 1; x < sx + bsize && x < w; x++)
     353           0 :                 draw_dot(out, x, i - 1, fg);
     354             :         }
     355           0 :         *prev_y = y;
     356           0 :         break;
     357           0 :     case BAR:
     358           0 :         for (x = sx; x < sx + bsize && x < w; x++)
     359           0 :             for (i = y; i < end; i++)
     360           0 :                 draw_dot(out, x, i, fg);
     361           0 :         break;
     362           0 :     case DOT:
     363           0 :         for (x = sx; x < sx + bsize && x < w; x++)
     364           0 :             draw_dot(out, x, y, fg);
     365           0 :         break;
     366             :     }
     367             : }
     368             : 
     369           0 : static int plot_freqs(AVFilterLink *inlink, AVFrame *in)
     370             : {
     371           0 :     AVFilterContext *ctx = inlink->dst;
     372           0 :     AVFilterLink *outlink = ctx->outputs[0];
     373           0 :     ShowFreqsContext *s = ctx->priv;
     374           0 :     const int win_size = s->win_size;
     375           0 :     char *colors, *color, *saveptr = NULL;
     376             :     AVFrame *out;
     377             :     int ch, n;
     378             : 
     379           0 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     380           0 :     if (!out)
     381           0 :         return AVERROR(ENOMEM);
     382             : 
     383           0 :     for (n = 0; n < outlink->h; n++)
     384           0 :         memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4);
     385             : 
     386             :     /* fill FFT input with the number of samples available */
     387           0 :     for (ch = 0; ch < s->nb_channels; ch++) {
     388           0 :         const float *p = (float *)in->extended_data[ch];
     389             : 
     390           0 :         for (n = 0; n < in->nb_samples; n++) {
     391           0 :             s->fft_data[ch][n].re = p[n] * s->window_func_lut[n];
     392           0 :             s->fft_data[ch][n].im = 0;
     393             :         }
     394           0 :         for (; n < win_size; n++) {
     395           0 :             s->fft_data[ch][n].re = 0;
     396           0 :             s->fft_data[ch][n].im = 0;
     397             :         }
     398             :     }
     399             : 
     400             :     /* run FFT on each samples set */
     401           0 :     for (ch = 0; ch < s->nb_channels; ch++) {
     402           0 :         av_fft_permute(s->fft, s->fft_data[ch]);
     403           0 :         av_fft_calc(s->fft, s->fft_data[ch]);
     404             :     }
     405             : 
     406             : #define RE(x, ch) s->fft_data[ch][x].re
     407             : #define IM(x, ch) s->fft_data[ch][x].im
     408             : #define M(a, b) (sqrt((a) * (a) + (b) * (b)))
     409             : 
     410           0 :     colors = av_strdup(s->colors);
     411           0 :     if (!colors) {
     412           0 :         av_frame_free(&out);
     413           0 :         return AVERROR(ENOMEM);
     414             :     }
     415             : 
     416           0 :     for (ch = 0; ch < s->nb_channels; ch++) {
     417           0 :         uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
     418           0 :         int prev_y = -1, f;
     419             :         double a;
     420             : 
     421           0 :         color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
     422           0 :         if (color)
     423           0 :             av_parse_color(fg, color, -1, ctx);
     424             : 
     425           0 :         a = av_clipd(M(RE(0, ch), 0) / s->scale, 0, 1);
     426           0 :         plot_freq(s, ch, a, 0, fg, &prev_y, out, outlink);
     427             : 
     428           0 :         for (f = 1; f < s->nb_freq; f++) {
     429           0 :             a = av_clipd(M(RE(f, ch), IM(f, ch)) / s->scale, 0, 1);
     430             : 
     431           0 :             plot_freq(s, ch, a, f, fg, &prev_y, out, outlink);
     432             :         }
     433             :     }
     434             : 
     435           0 :     av_free(colors);
     436           0 :     out->pts = in->pts;
     437           0 :     out->sample_aspect_ratio = (AVRational){1,1};
     438           0 :     return ff_filter_frame(outlink, out);
     439             : }
     440             : 
     441           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     442             : {
     443           0 :     AVFilterContext *ctx = inlink->dst;
     444           0 :     ShowFreqsContext *s = ctx->priv;
     445           0 :     AVFrame *fin = NULL;
     446           0 :     int consumed = 0;
     447           0 :     int ret = 0;
     448             : 
     449           0 :     if (s->pts == AV_NOPTS_VALUE)
     450           0 :         s->pts = in->pts - av_audio_fifo_size(s->fifo);
     451             : 
     452           0 :     av_audio_fifo_write(s->fifo, (void **)in->extended_data, in->nb_samples);
     453           0 :     while (av_audio_fifo_size(s->fifo) >= s->win_size) {
     454           0 :         fin = ff_get_audio_buffer(inlink, s->win_size);
     455           0 :         if (!fin) {
     456           0 :             ret = AVERROR(ENOMEM);
     457           0 :             goto fail;
     458             :         }
     459             : 
     460           0 :         fin->pts = s->pts + consumed;
     461           0 :         consumed += s->hop_size;
     462           0 :         ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
     463           0 :         if (ret < 0)
     464           0 :             goto fail;
     465             : 
     466           0 :         ret = plot_freqs(inlink, fin);
     467           0 :         av_frame_free(&fin);
     468           0 :         av_audio_fifo_drain(s->fifo, s->hop_size);
     469           0 :         if (ret < 0)
     470           0 :             goto fail;
     471             :     }
     472             : 
     473           0 : fail:
     474           0 :     s->pts = AV_NOPTS_VALUE;
     475           0 :     av_frame_free(&fin);
     476           0 :     av_frame_free(&in);
     477           0 :     return ret;
     478             : }
     479             : 
     480           0 : static av_cold void uninit(AVFilterContext *ctx)
     481             : {
     482           0 :     ShowFreqsContext *s = ctx->priv;
     483             :     int i;
     484             : 
     485           0 :     av_fft_end(s->fft);
     486           0 :     for (i = 0; i < s->nb_channels; i++) {
     487           0 :         if (s->fft_data)
     488           0 :             av_freep(&s->fft_data[i]);
     489           0 :         if (s->avg_data)
     490           0 :             av_freep(&s->avg_data[i]);
     491             :     }
     492           0 :     av_freep(&s->fft_data);
     493           0 :     av_freep(&s->avg_data);
     494           0 :     av_freep(&s->window_func_lut);
     495           0 :     av_audio_fifo_free(s->fifo);
     496           0 : }
     497             : 
     498             : static const AVFilterPad showfreqs_inputs[] = {
     499             :     {
     500             :         .name         = "default",
     501             :         .type         = AVMEDIA_TYPE_AUDIO,
     502             :         .filter_frame = filter_frame,
     503             :     },
     504             :     { NULL }
     505             : };
     506             : 
     507             : static const AVFilterPad showfreqs_outputs[] = {
     508             :     {
     509             :         .name          = "default",
     510             :         .type          = AVMEDIA_TYPE_VIDEO,
     511             :         .config_props  = config_output,
     512             :     },
     513             :     { NULL }
     514             : };
     515             : 
     516             : AVFilter ff_avf_showfreqs = {
     517             :     .name          = "showfreqs",
     518             :     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to a frequencies video output."),
     519             :     .init          = init,
     520             :     .uninit        = uninit,
     521             :     .query_formats = query_formats,
     522             :     .priv_size     = sizeof(ShowFreqsContext),
     523             :     .inputs        = showfreqs_inputs,
     524             :     .outputs       = showfreqs_outputs,
     525             :     .priv_class    = &showfreqs_class,
     526             : };

Generated by: LCOV version 1.13