FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avf_showfreqs.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <math.h>
22 
23 #include "libavcodec/avfft.h"
24 #include "libavutil/audio_fifo.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/avstring.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/parseutils.h"
31 #include "audio.h"
32 #include "video.h"
33 #include "avfilter.h"
34 #include "internal.h"
35 
43 
44 typedef struct ShowFreqsContext {
45  const AVClass *class;
46  int w, h;
47  int mode;
48  int fft_bits;
49  int ascale, fscale;
50  int avg;
51  int win_func;
54  float **avg_data;
56  float overlap;
59  int nb_freq;
60  int win_size;
61  float scale;
62  char *colors;
64  int64_t pts;
66 
67 #define OFFSET(x) offsetof(ShowFreqsContext, x)
68 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
69 
70 static const AVOption showfreqs_options[] = {
71  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
72  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
73  { "mode", "set display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=BAR}, 0, NB_MODES-1, FLAGS, "mode" },
74  { "line", "show lines", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "mode" },
75  { "bar", "show bars", 0, AV_OPT_TYPE_CONST, {.i64=BAR}, 0, 0, FLAGS, "mode" },
76  { "dot", "show dots", 0, AV_OPT_TYPE_CONST, {.i64=DOT}, 0, 0, FLAGS, "mode" },
77  { "ascale", "set amplitude scale", OFFSET(ascale), AV_OPT_TYPE_INT, {.i64=AS_LOG}, 0, NB_ASCALES-1, FLAGS, "ascale" },
78  { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=AS_LINEAR}, 0, 0, FLAGS, "ascale" },
79  { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=AS_SQRT}, 0, 0, FLAGS, "ascale" },
80  { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=AS_CBRT}, 0, 0, FLAGS, "ascale" },
81  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=AS_LOG}, 0, 0, FLAGS, "ascale" },
82  { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=FS_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
83  { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=FS_LINEAR}, 0, 0, FLAGS, "fscale" },
84  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_LOG}, 0, 0, FLAGS, "fscale" },
85  { "rlog", "reverse logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_RLOG}, 0, 0, FLAGS, "fscale" },
86  { "win_size", "set window size", OFFSET(fft_bits), AV_OPT_TYPE_INT, {.i64=11}, 4, 16, FLAGS, "fft" },
87  { "w16", 0, 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, "fft" },
88  { "w32", 0, 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, FLAGS, "fft" },
89  { "w64", 0, 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, FLAGS, "fft" },
90  { "w128", 0, 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, FLAGS, "fft" },
91  { "w256", 0, 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, FLAGS, "fft" },
92  { "w512", 0, 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, FLAGS, "fft" },
93  { "w1024", 0, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, FLAGS, "fft" },
94  { "w2048", 0, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, FLAGS, "fft" },
95  { "w4096", 0, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, FLAGS, "fft" },
96  { "w8192", 0, 0, AV_OPT_TYPE_CONST, {.i64=13}, 0, 0, FLAGS, "fft" },
97  { "w16384", 0, 0, AV_OPT_TYPE_CONST, {.i64=14}, 0, 0, FLAGS, "fft" },
98  { "w32768", 0, 0, AV_OPT_TYPE_CONST, {.i64=15}, 0, 0, FLAGS, "fft" },
99  { "w65536", 0, 0, AV_OPT_TYPE_CONST, {.i64=16}, 0, 0, FLAGS, "fft" },
100  { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
101  { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
102  { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
103  { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
104  { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
105  { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
106  { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
107  { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
108  { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
109  { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
110  { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
111  { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
112  { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
113  { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=1.}, 0., 1., FLAGS },
114  { "averaging", "set time averaging", OFFSET(avg), AV_OPT_TYPE_INT, {.i64=1}, 0, INT32_MAX, FLAGS },
115  { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
116  { NULL }
117 };
118 
119 AVFILTER_DEFINE_CLASS(showfreqs);
120 
122 {
125  AVFilterLink *inlink = ctx->inputs[0];
126  AVFilterLink *outlink = ctx->outputs[0];
128  static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
129 
130  /* set input audio formats */
131  formats = ff_make_format_list(sample_fmts);
132  if (!formats)
133  return AVERROR(ENOMEM);
134  ff_formats_ref(formats, &inlink->out_formats);
135 
136  layouts = ff_all_channel_layouts();
137  if (!layouts)
138  return AVERROR(ENOMEM);
139  ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
140 
141  formats = ff_all_samplerates();
142  if (!formats)
143  return AVERROR(ENOMEM);
144  ff_formats_ref(formats, &inlink->out_samplerates);
145 
146  /* set output video format */
147  formats = ff_make_format_list(pix_fmts);
148  if (!formats)
149  return AVERROR(ENOMEM);
150  ff_formats_ref(formats, &outlink->in_formats);
151 
152  return 0;
153 }
154 
155 static void generate_window_func(float *lut, int N, int win_func, float *overlap)
156 {
157  int n;
158 
159  switch (win_func) {
160  case WFUNC_RECT:
161  for (n = 0; n < N; n++)
162  lut[n] = 1.;
163  *overlap = 0.;
164  break;
165  case WFUNC_BARTLETT:
166  for (n = 0; n < N; n++)
167  lut[n] = 1.-FFABS((n-(N-1)/2.)/((N-1)/2.));
168  *overlap = 0.5;
169  break;
170  case WFUNC_HANNING:
171  for (n = 0; n < N; n++)
172  lut[n] = .5*(1-cos(2*M_PI*n/(N-1)));
173  *overlap = 0.5;
174  break;
175  case WFUNC_HAMMING:
176  for (n = 0; n < N; n++)
177  lut[n] = .54-.46*cos(2*M_PI*n/(N-1));
178  *overlap = 0.5;
179  break;
180  case WFUNC_BLACKMAN:
181  for (n = 0; n < N; n++)
182  lut[n] = .42659-.49656*cos(2*M_PI*n/(N-1))+.076849*cos(4*M_PI*n/(N-1));
183  *overlap = 0.661;
184  break;
185  case WFUNC_WELCH:
186  for (n = 0; n < N; n++)
187  lut[n] = 1.-(n-(N-1)/2.)/((N-1)/2.)*(n-(N-1)/2.)/((N-1)/2.);
188  *overlap = 0.293;
189  break;
190  case WFUNC_FLATTOP:
191  for (n = 0; n < N; n++)
192  lut[n] = 1.-1.985844164102*cos( 2*M_PI*n/(N-1))+1.791176438506*cos( 4*M_PI*n/(N-1))-
193  1.282075284005*cos( 6*M_PI*n/(N-1))+0.667777530266*cos( 8*M_PI*n/(N-1))-
194  0.240160796576*cos(10*M_PI*n/(N-1))+0.056656381764*cos(12*M_PI*n/(N-1))-
195  0.008134974479*cos(14*M_PI*n/(N-1))+0.000624544650*cos(16*M_PI*n/(N-1))-
196  0.000019808998*cos(18*M_PI*n/(N-1))+0.000000132974*cos(20*M_PI*n/(N-1));
197  *overlap = 0.841;
198  break;
199  case WFUNC_BHARRIS:
200  for (n = 0; n < N; n++)
201  lut[n] = 0.35875-0.48829*cos(2*M_PI*n/(N-1))+0.14128*cos(4*M_PI*n/(N-1))-0.01168*cos(6*M_PI*n/(N-1));
202  *overlap = 0.661;
203  break;
204  case WFUNC_BNUTTALL:
205  for (n = 0; n < N; n++)
206  lut[n] = 0.3635819-0.4891775*cos(2*M_PI*n/(N-1))+0.1365995*cos(4*M_PI*n/(N-1))-0.0106411*cos(6*M_PI*n/(N-1));
207  *overlap = 0.661;
208  break;
209  case WFUNC_BHANN:
210  for (n = 0; n < N; n++)
211  lut[n] = 0.62-0.48*FFABS(n/(double)(N-1)-.5)-0.38*cos(2*M_PI*n/(N-1));
212  *overlap = 0.5;
213  break;
214  case WFUNC_SINE:
215  for (n = 0; n < N; n++)
216  lut[n] = sin(M_PI*n/(N-1));
217  *overlap = 0.75;
218  break;
219  case WFUNC_NUTTALL:
220  for (n = 0; n < N; n++)
221  lut[n] = 0.355768-0.487396*cos(2*M_PI*n/(N-1))+0.144232*cos(4*M_PI*n/(N-1))-0.012604*cos(6*M_PI*n/(N-1));
222  *overlap = 0.663;
223  break;
224  default:
225  av_assert0(0);
226  }
227 }
228 
229 static int config_output(AVFilterLink *outlink)
230 {
231  AVFilterContext *ctx = outlink->src;
232  AVFilterLink *inlink = ctx->inputs[0];
233  ShowFreqsContext *s = ctx->priv;
234  float overlap;
235  int i;
236 
237  s->nb_freq = 1 << (s->fft_bits - 1);
238  s->win_size = s->nb_freq << 1;
240  av_fft_end(s->fft);
241  s->fft = av_fft_init(s->fft_bits, 0);
242  if (!s->fft) {
243  av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
244  "The window size might be too high.\n");
245  return AVERROR(ENOMEM);
246  }
247 
248  /* FFT buffers: x2 for each (display) channel buffer.
249  * Note: we use free and malloc instead of a realloc-like function to
250  * make sure the buffer is aligned in memory for the FFT functions. */
251  for (i = 0; i < s->nb_channels; i++) {
252  av_freep(&s->fft_data[i]);
253  av_freep(&s->avg_data[i]);
254  }
255  av_freep(&s->fft_data);
256  av_freep(&s->avg_data);
257  s->nb_channels = inlink->channels;
258 
259  s->fft_data = av_calloc(s->nb_channels, sizeof(*s->fft_data));
260  if (!s->fft_data)
261  return AVERROR(ENOMEM);
262  s->avg_data = av_calloc(s->nb_channels, sizeof(*s->avg_data));
263  if (!s->fft_data)
264  return AVERROR(ENOMEM);
265  for (i = 0; i < s->nb_channels; i++) {
266  s->fft_data[i] = av_calloc(s->win_size, sizeof(**s->fft_data));
267  s->avg_data[i] = av_calloc(s->nb_freq, sizeof(**s->avg_data));
268  if (!s->fft_data[i] || !s->avg_data[i])
269  return AVERROR(ENOMEM);
270  }
271 
272  /* pre-calc windowing function */
274  sizeof(*s->window_func_lut));
275  if (!s->window_func_lut)
276  return AVERROR(ENOMEM);
278  if (s->overlap == 1.)
279  s->overlap = overlap;
280  s->skip_samples = (1. - s->overlap) * s->win_size;
281  if (s->skip_samples < 1) {
282  av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
283  return AVERROR(EINVAL);
284  }
285 
286  for (s->scale = 0, i = 0; i < s->win_size; i++) {
287  s->scale += s->window_func_lut[i] * s->window_func_lut[i];
288  }
289 
290  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
291  outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
292  outlink->sample_aspect_ratio = (AVRational){1,1};
293  outlink->w = s->w;
294  outlink->h = s->h;
295 
296  s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
297  if (!s->fifo)
298  return AVERROR(ENOMEM);
299  return 0;
300 }
301 
302 static inline void draw_dot(AVFrame *out, int x, int y, uint8_t fg[4])
303 {
304 
305  uint32_t color = AV_RL32(out->data[0] + y * out->linesize[0] + x * 4);
306 
307  if ((color & 0xffffff) != 0)
308  AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg) | color);
309  else
310  AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
311 }
312 
313 static int get_sx(ShowFreqsContext *s, int f)
314 {
315  switch (s->fscale) {
316  case FS_LINEAR:
317  return (s->w/(float)s->nb_freq)*f;
318  case FS_LOG:
319  return s->w-pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.));
320  case FS_RLOG:
321  return pow(s->w, f/(s->nb_freq-1.));
322  }
323 
324  return 0;
325 }
326 
327 static float get_bsize(ShowFreqsContext *s, int f)
328 {
329  switch (s->fscale) {
330  case FS_LINEAR:
331  return s->w/(float)s->nb_freq;
332  case FS_LOG:
333  return pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.))-
334  pow(s->w, (s->nb_freq-f-2)/(s->nb_freq-1.));
335  case FS_RLOG:
336  return pow(s->w, (f+1)/(s->nb_freq-1.))-
337  pow(s->w, f /(s->nb_freq-1.));
338  }
339 
340  return 1.;
341 }
342 
343 static inline void plot_freq(ShowFreqsContext *s, int ch,
344  double a, int f, uint8_t fg[4], int *prev_y,
345  AVFrame *out, AVFilterLink *outlink)
346 {
347  const int w = s->w;
348  const float avg = s->avg_data[ch][f];
349  const float bsize = get_bsize(s, f);
350  const int sx = get_sx(s, f);
351  int x, y, i;
352 
353  switch(s->ascale) {
354  case AS_SQRT:
355  a = 1.0 - sqrt(a);
356  break;
357  case AS_CBRT:
358  a = 1.0 - cbrt(a);
359  break;
360  case AS_LOG:
361  a = log(av_clipd(a, 1e-6, 1)) / log(1e-6);
362  break;
363  case AS_LINEAR:
364  a = 1.0 - a;
365  break;
366  }
367  y = a * outlink->h - 1;
368  if (y < 0)
369  return;
370 
371  switch (s->avg) {
372  case 0:
373  y = s->avg_data[ch][f] = !outlink->frame_count ? y : FFMIN(avg, y);
374  break;
375  case 1:
376  break;
377  default:
378  s->avg_data[ch][f] = avg + y * (y - avg) / (FFMIN(outlink->frame_count + 1, s->avg) * y);
379  y = s->avg_data[ch][f];
380  break;
381  }
382 
383  switch(s->mode) {
384  case LINE:
385  if (*prev_y == -1) {
386  *prev_y = y;
387  }
388  if (y <= *prev_y) {
389  for (x = sx + 1; x < sx + bsize && x < w; x++)
390  draw_dot(out, x, y, fg);
391  for (i = y; i <= *prev_y; i++)
392  draw_dot(out, sx, i, fg);
393  } else {
394  for (i = *prev_y; i <= y; i++)
395  draw_dot(out, sx, i, fg);
396  for (x = sx + 1; x < sx + bsize && x < w; x++)
397  draw_dot(out, x, i - 1, fg);
398  }
399  *prev_y = y;
400  break;
401  case BAR:
402  for (x = sx; x < sx + bsize && x < w; x++)
403  for (i = y; i < outlink->h; i++)
404  draw_dot(out, x, i, fg);
405  break;
406  case DOT:
407  for (x = sx; x < sx + bsize && x < w; x++)
408  draw_dot(out, x, y, fg);
409  break;
410  }
411 }
412 
413 static int plot_freqs(AVFilterLink *inlink, AVFrame *in)
414 {
415  AVFilterContext *ctx = inlink->dst;
416  AVFilterLink *outlink = ctx->outputs[0];
417  ShowFreqsContext *s = ctx->priv;
418  const int win_size = s->win_size;
419  char *colors, *color, *saveptr = NULL;
420  AVFrame *out;
421  int ch, n;
422 
423  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
424  if (!out)
425  return AVERROR(ENOMEM);
426 
427  for (n = 0; n < outlink->h; n++)
428  memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4);
429 
430  /* fill FFT input with the number of samples available */
431  for (ch = 0; ch < s->nb_channels; ch++) {
432  const float *p = (float *)in->extended_data[ch];
433 
434  for (n = 0; n < in->nb_samples; n++) {
435  s->fft_data[ch][n].re = p[n] * s->window_func_lut[n];
436  s->fft_data[ch][n].im = 0;
437  }
438  for (; n < win_size; n++) {
439  s->fft_data[ch][n].re = 0;
440  s->fft_data[ch][n].im = 0;
441  }
442  }
443 
444  /* run FFT on each samples set */
445  for (ch = 0; ch < s->nb_channels; ch++) {
446  av_fft_permute(s->fft, s->fft_data[ch]);
447  av_fft_calc(s->fft, s->fft_data[ch]);
448  }
449 
450 #define RE(x, ch) s->fft_data[ch][x].re
451 #define IM(x, ch) s->fft_data[ch][x].im
452 #define M(a, b) (sqrt((a) * (a) + (b) * (b)))
453 
454  colors = av_strdup(s->colors);
455  if (!colors) {
456  av_frame_free(&out);
457  return AVERROR(ENOMEM);
458  }
459 
460  for (ch = 0; ch < s->nb_channels; ch++) {
461  uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
462  int prev_y = -1, f;
463  double a;
464 
465  color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
466  if (color)
467  av_parse_color(fg, color, -1, ctx);
468 
469  a = av_clipd(M(RE(0, ch), 0) / s->scale, 0, 1);
470  plot_freq(s, ch, a, 0, fg, &prev_y, out, outlink);
471 
472  for (f = 1; f < s->nb_freq; f++) {
473  a = av_clipd(M(RE(f, ch), IM(f, ch)) / s->scale, 0, 1);
474 
475  plot_freq(s, ch, a, f, fg, &prev_y, out, outlink);
476  }
477  }
478 
479  av_free(colors);
480  out->pts = in->pts;
481  return ff_filter_frame(outlink, out);
482 }
483 
484 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
485 {
486  AVFilterContext *ctx = inlink->dst;
487  ShowFreqsContext *s = ctx->priv;
488  AVFrame *fin = NULL;
489  int ret = 0;
490 
491  av_audio_fifo_write(s->fifo, (void **)in->extended_data, in->nb_samples);
492  while (av_audio_fifo_size(s->fifo) >= s->win_size) {
493  fin = ff_get_audio_buffer(inlink, s->win_size);
494  if (!fin) {
495  ret = AVERROR(ENOMEM);
496  goto fail;
497  }
498 
499  fin->pts = s->pts;
500  s->pts += s->skip_samples;
501  ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
502  if (ret < 0)
503  goto fail;
504 
505  ret = plot_freqs(inlink, fin);
506  av_frame_free(&fin);
508  if (ret < 0)
509  goto fail;
510  }
511 
512 fail:
513  av_frame_free(&fin);
514  av_frame_free(&in);
515  return ret;
516 }
517 
518 static av_cold void uninit(AVFilterContext *ctx)
519 {
520  ShowFreqsContext *s = ctx->priv;
521  int i;
522 
523  av_fft_end(s->fft);
524  for (i = 0; i < s->nb_channels; i++) {
525  av_freep(&s->fft_data[i]);
526  av_freep(&s->avg_data[i]);
527  }
528  av_freep(&s->fft_data);
529  av_freep(&s->avg_data);
532 }
533 
534 static const AVFilterPad showfreqs_inputs[] = {
535  {
536  .name = "default",
537  .type = AVMEDIA_TYPE_AUDIO,
538  .filter_frame = filter_frame,
539  },
540  { NULL }
541 };
542 
543 static const AVFilterPad showfreqs_outputs[] = {
544  {
545  .name = "default",
546  .type = AVMEDIA_TYPE_VIDEO,
547  .config_props = config_output,
548  },
549  { NULL }
550 };
551 
553  .name = "showfreqs",
554  .description = NULL_IF_CONFIG_SMALL("Convert input audio to a frequencies video output."),
555  .uninit = uninit,
556  .query_formats = query_formats,
557  .priv_size = sizeof(ShowFreqsContext),
558  .inputs = showfreqs_inputs,
559  .outputs = showfreqs_outputs,
560  .priv_class = &showfreqs_class,
561 };
float, planar
Definition: samplefmt.h:70
#define NULL
Definition: coverity.c:32
DisplayMode
Definition: avf_showfreqs.c:36
FFTContext * fft
Definition: avf_showfreqs.c:52
const char * s
Definition: avisynth_c.h:631
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:60
static int plot_freqs(AVFilterLink *inlink, AVFrame *in)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
#define av_realloc_f(p, o, n)
AVOption.
Definition: opt.h:255
av_cold void av_fft_end(FFTContext *s)
Definition: avfft.c:48
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
#define IM(x, ch)
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
FFTSample re
Definition: avfft.h:38
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
Definition: avfft.c:38
static enum AVSampleFormat formats[]
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:109
static AVRational av_make_q(int num, int den)
Create a rational.
Definition: rational.h:53
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
const char * name
Pad name.
Definition: internal.h:69
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
AVFilter ff_avf_showfreqs
#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:417
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1158
uint8_t
#define av_cold
Definition: attributes.h:74
mode
Definition: f_perms.c:27
AVOptions.
static const AVFilterPad showfreqs_outputs[]
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:94
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
static int get_sx(ShowFreqsContext *s, int f)
#define N
Definition: vf_pp7.c:73
FFTComplex ** fft_data
Definition: avf_showfreqs.c:53
#define av_log(a,...)
int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen, void *log_ctx)
Put the RGBA values that correspond to color_string in rgba_color.
Definition: parseutils.c:348
WindowFunc
Definition: avf_showfreqs.c:39
A filter pad used for either input or output.
Definition: internal.h:63
static int query_formats(AVFilterContext *ctx)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static av_always_inline double cbrt(double x)
Definition: libm.h:52
static float get_bsize(ShowFreqsContext *s, int f)
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:74
#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:148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
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 OFFSET(x)
Definition: avf_showfreqs.c:67
#define fail()
Definition: checkasm.h:57
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:95
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
AVFILTER_DEFINE_CLASS(showfreqs)
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:205
Definition: fft.h:88
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:81
float y
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:422
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:68
int n
Definition: avisynth_c.h:547
static int config_output(AVFilterLink *outlink)
Frame requests may need to loop in order to be fulfilled.
Definition: internal.h:374
FrequencyScale
Definition: avf_showfreqs.c:37
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:385
A list of supported channel layouts.
Definition: formats.h:85
static void draw_dot(AVFrame *out, int x, int y, uint8_t fg[4])
static av_cold void uninit(AVFilterContext *ctx)
float * window_func_lut
Definition: avf_showfreqs.c:55
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
static const AVOption showfreqs_options[]
Definition: avf_showfreqs.c:70
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
FFT functions.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad showfreqs_inputs[]
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
rational number numerator/denominator
Definition: rational.h:43
const char * name
Filter name.
Definition: avfilter.h:474
offset must point to two consecutive integers
Definition: opt.h:232
misc parsing utilities
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:209
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:379
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:260
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:113
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:178
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:184
FFTSample im
Definition: avfft.h:38
if(ret< 0)
Definition: vf_mcdeint.c:280
#define M(a, b)
#define av_free(p)
Audio FIFO Buffer.
AmplitudeScale
Definition: avf_showfreqs.c:38
A list of supported formats for one end of a filter link.
Definition: formats.h:64
#define RE(x, ch)
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:139
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
An instance of a filter.
Definition: avfilter.h:633
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
static void plot_freq(ShowFreqsContext *s, int ch, double a, int f, uint8_t fg[4], int *prev_y, AVFrame *out, AVFilterLink *outlink)
#define av_freep(p)
#define M_PI
Definition: mathematics.h:46
internal API functions
#define FLAGS
Definition: avf_showfreqs.c:68
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
Definition: avfft.c:43
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
AVAudioFifo * fifo
Definition: avf_showfreqs.c:63
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
for(j=16;j >0;--j)
#define AV_WL32(p, v)
Definition: intreadwrite.h:426