FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avf_showspectrum.c
Go to the documentation of this file.
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 <math.h>
30 
31 #include "libavcodec/avfft.h"
32 #include "libavutil/audio_fifo.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/avstring.h"
36 #include "libavutil/opt.h"
38 #include "audio.h"
39 #include "video.h"
40 #include "avfilter.h"
41 #include "internal.h"
42 #include "window_func.h"
43 
50 
51 typedef struct ShowSpectrumContext {
52  const AVClass *class;
53  int w, h;
59  int sliding; ///< 1 if sliding mode, 0 otherwise
60  int mode; ///< channel display mode
61  int color_mode; ///< display color scheme
62  int scale;
63  float saturation; ///< color saturation multiplier
64  float rotation; ///< color rotation
65  int data;
66  int xpos; ///< x position (current column)
67  FFTContext **fft; ///< Fast Fourier Transform context
68  int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits)
69  FFTComplex **fft_data; ///< bins holder for each (displayed) channels
70  float *window_func_lut; ///< Window function LUT
71  float **magnitudes;
72  float **phases;
73  int win_func;
74  int win_size;
75  double win_scale;
76  float overlap;
77  float gain;
78  int hop_size;
79  float *combine_buffer; ///< color combining buffer (3 * h items)
80  float **color_buffer; ///< color buffer (3 * h * ch items)
82  int64_t pts;
84  int legend;
87 
88 #define OFFSET(x) offsetof(ShowSpectrumContext, x)
89 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
90 
91 static const AVOption showspectrum_options[] = {
92  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
93  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
94  { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
95  { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
96  { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
97  { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
98  { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
99  { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
100  { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
101  { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
102  { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
103  { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
104  { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
105  { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
106  { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
107  { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
108  { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
109  { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
110  { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
111  { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
112  { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
113  { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
114  { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
115  { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
116  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
117  { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
118  { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
119  { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
120  { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
121  { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
122  { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
123  { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
124  { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
125  { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
126  { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
127  { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
128  { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
129  { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
130  { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
131  { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
132  { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
133  { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
134  { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
135  { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
136  { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
137  { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
138  { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
139  { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
140  { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
141  { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
142  { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
143  { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
144  { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS },
145  { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
146  { "data", "set data mode", OFFSET(data), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_DMODES-1, FLAGS, "data" },
147  { "magnitude", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_MAGNITUDE}, 0, 0, FLAGS, "data" },
148  { "phase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_PHASE}, 0, 0, FLAGS, "data" },
149  { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
150  { NULL }
151 };
152 
153 AVFILTER_DEFINE_CLASS(showspectrum);
154 
155 static const struct ColorTable {
156  float a, y, u, v;
157 } color_table[][8] = {
158  [INTENSITY] = {
159  { 0, 0, 0, 0 },
160  { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
161  { 0.30, .18572281794568020, .1772436246393981, .17475554840414750 },
162  { 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 },
163  { 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 },
164  { 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 },
165  { 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 },
166  { 1, 1, 0, 0 }},
167  [RAINBOW] = {
168  { 0, 0, 0, 0 },
169  { 0.13, 44/256., (189-128)/256., (138-128)/256. },
170  { 0.25, 29/256., (186-128)/256., (119-128)/256. },
171  { 0.38, 119/256., (194-128)/256., (53-128)/256. },
172  { 0.60, 111/256., (73-128)/256., (59-128)/256. },
173  { 0.73, 205/256., (19-128)/256., (149-128)/256. },
174  { 0.86, 135/256., (83-128)/256., (200-128)/256. },
175  { 1, 73/256., (95-128)/256., (225-128)/256. }},
176  [MORELAND] = {
177  { 0, 44/256., (181-128)/256., (112-128)/256. },
178  { 0.13, 126/256., (177-128)/256., (106-128)/256. },
179  { 0.25, 164/256., (163-128)/256., (109-128)/256. },
180  { 0.38, 200/256., (140-128)/256., (120-128)/256. },
181  { 0.60, 201/256., (117-128)/256., (141-128)/256. },
182  { 0.73, 177/256., (103-128)/256., (165-128)/256. },
183  { 0.86, 136/256., (100-128)/256., (183-128)/256. },
184  { 1, 68/256., (117-128)/256., (203-128)/256. }},
185  [NEBULAE] = {
186  { 0, 10/256., (134-128)/256., (132-128)/256. },
187  { 0.23, 21/256., (137-128)/256., (130-128)/256. },
188  { 0.45, 35/256., (134-128)/256., (134-128)/256. },
189  { 0.57, 51/256., (130-128)/256., (139-128)/256. },
190  { 0.67, 104/256., (116-128)/256., (162-128)/256. },
191  { 0.77, 120/256., (105-128)/256., (188-128)/256. },
192  { 0.87, 140/256., (105-128)/256., (188-128)/256. },
193  { 1, 1, 0, 0 }},
194  [FIRE] = {
195  { 0, 0, 0, 0 },
196  { 0.23, 44/256., (132-128)/256., (127-128)/256. },
197  { 0.45, 62/256., (116-128)/256., (140-128)/256. },
198  { 0.57, 75/256., (105-128)/256., (152-128)/256. },
199  { 0.67, 95/256., (91-128)/256., (166-128)/256. },
200  { 0.77, 126/256., (74-128)/256., (172-128)/256. },
201  { 0.87, 164/256., (73-128)/256., (162-128)/256. },
202  { 1, 1, 0, 0 }},
203  [FIERY] = {
204  { 0, 0, 0, 0 },
205  { 0.23, 36/256., (116-128)/256., (163-128)/256. },
206  { 0.45, 52/256., (102-128)/256., (200-128)/256. },
207  { 0.57, 116/256., (84-128)/256., (196-128)/256. },
208  { 0.67, 157/256., (67-128)/256., (181-128)/256. },
209  { 0.77, 193/256., (40-128)/256., (155-128)/256. },
210  { 0.87, 221/256., (101-128)/256., (134-128)/256. },
211  { 1, 1, 0, 0 }},
212  [FRUIT] = {
213  { 0, 0, 0, 0 },
214  { 0.20, 29/256., (136-128)/256., (119-128)/256. },
215  { 0.30, 60/256., (119-128)/256., (90-128)/256. },
216  { 0.40, 85/256., (91-128)/256., (85-128)/256. },
217  { 0.50, 116/256., (70-128)/256., (105-128)/256. },
218  { 0.60, 151/256., (50-128)/256., (146-128)/256. },
219  { 0.70, 191/256., (63-128)/256., (178-128)/256. },
220  { 1, 98/256., (80-128)/256., (221-128)/256. }},
221  [COOL] = {
222  { 0, 0, 0, 0 },
223  { .15, 0, .5, -.5 },
224  { 1, 1, -.5, .5 }},
225 };
226 
228 {
229  ShowSpectrumContext *s = ctx->priv;
230  int i;
231 
233  if (s->fft) {
234  for (i = 0; i < s->nb_display_channels; i++)
235  av_fft_end(s->fft[i]);
236  }
237  av_freep(&s->fft);
238  if (s->fft_data) {
239  for (i = 0; i < s->nb_display_channels; i++)
240  av_freep(&s->fft_data[i]);
241  }
242  av_freep(&s->fft_data);
243  if (s->color_buffer) {
244  for (i = 0; i < s->nb_display_channels; i++)
245  av_freep(&s->color_buffer[i]);
246  }
247  av_freep(&s->color_buffer);
249  if (s->magnitudes) {
250  for (i = 0; i < s->nb_display_channels; i++)
251  av_freep(&s->magnitudes[i]);
252  }
253  av_freep(&s->magnitudes);
256  if (s->phases) {
257  for (i = 0; i < s->nb_display_channels; i++)
258  av_freep(&s->phases[i]);
259  }
260  av_freep(&s->phases);
261 }
262 
264 {
267  AVFilterLink *inlink = ctx->inputs[0];
268  AVFilterLink *outlink = ctx->outputs[0];
271  int ret;
272 
273  /* set input audio formats */
274  formats = ff_make_format_list(sample_fmts);
275  if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
276  return ret;
277 
278  layouts = ff_all_channel_layouts();
279  if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
280  return ret;
281 
282  formats = ff_all_samplerates();
283  if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
284  return ret;
285 
286  /* set output video format */
287  formats = ff_make_format_list(pix_fmts);
288  if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
289  return ret;
290 
291  return 0;
292 }
293 
294 static int config_output(AVFilterLink *outlink)
295 {
296  AVFilterContext *ctx = outlink->src;
297  AVFilterLink *inlink = ctx->inputs[0];
298  ShowSpectrumContext *s = ctx->priv;
299  int i, fft_bits, h, w;
300  float overlap;
301 
302  s->pts = AV_NOPTS_VALUE;
303 
304  if (!strcmp(ctx->filter->name, "showspectrumpic"))
305  s->single_pic = 1;
306 
307  outlink->w = s->w;
308  outlink->h = s->h;
309 
310  if (s->legend) {
311  s->start_x = log10(inlink->sample_rate) * 25;
312  s->start_y = 64;
313  outlink->w += s->start_x * 2;
314  outlink->h += s->start_y * 2;
315  }
316 
317  h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? s->h : s->h / inlink->channels;
318  w = (s->mode == COMBINED || s->orientation == VERTICAL) ? s->w : s->w / inlink->channels;
319  s->channel_height = h;
320  s->channel_width = w;
321 
322  if (s->orientation == VERTICAL) {
323  /* FFT window size (precision) according to the requested output frame height */
324  for (fft_bits = 1; 1 << fft_bits < 2 * h; fft_bits++);
325  } else {
326  /* FFT window size (precision) according to the requested output frame width */
327  for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++);
328  }
329  s->win_size = 1 << fft_bits;
330 
331  if (!s->fft) {
332  s->fft = av_calloc(inlink->channels, sizeof(*s->fft));
333  if (!s->fft)
334  return AVERROR(ENOMEM);
335  }
336 
337  /* (re-)configuration if the video output changed (or first init) */
338  if (fft_bits != s->fft_bits) {
339  AVFrame *outpicref;
340 
341  s->fft_bits = fft_bits;
342 
343  /* FFT buffers: x2 for each (display) channel buffer.
344  * Note: we use free and malloc instead of a realloc-like function to
345  * make sure the buffer is aligned in memory for the FFT functions. */
346  for (i = 0; i < s->nb_display_channels; i++) {
347  av_fft_end(s->fft[i]);
348  av_freep(&s->fft_data[i]);
349  }
350  av_freep(&s->fft_data);
351 
352  s->nb_display_channels = inlink->channels;
353  for (i = 0; i < s->nb_display_channels; i++) {
354  s->fft[i] = av_fft_init(fft_bits, 0);
355  if (!s->fft[i]) {
356  av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
357  "The window size might be too high.\n");
358  return AVERROR(EINVAL);
359  }
360  }
361 
362  s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
363  if (!s->magnitudes)
364  return AVERROR(ENOMEM);
365  for (i = 0; i < s->nb_display_channels; i++) {
366  s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
367  if (!s->magnitudes[i])
368  return AVERROR(ENOMEM);
369  }
370 
371  s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases));
372  if (!s->phases)
373  return AVERROR(ENOMEM);
374  for (i = 0; i < s->nb_display_channels; i++) {
375  s->phases[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->phases));
376  if (!s->phases[i])
377  return AVERROR(ENOMEM);
378  }
379 
380  av_freep(&s->color_buffer);
382  if (!s->color_buffer)
383  return AVERROR(ENOMEM);
384  for (i = 0; i < s->nb_display_channels; i++) {
385  s->color_buffer[i] = av_calloc(s->orientation == VERTICAL ? s->h * 3 : s->w * 3, sizeof(**s->color_buffer));
386  if (!s->color_buffer[i])
387  return AVERROR(ENOMEM);
388  }
389 
390  s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
391  if (!s->fft_data)
392  return AVERROR(ENOMEM);
393  for (i = 0; i < s->nb_display_channels; i++) {
394  s->fft_data[i] = av_calloc(s->win_size, sizeof(**s->fft_data));
395  if (!s->fft_data[i])
396  return AVERROR(ENOMEM);
397  }
398 
399  /* pre-calc windowing function */
400  s->window_func_lut =
402  sizeof(*s->window_func_lut));
403  if (!s->window_func_lut)
404  return AVERROR(ENOMEM);
406  if (s->overlap == 1)
407  s->overlap = overlap;
408  s->hop_size = (1. - s->overlap) * s->win_size;
409  if (s->hop_size < 1) {
410  av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
411  return AVERROR(EINVAL);
412  }
413 
414  for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
415  s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
416  }
417  s->win_scale = 1. / sqrt(s->win_scale);
418 
419  /* prepare the initial picref buffer (black frame) */
421  s->outpicref = outpicref =
422  ff_get_video_buffer(outlink, outlink->w, outlink->h);
423  if (!outpicref)
424  return AVERROR(ENOMEM);
425  outlink->sample_aspect_ratio = (AVRational){1,1};
426  for (i = 0; i < outlink->h; i++) {
427  memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
428  memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
429  memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
430  }
431  outpicref->color_range = AVCOL_RANGE_JPEG;
432  }
433 
434  if ((s->orientation == VERTICAL && s->xpos >= s->w) ||
435  (s->orientation == HORIZONTAL && s->xpos >= s->h))
436  s->xpos = 0;
437 
438  outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
439  if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
440  outlink->frame_rate.den *= s->w;
441  if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
442  outlink->frame_rate.den *= s->h;
443 
444  if (s->orientation == VERTICAL) {
445  s->combine_buffer =
446  av_realloc_f(s->combine_buffer, s->h * 3,
447  sizeof(*s->combine_buffer));
448  } else {
449  s->combine_buffer =
450  av_realloc_f(s->combine_buffer, s->w * 3,
451  sizeof(*s->combine_buffer));
452  }
453 
454  av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n",
455  s->w, s->h, s->win_size);
456 
458  s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
459  if (!s->fifo)
460  return AVERROR(ENOMEM);
461  return 0;
462 }
463 
464 static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
465 {
466  ShowSpectrumContext *s = ctx->priv;
467  const float *window_func_lut = s->window_func_lut;
468  AVFrame *fin = arg;
469  const int ch = jobnr;
470  int n;
471 
472  /* fill FFT input with the number of samples available */
473  const float *p = (float *)fin->extended_data[ch];
474 
475  for (n = 0; n < s->win_size; n++) {
476  s->fft_data[ch][n].re = p[n] * window_func_lut[n];
477  s->fft_data[ch][n].im = 0;
478  }
479 
480  /* run FFT on each samples set */
481  av_fft_permute(s->fft[ch], s->fft_data[ch]);
482  av_fft_calc(s->fft[ch], s->fft_data[ch]);
483 
484  return 0;
485 }
486 
487 #define RE(y, ch) s->fft_data[ch][y].re
488 #define IM(y, ch) s->fft_data[ch][y].im
489 #define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch))
490 #define PHASE(y, ch) atan2(IM(y, ch), RE(y, ch))
491 
492 static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
493 {
494  ShowSpectrumContext *s = ctx->priv;
495  const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
496  int y, h = s->orientation == VERTICAL ? s->h : s->w;
497  const float f = s->gain * w;
498  const int ch = jobnr;
499  float *magnitudes = s->magnitudes[ch];
500 
501  for (y = 0; y < h; y++)
502  magnitudes[y] = MAGNITUDE(y, ch) * f;
503 
504  return 0;
505 }
506 
507 static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
508 {
509  ShowSpectrumContext *s = ctx->priv;
510  const int h = s->orientation == VERTICAL ? s->h : s->w;
511  const int ch = jobnr;
512  float *phases = s->phases[ch];
513  int y;
514 
515  for (y = 0; y < h; y++)
516  phases[y] = (PHASE(y, ch) / M_PI + 1) / 2;
517 
518  return 0;
519 }
520 
522 {
523  const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
524  int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
525  const float f = s->gain * w;
526 
527  for (ch = 0; ch < s->nb_display_channels; ch++) {
528  float *magnitudes = s->magnitudes[ch];
529 
530  for (y = 0; y < h; y++)
531  magnitudes[y] += MAGNITUDE(y, ch) * f;
532  }
533 }
534 
535 static void scale_magnitudes(ShowSpectrumContext *s, float scale)
536 {
537  int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
538 
539  for (ch = 0; ch < s->nb_display_channels; ch++) {
540  float *magnitudes = s->magnitudes[ch];
541 
542  for (y = 0; y < h; y++)
543  magnitudes[y] *= scale;
544  }
545 }
546 
548  float *yf, float *uf, float *vf)
549 {
550  switch (s->mode) {
551  case COMBINED:
552  // reduce range by channel count
553  *yf = 256.0f / s->nb_display_channels;
554  switch (s->color_mode) {
555  case RAINBOW:
556  case MORELAND:
557  case NEBULAE:
558  case FIRE:
559  case FIERY:
560  case FRUIT:
561  case COOL:
562  case INTENSITY:
563  *uf = *yf;
564  *vf = *yf;
565  break;
566  case CHANNEL:
567  /* adjust saturation for mixed UV coloring */
568  /* this factor is correct for infinite channels, an approximation otherwise */
569  *uf = *yf * M_PI;
570  *vf = *yf * M_PI;
571  break;
572  default:
573  av_assert0(0);
574  }
575  break;
576  case SEPARATE:
577  // full range
578  *yf = 256.0f;
579  *uf = 256.0f;
580  *vf = 256.0f;
581  break;
582  default:
583  av_assert0(0);
584  }
585 
586  if (s->color_mode == CHANNEL) {
587  if (s->nb_display_channels > 1) {
588  *uf *= 0.5 * sin((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
589  *vf *= 0.5 * cos((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
590  } else {
591  *uf *= 0.5 * sin(M_PI * s->rotation);
592  *vf *= 0.5 * cos(M_PI * s->rotation + M_PI_2);
593  }
594  } else {
595  *uf += *uf * sin(M_PI * s->rotation);
596  *vf += *vf * cos(M_PI * s->rotation + M_PI_2);
597  }
598 
599  *uf *= s->saturation;
600  *vf *= s->saturation;
601 }
602 
604  float yf, float uf, float vf,
605  float a, float *out)
606 {
607  if (s->color_mode > CHANNEL) {
608  const int cm = s->color_mode;
609  float y, u, v;
610  int i;
611 
612  for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
613  if (color_table[cm][i].a >= a)
614  break;
615  // i now is the first item >= the color
616  // now we know to interpolate between item i - 1 and i
617  if (a <= color_table[cm][i - 1].a) {
618  y = color_table[cm][i - 1].y;
619  u = color_table[cm][i - 1].u;
620  v = color_table[cm][i - 1].v;
621  } else if (a >= color_table[cm][i].a) {
622  y = color_table[cm][i].y;
623  u = color_table[cm][i].u;
624  v = color_table[cm][i].v;
625  } else {
626  float start = color_table[cm][i - 1].a;
627  float end = color_table[cm][i].a;
628  float lerpfrac = (a - start) / (end - start);
629  y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
630  + color_table[cm][i].y * lerpfrac;
631  u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
632  + color_table[cm][i].u * lerpfrac;
633  v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
634  + color_table[cm][i].v * lerpfrac;
635  }
636 
637  out[0] = y * yf;
638  out[1] = u * uf;
639  out[2] = v * vf;
640  } else {
641  out[0] = a * yf;
642  out[1] = a * uf;
643  out[2] = a * vf;
644  }
645 }
646 
648 {
649  int y;
650 
651  for (y = 0; y < size; y++) {
652  s->combine_buffer[3 * y ] = 0;
653  s->combine_buffer[3 * y + 1] = 127.5;
654  s->combine_buffer[3 * y + 2] = 127.5;
655  }
656 }
657 
658 static int plot_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
659 {
660  ShowSpectrumContext *s = ctx->priv;
661  const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
662  const int ch = jobnr;
663  float *magnitudes = s->magnitudes[ch];
664  float *phases = s->phases[ch];
665  float yf, uf, vf;
666  int y;
667 
668  /* decide color range */
669  color_range(s, ch, &yf, &uf, &vf);
670 
671  /* draw the channel */
672  for (y = 0; y < h; y++) {
673  int row = (s->mode == COMBINED) ? y : ch * h + y;
674  float *out = &s->color_buffer[ch][3 * row];
675  float a;
676 
677  switch (s->data) {
678  case D_MAGNITUDE:
679  /* get magnitude */
680  a = magnitudes[y];
681  break;
682  case D_PHASE:
683  /* get phase */
684  a = phases[y];
685  break;
686  default:
687  av_assert0(0);
688  }
689 
690  /* apply scale */
691  switch (s->scale) {
692  case LINEAR:
693  a = av_clipf(a, 0, 1);
694  break;
695  case SQRT:
696  a = av_clipf(sqrt(a), 0, 1);
697  break;
698  case CBRT:
699  a = av_clipf(cbrt(a), 0, 1);
700  break;
701  case FOURTHRT:
702  a = av_clipf(sqrt(sqrt(a)), 0, 1);
703  break;
704  case FIFTHRT:
705  a = av_clipf(pow(a, 0.20), 0, 1);
706  break;
707  case LOG:
708  a = 1 + log10(av_clipd(a, 1e-6, 1)) / 6; // zero = -120dBFS
709  break;
710  default:
711  av_assert0(0);
712  }
713 
714  pick_color(s, yf, uf, vf, a, out);
715  }
716 
717  return 0;
718 }
719 
720 static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
721 {
722  AVFilterContext *ctx = inlink->dst;
723  AVFilterLink *outlink = ctx->outputs[0];
724  ShowSpectrumContext *s = ctx->priv;
725  AVFrame *outpicref = s->outpicref;
726  int ret, plane, x, y, z = s->orientation == VERTICAL ? s->h : s->w;
727 
728  /* fill a new spectrum column */
729  /* initialize buffer for combining to black */
730  clear_combine_buffer(s, z);
731 
733 
734  for (y = 0; y < z * 3; y++) {
735  for (x = 0; x < s->nb_display_channels; x++) {
736  s->combine_buffer[y] += s->color_buffer[x][y];
737  }
738  }
739 
741  /* copy to output */
742  if (s->orientation == VERTICAL) {
743  if (s->sliding == SCROLL) {
744  for (plane = 0; plane < 3; plane++) {
745  for (y = 0; y < s->h; y++) {
746  uint8_t *p = outpicref->data[plane] +
747  y * outpicref->linesize[plane];
748  memmove(p, p + 1, s->w - 1);
749  }
750  }
751  s->xpos = s->w - 1;
752  } else if (s->sliding == RSCROLL) {
753  for (plane = 0; plane < 3; plane++) {
754  for (y = 0; y < s->h; y++) {
755  uint8_t *p = outpicref->data[plane] +
756  y * outpicref->linesize[plane];
757  memmove(p + 1, p, s->w - 1);
758  }
759  }
760  s->xpos = 0;
761  }
762  for (plane = 0; plane < 3; plane++) {
763  uint8_t *p = outpicref->data[plane] + s->start_x +
764  (outlink->h - 1 - s->start_y) * outpicref->linesize[plane] +
765  s->xpos;
766  for (y = 0; y < s->h; y++) {
767  *p = lrintf(av_clipf(s->combine_buffer[3 * y + plane], 0, 255));
768  p -= outpicref->linesize[plane];
769  }
770  }
771  } else {
772  if (s->sliding == SCROLL) {
773  for (plane = 0; plane < 3; plane++) {
774  for (y = 1; y < s->h; y++) {
775  memmove(outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
776  outpicref->data[plane] + (y ) * outpicref->linesize[plane],
777  s->w);
778  }
779  }
780  s->xpos = s->h - 1;
781  } else if (s->sliding == RSCROLL) {
782  for (plane = 0; plane < 3; plane++) {
783  for (y = s->h - 1; y >= 1; y--) {
784  memmove(outpicref->data[plane] + (y ) * outpicref->linesize[plane],
785  outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
786  s->w);
787  }
788  }
789  s->xpos = 0;
790  }
791  for (plane = 0; plane < 3; plane++) {
792  uint8_t *p = outpicref->data[plane] + s->start_x +
793  (s->xpos + s->start_y) * outpicref->linesize[plane];
794  for (x = 0; x < s->w; x++) {
795  *p = lrintf(av_clipf(s->combine_buffer[3 * x + plane], 0, 255));
796  p++;
797  }
798  }
799  }
800 
801  if (s->sliding != FULLFRAME || s->xpos == 0)
802  outpicref->pts = insamples->pts;
803 
804  s->xpos++;
805  if (s->orientation == VERTICAL && s->xpos >= s->w)
806  s->xpos = 0;
807  if (s->orientation == HORIZONTAL && s->xpos >= s->h)
808  s->xpos = 0;
809  if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
810  ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
811  if (ret < 0)
812  return ret;
813  }
814 
815  return s->win_size;
816 }
817 
818 #if CONFIG_SHOWSPECTRUM_FILTER
819 
820 static int request_frame(AVFilterLink *outlink)
821 {
822  ShowSpectrumContext *s = outlink->src->priv;
823  AVFilterLink *inlink = outlink->src->inputs[0];
824  unsigned i;
825  int ret;
826 
827  ret = ff_request_frame(inlink);
828  if (ret == AVERROR_EOF && s->sliding == FULLFRAME && s->xpos > 0 &&
829  s->outpicref) {
830  if (s->orientation == VERTICAL) {
831  for (i = 0; i < outlink->h; i++) {
832  memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos);
833  memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
834  memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
835  }
836  } else {
837  for (i = s->xpos; i < outlink->h; i++) {
838  memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w);
839  memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
840  memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
841  }
842  }
843  ret = ff_filter_frame(outlink, s->outpicref);
844  s->outpicref = NULL;
845  }
846 
847  return ret;
848 }
849 
850 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
851 {
852  AVFilterContext *ctx = inlink->dst;
853  ShowSpectrumContext *s = ctx->priv;
854  AVFrame *fin = NULL;
855  int ret = 0, consumed = 0;
856 
857  if (s->pts == AV_NOPTS_VALUE)
858  s->pts = insamples->pts - av_audio_fifo_size(s->fifo);
859 
860  av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
861  av_frame_free(&insamples);
862  while (av_audio_fifo_size(s->fifo) >= s->win_size) {
863  fin = ff_get_audio_buffer(inlink, s->win_size);
864  if (!fin) {
865  ret = AVERROR(ENOMEM);
866  goto fail;
867  }
868 
869  fin->pts = s->pts + consumed;
870  consumed += s->hop_size;
871  ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
872  if (ret < 0)
873  goto fail;
874 
875  av_assert0(fin->nb_samples == s->win_size);
876 
878 
879  if (s->data == D_MAGNITUDE)
881 
882  if (s->data == D_PHASE)
884 
885  ret = plot_spectrum_column(inlink, fin);
886  av_frame_free(&fin);
888  if (ret < 0)
889  goto fail;
890  }
891 
892 fail:
893  s->pts = AV_NOPTS_VALUE;
894  av_frame_free(&fin);
895  return ret;
896 }
897 
898 static const AVFilterPad showspectrum_inputs[] = {
899  {
900  .name = "default",
901  .type = AVMEDIA_TYPE_AUDIO,
902  .filter_frame = filter_frame,
903  },
904  { NULL }
905 };
906 
907 static const AVFilterPad showspectrum_outputs[] = {
908  {
909  .name = "default",
910  .type = AVMEDIA_TYPE_VIDEO,
911  .config_props = config_output,
912  .request_frame = request_frame,
913  },
914  { NULL }
915 };
916 
917 AVFilter ff_avf_showspectrum = {
918  .name = "showspectrum",
919  .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
920  .uninit = uninit,
921  .query_formats = query_formats,
922  .priv_size = sizeof(ShowSpectrumContext),
923  .inputs = showspectrum_inputs,
924  .outputs = showspectrum_outputs,
925  .priv_class = &showspectrum_class,
927 };
928 #endif // CONFIG_SHOWSPECTRUM_FILTER
929 
930 #if CONFIG_SHOWSPECTRUMPIC_FILTER
931 
932 static const AVOption showspectrumpic_options[] = {
933  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
934  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
935  { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" },
936  { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
937  { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
938  { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" },
939  { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
940  { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
941  { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
942  { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
943  { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
944  { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
945  { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
946  { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
947  { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
948  { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" },
949  { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
950  { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
951  { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
952  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
953  { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
954  { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
955  { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
956  { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
957  { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
958  { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
959  { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
960  { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
961  { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
962  { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
963  { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
964  { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
965  { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
966  { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
967  { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
968  { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
969  { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
970  { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
971  { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
972  { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
973  { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
974  { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
975  { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
976  { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
977  { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
978  { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
979  { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
980  { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
981  { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
982  { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
983  { NULL }
984 };
985 
986 AVFILTER_DEFINE_CLASS(showspectrumpic);
987 
988 static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
989 {
990  const uint8_t *font;
991  int font_height;
992  int i;
993 
994  font = avpriv_cga_font, font_height = 8;
995 
996  for (i = 0; txt[i]; i++) {
997  int char_y, mask;
998 
999  if (o) {
1000  for (char_y = font_height - 1; char_y >= 0; char_y--) {
1001  uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x;
1002  for (mask = 0x80; mask; mask >>= 1) {
1003  if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
1004  p[char_y] = ~p[char_y];
1005  p += pic->linesize[0];
1006  }
1007  }
1008  } else {
1009  uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8);
1010  for (char_y = 0; char_y < font_height; char_y++) {
1011  for (mask = 0x80; mask; mask >>= 1) {
1012  if (font[txt[i] * font_height + char_y] & mask)
1013  *p = ~(*p);
1014  p++;
1015  }
1016  p += pic->linesize[0] - 8;
1017  }
1018  }
1019  }
1020 }
1021 
1022 static int showspectrumpic_request_frame(AVFilterLink *outlink)
1023 {
1024  AVFilterContext *ctx = outlink->src;
1025  ShowSpectrumContext *s = ctx->priv;
1026  AVFilterLink *inlink = ctx->inputs[0];
1027  int ret, samples;
1028 
1029  ret = ff_request_frame(inlink);
1030  samples = av_audio_fifo_size(s->fifo);
1031  if (ret == AVERROR_EOF && s->outpicref && samples > 0) {
1032  int consumed = 0;
1033  int y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
1034  int ch, spf, spb;
1035  AVFrame *fin;
1036 
1037  spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz))));
1038  spf = FFMAX(1, spf);
1039 
1040  spb = (samples / (spf * sz)) * spf;
1041 
1042  fin = ff_get_audio_buffer(inlink, s->win_size);
1043  if (!fin)
1044  return AVERROR(ENOMEM);
1045 
1046  while (x < sz) {
1047  ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
1048  if (ret < 0) {
1049  av_frame_free(&fin);
1050  return ret;
1051  }
1052 
1053  av_audio_fifo_drain(s->fifo, spf);
1054 
1055  if (ret < s->win_size) {
1056  for (ch = 0; ch < s->nb_display_channels; ch++) {
1057  memset(fin->extended_data[ch] + ret * sizeof(float), 0,
1058  (s->win_size - ret) * sizeof(float));
1059  }
1060  }
1061 
1063  acalc_magnitudes(s);
1064 
1065  consumed += spf;
1066  if (consumed >= spb) {
1067  int h = s->orientation == VERTICAL ? s->h : s->w;
1068 
1069  scale_magnitudes(s, 1. / (consumed / spf));
1070  plot_spectrum_column(inlink, fin);
1071  consumed = 0;
1072  x++;
1073  for (ch = 0; ch < s->nb_display_channels; ch++)
1074  memset(s->magnitudes[ch], 0, h * sizeof(float));
1075  }
1076  }
1077 
1078  av_frame_free(&fin);
1079  s->outpicref->pts = 0;
1080 
1081  if (s->legend) {
1082  int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL);
1083  float spp = samples / (float)sz;
1084  uint8_t *dst;
1085 
1086  drawtext(s->outpicref, 2, outlink->h - 10, "CREATED BY LIBAVFILTER", 0);
1087 
1088  dst = s->outpicref->data[0] + (s->start_y - 1) * s->outpicref->linesize[0] + s->start_x - 1;
1089  for (x = 0; x < s->w + 1; x++)
1090  dst[x] = 200;
1091  dst = s->outpicref->data[0] + (s->start_y + s->h) * s->outpicref->linesize[0] + s->start_x - 1;
1092  for (x = 0; x < s->w + 1; x++)
1093  dst[x] = 200;
1094  for (y = 0; y < s->h + 2; y++) {
1095  dst = s->outpicref->data[0] + (y + s->start_y - 1) * s->outpicref->linesize[0];
1096  dst[s->start_x - 1] = 200;
1097  dst[s->start_x + s->w] = 200;
1098  }
1099  if (s->orientation == VERTICAL) {
1100  int h = s->mode == SEPARATE ? s->h / s->nb_display_channels : s->h;
1101  for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
1102  for (y = 0; y < h; y += 20) {
1103  dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[0];
1104  dst[s->start_x - 2] = 200;
1105  dst[s->start_x + s->w + 1] = 200;
1106  }
1107  for (y = 0; y < h; y += 40) {
1108  dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[0];
1109  dst[s->start_x - 3] = 200;
1110  dst[s->start_x + s->w + 2] = 200;
1111  }
1112  dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x;
1113  for (x = 0; x < s->w; x+=40)
1114  dst[x] = 200;
1115  dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x;
1116  for (x = 0; x < s->w; x+=80)
1117  dst[x] = 200;
1118  dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x;
1119  for (x = 0; x < s->w; x+=40) {
1120  dst[x] = 200;
1121  }
1122  dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x;
1123  for (x = 0; x < s->w; x+=80) {
1124  dst[x] = 200;
1125  }
1126  for (y = 0; y < h; y += 40) {
1127  float hertz = y * (inlink->sample_rate / 2) / (float)(1 << (int)ceil(log2(h)));
1128  char *units;
1129 
1130  if (hertz == 0)
1131  units = av_asprintf("DC");
1132  else
1133  units = av_asprintf("%.2f", hertz);
1134  if (!units)
1135  return AVERROR(ENOMEM);
1136 
1137  drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4, units, 0);
1138  av_free(units);
1139  }
1140  }
1141 
1142  for (x = 0; x < s->w; x+=80) {
1143  float seconds = x * spp / inlink->sample_rate;
1144  char *units;
1145 
1146  if (x == 0)
1147  units = av_asprintf("0");
1148  else if (log10(seconds) > 6)
1149  units = av_asprintf("%.2fh", seconds / (60 * 60));
1150  else if (log10(seconds) > 3)
1151  units = av_asprintf("%.2fm", seconds / 60);
1152  else
1153  units = av_asprintf("%.2fs", seconds);
1154  if (!units)
1155  return AVERROR(ENOMEM);
1156 
1157  drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->h + s->start_y + 6, units, 0);
1158  drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->start_y - 12, units, 0);
1159  av_free(units);
1160  }
1161 
1162  drawtext(s->outpicref, outlink->w / 2 - 4 * 4, outlink->h - s->start_y / 2, "TIME", 0);
1163  drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 14 * 4, "FREQUENCY (Hz)", 1);
1164  } else {
1165  int w = s->mode == SEPARATE ? s->w / s->nb_display_channels : s->w;
1166  for (y = 0; y < s->h; y += 20) {
1167  dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
1168  dst[s->start_x - 2] = 200;
1169  dst[s->start_x + s->w + 1] = 200;
1170  }
1171  for (y = 0; y < s->h; y += 40) {
1172  dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
1173  dst[s->start_x - 3] = 200;
1174  dst[s->start_x + s->w + 2] = 200;
1175  }
1176  for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
1177  dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
1178  for (x = 0; x < w; x+=40)
1179  dst[x] = 200;
1180  dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x + w * ch;
1181  for (x = 0; x < w; x+=80)
1182  dst[x] = 200;
1183  dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x + w * ch;
1184  for (x = 0; x < w; x+=40) {
1185  dst[x] = 200;
1186  }
1187  dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
1188  for (x = 0; x < w; x+=80) {
1189  dst[x] = 200;
1190  }
1191  for (x = 0; x < w; x += 80) {
1192  float hertz = x * (inlink->sample_rate / 2) / (float)(1 << (int)ceil(log2(w)));
1193  char *units;
1194 
1195  if (hertz == 0)
1196  units = av_asprintf("DC");
1197  else
1198  units = av_asprintf("%.2f", hertz);
1199  if (!units)
1200  return AVERROR(ENOMEM);
1201 
1202  drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->start_y - 12, units, 0);
1203  drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->h + s->start_y + 6, units, 0);
1204  av_free(units);
1205  }
1206  }
1207  for (y = 0; y < s->h; y+=40) {
1208  float seconds = y * spp / inlink->sample_rate;
1209  char *units;
1210 
1211  if (x == 0)
1212  units = av_asprintf("0");
1213  else if (log10(seconds) > 6)
1214  units = av_asprintf("%.2fh", seconds / (60 * 60));
1215  else if (log10(seconds) > 3)
1216  units = av_asprintf("%.2fm", seconds / 60);
1217  else
1218  units = av_asprintf("%.2fs", seconds);
1219  if (!units)
1220  return AVERROR(ENOMEM);
1221 
1222  drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0);
1223  av_free(units);
1224  }
1225  drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 4 * 4, "TIME", 1);
1226  drawtext(s->outpicref, outlink->w / 2 - 14 * 4, outlink->h - s->start_y / 2, "FREQUENCY (Hz)", 0);
1227  }
1228 
1229  for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) {
1230  int h = multi ? s->h / s->nb_display_channels : s->h;
1231 
1232  for (y = 0; y < h; y++) {
1233  float out[3] = { 0., 127.5, 127.5};
1234  int chn;
1235 
1236  for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) {
1237  float yf, uf, vf;
1238  int channel = (multi) ? s->nb_display_channels - ch - 1 : chn;
1239  float lout[3];
1240 
1241  color_range(s, channel, &yf, &uf, &vf);
1242  pick_color(s, yf, uf, vf, y / (float)h, lout);
1243  out[0] += lout[0];
1244  out[1] += lout[1];
1245  out[2] += lout[2];
1246  }
1247  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);
1248  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);
1249  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);
1250  }
1251 
1252  for (y = 0; ch == 0 && y < h; y += h / 10) {
1253  float value = 120.0 * log10(1. - y / (float)h);
1254  char *text;
1255 
1256  if (value < -120)
1257  break;
1258  text = av_asprintf("%.0f dB", value);
1259  if (!text)
1260  continue;
1261  drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 5, text, 0);
1262  av_free(text);
1263  }
1264  }
1265  }
1266 
1267  ret = ff_filter_frame(outlink, s->outpicref);
1268  s->outpicref = NULL;
1269  }
1270 
1271  return ret;
1272 }
1273 
1274 static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1275 {
1276  AVFilterContext *ctx = inlink->dst;
1277  ShowSpectrumContext *s = ctx->priv;
1278  int ret;
1279 
1280  ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
1281  av_frame_free(&insamples);
1282  return ret;
1283 }
1284 
1285 static const AVFilterPad showspectrumpic_inputs[] = {
1286  {
1287  .name = "default",
1288  .type = AVMEDIA_TYPE_AUDIO,
1289  .filter_frame = showspectrumpic_filter_frame,
1290  },
1291  { NULL }
1292 };
1293 
1294 static const AVFilterPad showspectrumpic_outputs[] = {
1295  {
1296  .name = "default",
1297  .type = AVMEDIA_TYPE_VIDEO,
1298  .config_props = config_output,
1299  .request_frame = showspectrumpic_request_frame,
1300  },
1301  { NULL }
1302 };
1303 
1304 AVFilter ff_avf_showspectrumpic = {
1305  .name = "showspectrumpic",
1306  .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
1307  .uninit = uninit,
1308  .query_formats = query_formats,
1309  .priv_size = sizeof(ShowSpectrumContext),
1310  .inputs = showspectrumpic_inputs,
1311  .outputs = showspectrumpic_outputs,
1312  .priv_class = &showspectrumpic_class,
1314 };
1315 
1316 #endif // CONFIG_SHOWSPECTRUMPIC_FILTER
int plane
Definition: avisynth_c.h:422
float, planar
Definition: samplefmt.h:69
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
#define av_realloc_f(p, o, n)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:194
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
av_cold void av_fft_end(FFTContext *s)
Definition: avfft.c:48
float rotation
color rotation
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
Main libavfilter public API header.
FFTComplex ** fft_data
bins holder for each (displayed) channels
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
float * window_func_lut
Window function LUT.
FFTSample re
Definition: avfft.h:38
color_range
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
Definition: avfft.c:38
int fft_bits
number of bits (FFT window size = 1<<fft_bits)
static int query_formats(AVFilterContext *ctx)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:92
static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
int sliding
1 if sliding mode, 0 otherwise
int mode
channel display mode
#define log2(x)
Definition: libm.h:404
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:230
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
DisplayScale
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:331
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:435
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1125
uint8_t
#define av_cold
Definition: attributes.h:82
static const AVOption showspectrum_options[]
AVOptions.
static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:287
static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int flags
Definition: log.c:57
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define lrintf(x)
Definition: libm_mips.h:70
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
SlideMode
#define cm
Definition: dvbsubdec.c:37
A filter pad used for either input or output.
Definition: internal.h:54
static int plot_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define PHASE(y, ch)
const uint8_t avpriv_cga_font[2048]
Definition: xga_font_data.c:29
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
static const uint16_t mask[17]
Definition: lzw.c:38
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
float ** color_buffer
color buffer (3 * h * ch items)
void * priv
private data for use by the filter
Definition: avfilter.h:338
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:439
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
float saturation
color saturation multiplier
#define cbrt
Definition: tablegen.h:35
const char * arg
Definition: jacosubdec.c:66
float * combine_buffer
color combining buffer (3 * h items)
simple assert() macros that are a bit more flexible than ISO C assert().
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
Definition: avfft.c:28
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:90
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:228
Definition: fft.h:88
audio channel layout utility functions
static int request_frame(AVFilterLink *outlink)
Definition: aeval.c:274
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
#define M_PI_2
Definition: mathematics.h:55
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: aeval.c:421
AVFormatContext * ctx
Definition: movenc.c:48
ColorMode
static void acalc_magnitudes(ShowSpectrumContext *s)
#define FLAGS
int n
Definition: avisynth_c.h:684
#define MAGNITUDE(y, ch)
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:401
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:489
#define FF_ARRAY_ELEMS(a)
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:479
A list of supported channel layouts.
Definition: formats.h:85
Orientation
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
static int config_output(AVFilterLink *outlink)
static void color_range(ShowSpectrumContext *s, int ch, float *yf, float *uf, float *vf)
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
static const struct ColorTable color_table[][8]
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:225
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
FFT functions.
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
void ff_generate_window_func(float *lut, int N, int win_func, float *overlap)
Definition: window_func.c:26
AVFILTER_DEFINE_CLASS(showspectrum)
Rational number (pair of numerator and denominator).
Definition: rational.h:58
const char * name
Filter name.
Definition: avfilter.h:148
#define u(width,...)
offset must point to two consecutive integers
Definition: opt.h:233
DataMode
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:335
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:557
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:363
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:208
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:112
static av_cold void uninit(AVFilterContext *ctx)
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:201
static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
int
FFTSample im
Definition: avfft.h:38
if(ret< 0)
Definition: vf_mcdeint.c:282
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
FFTContext ** fft
Fast Fourier Transform context.
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:76
int den
Denominator.
Definition: rational.h:60
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_YASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
avfilter_execute_func * execute
Definition: internal.h:155
#define av_free(p)
Audio FIFO Buffer.
A list of supported formats for one end of a filter link.
Definition: formats.h:64
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:138
An instance of a filter.
Definition: avfilter.h:323
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define OFFSET(x)
FILE * out
Definition: movenc.c:54
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
#define M_PI
Definition: mathematics.h:52
static void scale_magnitudes(ShowSpectrumContext *s, float scale)
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:405
formats
Definition: signature.h:48
static void pick_color(ShowSpectrumContext *s, float yf, float uf, float vf, float a, float *out)
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:241
int xpos
x position (current column)
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
Definition: avfft.c:43
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
static void clear_combine_buffer(ShowSpectrumContext *s, int size)
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:260
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:326
for(j=16;j >0;--j)
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
CGA/EGA/VGA ROM font data.
int color_mode
display color scheme
DisplayMode