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.

1476 lines
62KB

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