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.

1553 lines
64KB

  1. /*
  2. * Copyright (c) 2012-2013 Clément Bœsch
  3. * Copyright (c) 2013 Rudolf Polzer <divverent@xonotic.org>
  4. * Copyright (c) 2015 Paul B Mahol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * audio to spectrum (video) transmedia filter, based on ffplay rdft showmode
  25. * (by Michael Niedermayer) and lavfi/avf_showwaves (by Stefano Sabatini).
  26. */
  27. #include <math.h>
  28. #include "libavcodec/avfft.h"
  29. #include "libavutil/audio_fifo.h"
  30. #include "libavutil/avassert.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/channel_layout.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/parseutils.h"
  35. #include "libavutil/xga_font_data.h"
  36. #include "audio.h"
  37. #include "video.h"
  38. #include "avfilter.h"
  39. #include "filters.h"
  40. #include "internal.h"
  41. #include "window_func.h"
  42. enum DisplayMode { COMBINED, SEPARATE, NB_MODES };
  43. enum DataMode { D_MAGNITUDE, D_PHASE, NB_DMODES };
  44. enum DisplayScale { LINEAR, SQRT, CBRT, LOG, FOURTHRT, FIFTHRT, NB_SCALES };
  45. enum ColorMode { CHANNEL, INTENSITY, RAINBOW, MORELAND, NEBULAE, FIRE, FIERY, FRUIT, COOL, MAGMA, GREEN, NB_CLMODES };
  46. enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, NB_SLIDES };
  47. enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS };
  48. typedef struct ShowSpectrumContext {
  49. const AVClass *class;
  50. int w, h;
  51. char *rate_str;
  52. AVRational auto_frame_rate;
  53. AVRational frame_rate;
  54. AVFrame *outpicref;
  55. int nb_display_channels;
  56. int orientation;
  57. int channel_width;
  58. int channel_height;
  59. int sliding; ///< 1 if sliding mode, 0 otherwise
  60. int mode; ///< channel display mode
  61. int color_mode; ///< display color scheme
  62. int scale;
  63. float saturation; ///< color saturation multiplier
  64. float rotation; ///< color rotation
  65. int start, stop; ///< zoom mode
  66. int data;
  67. int xpos; ///< x position (current column)
  68. FFTContext **fft; ///< Fast Fourier Transform context
  69. FFTContext **ifft; ///< Inverse Fast Fourier Transform context
  70. int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits)
  71. FFTComplex **fft_data; ///< bins holder for each (displayed) channels
  72. FFTComplex **fft_scratch; ///< scratch buffers
  73. float *window_func_lut; ///< Window function LUT
  74. float **magnitudes;
  75. float **phases;
  76. int win_func;
  77. int win_size;
  78. int buf_size;
  79. double win_scale;
  80. float overlap;
  81. float gain;
  82. int consumed;
  83. int hop_size;
  84. float *combine_buffer; ///< color combining buffer (3 * h items)
  85. float **color_buffer; ///< color buffer (3 * h * ch items)
  86. AVAudioFifo *fifo;
  87. int64_t pts;
  88. int64_t old_pts;
  89. int old_len;
  90. int single_pic;
  91. int legend;
  92. int start_x, start_y;
  93. } ShowSpectrumContext;
  94. #define OFFSET(x) offsetof(ShowSpectrumContext, x)
  95. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  96. static const AVOption showspectrum_options[] = {
  97. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
  98. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
  99. { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
  100. { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
  101. { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
  102. { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
  103. { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
  104. { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
  105. { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
  106. { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
  107. { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
  108. { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
  109. { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
  110. { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
  111. { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
  112. { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
  113. { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
  114. { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
  115. { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
  116. { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
  117. { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
  118. { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
  119. { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
  120. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
  121. { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
  122. { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
  123. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
  124. { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
  125. { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
  126. { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
  127. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
  128. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
  129. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
  130. { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  131. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  132. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
  133. { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
  134. { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
  135. { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
  136. { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
  137. { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
  138. { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
  139. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
  140. { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
  141. { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
  142. { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
  143. { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
  144. { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
  145. { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
  146. { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
  147. { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
  148. { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
  149. { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
  150. { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
  151. { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS },
  152. { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
  153. { "data", "set data mode", OFFSET(data), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_DMODES-1, FLAGS, "data" },
  154. { "magnitude", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_MAGNITUDE}, 0, 0, FLAGS, "data" },
  155. { "phase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_PHASE}, 0, 0, FLAGS, "data" },
  156. { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
  157. { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
  158. { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
  159. { "fps", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = "auto"}, 0, 0, FLAGS },
  160. { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
  161. { NULL }
  162. };
  163. AVFILTER_DEFINE_CLASS(showspectrum);
  164. static const struct ColorTable {
  165. float a, y, u, v;
  166. } color_table[][8] = {
  167. [INTENSITY] = {
  168. { 0, 0, 0, 0 },
  169. { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
  170. { 0.30, .18572281794568020, .1772436246393981, .17475554840414750 },
  171. { 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 },
  172. { 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 },
  173. { 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 },
  174. { 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 },
  175. { 1, 1, 0, 0 }},
  176. [RAINBOW] = {
  177. { 0, 0, 0, 0 },
  178. { 0.13, 44/256., (189-128)/256., (138-128)/256. },
  179. { 0.25, 29/256., (186-128)/256., (119-128)/256. },
  180. { 0.38, 119/256., (194-128)/256., (53-128)/256. },
  181. { 0.60, 111/256., (73-128)/256., (59-128)/256. },
  182. { 0.73, 205/256., (19-128)/256., (149-128)/256. },
  183. { 0.86, 135/256., (83-128)/256., (200-128)/256. },
  184. { 1, 73/256., (95-128)/256., (225-128)/256. }},
  185. [MORELAND] = {
  186. { 0, 44/256., (181-128)/256., (112-128)/256. },
  187. { 0.13, 126/256., (177-128)/256., (106-128)/256. },
  188. { 0.25, 164/256., (163-128)/256., (109-128)/256. },
  189. { 0.38, 200/256., (140-128)/256., (120-128)/256. },
  190. { 0.60, 201/256., (117-128)/256., (141-128)/256. },
  191. { 0.73, 177/256., (103-128)/256., (165-128)/256. },
  192. { 0.86, 136/256., (100-128)/256., (183-128)/256. },
  193. { 1, 68/256., (117-128)/256., (203-128)/256. }},
  194. [NEBULAE] = {
  195. { 0, 10/256., (134-128)/256., (132-128)/256. },
  196. { 0.23, 21/256., (137-128)/256., (130-128)/256. },
  197. { 0.45, 35/256., (134-128)/256., (134-128)/256. },
  198. { 0.57, 51/256., (130-128)/256., (139-128)/256. },
  199. { 0.67, 104/256., (116-128)/256., (162-128)/256. },
  200. { 0.77, 120/256., (105-128)/256., (188-128)/256. },
  201. { 0.87, 140/256., (105-128)/256., (188-128)/256. },
  202. { 1, 1, 0, 0 }},
  203. [FIRE] = {
  204. { 0, 0, 0, 0 },
  205. { 0.23, 44/256., (132-128)/256., (127-128)/256. },
  206. { 0.45, 62/256., (116-128)/256., (140-128)/256. },
  207. { 0.57, 75/256., (105-128)/256., (152-128)/256. },
  208. { 0.67, 95/256., (91-128)/256., (166-128)/256. },
  209. { 0.77, 126/256., (74-128)/256., (172-128)/256. },
  210. { 0.87, 164/256., (73-128)/256., (162-128)/256. },
  211. { 1, 1, 0, 0 }},
  212. [FIERY] = {
  213. { 0, 0, 0, 0 },
  214. { 0.23, 36/256., (116-128)/256., (163-128)/256. },
  215. { 0.45, 52/256., (102-128)/256., (200-128)/256. },
  216. { 0.57, 116/256., (84-128)/256., (196-128)/256. },
  217. { 0.67, 157/256., (67-128)/256., (181-128)/256. },
  218. { 0.77, 193/256., (40-128)/256., (155-128)/256. },
  219. { 0.87, 221/256., (101-128)/256., (134-128)/256. },
  220. { 1, 1, 0, 0 }},
  221. [FRUIT] = {
  222. { 0, 0, 0, 0 },
  223. { 0.20, 29/256., (136-128)/256., (119-128)/256. },
  224. { 0.30, 60/256., (119-128)/256., (90-128)/256. },
  225. { 0.40, 85/256., (91-128)/256., (85-128)/256. },
  226. { 0.50, 116/256., (70-128)/256., (105-128)/256. },
  227. { 0.60, 151/256., (50-128)/256., (146-128)/256. },
  228. { 0.70, 191/256., (63-128)/256., (178-128)/256. },
  229. { 1, 98/256., (80-128)/256., (221-128)/256. }},
  230. [COOL] = {
  231. { 0, 0, 0, 0 },
  232. { .15, 0, .5, -.5 },
  233. { 1, 1, -.5, .5 }},
  234. [MAGMA] = {
  235. { 0, 0, 0, 0 },
  236. { 0.10, 23/256., (175-128)/256., (120-128)/256. },
  237. { 0.23, 43/256., (158-128)/256., (144-128)/256. },
  238. { 0.35, 85/256., (138-128)/256., (179-128)/256. },
  239. { 0.48, 96/256., (128-128)/256., (189-128)/256. },
  240. { 0.64, 128/256., (103-128)/256., (214-128)/256. },
  241. { 0.78, 167/256., (85-128)/256., (174-128)/256. },
  242. { 1, 205/256., (80-128)/256., (152-128)/256. }},
  243. [GREEN] = {
  244. { 0, 0, 0, 0 },
  245. { .75, .5, 0, -.5 },
  246. { 1, 1, 0, 0 }},
  247. };
  248. static av_cold void uninit(AVFilterContext *ctx)
  249. {
  250. ShowSpectrumContext *s = ctx->priv;
  251. int i;
  252. av_freep(&s->combine_buffer);
  253. if (s->fft) {
  254. for (i = 0; i < s->nb_display_channels; i++)
  255. av_fft_end(s->fft[i]);
  256. }
  257. av_freep(&s->fft);
  258. if (s->ifft) {
  259. for (i = 0; i < s->nb_display_channels; i++)
  260. av_fft_end(s->ifft[i]);
  261. }
  262. av_freep(&s->ifft);
  263. if (s->fft_data) {
  264. for (i = 0; i < s->nb_display_channels; i++)
  265. av_freep(&s->fft_data[i]);
  266. }
  267. av_freep(&s->fft_data);
  268. if (s->fft_scratch) {
  269. for (i = 0; i < s->nb_display_channels; i++)
  270. av_freep(&s->fft_scratch[i]);
  271. }
  272. av_freep(&s->fft_scratch);
  273. if (s->color_buffer) {
  274. for (i = 0; i < s->nb_display_channels; i++)
  275. av_freep(&s->color_buffer[i]);
  276. }
  277. av_freep(&s->color_buffer);
  278. av_freep(&s->window_func_lut);
  279. if (s->magnitudes) {
  280. for (i = 0; i < s->nb_display_channels; i++)
  281. av_freep(&s->magnitudes[i]);
  282. }
  283. av_freep(&s->magnitudes);
  284. av_frame_free(&s->outpicref);
  285. av_audio_fifo_free(s->fifo);
  286. if (s->phases) {
  287. for (i = 0; i < s->nb_display_channels; i++)
  288. av_freep(&s->phases[i]);
  289. }
  290. av_freep(&s->phases);
  291. }
  292. static int query_formats(AVFilterContext *ctx)
  293. {
  294. AVFilterFormats *formats = NULL;
  295. AVFilterChannelLayouts *layouts = NULL;
  296. AVFilterLink *inlink = ctx->inputs[0];
  297. AVFilterLink *outlink = ctx->outputs[0];
  298. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
  299. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE };
  300. int ret;
  301. /* set input audio formats */
  302. formats = ff_make_format_list(sample_fmts);
  303. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  304. return ret;
  305. layouts = ff_all_channel_layouts();
  306. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  307. return ret;
  308. formats = ff_all_samplerates();
  309. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  310. return ret;
  311. /* set output video format */
  312. formats = ff_make_format_list(pix_fmts);
  313. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  314. return ret;
  315. return 0;
  316. }
  317. static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  318. {
  319. ShowSpectrumContext *s = ctx->priv;
  320. AVFilterLink *inlink = ctx->inputs[0];
  321. const float *window_func_lut = s->window_func_lut;
  322. AVFrame *fin = arg;
  323. const int ch = jobnr;
  324. int n;
  325. /* fill FFT input with the number of samples available */
  326. const float *p = (float *)fin->extended_data[ch];
  327. for (n = 0; n < s->win_size; n++) {
  328. s->fft_data[ch][n].re = p[n] * window_func_lut[n];
  329. s->fft_data[ch][n].im = 0;
  330. }
  331. if (s->stop) {
  332. double theta, phi, psi, a, b, S, c;
  333. FFTComplex *g = s->fft_data[ch];
  334. FFTComplex *h = s->fft_scratch[ch];
  335. int L = s->buf_size;
  336. int N = s->win_size;
  337. int M = s->win_size / 2;
  338. phi = 2.0 * M_PI * (s->stop - s->start) / (double)inlink->sample_rate / (M - 1);
  339. theta = 2.0 * M_PI * s->start / (double)inlink->sample_rate;
  340. for (int n = 0; n < M; n++) {
  341. h[n].re = cos(n * n / 2.0 * phi);
  342. h[n].im = sin(n * n / 2.0 * phi);
  343. }
  344. for (int n = M; n < L; n++) {
  345. h[n].re = 0.0;
  346. h[n].im = 0.0;
  347. }
  348. for (int n = L - N; n < L; n++) {
  349. h[n].re = cos((L - n) * (L - n) / 2.0 * phi);
  350. h[n].im = sin((L - n) * (L - n) / 2.0 * phi);
  351. }
  352. for (int n = 0; n < N; n++) {
  353. g[n].re = s->fft_data[ch][n].re;
  354. g[n].im = s->fft_data[ch][n].im;
  355. }
  356. for (int n = N; n < L; n++) {
  357. g[n].re = 0.;
  358. g[n].im = 0.;
  359. }
  360. for (int n = 0; n < N; n++) {
  361. psi = n * theta + n * n / 2.0 * phi;
  362. c = cos(psi);
  363. S = -sin(psi);
  364. a = c * g[n].re - S * g[n].im;
  365. b = S * g[n].re + c * g[n].im;
  366. g[n].re = a;
  367. g[n].im = b;
  368. }
  369. av_fft_permute(s->fft[ch], h);
  370. av_fft_calc(s->fft[ch], h);
  371. av_fft_permute(s->fft[ch], g);
  372. av_fft_calc(s->fft[ch], g);
  373. for (int n = 0; n < L; n++) {
  374. c = g[n].re;
  375. S = g[n].im;
  376. a = c * h[n].re - S * h[n].im;
  377. b = S * h[n].re + c * h[n].im;
  378. g[n].re = a / L;
  379. g[n].im = b / L;
  380. }
  381. av_fft_permute(s->ifft[ch], g);
  382. av_fft_calc(s->ifft[ch], g);
  383. for (int k = 0; k < M; k++) {
  384. psi = k * k / 2.0 * phi;
  385. c = cos(psi);
  386. S = -sin(psi);
  387. a = c * g[k].re - S * g[k].im;
  388. b = S * g[k].re + c * g[k].im;
  389. s->fft_data[ch][k].re = a;
  390. s->fft_data[ch][k].im = b;
  391. }
  392. } else {
  393. /* run FFT on each samples set */
  394. av_fft_permute(s->fft[ch], s->fft_data[ch]);
  395. av_fft_calc(s->fft[ch], s->fft_data[ch]);
  396. }
  397. return 0;
  398. }
  399. static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
  400. {
  401. const uint8_t *font;
  402. int font_height;
  403. int i;
  404. font = avpriv_cga_font, font_height = 8;
  405. for (i = 0; txt[i]; i++) {
  406. int char_y, mask;
  407. if (o) {
  408. for (char_y = font_height - 1; char_y >= 0; char_y--) {
  409. uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x;
  410. for (mask = 0x80; mask; mask >>= 1) {
  411. if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
  412. p[char_y] = ~p[char_y];
  413. p += pic->linesize[0];
  414. }
  415. }
  416. } else {
  417. uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8);
  418. for (char_y = 0; char_y < font_height; char_y++) {
  419. for (mask = 0x80; mask; mask >>= 1) {
  420. if (font[txt[i] * font_height + char_y] & mask)
  421. *p = ~(*p);
  422. p++;
  423. }
  424. p += pic->linesize[0] - 8;
  425. }
  426. }
  427. }
  428. }
  429. static void color_range(ShowSpectrumContext *s, int ch,
  430. float *yf, float *uf, float *vf)
  431. {
  432. switch (s->mode) {
  433. case COMBINED:
  434. // reduce range by channel count
  435. *yf = 256.0f / s->nb_display_channels;
  436. switch (s->color_mode) {
  437. case RAINBOW:
  438. case MORELAND:
  439. case NEBULAE:
  440. case FIRE:
  441. case FIERY:
  442. case FRUIT:
  443. case COOL:
  444. case GREEN:
  445. case MAGMA:
  446. case INTENSITY:
  447. *uf = *yf;
  448. *vf = *yf;
  449. break;
  450. case CHANNEL:
  451. /* adjust saturation for mixed UV coloring */
  452. /* this factor is correct for infinite channels, an approximation otherwise */
  453. *uf = *yf * M_PI;
  454. *vf = *yf * M_PI;
  455. break;
  456. default:
  457. av_assert0(0);
  458. }
  459. break;
  460. case SEPARATE:
  461. // full range
  462. *yf = 256.0f;
  463. *uf = 256.0f;
  464. *vf = 256.0f;
  465. break;
  466. default:
  467. av_assert0(0);
  468. }
  469. if (s->color_mode == CHANNEL) {
  470. if (s->nb_display_channels > 1) {
  471. *uf *= 0.5 * sin((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
  472. *vf *= 0.5 * cos((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
  473. } else {
  474. *uf *= 0.5 * sin(M_PI * s->rotation);
  475. *vf *= 0.5 * cos(M_PI * s->rotation + M_PI_2);
  476. }
  477. } else {
  478. *uf += *uf * sin(M_PI * s->rotation);
  479. *vf += *vf * cos(M_PI * s->rotation + M_PI_2);
  480. }
  481. *uf *= s->saturation;
  482. *vf *= s->saturation;
  483. }
  484. static void pick_color(ShowSpectrumContext *s,
  485. float yf, float uf, float vf,
  486. float a, float *out)
  487. {
  488. if (s->color_mode > CHANNEL) {
  489. const int cm = s->color_mode;
  490. float y, u, v;
  491. int i;
  492. for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
  493. if (color_table[cm][i].a >= a)
  494. break;
  495. // i now is the first item >= the color
  496. // now we know to interpolate between item i - 1 and i
  497. if (a <= color_table[cm][i - 1].a) {
  498. y = color_table[cm][i - 1].y;
  499. u = color_table[cm][i - 1].u;
  500. v = color_table[cm][i - 1].v;
  501. } else if (a >= color_table[cm][i].a) {
  502. y = color_table[cm][i].y;
  503. u = color_table[cm][i].u;
  504. v = color_table[cm][i].v;
  505. } else {
  506. float start = color_table[cm][i - 1].a;
  507. float end = color_table[cm][i].a;
  508. float lerpfrac = (a - start) / (end - start);
  509. y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
  510. + color_table[cm][i].y * lerpfrac;
  511. u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
  512. + color_table[cm][i].u * lerpfrac;
  513. v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
  514. + color_table[cm][i].v * lerpfrac;
  515. }
  516. out[0] = y * yf;
  517. out[1] = u * uf;
  518. out[2] = v * vf;
  519. } else {
  520. out[0] = a * yf;
  521. out[1] = a * uf;
  522. out[2] = a * vf;
  523. }
  524. }
  525. static char *get_time(AVFilterContext *ctx, float seconds, int x)
  526. {
  527. char *units;
  528. if (x == 0)
  529. units = av_asprintf("0");
  530. else if (log10(seconds) > 6)
  531. units = av_asprintf("%.2fh", seconds / (60 * 60));
  532. else if (log10(seconds) > 3)
  533. units = av_asprintf("%.2fm", seconds / 60);
  534. else
  535. units = av_asprintf("%.2fs", seconds);
  536. return units;
  537. }
  538. static int draw_legend(AVFilterContext *ctx, int samples)
  539. {
  540. ShowSpectrumContext *s = ctx->priv;
  541. AVFilterLink *inlink = ctx->inputs[0];
  542. AVFilterLink *outlink = ctx->outputs[0];
  543. int ch, y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
  544. int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL);
  545. float spp = samples / (float)sz;
  546. char *text;
  547. uint8_t *dst;
  548. char chlayout_str[128];
  549. av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), inlink->channels,
  550. inlink->channel_layout);
  551. text = av_asprintf("%d Hz | %s", inlink->sample_rate, chlayout_str);
  552. drawtext(s->outpicref, 2, outlink->h - 10, "CREATED BY LIBAVFILTER", 0);
  553. drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, outlink->h - 10, text, 0);
  554. if (s->stop) {
  555. char *text = av_asprintf("Zoom: %d Hz - %d Hz", s->start, s->stop);
  556. drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, 3, text, 0);
  557. av_freep(&text);
  558. }
  559. av_freep(&text);
  560. dst = s->outpicref->data[0] + (s->start_y - 1) * s->outpicref->linesize[0] + s->start_x - 1;
  561. for (x = 0; x < s->w + 1; x++)
  562. dst[x] = 200;
  563. dst = s->outpicref->data[0] + (s->start_y + s->h) * s->outpicref->linesize[0] + s->start_x - 1;
  564. for (x = 0; x < s->w + 1; x++)
  565. dst[x] = 200;
  566. for (y = 0; y < s->h + 2; y++) {
  567. dst = s->outpicref->data[0] + (y + s->start_y - 1) * s->outpicref->linesize[0];
  568. dst[s->start_x - 1] = 200;
  569. dst[s->start_x + s->w] = 200;
  570. }
  571. if (s->orientation == VERTICAL) {
  572. int h = s->mode == SEPARATE ? s->h / s->nb_display_channels : s->h;
  573. int hh = s->mode == SEPARATE ? -(s->h % s->nb_display_channels) + 1 : 1;
  574. for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
  575. for (y = 0; y < h; y += 20) {
  576. dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
  577. dst[s->start_x - 2] = 200;
  578. dst[s->start_x + s->w + 1] = 200;
  579. }
  580. for (y = 0; y < h; y += 40) {
  581. dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
  582. dst[s->start_x - 3] = 200;
  583. dst[s->start_x + s->w + 2] = 200;
  584. }
  585. dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x;
  586. for (x = 0; x < s->w; x+=40)
  587. dst[x] = 200;
  588. dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x;
  589. for (x = 0; x < s->w; x+=80)
  590. dst[x] = 200;
  591. dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x;
  592. for (x = 0; x < s->w; x+=40) {
  593. dst[x] = 200;
  594. }
  595. dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x;
  596. for (x = 0; x < s->w; x+=80) {
  597. dst[x] = 200;
  598. }
  599. for (y = 0; y < h; y += 40) {
  600. float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
  601. float hertz = s->start + y * range / (float)(1 << (int)ceil(log2(h)));
  602. char *units;
  603. if (hertz == 0)
  604. units = av_asprintf("DC");
  605. else
  606. units = av_asprintf("%.2f", hertz);
  607. if (!units)
  608. return AVERROR(ENOMEM);
  609. drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4 - hh, units, 0);
  610. av_free(units);
  611. }
  612. }
  613. for (x = 0; x < s->w && s->single_pic; x+=80) {
  614. float seconds = x * spp / inlink->sample_rate;
  615. char *units = get_time(ctx, seconds, x);
  616. drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->h + s->start_y + 6, units, 0);
  617. drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->start_y - 12, units, 0);
  618. av_free(units);
  619. }
  620. drawtext(s->outpicref, outlink->w / 2 - 4 * 4, outlink->h - s->start_y / 2, "TIME", 0);
  621. drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 14 * 4, "FREQUENCY (Hz)", 1);
  622. } else {
  623. int w = s->mode == SEPARATE ? s->w / s->nb_display_channels : s->w;
  624. for (y = 0; y < s->h; y += 20) {
  625. dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
  626. dst[s->start_x - 2] = 200;
  627. dst[s->start_x + s->w + 1] = 200;
  628. }
  629. for (y = 0; y < s->h; y += 40) {
  630. dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
  631. dst[s->start_x - 3] = 200;
  632. dst[s->start_x + s->w + 2] = 200;
  633. }
  634. for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
  635. dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
  636. for (x = 0; x < w; x+=40)
  637. dst[x] = 200;
  638. dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x + w * ch;
  639. for (x = 0; x < w; x+=80)
  640. dst[x] = 200;
  641. dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x + w * ch;
  642. for (x = 0; x < w; x+=40) {
  643. dst[x] = 200;
  644. }
  645. dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
  646. for (x = 0; x < w; x+=80) {
  647. dst[x] = 200;
  648. }
  649. for (x = 0; x < w - 79; x += 80) {
  650. float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
  651. float hertz = s->start + x * range / (float)(1 << (int)ceil(log2(w)));
  652. char *units;
  653. if (hertz == 0)
  654. units = av_asprintf("DC");
  655. else
  656. units = av_asprintf("%.2f", hertz);
  657. if (!units)
  658. return AVERROR(ENOMEM);
  659. drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->start_y - 12, units, 0);
  660. drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->h + s->start_y + 6, units, 0);
  661. av_free(units);
  662. }
  663. }
  664. for (y = 0; y < s->h && s->single_pic; y+=40) {
  665. float seconds = y * spp / inlink->sample_rate;
  666. char *units = get_time(ctx, seconds, x);
  667. drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0);
  668. av_free(units);
  669. }
  670. drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 4 * 4, "TIME", 1);
  671. drawtext(s->outpicref, outlink->w / 2 - 14 * 4, outlink->h - s->start_y / 2, "FREQUENCY (Hz)", 0);
  672. }
  673. for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) {
  674. int h = multi ? s->h / s->nb_display_channels : s->h;
  675. for (y = 0; y < h; y++) {
  676. float out[3] = { 0., 127.5, 127.5};
  677. int chn;
  678. for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) {
  679. float yf, uf, vf;
  680. int channel = (multi) ? s->nb_display_channels - ch - 1 : chn;
  681. float lout[3];
  682. color_range(s, channel, &yf, &uf, &vf);
  683. pick_color(s, yf, uf, vf, y / (float)h, lout);
  684. out[0] += lout[0];
  685. out[1] += lout[1];
  686. out[2] += lout[2];
  687. }
  688. memset(s->outpicref->data[0]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[0] + s->w + s->start_x + 20, av_clip_uint8(out[0]), 10);
  689. memset(s->outpicref->data[1]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[1] + s->w + s->start_x + 20, av_clip_uint8(out[1]), 10);
  690. memset(s->outpicref->data[2]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[2] + s->w + s->start_x + 20, av_clip_uint8(out[2]), 10);
  691. }
  692. for (y = 0; ch == 0 && y < h; y += h / 10) {
  693. float value = 120.0 * log10(1. - y / (float)h);
  694. char *text;
  695. if (value < -120)
  696. break;
  697. text = av_asprintf("%.0f dB", value);
  698. if (!text)
  699. continue;
  700. drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 5, text, 0);
  701. av_free(text);
  702. }
  703. }
  704. return 0;
  705. }
  706. static int config_output(AVFilterLink *outlink)
  707. {
  708. AVFilterContext *ctx = outlink->src;
  709. AVFilterLink *inlink = ctx->inputs[0];
  710. ShowSpectrumContext *s = ctx->priv;
  711. int i, fft_bits, h, w;
  712. float overlap;
  713. s->stop = FFMIN(s->stop, inlink->sample_rate / 2);
  714. if (s->stop && s->stop <= s->start) {
  715. av_log(ctx, AV_LOG_ERROR, "Stop frequency should be greater than start.\n");
  716. return AVERROR(EINVAL);
  717. }
  718. if (!strcmp(ctx->filter->name, "showspectrumpic"))
  719. s->single_pic = 1;
  720. outlink->w = s->w;
  721. outlink->h = s->h;
  722. outlink->sample_aspect_ratio = (AVRational){1,1};
  723. if (s->legend) {
  724. s->start_x = (log10(inlink->sample_rate) + 1) * 25;
  725. s->start_y = 64;
  726. outlink->w += s->start_x * 2;
  727. outlink->h += s->start_y * 2;
  728. }
  729. h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? s->h : s->h / inlink->channels;
  730. w = (s->mode == COMBINED || s->orientation == VERTICAL) ? s->w : s->w / inlink->channels;
  731. s->channel_height = h;
  732. s->channel_width = w;
  733. if (s->orientation == VERTICAL) {
  734. /* FFT window size (precision) according to the requested output frame height */
  735. for (fft_bits = 1; 1 << fft_bits < 2 * h; fft_bits++);
  736. } else {
  737. /* FFT window size (precision) according to the requested output frame width */
  738. for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++);
  739. }
  740. s->win_size = 1 << fft_bits;
  741. s->buf_size = s->win_size << !!s->stop;
  742. if (!s->fft) {
  743. s->fft = av_calloc(inlink->channels, sizeof(*s->fft));
  744. if (!s->fft)
  745. return AVERROR(ENOMEM);
  746. }
  747. if (s->stop) {
  748. if (!s->ifft) {
  749. s->ifft = av_calloc(inlink->channels, sizeof(*s->ifft));
  750. if (!s->ifft)
  751. return AVERROR(ENOMEM);
  752. }
  753. }
  754. /* (re-)configuration if the video output changed (or first init) */
  755. if (fft_bits != s->fft_bits) {
  756. AVFrame *outpicref;
  757. s->fft_bits = fft_bits;
  758. /* FFT buffers: x2 for each (display) channel buffer.
  759. * Note: we use free and malloc instead of a realloc-like function to
  760. * make sure the buffer is aligned in memory for the FFT functions. */
  761. for (i = 0; i < s->nb_display_channels; i++) {
  762. if (s->stop) {
  763. av_fft_end(s->ifft[i]);
  764. av_freep(&s->fft_scratch[i]);
  765. }
  766. av_fft_end(s->fft[i]);
  767. av_freep(&s->fft_data[i]);
  768. }
  769. av_freep(&s->fft_data);
  770. s->nb_display_channels = inlink->channels;
  771. for (i = 0; i < s->nb_display_channels; i++) {
  772. s->fft[i] = av_fft_init(fft_bits + !!s->stop, 0);
  773. if (s->stop) {
  774. s->ifft[i] = av_fft_init(fft_bits + !!s->stop, 1);
  775. if (!s->ifft[i]) {
  776. av_log(ctx, AV_LOG_ERROR, "Unable to create Inverse FFT context. "
  777. "The window size might be too high.\n");
  778. return AVERROR(EINVAL);
  779. }
  780. }
  781. if (!s->fft[i]) {
  782. av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
  783. "The window size might be too high.\n");
  784. return AVERROR(EINVAL);
  785. }
  786. }
  787. s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
  788. if (!s->magnitudes)
  789. return AVERROR(ENOMEM);
  790. for (i = 0; i < s->nb_display_channels; i++) {
  791. s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
  792. if (!s->magnitudes[i])
  793. return AVERROR(ENOMEM);
  794. }
  795. s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases));
  796. if (!s->phases)
  797. return AVERROR(ENOMEM);
  798. for (i = 0; i < s->nb_display_channels; i++) {
  799. s->phases[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->phases));
  800. if (!s->phases[i])
  801. return AVERROR(ENOMEM);
  802. }
  803. av_freep(&s->color_buffer);
  804. s->color_buffer = av_calloc(s->nb_display_channels, sizeof(*s->color_buffer));
  805. if (!s->color_buffer)
  806. return AVERROR(ENOMEM);
  807. for (i = 0; i < s->nb_display_channels; i++) {
  808. s->color_buffer[i] = av_calloc(s->orientation == VERTICAL ? s->h * 3 : s->w * 3, sizeof(**s->color_buffer));
  809. if (!s->color_buffer[i])
  810. return AVERROR(ENOMEM);
  811. }
  812. s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
  813. if (!s->fft_data)
  814. return AVERROR(ENOMEM);
  815. s->fft_scratch = av_calloc(s->nb_display_channels, sizeof(*s->fft_scratch));
  816. if (!s->fft_scratch)
  817. return AVERROR(ENOMEM);
  818. for (i = 0; i < s->nb_display_channels; i++) {
  819. s->fft_data[i] = av_calloc(s->buf_size, sizeof(**s->fft_data));
  820. if (!s->fft_data[i])
  821. return AVERROR(ENOMEM);
  822. s->fft_scratch[i] = av_calloc(s->buf_size, sizeof(**s->fft_scratch));
  823. if (!s->fft_scratch[i])
  824. return AVERROR(ENOMEM);
  825. }
  826. /* pre-calc windowing function */
  827. s->window_func_lut =
  828. av_realloc_f(s->window_func_lut, s->win_size,
  829. sizeof(*s->window_func_lut));
  830. if (!s->window_func_lut)
  831. return AVERROR(ENOMEM);
  832. generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
  833. if (s->overlap == 1)
  834. s->overlap = overlap;
  835. s->hop_size = (1. - s->overlap) * s->win_size;
  836. if (s->hop_size < 1) {
  837. av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
  838. return AVERROR(EINVAL);
  839. }
  840. for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
  841. s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
  842. }
  843. s->win_scale = 1. / sqrt(s->win_scale);
  844. /* prepare the initial picref buffer (black frame) */
  845. av_frame_free(&s->outpicref);
  846. s->outpicref = outpicref =
  847. ff_get_video_buffer(outlink, outlink->w, outlink->h);
  848. if (!outpicref)
  849. return AVERROR(ENOMEM);
  850. outpicref->sample_aspect_ratio = (AVRational){1,1};
  851. for (i = 0; i < outlink->h; i++) {
  852. memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
  853. memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
  854. memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
  855. }
  856. outpicref->color_range = AVCOL_RANGE_JPEG;
  857. if (!s->single_pic && s->legend)
  858. draw_legend(ctx, 0);
  859. }
  860. if ((s->orientation == VERTICAL && s->xpos >= s->w) ||
  861. (s->orientation == HORIZONTAL && s->xpos >= s->h))
  862. s->xpos = 0;
  863. s->auto_frame_rate = av_make_q(inlink->sample_rate, s->hop_size);
  864. if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
  865. s->auto_frame_rate.den *= s->w;
  866. if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
  867. s->auto_frame_rate.den *= s->h;
  868. if (!s->single_pic && strcmp(s->rate_str, "auto")) {
  869. int ret = av_parse_video_rate(&s->frame_rate, s->rate_str);
  870. if (ret < 0)
  871. return ret;
  872. } else {
  873. s->frame_rate = s->auto_frame_rate;
  874. }
  875. outlink->frame_rate = s->frame_rate;
  876. outlink->time_base = av_inv_q(outlink->frame_rate);
  877. if (s->orientation == VERTICAL) {
  878. s->combine_buffer =
  879. av_realloc_f(s->combine_buffer, s->h * 3,
  880. sizeof(*s->combine_buffer));
  881. } else {
  882. s->combine_buffer =
  883. av_realloc_f(s->combine_buffer, s->w * 3,
  884. sizeof(*s->combine_buffer));
  885. }
  886. av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n",
  887. s->w, s->h, s->win_size);
  888. av_audio_fifo_free(s->fifo);
  889. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
  890. if (!s->fifo)
  891. return AVERROR(ENOMEM);
  892. return 0;
  893. }
  894. #define RE(y, ch) s->fft_data[ch][y].re
  895. #define IM(y, ch) s->fft_data[ch][y].im
  896. #define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch))
  897. #define PHASE(y, ch) atan2(IM(y, ch), RE(y, ch))
  898. static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  899. {
  900. ShowSpectrumContext *s = ctx->priv;
  901. const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
  902. int y, h = s->orientation == VERTICAL ? s->h : s->w;
  903. const float f = s->gain * w;
  904. const int ch = jobnr;
  905. float *magnitudes = s->magnitudes[ch];
  906. for (y = 0; y < h; y++)
  907. magnitudes[y] = MAGNITUDE(y, ch) * f;
  908. return 0;
  909. }
  910. static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  911. {
  912. ShowSpectrumContext *s = ctx->priv;
  913. const int h = s->orientation == VERTICAL ? s->h : s->w;
  914. const int ch = jobnr;
  915. float *phases = s->phases[ch];
  916. int y;
  917. for (y = 0; y < h; y++)
  918. phases[y] = (PHASE(y, ch) / M_PI + 1) / 2;
  919. return 0;
  920. }
  921. static void acalc_magnitudes(ShowSpectrumContext *s)
  922. {
  923. const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
  924. int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
  925. const float f = s->gain * w;
  926. for (ch = 0; ch < s->nb_display_channels; ch++) {
  927. float *magnitudes = s->magnitudes[ch];
  928. for (y = 0; y < h; y++)
  929. magnitudes[y] += MAGNITUDE(y, ch) * f;
  930. }
  931. }
  932. static void scale_magnitudes(ShowSpectrumContext *s, float scale)
  933. {
  934. int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
  935. for (ch = 0; ch < s->nb_display_channels; ch++) {
  936. float *magnitudes = s->magnitudes[ch];
  937. for (y = 0; y < h; y++)
  938. magnitudes[y] *= scale;
  939. }
  940. }
  941. static void clear_combine_buffer(ShowSpectrumContext *s, int size)
  942. {
  943. int y;
  944. for (y = 0; y < size; y++) {
  945. s->combine_buffer[3 * y ] = 0;
  946. s->combine_buffer[3 * y + 1] = 127.5;
  947. s->combine_buffer[3 * y + 2] = 127.5;
  948. }
  949. }
  950. static int plot_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  951. {
  952. ShowSpectrumContext *s = ctx->priv;
  953. const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
  954. const int ch = jobnr;
  955. float *magnitudes = s->magnitudes[ch];
  956. float *phases = s->phases[ch];
  957. float yf, uf, vf;
  958. int y;
  959. /* decide color range */
  960. color_range(s, ch, &yf, &uf, &vf);
  961. /* draw the channel */
  962. for (y = 0; y < h; y++) {
  963. int row = (s->mode == COMBINED) ? y : ch * h + y;
  964. float *out = &s->color_buffer[ch][3 * row];
  965. float a;
  966. switch (s->data) {
  967. case D_MAGNITUDE:
  968. /* get magnitude */
  969. a = magnitudes[y];
  970. break;
  971. case D_PHASE:
  972. /* get phase */
  973. a = phases[y];
  974. break;
  975. default:
  976. av_assert0(0);
  977. }
  978. /* apply scale */
  979. switch (s->scale) {
  980. case LINEAR:
  981. a = av_clipf(a, 0, 1);
  982. break;
  983. case SQRT:
  984. a = av_clipf(sqrt(a), 0, 1);
  985. break;
  986. case CBRT:
  987. a = av_clipf(cbrt(a), 0, 1);
  988. break;
  989. case FOURTHRT:
  990. a = av_clipf(sqrt(sqrt(a)), 0, 1);
  991. break;
  992. case FIFTHRT:
  993. a = av_clipf(pow(a, 0.20), 0, 1);
  994. break;
  995. case LOG:
  996. a = 1 + log10(av_clipd(a, 1e-6, 1)) / 6; // zero = -120dBFS
  997. break;
  998. default:
  999. av_assert0(0);
  1000. }
  1001. pick_color(s, yf, uf, vf, a, out);
  1002. }
  1003. return 0;
  1004. }
  1005. static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
  1006. {
  1007. AVFilterContext *ctx = inlink->dst;
  1008. AVFilterLink *outlink = ctx->outputs[0];
  1009. ShowSpectrumContext *s = ctx->priv;
  1010. AVFrame *outpicref = s->outpicref;
  1011. int ret, plane, x, y, z = s->orientation == VERTICAL ? s->h : s->w;
  1012. /* fill a new spectrum column */
  1013. /* initialize buffer for combining to black */
  1014. clear_combine_buffer(s, z);
  1015. ctx->internal->execute(ctx, plot_channel, NULL, NULL, s->nb_display_channels);
  1016. for (y = 0; y < z * 3; y++) {
  1017. for (x = 0; x < s->nb_display_channels; x++) {
  1018. s->combine_buffer[y] += s->color_buffer[x][y];
  1019. }
  1020. }
  1021. av_frame_make_writable(s->outpicref);
  1022. /* copy to output */
  1023. if (s->orientation == VERTICAL) {
  1024. if (s->sliding == SCROLL) {
  1025. for (plane = 0; plane < 3; plane++) {
  1026. for (y = 0; y < s->h; y++) {
  1027. uint8_t *p = outpicref->data[plane] + s->start_x +
  1028. (y + s->start_y) * outpicref->linesize[plane];
  1029. memmove(p, p + 1, s->w - 1);
  1030. }
  1031. }
  1032. s->xpos = s->w - 1;
  1033. } else if (s->sliding == RSCROLL) {
  1034. for (plane = 0; plane < 3; plane++) {
  1035. for (y = 0; y < s->h; y++) {
  1036. uint8_t *p = outpicref->data[plane] + s->start_x +
  1037. (y + s->start_y) * outpicref->linesize[plane];
  1038. memmove(p + 1, p, s->w - 1);
  1039. }
  1040. }
  1041. s->xpos = 0;
  1042. }
  1043. for (plane = 0; plane < 3; plane++) {
  1044. uint8_t *p = outpicref->data[plane] + s->start_x +
  1045. (outlink->h - 1 - s->start_y) * outpicref->linesize[plane] +
  1046. s->xpos;
  1047. for (y = 0; y < s->h; y++) {
  1048. *p = lrintf(av_clipf(s->combine_buffer[3 * y + plane], 0, 255));
  1049. p -= outpicref->linesize[plane];
  1050. }
  1051. }
  1052. } else {
  1053. if (s->sliding == SCROLL) {
  1054. for (plane = 0; plane < 3; plane++) {
  1055. for (y = 1; y < s->h; y++) {
  1056. memmove(outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
  1057. outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
  1058. s->w);
  1059. }
  1060. }
  1061. s->xpos = s->h - 1;
  1062. } else if (s->sliding == RSCROLL) {
  1063. for (plane = 0; plane < 3; plane++) {
  1064. for (y = s->h - 1; y >= 1; y--) {
  1065. memmove(outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
  1066. outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
  1067. s->w);
  1068. }
  1069. }
  1070. s->xpos = 0;
  1071. }
  1072. for (plane = 0; plane < 3; plane++) {
  1073. uint8_t *p = outpicref->data[plane] + s->start_x +
  1074. (s->xpos + s->start_y) * outpicref->linesize[plane];
  1075. for (x = 0; x < s->w; x++) {
  1076. *p = lrintf(av_clipf(s->combine_buffer[3 * x + plane], 0, 255));
  1077. p++;
  1078. }
  1079. }
  1080. }
  1081. if (s->sliding != FULLFRAME || s->xpos == 0)
  1082. outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
  1083. s->xpos++;
  1084. if (s->orientation == VERTICAL && s->xpos >= s->w)
  1085. s->xpos = 0;
  1086. if (s->orientation == HORIZONTAL && s->xpos >= s->h)
  1087. s->xpos = 0;
  1088. if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
  1089. if (s->old_pts < outpicref->pts) {
  1090. if (s->legend) {
  1091. char *units = get_time(ctx, insamples->pts /(float)inlink->sample_rate, x);
  1092. if (s->orientation == VERTICAL) {
  1093. for (y = 0; y < 10; y++) {
  1094. memset(s->outpicref->data[0] + outlink->w / 2 - 4 * s->old_len +
  1095. (outlink->h - s->start_y / 2 - 20 + y) * s->outpicref->linesize[0], 0, 10 * s->old_len);
  1096. }
  1097. drawtext(s->outpicref,
  1098. outlink->w / 2 - 4 * strlen(units),
  1099. outlink->h - s->start_y / 2 - 20,
  1100. units, 0);
  1101. } else {
  1102. for (y = 0; y < 10 * s->old_len; y++) {
  1103. memset(s->outpicref->data[0] + s->start_x / 7 + 20 +
  1104. (outlink->h / 2 - 4 * s->old_len + y) * s->outpicref->linesize[0], 0, 10);
  1105. }
  1106. drawtext(s->outpicref,
  1107. s->start_x / 7 + 20,
  1108. outlink->h / 2 - 4 * strlen(units),
  1109. units, 1);
  1110. }
  1111. s->old_len = strlen(units);
  1112. av_free(units);
  1113. }
  1114. s->old_pts = outpicref->pts;
  1115. ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
  1116. if (ret < 0)
  1117. return ret;
  1118. return 0;
  1119. }
  1120. }
  1121. return 1;
  1122. }
  1123. #if CONFIG_SHOWSPECTRUM_FILTER
  1124. static int activate(AVFilterContext *ctx)
  1125. {
  1126. AVFilterLink *inlink = ctx->inputs[0];
  1127. AVFilterLink *outlink = ctx->outputs[0];
  1128. ShowSpectrumContext *s = ctx->priv;
  1129. int ret;
  1130. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  1131. if (av_audio_fifo_size(s->fifo) < s->win_size) {
  1132. AVFrame *frame = NULL;
  1133. ret = ff_inlink_consume_frame(inlink, &frame);
  1134. if (ret < 0)
  1135. return ret;
  1136. if (ret > 0) {
  1137. s->pts = frame->pts;
  1138. s->consumed = 0;
  1139. av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
  1140. av_frame_free(&frame);
  1141. }
  1142. }
  1143. if (s->outpicref && av_audio_fifo_size(s->fifo) >= s->win_size) {
  1144. AVFrame *fin = ff_get_audio_buffer(inlink, s->win_size);
  1145. if (!fin)
  1146. return AVERROR(ENOMEM);
  1147. fin->pts = s->pts + s->consumed;
  1148. s->consumed += s->hop_size;
  1149. ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data,
  1150. FFMIN(s->win_size, av_audio_fifo_size(s->fifo)));
  1151. if (ret < 0) {
  1152. av_frame_free(&fin);
  1153. return ret;
  1154. }
  1155. av_assert0(fin->nb_samples == s->win_size);
  1156. ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
  1157. if (s->data == D_MAGNITUDE)
  1158. ctx->internal->execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels);
  1159. if (s->data == D_PHASE)
  1160. ctx->internal->execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels);
  1161. ret = plot_spectrum_column(inlink, fin);
  1162. av_frame_free(&fin);
  1163. av_audio_fifo_drain(s->fifo, s->hop_size);
  1164. if (ret <= 0)
  1165. return ret;
  1166. }
  1167. if (ff_outlink_get_status(inlink) == AVERROR_EOF &&
  1168. s->sliding == FULLFRAME &&
  1169. s->xpos > 0 && s->outpicref) {
  1170. int64_t pts;
  1171. if (s->orientation == VERTICAL) {
  1172. for (int i = 0; i < outlink->h; i++) {
  1173. memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos);
  1174. memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
  1175. memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
  1176. }
  1177. } else {
  1178. for (int i = s->xpos; i < outlink->h; i++) {
  1179. memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w);
  1180. memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
  1181. memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
  1182. }
  1183. }
  1184. s->outpicref->pts += s->consumed;
  1185. pts = s->outpicref->pts;
  1186. ret = ff_filter_frame(outlink, s->outpicref);
  1187. s->outpicref = NULL;
  1188. ff_outlink_set_status(outlink, AVERROR_EOF, pts);
  1189. return 0;
  1190. }
  1191. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  1192. if (ff_outlink_frame_wanted(outlink) && av_audio_fifo_size(s->fifo) < s->win_size) {
  1193. ff_inlink_request_frame(inlink);
  1194. return 0;
  1195. }
  1196. if (av_audio_fifo_size(s->fifo) >= s->win_size) {
  1197. ff_filter_set_ready(ctx, 10);
  1198. return 0;
  1199. }
  1200. return FFERROR_NOT_READY;
  1201. }
  1202. static const AVFilterPad showspectrum_inputs[] = {
  1203. {
  1204. .name = "default",
  1205. .type = AVMEDIA_TYPE_AUDIO,
  1206. },
  1207. { NULL }
  1208. };
  1209. static const AVFilterPad showspectrum_outputs[] = {
  1210. {
  1211. .name = "default",
  1212. .type = AVMEDIA_TYPE_VIDEO,
  1213. .config_props = config_output,
  1214. },
  1215. { NULL }
  1216. };
  1217. AVFilter ff_avf_showspectrum = {
  1218. .name = "showspectrum",
  1219. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
  1220. .uninit = uninit,
  1221. .query_formats = query_formats,
  1222. .priv_size = sizeof(ShowSpectrumContext),
  1223. .inputs = showspectrum_inputs,
  1224. .outputs = showspectrum_outputs,
  1225. .activate = activate,
  1226. .priv_class = &showspectrum_class,
  1227. .flags = AVFILTER_FLAG_SLICE_THREADS,
  1228. };
  1229. #endif // CONFIG_SHOWSPECTRUM_FILTER
  1230. #if CONFIG_SHOWSPECTRUMPIC_FILTER
  1231. static const AVOption showspectrumpic_options[] = {
  1232. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
  1233. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
  1234. { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" },
  1235. { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
  1236. { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
  1237. { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" },
  1238. { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
  1239. { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
  1240. { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
  1241. { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
  1242. { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
  1243. { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
  1244. { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
  1245. { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
  1246. { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
  1247. { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
  1248. { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
  1249. { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" },
  1250. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
  1251. { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
  1252. { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
  1253. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
  1254. { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
  1255. { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
  1256. { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
  1257. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
  1258. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
  1259. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
  1260. { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  1261. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  1262. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
  1263. { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
  1264. { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
  1265. { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
  1266. { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
  1267. { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
  1268. { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
  1269. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
  1270. { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
  1271. { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
  1272. { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
  1273. { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
  1274. { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
  1275. { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
  1276. { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
  1277. { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
  1278. { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
  1279. { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
  1280. { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
  1281. { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
  1282. { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
  1283. { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
  1284. { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
  1285. { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
  1286. { NULL }
  1287. };
  1288. AVFILTER_DEFINE_CLASS(showspectrumpic);
  1289. static int showspectrumpic_request_frame(AVFilterLink *outlink)
  1290. {
  1291. AVFilterContext *ctx = outlink->src;
  1292. ShowSpectrumContext *s = ctx->priv;
  1293. AVFilterLink *inlink = ctx->inputs[0];
  1294. int ret, samples;
  1295. ret = ff_request_frame(inlink);
  1296. samples = av_audio_fifo_size(s->fifo);
  1297. if (ret == AVERROR_EOF && s->outpicref && samples > 0) {
  1298. int consumed = 0;
  1299. int x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
  1300. int ch, spf, spb;
  1301. AVFrame *fin;
  1302. spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz))));
  1303. spf = FFMAX(1, spf);
  1304. spb = (samples / (spf * sz)) * spf;
  1305. fin = ff_get_audio_buffer(inlink, s->win_size);
  1306. if (!fin)
  1307. return AVERROR(ENOMEM);
  1308. while (x < sz) {
  1309. ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
  1310. if (ret < 0) {
  1311. av_frame_free(&fin);
  1312. return ret;
  1313. }
  1314. av_audio_fifo_drain(s->fifo, spf);
  1315. if (ret < s->win_size) {
  1316. for (ch = 0; ch < s->nb_display_channels; ch++) {
  1317. memset(fin->extended_data[ch] + ret * sizeof(float), 0,
  1318. (s->win_size - ret) * sizeof(float));
  1319. }
  1320. }
  1321. ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
  1322. acalc_magnitudes(s);
  1323. consumed += spf;
  1324. if (consumed >= spb) {
  1325. int h = s->orientation == VERTICAL ? s->h : s->w;
  1326. scale_magnitudes(s, 1. / (consumed / spf));
  1327. plot_spectrum_column(inlink, fin);
  1328. consumed = 0;
  1329. x++;
  1330. for (ch = 0; ch < s->nb_display_channels; ch++)
  1331. memset(s->magnitudes[ch], 0, h * sizeof(float));
  1332. }
  1333. }
  1334. av_frame_free(&fin);
  1335. s->outpicref->pts = 0;
  1336. if (s->legend)
  1337. draw_legend(ctx, samples);
  1338. ret = ff_filter_frame(outlink, s->outpicref);
  1339. s->outpicref = NULL;
  1340. }
  1341. return ret;
  1342. }
  1343. static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  1344. {
  1345. AVFilterContext *ctx = inlink->dst;
  1346. ShowSpectrumContext *s = ctx->priv;
  1347. int ret;
  1348. ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
  1349. av_frame_free(&insamples);
  1350. return ret;
  1351. }
  1352. static const AVFilterPad showspectrumpic_inputs[] = {
  1353. {
  1354. .name = "default",
  1355. .type = AVMEDIA_TYPE_AUDIO,
  1356. .filter_frame = showspectrumpic_filter_frame,
  1357. },
  1358. { NULL }
  1359. };
  1360. static const AVFilterPad showspectrumpic_outputs[] = {
  1361. {
  1362. .name = "default",
  1363. .type = AVMEDIA_TYPE_VIDEO,
  1364. .config_props = config_output,
  1365. .request_frame = showspectrumpic_request_frame,
  1366. },
  1367. { NULL }
  1368. };
  1369. AVFilter ff_avf_showspectrumpic = {
  1370. .name = "showspectrumpic",
  1371. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
  1372. .uninit = uninit,
  1373. .query_formats = query_formats,
  1374. .priv_size = sizeof(ShowSpectrumContext),
  1375. .inputs = showspectrumpic_inputs,
  1376. .outputs = showspectrumpic_outputs,
  1377. .priv_class = &showspectrumpic_class,
  1378. .flags = AVFILTER_FLAG_SLICE_THREADS,
  1379. };
  1380. #endif // CONFIG_SHOWSPECTRUMPIC_FILTER