You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

527 lines
19KB

  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. #include <float.h>
  21. #include <math.h>
  22. #include "libavcodec/avfft.h"
  23. #include "libavutil/audio_fifo.h"
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/parseutils.h"
  30. #include "audio.h"
  31. #include "video.h"
  32. #include "avfilter.h"
  33. #include "internal.h"
  34. #include "window_func.h"
  35. enum DisplayMode { LINE, BAR, DOT, NB_MODES };
  36. enum ChannelMode { COMBINED, SEPARATE, NB_CMODES };
  37. enum FrequencyScale { FS_LINEAR, FS_LOG, FS_RLOG, NB_FSCALES };
  38. enum AmplitudeScale { AS_LINEAR, AS_SQRT, AS_CBRT, AS_LOG, NB_ASCALES };
  39. typedef struct ShowFreqsContext {
  40. const AVClass *class;
  41. int w, h;
  42. int mode;
  43. int cmode;
  44. int fft_bits;
  45. int ascale, fscale;
  46. int avg;
  47. int win_func;
  48. FFTContext *fft;
  49. FFTComplex **fft_data;
  50. float **avg_data;
  51. float *window_func_lut;
  52. float overlap;
  53. float minamp;
  54. int hop_size;
  55. int nb_channels;
  56. int nb_freq;
  57. int win_size;
  58. float scale;
  59. char *colors;
  60. AVAudioFifo *fifo;
  61. int64_t pts;
  62. } ShowFreqsContext;
  63. #define OFFSET(x) offsetof(ShowFreqsContext, x)
  64. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  65. static const AVOption showfreqs_options[] = {
  66. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
  67. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
  68. { "mode", "set display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=BAR}, 0, NB_MODES-1, FLAGS, "mode" },
  69. { "line", "show lines", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "mode" },
  70. { "bar", "show bars", 0, AV_OPT_TYPE_CONST, {.i64=BAR}, 0, 0, FLAGS, "mode" },
  71. { "dot", "show dots", 0, AV_OPT_TYPE_CONST, {.i64=DOT}, 0, 0, FLAGS, "mode" },
  72. { "ascale", "set amplitude scale", OFFSET(ascale), AV_OPT_TYPE_INT, {.i64=AS_LOG}, 0, NB_ASCALES-1, FLAGS, "ascale" },
  73. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=AS_LINEAR}, 0, 0, FLAGS, "ascale" },
  74. { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=AS_SQRT}, 0, 0, FLAGS, "ascale" },
  75. { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=AS_CBRT}, 0, 0, FLAGS, "ascale" },
  76. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=AS_LOG}, 0, 0, FLAGS, "ascale" },
  77. { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=FS_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
  78. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=FS_LINEAR}, 0, 0, FLAGS, "fscale" },
  79. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_LOG}, 0, 0, FLAGS, "fscale" },
  80. { "rlog", "reverse logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_RLOG}, 0, 0, FLAGS, "fscale" },
  81. { "win_size", "set window size", OFFSET(fft_bits), AV_OPT_TYPE_INT, {.i64=11}, 4, 16, FLAGS, "fft" },
  82. { "w16", 0, 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, "fft" },
  83. { "w32", 0, 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, FLAGS, "fft" },
  84. { "w64", 0, 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, FLAGS, "fft" },
  85. { "w128", 0, 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, FLAGS, "fft" },
  86. { "w256", 0, 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, FLAGS, "fft" },
  87. { "w512", 0, 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, FLAGS, "fft" },
  88. { "w1024", 0, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, FLAGS, "fft" },
  89. { "w2048", 0, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, FLAGS, "fft" },
  90. { "w4096", 0, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, FLAGS, "fft" },
  91. { "w8192", 0, 0, AV_OPT_TYPE_CONST, {.i64=13}, 0, 0, FLAGS, "fft" },
  92. { "w16384", 0, 0, AV_OPT_TYPE_CONST, {.i64=14}, 0, 0, FLAGS, "fft" },
  93. { "w32768", 0, 0, AV_OPT_TYPE_CONST, {.i64=15}, 0, 0, FLAGS, "fft" },
  94. { "w65536", 0, 0, AV_OPT_TYPE_CONST, {.i64=16}, 0, 0, FLAGS, "fft" },
  95. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
  96. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
  97. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
  98. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  99. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
  100. { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
  101. { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
  102. { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
  103. { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
  104. { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
  105. { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
  106. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
  107. { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
  108. { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
  109. { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
  110. { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
  111. { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
  112. { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
  113. { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
  114. { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
  115. { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=1.}, 0., 1., FLAGS },
  116. { "averaging", "set time averaging", OFFSET(avg), AV_OPT_TYPE_INT, {.i64=1}, 0, INT32_MAX, FLAGS },
  117. { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
  118. { "cmode", "set channel mode", OFFSET(cmode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_CMODES-1, FLAGS, "cmode" },
  119. { "combined", "show all channels in same window", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "cmode" },
  120. { "separate", "show each channel in own window", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "cmode" },
  121. { "minamp", "set minimum amplitude", OFFSET(minamp), AV_OPT_TYPE_FLOAT, {.dbl=1e-6}, FLT_MIN, 1e-6, FLAGS },
  122. { NULL }
  123. };
  124. AVFILTER_DEFINE_CLASS(showfreqs);
  125. static int query_formats(AVFilterContext *ctx)
  126. {
  127. AVFilterFormats *formats = NULL;
  128. AVFilterChannelLayouts *layouts = NULL;
  129. AVFilterLink *inlink = ctx->inputs[0];
  130. AVFilterLink *outlink = ctx->outputs[0];
  131. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
  132. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
  133. int ret;
  134. /* set input audio formats */
  135. formats = ff_make_format_list(sample_fmts);
  136. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  137. return ret;
  138. layouts = ff_all_channel_layouts();
  139. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  140. return ret;
  141. formats = ff_all_samplerates();
  142. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  143. return ret;
  144. /* set output video format */
  145. formats = ff_make_format_list(pix_fmts);
  146. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  147. return ret;
  148. return 0;
  149. }
  150. static av_cold int init(AVFilterContext *ctx)
  151. {
  152. ShowFreqsContext *s = ctx->priv;
  153. s->pts = AV_NOPTS_VALUE;
  154. return 0;
  155. }
  156. static int config_output(AVFilterLink *outlink)
  157. {
  158. AVFilterContext *ctx = outlink->src;
  159. AVFilterLink *inlink = ctx->inputs[0];
  160. ShowFreqsContext *s = ctx->priv;
  161. float overlap;
  162. int i;
  163. s->nb_freq = 1 << (s->fft_bits - 1);
  164. s->win_size = s->nb_freq << 1;
  165. av_audio_fifo_free(s->fifo);
  166. av_fft_end(s->fft);
  167. s->fft = av_fft_init(s->fft_bits, 0);
  168. if (!s->fft) {
  169. av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
  170. "The window size might be too high.\n");
  171. return AVERROR(ENOMEM);
  172. }
  173. /* FFT buffers: x2 for each (display) channel buffer.
  174. * Note: we use free and malloc instead of a realloc-like function to
  175. * make sure the buffer is aligned in memory for the FFT functions. */
  176. for (i = 0; i < s->nb_channels; i++) {
  177. av_freep(&s->fft_data[i]);
  178. av_freep(&s->avg_data[i]);
  179. }
  180. av_freep(&s->fft_data);
  181. av_freep(&s->avg_data);
  182. s->nb_channels = inlink->channels;
  183. s->fft_data = av_calloc(s->nb_channels, sizeof(*s->fft_data));
  184. if (!s->fft_data)
  185. return AVERROR(ENOMEM);
  186. s->avg_data = av_calloc(s->nb_channels, sizeof(*s->avg_data));
  187. if (!s->fft_data)
  188. return AVERROR(ENOMEM);
  189. for (i = 0; i < s->nb_channels; i++) {
  190. s->fft_data[i] = av_calloc(s->win_size, sizeof(**s->fft_data));
  191. s->avg_data[i] = av_calloc(s->nb_freq, sizeof(**s->avg_data));
  192. if (!s->fft_data[i] || !s->avg_data[i])
  193. return AVERROR(ENOMEM);
  194. }
  195. /* pre-calc windowing function */
  196. s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size,
  197. sizeof(*s->window_func_lut));
  198. if (!s->window_func_lut)
  199. return AVERROR(ENOMEM);
  200. ff_generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
  201. if (s->overlap == 1.)
  202. s->overlap = overlap;
  203. s->hop_size = (1. - s->overlap) * s->win_size;
  204. if (s->hop_size < 1) {
  205. av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
  206. return AVERROR(EINVAL);
  207. }
  208. for (s->scale = 0, i = 0; i < s->win_size; i++) {
  209. s->scale += s->window_func_lut[i] * s->window_func_lut[i];
  210. }
  211. outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
  212. outlink->sample_aspect_ratio = (AVRational){1,1};
  213. outlink->w = s->w;
  214. outlink->h = s->h;
  215. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
  216. if (!s->fifo)
  217. return AVERROR(ENOMEM);
  218. return 0;
  219. }
  220. static inline void draw_dot(AVFrame *out, int x, int y, uint8_t fg[4])
  221. {
  222. uint32_t color = AV_RL32(out->data[0] + y * out->linesize[0] + x * 4);
  223. if ((color & 0xffffff) != 0)
  224. AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg) | color);
  225. else
  226. AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
  227. }
  228. static int get_sx(ShowFreqsContext *s, int f)
  229. {
  230. switch (s->fscale) {
  231. case FS_LINEAR:
  232. return (s->w/(float)s->nb_freq)*f;
  233. case FS_LOG:
  234. return s->w-pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.));
  235. case FS_RLOG:
  236. return pow(s->w, f/(s->nb_freq-1.));
  237. }
  238. return 0;
  239. }
  240. static float get_bsize(ShowFreqsContext *s, int f)
  241. {
  242. switch (s->fscale) {
  243. case FS_LINEAR:
  244. return s->w/(float)s->nb_freq;
  245. case FS_LOG:
  246. return pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.))-
  247. pow(s->w, (s->nb_freq-f-2)/(s->nb_freq-1.));
  248. case FS_RLOG:
  249. return pow(s->w, (f+1)/(s->nb_freq-1.))-
  250. pow(s->w, f /(s->nb_freq-1.));
  251. }
  252. return 1.;
  253. }
  254. static inline void plot_freq(ShowFreqsContext *s, int ch,
  255. double a, int f, uint8_t fg[4], int *prev_y,
  256. AVFrame *out, AVFilterLink *outlink)
  257. {
  258. const int w = s->w;
  259. const float min = s->minamp;
  260. const float avg = s->avg_data[ch][f];
  261. const float bsize = get_bsize(s, f);
  262. const int sx = get_sx(s, f);
  263. int end = outlink->h;
  264. int x, y, i;
  265. switch(s->ascale) {
  266. case AS_SQRT:
  267. a = 1.0 - sqrt(a);
  268. break;
  269. case AS_CBRT:
  270. a = 1.0 - cbrt(a);
  271. break;
  272. case AS_LOG:
  273. a = log(av_clipd(a, min, 1)) / log(min);
  274. break;
  275. case AS_LINEAR:
  276. a = 1.0 - a;
  277. break;
  278. }
  279. switch (s->cmode) {
  280. case COMBINED:
  281. y = a * outlink->h - 1;
  282. break;
  283. case SEPARATE:
  284. end = (outlink->h / s->nb_channels) * (ch + 1);
  285. y = (outlink->h / s->nb_channels) * ch + a * (outlink->h / s->nb_channels) - 1;
  286. break;
  287. default:
  288. av_assert0(0);
  289. }
  290. if (y < 0)
  291. return;
  292. switch (s->avg) {
  293. case 0:
  294. y = s->avg_data[ch][f] = !outlink->frame_count_in ? y : FFMIN(avg, y);
  295. break;
  296. case 1:
  297. break;
  298. default:
  299. s->avg_data[ch][f] = avg + y * (y - avg) / (FFMIN(outlink->frame_count_in + 1, s->avg) * y);
  300. y = s->avg_data[ch][f];
  301. break;
  302. }
  303. switch(s->mode) {
  304. case LINE:
  305. if (*prev_y == -1) {
  306. *prev_y = y;
  307. }
  308. if (y <= *prev_y) {
  309. for (x = sx + 1; x < sx + bsize && x < w; x++)
  310. draw_dot(out, x, y, fg);
  311. for (i = y; i <= *prev_y; i++)
  312. draw_dot(out, sx, i, fg);
  313. } else {
  314. for (i = *prev_y; i <= y; i++)
  315. draw_dot(out, sx, i, fg);
  316. for (x = sx + 1; x < sx + bsize && x < w; x++)
  317. draw_dot(out, x, i - 1, fg);
  318. }
  319. *prev_y = y;
  320. break;
  321. case BAR:
  322. for (x = sx; x < sx + bsize && x < w; x++)
  323. for (i = y; i < end; i++)
  324. draw_dot(out, x, i, fg);
  325. break;
  326. case DOT:
  327. for (x = sx; x < sx + bsize && x < w; x++)
  328. draw_dot(out, x, y, fg);
  329. break;
  330. }
  331. }
  332. static int plot_freqs(AVFilterLink *inlink, AVFrame *in)
  333. {
  334. AVFilterContext *ctx = inlink->dst;
  335. AVFilterLink *outlink = ctx->outputs[0];
  336. ShowFreqsContext *s = ctx->priv;
  337. const int win_size = s->win_size;
  338. char *colors, *color, *saveptr = NULL;
  339. AVFrame *out;
  340. int ch, n;
  341. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  342. if (!out)
  343. return AVERROR(ENOMEM);
  344. for (n = 0; n < outlink->h; n++)
  345. memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4);
  346. /* fill FFT input with the number of samples available */
  347. for (ch = 0; ch < s->nb_channels; ch++) {
  348. const float *p = (float *)in->extended_data[ch];
  349. for (n = 0; n < in->nb_samples; n++) {
  350. s->fft_data[ch][n].re = p[n] * s->window_func_lut[n];
  351. s->fft_data[ch][n].im = 0;
  352. }
  353. for (; n < win_size; n++) {
  354. s->fft_data[ch][n].re = 0;
  355. s->fft_data[ch][n].im = 0;
  356. }
  357. }
  358. /* run FFT on each samples set */
  359. for (ch = 0; ch < s->nb_channels; ch++) {
  360. av_fft_permute(s->fft, s->fft_data[ch]);
  361. av_fft_calc(s->fft, s->fft_data[ch]);
  362. }
  363. #define RE(x, ch) s->fft_data[ch][x].re
  364. #define IM(x, ch) s->fft_data[ch][x].im
  365. #define M(a, b) (sqrt((a) * (a) + (b) * (b)))
  366. colors = av_strdup(s->colors);
  367. if (!colors) {
  368. av_frame_free(&out);
  369. return AVERROR(ENOMEM);
  370. }
  371. for (ch = 0; ch < s->nb_channels; ch++) {
  372. uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
  373. int prev_y = -1, f;
  374. double a;
  375. color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
  376. if (color)
  377. av_parse_color(fg, color, -1, ctx);
  378. a = av_clipd(M(RE(0, ch), 0) / s->scale, 0, 1);
  379. plot_freq(s, ch, a, 0, fg, &prev_y, out, outlink);
  380. for (f = 1; f < s->nb_freq; f++) {
  381. a = av_clipd(M(RE(f, ch), IM(f, ch)) / s->scale, 0, 1);
  382. plot_freq(s, ch, a, f, fg, &prev_y, out, outlink);
  383. }
  384. }
  385. av_free(colors);
  386. out->pts = in->pts;
  387. out->sample_aspect_ratio = (AVRational){1,1};
  388. return ff_filter_frame(outlink, out);
  389. }
  390. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  391. {
  392. AVFilterContext *ctx = inlink->dst;
  393. ShowFreqsContext *s = ctx->priv;
  394. AVFrame *fin = NULL;
  395. int consumed = 0;
  396. int ret = 0;
  397. if (s->pts == AV_NOPTS_VALUE)
  398. s->pts = in->pts - av_audio_fifo_size(s->fifo);
  399. av_audio_fifo_write(s->fifo, (void **)in->extended_data, in->nb_samples);
  400. while (av_audio_fifo_size(s->fifo) >= s->win_size) {
  401. fin = ff_get_audio_buffer(inlink, s->win_size);
  402. if (!fin) {
  403. ret = AVERROR(ENOMEM);
  404. goto fail;
  405. }
  406. fin->pts = s->pts + consumed;
  407. consumed += s->hop_size;
  408. ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
  409. if (ret < 0)
  410. goto fail;
  411. ret = plot_freqs(inlink, fin);
  412. av_frame_free(&fin);
  413. av_audio_fifo_drain(s->fifo, s->hop_size);
  414. if (ret < 0)
  415. goto fail;
  416. }
  417. fail:
  418. s->pts = AV_NOPTS_VALUE;
  419. av_frame_free(&fin);
  420. av_frame_free(&in);
  421. return ret;
  422. }
  423. static av_cold void uninit(AVFilterContext *ctx)
  424. {
  425. ShowFreqsContext *s = ctx->priv;
  426. int i;
  427. av_fft_end(s->fft);
  428. for (i = 0; i < s->nb_channels; i++) {
  429. if (s->fft_data)
  430. av_freep(&s->fft_data[i]);
  431. if (s->avg_data)
  432. av_freep(&s->avg_data[i]);
  433. }
  434. av_freep(&s->fft_data);
  435. av_freep(&s->avg_data);
  436. av_freep(&s->window_func_lut);
  437. av_audio_fifo_free(s->fifo);
  438. }
  439. static const AVFilterPad showfreqs_inputs[] = {
  440. {
  441. .name = "default",
  442. .type = AVMEDIA_TYPE_AUDIO,
  443. .filter_frame = filter_frame,
  444. },
  445. { NULL }
  446. };
  447. static const AVFilterPad showfreqs_outputs[] = {
  448. {
  449. .name = "default",
  450. .type = AVMEDIA_TYPE_VIDEO,
  451. .config_props = config_output,
  452. },
  453. { NULL }
  454. };
  455. AVFilter ff_avf_showfreqs = {
  456. .name = "showfreqs",
  457. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a frequencies video output."),
  458. .init = init,
  459. .uninit = uninit,
  460. .query_formats = query_formats,
  461. .priv_size = sizeof(ShowFreqsContext),
  462. .inputs = showfreqs_inputs,
  463. .outputs = showfreqs_outputs,
  464. .priv_class = &showfreqs_class,
  465. };