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.

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