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.

550 lines
20KB

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