avf_showfreqs.c 18 KB

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