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.

571 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. int ret;
  124. /* set input audio formats */
  125. formats = ff_make_format_list(sample_fmts);
  126. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  127. return ret;
  128. layouts = ff_all_channel_layouts();
  129. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  130. return ret;
  131. formats = ff_all_samplerates();
  132. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  133. return ret;
  134. /* set output video format */
  135. formats = ff_make_format_list(pix_fmts);
  136. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  137. return ret;
  138. return 0;
  139. }
  140. static void generate_window_func(float *lut, int N, int win_func, float *overlap)
  141. {
  142. int n;
  143. switch (win_func) {
  144. case WFUNC_RECT:
  145. for (n = 0; n < N; n++)
  146. lut[n] = 1.;
  147. *overlap = 0.;
  148. break;
  149. case WFUNC_BARTLETT:
  150. for (n = 0; n < N; n++)
  151. lut[n] = 1.-fabs((n-(N-1)/2.)/((N-1)/2.));
  152. *overlap = 0.5;
  153. break;
  154. case WFUNC_HANNING:
  155. for (n = 0; n < N; n++)
  156. lut[n] = .5*(1-cos(2*M_PI*n/(N-1)));
  157. *overlap = 0.5;
  158. break;
  159. case WFUNC_HAMMING:
  160. for (n = 0; n < N; n++)
  161. lut[n] = .54-.46*cos(2*M_PI*n/(N-1));
  162. *overlap = 0.5;
  163. break;
  164. case WFUNC_BLACKMAN:
  165. for (n = 0; n < N; n++)
  166. lut[n] = .42659-.49656*cos(2*M_PI*n/(N-1))+.076849*cos(4*M_PI*n/(N-1));
  167. *overlap = 0.661;
  168. break;
  169. case WFUNC_WELCH:
  170. for (n = 0; n < N; n++)
  171. lut[n] = 1.-(n-(N-1)/2.)/((N-1)/2.)*(n-(N-1)/2.)/((N-1)/2.);
  172. *overlap = 0.293;
  173. break;
  174. case WFUNC_FLATTOP:
  175. for (n = 0; n < N; n++)
  176. lut[n] = 1.-1.985844164102*cos( 2*M_PI*n/(N-1))+1.791176438506*cos( 4*M_PI*n/(N-1))-
  177. 1.282075284005*cos( 6*M_PI*n/(N-1))+0.667777530266*cos( 8*M_PI*n/(N-1))-
  178. 0.240160796576*cos(10*M_PI*n/(N-1))+0.056656381764*cos(12*M_PI*n/(N-1))-
  179. 0.008134974479*cos(14*M_PI*n/(N-1))+0.000624544650*cos(16*M_PI*n/(N-1))-
  180. 0.000019808998*cos(18*M_PI*n/(N-1))+0.000000132974*cos(20*M_PI*n/(N-1));
  181. *overlap = 0.841;
  182. break;
  183. case WFUNC_BHARRIS:
  184. for (n = 0; n < N; n++)
  185. 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));
  186. *overlap = 0.661;
  187. break;
  188. case WFUNC_BNUTTALL:
  189. for (n = 0; n < N; n++)
  190. 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));
  191. *overlap = 0.661;
  192. break;
  193. case WFUNC_BHANN:
  194. for (n = 0; n < N; n++)
  195. lut[n] = 0.62-0.48*fabs(n/(double)(N-1)-.5)-0.38*cos(2*M_PI*n/(N-1));
  196. *overlap = 0.5;
  197. break;
  198. case WFUNC_SINE:
  199. for (n = 0; n < N; n++)
  200. lut[n] = sin(M_PI*n/(N-1));
  201. *overlap = 0.75;
  202. break;
  203. case WFUNC_NUTTALL:
  204. for (n = 0; n < N; n++)
  205. 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));
  206. *overlap = 0.663;
  207. break;
  208. case WFUNC_LANCZOS:
  209. #define SINC(x) (!(x)) ? 1 : sin(M_PI * (x))/(M_PI * (x));
  210. for (n = 0; n < N; n++)
  211. lut[n] = SINC((2.*n)/(N-1)-1);
  212. *overlap = 0.75;
  213. break;
  214. case WFUNC_GAUSS:
  215. for (n = 0; n < N; n++)
  216. lut[n] = pow(M_E,-0.5*pow((n-(N-1)/2)/(0.4*(N-1)/2.f),2));
  217. *overlap = 0.75;
  218. break;
  219. default:
  220. av_assert0(0);
  221. }
  222. }
  223. static int config_output(AVFilterLink *outlink)
  224. {
  225. AVFilterContext *ctx = outlink->src;
  226. AVFilterLink *inlink = ctx->inputs[0];
  227. ShowFreqsContext *s = ctx->priv;
  228. float overlap;
  229. int i;
  230. s->nb_freq = 1 << (s->fft_bits - 1);
  231. s->win_size = s->nb_freq << 1;
  232. av_audio_fifo_free(s->fifo);
  233. av_fft_end(s->fft);
  234. s->fft = av_fft_init(s->fft_bits, 0);
  235. if (!s->fft) {
  236. av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
  237. "The window size might be too high.\n");
  238. return AVERROR(ENOMEM);
  239. }
  240. /* FFT buffers: x2 for each (display) channel buffer.
  241. * Note: we use free and malloc instead of a realloc-like function to
  242. * make sure the buffer is aligned in memory for the FFT functions. */
  243. for (i = 0; i < s->nb_channels; i++) {
  244. av_freep(&s->fft_data[i]);
  245. av_freep(&s->avg_data[i]);
  246. }
  247. av_freep(&s->fft_data);
  248. av_freep(&s->avg_data);
  249. s->nb_channels = inlink->channels;
  250. s->fft_data = av_calloc(s->nb_channels, sizeof(*s->fft_data));
  251. if (!s->fft_data)
  252. return AVERROR(ENOMEM);
  253. s->avg_data = av_calloc(s->nb_channels, sizeof(*s->avg_data));
  254. if (!s->fft_data)
  255. return AVERROR(ENOMEM);
  256. for (i = 0; i < s->nb_channels; i++) {
  257. s->fft_data[i] = av_calloc(s->win_size, sizeof(**s->fft_data));
  258. s->avg_data[i] = av_calloc(s->nb_freq, sizeof(**s->avg_data));
  259. if (!s->fft_data[i] || !s->avg_data[i])
  260. return AVERROR(ENOMEM);
  261. }
  262. /* pre-calc windowing function */
  263. s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size,
  264. sizeof(*s->window_func_lut));
  265. if (!s->window_func_lut)
  266. return AVERROR(ENOMEM);
  267. generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
  268. if (s->overlap == 1.)
  269. s->overlap = overlap;
  270. s->skip_samples = (1. - s->overlap) * s->win_size;
  271. if (s->skip_samples < 1) {
  272. av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
  273. return AVERROR(EINVAL);
  274. }
  275. for (s->scale = 0, i = 0; i < s->win_size; i++) {
  276. s->scale += s->window_func_lut[i] * s->window_func_lut[i];
  277. }
  278. outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
  279. outlink->sample_aspect_ratio = (AVRational){1,1};
  280. outlink->w = s->w;
  281. outlink->h = s->h;
  282. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
  283. if (!s->fifo)
  284. return AVERROR(ENOMEM);
  285. return 0;
  286. }
  287. static inline void draw_dot(AVFrame *out, int x, int y, uint8_t fg[4])
  288. {
  289. uint32_t color = AV_RL32(out->data[0] + y * out->linesize[0] + x * 4);
  290. if ((color & 0xffffff) != 0)
  291. AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg) | color);
  292. else
  293. AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
  294. }
  295. static int get_sx(ShowFreqsContext *s, int f)
  296. {
  297. switch (s->fscale) {
  298. case FS_LINEAR:
  299. return (s->w/(float)s->nb_freq)*f;
  300. case FS_LOG:
  301. return s->w-pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.));
  302. case FS_RLOG:
  303. return pow(s->w, f/(s->nb_freq-1.));
  304. }
  305. return 0;
  306. }
  307. static float get_bsize(ShowFreqsContext *s, int f)
  308. {
  309. switch (s->fscale) {
  310. case FS_LINEAR:
  311. return s->w/(float)s->nb_freq;
  312. case FS_LOG:
  313. return pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.))-
  314. pow(s->w, (s->nb_freq-f-2)/(s->nb_freq-1.));
  315. case FS_RLOG:
  316. return pow(s->w, (f+1)/(s->nb_freq-1.))-
  317. pow(s->w, f /(s->nb_freq-1.));
  318. }
  319. return 1.;
  320. }
  321. static inline void plot_freq(ShowFreqsContext *s, int ch,
  322. double a, int f, uint8_t fg[4], int *prev_y,
  323. AVFrame *out, AVFilterLink *outlink)
  324. {
  325. const int w = s->w;
  326. const float avg = s->avg_data[ch][f];
  327. const float bsize = get_bsize(s, f);
  328. const int sx = get_sx(s, f);
  329. int x, y, i;
  330. switch(s->ascale) {
  331. case AS_SQRT:
  332. a = 1.0 - sqrt(a);
  333. break;
  334. case AS_CBRT:
  335. a = 1.0 - cbrt(a);
  336. break;
  337. case AS_LOG:
  338. a = log(av_clipd(a, 1e-6, 1)) / log(1e-6);
  339. break;
  340. case AS_LINEAR:
  341. a = 1.0 - a;
  342. break;
  343. }
  344. y = a * outlink->h - 1;
  345. if (y < 0)
  346. return;
  347. switch (s->avg) {
  348. case 0:
  349. y = s->avg_data[ch][f] = !outlink->frame_count ? y : FFMIN(avg, y);
  350. break;
  351. case 1:
  352. break;
  353. default:
  354. s->avg_data[ch][f] = avg + y * (y - avg) / (FFMIN(outlink->frame_count + 1, s->avg) * y);
  355. y = s->avg_data[ch][f];
  356. break;
  357. }
  358. switch(s->mode) {
  359. case LINE:
  360. if (*prev_y == -1) {
  361. *prev_y = y;
  362. }
  363. if (y <= *prev_y) {
  364. for (x = sx + 1; x < sx + bsize && x < w; x++)
  365. draw_dot(out, x, y, fg);
  366. for (i = y; i <= *prev_y; i++)
  367. draw_dot(out, sx, i, fg);
  368. } else {
  369. for (i = *prev_y; i <= y; i++)
  370. draw_dot(out, sx, i, fg);
  371. for (x = sx + 1; x < sx + bsize && x < w; x++)
  372. draw_dot(out, x, i - 1, fg);
  373. }
  374. *prev_y = y;
  375. break;
  376. case BAR:
  377. for (x = sx; x < sx + bsize && x < w; x++)
  378. for (i = y; i < outlink->h; i++)
  379. draw_dot(out, x, i, fg);
  380. break;
  381. case DOT:
  382. for (x = sx; x < sx + bsize && x < w; x++)
  383. draw_dot(out, x, y, fg);
  384. break;
  385. }
  386. }
  387. static int plot_freqs(AVFilterLink *inlink, AVFrame *in)
  388. {
  389. AVFilterContext *ctx = inlink->dst;
  390. AVFilterLink *outlink = ctx->outputs[0];
  391. ShowFreqsContext *s = ctx->priv;
  392. const int win_size = s->win_size;
  393. char *colors, *color, *saveptr = NULL;
  394. AVFrame *out;
  395. int ch, n;
  396. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  397. if (!out)
  398. return AVERROR(ENOMEM);
  399. for (n = 0; n < outlink->h; n++)
  400. memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4);
  401. /* fill FFT input with the number of samples available */
  402. for (ch = 0; ch < s->nb_channels; ch++) {
  403. const float *p = (float *)in->extended_data[ch];
  404. for (n = 0; n < in->nb_samples; n++) {
  405. s->fft_data[ch][n].re = p[n] * s->window_func_lut[n];
  406. s->fft_data[ch][n].im = 0;
  407. }
  408. for (; n < win_size; n++) {
  409. s->fft_data[ch][n].re = 0;
  410. s->fft_data[ch][n].im = 0;
  411. }
  412. }
  413. /* run FFT on each samples set */
  414. for (ch = 0; ch < s->nb_channels; ch++) {
  415. av_fft_permute(s->fft, s->fft_data[ch]);
  416. av_fft_calc(s->fft, s->fft_data[ch]);
  417. }
  418. #define RE(x, ch) s->fft_data[ch][x].re
  419. #define IM(x, ch) s->fft_data[ch][x].im
  420. #define M(a, b) (sqrt((a) * (a) + (b) * (b)))
  421. colors = av_strdup(s->colors);
  422. if (!colors) {
  423. av_frame_free(&out);
  424. return AVERROR(ENOMEM);
  425. }
  426. for (ch = 0; ch < s->nb_channels; ch++) {
  427. uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
  428. int prev_y = -1, f;
  429. double a;
  430. color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
  431. if (color)
  432. av_parse_color(fg, color, -1, ctx);
  433. a = av_clipd(M(RE(0, ch), 0) / s->scale, 0, 1);
  434. plot_freq(s, ch, a, 0, fg, &prev_y, out, outlink);
  435. for (f = 1; f < s->nb_freq; f++) {
  436. a = av_clipd(M(RE(f, ch), IM(f, ch)) / s->scale, 0, 1);
  437. plot_freq(s, ch, a, f, fg, &prev_y, out, outlink);
  438. }
  439. }
  440. av_free(colors);
  441. out->pts = in->pts;
  442. return ff_filter_frame(outlink, out);
  443. }
  444. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  445. {
  446. AVFilterContext *ctx = inlink->dst;
  447. ShowFreqsContext *s = ctx->priv;
  448. AVFrame *fin = NULL;
  449. int ret = 0;
  450. av_audio_fifo_write(s->fifo, (void **)in->extended_data, in->nb_samples);
  451. while (av_audio_fifo_size(s->fifo) >= s->win_size) {
  452. fin = ff_get_audio_buffer(inlink, s->win_size);
  453. if (!fin) {
  454. ret = AVERROR(ENOMEM);
  455. goto fail;
  456. }
  457. fin->pts = s->pts;
  458. s->pts += s->skip_samples;
  459. ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
  460. if (ret < 0)
  461. goto fail;
  462. ret = plot_freqs(inlink, fin);
  463. av_frame_free(&fin);
  464. av_audio_fifo_drain(s->fifo, s->skip_samples);
  465. if (ret < 0)
  466. goto fail;
  467. }
  468. fail:
  469. av_frame_free(&fin);
  470. av_frame_free(&in);
  471. return ret;
  472. }
  473. static av_cold void uninit(AVFilterContext *ctx)
  474. {
  475. ShowFreqsContext *s = ctx->priv;
  476. int i;
  477. av_fft_end(s->fft);
  478. for (i = 0; i < s->nb_channels; i++) {
  479. av_freep(&s->fft_data[i]);
  480. av_freep(&s->avg_data[i]);
  481. }
  482. av_freep(&s->fft_data);
  483. av_freep(&s->avg_data);
  484. av_freep(&s->window_func_lut);
  485. av_audio_fifo_free(s->fifo);
  486. }
  487. static const AVFilterPad showfreqs_inputs[] = {
  488. {
  489. .name = "default",
  490. .type = AVMEDIA_TYPE_AUDIO,
  491. .filter_frame = filter_frame,
  492. },
  493. { NULL }
  494. };
  495. static const AVFilterPad showfreqs_outputs[] = {
  496. {
  497. .name = "default",
  498. .type = AVMEDIA_TYPE_VIDEO,
  499. .config_props = config_output,
  500. },
  501. { NULL }
  502. };
  503. AVFilter ff_avf_showfreqs = {
  504. .name = "showfreqs",
  505. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a frequencies video output."),
  506. .uninit = uninit,
  507. .query_formats = query_formats,
  508. .priv_size = sizeof(ShowFreqsContext),
  509. .inputs = showfreqs_inputs,
  510. .outputs = showfreqs_outputs,
  511. .priv_class = &showfreqs_class,
  512. };