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"
37 #include "libavutil/parseutils.h"
39 #include "audio.h"
40 #include "video.h"
41 #include "avfilter.h"
42 #include "filters.h"
43 #include "internal.h"
44 #include "window_func.h"
45 
52 
53 typedef struct ShowSpectrumContext {
54  const AVClass *class;
55  int w, h;
56  char *rate_str;
64  int sliding; ///< 1 if sliding mode, 0 otherwise
65  int mode; ///< channel display mode
66  int color_mode; ///< display color scheme
67  int scale;
68  float saturation; ///< color saturation multiplier
69  float rotation; ///< color rotation
70  int start, stop; ///< zoom mode
71  int data;
72  int xpos; ///< x position (current column)
73  FFTContext **fft; ///< Fast Fourier Transform context
74  FFTContext **ifft; ///< Inverse Fast Fourier Transform context
75  int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits)
76  FFTComplex **fft_data; ///< bins holder for each (displayed) channels
77  FFTComplex **fft_scratch; ///< scratch buffers
78  float *window_func_lut; ///< Window function LUT
79  float **magnitudes;
80  float **phases;
81  int win_func;
82  int win_size;
83  int buf_size;
84  double win_scale;
85  float overlap;
86  float gain;
87  int consumed;
88  int hop_size;
89  float *combine_buffer; ///< color combining buffer (3 * h items)
90  float **color_buffer; ///< color buffer (3 * h * ch items)
92  int64_t pts;
93  int64_t old_pts;
94  int old_len;
96  int legend;
99 
100 #define OFFSET(x) offsetof(ShowSpectrumContext, x)
101 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
102 
103 static const AVOption showspectrum_options[] = {
104  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
105  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
106  { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
107  { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
108  { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
109  { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
110  { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
111  { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
112  { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
113  { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
114  { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
115  { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
116  { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
117  { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
118  { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
119  { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
120  { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
121  { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
122  { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
123  { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
124  { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
125  { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
126  { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
127  { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
128  { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
129  { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
130  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
131  { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
132  { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
133  { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
134  { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
135  { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
136  { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
137  { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
138  { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
139  { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
140  { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
141  { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
142  { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
143  { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
144  { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
145  { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
146  { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
147  { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
148  { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
149  { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
150  { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
151  { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
152  { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
153  { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
154  { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
155  { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
156  { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
157  { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
158  { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
159  { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS },
160  { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
161  { "data", "set data mode", OFFSET(data), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_DMODES-1, FLAGS, "data" },
162  { "magnitude", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_MAGNITUDE}, 0, 0, FLAGS, "data" },
163  { "phase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_PHASE}, 0, 0, FLAGS, "data" },
164  { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
165  { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
166  { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
167  { "fps", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = "auto"}, 0, 0, FLAGS },
168  { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
169  { NULL }
170 };
171 
172 AVFILTER_DEFINE_CLASS(showspectrum);
173 
174 static const struct ColorTable {
175  float a, y, u, v;
176 } color_table[][8] = {
177  [INTENSITY] = {
178  { 0, 0, 0, 0 },
179  { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
180  { 0.30, .18572281794568020, .1772436246393981, .17475554840414750 },
181  { 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 },
182  { 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 },
183  { 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 },
184  { 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 },
185  { 1, 1, 0, 0 }},
186  [RAINBOW] = {
187  { 0, 0, 0, 0 },
188  { 0.13, 44/256., (189-128)/256., (138-128)/256. },
189  { 0.25, 29/256., (186-128)/256., (119-128)/256. },
190  { 0.38, 119/256., (194-128)/256., (53-128)/256. },
191  { 0.60, 111/256., (73-128)/256., (59-128)/256. },
192  { 0.73, 205/256., (19-128)/256., (149-128)/256. },
193  { 0.86, 135/256., (83-128)/256., (200-128)/256. },
194  { 1, 73/256., (95-128)/256., (225-128)/256. }},
195  [MORELAND] = {
196  { 0, 44/256., (181-128)/256., (112-128)/256. },
197  { 0.13, 126/256., (177-128)/256., (106-128)/256. },
198  { 0.25, 164/256., (163-128)/256., (109-128)/256. },
199  { 0.38, 200/256., (140-128)/256., (120-128)/256. },
200  { 0.60, 201/256., (117-128)/256., (141-128)/256. },
201  { 0.73, 177/256., (103-128)/256., (165-128)/256. },
202  { 0.86, 136/256., (100-128)/256., (183-128)/256. },
203  { 1, 68/256., (117-128)/256., (203-128)/256. }},
204  [NEBULAE] = {
205  { 0, 10/256., (134-128)/256., (132-128)/256. },
206  { 0.23, 21/256., (137-128)/256., (130-128)/256. },
207  { 0.45, 35/256., (134-128)/256., (134-128)/256. },
208  { 0.57, 51/256., (130-128)/256., (139-128)/256. },
209  { 0.67, 104/256., (116-128)/256., (162-128)/256. },
210  { 0.77, 120/256., (105-128)/256., (188-128)/256. },
211  { 0.87, 140/256., (105-128)/256., (188-128)/256. },
212  { 1, 1, 0, 0 }},
213  [FIRE] = {
214  { 0, 0, 0, 0 },
215  { 0.23, 44/256., (132-128)/256., (127-128)/256. },
216  { 0.45, 62/256., (116-128)/256., (140-128)/256. },
217  { 0.57, 75/256., (105-128)/256., (152-128)/256. },
218  { 0.67, 95/256., (91-128)/256., (166-128)/256. },
219  { 0.77, 126/256., (74-128)/256., (172-128)/256. },
220  { 0.87, 164/256., (73-128)/256., (162-128)/256. },
221  { 1, 1, 0, 0 }},
222  [FIERY] = {
223  { 0, 0, 0, 0 },
224  { 0.23, 36/256., (116-128)/256., (163-128)/256. },
225  { 0.45, 52/256., (102-128)/256., (200-128)/256. },
226  { 0.57, 116/256., (84-128)/256., (196-128)/256. },
227  { 0.67, 157/256., (67-128)/256., (181-128)/256. },
228  { 0.77, 193/256., (40-128)/256., (155-128)/256. },
229  { 0.87, 221/256., (101-128)/256., (134-128)/256. },
230  { 1, 1, 0, 0 }},
231  [FRUIT] = {
232  { 0, 0, 0, 0 },
233  { 0.20, 29/256., (136-128)/256., (119-128)/256. },
234  { 0.30, 60/256., (119-128)/256., (90-128)/256. },
235  { 0.40, 85/256., (91-128)/256., (85-128)/256. },
236  { 0.50, 116/256., (70-128)/256., (105-128)/256. },
237  { 0.60, 151/256., (50-128)/256., (146-128)/256. },
238  { 0.70, 191/256., (63-128)/256., (178-128)/256. },
239  { 1, 98/256., (80-128)/256., (221-128)/256. }},
240  [COOL] = {
241  { 0, 0, 0, 0 },
242  { .15, 0, .5, -.5 },
243  { 1, 1, -.5, .5 }},
244  [MAGMA] = {
245  { 0, 0, 0, 0 },
246  { 0.10, 23/256., (175-128)/256., (120-128)/256. },
247  { 0.23, 43/256., (158-128)/256., (144-128)/256. },
248  { 0.35, 85/256., (138-128)/256., (179-128)/256. },
249  { 0.48, 96/256., (128-128)/256., (189-128)/256. },
250  { 0.64, 128/256., (103-128)/256., (214-128)/256. },
251  { 0.78, 167/256., (85-128)/256., (174-128)/256. },
252  { 1, 205/256., (80-128)/256., (152-128)/256. }},
253  [GREEN] = {
254  { 0, 0, 0, 0 },
255  { .75, .5, 0, -.5 },
256  { 1, 1, 0, 0 }},
257 };
258 
260 {
261  ShowSpectrumContext *s = ctx->priv;
262  int i;
263 
265  if (s->fft) {
266  for (i = 0; i < s->nb_display_channels; i++)
267  av_fft_end(s->fft[i]);
268  }
269  av_freep(&s->fft);
270  if (s->ifft) {
271  for (i = 0; i < s->nb_display_channels; i++)
272  av_fft_end(s->ifft[i]);
273  }
274  av_freep(&s->ifft);
275  if (s->fft_data) {
276  for (i = 0; i < s->nb_display_channels; i++)
277  av_freep(&s->fft_data[i]);
278  }
279  av_freep(&s->fft_data);
280  if (s->fft_scratch) {
281  for (i = 0; i < s->nb_display_channels; i++)
282  av_freep(&s->fft_scratch[i]);
283  }
284  av_freep(&s->fft_scratch);
285  if (s->color_buffer) {
286  for (i = 0; i < s->nb_display_channels; i++)
287  av_freep(&s->color_buffer[i]);
288  }
289  av_freep(&s->color_buffer);
291  if (s->magnitudes) {
292  for (i = 0; i < s->nb_display_channels; i++)
293  av_freep(&s->magnitudes[i]);
294  }
295  av_freep(&s->magnitudes);
298  if (s->phases) {
299  for (i = 0; i < s->nb_display_channels; i++)
300  av_freep(&s->phases[i]);
301  }
302  av_freep(&s->phases);
303 }
304 
306 {
309  AVFilterLink *inlink = ctx->inputs[0];
310  AVFilterLink *outlink = ctx->outputs[0];
313  int ret;
314 
315  /* set input audio formats */
316  formats = ff_make_format_list(sample_fmts);
317  if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
318  return ret;
319 
320  layouts = ff_all_channel_layouts();
321  if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
322  return ret;
323 
324  formats = ff_all_samplerates();
325  if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
326  return ret;
327 
328  /* set output video format */
329  formats = ff_make_format_list(pix_fmts);
330  if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
331  return ret;
332 
333  return 0;
334 }
335 
336 static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
337 {
338  ShowSpectrumContext *s = ctx->priv;
339  AVFilterLink *inlink = ctx->inputs[0];
340  const float *window_func_lut = s->window_func_lut;
341  AVFrame *fin = arg;
342  const int ch = jobnr;
343  int n;
344 
345  /* fill FFT input with the number of samples available */
346  const float *p = (float *)fin->extended_data[ch];
347 
348  for (n = 0; n < s->win_size; n++) {
349  s->fft_data[ch][n].re = p[n] * window_func_lut[n];
350  s->fft_data[ch][n].im = 0;
351  }
352 
353  if (s->stop) {
354  double theta, phi, psi, a, b, S, c;
355  FFTComplex *g = s->fft_data[ch];
356  FFTComplex *h = s->fft_scratch[ch];
357  int L = s->buf_size;
358  int N = s->win_size;
359  int M = s->win_size / 2;
360 
361  phi = 2.0 * M_PI * (s->stop - s->start) / (double)inlink->sample_rate / (M - 1);
362  theta = 2.0 * M_PI * s->start / (double)inlink->sample_rate;
363 
364  for (int n = 0; n < M; n++) {
365  h[n].re = cos(n * n / 2.0 * phi);
366  h[n].im = sin(n * n / 2.0 * phi);
367  }
368 
369  for (int n = M; n < L; n++) {
370  h[n].re = 0.0;
371  h[n].im = 0.0;
372  }
373 
374  for (int n = L - N; n < L; n++) {
375  h[n].re = cos((L - n) * (L - n) / 2.0 * phi);
376  h[n].im = sin((L - n) * (L - n) / 2.0 * phi);
377  }
378 
379  for (int n = 0; n < N; n++) {
380  g[n].re = s->fft_data[ch][n].re;
381  g[n].im = s->fft_data[ch][n].im;
382  }
383 
384  for (int n = N; n < L; n++) {
385  g[n].re = 0.;
386  g[n].im = 0.;
387  }
388 
389  for (int n = 0; n < N; n++) {
390  psi = n * theta + n * n / 2.0 * phi;
391  c = cos(psi);
392  S = -sin(psi);
393  a = c * g[n].re - S * g[n].im;
394  b = S * g[n].re + c * g[n].im;
395  g[n].re = a;
396  g[n].im = b;
397  }
398 
399  av_fft_permute(s->fft[ch], h);
400  av_fft_calc(s->fft[ch], h);
401 
402  av_fft_permute(s->fft[ch], g);
403  av_fft_calc(s->fft[ch], g);
404 
405  for (int n = 0; n < L; n++) {
406  c = g[n].re;
407  S = g[n].im;
408  a = c * h[n].re - S * h[n].im;
409  b = S * h[n].re + c * h[n].im;
410 
411  g[n].re = a / L;
412  g[n].im = b / L;
413  }
414 
415  av_fft_permute(s->ifft[ch], g);
416  av_fft_calc(s->ifft[ch], g);
417 
418  for (int k = 0; k < M; k++) {
419  psi = k * k / 2.0 * phi;
420  c = cos(psi);
421  S = -sin(psi);
422  a = c * g[k].re - S * g[k].im;
423  b = S * g[k].re + c * g[k].im;
424  s->fft_data[ch][k].re = a;
425  s->fft_data[ch][k].im = b;
426  }
427  } else {
428  /* run FFT on each samples set */
429  av_fft_permute(s->fft[ch], s->fft_data[ch]);
430  av_fft_calc(s->fft[ch], s->fft_data[ch]);
431  }
432 
433  return 0;
434 }
435 
436 static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
437 {
438  const uint8_t *font;
439  int font_height;
440  int i;
441 
442  font = avpriv_cga_font, font_height = 8;
443 
444  for (i = 0; txt[i]; i++) {
445  int char_y, mask;
446 
447  if (o) {
448  for (char_y = font_height - 1; char_y >= 0; char_y--) {
449  uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x;
450  for (mask = 0x80; mask; mask >>= 1) {
451  if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
452  p[char_y] = ~p[char_y];
453  p += pic->linesize[0];
454  }
455  }
456  } else {
457  uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8);
458  for (char_y = 0; char_y < font_height; char_y++) {
459  for (mask = 0x80; mask; mask >>= 1) {
460  if (font[txt[i] * font_height + char_y] & mask)
461  *p = ~(*p);
462  p++;
463  }
464  p += pic->linesize[0] - 8;
465  }
466  }
467  }
468 }
469 
471  float *yf, float *uf, float *vf)
472 {
473  switch (s->mode) {
474  case COMBINED:
475  // reduce range by channel count
476  *yf = 256.0f / s->nb_display_channels;
477  switch (s->color_mode) {
478  case RAINBOW:
479  case MORELAND:
480  case NEBULAE:
481  case FIRE:
482  case FIERY:
483  case FRUIT:
484  case COOL:
485  case GREEN:
486  case MAGMA:
487  case INTENSITY:
488  *uf = *yf;
489  *vf = *yf;
490  break;
491  case CHANNEL:
492  /* adjust saturation for mixed UV coloring */
493  /* this factor is correct for infinite channels, an approximation otherwise */
494  *uf = *yf * M_PI;
495  *vf = *yf * M_PI;
496  break;
497  default:
498  av_assert0(0);
499  }
500  break;
501  case SEPARATE:
502  // full range
503  *yf = 256.0f;
504  *uf = 256.0f;
505  *vf = 256.0f;
506  break;
507  default:
508  av_assert0(0);
509  }
510 
511  if (s->color_mode == CHANNEL) {
512  if (s->nb_display_channels > 1) {
513  *uf *= 0.5 * sin((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
514  *vf *= 0.5 * cos((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
515  } else {
516  *uf *= 0.5 * sin(M_PI * s->rotation);
517  *vf *= 0.5 * cos(M_PI * s->rotation + M_PI_2);
518  }
519  } else {
520  *uf += *uf * sin(M_PI * s->rotation);
521  *vf += *vf * cos(M_PI * s->rotation + M_PI_2);
522  }
523 
524  *uf *= s->saturation;
525  *vf *= s->saturation;
526 }
527 
529  float yf, float uf, float vf,
530  float a, float *out)
531 {
532  if (s->color_mode > CHANNEL) {
533  const int cm = s->color_mode;
534  float y, u, v;
535  int i;
536 
537  for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
538  if (color_table[cm][i].a >= a)
539  break;
540  // i now is the first item >= the color
541  // now we know to interpolate between item i - 1 and i
542  if (a <= color_table[cm][i - 1].a) {
543  y = color_table[cm][i - 1].y;
544  u = color_table[cm][i - 1].u;
545  v = color_table[cm][i - 1].v;
546  } else if (a >= color_table[cm][i].a) {
547  y = color_table[cm][i].y;
548  u = color_table[cm][i].u;
549  v = color_table[cm][i].v;
550  } else {
551  float start = color_table[cm][i - 1].a;
552  float end = color_table[cm][i].a;
553  float lerpfrac = (a - start) / (end - start);
554  y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
555  + color_table[cm][i].y * lerpfrac;
556  u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
557  + color_table[cm][i].u * lerpfrac;
558  v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
559  + color_table[cm][i].v * lerpfrac;
560  }
561 
562  out[0] = y * yf;
563  out[1] = u * uf;
564  out[2] = v * vf;
565  } else {
566  out[0] = a * yf;
567  out[1] = a * uf;
568  out[2] = a * vf;
569  }
570 }
571 
572 static char *get_time(AVFilterContext *ctx, float seconds, int x)
573 {
574  char *units;
575 
576  if (x == 0)
577  units = av_asprintf("0");
578  else if (log10(seconds) > 6)
579  units = av_asprintf("%.2fh", seconds / (60 * 60));
580  else if (log10(seconds) > 3)
581  units = av_asprintf("%.2fm", seconds / 60);
582  else
583  units = av_asprintf("%.2fs", seconds);
584  return units;
585 }
586 
587 static int draw_legend(AVFilterContext *ctx, int samples)
588 {
589  ShowSpectrumContext *s = ctx->priv;
590  AVFilterLink *inlink = ctx->inputs[0];
591  AVFilterLink *outlink = ctx->outputs[0];
592  int ch, y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
593  int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL);
594  float spp = samples / (float)sz;
595  char *text;
596  uint8_t *dst;
597  char chlayout_str[128];
598 
599  av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), inlink->channels,
600  inlink->channel_layout);
601 
602  text = av_asprintf("%d Hz | %s", inlink->sample_rate, chlayout_str);
603 
604  drawtext(s->outpicref, 2, outlink->h - 10, "CREATED BY LIBAVFILTER", 0);
605  drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, outlink->h - 10, text, 0);
606  if (s->stop) {
607  char *text = av_asprintf("Zoom: %d Hz - %d Hz", s->start, s->stop);
608  drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, 3, text, 0);
609  av_freep(&text);
610  }
611 
612  av_freep(&text);
613 
614  dst = s->outpicref->data[0] + (s->start_y - 1) * s->outpicref->linesize[0] + s->start_x - 1;
615  for (x = 0; x < s->w + 1; x++)
616  dst[x] = 200;
617  dst = s->outpicref->data[0] + (s->start_y + s->h) * s->outpicref->linesize[0] + s->start_x - 1;
618  for (x = 0; x < s->w + 1; x++)
619  dst[x] = 200;
620  for (y = 0; y < s->h + 2; y++) {
621  dst = s->outpicref->data[0] + (y + s->start_y - 1) * s->outpicref->linesize[0];
622  dst[s->start_x - 1] = 200;
623  dst[s->start_x + s->w] = 200;
624  }
625  if (s->orientation == VERTICAL) {
626  int h = s->mode == SEPARATE ? s->h / s->nb_display_channels : s->h;
627  int hh = s->mode == SEPARATE ? -(s->h % s->nb_display_channels) + 1 : 1;
628  for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
629  for (y = 0; y < h; y += 20) {
630  dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
631  dst[s->start_x - 2] = 200;
632  dst[s->start_x + s->w + 1] = 200;
633  }
634  for (y = 0; y < h; y += 40) {
635  dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
636  dst[s->start_x - 3] = 200;
637  dst[s->start_x + s->w + 2] = 200;
638  }
639  dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x;
640  for (x = 0; x < s->w; x+=40)
641  dst[x] = 200;
642  dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x;
643  for (x = 0; x < s->w; x+=80)
644  dst[x] = 200;
645  dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x;
646  for (x = 0; x < s->w; x+=40) {
647  dst[x] = 200;
648  }
649  dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x;
650  for (x = 0; x < s->w; x+=80) {
651  dst[x] = 200;
652  }
653  for (y = 0; y < h; y += 40) {
654  float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
655  float hertz = s->start + y * range / (float)(1 << (int)ceil(log2(h)));
656  char *units;
657 
658  if (hertz == 0)
659  units = av_asprintf("DC");
660  else
661  units = av_asprintf("%.2f", hertz);
662  if (!units)
663  return AVERROR(ENOMEM);
664 
665  drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4 - hh, units, 0);
666  av_free(units);
667  }
668  }
669 
670  for (x = 0; x < s->w && s->single_pic; x+=80) {
671  float seconds = x * spp / inlink->sample_rate;
672  char *units = get_time(ctx, seconds, x);
673 
674  drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->h + s->start_y + 6, units, 0);
675  drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->start_y - 12, units, 0);
676  av_free(units);
677  }
678 
679  drawtext(s->outpicref, outlink->w / 2 - 4 * 4, outlink->h - s->start_y / 2, "TIME", 0);
680  drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 14 * 4, "FREQUENCY (Hz)", 1);
681  } else {
682  int w = s->mode == SEPARATE ? s->w / s->nb_display_channels : s->w;
683  for (y = 0; y < s->h; y += 20) {
684  dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
685  dst[s->start_x - 2] = 200;
686  dst[s->start_x + s->w + 1] = 200;
687  }
688  for (y = 0; y < s->h; y += 40) {
689  dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
690  dst[s->start_x - 3] = 200;
691  dst[s->start_x + s->w + 2] = 200;
692  }
693  for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
694  dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
695  for (x = 0; x < w; x+=40)
696  dst[x] = 200;
697  dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x + w * ch;
698  for (x = 0; x < w; x+=80)
699  dst[x] = 200;
700  dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x + w * ch;
701  for (x = 0; x < w; x+=40) {
702  dst[x] = 200;
703  }
704  dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
705  for (x = 0; x < w; x+=80) {
706  dst[x] = 200;
707  }
708  for (x = 0; x < w - 79; x += 80) {
709  float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
710  float hertz = s->start + x * range / (float)(1 << (int)ceil(log2(w)));
711  char *units;
712 
713  if (hertz == 0)
714  units = av_asprintf("DC");
715  else
716  units = av_asprintf("%.2f", hertz);
717  if (!units)
718  return AVERROR(ENOMEM);
719 
720  drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->start_y - 12, units, 0);
721  drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->h + s->start_y + 6, units, 0);
722  av_free(units);
723  }
724  }
725  for (y = 0; y < s->h && s->single_pic; y+=40) {
726  float seconds = y * spp / inlink->sample_rate;
727  char *units = get_time(ctx, seconds, x);
728 
729  drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0);
730  av_free(units);
731  }
732  drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 4 * 4, "TIME", 1);
733  drawtext(s->outpicref, outlink->w / 2 - 14 * 4, outlink->h - s->start_y / 2, "FREQUENCY (Hz)", 0);
734  }
735 
736  for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) {
737  int h = multi ? s->h / s->nb_display_channels : s->h;
738 
739  for (y = 0; y < h; y++) {
740  float out[3] = { 0., 127.5, 127.5};
741  int chn;
742 
743  for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) {
744  float yf, uf, vf;
745  int channel = (multi) ? s->nb_display_channels - ch - 1 : chn;
746  float lout[3];
747 
748  color_range(s, channel, &yf, &uf, &vf);
749  pick_color(s, yf, uf, vf, y / (float)h, lout);
750  out[0] += lout[0];
751  out[1] += lout[1];
752  out[2] += lout[2];
753  }
754  memset(s->outpicref->data[0]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[0] + s->w + s->start_x + 20, av_clip_uint8(out[0]), 10);
755  memset(s->outpicref->data[1]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[1] + s->w + s->start_x + 20, av_clip_uint8(out[1]), 10);
756  memset(s->outpicref->data[2]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[2] + s->w + s->start_x + 20, av_clip_uint8(out[2]), 10);
757  }
758 
759  for (y = 0; ch == 0 && y < h; y += h / 10) {
760  float value = 120.0 * log10(1. - y / (float)h);
761  char *text;
762 
763  if (value < -120)
764  break;
765  text = av_asprintf("%.0f dB", value);
766  if (!text)
767  continue;
768  drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 5, text, 0);
769  av_free(text);
770  }
771  }
772 
773  return 0;
774 }
775 
776 static int config_output(AVFilterLink *outlink)
777 {
778  AVFilterContext *ctx = outlink->src;
779  AVFilterLink *inlink = ctx->inputs[0];
780  ShowSpectrumContext *s = ctx->priv;
781  int i, fft_bits, h, w;
782  float overlap;
783 
784  s->stop = FFMIN(s->stop, inlink->sample_rate / 2);
785  if (s->stop && s->stop <= s->start) {
786  av_log(ctx, AV_LOG_ERROR, "Stop frequency should be greater than start.\n");
787  return AVERROR(EINVAL);
788  }
789 
790  if (!strcmp(ctx->filter->name, "showspectrumpic"))
791  s->single_pic = 1;
792 
793  outlink->w = s->w;
794  outlink->h = s->h;
795  outlink->sample_aspect_ratio = (AVRational){1,1};
796 
797  if (s->legend) {
798  s->start_x = (log10(inlink->sample_rate) + 1) * 25;
799  s->start_y = 64;
800  outlink->w += s->start_x * 2;
801  outlink->h += s->start_y * 2;
802  }
803 
804  h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? s->h : s->h / inlink->channels;
805  w = (s->mode == COMBINED || s->orientation == VERTICAL) ? s->w : s->w / inlink->channels;
806  s->channel_height = h;
807  s->channel_width = w;
808 
809  if (s->orientation == VERTICAL) {
810  /* FFT window size (precision) according to the requested output frame height */
811  for (fft_bits = 1; 1 << fft_bits < 2 * h; fft_bits++);
812  } else {
813  /* FFT window size (precision) according to the requested output frame width */
814  for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++);
815  }
816 
817  s->win_size = 1 << fft_bits;
818  s->buf_size = s->win_size << !!s->stop;
819 
820  if (!s->fft) {
821  s->fft = av_calloc(inlink->channels, sizeof(*s->fft));
822  if (!s->fft)
823  return AVERROR(ENOMEM);
824  }
825 
826  if (s->stop) {
827  if (!s->ifft) {
828  s->ifft = av_calloc(inlink->channels, sizeof(*s->ifft));
829  if (!s->ifft)
830  return AVERROR(ENOMEM);
831  }
832  }
833 
834  /* (re-)configuration if the video output changed (or first init) */
835  if (fft_bits != s->fft_bits) {
836  AVFrame *outpicref;
837 
838  s->fft_bits = fft_bits;
839 
840  /* FFT buffers: x2 for each (display) channel buffer.
841  * Note: we use free and malloc instead of a realloc-like function to
842  * make sure the buffer is aligned in memory for the FFT functions. */
843  for (i = 0; i < s->nb_display_channels; i++) {
844  if (s->stop) {
845  av_fft_end(s->ifft[i]);
846  av_freep(&s->fft_scratch[i]);
847  }
848  av_fft_end(s->fft[i]);
849  av_freep(&s->fft_data[i]);
850  }
851  av_freep(&s->fft_data);
852 
853  s->nb_display_channels = inlink->channels;
854  for (i = 0; i < s->nb_display_channels; i++) {
855  s->fft[i] = av_fft_init(fft_bits + !!s->stop, 0);
856  if (s->stop) {
857  s->ifft[i] = av_fft_init(fft_bits + !!s->stop, 1);
858  if (!s->ifft[i]) {
859  av_log(ctx, AV_LOG_ERROR, "Unable to create Inverse FFT context. "
860  "The window size might be too high.\n");
861  return AVERROR(EINVAL);
862  }
863  }
864  if (!s->fft[i]) {
865  av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
866  "The window size might be too high.\n");
867  return AVERROR(EINVAL);
868  }
869  }
870 
871  s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
872  if (!s->magnitudes)
873  return AVERROR(ENOMEM);
874  for (i = 0; i < s->nb_display_channels; i++) {
875  s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
876  if (!s->magnitudes[i])
877  return AVERROR(ENOMEM);
878  }
879 
880  s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases));
881  if (!s->phases)
882  return AVERROR(ENOMEM);
883  for (i = 0; i < s->nb_display_channels; i++) {
884  s->phases[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->phases));
885  if (!s->phases[i])
886  return AVERROR(ENOMEM);
887  }
888 
889  av_freep(&s->color_buffer);
891  if (!s->color_buffer)
892  return AVERROR(ENOMEM);
893  for (i = 0; i < s->nb_display_channels; i++) {
894  s->color_buffer[i] = av_calloc(s->orientation == VERTICAL ? s->h * 3 : s->w * 3, sizeof(**s->color_buffer));
895  if (!s->color_buffer[i])
896  return AVERROR(ENOMEM);
897  }
898 
899  s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
900  if (!s->fft_data)
901  return AVERROR(ENOMEM);
903  if (!s->fft_scratch)
904  return AVERROR(ENOMEM);
905  for (i = 0; i < s->nb_display_channels; i++) {
906  s->fft_data[i] = av_calloc(s->buf_size, sizeof(**s->fft_data));
907  if (!s->fft_data[i])
908  return AVERROR(ENOMEM);
909 
910  s->fft_scratch[i] = av_calloc(s->buf_size, sizeof(**s->fft_scratch));
911  if (!s->fft_scratch[i])
912  return AVERROR(ENOMEM);
913  }
914 
915  /* pre-calc windowing function */
916  s->window_func_lut =
918  sizeof(*s->window_func_lut));
919  if (!s->window_func_lut)
920  return AVERROR(ENOMEM);
922  if (s->overlap == 1)
923  s->overlap = overlap;
924  s->hop_size = (1. - s->overlap) * s->win_size;
925  if (s->hop_size < 1) {
926  av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
927  return AVERROR(EINVAL);
928  }
929 
930  for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
931  s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
932  }
933  s->win_scale = 1. / sqrt(s->win_scale);
934 
935  /* prepare the initial picref buffer (black frame) */
937  s->outpicref = outpicref =
938  ff_get_video_buffer(outlink, outlink->w, outlink->h);
939  if (!outpicref)
940  return AVERROR(ENOMEM);
941  outpicref->sample_aspect_ratio = (AVRational){1,1};
942  for (i = 0; i < outlink->h; i++) {
943  memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
944  memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
945  memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
946  }
947  outpicref->color_range = AVCOL_RANGE_JPEG;
948 
949  if (!s->single_pic && s->legend)
950  draw_legend(ctx, 0);
951  }
952 
953  if ((s->orientation == VERTICAL && s->xpos >= s->w) ||
954  (s->orientation == HORIZONTAL && s->xpos >= s->h))
955  s->xpos = 0;
956 
957  s->auto_frame_rate = av_make_q(inlink->sample_rate, s->hop_size);
958  if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
959  s->auto_frame_rate.den *= s->w;
960  if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
961  s->auto_frame_rate.den *= s->h;
962  if (!s->single_pic && strcmp(s->rate_str, "auto")) {
963  int ret = av_parse_video_rate(&s->frame_rate, s->rate_str);
964  if (ret < 0)
965  return ret;
966  } else {
967  s->frame_rate = s->auto_frame_rate;
968  }
969  outlink->frame_rate = s->frame_rate;
970  outlink->time_base = av_inv_q(outlink->frame_rate);
971 
972  if (s->orientation == VERTICAL) {
973  s->combine_buffer =
974  av_realloc_f(s->combine_buffer, s->h * 3,
975  sizeof(*s->combine_buffer));
976  } else {
977  s->combine_buffer =
978  av_realloc_f(s->combine_buffer, s->w * 3,
979  sizeof(*s->combine_buffer));
980  }
981 
982  av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n",
983  s->w, s->h, s->win_size);
984 
986  s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
987  if (!s->fifo)
988  return AVERROR(ENOMEM);
989  return 0;
990 }
991 
992 #define RE(y, ch) s->fft_data[ch][y].re
993 #define IM(y, ch) s->fft_data[ch][y].im
994 #define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch))
995 #define PHASE(y, ch) atan2(IM(y, ch), RE(y, ch))
996 
997 static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
998 {
999  ShowSpectrumContext *s = ctx->priv;
1000  const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
1001  int y, h = s->orientation == VERTICAL ? s->h : s->w;
1002  const float f = s->gain * w;
1003  const int ch = jobnr;
1004  float *magnitudes = s->magnitudes[ch];
1005 
1006  for (y = 0; y < h; y++)
1007  magnitudes[y] = MAGNITUDE(y, ch) * f;
1008 
1009  return 0;
1010 }
1011 
1012 static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1013 {
1014  ShowSpectrumContext *s = ctx->priv;
1015  const int h = s->orientation == VERTICAL ? s->h : s->w;
1016  const int ch = jobnr;
1017  float *phases = s->phases[ch];
1018  int y;
1019 
1020  for (y = 0; y < h; y++)
1021  phases[y] = (PHASE(y, ch) / M_PI + 1) / 2;
1022 
1023  return 0;
1024 }
1025 
1027 {
1028  const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
1029  int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
1030  const float f = s->gain * w;
1031 
1032  for (ch = 0; ch < s->nb_display_channels; ch++) {
1033  float *magnitudes = s->magnitudes[ch];
1034 
1035  for (y = 0; y < h; y++)
1036  magnitudes[y] += MAGNITUDE(y, ch) * f;
1037  }
1038 }
1039 
1040 static void scale_magnitudes(ShowSpectrumContext *s, float scale)
1041 {
1042  int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
1043 
1044  for (ch = 0; ch < s->nb_display_channels; ch++) {
1045  float *magnitudes = s->magnitudes[ch];
1046 
1047  for (y = 0; y < h; y++)
1048  magnitudes[y] *= scale;
1049  }
1050 }
1051 
1053 {
1054  int y;
1055 
1056  for (y = 0; y < size; y++) {
1057  s->combine_buffer[3 * y ] = 0;
1058  s->combine_buffer[3 * y + 1] = 127.5;
1059  s->combine_buffer[3 * y + 2] = 127.5;
1060  }
1061 }
1062 
1063 static int plot_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1064 {
1065  ShowSpectrumContext *s = ctx->priv;
1066  const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
1067  const int ch = jobnr;
1068  float *magnitudes = s->magnitudes[ch];
1069  float *phases = s->phases[ch];
1070  float yf, uf, vf;
1071  int y;
1072 
1073  /* decide color range */
1074  color_range(s, ch, &yf, &uf, &vf);
1075 
1076  /* draw the channel */
1077  for (y = 0; y < h; y++) {
1078  int row = (s->mode == COMBINED) ? y : ch * h + y;
1079  float *out = &s->color_buffer[ch][3 * row];
1080  float a;
1081 
1082  switch (s->data) {
1083  case D_MAGNITUDE:
1084  /* get magnitude */
1085  a = magnitudes[y];
1086  break;
1087  case D_PHASE:
1088  /* get phase */
1089  a = phases[y];
1090  break;
1091  default:
1092  av_assert0(0);
1093  }
1094 
1095  /* apply scale */
1096  switch (s->scale) {
1097  case LINEAR:
1098  a = av_clipf(a, 0, 1);
1099  break;
1100  case SQRT:
1101  a = av_clipf(sqrt(a), 0, 1);
1102  break;
1103  case CBRT:
1104  a = av_clipf(cbrt(a), 0, 1);
1105  break;
1106  case FOURTHRT:
1107  a = av_clipf(sqrt(sqrt(a)), 0, 1);
1108  break;
1109  case FIFTHRT:
1110  a = av_clipf(pow(a, 0.20), 0, 1);
1111  break;
1112  case LOG:
1113  a = 1 + log10(av_clipd(a, 1e-6, 1)) / 6; // zero = -120dBFS
1114  break;
1115  default:
1116  av_assert0(0);
1117  }
1118 
1119  pick_color(s, yf, uf, vf, a, out);
1120  }
1121 
1122  return 0;
1123 }
1124 
1125 static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
1126 {
1127  AVFilterContext *ctx = inlink->dst;
1128  AVFilterLink *outlink = ctx->outputs[0];
1129  ShowSpectrumContext *s = ctx->priv;
1130  AVFrame *outpicref = s->outpicref;
1131  int ret, plane, x, y, z = s->orientation == VERTICAL ? s->h : s->w;
1132 
1133  /* fill a new spectrum column */
1134  /* initialize buffer for combining to black */
1135  clear_combine_buffer(s, z);
1136 
1138 
1139  for (y = 0; y < z * 3; y++) {
1140  for (x = 0; x < s->nb_display_channels; x++) {
1141  s->combine_buffer[y] += s->color_buffer[x][y];
1142  }
1143  }
1144 
1146  /* copy to output */
1147  if (s->orientation == VERTICAL) {
1148  if (s->sliding == SCROLL) {
1149  for (plane = 0; plane < 3; plane++) {
1150  for (y = 0; y < s->h; y++) {
1151  uint8_t *p = outpicref->data[plane] + s->start_x +
1152  (y + s->start_y) * outpicref->linesize[plane];
1153  memmove(p, p + 1, s->w - 1);
1154  }
1155  }
1156  s->xpos = s->w - 1;
1157  } else if (s->sliding == RSCROLL) {
1158  for (plane = 0; plane < 3; plane++) {
1159  for (y = 0; y < s->h; y++) {
1160  uint8_t *p = outpicref->data[plane] + s->start_x +
1161  (y + s->start_y) * outpicref->linesize[plane];
1162  memmove(p + 1, p, s->w - 1);
1163  }
1164  }
1165  s->xpos = 0;
1166  }
1167  for (plane = 0; plane < 3; plane++) {
1168  uint8_t *p = outpicref->data[plane] + s->start_x +
1169  (outlink->h - 1 - s->start_y) * outpicref->linesize[plane] +
1170  s->xpos;
1171  for (y = 0; y < s->h; y++) {
1172  *p = lrintf(av_clipf(s->combine_buffer[3 * y + plane], 0, 255));
1173  p -= outpicref->linesize[plane];
1174  }
1175  }
1176  } else {
1177  if (s->sliding == SCROLL) {
1178  for (plane = 0; plane < 3; plane++) {
1179  for (y = 1; y < s->h; y++) {
1180  memmove(outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
1181  outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
1182  s->w);
1183  }
1184  }
1185  s->xpos = s->h - 1;
1186  } else if (s->sliding == RSCROLL) {
1187  for (plane = 0; plane < 3; plane++) {
1188  for (y = s->h - 1; y >= 1; y--) {
1189  memmove(outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
1190  outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
1191  s->w);
1192  }
1193  }
1194  s->xpos = 0;
1195  }
1196  for (plane = 0; plane < 3; plane++) {
1197  uint8_t *p = outpicref->data[plane] + s->start_x +
1198  (s->xpos + s->start_y) * outpicref->linesize[plane];
1199  for (x = 0; x < s->w; x++) {
1200  *p = lrintf(av_clipf(s->combine_buffer[3 * x + plane], 0, 255));
1201  p++;
1202  }
1203  }
1204  }
1205 
1206  if (s->sliding != FULLFRAME || s->xpos == 0)
1207  outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
1208 
1209  s->xpos++;
1210  if (s->orientation == VERTICAL && s->xpos >= s->w)
1211  s->xpos = 0;
1212  if (s->orientation == HORIZONTAL && s->xpos >= s->h)
1213  s->xpos = 0;
1214  if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
1215  if (s->old_pts < outpicref->pts) {
1216  if (s->legend) {
1217  char *units = get_time(ctx, insamples->pts /(float)inlink->sample_rate, x);
1218 
1219  if (s->orientation == VERTICAL) {
1220  for (y = 0; y < 10; y++) {
1221  memset(s->outpicref->data[0] + outlink->w / 2 - 4 * s->old_len +
1222  (outlink->h - s->start_y / 2 - 20 + y) * s->outpicref->linesize[0], 0, 10 * s->old_len);
1223  }
1224  drawtext(s->outpicref,
1225  outlink->w / 2 - 4 * strlen(units),
1226  outlink->h - s->start_y / 2 - 20,
1227  units, 0);
1228  } else {
1229  for (y = 0; y < 10 * s->old_len; y++) {
1230  memset(s->outpicref->data[0] + s->start_x / 7 + 20 +
1231  (outlink->h / 2 - 4 * s->old_len + y) * s->outpicref->linesize[0], 0, 10);
1232  }
1233  drawtext(s->outpicref,
1234  s->start_x / 7 + 20,
1235  outlink->h / 2 - 4 * strlen(units),
1236  units, 1);
1237  }
1238  s->old_len = strlen(units);
1239  av_free(units);
1240  }
1241  s->old_pts = outpicref->pts;
1242  ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
1243  if (ret < 0)
1244  return ret;
1245  return 0;
1246  }
1247  }
1248 
1249  return 1;
1250 }
1251 
1252 #if CONFIG_SHOWSPECTRUM_FILTER
1253 
1254 static int activate(AVFilterContext *ctx)
1255 {
1256  AVFilterLink *inlink = ctx->inputs[0];
1257  AVFilterLink *outlink = ctx->outputs[0];
1258  ShowSpectrumContext *s = ctx->priv;
1259  int ret;
1260 
1261  FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
1262 
1263  if (av_audio_fifo_size(s->fifo) < s->win_size) {
1264  AVFrame *frame = NULL;
1265 
1266  ret = ff_inlink_consume_frame(inlink, &frame);
1267  if (ret < 0)
1268  return ret;
1269  if (ret > 0) {
1270  s->pts = frame->pts;
1271  s->consumed = 0;
1272 
1273  av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
1274  av_frame_free(&frame);
1275  }
1276  }
1277 
1278  if (s->outpicref && av_audio_fifo_size(s->fifo) >= s->win_size) {
1279  AVFrame *fin = ff_get_audio_buffer(inlink, s->win_size);
1280  if (!fin)
1281  return AVERROR(ENOMEM);
1282 
1283  fin->pts = s->pts + s->consumed;
1284  s->consumed += s->hop_size;
1285  ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data,
1287  if (ret < 0) {
1288  av_frame_free(&fin);
1289  return ret;
1290  }
1291 
1292  av_assert0(fin->nb_samples == s->win_size);
1293 
1295 
1296  if (s->data == D_MAGNITUDE)
1298 
1299  if (s->data == D_PHASE)
1301 
1302  ret = plot_spectrum_column(inlink, fin);
1303 
1304  av_frame_free(&fin);
1306  if (ret <= 0)
1307  return ret;
1308  }
1309 
1310  if (ff_outlink_get_status(inlink) == AVERROR_EOF &&
1311  s->sliding == FULLFRAME &&
1312  s->xpos > 0 && s->outpicref) {
1313  int64_t pts;
1314 
1315  if (s->orientation == VERTICAL) {
1316  for (int i = 0; i < outlink->h; i++) {
1317  memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos);
1318  memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
1319  memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
1320  }
1321  } else {
1322  for (int i = s->xpos; i < outlink->h; i++) {
1323  memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w);
1324  memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
1325  memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
1326  }
1327  }
1328  s->outpicref->pts += s->consumed;
1329  pts = s->outpicref->pts;
1330  ret = ff_filter_frame(outlink, s->outpicref);
1331  s->outpicref = NULL;
1332  ff_outlink_set_status(outlink, AVERROR_EOF, pts);
1333  return 0;
1334  }
1335 
1336  FF_FILTER_FORWARD_STATUS(inlink, outlink);
1337  if (ff_outlink_frame_wanted(outlink) && av_audio_fifo_size(s->fifo) < s->win_size) {
1338  ff_inlink_request_frame(inlink);
1339  return 0;
1340  }
1341 
1342  if (av_audio_fifo_size(s->fifo) >= s->win_size) {
1343  ff_filter_set_ready(ctx, 10);
1344  return 0;
1345  }
1346  return FFERROR_NOT_READY;
1347 }
1348 
1349 static const AVFilterPad showspectrum_inputs[] = {
1350  {
1351  .name = "default",
1352  .type = AVMEDIA_TYPE_AUDIO,
1353  },
1354  { NULL }
1355 };
1356 
1357 static const AVFilterPad showspectrum_outputs[] = {
1358  {
1359  .name = "default",
1360  .type = AVMEDIA_TYPE_VIDEO,
1361  .config_props = config_output,
1362  },
1363  { NULL }
1364 };
1365 
1367  .name = "showspectrum",
1368  .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
1369  .uninit = uninit,
1370  .query_formats = query_formats,
1371  .priv_size = sizeof(ShowSpectrumContext),
1372  .inputs = showspectrum_inputs,
1373  .outputs = showspectrum_outputs,
1374  .activate = activate,
1375  .priv_class = &showspectrum_class,
1377 };
1378 #endif // CONFIG_SHOWSPECTRUM_FILTER
1379 
1380 #if CONFIG_SHOWSPECTRUMPIC_FILTER
1381 
1382 static const AVOption showspectrumpic_options[] = {
1383  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
1384  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
1385  { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" },
1386  { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
1387  { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
1388  { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" },
1389  { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
1390  { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
1391  { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
1392  { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
1393  { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
1394  { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
1395  { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
1396  { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
1397  { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
1398  { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
1399  { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
1400  { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" },
1401  { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
1402  { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
1403  { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
1404  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
1405  { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
1406  { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
1407  { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
1408  { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
1409  { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
1410  { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
1411  { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1412  { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1413  { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
1414  { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
1415  { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
1416  { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
1417  { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
1418  { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
1419  { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
1420  { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
1421  { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
1422  { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
1423  { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
1424  { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
1425  { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
1426  { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
1427  { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
1428  { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
1429  { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
1430  { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
1431  { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
1432  { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
1433  { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
1434  { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
1435  { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
1436  { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
1437  { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
1438  { NULL }
1439 };
1440 
1441 AVFILTER_DEFINE_CLASS(showspectrumpic);
1442 
1443 static int showspectrumpic_request_frame(AVFilterLink *outlink)
1444 {
1445  AVFilterContext *ctx = outlink->src;
1446  ShowSpectrumContext *s = ctx->priv;
1447  AVFilterLink *inlink = ctx->inputs[0];
1448  int ret, samples;
1449 
1450  ret = ff_request_frame(inlink);
1451  samples = av_audio_fifo_size(s->fifo);
1452  if (ret == AVERROR_EOF && s->outpicref && samples > 0) {
1453  int consumed = 0;
1454  int x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
1455  int ch, spf, spb;
1456  AVFrame *fin;
1457 
1458  spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz))));
1459  spf = FFMAX(1, spf);
1460 
1461  spb = (samples / (spf * sz)) * spf;
1462 
1463  fin = ff_get_audio_buffer(inlink, s->win_size);
1464  if (!fin)
1465  return AVERROR(ENOMEM);
1466 
1467  while (x < sz) {
1468  ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
1469  if (ret < 0) {
1470  av_frame_free(&fin);
1471  return ret;
1472  }
1473 
1474  av_audio_fifo_drain(s->fifo, spf);
1475 
1476  if (ret < s->win_size) {
1477  for (ch = 0; ch < s->nb_display_channels; ch++) {
1478  memset(fin->extended_data[ch] + ret * sizeof(float), 0,
1479  (s->win_size - ret) * sizeof(float));
1480  }
1481  }
1482 
1484  acalc_magnitudes(s);
1485 
1486  consumed += spf;
1487  if (consumed >= spb) {
1488  int h = s->orientation == VERTICAL ? s->h : s->w;
1489 
1490  scale_magnitudes(s, 1. / (consumed / spf));
1491  plot_spectrum_column(inlink, fin);
1492  consumed = 0;
1493  x++;
1494  for (ch = 0; ch < s->nb_display_channels; ch++)
1495  memset(s->magnitudes[ch], 0, h * sizeof(float));
1496  }
1497  }
1498 
1499  av_frame_free(&fin);
1500  s->outpicref->pts = 0;
1501 
1502  if (s->legend)
1503  draw_legend(ctx, samples);
1504 
1505  ret = ff_filter_frame(outlink, s->outpicref);
1506  s->outpicref = NULL;
1507  }
1508 
1509  return ret;
1510 }
1511 
1512 static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1513 {
1514  AVFilterContext *ctx = inlink->dst;
1515  ShowSpectrumContext *s = ctx->priv;
1516  int ret;
1517 
1518  ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
1519  av_frame_free(&insamples);
1520  return ret;
1521 }
1522 
1523 static const AVFilterPad showspectrumpic_inputs[] = {
1524  {
1525  .name = "default",
1526  .type = AVMEDIA_TYPE_AUDIO,
1527  .filter_frame = showspectrumpic_filter_frame,
1528  },
1529  { NULL }
1530 };
1531 
1532 static const AVFilterPad showspectrumpic_outputs[] = {
1533  {
1534  .name = "default",
1535  .type = AVMEDIA_TYPE_VIDEO,
1536  .config_props = config_output,
1537  .request_frame = showspectrumpic_request_frame,
1538  },
1539  { NULL }
1540 };
1541 
1543  .name = "showspectrumpic",
1544  .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
1545  .uninit = uninit,
1546  .query_formats = query_formats,
1547  .priv_size = sizeof(ShowSpectrumContext),
1548  .inputs = showspectrumpic_inputs,
1549  .outputs = showspectrumpic_outputs,
1550  .priv_class = &showspectrumpic_class,
1552 };
1553 
1554 #endif // CONFIG_SHOWSPECTRUMPIC_FILTER
int plane
Definition: avisynth_c.h:422
float, planar
Definition: samplefmt.h:69
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1481
#define NULL
Definition: coverity.c:32
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:226
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:179
static int activate(AVFilterContext *ctx)
Definition: af_adelay.c:237
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:71
Main libavfilter public API header.
const char * g
Definition: vf_curves.c:115
FFTComplex ** fft_data
bins holder for each (displayed) channels
static int draw_legend(AVFilterContext *ctx, int samples)
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
const char * b
Definition: vf_curves.c:116
#define FFERROR_NOT_READY
Filters implementation helper functions.
Definition: filters.h:34
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
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
Definition: window_func.h:36
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:99
int sliding
1 if sliding mode, 0 otherwise
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1607
#define N
Definition: af_mcompand.c:54
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition: filters.h:172
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:244
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:346
#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:1080
#define M(a, b)
Definition: vp3dsp.c:44
static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
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)
#define f(width, name)
Definition: cbs_vp9.c:255
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVRational auto_frame_rate
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:253
static AVFrame * frame
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)
#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
static char * get_time(AVFilterContext *ctx, float seconds, int x)
#define av_log(a,...)
SlideMode
#define cm
Definition: dvbsubdec.c:37
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
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 S(s, c, i)
#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:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
float ** color_buffer
color buffer (3 * h * ch items)
void * priv
private data for use by the filter
Definition: avfilter.h:353
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:471
#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
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
#define FFMIN(a, b)
Definition: common.h:96
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
uint8_t w
Definition: llviddspenc.c:38
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
#define M_PI_2
Definition: mathematics.h:55
AVFormatContext * ctx
Definition: movenc.c:48
ColorMode
static void acalc_magnitudes(ShowSpectrumContext *s)
#define s(width, name)
Definition: cbs_vp9.c:257
#define FLAGS
int n
Definition: avisynth_c.h:684
#define MAGNITUDE(y, ch)
#define L(x)
Definition: vp56_arith.h:36
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
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:540
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define FF_ARRAY_ELEMS(a)
FFTContext ** ifft
Inverse Fast Fourier Transform context.
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:512
A list of supported channel layouts.
Definition: formats.h:85
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
Orientation
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:257
AVFilter ff_avf_showspectrum
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
FFT functions.
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:314
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
Definition: avfilter.c:1630
AVFILTER_DEFINE_CLASS(showspectrum)
Rational number (pair of numerator and denominator).
Definition: rational.h:58
const char * name
Filter name.
Definition: avfilter.h:148
offset must point to two consecutive integers
Definition: opt.h:233
DataMode
misc parsing utilities
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition: filters.h:226
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
static int64_t pts
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:611
#define flags(name, subs,...)
Definition: cbs_av1.c:596
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
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 av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
FFTSample im
Definition: avfft.h:38
if(ret< 0)
Definition: vf_mcdeint.c:279
static double c[64]
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
FFTContext ** fft
Fast Fourier Transform context.
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:193
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
int den
Denominator.
Definition: rational.h:60
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
AVFilter ff_avf_showspectrumpic
An instance of a filter.
Definition: avfilter.h:338
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:407
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:273
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:64
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:292
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:341
for(j=16;j >0;--j)
FFTComplex ** fft_scratch
scratch buffers
CGA/EGA/VGA ROM font data.
int color_mode
display color scheme
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_X86ASM &&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
DisplayMode