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.

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