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.

1598 lines
68KB

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