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.

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