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
66 int xpos; ///< x position (current column)
67 FFTContext **fft; ///< Fast Fourier Transform context
68 int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits)
69 FFTComplex **fft_data; ///< bins holder for each (displayed) channels
70 float *window_func_lut; ///< Window function LUT
79 float *combine_buffer; ///< color combining buffer (3 * h items)
80 float **color_buffer; ///< color buffer (3 * h * ch items)
86 } ShowSpectrumContext;
88 #define OFFSET(x) offsetof(ShowSpectrumContext, x)
89 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
91 static const AVOption showspectrum_options[] = {
92 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
93 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
94 { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
95 { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
96 { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
97 { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
98 { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
99 { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
100 { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
101 { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
102 { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
103 { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
104 { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
105 { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
106 { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
107 { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
108 { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
109 { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
110 { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
111 { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
112 { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
113 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
114 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
115 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
116 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
117 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
118 { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
119 { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
120 { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
121 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
122 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
123 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
124 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
125 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
126 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
127 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
128 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
129 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
130 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
131 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
132 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
133 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
134 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
135 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
136 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
137 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
138 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
139 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
140 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
141 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
142 { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
143 { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
144 { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
145 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS },
146 { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
147 { "data", "set data mode", OFFSET(data), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_DMODES-1, FLAGS, "data" },
148 { "magnitude", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_MAGNITUDE}, 0, 0, FLAGS, "data" },
149 { "phase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_PHASE}, 0, 0, FLAGS, "data" },
150 { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
154 AVFILTER_DEFINE_CLASS(showspectrum);
156 static const struct ColorTable {
158 } color_table[][8] = {
161 { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
162 { 0.30, .18572281794568020, .1772436246393981, .17475554840414750 },
163 { 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 },
164 { 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 },
165 { 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 },
166 { 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 },
170 { 0.13, 44/256., (189-128)/256., (138-128)/256. },
171 { 0.25, 29/256., (186-128)/256., (119-128)/256. },
172 { 0.38, 119/256., (194-128)/256., (53-128)/256. },
173 { 0.60, 111/256., (73-128)/256., (59-128)/256. },
174 { 0.73, 205/256., (19-128)/256., (149-128)/256. },
175 { 0.86, 135/256., (83-128)/256., (200-128)/256. },
176 { 1, 73/256., (95-128)/256., (225-128)/256. }},
178 { 0, 44/256., (181-128)/256., (112-128)/256. },
179 { 0.13, 126/256., (177-128)/256., (106-128)/256. },
180 { 0.25, 164/256., (163-128)/256., (109-128)/256. },
181 { 0.38, 200/256., (140-128)/256., (120-128)/256. },
182 { 0.60, 201/256., (117-128)/256., (141-128)/256. },
183 { 0.73, 177/256., (103-128)/256., (165-128)/256. },
184 { 0.86, 136/256., (100-128)/256., (183-128)/256. },
185 { 1, 68/256., (117-128)/256., (203-128)/256. }},
187 { 0, 10/256., (134-128)/256., (132-128)/256. },
188 { 0.23, 21/256., (137-128)/256., (130-128)/256. },
189 { 0.45, 35/256., (134-128)/256., (134-128)/256. },
190 { 0.57, 51/256., (130-128)/256., (139-128)/256. },
191 { 0.67, 104/256., (116-128)/256., (162-128)/256. },
192 { 0.77, 120/256., (105-128)/256., (188-128)/256. },
193 { 0.87, 140/256., (105-128)/256., (188-128)/256. },
197 { 0.23, 44/256., (132-128)/256., (127-128)/256. },
198 { 0.45, 62/256., (116-128)/256., (140-128)/256. },
199 { 0.57, 75/256., (105-128)/256., (152-128)/256. },
200 { 0.67, 95/256., (91-128)/256., (166-128)/256. },
201 { 0.77, 126/256., (74-128)/256., (172-128)/256. },
202 { 0.87, 164/256., (73-128)/256., (162-128)/256. },
206 { 0.23, 36/256., (116-128)/256., (163-128)/256. },
207 { 0.45, 52/256., (102-128)/256., (200-128)/256. },
208 { 0.57, 116/256., (84-128)/256., (196-128)/256. },
209 { 0.67, 157/256., (67-128)/256., (181-128)/256. },
210 { 0.77, 193/256., (40-128)/256., (155-128)/256. },
211 { 0.87, 221/256., (101-128)/256., (134-128)/256. },
215 { 0.20, 29/256., (136-128)/256., (119-128)/256. },
216 { 0.30, 60/256., (119-128)/256., (90-128)/256. },
217 { 0.40, 85/256., (91-128)/256., (85-128)/256. },
218 { 0.50, 116/256., (70-128)/256., (105-128)/256. },
219 { 0.60, 151/256., (50-128)/256., (146-128)/256. },
220 { 0.70, 191/256., (63-128)/256., (178-128)/256. },
221 { 1, 98/256., (80-128)/256., (221-128)/256. }},
228 { 0.10, 23/256., (175-128)/256., (120-128)/256. },
229 { 0.23, 43/256., (158-128)/256., (144-128)/256. },
230 { 0.35, 85/256., (138-128)/256., (179-128)/256. },
231 { 0.48, 96/256., (128-128)/256., (189-128)/256. },
232 { 0.64, 128/256., (103-128)/256., (214-128)/256. },
233 { 0.78, 167/256., (85-128)/256., (174-128)/256. },
234 { 1, 205/256., (80-128)/256., (152-128)/256. }},
237 static av_cold void uninit(AVFilterContext *ctx)
239 ShowSpectrumContext *s = ctx->priv;
242 av_freep(&s->combine_buffer);
244 for (i = 0; i < s->nb_display_channels; i++)
245 av_fft_end(s->fft[i]);
249 for (i = 0; i < s->nb_display_channels; i++)
250 av_freep(&s->fft_data[i]);
252 av_freep(&s->fft_data);
253 if (s->color_buffer) {
254 for (i = 0; i < s->nb_display_channels; i++)
255 av_freep(&s->color_buffer[i]);
257 av_freep(&s->color_buffer);
258 av_freep(&s->window_func_lut);
260 for (i = 0; i < s->nb_display_channels; i++)
261 av_freep(&s->magnitudes[i]);
263 av_freep(&s->magnitudes);
264 av_frame_free(&s->outpicref);
265 av_audio_fifo_free(s->fifo);
267 for (i = 0; i < s->nb_display_channels; i++)
268 av_freep(&s->phases[i]);
270 av_freep(&s->phases);
273 static int query_formats(AVFilterContext *ctx)
275 AVFilterFormats *formats = NULL;
276 AVFilterChannelLayouts *layouts = NULL;
277 AVFilterLink *inlink = ctx->inputs[0];
278 AVFilterLink *outlink = ctx->outputs[0];
279 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
280 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE };
283 /* set input audio formats */
284 formats = ff_make_format_list(sample_fmts);
285 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
288 layouts = ff_all_channel_layouts();
289 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
292 formats = ff_all_samplerates();
293 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
296 /* set output video format */
297 formats = ff_make_format_list(pix_fmts);
298 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
304 static int config_output(AVFilterLink *outlink)
306 AVFilterContext *ctx = outlink->src;
307 AVFilterLink *inlink = ctx->inputs[0];
308 ShowSpectrumContext *s = ctx->priv;
309 int i, fft_bits, h, w;
312 s->pts = AV_NOPTS_VALUE;
314 if (!strcmp(ctx->filter->name, "showspectrumpic"))
319 outlink->sample_aspect_ratio = (AVRational){1,1};
322 s->start_x = log10(inlink->sample_rate) * 25;
324 outlink->w += s->start_x * 2;
325 outlink->h += s->start_y * 2;
328 h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? s->h : s->h / inlink->channels;
329 w = (s->mode == COMBINED || s->orientation == VERTICAL) ? s->w : s->w / inlink->channels;
330 s->channel_height = h;
331 s->channel_width = w;
333 if (s->orientation == VERTICAL) {
334 /* FFT window size (precision) according to the requested output frame height */
335 for (fft_bits = 1; 1 << fft_bits < 2 * h; fft_bits++);
337 /* FFT window size (precision) according to the requested output frame width */
338 for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++);
340 s->win_size = 1 << fft_bits;
343 s->fft = av_calloc(inlink->channels, sizeof(*s->fft));
345 return AVERROR(ENOMEM);
348 /* (re-)configuration if the video output changed (or first init) */
349 if (fft_bits != s->fft_bits) {
352 s->fft_bits = fft_bits;
354 /* FFT buffers: x2 for each (display) channel buffer.
355 * Note: we use free and malloc instead of a realloc-like function to
356 * make sure the buffer is aligned in memory for the FFT functions. */
357 for (i = 0; i < s->nb_display_channels; i++) {
358 av_fft_end(s->fft[i]);
359 av_freep(&s->fft_data[i]);
361 av_freep(&s->fft_data);
363 s->nb_display_channels = inlink->channels;
364 for (i = 0; i < s->nb_display_channels; i++) {
365 s->fft[i] = av_fft_init(fft_bits, 0);
367 av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
368 "The window size might be too high.\n");
369 return AVERROR(EINVAL);
373 s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
375 return AVERROR(ENOMEM);
376 for (i = 0; i < s->nb_display_channels; i++) {
377 s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
378 if (!s->magnitudes[i])
379 return AVERROR(ENOMEM);
382 s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases));
384 return AVERROR(ENOMEM);
385 for (i = 0; i < s->nb_display_channels; i++) {
386 s->phases[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->phases));
388 return AVERROR(ENOMEM);
391 av_freep(&s->color_buffer);
392 s->color_buffer = av_calloc(s->nb_display_channels, sizeof(*s->color_buffer));
393 if (!s->color_buffer)
394 return AVERROR(ENOMEM);
395 for (i = 0; i < s->nb_display_channels; i++) {
396 s->color_buffer[i] = av_calloc(s->orientation == VERTICAL ? s->h * 3 : s->w * 3, sizeof(**s->color_buffer));
397 if (!s->color_buffer[i])
398 return AVERROR(ENOMEM);
401 s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
403 return AVERROR(ENOMEM);
404 for (i = 0; i < s->nb_display_channels; i++) {
405 s->fft_data[i] = av_calloc(s->win_size, sizeof(**s->fft_data));
407 return AVERROR(ENOMEM);
410 /* pre-calc windowing function */
412 av_realloc_f(s->window_func_lut, s->win_size,
413 sizeof(*s->window_func_lut));
414 if (!s->window_func_lut)
415 return AVERROR(ENOMEM);
416 generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
418 s->overlap = overlap;
419 s->hop_size = (1. - s->overlap) * s->win_size;
420 if (s->hop_size < 1) {
421 av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
422 return AVERROR(EINVAL);
425 for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
426 s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
428 s->win_scale = 1. / sqrt(s->win_scale);
430 /* prepare the initial picref buffer (black frame) */
431 av_frame_free(&s->outpicref);
432 s->outpicref = outpicref =
433 ff_get_video_buffer(outlink, outlink->w, outlink->h);
435 return AVERROR(ENOMEM);
436 outpicref->sample_aspect_ratio = (AVRational){1,1};
437 for (i = 0; i < outlink->h; i++) {
438 memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
439 memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
440 memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
442 outpicref->color_range = AVCOL_RANGE_JPEG;
445 if ((s->orientation == VERTICAL && s->xpos >= s->w) ||
446 (s->orientation == HORIZONTAL && s->xpos >= s->h))
449 outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
450 if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
451 outlink->frame_rate.den *= s->w;
452 if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
453 outlink->frame_rate.den *= s->h;
455 if (s->orientation == VERTICAL) {
457 av_realloc_f(s->combine_buffer, s->h * 3,
458 sizeof(*s->combine_buffer));
461 av_realloc_f(s->combine_buffer, s->w * 3,
462 sizeof(*s->combine_buffer));
465 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n",
466 s->w, s->h, s->win_size);
468 av_audio_fifo_free(s->fifo);
469 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
471 return AVERROR(ENOMEM);
475 static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
477 ShowSpectrumContext *s = ctx->priv;
478 const float *window_func_lut = s->window_func_lut;
480 const int ch = jobnr;
483 /* fill FFT input with the number of samples available */
484 const float *p = (float *)fin->extended_data[ch];
486 for (n = 0; n < s->win_size; n++) {
487 s->fft_data[ch][n].re = p[n] * window_func_lut[n];
488 s->fft_data[ch][n].im = 0;
491 /* run FFT on each samples set */
492 av_fft_permute(s->fft[ch], s->fft_data[ch]);
493 av_fft_calc(s->fft[ch], s->fft_data[ch]);
498 #define RE(y, ch) s->fft_data[ch][y].re
499 #define IM(y, ch) s->fft_data[ch][y].im
500 #define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch))
501 #define PHASE(y, ch) atan2(IM(y, ch), RE(y, ch))
503 static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
505 ShowSpectrumContext *s = ctx->priv;
506 const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
507 int y, h = s->orientation == VERTICAL ? s->h : s->w;
508 const float f = s->gain * w;
509 const int ch = jobnr;
510 float *magnitudes = s->magnitudes[ch];
512 for (y = 0; y < h; y++)
513 magnitudes[y] = MAGNITUDE(y, ch) * f;
518 static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
520 ShowSpectrumContext *s = ctx->priv;
521 const int h = s->orientation == VERTICAL ? s->h : s->w;
522 const int ch = jobnr;
523 float *phases = s->phases[ch];
526 for (y = 0; y < h; y++)
527 phases[y] = (PHASE(y, ch) / M_PI + 1) / 2;
532 static void acalc_magnitudes(ShowSpectrumContext *s)
534 const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
535 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
536 const float f = s->gain * w;
538 for (ch = 0; ch < s->nb_display_channels; ch++) {
539 float *magnitudes = s->magnitudes[ch];
541 for (y = 0; y < h; y++)
542 magnitudes[y] += MAGNITUDE(y, ch) * f;
546 static void scale_magnitudes(ShowSpectrumContext *s, float scale)
548 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
550 for (ch = 0; ch < s->nb_display_channels; ch++) {
551 float *magnitudes = s->magnitudes[ch];
553 for (y = 0; y < h; y++)
554 magnitudes[y] *= scale;
558 static void color_range(ShowSpectrumContext *s, int ch,
559 float *yf, float *uf, float *vf)
563 // reduce range by channel count
564 *yf = 256.0f / s->nb_display_channels;
565 switch (s->color_mode) {
579 /* adjust saturation for mixed UV coloring */
580 /* this factor is correct for infinite channels, an approximation otherwise */
598 if (s->color_mode == CHANNEL) {
599 if (s->nb_display_channels > 1) {
600 *uf *= 0.5 * sin((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
601 *vf *= 0.5 * cos((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
603 *uf *= 0.5 * sin(M_PI * s->rotation);
604 *vf *= 0.5 * cos(M_PI * s->rotation + M_PI_2);
607 *uf += *uf * sin(M_PI * s->rotation);
608 *vf += *vf * cos(M_PI * s->rotation + M_PI_2);
611 *uf *= s->saturation;
612 *vf *= s->saturation;
615 static void pick_color(ShowSpectrumContext *s,
616 float yf, float uf, float vf,
619 if (s->color_mode > CHANNEL) {
620 const int cm = s->color_mode;
624 for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
625 if (color_table[cm][i].a >= a)
627 // i now is the first item >= the color
628 // now we know to interpolate between item i - 1 and i
629 if (a <= color_table[cm][i - 1].a) {
630 y = color_table[cm][i - 1].y;
631 u = color_table[cm][i - 1].u;
632 v = color_table[cm][i - 1].v;
633 } else if (a >= color_table[cm][i].a) {
634 y = color_table[cm][i].y;
635 u = color_table[cm][i].u;
636 v = color_table[cm][i].v;
638 float start = color_table[cm][i - 1].a;
639 float end = color_table[cm][i].a;
640 float lerpfrac = (a - start) / (end - start);
641 y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
642 + color_table[cm][i].y * lerpfrac;
643 u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
644 + color_table[cm][i].u * lerpfrac;
645 v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
646 + color_table[cm][i].v * lerpfrac;
659 static void clear_combine_buffer(ShowSpectrumContext *s, int size)
663 for (y = 0; y < size; y++) {
664 s->combine_buffer[3 * y ] = 0;
665 s->combine_buffer[3 * y + 1] = 127.5;
666 s->combine_buffer[3 * y + 2] = 127.5;
670 static int plot_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
672 ShowSpectrumContext *s = ctx->priv;
673 const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
674 const int ch = jobnr;
675 float *magnitudes = s->magnitudes[ch];
676 float *phases = s->phases[ch];
680 /* decide color range */
681 color_range(s, ch, &yf, &uf, &vf);
683 /* draw the channel */
684 for (y = 0; y < h; y++) {
685 int row = (s->mode == COMBINED) ? y : ch * h + y;
686 float *out = &s->color_buffer[ch][3 * row];
705 a = av_clipf(a, 0, 1);
708 a = av_clipf(sqrt(a), 0, 1);
711 a = av_clipf(cbrt(a), 0, 1);
714 a = av_clipf(sqrt(sqrt(a)), 0, 1);
717 a = av_clipf(pow(a, 0.20), 0, 1);
720 a = 1 + log10(av_clipd(a, 1e-6, 1)) / 6; // zero = -120dBFS
726 pick_color(s, yf, uf, vf, a, out);
732 static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
734 AVFilterContext *ctx = inlink->dst;
735 AVFilterLink *outlink = ctx->outputs[0];
736 ShowSpectrumContext *s = ctx->priv;
737 AVFrame *outpicref = s->outpicref;
738 int ret, plane, x, y, z = s->orientation == VERTICAL ? s->h : s->w;
740 /* fill a new spectrum column */
741 /* initialize buffer for combining to black */
742 clear_combine_buffer(s, z);
744 ctx->internal->execute(ctx, plot_channel, NULL, NULL, s->nb_display_channels);
746 for (y = 0; y < z * 3; y++) {
747 for (x = 0; x < s->nb_display_channels; x++) {
748 s->combine_buffer[y] += s->color_buffer[x][y];
752 av_frame_make_writable(s->outpicref);
754 if (s->orientation == VERTICAL) {
755 if (s->sliding == SCROLL) {
756 for (plane = 0; plane < 3; plane++) {
757 for (y = 0; y < s->h; y++) {
758 uint8_t *p = outpicref->data[plane] +
759 y * outpicref->linesize[plane];
760 memmove(p, p + 1, s->w - 1);
764 } else if (s->sliding == RSCROLL) {
765 for (plane = 0; plane < 3; plane++) {
766 for (y = 0; y < s->h; y++) {
767 uint8_t *p = outpicref->data[plane] +
768 y * outpicref->linesize[plane];
769 memmove(p + 1, p, s->w - 1);
774 for (plane = 0; plane < 3; plane++) {
775 uint8_t *p = outpicref->data[plane] + s->start_x +
776 (outlink->h - 1 - s->start_y) * outpicref->linesize[plane] +
778 for (y = 0; y < s->h; y++) {
779 *p = lrintf(av_clipf(s->combine_buffer[3 * y + plane], 0, 255));
780 p -= outpicref->linesize[plane];
784 if (s->sliding == SCROLL) {
785 for (plane = 0; plane < 3; plane++) {
786 for (y = 1; y < s->h; y++) {
787 memmove(outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
788 outpicref->data[plane] + (y ) * outpicref->linesize[plane],
793 } else if (s->sliding == RSCROLL) {
794 for (plane = 0; plane < 3; plane++) {
795 for (y = s->h - 1; y >= 1; y--) {
796 memmove(outpicref->data[plane] + (y ) * outpicref->linesize[plane],
797 outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
803 for (plane = 0; plane < 3; plane++) {
804 uint8_t *p = outpicref->data[plane] + s->start_x +
805 (s->xpos + s->start_y) * outpicref->linesize[plane];
806 for (x = 0; x < s->w; x++) {
807 *p = lrintf(av_clipf(s->combine_buffer[3 * x + plane], 0, 255));
813 if (s->sliding != FULLFRAME || s->xpos == 0)
814 outpicref->pts = insamples->pts;
817 if (s->orientation == VERTICAL && s->xpos >= s->w)
819 if (s->orientation == HORIZONTAL && s->xpos >= s->h)
821 if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
822 ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
830 #if CONFIG_SHOWSPECTRUM_FILTER
832 static int request_frame(AVFilterLink *outlink)
834 ShowSpectrumContext *s = outlink->src->priv;
835 AVFilterLink *inlink = outlink->src->inputs[0];
839 ret = ff_request_frame(inlink);
840 if (ret == AVERROR_EOF && s->sliding == FULLFRAME && s->xpos > 0 &&
842 if (s->orientation == VERTICAL) {
843 for (i = 0; i < outlink->h; i++) {
844 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos);
845 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
846 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
849 for (i = s->xpos; i < outlink->h; i++) {
850 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w);
851 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
852 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
855 ret = ff_filter_frame(outlink, s->outpicref);
862 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
864 AVFilterContext *ctx = inlink->dst;
865 ShowSpectrumContext *s = ctx->priv;
867 int ret = 0, consumed = 0;
869 if (s->pts == AV_NOPTS_VALUE)
870 s->pts = insamples->pts - av_audio_fifo_size(s->fifo);
872 av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
873 av_frame_free(&insamples);
874 while (av_audio_fifo_size(s->fifo) >= s->win_size) {
875 fin = ff_get_audio_buffer(inlink, s->win_size);
877 ret = AVERROR(ENOMEM);
881 fin->pts = s->pts + consumed;
882 consumed += s->hop_size;
883 ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
887 av_assert0(fin->nb_samples == s->win_size);
889 ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
891 if (s->data == D_MAGNITUDE)
892 ctx->internal->execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels);
894 if (s->data == D_PHASE)
895 ctx->internal->execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels);
897 ret = plot_spectrum_column(inlink, fin);
899 av_audio_fifo_drain(s->fifo, s->hop_size);
905 s->pts = AV_NOPTS_VALUE;
910 static const AVFilterPad showspectrum_inputs[] = {
913 .type = AVMEDIA_TYPE_AUDIO,
914 .filter_frame = filter_frame,
919 static const AVFilterPad showspectrum_outputs[] = {
922 .type = AVMEDIA_TYPE_VIDEO,
923 .config_props = config_output,
924 .request_frame = request_frame,
929 AVFilter ff_avf_showspectrum = {
930 .name = "showspectrum",
931 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
933 .query_formats = query_formats,
934 .priv_size = sizeof(ShowSpectrumContext),
935 .inputs = showspectrum_inputs,
936 .outputs = showspectrum_outputs,
937 .priv_class = &showspectrum_class,
938 .flags = AVFILTER_FLAG_SLICE_THREADS,
940 #endif // CONFIG_SHOWSPECTRUM_FILTER
942 #if CONFIG_SHOWSPECTRUMPIC_FILTER
944 static const AVOption showspectrumpic_options[] = {
945 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
946 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
947 { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" },
948 { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
949 { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
950 { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" },
951 { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
952 { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
953 { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
954 { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
955 { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
956 { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
957 { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
958 { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
959 { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
960 { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
961 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" },
962 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
963 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
964 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
965 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
966 { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
967 { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
968 { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
969 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
970 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
971 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
972 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
973 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
974 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
975 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
976 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
977 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
978 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
979 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
980 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
981 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
982 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
983 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
984 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
985 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
986 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
987 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
988 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
989 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
990 { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
991 { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
992 { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
993 { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
994 { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
995 { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
999 AVFILTER_DEFINE_CLASS(showspectrumpic);
1001 static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
1003 const uint8_t *font;
1007 font = avpriv_cga_font, font_height = 8;
1009 for (i = 0; txt[i]; i++) {
1013 for (char_y = font_height - 1; char_y >= 0; char_y--) {
1014 uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x;
1015 for (mask = 0x80; mask; mask >>= 1) {
1016 if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
1017 p[char_y] = ~p[char_y];
1018 p += pic->linesize[0];
1022 uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8);
1023 for (char_y = 0; char_y < font_height; char_y++) {
1024 for (mask = 0x80; mask; mask >>= 1) {
1025 if (font[txt[i] * font_height + char_y] & mask)
1029 p += pic->linesize[0] - 8;
1035 static int showspectrumpic_request_frame(AVFilterLink *outlink)
1037 AVFilterContext *ctx = outlink->src;
1038 ShowSpectrumContext *s = ctx->priv;
1039 AVFilterLink *inlink = ctx->inputs[0];
1042 ret = ff_request_frame(inlink);
1043 samples = av_audio_fifo_size(s->fifo);
1044 if (ret == AVERROR_EOF && s->outpicref && samples > 0) {
1046 int y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
1050 spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz))));
1051 spf = FFMAX(1, spf);
1053 spb = (samples / (spf * sz)) * spf;
1055 fin = ff_get_audio_buffer(inlink, s->win_size);
1057 return AVERROR(ENOMEM);
1060 ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
1062 av_frame_free(&fin);
1066 av_audio_fifo_drain(s->fifo, spf);
1068 if (ret < s->win_size) {
1069 for (ch = 0; ch < s->nb_display_channels; ch++) {
1070 memset(fin->extended_data[ch] + ret * sizeof(float), 0,
1071 (s->win_size - ret) * sizeof(float));
1075 ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
1076 acalc_magnitudes(s);
1079 if (consumed >= spb) {
1080 int h = s->orientation == VERTICAL ? s->h : s->w;
1082 scale_magnitudes(s, 1. / (consumed / spf));
1083 plot_spectrum_column(inlink, fin);
1086 for (ch = 0; ch < s->nb_display_channels; ch++)
1087 memset(s->magnitudes[ch], 0, h * sizeof(float));
1091 av_frame_free(&fin);
1092 s->outpicref->pts = 0;
1095 int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL);
1096 float spp = samples / (float)sz;
1099 char chlayout_str[128];
1101 av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), inlink->channels,
1102 inlink->channel_layout);
1104 text = av_asprintf("%d Hz | %s", inlink->sample_rate, chlayout_str);
1106 drawtext(s->outpicref, 2, outlink->h - 10, "CREATED BY LIBAVFILTER", 0);
1107 drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, outlink->h - 10, text, 0);
1111 dst = s->outpicref->data[0] + (s->start_y - 1) * s->outpicref->linesize[0] + s->start_x - 1;
1112 for (x = 0; x < s->w + 1; x++)
1114 dst = s->outpicref->data[0] + (s->start_y + s->h) * s->outpicref->linesize[0] + s->start_x - 1;
1115 for (x = 0; x < s->w + 1; x++)
1117 for (y = 0; y < s->h + 2; y++) {
1118 dst = s->outpicref->data[0] + (y + s->start_y - 1) * s->outpicref->linesize[0];
1119 dst[s->start_x - 1] = 200;
1120 dst[s->start_x + s->w] = 200;
1122 if (s->orientation == VERTICAL) {
1123 int h = s->mode == SEPARATE ? s->h / s->nb_display_channels : s->h;
1124 int hh = s->mode == SEPARATE ? -(s->h % s->nb_display_channels) + 1 : 1;
1125 for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
1126 for (y = 0; y < h; y += 20) {
1127 dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
1128 dst[s->start_x - 2] = 200;
1129 dst[s->start_x + s->w + 1] = 200;
1131 for (y = 0; y < h; y += 40) {
1132 dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
1133 dst[s->start_x - 3] = 200;
1134 dst[s->start_x + s->w + 2] = 200;
1136 dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x;
1137 for (x = 0; x < s->w; x+=40)
1139 dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x;
1140 for (x = 0; x < s->w; x+=80)
1142 dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x;
1143 for (x = 0; x < s->w; x+=40) {
1146 dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x;
1147 for (x = 0; x < s->w; x+=80) {
1150 for (y = 0; y < h; y += 40) {
1151 float hertz = y * (inlink->sample_rate / 2) / (float)(1 << (int)ceil(log2(h)));
1155 units = av_asprintf("DC");
1157 units = av_asprintf("%.2f", hertz);
1159 return AVERROR(ENOMEM);
1161 drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4 - hh, units, 0);
1166 for (x = 0; x < s->w; x+=80) {
1167 float seconds = x * spp / inlink->sample_rate;
1171 units = av_asprintf("0");
1172 else if (log10(seconds) > 6)
1173 units = av_asprintf("%.2fh", seconds / (60 * 60));
1174 else if (log10(seconds) > 3)
1175 units = av_asprintf("%.2fm", seconds / 60);
1177 units = av_asprintf("%.2fs", seconds);
1179 return AVERROR(ENOMEM);
1181 drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->h + s->start_y + 6, units, 0);
1182 drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->start_y - 12, units, 0);
1186 drawtext(s->outpicref, outlink->w / 2 - 4 * 4, outlink->h - s->start_y / 2, "TIME", 0);
1187 drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 14 * 4, "FREQUENCY (Hz)", 1);
1189 int w = s->mode == SEPARATE ? s->w / s->nb_display_channels : s->w;
1190 for (y = 0; y < s->h; y += 20) {
1191 dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
1192 dst[s->start_x - 2] = 200;
1193 dst[s->start_x + s->w + 1] = 200;
1195 for (y = 0; y < s->h; y += 40) {
1196 dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
1197 dst[s->start_x - 3] = 200;
1198 dst[s->start_x + s->w + 2] = 200;
1200 for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
1201 dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
1202 for (x = 0; x < w; x+=40)
1204 dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x + w * ch;
1205 for (x = 0; x < w; x+=80)
1207 dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x + w * ch;
1208 for (x = 0; x < w; x+=40) {
1211 dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
1212 for (x = 0; x < w; x+=80) {
1215 for (x = 0; x < w - 79; x += 80) {
1216 float hertz = x * (inlink->sample_rate / 2) / (float)(1 << (int)ceil(log2(w)));
1220 units = av_asprintf("DC");
1222 units = av_asprintf("%.2f", hertz);
1224 return AVERROR(ENOMEM);
1226 drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->start_y - 12, units, 0);
1227 drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->h + s->start_y + 6, units, 0);
1231 for (y = 0; y < s->h; y+=40) {
1232 float seconds = y * spp / inlink->sample_rate;
1236 units = av_asprintf("0");
1237 else if (log10(seconds) > 6)
1238 units = av_asprintf("%.2fh", seconds / (60 * 60));
1239 else if (log10(seconds) > 3)
1240 units = av_asprintf("%.2fm", seconds / 60);
1242 units = av_asprintf("%.2fs", seconds);
1244 return AVERROR(ENOMEM);
1246 drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0);
1249 drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 4 * 4, "TIME", 1);
1250 drawtext(s->outpicref, outlink->w / 2 - 14 * 4, outlink->h - s->start_y / 2, "FREQUENCY (Hz)", 0);
1253 for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) {
1254 int h = multi ? s->h / s->nb_display_channels : s->h;
1256 for (y = 0; y < h; y++) {
1257 float out[3] = { 0., 127.5, 127.5};
1260 for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) {
1262 int channel = (multi) ? s->nb_display_channels - ch - 1 : chn;
1265 color_range(s, channel, &yf, &uf, &vf);
1266 pick_color(s, yf, uf, vf, y / (float)h, lout);
1271 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);
1272 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);
1273 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);
1276 for (y = 0; ch == 0 && y < h; y += h / 10) {
1277 float value = 120.0 * log10(1. - y / (float)h);
1282 text = av_asprintf("%.0f dB", value);
1285 drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 5, text, 0);
1291 ret = ff_filter_frame(outlink, s->outpicref);
1292 s->outpicref = NULL;
1298 static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1300 AVFilterContext *ctx = inlink->dst;
1301 ShowSpectrumContext *s = ctx->priv;
1304 ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
1305 av_frame_free(&insamples);
1309 static const AVFilterPad showspectrumpic_inputs[] = {
1312 .type = AVMEDIA_TYPE_AUDIO,
1313 .filter_frame = showspectrumpic_filter_frame,
1318 static const AVFilterPad showspectrumpic_outputs[] = {
1321 .type = AVMEDIA_TYPE_VIDEO,
1322 .config_props = config_output,
1323 .request_frame = showspectrumpic_request_frame,
1328 AVFilter ff_avf_showspectrumpic = {
1329 .name = "showspectrumpic",
1330 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
1332 .query_formats = query_formats,
1333 .priv_size = sizeof(ShowSpectrumContext),
1334 .inputs = showspectrumpic_inputs,
1335 .outputs = showspectrumpic_outputs,
1336 .priv_class = &showspectrumpic_class,
1337 .flags = AVFILTER_FLAG_SLICE_THREADS,
1340 #endif // CONFIG_SHOWSPECTRUMPIC_FILTER