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.

1278 lines
54KB

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