psy.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  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 "psy_table.h"
  23. #include "os.h"
  24. #include "lpc.h"
  25. #include "smallft.h"
  26. #include "scales.h"
  27. #include "misc.h"
  28. #define NEGINF -9999.f
  29. /*
  30. rephase = reverse phase limit (postpoint)
  31. 0 1 2 3 4 5 6 7 8 */
  32. static double stereo_threshholds[]= {0.0, 0.5, 1.0, 1.5, 2.5, 4.5, 8.5,16.5, 9e10};
  33. static double stereo_threshholds_rephase[]= {0.0, 0.5, 0.5, 1.0, 1.5, 1.5, 2.5, 2.5, 9e10};
  34. static double stereo_threshholds_low[]= {0.0, 0.5, 0.5, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0};
  35. static double stereo_threshholds_high[]= {0.0, 0.5, 0.5, 0.5, 1.0, 3.0, 5.5, 8.5, 0.0};
  36. static int m3n32[] = {21,13,10,4};
  37. static int m3n44[] = {15,9,7,3};
  38. static int m3n48[] = {14,8,6,3};
  39. static int m3n32x2[] = {42,26,20,8};
  40. static int m3n44x2[] = {30,18,14,6};
  41. static int m3n48x2[] = {28,16,12,6};
  42. static int freq_bfn128[128] = {
  43. 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
  44. 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
  45. 8, 8, 8, 8, 9, 9, 9, 9,10,10,10,10,11,11,11,11,
  46. 12,12,12,12,13,13,13,13,14,14,14,14,15,15,15,15,
  47. 16,16,16,16,17,17,17,17,18,18,18,18,19,19,19,19,
  48. 20,20,20,20,21,21,21,21,22,22,22,22,23,23,23,23,
  49. 24,24,24,24,25,25,25,24,23,22,21,20,19,18,17,16,
  50. 15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
  51. };
  52. static int freq_bfn256[256] = {
  53. 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
  54. 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
  55. 8, 8, 8, 8, 9, 9, 9, 9,10,10,10,10,11,11,11,11,
  56. 12,12,12,12,13,13,13,13,14,14,14,14,15,15,15,15,
  57. 16,16,16,16,17,17,17,17,18,18,18,18,19,19,19,19,
  58. 20,20,20,20,21,21,21,21,22,22,22,22,23,23,23,23,
  59. 24,24,24,24,25,25,25,25,26,26,26,26,27,27,27,27,
  60. 28,28,28,28,29,29,29,29,30,30,30,30,31,31,31,31,
  61. 32,32,32,32,33,33,33,33,34,34,34,34,35,35,35,35,
  62. 36,36,36,36,37,37,37,37,38,38,38,38,39,39,39,39,
  63. 40,40,40,40,41,41,41,41,42,42,42,42,43,43,43,43,
  64. 44,44,44,44,45,45,45,45,46,46,46,46,47,47,47,47,
  65. 48,48,48,48,49,49,49,49,50,50,50,50,51,50,49,48,
  66. 47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,
  67. 31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,
  68. 15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
  69. };
  70. static float nnmid_th=0.3;
  71. vorbis_look_psy_global *_vp_global_look(vorbis_info *vi){
  72. codec_setup_info *ci=vi->codec_setup;
  73. vorbis_info_psy_global *gi=&ci->psy_g_param;
  74. vorbis_look_psy_global *look=_ogg_calloc(1,sizeof(*look));
  75. look->channels=vi->channels;
  76. look->ampmax=-9999.;
  77. look->gi=gi;
  78. return(look);
  79. }
  80. void _vp_global_free(vorbis_look_psy_global *look){
  81. if(look){
  82. memset(look,0,sizeof(*look));
  83. _ogg_free(look);
  84. }
  85. }
  86. void _vi_gpsy_free(vorbis_info_psy_global *i){
  87. if(i){
  88. memset(i,0,sizeof(*i));
  89. _ogg_free(i);
  90. }
  91. }
  92. void _vi_psy_free(vorbis_info_psy *i){
  93. if(i){
  94. memset(i,0,sizeof(*i));
  95. _ogg_free(i);
  96. }
  97. }
  98. static void min_curve(float *c,
  99. float *c2){
  100. int i;
  101. for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
  102. }
  103. static void max_curve(float *c,
  104. float *c2){
  105. int i;
  106. for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
  107. }
  108. static void attenuate_curve(float *c,float att){
  109. int i;
  110. for(i=0;i<EHMER_MAX;i++)
  111. c[i]+=att;
  112. }
  113. static float ***setup_tone_curves(float curveatt_dB[P_BANDS],float binHz,int n,
  114. float center_boost, float center_decay_rate){
  115. int i,j,k,m;
  116. float ath[EHMER_MAX];
  117. float workc[P_BANDS][P_LEVELS][EHMER_MAX];
  118. float athc[P_LEVELS][EHMER_MAX];
  119. float *brute_buffer=alloca(n*sizeof(*brute_buffer));
  120. float ***ret=_ogg_malloc(sizeof(*ret)*P_BANDS);
  121. memset(workc,0,sizeof(workc));
  122. for(i=0;i<P_BANDS;i++){
  123. /* we add back in the ATH to avoid low level curves falling off to
  124. -infinity and unnecessarily cutting off high level curves in the
  125. curve limiting (last step). */
  126. /* A half-band's settings must be valid over the whole band, and
  127. it's better to mask too little than too much */
  128. int ath_offset=i*4;
  129. for(j=0;j<EHMER_MAX;j++){
  130. float min=999.;
  131. for(k=0;k<4;k++)
  132. if(j+k+ath_offset<MAX_ATH){
  133. if(min>ATH[j+k+ath_offset])min=ATH[j+k+ath_offset];
  134. }else{
  135. if(min>ATH[MAX_ATH-1])min=ATH[MAX_ATH-1];
  136. }
  137. ath[j]=min;
  138. }
  139. /* copy curves into working space, replicate the 50dB curve to 30
  140. and 40, replicate the 100dB curve to 110 */
  141. for(j=0;j<6;j++)
  142. memcpy(workc[i][j+2],tonemasks[i][j],EHMER_MAX*sizeof(*tonemasks[i][j]));
  143. memcpy(workc[i][0],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0]));
  144. memcpy(workc[i][1],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0]));
  145. /* apply centered curve boost/decay */
  146. for(j=0;j<P_LEVELS;j++){
  147. for(k=0;k<EHMER_MAX;k++){
  148. float adj=center_boost+abs(EHMER_OFFSET-k)*center_decay_rate;
  149. if(adj<0. && center_boost>0)adj=0.;
  150. if(adj>0. && center_boost<0)adj=0.;
  151. workc[i][j][k]+=adj;
  152. }
  153. }
  154. /* normalize curves so the driving amplitude is 0dB */
  155. /* make temp curves with the ATH overlayed */
  156. for(j=0;j<P_LEVELS;j++){
  157. attenuate_curve(workc[i][j],curveatt_dB[i]+100.-(j<2?2:j)*10.-P_LEVEL_0);
  158. memcpy(athc[j],ath,EHMER_MAX*sizeof(**athc));
  159. attenuate_curve(athc[j],+100.-j*10.f-P_LEVEL_0);
  160. max_curve(athc[j],workc[i][j]);
  161. }
  162. /* Now limit the louder curves.
  163. the idea is this: We don't know what the playback attenuation
  164. will be; 0dB SL moves every time the user twiddles the volume
  165. knob. So that means we have to use a single 'most pessimal' curve
  166. for all masking amplitudes, right? Wrong. The *loudest* sound
  167. can be in (we assume) a range of ...+100dB] SL. However, sounds
  168. 20dB down will be in a range ...+80], 40dB down is from ...+60],
  169. etc... */
  170. for(j=1;j<P_LEVELS;j++){
  171. min_curve(athc[j],athc[j-1]);
  172. min_curve(workc[i][j],athc[j]);
  173. }
  174. }
  175. for(i=0;i<P_BANDS;i++){
  176. int hi_curve,lo_curve,bin;
  177. ret[i]=_ogg_malloc(sizeof(**ret)*P_LEVELS);
  178. /* low frequency curves are measured with greater resolution than
  179. the MDCT/FFT will actually give us; we want the curve applied
  180. to the tone data to be pessimistic and thus apply the minimum
  181. masking possible for a given bin. That means that a single bin
  182. could span more than one octave and that the curve will be a
  183. composite of multiple octaves. It also may mean that a single
  184. bin may span > an eighth of an octave and that the eighth
  185. octave values may also be composited. */
  186. /* which octave curves will we be compositing? */
  187. bin=floor(fromOC(i*.5)/binHz);
  188. lo_curve= ceil(toOC(bin*binHz+1)*2);
  189. hi_curve= floor(toOC((bin+1)*binHz)*2);
  190. if(lo_curve>i)lo_curve=i;
  191. if(lo_curve<0)lo_curve=0;
  192. if(hi_curve>=P_BANDS)hi_curve=P_BANDS-1;
  193. for(m=0;m<P_LEVELS;m++){
  194. ret[i][m]=_ogg_malloc(sizeof(***ret)*(EHMER_MAX+2));
  195. for(j=0;j<n;j++)brute_buffer[j]=999.;
  196. /* render the curve into bins, then pull values back into curve.
  197. The point is that any inherent subsampling aliasing results in
  198. a safe minimum */
  199. for(k=lo_curve;k<=hi_curve;k++){
  200. int l=0;
  201. for(j=0;j<EHMER_MAX;j++){
  202. int lo_bin= fromOC(j*.125+k*.5-2.0625)/binHz;
  203. int hi_bin= fromOC(j*.125+k*.5-1.9375)/binHz+1;
  204. if(lo_bin<0)lo_bin=0;
  205. if(lo_bin>n)lo_bin=n;
  206. if(lo_bin<l)l=lo_bin;
  207. if(hi_bin<0)hi_bin=0;
  208. if(hi_bin>n)hi_bin=n;
  209. for(;l<hi_bin && l<n;l++)
  210. if(brute_buffer[l]>workc[k][m][j])
  211. brute_buffer[l]=workc[k][m][j];
  212. }
  213. for(;l<n;l++)
  214. if(brute_buffer[l]>workc[k][m][EHMER_MAX-1])
  215. brute_buffer[l]=workc[k][m][EHMER_MAX-1];
  216. }
  217. /* be equally paranoid about being valid up to next half ocatve */
  218. if(i+1<P_BANDS){
  219. int l=0;
  220. k=i+1;
  221. for(j=0;j<EHMER_MAX;j++){
  222. int lo_bin= fromOC(j*.125+i*.5-2.0625)/binHz;
  223. int hi_bin= fromOC(j*.125+i*.5-1.9375)/binHz+1;
  224. if(lo_bin<0)lo_bin=0;
  225. if(lo_bin>n)lo_bin=n;
  226. if(lo_bin<l)l=lo_bin;
  227. if(hi_bin<0)hi_bin=0;
  228. if(hi_bin>n)hi_bin=n;
  229. for(;l<hi_bin && l<n;l++)
  230. if(brute_buffer[l]>workc[k][m][j])
  231. brute_buffer[l]=workc[k][m][j];
  232. }
  233. for(;l<n;l++)
  234. if(brute_buffer[l]>workc[k][m][EHMER_MAX-1])
  235. brute_buffer[l]=workc[k][m][EHMER_MAX-1];
  236. }
  237. for(j=0;j<EHMER_MAX;j++){
  238. int bin=fromOC(j*.125+i*.5-2.)/binHz;
  239. if(bin<0){
  240. ret[i][m][j+2]=-999.;
  241. }else{
  242. if(bin>=n){
  243. ret[i][m][j+2]=-999.;
  244. }else{
  245. ret[i][m][j+2]=brute_buffer[bin];
  246. }
  247. }
  248. }
  249. /* add fenceposts */
  250. for(j=0;j<EHMER_OFFSET;j++)
  251. if(ret[i][m][j+2]>-200.f)break;
  252. ret[i][m][0]=j;
  253. for(j=EHMER_MAX-1;j>EHMER_OFFSET+1;j--)
  254. if(ret[i][m][j+2]>-200.f)
  255. break;
  256. ret[i][m][1]=j;
  257. }
  258. }
  259. return(ret);
  260. }
  261. void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,
  262. vorbis_info_psy_global *gi,int n,long rate){
  263. long i,j,lo=-99,hi=1;
  264. long maxoc;
  265. memset(p,0,sizeof(*p));
  266. p->eighth_octave_lines=gi->eighth_octave_lines;
  267. p->shiftoc=rint(log(gi->eighth_octave_lines*8.f)/log(2.f))-1;
  268. p->firstoc=toOC(.25f*rate*.5/n)*(1<<(p->shiftoc+1))-gi->eighth_octave_lines;
  269. maxoc=toOC((n+.25f)*rate*.5/n)*(1<<(p->shiftoc+1))+.5f;
  270. p->total_octave_lines=maxoc-p->firstoc+1;
  271. p->ath=_ogg_malloc(n*sizeof(*p->ath));
  272. p->octave=_ogg_malloc(n*sizeof(*p->octave));
  273. p->bark=_ogg_malloc(n*sizeof(*p->bark));
  274. p->vi=vi;
  275. p->n=n;
  276. p->rate=rate;
  277. /* AoTuV HF weighting etc. */
  278. p->n25p=n/4;
  279. p->n33p=n/3;
  280. p->n75p=n*3/4;
  281. p->nn25pt=vi->normal_partition/4;
  282. p->nn50pt=p->nn25pt*2;
  283. p->nn75pt=p->nn25pt*3;
  284. if(rate < 26000){
  285. /* below 26kHz */
  286. p->m_val = 0;
  287. for(i=0; i<4; i++) p->m3n[i] = 0;
  288. p->tonecomp_endp=0; // dummy
  289. p->tonecomp_thres=.25;
  290. p->st_freqlimit=n;
  291. p->min_nn_lp=0; p->nn_mec_s= 0; p->nn_mec_m= 0;
  292. }else if(rate < 38000){
  293. /* 32kHz */
  294. p->m_val = .93;
  295. if(n==128) { p->tonecomp_endp= 124; p->tonecomp_thres=.7;
  296. p->st_freqlimit=n; p->min_nn_lp=120;
  297. p->nn_mec_s = .48; p->nn_mec_m =.24;
  298. for(i=0; i<4; i++) p->m3n[i] = m3n32[i];}
  299. else if(n==256) { p->tonecomp_endp= 248; p->tonecomp_thres=.7;
  300. p->st_freqlimit=n; p->min_nn_lp=240;
  301. p->nn_mec_s = .48; p->nn_mec_m =.24;
  302. for(i=0; i<4; i++) p->m3n[i] = m3n32x2[i];}
  303. else if(n==1024){ p->tonecomp_endp= 992; p->tonecomp_thres=.7;
  304. p->st_freqlimit=n; p->min_nn_lp=960;
  305. p->nn_mec_s =.24; p->nn_mec_m =.12;}
  306. else if(n==2048){ p->tonecomp_endp=1984; p->tonecomp_thres=.7;
  307. p->st_freqlimit=n; p->min_nn_lp=1920;
  308. p->nn_mec_s =.24; p->nn_mec_m =.12;}
  309. }else if(rate > 46000){
  310. /* 48kHz */
  311. p->m_val = 1.205;
  312. if(n==128) { p->tonecomp_endp= 83; p->tonecomp_thres=.7;
  313. p->st_freqlimit= 88; p->min_nn_lp= 80;
  314. p->nn_mec_s = .48; p->nn_mec_m =.24;
  315. for(i=0; i<4; i++) p->m3n[i] = m3n48[i];}
  316. else if(n==256) { p->tonecomp_endp= 166; p->tonecomp_thres=.7;
  317. p->st_freqlimit= 176; p->min_nn_lp= 160;
  318. p->nn_mec_s = .48; p->nn_mec_m =.24;
  319. for(i=0; i<4; i++) p->m3n[i] = m3n48x2[i];}
  320. else if(n==1024){ p->tonecomp_endp= 664; p->tonecomp_thres=.7;
  321. p->st_freqlimit= 704; p->min_nn_lp= 640;
  322. p->nn_mec_s =.24; p->nn_mec_m =.12;}
  323. else if(n==2048){ p->tonecomp_endp=1328; p->tonecomp_thres=.7;
  324. p->st_freqlimit=1408; p->min_nn_lp=1280;
  325. p->nn_mec_s =.24; p->nn_mec_m =.12;}
  326. }else{
  327. /* 44.1kHz */
  328. p->m_val = 1.;
  329. if(n==128) { p->tonecomp_endp= 90; p->tonecomp_thres=.7; //15503.90625
  330. p->st_freqlimit= 96; p->min_nn_lp= 87; //16537.5 , 14987.109375
  331. p->nn_mec_s = .48; p->nn_mec_m =.24;
  332. for(i=0; i<4; i++) p->m3n[i] = m3n44[i];}
  333. else if(n==256) { p->tonecomp_endp= 180; p->tonecomp_thres=.7;
  334. p->st_freqlimit= 192; p->min_nn_lp= 174;
  335. p->nn_mec_s = .48; p->nn_mec_m =.24;
  336. for(i=0; i<4; i++) p->m3n[i] = m3n44x2[i];}
  337. else if(n==1024){ p->tonecomp_endp= 720; p->tonecomp_thres=.7;
  338. p->st_freqlimit= 768; p->min_nn_lp= 696;
  339. p->nn_mec_s =.24; p->nn_mec_m =.12;}
  340. else if(n==2048){ p->tonecomp_endp=1440; p->tonecomp_thres=.7;
  341. p->st_freqlimit=1536; p->min_nn_lp=1392;
  342. p->nn_mec_s =.24; p->nn_mec_m =.12;}
  343. }
  344. /* set up the lookups for a given blocksize and sample rate */
  345. j=0;
  346. for(i=0;i<MAX_ATH-1;i++){
  347. int endpos=rint(fromOC((i+1)*.125-2.)*2*n/rate);
  348. float base=ATH[i];
  349. if(j<endpos){
  350. float delta=(ATH[i+1]-base)/(endpos-j);
  351. for(;j<endpos && j<n;j++){
  352. p->ath[j]=base+100.;
  353. base+=delta;
  354. }
  355. }
  356. }
  357. for(i=j; i<n; i++){
  358. p->ath[i]=p->ath[j-1];
  359. }
  360. for(i=0;i<n;i++){
  361. float bark=toBARK(rate/(2*n)*i);
  362. for(;lo+vi->noisewindowlomin<i &&
  363. toBARK(rate/(2*n)*lo)<(bark-vi->noisewindowlo);lo++);
  364. for(;hi<=n && (hi<i+vi->noisewindowhimin ||
  365. toBARK(rate/(2*n)*hi)<(bark+vi->noisewindowhi));hi++);
  366. p->bark[i]=((lo-1)<<16)+(hi-1);
  367. }
  368. for(i=0;i<n;i++)
  369. p->octave[i]=toOC((i+.25f)*.5*rate/n)*(1<<(p->shiftoc+1))+.5f;
  370. p->tonecurves=setup_tone_curves(vi->toneatt,rate*.5/n,n,
  371. vi->tone_centerboost,vi->tone_decay);
  372. /* set up rolling noise median */
  373. p->noiseoffset=_ogg_malloc(P_NOISECURVES*sizeof(*p->noiseoffset));
  374. for(i=0;i<P_NOISECURVES;i++)
  375. p->noiseoffset[i]=_ogg_malloc(n*sizeof(**p->noiseoffset));
  376. for(i=0;i<n;i++){
  377. float halfoc=toOC((i+.5)*rate/(2.*n))*2.;
  378. int inthalfoc;
  379. float del;
  380. if(halfoc<0)halfoc=0;
  381. if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
  382. inthalfoc=(int)halfoc;
  383. del=halfoc-inthalfoc;
  384. for(j=0;j<P_NOISECURVES;j++)
  385. p->noiseoffset[j][i]=
  386. p->vi->noiseoff[j][inthalfoc]*(1.-del) +
  387. p->vi->noiseoff[j][inthalfoc+1]*del;
  388. }
  389. #if 0
  390. {
  391. static int ls=0;
  392. _analysis_output_always("noiseoff0",ls,p->noiseoffset[0],n,1,0,0);
  393. _analysis_output_always("noiseoff1",ls,p->noiseoffset[1],n,1,0,0);
  394. _analysis_output_always("noiseoff2",ls++,p->noiseoffset[2],n,1,0,0);
  395. }
  396. #endif
  397. }
  398. void _vp_psy_clear(vorbis_look_psy *p){
  399. int i,j;
  400. if(p){
  401. if(p->ath)_ogg_free(p->ath);
  402. if(p->octave)_ogg_free(p->octave);
  403. if(p->bark)_ogg_free(p->bark);
  404. if(p->tonecurves){
  405. for(i=0;i<P_BANDS;i++){
  406. for(j=0;j<P_LEVELS;j++){
  407. _ogg_free(p->tonecurves[i][j]);
  408. }
  409. _ogg_free(p->tonecurves[i]);
  410. }
  411. _ogg_free(p->tonecurves);
  412. }
  413. if(p->noiseoffset){
  414. for(i=0;i<P_NOISECURVES;i++){
  415. _ogg_free(p->noiseoffset[i]);
  416. }
  417. _ogg_free(p->noiseoffset);
  418. }
  419. memset(p,0,sizeof(*p));
  420. }
  421. }
  422. /* octave/(8*eighth_octave_lines) x scale and dB y scale */
  423. static void seed_curve(float *seed,
  424. const float **curves,
  425. float amp,
  426. int oc, int n,
  427. int linesper,float dBoffset){
  428. int i,post1;
  429. int seedptr;
  430. const float *posts,*curve;
  431. int choice=(int)((amp+dBoffset-P_LEVEL_0)*.1f);
  432. choice=max(choice,0);
  433. choice=min(choice,P_LEVELS-1);
  434. posts=curves[choice];
  435. curve=posts+2;
  436. post1=(int)posts[1];
  437. seedptr=oc+(posts[0]-EHMER_OFFSET)*linesper-(linesper>>1);
  438. for(i=posts[0];i<post1;i++){
  439. if(seedptr>0){
  440. float lin=amp+curve[i];
  441. if(seed[seedptr]<lin)seed[seedptr]=lin;
  442. }
  443. seedptr+=linesper;
  444. if(seedptr>=n)break;
  445. }
  446. }
  447. static void seed_loop(vorbis_look_psy *p,
  448. const float ***curves,
  449. const float *f,
  450. const float *flr,
  451. float *seed,
  452. float specmax){
  453. vorbis_info_psy *vi=p->vi;
  454. long n=p->n,i;
  455. float dBoffset=vi->max_curve_dB-specmax;
  456. /* prime the working vector with peak values */
  457. for(i=0;i<n;i++){
  458. float max=f[i];
  459. long oc=p->octave[i];
  460. while(i+1<n && p->octave[i+1]==oc){
  461. i++;
  462. if(f[i]>max)max=f[i];
  463. }
  464. if(max+6.f>flr[i]){
  465. oc=oc>>p->shiftoc;
  466. if(oc>=P_BANDS)oc=P_BANDS-1;
  467. if(oc<0)oc=0;
  468. seed_curve(seed,
  469. curves[oc],
  470. max,
  471. p->octave[i]-p->firstoc,
  472. p->total_octave_lines,
  473. p->eighth_octave_lines,
  474. dBoffset);
  475. }
  476. }
  477. }
  478. static void seed_chase(float *seeds, int linesper, long n){
  479. long *posstack=alloca(n*sizeof(*posstack));
  480. float *ampstack=alloca(n*sizeof(*ampstack));
  481. long stack=0;
  482. long pos=0;
  483. long i;
  484. for(i=0;i<n;i++){
  485. if(stack<2){
  486. posstack[stack]=i;
  487. ampstack[stack++]=seeds[i];
  488. }else{
  489. while(1){
  490. if(seeds[i]<ampstack[stack-1]){
  491. posstack[stack]=i;
  492. ampstack[stack++]=seeds[i];
  493. break;
  494. }else{
  495. if(i<posstack[stack-1]+linesper){
  496. if(stack>1 && ampstack[stack-1]<=ampstack[stack-2] &&
  497. i<posstack[stack-2]+linesper){
  498. /* we completely overlap, making stack-1 irrelevant. pop it */
  499. stack--;
  500. continue;
  501. }
  502. }
  503. posstack[stack]=i;
  504. ampstack[stack++]=seeds[i];
  505. break;
  506. }
  507. }
  508. }
  509. }
  510. /* the stack now contains only the positions that are relevant. Scan
  511. 'em straight through */
  512. for(i=0;i<stack;i++){
  513. long endpos;
  514. if(i<stack-1 && ampstack[i+1]>ampstack[i]){
  515. endpos=posstack[i+1];
  516. }else{
  517. endpos=posstack[i]+linesper+1; /* +1 is important, else bin 0 is
  518. discarded in short frames */
  519. }
  520. if(endpos>n)endpos=n;
  521. for(;pos<endpos;pos++)
  522. seeds[pos]=ampstack[i];
  523. }
  524. /* there. Linear time. I now remember this was on a problem set I
  525. had in Grad Skool... I didn't solve it at the time ;-) */
  526. }
  527. /* bleaugh, this is more complicated than it needs to be */
  528. #include<stdio.h>
  529. static void max_seeds(vorbis_look_psy *p,
  530. float *seed,
  531. float *flr){
  532. long n=p->total_octave_lines;
  533. int linesper=p->eighth_octave_lines;
  534. long linpos=0;
  535. long pos;
  536. seed_chase(seed,linesper,n); /* for masking */
  537. pos=p->octave[0]-p->firstoc-(linesper>>1);
  538. while(linpos+1<p->n){
  539. float minV=seed[pos];
  540. long end=((p->octave[linpos]+p->octave[linpos+1])>>1)-p->firstoc;
  541. if(minV>p->vi->tone_abs_limit)minV=p->vi->tone_abs_limit;
  542. while(pos+1<=end){
  543. pos++;
  544. if((seed[pos]>NEGINF && seed[pos]<minV) || minV==NEGINF)
  545. minV=seed[pos];
  546. }
  547. end=pos+p->firstoc;
  548. for(;linpos<p->n && p->octave[linpos]<=end;linpos++)
  549. if(flr[linpos]<minV)flr[linpos]=minV;
  550. }
  551. {
  552. float minV=seed[p->total_octave_lines-1];
  553. for(;linpos<p->n;linpos++)
  554. if(flr[linpos]<minV)flr[linpos]=minV;
  555. }
  556. }
  557. static void bark_noise_hybridmp(int n,const long *b,
  558. const float *f,
  559. float *noise,
  560. const float offset,
  561. const int fixed){
  562. float *N=alloca(n*sizeof(*N));
  563. float *X=alloca(n*sizeof(*N));
  564. float *XX=alloca(n*sizeof(*N));
  565. float *Y=alloca(n*sizeof(*N));
  566. float *XY=alloca(n*sizeof(*N));
  567. float tN, tX, tXX, tY, tXY;
  568. int i;
  569. int lo, hi;
  570. float R=0.f;
  571. float A=0.f;
  572. float B=0.f;
  573. float D=1.f;
  574. float w, x, y;
  575. tN = tX = tXX = tY = tXY = 0.f;
  576. y = f[0] + offset;
  577. if (y < 1.f) y = 1.f;
  578. w = y * y * .5;
  579. tN += w;
  580. tX += w;
  581. tY += w * y;
  582. N[0] = tN;
  583. X[0] = tX;
  584. XX[0] = tXX;
  585. Y[0] = tY;
  586. XY[0] = tXY;
  587. for (i = 1, x = 1.f; i < n; i++, x += 1.f) {
  588. y = f[i] + offset;
  589. if (y < 1.f) y = 1.f;
  590. w = y * y;
  591. tN += w;
  592. tX += w * x;
  593. tXX += w * x * x;
  594. tY += w * y;
  595. tXY += w * x * y;
  596. N[i] = tN;
  597. X[i] = tX;
  598. XX[i] = tXX;
  599. Y[i] = tY;
  600. XY[i] = tXY;
  601. }
  602. for (i = 0, x = 0.f;; i++, x += 1.f) {
  603. lo = b[i] >> 16;
  604. if( lo>=0 ) break;
  605. hi = b[i] & 0xffff;
  606. tN = N[hi] + N[-lo];
  607. tX = X[hi] - X[-lo];
  608. tXX = XX[hi] + XX[-lo];
  609. tY = Y[hi] + Y[-lo];
  610. tXY = XY[hi] - XY[-lo];
  611. A = tY * tXX - tX * tXY;
  612. B = tN * tXY - tX * tY;
  613. D = tN * tXX - tX * tX;
  614. R = (A + x * B) / D;
  615. if (R < 0.f)
  616. R = 0.f;
  617. noise[i] = R - offset;
  618. }
  619. for ( ;; i++, x += 1.f) {
  620. lo = b[i] >> 16;
  621. hi = b[i] & 0xffff;
  622. if(hi>=n)break;
  623. tN = N[hi] - N[lo];
  624. tX = X[hi] - X[lo];
  625. tXX = XX[hi] - XX[lo];
  626. tY = Y[hi] - Y[lo];
  627. tXY = XY[hi] - XY[lo];
  628. A = tY * tXX - tX * tXY;
  629. B = tN * tXY - tX * tY;
  630. D = tN * tXX - tX * tX;
  631. R = (A + x * B) / D;
  632. if (R < 0.f) R = 0.f;
  633. noise[i] = R - offset;
  634. }
  635. for ( ; i < n; i++, x += 1.f) {
  636. R = (A + x * B) / D;
  637. if (R < 0.f) R = 0.f;
  638. noise[i] = R - offset;
  639. }
  640. if (fixed <= 0) return;
  641. for (i = 0, x = 0.f;; i++, x += 1.f) {
  642. hi = i + fixed / 2;
  643. lo = hi - fixed;
  644. if(lo>=0)break;
  645. tN = N[hi] + N[-lo];
  646. tX = X[hi] - X[-lo];
  647. tXX = XX[hi] + XX[-lo];
  648. tY = Y[hi] + Y[-lo];
  649. tXY = XY[hi] - XY[-lo];
  650. A = tY * tXX - tX * tXY;
  651. B = tN * tXY - tX * tY;
  652. D = tN * tXX - tX * tX;
  653. R = (A + x * B) / D;
  654. if (R - offset < noise[i]) noise[i] = R - offset;
  655. }
  656. for ( ;; i++, x += 1.f) {
  657. hi = i + fixed / 2;
  658. lo = hi - fixed;
  659. if(hi>=n)break;
  660. tN = N[hi] - N[lo];
  661. tX = X[hi] - X[lo];
  662. tXX = XX[hi] - XX[lo];
  663. tY = Y[hi] - Y[lo];
  664. tXY = XY[hi] - XY[lo];
  665. A = tY * tXX - tX * tXY;
  666. B = tN * tXY - tX * tY;
  667. D = tN * tXX - tX * tX;
  668. R = (A + x * B) / D;
  669. if (R - offset < noise[i]) noise[i] = R - offset;
  670. }
  671. for ( ; i < n; i++, x += 1.f) {
  672. R = (A + x * B) / D;
  673. if (R - offset < noise[i]) noise[i] = R - offset;
  674. }
  675. }
  676. static float FLOOR1_fromdB_INV_LOOKUP[256]={
  677. 0.F, 8.81683e+06F, 8.27882e+06F, 7.77365e+06F, // 1-4
  678. 7.29930e+06F, 6.85389e+06F, 6.43567e+06F, 6.04296e+06F, // 5-8
  679. 5.67422e+06F, 5.32798e+06F, 5.00286e+06F, 4.69759e+06F, // 9-12
  680. 4.41094e+06F, 4.14178e+06F, 3.88905e+06F, 3.65174e+06F, // 13-16
  681. 3.42891e+06F, 3.21968e+06F, 3.02321e+06F, 2.83873e+06F, // 17-20
  682. 2.66551e+06F, 2.50286e+06F, 2.35014e+06F, 2.20673e+06F, // 21-24
  683. 2.07208e+06F, 1.94564e+06F, 1.82692e+06F, 1.71544e+06F, // 25-28
  684. 1.61076e+06F, 1.51247e+06F, 1.42018e+06F, 1.33352e+06F, // 29-32
  685. 1.25215e+06F, 1.17574e+06F, 1.10400e+06F, 1.03663e+06F, // 33-36
  686. 973377.F, 913981.F, 858210.F, 805842.F, // 37-40
  687. 756669.F, 710497.F, 667142.F, 626433.F, // 41-44
  688. 588208.F, 552316.F, 518613.F, 486967.F, // 45-48
  689. 457252.F, 429351.F, 403152.F, 378551.F, // 49-52
  690. 355452.F, 333762.F, 313396.F, 294273.F, // 53-56
  691. 276316.F, 259455.F, 243623.F, 228757.F, // 57-60
  692. 214798.F, 201691.F, 189384.F, 177828.F, // 61-64
  693. 166977.F, 156788.F, 147221.F, 138237.F, // 65-68
  694. 129802.F, 121881.F, 114444.F, 107461.F, // 69-72
  695. 100903.F, 94746.3F, 88964.9F, 83536.2F, // 73-76
  696. 78438.8F, 73652.5F, 69158.2F, 64938.1F, // 77-80
  697. 60975.6F, 57254.9F, 53761.2F, 50480.6F, // 81-84
  698. 47400.3F, 44507.9F, 41792.0F, 39241.9F, // 85-88
  699. 36847.3F, 34598.9F, 32487.7F, 30505.3F, // 89-92
  700. 28643.8F, 26896.0F, 25254.8F, 23713.7F, // 93-96
  701. 22266.7F, 20908.0F, 19632.2F, 18434.2F, // 97-100
  702. 17309.4F, 16253.1F, 15261.4F, 14330.1F, // 101-104
  703. 13455.7F, 12634.6F, 11863.7F, 11139.7F, // 105-108
  704. 10460.0F, 9821.72F, 9222.39F, 8659.64F, // 109-112
  705. 8131.23F, 7635.06F, 7169.17F, 6731.70F, // 113-116
  706. 6320.93F, 5935.23F, 5573.06F, 5232.99F, // 117-120
  707. 4913.67F, 4613.84F, 4332.30F, 4067.94F, // 121-124
  708. 3819.72F, 3586.64F, 3367.78F, 3162.28F, // 125-128
  709. 2969.31F, 2788.13F, 2617.99F, 2458.24F, // 129-132
  710. 2308.24F, 2167.39F, 2035.14F, 1910.95F, // 133-136
  711. 1794.35F, 1684.85F, 1582.04F, 1485.51F, // 137-140
  712. 1394.86F, 1309.75F, 1229.83F, 1154.78F, // 141-144
  713. 1084.32F, 1018.15F, 956.024F, 897.687F, // 145-148
  714. 842.910F, 791.475F, 743.179F, 697.830F, // 149-152
  715. 655.249F, 615.265F, 577.722F, 542.469F, // 153-156
  716. 509.367F, 478.286F, 449.101F, 421.696F, // 157-160
  717. 395.964F, 371.803F, 349.115F, 327.812F, // 161-164
  718. 307.809F, 289.026F, 271.390F, 254.830F, // 165-168
  719. 239.280F, 224.679F, 210.969F, 198.096F, // 169-172
  720. 186.008F, 174.658F, 164.000F, 153.993F, // 173-176
  721. 144.596F, 135.773F, 127.488F, 119.708F, // 177-180
  722. 112.404F, 105.545F, 99.1046F, 93.0572F, // 181-184
  723. 87.3788F, 82.0469F, 77.0404F, 72.3394F, // 185-188
  724. 67.9252F, 63.7804F, 59.8885F, 56.2341F, // 189-192
  725. 52.8027F, 49.5807F, 46.5553F, 43.7144F, // 193-196
  726. 41.0470F, 38.5423F, 36.1904F, 33.9821F, // 197-200
  727. 31.9085F, 29.9614F, 28.1332F, 26.4165F, // 201-204
  728. 24.8045F, 23.2910F, 21.8697F, 20.5352F, // 205-208
  729. 19.2822F, 18.1056F, 17.0008F, 15.9634F, // 209-212
  730. 14.9893F, 14.0746F, 13.2158F, 12.4094F, // 213-216
  731. 11.6522F, 10.9411F, 10.2735F, 9.64662F, // 217-220
  732. 9.05798F, 8.50526F, 7.98626F, 7.49894F, // 221-224
  733. 7.04135F, 6.61169F, 6.20824F, 5.82941F, // 225-228
  734. 5.47370F, 5.13970F, 4.82607F, 4.53158F, // 229-232
  735. 4.25507F, 3.99542F, 3.75162F, 3.52269F, // 233-236
  736. 3.30774F, 3.10590F, 2.91638F, 2.73842F, // 237-240
  737. 2.57132F, 2.41442F, 2.26709F, 2.12875F, // 241-244
  738. 1.99885F, 1.87688F, 1.76236F, 1.65482F, // 245-248
  739. 1.55384F, 1.45902F, 1.36999F, 1.28640F, // 249-252
  740. 1.20790F, 1.13419F, 1.06499F, 1.F // 253-256
  741. };
  742. void _vp_remove_floor(vorbis_look_psy *p,
  743. float *mdct,
  744. int *codedflr,
  745. float *residue,
  746. int sliding_lowpass){
  747. int i,n=p->n;
  748. if(sliding_lowpass>n)sliding_lowpass=n;
  749. for(i=0;i<sliding_lowpass;i++){
  750. residue[i]=
  751. mdct[i]*FLOOR1_fromdB_INV_LOOKUP[codedflr[i]];
  752. }
  753. for(;i<n;i++)
  754. residue[i]=0.;
  755. }
  756. void _vp_noisemask(vorbis_look_psy *p,
  757. float noise_compand_level,
  758. float *logmdct,
  759. float *logmask){
  760. int i,n=p->n;
  761. float *work=alloca(n*sizeof(*work));
  762. bark_noise_hybridmp(n,p->bark,logmdct,logmask,
  763. 140.,-1);
  764. for(i=0;i<n;i++)work[i]=logmdct[i]-logmask[i];
  765. bark_noise_hybridmp(n,p->bark,work,logmask,0.,
  766. p->vi->noisewindowfixed);
  767. for(i=0;i<n;i++)work[i]=logmdct[i]-work[i];
  768. #if 0
  769. {
  770. static int seq=0;
  771. float work2[n];
  772. for(i=0;i<n;i++){
  773. work2[i]=logmask[i]+work[i];
  774. }
  775. if(seq&1)
  776. _analysis_output("median2R",seq/2,work,n,1,0,0);
  777. else
  778. _analysis_output("median2L",seq/2,work,n,1,0,0);
  779. if(seq&1)
  780. _analysis_output("envelope2R",seq/2,work2,n,1,0,0);
  781. else
  782. _analysis_output("envelope2L",seq/2,work2,n,1,0,0);
  783. seq++;
  784. }
  785. #endif
  786. /* aoTuV M5 extension */
  787. i=0;
  788. if((p->vi->noisecompand_high[NOISE_COMPAND_LEVELS-1] > 1) && (noise_compand_level > 0)){
  789. int thter = p->n33p;
  790. for(;i<thter;i++){
  791. int dB=logmask[i]+.5;
  792. if(dB>=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1;
  793. if(dB<0)dB=0;
  794. logmask[i]= work[i]+p->vi->noisecompand[dB]-
  795. ((p->vi->noisecompand[dB]-p->vi->noisecompand_high[dB])*noise_compand_level);
  796. }
  797. }
  798. for(;i<n;i++){
  799. int dB=logmask[i]+.5;
  800. if(dB>=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1;
  801. if(dB<0)dB=0;
  802. logmask[i]= work[i]+p->vi->noisecompand[dB];
  803. }
  804. }
  805. void _vp_tonemask(vorbis_look_psy *p,
  806. float *logfft,
  807. float *logmask,
  808. float global_specmax,
  809. float local_specmax){
  810. int i,n=p->n;
  811. float *seed=alloca(sizeof(*seed)*p->total_octave_lines);
  812. float att=local_specmax+p->vi->ath_adjatt;
  813. for(i=0;i<p->total_octave_lines;i++)seed[i]=NEGINF;
  814. /* set the ATH (floating below localmax, not global max by a
  815. specified att) */
  816. if(att<p->vi->ath_maxatt)att=p->vi->ath_maxatt;
  817. for(i=0;i<n;i++)
  818. logmask[i]=p->ath[i]+att;
  819. /* tone masking */
  820. seed_loop(p,(const float ***)p->tonecurves,logfft,logmask,seed,global_specmax);
  821. max_seeds(p,seed,logmask);
  822. }
  823. void _vp_offset_and_mix(vorbis_look_psy *p,
  824. float *noise,
  825. float *tone,
  826. int offset_select,
  827. int bit_managed,
  828. float *logmask,
  829. float *mdct,
  830. float *logmdct,
  831. float *lastmdct, float *tempmdct,
  832. float low_compand,
  833. int end_block,
  834. int blocktype, int modenumber,
  835. int nW_modenumber,
  836. int lW_blocktype, int lW_modenumber, int lW_no, int padnum){
  837. int i,j,n=p->n;
  838. int m2_sw=0; /* aoTuV for M2 */
  839. int m3_sw, *m3n, m3_count, mdctbuf_flag; /* aoTuV for M3 */
  840. int m4_end, m4_lp_pos, m4_start; /* aoTuV for M4 */
  841. float de, coeffi, cx; /* aoTuV for M1 */
  842. float noise_rate, noise_rate_low, noise_center, rate_mod, tone_rate; /* aoTuV for M3 */
  843. float m4_thres; /* aoTuV for M4 */
  844. float toneatt=p->vi->tone_masteratt[offset_select];
  845. cx = p->m_val;
  846. m3n = p->m3n;
  847. m4_start=p->vi->normal_start;
  848. m4_end = p->tonecomp_endp;
  849. m4_thres = p->tonecomp_thres;
  850. m4_lp_pos=9999;
  851. end_block+=p->vi->normal_partition;
  852. if(end_block>n)end_block=n;
  853. /* Collapse of low(mid) frequency is prevented. (for 32/44/48kHz q-2) */
  854. if(low_compand<0 || toneatt<25.)low_compand=0;
  855. else low_compand*=(toneatt-25.);
  856. /** @ M2 PRE **/
  857. if(p->vi->normal_thresh<.48){
  858. if((cx > 0.5) && !modenumber && blocktype && (n==128)){
  859. if(p->vi->normal_thresh>.35) m2_sw = 10+(int)(p->vi->flacint*100);
  860. else m2_sw = 10;
  861. }
  862. }
  863. /* flag for lastmdct & tempmdct (bitrate managed mode) */
  864. if( (bit_managed && (offset_select==2)) || (!bit_managed && (offset_select==1)) ) mdctbuf_flag=1;
  865. else mdctbuf_flag=0;
  866. /** @ M3&M4 PRE **/
  867. if(cx < 0.5){
  868. m3_sw = 0; /* for M3 */
  869. m4_end=end_block; /* for M4 */
  870. }else{
  871. /** M3 PRE **/
  872. if((n == 128) && !modenumber && !blocktype){
  873. if(toneatt < 3) m3_count = 2; // q6~
  874. else m3_count = 3;
  875. if(!lW_blocktype && !lW_modenumber){ /* last window "short" - type "impulse" */
  876. if(lW_no < 8){
  877. /* impulse - @impulse case1 */
  878. noise_rate = 0.7-(float)(lW_no-1)/17;
  879. noise_center = (float)(lW_no*m3_count);
  880. tone_rate = 8-lW_no;
  881. }else{
  882. /* impulse - @impulse case2 */
  883. noise_rate = 0.3;
  884. noise_center = 25;
  885. tone_rate = 0;
  886. if((lW_no*m3_count) < 24) noise_center = lW_no*m3_count;
  887. }
  888. if(mdctbuf_flag == 1){
  889. for(i=0; i<128; i++) tempmdct[i] -= 5;
  890. }
  891. }else{ /* non_impulse - @Short(impulse) case */
  892. noise_rate = 0.7;
  893. noise_center = 0;
  894. tone_rate = 8.;
  895. if(mdctbuf_flag == 1){
  896. for(i=0; i<128; i++) tempmdct[i] = lastmdct[i] - 5;
  897. }
  898. }
  899. noise_rate_low = 0;
  900. m3_sw = 1;
  901. if(padnum)noise_rate*=.8;
  902. for(i=0;i<n;i++){
  903. float freqbuf;
  904. float cell=75/(float)freq_bfn128[i];
  905. for(j=1; j<=freq_bfn128[i]; j++){
  906. freqbuf = logmdct[i]-(cell*j);
  907. if((tempmdct[i+j] < freqbuf) && (mdctbuf_flag == 1))
  908. tempmdct[i+j] += (5./(float)freq_bfn128[i+j]);
  909. }
  910. }
  911. }else if((n == 256) && !modenumber && !blocktype){
  912. // for q-1/-2 44.1kHz/48kHz
  913. if(!lW_blocktype && !lW_modenumber){ /* last window "short" - type "impulse" */
  914. m3_count = 6;
  915. if(lW_no < 4){
  916. /* impulse - @impulse case1 */
  917. noise_rate = 0.4-(float)(lW_no-1)/11;
  918. noise_center = (float)(lW_no*m3_count+12);
  919. tone_rate = 8-lW_no*2;
  920. }else{
  921. /* impulse - @impulse case2 */
  922. noise_rate = 0.2;
  923. noise_center = 30;
  924. tone_rate = 0;
  925. }
  926. if(mdctbuf_flag == 1){
  927. for(i=0; i<256; i++) tempmdct[i] -= 10;
  928. }
  929. }else{ /* non_impulse - @Short(impulse) case */
  930. noise_rate = 0.6;
  931. noise_center = 12;
  932. tone_rate = 8.;
  933. if(mdctbuf_flag == 1){
  934. for(i=0; i<256; i++) tempmdct[i] = lastmdct[i] - 10;
  935. }
  936. }
  937. noise_rate_low = 0;
  938. m3_sw = 1;
  939. if(padnum)noise_rate*=.5;
  940. for(i=0;i<n;i++){
  941. float freqbuf;
  942. float cell=75/(float)freq_bfn256[i];
  943. for(j=1; j<=freq_bfn256[i]; j++){
  944. freqbuf = logmdct[i]-(cell*j);
  945. if((tempmdct[i+j] < freqbuf) &&(mdctbuf_flag == 1))
  946. tempmdct[i+j] += (10./(float)freq_bfn256[i+j]);
  947. }
  948. }
  949. }else m3_sw = 0;
  950. /** M4 PRE **/
  951. if(p->vi->normal_thresh>1.){
  952. m4_start = 9999;
  953. }else{
  954. if(m4_end>end_block)m4_lp_pos=m4_end;
  955. else m4_lp_pos=end_block;
  956. }
  957. }
  958. for(i=0;i<n;i++){
  959. float val= noise[i]+p->noiseoffset[offset_select][i];
  960. float tval= tone[i]+toneatt;
  961. if(i<=m4_start)tval-=low_compand;
  962. if(val>p->vi->noisemaxsupp)val=p->vi->noisemaxsupp;
  963. /* AoTuV */
  964. /** @ M2 MAIN **
  965. floor is pulled below suitably. (padding block only) (#2)
  966. by Aoyumi @ 2006/06/14
  967. */
  968. if(m2_sw){
  969. // the conspicuous low level pre-echo of the padding block origin is reduced.
  970. if((logmdct[i]-lastmdct[i]) > 20){
  971. if(i > m3n[3]) val -= (logmdct[i]-lastmdct[i]-20)/m2_sw;
  972. else val -= (logmdct[i]-lastmdct[i]-20)/(m2_sw+m2_sw);
  973. }
  974. }
  975. /* AoTuV */
  976. /** @ M3 MAIN **
  977. Dynamic impulse block noise control. (#6)
  978. 48/44.1/32kHz only.
  979. by Aoyumi @ 2007/07/27
  980. */
  981. if(m3_sw){
  982. if(val>tval){
  983. if( (val>lastmdct[i]) && (logmdct[i]>(tempmdct[i]+noise_center)) ){
  984. int toneac=0;
  985. float valmask=0;
  986. if(mdctbuf_flag == 1)tempmdct[i] = logmdct[i]; // reset
  987. if(logmdct[i]>lastmdct[i]){
  988. rate_mod = noise_rate;
  989. }else{
  990. rate_mod = noise_rate_low;
  991. }
  992. if( !padnum && (i<m4_end) && ((val-lastmdct[i])>20) ){
  993. float dBsub=(logmdct[i]-lastmdct[i]);
  994. if(dBsub>25){
  995. toneac=1;
  996. if(tval>-100){
  997. float tr_cur=tone_rate;
  998. if(dBsub<(25+tr_cur)) tr_cur=dBsub-25;
  999. else tval-=tr_cur;
  1000. if(tval<-100)tval=-100;
  1001. }
  1002. }
  1003. }
  1004. if(i > m3n[1]){
  1005. if((val-tval)>30) valmask=((val-tval-30)/10+30)*rate_mod;
  1006. else valmask=(val-tval)*rate_mod;
  1007. }else if(i > m3n[2]){
  1008. if((val-tval)>20) valmask=((val-tval-20)/10+20)*rate_mod;
  1009. else valmask=(val-tval)*rate_mod;
  1010. }else if(i > m3n[3]){
  1011. if((val-tval)>10) valmask=((val-tval-10)/10+10)*rate_mod*0.5;
  1012. else valmask=(val-tval)*rate_mod*0.5;
  1013. }else{
  1014. if((val-tval)>10) valmask=((val-tval-10)/10+10)*rate_mod*0.3;
  1015. else valmask=(val-tval)*rate_mod*0.3;
  1016. }
  1017. if((val-valmask)>lastmdct[i])val-=valmask;
  1018. else val=lastmdct[i];
  1019. if( toneac && ((val-lastmdct[i])>20) ){
  1020. val-=(val-lastmdct[i]-20)*.2;
  1021. }
  1022. }
  1023. }
  1024. }
  1025. /* AoTuV */
  1026. /** @ M4 MAIN **
  1027. The purpose of this portion is working Noise Normalization more correctly.
  1028. (There is this in order to prevent extreme boost of floor)
  1029. m4_start = start point
  1030. m4_end = end point
  1031. m4_thres = threshold
  1032. by Aoyumi @ 2006/03/20
  1033. */
  1034. //logmask[i]=max(val,tval);
  1035. if(val>tval){
  1036. logmask[i]=val;
  1037. }else if((i>m4_start) && (i<m4_end) && (logmdct[i]>-140)){ // -140dB(test OK)
  1038. if(logmdct[i]>val){
  1039. if(logmdct[i]<tval)tval-=(tval-val)*m4_thres;
  1040. }else{
  1041. if(val<tval)tval-=(tval-val)*m4_thres;
  1042. }
  1043. logmask[i]=tval;
  1044. }else logmask[i]=tval;
  1045. /* AoTuV */
  1046. /** @ M1 **
  1047. The following codes improve a noise problem.
  1048. A fundamental idea uses the value of masking and carries out
  1049. the relative compensation of the MDCT.
  1050. However, this code is not perfect and all noise problems cannot be solved.
  1051. by Aoyumi @ 2004/04/18
  1052. */
  1053. if(offset_select == 1) {
  1054. coeffi = -17.2; /* coeffi is a -17.2dB threshold */
  1055. val = val - logmdct[i]; /* val == mdct line value relative to floor in dB */
  1056. if(val > coeffi){
  1057. /* mdct value is > -17.2 dB below floor */
  1058. de = 1.0-((val-coeffi)*0.005*cx);
  1059. /* pro-rated attenuation:
  1060. -0.00 dB boost if mdct value is -17.2dB (relative to floor)
  1061. -0.77 dB boost if mdct value is 0dB (relative to floor)
  1062. -1.64 dB boost if mdct value is +17.2dB (relative to floor)
  1063. etc... */
  1064. if(de < 0) de = 0.0001;
  1065. }else
  1066. /* mdct value is <= -17.2 dB below floor */
  1067. de = 1.0-((val-coeffi)*0.0003*cx);
  1068. /* pro-rated attenuation:
  1069. +0.00 dB atten if mdct value is -17.2dB (relative to floor)
  1070. +0.45 dB atten if mdct value is -34.4dB (relative to floor)
  1071. etc... */
  1072. mdct[i] *= de;
  1073. }
  1074. }
  1075. /** @ M3 SET lastmdct **/
  1076. if((mdctbuf_flag==1) && (cx >= 0.5)){
  1077. if((n == 128) || (n == 256)){
  1078. for(i=0; i<n; i++) lastmdct[i] = logmdct[i];
  1079. }else{
  1080. if(!nW_modenumber){
  1081. int nsh;
  1082. if(n == 1024) nsh=128;
  1083. else nsh=256;
  1084. for(i=0; i<nsh; i++){
  1085. lastmdct[i] = logmdct[i*8];
  1086. for(j=1; j<8; j++){
  1087. if(lastmdct[i] > logmdct[i*8+j]){
  1088. lastmdct[i] = logmdct[i*8+j];
  1089. }
  1090. }
  1091. }
  1092. }
  1093. }
  1094. }
  1095. /* This affects calculation of a floor curve. */
  1096. for(i=m4_lp_pos; i<n; i++)logmdct[i]=-160;
  1097. }
  1098. float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd){
  1099. vorbis_info *vi=vd->vi;
  1100. codec_setup_info *ci=vi->codec_setup;
  1101. vorbis_info_psy_global *gi=&ci->psy_g_param;
  1102. int n=ci->blocksizes[vd->W]/2;
  1103. float secs=(float)n/vi->rate;
  1104. amp+=secs*gi->ampmax_att_per_sec;
  1105. if(amp<-9999)amp=-9999;
  1106. return(amp);
  1107. }
  1108. static void couple_lossless(float A, float B,
  1109. float *qA, float *qB){
  1110. int test1=fabs(*qA)>fabs(*qB);
  1111. test1-= fabs(*qA)<fabs(*qB);
  1112. if(!test1)test1=((fabs(A)>fabs(B))<<1)-1;
  1113. if(test1==1){
  1114. *qB=(*qA>0.f?*qA-*qB:*qB-*qA);
  1115. }else{
  1116. float temp=*qB;
  1117. *qB=(*qB>0.f?*qA-*qB:*qB-*qA);
  1118. *qA=temp;
  1119. }
  1120. if(*qB>fabs(*qA)*1.9999f){
  1121. *qB= -fabs(*qA)*2.f;
  1122. *qA= -*qA;
  1123. }
  1124. }
  1125. static float hypot_lookup[32]={
  1126. -0.009935, -0.011245, -0.012726, -0.014397,
  1127. -0.016282, -0.018407, -0.020800, -0.023494,
  1128. -0.026522, -0.029923, -0.033737, -0.038010,
  1129. -0.042787, -0.048121, -0.054064, -0.060671,
  1130. -0.068000, -0.076109, -0.085054, -0.094892,
  1131. -0.105675, -0.117451, -0.130260, -0.144134,
  1132. -0.159093, -0.175146, -0.192286, -0.210490,
  1133. -0.229718, -0.249913, -0.271001, -0.292893};
  1134. static void precomputed_couple_point(float premag,
  1135. int floorA,int floorB,
  1136. float *mag, float *ang){
  1137. int test=(floorA>floorB)-1;
  1138. int offset=31-abs(floorA-floorB);
  1139. float floormag=hypot_lookup[((offset<0)-1)&offset]+1.f; // floormag = 0.990065 ~ 0.707107
  1140. floormag*=FLOOR1_fromdB_INV_LOOKUP[(floorB&test)|(floorA&(~test))];
  1141. *mag=premag*floormag;
  1142. *ang=0.f;
  1143. }
  1144. /* just like below, this is currently set up to only do
  1145. single-step-depth coupling. Otherwise, we'd have to do more
  1146. copying (which will be inevitable later) */
  1147. /* doing the real circular magnitude calculation is audibly superior
  1148. to (A+B)/sqrt(2) */
  1149. static float dipole_hypot(float a, float b){
  1150. if(a>0.){
  1151. if(b>0.)return sqrt(a*a+b*b);
  1152. if(a>-b)return sqrt(a*a-b*b);
  1153. return -sqrt(b*b-a*a);
  1154. }
  1155. if(b<0.)return -sqrt(a*a+b*b);
  1156. if(-a>b)return -sqrt(a*a-b*b);
  1157. return sqrt(b*b-a*a);
  1158. }
  1159. static float round_hypot(float a, float b){
  1160. if(a>0.){
  1161. if(b>0.)return sqrt(a*a+b*b);
  1162. if(a>-b)return sqrt(a*a+b*b);
  1163. return -sqrt(b*b+a*a);
  1164. }
  1165. if(b<0.)return -sqrt(a*a+b*b);
  1166. if(-a>b)return -sqrt(a*a+b*b);
  1167. return sqrt(b*b+a*a);
  1168. }
  1169. /* modified hypot by aoyumi
  1170. better method should be found. */
  1171. static float min_indemnity_dipole_hypot(float a, float b){
  1172. float thnor=0.92;
  1173. float threv=0.84;
  1174. float a2 = a*a;
  1175. float b2 = b*b;
  1176. if(a>0.){
  1177. if(b>0.)return sqrt(a2+b2*thnor);
  1178. if(a>-b)return sqrt(a2-b2+b2*threv);
  1179. return -sqrt(b2-a2+a2*threv);
  1180. }
  1181. if(b<0.)return -sqrt(a2+b2*thnor);
  1182. if(-a>b)return -sqrt(a2-b2+b2*threv);
  1183. return sqrt(b2-a2+a2*threv);
  1184. }
  1185. /* revert to round hypot for now */
  1186. float **_vp_quantize_couple_memo(vorbis_block *vb,
  1187. vorbis_info_psy_global *g,
  1188. vorbis_look_psy *p,
  1189. vorbis_info_mapping0 *vi,
  1190. float **mdct){
  1191. int i,j,n=p->n;
  1192. float **ret=_vorbis_block_alloc(vb,vi->coupling_steps*sizeof(*ret));
  1193. int limit=g->coupling_pointlimit[p->vi->blockflag][PACKETBLOBS/2];
  1194. if(1){ // set new hypot
  1195. for(i=0;i<vi->coupling_steps;i++){
  1196. float *mdctM=mdct[vi->coupling_mag[i]];
  1197. float *mdctA=mdct[vi->coupling_ang[i]];
  1198. ret[i]=_vorbis_block_alloc(vb,n*sizeof(**ret));
  1199. for(j=0;j<n;j++)
  1200. ret[i][j]=min_indemnity_dipole_hypot(mdctM[j],mdctA[j]);
  1201. }
  1202. }else{
  1203. for(i=0;i<vi->coupling_steps;i++){
  1204. float *mdctM=mdct[vi->coupling_mag[i]];
  1205. float *mdctA=mdct[vi->coupling_ang[i]];
  1206. ret[i]=_vorbis_block_alloc(vb,n*sizeof(**ret));
  1207. for(j=0;j<limit;j++)
  1208. ret[i][j]=dipole_hypot(mdctM[j],mdctA[j]);
  1209. for(;j<n;j++)
  1210. ret[i][j]=round_hypot(mdctM[j],mdctA[j]);
  1211. }
  1212. }
  1213. return(ret);
  1214. }
  1215. /* this is for per-channel noise normalization */
  1216. static int apsort(const void *a, const void *b){
  1217. float f1=fabs(**(float**)a);
  1218. float f2=fabs(**(float**)b);
  1219. return (f1<f2)-(f1>f2);
  1220. }
  1221. /*** optimization of sort (for 8 or 32 element) ***/
  1222. #ifdef OPT_SORT
  1223. #define C(o,a,b)\
  1224. (fabs(data[o+a])>=fabs(data[o+b]))
  1225. #define O(o,a,b,c,d)\
  1226. {n[o]=o+a;n[o+1]=o+b;n[o+2]=o+c;n[o+3]=o+d;}
  1227. #define SORT4(o)\
  1228. if(C(o,2,3))if(C(o,0,1))if(C(o,0,2))if(C(o,1,2))O(o,0,1,2,3)\
  1229. else if(C(o,1,3))O(o,0,2,1,3)\
  1230. else O(o,0,2,3,1)\
  1231. else if(C(o,0,3))if(C(o,1,3))O(o,2,0,1,3)\
  1232. else O(o,2,0,3,1)\
  1233. else O(o,2,3,0,1)\
  1234. else if(C(o,1,2))if(C(o,0,2))O(o,1,0,2,3)\
  1235. else if(C(o,0,3))O(o,1,2,0,3)\
  1236. else O(o,1,2,3,0)\
  1237. else if(C(o,1,3))if(C(o,0,3))O(o,2,1,0,3)\
  1238. else O(o,2,1,3,0)\
  1239. else O(o,2,3,1,0)\
  1240. else if(C(o,0,1))if(C(o,0,3))if(C(o,1,3))O(o,0,1,3,2)\
  1241. else if(C(o,1,2))O(o,0,3,1,2)\
  1242. else O(o,0,3,2,1)\
  1243. else if(C(o,0,2))if(C(o,1,2))O(o,3,0,1,2)\
  1244. else O(o,3,0,2,1)\
  1245. else O(o,3,2,0,1)\
  1246. else if(C(o,1,3))if(C(o,0,3))O(o,1,0,3,2)\
  1247. else if(C(o,0,2))O(o,1,3,0,2)\
  1248. else O(o,1,3,2,0)\
  1249. else if(C(o,1,2))if(C(o,0,2))O(o,3,1,0,2)\
  1250. else O(o,3,1,2,0)\
  1251. else O(o,3,2,1,0)
  1252. static void sortindex_fix8(int *index,
  1253. float *data,
  1254. int offset){
  1255. int i,j,k,n[8];
  1256. index+=offset;
  1257. data+=offset;
  1258. SORT4(0)
  1259. SORT4(4)
  1260. j=0;k=4;
  1261. for(i=0;i<8;i++)
  1262. index[i]=n[(k>=8)||(j<4)&&C(0,n[j],n[k])?j++:k++]+offset;
  1263. }
  1264. static void sortindex_fix32(int *index,
  1265. float *data,
  1266. int offset){
  1267. int i,j,k,n[32];
  1268. for(i=0;i<32;i+=8)
  1269. sortindex_fix8(index,data,offset+i);
  1270. index+=offset;
  1271. for(i=j=0,k=8;i<16;i++)
  1272. n[i]=index[(k>=16)||(j<8)&&C(0,index[j],index[k])?j++:k++];
  1273. for(i=j=16,k=24;i<32;i++)
  1274. n[i]=index[(k>=32)||(j<24)&&C(0,index[j],index[k])?j++:k++];
  1275. for(i=j=0,k=16;i<32;i++)
  1276. index[i]=n[(k>=32)||(j<16)&&C(0,n[j],n[k])?j++:k++];
  1277. }
  1278. static void sortindex_shellsort(int *index,
  1279. float *data,
  1280. int offset,
  1281. int count){
  1282. int gap,pos,left,right,i,j;
  1283. index+=offset;
  1284. for(i=0;i<count;i++)index[i]=i+offset;
  1285. gap=1;
  1286. while (gap<=count)gap=gap*3+1;
  1287. gap/=3;
  1288. if(gap>=4)gap/=3;
  1289. while(gap>0){
  1290. for(pos=gap;pos<count;pos++){
  1291. for(left=pos-gap;left>=0;left-=gap){
  1292. i=index[left];j=index[left+gap];
  1293. if(!C(0,i,j)){
  1294. index[left]=j;
  1295. index[left+gap]=i;
  1296. }else break;
  1297. }
  1298. }
  1299. gap/=3;
  1300. }
  1301. }
  1302. static void sortindex(int *index,
  1303. float *data,
  1304. int offset,
  1305. int count){
  1306. if(count==8)sortindex_fix8(index,data,offset);
  1307. else if(count==32)sortindex_fix32(index,data,offset);
  1308. else sortindex_shellsort(index,data,offset,count);
  1309. }
  1310. #undef C
  1311. #undef O
  1312. #undef SORT4
  1313. #endif
  1314. /*** OPT_SORT End ***/
  1315. int **_vp_quantize_couple_sort(vorbis_block *vb,
  1316. vorbis_look_psy *p,
  1317. vorbis_info_mapping0 *vi,
  1318. float **mags){
  1319. #ifdef OPT_SORT
  1320. if(p->vi->normal_point_p){
  1321. int i,j,n=p->n;
  1322. int **ret=_vorbis_block_alloc(vb,vi->coupling_steps*sizeof(*ret));
  1323. int partition=p->vi->normal_partition;
  1324. for(i=0;i<vi->coupling_steps;i++){
  1325. ret[i]=_vorbis_block_alloc(vb,n*sizeof(**ret));
  1326. for(j=0;j<n;j+=partition){
  1327. sortindex(ret[i],mags[i],j,partition);
  1328. }
  1329. }
  1330. return(ret);
  1331. }
  1332. return(NULL);
  1333. #else
  1334. if(p->vi->normal_point_p){
  1335. int i,j,k,n=p->n;
  1336. int **ret=_vorbis_block_alloc(vb,vi->coupling_steps*sizeof(*ret));
  1337. int partition=p->vi->normal_partition;
  1338. float **work=alloca(sizeof(*work)*partition);
  1339. for(i=0;i<vi->coupling_steps;i++){
  1340. ret[i]=_vorbis_block_alloc(vb,n*sizeof(**ret));
  1341. for(j=0;j<n;j+=partition){
  1342. for(k=0;k<partition;k++)work[k]=mags[i]+k+j;
  1343. qsort(work,partition,sizeof(*work),apsort);
  1344. for(k=0;k<partition;k++)ret[i][k+j]=work[k]-mags[i];
  1345. }
  1346. }
  1347. return(ret);
  1348. }
  1349. return(NULL);
  1350. #endif
  1351. }
  1352. void _vp_noise_normalize_sort(vorbis_look_psy *p,
  1353. float *magnitudes,int *sortedindex){
  1354. #ifdef OPT_SORT
  1355. int j,n=p->n;
  1356. vorbis_info_psy *vi=p->vi;
  1357. int partition=vi->normal_partition;
  1358. int start=vi->normal_start;
  1359. for(j=start;j<n;j+=partition){
  1360. if(j+partition>n)partition=n-j;
  1361. sortindex(sortedindex-start,magnitudes,j,partition);
  1362. }
  1363. #else
  1364. int i,j,n=p->n;
  1365. vorbis_info_psy *vi=p->vi;
  1366. int partition=vi->normal_partition;
  1367. float **work=alloca(sizeof(*work)*partition);
  1368. int start=vi->normal_start;
  1369. for(j=start;j<n;j+=partition){
  1370. if(j+partition>n)partition=n-j;
  1371. for(i=0;i<partition;i++)work[i]=magnitudes+i+j;
  1372. qsort(work,partition,sizeof(*work),apsort);
  1373. for(i=0;i<partition;i++){
  1374. sortedindex[i+j-start]=work[i]-magnitudes;
  1375. }
  1376. }
  1377. #endif
  1378. }
  1379. void _vp_noise_normalize(vorbis_look_psy *p,
  1380. float *in,float *out,int *sortedindex,
  1381. int blocktype, int modenumber){
  1382. int i,j=0,n=p->n;
  1383. vorbis_info_psy *vi=p->vi;
  1384. int partition=vi->normal_partition;
  1385. int start=vi->normal_start;
  1386. if(start>n)start=n;
  1387. if(vi->normal_channel_p){
  1388. for(;j<start;j++)
  1389. out[j]=rint(in[j]);
  1390. for(;j+partition<=n;j+=partition){
  1391. float acc=0.;
  1392. int k;
  1393. int energy_loss=0;
  1394. int nn_count=0;
  1395. int div_low=j+p->nn25pt;
  1396. int div_high=j+p->nn75pt;
  1397. int low_flag=0;
  1398. int high_flag=0;
  1399. for(i=j;i<j+partition;i++){
  1400. if(rint(in[i])==0.f){
  1401. acc+=in[i]*in[i];
  1402. energy_loss++;
  1403. }
  1404. }
  1405. /* partition is 8 or 32 */
  1406. if(partition==8){ div_low--; div_high--; }
  1407. /* Expansion of Noise Normalization. by Aoyumi */
  1408. /* When the energy loss of a partition is large,
  1409. NN is performed in the middle of partition. (without impulse block) */
  1410. if((energy_loss==partition) && !(!modenumber && !blocktype)){
  1411. for(k=div_low; k<=div_high ;k+=p->nn50pt){
  1412. if(acc>=vi->normal_thresh && fabs(in[k])>nnmid_th){
  1413. out[k]=unitnorm(in[k]);
  1414. acc-=1.;
  1415. nn_count++;
  1416. if(k==div_low)low_flag=k;
  1417. else high_flag=k;
  1418. }
  1419. }
  1420. }
  1421. /* NN main */
  1422. for(i=0;i<partition;i++){
  1423. k=sortedindex[i+j-start];
  1424. if(in[k]*in[k]>=.25f){ // or rint(in[k])!=0.f
  1425. out[k]=rint(in[k]);
  1426. //acc-=in[k]*in[k];
  1427. }else{
  1428. if(acc<vi->normal_thresh)break;
  1429. if(low_flag==k || high_flag==k)continue;
  1430. out[k]=unitnorm(in[k]);
  1431. acc-=1.;
  1432. nn_count++;
  1433. }
  1434. }
  1435. /* The minimum energy complement */
  1436. if(modenumber && (energy_loss==partition) && (j<=p->min_nn_lp) && (nn_count)){
  1437. k=sortedindex[i+j-start];
  1438. if(fabs(in[k])>=p->nn_mec_m){
  1439. out[k]=unitnorm(in[k]);
  1440. i++;
  1441. }
  1442. }
  1443. // The last process
  1444. for(;i<partition;i++){
  1445. k=sortedindex[i+j-start];
  1446. if(low_flag==k || high_flag==k)continue;
  1447. else out[k]=0.;
  1448. }
  1449. }
  1450. }
  1451. for(;j<n;j++)
  1452. out[j]=rint(in[j]);
  1453. }
  1454. void _vp_couple(int blobno,
  1455. vorbis_info_psy_global *g,
  1456. vorbis_look_psy *p,
  1457. vorbis_info_mapping0 *vi,
  1458. float **res,
  1459. float **mag_memo,
  1460. int **mag_sort,
  1461. int **ifloor,
  1462. int *nonzero,
  1463. int sliding_lowpass,
  1464. int blocktype, int modenumber,
  1465. float **mdct, float **res_org){
  1466. int i,j,k,n=p->n;
  1467. /* perform any requested channel coupling */
  1468. /* point stereo can only be used in a first stage (in this encoder)
  1469. because of the dependency on floor lookups */
  1470. for(i=0;i<vi->coupling_steps;i++){
  1471. /* once we're doing multistage coupling in which a channel goes
  1472. through more than one coupling step, the floor vector
  1473. magnitudes will also have to be recalculated an propogated
  1474. along with PCM. Right now, we're not (that will wait until 5.1
  1475. most likely), so the code isn't here yet. The memory management
  1476. here is all assuming single depth couplings anyway. */
  1477. /* make sure coupling a zero and a nonzero channel results in two
  1478. nonzero channels. */
  1479. if(nonzero[vi->coupling_mag[i]] ||
  1480. nonzero[vi->coupling_ang[i]]){
  1481. float *rM=res[vi->coupling_mag[i]];
  1482. float *rA=res[vi->coupling_ang[i]];
  1483. float *rMo=res_org[vi->coupling_mag[i]];
  1484. float *rAo=res_org[vi->coupling_ang[i]];
  1485. float *qM=rM+n;
  1486. float *qA=rA+n;
  1487. float *mdctM=mdct[vi->coupling_mag[i]];
  1488. float *mdctA=mdct[vi->coupling_ang[i]];
  1489. int *floorM=ifloor[vi->coupling_mag[i]];
  1490. int *floorA=ifloor[vi->coupling_ang[i]];
  1491. float prepoint=stereo_threshholds[g->coupling_prepointamp[blobno]];
  1492. float postpoint=stereo_threshholds[g->coupling_postpointamp[blobno]];
  1493. float sth_low=stereo_threshholds_low[g->coupling_prepointamp[blobno]];
  1494. float sth_high=stereo_threshholds_high[g->coupling_postpointamp[blobno]];
  1495. float postpoint_backup;
  1496. float st_thresh;
  1497. int partition=(p->vi->normal_point_p?p->vi->normal_partition:p->n);
  1498. int pointlimit=g->coupling_pointlimit[p->vi->blockflag][blobno];
  1499. int freqlimit=p->st_freqlimit;
  1500. unsigned char Mc_treshp[2048];
  1501. unsigned char Ac_treshp[2048];
  1502. int lof_st;
  1503. int hif_st;
  1504. int hif_stcopy;
  1505. int old_lof_st=0;
  1506. int old_hif_st=0;
  1507. int Afreq_num=0;
  1508. int Mfreq_num=0;
  1509. int stcont_start=0; // M6 start point
  1510. nonzero[vi->coupling_mag[i]]=1;
  1511. nonzero[vi->coupling_ang[i]]=1;
  1512. postpoint_backup=postpoint;
  1513. /** @ M6 PRE **/
  1514. // lossless only?
  1515. if(!stereo_threshholds[g->coupling_postpointamp[blobno]])stcont_start=n;
  1516. else{
  1517. // exception handling
  1518. if((postpoint-sth_high)<prepoint)sth_high=postpoint-prepoint;
  1519. // start point setup
  1520. for(j=0;j<n;j++){
  1521. stcont_start=j;
  1522. if(p->noiseoffset[1][j]>=-2)break;
  1523. }
  1524. // start point correction & threshold setup
  1525. st_thresh=.1;
  1526. if(p->m_val<.5){
  1527. // low frequency limit
  1528. if(stcont_start<pointlimit)stcont_start=pointlimit;
  1529. }else if(p->vi->normal_thresh>1.)st_thresh=.25;
  1530. for(j=0;j<=freqlimit;j++){ // or j<n
  1531. if(fabs(rM[j])<st_thresh)Mc_treshp[j]=1;
  1532. else Mc_treshp[j]=0;
  1533. if(fabs(rA[j])<st_thresh)Ac_treshp[j]=1;
  1534. else Ac_treshp[j]=0;
  1535. }
  1536. }
  1537. for(j=0;j<n;j+=partition){
  1538. float acc=0.f;
  1539. float rpacc;
  1540. int energy_loss=0;
  1541. for(k=0;k<partition;k++){
  1542. int l=k+j;
  1543. float a=mdctM[l];
  1544. float b=mdctA[l];
  1545. float dummypoint;
  1546. float hypot_reserve;
  1547. float slow=0.f;
  1548. float shigh=0.f;
  1549. float slowM=0.f;
  1550. float slowA=0.f;
  1551. float shighM=0.f;
  1552. float shighA=0.f;
  1553. float rMs=fabs(rMo[l]);
  1554. float rAs=fabs(rAo[l]);
  1555. postpoint=postpoint_backup;
  1556. /* AoTuV */
  1557. /** @ M6 MAIN **
  1558. The threshold of a stereo is changed dynamically.
  1559. by Aoyumi @ 2006/06/04
  1560. */
  1561. if(l>=stcont_start){
  1562. int m;
  1563. int lof_num;
  1564. int hif_num;
  1565. lof_st=LOF_TABLE[l];
  1566. hif_st=HIF_TABLE[l];
  1567. /*** original calc.
  1568. float magicnum=.175; // 0.16�`0.19
  1569. lof_st=l-(l/2)*(magicnum*.5);
  1570. hif_st=l+l*magicnum;
  1571. ****/
  1572. hif_stcopy=hif_st;
  1573. // limit setting
  1574. if(hif_st>freqlimit)hif_st=freqlimit;
  1575. if(old_lof_st || old_hif_st){
  1576. if(hif_st>l){
  1577. // hif_st, lof_st ...absolute value
  1578. // lof_num, hif_num ...relative value
  1579. // low freq.(lower)
  1580. lof_num=lof_st-old_lof_st;
  1581. switch(lof_num){
  1582. case 0:
  1583. Afreq_num+=Ac_treshp[l-1];
  1584. Mfreq_num+=Mc_treshp[l-1];
  1585. break;
  1586. case 1:
  1587. Afreq_num+=Ac_treshp[l-1];
  1588. Mfreq_num+=Mc_treshp[l-1];
  1589. Afreq_num-=Ac_treshp[old_lof_st];
  1590. Mfreq_num-=Mc_treshp[old_lof_st];
  1591. break;
  1592. default:/* puts("err. low") */;break;
  1593. }
  1594. // high freq.(higher)
  1595. hif_num=hif_st-old_hif_st;
  1596. switch(hif_num){
  1597. case 0:
  1598. Afreq_num-=Ac_treshp[l];
  1599. Mfreq_num-=Mc_treshp[l];
  1600. break;
  1601. case 1:
  1602. Afreq_num-=Ac_treshp[l];
  1603. Mfreq_num-=Mc_treshp[l];
  1604. Afreq_num+=Ac_treshp[hif_st];
  1605. Mfreq_num+=Mc_treshp[hif_st];
  1606. break;
  1607. case 2:
  1608. Afreq_num-=Ac_treshp[l];
  1609. Mfreq_num-=Mc_treshp[l];
  1610. Afreq_num+=Ac_treshp[hif_st];
  1611. Mfreq_num+=Mc_treshp[hif_st];
  1612. Afreq_num+=Ac_treshp[hif_st-1];
  1613. Mfreq_num+=Mc_treshp[hif_st-1];
  1614. break;
  1615. default:/* puts("err. high") */;break;
  1616. }
  1617. }
  1618. }else{
  1619. for(m=lof_st; m<=hif_st; m++){
  1620. if(m==l)continue;
  1621. if(Ac_treshp[m]) Afreq_num++;
  1622. if(Mc_treshp[m]) Mfreq_num++;
  1623. }
  1624. }
  1625. if(l>=pointlimit){
  1626. shigh=sth_high/(hif_stcopy-lof_st);
  1627. shighA=shigh*Afreq_num;
  1628. shighM=shigh*Mfreq_num;
  1629. if((shighA+rAs)>(shighM+rMs))shigh=shighA;
  1630. else shigh=shighM;
  1631. }else{
  1632. slow=sth_low/(hif_stcopy-lof_st);
  1633. slowA=slow*Afreq_num;
  1634. slowM=slow*Mfreq_num;
  1635. if(p->noiseoffset[1][l]<-1){
  1636. slowA*=(p->noiseoffset[1][l]+2);
  1637. slowM*=(p->noiseoffset[1][l]+2);
  1638. }
  1639. }
  1640. old_lof_st=lof_st;
  1641. old_hif_st=hif_st;
  1642. }
  1643. if(l>=pointlimit){
  1644. postpoint-=shigh;
  1645. /* The following prevents an extreme reduction of residue. (2ch stereo only) */
  1646. if( ((a>0.) && (b<0.)) || ((b>0.) && (a<0.)) ){
  1647. hypot_reserve = fabs(fabs(a)-fabs(b));
  1648. if(hypot_reserve < 0.001){ // 0~0.000999-
  1649. dummypoint = stereo_threshholds_rephase[g->coupling_postpointamp[blobno]];
  1650. dummypoint = dummypoint+((postpoint-dummypoint)*(hypot_reserve*1000));
  1651. if(postpoint > dummypoint) postpoint = dummypoint;
  1652. }
  1653. }
  1654. }
  1655. if(l<sliding_lowpass){
  1656. if((l>=pointlimit && rMs<postpoint && rAs<postpoint) ||
  1657. (rMs<(prepoint-slowM) && rAs<(prepoint-slowA))){
  1658. precomputed_couple_point(mag_memo[i][l],
  1659. floorM[l],floorA[l],
  1660. qM+l,qA+l);
  1661. //if(rint(qM[l])==0.f)acc+=qM[l]*qM[l];
  1662. if(rint(qM[l])==0.f){
  1663. energy_loss++;
  1664. if(l>=pointlimit)acc+=qM[l]*qM[l];
  1665. }
  1666. }else{
  1667. couple_lossless(rM[l],rA[l],qM+l,qA+l);
  1668. }
  1669. }else{
  1670. qM[l]=0.;
  1671. qA[l]=0.;
  1672. }
  1673. }
  1674. /* Expansion of Noise Normalization(for point stereo). by Aoyumi */
  1675. if(p->vi->normal_point_p && p->vi->normal_start!=9999){
  1676. int div_low=j+p->nn25pt;
  1677. int div_high=j+p->nn75pt;
  1678. int low_flag=0;
  1679. int high_flag=0;
  1680. int nn_count=0;
  1681. int l; // k=parttion counter. l=mdct counter. j=partition block counter
  1682. /* partition is 8 or 32 */
  1683. if(partition==8){ div_low--; div_high--; }
  1684. rpacc=acc;
  1685. /* When the energy loss of a partition is large,
  1686. NN is performed in the middle of partition. (without impulse block) */
  1687. if((energy_loss==partition) && !(!modenumber && !blocktype)){
  1688. for(l=div_low; l<=div_high ;l+=p->nn50pt){
  1689. if(l>=pointlimit && acc>=p->vi->normal_thresh &&
  1690. fabs(qM[l])>nnmid_th && l<sliding_lowpass){
  1691. if( ((mdctM[l]>0.) && (mdctA[l]<0.)) || ((mdctA[l]>0.) && (mdctM[l]<0.)) ){
  1692. acc-=1.f; rpacc-=1.25;
  1693. }else{
  1694. acc-=1.f; rpacc-=1.f;
  1695. }
  1696. qM[l]=unitnorm(qM[l]);
  1697. nn_count++;
  1698. if(l==div_low)low_flag=l;
  1699. else high_flag=l;
  1700. }
  1701. }
  1702. }
  1703. /* NN main (point stereo) */
  1704. for(k=0;k<partition && acc>=p->vi->normal_thresh;k++){
  1705. l=mag_sort[i][j+k];
  1706. if(l<sliding_lowpass && l>=pointlimit && rint(qM[l])==0.f){
  1707. if(low_flag==l || high_flag==l)continue;
  1708. if( ((mdctM[l]>0.) && (mdctA[l]<0.)) || ((mdctA[l]>0.) && (mdctM[l]<0.)) ){
  1709. if(rpacc<p->vi->normal_thresh)continue;
  1710. acc-=1.f; rpacc-=1.25;
  1711. }else{
  1712. acc-=1.f; rpacc-=1.f;
  1713. }
  1714. qM[l]=unitnorm(qM[l]);
  1715. nn_count++;
  1716. }
  1717. }
  1718. /* The minimum energy complement. (long & trans. block) */
  1719. if(modenumber && (energy_loss==partition) && (j<=p->min_nn_lp) && (nn_count)){
  1720. for(;k<partition;k++){
  1721. l=mag_sort[i][j+k];
  1722. if((l>=pointlimit) && (rint(qM[l])==0.f) && (fabs(rM[l]+rA[l])>=p->nn_mec_s)){
  1723. qM[l]=unitnorm(qM[l]);
  1724. break;
  1725. }
  1726. }
  1727. }
  1728. }
  1729. }
  1730. }
  1731. }
  1732. }
  1733. /* aoTuV M5
  1734. noise_compand_level of low frequency is determined from the level of high frequency.
  1735. by Aoyumi @ 2005/09/14
  1736. return value
  1737. [normal compander] 0 <> 1.0 [high compander]
  1738. -1 @ disable
  1739. */
  1740. float lb_loudnoise_fix(vorbis_look_psy *p,
  1741. float noise_compand_level,
  1742. float *logmdct,
  1743. int lW_modenumber,
  1744. int blocktype, int modenumber){
  1745. int i, n=p->n, nq1=p->n25p, nq3=p->n75p;
  1746. double hi_th=0;
  1747. if(p->m_val < 0.5)return(-1); /* 48/44.1/32kHz only */
  1748. if(p->vi->normal_thresh>.45)return(-1); /* under q3 */
  1749. /* select trans. block(short>>long case). */
  1750. if(!modenumber)return(-1);
  1751. if(blocktype || lW_modenumber)return(noise_compand_level);
  1752. /* calculation of a threshold. */
  1753. for(i=nq1; i<nq3; i++){
  1754. if(logmdct[i]>-130)hi_th += logmdct[i];
  1755. else hi_th += -130;
  1756. }
  1757. hi_th /= n;
  1758. /* calculation of a high_compand_level */
  1759. if(hi_th > -40.) noise_compand_level=-1;
  1760. else if(hi_th < -50.) noise_compand_level=1.;
  1761. else noise_compand_level=1.-((hi_th+50)/10);
  1762. return(noise_compand_level);
  1763. }