psy.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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.57.2.1 2001/12/18 23:49:16 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 dependency */
  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. vorbis_info_psy *_vi_psy_copy(vorbis_info_psy *i){
  58. vorbis_info_psy *ret=_ogg_malloc(sizeof(*ret));
  59. memcpy(ret,i,sizeof(*ret));
  60. return(ret);
  61. }
  62. /* Set up decibel threshold slopes on a Bark frequency scale */
  63. /* ATH is the only bit left on a Bark scale. No reason to change it
  64. right now */
  65. static void set_curve(float *ref,float *c,int n, float crate){
  66. int i,j=0;
  67. for(i=0;i<MAX_BARK-1;i++){
  68. int endpos=rint(fromBARK(i+1)*2*n/crate);
  69. float base=ref[i];
  70. if(j<endpos){
  71. float delta=(ref[i+1]-base)/(endpos-j);
  72. for(;j<endpos && j<n;j++){
  73. c[j]=base;
  74. base+=delta;
  75. }
  76. }
  77. }
  78. }
  79. static void min_curve(float *c,
  80. float *c2){
  81. int i;
  82. for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
  83. }
  84. static void max_curve(float *c,
  85. float *c2){
  86. int i;
  87. for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
  88. }
  89. static void attenuate_curve(float *c,float att){
  90. int i;
  91. for(i=0;i<EHMER_MAX;i++)
  92. c[i]+=att;
  93. }
  94. static void interp_curve(float *c,float *c1,float *c2,float del){
  95. int i;
  96. for(i=0;i<EHMER_MAX;i++)
  97. c[i]=c2[i]*del+c1[i]*(1.f-del);
  98. }
  99. extern int analysis_noisy;
  100. static void setup_curve(float **c,
  101. int band,
  102. float *curveatt_dB){
  103. int i,j;
  104. float ath[EHMER_MAX];
  105. float tempc[P_LEVELS][EHMER_MAX];
  106. float *ATH=ATH_Bark_dB_lspconservative; /* just for limiting here */
  107. memcpy(c[0]+2,c[4]+2,sizeof(*c[0])*EHMER_MAX);
  108. memcpy(c[2]+2,c[4]+2,sizeof(*c[2])*EHMER_MAX);
  109. /* we add back in the ATH to avoid low level curves falling off to
  110. -infinity and unnecessarily cutting off high level curves in the
  111. curve limiting (last step). But again, remember... a half-band's
  112. settings must be valid over the whole band, and it's better to
  113. mask too little than too much, so be pessimistical. */
  114. for(i=0;i<EHMER_MAX;i++){
  115. float oc_min=band*.5+(i-EHMER_OFFSET)*.125;
  116. float oc_max=band*.5+(i-EHMER_OFFSET+1)*.125;
  117. float bark=toBARK(fromOC(oc_min));
  118. int ibark=floor(bark);
  119. float del=bark-ibark;
  120. float ath_min,ath_max;
  121. if(ibark<26)
  122. ath_min=ATH[ibark]*(1.f-del)+ATH[ibark+1]*del;
  123. else
  124. ath_min=ATH[25];
  125. bark=toBARK(fromOC(oc_max));
  126. ibark=floor(bark);
  127. del=bark-ibark;
  128. if(ibark<26)
  129. ath_max=ATH[ibark]*(1.f-del)+ATH[ibark+1]*del;
  130. else
  131. ath_max=ATH[25];
  132. ath[i]=min(ath_min,ath_max);
  133. }
  134. /* The c array comes in as dB curves at 20 40 60 80 100 dB.
  135. interpolate intermediate dB curves */
  136. for(i=1;i<P_LEVELS;i+=2){
  137. interp_curve(c[i]+2,c[i-1]+2,c[i+1]+2,.5);
  138. }
  139. /* normalize curves so the driving amplitude is 0dB */
  140. /* make temp curves with the ATH overlayed */
  141. for(i=0;i<P_LEVELS;i++){
  142. attenuate_curve(c[i]+2,curveatt_dB[i]);
  143. memcpy(tempc[i],ath,EHMER_MAX*sizeof(*tempc[i]));
  144. attenuate_curve(tempc[i],-i*10.f);
  145. max_curve(tempc[i],c[i]+2);
  146. }
  147. /* Now limit the louder curves.
  148. the idea is this: We don't know what the playback attenuation
  149. will be; 0dB SL moves every time the user twiddles the volume
  150. knob. So that means we have to use a single 'most pessimal' curve
  151. for all masking amplitudes, right? Wrong. The *loudest* sound
  152. can be in (we assume) a range of ...+100dB] SL. However, sounds
  153. 20dB down will be in a range ...+80], 40dB down is from ...+60],
  154. etc... */
  155. for(j=1;j<P_LEVELS;j++){
  156. min_curve(tempc[j],tempc[j-1]);
  157. min_curve(c[j]+2,tempc[j]);
  158. }
  159. /* add fenceposts */
  160. for(j=0;j<P_LEVELS;j++){
  161. for(i=0;i<EHMER_OFFSET;i++)
  162. if(c[j][i+2]>-200.f)break;
  163. c[j][0]=i;
  164. for(i=EHMER_MAX-1;i>EHMER_OFFSET+1;i--)
  165. if(c[j][i+2]>-200.f)
  166. break;
  167. c[j][1]=i;
  168. }
  169. }
  170. void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,
  171. vorbis_info_psy_global *gi,int n,long rate){
  172. long i,j,k,lo=-99,hi=0;
  173. long maxoc;
  174. memset(p,0,sizeof(*p));
  175. p->eighth_octave_lines=gi->eighth_octave_lines;
  176. p->shiftoc=rint(log(gi->eighth_octave_lines*8)/log(2))-1;
  177. p->firstoc=toOC(.25f*rate/n)*(1<<(p->shiftoc+1))-gi->eighth_octave_lines;
  178. maxoc=toOC((n*.5f-.25f)*rate/n)*(1<<(p->shiftoc+1))+.5f;
  179. p->total_octave_lines=maxoc-p->firstoc+1;
  180. if(vi->ath)
  181. p->ath=_ogg_malloc(n*sizeof(*p->ath));
  182. p->octave=_ogg_malloc(n*sizeof(*p->octave));
  183. p->bark=_ogg_malloc(n*sizeof(*p->bark));
  184. p->vi=vi;
  185. p->n=n;
  186. p->rate=rate;
  187. /* set up the lookups for a given blocksize and sample rate */
  188. if(vi->ath)
  189. set_curve(vi->ath, p->ath,n,rate);
  190. for(i=0;i<n;i++){
  191. float bark=toBARK(rate/(2*n)*i);
  192. for(;lo+vi->noisewindowlomin<i &&
  193. toBARK(rate/(2*n)*lo)<(bark-vi->noisewindowlo);lo++);
  194. for(;hi<n && (hi<i+vi->noisewindowhimin ||
  195. toBARK(rate/(2*n)*hi)<(bark+vi->noisewindowhi));hi++);
  196. p->bark[i]=(lo<<16)+hi;
  197. }
  198. for(i=0;i<n;i++)
  199. p->octave[i]=toOC((i*.5f+.25f)*rate/n)*(1<<(p->shiftoc+1))+.5f;
  200. p->tonecurves=_ogg_malloc(P_BANDS*sizeof(*p->tonecurves));
  201. p->noisethresh=_ogg_malloc(n*sizeof(*p->noisethresh));
  202. p->noiseoffset=_ogg_malloc(n*sizeof(*p->noiseoffset));
  203. for(i=0;i<P_BANDS;i++)
  204. p->tonecurves[i]=_ogg_malloc(P_LEVELS*sizeof(*p->tonecurves[i]));
  205. for(i=0;i<P_BANDS;i++)
  206. for(j=0;j<P_LEVELS;j++)
  207. p->tonecurves[i][j]=_ogg_malloc((EHMER_MAX+2)*sizeof(*p->tonecurves[i][j]));
  208. /* OK, yeah, this was a silly way to do it */
  209. memcpy(p->tonecurves[0][4]+2,tone_125_40dB_SL,sizeof(*p->tonecurves[0][4])*EHMER_MAX);
  210. memcpy(p->tonecurves[0][6]+2,tone_125_60dB_SL,sizeof(*p->tonecurves[0][6])*EHMER_MAX);
  211. memcpy(p->tonecurves[0][8]+2,tone_125_80dB_SL,sizeof(*p->tonecurves[0][8])*EHMER_MAX);
  212. memcpy(p->tonecurves[0][10]+2,tone_125_100dB_SL,sizeof(*p->tonecurves[0][10])*EHMER_MAX);
  213. memcpy(p->tonecurves[2][4]+2,tone_125_40dB_SL,sizeof(*p->tonecurves[2][4])*EHMER_MAX);
  214. memcpy(p->tonecurves[2][6]+2,tone_125_60dB_SL,sizeof(*p->tonecurves[2][6])*EHMER_MAX);
  215. memcpy(p->tonecurves[2][8]+2,tone_125_80dB_SL,sizeof(*p->tonecurves[2][8])*EHMER_MAX);
  216. memcpy(p->tonecurves[2][10]+2,tone_125_100dB_SL,sizeof(*p->tonecurves[2][10])*EHMER_MAX);
  217. memcpy(p->tonecurves[4][4]+2,tone_250_40dB_SL,sizeof(*p->tonecurves[4][4])*EHMER_MAX);
  218. memcpy(p->tonecurves[4][6]+2,tone_250_60dB_SL,sizeof(*p->tonecurves[4][6])*EHMER_MAX);
  219. memcpy(p->tonecurves[4][8]+2,tone_250_80dB_SL,sizeof(*p->tonecurves[4][8])*EHMER_MAX);
  220. memcpy(p->tonecurves[4][10]+2,tone_250_100dB_SL,sizeof(*p->tonecurves[4][10])*EHMER_MAX);
  221. memcpy(p->tonecurves[6][4]+2,tone_500_40dB_SL,sizeof(*p->tonecurves[6][4])*EHMER_MAX);
  222. memcpy(p->tonecurves[6][6]+2,tone_500_60dB_SL,sizeof(*p->tonecurves[6][6])*EHMER_MAX);
  223. memcpy(p->tonecurves[6][8]+2,tone_500_80dB_SL,sizeof(*p->tonecurves[6][8])*EHMER_MAX);
  224. memcpy(p->tonecurves[6][10]+2,tone_500_100dB_SL,sizeof(*p->tonecurves[6][10])*EHMER_MAX);
  225. memcpy(p->tonecurves[8][4]+2,tone_1000_40dB_SL,sizeof(*p->tonecurves[8][4])*EHMER_MAX);
  226. memcpy(p->tonecurves[8][6]+2,tone_1000_60dB_SL,sizeof(*p->tonecurves[8][6])*EHMER_MAX);
  227. memcpy(p->tonecurves[8][8]+2,tone_1000_80dB_SL,sizeof(*p->tonecurves[8][8])*EHMER_MAX);
  228. memcpy(p->tonecurves[8][10]+2,tone_1000_100dB_SL,sizeof(*p->tonecurves[8][10])*EHMER_MAX);
  229. memcpy(p->tonecurves[10][4]+2,tone_2000_40dB_SL,sizeof(*p->tonecurves[10][4])*EHMER_MAX);
  230. memcpy(p->tonecurves[10][6]+2,tone_2000_60dB_SL,sizeof(*p->tonecurves[10][6])*EHMER_MAX);
  231. memcpy(p->tonecurves[10][8]+2,tone_2000_80dB_SL,sizeof(*p->tonecurves[10][8])*EHMER_MAX);
  232. memcpy(p->tonecurves[10][10]+2,tone_2000_100dB_SL,sizeof(*p->tonecurves[10][10])*EHMER_MAX);
  233. memcpy(p->tonecurves[12][4]+2,tone_4000_40dB_SL,sizeof(*p->tonecurves[12][4])*EHMER_MAX);
  234. memcpy(p->tonecurves[12][6]+2,tone_4000_60dB_SL,sizeof(*p->tonecurves[12][6])*EHMER_MAX);
  235. memcpy(p->tonecurves[12][8]+2,tone_4000_80dB_SL,sizeof(*p->tonecurves[12][8])*EHMER_MAX);
  236. memcpy(p->tonecurves[12][10]+2,tone_4000_100dB_SL,sizeof(*p->tonecurves[12][10])*EHMER_MAX);
  237. memcpy(p->tonecurves[14][4]+2,tone_8000_40dB_SL,sizeof(*p->tonecurves[14][4])*EHMER_MAX);
  238. memcpy(p->tonecurves[14][6]+2,tone_8000_60dB_SL,sizeof(*p->tonecurves[14][6])*EHMER_MAX);
  239. memcpy(p->tonecurves[14][8]+2,tone_8000_80dB_SL,sizeof(*p->tonecurves[14][8])*EHMER_MAX);
  240. memcpy(p->tonecurves[14][10]+2,tone_8000_100dB_SL,sizeof(*p->tonecurves[14][10])*EHMER_MAX);
  241. memcpy(p->tonecurves[16][4]+2,tone_8000_40dB_SL,sizeof(*p->tonecurves[16][4])*EHMER_MAX);
  242. memcpy(p->tonecurves[16][6]+2,tone_8000_60dB_SL,sizeof(*p->tonecurves[16][6])*EHMER_MAX);
  243. memcpy(p->tonecurves[16][8]+2,tone_8000_80dB_SL,sizeof(*p->tonecurves[16][8])*EHMER_MAX);
  244. memcpy(p->tonecurves[16][10]+2,tone_8000_100dB_SL,sizeof(*p->tonecurves[16][10])*EHMER_MAX);
  245. for(i=0;i<P_BANDS;i+=2)
  246. for(j=4;j<P_LEVELS;j+=2)
  247. for(k=2;k<EHMER_MAX+2;k++)
  248. p->tonecurves[i][j][k]+=vi->tone_masteratt;
  249. /* interpolate curves between */
  250. for(i=1;i<P_BANDS;i+=2)
  251. for(j=4;j<P_LEVELS;j+=2){
  252. memcpy(p->tonecurves[i][j]+2,p->tonecurves[i-1][j]+2,EHMER_MAX*sizeof(*p->tonecurves[i][j]));
  253. /*interp_curve(p->tonecurves[i][j],
  254. p->tonecurves[i-1][j],
  255. p->tonecurves[i+1][j],.5);*/
  256. min_curve(p->tonecurves[i][j]+2,p->tonecurves[i+1][j]+2);
  257. }
  258. /* set up the final curves */
  259. for(i=0;i<P_BANDS;i++)
  260. setup_curve(p->tonecurves[i],i,vi->toneatt.block[i]);
  261. for(i=0;i<P_LEVELS;i++)
  262. _analysis_output("curve_63Hz",i,p->tonecurves[0][i]+2,EHMER_MAX,0,0);
  263. for(i=0;i<P_LEVELS;i++)
  264. _analysis_output("curve_88Hz",i,p->tonecurves[1][i]+2,EHMER_MAX,0,0);
  265. for(i=0;i<P_LEVELS;i++)
  266. _analysis_output("curve_125Hz",i,p->tonecurves[2][i]+2,EHMER_MAX,0,0);
  267. for(i=0;i<P_LEVELS;i++)
  268. _analysis_output("curve_170Hz",i,p->tonecurves[3][i]+2,EHMER_MAX,0,0);
  269. for(i=0;i<P_LEVELS;i++)
  270. _analysis_output("curve_250Hz",i,p->tonecurves[4][i]+2,EHMER_MAX,0,0);
  271. for(i=0;i<P_LEVELS;i++)
  272. _analysis_output("curve_350Hz",i,p->tonecurves[5][i]+2,EHMER_MAX,0,0);
  273. for(i=0;i<P_LEVELS;i++)
  274. _analysis_output("curve_500Hz",i,p->tonecurves[6][i]+2,EHMER_MAX,0,0);
  275. for(i=0;i<P_LEVELS;i++)
  276. _analysis_output("curve_700Hz",i,p->tonecurves[7][i]+2,EHMER_MAX,0,0);
  277. for(i=0;i<P_LEVELS;i++)
  278. _analysis_output("curve_1kHz",i,p->tonecurves[8][i]+2,EHMER_MAX,0,0);
  279. for(i=0;i<P_LEVELS;i++)
  280. _analysis_output("curve_1.4Hz",i,p->tonecurves[9][i]+2,EHMER_MAX,0,0);
  281. for(i=0;i<P_LEVELS;i++)
  282. _analysis_output("curve_2kHz",i,p->tonecurves[10][i]+2,EHMER_MAX,0,0);
  283. for(i=0;i<P_LEVELS;i++)
  284. _analysis_output("curve_2.4kHz",i,p->tonecurves[11][i]+2,EHMER_MAX,0,0);
  285. for(i=0;i<P_LEVELS;i++)
  286. _analysis_output("curve_4kHz",i,p->tonecurves[12][i]+2,EHMER_MAX,0,0);
  287. for(i=0;i<P_LEVELS;i++)
  288. _analysis_output("curve_5.6kHz",i,p->tonecurves[13][i]+2,EHMER_MAX,0,0);
  289. for(i=0;i<P_LEVELS;i++)
  290. _analysis_output("curve_8kHz",i,p->tonecurves[14][i]+2,EHMER_MAX,0,0);
  291. for(i=0;i<P_LEVELS;i++)
  292. _analysis_output("curve_11.5kHz",i,p->tonecurves[15][i]+2,EHMER_MAX,0,0);
  293. for(i=0;i<P_LEVELS;i++)
  294. _analysis_output("curve_16kHz",i,p->tonecurves[16][i]+2,EHMER_MAX,0,0);
  295. if(vi->curvelimitp){
  296. /* value limit the tonal masking curves; the peakatt not only
  297. optionally specifies maximum dynamic depth, but also
  298. limits the masking curves to a minimum depth */
  299. for(i=0;i<P_BANDS;i++)
  300. for(j=0;j<P_LEVELS;j++){
  301. for(k=2;k<EHMER_OFFSET+2+vi->curvelimitp;k++)
  302. if(p->tonecurves[i][j][k]> vi->peakatt.block[i][j])
  303. p->tonecurves[i][j][k]= vi->peakatt.block[i][j];
  304. else
  305. break;
  306. }
  307. }
  308. for(i=0;i<P_LEVELS;i++)
  309. _analysis_output("licurve_63Hz",i,p->tonecurves[0][i]+2,EHMER_MAX,0,0);
  310. for(i=0;i<P_LEVELS;i++)
  311. _analysis_output("licurve_88Hz",i,p->tonecurves[1][i]+2,EHMER_MAX,0,0);
  312. for(i=0;i<P_LEVELS;i++)
  313. _analysis_output("licurve_125Hz",i,p->tonecurves[2][i]+2,EHMER_MAX,0,0);
  314. for(i=0;i<P_LEVELS;i++)
  315. _analysis_output("licurve_170Hz",i,p->tonecurves[3][i]+2,EHMER_MAX,0,0);
  316. for(i=0;i<P_LEVELS;i++)
  317. _analysis_output("licurve_250Hz",i,p->tonecurves[4][i]+2,EHMER_MAX,0,0);
  318. for(i=0;i<P_LEVELS;i++)
  319. _analysis_output("licurve_350Hz",i,p->tonecurves[5][i]+2,EHMER_MAX,0,0);
  320. for(i=0;i<P_LEVELS;i++)
  321. _analysis_output("licurve_500Hz",i,p->tonecurves[6][i]+2,EHMER_MAX,0,0);
  322. for(i=0;i<P_LEVELS;i++)
  323. _analysis_output("licurve_700Hz",i,p->tonecurves[7][i]+2,EHMER_MAX,0,0);
  324. for(i=0;i<P_LEVELS;i++)
  325. _analysis_output("licurve_1kHz",i,p->tonecurves[8][i]+2,EHMER_MAX,0,0);
  326. for(i=0;i<P_LEVELS;i++)
  327. _analysis_output("licurve_1.4Hz",i,p->tonecurves[9][i]+2,EHMER_MAX,0,0);
  328. for(i=0;i<P_LEVELS;i++)
  329. _analysis_output("licurve_2kHz",i,p->tonecurves[10][i]+2,EHMER_MAX,0,0);
  330. for(i=0;i<P_LEVELS;i++)
  331. _analysis_output("licurve_2.4kHz",i,p->tonecurves[11][i]+2,EHMER_MAX,0,0);
  332. for(i=0;i<P_LEVELS;i++)
  333. _analysis_output("licurve_4kHz",i,p->tonecurves[12][i]+2,EHMER_MAX,0,0);
  334. for(i=0;i<P_LEVELS;i++)
  335. _analysis_output("licurve_5.6kHz",i,p->tonecurves[13][i]+2,EHMER_MAX,0,0);
  336. for(i=0;i<P_LEVELS;i++)
  337. _analysis_output("licurve_8kHz",i,p->tonecurves[14][i]+2,EHMER_MAX,0,0);
  338. for(i=0;i<P_LEVELS;i++)
  339. _analysis_output("licurve_11.5kHz",i,p->tonecurves[15][i]+2,EHMER_MAX,0,0);
  340. for(i=0;i<P_LEVELS;i++)
  341. _analysis_output("licurve_16kHz",i,p->tonecurves[16][i]+2,EHMER_MAX,0,0);
  342. if(vi->peakattp) /* we limit maximum depth only optionally */
  343. for(i=0;i<P_BANDS;i++)
  344. for(j=0;j<P_LEVELS;j++)
  345. if(p->tonecurves[i][j][EHMER_OFFSET+2]< vi->peakatt.block[i][j])
  346. p->tonecurves[i][j][EHMER_OFFSET+2]= vi->peakatt.block[i][j];
  347. for(i=0;i<P_LEVELS;i++)
  348. _analysis_output("pcurve_63Hz",i,p->tonecurves[0][i]+2,EHMER_MAX,0,0);
  349. for(i=0;i<P_LEVELS;i++)
  350. _analysis_output("pcurve_88Hz",i,p->tonecurves[1][i]+2,EHMER_MAX,0,0);
  351. for(i=0;i<P_LEVELS;i++)
  352. _analysis_output("pcurve_125Hz",i,p->tonecurves[2][i]+2,EHMER_MAX,0,0);
  353. for(i=0;i<P_LEVELS;i++)
  354. _analysis_output("pcurve_170Hz",i,p->tonecurves[3][i]+2,EHMER_MAX,0,0);
  355. for(i=0;i<P_LEVELS;i++)
  356. _analysis_output("pcurve_250Hz",i,p->tonecurves[4][i]+2,EHMER_MAX,0,0);
  357. for(i=0;i<P_LEVELS;i++)
  358. _analysis_output("pcurve_350Hz",i,p->tonecurves[5][i]+2,EHMER_MAX,0,0);
  359. for(i=0;i<P_LEVELS;i++)
  360. _analysis_output("pcurve_500Hz",i,p->tonecurves[6][i]+2,EHMER_MAX,0,0);
  361. for(i=0;i<P_LEVELS;i++)
  362. _analysis_output("pcurve_700Hz",i,p->tonecurves[7][i]+2,EHMER_MAX,0,0);
  363. for(i=0;i<P_LEVELS;i++)
  364. _analysis_output("pcurve_1kHz",i,p->tonecurves[8][i]+2,EHMER_MAX,0,0);
  365. for(i=0;i<P_LEVELS;i++)
  366. _analysis_output("pcurve_1.4Hz",i,p->tonecurves[9][i]+2,EHMER_MAX,0,0);
  367. for(i=0;i<P_LEVELS;i++)
  368. _analysis_output("pcurve_2kHz",i,p->tonecurves[10][i]+2,EHMER_MAX,0,0);
  369. for(i=0;i<P_LEVELS;i++)
  370. _analysis_output("pcurve_2.4kHz",i,p->tonecurves[11][i]+2,EHMER_MAX,0,0);
  371. for(i=0;i<P_LEVELS;i++)
  372. _analysis_output("pcurve_4kHz",i,p->tonecurves[12][i]+2,EHMER_MAX,0,0);
  373. for(i=0;i<P_LEVELS;i++)
  374. _analysis_output("pcurve_5.6kHz",i,p->tonecurves[13][i]+2,EHMER_MAX,0,0);
  375. for(i=0;i<P_LEVELS;i++)
  376. _analysis_output("pcurve_8kHz",i,p->tonecurves[14][i]+2,EHMER_MAX,0,0);
  377. for(i=0;i<P_LEVELS;i++)
  378. _analysis_output("pcurve_11.5kHz",i,p->tonecurves[15][i]+2,EHMER_MAX,0,0);
  379. for(i=0;i<P_LEVELS;i++)
  380. _analysis_output("pcurve_16kHz",i,p->tonecurves[16][i]+2,EHMER_MAX,0,0);
  381. /* but guarding is mandatory */
  382. for(i=0;i<P_BANDS;i++)
  383. for(j=0;j<P_LEVELS;j++)
  384. if(p->tonecurves[i][j][EHMER_OFFSET+2]< vi->tone_guard)
  385. p->tonecurves[i][j][EHMER_OFFSET+2]= vi->tone_guard;
  386. for(i=0;i<P_LEVELS;i++)
  387. _analysis_output("fcurve_63Hz",i,p->tonecurves[0][i]+2,EHMER_MAX,0,0);
  388. for(i=0;i<P_LEVELS;i++)
  389. _analysis_output("fcurve_88Hz",i,p->tonecurves[1][i]+2,EHMER_MAX,0,0);
  390. for(i=0;i<P_LEVELS;i++)
  391. _analysis_output("fcurve_125Hz",i,p->tonecurves[2][i]+2,EHMER_MAX,0,0);
  392. for(i=0;i<P_LEVELS;i++)
  393. _analysis_output("fcurve_170Hz",i,p->tonecurves[3][i]+2,EHMER_MAX,0,0);
  394. for(i=0;i<P_LEVELS;i++)
  395. _analysis_output("fcurve_250Hz",i,p->tonecurves[4][i]+2,EHMER_MAX,0,0);
  396. for(i=0;i<P_LEVELS;i++)
  397. _analysis_output("fcurve_350Hz",i,p->tonecurves[5][i]+2,EHMER_MAX,0,0);
  398. for(i=0;i<P_LEVELS;i++)
  399. _analysis_output("fcurve_500Hz",i,p->tonecurves[6][i]+2,EHMER_MAX,0,0);
  400. for(i=0;i<P_LEVELS;i++)
  401. _analysis_output("fcurve_700Hz",i,p->tonecurves[7][i]+2,EHMER_MAX,0,0);
  402. for(i=0;i<P_LEVELS;i++)
  403. _analysis_output("fcurve_1kHz",i,p->tonecurves[8][i]+2,EHMER_MAX,0,0);
  404. for(i=0;i<P_LEVELS;i++)
  405. _analysis_output("fcurve_1.4Hz",i,p->tonecurves[9][i]+2,EHMER_MAX,0,0);
  406. for(i=0;i<P_LEVELS;i++)
  407. _analysis_output("fcurve_2kHz",i,p->tonecurves[10][i]+2,EHMER_MAX,0,0);
  408. for(i=0;i<P_LEVELS;i++)
  409. _analysis_output("fcurve_2.4kHz",i,p->tonecurves[11][i]+2,EHMER_MAX,0,0);
  410. for(i=0;i<P_LEVELS;i++)
  411. _analysis_output("fcurve_4kHz",i,p->tonecurves[12][i]+2,EHMER_MAX,0,0);
  412. for(i=0;i<P_LEVELS;i++)
  413. _analysis_output("fcurve_5.6kHz",i,p->tonecurves[13][i]+2,EHMER_MAX,0,0);
  414. for(i=0;i<P_LEVELS;i++)
  415. _analysis_output("fcurve_8kHz",i,p->tonecurves[14][i]+2,EHMER_MAX,0,0);
  416. for(i=0;i<P_LEVELS;i++)
  417. _analysis_output("fcurve_11.5kHz",i,p->tonecurves[15][i]+2,EHMER_MAX,0,0);
  418. for(i=0;i<P_LEVELS;i++)
  419. _analysis_output("fcurve_16kHz",i,p->tonecurves[16][i]+2,EHMER_MAX,0,0);
  420. /* set up rolling noise median */
  421. for(i=0;i<n;i++){
  422. float halfoc=toOC((i+.5)*rate/(2.*n))*2.;
  423. int inthalfoc;
  424. float del;
  425. if(halfoc<0)halfoc=0;
  426. if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
  427. inthalfoc=(int)halfoc;
  428. del=halfoc-inthalfoc;
  429. p->noiseoffset[i]=
  430. p->vi->noiseoff[inthalfoc]*(1.-del) +
  431. p->vi->noiseoff[inthalfoc+1]*del;
  432. }
  433. analysis_noisy=1;
  434. _analysis_output("noiseoff",0,p->noiseoffset,n,1,0);
  435. _analysis_output("noisethresh",0,p->noisethresh,n,1,0);
  436. analysis_noisy=1;
  437. }
  438. void _vp_psy_clear(vorbis_look_psy *p){
  439. int i,j;
  440. if(p){
  441. if(p->ath)_ogg_free(p->ath);
  442. if(p->octave)_ogg_free(p->octave);
  443. if(p->bark)_ogg_free(p->bark);
  444. if(p->tonecurves){
  445. for(i=0;i<P_BANDS;i++){
  446. for(j=0;j<P_LEVELS;j++){
  447. _ogg_free(p->tonecurves[i][j]);
  448. }
  449. _ogg_free(p->tonecurves[i]);
  450. }
  451. _ogg_free(p->tonecurves);
  452. }
  453. _ogg_free(p->noiseoffset);
  454. _ogg_free(p->noisethresh);
  455. memset(p,0,sizeof(*p));
  456. }
  457. }
  458. /* octave/(8*eighth_octave_lines) x scale and dB y scale */
  459. static void seed_curve(float *seed,
  460. const float **curves,
  461. float amp,
  462. int oc, int n,
  463. int linesper,float dBoffset){
  464. int i,post1;
  465. int seedptr;
  466. const float *posts,*curve;
  467. int choice=(int)((amp+dBoffset)*.1f);
  468. choice=max(choice,0);
  469. choice=min(choice,P_LEVELS-1);
  470. posts=curves[choice];
  471. curve=posts+2;
  472. post1=(int)posts[1];
  473. seedptr=oc+(posts[0]-16)*linesper-(linesper>>1);
  474. for(i=posts[0];i<post1;i++){
  475. if(seedptr>0){
  476. float lin=amp+curve[i];
  477. if(seed[seedptr]<lin)seed[seedptr]=lin;
  478. }
  479. seedptr+=linesper;
  480. if(seedptr>=n)break;
  481. }
  482. }
  483. static void seed_loop(vorbis_look_psy *p,
  484. const float ***curves,
  485. const float *f,
  486. const float *flr,
  487. float *seed,
  488. float specmax){
  489. vorbis_info_psy *vi=p->vi;
  490. long n=p->n,i;
  491. float dBoffset=vi->max_curve_dB-specmax;
  492. /* prime the working vector with peak values */
  493. for(i=0;i<n;i++){
  494. float max=f[i];
  495. long oc=p->octave[i];
  496. while(i+1<n && p->octave[i+1]==oc){
  497. i++;
  498. if(f[i]>max)max=f[i];
  499. }
  500. if(max+6.f>flr[i]){
  501. oc=oc>>p->shiftoc;
  502. if(oc>=P_BANDS)oc=P_BANDS-1;
  503. if(oc<0)oc=0;
  504. seed_curve(seed,
  505. curves[oc],
  506. max,
  507. p->octave[i]-p->firstoc,
  508. p->total_octave_lines,
  509. p->eighth_octave_lines,
  510. dBoffset);
  511. }
  512. }
  513. }
  514. static void seed_chase(float *seeds, int linesper, long n){
  515. long *posstack=alloca(n*sizeof(*posstack));
  516. float *ampstack=alloca(n*sizeof(*ampstack));
  517. long stack=0;
  518. long pos=0;
  519. long i;
  520. for(i=0;i<n;i++){
  521. if(stack<2){
  522. posstack[stack]=i;
  523. ampstack[stack++]=seeds[i];
  524. }else{
  525. while(1){
  526. if(seeds[i]<ampstack[stack-1]){
  527. posstack[stack]=i;
  528. ampstack[stack++]=seeds[i];
  529. break;
  530. }else{
  531. if(i<posstack[stack-1]+linesper){
  532. if(stack>1 && ampstack[stack-1]<=ampstack[stack-2] &&
  533. i<posstack[stack-2]+linesper){
  534. /* we completely overlap, making stack-1 irrelevant. pop it */
  535. stack--;
  536. continue;
  537. }
  538. }
  539. posstack[stack]=i;
  540. ampstack[stack++]=seeds[i];
  541. break;
  542. }
  543. }
  544. }
  545. }
  546. /* the stack now contains only the positions that are relevant. Scan
  547. 'em straight through */
  548. for(i=0;i<stack;i++){
  549. long endpos;
  550. if(i<stack-1 && ampstack[i+1]>ampstack[i]){
  551. endpos=posstack[i+1];
  552. }else{
  553. endpos=posstack[i]+linesper+1; /* +1 is important, else bin 0 is
  554. discarded in short frames */
  555. }
  556. if(endpos>n)endpos=n;
  557. for(;pos<endpos;pos++)
  558. seeds[pos]=ampstack[i];
  559. }
  560. /* there. Linear time. I now remember this was on a problem set I
  561. had in Grad Skool... I didn't solve it at the time ;-) */
  562. }
  563. /* bleaugh, this is more complicated than it needs to be */
  564. static void max_seeds(vorbis_look_psy *p,
  565. float *seed,
  566. float *flr){
  567. long n=p->total_octave_lines;
  568. int linesper=p->eighth_octave_lines;
  569. long linpos=0;
  570. long pos;
  571. seed_chase(seed,linesper,n); /* for masking */
  572. pos=p->octave[0]-p->firstoc-(linesper>>1);
  573. while(linpos+1<p->n){
  574. float minV=seed[pos];
  575. long end=((p->octave[linpos]+p->octave[linpos+1])>>1)-p->firstoc;
  576. if(minV>p->vi->tone_abs_limit)minV=p->vi->tone_abs_limit;
  577. while(pos+1<=end){
  578. pos++;
  579. if((seed[pos]>NEGINF && seed[pos]<minV) || minV==NEGINF)
  580. minV=seed[pos];
  581. }
  582. /* seed scale is log. Floor is linear. Map back to it */
  583. end=pos+p->firstoc;
  584. for(;linpos<p->n && p->octave[linpos]<=end;linpos++)
  585. if(flr[linpos]<minV)flr[linpos]=minV;
  586. }
  587. {
  588. float minV=seed[p->total_octave_lines-1];
  589. for(;linpos<p->n;linpos++)
  590. if(flr[linpos]<minV)flr[linpos]=minV;
  591. }
  592. }
  593. static void bark_noise_hybridmp(int n,const long *b,
  594. const float *f,
  595. float *noise,
  596. const float offset,
  597. const int fixed){
  598. long i,hi=b[0]>>16,lo=b[0]>>16,hif=0,lof=0;
  599. double xa=0,xb=0;
  600. double ya=0,yb=0;
  601. double x2a=0,x2b=0;
  602. double y2a=0,y2b=0;
  603. double xya=0,xyb=0;
  604. double na=0,nb=0;
  605. for(i=0;i<n;i++){
  606. if(hi<n){
  607. /* find new lo/hi */
  608. int bi=b[i]&0xffffL;
  609. for(;hi<bi;hi++){
  610. int ii=(hi<0?-hi:hi);
  611. double bin=(f[ii]<-offset?1.:f[ii]+offset);
  612. double nn= bin*bin;
  613. na += nn;
  614. xa += hi*nn;
  615. ya += bin*nn;
  616. x2a += hi*hi*nn;
  617. y2a += bin*bin*nn;
  618. xya += hi*bin*nn;
  619. }
  620. bi=b[i]>>16;
  621. for(;lo<bi;lo++){
  622. int ii=(lo<0?-lo:lo);
  623. double bin=(f[ii]<-offset?1.:f[ii]+offset);
  624. double nn= bin*bin;
  625. na -= nn;
  626. xa -= lo*nn;
  627. ya -= bin*nn;
  628. x2a -= lo*lo*nn;
  629. y2a -= bin*bin*nn;
  630. xya -= lo*bin*nn;
  631. }
  632. }
  633. if(hif<n && fixed>0){
  634. int bi=i+fixed/2;
  635. if(bi>n)bi=n;
  636. for(;hif<bi;hif++){
  637. int ii=(hif<0?-hif:hif);
  638. double bin=(f[ii]<-offset?1.:f[ii]+offset);
  639. double nn= bin*bin;
  640. nb += nn;
  641. xb += hif*nn;
  642. yb += bin*nn;
  643. x2b += hif*hif*nn;
  644. y2b += bin*bin*nn;
  645. xyb += hif*bin*nn;
  646. }
  647. bi=i-(fixed+1)/2;
  648. for(;lof<bi;lof++){
  649. int ii=(lof<0?-lof:lof);
  650. double bin=(f[ii]<-offset?1.:f[ii]+offset);
  651. double nn= bin*bin;
  652. nb -= nn;
  653. xb -= lof*nn;
  654. yb -= bin*nn;
  655. x2b -= lof*lof*nn;
  656. y2b -= bin*bin*nn;
  657. xyb -= lof*bin*nn;
  658. }
  659. }
  660. {
  661. double va=0.f;
  662. if(na>2){
  663. double denom=1./(na*x2a-xa*xa);
  664. double a=(ya*x2a-xya*xa)*denom;
  665. double b=(na*xya-xa*ya)*denom;
  666. va=a+b*i;
  667. }
  668. if(va<0.)va=0.;
  669. if(fixed>0){
  670. double vb=0.f;
  671. if(nb>2){
  672. double denomf=1./(nb*x2b-xb*xb);
  673. double af=(yb*x2b-xyb*xb)*denomf;
  674. double bf=(nb*xyb-xb*yb)*denomf;
  675. vb=af+bf*i;
  676. }
  677. if(vb<0.)vb=0.;
  678. if(va>vb && vb>0.)va=vb;
  679. }
  680. noise[i]=va-offset;
  681. }
  682. }
  683. }
  684. void _vp_remove_floor(vorbis_look_psy *p,
  685. float *mdct,
  686. float *codedflr,
  687. float *residue){
  688. int i,n=p->n;
  689. for(i=0;i<n;i++)
  690. if(mdct[i]!=0.f)
  691. residue[i]=mdct[i]/codedflr[i];
  692. else
  693. residue[i]=0.f;
  694. }
  695. void _vp_compute_mask(vorbis_look_psy *p,
  696. float *logfft,
  697. float *logmdct,
  698. float *logmask,
  699. float global_specmax,
  700. float local_specmax,
  701. float bitrate_noise_offset){
  702. int i,n=p->n;
  703. static int seq=0;
  704. float *seed=alloca(sizeof(*seed)*p->total_octave_lines);
  705. for(i=0;i<p->total_octave_lines;i++)seed[i]=NEGINF;
  706. /* noise masking */
  707. if(p->vi->noisemaskp){
  708. float *work=alloca(n*sizeof(*work));
  709. bark_noise_hybridmp(n,p->bark,logmdct,logmask,
  710. 140.,-1);
  711. for(i=0;i<n;i++)work[i]=logmdct[i]-logmask[i];
  712. bark_noise_hybridmp(n,p->bark,work,logmask,0.,
  713. p->vi->noisewindowfixed);
  714. for(i=0;i<n;i++)work[i]=logmdct[i]-work[i];
  715. /* work[i] holds the median line (.5), logmask holds the upper
  716. envelope line (1.) */
  717. _analysis_output("noisemedian",seq,work,n,1,0);
  718. for(i=0;i<n;i++)logmask[i]+=work[i];
  719. _analysis_output("noiseenvelope",seq,logmask,n,1,0);
  720. for(i=0;i<n;i++)logmask[i]-=work[i];
  721. for(i=0;i<n;i++){
  722. int dB=logmask[i]+.5;
  723. if(dB>=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1;
  724. logmask[i]= work[i]+p->vi->noisecompand[dB]+p->noiseoffset[i]+bitrate_noise_offset;
  725. if(logmask[i]>p->vi->noisemaxsupp)logmask[i]=p->vi->noisemaxsupp;
  726. }
  727. _analysis_output("noise",seq,logmask,n,1,0);
  728. }else{
  729. for(i=0;i<n;i++)logmask[i]=NEGINF;
  730. }
  731. /* set the ATH (floating below localmax, not global max by a
  732. specified att) */
  733. if(p->vi->ath){
  734. float att=local_specmax+p->vi->ath_adjatt;
  735. if(att<p->vi->ath_maxatt)att=p->vi->ath_maxatt;
  736. for(i=0;i<n;i++){
  737. float av=p->ath[i]+att;
  738. if(av>logmask[i])logmask[i]=av;
  739. }
  740. }
  741. /* tone masking */
  742. seed_loop(p,(const float ***)p->tonecurves,logfft,logmask,seed,global_specmax);
  743. max_seeds(p,seed,logmask);
  744. /* doing this here is clean, but we need to find a faster way to do
  745. it than to just tack it on */
  746. for(i=0;i<n;i++)if(logmdct[i]>=logmask[i])break;
  747. if(i==n)
  748. for(i=0;i<n;i++)logmask[i]=NEGINF;
  749. else
  750. for(i=0;i<n;i++)
  751. logfft[i]=max(logmdct[i],logfft[i]);
  752. seq++;
  753. }
  754. float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd){
  755. vorbis_info *vi=vd->vi;
  756. codec_setup_info *ci=vi->codec_setup;
  757. vorbis_info_psy_global *gi=&ci->psy_g_param;
  758. int n=ci->blocksizes[vd->W]/2;
  759. float secs=(float)n/vi->rate;
  760. amp+=secs*gi->ampmax_att_per_sec;
  761. if(amp<-9999)amp=-9999;
  762. return(amp);
  763. }
  764. static void couple_lossless(float A, float B,
  765. float granule,float igranule,
  766. float *mag, float *ang,
  767. int flip_p){
  768. if(fabs(A)>fabs(B)){
  769. A=rint(A*igranule)*granule; /* must be done *after* the comparison */
  770. B=rint(B*igranule)*granule;
  771. *mag=A; *ang=(A>0.f?A-B:B-A);
  772. }else{
  773. A=rint(A*igranule)*granule;
  774. B=rint(B*igranule)*granule;
  775. *mag=B; *ang=(B>0.f?A-B:B-A);
  776. }
  777. if(flip_p && *ang>fabs(*mag)*1.9999f){
  778. *ang= -fabs(*mag)*2.f;
  779. *mag= -*mag;
  780. }
  781. }
  782. static void couple_point(float A, float B, float fA, float fB,
  783. float granule,float igranule,
  784. float fmag, float *mag, float *ang){
  785. float origmag=FAST_HYPOT(A*fA,B*fB),corr;
  786. if(fmag!=0.f){
  787. //float phase=rint((A-B)*.5/fmag);
  788. if(fabs(A)>fabs(B)){
  789. *mag=A;//phase=(A>0?phase:-phase);
  790. }else{
  791. *mag=B;//phase=(B>0?phase:-phase);
  792. }
  793. //switch((int)phase){
  794. //case 0:
  795. corr=origmag/FAST_HYPOT(fmag*fA,fmag*fB);
  796. *mag=rint(*mag*corr*igranule)*granule;
  797. *ang=0.f;
  798. //break;
  799. //default:
  800. //*mag=0.f;
  801. //*ang=0.f;
  802. //break;
  803. //}
  804. }else{
  805. *mag=0.f;
  806. *ang=0.f;
  807. }
  808. }
  809. void _vp_quantize_couple(vorbis_look_psy *p,
  810. vorbis_info_mapping0 *vi,
  811. float **pcm,
  812. float **sofar,
  813. float **quantized,
  814. int *nonzero,
  815. int passno){
  816. int i,j,k,n=p->n;
  817. vorbis_info_psy *info=p->vi;
  818. /* perform any requested channel coupling */
  819. for(i=0;i<vi->coupling_steps;i++){
  820. float granulem=info->couple_pass[passno].granulem;
  821. float igranulem=info->couple_pass[passno].igranulem;
  822. /* make sure coupling a zero and a nonzero channel results in two
  823. nonzero channels. */
  824. if(nonzero[vi->coupling_mag[i]] ||
  825. nonzero[vi->coupling_ang[i]]){
  826. float *pcmM=pcm[vi->coupling_mag[i]];
  827. float *pcmA=pcm[vi->coupling_ang[i]];
  828. float *floorM=pcm[vi->coupling_mag[i]]+n;
  829. float *floorA=pcm[vi->coupling_ang[i]]+n;
  830. float *sofarM=sofar[vi->coupling_mag[i]];
  831. float *sofarA=sofar[vi->coupling_ang[i]];
  832. float *qM=quantized[vi->coupling_mag[i]];
  833. float *qA=quantized[vi->coupling_ang[i]];
  834. nonzero[vi->coupling_mag[i]]=1;
  835. nonzero[vi->coupling_ang[i]]=1;
  836. for(j=0,k=0;j<n;k++){
  837. vp_couple *part=info->couple_pass[passno].couple_pass+k;
  838. float rqlimit=part->outofphase_requant_limit;
  839. float flip_p=part->outofphase_redundant_flip_p;
  840. for(;j<part->limit && j<p->n;j++){
  841. /* partition by partition; k is our by-location partition
  842. class counter */
  843. float ang,mag,fmag=max(fabs(pcmM[j]),fabs(pcmA[j]));
  844. if(fmag<part->amppost_point){
  845. couple_point(pcmM[j],pcmA[j],floorM[j],floorA[j],
  846. granulem,igranulem,fmag,&mag,&ang);
  847. }else{
  848. couple_lossless(pcmM[j],pcmA[j],
  849. granulem,igranulem,&mag,&ang,flip_p);
  850. }
  851. /* executive decision time: when requantizing and recoupling
  852. residue in order to progressively encode at finer
  853. resolution, an out of phase component that originally
  854. quntized to 2*mag can flip flop magnitude/angle if it
  855. requantizes to not-quite out of phase. If that happens,
  856. we opt not to fill in additional resolution (in order to
  857. simplify the iterative codebook design and
  858. efficiency). */
  859. qM[j]=mag-sofarM[j];
  860. qA[j]=ang-sofarA[j];
  861. if(qA[j]<-rqlimit || qA[j]>rqlimit){
  862. qM[j]=0.f;
  863. qA[j]=0.f;
  864. }
  865. }
  866. }
  867. }
  868. }
  869. }