psy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
  6. * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and the XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: psychoacoustics not including preecho
  14. last mod: $Id: psy.c,v 1.29.2.3 2000/11/04 06:43:50 xiphmont Exp $
  15. ********************************************************************/
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include <string.h>
  19. #include "vorbis/codec.h"
  20. #include "masking.h"
  21. #include "psy.h"
  22. #include "os.h"
  23. #include "lpc.h"
  24. #include "smallft.h"
  25. #include "scales.h"
  26. #include "misc.h"
  27. /* Why Bark scale for encoding but not masking computation? Because
  28. masking has a strong harmonic dependancy */
  29. /* the beginnings of real psychoacoustic infrastructure. This is
  30. still not tightly tuned */
  31. void _vi_psy_free(vorbis_info_psy *i){
  32. if(i){
  33. memset(i,0,sizeof(vorbis_info_psy));
  34. free(i);
  35. }
  36. }
  37. vorbis_info_psy *_vi_psy_copy(vorbis_info_psy *i){
  38. vorbis_info_psy *ret=_ogg_malloc(sizeof(vorbis_info_psy));
  39. memcpy(ret,i,sizeof(vorbis_info_psy));
  40. return(ret);
  41. }
  42. /* Set up decibel threshhold slopes on a Bark frequency scale */
  43. /* ATH is the only bit left on a Bark scale. No reason to change it
  44. right now */
  45. static void set_curve(float *ref,float *c,int n, float crate){
  46. int i,j=0;
  47. for(i=0;i<MAX_BARK-1;i++){
  48. int endpos=rint(fromBARK(i+1)*2*n/crate);
  49. float base=ref[i];
  50. if(j<endpos){
  51. float delta=(ref[i+1]-base)/(endpos-j);
  52. for(;j<endpos && j<n;j++){
  53. c[j]=base;
  54. base+=delta;
  55. }
  56. }
  57. }
  58. }
  59. static void min_curve(float *c,
  60. float *c2){
  61. int i;
  62. for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
  63. }
  64. static void max_curve(float *c,
  65. float *c2){
  66. int i;
  67. for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
  68. }
  69. static void attenuate_curve(float *c,float att){
  70. int i;
  71. for(i=0;i<EHMER_MAX;i++)
  72. c[i]+=att;
  73. }
  74. static void linear_curve(float *c){
  75. int i;
  76. for(i=0;i<EHMER_MAX;i++)
  77. if(c[i]<=-200.)
  78. c[i]=0.;
  79. else
  80. c[i]=fromdB(c[i]);
  81. }
  82. static void interp_curve(float *c,float *c1,float *c2,float del){
  83. int i;
  84. for(i=0;i<EHMER_MAX;i++)
  85. c[i]=c2[i]*del+c1[i]*(1.-del);
  86. }
  87. static void setup_curve(float **c,
  88. int band,
  89. float *curveatt_dB){
  90. int i,j;
  91. float ath[EHMER_MAX];
  92. float tempc[P_LEVELS][EHMER_MAX];
  93. memcpy(c[0],c[4],sizeof(float)*EHMER_MAX);
  94. memcpy(c[2],c[4],sizeof(float)*EHMER_MAX);
  95. /* we add back in the ATH to avoid low level curves falling off to
  96. -infinity and unneccessarily cutting off high level curves in the
  97. curve limiting (last step). But again, remember... a half-band's
  98. settings must be valid over the whole band, and it's better to
  99. mask too little than too much, so be pessimal. */
  100. for(i=0;i<EHMER_MAX;i++){
  101. float oc_min=band*.5-1+(i-EHMER_OFFSET)*.125;
  102. float oc_max=band*.5-1+(i-EHMER_OFFSET+1)*.125;
  103. float bark=toBARK(fromOC(oc_min));
  104. int ibark=floor(bark);
  105. float del=bark-ibark;
  106. float ath_min,ath_max;
  107. if(ibark<26)
  108. ath_min=ATH_Bark_dB[ibark]*(1.-del)+ATH_Bark_dB[ibark+1]*del;
  109. else
  110. ath_min=200.;
  111. bark=toBARK(fromOC(oc_max));
  112. ibark=floor(bark);
  113. del=bark-ibark;
  114. if(ibark<26)
  115. ath_max=ATH_Bark_dB[ibark]*(1.-del)+ATH_Bark_dB[ibark+1]*del;
  116. else
  117. ath_max=200.;
  118. ath[i]=min(ath_min,ath_max);
  119. }
  120. /* The c array is comes in as dB curves at 20 40 60 80 100 dB.
  121. interpolate intermediate dB curves */
  122. for(i=1;i<P_LEVELS;i+=2){
  123. interp_curve(c[i],c[i-1],c[i+1],.5);
  124. }
  125. /* normalize curves so the driving amplitude is 0dB */
  126. /* make temp curves with the ATH overlayed */
  127. for(i=0;i<P_LEVELS;i++){
  128. attenuate_curve(c[i],curveatt_dB[i]);
  129. memcpy(tempc[i],ath,EHMER_MAX*sizeof(float));
  130. attenuate_curve(tempc[i],-i*10.);
  131. max_curve(tempc[i],c[i]);
  132. }
  133. /* Now limit the louder curves.
  134. the idea is this: We don't know what the playback attenuation
  135. will be; 0dB SL moves every time the user twiddles the volume
  136. knob. So that means we have to use a single 'most pessimal' curve
  137. for all masking amplitudes, right? Wrong. The *loudest* sound
  138. can be in (we assume) a range of ...+100dB] SL. However, sounds
  139. 20dB down will be in a range ...+80], 40dB down is from ...+60],
  140. etc... */
  141. for(i=P_LEVELS-1;i>0;i--){
  142. for(j=0;j<i;j++)
  143. min_curve(c[i],tempc[j]);
  144. }
  145. /* take things out of dB domain into linear amplitude */
  146. for(i=0;i<P_LEVELS;i++)
  147. linear_curve(c[i]);
  148. }
  149. void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,int n,long rate){
  150. long i,j;
  151. memset(p,0,sizeof(vorbis_look_psy));
  152. p->ath=_ogg_malloc(n*sizeof(float));
  153. p->octave=_ogg_malloc(n*sizeof(int));
  154. p->bark=_ogg_malloc(n*sizeof(float));
  155. p->vi=vi;
  156. p->n=n;
  157. /* set up the lookups for a given blocksize and sample rate */
  158. /* Vorbis max sample rate is limited by 26 Bark (54kHz) */
  159. set_curve(ATH_Bark_dB, p->ath,n,rate);
  160. for(i=0;i<n;i++)
  161. p->ath[i]=fromdB(p->ath[i]);
  162. for(i=0;i<n;i++)
  163. p->bark[i]=toBARK(rate/(2*n)*i);
  164. for(i=0;i<n;i++){
  165. int oc=toOC((i+.5)*rate/(2*n))*2.+2; /* half octaves, actually */
  166. if(oc<0)oc=0;
  167. if(oc>=P_BANDS)oc=P_BANDS-1;
  168. p->octave[i]=oc;
  169. }
  170. p->tonecurves=_ogg_malloc(P_BANDS*sizeof(float **));
  171. p->noiseatt=_ogg_malloc(P_BANDS*sizeof(float **));
  172. p->peakatt=_ogg_malloc(P_BANDS*sizeof(float *));
  173. for(i=0;i<P_BANDS;i++){
  174. p->tonecurves[i]=_ogg_malloc(P_LEVELS*sizeof(float *));
  175. p->noiseatt[i]=_ogg_malloc(P_LEVELS*sizeof(float));
  176. p->peakatt[i]=_ogg_malloc(P_LEVELS*sizeof(float));
  177. }
  178. for(i=0;i<P_BANDS;i++)
  179. for(j=0;j<P_LEVELS;j++){
  180. p->tonecurves[i][j]=_ogg_malloc(EHMER_MAX*sizeof(float));
  181. }
  182. /* OK, yeah, this was a silly way to do it */
  183. memcpy(p->tonecurves[0][4],tone_125_40dB_SL,sizeof(float)*EHMER_MAX);
  184. memcpy(p->tonecurves[0][6],tone_125_60dB_SL,sizeof(float)*EHMER_MAX);
  185. memcpy(p->tonecurves[0][8],tone_125_80dB_SL,sizeof(float)*EHMER_MAX);
  186. memcpy(p->tonecurves[0][10],tone_125_100dB_SL,sizeof(float)*EHMER_MAX);
  187. memcpy(p->tonecurves[2][4],tone_125_40dB_SL,sizeof(float)*EHMER_MAX);
  188. memcpy(p->tonecurves[2][6],tone_125_60dB_SL,sizeof(float)*EHMER_MAX);
  189. memcpy(p->tonecurves[2][8],tone_125_80dB_SL,sizeof(float)*EHMER_MAX);
  190. memcpy(p->tonecurves[2][10],tone_125_100dB_SL,sizeof(float)*EHMER_MAX);
  191. memcpy(p->tonecurves[4][4],tone_250_40dB_SL,sizeof(float)*EHMER_MAX);
  192. memcpy(p->tonecurves[4][6],tone_250_60dB_SL,sizeof(float)*EHMER_MAX);
  193. memcpy(p->tonecurves[4][8],tone_250_80dB_SL,sizeof(float)*EHMER_MAX);
  194. memcpy(p->tonecurves[4][10],tone_250_100dB_SL,sizeof(float)*EHMER_MAX);
  195. memcpy(p->tonecurves[6][4],tone_500_40dB_SL,sizeof(float)*EHMER_MAX);
  196. memcpy(p->tonecurves[6][6],tone_500_60dB_SL,sizeof(float)*EHMER_MAX);
  197. memcpy(p->tonecurves[6][8],tone_500_80dB_SL,sizeof(float)*EHMER_MAX);
  198. memcpy(p->tonecurves[6][10],tone_500_100dB_SL,sizeof(float)*EHMER_MAX);
  199. memcpy(p->tonecurves[8][4],tone_1000_40dB_SL,sizeof(float)*EHMER_MAX);
  200. memcpy(p->tonecurves[8][6],tone_1000_60dB_SL,sizeof(float)*EHMER_MAX);
  201. memcpy(p->tonecurves[8][8],tone_1000_80dB_SL,sizeof(float)*EHMER_MAX);
  202. memcpy(p->tonecurves[8][10],tone_1000_100dB_SL,sizeof(float)*EHMER_MAX);
  203. memcpy(p->tonecurves[10][4],tone_2000_40dB_SL,sizeof(float)*EHMER_MAX);
  204. memcpy(p->tonecurves[10][6],tone_2000_60dB_SL,sizeof(float)*EHMER_MAX);
  205. memcpy(p->tonecurves[10][8],tone_2000_80dB_SL,sizeof(float)*EHMER_MAX);
  206. memcpy(p->tonecurves[10][10],tone_2000_100dB_SL,sizeof(float)*EHMER_MAX);
  207. memcpy(p->tonecurves[12][4],tone_4000_40dB_SL,sizeof(float)*EHMER_MAX);
  208. memcpy(p->tonecurves[12][6],tone_4000_60dB_SL,sizeof(float)*EHMER_MAX);
  209. memcpy(p->tonecurves[12][8],tone_4000_80dB_SL,sizeof(float)*EHMER_MAX);
  210. memcpy(p->tonecurves[12][10],tone_4000_100dB_SL,sizeof(float)*EHMER_MAX);
  211. memcpy(p->tonecurves[14][4],tone_8000_40dB_SL,sizeof(float)*EHMER_MAX);
  212. memcpy(p->tonecurves[14][6],tone_8000_60dB_SL,sizeof(float)*EHMER_MAX);
  213. memcpy(p->tonecurves[14][8],tone_8000_80dB_SL,sizeof(float)*EHMER_MAX);
  214. memcpy(p->tonecurves[14][10],tone_8000_100dB_SL,sizeof(float)*EHMER_MAX);
  215. memcpy(p->tonecurves[16][4],tone_8000_40dB_SL,sizeof(float)*EHMER_MAX);
  216. memcpy(p->tonecurves[16][6],tone_8000_60dB_SL,sizeof(float)*EHMER_MAX);
  217. memcpy(p->tonecurves[16][8],tone_8000_80dB_SL,sizeof(float)*EHMER_MAX);
  218. memcpy(p->tonecurves[16][10],tone_8000_100dB_SL,sizeof(float)*EHMER_MAX);
  219. /* interpolate curves between */
  220. for(i=1;i<P_BANDS;i+=2)
  221. for(j=4;j<P_LEVELS;j+=2){
  222. memcpy(p->tonecurves[i][j],p->tonecurves[i-1][j],EHMER_MAX*sizeof(float));
  223. /*interp_curve(p->tonecurves[i][j],
  224. p->tonecurves[i-1][j],
  225. p->tonecurves[i+1][j],.5);*/
  226. min_curve(p->tonecurves[i][j],p->tonecurves[i+1][j]);
  227. /*min_curve(p->tonecurves[i][j],p->tonecurves[i-1][j]);*/
  228. }
  229. /*for(i=0;i<P_BANDS-1;i++)
  230. for(j=4;j<P_LEVELS;j+=2)
  231. min_curve(p->tonecurves[i][j],p->tonecurves[i+1][j]);*/
  232. /* set up the final curves */
  233. for(i=0;i<P_BANDS;i++)
  234. setup_curve(p->tonecurves[i],i,vi->toneatt[i]);
  235. /* set up attenuation levels */
  236. for(i=0;i<P_BANDS;i++)
  237. for(j=0;j<P_LEVELS;j++){
  238. p->peakatt[i][j]=fromdB(p->vi->peakatt[i][j]);
  239. p->noiseatt[i][j]=fromdB(p->vi->noiseatt[i][j]);
  240. }
  241. }
  242. void _vp_psy_clear(vorbis_look_psy *p){
  243. int i,j;
  244. if(p){
  245. if(p->ath)free(p->ath);
  246. if(p->octave)free(p->octave);
  247. if(p->tonecurves){
  248. for(i=0;i<P_BANDS;i++){
  249. for(j=0;j<P_LEVELS;j++){
  250. free(p->tonecurves[i][j]);
  251. }
  252. free(p->noiseatt[i]);
  253. free(p->tonecurves[i]);
  254. free(p->peakatt[i]);
  255. }
  256. free(p->tonecurves);
  257. free(p->noiseatt);
  258. free(p->peakatt);
  259. }
  260. memset(p,0,sizeof(vorbis_look_psy));
  261. }
  262. }
  263. static void compute_decay_fixed(vorbis_look_psy *p,float *f, float *decay, int n){
  264. /* handle decay */
  265. int i;
  266. float decscale=fromdB(p->vi->decay_coeff*n);
  267. float attscale=1./fromdB(p->vi->attack_coeff);
  268. for(i=10;i<n;i++){
  269. float pre=decay[i];
  270. if(decay[i]){
  271. float val=decay[i]*decscale;
  272. float att=fabs(f[i]/val);
  273. if(att>attscale)
  274. decay[i]=fabs(f[i]/attscale);
  275. else
  276. decay[i]=val;
  277. }else{
  278. decay[i]=fabs(f[i]/attscale);
  279. }
  280. if(pre>f[i])f[i]=pre;
  281. }
  282. }
  283. static long _eights[EHMER_MAX+1]={
  284. 981,1069,1166,1272,
  285. 1387,1512,1649,1798,
  286. 1961,2139,2332,2543,
  287. 2774,3025,3298,3597,
  288. 3922,4277,4664,5087,
  289. 5547,6049,6597,7194,
  290. 7845,8555,9329,10173,
  291. 11094,12098,13193,14387,
  292. 15689,17109,18658,20347,
  293. 22188,24196,26386,28774,
  294. 31379,34219,37316,40693,
  295. 44376,48393,52772,57549,
  296. 62757,68437,74631,81386,
  297. 88752,96785,105545,115097,
  298. 125515};
  299. static int seed_curve(float *flr,
  300. float **curves,
  301. float amp,float specmax,
  302. int x,int n,float specatt,
  303. int maxEH){
  304. int i;
  305. float *curve;
  306. /* make this attenuation adjustable */
  307. int choice=(int)((todB(amp)-specmax+specatt)/10.+.5);
  308. choice=max(choice,0);
  309. choice=min(choice,P_LEVELS-1);
  310. for(i=maxEH;i>=0;i--)
  311. if(((x*_eights[i])>>12)<n)break;
  312. maxEH=i;
  313. curve=curves[choice];
  314. for(;i>=0;i--)
  315. if(curve[i]>0.)break;
  316. for(;i>=0;i--){
  317. float lin=curve[i];
  318. if(lin>0.){
  319. float *fp=flr+((x*_eights[i])>>12);
  320. lin*=amp;
  321. if(*fp<lin)*fp=lin;
  322. }else break;
  323. }
  324. return(maxEH);
  325. }
  326. static void seed_peak(float *flr,
  327. float *att,
  328. float amp,float specmax,
  329. int x,int n,float specatt){
  330. int prevx=(x*_eights[16])>>12;
  331. /* make this attenuation adjustable */
  332. int choice=rint((todB(amp)-specmax+specatt)/10.+.5);
  333. if(choice<0)choice=0;
  334. if(choice>=P_LEVELS)choice=P_LEVELS-1;
  335. if(prevx<n){
  336. float lin=att[choice];
  337. if(lin){
  338. lin*=amp;
  339. if(flr[prevx]<lin)flr[prevx]=lin;
  340. }
  341. }
  342. }
  343. static void seed_generic(vorbis_look_psy *p,
  344. float ***curves,
  345. float *f,
  346. float *flr,
  347. float *seeds,
  348. float specmax){
  349. vorbis_info_psy *vi=p->vi;
  350. long n=p->n,i;
  351. int maxEH=EHMER_MAX-1;
  352. /* prime the working vector with peak values */
  353. /* Use the 125 Hz curve up to 125 Hz and 8kHz curve after 8kHz. */
  354. for(i=0;i<n;i++)
  355. if(f[i]>flr[i])
  356. maxEH=seed_curve(seeds,curves[p->octave[i]],
  357. f[i],specmax,i,n,vi->max_curve_dB,maxEH);
  358. }
  359. static void seed_att(vorbis_look_psy *p,
  360. float **att,
  361. float *f,
  362. float *flr,
  363. float specmax){
  364. vorbis_info_psy *vi=p->vi;
  365. long n=p->n,i;
  366. for(i=0;i<n;i++)
  367. if(f[i]>flr[i])
  368. seed_peak(flr,att[p->octave[i]],f[i],
  369. specmax,i,n,vi->max_curve_dB);
  370. }
  371. static void seed_point(vorbis_look_psy *p,
  372. float **att,
  373. float *f,
  374. float *flr,
  375. float specmax){
  376. vorbis_info_psy *vi=p->vi;
  377. long n=p->n,i;
  378. for(i=0;i<n;i++){
  379. /* make this attenuation adjustable */
  380. int choice=rint((todB(f[i])-specmax+vi->max_curve_dB)/10.+.5);
  381. float lin;
  382. if(choice<0)choice=0;
  383. if(choice>=P_LEVELS)choice=P_LEVELS-1;
  384. lin=att[p->octave[i]][choice]*f[i];
  385. if(flr[i]<lin)flr[i]=lin;
  386. }
  387. }
  388. /* bleaugh, this is more complicated than it needs to be */
  389. static void max_seeds(vorbis_look_psy *p,float *seeds,float *flr){
  390. long n=p->n,i,j;
  391. long *posstack=alloca(n*sizeof(long));
  392. float *ampstack=alloca(n*sizeof(float));
  393. long stack=0;
  394. for(i=0;i<n;i++){
  395. if(stack<2){
  396. posstack[stack]=i;
  397. ampstack[stack++]=seeds[i];
  398. }else{
  399. while(1){
  400. if(seeds[i]<ampstack[stack-1]){
  401. posstack[stack]=i;
  402. ampstack[stack++]=seeds[i];
  403. break;
  404. }else{
  405. if(i<posstack[stack-1]*1.0905077080){
  406. if(stack>1 && ampstack[stack-1]<ampstack[stack-2] &&
  407. i<posstack[stack-2]*1.0905077080){
  408. /* we completely overlap, making stack-1 irrelevant. pop it */
  409. stack--;
  410. continue;
  411. }
  412. }
  413. posstack[stack]=i;
  414. ampstack[stack++]=seeds[i];
  415. break;
  416. }
  417. }
  418. }
  419. }
  420. /* the stack now contains only the positions that are relevant. Scan
  421. 'em straight through */
  422. {
  423. long pos=0;
  424. for(i=0;i<stack;i++){
  425. long endpos;
  426. if(i<stack-1 && ampstack[i+1]>ampstack[i]){
  427. endpos=posstack[i+1];
  428. }else{
  429. endpos=posstack[i]*1.0905077080+1; /* +1 is important, else bin 0 is
  430. discarded in short frames */
  431. }
  432. if(endpos>n)endpos=n;
  433. for(j=pos;j<endpos;j++)
  434. if(flr[j]<ampstack[i])
  435. flr[j]=ampstack[i];
  436. pos=endpos;
  437. }
  438. }
  439. /* there. Linear time. I now remember this was on a problem set I
  440. had in Grad Skool... I didn't solve it at the time ;-) */
  441. }
  442. static void bark_noise(long n,float *b,float *f,float *noise){
  443. long i=1,lo=0,hi=2;
  444. float acc=0.,val,del=0.;
  445. float *norm=alloca(n*sizeof(float));
  446. memset(noise,0,n*sizeof(float));
  447. memset(norm,0,n*sizeof(float));
  448. while(hi<n){
  449. val=todB_nn(f[i]*f[i])+400.;
  450. del=1./(i-lo);
  451. noise[lo]+=val*del;
  452. noise[i]-=val*del;
  453. norm[lo]+=del;
  454. norm[i]-=del;
  455. del=1./(hi-i);
  456. noise[i]-=val*del;
  457. noise[hi]+=val*del;
  458. norm[hi]+=del;
  459. norm[i]-=del;
  460. i++;
  461. for(;hi<n && b[hi]-.3<b[i];hi++);
  462. for(;lo<i-1 && b[lo]+.3<b[i];lo++);
  463. if(i==hi)hi++;
  464. }
  465. {
  466. long ilo=i-lo;
  467. long hii=hi-i;
  468. for(;i<n;i++){
  469. val=todB_nn(f[i]*f[i])+400.;
  470. del=1./(hii);
  471. noise[i]-=val*del;
  472. norm[i]-=del;
  473. del=1./(ilo);
  474. noise[i-ilo]+=val*del;
  475. noise[i]-=val*del;
  476. norm[i-ilo]+=del;
  477. norm[i]-=del;
  478. }
  479. for(i=1,lo=n-ilo;lo<n;lo++,i++){
  480. val=todB_nn(f[n-i]*f[n-i])+400.;
  481. del=1./ilo;
  482. noise[lo]+=val*del;
  483. norm[lo]+=del;
  484. }
  485. }
  486. acc=0;
  487. val=0;
  488. for(i=0;i<n;i++){
  489. val+=norm[i];
  490. norm[i]=val;
  491. acc+=noise[i];
  492. noise[i]=acc;
  493. }
  494. val=0;
  495. acc=0;
  496. for(i=0;i<n;i++){
  497. val+=norm[i];
  498. acc+=noise[i];
  499. if(val==0){
  500. noise[i]=0.;
  501. norm[i]=0;
  502. }else{
  503. float v=acc/val-400;
  504. noise[i]=sqrt(fromdB(v));
  505. }
  506. }
  507. }
  508. void _vp_compute_mask(vorbis_look_psy *p,float *f,
  509. float *flr,
  510. float *decay){
  511. float *smooth=alloca(sizeof(float)*p->n);
  512. int i,n=p->n;
  513. float specmax=0.;
  514. float *seed=alloca(sizeof(float)*p->n);
  515. float *seed2=alloca(sizeof(float)*p->n);
  516. memset(flr,0,n*sizeof(float));
  517. /* noise masking */
  518. if(p->vi->noisemaskp){
  519. memset(seed,0,n*sizeof(float));
  520. bark_noise(n,p->bark,f,seed);
  521. seed_point(p,p->noiseatt,seed,flr,specmax);
  522. }
  523. /* smooth the data is that's called for ********************************/
  524. for(i=0;i<n;i++)smooth[i]=fabs(f[i]);
  525. if(p->vi->smoothp){
  526. /* compute power^.5 of three neighboring bins to smooth for peaks
  527. that get split twixt bins/peaks that nail the bin. This evens
  528. out treatment as we're not doing additive masking any longer. */
  529. float acc=smooth[0]*smooth[0]+smooth[1]*smooth[1];
  530. float prev=smooth[0];
  531. smooth[0]=sqrt(acc);
  532. for(i=1;i<n-1;i++){
  533. float this=smooth[i];
  534. acc+=smooth[i+1]*smooth[i+1];
  535. if(acc<0)acc=0; /* it can happen due to finite precision */
  536. smooth[i]=sqrt(acc);
  537. acc-=prev*prev;
  538. prev=this;
  539. }
  540. if(acc<0)acc=0; /* in case it happens on the final iteration */
  541. smooth[n-1]=sqrt(acc);
  542. }
  543. /* find the highest peak so we know the limits *************************/
  544. for(i=0;i<n;i++){
  545. if(smooth[i]>specmax)specmax=smooth[i];
  546. }
  547. specmax=todB(specmax);
  548. /* set the ATH (floating below specmax by a specified att) */
  549. if(p->vi->athp){
  550. float att=specmax+p->vi->ath_adjatt;
  551. if(att<p->vi->ath_maxatt)att=p->vi->ath_maxatt;
  552. att=fromdB(att);
  553. for(i=0;i<n;i++){
  554. float av=p->ath[i]*att;
  555. if(av>flr[i])flr[i]=av;
  556. }
  557. }
  558. /* peak attenuation ******/
  559. if(p->vi->peakattp){
  560. memset(seed,0,n*sizeof(float));
  561. seed_att(p,p->peakatt,smooth,seed,specmax);
  562. max_seeds(p,seed,flr);
  563. }
  564. /* tone masking */
  565. if(p->vi->tonemaskp){
  566. memset(seed,0,n*sizeof(float));
  567. memset(seed2,0,n*sizeof(float));
  568. seed_generic(p,p->tonecurves,smooth,flr,seed2,specmax);
  569. max_seeds(p,seed2,seed2);
  570. for(i=0;i<n;i++)if(seed2[i]<flr[i])seed2[i]=flr[i];
  571. for(i=0;i<n;i++)if(seed2[i]<decay[i])seed2[i]=decay[i];
  572. seed_generic(p,p->tonecurves,smooth,seed2,seed,specmax);
  573. max_seeds(p,seed,seed);
  574. if(p->vi->decayp)
  575. compute_decay_fixed(p,seed,decay,n);
  576. for(i=0;i<n;i++)if(flr[i]<seed[i])flr[i]=seed[i];
  577. }
  578. /* doing this here is clean, but we need to find a faster way to do
  579. it than to just tack it on */
  580. for(i=0;i<n;i++)if(2.*f[i]>flr[i] || -2.*f[i]>flr[i])break;
  581. if(i==n)memset(flr,0,sizeof(float)*n);
  582. }
  583. /* this applies the floor and (optionally) tries to preserve noise
  584. energy in low resolution portions of the spectrum */
  585. /* f and flr are *linear* scale, not dB */
  586. void _vp_apply_floor(vorbis_look_psy *p,float *f, float *flr){
  587. float *work=alloca(p->n*sizeof(float));
  588. int j;
  589. /* subtract the floor */
  590. for(j=0;j<p->n;j++){
  591. if(flr[j]<=0)
  592. work[j]=0.;
  593. else
  594. work[j]=f[j]/flr[j];
  595. }
  596. memcpy(f,work,p->n*sizeof(float));
  597. }