]> git.sesse.net Git - ffmpeg/blob - libavfilter/avf_showfreqs.c
Merge commit 'c9943f00cfa2471d1b8a3a9ddc7a21049a71090e'
[ffmpeg] / libavfilter / avf_showfreqs.c
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 <math.h>
22
23 #include "libavcodec/avfft.h"
24 #include "libavutil/audio_fifo.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/parseutils.h"
31 #include "audio.h"
32 #include "video.h"
33 #include "avfilter.h"
34 #include "internal.h"
35
36 enum DisplayMode    { LINE, BAR, DOT, NB_MODES };
37 enum FrequencyScale { FS_LINEAR, FS_LOG, FS_RLOG, NB_FSCALES };
38 enum AmplitudeScale { AS_LINEAR, AS_SQRT, AS_CBRT, AS_LOG, NB_ASCALES };
39 enum WindowFunc     { WFUNC_RECT, WFUNC_HANNING, WFUNC_HAMMING, WFUNC_BLACKMAN,
40                       WFUNC_BARTLETT, WFUNC_WELCH, WFUNC_FLATTOP,
41                       WFUNC_BHARRIS, WFUNC_BNUTTALL, WFUNC_SINE, WFUNC_NUTTALL,
42                       WFUNC_BHANN, WFUNC_LANCZOS, WFUNC_GAUSS, NB_WFUNC };
43
44 typedef struct ShowFreqsContext {
45     const AVClass *class;
46     int w, h;
47     int mode;
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     int skip_samples;
58     int nb_channels;
59     int nb_freq;
60     int win_size;
61     float scale;
62     char *colors;
63     AVAudioFifo *fifo;
64     int64_t pts;
65 } ShowFreqsContext;
66
67 #define OFFSET(x) offsetof(ShowFreqsContext, x)
68 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
69
70 static const AVOption showfreqs_options[] = {
71     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
72     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
73     { "mode", "set display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=BAR}, 0, NB_MODES-1, FLAGS, "mode" },
74         { "line", "show lines",  0, AV_OPT_TYPE_CONST, {.i64=LINE},   0, 0, FLAGS, "mode" },
75         { "bar",  "show bars",   0, AV_OPT_TYPE_CONST, {.i64=BAR},    0, 0, FLAGS, "mode" },
76         { "dot",  "show dots",   0, AV_OPT_TYPE_CONST, {.i64=DOT},    0, 0, FLAGS, "mode" },
77     { "ascale", "set amplitude scale", OFFSET(ascale), AV_OPT_TYPE_INT, {.i64=AS_LOG}, 0, NB_ASCALES-1, FLAGS, "ascale" },
78         { "lin",  "linear",      0, AV_OPT_TYPE_CONST, {.i64=AS_LINEAR}, 0, 0, FLAGS, "ascale" },
79         { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=AS_SQRT},   0, 0, FLAGS, "ascale" },
80         { "cbrt", "cubic root",  0, AV_OPT_TYPE_CONST, {.i64=AS_CBRT},   0, 0, FLAGS, "ascale" },
81         { "log",  "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=AS_LOG},    0, 0, FLAGS, "ascale" },
82     { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=FS_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
83         { "lin",  "linear",              0, AV_OPT_TYPE_CONST, {.i64=FS_LINEAR}, 0, 0, FLAGS, "fscale" },
84         { "log",  "logarithmic",         0, AV_OPT_TYPE_CONST, {.i64=FS_LOG},    0, 0, FLAGS, "fscale" },
85         { "rlog", "reverse logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_RLOG},   0, 0, FLAGS, "fscale" },
86     { "win_size", "set window size", OFFSET(fft_bits), AV_OPT_TYPE_INT, {.i64=11}, 4, 16, FLAGS, "fft" },
87         { "w16",    0, 0, AV_OPT_TYPE_CONST, {.i64=4},  0, 0, FLAGS, "fft" },
88         { "w32",    0, 0, AV_OPT_TYPE_CONST, {.i64=5},  0, 0, FLAGS, "fft" },
89         { "w64",    0, 0, AV_OPT_TYPE_CONST, {.i64=6},  0, 0, FLAGS, "fft" },
90         { "w128",   0, 0, AV_OPT_TYPE_CONST, {.i64=7},  0, 0, FLAGS, "fft" },
91         { "w256",   0, 0, AV_OPT_TYPE_CONST, {.i64=8},  0, 0, FLAGS, "fft" },
92         { "w512",   0, 0, AV_OPT_TYPE_CONST, {.i64=9},  0, 0, FLAGS, "fft" },
93         { "w1024",  0, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, FLAGS, "fft" },
94         { "w2048",  0, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, FLAGS, "fft" },
95         { "w4096",  0, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, FLAGS, "fft" },
96         { "w8192",  0, 0, AV_OPT_TYPE_CONST, {.i64=13}, 0, 0, FLAGS, "fft" },
97         { "w16384", 0, 0, AV_OPT_TYPE_CONST, {.i64=14}, 0, 0, FLAGS, "fft" },
98         { "w32768", 0, 0, AV_OPT_TYPE_CONST, {.i64=15}, 0, 0, FLAGS, "fft" },
99         { "w65536", 0, 0, AV_OPT_TYPE_CONST, {.i64=16}, 0, 0, FLAGS, "fft" },
100     { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
101         { "rect",     "Rectangular",      0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT},     0, 0, FLAGS, "win_func" },
102         { "bartlett", "Bartlett",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
103         { "hanning",  "Hanning",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING},  0, 0, FLAGS, "win_func" },
104         { "hamming",  "Hamming",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING},  0, 0, FLAGS, "win_func" },
105         { "blackman", "Blackman",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
106         { "welch",    "Welch",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH},    0, 0, FLAGS, "win_func" },
107         { "flattop",  "Flat-top",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP},  0, 0, FLAGS, "win_func" },
108         { "bharris",  "Blackman-Harris",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS},  0, 0, FLAGS, "win_func" },
109         { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
110         { "bhann",    "Bartlett-Hann",    0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN},    0, 0, FLAGS, "win_func" },
111         { "sine",     "Sine",             0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE},     0, 0, FLAGS, "win_func" },
112         { "nuttall",  "Nuttall",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL},  0, 0, FLAGS, "win_func" },
113         { "lanczos",  "Lanczos",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS},  0, 0, FLAGS, "win_func" },
114         { "gauss",    "Gauss",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS},    0, 0, FLAGS, "win_func" },
115     { "overlap",  "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=1.}, 0., 1., FLAGS },
116     { "averaging", "set time averaging", OFFSET(avg), AV_OPT_TYPE_INT, {.i64=1}, 0, INT32_MAX, FLAGS },
117     { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
118     { NULL }
119 };
120
121 AVFILTER_DEFINE_CLASS(showfreqs);
122
123 static int query_formats(AVFilterContext *ctx)
124 {
125     AVFilterFormats *formats = NULL;
126     AVFilterChannelLayouts *layouts = NULL;
127     AVFilterLink *inlink = ctx->inputs[0];
128     AVFilterLink *outlink = ctx->outputs[0];
129     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
130     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
131
132     /* set input audio formats */
133     formats = ff_make_format_list(sample_fmts);
134     if (!formats)
135         return AVERROR(ENOMEM);
136     ff_formats_ref(formats, &inlink->out_formats);
137
138     layouts = ff_all_channel_layouts();
139     if (!layouts)
140         return AVERROR(ENOMEM);
141     ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
142
143     formats = ff_all_samplerates();
144     if (!formats)
145         return AVERROR(ENOMEM);
146     ff_formats_ref(formats, &inlink->out_samplerates);
147
148     /* set output video format */
149     formats = ff_make_format_list(pix_fmts);
150     if (!formats)
151         return AVERROR(ENOMEM);
152     ff_formats_ref(formats, &outlink->in_formats);
153
154     return 0;
155 }
156
157 static void generate_window_func(float *lut, int N, int win_func, float *overlap)
158 {
159     int n;
160
161     switch (win_func) {
162     case WFUNC_RECT:
163         for (n = 0; n < N; n++)
164             lut[n] = 1.;
165         *overlap = 0.;
166         break;
167     case WFUNC_BARTLETT:
168         for (n = 0; n < N; n++)
169             lut[n] = 1.-FFABS((n-(N-1)/2.)/((N-1)/2.));
170         *overlap = 0.5;
171         break;
172     case WFUNC_HANNING:
173         for (n = 0; n < N; n++)
174             lut[n] = .5*(1-cos(2*M_PI*n/(N-1)));
175         *overlap = 0.5;
176         break;
177     case WFUNC_HAMMING:
178         for (n = 0; n < N; n++)
179             lut[n] = .54-.46*cos(2*M_PI*n/(N-1));
180         *overlap = 0.5;
181         break;
182     case WFUNC_BLACKMAN:
183         for (n = 0; n < N; n++)
184             lut[n] = .42659-.49656*cos(2*M_PI*n/(N-1))+.076849*cos(4*M_PI*n/(N-1));
185         *overlap = 0.661;
186         break;
187     case WFUNC_WELCH:
188         for (n = 0; n < N; n++)
189             lut[n] = 1.-(n-(N-1)/2.)/((N-1)/2.)*(n-(N-1)/2.)/((N-1)/2.);
190         *overlap = 0.293;
191         break;
192     case WFUNC_FLATTOP:
193         for (n = 0; n < N; n++)
194             lut[n] = 1.-1.985844164102*cos( 2*M_PI*n/(N-1))+1.791176438506*cos( 4*M_PI*n/(N-1))-
195                         1.282075284005*cos( 6*M_PI*n/(N-1))+0.667777530266*cos( 8*M_PI*n/(N-1))-
196                         0.240160796576*cos(10*M_PI*n/(N-1))+0.056656381764*cos(12*M_PI*n/(N-1))-
197                         0.008134974479*cos(14*M_PI*n/(N-1))+0.000624544650*cos(16*M_PI*n/(N-1))-
198                         0.000019808998*cos(18*M_PI*n/(N-1))+0.000000132974*cos(20*M_PI*n/(N-1));
199         *overlap = 0.841;
200         break;
201     case WFUNC_BHARRIS:
202         for (n = 0; n < N; n++)
203             lut[n] = 0.35875-0.48829*cos(2*M_PI*n/(N-1))+0.14128*cos(4*M_PI*n/(N-1))-0.01168*cos(6*M_PI*n/(N-1));
204         *overlap = 0.661;
205         break;
206     case WFUNC_BNUTTALL:
207         for (n = 0; n < N; n++)
208             lut[n] = 0.3635819-0.4891775*cos(2*M_PI*n/(N-1))+0.1365995*cos(4*M_PI*n/(N-1))-0.0106411*cos(6*M_PI*n/(N-1));
209         *overlap = 0.661;
210         break;
211     case WFUNC_BHANN:
212         for (n = 0; n < N; n++)
213             lut[n] = 0.62-0.48*FFABS(n/(double)(N-1)-.5)-0.38*cos(2*M_PI*n/(N-1));
214         *overlap = 0.5;
215         break;
216     case WFUNC_SINE:
217         for (n = 0; n < N; n++)
218             lut[n] = sin(M_PI*n/(N-1));
219         *overlap = 0.75;
220         break;
221     case WFUNC_NUTTALL:
222         for (n = 0; n < N; n++)
223             lut[n] = 0.355768-0.487396*cos(2*M_PI*n/(N-1))+0.144232*cos(4*M_PI*n/(N-1))-0.012604*cos(6*M_PI*n/(N-1));
224         *overlap = 0.663;
225         break;
226     case WFUNC_LANCZOS:
227 #define SINC(x) (!(x)) ? 1 : sin(M_PI * (x))/(M_PI * (x));
228         for (n = 0; n < N; n++)
229             lut[n] = SINC((2.*n)/(N-1)-1);
230         *overlap = 0.75;
231         break;
232     case WFUNC_GAUSS:
233         for (n = 0; n < N; n++)
234             lut[n] = pow(M_E,-0.5*pow((n-(N-1)/2)/(0.4*(N-1)/2.f),2));
235         *overlap = 0.75;
236         break;
237     default:
238         av_assert0(0);
239     }
240 }
241
242 static int config_output(AVFilterLink *outlink)
243 {
244     AVFilterContext *ctx = outlink->src;
245     AVFilterLink *inlink = ctx->inputs[0];
246     ShowFreqsContext *s = ctx->priv;
247     float overlap;
248     int i;
249
250     s->nb_freq = 1 << (s->fft_bits - 1);
251     s->win_size = s->nb_freq << 1;
252     av_audio_fifo_free(s->fifo);
253     av_fft_end(s->fft);
254     s->fft = av_fft_init(s->fft_bits, 0);
255     if (!s->fft) {
256         av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
257                "The window size might be too high.\n");
258         return AVERROR(ENOMEM);
259     }
260
261     /* FFT buffers: x2 for each (display) channel buffer.
262      * Note: we use free and malloc instead of a realloc-like function to
263      * make sure the buffer is aligned in memory for the FFT functions. */
264     for (i = 0; i < s->nb_channels; i++) {
265         av_freep(&s->fft_data[i]);
266         av_freep(&s->avg_data[i]);
267     }
268     av_freep(&s->fft_data);
269     av_freep(&s->avg_data);
270     s->nb_channels = inlink->channels;
271
272     s->fft_data = av_calloc(s->nb_channels, sizeof(*s->fft_data));
273     if (!s->fft_data)
274         return AVERROR(ENOMEM);
275     s->avg_data = av_calloc(s->nb_channels, sizeof(*s->avg_data));
276     if (!s->fft_data)
277         return AVERROR(ENOMEM);
278     for (i = 0; i < s->nb_channels; i++) {
279         s->fft_data[i] = av_calloc(s->win_size, sizeof(**s->fft_data));
280         s->avg_data[i] = av_calloc(s->nb_freq, sizeof(**s->avg_data));
281         if (!s->fft_data[i] || !s->avg_data[i])
282             return AVERROR(ENOMEM);
283     }
284
285     /* pre-calc windowing function */
286     s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size,
287                                       sizeof(*s->window_func_lut));
288     if (!s->window_func_lut)
289         return AVERROR(ENOMEM);
290     generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
291     if (s->overlap == 1.)
292         s->overlap = overlap;
293     s->skip_samples = (1. - s->overlap) * s->win_size;
294     if (s->skip_samples < 1) {
295         av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
296         return AVERROR(EINVAL);
297     }
298
299     for (s->scale = 0, i = 0; i < s->win_size; i++) {
300         s->scale += s->window_func_lut[i] * s->window_func_lut[i];
301     }
302
303     outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
304     outlink->sample_aspect_ratio = (AVRational){1,1};
305     outlink->w = s->w;
306     outlink->h = s->h;
307
308     s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
309     if (!s->fifo)
310         return AVERROR(ENOMEM);
311     return 0;
312 }
313
314 static inline void draw_dot(AVFrame *out, int x, int y, uint8_t fg[4])
315 {
316
317     uint32_t color = AV_RL32(out->data[0] + y * out->linesize[0] + x * 4);
318
319     if ((color & 0xffffff) != 0)
320         AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg) | color);
321     else
322         AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
323 }
324
325 static int get_sx(ShowFreqsContext *s, int f)
326 {
327     switch (s->fscale) {
328     case FS_LINEAR:
329         return (s->w/(float)s->nb_freq)*f;
330     case FS_LOG:
331         return s->w-pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.));
332     case FS_RLOG:
333         return pow(s->w, f/(s->nb_freq-1.));
334     }
335
336     return 0;
337 }
338
339 static float get_bsize(ShowFreqsContext *s, int f)
340 {
341     switch (s->fscale) {
342     case FS_LINEAR:
343         return s->w/(float)s->nb_freq;
344     case FS_LOG:
345         return pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.))-
346                pow(s->w, (s->nb_freq-f-2)/(s->nb_freq-1.));
347     case FS_RLOG:
348         return pow(s->w, (f+1)/(s->nb_freq-1.))-
349                pow(s->w,  f   /(s->nb_freq-1.));
350     }
351
352     return 1.;
353 }
354
355 static inline void plot_freq(ShowFreqsContext *s, int ch,
356                              double a, int f, uint8_t fg[4], int *prev_y,
357                              AVFrame *out, AVFilterLink *outlink)
358 {
359     const int w = s->w;
360     const float avg = s->avg_data[ch][f];
361     const float bsize = get_bsize(s, f);
362     const int sx = get_sx(s, f);
363     int x, y, i;
364
365     switch(s->ascale) {
366     case AS_SQRT:
367         a = 1.0 - sqrt(a);
368         break;
369     case AS_CBRT:
370         a = 1.0 - cbrt(a);
371         break;
372     case AS_LOG:
373         a = log(av_clipd(a, 1e-6, 1)) / log(1e-6);
374         break;
375     case AS_LINEAR:
376         a = 1.0 - a;
377         break;
378     }
379     y = a * outlink->h - 1;
380     if (y < 0)
381         return;
382
383     switch (s->avg) {
384     case 0:
385         y = s->avg_data[ch][f] = !outlink->frame_count ? y : FFMIN(avg, y);
386         break;
387     case 1:
388         break;
389     default:
390         s->avg_data[ch][f] = avg + y * (y - avg) / (FFMIN(outlink->frame_count + 1, s->avg) * y);
391         y = s->avg_data[ch][f];
392         break;
393     }
394
395     switch(s->mode) {
396     case LINE:
397         if (*prev_y == -1) {
398             *prev_y = y;
399         }
400         if (y <= *prev_y) {
401             for (x = sx + 1; x < sx + bsize && x < w; x++)
402                 draw_dot(out, x, y, fg);
403             for (i = y; i <= *prev_y; i++)
404                 draw_dot(out, sx, i, fg);
405         } else {
406             for (i = *prev_y; i <= y; i++)
407                 draw_dot(out, sx, i, fg);
408             for (x = sx + 1; x < sx + bsize && x < w; x++)
409                 draw_dot(out, x, i - 1, fg);
410         }
411         *prev_y = y;
412         break;
413     case BAR:
414         for (x = sx; x < sx + bsize && x < w; x++)
415             for (i = y; i < outlink->h; i++)
416                 draw_dot(out, x, i, fg);
417         break;
418     case DOT:
419         for (x = sx; x < sx + bsize && x < w; x++)
420             draw_dot(out, x, y, fg);
421         break;
422     }
423 }
424
425 static int plot_freqs(AVFilterLink *inlink, AVFrame *in)
426 {
427     AVFilterContext *ctx = inlink->dst;
428     AVFilterLink *outlink = ctx->outputs[0];
429     ShowFreqsContext *s = ctx->priv;
430     const int win_size = s->win_size;
431     char *colors, *color, *saveptr = NULL;
432     AVFrame *out;
433     int ch, n;
434
435     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
436     if (!out)
437         return AVERROR(ENOMEM);
438
439     for (n = 0; n < outlink->h; n++)
440         memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4);
441
442     /* fill FFT input with the number of samples available */
443     for (ch = 0; ch < s->nb_channels; ch++) {
444         const float *p = (float *)in->extended_data[ch];
445
446         for (n = 0; n < in->nb_samples; n++) {
447             s->fft_data[ch][n].re = p[n] * s->window_func_lut[n];
448             s->fft_data[ch][n].im = 0;
449         }
450         for (; n < win_size; n++) {
451             s->fft_data[ch][n].re = 0;
452             s->fft_data[ch][n].im = 0;
453         }
454     }
455
456     /* run FFT on each samples set */
457     for (ch = 0; ch < s->nb_channels; ch++) {
458         av_fft_permute(s->fft, s->fft_data[ch]);
459         av_fft_calc(s->fft, s->fft_data[ch]);
460     }
461
462 #define RE(x, ch) s->fft_data[ch][x].re
463 #define IM(x, ch) s->fft_data[ch][x].im
464 #define M(a, b) (sqrt((a) * (a) + (b) * (b)))
465
466     colors = av_strdup(s->colors);
467     if (!colors) {
468         av_frame_free(&out);
469         return AVERROR(ENOMEM);
470     }
471
472     for (ch = 0; ch < s->nb_channels; ch++) {
473         uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
474         int prev_y = -1, f;
475         double a;
476
477         color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
478         if (color)
479             av_parse_color(fg, color, -1, ctx);
480
481         a = av_clipd(M(RE(0, ch), 0) / s->scale, 0, 1);
482         plot_freq(s, ch, a, 0, fg, &prev_y, out, outlink);
483
484         for (f = 1; f < s->nb_freq; f++) {
485             a = av_clipd(M(RE(f, ch), IM(f, ch)) / s->scale, 0, 1);
486
487             plot_freq(s, ch, a, f, fg, &prev_y, out, outlink);
488         }
489     }
490
491     av_free(colors);
492     out->pts = in->pts;
493     return ff_filter_frame(outlink, out);
494 }
495
496 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
497 {
498     AVFilterContext *ctx = inlink->dst;
499     ShowFreqsContext *s = ctx->priv;
500     AVFrame *fin = NULL;
501     int ret = 0;
502
503     av_audio_fifo_write(s->fifo, (void **)in->extended_data, in->nb_samples);
504     while (av_audio_fifo_size(s->fifo) >= s->win_size) {
505         fin = ff_get_audio_buffer(inlink, s->win_size);
506         if (!fin) {
507             ret = AVERROR(ENOMEM);
508             goto fail;
509         }
510
511         fin->pts = s->pts;
512         s->pts += s->skip_samples;
513         ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
514         if (ret < 0)
515             goto fail;
516
517         ret = plot_freqs(inlink, fin);
518         av_frame_free(&fin);
519         av_audio_fifo_drain(s->fifo, s->skip_samples);
520         if (ret < 0)
521             goto fail;
522     }
523
524 fail:
525     av_frame_free(&fin);
526     av_frame_free(&in);
527     return ret;
528 }
529
530 static av_cold void uninit(AVFilterContext *ctx)
531 {
532     ShowFreqsContext *s = ctx->priv;
533     int i;
534
535     av_fft_end(s->fft);
536     for (i = 0; i < s->nb_channels; i++) {
537         av_freep(&s->fft_data[i]);
538         av_freep(&s->avg_data[i]);
539     }
540     av_freep(&s->fft_data);
541     av_freep(&s->avg_data);
542     av_freep(&s->window_func_lut);
543     av_audio_fifo_free(s->fifo);
544 }
545
546 static const AVFilterPad showfreqs_inputs[] = {
547     {
548         .name         = "default",
549         .type         = AVMEDIA_TYPE_AUDIO,
550         .filter_frame = filter_frame,
551     },
552     { NULL }
553 };
554
555 static const AVFilterPad showfreqs_outputs[] = {
556     {
557         .name          = "default",
558         .type          = AVMEDIA_TYPE_VIDEO,
559         .config_props  = config_output,
560     },
561     { NULL }
562 };
563
564 AVFilter ff_avf_showfreqs = {
565     .name          = "showfreqs",
566     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to a frequencies video output."),
567     .uninit        = uninit,
568     .query_formats = query_formats,
569     .priv_size     = sizeof(ShowFreqsContext),
570     .inputs        = showfreqs_inputs,
571     .outputs       = showfreqs_outputs,
572     .priv_class    = &showfreqs_class,
573 };