psy.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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.20.2.4 2000/06/14 01:24:18 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. if(j<endpos){
  44. double delta=(ref[i+1]-base)/(endpos-j);
  45. for(;j<endpos && j<n;j++){
  46. c[j]=base;
  47. base+=delta;
  48. }
  49. }
  50. }
  51. }
  52. static void min_curve(double *c,
  53. double *c2){
  54. int i;
  55. for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
  56. }
  57. static void max_curve(double *c,
  58. double *c2){
  59. int i;
  60. for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
  61. }
  62. static void attenuate_curve(double *c,double att){
  63. int i;
  64. for(i=0;i<EHMER_MAX;i++)
  65. c[i]+=att;
  66. }
  67. static void linear_curve(double *c){
  68. int i;
  69. for(i=0;i<EHMER_MAX;i++)
  70. if(c[i]<=-900.)
  71. c[i]=0.;
  72. else
  73. c[i]=fromdB(c[i]);
  74. }
  75. static void interp_curve_dB(double *c,double *c1,double *c2,double del){
  76. int i;
  77. for(i=0;i<EHMER_MAX;i++)
  78. c[i]=fromdB(todB(c2[i])*del+todB(c1[i])*(1.-del));
  79. }
  80. static void interp_curve(double *c,double *c1,double *c2,double del){
  81. int i;
  82. for(i=0;i<EHMER_MAX;i++)
  83. c[i]=c2[i]*del+c1[i]*(1.-del);
  84. }
  85. static void setup_curve(double **c,
  86. int oc,
  87. double *curveatt_dB){
  88. int i,j;
  89. double tempc[9][EHMER_MAX];
  90. double ath[EHMER_MAX];
  91. for(i=0;i<EHMER_MAX;i++){
  92. double bark=toBARK(fromOC(oc*.5+(i-EHMER_OFFSET)*.125));
  93. int ibark=floor(bark);
  94. double del=bark-ibark;
  95. if(ibark<26)
  96. ath[i]=ATH_Bark_dB[ibark]*(1.-del)+ATH_Bark_dB[ibark+1]*del;
  97. else
  98. ath[i]=200;
  99. }
  100. memcpy(c[0],c[2],sizeof(double)*EHMER_MAX);
  101. /* the temp curves are a bit roundabout, but this is only in
  102. init. */
  103. for(i=0;i<5;i++){
  104. memcpy(tempc[i*2],c[i*2],sizeof(double)*EHMER_MAX);
  105. attenuate_curve(tempc[i*2],curveatt_dB[i]+(i+1)*20);
  106. max_curve(tempc[i*2],ath);
  107. attenuate_curve(tempc[i*2],-(i+1)*20);
  108. }
  109. /* normalize them so the driving amplitude is 0dB */
  110. for(i=0;i<5;i++){
  111. attenuate_curve(c[i*2],curveatt_dB[i]);
  112. }
  113. /* The c array is comes in as dB curves at 20 40 60 80 100 dB.
  114. interpolate intermediate dB curves */
  115. for(i=0;i<7;i+=2){
  116. interp_curve(c[i+1],c[i],c[i+2],.5);
  117. interp_curve(tempc[i+1],tempc[i],tempc[i+2],.5);
  118. }
  119. /* take things out of dB domain into linear amplitude */
  120. for(i=0;i<9;i++)
  121. linear_curve(c[i]);
  122. for(i=0;i<9;i++)
  123. linear_curve(tempc[i]);
  124. /* Now limit the louder curves.
  125. the idea is this: We don't know what the playback attenuation
  126. will be; 0dB SL moves every time the user twiddles the volume
  127. knob. So that means we have to use a single 'most pessimal' curve
  128. for all masking amplitudes, right? Wrong. The *loudest* sound
  129. can be in (we assume) a range of ...+100dB] SL. However, sounds
  130. 20dB down will be in a range ...+80], 40dB down is from ...+60],
  131. etc... */
  132. for(i=8;i>=0;i--){
  133. for(j=0;j<i;j++)
  134. min_curve(c[i],tempc[j]);
  135. }
  136. }
  137. void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,int n,long rate){
  138. long i,j;
  139. double rate2=rate/2.;
  140. memset(p,0,sizeof(vorbis_look_psy));
  141. p->ath=malloc(n*sizeof(double));
  142. p->octave=malloc(n*sizeof(int));
  143. p->vi=vi;
  144. p->n=n;
  145. /* set up the lookups for a given blocksize and sample rate */
  146. /* Vorbis max sample rate is limited by 26 Bark (54kHz) */
  147. set_curve(ATH_Bark_dB, p->ath,n,rate);
  148. for(i=0;i<n;i++)
  149. p->ath[i]=fromdB(p->ath[i]+vi->ath_att);
  150. for(i=0;i<n;i++){
  151. int oc=rint(toOC((i+.5)*rate2/n)*2.);
  152. if(oc<0)oc=0;
  153. if(oc>12)oc=12;
  154. p->octave[i]=oc;
  155. }
  156. p->tonecurves=malloc(13*sizeof(double **));
  157. p->noisecurves=malloc(13*sizeof(double **));
  158. p->peakatt=malloc(7*sizeof(double *));
  159. for(i=0;i<13;i++){
  160. p->tonecurves[i]=malloc(9*sizeof(double *));
  161. p->noisecurves[i]=malloc(9*sizeof(double *));
  162. }
  163. for(i=0;i<7;i++)
  164. p->peakatt[i]=malloc(5*sizeof(double));
  165. for(i=0;i<13;i++)
  166. for(j=0;j<9;j++){
  167. p->tonecurves[i][j]=malloc(EHMER_MAX*sizeof(double));
  168. p->noisecurves[i][j]=malloc(EHMER_MAX*sizeof(double));
  169. }
  170. /* OK, yeah, this was a silly way to do it */
  171. memcpy(p->tonecurves[0][2],tone_125_80dB_SL,sizeof(double)*EHMER_MAX);
  172. memcpy(p->tonecurves[0][4],tone_125_80dB_SL,sizeof(double)*EHMER_MAX);
  173. memcpy(p->tonecurves[0][6],tone_125_80dB_SL,sizeof(double)*EHMER_MAX);
  174. memcpy(p->tonecurves[0][8],tone_125_100dB_SL,sizeof(double)*EHMER_MAX);
  175. memcpy(p->tonecurves[2][2],tone_250_40dB_SL,sizeof(double)*EHMER_MAX);
  176. memcpy(p->tonecurves[2][4],tone_250_60dB_SL,sizeof(double)*EHMER_MAX);
  177. memcpy(p->tonecurves[2][6],tone_250_80dB_SL,sizeof(double)*EHMER_MAX);
  178. memcpy(p->tonecurves[2][8],tone_250_80dB_SL,sizeof(double)*EHMER_MAX);
  179. memcpy(p->tonecurves[4][2],tone_500_40dB_SL,sizeof(double)*EHMER_MAX);
  180. memcpy(p->tonecurves[4][4],tone_500_60dB_SL,sizeof(double)*EHMER_MAX);
  181. memcpy(p->tonecurves[4][6],tone_500_80dB_SL,sizeof(double)*EHMER_MAX);
  182. memcpy(p->tonecurves[4][8],tone_500_100dB_SL,sizeof(double)*EHMER_MAX);
  183. memcpy(p->tonecurves[6][2],tone_1000_40dB_SL,sizeof(double)*EHMER_MAX);
  184. memcpy(p->tonecurves[6][4],tone_1000_60dB_SL,sizeof(double)*EHMER_MAX);
  185. memcpy(p->tonecurves[6][6],tone_1000_80dB_SL,sizeof(double)*EHMER_MAX);
  186. memcpy(p->tonecurves[6][8],tone_1000_100dB_SL,sizeof(double)*EHMER_MAX);
  187. memcpy(p->tonecurves[8][2],tone_2000_40dB_SL,sizeof(double)*EHMER_MAX);
  188. memcpy(p->tonecurves[8][4],tone_2000_60dB_SL,sizeof(double)*EHMER_MAX);
  189. memcpy(p->tonecurves[8][6],tone_2000_80dB_SL,sizeof(double)*EHMER_MAX);
  190. memcpy(p->tonecurves[8][8],tone_2000_100dB_SL,sizeof(double)*EHMER_MAX);
  191. memcpy(p->tonecurves[10][2],tone_4000_40dB_SL,sizeof(double)*EHMER_MAX);
  192. memcpy(p->tonecurves[10][4],tone_4000_60dB_SL,sizeof(double)*EHMER_MAX);
  193. memcpy(p->tonecurves[10][6],tone_4000_80dB_SL,sizeof(double)*EHMER_MAX);
  194. memcpy(p->tonecurves[10][8],tone_4000_100dB_SL,sizeof(double)*EHMER_MAX);
  195. memcpy(p->tonecurves[12][2],tone_4000_40dB_SL,sizeof(double)*EHMER_MAX);
  196. memcpy(p->tonecurves[12][4],tone_4000_60dB_SL,sizeof(double)*EHMER_MAX);
  197. memcpy(p->tonecurves[12][6],tone_8000_80dB_SL,sizeof(double)*EHMER_MAX);
  198. memcpy(p->tonecurves[12][8],tone_8000_100dB_SL,sizeof(double)*EHMER_MAX);
  199. memcpy(p->noisecurves[0][2],noise_500_60dB_SL,sizeof(double)*EHMER_MAX);
  200. memcpy(p->noisecurves[0][4],noise_500_60dB_SL,sizeof(double)*EHMER_MAX);
  201. memcpy(p->noisecurves[0][6],noise_500_80dB_SL,sizeof(double)*EHMER_MAX);
  202. memcpy(p->noisecurves[0][8],noise_500_80dB_SL,sizeof(double)*EHMER_MAX);
  203. memcpy(p->noisecurves[2][2],noise_500_60dB_SL,sizeof(double)*EHMER_MAX);
  204. memcpy(p->noisecurves[2][4],noise_500_60dB_SL,sizeof(double)*EHMER_MAX);
  205. memcpy(p->noisecurves[2][6],noise_500_80dB_SL,sizeof(double)*EHMER_MAX);
  206. memcpy(p->noisecurves[2][8],noise_500_80dB_SL,sizeof(double)*EHMER_MAX);
  207. memcpy(p->noisecurves[4][2],noise_500_60dB_SL,sizeof(double)*EHMER_MAX);
  208. memcpy(p->noisecurves[4][4],noise_500_60dB_SL,sizeof(double)*EHMER_MAX);
  209. memcpy(p->noisecurves[4][6],noise_500_80dB_SL,sizeof(double)*EHMER_MAX);
  210. memcpy(p->noisecurves[4][8],noise_500_80dB_SL,sizeof(double)*EHMER_MAX);
  211. memcpy(p->noisecurves[6][2],noise_1000_60dB_SL,sizeof(double)*EHMER_MAX);
  212. memcpy(p->noisecurves[6][4],noise_1000_60dB_SL,sizeof(double)*EHMER_MAX);
  213. memcpy(p->noisecurves[6][6],noise_1000_80dB_SL,sizeof(double)*EHMER_MAX);
  214. memcpy(p->noisecurves[6][8],noise_1000_80dB_SL,sizeof(double)*EHMER_MAX);
  215. memcpy(p->noisecurves[8][2],noise_2000_60dB_SL,sizeof(double)*EHMER_MAX);
  216. memcpy(p->noisecurves[8][4],noise_2000_60dB_SL,sizeof(double)*EHMER_MAX);
  217. memcpy(p->noisecurves[8][6],noise_2000_80dB_SL,sizeof(double)*EHMER_MAX);
  218. memcpy(p->noisecurves[8][8],noise_2000_80dB_SL,sizeof(double)*EHMER_MAX);
  219. memcpy(p->noisecurves[10][2],noise_4000_60dB_SL,sizeof(double)*EHMER_MAX);
  220. memcpy(p->noisecurves[10][4],noise_4000_60dB_SL,sizeof(double)*EHMER_MAX);
  221. memcpy(p->noisecurves[10][6],noise_4000_80dB_SL,sizeof(double)*EHMER_MAX);
  222. memcpy(p->noisecurves[10][8],noise_4000_80dB_SL,sizeof(double)*EHMER_MAX);
  223. memcpy(p->noisecurves[12][2],noise_4000_60dB_SL,sizeof(double)*EHMER_MAX);
  224. memcpy(p->noisecurves[12][4],noise_4000_60dB_SL,sizeof(double)*EHMER_MAX);
  225. memcpy(p->noisecurves[12][6],noise_4000_80dB_SL,sizeof(double)*EHMER_MAX);
  226. memcpy(p->noisecurves[12][8],noise_4000_80dB_SL,sizeof(double)*EHMER_MAX);
  227. setup_curve(p->tonecurves[0],0,vi->toneatt_125Hz);
  228. setup_curve(p->tonecurves[2],2,vi->toneatt_250Hz);
  229. setup_curve(p->tonecurves[4],4,vi->toneatt_500Hz);
  230. setup_curve(p->tonecurves[6],6,vi->toneatt_1000Hz);
  231. setup_curve(p->tonecurves[8],8,vi->toneatt_2000Hz);
  232. setup_curve(p->tonecurves[10],10,vi->toneatt_4000Hz);
  233. setup_curve(p->tonecurves[12],12,vi->toneatt_8000Hz);
  234. setup_curve(p->noisecurves[0],0,vi->noiseatt_125Hz);
  235. setup_curve(p->noisecurves[2],2,vi->noiseatt_250Hz);
  236. setup_curve(p->noisecurves[4],4,vi->noiseatt_500Hz);
  237. setup_curve(p->noisecurves[6],6,vi->noiseatt_1000Hz);
  238. setup_curve(p->noisecurves[8],8,vi->noiseatt_2000Hz);
  239. setup_curve(p->noisecurves[10],10,vi->noiseatt_4000Hz);
  240. setup_curve(p->noisecurves[12],12,vi->noiseatt_8000Hz);
  241. for(i=1;i<13;i+=2)
  242. for(j=0;j<9;j++){
  243. interp_curve_dB(p->tonecurves[i][j],
  244. p->tonecurves[i-1][j],
  245. p->tonecurves[i+1][j],.5);
  246. interp_curve_dB(p->noisecurves[i][j],
  247. p->noisecurves[i-1][j],
  248. p->noisecurves[i+1][j],.5);
  249. }
  250. for(i=0;i<5;i++){
  251. p->peakatt[0][i]=fromdB(p->vi->peakatt_125Hz[i]);
  252. p->peakatt[1][i]=fromdB(p->vi->peakatt_250Hz[i]);
  253. p->peakatt[2][i]=fromdB(p->vi->peakatt_500Hz[i]);
  254. p->peakatt[3][i]=fromdB(p->vi->peakatt_1000Hz[i]);
  255. p->peakatt[4][i]=fromdB(p->vi->peakatt_2000Hz[i]);
  256. p->peakatt[5][i]=fromdB(p->vi->peakatt_4000Hz[i]);
  257. p->peakatt[6][i]=fromdB(p->vi->peakatt_8000Hz[i]);
  258. }
  259. }
  260. void _vp_psy_clear(vorbis_look_psy *p){
  261. int i,j;
  262. if(p){
  263. if(p->ath)free(p->ath);
  264. if(p->octave)free(p->octave);
  265. if(p->noisecurves){
  266. for(i=0;i<13;i++){
  267. for(j=0;j<9;j++){
  268. free(p->tonecurves[i][j]);
  269. free(p->noisecurves[i][j]);
  270. }
  271. free(p->noisecurves[i]);
  272. free(p->tonecurves[i]);
  273. }
  274. for(i=0;i<7;i++)
  275. free(p->peakatt[i]);
  276. free(p->tonecurves);
  277. free(p->noisecurves);
  278. free(p->peakatt);
  279. }
  280. memset(p,0,sizeof(vorbis_look_psy));
  281. }
  282. }
  283. static void compute_decay(vorbis_look_psy *p,double *f, double *decay, int n){
  284. /* handle decay */
  285. int i;
  286. double decscale=1.-pow(p->vi->decay_coeff,n);
  287. double attscale=1.-pow(p->vi->attack_coeff,n);
  288. for(i=0;i<n;i++){
  289. double del=f[i]-decay[i];
  290. if(del>0)
  291. /* add energy */
  292. decay[i]+=del*attscale;
  293. else
  294. /* remove energy */
  295. decay[i]+=del*decscale;
  296. if(decay[i]>f[i])f[i]=decay[i];
  297. }
  298. }
  299. static double _eights[EHMER_MAX+1]={
  300. .2394004745,.2610680627,.2846967347,.3104639837,
  301. .3385633673,.3692059617,.4026219471,.4390623367,
  302. .4788008625,.5221360312,.5693933667,.6209278553,
  303. .6771266124,.7384117901,.8052437489,.8781245150,
  304. .9576015522,1.0442718740,1.1387865279,1.2418554865,
  305. 1.3542529803,1.4768233137,1.6104872070,1.7562487129,
  306. 1.9152027587,2.0885433709,2.2775726445,2.4837105245,
  307. 2.7085054716,2.9536460940,3.2209738324,3.5124967917,
  308. 3.8304048259,4.1770859876,4.5551444666,4.9674201522,
  309. 5.4170099651,5.9072911215,6.4419465017,7.0249923150,
  310. 7.6608082685,8.3541704668,9.1102872884,9.9348385106,
  311. 10.8340179740,11.8145801099,12.8838906772,14.0499820932,
  312. 15.3216137706,16.7083379167,18.2205712869,19.8696734335,
  313. 21.6680320357,23.6291559533,25.7677767018,28.0999591127,
  314. 30.6432220084};
  315. static void seed_curve(double *flr,
  316. double **curves,
  317. double amp,double specmax,
  318. int x,int n,double specatt){
  319. int i;
  320. int prevx=x*_eights[0]+.5;
  321. int nextx;
  322. /* make this attenuation adjustable */
  323. int choice=(int)((todB(amp)-specmax+specatt)/10.-1.5);
  324. choice=max(choice,0);
  325. choice=min(choice,8);
  326. for(i=0;i<EHMER_MAX;i++){
  327. if(prevx<n){
  328. double lin=curves[choice][i];
  329. nextx=x*_eights[i+1]+.5;
  330. nextx=(nextx<n?nextx:n);
  331. if(lin){
  332. lin*=amp;
  333. if(prevx<0){
  334. if(nextx>=0){
  335. flr[0]=max(flr[0],lin);
  336. }
  337. }else{
  338. flr[prevx]=max(flr[prevx],lin);
  339. }
  340. }
  341. prevx=nextx;
  342. }
  343. }
  344. }
  345. static void seed_peak(double *flr,
  346. double *att,
  347. double amp,double specmax,
  348. int x,int n,double specatt){
  349. int prevx=x*_eights[16];
  350. int nextx=x*_eights[17];
  351. /* make this attenuation adjustable */
  352. int choice=rint((todB(amp)-specmax+specatt)/20.)-1;
  353. if(choice<0)choice=0;
  354. if(choice>4)choice=4;
  355. if(prevx<n){
  356. double lin=att[choice];
  357. if(lin){
  358. lin*=amp;
  359. if(prevx<0){
  360. if(nextx>=0){
  361. if(flr[0]<lin)flr[0]=lin;
  362. }
  363. }else{
  364. if(flr[prevx]<lin)flr[prevx]=lin;
  365. }
  366. }
  367. }
  368. }
  369. static void seed_generic(vorbis_look_psy *p,
  370. double ***curves,
  371. double *f,
  372. double *flr,
  373. double specmax){
  374. vorbis_info_psy *vi=p->vi;
  375. long n=p->n,i;
  376. /* prime the working vector with peak values */
  377. /* Use the 125 Hz curve up to 125 Hz and 8kHz curve after 8kHz. */
  378. for(i=0;i<n;i++)
  379. if(f[i]>flr[i])
  380. seed_curve(flr,curves[p->octave[i]],f[i],
  381. specmax,i,n,vi->max_curve_dB);
  382. }
  383. static void seed_att(vorbis_look_psy *p,
  384. double *f,
  385. double *flr,
  386. double specmax){
  387. vorbis_info_psy *vi=p->vi;
  388. long n=p->n,i;
  389. for(i=0;i<n;i++)
  390. if(f[i]>flr[i])
  391. seed_peak(flr,p->peakatt[(p->octave[i]+1)>>1],f[i],
  392. specmax,i,n,vi->max_curve_dB);
  393. }
  394. /* bleaugh, this is more complicated than it needs to be */
  395. static void max_seeds(vorbis_look_psy *p,double *flr){
  396. long n=p->n,i,j;
  397. long *posstack=alloca(n*sizeof(long));
  398. double *ampstack=alloca(n*sizeof(double));
  399. long stack=0;
  400. for(i=0;i<n;i++){
  401. if(stack<2){
  402. posstack[stack]=i;
  403. ampstack[stack++]=flr[i];
  404. }else{
  405. while(1){
  406. if(flr[i]<ampstack[stack-1]){
  407. posstack[stack]=i;
  408. ampstack[stack++]=flr[i];
  409. break;
  410. }else{
  411. if(i<posstack[stack-1]*1.0905077080){
  412. if(stack>1 && ampstack[stack-1]<ampstack[stack-2] &&
  413. i<posstack[stack-2]*1.0905077080){
  414. /* we completely overlap, making stack-1 irrelevant. pop it */
  415. stack--;
  416. continue;
  417. }
  418. }
  419. posstack[stack]=i;
  420. ampstack[stack++]=flr[i];
  421. break;
  422. }
  423. }
  424. }
  425. }
  426. /* the stack now contains only the positions that are relevant. Scan
  427. 'em straight through */
  428. {
  429. long pos=0;
  430. for(i=0;i<stack;i++){
  431. long endpos;
  432. if(i<stack-1 && ampstack[i+1]>ampstack[i]){
  433. endpos=posstack[i+1];
  434. }else{
  435. endpos=posstack[i]*1.0905077080+1; /* +1 is important, else bin 0 is
  436. discarded in short frames */
  437. }
  438. if(endpos>n)endpos=n;
  439. for(j=pos;j<endpos;j++)flr[j]=ampstack[i];
  440. pos=endpos;
  441. }
  442. }
  443. /* there. Linear time. I now remember this was on a problem set I
  444. had in Grad Skool... I didn't solve it at the time ;-) */
  445. }
  446. #define noiseBIAS 5
  447. static void quarter_octave_noise(vorbis_look_psy *p,double *f,double *noise){
  448. long i,n=p->n;
  449. long lo=0,hi=0;
  450. double acc=0.;
  451. for(i=0;i<n;i++){
  452. /* not exactly correct, (the center frequency should be centered
  453. on a *log* scale), but not worth quibbling */
  454. long newhi=i*_eights[18]+noiseBIAS;
  455. long newlo=i*_eights[15]-noiseBIAS;
  456. if(newhi>n)newhi=n;
  457. for(;lo<newlo;lo++)
  458. acc-=todB(f[lo]); /* yeah, this ain't RMS */
  459. for(;hi<newhi;hi++)
  460. acc+=todB(f[hi]);
  461. noise[i]=fromdB(acc/(hi-lo));
  462. }
  463. }
  464. /* stability doesn't matter */
  465. static int comp(const void *a,const void *b){
  466. if(fabs(**(double **)a)<fabs(**(double **)b))
  467. return(1);
  468. else
  469. return(-1);
  470. }
  471. /* move ath and absolute masking to 'apply_floor' to avoid confusion
  472. with noise fitting and a floor that warbles due to bad LPC fit */
  473. static int frameno=-1;
  474. void _vp_compute_mask(vorbis_look_psy *p,double *f,
  475. double *flr,
  476. double *decay){
  477. double *work=alloca(sizeof(double)*p->n);
  478. double *work2=alloca(sizeof(double)*p->n);
  479. int i,n=p->n;
  480. double specmax=0.;
  481. frameno++;
  482. memset(flr,0,n*sizeof(double));
  483. for(i=0;i<n;i++)work[i]=fabs(f[i]);
  484. /* find the highest peak so we know the limits */
  485. for(i=0;i<n;i++){
  486. if(work[i]>specmax)specmax=work[i];
  487. }
  488. specmax=todB(specmax);
  489. /* don't use the smoothed data for noise */
  490. if(p->vi->noisemaskp){
  491. quarter_octave_noise(p,f,work2);
  492. seed_generic(p,p->noisecurves,work2,flr,specmax);
  493. }
  494. /* ... or peak att */
  495. if(p->vi->peakattp)
  496. seed_att(p,work,flr,specmax);
  497. if(p->vi->smoothp){
  498. /* compute power^.5 of three neighboring bins to smooth for peaks
  499. that get split twixt bins/peaks that nail the bin. This evens
  500. out treatment as we're not doing additive masking any longer. */
  501. double acc=work[0]*work[0]+work[1]*work[1];
  502. double prev=work[0];
  503. work[0]=sqrt(acc);
  504. for(i=1;i<n-1;i++){
  505. double this=work[i];
  506. acc+=work[i+1]*work[i+1];
  507. work[i]=sqrt(acc);
  508. acc-=prev*prev;
  509. prev=this;
  510. }
  511. work[n-1]=sqrt(acc);
  512. }
  513. /* seed the tone masking */
  514. if(p->vi->tonemaskp){
  515. if(p->vi->decayp){
  516. memset(work2,0,n*sizeof(double));
  517. seed_generic(p,p->tonecurves,work,work2,specmax);
  518. /* chase the seeds */
  519. max_seeds(p,flr);
  520. max_seeds(p,work2);
  521. /* compute, update and apply decay accumulator */
  522. compute_decay(p,work2,decay,n);
  523. for(i=0;i<n;i++)if(flr[i]<work2[i])flr[i]=work2[i];
  524. }else{
  525. seed_generic(p,p->tonecurves,work,flr,specmax);
  526. /* chase the seeds */
  527. max_seeds(p,flr);
  528. }
  529. }else{
  530. max_seeds(p,flr);
  531. }
  532. }
  533. /* this applies the floor and (optionally) tries to preserve noise
  534. energy in low resolution portions of the spectrum */
  535. /* f and flr are *linear* scale, not dB */
  536. void _vp_apply_floor(vorbis_look_psy *p,double *f, double *flr){
  537. double *work=alloca(p->n*sizeof(double));
  538. double thresh=fromdB(p->vi->noisefit_threshdB);
  539. int i,j,addcount=0;
  540. thresh*=thresh;
  541. /* subtract the floor */
  542. for(j=0;j<p->n;j++){
  543. if(flr[j]<=0)
  544. work[j]=0.;
  545. else
  546. work[j]=f[j]/flr[j];
  547. }
  548. /* mask off the ATH. This should be floating below specmax too, but
  549. for now, 0dB is fixed... */
  550. if(p->vi->athp)
  551. for(j=0;j<p->n;j++)
  552. if(fabs(f[j])<p->ath[j]){
  553. /* zeroes can cause rounding stability issues */
  554. if(f[j]>0)
  555. work[j]=.1;
  556. else
  557. if(f[j]<0)
  558. work[j]=-.1;
  559. }
  560. /* look at spectral energy levels. Noise is noise; sensation level
  561. is important */
  562. if(p->vi->noisefitp){
  563. double **index=alloca(p->vi->noisefit_subblock*sizeof(double *));
  564. /* we're looking for zero values that we want to reinstate (to
  565. floor level) in order to raise the SL noise level back closer
  566. to original. Desired result; the SL of each block being as
  567. close to (but still less than) the original as possible. Don't
  568. bother if the net result is a change of less than
  569. p->vi->noisefit_thresh dB */
  570. for(i=0;i<p->n;){
  571. double original_SL=0.;
  572. double current_SL=0.;
  573. int z=0;
  574. /* compute current SL */
  575. for(j=0;j<p->vi->noisefit_subblock && i<p->n;j++,i++){
  576. double y=(f[i]*f[i]);
  577. original_SL+=y;
  578. if(work[i]){
  579. current_SL+=y;
  580. }else{
  581. if(p->vi->athp){
  582. if(fabs(f[j])>=p->ath[j])index[z++]=f+i;
  583. }else
  584. index[z++]=f+i;
  585. }
  586. }
  587. /* sort the values below mask; add back the largest first, stop
  588. when we violate the desired result above (which may be
  589. immediately) */
  590. if(z && current_SL*thresh<original_SL){
  591. qsort(index,z,sizeof(double *),&comp);
  592. for(j=0;j<z;j++){
  593. int p=index[j]-f;
  594. double val=flr[p]*flr[p]+current_SL;
  595. if(val<original_SL){
  596. addcount++;
  597. if(f[p]>0)
  598. work[p]=1;
  599. else
  600. work[p]=-1;
  601. current_SL=val;
  602. }else
  603. break;
  604. }
  605. }
  606. }
  607. }
  608. memcpy(f,work,p->n*sizeof(double));
  609. }