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