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/xga_font_data.h"
42 #include "window_func.h"
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 };
51 typedef struct ShowSpectrumContext {
55 int nb_display_channels;
59 int sliding; ///< 1 if sliding mode, 0 otherwise
60 int mode; ///< channel display mode
61 int color_mode; ///< display color scheme
63 float saturation; ///< color saturation multiplier
64 float rotation; ///< color rotation
65 int start, stop; ///< zoom mode
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
83 float *combine_buffer; ///< color combining buffer (3 * h items)
84 float **color_buffer; ///< color buffer (3 * h * ch items)
90 } ShowSpectrumContext;
92 #define OFFSET(x) offsetof(ShowSpectrumContext, x)
93 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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 },
160 AVFILTER_DEFINE_CLASS(showspectrum);
162 static const struct ColorTable {
164 } color_table[][8] = {
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 },
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. }},
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. }},
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. },
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. },
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. },
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. }},
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. }},
243 static av_cold void uninit(AVFilterContext *ctx)
245 ShowSpectrumContext *s = ctx->priv;
248 av_freep(&s->combine_buffer);
250 for (i = 0; i < s->nb_display_channels; i++)
251 av_fft_end(s->fft[i]);
255 for (i = 0; i < s->nb_display_channels; i++)
256 av_fft_end(s->ifft[i]);
260 for (i = 0; i < s->nb_display_channels; i++)
261 av_freep(&s->fft_data[i]);
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]);
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]);
273 av_freep(&s->color_buffer);
274 av_freep(&s->window_func_lut);
276 for (i = 0; i < s->nb_display_channels; i++)
277 av_freep(&s->magnitudes[i]);
279 av_freep(&s->magnitudes);
280 av_frame_free(&s->outpicref);
281 av_audio_fifo_free(s->fifo);
283 for (i = 0; i < s->nb_display_channels; i++)
284 av_freep(&s->phases[i]);
286 av_freep(&s->phases);
289 static int query_formats(AVFilterContext *ctx)
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 };
299 /* set input audio formats */
300 formats = ff_make_format_list(sample_fmts);
301 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
304 layouts = ff_all_channel_layouts();
305 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
308 formats = ff_all_samplerates();
309 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
312 /* set output video format */
313 formats = ff_make_format_list(pix_fmts);
314 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
320 static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
322 ShowSpectrumContext *s = ctx->priv;
323 AVFilterLink *inlink = ctx->inputs[0];
324 const float *window_func_lut = s->window_func_lut;
326 const int ch = jobnr;
329 /* fill FFT input with the number of samples available */
330 const float *p = (float *)fin->extended_data[ch];
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;
338 double theta, phi, psi, a, b, S, c;
339 FFTComplex *g = s->fft_data[ch];
340 FFTComplex *h = s->fft_scratch[ch];
343 int M = s->win_size / 2;
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;
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);
353 for (int n = M; n < L; n++) {
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);
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;
368 for (int n = N; n < L; n++) {
373 for (int n = 0; n < N; n++) {
374 psi = n * theta + n * n / 2.0 * phi;
377 a = c * g[n].re - S * g[n].im;
378 b = S * g[n].re + c * g[n].im;
383 av_fft_permute(s->fft[ch], h);
384 av_fft_calc(s->fft[ch], h);
386 av_fft_permute(s->fft[ch], g);
387 av_fft_calc(s->fft[ch], g);
389 for (int n = 0; n < L; n++) {
392 a = c * h[n].re - S * h[n].im;
393 b = S * h[n].re + c * h[n].im;
399 av_fft_permute(s->ifft[ch], g);
400 av_fft_calc(s->ifft[ch], g);
402 for (int k = 0; k < M; k++) {
403 psi = k * k / 2.0 * phi;
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;
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]);
420 static int config_output(AVFilterLink *outlink)
422 AVFilterContext *ctx = outlink->src;
423 AVFilterLink *inlink = ctx->inputs[0];
424 ShowSpectrumContext *s = ctx->priv;
425 int i, fft_bits, h, w;
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);
434 s->pts = AV_NOPTS_VALUE;
436 if (!strcmp(ctx->filter->name, "showspectrumpic"))
441 outlink->sample_aspect_ratio = (AVRational){1,1};
444 s->start_x = log10(inlink->sample_rate) * 25;
446 outlink->w += s->start_x * 2;
447 outlink->h += s->start_y * 2;
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;
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++);
459 /* FFT window size (precision) according to the requested output frame width */
460 for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++);
463 s->win_size = 1 << fft_bits;
464 s->buf_size = s->win_size << !!s->stop;
467 s->fft = av_calloc(inlink->channels, sizeof(*s->fft));
469 return AVERROR(ENOMEM);
474 s->ifft = av_calloc(inlink->channels, sizeof(*s->ifft));
476 return AVERROR(ENOMEM);
480 /* (re-)configuration if the video output changed (or first init) */
481 if (fft_bits != s->fft_bits) {
484 s->fft_bits = fft_bits;
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++) {
491 av_fft_end(s->ifft[i]);
492 av_freep(&s->fft_scratch[i]);
494 av_fft_end(s->fft[i]);
495 av_freep(&s->fft_data[i]);
497 av_freep(&s->fft_data);
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);
503 s->ifft[i] = av_fft_init(fft_bits + !!s->stop, 1);
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);
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);
517 s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*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);
526 s->phases = av_calloc(s->nb_display_channels, sizeof(*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));
532 return AVERROR(ENOMEM);
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);
545 s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
547 return AVERROR(ENOMEM);
548 s->fft_scratch = av_calloc(s->nb_display_channels, sizeof(*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));
554 return AVERROR(ENOMEM);
556 s->fft_scratch[i] = av_calloc(s->buf_size, sizeof(**s->fft_scratch));
557 if (!s->fft_scratch[i])
558 return AVERROR(ENOMEM);
561 /* pre-calc windowing function */
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);
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);
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];
579 s->win_scale = 1. / sqrt(s->win_scale);
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);
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);
593 outpicref->color_range = AVCOL_RANGE_JPEG;
596 if ((s->orientation == VERTICAL && s->xpos >= s->w) ||
597 (s->orientation == HORIZONTAL && s->xpos >= s->h))
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;
606 if (s->orientation == VERTICAL) {
608 av_realloc_f(s->combine_buffer, s->h * 3,
609 sizeof(*s->combine_buffer));
612 av_realloc_f(s->combine_buffer, s->w * 3,
613 sizeof(*s->combine_buffer));
616 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n",
617 s->w, s->h, s->win_size);
619 av_audio_fifo_free(s->fifo);
620 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
622 return AVERROR(ENOMEM);
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))
631 static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
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];
640 for (y = 0; y < h; y++)
641 magnitudes[y] = MAGNITUDE(y, ch) * f;
646 static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
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];
654 for (y = 0; y < h; y++)
655 phases[y] = (PHASE(y, ch) / M_PI + 1) / 2;
660 static void acalc_magnitudes(ShowSpectrumContext *s)
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;
666 for (ch = 0; ch < s->nb_display_channels; ch++) {
667 float *magnitudes = s->magnitudes[ch];
669 for (y = 0; y < h; y++)
670 magnitudes[y] += MAGNITUDE(y, ch) * f;
674 static void scale_magnitudes(ShowSpectrumContext *s, float scale)
676 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
678 for (ch = 0; ch < s->nb_display_channels; ch++) {
679 float *magnitudes = s->magnitudes[ch];
681 for (y = 0; y < h; y++)
682 magnitudes[y] *= scale;
686 static void color_range(ShowSpectrumContext *s, int ch,
687 float *yf, float *uf, float *vf)
691 // reduce range by channel count
692 *yf = 256.0f / s->nb_display_channels;
693 switch (s->color_mode) {
707 /* adjust saturation for mixed UV coloring */
708 /* this factor is correct for infinite channels, an approximation otherwise */
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);
731 *uf *= 0.5 * sin(M_PI * s->rotation);
732 *vf *= 0.5 * cos(M_PI * s->rotation + M_PI_2);
735 *uf += *uf * sin(M_PI * s->rotation);
736 *vf += *vf * cos(M_PI * s->rotation + M_PI_2);
739 *uf *= s->saturation;
740 *vf *= s->saturation;
743 static void pick_color(ShowSpectrumContext *s,
744 float yf, float uf, float vf,
747 if (s->color_mode > CHANNEL) {
748 const int cm = s->color_mode;
752 for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
753 if (color_table[cm][i].a >= a)
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;
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;
787 static void clear_combine_buffer(ShowSpectrumContext *s, int size)
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;
798 static int plot_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
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];
808 /* decide color range */
809 color_range(s, ch, &yf, &uf, &vf);
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];
833 a = av_clipf(a, 0, 1);
836 a = av_clipf(sqrt(a), 0, 1);
839 a = av_clipf(cbrt(a), 0, 1);
842 a = av_clipf(sqrt(sqrt(a)), 0, 1);
845 a = av_clipf(pow(a, 0.20), 0, 1);
848 a = 1 + log10(av_clipd(a, 1e-6, 1)) / 6; // zero = -120dBFS
854 pick_color(s, yf, uf, vf, a, out);
860 static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
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;
868 /* fill a new spectrum column */
869 /* initialize buffer for combining to black */
870 clear_combine_buffer(s, z);
872 ctx->internal->execute(ctx, plot_channel, NULL, NULL, s->nb_display_channels);
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];
880 av_frame_make_writable(s->outpicref);
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);
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);
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] +
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];
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],
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],
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));
941 if (s->sliding != FULLFRAME || s->xpos == 0)
942 outpicref->pts = insamples->pts;
945 if (s->orientation == VERTICAL && s->xpos >= s->w)
947 if (s->orientation == HORIZONTAL && s->xpos >= s->h)
949 if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
950 ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
958 #if CONFIG_SHOWSPECTRUM_FILTER
960 static int request_frame(AVFilterLink *outlink)
962 ShowSpectrumContext *s = outlink->src->priv;
963 AVFilterLink *inlink = outlink->src->inputs[0];
967 ret = ff_request_frame(inlink);
968 if (ret == AVERROR_EOF && s->sliding == FULLFRAME && s->xpos > 0 &&
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);
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);
983 ret = ff_filter_frame(outlink, s->outpicref);
990 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
992 AVFilterContext *ctx = inlink->dst;
993 ShowSpectrumContext *s = ctx->priv;
995 int ret = 0, consumed = 0;
997 if (s->pts == AV_NOPTS_VALUE)
998 s->pts = insamples->pts - av_audio_fifo_size(s->fifo);
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);
1005 ret = AVERROR(ENOMEM);
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);
1015 av_assert0(fin->nb_samples == s->win_size);
1017 ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
1019 if (s->data == D_MAGNITUDE)
1020 ctx->internal->execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels);
1022 if (s->data == D_PHASE)
1023 ctx->internal->execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels);
1025 ret = plot_spectrum_column(inlink, fin);
1026 av_frame_free(&fin);
1027 av_audio_fifo_drain(s->fifo, s->hop_size);
1033 s->pts = AV_NOPTS_VALUE;
1034 av_frame_free(&fin);
1038 static const AVFilterPad showspectrum_inputs[] = {
1041 .type = AVMEDIA_TYPE_AUDIO,
1042 .filter_frame = filter_frame,
1047 static const AVFilterPad showspectrum_outputs[] = {
1050 .type = AVMEDIA_TYPE_VIDEO,
1051 .config_props = config_output,
1052 .request_frame = request_frame,
1057 AVFilter ff_avf_showspectrum = {
1058 .name = "showspectrum",
1059 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
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,
1068 #endif // CONFIG_SHOWSPECTRUM_FILTER
1070 #if CONFIG_SHOWSPECTRUMPIC_FILTER
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 },
1127 AVFILTER_DEFINE_CLASS(showspectrumpic);
1129 static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
1131 const uint8_t *font;
1135 font = avpriv_cga_font, font_height = 8;
1137 for (i = 0; txt[i]; i++) {
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];
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)
1157 p += pic->linesize[0] - 8;
1163 static int showspectrumpic_request_frame(AVFilterLink *outlink)
1165 AVFilterContext *ctx = outlink->src;
1166 ShowSpectrumContext *s = ctx->priv;
1167 AVFilterLink *inlink = ctx->inputs[0];
1170 ret = ff_request_frame(inlink);
1171 samples = av_audio_fifo_size(s->fifo);
1172 if (ret == AVERROR_EOF && s->outpicref && samples > 0) {
1174 int y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
1178 spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz))));
1179 spf = FFMAX(1, spf);
1181 spb = (samples / (spf * sz)) * spf;
1183 fin = ff_get_audio_buffer(inlink, s->win_size);
1185 return AVERROR(ENOMEM);
1188 ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
1190 av_frame_free(&fin);
1194 av_audio_fifo_drain(s->fifo, spf);
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));
1203 ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
1204 acalc_magnitudes(s);
1207 if (consumed >= spb) {
1208 int h = s->orientation == VERTICAL ? s->h : s->w;
1210 scale_magnitudes(s, 1. / (consumed / spf));
1211 plot_spectrum_column(inlink, fin);
1214 for (ch = 0; ch < s->nb_display_channels; ch++)
1215 memset(s->magnitudes[ch], 0, h * sizeof(float));
1219 av_frame_free(&fin);
1220 s->outpicref->pts = 0;
1223 int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL);
1224 float spp = samples / (float)sz;
1227 char chlayout_str[128];
1229 av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), inlink->channels,
1230 inlink->channel_layout);
1232 text = av_asprintf("%d Hz | %s", inlink->sample_rate, chlayout_str);
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);
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++)
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++)
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;
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;
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;
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)
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)
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) {
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) {
1278 for (y = 0; y < h; y += 40) {
1279 float hertz = y * (inlink->sample_rate / 2) / (float)(1 << (int)ceil(log2(h)));
1283 units = av_asprintf("DC");
1285 units = av_asprintf("%.2f", hertz);
1287 return AVERROR(ENOMEM);
1289 drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4 - hh, units, 0);
1294 for (x = 0; x < s->w; x+=80) {
1295 float seconds = x * spp / inlink->sample_rate;
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);
1305 units = av_asprintf("%.2fs", seconds);
1307 return AVERROR(ENOMEM);
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);
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);
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;
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;
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)
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)
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) {
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) {
1343 for (x = 0; x < w - 79; x += 80) {
1344 float hertz = x * (inlink->sample_rate / 2) / (float)(1 << (int)ceil(log2(w)));
1348 units = av_asprintf("DC");
1350 units = av_asprintf("%.2f", hertz);
1352 return AVERROR(ENOMEM);
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);
1359 for (y = 0; y < s->h; y+=40) {
1360 float seconds = y * spp / inlink->sample_rate;
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);
1370 units = av_asprintf("%.2fs", seconds);
1372 return AVERROR(ENOMEM);
1374 drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0);
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);
1381 for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) {
1382 int h = multi ? s->h / s->nb_display_channels : s->h;
1384 for (y = 0; y < h; y++) {
1385 float out[3] = { 0., 127.5, 127.5};
1388 for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) {
1390 int channel = (multi) ? s->nb_display_channels - ch - 1 : chn;
1393 color_range(s, channel, &yf, &uf, &vf);
1394 pick_color(s, yf, uf, vf, y / (float)h, lout);
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);
1404 for (y = 0; ch == 0 && y < h; y += h / 10) {
1405 float value = 120.0 * log10(1. - y / (float)h);
1410 text = av_asprintf("%.0f dB", value);
1413 drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 5, text, 0);
1419 ret = ff_filter_frame(outlink, s->outpicref);
1420 s->outpicref = NULL;
1426 static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1428 AVFilterContext *ctx = inlink->dst;
1429 ShowSpectrumContext *s = ctx->priv;
1432 ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
1433 av_frame_free(&insamples);
1437 static const AVFilterPad showspectrumpic_inputs[] = {
1440 .type = AVMEDIA_TYPE_AUDIO,
1441 .filter_frame = showspectrumpic_filter_frame,
1446 static const AVFilterPad showspectrumpic_outputs[] = {
1449 .type = AVMEDIA_TYPE_VIDEO,
1450 .config_props = config_output,
1451 .request_frame = showspectrumpic_request_frame,
1456 AVFilter ff_avf_showspectrumpic = {
1457 .name = "showspectrumpic",
1458 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
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,
1468 #endif // CONFIG_SHOWSPECTRUMPIC_FILTER