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 DisplayScale { LINEAR, SQRT, CBRT, LOG, FOURTHRT, FIFTHRT, NB_SCALES };
49 enum ColorMode { CHANNEL, INTENSITY, RAINBOW, MORELAND, NEBULAE, FIRE, FIERY, FRUIT, COOL, MAGMA, GREEN, NB_CLMODES };
50 enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, NB_SLIDES };
51 enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS };
53 typedef struct ShowSpectrumContext {
57 AVRational auto_frame_rate;
58 AVRational frame_rate;
60 int nb_display_channels;
64 int sliding; ///< 1 if sliding mode, 0 otherwise
65 int mode; ///< channel display mode
66 int color_mode; ///< display color scheme
68 float saturation; ///< color saturation multiplier
69 float rotation; ///< color rotation
70 int start, stop; ///< zoom mode
72 int xpos; ///< x position (current column)
73 FFTContext **fft; ///< Fast Fourier Transform context
74 FFTContext **ifft; ///< Inverse Fast Fourier Transform context
75 int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits)
76 FFTComplex **fft_data; ///< bins holder for each (displayed) channels
77 FFTComplex **fft_scratch; ///< scratch buffers
78 float *window_func_lut; ///< Window function LUT
89 float *combine_buffer; ///< color combining buffer (3 * h items)
90 float **color_buffer; ///< color buffer (3 * h * ch items)
98 } ShowSpectrumContext;
100 #define OFFSET(x) offsetof(ShowSpectrumContext, x)
101 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
103 static const AVOption showspectrum_options[] = {
104 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
105 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
106 { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
107 { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
108 { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
109 { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
110 { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
111 { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
112 { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
113 { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
114 { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
115 { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
116 { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
117 { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
118 { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
119 { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
120 { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
121 { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
122 { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
123 { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
124 { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
125 { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
126 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
127 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
128 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
129 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
130 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
131 { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
132 { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
133 { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
134 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
135 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
136 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
137 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
138 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
139 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
140 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
141 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
142 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
143 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
144 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
145 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
146 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
147 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
148 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
149 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
150 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
151 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
152 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
153 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
154 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
155 { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
156 { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
157 { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
158 { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
159 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS },
160 { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
161 { "data", "set data mode", OFFSET(data), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_DMODES-1, FLAGS, "data" },
162 { "magnitude", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_MAGNITUDE}, 0, 0, FLAGS, "data" },
163 { "phase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_PHASE}, 0, 0, FLAGS, "data" },
164 { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
165 { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
166 { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
167 { "fps", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = "auto"}, 0, 0, FLAGS },
168 { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
172 AVFILTER_DEFINE_CLASS(showspectrum);
174 static const struct ColorTable {
176 } color_table[][8] = {
179 { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
180 { 0.30, .18572281794568020, .1772436246393981, .17475554840414750 },
181 { 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 },
182 { 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 },
183 { 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 },
184 { 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 },
188 { 0.13, 44/256., (189-128)/256., (138-128)/256. },
189 { 0.25, 29/256., (186-128)/256., (119-128)/256. },
190 { 0.38, 119/256., (194-128)/256., (53-128)/256. },
191 { 0.60, 111/256., (73-128)/256., (59-128)/256. },
192 { 0.73, 205/256., (19-128)/256., (149-128)/256. },
193 { 0.86, 135/256., (83-128)/256., (200-128)/256. },
194 { 1, 73/256., (95-128)/256., (225-128)/256. }},
196 { 0, 44/256., (181-128)/256., (112-128)/256. },
197 { 0.13, 126/256., (177-128)/256., (106-128)/256. },
198 { 0.25, 164/256., (163-128)/256., (109-128)/256. },
199 { 0.38, 200/256., (140-128)/256., (120-128)/256. },
200 { 0.60, 201/256., (117-128)/256., (141-128)/256. },
201 { 0.73, 177/256., (103-128)/256., (165-128)/256. },
202 { 0.86, 136/256., (100-128)/256., (183-128)/256. },
203 { 1, 68/256., (117-128)/256., (203-128)/256. }},
205 { 0, 10/256., (134-128)/256., (132-128)/256. },
206 { 0.23, 21/256., (137-128)/256., (130-128)/256. },
207 { 0.45, 35/256., (134-128)/256., (134-128)/256. },
208 { 0.57, 51/256., (130-128)/256., (139-128)/256. },
209 { 0.67, 104/256., (116-128)/256., (162-128)/256. },
210 { 0.77, 120/256., (105-128)/256., (188-128)/256. },
211 { 0.87, 140/256., (105-128)/256., (188-128)/256. },
215 { 0.23, 44/256., (132-128)/256., (127-128)/256. },
216 { 0.45, 62/256., (116-128)/256., (140-128)/256. },
217 { 0.57, 75/256., (105-128)/256., (152-128)/256. },
218 { 0.67, 95/256., (91-128)/256., (166-128)/256. },
219 { 0.77, 126/256., (74-128)/256., (172-128)/256. },
220 { 0.87, 164/256., (73-128)/256., (162-128)/256. },
224 { 0.23, 36/256., (116-128)/256., (163-128)/256. },
225 { 0.45, 52/256., (102-128)/256., (200-128)/256. },
226 { 0.57, 116/256., (84-128)/256., (196-128)/256. },
227 { 0.67, 157/256., (67-128)/256., (181-128)/256. },
228 { 0.77, 193/256., (40-128)/256., (155-128)/256. },
229 { 0.87, 221/256., (101-128)/256., (134-128)/256. },
233 { 0.20, 29/256., (136-128)/256., (119-128)/256. },
234 { 0.30, 60/256., (119-128)/256., (90-128)/256. },
235 { 0.40, 85/256., (91-128)/256., (85-128)/256. },
236 { 0.50, 116/256., (70-128)/256., (105-128)/256. },
237 { 0.60, 151/256., (50-128)/256., (146-128)/256. },
238 { 0.70, 191/256., (63-128)/256., (178-128)/256. },
239 { 1, 98/256., (80-128)/256., (221-128)/256. }},
246 { 0.10, 23/256., (175-128)/256., (120-128)/256. },
247 { 0.23, 43/256., (158-128)/256., (144-128)/256. },
248 { 0.35, 85/256., (138-128)/256., (179-128)/256. },
249 { 0.48, 96/256., (128-128)/256., (189-128)/256. },
250 { 0.64, 128/256., (103-128)/256., (214-128)/256. },
251 { 0.78, 167/256., (85-128)/256., (174-128)/256. },
252 { 1, 205/256., (80-128)/256., (152-128)/256. }},
259 static av_cold void uninit(AVFilterContext *ctx)
261 ShowSpectrumContext *s = ctx->priv;
264 av_freep(&s->combine_buffer);
266 for (i = 0; i < s->nb_display_channels; i++)
267 av_fft_end(s->fft[i]);
271 for (i = 0; i < s->nb_display_channels; i++)
272 av_fft_end(s->ifft[i]);
276 for (i = 0; i < s->nb_display_channels; i++)
277 av_freep(&s->fft_data[i]);
279 av_freep(&s->fft_data);
280 if (s->fft_scratch) {
281 for (i = 0; i < s->nb_display_channels; i++)
282 av_freep(&s->fft_scratch[i]);
284 av_freep(&s->fft_scratch);
285 if (s->color_buffer) {
286 for (i = 0; i < s->nb_display_channels; i++)
287 av_freep(&s->color_buffer[i]);
289 av_freep(&s->color_buffer);
290 av_freep(&s->window_func_lut);
292 for (i = 0; i < s->nb_display_channels; i++)
293 av_freep(&s->magnitudes[i]);
295 av_freep(&s->magnitudes);
296 av_frame_free(&s->outpicref);
297 av_audio_fifo_free(s->fifo);
299 for (i = 0; i < s->nb_display_channels; i++)
300 av_freep(&s->phases[i]);
302 av_freep(&s->phases);
305 static int query_formats(AVFilterContext *ctx)
307 AVFilterFormats *formats = NULL;
308 AVFilterChannelLayouts *layouts = NULL;
309 AVFilterLink *inlink = ctx->inputs[0];
310 AVFilterLink *outlink = ctx->outputs[0];
311 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
312 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE };
315 /* set input audio formats */
316 formats = ff_make_format_list(sample_fmts);
317 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
320 layouts = ff_all_channel_layouts();
321 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
324 formats = ff_all_samplerates();
325 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
328 /* set output video format */
329 formats = ff_make_format_list(pix_fmts);
330 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
336 static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
338 ShowSpectrumContext *s = ctx->priv;
339 AVFilterLink *inlink = ctx->inputs[0];
340 const float *window_func_lut = s->window_func_lut;
342 const int ch = jobnr;
345 /* fill FFT input with the number of samples available */
346 const float *p = (float *)fin->extended_data[ch];
348 for (n = 0; n < s->win_size; n++) {
349 s->fft_data[ch][n].re = p[n] * window_func_lut[n];
350 s->fft_data[ch][n].im = 0;
354 double theta, phi, psi, a, b, S, c;
355 FFTComplex *g = s->fft_data[ch];
356 FFTComplex *h = s->fft_scratch[ch];
359 int M = s->win_size / 2;
361 phi = 2.0 * M_PI * (s->stop - s->start) / (double)inlink->sample_rate / (M - 1);
362 theta = 2.0 * M_PI * s->start / (double)inlink->sample_rate;
364 for (int n = 0; n < M; n++) {
365 h[n].re = cos(n * n / 2.0 * phi);
366 h[n].im = sin(n * n / 2.0 * phi);
369 for (int n = M; n < L; n++) {
374 for (int n = L - N; n < L; n++) {
375 h[n].re = cos((L - n) * (L - n) / 2.0 * phi);
376 h[n].im = sin((L - n) * (L - n) / 2.0 * phi);
379 for (int n = 0; n < N; n++) {
380 g[n].re = s->fft_data[ch][n].re;
381 g[n].im = s->fft_data[ch][n].im;
384 for (int n = N; n < L; n++) {
389 for (int n = 0; n < N; n++) {
390 psi = n * theta + n * n / 2.0 * phi;
393 a = c * g[n].re - S * g[n].im;
394 b = S * g[n].re + c * g[n].im;
399 av_fft_permute(s->fft[ch], h);
400 av_fft_calc(s->fft[ch], h);
402 av_fft_permute(s->fft[ch], g);
403 av_fft_calc(s->fft[ch], g);
405 for (int n = 0; n < L; n++) {
408 a = c * h[n].re - S * h[n].im;
409 b = S * h[n].re + c * h[n].im;
415 av_fft_permute(s->ifft[ch], g);
416 av_fft_calc(s->ifft[ch], g);
418 for (int k = 0; k < M; k++) {
419 psi = k * k / 2.0 * phi;
422 a = c * g[k].re - S * g[k].im;
423 b = S * g[k].re + c * g[k].im;
424 s->fft_data[ch][k].re = a;
425 s->fft_data[ch][k].im = b;
428 /* run FFT on each samples set */
429 av_fft_permute(s->fft[ch], s->fft_data[ch]);
430 av_fft_calc(s->fft[ch], s->fft_data[ch]);
436 static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
442 font = avpriv_cga_font, font_height = 8;
444 for (i = 0; txt[i]; i++) {
448 for (char_y = font_height - 1; char_y >= 0; char_y--) {
449 uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x;
450 for (mask = 0x80; mask; mask >>= 1) {
451 if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
452 p[char_y] = ~p[char_y];
453 p += pic->linesize[0];
457 uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8);
458 for (char_y = 0; char_y < font_height; char_y++) {
459 for (mask = 0x80; mask; mask >>= 1) {
460 if (font[txt[i] * font_height + char_y] & mask)
464 p += pic->linesize[0] - 8;
470 static void color_range(ShowSpectrumContext *s, int ch,
471 float *yf, float *uf, float *vf)
475 // reduce range by channel count
476 *yf = 256.0f / s->nb_display_channels;
477 switch (s->color_mode) {
492 /* adjust saturation for mixed UV coloring */
493 /* this factor is correct for infinite channels, an approximation otherwise */
511 if (s->color_mode == CHANNEL) {
512 if (s->nb_display_channels > 1) {
513 *uf *= 0.5 * sin((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
514 *vf *= 0.5 * cos((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
516 *uf *= 0.5 * sin(M_PI * s->rotation);
517 *vf *= 0.5 * cos(M_PI * s->rotation + M_PI_2);
520 *uf += *uf * sin(M_PI * s->rotation);
521 *vf += *vf * cos(M_PI * s->rotation + M_PI_2);
524 *uf *= s->saturation;
525 *vf *= s->saturation;
528 static void pick_color(ShowSpectrumContext *s,
529 float yf, float uf, float vf,
532 if (s->color_mode > CHANNEL) {
533 const int cm = s->color_mode;
537 for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
538 if (color_table[cm][i].a >= a)
540 // i now is the first item >= the color
541 // now we know to interpolate between item i - 1 and i
542 if (a <= color_table[cm][i - 1].a) {
543 y = color_table[cm][i - 1].y;
544 u = color_table[cm][i - 1].u;
545 v = color_table[cm][i - 1].v;
546 } else if (a >= color_table[cm][i].a) {
547 y = color_table[cm][i].y;
548 u = color_table[cm][i].u;
549 v = color_table[cm][i].v;
551 float start = color_table[cm][i - 1].a;
552 float end = color_table[cm][i].a;
553 float lerpfrac = (a - start) / (end - start);
554 y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
555 + color_table[cm][i].y * lerpfrac;
556 u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
557 + color_table[cm][i].u * lerpfrac;
558 v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
559 + color_table[cm][i].v * lerpfrac;
572 static char *get_time(AVFilterContext *ctx, float seconds, int x)
577 units = av_asprintf("0");
578 else if (log10(seconds) > 6)
579 units = av_asprintf("%.2fh", seconds / (60 * 60));
580 else if (log10(seconds) > 3)
581 units = av_asprintf("%.2fm", seconds / 60);
583 units = av_asprintf("%.2fs", seconds);
587 static int draw_legend(AVFilterContext *ctx, int samples)
589 ShowSpectrumContext *s = ctx->priv;
590 AVFilterLink *inlink = ctx->inputs[0];
591 AVFilterLink *outlink = ctx->outputs[0];
592 int ch, y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
593 int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL);
594 float spp = samples / (float)sz;
597 char chlayout_str[128];
599 av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), inlink->channels,
600 inlink->channel_layout);
602 text = av_asprintf("%d Hz | %s", inlink->sample_rate, chlayout_str);
604 drawtext(s->outpicref, 2, outlink->h - 10, "CREATED BY LIBAVFILTER", 0);
605 drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, outlink->h - 10, text, 0);
607 char *text = av_asprintf("Zoom: %d Hz - %d Hz", s->start, s->stop);
608 drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, 3, text, 0);
614 dst = s->outpicref->data[0] + (s->start_y - 1) * s->outpicref->linesize[0] + s->start_x - 1;
615 for (x = 0; x < s->w + 1; x++)
617 dst = s->outpicref->data[0] + (s->start_y + s->h) * s->outpicref->linesize[0] + s->start_x - 1;
618 for (x = 0; x < s->w + 1; x++)
620 for (y = 0; y < s->h + 2; y++) {
621 dst = s->outpicref->data[0] + (y + s->start_y - 1) * s->outpicref->linesize[0];
622 dst[s->start_x - 1] = 200;
623 dst[s->start_x + s->w] = 200;
625 if (s->orientation == VERTICAL) {
626 int h = s->mode == SEPARATE ? s->h / s->nb_display_channels : s->h;
627 int hh = s->mode == SEPARATE ? -(s->h % s->nb_display_channels) + 1 : 1;
628 for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
629 for (y = 0; y < h; y += 20) {
630 dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
631 dst[s->start_x - 2] = 200;
632 dst[s->start_x + s->w + 1] = 200;
634 for (y = 0; y < h; y += 40) {
635 dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
636 dst[s->start_x - 3] = 200;
637 dst[s->start_x + s->w + 2] = 200;
639 dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x;
640 for (x = 0; x < s->w; x+=40)
642 dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x;
643 for (x = 0; x < s->w; x+=80)
645 dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x;
646 for (x = 0; x < s->w; x+=40) {
649 dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x;
650 for (x = 0; x < s->w; x+=80) {
653 for (y = 0; y < h; y += 40) {
654 float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
655 float hertz = s->start + y * range / (float)(1 << (int)ceil(log2(h)));
659 units = av_asprintf("DC");
661 units = av_asprintf("%.2f", hertz);
663 return AVERROR(ENOMEM);
665 drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4 - hh, units, 0);
670 for (x = 0; x < s->w && s->single_pic; x+=80) {
671 float seconds = x * spp / inlink->sample_rate;
672 char *units = get_time(ctx, seconds, x);
674 drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->h + s->start_y + 6, units, 0);
675 drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->start_y - 12, units, 0);
679 drawtext(s->outpicref, outlink->w / 2 - 4 * 4, outlink->h - s->start_y / 2, "TIME", 0);
680 drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 14 * 4, "FREQUENCY (Hz)", 1);
682 int w = s->mode == SEPARATE ? s->w / s->nb_display_channels : s->w;
683 for (y = 0; y < s->h; y += 20) {
684 dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
685 dst[s->start_x - 2] = 200;
686 dst[s->start_x + s->w + 1] = 200;
688 for (y = 0; y < s->h; y += 40) {
689 dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
690 dst[s->start_x - 3] = 200;
691 dst[s->start_x + s->w + 2] = 200;
693 for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
694 dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
695 for (x = 0; x < w; x+=40)
697 dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x + w * ch;
698 for (x = 0; x < w; x+=80)
700 dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x + w * ch;
701 for (x = 0; x < w; x+=40) {
704 dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
705 for (x = 0; x < w; x+=80) {
708 for (x = 0; x < w - 79; x += 80) {
709 float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
710 float hertz = s->start + x * range / (float)(1 << (int)ceil(log2(w)));
714 units = av_asprintf("DC");
716 units = av_asprintf("%.2f", hertz);
718 return AVERROR(ENOMEM);
720 drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->start_y - 12, units, 0);
721 drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->h + s->start_y + 6, units, 0);
725 for (y = 0; y < s->h && s->single_pic; y+=40) {
726 float seconds = y * spp / inlink->sample_rate;
727 char *units = get_time(ctx, seconds, x);
729 drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0);
732 drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 4 * 4, "TIME", 1);
733 drawtext(s->outpicref, outlink->w / 2 - 14 * 4, outlink->h - s->start_y / 2, "FREQUENCY (Hz)", 0);
736 for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) {
737 int h = multi ? s->h / s->nb_display_channels : s->h;
739 for (y = 0; y < h; y++) {
740 float out[3] = { 0., 127.5, 127.5};
743 for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) {
745 int channel = (multi) ? s->nb_display_channels - ch - 1 : chn;
748 color_range(s, channel, &yf, &uf, &vf);
749 pick_color(s, yf, uf, vf, y / (float)h, lout);
754 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);
755 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);
756 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);
759 for (y = 0; ch == 0 && y < h; y += h / 10) {
760 float value = 120.0 * log10(1. - y / (float)h);
765 text = av_asprintf("%.0f dB", value);
768 drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 5, text, 0);
776 static int config_output(AVFilterLink *outlink)
778 AVFilterContext *ctx = outlink->src;
779 AVFilterLink *inlink = ctx->inputs[0];
780 ShowSpectrumContext *s = ctx->priv;
781 int i, fft_bits, h, w;
784 s->stop = FFMIN(s->stop, inlink->sample_rate / 2);
785 if (s->stop && s->stop <= s->start) {
786 av_log(ctx, AV_LOG_ERROR, "Stop frequency should be greater than start.\n");
787 return AVERROR(EINVAL);
790 if (!strcmp(ctx->filter->name, "showspectrumpic"))
795 outlink->sample_aspect_ratio = (AVRational){1,1};
798 s->start_x = (log10(inlink->sample_rate) + 1) * 25;
800 outlink->w += s->start_x * 2;
801 outlink->h += s->start_y * 2;
804 h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? s->h : s->h / inlink->channels;
805 w = (s->mode == COMBINED || s->orientation == VERTICAL) ? s->w : s->w / inlink->channels;
806 s->channel_height = h;
807 s->channel_width = w;
809 if (s->orientation == VERTICAL) {
810 /* FFT window size (precision) according to the requested output frame height */
811 for (fft_bits = 1; 1 << fft_bits < 2 * h; fft_bits++);
813 /* FFT window size (precision) according to the requested output frame width */
814 for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++);
817 s->win_size = 1 << fft_bits;
818 s->buf_size = s->win_size << !!s->stop;
821 s->fft = av_calloc(inlink->channels, sizeof(*s->fft));
823 return AVERROR(ENOMEM);
828 s->ifft = av_calloc(inlink->channels, sizeof(*s->ifft));
830 return AVERROR(ENOMEM);
834 /* (re-)configuration if the video output changed (or first init) */
835 if (fft_bits != s->fft_bits) {
838 s->fft_bits = fft_bits;
840 /* FFT buffers: x2 for each (display) channel buffer.
841 * Note: we use free and malloc instead of a realloc-like function to
842 * make sure the buffer is aligned in memory for the FFT functions. */
843 for (i = 0; i < s->nb_display_channels; i++) {
845 av_fft_end(s->ifft[i]);
846 av_freep(&s->fft_scratch[i]);
848 av_fft_end(s->fft[i]);
849 av_freep(&s->fft_data[i]);
851 av_freep(&s->fft_data);
853 s->nb_display_channels = inlink->channels;
854 for (i = 0; i < s->nb_display_channels; i++) {
855 s->fft[i] = av_fft_init(fft_bits + !!s->stop, 0);
857 s->ifft[i] = av_fft_init(fft_bits + !!s->stop, 1);
859 av_log(ctx, AV_LOG_ERROR, "Unable to create Inverse FFT context. "
860 "The window size might be too high.\n");
861 return AVERROR(EINVAL);
865 av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
866 "The window size might be too high.\n");
867 return AVERROR(EINVAL);
871 s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
873 return AVERROR(ENOMEM);
874 for (i = 0; i < s->nb_display_channels; i++) {
875 s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
876 if (!s->magnitudes[i])
877 return AVERROR(ENOMEM);
880 s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases));
882 return AVERROR(ENOMEM);
883 for (i = 0; i < s->nb_display_channels; i++) {
884 s->phases[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->phases));
886 return AVERROR(ENOMEM);
889 av_freep(&s->color_buffer);
890 s->color_buffer = av_calloc(s->nb_display_channels, sizeof(*s->color_buffer));
891 if (!s->color_buffer)
892 return AVERROR(ENOMEM);
893 for (i = 0; i < s->nb_display_channels; i++) {
894 s->color_buffer[i] = av_calloc(s->orientation == VERTICAL ? s->h * 3 : s->w * 3, sizeof(**s->color_buffer));
895 if (!s->color_buffer[i])
896 return AVERROR(ENOMEM);
899 s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
901 return AVERROR(ENOMEM);
902 s->fft_scratch = av_calloc(s->nb_display_channels, sizeof(*s->fft_scratch));
904 return AVERROR(ENOMEM);
905 for (i = 0; i < s->nb_display_channels; i++) {
906 s->fft_data[i] = av_calloc(s->buf_size, sizeof(**s->fft_data));
908 return AVERROR(ENOMEM);
910 s->fft_scratch[i] = av_calloc(s->buf_size, sizeof(**s->fft_scratch));
911 if (!s->fft_scratch[i])
912 return AVERROR(ENOMEM);
915 /* pre-calc windowing function */
917 av_realloc_f(s->window_func_lut, s->win_size,
918 sizeof(*s->window_func_lut));
919 if (!s->window_func_lut)
920 return AVERROR(ENOMEM);
921 generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
923 s->overlap = overlap;
924 s->hop_size = (1. - s->overlap) * s->win_size;
925 if (s->hop_size < 1) {
926 av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
927 return AVERROR(EINVAL);
930 for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
931 s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
933 s->win_scale = 1. / sqrt(s->win_scale);
935 /* prepare the initial picref buffer (black frame) */
936 av_frame_free(&s->outpicref);
937 s->outpicref = outpicref =
938 ff_get_video_buffer(outlink, outlink->w, outlink->h);
940 return AVERROR(ENOMEM);
941 outpicref->sample_aspect_ratio = (AVRational){1,1};
942 for (i = 0; i < outlink->h; i++) {
943 memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
944 memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
945 memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
947 outpicref->color_range = AVCOL_RANGE_JPEG;
949 if (!s->single_pic && s->legend)
953 if ((s->orientation == VERTICAL && s->xpos >= s->w) ||
954 (s->orientation == HORIZONTAL && s->xpos >= s->h))
957 s->auto_frame_rate = av_make_q(inlink->sample_rate, s->hop_size);
958 if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
959 s->auto_frame_rate.den *= s->w;
960 if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
961 s->auto_frame_rate.den *= s->h;
962 if (!s->single_pic && strcmp(s->rate_str, "auto")) {
963 int ret = av_parse_video_rate(&s->frame_rate, s->rate_str);
967 s->frame_rate = s->auto_frame_rate;
969 outlink->frame_rate = s->frame_rate;
970 outlink->time_base = av_inv_q(outlink->frame_rate);
972 if (s->orientation == VERTICAL) {
974 av_realloc_f(s->combine_buffer, s->h * 3,
975 sizeof(*s->combine_buffer));
978 av_realloc_f(s->combine_buffer, s->w * 3,
979 sizeof(*s->combine_buffer));
982 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n",
983 s->w, s->h, s->win_size);
985 av_audio_fifo_free(s->fifo);
986 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
988 return AVERROR(ENOMEM);
992 #define RE(y, ch) s->fft_data[ch][y].re
993 #define IM(y, ch) s->fft_data[ch][y].im
994 #define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch))
995 #define PHASE(y, ch) atan2(IM(y, ch), RE(y, ch))
997 static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
999 ShowSpectrumContext *s = ctx->priv;
1000 const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
1001 int y, h = s->orientation == VERTICAL ? s->h : s->w;
1002 const float f = s->gain * w;
1003 const int ch = jobnr;
1004 float *magnitudes = s->magnitudes[ch];
1006 for (y = 0; y < h; y++)
1007 magnitudes[y] = MAGNITUDE(y, ch) * f;
1012 static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1014 ShowSpectrumContext *s = ctx->priv;
1015 const int h = s->orientation == VERTICAL ? s->h : s->w;
1016 const int ch = jobnr;
1017 float *phases = s->phases[ch];
1020 for (y = 0; y < h; y++)
1021 phases[y] = (PHASE(y, ch) / M_PI + 1) / 2;
1026 static void acalc_magnitudes(ShowSpectrumContext *s)
1028 const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
1029 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
1030 const float f = s->gain * w;
1032 for (ch = 0; ch < s->nb_display_channels; ch++) {
1033 float *magnitudes = s->magnitudes[ch];
1035 for (y = 0; y < h; y++)
1036 magnitudes[y] += MAGNITUDE(y, ch) * f;
1040 static void scale_magnitudes(ShowSpectrumContext *s, float scale)
1042 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
1044 for (ch = 0; ch < s->nb_display_channels; ch++) {
1045 float *magnitudes = s->magnitudes[ch];
1047 for (y = 0; y < h; y++)
1048 magnitudes[y] *= scale;
1052 static void clear_combine_buffer(ShowSpectrumContext *s, int size)
1056 for (y = 0; y < size; y++) {
1057 s->combine_buffer[3 * y ] = 0;
1058 s->combine_buffer[3 * y + 1] = 127.5;
1059 s->combine_buffer[3 * y + 2] = 127.5;
1063 static int plot_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1065 ShowSpectrumContext *s = ctx->priv;
1066 const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
1067 const int ch = jobnr;
1068 float *magnitudes = s->magnitudes[ch];
1069 float *phases = s->phases[ch];
1073 /* decide color range */
1074 color_range(s, ch, &yf, &uf, &vf);
1076 /* draw the channel */
1077 for (y = 0; y < h; y++) {
1078 int row = (s->mode == COMBINED) ? y : ch * h + y;
1079 float *out = &s->color_buffer[ch][3 * row];
1098 a = av_clipf(a, 0, 1);
1101 a = av_clipf(sqrt(a), 0, 1);
1104 a = av_clipf(cbrt(a), 0, 1);
1107 a = av_clipf(sqrt(sqrt(a)), 0, 1);
1110 a = av_clipf(pow(a, 0.20), 0, 1);
1113 a = 1 + log10(av_clipd(a, 1e-6, 1)) / 6; // zero = -120dBFS
1119 pick_color(s, yf, uf, vf, a, out);
1125 static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
1127 AVFilterContext *ctx = inlink->dst;
1128 AVFilterLink *outlink = ctx->outputs[0];
1129 ShowSpectrumContext *s = ctx->priv;
1130 AVFrame *outpicref = s->outpicref;
1131 int ret, plane, x, y, z = s->orientation == VERTICAL ? s->h : s->w;
1133 /* fill a new spectrum column */
1134 /* initialize buffer for combining to black */
1135 clear_combine_buffer(s, z);
1137 ctx->internal->execute(ctx, plot_channel, NULL, NULL, s->nb_display_channels);
1139 for (y = 0; y < z * 3; y++) {
1140 for (x = 0; x < s->nb_display_channels; x++) {
1141 s->combine_buffer[y] += s->color_buffer[x][y];
1145 av_frame_make_writable(s->outpicref);
1146 /* copy to output */
1147 if (s->orientation == VERTICAL) {
1148 if (s->sliding == SCROLL) {
1149 for (plane = 0; plane < 3; plane++) {
1150 for (y = 0; y < s->h; y++) {
1151 uint8_t *p = outpicref->data[plane] + s->start_x +
1152 (y + s->start_y) * outpicref->linesize[plane];
1153 memmove(p, p + 1, s->w - 1);
1157 } else if (s->sliding == RSCROLL) {
1158 for (plane = 0; plane < 3; plane++) {
1159 for (y = 0; y < s->h; y++) {
1160 uint8_t *p = outpicref->data[plane] + s->start_x +
1161 (y + s->start_y) * outpicref->linesize[plane];
1162 memmove(p + 1, p, s->w - 1);
1167 for (plane = 0; plane < 3; plane++) {
1168 uint8_t *p = outpicref->data[plane] + s->start_x +
1169 (outlink->h - 1 - s->start_y) * outpicref->linesize[plane] +
1171 for (y = 0; y < s->h; y++) {
1172 *p = lrintf(av_clipf(s->combine_buffer[3 * y + plane], 0, 255));
1173 p -= outpicref->linesize[plane];
1177 if (s->sliding == SCROLL) {
1178 for (plane = 0; plane < 3; plane++) {
1179 for (y = 1; y < s->h; y++) {
1180 memmove(outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
1181 outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
1186 } else if (s->sliding == RSCROLL) {
1187 for (plane = 0; plane < 3; plane++) {
1188 for (y = s->h - 1; y >= 1; y--) {
1189 memmove(outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
1190 outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
1196 for (plane = 0; plane < 3; plane++) {
1197 uint8_t *p = outpicref->data[plane] + s->start_x +
1198 (s->xpos + s->start_y) * outpicref->linesize[plane];
1199 for (x = 0; x < s->w; x++) {
1200 *p = lrintf(av_clipf(s->combine_buffer[3 * x + plane], 0, 255));
1206 if (s->sliding != FULLFRAME || s->xpos == 0)
1207 outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
1210 if (s->orientation == VERTICAL && s->xpos >= s->w)
1212 if (s->orientation == HORIZONTAL && s->xpos >= s->h)
1214 if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
1215 if (s->old_pts < outpicref->pts) {
1217 char *units = get_time(ctx, insamples->pts /(float)inlink->sample_rate, x);
1219 if (s->orientation == VERTICAL) {
1220 for (y = 0; y < 10; y++) {
1221 memset(s->outpicref->data[0] + outlink->w / 2 - 4 * s->old_len +
1222 (outlink->h - s->start_y / 2 - 20 + y) * s->outpicref->linesize[0], 0, 10 * s->old_len);
1224 drawtext(s->outpicref,
1225 outlink->w / 2 - 4 * strlen(units),
1226 outlink->h - s->start_y / 2 - 20,
1229 for (y = 0; y < 10 * s->old_len; y++) {
1230 memset(s->outpicref->data[0] + s->start_x / 7 + 20 +
1231 (outlink->h / 2 - 4 * s->old_len + y) * s->outpicref->linesize[0], 0, 10);
1233 drawtext(s->outpicref,
1234 s->start_x / 7 + 20,
1235 outlink->h / 2 - 4 * strlen(units),
1238 s->old_len = strlen(units);
1241 s->old_pts = outpicref->pts;
1242 ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
1252 #if CONFIG_SHOWSPECTRUM_FILTER
1254 static int activate(AVFilterContext *ctx)
1256 AVFilterLink *inlink = ctx->inputs[0];
1257 AVFilterLink *outlink = ctx->outputs[0];
1258 ShowSpectrumContext *s = ctx->priv;
1261 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
1263 if (av_audio_fifo_size(s->fifo) < s->win_size) {
1264 AVFrame *frame = NULL;
1266 ret = ff_inlink_consume_frame(inlink, &frame);
1270 s->pts = frame->pts;
1273 av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
1274 av_frame_free(&frame);
1278 if (s->outpicref && av_audio_fifo_size(s->fifo) >= s->win_size) {
1279 AVFrame *fin = ff_get_audio_buffer(inlink, s->win_size);
1281 return AVERROR(ENOMEM);
1283 fin->pts = s->pts + s->consumed;
1284 s->consumed += s->hop_size;
1285 ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data,
1286 FFMIN(s->win_size, av_audio_fifo_size(s->fifo)));
1288 av_frame_free(&fin);
1292 av_assert0(fin->nb_samples == s->win_size);
1294 ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
1296 if (s->data == D_MAGNITUDE)
1297 ctx->internal->execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels);
1299 if (s->data == D_PHASE)
1300 ctx->internal->execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels);
1302 ret = plot_spectrum_column(inlink, fin);
1304 av_frame_free(&fin);
1305 av_audio_fifo_drain(s->fifo, s->hop_size);
1310 if (ff_outlink_get_status(inlink) == AVERROR_EOF &&
1311 s->sliding == FULLFRAME &&
1312 s->xpos > 0 && s->outpicref) {
1315 if (s->orientation == VERTICAL) {
1316 for (int i = 0; i < outlink->h; i++) {
1317 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos);
1318 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
1319 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
1322 for (int i = s->xpos; i < outlink->h; i++) {
1323 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w);
1324 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
1325 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
1328 s->outpicref->pts += s->consumed;
1329 pts = s->outpicref->pts;
1330 ret = ff_filter_frame(outlink, s->outpicref);
1331 s->outpicref = NULL;
1332 ff_outlink_set_status(outlink, AVERROR_EOF, pts);
1336 FF_FILTER_FORWARD_STATUS(inlink, outlink);
1337 if (ff_outlink_frame_wanted(outlink) && av_audio_fifo_size(s->fifo) < s->win_size) {
1338 ff_inlink_request_frame(inlink);
1342 if (av_audio_fifo_size(s->fifo) >= s->win_size) {
1343 ff_filter_set_ready(ctx, 10);
1346 return FFERROR_NOT_READY;
1349 static const AVFilterPad showspectrum_inputs[] = {
1352 .type = AVMEDIA_TYPE_AUDIO,
1357 static const AVFilterPad showspectrum_outputs[] = {
1360 .type = AVMEDIA_TYPE_VIDEO,
1361 .config_props = config_output,
1366 AVFilter ff_avf_showspectrum = {
1367 .name = "showspectrum",
1368 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
1370 .query_formats = query_formats,
1371 .priv_size = sizeof(ShowSpectrumContext),
1372 .inputs = showspectrum_inputs,
1373 .outputs = showspectrum_outputs,
1374 .activate = activate,
1375 .priv_class = &showspectrum_class,
1376 .flags = AVFILTER_FLAG_SLICE_THREADS,
1378 #endif // CONFIG_SHOWSPECTRUM_FILTER
1380 #if CONFIG_SHOWSPECTRUMPIC_FILTER
1382 static const AVOption showspectrumpic_options[] = {
1383 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
1384 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
1385 { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" },
1386 { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
1387 { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
1388 { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" },
1389 { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
1390 { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
1391 { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
1392 { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
1393 { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
1394 { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
1395 { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
1396 { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
1397 { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
1398 { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
1399 { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
1400 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" },
1401 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
1402 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
1403 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
1404 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
1405 { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
1406 { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
1407 { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
1408 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
1409 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
1410 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
1411 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1412 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1413 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
1414 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
1415 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
1416 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
1417 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
1418 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
1419 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
1420 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
1421 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
1422 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
1423 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
1424 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
1425 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
1426 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
1427 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
1428 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
1429 { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
1430 { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
1431 { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
1432 { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
1433 { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
1434 { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
1435 { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
1436 { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
1437 { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
1441 AVFILTER_DEFINE_CLASS(showspectrumpic);
1443 static int showspectrumpic_request_frame(AVFilterLink *outlink)
1445 AVFilterContext *ctx = outlink->src;
1446 ShowSpectrumContext *s = ctx->priv;
1447 AVFilterLink *inlink = ctx->inputs[0];
1450 ret = ff_request_frame(inlink);
1451 samples = av_audio_fifo_size(s->fifo);
1452 if (ret == AVERROR_EOF && s->outpicref && samples > 0) {
1454 int x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
1458 spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz))));
1459 spf = FFMAX(1, spf);
1461 spb = (samples / (spf * sz)) * spf;
1463 fin = ff_get_audio_buffer(inlink, s->win_size);
1465 return AVERROR(ENOMEM);
1468 ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
1470 av_frame_free(&fin);
1474 av_audio_fifo_drain(s->fifo, spf);
1476 if (ret < s->win_size) {
1477 for (ch = 0; ch < s->nb_display_channels; ch++) {
1478 memset(fin->extended_data[ch] + ret * sizeof(float), 0,
1479 (s->win_size - ret) * sizeof(float));
1483 ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
1484 acalc_magnitudes(s);
1487 if (consumed >= spb) {
1488 int h = s->orientation == VERTICAL ? s->h : s->w;
1490 scale_magnitudes(s, 1. / (consumed / spf));
1491 plot_spectrum_column(inlink, fin);
1494 for (ch = 0; ch < s->nb_display_channels; ch++)
1495 memset(s->magnitudes[ch], 0, h * sizeof(float));
1499 av_frame_free(&fin);
1500 s->outpicref->pts = 0;
1503 draw_legend(ctx, samples);
1505 ret = ff_filter_frame(outlink, s->outpicref);
1506 s->outpicref = NULL;
1512 static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1514 AVFilterContext *ctx = inlink->dst;
1515 ShowSpectrumContext *s = ctx->priv;
1518 ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
1519 av_frame_free(&insamples);
1523 static const AVFilterPad showspectrumpic_inputs[] = {
1526 .type = AVMEDIA_TYPE_AUDIO,
1527 .filter_frame = showspectrumpic_filter_frame,
1532 static const AVFilterPad showspectrumpic_outputs[] = {
1535 .type = AVMEDIA_TYPE_VIDEO,
1536 .config_props = config_output,
1537 .request_frame = showspectrumpic_request_frame,
1542 AVFilter ff_avf_showspectrumpic = {
1543 .name = "showspectrumpic",
1544 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
1546 .query_formats = query_formats,
1547 .priv_size = sizeof(ShowSpectrumContext),
1548 .inputs = showspectrumpic_inputs,
1549 .outputs = showspectrumpic_outputs,
1550 .priv_class = &showspectrumpic_class,
1551 .flags = AVFILTER_FLAG_SLICE_THREADS,
1554 #endif // CONFIG_SHOWSPECTRUMPIC_FILTER