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
6 * This file is part of FFmpeg.
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.
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.
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
25 * audio to spectrum (video) transmedia filter, based on ffplay rdft showmode
26 * (by Michael Niedermayer) and lavfi/avf_showwaves (by Stefano Sabatini).
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/parseutils.h"
38 #include "libavutil/xga_font_data.h"
44 #include "window_func.h"
46 enum DisplayMode { COMBINED, SEPARATE, NB_MODES };
47 enum DataMode { D_MAGNITUDE, D_PHASE, NB_DMODES };
48 enum FrequencyScale { F_LINEAR, F_LOG, NB_FSCALES };
49 enum DisplayScale { LINEAR, SQRT, CBRT, LOG, FOURTHRT, FIFTHRT, NB_SCALES };
50 enum ColorMode { CHANNEL, INTENSITY, RAINBOW, MORELAND, NEBULAE, FIRE, FIERY, FRUIT, COOL, MAGMA, GREEN, VIRIDIS, PLASMA, CIVIDIS, TERRAIN, NB_CLMODES };
51 enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, NB_SLIDES };
52 enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS };
54 typedef struct ShowSpectrumContext {
58 AVRational auto_frame_rate;
59 AVRational frame_rate;
61 int nb_display_channels;
65 int sliding; ///< 1 if sliding mode, 0 otherwise
66 int mode; ///< channel display mode
67 int color_mode; ///< display color scheme
70 float saturation; ///< color saturation multiplier
71 float rotation; ///< color rotation
72 int start, stop; ///< zoom mode
74 int xpos; ///< x position (current column)
75 FFTContext **fft; ///< Fast Fourier Transform context
76 FFTContext **ifft; ///< Inverse Fast Fourier Transform context
77 int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits)
78 FFTComplex **fft_data; ///< bins holder for each (displayed) channels
79 FFTComplex **fft_scratch; ///< scratch buffers
80 float *window_func_lut; ///< Window function LUT
91 float *combine_buffer; ///< color combining buffer (3 * h items)
92 float **color_buffer; ///< color buffer (3 * h * ch items)
100 int (*plot_channel)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
101 } ShowSpectrumContext;
103 #define OFFSET(x) offsetof(ShowSpectrumContext, x)
104 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
106 static const AVOption showspectrum_options[] = {
107 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
108 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
109 { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
110 { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
111 { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
112 { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
113 { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
114 { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
115 { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
116 { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
117 { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
118 { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
119 { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
120 { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
121 { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
122 { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
123 { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
124 { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
125 { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
126 { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
127 { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
128 { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
129 { "viridis", "viridis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=VIRIDIS}, 0, 0, FLAGS, "color" },
130 { "plasma", "plasma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=PLASMA}, 0, 0, FLAGS, "color" },
131 { "cividis", "cividis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=CIVIDIS}, 0, 0, FLAGS, "color" },
132 { "terrain", "terrain based coloring", 0, AV_OPT_TYPE_CONST, {.i64=TERRAIN}, 0, 0, FLAGS, "color" },
133 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
134 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
135 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
136 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
137 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
138 { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
139 { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
140 { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=F_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
141 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=F_LINEAR}, 0, 0, FLAGS, "fscale" },
142 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=F_LOG}, 0, 0, FLAGS, "fscale" },
143 { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
144 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
145 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
146 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
147 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
148 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
149 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
150 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
151 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
152 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
153 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
154 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
155 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
156 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
157 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
158 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
159 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
160 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
161 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
162 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
163 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
164 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
165 { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
166 { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
167 { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
168 { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
169 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS },
170 { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
171 { "data", "set data mode", OFFSET(data), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_DMODES-1, FLAGS, "data" },
172 { "magnitude", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_MAGNITUDE}, 0, 0, FLAGS, "data" },
173 { "phase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_PHASE}, 0, 0, FLAGS, "data" },
174 { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
175 { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
176 { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
177 { "fps", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = "auto"}, 0, 0, FLAGS },
178 { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
182 AVFILTER_DEFINE_CLASS(showspectrum);
184 static const struct ColorTable {
186 } color_table[][8] = {
189 { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
190 { 0.30, .18572281794568020, .1772436246393981, .17475554840414750 },
191 { 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 },
192 { 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 },
193 { 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 },
194 { 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 },
198 { 0.13, 44/256., (189-128)/256., (138-128)/256. },
199 { 0.25, 29/256., (186-128)/256., (119-128)/256. },
200 { 0.38, 119/256., (194-128)/256., (53-128)/256. },
201 { 0.60, 111/256., (73-128)/256., (59-128)/256. },
202 { 0.73, 205/256., (19-128)/256., (149-128)/256. },
203 { 0.86, 135/256., (83-128)/256., (200-128)/256. },
204 { 1, 73/256., (95-128)/256., (225-128)/256. }},
206 { 0, 44/256., (181-128)/256., (112-128)/256. },
207 { 0.13, 126/256., (177-128)/256., (106-128)/256. },
208 { 0.25, 164/256., (163-128)/256., (109-128)/256. },
209 { 0.38, 200/256., (140-128)/256., (120-128)/256. },
210 { 0.60, 201/256., (117-128)/256., (141-128)/256. },
211 { 0.73, 177/256., (103-128)/256., (165-128)/256. },
212 { 0.86, 136/256., (100-128)/256., (183-128)/256. },
213 { 1, 68/256., (117-128)/256., (203-128)/256. }},
215 { 0, 10/256., (134-128)/256., (132-128)/256. },
216 { 0.23, 21/256., (137-128)/256., (130-128)/256. },
217 { 0.45, 35/256., (134-128)/256., (134-128)/256. },
218 { 0.57, 51/256., (130-128)/256., (139-128)/256. },
219 { 0.67, 104/256., (116-128)/256., (162-128)/256. },
220 { 0.77, 120/256., (105-128)/256., (188-128)/256. },
221 { 0.87, 140/256., (105-128)/256., (188-128)/256. },
225 { 0.23, 44/256., (132-128)/256., (127-128)/256. },
226 { 0.45, 62/256., (116-128)/256., (140-128)/256. },
227 { 0.57, 75/256., (105-128)/256., (152-128)/256. },
228 { 0.67, 95/256., (91-128)/256., (166-128)/256. },
229 { 0.77, 126/256., (74-128)/256., (172-128)/256. },
230 { 0.87, 164/256., (73-128)/256., (162-128)/256. },
234 { 0.23, 36/256., (116-128)/256., (163-128)/256. },
235 { 0.45, 52/256., (102-128)/256., (200-128)/256. },
236 { 0.57, 116/256., (84-128)/256., (196-128)/256. },
237 { 0.67, 157/256., (67-128)/256., (181-128)/256. },
238 { 0.77, 193/256., (40-128)/256., (155-128)/256. },
239 { 0.87, 221/256., (101-128)/256., (134-128)/256. },
243 { 0.20, 29/256., (136-128)/256., (119-128)/256. },
244 { 0.30, 60/256., (119-128)/256., (90-128)/256. },
245 { 0.40, 85/256., (91-128)/256., (85-128)/256. },
246 { 0.50, 116/256., (70-128)/256., (105-128)/256. },
247 { 0.60, 151/256., (50-128)/256., (146-128)/256. },
248 { 0.70, 191/256., (63-128)/256., (178-128)/256. },
249 { 1, 98/256., (80-128)/256., (221-128)/256. }},
256 { 0.10, 23/256., (175-128)/256., (120-128)/256. },
257 { 0.23, 43/256., (158-128)/256., (144-128)/256. },
258 { 0.35, 85/256., (138-128)/256., (179-128)/256. },
259 { 0.48, 96/256., (128-128)/256., (189-128)/256. },
260 { 0.64, 128/256., (103-128)/256., (214-128)/256. },
261 { 0.92, 205/256., (80-128)/256., (152-128)/256. },
269 { 0.10, 0x39/255., (0x9D -128)/255., (0x8F -128)/255. },
270 { 0.23, 0x5C/255., (0x9A -128)/255., (0x68 -128)/255. },
271 { 0.35, 0x69/255., (0x93 -128)/255., (0x57 -128)/255. },
272 { 0.48, 0x76/255., (0x88 -128)/255., (0x4B -128)/255. },
273 { 0.64, 0x8A/255., (0x72 -128)/255., (0x4F -128)/255. },
274 { 0.80, 0xA3/255., (0x50 -128)/255., (0x66 -128)/255. },
275 { 1, 0xCC/255., (0x2F -128)/255., (0x87 -128)/255. }},
278 { 0.10, 0x27/255., (0xC2 -128)/255., (0x82 -128)/255. },
279 { 0.58, 0x5B/255., (0x9A -128)/255., (0xAE -128)/255. },
280 { 0.70, 0x89/255., (0x44 -128)/255., (0xAB -128)/255. },
281 { 0.80, 0xB4/255., (0x2B -128)/255., (0x9E -128)/255. },
282 { 0.91, 0xD2/255., (0x38 -128)/255., (0x92 -128)/255. },
286 { 0.20, 0x28/255., (0x98 -128)/255., (0x6F -128)/255. },
287 { 0.50, 0x48/255., (0x95 -128)/255., (0x74 -128)/255. },
288 { 0.63, 0x69/255., (0x84 -128)/255., (0x7F -128)/255. },
289 { 0.76, 0x89/255., (0x75 -128)/255., (0x84 -128)/255. },
290 { 0.90, 0xCE/255., (0x35 -128)/255., (0x95 -128)/255. },
295 { 0.60, 1, -.5, -.5 },
296 { 0.85, 1, -.5, .5 },
300 static av_cold void uninit(AVFilterContext *ctx)
302 ShowSpectrumContext *s = ctx->priv;
305 av_freep(&s->combine_buffer);
307 for (i = 0; i < s->nb_display_channels; i++)
308 av_fft_end(s->fft[i]);
312 for (i = 0; i < s->nb_display_channels; i++)
313 av_fft_end(s->ifft[i]);
317 for (i = 0; i < s->nb_display_channels; i++)
318 av_freep(&s->fft_data[i]);
320 av_freep(&s->fft_data);
321 if (s->fft_scratch) {
322 for (i = 0; i < s->nb_display_channels; i++)
323 av_freep(&s->fft_scratch[i]);
325 av_freep(&s->fft_scratch);
326 if (s->color_buffer) {
327 for (i = 0; i < s->nb_display_channels; i++)
328 av_freep(&s->color_buffer[i]);
330 av_freep(&s->color_buffer);
331 av_freep(&s->window_func_lut);
333 for (i = 0; i < s->nb_display_channels; i++)
334 av_freep(&s->magnitudes[i]);
336 av_freep(&s->magnitudes);
337 av_frame_free(&s->outpicref);
338 av_audio_fifo_free(s->fifo);
340 for (i = 0; i < s->nb_display_channels; i++)
341 av_freep(&s->phases[i]);
343 av_freep(&s->phases);
346 static int query_formats(AVFilterContext *ctx)
348 AVFilterFormats *formats = NULL;
349 AVFilterChannelLayouts *layouts = NULL;
350 AVFilterLink *inlink = ctx->inputs[0];
351 AVFilterLink *outlink = ctx->outputs[0];
352 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
353 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE };
356 /* set input audio formats */
357 formats = ff_make_format_list(sample_fmts);
358 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
361 layouts = ff_all_channel_layouts();
362 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
365 formats = ff_all_samplerates();
366 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
369 /* set output video format */
370 formats = ff_make_format_list(pix_fmts);
371 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
377 static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
379 ShowSpectrumContext *s = ctx->priv;
380 AVFilterLink *inlink = ctx->inputs[0];
381 const float *window_func_lut = s->window_func_lut;
383 const int ch = jobnr;
386 /* fill FFT input with the number of samples available */
387 const float *p = (float *)fin->extended_data[ch];
389 for (n = 0; n < s->win_size; n++) {
390 s->fft_data[ch][n].re = p[n] * window_func_lut[n];
391 s->fft_data[ch][n].im = 0;
395 float theta, phi, psi, a, b, S, c;
396 FFTComplex *g = s->fft_data[ch];
397 FFTComplex *h = s->fft_scratch[ch];
400 int M = s->win_size / 2;
402 phi = 2.f * M_PI * (s->stop - s->start) / (float)inlink->sample_rate / (M - 1);
403 theta = 2.f * M_PI * s->start / (float)inlink->sample_rate;
405 for (int n = 0; n < M; n++) {
406 h[n].re = cosf(n * n / 2.f * phi);
407 h[n].im = sinf(n * n / 2.f * phi);
410 for (int n = M; n < L; n++) {
415 for (int n = L - N; n < L; n++) {
416 h[n].re = cosf((L - n) * (L - n) / 2.f * phi);
417 h[n].im = sinf((L - n) * (L - n) / 2.f * phi);
420 for (int n = 0; n < N; n++) {
421 g[n].re = s->fft_data[ch][n].re;
422 g[n].im = s->fft_data[ch][n].im;
425 for (int n = N; n < L; n++) {
430 for (int n = 0; n < N; n++) {
431 psi = n * theta + n * n / 2.f * phi;
434 a = c * g[n].re - S * g[n].im;
435 b = S * g[n].re + c * g[n].im;
440 av_fft_permute(s->fft[ch], h);
441 av_fft_calc(s->fft[ch], h);
443 av_fft_permute(s->fft[ch], g);
444 av_fft_calc(s->fft[ch], g);
446 for (int n = 0; n < L; n++) {
449 a = c * h[n].re - S * h[n].im;
450 b = S * h[n].re + c * h[n].im;
456 av_fft_permute(s->ifft[ch], g);
457 av_fft_calc(s->ifft[ch], g);
459 for (int k = 0; k < M; k++) {
460 psi = k * k / 2.f * phi;
463 a = c * g[k].re - S * g[k].im;
464 b = S * g[k].re + c * g[k].im;
465 s->fft_data[ch][k].re = a;
466 s->fft_data[ch][k].im = b;
469 /* run FFT on each samples set */
470 av_fft_permute(s->fft[ch], s->fft_data[ch]);
471 av_fft_calc(s->fft[ch], s->fft_data[ch]);
477 static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
483 font = avpriv_cga_font, font_height = 8;
485 for (i = 0; txt[i]; i++) {
489 for (char_y = font_height - 1; char_y >= 0; char_y--) {
490 uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x;
491 for (mask = 0x80; mask; mask >>= 1) {
492 if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
493 p[char_y] = ~p[char_y];
494 p += pic->linesize[0];
498 uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8);
499 for (char_y = 0; char_y < font_height; char_y++) {
500 for (mask = 0x80; mask; mask >>= 1) {
501 if (font[txt[i] * font_height + char_y] & mask)
505 p += pic->linesize[0] - 8;
511 static void color_range(ShowSpectrumContext *s, int ch,
512 float *yf, float *uf, float *vf)
516 // reduce range by channel count
517 *yf = 256.0f / s->nb_display_channels;
518 switch (s->color_mode) {
537 /* adjust saturation for mixed UV coloring */
538 /* this factor is correct for infinite channels, an approximation otherwise */
556 if (s->color_mode == CHANNEL) {
557 if (s->nb_display_channels > 1) {
558 *uf *= 0.5f * sinf((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
559 *vf *= 0.5f * cosf((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
561 *uf *= 0.5f * sinf(M_PI * s->rotation);
562 *vf *= 0.5f * cosf(M_PI * s->rotation + M_PI_2);
565 *uf += *uf * sinf(M_PI * s->rotation);
566 *vf += *vf * cosf(M_PI * s->rotation + M_PI_2);
569 *uf *= s->saturation;
570 *vf *= s->saturation;
573 static void pick_color(ShowSpectrumContext *s,
574 float yf, float uf, float vf,
577 if (s->color_mode > CHANNEL) {
578 const int cm = s->color_mode;
582 for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
583 if (color_table[cm][i].a >= a)
585 // i now is the first item >= the color
586 // now we know to interpolate between item i - 1 and i
587 if (a <= color_table[cm][i - 1].a) {
588 y = color_table[cm][i - 1].y;
589 u = color_table[cm][i - 1].u;
590 v = color_table[cm][i - 1].v;
591 } else if (a >= color_table[cm][i].a) {
592 y = color_table[cm][i].y;
593 u = color_table[cm][i].u;
594 v = color_table[cm][i].v;
596 float start = color_table[cm][i - 1].a;
597 float end = color_table[cm][i].a;
598 float lerpfrac = (a - start) / (end - start);
599 y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
600 + color_table[cm][i].y * lerpfrac;
601 u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
602 + color_table[cm][i].u * lerpfrac;
603 v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
604 + color_table[cm][i].v * lerpfrac;
617 static char *get_time(AVFilterContext *ctx, float seconds, int x)
622 units = av_asprintf("0");
623 else if (log10(seconds) > 6)
624 units = av_asprintf("%.2fh", seconds / (60 * 60));
625 else if (log10(seconds) > 3)
626 units = av_asprintf("%.2fm", seconds / 60);
628 units = av_asprintf("%.2fs", seconds);
632 static float log_scale(const float value, const float min, const float max)
640 const float b = logf(max / min) / (max - min);
641 const float a = max / expf(max * b);
643 return expf(value * b) * a;
647 static float get_log_hz(const int bin, const int num_bins, const float sample_rate)
649 const float max_freq = sample_rate / 2;
650 const float hz_per_bin = max_freq / num_bins;
651 const float freq = hz_per_bin * bin;
652 const float scaled_freq = log_scale(freq + 1, 21, max_freq) - 1;
654 return num_bins * scaled_freq / max_freq;
657 static float inv_log_scale(const float value, const float min, const float max)
665 const float b = logf(max / min) / (max - min);
666 const float a = max / expf(max * b);
668 return logf(value / a) / b;
672 static float bin_pos(const int bin, const int num_bins, const float sample_rate)
674 const float max_freq = sample_rate / 2;
675 const float hz_per_bin = max_freq / num_bins;
676 const float freq = hz_per_bin * bin;
677 const float scaled_freq = inv_log_scale(freq + 1, 21, max_freq) - 1;
679 return num_bins * scaled_freq / max_freq;
682 static int draw_legend(AVFilterContext *ctx, int samples)
684 ShowSpectrumContext *s = ctx->priv;
685 AVFilterLink *inlink = ctx->inputs[0];
686 AVFilterLink *outlink = ctx->outputs[0];
687 int ch, y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
688 int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL);
689 float spp = samples / (float)sz;
692 char chlayout_str[128];
694 av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), inlink->channels,
695 inlink->channel_layout);
697 text = av_asprintf("%d Hz | %s", inlink->sample_rate, chlayout_str);
699 drawtext(s->outpicref, 2, outlink->h - 10, "CREATED BY LIBAVFILTER", 0);
700 drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, outlink->h - 10, text, 0);
702 char *text = av_asprintf("Zoom: %d Hz - %d Hz", s->start, s->stop);
703 drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, 3, text, 0);
709 dst = s->outpicref->data[0] + (s->start_y - 1) * s->outpicref->linesize[0] + s->start_x - 1;
710 for (x = 0; x < s->w + 1; x++)
712 dst = s->outpicref->data[0] + (s->start_y + s->h) * s->outpicref->linesize[0] + s->start_x - 1;
713 for (x = 0; x < s->w + 1; x++)
715 for (y = 0; y < s->h + 2; y++) {
716 dst = s->outpicref->data[0] + (y + s->start_y - 1) * s->outpicref->linesize[0];
717 dst[s->start_x - 1] = 200;
718 dst[s->start_x + s->w] = 200;
720 if (s->orientation == VERTICAL) {
721 int h = s->mode == SEPARATE ? s->h / s->nb_display_channels : s->h;
722 int hh = s->mode == SEPARATE ? -(s->h % s->nb_display_channels) + 1 : 1;
723 for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
724 for (y = 0; y < h; y += 20) {
725 dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
726 dst[s->start_x - 2] = 200;
727 dst[s->start_x + s->w + 1] = 200;
729 for (y = 0; y < h; y += 40) {
730 dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
731 dst[s->start_x - 3] = 200;
732 dst[s->start_x + s->w + 2] = 200;
734 dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x;
735 for (x = 0; x < s->w; x+=40)
737 dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x;
738 for (x = 0; x < s->w; x+=80)
740 dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x;
741 for (x = 0; x < s->w; x+=40) {
744 dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x;
745 for (x = 0; x < s->w; x+=80) {
748 for (y = 0; y < h; y += 40) {
749 float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
750 float bin = s->fscale == F_LINEAR ? y : get_log_hz(y, h, inlink->sample_rate);
751 float hertz = s->start + bin * range / (float)(1 << (int)ceil(log2(h)));
755 units = av_asprintf("DC");
757 units = av_asprintf("%.2f", hertz);
759 return AVERROR(ENOMEM);
761 drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4 - hh, units, 0);
766 for (x = 0; x < s->w && s->single_pic; x+=80) {
767 float seconds = x * spp / inlink->sample_rate;
768 char *units = get_time(ctx, seconds, x);
770 drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->h + s->start_y + 6, units, 0);
771 drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->start_y - 12, units, 0);
775 drawtext(s->outpicref, outlink->w / 2 - 4 * 4, outlink->h - s->start_y / 2, "TIME", 0);
776 drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 14 * 4, "FREQUENCY (Hz)", 1);
778 int w = s->mode == SEPARATE ? s->w / s->nb_display_channels : s->w;
779 for (y = 0; y < s->h; y += 20) {
780 dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
781 dst[s->start_x - 2] = 200;
782 dst[s->start_x + s->w + 1] = 200;
784 for (y = 0; y < s->h; y += 40) {
785 dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
786 dst[s->start_x - 3] = 200;
787 dst[s->start_x + s->w + 2] = 200;
789 for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
790 dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
791 for (x = 0; x < w; x+=40)
793 dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x + w * ch;
794 for (x = 0; x < w; x+=80)
796 dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x + w * ch;
797 for (x = 0; x < w; x+=40) {
800 dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
801 for (x = 0; x < w; x+=80) {
804 for (x = 0; x < w - 79; x += 80) {
805 float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
806 float bin = s->fscale == F_LINEAR ? x : get_log_hz(x, w, inlink->sample_rate);
807 float hertz = s->start + bin * range / (float)(1 << (int)ceil(log2(w)));
811 units = av_asprintf("DC");
813 units = av_asprintf("%.2f", hertz);
815 return AVERROR(ENOMEM);
817 drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->start_y - 12, units, 0);
818 drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->h + s->start_y + 6, units, 0);
822 for (y = 0; y < s->h && s->single_pic; y+=40) {
823 float seconds = y * spp / inlink->sample_rate;
824 char *units = get_time(ctx, seconds, x);
826 drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0);
829 drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 4 * 4, "TIME", 1);
830 drawtext(s->outpicref, outlink->w / 2 - 14 * 4, outlink->h - s->start_y / 2, "FREQUENCY (Hz)", 0);
833 for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) {
834 int h = multi ? s->h / s->nb_display_channels : s->h;
836 for (y = 0; y < h; y++) {
837 float out[3] = { 0., 127.5, 127.5};
840 for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) {
842 int channel = (multi) ? s->nb_display_channels - ch - 1 : chn;
845 color_range(s, channel, &yf, &uf, &vf);
846 pick_color(s, yf, uf, vf, y / (float)h, lout);
851 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);
852 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);
853 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);
856 for (y = 0; ch == 0 && y < h; y += h / 10) {
857 float value = 120.f * log10f(1.f - y / (float)h);
862 text = av_asprintf("%.0f dB", value);
865 drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 5, text, 0);
873 static float get_value(AVFilterContext *ctx, int ch, int y)
875 ShowSpectrumContext *s = ctx->priv;
876 float *magnitudes = s->magnitudes[ch];
877 float *phases = s->phases[ch];
896 a = av_clipf(a, 0, 1);
899 a = av_clipf(sqrtf(a), 0, 1);
902 a = av_clipf(cbrtf(a), 0, 1);
905 a = av_clipf(sqrtf(sqrtf(a)), 0, 1);
908 a = av_clipf(powf(a, 0.20), 0, 1);
911 a = 1.f + log10f(av_clipf(a, 1e-6, 1)) / 6.f; // zero = -120dBFS
920 static int plot_channel_lin(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
922 ShowSpectrumContext *s = ctx->priv;
923 const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
924 const int ch = jobnr;
928 /* decide color range */
929 color_range(s, ch, &yf, &uf, &vf);
931 /* draw the channel */
932 for (y = 0; y < h; y++) {
933 int row = (s->mode == COMBINED) ? y : ch * h + y;
934 float *out = &s->color_buffer[ch][3 * row];
935 float a = get_value(ctx, ch, y);
937 pick_color(s, yf, uf, vf, a, out);
943 static int plot_channel_log(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
945 ShowSpectrumContext *s = ctx->priv;
946 AVFilterLink *inlink = ctx->inputs[0];
947 const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
948 const int ch = jobnr;
952 /* decide color range */
953 color_range(s, ch, &yf, &uf, &vf);
955 /* draw the channel */
956 for (y = 0; y < h && yy < h; yy++) {
957 float pos0 = bin_pos(yy+0, h, inlink->sample_rate);
958 float pos1 = bin_pos(yy+1, h, inlink->sample_rate);
959 float delta = pos1 - pos0;
962 a0 = get_value(ctx, ch, yy+0);
963 a1 = get_value(ctx, ch, FFMIN(yy+1, h-1));
964 for (float j = pos0; j < pos1 && y + j - pos0 < h; j++) {
965 float row = (s->mode == COMBINED) ? y + j - pos0 : ch * h + y + j - pos0;
966 float *out = &s->color_buffer[ch][3 * FFMIN(lrintf(row), h-1)];
967 float lerpfrac = (j - pos0) / delta;
969 pick_color(s, yf, uf, vf, lerpfrac * a1 + (1.f-lerpfrac) * a0, out);
977 static int config_output(AVFilterLink *outlink)
979 AVFilterContext *ctx = outlink->src;
980 AVFilterLink *inlink = ctx->inputs[0];
981 ShowSpectrumContext *s = ctx->priv;
982 int i, fft_bits, h, w;
986 case F_LINEAR: s->plot_channel = plot_channel_lin; break;
987 case F_LOG: s->plot_channel = plot_channel_log; break;
988 default: return AVERROR_BUG;
991 s->stop = FFMIN(s->stop, inlink->sample_rate / 2);
992 if (s->stop && s->stop <= s->start) {
993 av_log(ctx, AV_LOG_ERROR, "Stop frequency should be greater than start.\n");
994 return AVERROR(EINVAL);
997 if (!strcmp(ctx->filter->name, "showspectrumpic"))
1002 outlink->sample_aspect_ratio = (AVRational){1,1};
1005 s->start_x = (log10(inlink->sample_rate) + 1) * 25;
1007 outlink->w += s->start_x * 2;
1008 outlink->h += s->start_y * 2;
1011 h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? s->h : s->h / inlink->channels;
1012 w = (s->mode == COMBINED || s->orientation == VERTICAL) ? s->w : s->w / inlink->channels;
1013 s->channel_height = h;
1014 s->channel_width = w;
1016 if (s->orientation == VERTICAL) {
1017 /* FFT window size (precision) according to the requested output frame height */
1018 for (fft_bits = 1; 1 << fft_bits < 2 * h; fft_bits++);
1020 /* FFT window size (precision) according to the requested output frame width */
1021 for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++);
1024 s->win_size = 1 << fft_bits;
1025 s->buf_size = s->win_size << !!s->stop;
1028 s->fft = av_calloc(inlink->channels, sizeof(*s->fft));
1030 return AVERROR(ENOMEM);
1035 s->ifft = av_calloc(inlink->channels, sizeof(*s->ifft));
1037 return AVERROR(ENOMEM);
1041 /* (re-)configuration if the video output changed (or first init) */
1042 if (fft_bits != s->fft_bits) {
1045 s->fft_bits = fft_bits;
1047 /* FFT buffers: x2 for each (display) channel buffer.
1048 * Note: we use free and malloc instead of a realloc-like function to
1049 * make sure the buffer is aligned in memory for the FFT functions. */
1050 for (i = 0; i < s->nb_display_channels; i++) {
1052 av_fft_end(s->ifft[i]);
1053 av_freep(&s->fft_scratch[i]);
1055 av_fft_end(s->fft[i]);
1056 av_freep(&s->fft_data[i]);
1058 av_freep(&s->fft_data);
1060 s->nb_display_channels = inlink->channels;
1061 for (i = 0; i < s->nb_display_channels; i++) {
1062 s->fft[i] = av_fft_init(fft_bits + !!s->stop, 0);
1064 s->ifft[i] = av_fft_init(fft_bits + !!s->stop, 1);
1066 av_log(ctx, AV_LOG_ERROR, "Unable to create Inverse FFT context. "
1067 "The window size might be too high.\n");
1068 return AVERROR(EINVAL);
1072 av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
1073 "The window size might be too high.\n");
1074 return AVERROR(EINVAL);
1078 s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
1080 return AVERROR(ENOMEM);
1081 for (i = 0; i < s->nb_display_channels; i++) {
1082 s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
1083 if (!s->magnitudes[i])
1084 return AVERROR(ENOMEM);
1087 s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases));
1089 return AVERROR(ENOMEM);
1090 for (i = 0; i < s->nb_display_channels; i++) {
1091 s->phases[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->phases));
1093 return AVERROR(ENOMEM);
1096 av_freep(&s->color_buffer);
1097 s->color_buffer = av_calloc(s->nb_display_channels, sizeof(*s->color_buffer));
1098 if (!s->color_buffer)
1099 return AVERROR(ENOMEM);
1100 for (i = 0; i < s->nb_display_channels; i++) {
1101 s->color_buffer[i] = av_calloc(s->orientation == VERTICAL ? s->h * 3 : s->w * 3, sizeof(**s->color_buffer));
1102 if (!s->color_buffer[i])
1103 return AVERROR(ENOMEM);
1106 s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
1108 return AVERROR(ENOMEM);
1109 s->fft_scratch = av_calloc(s->nb_display_channels, sizeof(*s->fft_scratch));
1110 if (!s->fft_scratch)
1111 return AVERROR(ENOMEM);
1112 for (i = 0; i < s->nb_display_channels; i++) {
1113 s->fft_data[i] = av_calloc(s->buf_size, sizeof(**s->fft_data));
1114 if (!s->fft_data[i])
1115 return AVERROR(ENOMEM);
1117 s->fft_scratch[i] = av_calloc(s->buf_size, sizeof(**s->fft_scratch));
1118 if (!s->fft_scratch[i])
1119 return AVERROR(ENOMEM);
1122 /* pre-calc windowing function */
1123 s->window_func_lut =
1124 av_realloc_f(s->window_func_lut, s->win_size,
1125 sizeof(*s->window_func_lut));
1126 if (!s->window_func_lut)
1127 return AVERROR(ENOMEM);
1128 generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
1129 if (s->overlap == 1)
1130 s->overlap = overlap;
1131 s->hop_size = (1.f - s->overlap) * s->win_size;
1132 if (s->hop_size < 1) {
1133 av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
1134 return AVERROR(EINVAL);
1137 for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
1138 s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
1140 s->win_scale = 1.f / sqrtf(s->win_scale);
1142 /* prepare the initial picref buffer (black frame) */
1143 av_frame_free(&s->outpicref);
1144 s->outpicref = outpicref =
1145 ff_get_video_buffer(outlink, outlink->w, outlink->h);
1147 return AVERROR(ENOMEM);
1148 outpicref->sample_aspect_ratio = (AVRational){1,1};
1149 for (i = 0; i < outlink->h; i++) {
1150 memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
1151 memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
1152 memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
1154 outpicref->color_range = AVCOL_RANGE_JPEG;
1156 if (!s->single_pic && s->legend)
1157 draw_legend(ctx, 0);
1160 if ((s->orientation == VERTICAL && s->xpos >= s->w) ||
1161 (s->orientation == HORIZONTAL && s->xpos >= s->h))
1164 s->auto_frame_rate = av_make_q(inlink->sample_rate, s->hop_size);
1165 if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
1166 s->auto_frame_rate.den *= s->w;
1167 if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
1168 s->auto_frame_rate.den *= s->h;
1169 if (!s->single_pic && strcmp(s->rate_str, "auto")) {
1170 int ret = av_parse_video_rate(&s->frame_rate, s->rate_str);
1174 s->frame_rate = s->auto_frame_rate;
1176 outlink->frame_rate = s->frame_rate;
1177 outlink->time_base = av_inv_q(outlink->frame_rate);
1179 if (s->orientation == VERTICAL) {
1181 av_realloc_f(s->combine_buffer, s->h * 3,
1182 sizeof(*s->combine_buffer));
1185 av_realloc_f(s->combine_buffer, s->w * 3,
1186 sizeof(*s->combine_buffer));
1189 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n",
1190 s->w, s->h, s->win_size);
1192 av_audio_fifo_free(s->fifo);
1193 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
1195 return AVERROR(ENOMEM);
1199 #define RE(y, ch) s->fft_data[ch][y].re
1200 #define IM(y, ch) s->fft_data[ch][y].im
1201 #define MAGNITUDE(y, ch) hypotf(RE(y, ch), IM(y, ch))
1202 #define PHASE(y, ch) atan2f(IM(y, ch), RE(y, ch))
1204 static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1206 ShowSpectrumContext *s = ctx->priv;
1207 const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
1208 int y, h = s->orientation == VERTICAL ? s->h : s->w;
1209 const float f = s->gain * w;
1210 const int ch = jobnr;
1211 float *magnitudes = s->magnitudes[ch];
1213 for (y = 0; y < h; y++)
1214 magnitudes[y] = MAGNITUDE(y, ch) * f;
1219 static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1221 ShowSpectrumContext *s = ctx->priv;
1222 const int h = s->orientation == VERTICAL ? s->h : s->w;
1223 const int ch = jobnr;
1224 float *phases = s->phases[ch];
1227 for (y = 0; y < h; y++)
1228 phases[y] = (PHASE(y, ch) / M_PI + 1) / 2;
1233 static void acalc_magnitudes(ShowSpectrumContext *s)
1235 const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
1236 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
1237 const float f = s->gain * w;
1239 for (ch = 0; ch < s->nb_display_channels; ch++) {
1240 float *magnitudes = s->magnitudes[ch];
1242 for (y = 0; y < h; y++)
1243 magnitudes[y] += MAGNITUDE(y, ch) * f;
1247 static void scale_magnitudes(ShowSpectrumContext *s, float scale)
1249 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
1251 for (ch = 0; ch < s->nb_display_channels; ch++) {
1252 float *magnitudes = s->magnitudes[ch];
1254 for (y = 0; y < h; y++)
1255 magnitudes[y] *= scale;
1259 static void clear_combine_buffer(ShowSpectrumContext *s, int size)
1263 for (y = 0; y < size; y++) {
1264 s->combine_buffer[3 * y ] = 0;
1265 s->combine_buffer[3 * y + 1] = 127.5;
1266 s->combine_buffer[3 * y + 2] = 127.5;
1270 static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
1272 AVFilterContext *ctx = inlink->dst;
1273 AVFilterLink *outlink = ctx->outputs[0];
1274 ShowSpectrumContext *s = ctx->priv;
1275 AVFrame *outpicref = s->outpicref;
1276 int ret, plane, x, y, z = s->orientation == VERTICAL ? s->h : s->w;
1278 /* fill a new spectrum column */
1279 /* initialize buffer for combining to black */
1280 clear_combine_buffer(s, z);
1282 ctx->internal->execute(ctx, s->plot_channel, NULL, NULL, s->nb_display_channels);
1284 for (y = 0; y < z * 3; y++) {
1285 for (x = 0; x < s->nb_display_channels; x++) {
1286 s->combine_buffer[y] += s->color_buffer[x][y];
1290 av_frame_make_writable(s->outpicref);
1291 /* copy to output */
1292 if (s->orientation == VERTICAL) {
1293 if (s->sliding == SCROLL) {
1294 for (plane = 0; plane < 3; plane++) {
1295 for (y = 0; y < s->h; y++) {
1296 uint8_t *p = outpicref->data[plane] + s->start_x +
1297 (y + s->start_y) * outpicref->linesize[plane];
1298 memmove(p, p + 1, s->w - 1);
1302 } else if (s->sliding == RSCROLL) {
1303 for (plane = 0; plane < 3; plane++) {
1304 for (y = 0; y < s->h; y++) {
1305 uint8_t *p = outpicref->data[plane] + s->start_x +
1306 (y + s->start_y) * outpicref->linesize[plane];
1307 memmove(p + 1, p, s->w - 1);
1312 for (plane = 0; plane < 3; plane++) {
1313 uint8_t *p = outpicref->data[plane] + s->start_x +
1314 (outlink->h - 1 - s->start_y) * outpicref->linesize[plane] +
1316 for (y = 0; y < s->h; y++) {
1317 *p = lrintf(av_clipf(s->combine_buffer[3 * y + plane], 0, 255));
1318 p -= outpicref->linesize[plane];
1322 if (s->sliding == SCROLL) {
1323 for (plane = 0; plane < 3; plane++) {
1324 for (y = 1; y < s->h; y++) {
1325 memmove(outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
1326 outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
1331 } else if (s->sliding == RSCROLL) {
1332 for (plane = 0; plane < 3; plane++) {
1333 for (y = s->h - 1; y >= 1; y--) {
1334 memmove(outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
1335 outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
1341 for (plane = 0; plane < 3; plane++) {
1342 uint8_t *p = outpicref->data[plane] + s->start_x +
1343 (s->xpos + s->start_y) * outpicref->linesize[plane];
1344 for (x = 0; x < s->w; x++) {
1345 *p = lrintf(av_clipf(s->combine_buffer[3 * x + plane], 0, 255));
1351 if (s->sliding != FULLFRAME || s->xpos == 0)
1352 outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
1355 if (s->orientation == VERTICAL && s->xpos >= s->w)
1357 if (s->orientation == HORIZONTAL && s->xpos >= s->h)
1359 if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
1360 if (s->old_pts < outpicref->pts) {
1362 char *units = get_time(ctx, insamples->pts /(float)inlink->sample_rate, x);
1364 if (s->orientation == VERTICAL) {
1365 for (y = 0; y < 10; y++) {
1366 memset(s->outpicref->data[0] + outlink->w / 2 - 4 * s->old_len +
1367 (outlink->h - s->start_y / 2 - 20 + y) * s->outpicref->linesize[0], 0, 10 * s->old_len);
1369 drawtext(s->outpicref,
1370 outlink->w / 2 - 4 * strlen(units),
1371 outlink->h - s->start_y / 2 - 20,
1374 for (y = 0; y < 10 * s->old_len; y++) {
1375 memset(s->outpicref->data[0] + s->start_x / 7 + 20 +
1376 (outlink->h / 2 - 4 * s->old_len + y) * s->outpicref->linesize[0], 0, 10);
1378 drawtext(s->outpicref,
1379 s->start_x / 7 + 20,
1380 outlink->h / 2 - 4 * strlen(units),
1383 s->old_len = strlen(units);
1386 s->old_pts = outpicref->pts;
1387 ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
1397 #if CONFIG_SHOWSPECTRUM_FILTER
1399 static int activate(AVFilterContext *ctx)
1401 AVFilterLink *inlink = ctx->inputs[0];
1402 AVFilterLink *outlink = ctx->outputs[0];
1403 ShowSpectrumContext *s = ctx->priv;
1406 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
1408 if (av_audio_fifo_size(s->fifo) < s->win_size) {
1409 AVFrame *frame = NULL;
1411 ret = ff_inlink_consume_frame(inlink, &frame);
1415 s->pts = frame->pts;
1418 av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
1419 av_frame_free(&frame);
1423 if (s->outpicref && av_audio_fifo_size(s->fifo) >= s->win_size) {
1424 AVFrame *fin = ff_get_audio_buffer(inlink, s->win_size);
1426 return AVERROR(ENOMEM);
1428 fin->pts = s->pts + s->consumed;
1429 s->consumed += s->hop_size;
1430 ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data,
1431 FFMIN(s->win_size, av_audio_fifo_size(s->fifo)));
1433 av_frame_free(&fin);
1437 av_assert0(fin->nb_samples == s->win_size);
1439 ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
1441 if (s->data == D_MAGNITUDE)
1442 ctx->internal->execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels);
1444 if (s->data == D_PHASE)
1445 ctx->internal->execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels);
1447 ret = plot_spectrum_column(inlink, fin);
1449 av_frame_free(&fin);
1450 av_audio_fifo_drain(s->fifo, s->hop_size);
1455 if (ff_outlink_get_status(inlink) == AVERROR_EOF &&
1456 s->sliding == FULLFRAME &&
1457 s->xpos > 0 && s->outpicref) {
1460 if (s->orientation == VERTICAL) {
1461 for (int i = 0; i < outlink->h; i++) {
1462 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos);
1463 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
1464 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
1467 for (int i = s->xpos; i < outlink->h; i++) {
1468 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w);
1469 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
1470 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
1473 s->outpicref->pts += s->consumed;
1474 pts = s->outpicref->pts;
1475 ret = ff_filter_frame(outlink, s->outpicref);
1476 s->outpicref = NULL;
1477 ff_outlink_set_status(outlink, AVERROR_EOF, pts);
1481 FF_FILTER_FORWARD_STATUS(inlink, outlink);
1482 if (ff_outlink_frame_wanted(outlink) && av_audio_fifo_size(s->fifo) < s->win_size) {
1483 ff_inlink_request_frame(inlink);
1487 if (av_audio_fifo_size(s->fifo) >= s->win_size) {
1488 ff_filter_set_ready(ctx, 10);
1491 return FFERROR_NOT_READY;
1494 static const AVFilterPad showspectrum_inputs[] = {
1497 .type = AVMEDIA_TYPE_AUDIO,
1502 static const AVFilterPad showspectrum_outputs[] = {
1505 .type = AVMEDIA_TYPE_VIDEO,
1506 .config_props = config_output,
1511 AVFilter ff_avf_showspectrum = {
1512 .name = "showspectrum",
1513 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
1515 .query_formats = query_formats,
1516 .priv_size = sizeof(ShowSpectrumContext),
1517 .inputs = showspectrum_inputs,
1518 .outputs = showspectrum_outputs,
1519 .activate = activate,
1520 .priv_class = &showspectrum_class,
1521 .flags = AVFILTER_FLAG_SLICE_THREADS,
1523 #endif // CONFIG_SHOWSPECTRUM_FILTER
1525 #if CONFIG_SHOWSPECTRUMPIC_FILTER
1527 static const AVOption showspectrumpic_options[] = {
1528 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
1529 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
1530 { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" },
1531 { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
1532 { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
1533 { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" },
1534 { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
1535 { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
1536 { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
1537 { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
1538 { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
1539 { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
1540 { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
1541 { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
1542 { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
1543 { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
1544 { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
1545 { "viridis", "viridis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=VIRIDIS}, 0, 0, FLAGS, "color" },
1546 { "plasma", "plasma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=PLASMA}, 0, 0, FLAGS, "color" },
1547 { "cividis", "cividis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=CIVIDIS}, 0, 0, FLAGS, "color" },
1548 { "terrain", "terrain based coloring", 0, AV_OPT_TYPE_CONST, {.i64=TERRAIN}, 0, 0, FLAGS, "color" },
1549 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" },
1550 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
1551 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
1552 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
1553 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
1554 { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
1555 { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
1556 { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=F_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
1557 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=F_LINEAR}, 0, 0, FLAGS, "fscale" },
1558 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=F_LOG}, 0, 0, FLAGS, "fscale" },
1559 { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
1560 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
1561 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
1562 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
1563 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1564 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1565 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
1566 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
1567 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
1568 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
1569 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
1570 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
1571 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
1572 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
1573 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
1574 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
1575 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
1576 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
1577 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
1578 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
1579 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
1580 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
1581 { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
1582 { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
1583 { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
1584 { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
1585 { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
1586 { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
1587 { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
1588 { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
1589 { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
1593 AVFILTER_DEFINE_CLASS(showspectrumpic);
1595 static int showspectrumpic_request_frame(AVFilterLink *outlink)
1597 AVFilterContext *ctx = outlink->src;
1598 ShowSpectrumContext *s = ctx->priv;
1599 AVFilterLink *inlink = ctx->inputs[0];
1602 ret = ff_request_frame(inlink);
1603 samples = av_audio_fifo_size(s->fifo);
1604 if (ret == AVERROR_EOF && s->outpicref && samples > 0) {
1606 int x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
1610 spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz))));
1611 spf = FFMAX(1, spf);
1613 spb = (samples / (spf * sz)) * spf;
1615 fin = ff_get_audio_buffer(inlink, s->win_size);
1617 return AVERROR(ENOMEM);
1620 ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
1622 av_frame_free(&fin);
1626 av_audio_fifo_drain(s->fifo, spf);
1628 if (ret < s->win_size) {
1629 for (ch = 0; ch < s->nb_display_channels; ch++) {
1630 memset(fin->extended_data[ch] + ret * sizeof(float), 0,
1631 (s->win_size - ret) * sizeof(float));
1635 ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
1636 acalc_magnitudes(s);
1639 if (consumed >= spb) {
1640 int h = s->orientation == VERTICAL ? s->h : s->w;
1642 scale_magnitudes(s, 1.f / (consumed / spf));
1643 plot_spectrum_column(inlink, fin);
1646 for (ch = 0; ch < s->nb_display_channels; ch++)
1647 memset(s->magnitudes[ch], 0, h * sizeof(float));
1651 av_frame_free(&fin);
1652 s->outpicref->pts = 0;
1655 draw_legend(ctx, samples);
1657 ret = ff_filter_frame(outlink, s->outpicref);
1658 s->outpicref = NULL;
1664 static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1666 AVFilterContext *ctx = inlink->dst;
1667 ShowSpectrumContext *s = ctx->priv;
1670 ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
1671 av_frame_free(&insamples);
1675 static const AVFilterPad showspectrumpic_inputs[] = {
1678 .type = AVMEDIA_TYPE_AUDIO,
1679 .filter_frame = showspectrumpic_filter_frame,
1684 static const AVFilterPad showspectrumpic_outputs[] = {
1687 .type = AVMEDIA_TYPE_VIDEO,
1688 .config_props = config_output,
1689 .request_frame = showspectrumpic_request_frame,
1694 AVFilter ff_avf_showspectrumpic = {
1695 .name = "showspectrumpic",
1696 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
1698 .query_formats = query_formats,
1699 .priv_size = sizeof(ShowSpectrumContext),
1700 .inputs = showspectrumpic_inputs,
1701 .outputs = showspectrumpic_outputs,
1702 .priv_class = &showspectrumpic_class,
1703 .flags = AVFILTER_FLAG_SLICE_THREADS,
1706 #endif // CONFIG_SHOWSPECTRUMPIC_FILTER