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