psy.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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-2007 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: psychoacoustics not including preecho
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <math.h>
  17. #include <string.h>
  18. #include "vorbis/codec.h"
  19. #include "codec_internal.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. #define NEGINF -9999.f
  28. static const double stereo_threshholds[]={0.0, .5, 1.0, 1.5, 2.5, 4.5, 8.5, 16.5, 9e10};
  29. static const double stereo_threshholds_limited[]={0.0, .5, 1.0, 1.5, 2.0, 2.5, 4.5, 8.5, 9e10};
  30. vorbis_look_psy_global *_vp_global_look(vorbis_info *vi){
  31. codec_setup_info *ci=vi->codec_setup;
  32. vorbis_info_psy_global *gi=&ci->psy_g_param;
  33. vorbis_look_psy_global *look=_ogg_calloc(1,sizeof(*look));
  34. look->channels=vi->channels;
  35. look->ampmax=-9999.;
  36. look->gi=gi;
  37. return(look);
  38. }
  39. void _vp_global_free(vorbis_look_psy_global *look){
  40. if(look){
  41. memset(look,0,sizeof(*look));
  42. _ogg_free(look);
  43. }
  44. }
  45. void _vi_gpsy_free(vorbis_info_psy_global *i){
  46. if(i){
  47. memset(i,0,sizeof(*i));
  48. _ogg_free(i);
  49. }
  50. }
  51. void _vi_psy_free(vorbis_info_psy *i){
  52. if(i){
  53. memset(i,0,sizeof(*i));
  54. _ogg_free(i);
  55. }
  56. }
  57. static void min_curve(float *c,
  58. float *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 max_curve(float *c,
  63. float *c2){
  64. int i;
  65. for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
  66. }
  67. static void attenuate_curve(float *c,float att){
  68. int i;
  69. for(i=0;i<EHMER_MAX;i++)
  70. c[i]+=att;
  71. }
  72. static float ***setup_tone_curves(float curveatt_dB[P_BANDS],float binHz,int n,
  73. float center_boost, float center_decay_rate){
  74. int i,j,k,m;
  75. float ath[EHMER_MAX];
  76. float workc[P_BANDS][P_LEVELS][EHMER_MAX];
  77. float athc[P_LEVELS][EHMER_MAX];
  78. float *brute_buffer=alloca(n*sizeof(*brute_buffer));
  79. float ***ret=_ogg_malloc(sizeof(*ret)*P_BANDS);
  80. memset(workc,0,sizeof(workc));
  81. for(i=0;i<P_BANDS;i++){
  82. /* we add back in the ATH to avoid low level curves falling off to
  83. -infinity and unnecessarily cutting off high level curves in the
  84. curve limiting (last step). */
  85. /* A half-band's settings must be valid over the whole band, and
  86. it's better to mask too little than too much */
  87. int ath_offset=i*4;
  88. for(j=0;j<EHMER_MAX;j++){
  89. float min=999.;
  90. for(k=0;k<4;k++)
  91. if(j+k+ath_offset<MAX_ATH){
  92. if(min>ATH[j+k+ath_offset])min=ATH[j+k+ath_offset];
  93. }else{
  94. if(min>ATH[MAX_ATH-1])min=ATH[MAX_ATH-1];
  95. }
  96. ath[j]=min;
  97. }
  98. /* copy curves into working space, replicate the 50dB curve to 30
  99. and 40, replicate the 100dB curve to 110 */
  100. for(j=0;j<6;j++)
  101. memcpy(workc[i][j+2],tonemasks[i][j],EHMER_MAX*sizeof(*tonemasks[i][j]));
  102. memcpy(workc[i][0],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0]));
  103. memcpy(workc[i][1],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0]));
  104. /* apply centered curve boost/decay */
  105. for(j=0;j<P_LEVELS;j++){
  106. for(k=0;k<EHMER_MAX;k++){
  107. float adj=center_boost+abs(EHMER_OFFSET-k)*center_decay_rate;
  108. if(adj<0. && center_boost>0)adj=0.;
  109. if(adj>0. && center_boost<0)adj=0.;
  110. workc[i][j][k]+=adj;
  111. }
  112. }
  113. /* normalize curves so the driving amplitude is 0dB */
  114. /* make temp curves with the ATH overlayed */
  115. for(j=0;j<P_LEVELS;j++){
  116. attenuate_curve(workc[i][j],curveatt_dB[i]+100.-(j<2?2:j)*10.-P_LEVEL_0);
  117. memcpy(athc[j],ath,EHMER_MAX*sizeof(**athc));
  118. attenuate_curve(athc[j],+100.-j*10.f-P_LEVEL_0);
  119. max_curve(athc[j],workc[i][j]);
  120. }
  121. /* Now limit the louder curves.
  122. the idea is this: We don't know what the playback attenuation
  123. will be; 0dB SL moves every time the user twiddles the volume
  124. knob. So that means we have to use a single 'most pessimal' curve
  125. for all masking amplitudes, right? Wrong. The *loudest* sound
  126. can be in (we assume) a range of ...+100dB] SL. However, sounds
  127. 20dB down will be in a range ...+80], 40dB down is from ...+60],
  128. etc... */
  129. for(j=1;j<P_LEVELS;j++){
  130. min_curve(athc[j],athc[j-1]);
  131. min_curve(workc[i][j],athc[j]);
  132. }
  133. }
  134. for(i=0;i<P_BANDS;i++){
  135. int hi_curve,lo_curve,bin;
  136. ret[i]=_ogg_malloc(sizeof(**ret)*P_LEVELS);
  137. /* low frequency curves are measured with greater resolution than
  138. the MDCT/FFT will actually give us; we want the curve applied
  139. to the tone data to be pessimistic and thus apply the minimum
  140. masking possible for a given bin. That means that a single bin
  141. could span more than one octave and that the curve will be a
  142. composite of multiple octaves. It also may mean that a single
  143. bin may span > an eighth of an octave and that the eighth
  144. octave values may also be composited. */
  145. /* which octave curves will we be compositing? */
  146. bin=floor(fromOC(i*.5)/binHz);
  147. lo_curve= ceil(toOC(bin*binHz+1)*2);
  148. hi_curve= floor(toOC((bin+1)*binHz)*2);
  149. if(lo_curve>i)lo_curve=i;
  150. if(lo_curve<0)lo_curve=0;
  151. if(hi_curve>=P_BANDS)hi_curve=P_BANDS-1;
  152. for(m=0;m<P_LEVELS;m++){
  153. ret[i][m]=_ogg_malloc(sizeof(***ret)*(EHMER_MAX+2));
  154. for(j=0;j<n;j++)brute_buffer[j]=999.;
  155. /* render the curve into bins, then pull values back into curve.
  156. The point is that any inherent subsampling aliasing results in
  157. a safe minimum */
  158. for(k=lo_curve;k<=hi_curve;k++){
  159. int l=0;
  160. for(j=0;j<EHMER_MAX;j++){
  161. int lo_bin= fromOC(j*.125+k*.5-2.0625)/binHz;
  162. int hi_bin= fromOC(j*.125+k*.5-1.9375)/binHz+1;
  163. if(lo_bin<0)lo_bin=0;
  164. if(lo_bin>n)lo_bin=n;
  165. if(lo_bin<l)l=lo_bin;
  166. if(hi_bin<0)hi_bin=0;
  167. if(hi_bin>n)hi_bin=n;
  168. for(;l<hi_bin && l<n;l++)
  169. if(brute_buffer[l]>workc[k][m][j])
  170. brute_buffer[l]=workc[k][m][j];
  171. }
  172. for(;l<n;l++)
  173. if(brute_buffer[l]>workc[k][m][EHMER_MAX-1])
  174. brute_buffer[l]=workc[k][m][EHMER_MAX-1];
  175. }
  176. /* be equally paranoid about being valid up to next half ocatve */
  177. if(i+1<P_BANDS){
  178. int l=0;
  179. k=i+1;
  180. for(j=0;j<EHMER_MAX;j++){
  181. int lo_bin= fromOC(j*.125+i*.5-2.0625)/binHz;
  182. int hi_bin= fromOC(j*.125+i*.5-1.9375)/binHz+1;
  183. if(lo_bin<0)lo_bin=0;
  184. if(lo_bin>n)lo_bin=n;
  185. if(lo_bin<l)l=lo_bin;
  186. if(hi_bin<0)hi_bin=0;
  187. if(hi_bin>n)hi_bin=n;
  188. for(;l<hi_bin && l<n;l++)
  189. if(brute_buffer[l]>workc[k][m][j])
  190. brute_buffer[l]=workc[k][m][j];
  191. }
  192. for(;l<n;l++)
  193. if(brute_buffer[l]>workc[k][m][EHMER_MAX-1])
  194. brute_buffer[l]=workc[k][m][EHMER_MAX-1];
  195. }
  196. for(j=0;j<EHMER_MAX;j++){
  197. int bin=fromOC(j*.125+i*.5-2.)/binHz;
  198. if(bin<0){
  199. ret[i][m][j+2]=-999.;
  200. }else{
  201. if(bin>=n){
  202. ret[i][m][j+2]=-999.;
  203. }else{
  204. ret[i][m][j+2]=brute_buffer[bin];
  205. }
  206. }
  207. }
  208. /* add fenceposts */
  209. for(j=0;j<EHMER_OFFSET;j++)
  210. if(ret[i][m][j+2]>-200.f)break;
  211. ret[i][m][0]=j;
  212. for(j=EHMER_MAX-1;j>EHMER_OFFSET+1;j--)
  213. if(ret[i][m][j+2]>-200.f)
  214. break;
  215. ret[i][m][1]=j;
  216. }
  217. }
  218. return(ret);
  219. }
  220. void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,
  221. vorbis_info_psy_global *gi,int n,long rate){
  222. long i,j,lo=-99,hi=1;
  223. long maxoc;
  224. memset(p,0,sizeof(*p));
  225. p->eighth_octave_lines=gi->eighth_octave_lines;
  226. p->shiftoc=rint(log(gi->eighth_octave_lines*8.f)/log(2.f))-1;
  227. p->firstoc=toOC(.25f*rate*.5/n)*(1<<(p->shiftoc+1))-gi->eighth_octave_lines;
  228. maxoc=toOC((n+.25f)*rate*.5/n)*(1<<(p->shiftoc+1))+.5f;
  229. p->total_octave_lines=maxoc-p->firstoc+1;
  230. p->ath=_ogg_malloc(n*sizeof(*p->ath));
  231. p->octave=_ogg_malloc(n*sizeof(*p->octave));
  232. p->bark=_ogg_malloc(n*sizeof(*p->bark));
  233. p->vi=vi;
  234. p->n=n;
  235. p->rate=rate;
  236. /* AoTuV HF weighting */
  237. p->m_val = 1.;
  238. if(rate < 26000) p->m_val = 0;
  239. else if(rate < 38000) p->m_val = .94; /* 32kHz */
  240. else if(rate > 46000) p->m_val = 1.275; /* 48kHz */
  241. /* set up the lookups for a given blocksize and sample rate */
  242. for(i=0,j=0;i<MAX_ATH-1;i++){
  243. int endpos=rint(fromOC((i+1)*.125-2.)*2*n/rate);
  244. float base=ATH[i];
  245. if(j<endpos){
  246. float delta=(ATH[i+1]-base)/(endpos-j);
  247. for(;j<endpos && j<n;j++){
  248. p->ath[j]=base+100.;
  249. base+=delta;
  250. }
  251. }
  252. }
  253. for(;j<n;j++){
  254. p->ath[j]=p->ath[j-1];
  255. }
  256. for(i=0;i<n;i++){
  257. float bark=toBARK(rate/(2*n)*i);
  258. for(;lo+vi->noisewindowlomin<i &&
  259. toBARK(rate/(2*n)*lo)<(bark-vi->noisewindowlo);lo++);
  260. for(;hi<=n && (hi<i+vi->noisewindowhimin ||
  261. toBARK(rate/(2*n)*hi)<(bark+vi->noisewindowhi));hi++);
  262. p->bark[i]=((lo-1)<<16)+(hi-1);
  263. }
  264. for(i=0;i<n;i++)
  265. p->octave[i]=toOC((i+.25f)*.5*rate/n)*(1<<(p->shiftoc+1))+.5f;
  266. p->tonecurves=setup_tone_curves(vi->toneatt,rate*.5/n,n,
  267. vi->tone_centerboost,vi->tone_decay);
  268. /* set up rolling noise median */
  269. p->noiseoffset=_ogg_malloc(P_NOISECURVES*sizeof(*p->noiseoffset));
  270. for(i=0;i<P_NOISECURVES;i++)
  271. p->noiseoffset[i]=_ogg_malloc(n*sizeof(**p->noiseoffset));
  272. for(i=0;i<n;i++){
  273. float halfoc=toOC((i+.5)*rate/(2.*n))*2.;
  274. int inthalfoc;
  275. float del;
  276. if(halfoc<0)halfoc=0;
  277. if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
  278. inthalfoc=(int)halfoc;
  279. del=halfoc-inthalfoc;
  280. for(j=0;j<P_NOISECURVES;j++)
  281. p->noiseoffset[j][i]=
  282. p->vi->noiseoff[j][inthalfoc]*(1.-del) +
  283. p->vi->noiseoff[j][inthalfoc+1]*del;
  284. }
  285. #if 0
  286. {
  287. static int ls=0;
  288. _analysis_output_always("noiseoff0",ls,p->noiseoffset[0],n,1,0,0);
  289. _analysis_output_always("noiseoff1",ls,p->noiseoffset[1],n,1,0,0);
  290. _analysis_output_always("noiseoff2",ls++,p->noiseoffset[2],n,1,0,0);
  291. }
  292. #endif
  293. }
  294. void _vp_psy_clear(vorbis_look_psy *p){
  295. int i,j;
  296. if(p){
  297. if(p->ath)_ogg_free(p->ath);
  298. if(p->octave)_ogg_free(p->octave);
  299. if(p->bark)_ogg_free(p->bark);
  300. if(p->tonecurves){
  301. for(i=0;i<P_BANDS;i++){
  302. for(j=0;j<P_LEVELS;j++){
  303. _ogg_free(p->tonecurves[i][j]);
  304. }
  305. _ogg_free(p->tonecurves[i]);
  306. }
  307. _ogg_free(p->tonecurves);
  308. }
  309. if(p->noiseoffset){
  310. for(i=0;i<P_NOISECURVES;i++){
  311. _ogg_free(p->noiseoffset[i]);
  312. }
  313. _ogg_free(p->noiseoffset);
  314. }
  315. memset(p,0,sizeof(*p));
  316. }
  317. }
  318. /* octave/(8*eighth_octave_lines) x scale and dB y scale */
  319. static void seed_curve(float *seed,
  320. const float **curves,
  321. float amp,
  322. int oc, int n,
  323. int linesper,float dBoffset){
  324. int i,post1;
  325. int seedptr;
  326. const float *posts,*curve;
  327. int choice=(int)((amp+dBoffset-P_LEVEL_0)*.1f);
  328. choice=max(choice,0);
  329. choice=min(choice,P_LEVELS-1);
  330. posts=curves[choice];
  331. curve=posts+2;
  332. post1=(int)posts[1];
  333. seedptr=oc+(posts[0]-EHMER_OFFSET)*linesper-(linesper>>1);
  334. for(i=posts[0];i<post1;i++){
  335. if(seedptr>0){
  336. float lin=amp+curve[i];
  337. if(seed[seedptr]<lin)seed[seedptr]=lin;
  338. }
  339. seedptr+=linesper;
  340. if(seedptr>=n)break;
  341. }
  342. }
  343. static void seed_loop(vorbis_look_psy *p,
  344. const float ***curves,
  345. const float *f,
  346. const float *flr,
  347. float *seed,
  348. float specmax){
  349. vorbis_info_psy *vi=p->vi;
  350. long n=p->n,i;
  351. float dBoffset=vi->max_curve_dB-specmax;
  352. /* prime the working vector with peak values */
  353. for(i=0;i<n;i++){
  354. float max=f[i];
  355. long oc=p->octave[i];
  356. while(i+1<n && p->octave[i+1]==oc){
  357. i++;
  358. if(f[i]>max)max=f[i];
  359. }
  360. if(max+6.f>flr[i]){
  361. oc=oc>>p->shiftoc;
  362. if(oc>=P_BANDS)oc=P_BANDS-1;
  363. if(oc<0)oc=0;
  364. seed_curve(seed,
  365. curves[oc],
  366. max,
  367. p->octave[i]-p->firstoc,
  368. p->total_octave_lines,
  369. p->eighth_octave_lines,
  370. dBoffset);
  371. }
  372. }
  373. }
  374. static void seed_chase(float *seeds, int linesper, long n){
  375. long *posstack=alloca(n*sizeof(*posstack));
  376. float *ampstack=alloca(n*sizeof(*ampstack));
  377. long stack=0;
  378. long pos=0;
  379. long i;
  380. for(i=0;i<n;i++){
  381. if(stack<2){
  382. posstack[stack]=i;
  383. ampstack[stack++]=seeds[i];
  384. }else{
  385. while(1){
  386. if(seeds[i]<ampstack[stack-1]){
  387. posstack[stack]=i;
  388. ampstack[stack++]=seeds[i];
  389. break;
  390. }else{
  391. if(i<posstack[stack-1]+linesper){
  392. if(stack>1 && ampstack[stack-1]<=ampstack[stack-2] &&
  393. i<posstack[stack-2]+linesper){
  394. /* we completely overlap, making stack-1 irrelevant. pop it */
  395. stack--;
  396. continue;
  397. }
  398. }
  399. posstack[stack]=i;
  400. ampstack[stack++]=seeds[i];
  401. break;
  402. }
  403. }
  404. }
  405. }
  406. /* the stack now contains only the positions that are relevant. Scan
  407. 'em straight through */
  408. for(i=0;i<stack;i++){
  409. long endpos;
  410. if(i<stack-1 && ampstack[i+1]>ampstack[i]){
  411. endpos=posstack[i+1];
  412. }else{
  413. endpos=posstack[i]+linesper+1; /* +1 is important, else bin 0 is
  414. discarded in short frames */
  415. }
  416. if(endpos>n)endpos=n;
  417. for(;pos<endpos;pos++)
  418. seeds[pos]=ampstack[i];
  419. }
  420. /* there. Linear time. I now remember this was on a problem set I
  421. had in Grad Skool... I didn't solve it at the time ;-) */
  422. }
  423. /* bleaugh, this is more complicated than it needs to be */
  424. #include<stdio.h>
  425. static void max_seeds(vorbis_look_psy *p,
  426. float *seed,
  427. float *flr){
  428. long n=p->total_octave_lines;
  429. int linesper=p->eighth_octave_lines;
  430. long linpos=0;
  431. long pos;
  432. seed_chase(seed,linesper,n); /* for masking */
  433. pos=p->octave[0]-p->firstoc-(linesper>>1);
  434. while(linpos+1<p->n){
  435. float minV=seed[pos];
  436. long end=((p->octave[linpos]+p->octave[linpos+1])>>1)-p->firstoc;
  437. if(minV>p->vi->tone_abs_limit)minV=p->vi->tone_abs_limit;
  438. while(pos+1<=end){
  439. pos++;
  440. if((seed[pos]>NEGINF && seed[pos]<minV) || minV==NEGINF)
  441. minV=seed[pos];
  442. }
  443. end=pos+p->firstoc;
  444. for(;linpos<p->n && p->octave[linpos]<=end;linpos++)
  445. if(flr[linpos]<minV)flr[linpos]=minV;
  446. }
  447. {
  448. float minV=seed[p->total_octave_lines-1];
  449. for(;linpos<p->n;linpos++)
  450. if(flr[linpos]<minV)flr[linpos]=minV;
  451. }
  452. }
  453. static void bark_noise_hybridmp(int n,const long *b,
  454. const float *f,
  455. float *noise,
  456. const float offset,
  457. const int fixed){
  458. float *N=alloca(n*sizeof(*N));
  459. float *X=alloca(n*sizeof(*N));
  460. float *XX=alloca(n*sizeof(*N));
  461. float *Y=alloca(n*sizeof(*N));
  462. float *XY=alloca(n*sizeof(*N));
  463. float tN, tX, tXX, tY, tXY;
  464. int i;
  465. int lo, hi;
  466. float R=0.f;
  467. float A=0.f;
  468. float B=0.f;
  469. float D=1.f;
  470. float w, x, y;
  471. tN = tX = tXX = tY = tXY = 0.f;
  472. y = f[0] + offset;
  473. if (y < 1.f) y = 1.f;
  474. w = y * y * .5;
  475. tN += w;
  476. tX += w;
  477. tY += w * y;
  478. N[0] = tN;
  479. X[0] = tX;
  480. XX[0] = tXX;
  481. Y[0] = tY;
  482. XY[0] = tXY;
  483. for (i = 1, x = 1.f; i < n; i++, x += 1.f) {
  484. y = f[i] + offset;
  485. if (y < 1.f) y = 1.f;
  486. w = y * y;
  487. tN += w;
  488. tX += w * x;
  489. tXX += w * x * x;
  490. tY += w * y;
  491. tXY += w * x * y;
  492. N[i] = tN;
  493. X[i] = tX;
  494. XX[i] = tXX;
  495. Y[i] = tY;
  496. XY[i] = tXY;
  497. }
  498. for (i = 0, x = 0.f;; i++, x += 1.f) {
  499. lo = b[i] >> 16;
  500. if( lo>=0 ) break;
  501. hi = b[i] & 0xffff;
  502. tN = N[hi] + N[-lo];
  503. tX = X[hi] - X[-lo];
  504. tXX = XX[hi] + XX[-lo];
  505. tY = Y[hi] + Y[-lo];
  506. tXY = XY[hi] - XY[-lo];
  507. A = tY * tXX - tX * tXY;
  508. B = tN * tXY - tX * tY;
  509. D = tN * tXX - tX * tX;
  510. R = (A + x * B) / D;
  511. if (R < 0.f)
  512. R = 0.f;
  513. noise[i] = R - offset;
  514. }
  515. for ( ;; i++, x += 1.f) {
  516. lo = b[i] >> 16;
  517. hi = b[i] & 0xffff;
  518. if(hi>=n)break;
  519. tN = N[hi] - N[lo];
  520. tX = X[hi] - X[lo];
  521. tXX = XX[hi] - XX[lo];
  522. tY = Y[hi] - Y[lo];
  523. tXY = XY[hi] - XY[lo];
  524. A = tY * tXX - tX * tXY;
  525. B = tN * tXY - tX * tY;
  526. D = tN * tXX - tX * tX;
  527. R = (A + x * B) / D;
  528. if (R < 0.f) R = 0.f;
  529. noise[i] = R - offset;
  530. }
  531. for ( ; i < n; i++, x += 1.f) {
  532. R = (A + x * B) / D;
  533. if (R < 0.f) R = 0.f;
  534. noise[i] = R - offset;
  535. }
  536. if (fixed <= 0) return;
  537. for (i = 0, x = 0.f;; i++, x += 1.f) {
  538. hi = i + fixed / 2;
  539. lo = hi - fixed;
  540. if(lo>=0)break;
  541. tN = N[hi] + N[-lo];
  542. tX = X[hi] - X[-lo];
  543. tXX = XX[hi] + XX[-lo];
  544. tY = Y[hi] + Y[-lo];
  545. tXY = XY[hi] - XY[-lo];
  546. A = tY * tXX - tX * tXY;
  547. B = tN * tXY - tX * tY;
  548. D = tN * tXX - tX * tX;
  549. R = (A + x * B) / D;
  550. if (R - offset < noise[i]) noise[i] = R - offset;
  551. }
  552. for ( ;; i++, x += 1.f) {
  553. hi = i + fixed / 2;
  554. lo = hi - fixed;
  555. if(hi>=n)break;
  556. tN = N[hi] - N[lo];
  557. tX = X[hi] - X[lo];
  558. tXX = XX[hi] - XX[lo];
  559. tY = Y[hi] - Y[lo];
  560. tXY = XY[hi] - XY[lo];
  561. A = tY * tXX - tX * tXY;
  562. B = tN * tXY - tX * tY;
  563. D = tN * tXX - tX * tX;
  564. R = (A + x * B) / D;
  565. if (R - offset < noise[i]) noise[i] = R - offset;
  566. }
  567. for ( ; i < n; i++, x += 1.f) {
  568. R = (A + x * B) / D;
  569. if (R - offset < noise[i]) noise[i] = R - offset;
  570. }
  571. }
  572. static const float FLOOR1_fromdB_INV_LOOKUP[256]={
  573. 0.F, 8.81683e+06F, 8.27882e+06F, 7.77365e+06F,
  574. 7.29930e+06F, 6.85389e+06F, 6.43567e+06F, 6.04296e+06F,
  575. 5.67422e+06F, 5.32798e+06F, 5.00286e+06F, 4.69759e+06F,
  576. 4.41094e+06F, 4.14178e+06F, 3.88905e+06F, 3.65174e+06F,
  577. 3.42891e+06F, 3.21968e+06F, 3.02321e+06F, 2.83873e+06F,
  578. 2.66551e+06F, 2.50286e+06F, 2.35014e+06F, 2.20673e+06F,
  579. 2.07208e+06F, 1.94564e+06F, 1.82692e+06F, 1.71544e+06F,
  580. 1.61076e+06F, 1.51247e+06F, 1.42018e+06F, 1.33352e+06F,
  581. 1.25215e+06F, 1.17574e+06F, 1.10400e+06F, 1.03663e+06F,
  582. 973377.F, 913981.F, 858210.F, 805842.F,
  583. 756669.F, 710497.F, 667142.F, 626433.F,
  584. 588208.F, 552316.F, 518613.F, 486967.F,
  585. 457252.F, 429351.F, 403152.F, 378551.F,
  586. 355452.F, 333762.F, 313396.F, 294273.F,
  587. 276316.F, 259455.F, 243623.F, 228757.F,
  588. 214798.F, 201691.F, 189384.F, 177828.F,
  589. 166977.F, 156788.F, 147221.F, 138237.F,
  590. 129802.F, 121881.F, 114444.F, 107461.F,
  591. 100903.F, 94746.3F, 88964.9F, 83536.2F,
  592. 78438.8F, 73652.5F, 69158.2F, 64938.1F,
  593. 60975.6F, 57254.9F, 53761.2F, 50480.6F,
  594. 47400.3F, 44507.9F, 41792.0F, 39241.9F,
  595. 36847.3F, 34598.9F, 32487.7F, 30505.3F,
  596. 28643.8F, 26896.0F, 25254.8F, 23713.7F,
  597. 22266.7F, 20908.0F, 19632.2F, 18434.2F,
  598. 17309.4F, 16253.1F, 15261.4F, 14330.1F,
  599. 13455.7F, 12634.6F, 11863.7F, 11139.7F,
  600. 10460.0F, 9821.72F, 9222.39F, 8659.64F,
  601. 8131.23F, 7635.06F, 7169.17F, 6731.70F,
  602. 6320.93F, 5935.23F, 5573.06F, 5232.99F,
  603. 4913.67F, 4613.84F, 4332.30F, 4067.94F,
  604. 3819.72F, 3586.64F, 3367.78F, 3162.28F,
  605. 2969.31F, 2788.13F, 2617.99F, 2458.24F,
  606. 2308.24F, 2167.39F, 2035.14F, 1910.95F,
  607. 1794.35F, 1684.85F, 1582.04F, 1485.51F,
  608. 1394.86F, 1309.75F, 1229.83F, 1154.78F,
  609. 1084.32F, 1018.15F, 956.024F, 897.687F,
  610. 842.910F, 791.475F, 743.179F, 697.830F,
  611. 655.249F, 615.265F, 577.722F, 542.469F,
  612. 509.367F, 478.286F, 449.101F, 421.696F,
  613. 395.964F, 371.803F, 349.115F, 327.812F,
  614. 307.809F, 289.026F, 271.390F, 254.830F,
  615. 239.280F, 224.679F, 210.969F, 198.096F,
  616. 186.008F, 174.658F, 164.000F, 153.993F,
  617. 144.596F, 135.773F, 127.488F, 119.708F,
  618. 112.404F, 105.545F, 99.1046F, 93.0572F,
  619. 87.3788F, 82.0469F, 77.0404F, 72.3394F,
  620. 67.9252F, 63.7804F, 59.8885F, 56.2341F,
  621. 52.8027F, 49.5807F, 46.5553F, 43.7144F,
  622. 41.0470F, 38.5423F, 36.1904F, 33.9821F,
  623. 31.9085F, 29.9614F, 28.1332F, 26.4165F,
  624. 24.8045F, 23.2910F, 21.8697F, 20.5352F,
  625. 19.2822F, 18.1056F, 17.0008F, 15.9634F,
  626. 14.9893F, 14.0746F, 13.2158F, 12.4094F,
  627. 11.6522F, 10.9411F, 10.2735F, 9.64662F,
  628. 9.05798F, 8.50526F, 7.98626F, 7.49894F,
  629. 7.04135F, 6.61169F, 6.20824F, 5.82941F,
  630. 5.47370F, 5.13970F, 4.82607F, 4.53158F,
  631. 4.25507F, 3.99542F, 3.75162F, 3.52269F,
  632. 3.30774F, 3.10590F, 2.91638F, 2.73842F,
  633. 2.57132F, 2.41442F, 2.26709F, 2.12875F,
  634. 1.99885F, 1.87688F, 1.76236F, 1.65482F,
  635. 1.55384F, 1.45902F, 1.36999F, 1.28640F,
  636. 1.20790F, 1.13419F, 1.06499F, 1.F
  637. };
  638. void _vp_remove_floor(vorbis_look_psy *p,
  639. float *mdct,
  640. int *codedflr,
  641. float *residue,
  642. int sliding_lowpass){
  643. int i,n=p->n;
  644. if(sliding_lowpass>n)sliding_lowpass=n;
  645. for(i=0;i<sliding_lowpass;i++){
  646. residue[i]=
  647. mdct[i]*FLOOR1_fromdB_INV_LOOKUP[codedflr[i]];
  648. }
  649. for(;i<n;i++)
  650. residue[i]=0.;
  651. }
  652. void _vp_noisemask(vorbis_look_psy *p,
  653. float *logmdct,
  654. float *logmask){
  655. int i,n=p->n;
  656. float *work=alloca(n*sizeof(*work));
  657. bark_noise_hybridmp(n,p->bark,logmdct,logmask,
  658. 140.,-1);
  659. for(i=0;i<n;i++)work[i]=logmdct[i]-logmask[i];
  660. bark_noise_hybridmp(n,p->bark,work,logmask,0.,
  661. p->vi->noisewindowfixed);
  662. for(i=0;i<n;i++)work[i]=logmdct[i]-work[i];
  663. #if 0
  664. {
  665. static int seq=0;
  666. float work2[n];
  667. for(i=0;i<n;i++){
  668. work2[i]=logmask[i]+work[i];
  669. }
  670. if(seq&1)
  671. _analysis_output("median2R",seq/2,work,n,1,0,0);
  672. else
  673. _analysis_output("median2L",seq/2,work,n,1,0,0);
  674. if(seq&1)
  675. _analysis_output("envelope2R",seq/2,work2,n,1,0,0);
  676. else
  677. _analysis_output("envelope2L",seq/2,work2,n,1,0,0);
  678. seq++;
  679. }
  680. #endif
  681. for(i=0;i<n;i++){
  682. int dB=logmask[i]+.5;
  683. if(dB>=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1;
  684. if(dB<0)dB=0;
  685. logmask[i]= work[i]+p->vi->noisecompand[dB];
  686. }
  687. }
  688. void _vp_tonemask(vorbis_look_psy *p,
  689. float *logfft,
  690. float *logmask,
  691. float global_specmax,
  692. float local_specmax){
  693. int i,n=p->n;
  694. float *seed=alloca(sizeof(*seed)*p->total_octave_lines);
  695. float att=local_specmax+p->vi->ath_adjatt;
  696. for(i=0;i<p->total_octave_lines;i++)seed[i]=NEGINF;
  697. /* set the ATH (floating below localmax, not global max by a
  698. specified att) */
  699. if(att<p->vi->ath_maxatt)att=p->vi->ath_maxatt;
  700. for(i=0;i<n;i++)
  701. logmask[i]=p->ath[i]+att;
  702. /* tone masking */
  703. seed_loop(p,(const float ***)p->tonecurves,logfft,logmask,seed,global_specmax);
  704. max_seeds(p,seed,logmask);
  705. }
  706. void _vp_offset_and_mix(vorbis_look_psy *p,
  707. float *noise,
  708. float *tone,
  709. int offset_select,
  710. float *logmask,
  711. float *mdct,
  712. float *logmdct){
  713. int i,n=p->n;
  714. float de, coeffi, cx;/* AoTuV */
  715. float toneatt=p->vi->tone_masteratt[offset_select];
  716. cx = p->m_val;
  717. for(i=0;i<n;i++){
  718. float val= noise[i]+p->noiseoffset[offset_select][i];
  719. if(val>p->vi->noisemaxsupp)val=p->vi->noisemaxsupp;
  720. logmask[i]=max(val,tone[i]+toneatt);
  721. /* AoTuV */
  722. /** @ M1 **
  723. The following codes improve a noise problem.
  724. A fundamental idea uses the value of masking and carries out
  725. the relative compensation of the MDCT.
  726. However, this code is not perfect and all noise problems cannot be solved.
  727. by Aoyumi @ 2004/04/18
  728. */
  729. if(offset_select == 1) {
  730. coeffi = -17.2; /* coeffi is a -17.2dB threshold */
  731. val = val - logmdct[i]; /* val == mdct line value relative to floor in dB */
  732. if(val > coeffi){
  733. /* mdct value is > -17.2 dB below floor */
  734. de = 1.0-((val-coeffi)*0.005*cx);
  735. /* pro-rated attenuation:
  736. -0.00 dB boost if mdct value is -17.2dB (relative to floor)
  737. -0.77 dB boost if mdct value is 0dB (relative to floor)
  738. -1.64 dB boost if mdct value is +17.2dB (relative to floor)
  739. etc... */
  740. if(de < 0) de = 0.0001;
  741. }else
  742. /* mdct value is <= -17.2 dB below floor */
  743. de = 1.0-((val-coeffi)*0.0003*cx);
  744. /* pro-rated attenuation:
  745. +0.00 dB atten if mdct value is -17.2dB (relative to floor)
  746. +0.45 dB atten if mdct value is -34.4dB (relative to floor)
  747. etc... */
  748. mdct[i] *= de;
  749. }
  750. }
  751. }
  752. float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd){
  753. vorbis_info *vi=vd->vi;
  754. codec_setup_info *ci=vi->codec_setup;
  755. vorbis_info_psy_global *gi=&ci->psy_g_param;
  756. int n=ci->blocksizes[vd->W]/2;
  757. float secs=(float)n/vi->rate;
  758. amp+=secs*gi->ampmax_att_per_sec;
  759. if(amp<-9999)amp=-9999;
  760. return(amp);
  761. }
  762. static void couple_lossless(float A, float B,
  763. float *qA, float *qB){
  764. int test1=fabs(*qA)>fabs(*qB);
  765. test1-= fabs(*qA)<fabs(*qB);
  766. if(!test1)test1=((fabs(A)>fabs(B))<<1)-1;
  767. if(test1==1){
  768. *qB=(*qA>0.f?*qA-*qB:*qB-*qA);
  769. }else{
  770. float temp=*qB;
  771. *qB=(*qB>0.f?*qA-*qB:*qB-*qA);
  772. *qA=temp;
  773. }
  774. if(*qB>fabs(*qA)*1.9999f){
  775. *qB= -fabs(*qA)*2.f;
  776. *qA= -*qA;
  777. }
  778. }
  779. static const float hypot_lookup[32]={
  780. -0.009935, -0.011245, -0.012726, -0.014397,
  781. -0.016282, -0.018407, -0.020800, -0.023494,
  782. -0.026522, -0.029923, -0.033737, -0.038010,
  783. -0.042787, -0.048121, -0.054064, -0.060671,
  784. -0.068000, -0.076109, -0.085054, -0.094892,
  785. -0.105675, -0.117451, -0.130260, -0.144134,
  786. -0.159093, -0.175146, -0.192286, -0.210490,
  787. -0.229718, -0.249913, -0.271001, -0.292893};
  788. static void precomputed_couple_point(float premag,
  789. int floorA,int floorB,
  790. float *mag, float *ang){
  791. int test=(floorA>floorB)-1;
  792. int offset=31-abs(floorA-floorB);
  793. float floormag=hypot_lookup[((offset<0)-1)&offset]+1.f;
  794. floormag*=FLOOR1_fromdB_INV_LOOKUP[(floorB&test)|(floorA&(~test))];
  795. *mag=premag*floormag;
  796. *ang=0.f;
  797. }
  798. /* just like below, this is currently set up to only do
  799. single-step-depth coupling. Otherwise, we'd have to do more
  800. copying (which will be inevitable later) */
  801. /* doing the real circular magnitude calculation is audibly superior
  802. to (A+B)/sqrt(2) */
  803. static float dipole_hypot(float a, float b){
  804. if(a>0.){
  805. if(b>0.)return sqrt(a*a+b*b);
  806. if(a>-b)return sqrt(a*a-b*b);
  807. return -sqrt(b*b-a*a);
  808. }
  809. if(b<0.)return -sqrt(a*a+b*b);
  810. if(-a>b)return -sqrt(a*a-b*b);
  811. return sqrt(b*b-a*a);
  812. }
  813. static float round_hypot(float a, float b){
  814. if(a>0.){
  815. if(b>0.)return sqrt(a*a+b*b);
  816. if(a>-b)return sqrt(a*a+b*b);
  817. return -sqrt(b*b+a*a);
  818. }
  819. if(b<0.)return -sqrt(a*a+b*b);
  820. if(-a>b)return -sqrt(a*a+b*b);
  821. return sqrt(b*b+a*a);
  822. }
  823. /* revert to round hypot for now */
  824. float **_vp_quantize_couple_memo(vorbis_block *vb,
  825. vorbis_info_psy_global *g,
  826. vorbis_look_psy *p,
  827. vorbis_info_mapping0 *vi,
  828. float **mdct){
  829. int i,j,n=p->n;
  830. float **ret=_vorbis_block_alloc(vb,vi->coupling_steps*sizeof(*ret));
  831. int limit=g->coupling_pointlimit[p->vi->blockflag][PACKETBLOBS/2];
  832. for(i=0;i<vi->coupling_steps;i++){
  833. float *mdctM=mdct[vi->coupling_mag[i]];
  834. float *mdctA=mdct[vi->coupling_ang[i]];
  835. ret[i]=_vorbis_block_alloc(vb,n*sizeof(**ret));
  836. for(j=0;j<limit;j++)
  837. ret[i][j]=dipole_hypot(mdctM[j],mdctA[j]);
  838. for(;j<n;j++)
  839. ret[i][j]=round_hypot(mdctM[j],mdctA[j]);
  840. }
  841. return(ret);
  842. }
  843. /* this is for per-channel noise normalization */
  844. static int apsort(const void *a, const void *b){
  845. float f1=fabs(**(float**)a);
  846. float f2=fabs(**(float**)b);
  847. return (f1<f2)-(f1>f2);
  848. }
  849. int **_vp_quantize_couple_sort(vorbis_block *vb,
  850. vorbis_look_psy *p,
  851. vorbis_info_mapping0 *vi,
  852. float **mags){
  853. if(p->vi->normal_point_p){
  854. int i,j,k,n=p->n;
  855. int **ret=_vorbis_block_alloc(vb,vi->coupling_steps*sizeof(*ret));
  856. int partition=p->vi->normal_partition;
  857. float **work=alloca(sizeof(*work)*partition);
  858. for(i=0;i<vi->coupling_steps;i++){
  859. ret[i]=_vorbis_block_alloc(vb,n*sizeof(**ret));
  860. for(j=0;j<n;j+=partition){
  861. for(k=0;k<partition;k++)work[k]=mags[i]+k+j;
  862. qsort(work,partition,sizeof(*work),apsort);
  863. for(k=0;k<partition;k++)ret[i][k+j]=work[k]-mags[i];
  864. }
  865. }
  866. return(ret);
  867. }
  868. return(NULL);
  869. }
  870. void _vp_noise_normalize_sort(vorbis_look_psy *p,
  871. float *magnitudes,int *sortedindex){
  872. int i,j,n=p->n;
  873. vorbis_info_psy *vi=p->vi;
  874. int partition=vi->normal_partition;
  875. float **work=alloca(sizeof(*work)*partition);
  876. int start=vi->normal_start;
  877. for(j=start;j<n;j+=partition){
  878. if(j+partition>n)partition=n-j;
  879. for(i=0;i<partition;i++)work[i]=magnitudes+i+j;
  880. qsort(work,partition,sizeof(*work),apsort);
  881. for(i=0;i<partition;i++){
  882. sortedindex[i+j-start]=work[i]-magnitudes;
  883. }
  884. }
  885. }
  886. void _vp_noise_normalize(vorbis_look_psy *p,
  887. float *in,float *out,int *sortedindex){
  888. int flag=0,i,j=0,n=p->n;
  889. vorbis_info_psy *vi=p->vi;
  890. int partition=vi->normal_partition;
  891. int start=vi->normal_start;
  892. if(start>n)start=n;
  893. if(vi->normal_channel_p){
  894. for(;j<start;j++)
  895. out[j]=rint(in[j]);
  896. for(;j+partition<=n;j+=partition){
  897. float acc=0.;
  898. int k;
  899. for(i=j;i<j+partition;i++)
  900. acc+=in[i]*in[i];
  901. for(i=0;i<partition;i++){
  902. k=sortedindex[i+j-start];
  903. if(in[k]*in[k]>=.25f){
  904. out[k]=rint(in[k]);
  905. acc-=in[k]*in[k];
  906. flag=1;
  907. }else{
  908. if(acc<vi->normal_thresh)break;
  909. out[k]=unitnorm(in[k]);
  910. acc-=1.;
  911. }
  912. }
  913. for(;i<partition;i++){
  914. k=sortedindex[i+j-start];
  915. out[k]=0.;
  916. }
  917. }
  918. }
  919. for(;j<n;j++)
  920. out[j]=rint(in[j]);
  921. }
  922. void _vp_couple(int blobno,
  923. vorbis_info_psy_global *g,
  924. vorbis_look_psy *p,
  925. vorbis_info_mapping0 *vi,
  926. float **res,
  927. float **mag_memo,
  928. int **mag_sort,
  929. int **ifloor,
  930. int *nonzero,
  931. int sliding_lowpass){
  932. int i,j,k,n=p->n;
  933. /* perform any requested channel coupling */
  934. /* point stereo can only be used in a first stage (in this encoder)
  935. because of the dependency on floor lookups */
  936. for(i=0;i<vi->coupling_steps;i++){
  937. /* once we're doing multistage coupling in which a channel goes
  938. through more than one coupling step, the floor vector
  939. magnitudes will also have to be recalculated an propogated
  940. along with PCM. Right now, we're not (that will wait until 5.1
  941. most likely), so the code isn't here yet. The memory management
  942. here is all assuming single depth couplings anyway. */
  943. /* make sure coupling a zero and a nonzero channel results in two
  944. nonzero channels. */
  945. if(nonzero[vi->coupling_mag[i]] ||
  946. nonzero[vi->coupling_ang[i]]){
  947. float *rM=res[vi->coupling_mag[i]];
  948. float *rA=res[vi->coupling_ang[i]];
  949. float *qM=rM+n;
  950. float *qA=rA+n;
  951. int *floorM=ifloor[vi->coupling_mag[i]];
  952. int *floorA=ifloor[vi->coupling_ang[i]];
  953. float prepoint=stereo_threshholds[g->coupling_prepointamp[blobno]];
  954. float postpoint=stereo_threshholds[g->coupling_postpointamp[blobno]];
  955. int partition=(p->vi->normal_point_p?p->vi->normal_partition:p->n);
  956. int limit=g->coupling_pointlimit[p->vi->blockflag][blobno];
  957. int pointlimit=limit;
  958. nonzero[vi->coupling_mag[i]]=1;
  959. nonzero[vi->coupling_ang[i]]=1;
  960. /* The threshold of a stereo is changed with the size of n */
  961. if(n > 1000)
  962. postpoint=stereo_threshholds_limited[g->coupling_postpointamp[blobno]];
  963. for(j=0;j<p->n;j+=partition){
  964. float acc=0.f;
  965. for(k=0;k<partition;k++){
  966. int l=k+j;
  967. if(l<sliding_lowpass){
  968. if((l>=limit && fabs(rM[l])<postpoint && fabs(rA[l])<postpoint) ||
  969. (fabs(rM[l])<prepoint && fabs(rA[l])<prepoint)){
  970. precomputed_couple_point(mag_memo[i][l],
  971. floorM[l],floorA[l],
  972. qM+l,qA+l);
  973. if(rint(qM[l])==0.f)acc+=qM[l]*qM[l];
  974. }else{
  975. couple_lossless(rM[l],rA[l],qM+l,qA+l);
  976. }
  977. }else{
  978. qM[l]=0.;
  979. qA[l]=0.;
  980. }
  981. }
  982. if(p->vi->normal_point_p){
  983. for(k=0;k<partition && acc>=p->vi->normal_thresh;k++){
  984. int l=mag_sort[i][j+k];
  985. if(l<sliding_lowpass && l>=pointlimit && rint(qM[l])==0.f){
  986. qM[l]=unitnorm(qM[l]);
  987. acc-=1.f;
  988. }
  989. }
  990. }
  991. }
  992. }
  993. }
  994. }
  995. /* AoTuV */
  996. /** @ M2 **
  997. The boost problem by the combination of noise normalization and point stereo is eased.
  998. However, this is a temporary patch.
  999. by Aoyumi @ 2004/04/18
  1000. */
  1001. void hf_reduction(vorbis_info_psy_global *g,
  1002. vorbis_look_psy *p,
  1003. vorbis_info_mapping0 *vi,
  1004. float **mdct){
  1005. int i,j,n=p->n, de=0.3*p->m_val;
  1006. int limit=g->coupling_pointlimit[p->vi->blockflag][PACKETBLOBS/2];
  1007. for(i=0; i<vi->coupling_steps; i++){
  1008. /* for(j=start; j<limit; j++){} // ???*/
  1009. for(j=limit; j<n; j++)
  1010. mdct[i][j] *= (1.0 - de*((float)(j-limit) / (float)(n-limit)));
  1011. }
  1012. }