psy.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: psychoacoustics not including preecho
  13. last mod: $Id: psy.c,v 1.48.2.9 2001/08/13 00:20:11 xiphmont Exp $
  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. /* Why Bark scale for encoding but not masking computation? Because
  29. masking has a strong harmonic dependancy */
  30. vorbis_look_psy_global *_vp_global_look(vorbis_info *vi){
  31. int i,j;
  32. codec_setup_info *ci=vi->codec_setup;
  33. vorbis_info_psy_global *gi=ci->psy_g_param;
  34. vorbis_look_psy_global *look=_ogg_calloc(1,sizeof(vorbis_look_psy_global));
  35. int shiftoc=rint(log(gi->eighth_octave_lines*8)/log(2))-1;
  36. look->decaylines=toOC(96000.f)*(1<<(shiftoc+1))+.5f; /* max sample
  37. rate of
  38. 192000kHz
  39. for now */
  40. look->decay=_ogg_calloc(vi->channels,sizeof(float *));
  41. for(i=0;i<vi->channels;i++){
  42. look->decay[i]=_ogg_calloc(look->decaylines,sizeof(float));
  43. for(j=0;j<look->decaylines;j++)
  44. look->decay[i][j]=-9999.;
  45. }
  46. look->channels=vi->channels;
  47. look->ampmax=-9999.;
  48. look->gi=gi;
  49. return(look);
  50. }
  51. void _vp_global_free(vorbis_look_psy_global *look){
  52. int i;
  53. if(look->decay){
  54. for(i=0;i<look->channels;i++)
  55. _ogg_free(look->decay[i]);
  56. _ogg_free(look->decay);
  57. }
  58. memset(look,0,sizeof(vorbis_look_psy_global));
  59. _ogg_free(look);
  60. }
  61. void _vi_psy_free(vorbis_info_psy *i){
  62. if(i){
  63. memset(i,0,sizeof(vorbis_info_psy));
  64. _ogg_free(i);
  65. }
  66. }
  67. vorbis_info_psy *_vi_psy_copy(vorbis_info_psy *i){
  68. vorbis_info_psy *ret=_ogg_malloc(sizeof(vorbis_info_psy));
  69. memcpy(ret,i,sizeof(vorbis_info_psy));
  70. return(ret);
  71. }
  72. /* Set up decibel threshold slopes on a Bark frequency scale */
  73. /* ATH is the only bit left on a Bark scale. No reason to change it
  74. right now */
  75. static void set_curve(float *ref,float *c,int n, float crate){
  76. int i,j=0;
  77. for(i=0;i<MAX_BARK-1;i++){
  78. int endpos=rint(fromBARK(i+1)*2*n/crate);
  79. float base=ref[i];
  80. if(j<endpos){
  81. float delta=(ref[i+1]-base)/(endpos-j);
  82. for(;j<endpos && j<n;j++){
  83. c[j]=base;
  84. base+=delta;
  85. }
  86. }
  87. }
  88. }
  89. static void min_curve(float *c,
  90. float *c2){
  91. int i;
  92. for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
  93. }
  94. static void max_curve(float *c,
  95. float *c2){
  96. int i;
  97. for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
  98. }
  99. static void attenuate_curve(float *c,float att){
  100. int i;
  101. for(i=0;i<EHMER_MAX;i++)
  102. c[i]+=att;
  103. }
  104. static void interp_curve(float *c,float *c1,float *c2,float del){
  105. int i;
  106. for(i=0;i<EHMER_MAX;i++)
  107. c[i]=c2[i]*del+c1[i]*(1.f-del);
  108. }
  109. extern int analysis_noisy;
  110. static void setup_curve(float **c,
  111. int band,
  112. float *curveatt_dB){
  113. int i,j;
  114. float ath[EHMER_MAX];
  115. float tempc[P_LEVELS][EHMER_MAX];
  116. float *ATH=ATH_Bark_dB_lspconservative; /* just for limiting here */
  117. memcpy(c[0]+2,c[4]+2,sizeof(float)*EHMER_MAX);
  118. memcpy(c[2]+2,c[4]+2,sizeof(float)*EHMER_MAX);
  119. /* we add back in the ATH to avoid low level curves falling off to
  120. -infinity and unneccessarily cutting off high level curves in the
  121. curve limiting (last step). But again, remember... a half-band's
  122. settings must be valid over the whole band, and it's better to
  123. mask too little than too much, so be pessimal. */
  124. for(i=0;i<EHMER_MAX;i++){
  125. float oc_min=band*.5+(i-EHMER_OFFSET)*.125;
  126. float oc_max=band*.5+(i-EHMER_OFFSET+1)*.125;
  127. float bark=toBARK(fromOC(oc_min));
  128. int ibark=floor(bark);
  129. float del=bark-ibark;
  130. float ath_min,ath_max;
  131. if(ibark<26)
  132. ath_min=ATH[ibark]*(1.f-del)+ATH[ibark+1]*del;
  133. else
  134. ath_min=ATH[25];
  135. bark=toBARK(fromOC(oc_max));
  136. ibark=floor(bark);
  137. del=bark-ibark;
  138. if(ibark<26)
  139. ath_max=ATH[ibark]*(1.f-del)+ATH[ibark+1]*del;
  140. else
  141. ath_max=ATH[25];
  142. ath[i]=min(ath_min,ath_max);
  143. }
  144. /* The c array is comes in as dB curves at 20 40 60 80 100 dB.
  145. interpolate intermediate dB curves */
  146. for(i=1;i<P_LEVELS;i+=2){
  147. interp_curve(c[i]+2,c[i-1]+2,c[i+1]+2,.5);
  148. }
  149. /* normalize curves so the driving amplitude is 0dB */
  150. /* make temp curves with the ATH overlayed */
  151. for(i=0;i<P_LEVELS;i++){
  152. attenuate_curve(c[i]+2,curveatt_dB[i]);
  153. memcpy(tempc[i],ath,EHMER_MAX*sizeof(float));
  154. attenuate_curve(tempc[i],-i*10.f);
  155. max_curve(tempc[i],c[i]+2);
  156. }
  157. /* Now limit the louder curves.
  158. the idea is this: We don't know what the playback attenuation
  159. will be; 0dB SL moves every time the user twiddles the volume
  160. knob. So that means we have to use a single 'most pessimal' curve
  161. for all masking amplitudes, right? Wrong. The *loudest* sound
  162. can be in (we assume) a range of ...+100dB] SL. However, sounds
  163. 20dB down will be in a range ...+80], 40dB down is from ...+60],
  164. etc... */
  165. for(j=1;j<P_LEVELS;j++){
  166. min_curve(tempc[j],tempc[j-1]);
  167. min_curve(c[j]+2,tempc[j]);
  168. }
  169. /* add fenceposts */
  170. for(j=0;j<P_LEVELS;j++){
  171. for(i=0;i<EHMER_OFFSET;i++)
  172. if(c[j][i+2]>-200.f)break;
  173. c[j][0]=i;
  174. for(i=EHMER_MAX-1;i>EHMER_OFFSET+1;i--)
  175. if(c[j][i+2]>-200.f)
  176. break;
  177. c[j][1]=i;
  178. }
  179. }
  180. void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,
  181. vorbis_info_psy_global *gi,int n,long rate){
  182. long i,j,k,lo=0,hi=0;
  183. long maxoc;
  184. memset(p,0,sizeof(vorbis_look_psy));
  185. p->eighth_octave_lines=gi->eighth_octave_lines;
  186. p->shiftoc=rint(log(gi->eighth_octave_lines*8)/log(2))-1;
  187. p->firstoc=toOC(.25f*rate/n)*(1<<(p->shiftoc+1))-gi->eighth_octave_lines;
  188. maxoc=toOC((n*.5f-.25f)*rate/n)*(1<<(p->shiftoc+1))+.5f;
  189. p->total_octave_lines=maxoc-p->firstoc+1;
  190. if(vi->ath)
  191. p->ath=_ogg_malloc(n*sizeof(float));
  192. p->octave=_ogg_malloc(n*sizeof(long));
  193. p->bark=_ogg_malloc(n*sizeof(unsigned long));
  194. p->vi=vi;
  195. p->n=n;
  196. p->rate=rate;
  197. /* set up the lookups for a given blocksize and sample rate */
  198. if(vi->ath)
  199. set_curve(vi->ath, p->ath,n,rate);
  200. for(i=0;i<n;i++){
  201. float bark=toBARK(rate/(2*n)*i);
  202. for(;lo+vi->noisewindowlomin<i &&
  203. toBARK(rate/(2*n)*lo)<(bark-vi->noisewindowlo);lo++);
  204. for(;hi<n && (hi<i+vi->noisewindowhimin ||
  205. toBARK(rate/(2*n)*hi)<(bark+vi->noisewindowhi));hi++);
  206. p->bark[i]=(hi<<16)+lo;
  207. }
  208. for(i=0;i<n;i++)
  209. p->octave[i]=toOC((i*.5f+.25f)*rate/n)*(1<<(p->shiftoc+1))+.5f;
  210. p->tonecurves=_ogg_malloc(P_BANDS*sizeof(float **));
  211. p->noisethresh=_ogg_malloc(n*sizeof(float));
  212. p->noiseoffset=_ogg_malloc(n*sizeof(float));
  213. for(i=0;i<P_BANDS;i++)
  214. p->tonecurves[i]=_ogg_malloc(P_LEVELS*sizeof(float *));
  215. for(i=0;i<P_BANDS;i++)
  216. for(j=0;j<P_LEVELS;j++)
  217. p->tonecurves[i][j]=_ogg_malloc((EHMER_MAX+2)*sizeof(float));
  218. /* OK, yeah, this was a silly way to do it */
  219. memcpy(p->tonecurves[0][4]+2,tone_125_40dB_SL,sizeof(float)*EHMER_MAX);
  220. memcpy(p->tonecurves[0][6]+2,tone_125_60dB_SL,sizeof(float)*EHMER_MAX);
  221. memcpy(p->tonecurves[0][8]+2,tone_125_80dB_SL,sizeof(float)*EHMER_MAX);
  222. memcpy(p->tonecurves[0][10]+2,tone_125_100dB_SL,sizeof(float)*EHMER_MAX);
  223. memcpy(p->tonecurves[2][4]+2,tone_125_40dB_SL,sizeof(float)*EHMER_MAX);
  224. memcpy(p->tonecurves[2][6]+2,tone_125_60dB_SL,sizeof(float)*EHMER_MAX);
  225. memcpy(p->tonecurves[2][8]+2,tone_125_80dB_SL,sizeof(float)*EHMER_MAX);
  226. memcpy(p->tonecurves[2][10]+2,tone_125_100dB_SL,sizeof(float)*EHMER_MAX);
  227. memcpy(p->tonecurves[4][4]+2,tone_250_40dB_SL,sizeof(float)*EHMER_MAX);
  228. memcpy(p->tonecurves[4][6]+2,tone_250_60dB_SL,sizeof(float)*EHMER_MAX);
  229. memcpy(p->tonecurves[4][8]+2,tone_250_80dB_SL,sizeof(float)*EHMER_MAX);
  230. memcpy(p->tonecurves[4][10]+2,tone_250_100dB_SL,sizeof(float)*EHMER_MAX);
  231. memcpy(p->tonecurves[6][4]+2,tone_500_40dB_SL,sizeof(float)*EHMER_MAX);
  232. memcpy(p->tonecurves[6][6]+2,tone_500_60dB_SL,sizeof(float)*EHMER_MAX);
  233. memcpy(p->tonecurves[6][8]+2,tone_500_80dB_SL,sizeof(float)*EHMER_MAX);
  234. memcpy(p->tonecurves[6][10]+2,tone_500_100dB_SL,sizeof(float)*EHMER_MAX);
  235. memcpy(p->tonecurves[8][4]+2,tone_1000_40dB_SL,sizeof(float)*EHMER_MAX);
  236. memcpy(p->tonecurves[8][6]+2,tone_1000_60dB_SL,sizeof(float)*EHMER_MAX);
  237. memcpy(p->tonecurves[8][8]+2,tone_1000_80dB_SL,sizeof(float)*EHMER_MAX);
  238. memcpy(p->tonecurves[8][10]+2,tone_1000_100dB_SL,sizeof(float)*EHMER_MAX);
  239. memcpy(p->tonecurves[10][4]+2,tone_2000_40dB_SL,sizeof(float)*EHMER_MAX);
  240. memcpy(p->tonecurves[10][6]+2,tone_2000_60dB_SL,sizeof(float)*EHMER_MAX);
  241. memcpy(p->tonecurves[10][8]+2,tone_2000_80dB_SL,sizeof(float)*EHMER_MAX);
  242. memcpy(p->tonecurves[10][10]+2,tone_2000_100dB_SL,sizeof(float)*EHMER_MAX);
  243. memcpy(p->tonecurves[12][4]+2,tone_4000_40dB_SL,sizeof(float)*EHMER_MAX);
  244. memcpy(p->tonecurves[12][6]+2,tone_4000_60dB_SL,sizeof(float)*EHMER_MAX);
  245. memcpy(p->tonecurves[12][8]+2,tone_4000_80dB_SL,sizeof(float)*EHMER_MAX);
  246. memcpy(p->tonecurves[12][10]+2,tone_4000_100dB_SL,sizeof(float)*EHMER_MAX);
  247. memcpy(p->tonecurves[14][4]+2,tone_8000_40dB_SL,sizeof(float)*EHMER_MAX);
  248. memcpy(p->tonecurves[14][6]+2,tone_8000_60dB_SL,sizeof(float)*EHMER_MAX);
  249. memcpy(p->tonecurves[14][8]+2,tone_8000_80dB_SL,sizeof(float)*EHMER_MAX);
  250. memcpy(p->tonecurves[14][10]+2,tone_8000_100dB_SL,sizeof(float)*EHMER_MAX);
  251. memcpy(p->tonecurves[16][4]+2,tone_8000_40dB_SL,sizeof(float)*EHMER_MAX);
  252. memcpy(p->tonecurves[16][6]+2,tone_8000_60dB_SL,sizeof(float)*EHMER_MAX);
  253. memcpy(p->tonecurves[16][8]+2,tone_8000_80dB_SL,sizeof(float)*EHMER_MAX);
  254. memcpy(p->tonecurves[16][10]+2,tone_8000_100dB_SL,sizeof(float)*EHMER_MAX);
  255. /* value limit the tonal masking curves; the peakatt not only
  256. optionally specifies maximum dynamic depth, but also [always]
  257. limits the masking curves to a minimum depth */
  258. for(i=0;i<P_BANDS;i+=2)
  259. for(j=4;j<P_LEVELS;j+=2)
  260. for(k=2;k<EHMER_MAX+2;k++)
  261. p->tonecurves[i][j][k]+=vi->tone_masteratt;
  262. /* interpolate curves between */
  263. for(i=1;i<P_BANDS;i+=2)
  264. for(j=4;j<P_LEVELS;j+=2){
  265. memcpy(p->tonecurves[i][j]+2,p->tonecurves[i-1][j]+2,EHMER_MAX*sizeof(float));
  266. /*interp_curve(p->tonecurves[i][j],
  267. p->tonecurves[i-1][j],
  268. p->tonecurves[i+1][j],.5);*/
  269. min_curve(p->tonecurves[i][j]+2,p->tonecurves[i+1][j]+2);
  270. }
  271. /* set up the final curves */
  272. for(i=0;i<P_BANDS;i++)
  273. setup_curve(p->tonecurves[i],i,vi->toneatt->block[i]);
  274. if(vi->curvelimitp){
  275. /* value limit the tonal masking curves; the peakatt not only
  276. optionally specifies maximum dynamic depth, but also [always]
  277. limits the masking curves to a minimum depth */
  278. for(i=0;i<P_BANDS;i++)
  279. for(j=0;j<P_LEVELS;j++){
  280. for(k=2;k<EHMER_OFFSET+2+vi->curvelimitp;k++)
  281. if(p->tonecurves[i][j][k]> vi->peakatt->block[i][j])
  282. p->tonecurves[i][j][k]= vi->peakatt->block[i][j];
  283. else
  284. break;
  285. }
  286. }
  287. if(vi->peakattp) /* we limit depth only optionally */
  288. for(i=0;i<P_BANDS;i++)
  289. for(j=0;j<P_LEVELS;j++)
  290. if(p->tonecurves[i][j][EHMER_OFFSET+2]< vi->peakatt->block[i][j])
  291. p->tonecurves[i][j][EHMER_OFFSET+2]= vi->peakatt->block[i][j];
  292. /* but guarding is mandatory */
  293. for(i=0;i<P_BANDS;i++)
  294. for(j=0;j<P_LEVELS;j++)
  295. if(p->tonecurves[i][j][EHMER_OFFSET+2]< vi->tone_maxatt)
  296. p->tonecurves[i][j][EHMER_OFFSET+2]= vi->tone_maxatt;
  297. /* set up rolling noise median */
  298. for(i=0;i<n;i++){
  299. float halfoc=toOC((i+.5)*rate/(2.*n))*2.;
  300. int inthalfoc;
  301. float del;
  302. if(halfoc<0)halfoc=0;
  303. if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
  304. inthalfoc=(int)halfoc;
  305. del=halfoc-inthalfoc;
  306. p->noisethresh[i]=((p->vi->noisethresh[inthalfoc]*(1.-del) +
  307. p->vi->noisethresh[inthalfoc+1]*del))*2.f-1.f;
  308. p->noiseoffset[i]=
  309. p->vi->noiseoff[inthalfoc]*(1.-del) +
  310. p->vi->noiseoff[inthalfoc+1]*del;
  311. }
  312. analysis_noisy=1;
  313. _analysis_output("noiseoff",0,p->noiseoffset,n,1,0);
  314. _analysis_output("noisethresh",0,p->noisethresh,n,1,0);
  315. for(i=0;i<P_LEVELS;i++)
  316. _analysis_output("curve_63Hz",i,p->tonecurves[0][i]+2,EHMER_MAX,0,0);
  317. for(i=0;i<P_LEVELS;i++)
  318. _analysis_output("curve_88Hz",i,p->tonecurves[1][i]+2,EHMER_MAX,0,0);
  319. for(i=0;i<P_LEVELS;i++)
  320. _analysis_output("curve_125Hz",i,p->tonecurves[2][i]+2,EHMER_MAX,0,0);
  321. for(i=0;i<P_LEVELS;i++)
  322. _analysis_output("curve_170Hz",i,p->tonecurves[3][i]+2,EHMER_MAX,0,0);
  323. for(i=0;i<P_LEVELS;i++)
  324. _analysis_output("curve_250Hz",i,p->tonecurves[4][i]+2,EHMER_MAX,0,0);
  325. for(i=0;i<P_LEVELS;i++)
  326. _analysis_output("curve_350Hz",i,p->tonecurves[5][i]+2,EHMER_MAX,0,0);
  327. for(i=0;i<P_LEVELS;i++)
  328. _analysis_output("curve_500Hz",i,p->tonecurves[6][i]+2,EHMER_MAX,0,0);
  329. for(i=0;i<P_LEVELS;i++)
  330. _analysis_output("curve_700Hz",i,p->tonecurves[7][i]+2,EHMER_MAX,0,0);
  331. for(i=0;i<P_LEVELS;i++)
  332. _analysis_output("curve_1kHz",i,p->tonecurves[8][i]+2,EHMER_MAX,0,0);
  333. for(i=0;i<P_LEVELS;i++)
  334. _analysis_output("curve_1.4Hz",i,p->tonecurves[9][i]+2,EHMER_MAX,0,0);
  335. for(i=0;i<P_LEVELS;i++)
  336. _analysis_output("curve_2kHz",i,p->tonecurves[10][i]+2,EHMER_MAX,0,0);
  337. for(i=0;i<P_LEVELS;i++)
  338. _analysis_output("curve_2.4kHz",i,p->tonecurves[11][i]+2,EHMER_MAX,0,0);
  339. for(i=0;i<P_LEVELS;i++)
  340. _analysis_output("curve_4kHz",i,p->tonecurves[12][i]+2,EHMER_MAX,0,0);
  341. for(i=0;i<P_LEVELS;i++)
  342. _analysis_output("curve_5.6kHz",i,p->tonecurves[13][i]+2,EHMER_MAX,0,0);
  343. for(i=0;i<P_LEVELS;i++)
  344. _analysis_output("curve_8kHz",i,p->tonecurves[14][i]+2,EHMER_MAX,0,0);
  345. for(i=0;i<P_LEVELS;i++)
  346. _analysis_output("curve_11.5kHz",i,p->tonecurves[15][i]+2,EHMER_MAX,0,0);
  347. for(i=0;i<P_LEVELS;i++)
  348. _analysis_output("curve_16kHz",i,p->tonecurves[16][i]+2,EHMER_MAX,0,0);
  349. analysis_noisy=1;
  350. }
  351. void _vp_psy_clear(vorbis_look_psy *p){
  352. int i,j;
  353. if(p){
  354. if(p->ath)_ogg_free(p->ath);
  355. if(p->octave)_ogg_free(p->octave);
  356. if(p->bark)_ogg_free(p->bark);
  357. if(p->tonecurves){
  358. for(i=0;i<P_BANDS;i++){
  359. for(j=0;j<P_LEVELS;j++){
  360. _ogg_free(p->tonecurves[i][j]);
  361. }
  362. _ogg_free(p->tonecurves[i]);
  363. }
  364. _ogg_free(p->tonecurves);
  365. _ogg_free(p->noiseoffset);
  366. }
  367. memset(p,0,sizeof(vorbis_look_psy));
  368. }
  369. }
  370. /* octave/(8*eighth_octave_lines) x scale and dB y scale */
  371. static void seed_curve(float *seed,
  372. const float **curves,
  373. float amp,
  374. int oc, int n,
  375. int linesper,float dBoffset){
  376. int i,post1;
  377. int seedptr;
  378. const float *posts,*curve;
  379. int choice=(int)((amp+dBoffset)*.1f);
  380. choice=max(choice,0);
  381. choice=min(choice,P_LEVELS-1);
  382. posts=curves[choice];
  383. curve=posts+2;
  384. post1=(int)posts[1];
  385. seedptr=oc+(posts[0]-16)*linesper-(linesper>>1);
  386. for(i=posts[0];i<post1;i++){
  387. if(seedptr>0){
  388. float lin=amp+curve[i];
  389. if(seed[seedptr]<lin)seed[seedptr]=lin;
  390. }
  391. seedptr+=linesper;
  392. if(seedptr>=n)break;
  393. }
  394. }
  395. static void seed_loop(vorbis_look_psy *p,
  396. const float ***curves,
  397. const float *f,
  398. const float *flr,
  399. float *seed,
  400. float specmax){
  401. vorbis_info_psy *vi=p->vi;
  402. long n=p->n,i;
  403. float dBoffset=vi->max_curve_dB-specmax;
  404. /* prime the working vector with peak values */
  405. for(i=0;i<n;i++){
  406. float max=f[i];
  407. long oc=p->octave[i];
  408. while(i+1<n && p->octave[i+1]==oc){
  409. i++;
  410. if(f[i]>max)max=f[i];
  411. }
  412. if(max+6.f>flr[i]){
  413. oc=oc>>p->shiftoc;
  414. if(oc>=P_BANDS)oc=P_BANDS-1;
  415. if(oc<0)oc=0;
  416. seed_curve(seed,
  417. curves[oc],
  418. max,
  419. p->octave[i]-p->firstoc,
  420. p->total_octave_lines,
  421. p->eighth_octave_lines,
  422. dBoffset);
  423. }
  424. }
  425. }
  426. static void seed_chase(float *seeds, int linesper, long n){
  427. long *posstack=alloca(n*sizeof(long));
  428. float *ampstack=alloca(n*sizeof(float));
  429. long stack=0;
  430. long pos=0;
  431. long i;
  432. for(i=0;i<n;i++){
  433. if(stack<2){
  434. posstack[stack]=i;
  435. ampstack[stack++]=seeds[i];
  436. }else{
  437. while(1){
  438. if(seeds[i]<ampstack[stack-1]){
  439. posstack[stack]=i;
  440. ampstack[stack++]=seeds[i];
  441. break;
  442. }else{
  443. if(i<posstack[stack-1]+linesper){
  444. if(stack>1 && ampstack[stack-1]<=ampstack[stack-2] &&
  445. i<posstack[stack-2]+linesper){
  446. /* we completely overlap, making stack-1 irrelevant. pop it */
  447. stack--;
  448. continue;
  449. }
  450. }
  451. posstack[stack]=i;
  452. ampstack[stack++]=seeds[i];
  453. break;
  454. }
  455. }
  456. }
  457. }
  458. /* the stack now contains only the positions that are relevant. Scan
  459. 'em straight through */
  460. for(i=0;i<stack;i++){
  461. long endpos;
  462. if(i<stack-1 && ampstack[i+1]>ampstack[i]){
  463. endpos=posstack[i+1];
  464. }else{
  465. endpos=posstack[i]+linesper+1; /* +1 is important, else bin 0 is
  466. discarded in short frames */
  467. }
  468. if(endpos>n)endpos=n;
  469. for(;pos<endpos;pos++)
  470. seeds[pos]=ampstack[i];
  471. }
  472. /* there. Linear time. I now remember this was on a problem set I
  473. had in Grad Skool... I didn't solve it at the time ;-) */
  474. }
  475. /* bleaugh, this is more complicated than it needs to be */
  476. static void max_seeds(vorbis_look_psy *p,
  477. vorbis_look_psy_global *g,
  478. int channel,
  479. float *seed,
  480. float *flr){
  481. long n=p->total_octave_lines;
  482. int linesper=p->eighth_octave_lines;
  483. long linpos=0;
  484. long pos;
  485. seed_chase(seed,linesper,n); /* for masking */
  486. pos=p->octave[0]-p->firstoc-(linesper>>1);
  487. while(linpos+1<p->n){
  488. float minV=seed[pos];
  489. long end=((p->octave[linpos]+p->octave[linpos+1])>>1)-p->firstoc;
  490. while(pos+1<=end){
  491. pos++;
  492. if((seed[pos]>NEGINF && seed[pos]<minV) || minV==NEGINF)
  493. minV=seed[pos];
  494. }
  495. /* seed scale is log. Floor is linear. Map back to it */
  496. end=pos+p->firstoc;
  497. for(;linpos<p->n && p->octave[linpos]<=end;linpos++)
  498. if(flr[linpos]<minV)flr[linpos]=minV;
  499. }
  500. {
  501. float minV=seed[p->total_octave_lines-1];
  502. for(;linpos<p->n;linpos++)
  503. if(flr[linpos]<minV)flr[linpos]=minV;
  504. }
  505. }
  506. /* set to match vorbis_quantdblook.h */
  507. #define BINCOUNT 280
  508. #define LASTBIN (BINCOUNT-1)
  509. static int psy_dBquant(const float *x){
  510. int i= *x*2.f+279.5f;
  511. if(i>279)return(279);
  512. if(i<0)return(0);
  513. return i;
  514. }
  515. static void bark_noise_median(int n,const long *b,
  516. const float *f,
  517. float *noise,
  518. const float *thresh,
  519. const int fixed,
  520. const float *off){
  521. int i=0,lo=-1,hi=-1,fixedc=0;
  522. int median=LASTBIN>>1;
  523. int barkradix[BINCOUNT];
  524. int barkcountbelow=0;
  525. int fixedradix[BINCOUNT];
  526. int fixedcountbelow=0;
  527. memset(barkradix,0,sizeof(barkradix));
  528. if(fixed>0){
  529. memset(fixedradix,0,sizeof(fixedradix));
  530. /* bootstrap the fixed window case seperately */
  531. for(i=0;i<(fixed>>1);i++){
  532. int bin=psy_dBquant(f+i);
  533. fixedradix[bin]++;
  534. fixedc++;
  535. if(bin<=median)
  536. fixedcountbelow++;
  537. }
  538. }
  539. for(i=0;i<n;i++){
  540. /* find new lo/hi */
  541. int bi=b[i]>>16;
  542. for(;hi<bi;hi++){
  543. int bin=psy_dBquant(f+hi);
  544. barkradix[bin]++;
  545. if(bin<=median)
  546. barkcountbelow++;
  547. }
  548. bi=b[i]&0xffff;
  549. for(;lo<bi;lo++){
  550. int bin=psy_dBquant(f+lo);
  551. barkradix[bin]--;
  552. if(bin<=median)
  553. barkcountbelow--;
  554. }
  555. if(fixed>0){
  556. bi=i+(fixed>>1);
  557. if(bi<n){
  558. int bin=psy_dBquant(f+bi);
  559. fixedradix[bin]++;
  560. fixedc++;
  561. if(bin<=median)
  562. fixedcountbelow++;
  563. }
  564. bi-=fixed;
  565. if(bi>=0){
  566. int bin=psy_dBquant(f+bi);
  567. fixedradix[bin]--;
  568. fixedc--;
  569. if(bin<=median)
  570. fixedcountbelow--;
  571. }
  572. }
  573. /* move the median if needed */
  574. {
  575. int bark_th = rint((thresh[i]+1.)*(hi-lo)*.5);
  576. if(fixed>0){
  577. int fixed_th = rint((thresh[i]+1.)*(fixedc)*.5);
  578. while(bark_th>=barkcountbelow &&
  579. fixed_th>=fixedcountbelow /* && median<LASTBIN by rep invariant */
  580. ){
  581. median++;
  582. barkcountbelow+=barkradix[median];
  583. fixedcountbelow+=fixedradix[median];
  584. }
  585. while(bark_th<barkcountbelow ||
  586. fixed_th<fixedcountbelow /* && median>=0 by rep invariant */
  587. ){
  588. barkcountbelow-=barkradix[median];
  589. fixedcountbelow-=fixedradix[median];
  590. median--;
  591. }
  592. }else{
  593. while(bark_th>=barkcountbelow){
  594. median++;
  595. barkcountbelow+=barkradix[median];
  596. }
  597. while(bark_th<barkcountbelow){
  598. barkcountbelow-=barkradix[median];
  599. median--;
  600. }
  601. }
  602. }
  603. noise[i]= (median+1)*.5f+off[i]-140.f;
  604. }
  605. }
  606. static void bark_noise_pointmp(int n,const long *b,
  607. const float *f,
  608. float *noise,
  609. const int fixed){
  610. long i,hi=0,lo=0,hif=0,lof=0;
  611. double xa=0,xb=0;
  612. double ya=0,yb=0;
  613. double x2a=0,x2b=0;
  614. double y2a=0,y2b=0;
  615. double xya=0,xyb=0;
  616. double na=0,nb=0;
  617. for(i=0;i<n;i++){
  618. if(hi<n){
  619. /* find new lo/hi */
  620. int bi=b[i]>>16;
  621. for(;hi<bi;hi++){
  622. double bin=(f[hi]<-140.f?0.:f[hi]+140.);
  623. double nn= bin*bin;
  624. na += nn;
  625. xa += hi*nn;
  626. ya += bin*nn;
  627. x2a += hi*hi*nn;
  628. y2a += bin*bin*nn;
  629. xya += hi*bin*nn;
  630. }
  631. bi=b[i]&0xffff;
  632. for(;lo<bi;lo++){
  633. double bin=(f[lo]<-140.f?0.:f[lo]+140.);
  634. double nn= bin*bin;
  635. na -= nn;
  636. xa -= lo*nn;
  637. ya -= bin*nn;
  638. x2a -= lo*lo*nn;
  639. y2a -= bin*bin*nn;
  640. xya -= lo*bin*nn;
  641. }
  642. }
  643. if(hif<n && fixed>0){
  644. int bi=i+fixed/2;
  645. if(bi>n)bi=n;
  646. for(;hif<bi;hif++){
  647. double bin=(f[hif]<-140.f?0.:f[hif]+140.);
  648. double nn= bin*bin;
  649. nb += nn;
  650. xb += hif*nn;
  651. yb += bin*nn;
  652. x2b += hif*hif*nn;
  653. y2b += bin*bin*nn;
  654. xyb += hif*bin*nn;
  655. }
  656. bi=i-(fixed+1)/2;
  657. if(bi<0)bi=0;
  658. for(;lof<bi;lof++){
  659. double bin=(f[lof]<-140.f?0.:f[lof]+140.);
  660. double nn= bin*bin;
  661. nb -= nn;
  662. xb -= lof*nn;
  663. yb -= bin*nn;
  664. x2b -= lof*lof*nn;
  665. y2b -= bin*bin*nn;
  666. xyb -= lof*bin*nn;
  667. }
  668. }
  669. {
  670. double denom=1./(na*x2a-xa*xa);
  671. double a=(ya*x2a-xya*xa)*denom;
  672. double b=(na*xya-xa*ya)*denom;
  673. double va=a+b*i;
  674. if(fixed>0){
  675. double denomf=1./(nb*x2b-xb*xb);
  676. double af=(yb*x2b-xyb*xb)*denomf;
  677. double bf=(nb*xyb-xb*yb)*denomf;
  678. double vb=af+bf*i;
  679. if(va>vb)va=vb;
  680. }
  681. noise[i]=va-140.f;
  682. }
  683. }
  684. }
  685. static void bark_noise_hybridmp(int n,const long *b,
  686. const float *f,
  687. float *noise,
  688. const int fixed){
  689. long i,hi=0,lo=0,hif=0,lof=0;
  690. double xa=0,xb=0;
  691. double ya=0,yb=0;
  692. double x2a=0,x2b=0;
  693. double y2a=0,y2b=0;
  694. double xya=0,xyb=0;
  695. double na=0,nb=0;
  696. int first=-1,firstf=-1;
  697. int last=0,lastf=0;
  698. int rna=0,rnb=0;
  699. for(i=0;i<n;i++){
  700. if(hi<n){
  701. /* find new lo/hi */
  702. int bi=b[i]>>16;
  703. for(;hi<bi;hi++){
  704. double bin=f[hi];
  705. if(bin>0.f){
  706. double nn= bin*bin;
  707. nn*=nn;
  708. na += nn;
  709. xa += hi*nn;
  710. ya += bin*nn;
  711. x2a += hi*hi*nn;
  712. y2a += bin*bin*nn;
  713. xya += hi*bin*nn;
  714. last=hi;
  715. rna++;
  716. if(first==-1)first=hi;
  717. }
  718. }
  719. bi=b[i]&0xffff;
  720. for(;lo<bi;lo++){
  721. double bin=f[lo];
  722. if(bin>0.f){
  723. double nn= bin*bin;
  724. nn*=nn;
  725. na -= nn;
  726. xa -= lo*nn;
  727. ya -= bin*nn;
  728. x2a -= lo*lo*nn;
  729. y2a -= bin*bin*nn;
  730. xya -= lo*bin*nn;
  731. rna--;
  732. }
  733. if(first<lo)first=-1;
  734. if(last<lo){
  735. first=-1;
  736. }else{
  737. for(first=lo;first<hi;first++)
  738. if(f[first]>0.f)break;
  739. if(first==hi)first=-1;
  740. }
  741. }
  742. }
  743. if(hif<n && fixed>0){
  744. int bi=i+fixed/2;
  745. if(bi>n)bi=n;
  746. for(;hif<bi;hif++){
  747. double bin=f[hif];
  748. if(bin>0.f){
  749. double nn= bin*bin;
  750. nn*=nn;
  751. nb += nn;
  752. xb += hif*nn;
  753. yb += bin*nn;
  754. x2b += hif*hif*nn;
  755. y2b += bin*bin*nn;
  756. xyb += hif*bin*nn;
  757. lastf=hif;
  758. rnb++;
  759. if(firstf==-1)firstf=hif;
  760. }
  761. }
  762. bi=i-(fixed+1)/2;
  763. if(bi<0)bi=0;
  764. for(;lof<bi;lof++){
  765. double bin=f[lof];
  766. if(bin>0.f){
  767. double nn= bin*bin;
  768. nn*=nn;
  769. nb -= nn;
  770. xb -= lof*nn;
  771. yb -= bin*nn;
  772. x2b -= lof*lof*nn;
  773. y2b -= bin*bin*nn;
  774. xyb -= lof*bin*nn;
  775. rnb--;
  776. }
  777. if(firstf<lof)firstf=-1;
  778. if(lastf<lof){
  779. firstf=-1;
  780. }else{
  781. for(firstf=lof;firstf<hif;firstf++)
  782. if(f[firstf]>0.f)break;
  783. if(firstf==hif)firstf=-1;
  784. }
  785. }
  786. }
  787. {
  788. double va;
  789. if(rna>2 && (last-first)*3/2>hi-lo){
  790. double denom=1./(na*x2a-xa*xa);
  791. double a=(ya*x2a-xya*xa)*denom;
  792. double b=(na*xya-xa*ya)*denom;
  793. va=a+b*i;
  794. }else{
  795. va=0.f;
  796. if(na>.5)va=ya/na;
  797. }
  798. if(va<0.)va=0.;
  799. if(fixed>0){
  800. double vb;
  801. if(rnb>2 && (lastf-firstf)*3/2>hif-lof){
  802. double denomf=1./(nb*x2b-xb*xb);
  803. double af=(yb*x2b-xyb*xb)*denomf;
  804. double bf=(nb*xyb-xb*yb)*denomf;
  805. vb=af+bf*i;
  806. }else{
  807. vb=0.f;
  808. if(nb>.5)vb=yb/nb;
  809. }
  810. if(vb<0.)vb=0.;
  811. if(va>vb && vb>0.)va=vb;
  812. }
  813. noise[i]=va;
  814. }
  815. }
  816. }
  817. void _vp_remove_floor(vorbis_look_psy *p,
  818. vorbis_look_psy_global *g,
  819. float *logmdct,
  820. float *mdct,
  821. float *codedflr,
  822. float *residue,
  823. float local_specmax){
  824. int i,n=p->n;
  825. for(i=0;i<n;i++)
  826. if(mdct[i]!=0.f)
  827. residue[i]=mdct[i]/codedflr[i];
  828. else
  829. residue[i]=0.f;
  830. }
  831. void _vp_compute_mask(vorbis_look_psy *p,
  832. vorbis_look_psy_global *g,
  833. int channel,
  834. float *logfft,
  835. float *logmdct,
  836. float *logmask,
  837. float global_specmax,
  838. float local_specmax,
  839. int lastsize){
  840. int i,n=p->n;
  841. static int seq=0;
  842. float *seed=alloca(sizeof(float)*p->total_octave_lines);
  843. for(i=0;i<p->total_octave_lines;i++)seed[i]=NEGINF;
  844. /* noise masking */
  845. if(p->vi->noisemaskp){
  846. #if 1
  847. float *work=alloca(n*sizeof(float));
  848. bark_noise_pointmp(n,p->bark,logmdct,logmask,
  849. -1);
  850. for(i=0;i<n;i++)work[i]=logmdct[i]-logmask[i];
  851. _analysis_output("medianmdct",seq,work,n,1,0);
  852. bark_noise_hybridmp(n,p->bark,work,logmask,
  853. p->vi->noisewindowfixed);
  854. for(i=0;i<n;i++)work[i]=logmdct[i]-work[i];
  855. /* work[i] holds the median line (.5), logmask holds the upper
  856. envelope line (1.) */
  857. _analysis_output("median",seq,work,n,1,0);
  858. _analysis_output("envelope",seq,logmask,n,1,0);
  859. for(i=0;i<n;i++)logmask[i]=
  860. work[i]+
  861. p->noisethresh[i]*logmask[i]+p->noiseoffset[i];
  862. #else
  863. bark_noise_median(n,p->bark,logmdct,logmask,p->noisethresh,
  864. p->vi->noisewindowfixed,p->noiseoffset);
  865. #endif
  866. _analysis_output("noise",seq,logmask,n,1,0);
  867. }else{
  868. for(i=0;i<n;i++)logmask[i]=NEGINF;
  869. }
  870. /* set the ATH (floating below localmax, not global max by a
  871. specified att) */
  872. if(p->vi->ath){
  873. float att=local_specmax+p->vi->ath_adjatt;
  874. if(att<p->vi->ath_maxatt)att=p->vi->ath_maxatt;
  875. for(i=0;i<n;i++){
  876. float av=p->ath[i]+att;
  877. if(av>logmask[i])logmask[i]=av;
  878. }
  879. }
  880. /* tone/peak masking */
  881. seed_loop(p,(const float ***)p->tonecurves,logfft,logmask,seed,global_specmax);
  882. max_seeds(p,g,channel,seed,logmask);
  883. /* suppress any curve > p->vi->noisemaxsupp */
  884. if(p->vi->noisemaxsupp<0.f)
  885. for(i=0;i<n;i++)
  886. if(logmask[i]>p->vi->noisemaxsupp)
  887. logmask[i]=p->vi->noisemaxsupp;
  888. /* doing this here is clean, but we need to find a faster way to do
  889. it than to just tack it on */
  890. for(i=0;i<n;i++)if(logmdct[i]>=logmask[i])break;
  891. if(i==n)
  892. for(i=0;i<n;i++)logmask[i]=NEGINF;
  893. else
  894. for(i=0;i<n;i++)
  895. logfft[i]=max(logmdct[i],logfft[i]);
  896. seq++;
  897. }
  898. float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd){
  899. vorbis_info *vi=vd->vi;
  900. codec_setup_info *ci=vi->codec_setup;
  901. vorbis_info_psy_global *gi=ci->psy_g_param;
  902. int n=ci->blocksizes[vd->W]/2;
  903. float secs=(float)n/vi->rate;
  904. amp+=secs*gi->ampmax_att_per_sec;
  905. if(amp<-9999)amp=-9999;
  906. return(amp);
  907. }
  908. static void couple_lossless(float A, float B, float *mag, float *ang){
  909. if(fabs(A)>fabs(B)){
  910. *mag=A; *ang=(A>0.f?A-B:B-A);
  911. }else{
  912. *mag=B; *ang=(B>0.f?A-B:B-A);
  913. }
  914. }
  915. static void couple_8phase(float A, float B, float *mag, float *ang){
  916. if(fabs(A)>fabs(B)){
  917. *mag=A; *ang=(A>0?A-B:B-A);
  918. }else{
  919. *mag=B; *ang=(B>0?A-B:B-A);
  920. }
  921. if(*mag!=0.f)
  922. switch((int)(rint(*ang / *mag))){
  923. case 0:
  924. *ang=0;
  925. break;
  926. case 2:case -2:
  927. *ang=-2*fabs(*mag);
  928. break;
  929. case 1:
  930. *ang= *mag;
  931. break;
  932. case -1:
  933. *ang= -*mag;
  934. break;
  935. }
  936. }
  937. static void couple_6phase(float A, float B, float *mag, float *ang){
  938. if(fabs(A)>fabs(B)){
  939. *mag=A; *ang=(A>0?A-B:B-A);
  940. }else{
  941. *mag=B; *ang=(B>0?A-B:B-A);
  942. }
  943. if(*mag!=0.f)
  944. switch((int)(rint(*ang / *mag))){
  945. case -2:case 2:
  946. *mag=0;
  947. /*fall*/
  948. case 0:
  949. *ang=0;
  950. break;
  951. case 1:
  952. *ang= *mag;
  953. break;
  954. case -1:
  955. *ang= -*mag;
  956. break;
  957. }
  958. }
  959. static void couple_point(float A, float B, float *mag, float *ang){
  960. if(fabs(A)>fabs(B)){
  961. *mag=A; *ang=(A>0?A-B:B-A);
  962. }else{
  963. *mag=B; *ang=(B>0?A-B:B-A);
  964. }
  965. if(*mag!=0.f)
  966. switch((int)(rint(*ang / *mag))){
  967. case -2:case 2:
  968. *mag=0;
  969. /* fall */
  970. case 0:case 1: case -1:
  971. *ang=0;
  972. break;
  973. }
  974. }
  975. void _vp_quantize_couple(vorbis_look_psy *p,
  976. vorbis_info_mapping0 *vi,
  977. float **pcm,
  978. float **sofar,
  979. float **quantized,
  980. int *nonzero,
  981. int passno){
  982. int i,j,k,n=p->n;
  983. vorbis_info_psy *info=p->vi;
  984. /* perform any requested channel coupling */
  985. for(i=0;i<vi->coupling_steps;i++){
  986. float granulem=info->couple_pass[passno].granulem;
  987. float igranulem=info->couple_pass[passno].igranulem;
  988. /* make sure coupling a zero and a nonzero channel results in two
  989. nonzero channels. */
  990. if(nonzero[vi->coupling_mag[i]] ||
  991. nonzero[vi->coupling_ang[i]]){
  992. float *pcmM=pcm[vi->coupling_mag[i]];
  993. float *pcmA=pcm[vi->coupling_ang[i]];
  994. float *sofarM=sofar[vi->coupling_mag[i]];
  995. float *sofarA=sofar[vi->coupling_ang[i]];
  996. float *qM=quantized[vi->coupling_mag[i]];
  997. float *qA=quantized[vi->coupling_ang[i]];
  998. nonzero[vi->coupling_mag[i]]=1;
  999. nonzero[vi->coupling_ang[i]]=1;
  1000. for(j=0,k=0;j<n;k++){
  1001. vp_couple *part=info->couple_pass[passno].couple_pass+k;
  1002. for(;j<part->limit && j<p->n;j++){
  1003. /* partition by partition; k is our by-location partition
  1004. class counter */
  1005. float Am=rint(pcmM[j]*igranulem)*granulem;
  1006. float Bm=rint(pcmA[j]*igranulem)*granulem;
  1007. float ang,mag,fmag=max(fabs(Am),fabs(Bm));
  1008. if(fmag<part->amppost_point){
  1009. couple_point(Am,Bm,&mag,&ang);
  1010. }else{
  1011. if(fmag<part->amppost_6phase){
  1012. couple_6phase(Am,Bm,&mag,&ang);
  1013. }else{
  1014. if(fmag<part->amppost_8phase){
  1015. couple_8phase(Am,Bm,&mag,&ang);
  1016. }else{
  1017. couple_lossless(Am,Bm,&mag,&ang);
  1018. }
  1019. }
  1020. }
  1021. fmag=rint(fmag);
  1022. if(ang>fmag*1.9999f)ang=-fmag*2.f;
  1023. qM[j]=mag-sofarM[j];
  1024. qA[j]=ang-sofarA[j];
  1025. }
  1026. }
  1027. }
  1028. }
  1029. }