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/channel_layout.h"
35 #include "libavutil/opt.h"
40 #include "window_func.h"
42 enum DisplayMode { COMBINED, SEPARATE, NB_MODES };
43 enum DisplayScale { LINEAR, SQRT, CBRT, LOG, FOURTHRT, FIFTHRT, NB_SCALES };
44 enum ColorMode { CHANNEL, INTENSITY, RAINBOW, MORELAND, NEBULAE, FIRE, FIERY, FRUIT, NB_CLMODES };
45 enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, NB_SLIDES };
46 enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS };
52 int nb_display_channels;
56 int sliding; ///< 1 if sliding mode, 0 otherwise
57 int mode; ///< channel display mode
58 int color_mode; ///< display color scheme
60 float saturation; ///< color saturation multiplier
61 int xpos; ///< x position (current column)
62 FFTContext *fft; ///< Fast Fourier Transform context
63 int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits)
64 FFTComplex **fft_data; ///< bins holder for each (displayed) channels
65 float *window_func_lut; ///< Window function LUT
73 float *combine_buffer; ///< color combining buffer (3 * h items)
77 } ShowSpectrumContext;
79 #define OFFSET(x) offsetof(ShowSpectrumContext, x)
80 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
82 static const AVOption showspectrum_options[] = {
83 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
84 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
85 { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
86 { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
87 { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
88 { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
89 { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
90 { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
91 { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
92 { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
93 { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
94 { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
95 { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
96 { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
97 { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
98 { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
99 { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
100 { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
101 { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
102 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
103 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
104 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
105 { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
106 { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
107 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
108 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
109 { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
110 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
111 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
112 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
113 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
114 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
115 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
116 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
117 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
118 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
119 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
120 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
121 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
122 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
123 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
124 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
125 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
126 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
127 { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
128 { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
129 { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
130 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS },
131 { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
135 AVFILTER_DEFINE_CLASS(showspectrum);
137 static const struct ColorTable {
139 } color_table[][8] = {
142 { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
143 { 0.30, .18572281794568020, .1772436246393981, .17475554840414750 },
144 { 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 },
145 { 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 },
146 { 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 },
147 { 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 },
151 { 0.13, 44/256., (189-128)/256., (138-128)/256. },
152 { 0.25, 29/256., (186-128)/256., (119-128)/256. },
153 { 0.38, 119/256., (194-128)/256., (53-128)/256. },
154 { 0.60, 111/256., (73-128)/256., (59-128)/256. },
155 { 0.73, 205/256., (19-128)/256., (149-128)/256. },
156 { 0.86, 135/256., (83-128)/256., (200-128)/256. },
157 { 1, 73/256., (95-128)/256., (225-128)/256. }},
159 { 0, 44/256., (181-128)/256., (112-128)/256. },
160 { 0.13, 126/256., (177-128)/256., (106-128)/256. },
161 { 0.25, 164/256., (163-128)/256., (109-128)/256. },
162 { 0.38, 200/256., (140-128)/256., (120-128)/256. },
163 { 0.60, 201/256., (117-128)/256., (141-128)/256. },
164 { 0.73, 177/256., (103-128)/256., (165-128)/256. },
165 { 0.86, 136/256., (100-128)/256., (183-128)/256. },
166 { 1, 68/256., (117-128)/256., (203-128)/256. }},
168 { 0, 10/256., (134-128)/256., (132-128)/256. },
169 { 0.23, 21/256., (137-128)/256., (130-128)/256. },
170 { 0.45, 35/256., (134-128)/256., (134-128)/256. },
171 { 0.57, 51/256., (130-128)/256., (139-128)/256. },
172 { 0.67, 104/256., (116-128)/256., (162-128)/256. },
173 { 0.77, 120/256., (105-128)/256., (188-128)/256. },
174 { 0.87, 140/256., (105-128)/256., (188-128)/256. },
178 { 0.23, 44/256., (132-128)/256., (127-128)/256. },
179 { 0.45, 62/256., (116-128)/256., (140-128)/256. },
180 { 0.57, 75/256., (105-128)/256., (152-128)/256. },
181 { 0.67, 95/256., (91-128)/256., (166-128)/256. },
182 { 0.77, 126/256., (74-128)/256., (172-128)/256. },
183 { 0.87, 164/256., (73-128)/256., (162-128)/256. },
187 { 0.23, 36/256., (116-128)/256., (163-128)/256. },
188 { 0.45, 52/256., (102-128)/256., (200-128)/256. },
189 { 0.57, 116/256., (84-128)/256., (196-128)/256. },
190 { 0.67, 157/256., (67-128)/256., (181-128)/256. },
191 { 0.77, 193/256., (40-128)/256., (155-128)/256. },
192 { 0.87, 221/256., (101-128)/256., (134-128)/256. },
196 { 0.20, 29/256., (136-128)/256., (119-128)/256. },
197 { 0.30, 60/256., (119-128)/256., (90-128)/256. },
198 { 0.40, 85/256., (91-128)/256., (85-128)/256. },
199 { 0.50, 116/256., (70-128)/256., (105-128)/256. },
200 { 0.60, 151/256., (50-128)/256., (146-128)/256. },
201 { 0.70, 191/256., (63-128)/256., (178-128)/256. },
202 { 1, 98/256., (80-128)/256., (221-128)/256. }},
205 static av_cold void uninit(AVFilterContext *ctx)
207 ShowSpectrumContext *s = ctx->priv;
210 av_freep(&s->combine_buffer);
213 for (i = 0; i < s->nb_display_channels; i++)
214 av_freep(&s->fft_data[i]);
216 av_freep(&s->fft_data);
217 av_freep(&s->window_func_lut);
219 for (i = 0; i < s->nb_display_channels; i++)
220 av_freep(&s->magnitudes[i]);
222 av_freep(&s->magnitudes);
223 av_frame_free(&s->outpicref);
224 av_audio_fifo_free(s->fifo);
227 static int query_formats(AVFilterContext *ctx)
229 AVFilterFormats *formats = NULL;
230 AVFilterChannelLayouts *layouts = NULL;
231 AVFilterLink *inlink = ctx->inputs[0];
232 AVFilterLink *outlink = ctx->outputs[0];
233 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
234 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE };
237 /* set input audio formats */
238 formats = ff_make_format_list(sample_fmts);
239 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
242 layouts = ff_all_channel_layouts();
243 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
246 formats = ff_all_samplerates();
247 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
250 /* set output video format */
251 formats = ff_make_format_list(pix_fmts);
252 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
258 static int config_output(AVFilterLink *outlink)
260 AVFilterContext *ctx = outlink->src;
261 AVFilterLink *inlink = ctx->inputs[0];
262 ShowSpectrumContext *s = ctx->priv;
263 int i, fft_bits, h, w;
266 if (!strcmp(ctx->filter->name, "showspectrumpic"))
272 h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? outlink->h : outlink->h / inlink->channels;
273 w = (s->mode == COMBINED || s->orientation == VERTICAL) ? outlink->w : outlink->w / inlink->channels;
274 s->channel_height = h;
275 s->channel_width = w;
277 if (s->orientation == VERTICAL) {
278 /* FFT window size (precision) according to the requested output frame height */
279 for (fft_bits = 1; 1 << fft_bits < 2 * h; fft_bits++);
281 /* FFT window size (precision) according to the requested output frame width */
282 for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++);
284 s->win_size = 1 << fft_bits;
286 /* (re-)configuration if the video output changed (or first init) */
287 if (fft_bits != s->fft_bits) {
291 s->fft = av_fft_init(fft_bits, 0);
293 av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
294 "The window size might be too high.\n");
295 return AVERROR(EINVAL);
297 s->fft_bits = fft_bits;
299 /* FFT buffers: x2 for each (display) channel buffer.
300 * Note: we use free and malloc instead of a realloc-like function to
301 * make sure the buffer is aligned in memory for the FFT functions. */
302 for (i = 0; i < s->nb_display_channels; i++)
303 av_freep(&s->fft_data[i]);
304 av_freep(&s->fft_data);
305 s->nb_display_channels = inlink->channels;
307 s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
309 return AVERROR(ENOMEM);
310 for (i = 0; i < s->nb_display_channels; i++) {
311 s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
312 if (!s->magnitudes[i])
313 return AVERROR(ENOMEM);
316 s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
318 return AVERROR(ENOMEM);
319 for (i = 0; i < s->nb_display_channels; i++) {
320 s->fft_data[i] = av_calloc(s->win_size, sizeof(**s->fft_data));
322 return AVERROR(ENOMEM);
325 /* pre-calc windowing function */
327 av_realloc_f(s->window_func_lut, s->win_size,
328 sizeof(*s->window_func_lut));
329 if (!s->window_func_lut)
330 return AVERROR(ENOMEM);
331 ff_generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
333 s->overlap = overlap;
334 s->skip_samples = (1. - s->overlap) * s->win_size;
335 if (s->skip_samples < 1) {
336 av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
337 return AVERROR(EINVAL);
340 for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
341 s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
343 s->win_scale = 1. / sqrt(s->win_scale);
345 /* prepare the initial picref buffer (black frame) */
346 av_frame_free(&s->outpicref);
347 s->outpicref = outpicref =
348 ff_get_video_buffer(outlink, outlink->w, outlink->h);
350 return AVERROR(ENOMEM);
351 outlink->sample_aspect_ratio = (AVRational){1,1};
352 for (i = 0; i < outlink->h; i++) {
353 memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
354 memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
355 memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
359 if ((s->orientation == VERTICAL && s->xpos >= outlink->w) ||
360 (s->orientation == HORIZONTAL && s->xpos >= outlink->h))
363 outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
364 if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
365 outlink->frame_rate.den *= outlink->w;
366 if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
367 outlink->frame_rate.den *= outlink->h;
369 if (s->orientation == VERTICAL) {
371 av_realloc_f(s->combine_buffer, outlink->h * 3,
372 sizeof(*s->combine_buffer));
375 av_realloc_f(s->combine_buffer, outlink->w * 3,
376 sizeof(*s->combine_buffer));
379 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n",
380 s->w, s->h, s->win_size);
382 av_audio_fifo_free(s->fifo);
383 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
385 return AVERROR(ENOMEM);
389 static void run_fft(ShowSpectrumContext *s, AVFrame *fin)
393 /* fill FFT input with the number of samples available */
394 for (ch = 0; ch < s->nb_display_channels; ch++) {
395 const float *p = (float *)fin->extended_data[ch];
397 for (n = 0; n < s->win_size; n++) {
398 s->fft_data[ch][n].re = p[n] * s->window_func_lut[n];
399 s->fft_data[ch][n].im = 0;
403 /* run FFT on each samples set */
404 for (ch = 0; ch < s->nb_display_channels; ch++) {
405 av_fft_permute(s->fft, s->fft_data[ch]);
406 av_fft_calc(s->fft, s->fft_data[ch]);
410 #define RE(y, ch) s->fft_data[ch][y].re
411 #define IM(y, ch) s->fft_data[ch][y].im
412 #define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch))
414 static void calc_magnitudes(ShowSpectrumContext *s)
416 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
418 for (ch = 0; ch < s->nb_display_channels; ch++) {
419 float *magnitudes = s->magnitudes[ch];
421 for (y = 0; y < h; y++)
422 magnitudes[y] = MAGNITUDE(y, ch);
426 static void acalc_magnitudes(ShowSpectrumContext *s)
428 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
430 for (ch = 0; ch < s->nb_display_channels; ch++) {
431 float *magnitudes = s->magnitudes[ch];
433 for (y = 0; y < h; y++)
434 magnitudes[y] += MAGNITUDE(y, ch);
438 static void scale_magnitudes(ShowSpectrumContext *s, float scale)
440 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
442 for (ch = 0; ch < s->nb_display_channels; ch++) {
443 float *magnitudes = s->magnitudes[ch];
445 for (y = 0; y < h; y++)
446 magnitudes[y] *= scale;
450 static void pick_color(ShowSpectrumContext *s,
451 float yf, float uf, float vf,
454 if (s->color_mode > CHANNEL) {
455 const int cm = s->color_mode;
459 for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
460 if (color_table[cm][i].a >= a)
462 // i now is the first item >= the color
463 // now we know to interpolate between item i - 1 and i
464 if (a <= color_table[cm][i - 1].a) {
465 y = color_table[cm][i - 1].y;
466 u = color_table[cm][i - 1].u;
467 v = color_table[cm][i - 1].v;
468 } else if (a >= color_table[cm][i].a) {
469 y = color_table[cm][i].y;
470 u = color_table[cm][i].u;
471 v = color_table[cm][i].v;
473 float start = color_table[cm][i - 1].a;
474 float end = color_table[cm][i].a;
475 float lerpfrac = (a - start) / (end - start);
476 y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
477 + color_table[cm][i].y * lerpfrac;
478 u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
479 + color_table[cm][i].u * lerpfrac;
480 v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
481 + color_table[cm][i].v * lerpfrac;
494 static void clear_combine_buffer(ShowSpectrumContext *s, int size)
498 for (y = 0; y < size; y++) {
499 s->combine_buffer[3 * y ] = 0;
500 s->combine_buffer[3 * y + 1] = 127.5;
501 s->combine_buffer[3 * y + 2] = 127.5;
505 static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
508 AVFilterContext *ctx = inlink->dst;
509 AVFilterLink *outlink = ctx->outputs[0];
510 ShowSpectrumContext *s = ctx->priv;
511 AVFrame *outpicref = s->outpicref;
512 const double w = s->win_scale;
513 const float g = s->gain;
514 int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
518 /* fill a new spectrum column */
519 /* initialize buffer for combining to black */
520 clear_combine_buffer(s, s->orientation == VERTICAL ? outlink->h : outlink->w);
522 for (ch = 0; ch < s->nb_display_channels; ch++) {
523 float *magnitudes = s->magnitudes[ch];
526 /* decide color range */
529 // reduce range by channel count
530 yf = 256.0f / s->nb_display_channels;
531 switch (s->color_mode) {
543 /* adjust saturation for mixed UV coloring */
544 /* this factor is correct for infinite channels, an approximation otherwise */
562 if (s->color_mode == CHANNEL) {
563 if (s->nb_display_channels > 1) {
564 uf *= 0.5 * sin((2 * M_PI * ch) / s->nb_display_channels);
565 vf *= 0.5 * cos((2 * M_PI * ch) / s->nb_display_channels);
574 /* draw the channel */
575 for (y = 0; y < h; y++) {
576 int row = (s->mode == COMBINED) ? y : ch * h + y;
577 float *out = &s->combine_buffer[3 * row];
580 float a = g * w * magnitudes[y];
599 a = 1 + log10(av_clipd(a * w, 1e-6, 1)) / 6; // zero = -120dBFS
605 pick_color(s, yf, uf, vf, a, out);
609 av_frame_make_writable(s->outpicref);
611 if (s->orientation == VERTICAL) {
612 if (s->sliding == SCROLL) {
613 for (plane = 0; plane < 3; plane++) {
614 for (y = 0; y < outlink->h; y++) {
615 uint8_t *p = outpicref->data[plane] +
616 y * outpicref->linesize[plane];
617 memmove(p, p + 1, outlink->w - 1);
620 s->xpos = outlink->w - 1;
621 } else if (s->sliding == RSCROLL) {
622 for (plane = 0; plane < 3; plane++) {
623 for (y = 0; y < outlink->h; y++) {
624 uint8_t *p = outpicref->data[plane] +
625 y * outpicref->linesize[plane];
626 memmove(p + 1, p, outlink->w - 1);
631 for (plane = 0; plane < 3; plane++) {
632 uint8_t *p = outpicref->data[plane] +
633 (outlink->h - 1) * outpicref->linesize[plane] +
635 for (y = 0; y < outlink->h; y++) {
636 *p = lrintf(av_clipf(s->combine_buffer[3 * y + plane], 0, 255));
637 p -= outpicref->linesize[plane];
641 if (s->sliding == SCROLL) {
642 for (plane = 0; plane < 3; plane++) {
643 for (y = 1; y < outlink->h; y++) {
644 memmove(outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
645 outpicref->data[plane] + (y ) * outpicref->linesize[plane],
649 s->xpos = outlink->h - 1;
650 } else if (s->sliding == RSCROLL) {
651 for (plane = 0; plane < 3; plane++) {
652 for (y = outlink->h - 1; y >= 1; y--) {
653 memmove(outpicref->data[plane] + (y ) * outpicref->linesize[plane],
654 outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
660 for (plane = 0; plane < 3; plane++) {
661 uint8_t *p = outpicref->data[plane] +
662 s->xpos * outpicref->linesize[plane];
663 for (x = 0; x < outlink->w; x++) {
664 *p = lrintf(av_clipf(s->combine_buffer[3 * x + plane], 0, 255));
670 if (s->sliding != FULLFRAME || s->xpos == 0)
671 outpicref->pts = insamples->pts;
674 if (s->orientation == VERTICAL && s->xpos >= outlink->w)
676 if (s->orientation == HORIZONTAL && s->xpos >= outlink->h)
678 if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
679 ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
687 #if CONFIG_SHOWSPECTRUM_FILTER
689 static int request_frame(AVFilterLink *outlink)
691 ShowSpectrumContext *s = outlink->src->priv;
692 AVFilterLink *inlink = outlink->src->inputs[0];
696 ret = ff_request_frame(inlink);
697 if (ret == AVERROR_EOF && s->sliding == FULLFRAME && s->xpos > 0 &&
699 if (s->orientation == VERTICAL) {
700 for (i = 0; i < outlink->h; i++) {
701 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos);
702 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
703 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
706 for (i = s->xpos; i < outlink->h; i++) {
707 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w);
708 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
709 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
712 ret = ff_filter_frame(outlink, s->outpicref);
719 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
721 AVFilterContext *ctx = inlink->dst;
722 ShowSpectrumContext *s = ctx->priv;
726 av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
727 av_frame_free(&insamples);
728 while (av_audio_fifo_size(s->fifo) >= s->win_size) {
729 fin = ff_get_audio_buffer(inlink, s->win_size);
731 ret = AVERROR(ENOMEM);
736 s->pts += s->skip_samples;
737 ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
741 av_assert0(fin->nb_samples == s->win_size);
746 ret = plot_spectrum_column(inlink, fin);
748 av_audio_fifo_drain(s->fifo, s->skip_samples);
758 static const AVFilterPad showspectrum_inputs[] = {
761 .type = AVMEDIA_TYPE_AUDIO,
762 .filter_frame = filter_frame,
767 static const AVFilterPad showspectrum_outputs[] = {
770 .type = AVMEDIA_TYPE_VIDEO,
771 .config_props = config_output,
772 .request_frame = request_frame,
777 AVFilter ff_avf_showspectrum = {
778 .name = "showspectrum",
779 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
781 .query_formats = query_formats,
782 .priv_size = sizeof(ShowSpectrumContext),
783 .inputs = showspectrum_inputs,
784 .outputs = showspectrum_outputs,
785 .priv_class = &showspectrum_class,
787 #endif // CONFIG_SHOWSPECTRUM_FILTER
789 #if CONFIG_SHOWSPECTRUMPIC_FILTER
791 static const AVOption showspectrumpic_options[] = {
792 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
793 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
794 { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" },
795 { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
796 { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
797 { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" },
798 { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
799 { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
800 { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
801 { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
802 { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
803 { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
804 { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
805 { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
806 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" },
807 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
808 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
809 { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
810 { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
811 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
812 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
813 { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
814 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
815 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
816 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
817 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
818 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
819 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
820 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
821 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
822 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
823 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
824 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
825 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
826 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
827 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
828 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
829 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
830 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
831 { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
832 { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
833 { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
834 { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
838 AVFILTER_DEFINE_CLASS(showspectrumpic);
840 static int showspectrumpic_request_frame(AVFilterLink *outlink)
842 ShowSpectrumContext *s = outlink->src->priv;
843 AVFilterLink *inlink = outlink->src->inputs[0];
846 ret = ff_request_frame(inlink);
847 if (ret == AVERROR_EOF && s->outpicref) {
848 int samples = av_audio_fifo_size(s->fifo);
850 int x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
854 spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz))));
855 spb = (samples / (spf * sz)) * spf;
857 fin = ff_get_audio_buffer(inlink, s->win_size);
859 return AVERROR(ENOMEM);
862 ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
868 av_audio_fifo_drain(s->fifo, spf);
870 if (ret < s->win_size) {
871 for (ch = 0; ch < s->nb_display_channels; ch++) {
872 memset(fin->extended_data[ch] + ret * sizeof(float), 0,
873 (s->win_size - ret) * sizeof(float));
881 if (consumed >= spb) {
882 int h = s->orientation == VERTICAL ? s->h : s->w;
884 scale_magnitudes(s, 1. / (consumed / spf));
885 plot_spectrum_column(inlink, fin);
888 for (ch = 0; ch < s->nb_display_channels; ch++)
889 memset(s->magnitudes[ch], 0, h * sizeof(float));
894 s->outpicref->pts = 0;
895 ret = ff_filter_frame(outlink, s->outpicref);
902 static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
904 AVFilterContext *ctx = inlink->dst;
905 ShowSpectrumContext *s = ctx->priv;
908 ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
909 av_frame_free(&insamples);
913 static const AVFilterPad showspectrumpic_inputs[] = {
916 .type = AVMEDIA_TYPE_AUDIO,
917 .filter_frame = showspectrumpic_filter_frame,
922 static const AVFilterPad showspectrumpic_outputs[] = {
925 .type = AVMEDIA_TYPE_VIDEO,
926 .config_props = config_output,
927 .request_frame = showspectrumpic_request_frame,
932 AVFilter ff_avf_showspectrumpic = {
933 .name = "showspectrumpic",
934 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
936 .query_formats = query_formats,
937 .priv_size = sizeof(ShowSpectrumContext),
938 .inputs = showspectrumpic_inputs,
939 .outputs = showspectrumpic_outputs,
940 .priv_class = &showspectrumpic_class,
943 #endif // CONFIG_SHOWSPECTRUMPIC_FILTER