1 /*
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
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 /**
24 * @file
25 * audio to spectrum (video) transmedia filter, based on ffplay rdft showmode
26 * (by Michael Niedermayer) and lavfi/avf_showwaves (by Stefano Sabatini).
27 */
28
29 #include "config_components.h"
30
31 #include <float.h>
32 #include <math.h>
33
34 #include "libavutil/tx.h"
35 #include "libavutil/avassert.h"
36 #include "libavutil/avstring.h"
37 #include "libavutil/channel_layout.h"
38 #include "libavutil/cpu.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/parseutils.h"
41 #include "libavutil/xga_font_data.h"
42 #include "audio.h"
43 #include "video.h"
44 #include "avfilter.h"
45 #include "filters.h"
46 #include "internal.h"
47 #include "window_func.h"
48
49 enum DisplayMode { COMBINED, SEPARATE, NB_MODES };
50 enum DataMode { D_MAGNITUDE, D_PHASE, D_UPHASE, NB_DMODES };
51 enum FrequencyScale { F_LINEAR, F_LOG, NB_FSCALES };
52 enum DisplayScale { LINEAR, SQRT, CBRT, LOG, FOURTHRT, FIFTHRT, NB_SCALES };
53 enum ColorMode { CHANNEL, INTENSITY, RAINBOW, MORELAND, NEBULAE, FIRE, FIERY, FRUIT, COOL, MAGMA, GREEN, VIRIDIS, PLASMA, CIVIDIS, TERRAIN, NB_CLMODES };
54 enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, LREPLACE, NB_SLIDES };
55 enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS };
56
57 #define DEFAULT_LENGTH 300
58
59 typedef struct ShowSpectrumContext {
60 const AVClass *class;
61 int w, h;
62 char *rate_str;
63 AVRational auto_frame_rate;
64 AVRational frame_rate;
65 AVFrame *outpicref;
66 AVFrame *in_frame;
67 int nb_display_channels;
68 int orientation;
69 int channel_width;
70 int channel_height;
71 int sliding; ///< 1 if sliding mode, 0 otherwise
72 int mode; ///< channel display mode
73 int color_mode; ///< display color scheme
74 int scale;
75 int fscale;
76 float saturation; ///< color saturation multiplier
77 float rotation; ///< color rotation
78 int start, stop; ///< zoom mode
79 int data;
80 int xpos; ///< x position (current column)
81 AVTXContext **fft; ///< Fast Fourier Transform context
82 AVTXContext **ifft; ///< Inverse Fast Fourier Transform context
83 av_tx_fn tx_fn;
84 av_tx_fn itx_fn;
85 int fft_size; ///< number of coeffs (FFT window size)
86 AVComplexFloat **fft_in; ///< input FFT coeffs
87 AVComplexFloat **fft_data; ///< bins holder for each (displayed) channels
88 AVComplexFloat **fft_scratch;///< scratch buffers
89 float *window_func_lut; ///< Window function LUT
90 float **magnitudes;
91 float **phases;
92 int win_func;
93 int win_size;
94 int buf_size;
95 double win_scale;
96 float overlap;
97 float gain;
98 int hop_size;
99 float *combine_buffer; ///< color combining buffer (4 * h items)
100 float **color_buffer; ///< color buffer (4 * h * ch items)
101 int64_t pts;
102 int64_t old_pts;
103 int64_t in_pts;
104 int old_len;
105 int single_pic;
106 int legend;
107 int start_x, start_y;
108 float drange, limit;
109 float dmin, dmax;
110 uint64_t samples;
111 int (*plot_channel)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
112
113 float opacity_factor;
114
115 AVFrame **frames;
116 unsigned int nb_frames;
117 unsigned int frames_size;
118 } ShowSpectrumContext;
119
120 #define OFFSET(x) offsetof(ShowSpectrumContext, x)
121 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
122
123 static const AVOption showspectrum_options[] = {
124 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
125 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
126 { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
127 { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
128 { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
129 { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
130 { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
131 { "lreplace", "replace from right to left", 0, AV_OPT_TYPE_CONST, {.i64=LREPLACE}, 0, 0, FLAGS, "slide" },
132 { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
133 { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
134 { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
135 { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
136 { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
137 { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
138 { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
139 { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
140 { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
141 { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
142 { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
143 { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
144 { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
145 { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
146 { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
147 { "viridis", "viridis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=VIRIDIS}, 0, 0, FLAGS, "color" },
148 { "plasma", "plasma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=PLASMA}, 0, 0, FLAGS, "color" },
149 { "cividis", "cividis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=CIVIDIS}, 0, 0, FLAGS, "color" },
150 { "terrain", "terrain based coloring", 0, AV_OPT_TYPE_CONST, {.i64=TERRAIN}, 0, 0, FLAGS, "color" },
151 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
152 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
153 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
154 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
155 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
156 { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
157 { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
158 { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=F_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
159 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=F_LINEAR}, 0, 0, FLAGS, "fscale" },
160 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=F_LOG}, 0, 0, FLAGS, "fscale" },
161 { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
162 WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_HANNING),
163 { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
164 { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
165 { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
166 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS },
167 { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
168 { "data", "set data mode", OFFSET(data), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_DMODES-1, FLAGS, "data" },
169 { "magnitude", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_MAGNITUDE}, 0, 0, FLAGS, "data" },
170 { "phase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_PHASE}, 0, 0, FLAGS, "data" },
171 { "uphase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_UPHASE}, 0, 0, FLAGS, "data" },
172 { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
173 { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
174 { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
175 { "fps", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = "auto"}, 0, 0, FLAGS },
176 { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
177 { "drange", "set dynamic range in dBFS", OFFSET(drange), AV_OPT_TYPE_FLOAT, {.dbl = 120}, 10, 200, FLAGS },
178 { "limit", "set upper limit in dBFS", OFFSET(limit), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -100, 100, FLAGS },
179 { "opacity", "set opacity strength", OFFSET(opacity_factor), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 10, FLAGS },
180 { NULL }
181 };
182
183 AVFILTER_DEFINE_CLASS(showspectrum);
184
185 static const struct ColorTable {
186 float a, y, u, v;
187 } color_table[][8] = {
188 [INTENSITY] = {
189 { 0, 0, 0, 0 },
190 { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
191 { 0.30, .18572281794568020, .1772436246393981, .17475554840414750 },
192 { 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 },
193 { 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 },
194 { 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 },
195 { 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 },
196 { 1, 1, 0, 0 }},
197 [RAINBOW] = {
198 { 0, 0, 0, 0 },
199 { 0.13, 44/256., (189-128)/256., (138-128)/256. },
200 { 0.25, 29/256., (186-128)/256., (119-128)/256. },
201 { 0.38, 119/256., (194-128)/256., (53-128)/256. },
202 { 0.60, 111/256., (73-128)/256., (59-128)/256. },
203 { 0.73, 205/256., (19-128)/256., (149-128)/256. },
204 { 0.86, 135/256., (83-128)/256., (200-128)/256. },
205 { 1, 73/256., (95-128)/256., (225-128)/256. }},
206 [MORELAND] = {
207 { 0, 44/256., (181-128)/256., (112-128)/256. },
208 { 0.13, 126/256., (177-128)/256., (106-128)/256. },
209 { 0.25, 164/256., (163-128)/256., (109-128)/256. },
210 { 0.38, 200/256., (140-128)/256., (120-128)/256. },
211 { 0.60, 201/256., (117-128)/256., (141-128)/256. },
212 { 0.73, 177/256., (103-128)/256., (165-128)/256. },
213 { 0.86, 136/256., (100-128)/256., (183-128)/256. },
214 { 1, 68/256., (117-128)/256., (203-128)/256. }},
215 [NEBULAE] = {
216 { 0, 10/256., (134-128)/256., (132-128)/256. },
217 { 0.23, 21/256., (137-128)/256., (130-128)/256. },
218 { 0.45, 35/256., (134-128)/256., (134-128)/256. },
219 { 0.57, 51/256., (130-128)/256., (139-128)/256. },
220 { 0.67, 104/256., (116-128)/256., (162-128)/256. },
221 { 0.77, 120/256., (105-128)/256., (188-128)/256. },
222 { 0.87, 140/256., (105-128)/256., (188-128)/256. },
223 { 1, 1, 0, 0 }},
224 [FIRE] = {
225 { 0, 0, 0, 0 },
226 { 0.23, 44/256., (132-128)/256., (127-128)/256. },
227 { 0.45, 62/256., (116-128)/256., (140-128)/256. },
228 { 0.57, 75/256., (105-128)/256., (152-128)/256. },
229 { 0.67, 95/256., (91-128)/256., (166-128)/256. },
230 { 0.77, 126/256., (74-128)/256., (172-128)/256. },
231 { 0.87, 164/256., (73-128)/256., (162-128)/256. },
232 { 1, 1, 0, 0 }},
233 [FIERY] = {
234 { 0, 0, 0, 0 },
235 { 0.23, 36/256., (116-128)/256., (163-128)/256. },
236 { 0.45, 52/256., (102-128)/256., (200-128)/256. },
237 { 0.57, 116/256., (84-128)/256., (196-128)/256. },
238 { 0.67, 157/256., (67-128)/256., (181-128)/256. },
239 { 0.77, 193/256., (40-128)/256., (155-128)/256. },
240 { 0.87, 221/256., (101-128)/256., (134-128)/256. },
241 { 1, 1, 0, 0 }},
242 [FRUIT] = {
243 { 0, 0, 0, 0 },
244 { 0.20, 29/256., (136-128)/256., (119-128)/256. },
245 { 0.30, 60/256., (119-128)/256., (90-128)/256. },
246 { 0.40, 85/256., (91-128)/256., (85-128)/256. },
247 { 0.50, 116/256., (70-128)/256., (105-128)/256. },
248 { 0.60, 151/256., (50-128)/256., (146-128)/256. },
249 { 0.70, 191/256., (63-128)/256., (178-128)/256. },
250 { 1, 98/256., (80-128)/256., (221-128)/256. }},
251 [COOL] = {
252 { 0, 0, 0, 0 },
253 { .15, 0, .5, -.5 },
254 { 1, 1, -.5, .5 }},
255 [MAGMA] = {
256 { 0, 0, 0, 0 },
257 { 0.10, 23/256., (175-128)/256., (120-128)/256. },
258 { 0.23, 43/256., (158-128)/256., (144-128)/256. },
259 { 0.35, 85/256., (138-128)/256., (179-128)/256. },
260 { 0.48, 96/256., (128-128)/256., (189-128)/256. },
261 { 0.64, 128/256., (103-128)/256., (214-128)/256. },
262 { 0.92, 205/256., (80-128)/256., (152-128)/256. },
263 { 1, 1, 0, 0 }},
264 [GREEN] = {
265 { 0, 0, 0, 0 },
266 { .75, .5, 0, -.5 },
267 { 1, 1, 0, 0 }},
268 [VIRIDIS] = {
269 { 0, 0, 0, 0 },
270 { 0.10, 0x39/255., (0x9D -128)/255., (0x8F -128)/255. },
271 { 0.23, 0x5C/255., (0x9A -128)/255., (0x68 -128)/255. },
272 { 0.35, 0x69/255., (0x93 -128)/255., (0x57 -128)/255. },
273 { 0.48, 0x76/255., (0x88 -128)/255., (0x4B -128)/255. },
274 { 0.64, 0x8A/255., (0x72 -128)/255., (0x4F -128)/255. },
275 { 0.80, 0xA3/255., (0x50 -128)/255., (0x66 -128)/255. },
276 { 1, 0xCC/255., (0x2F -128)/255., (0x87 -128)/255. }},
277 [PLASMA] = {
278 { 0, 0, 0, 0 },
279 { 0.10, 0x27/255., (0xC2 -128)/255., (0x82 -128)/255. },
280 { 0.58, 0x5B/255., (0x9A -128)/255., (0xAE -128)/255. },
281 { 0.70, 0x89/255., (0x44 -128)/255., (0xAB -128)/255. },
282 { 0.80, 0xB4/255., (0x2B -128)/255., (0x9E -128)/255. },
283 { 0.91, 0xD2/255., (0x38 -128)/255., (0x92 -128)/255. },
284 { 1, 1, 0, 0. }},
285 [CIVIDIS] = {
286 { 0, 0, 0, 0 },
287 { 0.20, 0x28/255., (0x98 -128)/255., (0x6F -128)/255. },
288 { 0.50, 0x48/255., (0x95 -128)/255., (0x74 -128)/255. },
289 { 0.63, 0x69/255., (0x84 -128)/255., (0x7F -128)/255. },
290 { 0.76, 0x89/255., (0x75 -128)/255., (0x84 -128)/255. },
291 { 0.90, 0xCE/255., (0x35 -128)/255., (0x95 -128)/255. },
292 { 1, 1, 0, 0. }},
293 [TERRAIN] = {
294 { 0, 0, 0, 0 },
295 { 0.15, 0, .5, 0 },
296 { 0.60, 1, -.5, -.5 },
297 { 0.85, 1, -.5, .5 },
298 { 1, 1, 0, 0 }},
299 };
300
uninit(AVFilterContext * ctx)301 static av_cold void uninit(AVFilterContext *ctx)
302 {
303 ShowSpectrumContext *s = ctx->priv;
304 int i;
305
306 av_freep(&s->combine_buffer);
307 if (s->fft) {
308 for (i = 0; i < s->nb_display_channels; i++)
309 av_tx_uninit(&s->fft[i]);
310 }
311 av_freep(&s->fft);
312 if (s->ifft) {
313 for (i = 0; i < s->nb_display_channels; i++)
314 av_tx_uninit(&s->ifft[i]);
315 }
316 av_freep(&s->ifft);
317 if (s->fft_data) {
318 for (i = 0; i < s->nb_display_channels; i++)
319 av_freep(&s->fft_data[i]);
320 }
321 av_freep(&s->fft_data);
322 if (s->fft_in) {
323 for (i = 0; i < s->nb_display_channels; i++)
324 av_freep(&s->fft_in[i]);
325 }
326 av_freep(&s->fft_in);
327 if (s->fft_scratch) {
328 for (i = 0; i < s->nb_display_channels; i++)
329 av_freep(&s->fft_scratch[i]);
330 }
331 av_freep(&s->fft_scratch);
332 if (s->color_buffer) {
333 for (i = 0; i < s->nb_display_channels; i++)
334 av_freep(&s->color_buffer[i]);
335 }
336 av_freep(&s->color_buffer);
337 av_freep(&s->window_func_lut);
338 if (s->magnitudes) {
339 for (i = 0; i < s->nb_display_channels; i++)
340 av_freep(&s->magnitudes[i]);
341 }
342 av_freep(&s->magnitudes);
343 av_frame_free(&s->outpicref);
344 av_frame_free(&s->in_frame);
345 if (s->phases) {
346 for (i = 0; i < s->nb_display_channels; i++)
347 av_freep(&s->phases[i]);
348 }
349 av_freep(&s->phases);
350
351 while (s->nb_frames > 0) {
352 av_frame_free(&s->frames[s->nb_frames - 1]);
353 s->nb_frames--;
354 }
355
356 av_freep(&s->frames);
357 }
358
query_formats(AVFilterContext * ctx)359 static int query_formats(AVFilterContext *ctx)
360 {
361 AVFilterFormats *formats = NULL;
362 AVFilterChannelLayouts *layouts = NULL;
363 AVFilterLink *inlink = ctx->inputs[0];
364 AVFilterLink *outlink = ctx->outputs[0];
365 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
366 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE };
367 int ret;
368
369 /* set input audio formats */
370 formats = ff_make_format_list(sample_fmts);
371 if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0)
372 return ret;
373
374 layouts = ff_all_channel_counts();
375 if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
376 return ret;
377
378 formats = ff_all_samplerates();
379 if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
380 return ret;
381
382 /* set output video format */
383 formats = ff_make_format_list(pix_fmts);
384 if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
385 return ret;
386
387 return 0;
388 }
389
run_channel_fft(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)390 static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
391 {
392 ShowSpectrumContext *s = ctx->priv;
393 AVFilterLink *inlink = ctx->inputs[0];
394 const float *window_func_lut = s->window_func_lut;
395 AVFrame *fin = arg;
396 const int ch = jobnr;
397 int n;
398
399 /* fill FFT input with the number of samples available */
400 const float *p = (float *)fin->extended_data[ch];
401 float *in_frame = (float *)s->in_frame->extended_data[ch];
402
403 memmove(in_frame, in_frame + s->hop_size, (s->fft_size - s->hop_size) * sizeof(float));
404 memcpy(in_frame + s->fft_size - s->hop_size, p, fin->nb_samples * sizeof(float));
405
406 for (int i = fin->nb_samples; i < s->hop_size; i++)
407 in_frame[i + s->fft_size - s->hop_size] = 0.f;
408
409 if (s->stop) {
410 float theta, phi, psi, a, b, S, c;
411 AVComplexFloat *f = s->fft_in[ch];
412 AVComplexFloat *g = s->fft_data[ch];
413 AVComplexFloat *h = s->fft_scratch[ch];
414 int L = s->buf_size;
415 int N = s->win_size;
416 int M = s->win_size / 2;
417
418 for (n = 0; n < s->win_size; n++) {
419 s->fft_data[ch][n].re = in_frame[n] * window_func_lut[n];
420 s->fft_data[ch][n].im = 0;
421 }
422
423 phi = 2.f * M_PI * (s->stop - s->start) / (float)inlink->sample_rate / (M - 1);
424 theta = 2.f * M_PI * s->start / (float)inlink->sample_rate;
425
426 for (int n = 0; n < M; n++) {
427 h[n].re = cosf(n * n / 2.f * phi);
428 h[n].im = sinf(n * n / 2.f * phi);
429 }
430
431 for (int n = M; n < L; n++) {
432 h[n].re = 0.f;
433 h[n].im = 0.f;
434 }
435
436 for (int n = L - N; n < L; n++) {
437 h[n].re = cosf((L - n) * (L - n) / 2.f * phi);
438 h[n].im = sinf((L - n) * (L - n) / 2.f * phi);
439 }
440
441 for (int n = N; n < L; n++) {
442 g[n].re = 0.f;
443 g[n].im = 0.f;
444 }
445
446 for (int n = 0; n < N; n++) {
447 psi = n * theta + n * n / 2.f * phi;
448 c = cosf(psi);
449 S = -sinf(psi);
450 a = c * g[n].re - S * g[n].im;
451 b = S * g[n].re + c * g[n].im;
452 g[n].re = a;
453 g[n].im = b;
454 }
455
456 memcpy(f, h, s->buf_size * sizeof(*f));
457 s->tx_fn(s->fft[ch], h, f, sizeof(float));
458
459 memcpy(f, g, s->buf_size * sizeof(*f));
460 s->tx_fn(s->fft[ch], g, f, sizeof(float));
461
462 for (int n = 0; n < L; n++) {
463 c = g[n].re;
464 S = g[n].im;
465 a = c * h[n].re - S * h[n].im;
466 b = S * h[n].re + c * h[n].im;
467
468 g[n].re = a / L;
469 g[n].im = b / L;
470 }
471
472 memcpy(f, g, s->buf_size * sizeof(*f));
473 s->itx_fn(s->ifft[ch], g, f, sizeof(float));
474
475 for (int k = 0; k < M; k++) {
476 psi = k * k / 2.f * phi;
477 c = cosf(psi);
478 S = -sinf(psi);
479 a = c * g[k].re - S * g[k].im;
480 b = S * g[k].re + c * g[k].im;
481 s->fft_data[ch][k].re = a;
482 s->fft_data[ch][k].im = b;
483 }
484 } else {
485 for (n = 0; n < s->win_size; n++) {
486 s->fft_in[ch][n].re = in_frame[n] * window_func_lut[n];
487 s->fft_in[ch][n].im = 0;
488 }
489
490 /* run FFT on each samples set */
491 s->tx_fn(s->fft[ch], s->fft_data[ch], s->fft_in[ch], sizeof(float));
492 }
493
494 return 0;
495 }
496
drawtext(AVFrame * pic,int x,int y,const char * txt,int o)497 static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
498 {
499 const uint8_t *font;
500 int font_height;
501
502 font = avpriv_cga_font, font_height = 8;
503
504 for (int i = 0; txt[i]; i++) {
505 int char_y, mask;
506
507 if (o) {
508 for (char_y = font_height - 1; char_y >= 0; char_y--) {
509 uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x;
510 for (mask = 0x80; mask; mask >>= 1) {
511 if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
512 p[char_y] = ~p[char_y];
513 p += pic->linesize[0];
514 }
515 }
516 } else {
517 uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8);
518 for (char_y = 0; char_y < font_height; char_y++) {
519 for (mask = 0x80; mask; mask >>= 1) {
520 if (font[txt[i] * font_height + char_y] & mask)
521 *p = ~(*p);
522 p++;
523 }
524 p += pic->linesize[0] - 8;
525 }
526 }
527 }
528
529 for (int i = 0; txt[i] && pic->data[3]; i++) {
530 int char_y, mask;
531
532 if (o) {
533 for (char_y = font_height - 1; char_y >= 0; char_y--) {
534 uint8_t *p = pic->data[3] + (y + i * 10) * pic->linesize[3] + x;
535 for (mask = 0x80; mask; mask >>= 1) {
536 for (int k = 0; k < 8; k++)
537 p[k] = 255;
538 p += pic->linesize[3];
539 }
540 }
541 } else {
542 uint8_t *p = pic->data[3] + y*pic->linesize[3] + (x + i*8);
543 for (char_y = 0; char_y < font_height; char_y++) {
544 for (mask = 0x80; mask; mask >>= 1)
545 *p++ = 255;
546 p += pic->linesize[3] - 8;
547 }
548 }
549 }
550 }
551
color_range(ShowSpectrumContext * s,int ch,float * yf,float * uf,float * vf)552 static void color_range(ShowSpectrumContext *s, int ch,
553 float *yf, float *uf, float *vf)
554 {
555 switch (s->mode) {
556 case COMBINED:
557 // reduce range by channel count
558 *yf = 256.0f / s->nb_display_channels;
559 switch (s->color_mode) {
560 case RAINBOW:
561 case MORELAND:
562 case NEBULAE:
563 case FIRE:
564 case FIERY:
565 case FRUIT:
566 case COOL:
567 case GREEN:
568 case VIRIDIS:
569 case PLASMA:
570 case CIVIDIS:
571 case TERRAIN:
572 case MAGMA:
573 case INTENSITY:
574 *uf = *yf;
575 *vf = *yf;
576 break;
577 case CHANNEL:
578 /* adjust saturation for mixed UV coloring */
579 /* this factor is correct for infinite channels, an approximation otherwise */
580 *uf = *yf * M_PI;
581 *vf = *yf * M_PI;
582 break;
583 default:
584 av_assert0(0);
585 }
586 break;
587 case SEPARATE:
588 // full range
589 *yf = 256.0f;
590 *uf = 256.0f;
591 *vf = 256.0f;
592 break;
593 default:
594 av_assert0(0);
595 }
596
597 if (s->color_mode == CHANNEL) {
598 if (s->nb_display_channels > 1) {
599 *uf *= 0.5f * sinf((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
600 *vf *= 0.5f * cosf((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
601 } else {
602 *uf *= 0.5f * sinf(M_PI * s->rotation);
603 *vf *= 0.5f * cosf(M_PI * s->rotation + M_PI_2);
604 }
605 } else {
606 *uf += *uf * sinf(M_PI * s->rotation);
607 *vf += *vf * cosf(M_PI * s->rotation + M_PI_2);
608 }
609
610 *uf *= s->saturation;
611 *vf *= s->saturation;
612 }
613
pick_color(ShowSpectrumContext * s,float yf,float uf,float vf,float a,float * out)614 static void pick_color(ShowSpectrumContext *s,
615 float yf, float uf, float vf,
616 float a, float *out)
617 {
618 const float af = s->opacity_factor * 255.f;
619
620 if (s->color_mode > CHANNEL) {
621 const int cm = s->color_mode;
622 float y, u, v;
623 int i;
624
625 for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
626 if (color_table[cm][i].a >= a)
627 break;
628 // i now is the first item >= the color
629 // now we know to interpolate between item i - 1 and i
630 if (a <= color_table[cm][i - 1].a) {
631 y = color_table[cm][i - 1].y;
632 u = color_table[cm][i - 1].u;
633 v = color_table[cm][i - 1].v;
634 } else if (a >= color_table[cm][i].a) {
635 y = color_table[cm][i].y;
636 u = color_table[cm][i].u;
637 v = color_table[cm][i].v;
638 } else {
639 float start = color_table[cm][i - 1].a;
640 float end = color_table[cm][i].a;
641 float lerpfrac = (a - start) / (end - start);
642 y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
643 + color_table[cm][i].y * lerpfrac;
644 u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
645 + color_table[cm][i].u * lerpfrac;
646 v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
647 + color_table[cm][i].v * lerpfrac;
648 }
649
650 out[0] = y * yf;
651 out[1] = u * uf;
652 out[2] = v * vf;
653 out[3] = a * af;
654 } else {
655 out[0] = a * yf;
656 out[1] = a * uf;
657 out[2] = a * vf;
658 out[3] = a * af;
659 }
660 }
661
get_time(AVFilterContext * ctx,float seconds,int x)662 static char *get_time(AVFilterContext *ctx, float seconds, int x)
663 {
664 char *units;
665
666 if (x == 0)
667 units = av_asprintf("0");
668 else if (log10(seconds) > 6)
669 units = av_asprintf("%.2fh", seconds / (60 * 60));
670 else if (log10(seconds) > 3)
671 units = av_asprintf("%.2fm", seconds / 60);
672 else
673 units = av_asprintf("%.2fs", seconds);
674 return units;
675 }
676
log_scale(const float bin,const float bmin,const float bmax,const float min,const float max)677 static float log_scale(const float bin,
678 const float bmin, const float bmax,
679 const float min, const float max)
680 {
681 return exp2f(((bin - bmin) / (bmax - bmin)) * (log2f(max) - log2f(min)) + log2f(min));
682 }
683
get_hz(const float bin,const float bmax,const float min,const float max,int fscale)684 static float get_hz(const float bin, const float bmax,
685 const float min, const float max,
686 int fscale)
687 {
688 switch (fscale) {
689 case F_LINEAR:
690 return min + (bin / bmax) * (max - min);
691 case F_LOG:
692 return min + log_scale(bin, 0, bmax, 20.f, max - min);
693 default:
694 return 0.f;
695 }
696 }
697
inv_log_scale(float bin,float bmin,float bmax,float min,float max)698 static float inv_log_scale(float bin,
699 float bmin, float bmax,
700 float min, float max)
701 {
702 return (min * exp2f((bin * (log2f(max) - log2f(20.f))) / bmax) + min) * bmax / max;
703 }
704
bin_pos(const int bin,const int num_bins,const float min,const float max)705 static float bin_pos(const int bin, const int num_bins, const float min, const float max)
706 {
707 return inv_log_scale(bin, 0.f, num_bins, 20.f, max - min);
708 }
709
get_scale(AVFilterContext * ctx,int scale,float a)710 static float get_scale(AVFilterContext *ctx, int scale, float a)
711 {
712 ShowSpectrumContext *s = ctx->priv;
713 const float dmin = s->dmin;
714 const float dmax = s->dmax;
715
716 a = av_clipf(a, dmin, dmax);
717 if (scale != LOG)
718 a = (a - dmin) / (dmax - dmin);
719
720 switch (scale) {
721 case LINEAR:
722 break;
723 case SQRT:
724 a = sqrtf(a);
725 break;
726 case CBRT:
727 a = cbrtf(a);
728 break;
729 case FOURTHRT:
730 a = sqrtf(sqrtf(a));
731 break;
732 case FIFTHRT:
733 a = powf(a, 0.2f);
734 break;
735 case LOG:
736 a = (s->drange - s->limit + log10f(a) * 20.f) / s->drange;
737 break;
738 default:
739 av_assert0(0);
740 }
741
742 return a;
743 }
744
get_iscale(AVFilterContext * ctx,int scale,float a)745 static float get_iscale(AVFilterContext *ctx, int scale, float a)
746 {
747 ShowSpectrumContext *s = ctx->priv;
748 const float dmin = s->dmin;
749 const float dmax = s->dmax;
750
751 switch (scale) {
752 case LINEAR:
753 break;
754 case SQRT:
755 a = a * a;
756 break;
757 case CBRT:
758 a = a * a * a;
759 break;
760 case FOURTHRT:
761 a = a * a * a * a;
762 break;
763 case FIFTHRT:
764 a = a * a * a * a * a;
765 break;
766 case LOG:
767 a = expf(M_LN10 * (a * s->drange - s->drange + s->limit) / 20.f);
768 break;
769 default:
770 av_assert0(0);
771 }
772
773 if (scale != LOG)
774 a = a * (dmax - dmin) + dmin;
775
776 return a;
777 }
778
draw_legend(AVFilterContext * ctx,uint64_t samples)779 static int draw_legend(AVFilterContext *ctx, uint64_t samples)
780 {
781 ShowSpectrumContext *s = ctx->priv;
782 AVFilterLink *inlink = ctx->inputs[0];
783 AVFilterLink *outlink = ctx->outputs[0];
784 int ch, y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
785 int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL);
786 float spp = samples / (float)sz;
787 char *text;
788 uint8_t *dst;
789 char chlayout_str[128];
790
791 av_channel_layout_describe(&inlink->ch_layout, chlayout_str, sizeof(chlayout_str));
792
793 text = av_asprintf("%d Hz | %s", inlink->sample_rate, chlayout_str);
794 if (!text)
795 return AVERROR(ENOMEM);
796
797 drawtext(s->outpicref, 2, outlink->h - 10, "CREATED BY LIBAVFILTER", 0);
798 drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, outlink->h - 10, text, 0);
799 av_freep(&text);
800 if (s->stop) {
801 text = av_asprintf("Zoom: %d Hz - %d Hz", s->start, s->stop);
802 if (!text)
803 return AVERROR(ENOMEM);
804 drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, 3, text, 0);
805 av_freep(&text);
806 }
807
808 dst = s->outpicref->data[0] + (s->start_y - 1) * s->outpicref->linesize[0] + s->start_x - 1;
809 for (x = 0; x < s->w + 1; x++)
810 dst[x] = 200;
811 dst = s->outpicref->data[0] + (s->start_y + s->h) * s->outpicref->linesize[0] + s->start_x - 1;
812 for (x = 0; x < s->w + 1; x++)
813 dst[x] = 200;
814 for (y = 0; y < s->h + 2; y++) {
815 dst = s->outpicref->data[0] + (y + s->start_y - 1) * s->outpicref->linesize[0];
816 dst[s->start_x - 1] = 200;
817 dst[s->start_x + s->w] = 200;
818 }
819 if (s->orientation == VERTICAL) {
820 int h = s->mode == SEPARATE ? s->h / s->nb_display_channels : s->h;
821 int hh = s->mode == SEPARATE ? -(s->h % s->nb_display_channels) + 1 : 1;
822 for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
823 for (y = 0; y < h; y += 20) {
824 dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
825 dst[s->start_x - 2] = 200;
826 dst[s->start_x + s->w + 1] = 200;
827 }
828 for (y = 0; y < h; y += 40) {
829 dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
830 dst[s->start_x - 3] = 200;
831 dst[s->start_x + s->w + 2] = 200;
832 }
833 dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x;
834 for (x = 0; x < s->w; x+=40)
835 dst[x] = 200;
836 dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x;
837 for (x = 0; x < s->w; x+=80)
838 dst[x] = 200;
839 dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x;
840 for (x = 0; x < s->w; x+=40) {
841 dst[x] = 200;
842 }
843 dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x;
844 for (x = 0; x < s->w; x+=80) {
845 dst[x] = 200;
846 }
847 for (y = 0; y < h; y += 40) {
848 float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
849 float hertz = get_hz(y, h, s->start, s->start + range, s->fscale);
850 char *units;
851
852 if (hertz == 0)
853 units = av_asprintf("DC");
854 else
855 units = av_asprintf("%.2f", hertz);
856 if (!units)
857 return AVERROR(ENOMEM);
858
859 drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4 - hh, units, 0);
860 av_free(units);
861 }
862 }
863
864 for (x = 0; x < s->w && s->single_pic; x+=80) {
865 float seconds = x * spp / inlink->sample_rate;
866 char *units = get_time(ctx, seconds, x);
867 if (!units)
868 return AVERROR(ENOMEM);
869
870 drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->h + s->start_y + 6, units, 0);
871 drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->start_y - 12, units, 0);
872 av_free(units);
873 }
874
875 drawtext(s->outpicref, outlink->w / 2 - 4 * 4, outlink->h - s->start_y / 2, "TIME", 0);
876 drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 14 * 4, "FREQUENCY (Hz)", 1);
877 } else {
878 int w = s->mode == SEPARATE ? s->w / s->nb_display_channels : s->w;
879 for (y = 0; y < s->h; y += 20) {
880 dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
881 dst[s->start_x - 2] = 200;
882 dst[s->start_x + s->w + 1] = 200;
883 }
884 for (y = 0; y < s->h; y += 40) {
885 dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
886 dst[s->start_x - 3] = 200;
887 dst[s->start_x + s->w + 2] = 200;
888 }
889 for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
890 dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
891 for (x = 0; x < w; x+=40)
892 dst[x] = 200;
893 dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x + w * ch;
894 for (x = 0; x < w; x+=80)
895 dst[x] = 200;
896 dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x + w * ch;
897 for (x = 0; x < w; x+=40) {
898 dst[x] = 200;
899 }
900 dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
901 for (x = 0; x < w; x+=80) {
902 dst[x] = 200;
903 }
904 for (x = 0; x < w - 79; x += 80) {
905 float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
906 float hertz = get_hz(x, w, s->start, s->start + range, s->fscale);
907 char *units;
908
909 if (hertz == 0)
910 units = av_asprintf("DC");
911 else
912 units = av_asprintf("%.2f", hertz);
913 if (!units)
914 return AVERROR(ENOMEM);
915
916 drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->start_y - 12, units, 0);
917 drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->h + s->start_y + 6, units, 0);
918 av_free(units);
919 }
920 }
921 for (y = 0; y < s->h && s->single_pic; y+=40) {
922 float seconds = y * spp / inlink->sample_rate;
923 char *units = get_time(ctx, seconds, x);
924 if (!units)
925 return AVERROR(ENOMEM);
926
927 drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0);
928 av_free(units);
929 }
930 drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 4 * 4, "TIME", 1);
931 drawtext(s->outpicref, outlink->w / 2 - 14 * 4, outlink->h - s->start_y / 2, "FREQUENCY (Hz)", 0);
932 }
933
934 for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) {
935 int h = multi ? s->h / s->nb_display_channels : s->h;
936
937 for (y = 0; y < h; y++) {
938 float out[4] = { 0., 127.5, 127.5, 0.f};
939 int chn;
940
941 for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) {
942 float yf, uf, vf;
943 int channel = (multi) ? s->nb_display_channels - ch - 1 : chn;
944 float lout[4];
945
946 color_range(s, channel, &yf, &uf, &vf);
947 pick_color(s, yf, uf, vf, y / (float)h, lout);
948 out[0] += lout[0];
949 out[1] += lout[1];
950 out[2] += lout[2];
951 out[3] += lout[3];
952 }
953 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);
954 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);
955 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);
956 if (s->outpicref->data[3])
957 memset(s->outpicref->data[3]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[3] + s->w + s->start_x + 20, av_clip_uint8(out[3]), 10);
958 }
959
960 for (y = 0; ch == 0 && y < h + 5; y += 25) {
961 static const char *log_fmt = "%.0f";
962 static const char *lin_fmt = "%.3f";
963 const float a = av_clipf(1.f - y / (float)(h - 1), 0.f, 1.f);
964 const float value = s->scale == LOG ? log10f(get_iscale(ctx, s->scale, a)) * 20.f : get_iscale(ctx, s->scale, a);
965 char *text;
966
967 text = av_asprintf(s->scale == LOG ? log_fmt : lin_fmt, value);
968 if (!text)
969 continue;
970 drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 3, text, 0);
971 av_free(text);
972 }
973 }
974
975 if (s->scale == LOG)
976 drawtext(s->outpicref, s->w + s->start_x + 22, s->start_y + s->h + 20, "dBFS", 0);
977
978 return 0;
979 }
980
get_value(AVFilterContext * ctx,int ch,int y)981 static float get_value(AVFilterContext *ctx, int ch, int y)
982 {
983 ShowSpectrumContext *s = ctx->priv;
984 float *magnitudes = s->magnitudes[ch];
985 float *phases = s->phases[ch];
986 float a;
987
988 switch (s->data) {
989 case D_MAGNITUDE:
990 /* get magnitude */
991 a = magnitudes[y];
992 break;
993 case D_UPHASE:
994 case D_PHASE:
995 /* get phase */
996 a = phases[y];
997 break;
998 default:
999 av_assert0(0);
1000 }
1001
1002 return av_clipf(get_scale(ctx, s->scale, a), 0.f, 1.f);
1003 }
1004
plot_channel_lin(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)1005 static int plot_channel_lin(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1006 {
1007 ShowSpectrumContext *s = ctx->priv;
1008 const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
1009 const int ch = jobnr;
1010 float yf, uf, vf;
1011 int y;
1012
1013 /* decide color range */
1014 color_range(s, ch, &yf, &uf, &vf);
1015
1016 /* draw the channel */
1017 for (y = 0; y < h; y++) {
1018 int row = (s->mode == COMBINED) ? y : ch * h + y;
1019 float *out = &s->color_buffer[ch][4 * row];
1020 float a = get_value(ctx, ch, y);
1021
1022 pick_color(s, yf, uf, vf, a, out);
1023 }
1024
1025 return 0;
1026 }
1027
plot_channel_log(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)1028 static int plot_channel_log(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1029 {
1030 ShowSpectrumContext *s = ctx->priv;
1031 AVFilterLink *inlink = ctx->inputs[0];
1032 const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
1033 const int ch = jobnr;
1034 float yf, uf, vf;
1035
1036 /* decide color range */
1037 color_range(s, ch, &yf, &uf, &vf);
1038
1039 /* draw the channel */
1040 for (int yy = 0; yy < h; yy++) {
1041 float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
1042 float pos = bin_pos(yy, h, s->start, s->start + range);
1043 float delta = pos - floorf(pos);
1044 float a0, a1;
1045
1046 a0 = get_value(ctx, ch, av_clip(pos, 0, h-1));
1047 a1 = get_value(ctx, ch, av_clip(pos+1, 0, h-1));
1048 {
1049 int row = (s->mode == COMBINED) ? yy : ch * h + yy;
1050 float *out = &s->color_buffer[ch][4 * row];
1051
1052 pick_color(s, yf, uf, vf, delta * a1 + (1.f - delta) * a0, out);
1053 }
1054 }
1055
1056 return 0;
1057 }
1058
config_output(AVFilterLink * outlink)1059 static int config_output(AVFilterLink *outlink)
1060 {
1061 AVFilterContext *ctx = outlink->src;
1062 AVFilterLink *inlink = ctx->inputs[0];
1063 ShowSpectrumContext *s = ctx->priv;
1064 int i, fft_size, h, w, ret;
1065 float overlap;
1066
1067 s->old_pts = AV_NOPTS_VALUE;
1068 s->dmax = expf(s->limit * M_LN10 / 20.f);
1069 s->dmin = expf((s->limit - s->drange) * M_LN10 / 20.f);
1070
1071 switch (s->fscale) {
1072 case F_LINEAR: s->plot_channel = plot_channel_lin; break;
1073 case F_LOG: s->plot_channel = plot_channel_log; break;
1074 default: return AVERROR_BUG;
1075 }
1076
1077 s->stop = FFMIN(s->stop, inlink->sample_rate / 2);
1078 if ((s->stop || s->start) && s->stop <= s->start) {
1079 av_log(ctx, AV_LOG_ERROR, "Stop frequency should be greater than start.\n");
1080 return AVERROR(EINVAL);
1081 }
1082
1083 if (!strcmp(ctx->filter->name, "showspectrumpic"))
1084 s->single_pic = 1;
1085
1086 outlink->w = s->w;
1087 outlink->h = s->h;
1088 outlink->sample_aspect_ratio = (AVRational){1,1};
1089
1090 if (s->legend) {
1091 s->start_x = (log10(inlink->sample_rate) + 1) * 25;
1092 s->start_y = 64;
1093 outlink->w += s->start_x * 2;
1094 outlink->h += s->start_y * 2;
1095 }
1096
1097 h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? s->h : s->h / inlink->ch_layout.nb_channels;
1098 w = (s->mode == COMBINED || s->orientation == VERTICAL) ? s->w : s->w / inlink->ch_layout.nb_channels;
1099 s->channel_height = h;
1100 s->channel_width = w;
1101
1102 if (s->orientation == VERTICAL) {
1103 /* FFT window size (precision) according to the requested output frame height */
1104 fft_size = h * 2;
1105 } else {
1106 /* FFT window size (precision) according to the requested output frame width */
1107 fft_size = w * 2;
1108 }
1109
1110 s->win_size = fft_size;
1111 s->buf_size = FFALIGN(s->win_size << (!!s->stop), av_cpu_max_align());
1112
1113 if (!s->fft) {
1114 s->fft = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->fft));
1115 if (!s->fft)
1116 return AVERROR(ENOMEM);
1117 }
1118
1119 if (s->stop) {
1120 if (!s->ifft) {
1121 s->ifft = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->ifft));
1122 if (!s->ifft)
1123 return AVERROR(ENOMEM);
1124 }
1125 }
1126
1127 /* (re-)configuration if the video output changed (or first init) */
1128 if (fft_size != s->fft_size) {
1129 AVFrame *outpicref;
1130
1131 s->fft_size = fft_size;
1132
1133 /* FFT buffers: x2 for each (display) channel buffer.
1134 * Note: we use free and malloc instead of a realloc-like function to
1135 * make sure the buffer is aligned in memory for the FFT functions. */
1136 for (i = 0; i < s->nb_display_channels; i++) {
1137 if (s->stop) {
1138 av_tx_uninit(&s->ifft[i]);
1139 av_freep(&s->fft_scratch[i]);
1140 }
1141 av_tx_uninit(&s->fft[i]);
1142 av_freep(&s->fft_in[i]);
1143 av_freep(&s->fft_data[i]);
1144 }
1145 av_freep(&s->fft_data);
1146
1147 s->nb_display_channels = inlink->ch_layout.nb_channels;
1148 for (i = 0; i < s->nb_display_channels; i++) {
1149 float scale;
1150
1151 ret = av_tx_init(&s->fft[i], &s->tx_fn, AV_TX_FLOAT_FFT, 0, fft_size << (!!s->stop), &scale, 0);
1152 if (s->stop) {
1153 ret = av_tx_init(&s->ifft[i], &s->itx_fn, AV_TX_FLOAT_FFT, 1, fft_size << (!!s->stop), &scale, 0);
1154 if (ret < 0) {
1155 av_log(ctx, AV_LOG_ERROR, "Unable to create Inverse FFT context. "
1156 "The window size might be too high.\n");
1157 return ret;
1158 }
1159 }
1160 if (ret < 0) {
1161 av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
1162 "The window size might be too high.\n");
1163 return ret;
1164 }
1165 }
1166
1167 s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
1168 if (!s->magnitudes)
1169 return AVERROR(ENOMEM);
1170 for (i = 0; i < s->nb_display_channels; i++) {
1171 s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
1172 if (!s->magnitudes[i])
1173 return AVERROR(ENOMEM);
1174 }
1175
1176 s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases));
1177 if (!s->phases)
1178 return AVERROR(ENOMEM);
1179 for (i = 0; i < s->nb_display_channels; i++) {
1180 s->phases[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->phases));
1181 if (!s->phases[i])
1182 return AVERROR(ENOMEM);
1183 }
1184
1185 av_freep(&s->color_buffer);
1186 s->color_buffer = av_calloc(s->nb_display_channels, sizeof(*s->color_buffer));
1187 if (!s->color_buffer)
1188 return AVERROR(ENOMEM);
1189 for (i = 0; i < s->nb_display_channels; i++) {
1190 s->color_buffer[i] = av_calloc(s->orientation == VERTICAL ? s->h * 4 : s->w * 4, sizeof(**s->color_buffer));
1191 if (!s->color_buffer[i])
1192 return AVERROR(ENOMEM);
1193 }
1194
1195 s->fft_in = av_calloc(s->nb_display_channels, sizeof(*s->fft_in));
1196 if (!s->fft_in)
1197 return AVERROR(ENOMEM);
1198 s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
1199 if (!s->fft_data)
1200 return AVERROR(ENOMEM);
1201 s->fft_scratch = av_calloc(s->nb_display_channels, sizeof(*s->fft_scratch));
1202 if (!s->fft_scratch)
1203 return AVERROR(ENOMEM);
1204 for (i = 0; i < s->nb_display_channels; i++) {
1205 s->fft_in[i] = av_calloc(s->buf_size, sizeof(**s->fft_in));
1206 if (!s->fft_in[i])
1207 return AVERROR(ENOMEM);
1208
1209 s->fft_data[i] = av_calloc(s->buf_size, sizeof(**s->fft_data));
1210 if (!s->fft_data[i])
1211 return AVERROR(ENOMEM);
1212
1213 s->fft_scratch[i] = av_calloc(s->buf_size, sizeof(**s->fft_scratch));
1214 if (!s->fft_scratch[i])
1215 return AVERROR(ENOMEM);
1216 }
1217
1218 /* pre-calc windowing function */
1219 s->window_func_lut =
1220 av_realloc_f(s->window_func_lut, s->win_size,
1221 sizeof(*s->window_func_lut));
1222 if (!s->window_func_lut)
1223 return AVERROR(ENOMEM);
1224 generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
1225 if (s->overlap == 1)
1226 s->overlap = overlap;
1227 s->hop_size = (1.f - s->overlap) * s->win_size;
1228 if (s->hop_size < 1) {
1229 av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
1230 return AVERROR(EINVAL);
1231 }
1232
1233 for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
1234 s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
1235 }
1236 s->win_scale = 1.f / sqrtf(s->win_scale);
1237
1238 /* prepare the initial picref buffer (black frame) */
1239 av_frame_free(&s->outpicref);
1240 s->outpicref = outpicref =
1241 ff_get_video_buffer(outlink, outlink->w, outlink->h);
1242 if (!outpicref)
1243 return AVERROR(ENOMEM);
1244 outpicref->sample_aspect_ratio = (AVRational){1,1};
1245 for (i = 0; i < outlink->h; i++) {
1246 memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
1247 memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
1248 memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
1249 if (outpicref->data[3])
1250 memset(outpicref->data[3] + i * outpicref->linesize[3], 0, outlink->w);
1251 }
1252 outpicref->color_range = AVCOL_RANGE_JPEG;
1253
1254 if (!s->single_pic && s->legend)
1255 draw_legend(ctx, 0);
1256 }
1257
1258 if ((s->orientation == VERTICAL && s->xpos >= s->w) ||
1259 (s->orientation == HORIZONTAL && s->xpos >= s->h))
1260 s->xpos = 0;
1261
1262 if (s->sliding == LREPLACE) {
1263 if (s->orientation == VERTICAL)
1264 s->xpos = s->w - 1;
1265 if (s->orientation == HORIZONTAL)
1266 s->xpos = s->h - 1;
1267 }
1268
1269 s->auto_frame_rate = av_make_q(inlink->sample_rate, s->hop_size);
1270 if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
1271 s->auto_frame_rate = av_mul_q(s->auto_frame_rate, av_make_q(1, s->w));
1272 if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
1273 s->auto_frame_rate = av_mul_q(s->auto_frame_rate, av_make_q(1, s->h));
1274 if (!s->single_pic && strcmp(s->rate_str, "auto")) {
1275 int ret = av_parse_video_rate(&s->frame_rate, s->rate_str);
1276 if (ret < 0)
1277 return ret;
1278 } else if (s->single_pic) {
1279 s->frame_rate = av_make_q(1, 1);
1280 } else {
1281 s->frame_rate = s->auto_frame_rate;
1282 }
1283 outlink->frame_rate = s->frame_rate;
1284 outlink->time_base = av_inv_q(outlink->frame_rate);
1285
1286 if (s->orientation == VERTICAL) {
1287 s->combine_buffer =
1288 av_realloc_f(s->combine_buffer, s->h * 4,
1289 sizeof(*s->combine_buffer));
1290 } else {
1291 s->combine_buffer =
1292 av_realloc_f(s->combine_buffer, s->w * 4,
1293 sizeof(*s->combine_buffer));
1294 }
1295
1296 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n",
1297 s->w, s->h, s->win_size);
1298
1299 s->in_frame = ff_get_audio_buffer(inlink, s->win_size);
1300 if (!s->in_frame)
1301 return AVERROR(ENOMEM);
1302
1303 s->frames = av_fast_realloc(NULL, &s->frames_size,
1304 DEFAULT_LENGTH * sizeof(*(s->frames)));
1305 if (!s->frames)
1306 return AVERROR(ENOMEM);
1307
1308 return 0;
1309 }
1310
1311 #define RE(y, ch) s->fft_data[ch][y].re
1312 #define IM(y, ch) s->fft_data[ch][y].im
1313 #define MAGNITUDE(y, ch) hypotf(RE(y, ch), IM(y, ch))
1314 #define PHASE(y, ch) atan2f(IM(y, ch), RE(y, ch))
1315
calc_channel_magnitudes(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)1316 static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1317 {
1318 ShowSpectrumContext *s = ctx->priv;
1319 const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
1320 int y, h = s->orientation == VERTICAL ? s->h : s->w;
1321 const float f = s->gain * w;
1322 const int ch = jobnr;
1323 float *magnitudes = s->magnitudes[ch];
1324
1325 for (y = 0; y < h; y++)
1326 magnitudes[y] = MAGNITUDE(y, ch) * f;
1327
1328 return 0;
1329 }
1330
calc_channel_phases(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)1331 static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1332 {
1333 ShowSpectrumContext *s = ctx->priv;
1334 const int h = s->orientation == VERTICAL ? s->h : s->w;
1335 const int ch = jobnr;
1336 float *phases = s->phases[ch];
1337 int y;
1338
1339 for (y = 0; y < h; y++)
1340 phases[y] = (PHASE(y, ch) / M_PI + 1) / 2;
1341
1342 return 0;
1343 }
1344
unwrap(float * x,int N,float tol,float * mi,float * ma)1345 static void unwrap(float *x, int N, float tol, float *mi, float *ma)
1346 {
1347 const float rng = 2.f * M_PI;
1348 float prev_p = 0.f;
1349 float max = -FLT_MAX;
1350 float min = FLT_MAX;
1351
1352 for (int i = 0; i < N; i++) {
1353 const float d = x[FFMIN(i + 1, N)] - x[i];
1354 const float p = ceilf(fabsf(d) / rng) * rng * (((d < tol) > 0.f) - ((d > -tol) > 0.f));
1355
1356 x[i] += p + prev_p;
1357 prev_p += p;
1358 max = fmaxf(x[i], max);
1359 min = fminf(x[i], min);
1360 }
1361
1362 *mi = min;
1363 *ma = max;
1364 }
1365
calc_channel_uphases(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)1366 static int calc_channel_uphases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1367 {
1368 ShowSpectrumContext *s = ctx->priv;
1369 const int h = s->orientation == VERTICAL ? s->h : s->w;
1370 const int ch = jobnr;
1371 float *phases = s->phases[ch];
1372 float min, max, scale;
1373 int y;
1374
1375 for (y = 0; y < h; y++)
1376 phases[y] = PHASE(y, ch);
1377 unwrap(phases, h, M_PI, &min, &max);
1378 scale = 1.f / (max - min + FLT_MIN);
1379 for (y = 0; y < h; y++)
1380 phases[y] = fabsf((phases[y] - min) * scale);
1381
1382 return 0;
1383 }
1384
acalc_magnitudes(ShowSpectrumContext * s)1385 static void acalc_magnitudes(ShowSpectrumContext *s)
1386 {
1387 const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
1388 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
1389 const float f = s->gain * w;
1390
1391 for (ch = 0; ch < s->nb_display_channels; ch++) {
1392 float *magnitudes = s->magnitudes[ch];
1393
1394 for (y = 0; y < h; y++)
1395 magnitudes[y] += MAGNITUDE(y, ch) * f;
1396 }
1397 }
1398
scale_magnitudes(ShowSpectrumContext * s,float scale)1399 static void scale_magnitudes(ShowSpectrumContext *s, float scale)
1400 {
1401 int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
1402
1403 for (ch = 0; ch < s->nb_display_channels; ch++) {
1404 float *magnitudes = s->magnitudes[ch];
1405
1406 for (y = 0; y < h; y++)
1407 magnitudes[y] *= scale;
1408 }
1409 }
1410
clear_combine_buffer(ShowSpectrumContext * s,int size)1411 static void clear_combine_buffer(ShowSpectrumContext *s, int size)
1412 {
1413 int y;
1414
1415 for (y = 0; y < size; y++) {
1416 s->combine_buffer[4 * y ] = 0;
1417 s->combine_buffer[4 * y + 1] = 127.5;
1418 s->combine_buffer[4 * y + 2] = 127.5;
1419 s->combine_buffer[4 * y + 3] = 0;
1420 }
1421 }
1422
plot_spectrum_column(AVFilterLink * inlink,AVFrame * insamples)1423 static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
1424 {
1425 AVFilterContext *ctx = inlink->dst;
1426 AVFilterLink *outlink = ctx->outputs[0];
1427 ShowSpectrumContext *s = ctx->priv;
1428 AVFrame *outpicref = s->outpicref;
1429 int ret, plane, x, y, z = s->orientation == VERTICAL ? s->h : s->w;
1430 const int alpha = outpicref->data[3] != NULL;
1431
1432 /* fill a new spectrum column */
1433 /* initialize buffer for combining to black */
1434 clear_combine_buffer(s, z);
1435
1436 ff_filter_execute(ctx, s->plot_channel, NULL, NULL, s->nb_display_channels);
1437
1438 for (y = 0; y < z * 4; y++) {
1439 for (x = 0; x < s->nb_display_channels; x++) {
1440 s->combine_buffer[y] += s->color_buffer[x][y];
1441 }
1442 }
1443
1444 av_frame_make_writable(s->outpicref);
1445 /* copy to output */
1446 if (s->orientation == VERTICAL) {
1447 if (s->sliding == SCROLL) {
1448 for (plane = 0; plane < 3 + alpha; plane++) {
1449 for (y = 0; y < s->h; y++) {
1450 uint8_t *p = outpicref->data[plane] + s->start_x +
1451 (y + s->start_y) * outpicref->linesize[plane];
1452 memmove(p, p + 1, s->w - 1);
1453 }
1454 }
1455 s->xpos = s->w - 1;
1456 } else if (s->sliding == RSCROLL) {
1457 for (plane = 0; plane < 3 + alpha; plane++) {
1458 for (y = 0; y < s->h; y++) {
1459 uint8_t *p = outpicref->data[plane] + s->start_x +
1460 (y + s->start_y) * outpicref->linesize[plane];
1461 memmove(p + 1, p, s->w - 1);
1462 }
1463 }
1464 s->xpos = 0;
1465 }
1466 for (plane = 0; plane < 3; plane++) {
1467 uint8_t *p = outpicref->data[plane] + s->start_x +
1468 (outlink->h - 1 - s->start_y) * outpicref->linesize[plane] +
1469 s->xpos;
1470 for (y = 0; y < s->h; y++) {
1471 *p = lrintf(av_clipf(s->combine_buffer[4 * y + plane], 0, 255));
1472 p -= outpicref->linesize[plane];
1473 }
1474 }
1475 if (alpha) {
1476 uint8_t *p = outpicref->data[3] + s->start_x +
1477 (outlink->h - 1 - s->start_y) * outpicref->linesize[3] +
1478 s->xpos;
1479 for (y = 0; y < s->h; y++) {
1480 *p = lrintf(av_clipf(s->combine_buffer[4 * y + 3], 0, 255));
1481 p -= outpicref->linesize[3];
1482 }
1483 }
1484 } else {
1485 if (s->sliding == SCROLL) {
1486 for (plane = 0; plane < 3 + alpha; plane++) {
1487 for (y = 1; y < s->h; y++) {
1488 memmove(outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
1489 outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
1490 s->w);
1491 }
1492 }
1493 s->xpos = s->h - 1;
1494 } else if (s->sliding == RSCROLL) {
1495 for (plane = 0; plane < 3 + alpha; plane++) {
1496 for (y = s->h - 1; y >= 1; y--) {
1497 memmove(outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
1498 outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
1499 s->w);
1500 }
1501 }
1502 s->xpos = 0;
1503 }
1504 for (plane = 0; plane < 3; plane++) {
1505 uint8_t *p = outpicref->data[plane] + s->start_x +
1506 (s->xpos + s->start_y) * outpicref->linesize[plane];
1507 for (x = 0; x < s->w; x++) {
1508 *p = lrintf(av_clipf(s->combine_buffer[4 * x + plane], 0, 255));
1509 p++;
1510 }
1511 }
1512 if (alpha) {
1513 uint8_t *p = outpicref->data[3] + s->start_x +
1514 (s->xpos + s->start_y) * outpicref->linesize[3];
1515 for (x = 0; x < s->w; x++) {
1516 *p = lrintf(av_clipf(s->combine_buffer[4 * x + 3], 0, 255));
1517 p++;
1518 }
1519 }
1520 }
1521
1522 if (s->sliding != FULLFRAME || s->xpos == 0)
1523 s->pts = outpicref->pts = av_rescale_q(s->in_pts, inlink->time_base, outlink->time_base);
1524
1525 if (s->sliding == LREPLACE) {
1526 s->xpos--;
1527 if (s->orientation == VERTICAL && s->xpos < 0)
1528 s->xpos = s->w - 1;
1529 if (s->orientation == HORIZONTAL && s->xpos < 0)
1530 s->xpos = s->h - 1;
1531 } else {
1532 s->xpos++;
1533 if (s->orientation == VERTICAL && s->xpos >= s->w)
1534 s->xpos = 0;
1535 if (s->orientation == HORIZONTAL && s->xpos >= s->h)
1536 s->xpos = 0;
1537 }
1538
1539 if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
1540 if (s->old_pts < outpicref->pts || s->sliding == FULLFRAME) {
1541 AVFrame *clone;
1542
1543 if (s->legend) {
1544 char *units = get_time(ctx, insamples->pts /(float)inlink->sample_rate, x);
1545 if (!units)
1546 return AVERROR(ENOMEM);
1547
1548 if (s->orientation == VERTICAL) {
1549 for (y = 0; y < 10; y++) {
1550 memset(s->outpicref->data[0] + outlink->w / 2 - 4 * s->old_len +
1551 (outlink->h - s->start_y / 2 - 20 + y) * s->outpicref->linesize[0], 0, 10 * s->old_len);
1552 }
1553 drawtext(s->outpicref,
1554 outlink->w / 2 - 4 * strlen(units),
1555 outlink->h - s->start_y / 2 - 20,
1556 units, 0);
1557 } else {
1558 for (y = 0; y < 10 * s->old_len; y++) {
1559 memset(s->outpicref->data[0] + s->start_x / 7 + 20 +
1560 (outlink->h / 2 - 4 * s->old_len + y) * s->outpicref->linesize[0], 0, 10);
1561 }
1562 drawtext(s->outpicref,
1563 s->start_x / 7 + 20,
1564 outlink->h / 2 - 4 * strlen(units),
1565 units, 1);
1566 }
1567 s->old_len = strlen(units);
1568 av_free(units);
1569 }
1570 s->old_pts = outpicref->pts;
1571 clone = av_frame_clone(s->outpicref);
1572 if (!clone)
1573 return AVERROR(ENOMEM);
1574 ret = ff_filter_frame(outlink, clone);
1575 if (ret < 0)
1576 return ret;
1577 return 0;
1578 }
1579 }
1580
1581 return 1;
1582 }
1583
1584 #if CONFIG_SHOWSPECTRUM_FILTER
1585
activate(AVFilterContext * ctx)1586 static int activate(AVFilterContext *ctx)
1587 {
1588 AVFilterLink *inlink = ctx->inputs[0];
1589 AVFilterLink *outlink = ctx->outputs[0];
1590 ShowSpectrumContext *s = ctx->priv;
1591 int ret, status;
1592 int64_t pts;
1593
1594 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
1595
1596 if (s->outpicref) {
1597 AVFrame *fin;
1598
1599 ret = ff_inlink_consume_samples(inlink, s->hop_size, s->hop_size, &fin);
1600 if (ret < 0)
1601 return ret;
1602 if (ret > 0) {
1603 ff_filter_execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
1604
1605 if (s->data == D_MAGNITUDE)
1606 ff_filter_execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels);
1607
1608 if (s->data == D_PHASE)
1609 ff_filter_execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels);
1610
1611 if (s->data == D_UPHASE)
1612 ff_filter_execute(ctx, calc_channel_uphases, NULL, NULL, s->nb_display_channels);
1613
1614 if (s->sliding != FULLFRAME || s->xpos == 0)
1615 s->in_pts = fin->pts;
1616 ret = plot_spectrum_column(inlink, fin);
1617 av_frame_free(&fin);
1618 if (ret <= 0)
1619 return ret;
1620 }
1621 }
1622
1623 if (ff_outlink_get_status(inlink) == AVERROR_EOF &&
1624 s->sliding == FULLFRAME &&
1625 s->xpos > 0 && s->outpicref) {
1626
1627 if (s->orientation == VERTICAL) {
1628 for (int i = 0; i < outlink->h; i++) {
1629 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos);
1630 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
1631 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
1632 if (s->outpicref->data[3])
1633 memset(s->outpicref->data[3] + i * s->outpicref->linesize[3] + s->xpos, 0, outlink->w - s->xpos);
1634 }
1635 } else {
1636 for (int i = s->xpos; i < outlink->h; i++) {
1637 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w);
1638 memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
1639 memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
1640 if (s->outpicref->data[3])
1641 memset(s->outpicref->data[3] + i * s->outpicref->linesize[3], 0, outlink->w);
1642 }
1643 }
1644 s->outpicref->pts = av_rescale_q(s->in_pts, inlink->time_base, outlink->time_base);
1645 pts = s->outpicref->pts;
1646 ret = ff_filter_frame(outlink, s->outpicref);
1647 s->outpicref = NULL;
1648 ff_outlink_set_status(outlink, AVERROR_EOF, pts);
1649 return 0;
1650 }
1651
1652 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
1653 if (status == AVERROR_EOF) {
1654 ff_outlink_set_status(outlink, status, s->pts);
1655 return 0;
1656 }
1657 }
1658
1659 if (ff_inlink_queued_samples(inlink) >= s->hop_size) {
1660 ff_filter_set_ready(ctx, 10);
1661 return 0;
1662 }
1663
1664 if (ff_outlink_frame_wanted(outlink)) {
1665 ff_inlink_request_frame(inlink);
1666 return 0;
1667 }
1668
1669 return FFERROR_NOT_READY;
1670 }
1671
1672 static const AVFilterPad showspectrum_inputs[] = {
1673 {
1674 .name = "default",
1675 .type = AVMEDIA_TYPE_AUDIO,
1676 },
1677 };
1678
1679 static const AVFilterPad showspectrum_outputs[] = {
1680 {
1681 .name = "default",
1682 .type = AVMEDIA_TYPE_VIDEO,
1683 .config_props = config_output,
1684 },
1685 };
1686
1687 const AVFilter ff_avf_showspectrum = {
1688 .name = "showspectrum",
1689 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
1690 .uninit = uninit,
1691 .priv_size = sizeof(ShowSpectrumContext),
1692 FILTER_INPUTS(showspectrum_inputs),
1693 FILTER_OUTPUTS(showspectrum_outputs),
1694 FILTER_QUERY_FUNC(query_formats),
1695 .activate = activate,
1696 .priv_class = &showspectrum_class,
1697 .flags = AVFILTER_FLAG_SLICE_THREADS,
1698 };
1699 #endif // CONFIG_SHOWSPECTRUM_FILTER
1700
1701 #if CONFIG_SHOWSPECTRUMPIC_FILTER
1702
1703 static const AVOption showspectrumpic_options[] = {
1704 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
1705 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
1706 { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" },
1707 { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
1708 { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
1709 { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" },
1710 { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
1711 { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
1712 { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
1713 { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
1714 { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
1715 { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
1716 { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
1717 { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
1718 { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
1719 { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
1720 { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
1721 { "viridis", "viridis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=VIRIDIS}, 0, 0, FLAGS, "color" },
1722 { "plasma", "plasma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=PLASMA}, 0, 0, FLAGS, "color" },
1723 { "cividis", "cividis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=CIVIDIS}, 0, 0, FLAGS, "color" },
1724 { "terrain", "terrain based coloring", 0, AV_OPT_TYPE_CONST, {.i64=TERRAIN}, 0, 0, FLAGS, "color" },
1725 { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" },
1726 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
1727 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
1728 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
1729 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
1730 { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
1731 { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
1732 { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=F_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
1733 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=F_LINEAR}, 0, 0, FLAGS, "fscale" },
1734 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=F_LOG}, 0, 0, FLAGS, "fscale" },
1735 { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
1736 WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_HANNING),
1737 { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
1738 { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
1739 { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
1740 { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
1741 { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
1742 { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
1743 { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
1744 { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
1745 { "drange", "set dynamic range in dBFS", OFFSET(drange), AV_OPT_TYPE_FLOAT, {.dbl = 120}, 10, 200, FLAGS },
1746 { "limit", "set upper limit in dBFS", OFFSET(limit), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -100, 100, FLAGS },
1747 { "opacity", "set opacity strength", OFFSET(opacity_factor), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 10, FLAGS },
1748 { NULL }
1749 };
1750
1751 AVFILTER_DEFINE_CLASS(showspectrumpic);
1752
showspectrumpic_request_frame(AVFilterLink * outlink)1753 static int showspectrumpic_request_frame(AVFilterLink *outlink)
1754 {
1755 AVFilterContext *ctx = outlink->src;
1756 ShowSpectrumContext *s = ctx->priv;
1757 AVFilterLink *inlink = ctx->inputs[0];
1758 int ret;
1759
1760 ret = ff_request_frame(inlink);
1761 if (ret == AVERROR_EOF && s->outpicref && s->samples > 0) {
1762 int consumed = 0;
1763 int x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
1764 unsigned int nb_frame = 0;
1765 int ch, spf, spb;
1766 int src_offset = 0;
1767 AVFrame *fin;
1768
1769 spf = s->win_size * (s->samples / ((s->win_size * sz) * ceil(s->samples / (float)(s->win_size * sz))));
1770 spf = FFMAX(1, spf);
1771
1772 spb = (s->samples / (spf * sz)) * spf;
1773
1774 fin = ff_get_audio_buffer(inlink, spf);
1775 if (!fin)
1776 return AVERROR(ENOMEM);
1777
1778 while (x < sz) {
1779 int acc_samples = 0;
1780 int dst_offset = 0;
1781
1782 while (nb_frame < s->nb_frames) {
1783 AVFrame *cur_frame = s->frames[nb_frame];
1784 int cur_frame_samples = cur_frame->nb_samples;
1785 int nb_samples = 0;
1786
1787 if (acc_samples < spf) {
1788 nb_samples = FFMIN(spf - acc_samples, cur_frame_samples - src_offset);
1789 acc_samples += nb_samples;
1790 av_samples_copy(fin->extended_data, cur_frame->extended_data,
1791 dst_offset, src_offset, nb_samples,
1792 cur_frame->ch_layout.nb_channels, AV_SAMPLE_FMT_FLTP);
1793 }
1794
1795 src_offset += nb_samples;
1796 dst_offset += nb_samples;
1797 if (cur_frame_samples <= src_offset) {
1798 av_frame_free(&s->frames[nb_frame]);
1799 nb_frame++;
1800 src_offset = 0;
1801 }
1802
1803 if (acc_samples == spf)
1804 break;
1805 }
1806
1807 ff_filter_execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
1808 acalc_magnitudes(s);
1809
1810 consumed += spf;
1811 if (consumed >= spb) {
1812 int h = s->orientation == VERTICAL ? s->h : s->w;
1813
1814 scale_magnitudes(s, 1.f / (consumed / spf));
1815 plot_spectrum_column(inlink, fin);
1816 consumed = 0;
1817 x++;
1818 for (ch = 0; ch < s->nb_display_channels; ch++)
1819 memset(s->magnitudes[ch], 0, h * sizeof(float));
1820 }
1821 }
1822
1823 av_frame_free(&fin);
1824 s->outpicref->pts = 0;
1825
1826 if (s->legend)
1827 draw_legend(ctx, s->samples);
1828
1829 ret = ff_filter_frame(outlink, s->outpicref);
1830 s->outpicref = NULL;
1831 }
1832
1833 return ret;
1834 }
1835
showspectrumpic_filter_frame(AVFilterLink * inlink,AVFrame * insamples)1836 static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1837 {
1838 AVFilterContext *ctx = inlink->dst;
1839 ShowSpectrumContext *s = ctx->priv;
1840 void *ptr;
1841
1842 if (s->nb_frames + 1ULL > s->frames_size / sizeof(*(s->frames))) {
1843 ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2);
1844 if (!ptr)
1845 return AVERROR(ENOMEM);
1846 s->frames = ptr;
1847 }
1848
1849 s->frames[s->nb_frames] = insamples;
1850 s->samples += insamples->nb_samples;
1851 s->nb_frames++;
1852
1853 return 0;
1854 }
1855
1856 static const AVFilterPad showspectrumpic_inputs[] = {
1857 {
1858 .name = "default",
1859 .type = AVMEDIA_TYPE_AUDIO,
1860 .filter_frame = showspectrumpic_filter_frame,
1861 },
1862 };
1863
1864 static const AVFilterPad showspectrumpic_outputs[] = {
1865 {
1866 .name = "default",
1867 .type = AVMEDIA_TYPE_VIDEO,
1868 .config_props = config_output,
1869 .request_frame = showspectrumpic_request_frame,
1870 },
1871 };
1872
1873 const AVFilter ff_avf_showspectrumpic = {
1874 .name = "showspectrumpic",
1875 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
1876 .uninit = uninit,
1877 .priv_size = sizeof(ShowSpectrumContext),
1878 FILTER_INPUTS(showspectrumpic_inputs),
1879 FILTER_OUTPUTS(showspectrumpic_outputs),
1880 FILTER_QUERY_FUNC(query_formats),
1881 .priv_class = &showspectrumpic_class,
1882 .flags = AVFILTER_FLAG_SLICE_THREADS,
1883 };
1884
1885 #endif // CONFIG_SHOWSPECTRUMPIC_FILTER
1886