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.

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