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