avf_showspectrum.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  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/parseutils.h"
  35. #include "libavutil/xga_font_data.h"
  36. #include "audio.h"
  37. #include "video.h"
  38. #include "avfilter.h"
  39. #include "filters.h"
  40. #include "internal.h"
  41. #include "window_func.h"
  42. enum DisplayMode { COMBINED, SEPARATE, NB_MODES };
  43. enum DataMode { D_MAGNITUDE, D_PHASE, NB_DMODES };
  44. enum FrequencyScale { F_LINEAR, F_LOG, NB_FSCALES };
  45. enum DisplayScale { LINEAR, SQRT, CBRT, LOG, FOURTHRT, FIFTHRT, NB_SCALES };
  46. enum ColorMode { CHANNEL, INTENSITY, RAINBOW, MORELAND, NEBULAE, FIRE, FIERY, FRUIT, COOL, MAGMA, GREEN, VIRIDIS, PLASMA, CIVIDIS, TERRAIN, NB_CLMODES };
  47. enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, NB_SLIDES };
  48. enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS };
  49. typedef struct ShowSpectrumContext {
  50. const AVClass *class;
  51. int w, h;
  52. char *rate_str;
  53. AVRational auto_frame_rate;
  54. AVRational frame_rate;
  55. AVFrame *outpicref;
  56. int nb_display_channels;
  57. int orientation;
  58. int channel_width;
  59. int channel_height;
  60. int sliding; ///< 1 if sliding mode, 0 otherwise
  61. int mode; ///< channel display mode
  62. int color_mode; ///< display color scheme
  63. int scale;
  64. int fscale;
  65. float saturation; ///< color saturation multiplier
  66. float rotation; ///< color rotation
  67. int start, stop; ///< zoom mode
  68. int data;
  69. int xpos; ///< x position (current column)
  70. FFTContext **fft; ///< Fast Fourier Transform context
  71. FFTContext **ifft; ///< Inverse Fast Fourier Transform context
  72. int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits)
  73. FFTComplex **fft_data; ///< bins holder for each (displayed) channels
  74. FFTComplex **fft_scratch; ///< scratch buffers
  75. float *window_func_lut; ///< Window function LUT
  76. float **magnitudes;
  77. float **phases;
  78. int win_func;
  79. int win_size;
  80. int buf_size;
  81. double win_scale;
  82. float overlap;
  83. float gain;
  84. int consumed;
  85. int hop_size;
  86. float *combine_buffer; ///< color combining buffer (3 * h items)
  87. float **color_buffer; ///< color buffer (3 * h * ch items)
  88. AVAudioFifo *fifo;
  89. int64_t pts;
  90. int64_t old_pts;
  91. int old_len;
  92. int single_pic;
  93. int legend;
  94. int start_x, start_y;
  95. int (*plot_channel)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  96. } ShowSpectrumContext;
  97. #define OFFSET(x) offsetof(ShowSpectrumContext, x)
  98. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  99. static const AVOption showspectrum_options[] = {
  100. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
  101. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
  102. { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
  103. { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
  104. { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
  105. { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
  106. { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
  107. { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
  108. { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
  109. { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
  110. { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
  111. { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
  112. { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
  113. { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
  114. { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
  115. { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
  116. { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
  117. { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
  118. { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
  119. { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
  120. { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
  121. { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
  122. { "viridis", "viridis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=VIRIDIS}, 0, 0, FLAGS, "color" },
  123. { "plasma", "plasma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=PLASMA}, 0, 0, FLAGS, "color" },
  124. { "cividis", "cividis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=CIVIDIS}, 0, 0, FLAGS, "color" },
  125. { "terrain", "terrain based coloring", 0, AV_OPT_TYPE_CONST, {.i64=TERRAIN}, 0, 0, FLAGS, "color" },
  126. { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
  127. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
  128. { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
  129. { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
  130. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
  131. { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
  132. { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
  133. { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=F_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
  134. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=F_LINEAR}, 0, 0, FLAGS, "fscale" },
  135. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=F_LOG}, 0, 0, FLAGS, "fscale" },
  136. { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
  137. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
  138. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
  139. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
  140. { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  141. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  142. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
  143. { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
  144. { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
  145. { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
  146. { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
  147. { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
  148. { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
  149. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
  150. { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
  151. { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
  152. { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
  153. { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
  154. { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
  155. { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
  156. { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
  157. { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
  158. { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
  159. { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
  160. { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
  161. { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
  162. { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS },
  163. { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
  164. { "data", "set data mode", OFFSET(data), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_DMODES-1, FLAGS, "data" },
  165. { "magnitude", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_MAGNITUDE}, 0, 0, FLAGS, "data" },
  166. { "phase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_PHASE}, 0, 0, FLAGS, "data" },
  167. { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
  168. { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
  169. { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
  170. { "fps", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = "auto"}, 0, 0, FLAGS },
  171. { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
  172. { NULL }
  173. };
  174. AVFILTER_DEFINE_CLASS(showspectrum);
  175. static const struct ColorTable {
  176. float a, y, u, v;
  177. } color_table[][8] = {
  178. [INTENSITY] = {
  179. { 0, 0, 0, 0 },
  180. { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
  181. { 0.30, .18572281794568020, .1772436246393981, .17475554840414750 },
  182. { 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 },
  183. { 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 },
  184. { 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 },
  185. { 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 },
  186. { 1, 1, 0, 0 }},
  187. [RAINBOW] = {
  188. { 0, 0, 0, 0 },
  189. { 0.13, 44/256., (189-128)/256., (138-128)/256. },
  190. { 0.25, 29/256., (186-128)/256., (119-128)/256. },
  191. { 0.38, 119/256., (194-128)/256., (53-128)/256. },
  192. { 0.60, 111/256., (73-128)/256., (59-128)/256. },
  193. { 0.73, 205/256., (19-128)/256., (149-128)/256. },
  194. { 0.86, 135/256., (83-128)/256., (200-128)/256. },
  195. { 1, 73/256., (95-128)/256., (225-128)/256. }},
  196. [MORELAND] = {
  197. { 0, 44/256., (181-128)/256., (112-128)/256. },
  198. { 0.13, 126/256., (177-128)/256., (106-128)/256. },
  199. { 0.25, 164/256., (163-128)/256., (109-128)/256. },
  200. { 0.38, 200/256., (140-128)/256., (120-128)/256. },
  201. { 0.60, 201/256., (117-128)/256., (141-128)/256. },
  202. { 0.73, 177/256., (103-128)/256., (165-128)/256. },
  203. { 0.86, 136/256., (100-128)/256., (183-128)/256. },
  204. { 1, 68/256., (117-128)/256., (203-128)/256. }},
  205. [NEBULAE] = {
  206. { 0, 10/256., (134-128)/256., (132-128)/256. },
  207. { 0.23, 21/256., (137-128)/256., (130-128)/256. },
  208. { 0.45, 35/256., (134-128)/256., (134-128)/256. },
  209. { 0.57, 51/256., (130-128)/256., (139-128)/256. },
  210. { 0.67, 104/256., (116-128)/256., (162-128)/256. },
  211. { 0.77, 120/256., (105-128)/256., (188-128)/256. },
  212. { 0.87, 140/256., (105-128)/256., (188-128)/256. },
  213. { 1, 1, 0, 0 }},
  214. [FIRE] = {
  215. { 0, 0, 0, 0 },
  216. { 0.23, 44/256., (132-128)/256., (127-128)/256. },
  217. { 0.45, 62/256., (116-128)/256., (140-128)/256. },
  218. { 0.57, 75/256., (105-128)/256., (152-128)/256. },
  219. { 0.67, 95/256., (91-128)/256., (166-128)/256. },
  220. { 0.77, 126/256., (74-128)/256., (172-128)/256. },
  221. { 0.87, 164/256., (73-128)/256., (162-128)/256. },
  222. { 1, 1, 0, 0 }},
  223. [FIERY] = {
  224. { 0, 0, 0, 0 },
  225. { 0.23, 36/256., (116-128)/256., (163-128)/256. },
  226. { 0.45, 52/256., (102-128)/256., (200-128)/256. },
  227. { 0.57, 116/256., (84-128)/256., (196-128)/256. },
  228. { 0.67, 157/256., (67-128)/256., (181-128)/256. },
  229. { 0.77, 193/256., (40-128)/256., (155-128)/256. },
  230. { 0.87, 221/256., (101-128)/256., (134-128)/256. },
  231. { 1, 1, 0, 0 }},
  232. [FRUIT] = {
  233. { 0, 0, 0, 0 },
  234. { 0.20, 29/256., (136-128)/256., (119-128)/256. },
  235. { 0.30, 60/256., (119-128)/256., (90-128)/256. },
  236. { 0.40, 85/256., (91-128)/256., (85-128)/256. },
  237. { 0.50, 116/256., (70-128)/256., (105-128)/256. },
  238. { 0.60, 151/256., (50-128)/256., (146-128)/256. },
  239. { 0.70, 191/256., (63-128)/256., (178-128)/256. },
  240. { 1, 98/256., (80-128)/256., (221-128)/256. }},
  241. [COOL] = {
  242. { 0, 0, 0, 0 },
  243. { .15, 0, .5, -.5 },
  244. { 1, 1, -.5, .5 }},
  245. [MAGMA] = {
  246. { 0, 0, 0, 0 },
  247. { 0.10, 23/256., (175-128)/256., (120-128)/256. },
  248. { 0.23, 43/256., (158-128)/256., (144-128)/256. },
  249. { 0.35, 85/256., (138-128)/256., (179-128)/256. },
  250. { 0.48, 96/256., (128-128)/256., (189-128)/256. },
  251. { 0.64, 128/256., (103-128)/256., (214-128)/256. },
  252. { 0.92, 205/256., (80-128)/256., (152-128)/256. },
  253. { 1, 1, 0, 0 }},
  254. [GREEN] = {
  255. { 0, 0, 0, 0 },
  256. { .75, .5, 0, -.5 },
  257. { 1, 1, 0, 0 }},
  258. [VIRIDIS] = {
  259. { 0, 0, 0, 0 },
  260. { 0.10, 0x39/255., (0x9D -128)/255., (0x8F -128)/255. },
  261. { 0.23, 0x5C/255., (0x9A -128)/255., (0x68 -128)/255. },
  262. { 0.35, 0x69/255., (0x93 -128)/255., (0x57 -128)/255. },
  263. { 0.48, 0x76/255., (0x88 -128)/255., (0x4B -128)/255. },
  264. { 0.64, 0x8A/255., (0x72 -128)/255., (0x4F -128)/255. },
  265. { 0.80, 0xA3/255., (0x50 -128)/255., (0x66 -128)/255. },
  266. { 1, 0xCC/255., (0x2F -128)/255., (0x87 -128)/255. }},
  267. [PLASMA] = {
  268. { 0, 0, 0, 0 },
  269. { 0.10, 0x27/255., (0xC2 -128)/255., (0x82 -128)/255. },
  270. { 0.58, 0x5B/255., (0x9A -128)/255., (0xAE -128)/255. },
  271. { 0.70, 0x89/255., (0x44 -128)/255., (0xAB -128)/255. },
  272. { 0.80, 0xB4/255., (0x2B -128)/255., (0x9E -128)/255. },
  273. { 0.91, 0xD2/255., (0x38 -128)/255., (0x92 -128)/255. },
  274. { 1, 1, 0, 0. }},
  275. [CIVIDIS] = {
  276. { 0, 0, 0, 0 },
  277. { 0.20, 0x28/255., (0x98 -128)/255., (0x6F -128)/255. },
  278. { 0.50, 0x48/255., (0x95 -128)/255., (0x74 -128)/255. },
  279. { 0.63, 0x69/255., (0x84 -128)/255., (0x7F -128)/255. },
  280. { 0.76, 0x89/255., (0x75 -128)/255., (0x84 -128)/255. },
  281. { 0.90, 0xCE/255., (0x35 -128)/255., (0x95 -128)/255. },
  282. { 1, 1, 0, 0. }},
  283. [TERRAIN] = {
  284. { 0, 0, 0, 0 },
  285. { 0.15, 0, .5, 0 },
  286. { 0.60, 1, -.5, -.5 },
  287. { 0.85, 1, -.5, .5 },
  288. { 1, 1, 0, 0 }},
  289. };
  290. static av_cold void uninit(AVFilterContext *ctx)
  291. {
  292. ShowSpectrumContext *s = ctx->priv;
  293. int i;
  294. av_freep(&s->combine_buffer);
  295. if (s->fft) {
  296. for (i = 0; i < s->nb_display_channels; i++)
  297. av_fft_end(s->fft[i]);
  298. }
  299. av_freep(&s->fft);
  300. if (s->ifft) {
  301. for (i = 0; i < s->nb_display_channels; i++)
  302. av_fft_end(s->ifft[i]);
  303. }
  304. av_freep(&s->ifft);
  305. if (s->fft_data) {
  306. for (i = 0; i < s->nb_display_channels; i++)
  307. av_freep(&s->fft_data[i]);
  308. }
  309. av_freep(&s->fft_data);
  310. if (s->fft_scratch) {
  311. for (i = 0; i < s->nb_display_channels; i++)
  312. av_freep(&s->fft_scratch[i]);
  313. }
  314. av_freep(&s->fft_scratch);
  315. if (s->color_buffer) {
  316. for (i = 0; i < s->nb_display_channels; i++)
  317. av_freep(&s->color_buffer[i]);
  318. }
  319. av_freep(&s->color_buffer);
  320. av_freep(&s->window_func_lut);
  321. if (s->magnitudes) {
  322. for (i = 0; i < s->nb_display_channels; i++)
  323. av_freep(&s->magnitudes[i]);
  324. }
  325. av_freep(&s->magnitudes);
  326. av_frame_free(&s->outpicref);
  327. av_audio_fifo_free(s->fifo);
  328. if (s->phases) {
  329. for (i = 0; i < s->nb_display_channels; i++)
  330. av_freep(&s->phases[i]);
  331. }
  332. av_freep(&s->phases);
  333. }
  334. static int query_formats(AVFilterContext *ctx)
  335. {
  336. AVFilterFormats *formats = NULL;
  337. AVFilterChannelLayouts *layouts = NULL;
  338. AVFilterLink *inlink = ctx->inputs[0];
  339. AVFilterLink *outlink = ctx->outputs[0];
  340. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
  341. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE };
  342. int ret;
  343. /* set input audio formats */
  344. formats = ff_make_format_list(sample_fmts);
  345. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  346. return ret;
  347. layouts = ff_all_channel_layouts();
  348. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  349. return ret;
  350. formats = ff_all_samplerates();
  351. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  352. return ret;
  353. /* set output video format */
  354. formats = ff_make_format_list(pix_fmts);
  355. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  356. return ret;
  357. return 0;
  358. }
  359. static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  360. {
  361. ShowSpectrumContext *s = ctx->priv;
  362. AVFilterLink *inlink = ctx->inputs[0];
  363. const float *window_func_lut = s->window_func_lut;
  364. AVFrame *fin = arg;
  365. const int ch = jobnr;
  366. int n;
  367. /* fill FFT input with the number of samples available */
  368. const float *p = (float *)fin->extended_data[ch];
  369. for (n = 0; n < s->win_size; n++) {
  370. s->fft_data[ch][n].re = p[n] * window_func_lut[n];
  371. s->fft_data[ch][n].im = 0;
  372. }
  373. if (s->stop) {
  374. float theta, phi, psi, a, b, S, c;
  375. FFTComplex *g = s->fft_data[ch];
  376. FFTComplex *h = s->fft_scratch[ch];
  377. int L = s->buf_size;
  378. int N = s->win_size;
  379. int M = s->win_size / 2;
  380. phi = 2.f * M_PI * (s->stop - s->start) / (float)inlink->sample_rate / (M - 1);
  381. theta = 2.f * M_PI * s->start / (float)inlink->sample_rate;
  382. for (int n = 0; n < M; n++) {
  383. h[n].re = cosf(n * n / 2.f * phi);
  384. h[n].im = sinf(n * n / 2.f * phi);
  385. }
  386. for (int n = M; n < L; n++) {
  387. h[n].re = 0.f;
  388. h[n].im = 0.f;
  389. }
  390. for (int n = L - N; n < L; n++) {
  391. h[n].re = cosf((L - n) * (L - n) / 2.f * phi);
  392. h[n].im = sinf((L - n) * (L - n) / 2.f * phi);
  393. }
  394. for (int n = 0; n < N; n++) {
  395. g[n].re = s->fft_data[ch][n].re;
  396. g[n].im = s->fft_data[ch][n].im;
  397. }
  398. for (int n = N; n < L; n++) {
  399. g[n].re = 0.f;
  400. g[n].im = 0.f;
  401. }
  402. for (int n = 0; n < N; n++) {
  403. psi = n * theta + n * n / 2.f * phi;
  404. c = cosf(psi);
  405. S = -sinf(psi);
  406. a = c * g[n].re - S * g[n].im;
  407. b = S * g[n].re + c * g[n].im;
  408. g[n].re = a;
  409. g[n].im = b;
  410. }
  411. av_fft_permute(s->fft[ch], h);
  412. av_fft_calc(s->fft[ch], h);
  413. av_fft_permute(s->fft[ch], g);
  414. av_fft_calc(s->fft[ch], g);
  415. for (int n = 0; n < L; n++) {
  416. c = g[n].re;
  417. S = g[n].im;
  418. a = c * h[n].re - S * h[n].im;
  419. b = S * h[n].re + c * h[n].im;
  420. g[n].re = a / L;
  421. g[n].im = b / L;
  422. }
  423. av_fft_permute(s->ifft[ch], g);
  424. av_fft_calc(s->ifft[ch], g);
  425. for (int k = 0; k < M; k++) {
  426. psi = k * k / 2.f * phi;
  427. c = cosf(psi);
  428. S = -sinf(psi);
  429. a = c * g[k].re - S * g[k].im;
  430. b = S * g[k].re + c * g[k].im;
  431. s->fft_data[ch][k].re = a;
  432. s->fft_data[ch][k].im = b;
  433. }
  434. } else {
  435. /* run FFT on each samples set */
  436. av_fft_permute(s->fft[ch], s->fft_data[ch]);
  437. av_fft_calc(s->fft[ch], s->fft_data[ch]);
  438. }
  439. return 0;
  440. }
  441. static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
  442. {
  443. const uint8_t *font;
  444. int font_height;
  445. int i;
  446. font = avpriv_cga_font, font_height = 8;
  447. for (i = 0; txt[i]; i++) {
  448. int char_y, mask;
  449. if (o) {
  450. for (char_y = font_height - 1; char_y >= 0; char_y--) {
  451. uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x;
  452. for (mask = 0x80; mask; mask >>= 1) {
  453. if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
  454. p[char_y] = ~p[char_y];
  455. p += pic->linesize[0];
  456. }
  457. }
  458. } else {
  459. uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8);
  460. for (char_y = 0; char_y < font_height; char_y++) {
  461. for (mask = 0x80; mask; mask >>= 1) {
  462. if (font[txt[i] * font_height + char_y] & mask)
  463. *p = ~(*p);
  464. p++;
  465. }
  466. p += pic->linesize[0] - 8;
  467. }
  468. }
  469. }
  470. }
  471. static void color_range(ShowSpectrumContext *s, int ch,
  472. float *yf, float *uf, float *vf)
  473. {
  474. switch (s->mode) {
  475. case COMBINED:
  476. // reduce range by channel count
  477. *yf = 256.0f / s->nb_display_channels;
  478. switch (s->color_mode) {
  479. case RAINBOW:
  480. case MORELAND:
  481. case NEBULAE:
  482. case FIRE:
  483. case FIERY:
  484. case FRUIT:
  485. case COOL:
  486. case GREEN:
  487. case VIRIDIS:
  488. case PLASMA:
  489. case CIVIDIS:
  490. case TERRAIN:
  491. case MAGMA:
  492. case INTENSITY:
  493. *uf = *yf;
  494. *vf = *yf;
  495. break;
  496. case CHANNEL:
  497. /* adjust saturation for mixed UV coloring */
  498. /* this factor is correct for infinite channels, an approximation otherwise */
  499. *uf = *yf * M_PI;
  500. *vf = *yf * M_PI;
  501. break;
  502. default:
  503. av_assert0(0);
  504. }
  505. break;
  506. case SEPARATE:
  507. // full range
  508. *yf = 256.0f;
  509. *uf = 256.0f;
  510. *vf = 256.0f;
  511. break;
  512. default:
  513. av_assert0(0);
  514. }
  515. if (s->color_mode == CHANNEL) {
  516. if (s->nb_display_channels > 1) {
  517. *uf *= 0.5f * sinf((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
  518. *vf *= 0.5f * cosf((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation);
  519. } else {
  520. *uf *= 0.5f * sinf(M_PI * s->rotation);
  521. *vf *= 0.5f * cosf(M_PI * s->rotation + M_PI_2);
  522. }
  523. } else {
  524. *uf += *uf * sinf(M_PI * s->rotation);
  525. *vf += *vf * cosf(M_PI * s->rotation + M_PI_2);
  526. }
  527. *uf *= s->saturation;
  528. *vf *= s->saturation;
  529. }
  530. static void pick_color(ShowSpectrumContext *s,
  531. float yf, float uf, float vf,
  532. float a, float *out)
  533. {
  534. if (s->color_mode > CHANNEL) {
  535. const int cm = s->color_mode;
  536. float y, u, v;
  537. int i;
  538. for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
  539. if (color_table[cm][i].a >= a)
  540. break;
  541. // i now is the first item >= the color
  542. // now we know to interpolate between item i - 1 and i
  543. if (a <= color_table[cm][i - 1].a) {
  544. y = color_table[cm][i - 1].y;
  545. u = color_table[cm][i - 1].u;
  546. v = color_table[cm][i - 1].v;
  547. } else if (a >= color_table[cm][i].a) {
  548. y = color_table[cm][i].y;
  549. u = color_table[cm][i].u;
  550. v = color_table[cm][i].v;
  551. } else {
  552. float start = color_table[cm][i - 1].a;
  553. float end = color_table[cm][i].a;
  554. float lerpfrac = (a - start) / (end - start);
  555. y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
  556. + color_table[cm][i].y * lerpfrac;
  557. u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
  558. + color_table[cm][i].u * lerpfrac;
  559. v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
  560. + color_table[cm][i].v * lerpfrac;
  561. }
  562. out[0] = y * yf;
  563. out[1] = u * uf;
  564. out[2] = v * vf;
  565. } else {
  566. out[0] = a * yf;
  567. out[1] = a * uf;
  568. out[2] = a * vf;
  569. }
  570. }
  571. static char *get_time(AVFilterContext *ctx, float seconds, int x)
  572. {
  573. char *units;
  574. if (x == 0)
  575. units = av_asprintf("0");
  576. else if (log10(seconds) > 6)
  577. units = av_asprintf("%.2fh", seconds / (60 * 60));
  578. else if (log10(seconds) > 3)
  579. units = av_asprintf("%.2fm", seconds / 60);
  580. else
  581. units = av_asprintf("%.2fs", seconds);
  582. return units;
  583. }
  584. static float log_scale(const float value, const float min, const float max)
  585. {
  586. if (value < min)
  587. return min;
  588. if (value > max)
  589. return max;
  590. {
  591. const float b = logf(max / min) / (max - min);
  592. const float a = max / expf(max * b);
  593. return expf(value * b) * a;
  594. }
  595. }
  596. static float get_log_hz(const int bin, const int num_bins, const float sample_rate)
  597. {
  598. const float max_freq = sample_rate / 2;
  599. const float hz_per_bin = max_freq / num_bins;
  600. const float freq = hz_per_bin * bin;
  601. const float scaled_freq = log_scale(freq + 1, 21, max_freq) - 1;
  602. return num_bins * scaled_freq / max_freq;
  603. }
  604. static float inv_log_scale(const float value, const float min, const float max)
  605. {
  606. if (value < min)
  607. return min;
  608. if (value > max)
  609. return max;
  610. {
  611. const float b = logf(max / min) / (max - min);
  612. const float a = max / expf(max * b);
  613. return logf(value / a) / b;
  614. }
  615. }
  616. static float bin_pos(const int bin, const int num_bins, const float sample_rate)
  617. {
  618. const float max_freq = sample_rate / 2;
  619. const float hz_per_bin = max_freq / num_bins;
  620. const float freq = hz_per_bin * bin;
  621. const float scaled_freq = inv_log_scale(freq + 1, 21, max_freq) - 1;
  622. return num_bins * scaled_freq / max_freq;
  623. }
  624. static int draw_legend(AVFilterContext *ctx, int samples)
  625. {
  626. ShowSpectrumContext *s = ctx->priv;
  627. AVFilterLink *inlink = ctx->inputs[0];
  628. AVFilterLink *outlink = ctx->outputs[0];
  629. int ch, y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
  630. int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL);
  631. float spp = samples / (float)sz;
  632. char *text;
  633. uint8_t *dst;
  634. char chlayout_str[128];
  635. av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), inlink->channels,
  636. inlink->channel_layout);
  637. text = av_asprintf("%d Hz | %s", inlink->sample_rate, chlayout_str);
  638. drawtext(s->outpicref, 2, outlink->h - 10, "CREATED BY LIBAVFILTER", 0);
  639. drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, outlink->h - 10, text, 0);
  640. if (s->stop) {
  641. char *text = av_asprintf("Zoom: %d Hz - %d Hz", s->start, s->stop);
  642. drawtext(s->outpicref, outlink->w - 2 - strlen(text) * 10, 3, text, 0);
  643. av_freep(&text);
  644. }
  645. av_freep(&text);
  646. dst = s->outpicref->data[0] + (s->start_y - 1) * s->outpicref->linesize[0] + s->start_x - 1;
  647. for (x = 0; x < s->w + 1; x++)
  648. dst[x] = 200;
  649. dst = s->outpicref->data[0] + (s->start_y + s->h) * s->outpicref->linesize[0] + s->start_x - 1;
  650. for (x = 0; x < s->w + 1; x++)
  651. dst[x] = 200;
  652. for (y = 0; y < s->h + 2; y++) {
  653. dst = s->outpicref->data[0] + (y + s->start_y - 1) * s->outpicref->linesize[0];
  654. dst[s->start_x - 1] = 200;
  655. dst[s->start_x + s->w] = 200;
  656. }
  657. if (s->orientation == VERTICAL) {
  658. int h = s->mode == SEPARATE ? s->h / s->nb_display_channels : s->h;
  659. int hh = s->mode == SEPARATE ? -(s->h % s->nb_display_channels) + 1 : 1;
  660. for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
  661. for (y = 0; y < h; y += 20) {
  662. dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
  663. dst[s->start_x - 2] = 200;
  664. dst[s->start_x + s->w + 1] = 200;
  665. }
  666. for (y = 0; y < h; y += 40) {
  667. dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - hh) * s->outpicref->linesize[0];
  668. dst[s->start_x - 3] = 200;
  669. dst[s->start_x + s->w + 2] = 200;
  670. }
  671. dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x;
  672. for (x = 0; x < s->w; x+=40)
  673. dst[x] = 200;
  674. dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x;
  675. for (x = 0; x < s->w; x+=80)
  676. dst[x] = 200;
  677. dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x;
  678. for (x = 0; x < s->w; x+=40) {
  679. dst[x] = 200;
  680. }
  681. dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x;
  682. for (x = 0; x < s->w; x+=80) {
  683. dst[x] = 200;
  684. }
  685. for (y = 0; y < h; y += 40) {
  686. float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
  687. float bin = s->fscale == F_LINEAR ? y : get_log_hz(y, h, inlink->sample_rate);
  688. float hertz = s->start + bin * range / (float)(1 << (int)ceil(log2(h)));
  689. char *units;
  690. if (hertz == 0)
  691. units = av_asprintf("DC");
  692. else
  693. units = av_asprintf("%.2f", hertz);
  694. if (!units)
  695. return AVERROR(ENOMEM);
  696. drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4 - hh, units, 0);
  697. av_free(units);
  698. }
  699. }
  700. for (x = 0; x < s->w && s->single_pic; x+=80) {
  701. float seconds = x * spp / inlink->sample_rate;
  702. char *units = get_time(ctx, seconds, x);
  703. drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->h + s->start_y + 6, units, 0);
  704. drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->start_y - 12, units, 0);
  705. av_free(units);
  706. }
  707. drawtext(s->outpicref, outlink->w / 2 - 4 * 4, outlink->h - s->start_y / 2, "TIME", 0);
  708. drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 14 * 4, "FREQUENCY (Hz)", 1);
  709. } else {
  710. int w = s->mode == SEPARATE ? s->w / s->nb_display_channels : s->w;
  711. for (y = 0; y < s->h; y += 20) {
  712. dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
  713. dst[s->start_x - 2] = 200;
  714. dst[s->start_x + s->w + 1] = 200;
  715. }
  716. for (y = 0; y < s->h; y += 40) {
  717. dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0];
  718. dst[s->start_x - 3] = 200;
  719. dst[s->start_x + s->w + 2] = 200;
  720. }
  721. for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) {
  722. dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
  723. for (x = 0; x < w; x+=40)
  724. dst[x] = 200;
  725. dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x + w * ch;
  726. for (x = 0; x < w; x+=80)
  727. dst[x] = 200;
  728. dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x + w * ch;
  729. for (x = 0; x < w; x+=40) {
  730. dst[x] = 200;
  731. }
  732. dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x + w * ch;
  733. for (x = 0; x < w; x+=80) {
  734. dst[x] = 200;
  735. }
  736. for (x = 0; x < w - 79; x += 80) {
  737. float range = s->stop ? s->stop - s->start : inlink->sample_rate / 2;
  738. float bin = s->fscale == F_LINEAR ? x : get_log_hz(x, w, inlink->sample_rate);
  739. float hertz = s->start + bin * range / (float)(1 << (int)ceil(log2(w)));
  740. char *units;
  741. if (hertz == 0)
  742. units = av_asprintf("DC");
  743. else
  744. units = av_asprintf("%.2f", hertz);
  745. if (!units)
  746. return AVERROR(ENOMEM);
  747. drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->start_y - 12, units, 0);
  748. drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->h + s->start_y + 6, units, 0);
  749. av_free(units);
  750. }
  751. }
  752. for (y = 0; y < s->h && s->single_pic; y+=40) {
  753. float seconds = y * spp / inlink->sample_rate;
  754. char *units = get_time(ctx, seconds, x);
  755. drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0);
  756. av_free(units);
  757. }
  758. drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 4 * 4, "TIME", 1);
  759. drawtext(s->outpicref, outlink->w / 2 - 14 * 4, outlink->h - s->start_y / 2, "FREQUENCY (Hz)", 0);
  760. }
  761. for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) {
  762. int h = multi ? s->h / s->nb_display_channels : s->h;
  763. for (y = 0; y < h; y++) {
  764. float out[3] = { 0., 127.5, 127.5};
  765. int chn;
  766. for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) {
  767. float yf, uf, vf;
  768. int channel = (multi) ? s->nb_display_channels - ch - 1 : chn;
  769. float lout[3];
  770. color_range(s, channel, &yf, &uf, &vf);
  771. pick_color(s, yf, uf, vf, y / (float)h, lout);
  772. out[0] += lout[0];
  773. out[1] += lout[1];
  774. out[2] += lout[2];
  775. }
  776. 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);
  777. 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);
  778. 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);
  779. }
  780. for (y = 0; ch == 0 && y < h; y += h / 10) {
  781. float value = 120.f * log10f(1.f - y / (float)h);
  782. char *text;
  783. if (value < -120)
  784. break;
  785. text = av_asprintf("%.0f dB", value);
  786. if (!text)
  787. continue;
  788. drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 5, text, 0);
  789. av_free(text);
  790. }
  791. }
  792. return 0;
  793. }
  794. static float get_value(AVFilterContext *ctx, int ch, int y)
  795. {
  796. ShowSpectrumContext *s = ctx->priv;
  797. float *magnitudes = s->magnitudes[ch];
  798. float *phases = s->phases[ch];
  799. float a;
  800. switch (s->data) {
  801. case D_MAGNITUDE:
  802. /* get magnitude */
  803. a = magnitudes[y];
  804. break;
  805. case D_PHASE:
  806. /* get phase */
  807. a = phases[y];
  808. break;
  809. default:
  810. av_assert0(0);
  811. }
  812. /* apply scale */
  813. switch (s->scale) {
  814. case LINEAR:
  815. a = av_clipf(a, 0, 1);
  816. break;
  817. case SQRT:
  818. a = av_clipf(sqrtf(a), 0, 1);
  819. break;
  820. case CBRT:
  821. a = av_clipf(cbrtf(a), 0, 1);
  822. break;
  823. case FOURTHRT:
  824. a = av_clipf(sqrtf(sqrtf(a)), 0, 1);
  825. break;
  826. case FIFTHRT:
  827. a = av_clipf(powf(a, 0.20), 0, 1);
  828. break;
  829. case LOG:
  830. a = 1.f + log10f(av_clipf(a, 1e-6, 1)) / 6.f; // zero = -120dBFS
  831. break;
  832. default:
  833. av_assert0(0);
  834. }
  835. return a;
  836. }
  837. static int plot_channel_lin(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  838. {
  839. ShowSpectrumContext *s = ctx->priv;
  840. const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
  841. const int ch = jobnr;
  842. float yf, uf, vf;
  843. int y;
  844. /* decide color range */
  845. color_range(s, ch, &yf, &uf, &vf);
  846. /* draw the channel */
  847. for (y = 0; y < h; y++) {
  848. int row = (s->mode == COMBINED) ? y : ch * h + y;
  849. float *out = &s->color_buffer[ch][3 * row];
  850. float a = get_value(ctx, ch, y);
  851. pick_color(s, yf, uf, vf, a, out);
  852. }
  853. return 0;
  854. }
  855. static int plot_channel_log(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  856. {
  857. ShowSpectrumContext *s = ctx->priv;
  858. AVFilterLink *inlink = ctx->inputs[0];
  859. const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
  860. const int ch = jobnr;
  861. float y, yf, uf, vf;
  862. int yy = 0;
  863. /* decide color range */
  864. color_range(s, ch, &yf, &uf, &vf);
  865. /* draw the channel */
  866. for (y = 0; y < h && yy < h; yy++) {
  867. float pos0 = bin_pos(yy+0, h, inlink->sample_rate);
  868. float pos1 = bin_pos(yy+1, h, inlink->sample_rate);
  869. float delta = pos1 - pos0;
  870. float a0, a1;
  871. a0 = get_value(ctx, ch, yy+0);
  872. a1 = get_value(ctx, ch, FFMIN(yy+1, h-1));
  873. for (float j = pos0; j < pos1 && y + j - pos0 < h; j++) {
  874. float row = (s->mode == COMBINED) ? y + j - pos0 : ch * h + y + j - pos0;
  875. float *out = &s->color_buffer[ch][3 * FFMIN(lrintf(row), h-1)];
  876. float lerpfrac = (j - pos0) / delta;
  877. pick_color(s, yf, uf, vf, lerpfrac * a1 + (1.f-lerpfrac) * a0, out);
  878. }
  879. y += delta;
  880. }
  881. return 0;
  882. }
  883. static int config_output(AVFilterLink *outlink)
  884. {
  885. AVFilterContext *ctx = outlink->src;
  886. AVFilterLink *inlink = ctx->inputs[0];
  887. ShowSpectrumContext *s = ctx->priv;
  888. int i, fft_bits, h, w;
  889. float overlap;
  890. switch (s->fscale) {
  891. case F_LINEAR: s->plot_channel = plot_channel_lin; break;
  892. case F_LOG: s->plot_channel = plot_channel_log; break;
  893. default: return AVERROR_BUG;
  894. }
  895. s->stop = FFMIN(s->stop, inlink->sample_rate / 2);
  896. if (s->stop && s->stop <= s->start) {
  897. av_log(ctx, AV_LOG_ERROR, "Stop frequency should be greater than start.\n");
  898. return AVERROR(EINVAL);
  899. }
  900. if (!strcmp(ctx->filter->name, "showspectrumpic"))
  901. s->single_pic = 1;
  902. outlink->w = s->w;
  903. outlink->h = s->h;
  904. outlink->sample_aspect_ratio = (AVRational){1,1};
  905. if (s->legend) {
  906. s->start_x = (log10(inlink->sample_rate) + 1) * 25;
  907. s->start_y = 64;
  908. outlink->w += s->start_x * 2;
  909. outlink->h += s->start_y * 2;
  910. }
  911. h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? s->h : s->h / inlink->channels;
  912. w = (s->mode == COMBINED || s->orientation == VERTICAL) ? s->w : s->w / inlink->channels;
  913. s->channel_height = h;
  914. s->channel_width = w;
  915. if (s->orientation == VERTICAL) {
  916. /* FFT window size (precision) according to the requested output frame height */
  917. for (fft_bits = 1; 1 << fft_bits < 2 * h; fft_bits++);
  918. } else {
  919. /* FFT window size (precision) according to the requested output frame width */
  920. for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++);
  921. }
  922. s->win_size = 1 << fft_bits;
  923. s->buf_size = s->win_size << !!s->stop;
  924. if (!s->fft) {
  925. s->fft = av_calloc(inlink->channels, sizeof(*s->fft));
  926. if (!s->fft)
  927. return AVERROR(ENOMEM);
  928. }
  929. if (s->stop) {
  930. if (!s->ifft) {
  931. s->ifft = av_calloc(inlink->channels, sizeof(*s->ifft));
  932. if (!s->ifft)
  933. return AVERROR(ENOMEM);
  934. }
  935. }
  936. /* (re-)configuration if the video output changed (or first init) */
  937. if (fft_bits != s->fft_bits) {
  938. AVFrame *outpicref;
  939. s->fft_bits = fft_bits;
  940. /* FFT buffers: x2 for each (display) channel buffer.
  941. * Note: we use free and malloc instead of a realloc-like function to
  942. * make sure the buffer is aligned in memory for the FFT functions. */
  943. for (i = 0; i < s->nb_display_channels; i++) {
  944. if (s->stop) {
  945. av_fft_end(s->ifft[i]);
  946. av_freep(&s->fft_scratch[i]);
  947. }
  948. av_fft_end(s->fft[i]);
  949. av_freep(&s->fft_data[i]);
  950. }
  951. av_freep(&s->fft_data);
  952. s->nb_display_channels = inlink->channels;
  953. for (i = 0; i < s->nb_display_channels; i++) {
  954. s->fft[i] = av_fft_init(fft_bits + !!s->stop, 0);
  955. if (s->stop) {
  956. s->ifft[i] = av_fft_init(fft_bits + !!s->stop, 1);
  957. if (!s->ifft[i]) {
  958. av_log(ctx, AV_LOG_ERROR, "Unable to create Inverse FFT context. "
  959. "The window size might be too high.\n");
  960. return AVERROR(EINVAL);
  961. }
  962. }
  963. if (!s->fft[i]) {
  964. av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
  965. "The window size might be too high.\n");
  966. return AVERROR(EINVAL);
  967. }
  968. }
  969. s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
  970. if (!s->magnitudes)
  971. return AVERROR(ENOMEM);
  972. for (i = 0; i < s->nb_display_channels; i++) {
  973. s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
  974. if (!s->magnitudes[i])
  975. return AVERROR(ENOMEM);
  976. }
  977. s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases));
  978. if (!s->phases)
  979. return AVERROR(ENOMEM);
  980. for (i = 0; i < s->nb_display_channels; i++) {
  981. s->phases[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->phases));
  982. if (!s->phases[i])
  983. return AVERROR(ENOMEM);
  984. }
  985. av_freep(&s->color_buffer);
  986. s->color_buffer = av_calloc(s->nb_display_channels, sizeof(*s->color_buffer));
  987. if (!s->color_buffer)
  988. return AVERROR(ENOMEM);
  989. for (i = 0; i < s->nb_display_channels; i++) {
  990. s->color_buffer[i] = av_calloc(s->orientation == VERTICAL ? s->h * 3 : s->w * 3, sizeof(**s->color_buffer));
  991. if (!s->color_buffer[i])
  992. return AVERROR(ENOMEM);
  993. }
  994. s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data));
  995. if (!s->fft_data)
  996. return AVERROR(ENOMEM);
  997. s->fft_scratch = av_calloc(s->nb_display_channels, sizeof(*s->fft_scratch));
  998. if (!s->fft_scratch)
  999. return AVERROR(ENOMEM);
  1000. for (i = 0; i < s->nb_display_channels; i++) {
  1001. s->fft_data[i] = av_calloc(s->buf_size, sizeof(**s->fft_data));
  1002. if (!s->fft_data[i])
  1003. return AVERROR(ENOMEM);
  1004. s->fft_scratch[i] = av_calloc(s->buf_size, sizeof(**s->fft_scratch));
  1005. if (!s->fft_scratch[i])
  1006. return AVERROR(ENOMEM);
  1007. }
  1008. /* pre-calc windowing function */
  1009. s->window_func_lut =
  1010. av_realloc_f(s->window_func_lut, s->win_size,
  1011. sizeof(*s->window_func_lut));
  1012. if (!s->window_func_lut)
  1013. return AVERROR(ENOMEM);
  1014. generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
  1015. if (s->overlap == 1)
  1016. s->overlap = overlap;
  1017. s->hop_size = (1.f - s->overlap) * s->win_size;
  1018. if (s->hop_size < 1) {
  1019. av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
  1020. return AVERROR(EINVAL);
  1021. }
  1022. for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
  1023. s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
  1024. }
  1025. s->win_scale = 1.f / sqrtf(s->win_scale);
  1026. /* prepare the initial picref buffer (black frame) */
  1027. av_frame_free(&s->outpicref);
  1028. s->outpicref = outpicref =
  1029. ff_get_video_buffer(outlink, outlink->w, outlink->h);
  1030. if (!outpicref)
  1031. return AVERROR(ENOMEM);
  1032. outpicref->sample_aspect_ratio = (AVRational){1,1};
  1033. for (i = 0; i < outlink->h; i++) {
  1034. memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
  1035. memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
  1036. memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
  1037. }
  1038. outpicref->color_range = AVCOL_RANGE_JPEG;
  1039. if (!s->single_pic && s->legend)
  1040. draw_legend(ctx, 0);
  1041. }
  1042. if ((s->orientation == VERTICAL && s->xpos >= s->w) ||
  1043. (s->orientation == HORIZONTAL && s->xpos >= s->h))
  1044. s->xpos = 0;
  1045. s->auto_frame_rate = av_make_q(inlink->sample_rate, s->hop_size);
  1046. if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
  1047. s->auto_frame_rate.den *= s->w;
  1048. if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
  1049. s->auto_frame_rate.den *= s->h;
  1050. if (!s->single_pic && strcmp(s->rate_str, "auto")) {
  1051. int ret = av_parse_video_rate(&s->frame_rate, s->rate_str);
  1052. if (ret < 0)
  1053. return ret;
  1054. } else {
  1055. s->frame_rate = s->auto_frame_rate;
  1056. }
  1057. outlink->frame_rate = s->frame_rate;
  1058. outlink->time_base = av_inv_q(outlink->frame_rate);
  1059. if (s->orientation == VERTICAL) {
  1060. s->combine_buffer =
  1061. av_realloc_f(s->combine_buffer, s->h * 3,
  1062. sizeof(*s->combine_buffer));
  1063. } else {
  1064. s->combine_buffer =
  1065. av_realloc_f(s->combine_buffer, s->w * 3,
  1066. sizeof(*s->combine_buffer));
  1067. }
  1068. av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n",
  1069. s->w, s->h, s->win_size);
  1070. av_audio_fifo_free(s->fifo);
  1071. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
  1072. if (!s->fifo)
  1073. return AVERROR(ENOMEM);
  1074. return 0;
  1075. }
  1076. #define RE(y, ch) s->fft_data[ch][y].re
  1077. #define IM(y, ch) s->fft_data[ch][y].im
  1078. #define MAGNITUDE(y, ch) hypotf(RE(y, ch), IM(y, ch))
  1079. #define PHASE(y, ch) atan2f(IM(y, ch), RE(y, ch))
  1080. static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  1081. {
  1082. ShowSpectrumContext *s = ctx->priv;
  1083. const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
  1084. int y, h = s->orientation == VERTICAL ? s->h : s->w;
  1085. const float f = s->gain * w;
  1086. const int ch = jobnr;
  1087. float *magnitudes = s->magnitudes[ch];
  1088. for (y = 0; y < h; y++)
  1089. magnitudes[y] = MAGNITUDE(y, ch) * f;
  1090. return 0;
  1091. }
  1092. static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  1093. {
  1094. ShowSpectrumContext *s = ctx->priv;
  1095. const int h = s->orientation == VERTICAL ? s->h : s->w;
  1096. const int ch = jobnr;
  1097. float *phases = s->phases[ch];
  1098. int y;
  1099. for (y = 0; y < h; y++)
  1100. phases[y] = (PHASE(y, ch) / M_PI + 1) / 2;
  1101. return 0;
  1102. }
  1103. static void acalc_magnitudes(ShowSpectrumContext *s)
  1104. {
  1105. const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1);
  1106. int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
  1107. const float f = s->gain * w;
  1108. for (ch = 0; ch < s->nb_display_channels; ch++) {
  1109. float *magnitudes = s->magnitudes[ch];
  1110. for (y = 0; y < h; y++)
  1111. magnitudes[y] += MAGNITUDE(y, ch) * f;
  1112. }
  1113. }
  1114. static void scale_magnitudes(ShowSpectrumContext *s, float scale)
  1115. {
  1116. int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
  1117. for (ch = 0; ch < s->nb_display_channels; ch++) {
  1118. float *magnitudes = s->magnitudes[ch];
  1119. for (y = 0; y < h; y++)
  1120. magnitudes[y] *= scale;
  1121. }
  1122. }
  1123. static void clear_combine_buffer(ShowSpectrumContext *s, int size)
  1124. {
  1125. int y;
  1126. for (y = 0; y < size; y++) {
  1127. s->combine_buffer[3 * y ] = 0;
  1128. s->combine_buffer[3 * y + 1] = 127.5;
  1129. s->combine_buffer[3 * y + 2] = 127.5;
  1130. }
  1131. }
  1132. static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
  1133. {
  1134. AVFilterContext *ctx = inlink->dst;
  1135. AVFilterLink *outlink = ctx->outputs[0];
  1136. ShowSpectrumContext *s = ctx->priv;
  1137. AVFrame *outpicref = s->outpicref;
  1138. int ret, plane, x, y, z = s->orientation == VERTICAL ? s->h : s->w;
  1139. /* fill a new spectrum column */
  1140. /* initialize buffer for combining to black */
  1141. clear_combine_buffer(s, z);
  1142. ctx->internal->execute(ctx, s->plot_channel, NULL, NULL, s->nb_display_channels);
  1143. for (y = 0; y < z * 3; y++) {
  1144. for (x = 0; x < s->nb_display_channels; x++) {
  1145. s->combine_buffer[y] += s->color_buffer[x][y];
  1146. }
  1147. }
  1148. av_frame_make_writable(s->outpicref);
  1149. /* copy to output */
  1150. if (s->orientation == VERTICAL) {
  1151. if (s->sliding == SCROLL) {
  1152. for (plane = 0; plane < 3; plane++) {
  1153. for (y = 0; y < s->h; y++) {
  1154. uint8_t *p = outpicref->data[plane] + s->start_x +
  1155. (y + s->start_y) * outpicref->linesize[plane];
  1156. memmove(p, p + 1, s->w - 1);
  1157. }
  1158. }
  1159. s->xpos = s->w - 1;
  1160. } else if (s->sliding == RSCROLL) {
  1161. for (plane = 0; plane < 3; plane++) {
  1162. for (y = 0; y < s->h; y++) {
  1163. uint8_t *p = outpicref->data[plane] + s->start_x +
  1164. (y + s->start_y) * outpicref->linesize[plane];
  1165. memmove(p + 1, p, s->w - 1);
  1166. }
  1167. }
  1168. s->xpos = 0;
  1169. }
  1170. for (plane = 0; plane < 3; plane++) {
  1171. uint8_t *p = outpicref->data[plane] + s->start_x +
  1172. (outlink->h - 1 - s->start_y) * outpicref->linesize[plane] +
  1173. s->xpos;
  1174. for (y = 0; y < s->h; y++) {
  1175. *p = lrintf(av_clipf(s->combine_buffer[3 * y + plane], 0, 255));
  1176. p -= outpicref->linesize[plane];
  1177. }
  1178. }
  1179. } else {
  1180. if (s->sliding == SCROLL) {
  1181. for (plane = 0; plane < 3; plane++) {
  1182. for (y = 1; y < s->h; y++) {
  1183. memmove(outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
  1184. outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
  1185. s->w);
  1186. }
  1187. }
  1188. s->xpos = s->h - 1;
  1189. } else if (s->sliding == RSCROLL) {
  1190. for (plane = 0; plane < 3; plane++) {
  1191. for (y = s->h - 1; y >= 1; y--) {
  1192. memmove(outpicref->data[plane] + (y + s->start_y) * outpicref->linesize[plane] + s->start_x,
  1193. outpicref->data[plane] + (y-1 + s->start_y) * outpicref->linesize[plane] + s->start_x,
  1194. s->w);
  1195. }
  1196. }
  1197. s->xpos = 0;
  1198. }
  1199. for (plane = 0; plane < 3; plane++) {
  1200. uint8_t *p = outpicref->data[plane] + s->start_x +
  1201. (s->xpos + s->start_y) * outpicref->linesize[plane];
  1202. for (x = 0; x < s->w; x++) {
  1203. *p = lrintf(av_clipf(s->combine_buffer[3 * x + plane], 0, 255));
  1204. p++;
  1205. }
  1206. }
  1207. }
  1208. if (s->sliding != FULLFRAME || s->xpos == 0)
  1209. outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
  1210. s->xpos++;
  1211. if (s->orientation == VERTICAL && s->xpos >= s->w)
  1212. s->xpos = 0;
  1213. if (s->orientation == HORIZONTAL && s->xpos >= s->h)
  1214. s->xpos = 0;
  1215. if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
  1216. if (s->old_pts < outpicref->pts) {
  1217. if (s->legend) {
  1218. char *units = get_time(ctx, insamples->pts /(float)inlink->sample_rate, x);
  1219. if (s->orientation == VERTICAL) {
  1220. for (y = 0; y < 10; y++) {
  1221. memset(s->outpicref->data[0] + outlink->w / 2 - 4 * s->old_len +
  1222. (outlink->h - s->start_y / 2 - 20 + y) * s->outpicref->linesize[0], 0, 10 * s->old_len);
  1223. }
  1224. drawtext(s->outpicref,
  1225. outlink->w / 2 - 4 * strlen(units),
  1226. outlink->h - s->start_y / 2 - 20,
  1227. units, 0);
  1228. } else {
  1229. for (y = 0; y < 10 * s->old_len; y++) {
  1230. memset(s->outpicref->data[0] + s->start_x / 7 + 20 +
  1231. (outlink->h / 2 - 4 * s->old_len + y) * s->outpicref->linesize[0], 0, 10);
  1232. }
  1233. drawtext(s->outpicref,
  1234. s->start_x / 7 + 20,
  1235. outlink->h / 2 - 4 * strlen(units),
  1236. units, 1);
  1237. }
  1238. s->old_len = strlen(units);
  1239. av_free(units);
  1240. }
  1241. s->old_pts = outpicref->pts;
  1242. ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
  1243. if (ret < 0)
  1244. return ret;
  1245. return 0;
  1246. }
  1247. }
  1248. return 1;
  1249. }
  1250. #if CONFIG_SHOWSPECTRUM_FILTER
  1251. static int activate(AVFilterContext *ctx)
  1252. {
  1253. AVFilterLink *inlink = ctx->inputs[0];
  1254. AVFilterLink *outlink = ctx->outputs[0];
  1255. ShowSpectrumContext *s = ctx->priv;
  1256. int ret;
  1257. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  1258. if (av_audio_fifo_size(s->fifo) < s->win_size) {
  1259. AVFrame *frame = NULL;
  1260. ret = ff_inlink_consume_frame(inlink, &frame);
  1261. if (ret < 0)
  1262. return ret;
  1263. if (ret > 0) {
  1264. s->pts = frame->pts;
  1265. s->consumed = 0;
  1266. av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
  1267. av_frame_free(&frame);
  1268. }
  1269. }
  1270. if (s->outpicref && av_audio_fifo_size(s->fifo) >= s->win_size) {
  1271. AVFrame *fin = ff_get_audio_buffer(inlink, s->win_size);
  1272. if (!fin)
  1273. return AVERROR(ENOMEM);
  1274. fin->pts = s->pts + s->consumed;
  1275. s->consumed += s->hop_size;
  1276. ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data,
  1277. FFMIN(s->win_size, av_audio_fifo_size(s->fifo)));
  1278. if (ret < 0) {
  1279. av_frame_free(&fin);
  1280. return ret;
  1281. }
  1282. av_assert0(fin->nb_samples == s->win_size);
  1283. ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
  1284. if (s->data == D_MAGNITUDE)
  1285. ctx->internal->execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels);
  1286. if (s->data == D_PHASE)
  1287. ctx->internal->execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels);
  1288. ret = plot_spectrum_column(inlink, fin);
  1289. av_frame_free(&fin);
  1290. av_audio_fifo_drain(s->fifo, s->hop_size);
  1291. if (ret <= 0)
  1292. return ret;
  1293. }
  1294. if (ff_outlink_get_status(inlink) == AVERROR_EOF &&
  1295. s->sliding == FULLFRAME &&
  1296. s->xpos > 0 && s->outpicref) {
  1297. int64_t pts;
  1298. if (s->orientation == VERTICAL) {
  1299. for (int i = 0; i < outlink->h; i++) {
  1300. memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos);
  1301. memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
  1302. memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
  1303. }
  1304. } else {
  1305. for (int i = s->xpos; i < outlink->h; i++) {
  1306. memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w);
  1307. memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
  1308. memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
  1309. }
  1310. }
  1311. s->outpicref->pts += s->consumed;
  1312. pts = s->outpicref->pts;
  1313. ret = ff_filter_frame(outlink, s->outpicref);
  1314. s->outpicref = NULL;
  1315. ff_outlink_set_status(outlink, AVERROR_EOF, pts);
  1316. return 0;
  1317. }
  1318. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  1319. if (ff_outlink_frame_wanted(outlink) && av_audio_fifo_size(s->fifo) < s->win_size) {
  1320. ff_inlink_request_frame(inlink);
  1321. return 0;
  1322. }
  1323. if (av_audio_fifo_size(s->fifo) >= s->win_size) {
  1324. ff_filter_set_ready(ctx, 10);
  1325. return 0;
  1326. }
  1327. return FFERROR_NOT_READY;
  1328. }
  1329. static const AVFilterPad showspectrum_inputs[] = {
  1330. {
  1331. .name = "default",
  1332. .type = AVMEDIA_TYPE_AUDIO,
  1333. },
  1334. { NULL }
  1335. };
  1336. static const AVFilterPad showspectrum_outputs[] = {
  1337. {
  1338. .name = "default",
  1339. .type = AVMEDIA_TYPE_VIDEO,
  1340. .config_props = config_output,
  1341. },
  1342. { NULL }
  1343. };
  1344. AVFilter ff_avf_showspectrum = {
  1345. .name = "showspectrum",
  1346. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
  1347. .uninit = uninit,
  1348. .query_formats = query_formats,
  1349. .priv_size = sizeof(ShowSpectrumContext),
  1350. .inputs = showspectrum_inputs,
  1351. .outputs = showspectrum_outputs,
  1352. .activate = activate,
  1353. .priv_class = &showspectrum_class,
  1354. .flags = AVFILTER_FLAG_SLICE_THREADS,
  1355. };
  1356. #endif // CONFIG_SHOWSPECTRUM_FILTER
  1357. #if CONFIG_SHOWSPECTRUMPIC_FILTER
  1358. static const AVOption showspectrumpic_options[] = {
  1359. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
  1360. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
  1361. { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" },
  1362. { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
  1363. { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
  1364. { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" },
  1365. { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
  1366. { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
  1367. { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
  1368. { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
  1369. { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
  1370. { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
  1371. { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
  1372. { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" },
  1373. { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" },
  1374. { "magma", "magma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MAGMA}, 0, 0, FLAGS, "color" },
  1375. { "green", "green based coloring", 0, AV_OPT_TYPE_CONST, {.i64=GREEN}, 0, 0, FLAGS, "color" },
  1376. { "viridis", "viridis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=VIRIDIS}, 0, 0, FLAGS, "color" },
  1377. { "plasma", "plasma based coloring", 0, AV_OPT_TYPE_CONST, {.i64=PLASMA}, 0, 0, FLAGS, "color" },
  1378. { "cividis", "cividis based coloring", 0, AV_OPT_TYPE_CONST, {.i64=CIVIDIS}, 0, 0, FLAGS, "color" },
  1379. { "terrain", "terrain based coloring", 0, AV_OPT_TYPE_CONST, {.i64=TERRAIN}, 0, 0, FLAGS, "color" },
  1380. { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" },
  1381. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
  1382. { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
  1383. { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
  1384. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
  1385. { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
  1386. { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
  1387. { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=F_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
  1388. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=F_LINEAR}, 0, 0, FLAGS, "fscale" },
  1389. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=F_LOG}, 0, 0, FLAGS, "fscale" },
  1390. { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
  1391. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
  1392. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
  1393. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
  1394. { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  1395. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  1396. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
  1397. { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
  1398. { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
  1399. { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
  1400. { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
  1401. { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
  1402. { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
  1403. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
  1404. { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
  1405. { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
  1406. { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
  1407. { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
  1408. { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
  1409. { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
  1410. { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
  1411. { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
  1412. { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
  1413. { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
  1414. { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
  1415. { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
  1416. { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS },
  1417. { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
  1418. { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
  1419. { "start", "start frequency", OFFSET(start), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
  1420. { "stop", "stop frequency", OFFSET(stop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT32_MAX, FLAGS },
  1421. { NULL }
  1422. };
  1423. AVFILTER_DEFINE_CLASS(showspectrumpic);
  1424. static int showspectrumpic_request_frame(AVFilterLink *outlink)
  1425. {
  1426. AVFilterContext *ctx = outlink->src;
  1427. ShowSpectrumContext *s = ctx->priv;
  1428. AVFilterLink *inlink = ctx->inputs[0];
  1429. int ret, samples;
  1430. ret = ff_request_frame(inlink);
  1431. samples = av_audio_fifo_size(s->fifo);
  1432. if (ret == AVERROR_EOF && s->outpicref && samples > 0) {
  1433. int consumed = 0;
  1434. int x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
  1435. int ch, spf, spb;
  1436. AVFrame *fin;
  1437. spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz))));
  1438. spf = FFMAX(1, spf);
  1439. spb = (samples / (spf * sz)) * spf;
  1440. fin = ff_get_audio_buffer(inlink, s->win_size);
  1441. if (!fin)
  1442. return AVERROR(ENOMEM);
  1443. while (x < sz) {
  1444. ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
  1445. if (ret < 0) {
  1446. av_frame_free(&fin);
  1447. return ret;
  1448. }
  1449. av_audio_fifo_drain(s->fifo, spf);
  1450. if (ret < s->win_size) {
  1451. for (ch = 0; ch < s->nb_display_channels; ch++) {
  1452. memset(fin->extended_data[ch] + ret * sizeof(float), 0,
  1453. (s->win_size - ret) * sizeof(float));
  1454. }
  1455. }
  1456. ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels);
  1457. acalc_magnitudes(s);
  1458. consumed += spf;
  1459. if (consumed >= spb) {
  1460. int h = s->orientation == VERTICAL ? s->h : s->w;
  1461. scale_magnitudes(s, 1.f / (consumed / spf));
  1462. plot_spectrum_column(inlink, fin);
  1463. consumed = 0;
  1464. x++;
  1465. for (ch = 0; ch < s->nb_display_channels; ch++)
  1466. memset(s->magnitudes[ch], 0, h * sizeof(float));
  1467. }
  1468. }
  1469. av_frame_free(&fin);
  1470. s->outpicref->pts = 0;
  1471. if (s->legend)
  1472. draw_legend(ctx, samples);
  1473. ret = ff_filter_frame(outlink, s->outpicref);
  1474. s->outpicref = NULL;
  1475. }
  1476. return ret;
  1477. }
  1478. static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  1479. {
  1480. AVFilterContext *ctx = inlink->dst;
  1481. ShowSpectrumContext *s = ctx->priv;
  1482. int ret;
  1483. ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
  1484. av_frame_free(&insamples);
  1485. return ret;
  1486. }
  1487. static const AVFilterPad showspectrumpic_inputs[] = {
  1488. {
  1489. .name = "default",
  1490. .type = AVMEDIA_TYPE_AUDIO,
  1491. .filter_frame = showspectrumpic_filter_frame,
  1492. },
  1493. { NULL }
  1494. };
  1495. static const AVFilterPad showspectrumpic_outputs[] = {
  1496. {
  1497. .name = "default",
  1498. .type = AVMEDIA_TYPE_VIDEO,
  1499. .config_props = config_output,
  1500. .request_frame = showspectrumpic_request_frame,
  1501. },
  1502. { NULL }
  1503. };
  1504. AVFilter ff_avf_showspectrumpic = {
  1505. .name = "showspectrumpic",
  1506. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
  1507. .uninit = uninit,
  1508. .query_formats = query_formats,
  1509. .priv_size = sizeof(ShowSpectrumContext),
  1510. .inputs = showspectrumpic_inputs,
  1511. .outputs = showspectrumpic_outputs,
  1512. .priv_class = &showspectrumpic_class,
  1513. .flags = AVFILTER_FLAG_SLICE_THREADS,
  1514. };
  1515. #endif // CONFIG_SHOWSPECTRUMPIC_FILTER