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.

1578 lines
66KB

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