avf_showspectrum.c 52 KB

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