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.

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