psy.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. ********************************************************************
  11. function: psychoacoustics not including preecho
  12. last mod: $Id: psy.c,v 1.44.2.2 2001/03/28 04:50:55 segher Exp $
  13. ********************************************************************/
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include <string.h>
  17. #include "vorbis/codec.h"
  18. #include "codec_internal.h"
  19. #include "masking.h"
  20. #include "psy.h"
  21. #include "os.h"
  22. #include "lpc.h"
  23. #include "smallft.h"
  24. #include "scales.h"
  25. #include "misc.h"
  26. //#define todB_t(x) todB(x)
  27. #define todB_t(x) (dbtab[(*(int *)&(x) >> 20)&2047])
  28. static float dbtab[2048];
  29. static void initdbtab()
  30. {
  31. int i, x;
  32. for (i = 0; i < 2048; i++) {
  33. *(int *)(dbtab + i) = (i << 20) | (1 << 19);
  34. dbtab[i] = todB(dbtab[i]);
  35. //fprintf(stderr, "%4d: %08x %12.6f\n", i, (i << 20) | (1 << 19), dbtab[i]);
  36. }
  37. }
  38. #define NEGINF -9999.f
  39. /* Why Bark scale for encoding but not masking computation? Because
  40. masking has a strong harmonic dependancy */
  41. /* the beginnings of real psychoacoustic infrastructure. This is
  42. still not tightly tuned */
  43. void _vi_psy_free(vorbis_info_psy *i){
  44. if(i){
  45. memset(i,0,sizeof(vorbis_info_psy));
  46. _ogg_free(i);
  47. }
  48. }
  49. vorbis_info_psy *_vi_psy_copy(vorbis_info_psy *i){
  50. vorbis_info_psy *ret=_ogg_malloc(sizeof(vorbis_info_psy));
  51. memcpy(ret,i,sizeof(vorbis_info_psy));
  52. return(ret);
  53. }
  54. /* Set up decibel threshold slopes on a Bark frequency scale */
  55. /* ATH is the only bit left on a Bark scale. No reason to change it
  56. right now */
  57. static void set_curve(float *ref,float *c,int n, float crate){
  58. int i,j=0;
  59. for(i=0;i<MAX_BARK-1;i++){
  60. int endpos=rint(fromBARK(i+1)*2*n/crate);
  61. float base=ref[i];
  62. if(j<endpos){
  63. float delta=(ref[i+1]-base)/(endpos-j);
  64. for(;j<endpos && j<n;j++){
  65. c[j]=base;
  66. base+=delta;
  67. }
  68. }
  69. }
  70. }
  71. static void min_curve(float *c,
  72. float *c2){
  73. int i;
  74. for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
  75. }
  76. static void max_curve(float *c,
  77. float *c2){
  78. int i;
  79. for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
  80. }
  81. static void attenuate_curve(float *c,float att){
  82. int i;
  83. for(i=0;i<EHMER_MAX;i++)
  84. c[i]+=att;
  85. }
  86. static void interp_curve(float *c,float *c1,float *c2,float del){
  87. int i;
  88. for(i=0;i<EHMER_MAX;i++)
  89. c[i]=c2[i]*del+c1[i]*(1.f-del);
  90. }
  91. static void setup_curve(float **c,
  92. int band,
  93. float *curveatt_dB){
  94. int i,j;
  95. float ath[EHMER_MAX];
  96. float tempc[P_LEVELS][EHMER_MAX];
  97. memcpy(c[0]+2,c[4]+2,sizeof(float)*EHMER_MAX);
  98. memcpy(c[2]+2,c[4]+2,sizeof(float)*EHMER_MAX);
  99. /* we add back in the ATH to avoid low level curves falling off to
  100. -infinity and unneccessarily cutting off high level curves in the
  101. curve limiting (last step). But again, remember... a half-band's
  102. settings must be valid over the whole band, and it's better to
  103. mask too little than too much, so be pessimal. */
  104. for(i=0;i<EHMER_MAX;i++){
  105. float oc_min=band*.5+(i-EHMER_OFFSET)*.125;
  106. float oc_max=band*.5+(i-EHMER_OFFSET+1)*.125;
  107. float bark=toBARK(fromOC(oc_min));
  108. int ibark=floor(bark);
  109. float del=bark-ibark;
  110. float ath_min,ath_max;
  111. if(ibark<26)
  112. ath_min=ATH_Bark_dB[ibark]*(1.f-del)+ATH_Bark_dB[ibark+1]*del;
  113. else
  114. ath_min=ATH_Bark_dB[25];
  115. bark=toBARK(fromOC(oc_max));
  116. ibark=floor(bark);
  117. del=bark-ibark;
  118. if(ibark<26)
  119. ath_max=ATH_Bark_dB[ibark]*(1.f-del)+ATH_Bark_dB[ibark+1]*del;
  120. else
  121. ath_max=ATH_Bark_dB[25];
  122. ath[i]=min(ath_min,ath_max);
  123. }
  124. /* The c array is comes in as dB curves at 20 40 60 80 100 dB.
  125. interpolate intermediate dB curves */
  126. for(i=1;i<P_LEVELS;i+=2){
  127. interp_curve(c[i]+2,c[i-1]+2,c[i+1]+2,.5);
  128. }
  129. /* normalize curves so the driving amplitude is 0dB */
  130. /* make temp curves with the ATH overlayed */
  131. for(i=0;i<P_LEVELS;i++){
  132. attenuate_curve(c[i]+2,curveatt_dB[i]);
  133. memcpy(tempc[i],ath,EHMER_MAX*sizeof(float));
  134. attenuate_curve(tempc[i],-i*10.f);
  135. max_curve(tempc[i],c[i]+2);
  136. }
  137. /* Now limit the louder curves.
  138. the idea is this: We don't know what the playback attenuation
  139. will be; 0dB SL moves every time the user twiddles the volume
  140. knob. So that means we have to use a single 'most pessimal' curve
  141. for all masking amplitudes, right? Wrong. The *loudest* sound
  142. can be in (we assume) a range of ...+100dB] SL. However, sounds
  143. 20dB down will be in a range ...+80], 40dB down is from ...+60],
  144. etc... */
  145. for(j=1;j<P_LEVELS;j++){
  146. min_curve(tempc[j],tempc[j-1]);
  147. min_curve(c[j]+2,tempc[j]);
  148. }
  149. /* add fenceposts */
  150. for(j=0;j<P_LEVELS;j++){
  151. for(i=0;i<EHMER_MAX;i++)
  152. if(c[j][i+2]>-200.f)break;
  153. c[j][0]=i;
  154. for(i=EHMER_MAX-1;i>=0;i--)
  155. if(c[j][i+2]>-200.f)
  156. break;
  157. c[j][1]=i;
  158. }
  159. }
  160. void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,int n,long rate){
  161. long i,j;
  162. long maxoc;
  163. memset(p,0,sizeof(vorbis_look_psy));
  164. initdbtab();
  165. p->eighth_octave_lines=vi->eighth_octave_lines;
  166. p->shiftoc=rint(log(vi->eighth_octave_lines*8)/log(2))-1;
  167. p->firstoc=toOC(.25f*rate/n)*(1<<(p->shiftoc+1))-vi->eighth_octave_lines;
  168. maxoc=toOC((n*.5f-.25f)*rate/n)*(1<<(p->shiftoc+1))+.5f;
  169. p->total_octave_lines=maxoc-p->firstoc+1;
  170. p->ath=_ogg_malloc(n*sizeof(float));
  171. p->octave=_ogg_malloc(n*sizeof(long));
  172. p->bark=_ogg_malloc(n*sizeof(float));
  173. p->vi=vi;
  174. p->n=n;
  175. /* set up the lookups for a given blocksize and sample rate */
  176. /* Vorbis max sample rate is currently limited by 26 Bark (54kHz) */
  177. set_curve(ATH_Bark_dB, p->ath,n,rate);
  178. for(i=0;i<n;i++)
  179. p->bark[i]=toBARK(rate/(2*n)*i);
  180. for(i=0;i<n;i++)
  181. p->octave[i]=toOC((i*.5f+.25f)*rate/n)*(1<<(p->shiftoc+1))+.5f;
  182. p->tonecurves=_ogg_malloc(P_BANDS*sizeof(float **));
  183. p->noisemedian=_ogg_malloc(n*sizeof(float));
  184. p->inoisemedian=_ogg_malloc(n*sizeof(int));
  185. p->barknoisewindowlo=_ogg_malloc(n*sizeof(int));
  186. p->barknoisewindowhi=_ogg_malloc(n*sizeof(int));
  187. p->noiseoffset=_ogg_malloc(n*sizeof(float));
  188. p->peakatt=_ogg_malloc(P_BANDS*sizeof(float *));
  189. for(i=0;i<P_BANDS;i++){
  190. p->tonecurves[i]=_ogg_malloc(P_LEVELS*sizeof(float *));
  191. p->peakatt[i]=_ogg_malloc(P_LEVELS*sizeof(float));
  192. }
  193. for(i=0;i<P_BANDS;i++)
  194. for(j=0;j<P_LEVELS;j++){
  195. p->tonecurves[i][j]=_ogg_malloc((EHMER_MAX+2)*sizeof(float));
  196. }
  197. /* OK, yeah, this was a silly way to do it */
  198. memcpy(p->tonecurves[0][4]+2,tone_125_40dB_SL,sizeof(float)*EHMER_MAX);
  199. memcpy(p->tonecurves[0][6]+2,tone_125_60dB_SL,sizeof(float)*EHMER_MAX);
  200. memcpy(p->tonecurves[0][8]+2,tone_125_80dB_SL,sizeof(float)*EHMER_MAX);
  201. memcpy(p->tonecurves[0][10]+2,tone_125_100dB_SL,sizeof(float)*EHMER_MAX);
  202. memcpy(p->tonecurves[2][4]+2,tone_125_40dB_SL,sizeof(float)*EHMER_MAX);
  203. memcpy(p->tonecurves[2][6]+2,tone_125_60dB_SL,sizeof(float)*EHMER_MAX);
  204. memcpy(p->tonecurves[2][8]+2,tone_125_80dB_SL,sizeof(float)*EHMER_MAX);
  205. memcpy(p->tonecurves[2][10]+2,tone_125_100dB_SL,sizeof(float)*EHMER_MAX);
  206. memcpy(p->tonecurves[4][4]+2,tone_250_40dB_SL,sizeof(float)*EHMER_MAX);
  207. memcpy(p->tonecurves[4][6]+2,tone_250_60dB_SL,sizeof(float)*EHMER_MAX);
  208. memcpy(p->tonecurves[4][8]+2,tone_250_80dB_SL,sizeof(float)*EHMER_MAX);
  209. memcpy(p->tonecurves[4][10]+2,tone_250_100dB_SL,sizeof(float)*EHMER_MAX);
  210. memcpy(p->tonecurves[6][4]+2,tone_500_40dB_SL,sizeof(float)*EHMER_MAX);
  211. memcpy(p->tonecurves[6][6]+2,tone_500_60dB_SL,sizeof(float)*EHMER_MAX);
  212. memcpy(p->tonecurves[6][8]+2,tone_500_80dB_SL,sizeof(float)*EHMER_MAX);
  213. memcpy(p->tonecurves[6][10]+2,tone_500_100dB_SL,sizeof(float)*EHMER_MAX);
  214. memcpy(p->tonecurves[8][4]+2,tone_1000_40dB_SL,sizeof(float)*EHMER_MAX);
  215. memcpy(p->tonecurves[8][6]+2,tone_1000_60dB_SL,sizeof(float)*EHMER_MAX);
  216. memcpy(p->tonecurves[8][8]+2,tone_1000_80dB_SL,sizeof(float)*EHMER_MAX);
  217. memcpy(p->tonecurves[8][10]+2,tone_1000_100dB_SL,sizeof(float)*EHMER_MAX);
  218. memcpy(p->tonecurves[10][4]+2,tone_2000_40dB_SL,sizeof(float)*EHMER_MAX);
  219. memcpy(p->tonecurves[10][6]+2,tone_2000_60dB_SL,sizeof(float)*EHMER_MAX);
  220. memcpy(p->tonecurves[10][8]+2,tone_2000_80dB_SL,sizeof(float)*EHMER_MAX);
  221. memcpy(p->tonecurves[10][10]+2,tone_2000_100dB_SL,sizeof(float)*EHMER_MAX);
  222. memcpy(p->tonecurves[12][4]+2,tone_4000_40dB_SL,sizeof(float)*EHMER_MAX);
  223. memcpy(p->tonecurves[12][6]+2,tone_4000_60dB_SL,sizeof(float)*EHMER_MAX);
  224. memcpy(p->tonecurves[12][8]+2,tone_4000_80dB_SL,sizeof(float)*EHMER_MAX);
  225. memcpy(p->tonecurves[12][10]+2,tone_4000_100dB_SL,sizeof(float)*EHMER_MAX);
  226. memcpy(p->tonecurves[14][4]+2,tone_8000_40dB_SL,sizeof(float)*EHMER_MAX);
  227. memcpy(p->tonecurves[14][6]+2,tone_8000_60dB_SL,sizeof(float)*EHMER_MAX);
  228. memcpy(p->tonecurves[14][8]+2,tone_8000_80dB_SL,sizeof(float)*EHMER_MAX);
  229. memcpy(p->tonecurves[14][10]+2,tone_8000_100dB_SL,sizeof(float)*EHMER_MAX);
  230. memcpy(p->tonecurves[16][4]+2,tone_8000_40dB_SL,sizeof(float)*EHMER_MAX);
  231. memcpy(p->tonecurves[16][6]+2,tone_8000_60dB_SL,sizeof(float)*EHMER_MAX);
  232. memcpy(p->tonecurves[16][8]+2,tone_8000_80dB_SL,sizeof(float)*EHMER_MAX);
  233. memcpy(p->tonecurves[16][10]+2,tone_8000_100dB_SL,sizeof(float)*EHMER_MAX);
  234. /* interpolate curves between */
  235. for(i=1;i<P_BANDS;i+=2)
  236. for(j=4;j<P_LEVELS;j+=2){
  237. memcpy(p->tonecurves[i][j]+2,p->tonecurves[i-1][j]+2,EHMER_MAX*sizeof(float));
  238. /*interp_curve(p->tonecurves[i][j],
  239. p->tonecurves[i-1][j],
  240. p->tonecurves[i+1][j],.5);*/
  241. min_curve(p->tonecurves[i][j]+2,p->tonecurves[i+1][j]+2);
  242. }
  243. /* set up the final curves */
  244. for(i=0;i<P_BANDS;i++)
  245. setup_curve(p->tonecurves[i],i,vi->toneatt[i]);
  246. /* set up attenuation levels */
  247. for(i=0;i<P_BANDS;i++)
  248. for(j=0;j<P_LEVELS;j++){
  249. p->peakatt[i][j]=p->vi->peakatt[i][j];
  250. }
  251. /* set up rolling noise median */
  252. for(i=0;i<n;i++){
  253. float halfoc=toOC((i+.5)*rate/(2.*n))*2.+2.;
  254. int inthalfoc;
  255. float del;
  256. if(halfoc<0)halfoc=0;
  257. if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
  258. inthalfoc=(int)halfoc;
  259. del=halfoc-inthalfoc;
  260. p->noisemedian[i]=
  261. p->vi->noisemedian[inthalfoc*2]*(1.-del) +
  262. p->vi->noisemedian[inthalfoc*2+2]*del;
  263. p->inoisemedian[i]=(int)(65536.0f*p->noisemedian[i]);
  264. p->noiseoffset[i]=
  265. p->vi->noisemedian[inthalfoc*2+1]*(1.-del) +
  266. p->vi->noisemedian[inthalfoc*2+3]*del;
  267. }
  268. {
  269. long i=0,lo=0,hi=0;
  270. float bi;
  271. for(i=0;i<n;i++){
  272. /* find new lo/hi */
  273. bi=p->bark[i]+p->vi->noisewindowhi;
  274. for(;hi<n && (hi<i+p->vi->noisewindowhimin || p->bark[hi]<=bi);hi++) ;
  275. p->barknoisewindowhi[i] = hi;
  276. bi=p->bark[i]-p->vi->noisewindowlo;
  277. for(;lo<i && lo+p->vi->noisewindowlomin<i && p->bark[lo]<=bi;lo++) ;
  278. p->barknoisewindowlo[i] = lo;
  279. }
  280. }
  281. /*_analysis_output("mediancurve",0,p->noisemedian,n,0,0);*/
  282. }
  283. void _vp_psy_clear(vorbis_look_psy *p){
  284. int i,j;
  285. if(p){
  286. if(p->ath)_ogg_free(p->ath);
  287. if(p->octave)_ogg_free(p->octave);
  288. if(p->bark)_ogg_free(p->bark);
  289. if(p->tonecurves){
  290. for(i=0;i<P_BANDS;i++){
  291. for(j=0;j<P_LEVELS;j++){
  292. _ogg_free(p->tonecurves[i][j]);
  293. }
  294. _ogg_free(p->tonecurves[i]);
  295. _ogg_free(p->peakatt[i]);
  296. }
  297. _ogg_free(p->tonecurves);
  298. _ogg_free(p->noisemedian);
  299. _ogg_free(p->inoisemedian);
  300. _ogg_free(p->barknoisewindowlo);
  301. _ogg_free(p->barknoisewindowhi);
  302. _ogg_free(p->noiseoffset);
  303. _ogg_free(p->peakatt);
  304. }
  305. memset(p,0,sizeof(vorbis_look_psy));
  306. }
  307. }
  308. /* octave/(8*eighth_octave_lines) x scale and dB y scale */
  309. static void seed_curve(float *seed,
  310. float **curves,
  311. float amp,
  312. int oc,int n,int linesper,float dBoffset){
  313. int i;
  314. long seedptr;
  315. float *posts,*curve;
  316. int a,b;
  317. int choice=(int)((amp+dBoffset)*.1f);
  318. choice=max(choice,0);
  319. choice=min(choice,P_LEVELS-1);
  320. posts=curves[choice];
  321. a=posts[0];
  322. b=posts[1];
  323. curve=posts+2;
  324. seedptr=oc+(a-16)*linesper-(linesper>>1);
  325. for(i=a;i<b;i++){
  326. if(seedptr>0){
  327. float lin=amp+curve[i];
  328. if(seed[seedptr]<lin)seed[seedptr]=lin;
  329. }
  330. seedptr+=linesper;
  331. if(seedptr>=n)break;
  332. }
  333. }
  334. static void seed_peak(float *seed,
  335. float *att,
  336. float amp,
  337. int oc,
  338. int linesper,
  339. float dBoffset){
  340. long seedptr;
  341. int choice=(int)((amp+dBoffset)*.1f);
  342. choice=max(choice,0);
  343. choice=min(choice,P_LEVELS-1);
  344. seedptr=oc-(linesper>>1);
  345. amp+=att[choice];
  346. if(seed[seedptr]<amp)seed[seedptr]=amp;
  347. }
  348. static void seed_loop(vorbis_look_psy *p,
  349. float ***curves,
  350. float **att,
  351. float *f,
  352. float *flr,
  353. float *minseed,
  354. float *maxseed,
  355. float specmax){
  356. vorbis_info_psy *vi=p->vi;
  357. long n=p->n,i;
  358. float dBoffset=vi->max_curve_dB-specmax;
  359. /* prime the working vector with peak values */
  360. for(i=0;i<n;i++){
  361. float max=f[i];
  362. long oc=p->octave[i];
  363. while(i+1<n && p->octave[i+1]==oc){
  364. i++;
  365. if(f[i]>max)max=f[i];
  366. }
  367. if(max>flr[i]){
  368. oc=oc>>p->shiftoc;
  369. if(oc>=P_BANDS)oc=P_BANDS-1;
  370. if(oc<0)oc=0;
  371. if(vi->tonemaskp)
  372. seed_curve(minseed,
  373. curves[oc],
  374. max,
  375. p->octave[i]-p->firstoc,
  376. p->total_octave_lines,
  377. p->eighth_octave_lines,
  378. dBoffset);
  379. if(vi->peakattp)
  380. seed_peak(maxseed,
  381. att[oc],
  382. max,
  383. p->octave[i]-p->firstoc,
  384. p->eighth_octave_lines,
  385. dBoffset);
  386. }
  387. }
  388. }
  389. static void bound_loop(vorbis_look_psy *p,
  390. float *f,
  391. float *seeds,
  392. float *flr,
  393. float att){
  394. long n=p->n,i;
  395. long off=(p->eighth_octave_lines>>1)+p->firstoc;
  396. long *ocp=p->octave;
  397. for(i=0;i<n;i++){
  398. long oc=ocp[i]-off;
  399. float v=f[i]+att;
  400. if(seeds[oc]<v)seeds[oc]=v;
  401. }
  402. }
  403. static void seed_chase(float *seeds, int linesper, long n){
  404. long *posstack=alloca(n*sizeof(long));
  405. float *ampstack=alloca(n*sizeof(float));
  406. long stack=0;
  407. long pos=0;
  408. long i;
  409. for(i=0;i<n;i++){
  410. if(stack<2){
  411. posstack[stack]=i;
  412. ampstack[stack++]=seeds[i];
  413. }else{
  414. while(1){
  415. if(seeds[i]<ampstack[stack-1]){
  416. posstack[stack]=i;
  417. ampstack[stack++]=seeds[i];
  418. break;
  419. }else{
  420. if(i<posstack[stack-1]+linesper){
  421. if(stack>1 && ampstack[stack-1]<=ampstack[stack-2] &&
  422. i<posstack[stack-2]+linesper){
  423. /* we completely overlap, making stack-1 irrelevant. pop it */
  424. stack--;
  425. continue;
  426. }
  427. }
  428. posstack[stack]=i;
  429. ampstack[stack++]=seeds[i];
  430. break;
  431. }
  432. }
  433. }
  434. }
  435. /* the stack now contains only the positions that are relevant. Scan
  436. 'em straight through */
  437. for(i=0;i<stack;i++){
  438. long endpos;
  439. if(i<stack-1 && ampstack[i+1]>ampstack[i]){
  440. endpos=posstack[i+1];
  441. }else{
  442. endpos=posstack[i]+linesper+1; /* +1 is important, else bin 0 is
  443. discarded in short frames */
  444. }
  445. if(endpos>n)endpos=n;
  446. for(;pos<endpos;pos++)
  447. seeds[pos]=ampstack[i];
  448. }
  449. /* there. Linear time. I now remember this was on a problem set I
  450. had in Grad Skool... I didn't solve it at the time ;-) */
  451. }
  452. /* bleaugh, this is more complicated than it needs to be */
  453. static void max_seeds(vorbis_look_psy *p,float *minseed,float *maxseed,
  454. float *flr){
  455. long n=p->total_octave_lines;
  456. int linesper=p->eighth_octave_lines;
  457. long linpos=0;
  458. long pos;
  459. seed_chase(minseed,linesper,n); /* for masking */
  460. seed_chase(maxseed,linesper,n); /* for peak att */
  461. pos=p->octave[0]-p->firstoc-(linesper>>1);
  462. while(linpos+1<p->n){
  463. float min=minseed[pos];
  464. float max=maxseed[pos];
  465. long end=((p->octave[linpos]+p->octave[linpos+1])>>1)-p->firstoc;
  466. while(pos+1<=end){
  467. pos++;
  468. if((minseed[pos]>NEGINF && minseed[pos]<min) || min==NEGINF)
  469. min=minseed[pos];
  470. if(maxseed[pos]>max)max=maxseed[pos];
  471. }
  472. if(max<min)max=min;
  473. /* seed scale is log. Floor is linear. Map back to it */
  474. end=pos+p->firstoc;
  475. for(;linpos<p->n && p->octave[linpos]<=end;linpos++)
  476. if(flr[linpos]<max)flr[linpos]=max;
  477. }
  478. {
  479. float min=minseed[p->total_octave_lines-1];
  480. float max=maxseed[p->total_octave_lines-1];
  481. if(max<min)max=min;
  482. for(;linpos<p->n;linpos++)
  483. if(flr[linpos]<max)flr[linpos]=max;
  484. }
  485. }
  486. /* quarter-dB bins */
  487. #define BIN(x) ((int)((x)*negFour))
  488. #define BINdB(x) ((x)*negQuarter)
  489. #define BINCOUNT (200*4)
  490. #define LASTBIN (BINCOUNT-1)
  491. static void old_bark_noise_median(long n,float *b,float *f,float *noise,
  492. float lowidth,float hiwidth,
  493. int lomin,int himin,
  494. float *thresh,float *off){
  495. long i=0,lo=0,hi=0;
  496. float bi,threshi;
  497. long median=LASTBIN;
  498. float negFour = -4.0f;
  499. float negQuarter = -0.25f;
  500. /* these are really integral values, but we store them in floats to
  501. avoid excessive float/int conversions, which GCC and MSVC are
  502. farily poor at optimizing. */
  503. float radix[BINCOUNT];
  504. float countabove=0;
  505. float countbelow=0;
  506. memset(radix,0,sizeof(radix));
  507. for(i=0;i<n;i++){
  508. /* find new lo/hi */
  509. bi=b[i]+hiwidth;
  510. for(;hi<n && (hi<i+himin || b[hi]<=bi);hi++){
  511. int bin=BIN(f[hi]);
  512. if(bin>LASTBIN)bin=LASTBIN;
  513. if(bin<0)bin=0;
  514. radix[bin]++;
  515. if(bin<median)
  516. countabove++;
  517. else
  518. countbelow++;
  519. }
  520. //fprintf(stderr, "%d: %d, ", i, hi);
  521. bi=b[i]-lowidth;
  522. for(;lo<i && lo+lomin<i && b[lo]<=bi;lo++){
  523. int bin=BIN(f[lo]);
  524. if(bin>LASTBIN)bin=LASTBIN;
  525. if(bin<0)bin=0;
  526. radix[bin]--;
  527. if(bin<median)
  528. countabove--;
  529. else
  530. countbelow--;
  531. }
  532. //fprintf(stderr, "%d\n", lo);
  533. /* move the median if needed */
  534. // if(countabove+countbelow){
  535. threshi = thresh[i]*(countabove+countbelow);
  536. while(threshi>countbelow && median>0){
  537. median--;
  538. countabove-=radix[median];
  539. countbelow+=radix[median];
  540. }
  541. while(threshi<(countbelow-radix[median]) &&
  542. median<LASTBIN){
  543. countabove+=radix[median];
  544. countbelow-=radix[median];
  545. median++;
  546. }
  547. // }
  548. noise[i]=BINdB(median)+off[i];
  549. }
  550. //exit(1);
  551. }
  552. static void bark_noise_median(long n,float *b,float *f,float *noise,
  553. float lowidth,float hiwidth,
  554. int lomin,int himin,
  555. float *thresh,int *ithresh,int *bhi, int *blo, float *off){
  556. long i=0,lo=0,hi=0;
  557. // float bi;
  558. int threshi;
  559. long median=LASTBIN;
  560. float negFour = -4.0f;
  561. float negQuarter = -0.25f;
  562. int radix[BINCOUNT];
  563. int countabove=0;
  564. int countbelow=0;
  565. //old_bark_noise_median(n,b,f,noise,lowidth,hiwidth,lomin,himin,thresh,off);return;
  566. memset(radix,0,sizeof(radix));
  567. for(i=0;i<n;i++){
  568. /* find new lo/hi */
  569. // bi=b[i]+hiwidth;
  570. // for(;hi<n && (hi<i+himin || b[hi]<=bi);hi++){
  571. for(;hi<bhi[i];hi++){
  572. int bin=BIN(f[hi]);
  573. if(bin>LASTBIN)bin=LASTBIN;
  574. if(bin<0)bin=0;
  575. radix[bin]++;
  576. //countabove += (bin < median);
  577. //countbelow += (bin >= median);
  578. if(bin<median)
  579. countabove++;
  580. else
  581. countbelow++;
  582. }
  583. //fprintf(stderr, "%d: %d, ", i, hi);
  584. // bi=b[i]-lowidth;
  585. // for(;lo<i && lo+lomin<i && b[lo]<=bi;lo++){
  586. for(;lo<blo[i];lo++){
  587. int bin=BIN(f[lo]);
  588. if(bin>LASTBIN)bin=LASTBIN;
  589. if(bin<0)bin=0;
  590. radix[bin]--;
  591. //countabove -= (bin < median);
  592. //countbelow -= (bin >= median);
  593. if(bin<median)
  594. countabove--;
  595. else
  596. countbelow--;
  597. }
  598. //fprintf(stderr, "%d\n", lo);
  599. /* move the median if needed */
  600. // threshi = thresh[i]*(countabove+countbelow);
  601. // threshi = (ithresh[i]*(countabove+countbelow)+32768)>>16;
  602. threshi = (ithresh[i]*(countabove+countbelow))>>16;
  603. //!fprintf(stderr, "%f %f\n", (float)threshi, thresh[i]*(countabove+countbelow));
  604. //if(n==1024)fprintf(stderr, "%f\n", thresh[i]);
  605. while(threshi>countbelow && median>0){
  606. median--;
  607. countabove-=radix[median];
  608. countbelow+=radix[median];
  609. }
  610. while(threshi<(countbelow-radix[median]) &&
  611. median<LASTBIN){
  612. countabove+=radix[median];
  613. countbelow-=radix[median];
  614. median++;
  615. }
  616. noise[i]=BINdB(median)+off[i];
  617. }
  618. //if(n==1024)exit(1);
  619. //!exit(1);
  620. }
  621. float _vp_compute_mask(vorbis_look_psy *p,
  622. float *fft,
  623. float *mdct,
  624. float *flr,
  625. float *decay,
  626. float specmax){
  627. int i,n=p->n;
  628. float localmax=NEGINF;
  629. static int seq=0;
  630. float *minseed=alloca(sizeof(float)*p->total_octave_lines);
  631. float *maxseed=alloca(sizeof(float)*p->total_octave_lines);
  632. for(i=0;i<p->total_octave_lines;i++)minseed[i]=maxseed[i]=NEGINF;
  633. /* go to dB scale. Also find the highest peak so we know the limits */
  634. for(i=0;i<n;i++){
  635. fft[i]=todB_t(fft[i]);
  636. if(fft[i]>localmax)localmax=fft[i];
  637. }
  638. if(specmax<localmax)specmax=localmax;
  639. for(i=0;i<n;i++){
  640. mdct[i]=todB_t(mdct[i]);
  641. }
  642. _analysis_output("mdct",seq,mdct,n,0,0);
  643. _analysis_output("fft",seq,fft,n,0,0);
  644. /* noise masking */
  645. if(p->vi->noisemaskp){
  646. bark_noise_median(n,p->bark,mdct,flr,
  647. p->vi->noisewindowlo,
  648. p->vi->noisewindowhi,
  649. p->vi->noisewindowlomin,
  650. p->vi->noisewindowhimin,
  651. p->noisemedian,
  652. p->inoisemedian,
  653. p->barknoisewindowhi,
  654. p->barknoisewindowlo,
  655. p->noiseoffset);
  656. /* suppress any noise curve > specmax+p->vi->noisemaxsupp */
  657. for(i=0;i<n;i++)
  658. if(flr[i]>specmax+p->vi->noisemaxsupp)
  659. flr[i]=specmax+p->vi->noisemaxsupp;
  660. _analysis_output("noise",seq,flr,n,0,0);
  661. }else{
  662. for(i=0;i<n;i++)flr[i]=NEGINF;
  663. }
  664. /* set the ATH (floating below localmax, not global max by a
  665. specified att) */
  666. if(p->vi->athp){
  667. float att=localmax+p->vi->ath_adjatt;
  668. if(att<p->vi->ath_maxatt)att=p->vi->ath_maxatt;
  669. for(i=0;i<n;i++){
  670. float av=p->ath[i]+att;
  671. if(av>flr[i])flr[i]=av;
  672. }
  673. }
  674. _analysis_output("ath",seq,flr,n,0,0);
  675. /* tone/peak masking */
  676. /* XXX apply decay to the fft here */
  677. seed_loop(p,p->tonecurves,p->peakatt,fft,flr,minseed,maxseed,specmax);
  678. bound_loop(p,mdct,maxseed,flr,p->vi->bound_att_dB);
  679. _analysis_output("minseed",seq,minseed,p->total_octave_lines,0,0);
  680. _analysis_output("maxseed",seq,maxseed,p->total_octave_lines,0,0);
  681. max_seeds(p,minseed,maxseed,flr);
  682. _analysis_output("final",seq,flr,n,0,0);
  683. /* doing this here is clean, but we need to find a faster way to do
  684. it than to just tack it on */
  685. for(i=0;i<n;i++)if(mdct[i]>=flr[i])break;
  686. if(i==n)for(i=0;i<n;i++)flr[i]=NEGINF;
  687. seq++;
  688. return(specmax);
  689. }
  690. /* this applies the floor and (optionally) tries to preserve noise
  691. energy in low resolution portions of the spectrum */
  692. /* f and flr are *linear* scale, not dB */
  693. void _vp_apply_floor(vorbis_look_psy *p,float *f, float *flr){
  694. float *work=alloca(p->n*sizeof(float));
  695. int j;
  696. /* subtract the floor */
  697. for(j=0;j<p->n;j++){
  698. if(flr[j]<=0)
  699. work[j]=0.f;
  700. else
  701. work[j]=f[j]/flr[j];
  702. }
  703. memcpy(f,work,p->n*sizeof(float));
  704. }
  705. float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd){
  706. vorbis_info *vi=vd->vi;
  707. codec_setup_info *ci=vi->codec_setup;
  708. int n=ci->blocksizes[vd->W]/2;
  709. float secs=(float)n/vi->rate;
  710. amp+=secs*ci->ampmax_att_per_sec;
  711. if(amp<-9999)amp=-9999;
  712. return(amp);
  713. }