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.

1317 lines
56KB

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