psy.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH 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.16.2.2.2.13 2000/05/08 08:25:43 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. /* Why Bark scale for encoding but not masking? Because masking has a
  27. strong harmonic dependancy */
  28. /* the beginnings of real psychoacoustic infrastructure. This is
  29. still not tightly tuned */
  30. void _vi_psy_free(vorbis_info_psy *i){
  31. if(i){
  32. memset(i,0,sizeof(vorbis_info_psy));
  33. free(i);
  34. }
  35. }
  36. /* Set up decibel threshhold slopes on a Bark frequency scale */
  37. /* the only bit left on a Bark scale. No reason to change it right now */
  38. static void set_curve(double *ref,double *c,int n, double crate){
  39. int i,j=0;
  40. for(i=0;i<MAX_BARK-1;i++){
  41. int endpos=rint(fromBARK(i+1)*2*n/crate);
  42. double base=ref[i];
  43. double delta=(ref[i+1]-base)/(endpos-j);
  44. for(;j<endpos && j<n;j++){
  45. c[j]=base;
  46. base+=delta;
  47. }
  48. }
  49. }
  50. static void min_curve(double *c,
  51. double *c2){
  52. int i;
  53. for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
  54. }
  55. static void max_curve(double *c,
  56. double *c2){
  57. int i;
  58. for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
  59. }
  60. static void attenuate_curve(double *c,double att){
  61. int i;
  62. for(i=0;i<EHMER_MAX;i++)
  63. c[i]+=att;
  64. }
  65. static void linear_curve(double *c){
  66. int i;
  67. for(i=0;i<EHMER_MAX;i++)
  68. if(c[i]<=-900.)
  69. c[i]=0.;
  70. else
  71. c[i]=fromdB(c[i]);
  72. }
  73. static void interp_curve_dB(double *c,double *c1,double *c2,double del){
  74. int i;
  75. for(i=0;i<EHMER_MAX;i++)
  76. c[i]=fromdB(todB(c2[i])*del+todB(c1[i])*(1.-del));
  77. }
  78. static void interp_curve(double *c,double *c1,double *c2,double del){
  79. int i;
  80. for(i=0;i<EHMER_MAX;i++)
  81. c[i]=c2[i]*del+c1[i]*(1.-del);
  82. }
  83. static void setup_curve(double **c,
  84. int oc,
  85. double *curveatt_dB){
  86. int i,j;
  87. double tempc[9][EHMER_MAX];
  88. double ath[EHMER_MAX];
  89. for(i=0;i<EHMER_MAX;i++){
  90. double bark=toBARK(fromOC(oc*.5+(i-EHMER_OFFSET)*.125));
  91. int ibark=floor(bark);
  92. double del=bark-ibark;
  93. if(ibark<26)
  94. ath[i]=ATH_Bark_dB[ibark]*(1.-del)+ATH_Bark_dB[ibark+1]*del;
  95. else
  96. ath[i]=200;
  97. }
  98. memcpy(c[0],c[2],sizeof(double)*EHMER_MAX);
  99. /* the temp curves are a bit roundabout, but this is only in
  100. init. */
  101. for(i=0;i<5;i++){
  102. memcpy(tempc[i*2],c[i*2],sizeof(double)*EHMER_MAX);
  103. attenuate_curve(tempc[i*2],curveatt_dB[i]+(i+1)*20);
  104. max_curve(tempc[i*2],ath);
  105. attenuate_curve(tempc[i*2],-(i+1)*20);
  106. }
  107. /* normalize them so the driving amplitude is 0dB */
  108. for(i=0;i<5;i++){
  109. attenuate_curve(c[i*2],curveatt_dB[i]);
  110. }
  111. /* The c array is comes in as dB curves at 20 40 60 80 100 dB.
  112. interpolate intermediate dB curves */
  113. for(i=0;i<7;i+=2){
  114. interp_curve(c[i+1],c[i],c[i+2],.5);
  115. interp_curve(tempc[i+1],tempc[i],tempc[i+2],.5);
  116. }
  117. /* take things out of dB domain into linear amplitude */
  118. for(i=0;i<9;i++)
  119. linear_curve(c[i]);
  120. for(i=0;i<9;i++)
  121. linear_curve(tempc[i]);
  122. /* Now limit the louder curves.
  123. the idea is this: We don't know what the playback attenuation
  124. will be; 0dB SL moves every time the user twiddles the volume
  125. knob. So that means we have to use a single 'most pessimal' curve
  126. for all masking amplitudes, right? Wrong. The *loudest* sound
  127. can be in (we assume) a range of ...+100dB] SL. However, sounds
  128. 20dB down will be in a range ...+80], 40dB down is from ...+60],
  129. etc... */
  130. for(i=8;i>=0;i--){
  131. for(j=0;j<i;j++)
  132. min_curve(c[i],tempc[j]);
  133. }
  134. }
  135. void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,int n,long rate){
  136. long i,j;
  137. double rate2=rate/2.;
  138. memset(p,0,sizeof(vorbis_look_psy));
  139. p->ath=malloc(n*sizeof(double));
  140. p->octave=malloc(n*sizeof(int));
  141. p->vi=vi;
  142. p->n=n;
  143. /* set up the lookups for a given blocksize and sample rate */
  144. /* Vorbis max sample rate is limited by 26 Bark (54kHz) */
  145. set_curve(ATH_Bark_dB, p->ath,n,rate);
  146. for(i=0;i<n;i++)
  147. p->ath[i]=fromdB(p->ath[i]+vi->ath_att);
  148. for(i=0;i<n;i++){
  149. int oc=rint(toOC((i+.5)*rate2/n)*2.);
  150. if(oc<0)oc=0;
  151. if(oc>10)oc=10;
  152. p->octave[i]=oc;
  153. }
  154. p->tonecurves=malloc(11*sizeof(double **));
  155. p->noisecurves=malloc(11*sizeof(double **));
  156. for(i=0;i<11;i++){
  157. p->tonecurves[i]=malloc(9*sizeof(double *));
  158. p->noisecurves[i]=malloc(9*sizeof(double *));
  159. }
  160. for(i=0;i<11;i++)
  161. for(j=0;j<9;j++){
  162. p->tonecurves[i][j]=malloc(EHMER_MAX*sizeof(double));
  163. p->noisecurves[i][j]=malloc(EHMER_MAX*sizeof(double));
  164. }
  165. memcpy(p->tonecurves[0][2],tone_250_40dB_SL,sizeof(double)*EHMER_MAX);
  166. memcpy(p->tonecurves[0][4],tone_250_60dB_SL,sizeof(double)*EHMER_MAX);
  167. memcpy(p->tonecurves[0][6],tone_250_80dB_SL,sizeof(double)*EHMER_MAX);
  168. memcpy(p->tonecurves[0][8],tone_250_80dB_SL,sizeof(double)*EHMER_MAX);
  169. memcpy(p->tonecurves[2][2],tone_500_40dB_SL,sizeof(double)*EHMER_MAX);
  170. memcpy(p->tonecurves[2][4],tone_500_60dB_SL,sizeof(double)*EHMER_MAX);
  171. memcpy(p->tonecurves[2][6],tone_500_80dB_SL,sizeof(double)*EHMER_MAX);
  172. memcpy(p->tonecurves[2][8],tone_500_100dB_SL,sizeof(double)*EHMER_MAX);
  173. memcpy(p->tonecurves[4][2],tone_1000_40dB_SL,sizeof(double)*EHMER_MAX);
  174. memcpy(p->tonecurves[4][4],tone_1000_60dB_SL,sizeof(double)*EHMER_MAX);
  175. memcpy(p->tonecurves[4][6],tone_1000_80dB_SL,sizeof(double)*EHMER_MAX);
  176. memcpy(p->tonecurves[4][8],tone_1000_100dB_SL,sizeof(double)*EHMER_MAX);
  177. memcpy(p->tonecurves[6][2],tone_2000_40dB_SL,sizeof(double)*EHMER_MAX);
  178. memcpy(p->tonecurves[6][4],tone_2000_60dB_SL,sizeof(double)*EHMER_MAX);
  179. memcpy(p->tonecurves[6][6],tone_2000_80dB_SL,sizeof(double)*EHMER_MAX);
  180. memcpy(p->tonecurves[6][8],tone_2000_100dB_SL,sizeof(double)*EHMER_MAX);
  181. memcpy(p->tonecurves[8][2],tone_4000_40dB_SL,sizeof(double)*EHMER_MAX);
  182. memcpy(p->tonecurves[8][4],tone_4000_60dB_SL,sizeof(double)*EHMER_MAX);
  183. memcpy(p->tonecurves[8][6],tone_4000_80dB_SL,sizeof(double)*EHMER_MAX);
  184. memcpy(p->tonecurves[8][8],tone_4000_100dB_SL,sizeof(double)*EHMER_MAX);
  185. memcpy(p->tonecurves[10][2],tone_8000_60dB_SL,sizeof(double)*EHMER_MAX);
  186. memcpy(p->tonecurves[10][4],tone_8000_60dB_SL,sizeof(double)*EHMER_MAX);
  187. memcpy(p->tonecurves[10][6],tone_8000_80dB_SL,sizeof(double)*EHMER_MAX);
  188. memcpy(p->tonecurves[10][8],tone_8000_100dB_SL,sizeof(double)*EHMER_MAX);
  189. memcpy(p->noisecurves[0][2],noise_500_60dB_SL,sizeof(double)*EHMER_MAX);
  190. memcpy(p->noisecurves[0][4],noise_500_60dB_SL,sizeof(double)*EHMER_MAX);
  191. memcpy(p->noisecurves[0][6],noise_500_80dB_SL,sizeof(double)*EHMER_MAX);
  192. memcpy(p->noisecurves[0][8],noise_500_80dB_SL,sizeof(double)*EHMER_MAX);
  193. memcpy(p->noisecurves[2][2],noise_500_60dB_SL,sizeof(double)*EHMER_MAX);
  194. memcpy(p->noisecurves[2][4],noise_500_60dB_SL,sizeof(double)*EHMER_MAX);
  195. memcpy(p->noisecurves[2][6],noise_500_80dB_SL,sizeof(double)*EHMER_MAX);
  196. memcpy(p->noisecurves[2][8],noise_500_80dB_SL,sizeof(double)*EHMER_MAX);
  197. memcpy(p->noisecurves[4][2],noise_1000_60dB_SL,sizeof(double)*EHMER_MAX);
  198. memcpy(p->noisecurves[4][4],noise_1000_60dB_SL,sizeof(double)*EHMER_MAX);
  199. memcpy(p->noisecurves[4][6],noise_1000_80dB_SL,sizeof(double)*EHMER_MAX);
  200. memcpy(p->noisecurves[4][8],noise_1000_80dB_SL,sizeof(double)*EHMER_MAX);
  201. memcpy(p->noisecurves[6][2],noise_2000_60dB_SL,sizeof(double)*EHMER_MAX);
  202. memcpy(p->noisecurves[6][4],noise_2000_60dB_SL,sizeof(double)*EHMER_MAX);
  203. memcpy(p->noisecurves[6][6],noise_2000_80dB_SL,sizeof(double)*EHMER_MAX);
  204. memcpy(p->noisecurves[6][8],noise_2000_80dB_SL,sizeof(double)*EHMER_MAX);
  205. memcpy(p->noisecurves[8][2],noise_4000_60dB_SL,sizeof(double)*EHMER_MAX);
  206. memcpy(p->noisecurves[8][4],noise_4000_60dB_SL,sizeof(double)*EHMER_MAX);
  207. memcpy(p->noisecurves[8][6],noise_4000_80dB_SL,sizeof(double)*EHMER_MAX);
  208. memcpy(p->noisecurves[8][8],noise_4000_80dB_SL,sizeof(double)*EHMER_MAX);
  209. memcpy(p->noisecurves[10][2],noise_4000_60dB_SL,sizeof(double)*EHMER_MAX);
  210. memcpy(p->noisecurves[10][4],noise_4000_60dB_SL,sizeof(double)*EHMER_MAX);
  211. memcpy(p->noisecurves[10][6],noise_4000_80dB_SL,sizeof(double)*EHMER_MAX);
  212. memcpy(p->noisecurves[10][8],noise_4000_80dB_SL,sizeof(double)*EHMER_MAX);
  213. setup_curve(p->tonecurves[0],0,vi->toneatt_250Hz);
  214. setup_curve(p->tonecurves[2],2,vi->toneatt_500Hz);
  215. setup_curve(p->tonecurves[4],4,vi->toneatt_1000Hz);
  216. setup_curve(p->tonecurves[6],6,vi->toneatt_2000Hz);
  217. setup_curve(p->tonecurves[8],8,vi->toneatt_4000Hz);
  218. setup_curve(p->tonecurves[10],10,vi->toneatt_8000Hz);
  219. setup_curve(p->noisecurves[0],0,vi->noiseatt_250Hz);
  220. setup_curve(p->noisecurves[2],2,vi->noiseatt_500Hz);
  221. setup_curve(p->noisecurves[4],4,vi->noiseatt_1000Hz);
  222. setup_curve(p->noisecurves[6],6,vi->noiseatt_2000Hz);
  223. setup_curve(p->noisecurves[8],8,vi->noiseatt_4000Hz);
  224. setup_curve(p->noisecurves[10],10,vi->noiseatt_8000Hz);
  225. for(i=1;i<11;i+=2)
  226. for(j=0;j<9;j++){
  227. interp_curve_dB(p->tonecurves[i][j],
  228. p->tonecurves[i-1][j],
  229. p->tonecurves[i+1][j],.5);
  230. interp_curve_dB(p->noisecurves[i][j],
  231. p->noisecurves[i-1][j],
  232. p->noisecurves[i+1][j],.5);
  233. }
  234. }
  235. void _vp_psy_clear(vorbis_look_psy *p){
  236. int i,j;
  237. if(p){
  238. if(p->ath)free(p->ath);
  239. if(p->octave)free(p->octave);
  240. if(p->noisecurves){
  241. for(i=0;i<11;i++){
  242. for(j=0;j<9;j++){
  243. free(p->tonecurves[i][j]);
  244. free(p->noisecurves[i][j]);
  245. }
  246. free(p->noisecurves[i]);
  247. free(p->tonecurves[i]);
  248. }
  249. free(p->tonecurves);
  250. free(p->noisecurves);
  251. }
  252. memset(p,0,sizeof(vorbis_look_psy));
  253. }
  254. }
  255. static void compute_decay(vorbis_look_psy *p,double *f, double *decay, int n){
  256. int i;
  257. /* handle decay */
  258. if(p->vi->decayp && decay){
  259. double decscale=1.-pow(p->vi->decay_coeff,n);
  260. double attscale=1.-pow(p->vi->attack_coeff,n);
  261. for(i=0;i<n;i++){
  262. double del=f[i]-decay[i];
  263. if(del>0)
  264. /* add energy */
  265. decay[i]+=del*attscale;
  266. else
  267. /* remove energy */
  268. decay[i]+=del*decscale;
  269. if(decay[i]>f[i])f[i]=decay[i];
  270. }
  271. }
  272. }
  273. static double _eights[EHMER_MAX+1]={
  274. .2500000000000000000,.2726269331663144148,
  275. .2973017787506802667,.3242098886627524165,
  276. .3535533905932737622,.3855527063519852059,
  277. .4204482076268572715,.4585020216023356159,
  278. .5000000000000000000,.5452538663326288296,
  279. .5946035575013605334,.6484197773255048330,
  280. .7071067811865475244,.7711054127039704118,
  281. .8408964152537145430,.9170040432046712317,
  282. 1.000000000000000000,1.090507732665257659,
  283. 1.189207115002721066,1.296839554651009665,
  284. 1.414213562373095048,1.542210825407940823,
  285. 1.681792830507429085,1.834008086409342463,
  286. 2.000000000000000000,2.181015465330515318,
  287. 2.378414230005442133,2.593679109302019331,
  288. 2.828427124746190097,3.084421650815881646,
  289. 3.363585661014858171,3.668016172818684926,
  290. 4.000000000000000000,4.362030930661030635,
  291. 4.756828460010884265,5.187358218604038662,
  292. 5.656854249492380193,6.168843301631763292,
  293. 6.727171322029716341,7.336032345637369851,
  294. 8.000000000000000000,8.724061861322061270,
  295. 9.513656920021768529,10.37471643720807732,
  296. 11.31370849898476038,12.33768660326352658,
  297. 13.45434264405943268,14.67206469127473970,
  298. 16.00000000000000000,17.44812372264412253,
  299. 19.02731384004353705,20.74943287441615464,
  300. 22.62741699796952076,24.67537320652705316,
  301. 26.90868528811886536,29.34412938254947939};
  302. static void seed_peaks(double *floor,
  303. double **curves,
  304. double amp,double specmax,
  305. int x,int n,double specatt){
  306. int i;
  307. double x16=x*(1./16.);
  308. int prevx=x*_eights[0]-x16;
  309. int nextx;
  310. /* make this attenuation adjustable */
  311. int choice=rint((todB(amp)-specmax+specatt)/10.)-2;
  312. if(choice<0)choice=0;
  313. if(choice>8)choice=8;
  314. for(i=0;i<EHMER_MAX;i++){
  315. if(prevx<n){
  316. double lin=curves[choice][i];
  317. nextx=x*_eights[i]+x16;
  318. nextx=(nextx<n?nextx:n);
  319. if(lin){
  320. lin*=amp;
  321. if(floor[prevx]<lin)floor[prevx]=lin;
  322. }
  323. prevx=nextx;
  324. }
  325. }
  326. }
  327. static void seed_generic(vorbis_look_psy *p,
  328. double ***curves,
  329. double *f,
  330. double *flr,
  331. double specmax){
  332. vorbis_info_psy *vi=p->vi;
  333. long n=p->n,i;
  334. /* prime the working vector with peak values */
  335. /* Use the 250 Hz curve up to 250 Hz and 8kHz curve after 8kHz. */
  336. for(i=0;i<n;i++)
  337. if(f[i]>flr[i])
  338. seed_peaks(flr,curves[p->octave[i]],f[i],
  339. specmax,i,n,vi->max_curve_dB);
  340. }
  341. /* bleaugh, this is more complicated than it needs to be */
  342. static void max_seeds(vorbis_look_psy *p,double *flr){
  343. long n=p->n,i,j;
  344. long *posstack=alloca(n*sizeof(long));
  345. double *ampstack=alloca(n*sizeof(double));
  346. long stack=0;
  347. for(i=0;i<n;i++){
  348. if(stack<2){
  349. posstack[stack]=i;
  350. ampstack[stack++]=flr[i];
  351. }else{
  352. while(1){
  353. if(flr[i]<ampstack[stack-1]){
  354. posstack[stack]=i;
  355. ampstack[stack++]=flr[i];
  356. break;
  357. }else{
  358. if(i<posstack[stack-1]*17/15){
  359. if(stack>1 && ampstack[stack-1]<ampstack[stack-2] &&
  360. i<posstack[stack-2]*17/15){
  361. /* we completely overlap, making stack-1 irrelevant. pop it */
  362. stack--;
  363. continue;
  364. }
  365. }
  366. posstack[stack]=i;
  367. ampstack[stack++]=flr[i];
  368. break;
  369. }
  370. }
  371. }
  372. }
  373. /* the stack now contains only the positions that are relevant. Scan
  374. 'em straight through */
  375. {
  376. long pos=0;
  377. for(i=0;i<stack;i++){
  378. long endpos;
  379. if(i<stack-1 && ampstack[i+1]>ampstack[i]){
  380. endpos=posstack[i+1];
  381. }else{
  382. endpos=posstack[i]*17/15;
  383. }
  384. if(endpos>n)endpos=n;
  385. for(j=pos;j<endpos;j++)flr[j]=ampstack[i];
  386. pos=endpos;
  387. }
  388. }
  389. /* there. Linear time. I now remember this was on a problem set I
  390. had in Grad Skool... I didn't solve it at the time ;-) */
  391. }
  392. #define noiseBIAS 5
  393. static void third_octave_noise(vorbis_look_psy *p,double *f,double *noise){
  394. long i,n=p->n;
  395. long lo=0,hi=0;
  396. double acc=0.;
  397. for(i=0;i<n;i++){
  398. /* not exactly correct, (the center frequency should be centered
  399. on a *log* scale), but not worth quibbling */
  400. long newhi=i*7/5+noiseBIAS;
  401. long newlo=i*5/7-noiseBIAS;
  402. if(newhi>n)newhi=n;
  403. for(;lo<newlo;lo++)
  404. acc-=todB(f[lo]); /* yeah, this ain't RMS */
  405. for(;hi<newhi;hi++)
  406. acc+=todB(f[hi]);
  407. noise[i]=fromdB(acc/(hi-lo));
  408. }
  409. }
  410. /* stability doesn't matter */
  411. static int comp(const void *a,const void *b){
  412. if(fabs(**(double **)a)<fabs(**(double **)b))
  413. return(1);
  414. else
  415. return(-1);
  416. }
  417. static int frameno=-1;
  418. void _vp_compute_mask(vorbis_look_psy *p,double *f,
  419. double *flr,
  420. double *mask,
  421. double *decay){
  422. double *noise=alloca(sizeof(double)*p->n);
  423. double *work=alloca(sizeof(double)*p->n);
  424. int i,n=p->n;
  425. double specmax=0.;
  426. frameno++;
  427. /* don't use the smoothed data for noise */
  428. third_octave_noise(p,f,noise);
  429. /* compute, update and apply decay accumulator */
  430. for(i=0;i<n;i++)work[i]=fabs(f[i]);
  431. compute_decay(p,work,decay,n);
  432. if(p->vi->smoothp){
  433. /* compute power^.5 of three neighboring bins to smooth for peaks
  434. that get split twixt bins/peaks that nail the bin. This evens
  435. out treatment as we're not doing additive masking any longer. */
  436. double acc=work[0]*work[0]+work[1]*work[1];
  437. double prev=work[0];
  438. work[0]=sqrt(acc);
  439. for(i=1;i<n-1;i++){
  440. double this=work[i];
  441. acc+=work[i+1]*work[i+1];
  442. work[i]=sqrt(acc);
  443. acc-=prev*prev;
  444. prev=this;
  445. }
  446. work[n-1]=sqrt(acc);
  447. }
  448. /* find the highest peak so we know the limits */
  449. for(i=0;i<n;i++){
  450. if(work[i]>specmax)specmax=work[i];
  451. }
  452. specmax=todB(specmax);
  453. memset(flr,0,n*sizeof(double));
  454. /* seed the tone masking */
  455. if(p->vi->tonemaskp)
  456. seed_generic(p,p->tonecurves,work,flr,specmax);
  457. /* seed the noise masking */
  458. if(p->vi->noisemaskp)
  459. seed_generic(p,p->noisecurves,noise,flr,specmax);
  460. /* chase the seeds */
  461. max_seeds(p,flr);
  462. /* mask off the ATH */
  463. if(p->vi->athp)
  464. for(i=0;i<n;i++)
  465. mask[i]=max(p->ath[i],flr[i]*.5);
  466. else
  467. for(i=0;i<n;i++)
  468. mask[i]=flr[i]*.5;
  469. }
  470. /* this applies the floor and (optionally) tries to preserve noise
  471. energy in low resolution portions of the spectrum */
  472. /* f and flr are *linear* scale, not dB */
  473. void _vp_apply_floor(vorbis_look_psy *p,double *f,
  474. double *flr,double *mask){
  475. double *work=alloca(p->n*sizeof(double));
  476. double thresh=fromdB(p->vi->noisefit_threshdB);
  477. int i,j,addcount=0;
  478. thresh*=thresh;
  479. /* subtract the floor */
  480. for(j=0;j<p->n;j++){
  481. if(flr[j]<=0 || fabs(f[j])<mask[j])
  482. work[j]=0.;
  483. else
  484. work[j]=f[j]/flr[j];
  485. }
  486. /* look at spectral energy levels. Noise is noise; sensation level
  487. is important */
  488. if(p->vi->noisefitp){
  489. double **index=alloca(p->vi->noisefit_subblock*sizeof(double *));
  490. /* we're looking for zero values that we want to reinstate (to
  491. floor level) in order to raise the SL noise level back closer
  492. to original. Desired result; the SL of each block being as
  493. close to (but still less than) the original as possible. Don't
  494. bother if the net result is a change of less than
  495. p->vi->noisefit_thresh dB */
  496. for(i=0;i<p->n;){
  497. double original_SL=0.;
  498. double current_SL=0.;
  499. int z=0;
  500. /* compute current SL */
  501. for(j=0;j<p->vi->noisefit_subblock && i<p->n;j++,i++){
  502. double y=(f[i]*f[i]);
  503. original_SL+=y;
  504. if(work[i]){
  505. current_SL+=y;
  506. }else{
  507. index[z++]=f+i;
  508. }
  509. }
  510. /* sort the values below mask; add back the largest first, stop
  511. when we violate the desired result above (which may be
  512. immediately) */
  513. if(z && current_SL*thresh<original_SL){
  514. qsort(index,z,sizeof(double *),&comp);
  515. for(j=0;j<z;j++){
  516. int p=index[j]-f;
  517. double val=flr[p]*flr[p]+current_SL;
  518. if(val<original_SL && mask[p]<flr[p]){
  519. addcount++;
  520. if(f[p]>0)
  521. work[p]=1;
  522. else
  523. work[p]=-1;
  524. current_SL=val;
  525. }else
  526. break;
  527. }
  528. }
  529. }
  530. }
  531. memcpy(f,work,p->n*sizeof(double));
  532. }