]> git.sesse.net Git - ffmpeg/blob - libavfilter/avf_showspectrum.c
avfilter/avf_showspectrum: fix scaling in zoom mode
[ffmpeg] / libavfilter / avf_showspectrum.c
1 /*
2  * Copyright (c) 2012-2013 Clément Bœsch
3  * Copyright (c) 2013 Rudolf Polzer <divverent@xonotic.org>
4  * Copyright (c) 2015 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * audio to spectrum (video) transmedia filter, based on ffplay rdft showmode
26  * (by Michael Niedermayer) and lavfi/avf_showwaves (by Stefano Sabatini).
27  */
28
29 #include <math.h>
30
31 #include "libavcodec/avfft.h"
32 #include "libavutil/audio_fifo.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/channel_layout.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/xga_font_data.h"
38 #include "audio.h"
39 #include "video.h"
40 #include "avfilter.h"
41 #include "internal.h"
42 #include "window_func.h"
43
44 enum DisplayMode  { COMBINED, SEPARATE, NB_MODES };
45 enum DataMode     { D_MAGNITUDE, D_PHASE, NB_DMODES };
46 enum DisplayScale { LINEAR, SQRT, CBRT, LOG, FOURTHRT, FIFTHRT, NB_SCALES };
47 enum ColorMode    { CHANNEL, INTENSITY, RAINBOW, MORELAND, NEBULAE, FIRE, FIERY, FRUIT, COOL, MAGMA, NB_CLMODES };
48 enum SlideMode    { REPLACE, SCROLL, FULLFRAME, RSCROLL, NB_SLIDES };
49 enum Orientation  { VERTICAL, HORIZONTAL, NB_ORIENTATIONS };
50
51 typedef struct ShowSpectrumContext {
52     const AVClass *class;
53     int w, h;
54     AVFrame *outpicref;
55     int nb_display_channels;
56     int orientation;
57     int channel_width;
58     int channel_height;
59     int sliding;                ///< 1 if sliding mode, 0 otherwise
60     int mode;                   ///< channel display mode
61     int color_mode;             ///< display color scheme
62     int scale;
63     float saturation;           ///< color saturation multiplier
64     float rotation;             ///< color rotation
65     int start, stop;            ///< zoom mode
66     int data;
67     int xpos;                   ///< x position (current column)
68     FFTContext **fft;           ///< Fast Fourier Transform context
69     FFTContext **ifft;          ///< Inverse Fast Fourier Transform context
70     int fft_bits;               ///< number of bits (FFT window size = 1<<fft_bits)
71     FFTComplex **fft_data;      ///< bins holder for each (displayed) channels
72     FFTComplex **fft_scratch;   ///< scratch buffers
73     float *window_func_lut;     ///< Window function LUT
74     float **magnitudes;
75     float **phases;
76     int win_func;
77     int win_size;
78     int buf_size;
79     double win_scale;
80     float overlap;
81     float gain;
82     int hop_size;
83     float *combine_buffer;      ///< color combining buffer (3 * h items)
84     float **color_buffer;       ///< color buffer (3 * h * ch items)
85     AVAudioFifo *fifo;
86     int64_t pts;
87     int single_pic;
88     int legend;
89     int start_x, start_y;
90 } ShowSpectrumContext;
91
92 #define OFFSET(x) offsetof(ShowSpectrumContext, x)
93 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
94
95 static const AVOption showspectrum_options[] = {
96     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
97     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
98     { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
99         { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
100         { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
101         { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
102         { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
103     { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
104         { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
105         { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
106     { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
107         { "channel",   "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL},   0, 0, FLAGS, "color" },
108         { "intensity", "intensity based coloring",        0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
109         { "rainbow",   "rainbow based coloring",          0, AV_OPT_TYPE_CONST, {.i64=RAINBOW},   0, 0, FLAGS, "color" },
110         { "moreland",  "moreland based coloring",         0, AV_OPT_TYPE_CONST, {.i64=MORELAND},  0, 0, FLAGS, "color" },
111         { "nebulae",   "nebulae based coloring",          0, AV_OPT_TYPE_CONST, {.i64=NEBULAE},   0, 0, FLAGS, "color" },
112         { "fire",      "fire based coloring",             0, AV_OPT_TYPE_CONST, {.i64=FIRE},      0, 0, FLAGS, "color" },
113         { "fiery",     "fiery based coloring",            0, AV_OPT_TYPE_CONST, {.i64=FIERY},     0, 0, FLAGS, "color" },
114         { "fruit",     "fruit based coloring",            0, AV_OPT_TYPE_CONST, {.i64=FRUIT},     0, 0, FLAGS, "color" },
115         { "cool",      "cool based coloring",             0, AV_OPT_TYPE_CONST, {.i64=COOL},      0, 0, FLAGS, "color" },
116         { "magma",     "magma based coloring",            0, AV_OPT_TYPE_CONST, {.i64=MAGMA},     0, 0, FLAGS, "color" },
117     { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
118         { "lin",  "linear",      0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
119         { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT},   0, 0, FLAGS, "scale" },
120         { "cbrt", "cubic root",  0, AV_OPT_TYPE_CONST, {.i64=CBRT},   0, 0, FLAGS, "scale" },
121         { "log",  "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG},    0, 0, FLAGS, "scale" },
122         { "4thrt","4th root",    0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
123         { "5thrt","5th root",    0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT},  0, 0, FLAGS, "scale" },
124     { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
125     { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
126         { "rect",     "Rectangular",      0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT},     0, 0, FLAGS, "win_func" },
127         { "bartlett", "Bartlett",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
128         { "hann",     "Hann",             0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING},  0, 0, FLAGS, "win_func" },
129         { "hanning",  "Hanning",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING},  0, 0, FLAGS, "win_func" },
130         { "hamming",  "Hamming",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING},  0, 0, FLAGS, "win_func" },
131         { "blackman", "Blackman",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
132         { "welch",    "Welch",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH},    0, 0, FLAGS, "win_func" },
133         { "flattop",  "Flat-top",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP},  0, 0, FLAGS, "win_func" },
134         { "bharris",  "Blackman-Harris",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS},  0, 0, FLAGS, "win_func" },
135         { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
136         { "bhann",    "Bartlett-Hann",    0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN},    0, 0, FLAGS, "win_func" },
137         { "sine",     "Sine",             0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE},     0, 0, FLAGS, "win_func" },
138         { "nuttall",  "Nuttall",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL},  0, 0, FLAGS, "win_func" },
139         { "lanczos",  "Lanczos",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS},  0, 0, FLAGS, "win_func" },
140         { "gauss",    "Gauss",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS},    0, 0, FLAGS, "win_func" },
141         { "tukey",    "Tukey",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY},    0, 0, FLAGS, "win_func" },
142         { "dolph",    "Dolph-Chebyshev",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH},    0, 0, FLAGS, "win_func" },
143         { "cauchy",   "Cauchy",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY},   0, 0, FLAGS, "win_func" },
144         { "parzen",   "Parzen",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN},   0, 0, FLAGS, "win_func" },
145         { "poisson",  "Poisson",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON},  0, 0, FLAGS, "win_func" },
146     { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
147         { "vertical",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL},   0, 0, FLAGS, "orientation" },
148         { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
149     { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS },
150     { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
151     { "data", "set data mode", OFFSET(data), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_DMODES-1, FLAGS, "data" },
152         { "magnitude", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_MAGNITUDE}, 0, 0, FLAGS, "data" },
153         { "phase",     NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_PHASE},     0, 0, FLAGS, "data" },
154     { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
155     { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
156     { "stop",  "stop frequency",  OFFSET(stop),  AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
157     { NULL }
158 };
159
160 AVFILTER_DEFINE_CLASS(showspectrum);
161
162 static const struct ColorTable {
163     float a, y, u, v;
164 } color_table[][8] = {
165     [INTENSITY] = {
166     {    0,                  0,                  0,                   0 },
167     { 0.13, .03587126228984074,  .1573300977624594, -.02548747583751842 },
168     { 0.30, .18572281794568020,  .1772436246393981,  .17475554840414750 },
169     { 0.60, .28184980583656130, -.1593064119945782,  .47132074554608920 },
170     { 0.73, .65830621175547810, -.3716070802232764,  .24352759331252930 },
171     { 0.78, .76318535758242900, -.4307467689263783,  .16866496622310430 },
172     { 0.91, .95336363636363640, -.2045454545454546,  .03313636363636363 },
173     {    1,                  1,                  0,                   0 }},
174     [RAINBOW] = {
175     {    0,                  0,                  0,                   0 },
176     { 0.13,            44/256.,     (189-128)/256.,      (138-128)/256. },
177     { 0.25,            29/256.,     (186-128)/256.,      (119-128)/256. },
178     { 0.38,           119/256.,     (194-128)/256.,       (53-128)/256. },
179     { 0.60,           111/256.,      (73-128)/256.,       (59-128)/256. },
180     { 0.73,           205/256.,      (19-128)/256.,      (149-128)/256. },
181     { 0.86,           135/256.,      (83-128)/256.,      (200-128)/256. },
182     {    1,            73/256.,      (95-128)/256.,      (225-128)/256. }},
183     [MORELAND] = {
184     {    0,            44/256.,     (181-128)/256.,      (112-128)/256. },
185     { 0.13,           126/256.,     (177-128)/256.,      (106-128)/256. },
186     { 0.25,           164/256.,     (163-128)/256.,      (109-128)/256. },
187     { 0.38,           200/256.,     (140-128)/256.,      (120-128)/256. },
188     { 0.60,           201/256.,     (117-128)/256.,      (141-128)/256. },
189     { 0.73,           177/256.,     (103-128)/256.,      (165-128)/256. },
190     { 0.86,           136/256.,     (100-128)/256.,      (183-128)/256. },
191     {    1,            68/256.,     (117-128)/256.,      (203-128)/256. }},
192     [NEBULAE] = {
193     {    0,            10/256.,     (134-128)/256.,      (132-128)/256. },
194     { 0.23,            21/256.,     (137-128)/256.,      (130-128)/256. },
195     { 0.45,            35/256.,     (134-128)/256.,      (134-128)/256. },
196     { 0.57,            51/256.,     (130-128)/256.,      (139-128)/256. },
197     { 0.67,           104/256.,     (116-128)/256.,      (162-128)/256. },
198     { 0.77,           120/256.,     (105-128)/256.,      (188-128)/256. },
199     { 0.87,           140/256.,     (105-128)/256.,      (188-128)/256. },
200     {    1,                  1,                  0,                   0 }},
201     [FIRE] = {
202     {    0,                  0,                  0,                   0 },
203     { 0.23,            44/256.,     (132-128)/256.,      (127-128)/256. },
204     { 0.45,            62/256.,     (116-128)/256.,      (140-128)/256. },
205     { 0.57,            75/256.,     (105-128)/256.,      (152-128)/256. },
206     { 0.67,            95/256.,      (91-128)/256.,      (166-128)/256. },
207     { 0.77,           126/256.,      (74-128)/256.,      (172-128)/256. },
208     { 0.87,           164/256.,      (73-128)/256.,      (162-128)/256. },
209     {    1,                  1,                  0,                   0 }},
210     [FIERY] = {
211     {    0,                  0,                  0,                   0 },
212     { 0.23,            36/256.,     (116-128)/256.,      (163-128)/256. },
213     { 0.45,            52/256.,     (102-128)/256.,      (200-128)/256. },
214     { 0.57,           116/256.,      (84-128)/256.,      (196-128)/256. },
215     { 0.67,           157/256.,      (67-128)/256.,      (181-128)/256. },
216     { 0.77,           193/256.,      (40-128)/256.,      (155-128)/256. },
217     { 0.87,           221/256.,     (101-128)/256.,      (134-128)/256. },
218     {    1,                  1,                  0,                   0 }},
219     [FRUIT] = {
220     {    0,                  0,                  0,                   0 },
221     { 0.20,            29/256.,     (136-128)/256.,      (119-128)/256. },
222     { 0.30,            60/256.,     (119-128)/256.,       (90-128)/256. },
223     { 0.40,            85/256.,      (91-128)/256.,       (85-128)/256. },
224     { 0.50,           116/256.,      (70-128)/256.,      (105-128)/256. },
225     { 0.60,           151/256.,      (50-128)/256.,      (146-128)/256. },
226     { 0.70,           191/256.,      (63-128)/256.,      (178-128)/256. },
227     {    1,            98/256.,      (80-128)/256.,      (221-128)/256. }},
228     [COOL] = {
229     {    0,                  0,                  0,                   0 },
230     {  .15,                  0,                 .5,                 -.5 },
231     {    1,                  1,                -.5,                  .5 }},
232     [MAGMA] = {
233     {    0,                  0,                  0,                   0 },
234     { 0.10,            23/256.,     (175-128)/256.,      (120-128)/256. },
235     { 0.23,            43/256.,     (158-128)/256.,      (144-128)/256. },
236     { 0.35,            85/256.,     (138-128)/256.,      (179-128)/256. },
237     { 0.48,            96/256.,     (128-128)/256.,      (189-128)/256. },
238     { 0.64,           128/256.,     (103-128)/256.,      (214-128)/256. },
239     { 0.78,           167/256.,      (85-128)/256.,      (174-128)/256. },
240     {    1,           205/256.,      (80-128)/256.,      (152-128)/256. }},
241 };
242
243 static av_cold void uninit(AVFilterContext *ctx)
244 {
245     ShowSpectrumContext *s = ctx->priv;
246     int i;
247
248     av_freep(&s->combine_buffer);
249     if (s->fft) {
250         for (i = 0; i < s->nb_display_channels; i++)
251             av_fft_end(s->fft[i]);
252     }
253     av_freep(&s->fft);
254     if (s->ifft) {
255         for (i = 0; i < s->nb_display_channels; i++)
256             av_fft_end(s->ifft[i]);
257     }
258     av_freep(&s->ifft);
259     if (s->fft_data) {
260         for (i = 0; i < s->nb_display_channels; i++)
261             av_freep(&s->fft_data[i]);
262     }
263     av_freep(&s->fft_data);
264     if (s->fft_scratch) {
265         for (i = 0; i < s->nb_display_channels; i++)
266             av_freep(&s->fft_scratch[i]);
267     }
268     av_freep(&s->fft_scratch);
269     if (s->color_buffer) {
270         for (i = 0; i < s->nb_display_channels; i++)
271             av_freep(&s->color_buffer[i]);
272     }
273     av_freep(&s->color_buffer);
274     av_freep(&s->window_func_lut);
275     if (s->magnitudes) {
276         for (i = 0; i < s->nb_display_channels; i++)
277             av_freep(&s->magnitudes[i]);
278     }
279     av_freep(&s->magnitudes);
280     av_frame_free(&s->outpicref);
281     av_audio_fifo_free(s->fifo);
282     if (s->phases) {
283         for (i = 0; i < s->nb_display_channels; i++)
284             av_freep(&s->phases[i]);
285     }
286     av_freep(&s->phases);
287 }
288
289 static int query_formats(AVFilterContext *ctx)
290 {
291     AVFilterFormats *formats = NULL;
292     AVFilterChannelLayouts *layouts = NULL;
293     AVFilterLink *inlink = ctx->inputs[0];
294     AVFilterLink *outlink = ctx->outputs[0];
295     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
296     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE };
297     int ret;
298
299     /* set input audio formats */
300     formats = ff_make_format_list(sample_fmts);
301     if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
302         return ret;
303
304     layouts = ff_all_channel_layouts();
305     if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
306         return ret;
307
308     formats = ff_all_samplerates();
309     if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
310         return ret;
311
312     /* set output video format */
313     formats = ff_make_format_list(pix_fmts);
314     if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
315         return ret;
316
317     return 0;
318 }
319
320 static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
321 {
322     ShowSpectrumContext *s = ctx->priv;
323     AVFilterLink *inlink = ctx->inputs[0];
324     const float *window_func_lut = s->window_func_lut;
325     AVFrame *fin = arg;
326     const int ch = jobnr;
327     int n;
328
329     /* fill FFT input with the number of samples available */
330     const float *p = (float *)fin->extended_data[ch];
331
332     for (n = 0; n < s->win_size; n++) {
333         s->fft_data[ch][n].re = p[n] * window_func_lut[n];
334         s->fft_data[ch][n].im = 0;
335     }
336
337     if (s->stop) {
338         double theta, phi, psi, a, b, S, c;
339         FFTComplex *g = s->fft_data[ch];
340         FFTComplex *h = s->fft_scratch[ch];
341         int L = s->buf_size;
342         int N = s->win_size;
343         int M = s->win_size / 2;
344
345         phi = 2.0 * M_PI * (s->stop - s->start) / (double)inlink->sample_rate / (M - 1);
346         theta = 2.0 * M_PI * s->start / (double)inlink->sample_rate;
347
348         for (int n = 0; n < M; n++) {
349             h[n].re = cos(n * n / 2.0 * phi);
350             h[n].im = sin(n * n / 2.0 * phi);
351         }
352
353         for (int n = M; n < L; n++) {
354             h[n].re = 0.0;
355             h[n].im = 0.0;
356         }
357
358         for (int n = L - N; n < L; n++) {
359             h[n].re = cos((L - n) * (L - n) / 2.0 * phi);
360             h[n].im = sin((L - n) * (L - n) / 2.0 * phi);
361         }
362
363         for (int n = 0; n < N; n++) {
364             g[n].re = s->fft_data[ch][n].re;
365             g[n].im = s->fft_data[ch][n].im;
366         }
367
368         for (int n = N; n < L; n++) {
369             g[n].re = 0.;
370             g[n].im = 0.;
371         }
372
373         for (int n = 0; n < N; n++) {
374             psi = n * theta + n * n / 2.0 * phi;
375             c =  cos(psi);
376             S = -sin(psi);
377             a = c * g[n].re - S * g[n].im;
378             b = S * g[n].re + c * g[n].im;
379             g[n].re = a;
380             g[n].im = b;
381         }
382
383         av_fft_permute(s->fft[ch], h);
384         av_fft_calc(s->fft[ch], h);
385
386         av_fft_permute(s->fft[ch], g);
387         av_fft_calc(s->fft[ch], g);
388
389         for (int n = 0; n < L; n++) {
390             c = g[n].re;
391             S = g[n].im;
392             a = c * h[n].re - S * h[n].im;
393             b = S * h[n].re + c * h[n].im;
394
395             g[n].re = a / L;
396             g[n].im = b / L;
397         }
398
399         av_fft_permute(s->ifft[ch], g);
400         av_fft_calc(s->ifft[ch], g);
401
402         for (int k = 0; k < M; k++) {
403             psi = k * k / 2.0 * phi;
404             c =  cos(psi);
405             S = -sin(psi);
406             a = c * g[k].re - S * g[k].im;
407             b = S * g[k].re + c * g[k].im;
408             s->fft_data[ch][k].re = a;
409             s->fft_data[ch][k].im = b;
410         }
411     } else {
412         /* run FFT on each samples set */
413         av_fft_permute(s->fft[ch], s->fft_data[ch]);
414         av_fft_calc(s->fft[ch], s->fft_data[ch]);
415     }
416
417     return 0;
418 }
419
420 static int config_output(AVFilterLink *outlink)
421 {
422     AVFilterContext *ctx = outlink->src;
423     AVFilterLink *inlink = ctx->inputs[0];
424     ShowSpectrumContext *s = ctx->priv;
425     int i, fft_bits, h, w;
426     float overlap;
427
428     s->stop = FFMIN(s->stop, inlink->sample_rate / 2);
429     if (s->stop && s->stop <= s->start) {
430         av_log(ctx, AV_LOG_ERROR, "Stop frequency should be greater than start.\n");
431         return AVERROR(EINVAL);
432     }
433
434     s->pts = AV_NOPTS_VALUE;
435
436     if (!strcmp(ctx->filter->name, "showspectrumpic"))
437         s->single_pic = 1;
438
439     outlink->w = s->w;
440     outlink->h = s->h;
441     outlink->sample_aspect_ratio = (AVRational){1,1};
442
443     if (s->legend) {
444         s->start_x = log10(inlink->sample_rate) * 25;
445         s->start_y = 64;
446         outlink->w += s->start_x * 2;
447         outlink->h += s->start_y * 2;
448     }
449
450     h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? s->h : s->h / inlink->channels;
451     w = (s->mode == COMBINED || s->orientation == VERTICAL)   ? s->w : s->w / inlink->channels;
452     s->channel_height = h;
453     s->channel_width  = w;
454
455     if (s->orientation == VERTICAL) {
456         /* FFT window size (precision) according to the requested output frame height */
457         for (fft_bits = 1; 1 << fft_bits < 2 * h; fft_bits++);
458     } else {
459         /* FFT window size (precision) according to the requested output frame width */
460         for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++);
461     }
462
463     s->win_size = 1 << fft_bits;
464     s->buf_size = s->win_size << !!s->stop;
465
466     if (!s->fft) {
467         s->fft = av_calloc(inlink->channels, sizeof(*s->fft));
468         if (!s->fft)
469             return AVERROR(ENOMEM);
470     }
471
472     if (s->stop) {
473         if (!s->ifft) {
474             s->ifft = av_calloc(inlink->channels, sizeof(*s->ifft));
475             if (!s->ifft)
476                 return AVERROR(ENOMEM);
477         }
478     }
479
480     /* (re-)configuration if the video output changed (or first init) */
481     if (fft_bits != s->fft_bits) {
482         AVFrame *outpicref;
483
484         s->fft_bits = fft_bits;
485
486         /* FFT buffers: x2 for each (display) channel buffer.
487          * Note: we use free and malloc instead of a realloc-like function to
488          * make sure the buffer is aligned in memory for the FFT functions. */
489         for (i = 0; i < s->nb_display_channels; i++) {
490             if (s->stop) {
491                 av_fft_end(s->ifft[i]);
492                 av_freep(&s->fft_scratch[i]);
493             }
494             av_fft_end(s->fft[i]);
495             av_freep(&s->fft_data[i]);
496         }
497         av_freep(&s->fft_data);
498
499         s->nb_display_channels = inlink->channels;
500         for (i = 0; i < s->nb_display_channels; i++) {
501             s->fft[i] = av_fft_init(fft_bits + !!s->stop, 0);
502             if (s->stop) {
503                 s->ifft[i] = av_fft_init(fft_bits + !!s->stop, 1);
504                 if (!s->ifft[i]) {
505                     av_log(ctx, AV_LOG_ERROR, "Unable to create Inverse FFT context. "
506                            "The window size might be too high.\n");
507                     return AVERROR(EINVAL);
508                 }
509             }
510             if (!s->fft[i]) {
511                 av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
512                        "The window size might be too high.\n");
513                 return AVERROR(EINVAL);
514             }
515         }
516
517         s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
518         if (!s->magnitudes)
519             return AVERROR(ENOMEM);
520         for (i = 0; i < s->nb_display_channels; i++) {
521             s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
522             if (!s->magnitudes[i])
523                 return AVERROR(ENOMEM);
524         }
525
526         s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases));
527         if (!s->phases)
528             return AVERROR(ENOMEM);
529         for (i = 0; i < s->nb_display_channels; i++) {
530             s->phases[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->phases));
531             if (!s->phases[i])
532                 return AVERROR(ENOMEM);
533         }
534
535         av_freep(&s->color_buffer);
536         s->color_buffer = av_calloc(s->nb_display_channels, sizeof(*s->color_buffer));
537         if (!s->color_buffer)
538             return AVERROR(ENOMEM);
539         for (i = 0; i < s->nb_display_channels; i++) {
540             s->color_buffer[i] = av_calloc(s->orientation == VERTICAL ? s->h * 3 : s->w * 3, sizeof(**s->color_buffer));
541             if (!s->color_buffer[i])
542                 return AVERROR(ENOMEM);
543         }
544
545         s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
546         if (!s->fft_data)
547             return AVERROR(ENOMEM);
548         s->fft_scratch = av_calloc(s->nb_display_channels, sizeof(*s->fft_scratch));
549         if (!s->fft_scratch)
550             return AVERROR(ENOMEM);
551         for (i = 0; i < s->nb_display_channels; i++) {
552             s->fft_data[i] = av_calloc(s->buf_size, sizeof(**s->fft_data));
553             if (!s->fft_data[i])
554                 return AVERROR(ENOMEM);
555
556             s->fft_scratch[i] = av_calloc(s->buf_size, sizeof(**s->fft_scratch));
557             if (!s->fft_scratch[i])
558                 return AVERROR(ENOMEM);
559         }
560
561         /* pre-calc windowing function */
562         s->window_func_lut =
563             av_realloc_f(s->window_func_lut, s->win_size,
564                          sizeof(*s->window_func_lut));
565         if (!s->window_func_lut)
566             return AVERROR(ENOMEM);
567         generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
568         if (s->overlap == 1)
569             s->overlap = overlap;
570         s->hop_size = (1. - s->overlap) * s->win_size;
571         if (s->hop_size < 1) {
572             av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
573             return AVERROR(EINVAL);
574         }
575
576         for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
577             s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
578         }
579         s->win_scale = 1. / sqrt(s->win_scale);
580
581         /* prepare the initial picref buffer (black frame) */
582         av_frame_free(&s->outpicref);
583         s->outpicref = outpicref =
584             ff_get_video_buffer(outlink, outlink->w, outlink->h);
585         if (!outpicref)
586             return AVERROR(ENOMEM);
587         outpicref->sample_aspect_ratio = (AVRational){1,1};
588         for (i = 0; i < outlink->h; i++) {
589             memset(outpicref->data[0] + i * outpicref->linesize[0],   0, outlink->w);
590             memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
591             memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
592         }
593         outpicref->color_range = AVCOL_RANGE_JPEG;
594     }
595
596     if ((s->orientation == VERTICAL   && s->xpos >= s->w) ||
597         (s->orientation == HORIZONTAL && s->xpos >= s->h))
598         s->xpos = 0;
599
600     outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
601     if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
602         outlink->frame_rate.den *= s->w;
603     if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
604         outlink->frame_rate.den *= s->h;
605
606     if (s->orientation == VERTICAL) {
607         s->combine_buffer =
608             av_realloc_f(s->combine_buffer, s->h * 3,
609                          sizeof(*s->combine_buffer));
610     } else {
611         s->combine_buffer =
612             av_realloc_f(s->combine_buffer, s->w * 3,
613                          sizeof(*s->combine_buffer));
614     }
615
616     av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n",
617            s->w, s->h, s->win_size);
618
619     av_audio_fifo_free(s->fifo);
620     s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
621     if (!s->fifo)
622         return AVERROR(ENOMEM);
623     return 0;
624 }
625
626 #define RE(y, ch) s->fft_data[ch][y].re
627 #define IM(y, ch) s->fft_data[ch][y].im
628 #define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch))
629 #define PHASE(y, ch) atan2(IM(y, ch), RE(y, ch))
630
631 static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
632 {
633     ShowSpectrumContext *s = ctx->priv;
634     const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
635     int y, h = s->orientation == VERTICAL ? s->h : s->w;
636     const float f = s->gain * w;
637     const int ch = jobnr;
638     float *magnitudes = s->magnitudes[ch];
639
640     for (y = 0; y < h; y++)
641         magnitudes[y] = MAGNITUDE(y, ch) * f;
642
643     return 0;
644 }
645
646 static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
647 {
648     ShowSpectrumContext *s = ctx->priv;
649     const int h = s->orientation == VERTICAL ? s->h : s->w;
650     const int ch = jobnr;
651     float *phases = s->phases[ch];
652     int y;
653
654     for (y = 0; y < h; y++)
655         phases[y] = (PHASE(y, ch) / M_PI + 1) / 2;
656
657     return 0;
658 }
659
660 static void acalc_magnitudes(ShowSpectrumContext *s)
661 {
662     const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
663     int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
664     const float f = s->gain * w;
665
666     for (ch = 0; ch < s->nb_display_channels; ch++) {
667         float *magnitudes = s->magnitudes[ch];
668
669         for (y = 0; y < h; y++)
670             magnitudes[y] += MAGNITUDE(y, ch) * f;
671     }
672 }
673
674 static void scale_magnitudes(ShowSpectrumContext *s, float scale)
675 {
676     int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
677
678     for (ch = 0; ch < s->nb_display_channels; ch++) {
679         float *magnitudes = s->magnitudes[ch];
680
681         for (y = 0; y < h; y++)
682             magnitudes[y] *= scale;
683     }
684 }
685
686 static void color_range(ShowSpectrumContext *s, int ch,
687                         float *yf, float *uf, float *vf)
688 {
689     switch (s->mode) {
690     case COMBINED:
691         // reduce range by channel count
692         *yf = 256.0f / s->nb_display_channels;
693         switch (s->color_mode) {
694         case RAINBOW:
695         case MORELAND:
696         case NEBULAE:
697         case FIRE:
698         case FIERY:
699         case FRUIT:
700         case COOL:
701         case MAGMA:
702         case INTENSITY:
703             *uf = *yf;
704             *vf = *yf;
705             break;
706         case CHANNEL:
707             /* adjust saturation for mixed UV coloring */
708             /* this factor is correct for infinite channels, an approximation otherwise */
709             *uf = *yf * M_PI;
710             *vf = *yf * M_PI;
711             break;
712         default:
713             av_assert0(0);
714         }
715         break;
716     case SEPARATE:
717         // full range
718         *yf = 256.0f;
719         *uf = 256.0f;
720         *vf = 256.0f;
721         break;
722     default:
723         av_assert0(0);
724     }
725
726     if (s->color_mode == CHANNEL) {
727         if (s->nb_display_channels > 1) {
728             *uf *= 0.5 * sin((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
729             *vf *= 0.5 * cos((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
730         } else {
731             *uf *= 0.5 * sin(M_PI * s->rotation);
732             *vf *= 0.5 * cos(M_PI * s->rotation + M_PI_2);
733         }
734     } else {
735         *uf += *uf * sin(M_PI * s->rotation);
736         *vf += *vf * cos(M_PI * s->rotation + M_PI_2);
737     }
738
739     *uf *= s->saturation;
740     *vf *= s->saturation;
741 }
742
743 static void pick_color(ShowSpectrumContext *s,
744                        float yf, float uf, float vf,
745                        float a, float *out)
746 {
747     if (s->color_mode > CHANNEL) {
748         const int cm = s->color_mode;
749         float y, u, v;
750         int i;
751
752         for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
753             if (color_table[cm][i].a >= a)
754                 break;
755         // i now is the first item >= the color
756         // now we know to interpolate between item i - 1 and i
757         if (a <= color_table[cm][i - 1].a) {
758             y = color_table[cm][i - 1].y;
759             u = color_table[cm][i - 1].u;
760             v = color_table[cm][i - 1].v;
761         } else if (a >= color_table[cm][i].a) {
762             y = color_table[cm][i].y;
763             u = color_table[cm][i].u;
764             v = color_table[cm][i].v;
765         } else {
766             float start = color_table[cm][i - 1].a;
767             float end = color_table[cm][i].a;
768             float lerpfrac = (a - start) / (end - start);
769             y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
770               + color_table[cm][i].y * lerpfrac;
771             u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
772               + color_table[cm][i].u * lerpfrac;
773             v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
774               + color_table[cm][i].v * lerpfrac;
775         }
776
777         out[0] = y * yf;
778         out[1] = u * uf;
779         out[2] = v * vf;
780     } else {
781         out[0] = a * yf;
782         out[1] = a * uf;
783         out[2] = a * vf;
784     }
785 }
786
787 static void clear_combine_buffer(ShowSpectrumContext *s, int size)
788 {
789     int y;
790
791     for (y = 0; y < size; y++) {
792         s->combine_buffer[3 * y    ] = 0;
793         s->combine_buffer[3 * y + 1] = 127.5;
794         s->combine_buffer[3 * y + 2] = 127.5;
795     }
796 }
797
798 static int plot_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
799 {
800     ShowSpectrumContext *s = ctx->priv;
801     const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
802     const int ch = jobnr;
803     float *magnitudes = s->magnitudes[ch];
804     float *phases = s->phases[ch];
805     float yf, uf, vf;
806     int y;
807
808     /* decide color range */
809     color_range(s, ch, &yf, &uf, &vf);
810
811     /* draw the channel */
812     for (y = 0; y < h; y++) {
813         int row = (s->mode == COMBINED) ? y : ch * h + y;
814         float *out = &s->color_buffer[ch][3 * row];
815         float a;
816
817         switch (s->data) {
818         case D_MAGNITUDE:
819             /* get magnitude */
820             a = magnitudes[y];
821             break;
822         case D_PHASE:
823             /* get phase */
824             a = phases[y];
825             break;
826         default:
827             av_assert0(0);
828         }
829
830         /* apply scale */
831         switch (s->scale) {
832         case LINEAR:
833             a = av_clipf(a, 0, 1);
834             break;
835         case SQRT:
836             a = av_clipf(sqrt(a), 0, 1);
837             break;
838         case CBRT:
839             a = av_clipf(cbrt(a), 0, 1);
840             break;
841         case FOURTHRT:
842             a = av_clipf(sqrt(sqrt(a)), 0, 1);
843             break;
844         case FIFTHRT:
845             a = av_clipf(pow(a, 0.20), 0, 1);
846             break;
847         case LOG:
848             a = 1 + log10(av_clipd(a, 1e-6, 1)) / 6; // zero = -120dBFS
849             break;
850         default:
851             av_assert0(0);
852         }
853
854         pick_color(s, yf, uf, vf, a, out);
855     }
856
857     return 0;
858 }
859
860 static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
861 {
862     AVFilterContext *ctx = inlink->dst;
863     AVFilterLink *outlink = ctx->outputs[0];
864     ShowSpectrumContext *s = ctx->priv;
865     AVFrame *outpicref = s->outpicref;
866     int ret, plane, x, y, z = s->orientation == VERTICAL ? s->h : s->w;
867
868     /* fill a new spectrum column */
869     /* initialize buffer for combining to black */
870     clear_combine_buffer(s, z);
871
872     ctx->internal->execute(ctx, plot_channel, NULL, NULL, s->nb_display_channels);
873
874     for (y = 0; y < z * 3; y++) {
875         for (x = 0; x < s->nb_display_channels; x++) {
876             s->combine_buffer[y] += s->color_buffer[x][y];
877         }
878     }
879
880     av_frame_make_writable(s->outpicref);
881     /* copy to output */
882     if (s->orientation == VERTICAL) {
883         if (s->sliding == SCROLL) {
884             for (plane = 0; plane < 3; plane++) {
885                 for (y = 0; y < s->h; y++) {
886                     uint8_t *p = outpicref->data[plane] +
887                                  y * outpicref->linesize[plane];
888                     memmove(p, p + 1, s->w - 1);
889                 }
890             }
891             s->xpos = s->w - 1;
892         } else if (s->sliding == RSCROLL) {
893             for (plane = 0; plane < 3; plane++) {
894                 for (y = 0; y < s->h; y++) {
895                     uint8_t *p = outpicref->data[plane] +
896                                  y * outpicref->linesize[plane];
897                     memmove(p + 1, p, s->w - 1);
898                 }
899             }
900             s->xpos = 0;
901         }
902         for (plane = 0; plane < 3; plane++) {
903             uint8_t *p = outpicref->data[plane] + s->start_x +
904                          (outlink->h - 1 - s->start_y) * outpicref->linesize[plane] +
905                          s->xpos;
906             for (y = 0; y < s->h; y++) {
907                 *p = lrintf(av_clipf(s->combine_buffer[3 * y + plane], 0, 255));
908                 p -= outpicref->linesize[plane];
909             }
910         }
911     } else {
912         if (s->sliding == SCROLL) {
913             for (plane = 0; plane < 3; plane++) {
914                 for (y = 1; y < s->h; y++) {
915                     memmove(outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
916                             outpicref->data[plane] + (y  ) * outpicref->linesize[plane],
917                             s->w);
918                 }
919             }
920             s->xpos = s->h - 1;
921         } else if (s->sliding == RSCROLL) {
922             for (plane = 0; plane < 3; plane++) {
923                 for (y = s->h - 1; y >= 1; y--) {
924                     memmove(outpicref->data[plane] + (y  ) * outpicref->linesize[plane],
925                             outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
926                             s->w);
927                 }
928             }
929             s->xpos = 0;
930         }
931         for (plane = 0; plane < 3; plane++) {
932             uint8_t *p = outpicref->data[plane] + s->start_x +
933                          (s->xpos + s->start_y) * outpicref->linesize[plane];
934             for (x = 0; x < s->w; x++) {
935                 *p = lrintf(av_clipf(s->combine_buffer[3 * x + plane], 0, 255));
936                 p++;
937             }
938         }
939     }
940
941     if (s->sliding != FULLFRAME || s->xpos == 0)
942         outpicref->pts = insamples->pts;
943
944     s->xpos++;
945     if (s->orientation == VERTICAL && s->xpos >= s->w)
946         s->xpos = 0;
947     if (s->orientation == HORIZONTAL && s->xpos >= s->h)
948         s->xpos = 0;
949     if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
950         ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
951         if (ret < 0)
952             return ret;
953     }
954
955     return s->win_size;
956 }
957
958 #if CONFIG_SHOWSPECTRUM_FILTER
959
960 static int request_frame(AVFilterLink *outlink)
961 {
962     ShowSpectrumContext *s = outlink->src->priv;
963     AVFilterLink *inlink = outlink->src->inputs[0];
964     unsigned i;
965     int ret;
966
967     ret = ff_request_frame(inlink);
968     if (ret == AVERROR_EOF && s->sliding == FULLFRAME && s->xpos > 0 &&
969         s->outpicref) {
970         if (s->orientation == VERTICAL) {
971             for (i = 0; i < outlink->h; i++) {
972                 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos,   0, outlink->w - s->xpos);
973                 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
974                 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
975             }
976         } else {
977             for (i = s->xpos; i < outlink->h; i++) {
978                 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0],   0, outlink->w);
979                 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
980                 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
981             }
982         }
983         ret = ff_filter_frame(outlink, s->outpicref);
984         s->outpicref = NULL;
985     }
986
987     return ret;
988 }
989
990 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
991 {
992     AVFilterContext *ctx = inlink->dst;
993     ShowSpectrumContext *s = ctx->priv;
994     AVFrame *fin = NULL;
995     int ret = 0, consumed = 0;
996
997     if (s->pts == AV_NOPTS_VALUE)
998         s->pts = insamples->pts - av_audio_fifo_size(s->fifo);
999
1000     av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
1001     av_frame_free(&insamples);
1002     while (av_audio_fifo_size(s->fifo) >= s->win_size) {
1003         fin = ff_get_audio_buffer(inlink, s->win_size);
1004         if (!fin) {
1005             ret = AVERROR(ENOMEM);
1006             goto fail;
1007         }
1008
1009         fin->pts = s->pts + consumed;
1010         consumed += s->hop_size;
1011         ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
1012         if (ret < 0)
1013             goto fail;
1014
1015         av_assert0(fin->nb_samples == s->win_size);
1016
1017         ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
1018
1019         if (s->data == D_MAGNITUDE)
1020             ctx->internal->execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels);
1021
1022         if (s->data == D_PHASE)
1023             ctx->internal->execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels);
1024
1025         ret = plot_spectrum_column(inlink, fin);
1026         av_frame_free(&fin);
1027         av_audio_fifo_drain(s->fifo, s->hop_size);
1028         if (ret < 0)
1029             goto fail;
1030     }
1031
1032 fail:
1033     s->pts = AV_NOPTS_VALUE;
1034     av_frame_free(&fin);
1035     return ret;
1036 }
1037
1038 static const AVFilterPad showspectrum_inputs[] = {
1039     {
1040         .name         = "default",
1041         .type         = AVMEDIA_TYPE_AUDIO,
1042         .filter_frame = filter_frame,
1043     },
1044     { NULL }
1045 };
1046
1047 static const AVFilterPad showspectrum_outputs[] = {
1048     {
1049         .name          = "default",
1050         .type          = AVMEDIA_TYPE_VIDEO,
1051         .config_props  = config_output,
1052         .request_frame = request_frame,
1053     },
1054     { NULL }
1055 };
1056
1057 AVFilter ff_avf_showspectrum = {
1058     .name          = "showspectrum",
1059     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
1060     .uninit        = uninit,
1061     .query_formats = query_formats,
1062     .priv_size     = sizeof(ShowSpectrumContext),
1063     .inputs        = showspectrum_inputs,
1064     .outputs       = showspectrum_outputs,
1065     .priv_class    = &showspectrum_class,
1066     .flags         = AVFILTER_FLAG_SLICE_THREADS,
1067 };
1068 #endif // CONFIG_SHOWSPECTRUM_FILTER
1069
1070 #if CONFIG_SHOWSPECTRUMPIC_FILTER
1071
1072 static const AVOption showspectrumpic_options[] = {
1073     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
1074     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
1075     { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" },
1076         { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
1077         { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
1078     { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" },
1079         { "channel",   "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL},   0, 0, FLAGS, "color" },
1080         { "intensity", "intensity based coloring",        0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
1081         { "rainbow",   "rainbow based coloring",          0, AV_OPT_TYPE_CONST, {.i64=RAINBOW},   0, 0, FLAGS, "color" },
1082         { "moreland",  "moreland based coloring",         0, AV_OPT_TYPE_CONST, {.i64=MORELAND},  0, 0, FLAGS, "color" },
1083         { "nebulae",   "nebulae based coloring",          0, AV_OPT_TYPE_CONST, {.i64=NEBULAE},   0, 0, FLAGS, "color" },
1084         { "fire",      "fire based coloring",             0, AV_OPT_TYPE_CONST, {.i64=FIRE},      0, 0, FLAGS, "color" },
1085         { "fiery",     "fiery based coloring",            0, AV_OPT_TYPE_CONST, {.i64=FIERY},     0, 0, FLAGS, "color" },
1086         { "fruit",     "fruit based coloring",            0, AV_OPT_TYPE_CONST, {.i64=FRUIT},     0, 0, FLAGS, "color" },
1087         { "cool",      "cool based coloring",             0, AV_OPT_TYPE_CONST, {.i64=COOL},      0, 0, FLAGS, "color" },
1088         { "magma",     "magma based coloring",            0, AV_OPT_TYPE_CONST, {.i64=MAGMA},     0, 0, FLAGS, "color" },
1089     { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" },
1090         { "lin",  "linear",      0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
1091         { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT},   0, 0, FLAGS, "scale" },
1092         { "cbrt", "cubic root",  0, AV_OPT_TYPE_CONST, {.i64=CBRT},   0, 0, FLAGS, "scale" },
1093         { "log",  "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG},    0, 0, FLAGS, "scale" },
1094         { "4thrt","4th root",    0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
1095         { "5thrt","5th root",    0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT},  0, 0, FLAGS, "scale" },
1096     { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
1097     { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
1098         { "rect",     "Rectangular",      0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT},     0, 0, FLAGS, "win_func" },
1099         { "bartlett", "Bartlett",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
1100         { "hann",     "Hann",             0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING},  0, 0, FLAGS, "win_func" },
1101         { "hanning",  "Hanning",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING},  0, 0, FLAGS, "win_func" },
1102         { "hamming",  "Hamming",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING},  0, 0, FLAGS, "win_func" },
1103         { "blackman", "Blackman",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
1104         { "welch",    "Welch",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH},    0, 0, FLAGS, "win_func" },
1105         { "flattop",  "Flat-top",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP},  0, 0, FLAGS, "win_func" },
1106         { "bharris",  "Blackman-Harris",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS},  0, 0, FLAGS, "win_func" },
1107         { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
1108         { "bhann",    "Bartlett-Hann",    0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN},    0, 0, FLAGS, "win_func" },
1109         { "sine",     "Sine",             0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE},     0, 0, FLAGS, "win_func" },
1110         { "nuttall",  "Nuttall",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL},  0, 0, FLAGS, "win_func" },
1111         { "lanczos",  "Lanczos",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS},  0, 0, FLAGS, "win_func" },
1112         { "gauss",    "Gauss",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS},    0, 0, FLAGS, "win_func" },
1113         { "tukey",    "Tukey",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY},    0, 0, FLAGS, "win_func" },
1114         { "dolph",    "Dolph-Chebyshev",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH},    0, 0, FLAGS, "win_func" },
1115         { "cauchy",   "Cauchy",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY},   0, 0, FLAGS, "win_func" },
1116         { "parzen",   "Parzen",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN},   0, 0, FLAGS, "win_func" },
1117         { "poisson",  "Poisson",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON},  0, 0, FLAGS, "win_func" },
1118     { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
1119         { "vertical",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL},   0, 0, FLAGS, "orientation" },
1120         { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
1121     { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
1122     { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
1123     { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
1124     { NULL }
1125 };
1126
1127 AVFILTER_DEFINE_CLASS(showspectrumpic);
1128
1129 static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
1130 {
1131     const uint8_t *font;
1132     int font_height;
1133     int i;
1134
1135     font = avpriv_cga_font,   font_height =  8;
1136
1137     for (i = 0; txt[i]; i++) {
1138         int char_y, mask;
1139
1140         if (o) {
1141             for (char_y = font_height - 1; char_y >= 0; char_y--) {
1142                 uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x;
1143                 for (mask = 0x80; mask; mask >>= 1) {
1144                     if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
1145                         p[char_y] = ~p[char_y];
1146                     p += pic->linesize[0];
1147                 }
1148             }
1149         } else {
1150             uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8);
1151             for (char_y = 0; char_y < font_height; char_y++) {
1152                 for (mask = 0x80; mask; mask >>= 1) {
1153                     if (font[txt[i] * font_height + char_y] & mask)
1154                         *p = ~(*p);
1155                     p++;
1156                 }
1157                 p += pic->linesize[0] - 8;
1158             }
1159         }
1160     }
1161 }
1162
1163 static int showspectrumpic_request_frame(AVFilterLink *outlink)
1164 {
1165     AVFilterContext *ctx = outlink->src;
1166     ShowSpectrumContext *s = ctx->priv;
1167     AVFilterLink *inlink = ctx->inputs[0];
1168     int ret, samples;
1169
1170     ret = ff_request_frame(inlink);
1171     samples = av_audio_fifo_size(s->fifo);
1172     if (ret == AVERROR_EOF && s->outpicref && samples > 0) {
1173         int consumed = 0;
1174         int y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
1175         int ch, spf, spb;
1176         AVFrame *fin;
1177
1178         spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz))));
1179         spf = FFMAX(1, spf);
1180
1181         spb = (samples / (spf * sz)) * spf;
1182
1183         fin = ff_get_audio_buffer(inlink, s->win_size);
1184         if (!fin)
1185             return AVERROR(ENOMEM);
1186
1187         while (x < sz) {
1188             ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
1189             if (ret < 0) {
1190                 av_frame_free(&fin);
1191                 return ret;
1192             }
1193
1194             av_audio_fifo_drain(s->fifo, spf);
1195
1196             if (ret < s->win_size) {
1197                 for (ch = 0; ch < s->nb_display_channels; ch++) {
1198                     memset(fin->extended_data[ch] + ret * sizeof(float), 0,
1199                            (s->win_size - ret) * sizeof(float));
1200                 }
1201             }
1202
1203             ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
1204             acalc_magnitudes(s);
1205
1206             consumed += spf;
1207             if (consumed >= spb) {
1208                 int h = s->orientation == VERTICAL ? s->h : s->w;
1209
1210                 scale_magnitudes(s, 1. / (consumed / spf));
1211                 plot_spectrum_column(inlink, fin);
1212                 consumed = 0;
1213                 x++;
1214                 for (ch = 0; ch < s->nb_display_channels; ch++)
1215                     memset(s->magnitudes[ch], 0, h * sizeof(float));
1216             }
1217         }
1218
1219         av_frame_free(&fin);
1220         s->outpicref->pts = 0;
1221
1222         if (s->legend) {
1223             int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL);
1224             float spp = samples / (float)sz;
1225             char *text;
1226             uint8_t *dst;
1227             char chlayout_str[128];
1228
1229             av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), inlink->channels,
1230                                          inlink->channel_layout);
1231
1232             text = av_asprintf("%d Hz | %s", inlink->sample_rate, chlayout_str);
1233
1234             drawtext(s->outpicref, 2, outlink->h - 10, "CREATED BY LIBAVFILTER", 0);
1235             drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, outlink->h - 10, text, 0);
1236
1237             av_freep(&text);
1238
1239             dst = s->outpicref->data[0] + (s->start_y - 1) * s->outpicref->linesize[0] + s->start_x - 1;
1240             for (x = 0; x < s->w + 1; x++)
1241                 dst[x] = 200;
1242             dst = s->outpicref->data[0] + (s->start_y + s->h) * s->outpicref->linesize[0] + s->start_x - 1;
1243             for (x = 0; x < s->w + 1; x++)
1244                 dst[x] = 200;
1245             for (y = 0; y < s->h + 2; y++) {
1246                 dst = s->outpicref->data[0] + (y + s->start_y - 1) * s->outpicref->linesize[0];
1247                 dst[s->start_x - 1] = 200;
1248                 dst[s->start_x + s->w] = 200;
1249             }
1250             if (s->orientation == VERTICAL) {
1251                 int h = s->mode == SEPARATE ? s->h / s->nb_display_channels : s->h;
1252                 int hh = s->mode == SEPARATE ? -(s->h % s->nb_display_channels) + 1 : 1;
1253                 for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
1254                     for (y = 0; y < h; y += 20) {
1255                         dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
1256                         dst[s->start_x - 2] = 200;
1257                         dst[s->start_x + s->w + 1] = 200;
1258                     }
1259                     for (y = 0; y < h; y += 40) {
1260                         dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
1261                         dst[s->start_x - 3] = 200;
1262                         dst[s->start_x + s->w + 2] = 200;
1263                     }
1264                     dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x;
1265                     for (x = 0; x < s->w; x+=40)
1266                         dst[x] = 200;
1267                     dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x;
1268                     for (x = 0; x < s->w; x+=80)
1269                         dst[x] = 200;
1270                     dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x;
1271                     for (x = 0; x < s->w; x+=40) {
1272                         dst[x] = 200;
1273                     }
1274                     dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x;
1275                     for (x = 0; x < s->w; x+=80) {
1276                         dst[x] = 200;
1277                     }
1278                     for (y = 0; y < h; y += 40) {
1279                         float hertz = y * (inlink->sample_rate / 2) / (float)(1 << (int)ceil(log2(h)));
1280                         char *units;
1281
1282                         if (hertz == 0)
1283                             units = av_asprintf("DC");
1284                         else
1285                             units = av_asprintf("%.2f", hertz);
1286                         if (!units)
1287                             return AVERROR(ENOMEM);
1288
1289                         drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4 - hh, units, 0);
1290                         av_free(units);
1291                     }
1292                 }
1293
1294                 for (x = 0; x < s->w; x+=80) {
1295                     float seconds = x * spp / inlink->sample_rate;
1296                     char *units;
1297
1298                     if (x == 0)
1299                         units = av_asprintf("0");
1300                     else if (log10(seconds) > 6)
1301                         units = av_asprintf("%.2fh", seconds / (60 * 60));
1302                     else if (log10(seconds) > 3)
1303                         units = av_asprintf("%.2fm", seconds / 60);
1304                     else
1305                         units = av_asprintf("%.2fs", seconds);
1306                     if (!units)
1307                         return AVERROR(ENOMEM);
1308
1309                     drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->h + s->start_y + 6, units, 0);
1310                     drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->start_y - 12, units, 0);
1311                     av_free(units);
1312                 }
1313
1314                 drawtext(s->outpicref, outlink->w / 2 - 4 * 4, outlink->h - s->start_y / 2, "TIME", 0);
1315                 drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 14 * 4, "FREQUENCY (Hz)", 1);
1316             } else {
1317                 int w = s->mode == SEPARATE ? s->w / s->nb_display_channels : s->w;
1318                 for (y = 0; y < s->h; y += 20) {
1319                     dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
1320                     dst[s->start_x - 2] = 200;
1321                     dst[s->start_x + s->w + 1] = 200;
1322                 }
1323                 for (y = 0; y < s->h; y += 40) {
1324                     dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
1325                     dst[s->start_x - 3] = 200;
1326                     dst[s->start_x + s->w + 2] = 200;
1327                 }
1328                 for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
1329                     dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
1330                     for (x = 0; x < w; x+=40)
1331                         dst[x] = 200;
1332                     dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x + w * ch;
1333                     for (x = 0; x < w; x+=80)
1334                         dst[x] = 200;
1335                     dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x + w * ch;
1336                     for (x = 0; x < w; x+=40) {
1337                         dst[x] = 200;
1338                     }
1339                     dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
1340                     for (x = 0; x < w; x+=80) {
1341                         dst[x] = 200;
1342                     }
1343                     for (x = 0; x < w - 79; x += 80) {
1344                         float hertz = x * (inlink->sample_rate / 2) / (float)(1 << (int)ceil(log2(w)));
1345                         char *units;
1346
1347                         if (hertz == 0)
1348                             units = av_asprintf("DC");
1349                         else
1350                             units = av_asprintf("%.2f", hertz);
1351                         if (!units)
1352                             return AVERROR(ENOMEM);
1353
1354                         drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->start_y - 12, units, 0);
1355                         drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->h + s->start_y + 6, units, 0);
1356                         av_free(units);
1357                     }
1358                 }
1359                 for (y = 0; y < s->h; y+=40) {
1360                     float seconds = y * spp / inlink->sample_rate;
1361                     char *units;
1362
1363                     if (x == 0)
1364                         units = av_asprintf("0");
1365                     else if (log10(seconds) > 6)
1366                         units = av_asprintf("%.2fh", seconds / (60 * 60));
1367                     else if (log10(seconds) > 3)
1368                         units = av_asprintf("%.2fm", seconds / 60);
1369                     else
1370                         units = av_asprintf("%.2fs", seconds);
1371                     if (!units)
1372                         return AVERROR(ENOMEM);
1373
1374                     drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0);
1375                     av_free(units);
1376                 }
1377                 drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 4 * 4, "TIME", 1);
1378                 drawtext(s->outpicref, outlink->w / 2 - 14 * 4, outlink->h - s->start_y / 2, "FREQUENCY (Hz)", 0);
1379             }
1380
1381             for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) {
1382                 int h = multi ? s->h / s->nb_display_channels : s->h;
1383
1384                 for (y = 0; y < h; y++) {
1385                     float out[3] = { 0., 127.5, 127.5};
1386                     int chn;
1387
1388                     for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) {
1389                         float yf, uf, vf;
1390                         int channel = (multi) ? s->nb_display_channels - ch - 1 : chn;
1391                         float lout[3];
1392
1393                         color_range(s, channel, &yf, &uf, &vf);
1394                         pick_color(s, yf, uf, vf, y / (float)h, lout);
1395                         out[0] += lout[0];
1396                         out[1] += lout[1];
1397                         out[2] += lout[2];
1398                     }
1399                     memset(s->outpicref->data[0]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[0] + s->w + s->start_x + 20, av_clip_uint8(out[0]), 10);
1400                     memset(s->outpicref->data[1]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[1] + s->w + s->start_x + 20, av_clip_uint8(out[1]), 10);
1401                     memset(s->outpicref->data[2]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[2] + s->w + s->start_x + 20, av_clip_uint8(out[2]), 10);
1402                 }
1403
1404                 for (y = 0; ch == 0 && y < h; y += h / 10) {
1405                     float value = 120.0 * log10(1. - y / (float)h);
1406                     char *text;
1407
1408                     if (value < -120)
1409                         break;
1410                     text = av_asprintf("%.0f dB", value);
1411                     if (!text)
1412                         continue;
1413                     drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 5, text, 0);
1414                     av_free(text);
1415                 }
1416             }
1417         }
1418
1419         ret = ff_filter_frame(outlink, s->outpicref);
1420         s->outpicref = NULL;
1421     }
1422
1423     return ret;
1424 }
1425
1426 static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1427 {
1428     AVFilterContext *ctx = inlink->dst;
1429     ShowSpectrumContext *s = ctx->priv;
1430     int ret;
1431
1432     ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
1433     av_frame_free(&insamples);
1434     return ret;
1435 }
1436
1437 static const AVFilterPad showspectrumpic_inputs[] = {
1438     {
1439         .name         = "default",
1440         .type         = AVMEDIA_TYPE_AUDIO,
1441         .filter_frame = showspectrumpic_filter_frame,
1442     },
1443     { NULL }
1444 };
1445
1446 static const AVFilterPad showspectrumpic_outputs[] = {
1447     {
1448         .name          = "default",
1449         .type          = AVMEDIA_TYPE_VIDEO,
1450         .config_props  = config_output,
1451         .request_frame = showspectrumpic_request_frame,
1452     },
1453     { NULL }
1454 };
1455
1456 AVFilter ff_avf_showspectrumpic = {
1457     .name          = "showspectrumpic",
1458     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
1459     .uninit        = uninit,
1460     .query_formats = query_formats,
1461     .priv_size     = sizeof(ShowSpectrumContext),
1462     .inputs        = showspectrumpic_inputs,
1463     .outputs       = showspectrumpic_outputs,
1464     .priv_class    = &showspectrumpic_class,
1465     .flags         = AVFILTER_FLAG_SLICE_THREADS,
1466 };
1467
1468 #endif // CONFIG_SHOWSPECTRUMPIC_FILTER