floor1.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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: floor backend 1 implementation
  13. last mod: $Id: floor1.c,v 1.16.2.3 2001/12/12 09:13:39 xiphmont Exp $
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include <ogg/ogg.h>
  19. #include "vorbis/codec.h"
  20. #include "codec_internal.h"
  21. #include "registry.h"
  22. #include "codebook.h"
  23. #include "misc.h"
  24. #include "scales.h"
  25. #include <stdio.h>
  26. #define floor1_rangedB 140 /* floor 1 fixed at -140dB to 0dB range */
  27. typedef struct {
  28. int sorted_index[VIF_POSIT+2];
  29. int forward_index[VIF_POSIT+2];
  30. int reverse_index[VIF_POSIT+2];
  31. int hineighbor[VIF_POSIT];
  32. int loneighbor[VIF_POSIT];
  33. int posts;
  34. int n;
  35. int quant_q;
  36. vorbis_info_floor1 *vi;
  37. long phrasebits;
  38. long postbits;
  39. long frames;
  40. } vorbis_look_floor1;
  41. typedef struct lsfit_acc{
  42. long x0;
  43. long x1;
  44. long xa;
  45. long ya;
  46. long x2a;
  47. long y2a;
  48. long xya;
  49. long n;
  50. long an;
  51. long un;
  52. long edgey0;
  53. long edgey1;
  54. } lsfit_acc;
  55. /***********************************************/
  56. static vorbis_info_floor *floor1_copy_info (vorbis_info_floor *i){
  57. vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
  58. vorbis_info_floor1 *ret=_ogg_malloc(sizeof(*ret));
  59. memcpy(ret,info,sizeof(*ret));
  60. return(ret);
  61. }
  62. static void floor1_free_info(vorbis_info_floor *i){
  63. vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
  64. if(info){
  65. memset(info,0,sizeof(*info));
  66. _ogg_free(info);
  67. }
  68. }
  69. static void floor1_free_look(vorbis_look_floor *i){
  70. vorbis_look_floor1 *look=(vorbis_look_floor1 *)i;
  71. if(look){
  72. /*fprintf(stderr,"floor 1 bit usage %f:%f (%f total)\n",
  73. (float)look->phrasebits/look->frames,
  74. (float)look->postbits/look->frames,
  75. (float)(look->postbits+look->phrasebits)/look->frames);*/
  76. memset(look,0,sizeof(*look));
  77. free(look);
  78. }
  79. }
  80. static int ilog(unsigned int v){
  81. int ret=0;
  82. while(v){
  83. ret++;
  84. v>>=1;
  85. }
  86. return(ret);
  87. }
  88. static int ilog2(unsigned int v){
  89. int ret=0;
  90. while(v>1){
  91. ret++;
  92. v>>=1;
  93. }
  94. return(ret);
  95. }
  96. static void floor1_pack (vorbis_info_floor *i,oggpack_buffer *opb){
  97. vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
  98. int j,k;
  99. int count=0;
  100. int rangebits;
  101. int maxposit=info->postlist[1];
  102. int maxclass=-1;
  103. /* save out partitions */
  104. oggpack_write(opb,info->partitions,5); /* only 0 to 31 legal */
  105. for(j=0;j<info->partitions;j++){
  106. oggpack_write(opb,info->partitionclass[j],4); /* only 0 to 15 legal */
  107. if(maxclass<info->partitionclass[j])maxclass=info->partitionclass[j];
  108. }
  109. /* save out partition classes */
  110. for(j=0;j<maxclass+1;j++){
  111. oggpack_write(opb,info->class_dim[j]-1,3); /* 1 to 8 */
  112. oggpack_write(opb,info->class_subs[j],2); /* 0 to 3 */
  113. if(info->class_subs[j])oggpack_write(opb,info->class_book[j],8);
  114. for(k=0;k<(1<<info->class_subs[j]);k++)
  115. oggpack_write(opb,info->class_subbook[j][k]+1,8);
  116. }
  117. /* save out the post list */
  118. oggpack_write(opb,info->mult-1,2); /* only 1,2,3,4 legal now */
  119. oggpack_write(opb,ilog2(maxposit),4);
  120. rangebits=ilog2(maxposit);
  121. for(j=0,k=0;j<info->partitions;j++){
  122. count+=info->class_dim[info->partitionclass[j]];
  123. for(;k<count;k++)
  124. oggpack_write(opb,info->postlist[k+2],rangebits);
  125. }
  126. }
  127. static vorbis_info_floor *floor1_unpack (vorbis_info *vi,oggpack_buffer *opb){
  128. codec_setup_info *ci=vi->codec_setup;
  129. int j,k,count=0,maxclass=-1,rangebits;
  130. vorbis_info_floor1 *info=_ogg_calloc(1,sizeof(*info));
  131. /* read partitions */
  132. info->partitions=oggpack_read(opb,5); /* only 0 to 31 legal */
  133. for(j=0;j<info->partitions;j++){
  134. info->partitionclass[j]=oggpack_read(opb,4); /* only 0 to 15 legal */
  135. if(maxclass<info->partitionclass[j])maxclass=info->partitionclass[j];
  136. }
  137. /* read partition classes */
  138. for(j=0;j<maxclass+1;j++){
  139. info->class_dim[j]=oggpack_read(opb,3)+1; /* 1 to 8 */
  140. info->class_subs[j]=oggpack_read(opb,2); /* 0,1,2,3 bits */
  141. if(info->class_subs[j]<0)
  142. goto err_out;
  143. if(info->class_subs[j])info->class_book[j]=oggpack_read(opb,8);
  144. if(info->class_book[j]<0 || info->class_book[j]>=ci->books)
  145. goto err_out;
  146. for(k=0;k<(1<<info->class_subs[j]);k++){
  147. info->class_subbook[j][k]=oggpack_read(opb,8)-1;
  148. if(info->class_subbook[j][k]<-1 || info->class_subbook[j][k]>=ci->books)
  149. goto err_out;
  150. }
  151. }
  152. /* read the post list */
  153. info->mult=oggpack_read(opb,2)+1; /* only 1,2,3,4 legal now */
  154. rangebits=oggpack_read(opb,4);
  155. for(j=0,k=0;j<info->partitions;j++){
  156. count+=info->class_dim[info->partitionclass[j]];
  157. for(;k<count;k++){
  158. int t=info->postlist[k+2]=oggpack_read(opb,rangebits);
  159. if(t<0 || t>=(1<<rangebits))
  160. goto err_out;
  161. }
  162. }
  163. info->postlist[0]=0;
  164. info->postlist[1]=1<<rangebits;
  165. return(info);
  166. err_out:
  167. floor1_free_info(info);
  168. return(NULL);
  169. }
  170. static int icomp(const void *a,const void *b){
  171. return(**(int **)a-**(int **)b);
  172. }
  173. static vorbis_look_floor *floor1_look(vorbis_dsp_state *vd,vorbis_info_mode *mi,
  174. vorbis_info_floor *in){
  175. int *sortpointer[VIF_POSIT+2];
  176. vorbis_info_floor1 *info=(vorbis_info_floor1 *)in;
  177. vorbis_look_floor1 *look=_ogg_calloc(1,sizeof(*look));
  178. int i,j,n=0;
  179. look->vi=info;
  180. look->n=info->postlist[1];
  181. /* we drop each position value in-between already decoded values,
  182. and use linear interpolation to predict each new value past the
  183. edges. The positions are read in the order of the position
  184. list... we precompute the bounding positions in the lookup. Of
  185. course, the neighbors can change (if a position is declined), but
  186. this is an initial mapping */
  187. for(i=0;i<info->partitions;i++)n+=info->class_dim[info->partitionclass[i]];
  188. n+=2;
  189. look->posts=n;
  190. /* also store a sorted position index */
  191. for(i=0;i<n;i++)sortpointer[i]=info->postlist+i;
  192. qsort(sortpointer,n,sizeof(*sortpointer),icomp);
  193. /* points from sort order back to range number */
  194. for(i=0;i<n;i++)look->forward_index[i]=sortpointer[i]-info->postlist;
  195. /* points from range order to sorted position */
  196. for(i=0;i<n;i++)look->reverse_index[look->forward_index[i]]=i;
  197. /* we actually need the post values too */
  198. for(i=0;i<n;i++)look->sorted_index[i]=info->postlist[look->forward_index[i]];
  199. /* quantize values to multiplier spec */
  200. switch(info->mult){
  201. case 1: /* 1024 -> 256 */
  202. look->quant_q=256;
  203. break;
  204. case 2: /* 1024 -> 128 */
  205. look->quant_q=128;
  206. break;
  207. case 3: /* 1024 -> 86 */
  208. look->quant_q=86;
  209. break;
  210. case 4: /* 1024 -> 64 */
  211. look->quant_q=64;
  212. break;
  213. }
  214. /* discover our neighbors for decode where we don't use fit flags
  215. (that would push the neighbors outward) */
  216. for(i=0;i<n-2;i++){
  217. int lo=0;
  218. int hi=1;
  219. int lx=0;
  220. int hx=look->n;
  221. int currentx=info->postlist[i+2];
  222. for(j=0;j<i+2;j++){
  223. int x=info->postlist[j];
  224. if(x>lx && x<currentx){
  225. lo=j;
  226. lx=x;
  227. }
  228. if(x<hx && x>currentx){
  229. hi=j;
  230. hx=x;
  231. }
  232. }
  233. look->loneighbor[i]=lo;
  234. look->hineighbor[i]=hi;
  235. }
  236. return(look);
  237. }
  238. static int render_point(int x0,int x1,int y0,int y1,int x){
  239. y0&=0x7fff; /* mask off flag */
  240. y1&=0x7fff;
  241. {
  242. int dy=y1-y0;
  243. int adx=x1-x0;
  244. int ady=abs(dy);
  245. int err=ady*(x-x0);
  246. int off=err/adx;
  247. if(dy<0)return(y0-off);
  248. return(y0+off);
  249. }
  250. }
  251. static int vorbis_dBquant(const float *x){
  252. int i= *x*7.3142857f+1023.5f;
  253. if(i>1023)return(1023);
  254. if(i<0)return(0);
  255. return i;
  256. }
  257. static float FLOOR_fromdB_LOOKUP[256]={
  258. 1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F,
  259. 1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F,
  260. 1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F,
  261. 2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F,
  262. 2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F,
  263. 3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F,
  264. 4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F,
  265. 6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F,
  266. 7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F,
  267. 1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F,
  268. 1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F,
  269. 1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F,
  270. 2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F,
  271. 2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F,
  272. 3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F,
  273. 4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F,
  274. 5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F,
  275. 7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F,
  276. 9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F,
  277. 1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F,
  278. 1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F,
  279. 2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F,
  280. 2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F,
  281. 3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F,
  282. 4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F,
  283. 5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F,
  284. 7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F,
  285. 9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F,
  286. 0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F,
  287. 0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F,
  288. 0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F,
  289. 0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F,
  290. 0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F,
  291. 0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F,
  292. 0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F,
  293. 0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F,
  294. 0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F,
  295. 0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F,
  296. 0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F,
  297. 0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F,
  298. 0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F,
  299. 0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F,
  300. 0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F,
  301. 0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F,
  302. 0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F,
  303. 0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F,
  304. 0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F,
  305. 0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F,
  306. 0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F,
  307. 0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F,
  308. 0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F,
  309. 0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F,
  310. 0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F,
  311. 0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F,
  312. 0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F,
  313. 0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F,
  314. 0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F,
  315. 0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F,
  316. 0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F,
  317. 0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F,
  318. 0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F,
  319. 0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F,
  320. 0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F,
  321. 0.82788260F, 0.88168307F, 0.9389798F, 1.F,
  322. };
  323. static void render_line(int x0,int x1,int y0,int y1,float *d){
  324. int dy=y1-y0;
  325. int adx=x1-x0;
  326. int ady=abs(dy);
  327. int base=dy/adx;
  328. int sy=(dy<0?base-1:base+1);
  329. int x=x0;
  330. int y=y0;
  331. int err=0;
  332. ady-=abs(base*adx);
  333. d[x]*=FLOOR_fromdB_LOOKUP[y];
  334. while(++x<x1){
  335. err=err+ady;
  336. if(err>=adx){
  337. err-=adx;
  338. y+=sy;
  339. }else{
  340. y+=base;
  341. }
  342. d[x]*=FLOOR_fromdB_LOOKUP[y];
  343. }
  344. }
  345. static void render_line0(int x0,int x1,int y0,int y1,float *d){
  346. int dy=y1-y0;
  347. int adx=x1-x0;
  348. int ady=abs(dy);
  349. int base=dy/adx;
  350. int sy=(dy<0?base-1:base+1);
  351. int x=x0;
  352. int y=y0;
  353. int err=0;
  354. ady-=abs(base*adx);
  355. d[x]=FLOOR_fromdB_LOOKUP[y];
  356. while(++x<x1){
  357. err=err+ady;
  358. if(err>=adx){
  359. err-=adx;
  360. y+=sy;
  361. }else{
  362. y+=base;
  363. }
  364. d[x]=FLOOR_fromdB_LOOKUP[y];
  365. }
  366. }
  367. /* the floor has already been filtered to only include relevant sections */
  368. static int accumulate_fit(const float *flr,const float *mdct,
  369. int x0, int x1,lsfit_acc *a,
  370. int n,vorbis_info_floor1 *info){
  371. long i;
  372. int quantized=vorbis_dBquant(flr+x0);
  373. long xa=0,ya=0,x2a=0,y2a=0,xya=0,na=0, xb=0,yb=0,x2b=0,y2b=0,xyb=0,nb=0;
  374. memset(a,0,sizeof(*a));
  375. a->x0=x0;
  376. a->x1=x1;
  377. a->edgey0=quantized;
  378. if(x1>n)x1=n;
  379. for(i=x0;i<x1;i++){
  380. int quantized=vorbis_dBquant(flr+i);
  381. if(quantized){
  382. if(mdct[i]+info->twofitatten>=flr[i]){
  383. xa += i;
  384. ya += quantized;
  385. x2a += i*i;
  386. y2a += quantized*quantized;
  387. xya += i*quantized;
  388. na++;
  389. }else{
  390. xb += i;
  391. yb += quantized;
  392. x2b += i*i;
  393. y2b += quantized*quantized;
  394. xyb += i*quantized;
  395. nb++;
  396. }
  397. }
  398. }
  399. xb+=xa;
  400. yb+=ya;
  401. x2b+=x2a;
  402. y2b+=y2a;
  403. xyb+=xya;
  404. nb+=na;
  405. /* weight toward the actually used frequencies if we meet the threshhold */
  406. {
  407. int weight;
  408. if(nb<info->twofitminsize || na<info->twofitminused){
  409. weight=0;
  410. }else{
  411. weight=nb*info->twofitweight/na;
  412. }
  413. a->xa=xa*weight+xb;
  414. a->ya=ya*weight+yb;
  415. a->x2a=x2a*weight+x2b;
  416. a->y2a=y2a*weight+y2b;
  417. a->xya=xya*weight+xyb;
  418. a->an=na*weight+nb;
  419. a->n=nb;
  420. a->un=na;
  421. if(nb>=info->unusedminsize)a->un++;
  422. }
  423. a->edgey1=-200;
  424. if(x1<n){
  425. int quantized=vorbis_dBquant(flr+i);
  426. a->edgey1=quantized;
  427. }
  428. return(a->n);
  429. }
  430. /* returns < 0 on too few points to fit, >=0 (meansq error) on success */
  431. static int fit_line(lsfit_acc *a,int fits,int *y0,int *y1){
  432. long x=0,y=0,x2=0,y2=0,xy=0,n=0,an=0,i;
  433. long x0=a[0].x0;
  434. long x1=a[fits-1].x1;
  435. for(i=0;i<fits;i++){
  436. if(a[i].un){
  437. x+=a[i].xa;
  438. y+=a[i].ya;
  439. x2+=a[i].x2a;
  440. y2+=a[i].y2a;
  441. xy+=a[i].xya;
  442. n+=a[i].n;
  443. an+=a[i].an;
  444. }
  445. }
  446. if(*y0>=0){ /* hint used to break degenerate cases */
  447. x+= x0;
  448. y+= *y0;
  449. x2+= x0 * x0;
  450. y2+= *y0 * *y0;
  451. xy+= *y0 * x0;
  452. n++;
  453. an++;
  454. }
  455. if(*y1>=0){ /* hint used to break degenerate cases */
  456. x+= x1;
  457. y+= *y1;
  458. x2+= x1 * x1;
  459. y2+= *y1 * *y1;
  460. xy+= *y1 * x1;
  461. n++;
  462. an++;
  463. }
  464. if(n<2)return(n-2);
  465. {
  466. /* need 64 bit multiplies, which C doesn't give portably as int */
  467. double fx=x;
  468. double fy=y;
  469. double fx2=x2;
  470. double fxy=xy;
  471. double denom=1./(an*fx2-fx*fx);
  472. double a=(fy*fx2-fxy*fx)*denom;
  473. double b=(an*fxy-fx*fy)*denom;
  474. *y0=rint(a+b*x0);
  475. *y1=rint(a+b*x1);
  476. /* limit to our range! */
  477. if(*y0>1023)*y0=1023;
  478. if(*y1>1023)*y1=1023;
  479. if(*y0<0)*y0=0;
  480. if(*y1<0)*y1=0;
  481. return(0);
  482. }
  483. }
  484. /*static void fit_line_point(lsfit_acc *a,int fits,int *y0,int *y1){
  485. long y=0;
  486. int i;
  487. for(i=0;i<fits && y==0;i++)
  488. y+=a[i].ya;
  489. *y0=*y1=y;
  490. }*/
  491. static int inspect_error(int x0,int x1,int y0,int y1,const float *mask,
  492. const float *mdct,
  493. vorbis_info_floor1 *info){
  494. int dy=y1-y0;
  495. int adx=x1-x0;
  496. int ady=abs(dy);
  497. int base=dy/adx;
  498. int sy=(dy<0?base-1:base+1);
  499. int x=x0;
  500. int y=y0;
  501. int err=0;
  502. int val=vorbis_dBquant(mask+x);
  503. int mse=0;
  504. int n=0;
  505. ady-=abs(base*adx);
  506. if(mdct[x]+info->twofitatten>=mask[x]){
  507. if(y+info->maxover<val)return(1);
  508. if(y-info->maxunder>val)return(1);
  509. mse=(y-val);
  510. mse*=mse;
  511. n++;
  512. }
  513. while(++x<x1){
  514. err=err+ady;
  515. if(err>=adx){
  516. err-=adx;
  517. y+=sy;
  518. }else{
  519. y+=base;
  520. }
  521. if(mdct[x]+info->twofitatten>=mask[x]){
  522. val=vorbis_dBquant(mask+x);
  523. if(val){
  524. if(y+info->maxover<val)return(1);
  525. if(y-info->maxunder>val)return(1);
  526. mse+=((y-val)*(y-val));
  527. n++;
  528. }
  529. }
  530. }
  531. if(n){
  532. if(info->maxover*info->maxover/n>info->maxerr)return(0);
  533. if(info->maxunder*info->maxunder/n>info->maxerr)return(0);
  534. if(mse/n>info->maxerr)return(1);
  535. }
  536. return(0);
  537. }
  538. static int post_Y(int *A,int *B,int pos){
  539. if(A[pos]<0)
  540. return B[pos];
  541. if(B[pos]<0)
  542. return A[pos];
  543. return (A[pos]+B[pos])>>1;
  544. }
  545. static int floor1_forward(vorbis_block *vb,vorbis_look_floor *in,
  546. float *mdct, const float *logmdct, /* in */
  547. const float *logmask, const float *logmax, /* in */
  548. float *codedflr){ /* out */
  549. static int seq=0;
  550. long i,j,k,l;
  551. vorbis_look_floor1 *look=(vorbis_look_floor1 *)in;
  552. vorbis_info_floor1 *info=look->vi;
  553. long n=info->n;
  554. long posts=look->posts;
  555. long nonzero=0;
  556. lsfit_acc fits[VIF_POSIT+1];
  557. int fit_valueA[VIF_POSIT+2]; /* index by range list position */
  558. int fit_valueB[VIF_POSIT+2]; /* index by range list position */
  559. int fit_flag[VIF_POSIT+2];
  560. int loneighbor[VIF_POSIT+2]; /* sorted index of range list position (+2) */
  561. int hineighbor[VIF_POSIT+2];
  562. int memo[VIF_POSIT+2];
  563. codec_setup_info *ci=vb->vd->vi->codec_setup;
  564. static_codebook **sbooks=ci->book_param;
  565. codebook *books=NULL;
  566. int writeflag=0;
  567. if(vb->vd->backend_state){
  568. books=((backend_lookup_state *)(vb->vd->backend_state))->
  569. fullbooks;
  570. writeflag=1;
  571. }
  572. memset(fit_flag,0,sizeof(fit_flag));
  573. for(i=0;i<posts;i++)loneighbor[i]=0; /* 0 for the implicit 0 post */
  574. for(i=0;i<posts;i++)hineighbor[i]=1; /* 1 for the implicit post at n */
  575. for(i=0;i<posts;i++)memo[i]=-1; /* no neighbor yet */
  576. /* Scan back from high edge to first 'used' frequency */
  577. for(;n>info->unusedmin_n;n--)
  578. if(logmdct[n-1]>-floor1_rangedB &&
  579. logmdct[n-1]+info->twofitatten>logmask[n-1])break;
  580. /* quantize the relevant floor points and collect them into line fit
  581. structures (one per minimal division) at the same time */
  582. if(posts==0){
  583. nonzero+=accumulate_fit(logmask,logmax,0,n,fits,n,info);
  584. }else{
  585. for(i=0;i<posts-1;i++)
  586. nonzero+=accumulate_fit(logmask,logmax,look->sorted_index[i],
  587. look->sorted_index[i+1],fits+i,
  588. n,info);
  589. }
  590. if(nonzero){
  591. /* start by fitting the implicit base case.... */
  592. int y0=-200;
  593. int y1=-200;
  594. int mse=fit_line(fits,posts-1,&y0,&y1);
  595. if(mse<0){
  596. /* Only a single nonzero point */
  597. y0=-200;
  598. y1=0;
  599. fit_line(fits,posts-1,&y0,&y1);
  600. }
  601. fit_flag[0]=1;
  602. fit_flag[1]=1;
  603. fit_valueA[0]=y0;
  604. fit_valueB[0]=y0;
  605. fit_valueB[1]=y1;
  606. fit_valueA[1]=y1;
  607. if(mse>=0){
  608. /* Non degenerate case */
  609. /* start progressive splitting. This is a greedy, non-optimal
  610. algorithm, but simple and close enough to the best
  611. answer. */
  612. for(i=2;i<posts;i++){
  613. int sortpos=look->reverse_index[i];
  614. int ln=loneighbor[sortpos];
  615. int hn=hineighbor[sortpos];
  616. /* eliminate repeat searches of a particular range with a memo */
  617. if(memo[ln]!=hn){
  618. /* haven't performed this error search yet */
  619. int lsortpos=look->reverse_index[ln];
  620. int hsortpos=look->reverse_index[hn];
  621. memo[ln]=hn;
  622. /* if this is an empty segment, its endpoints don't matter.
  623. Mark as such */
  624. for(j=lsortpos;j<hsortpos;j++)
  625. if(fits[j].un)break;
  626. if(j==hsortpos){
  627. /* empty segment; important to note that this does not
  628. break 0/n post case */
  629. fit_valueB[ln]=-200;
  630. if(fit_valueA[ln]<0)
  631. fit_flag[ln]=0;
  632. fit_valueA[hn]=-200;
  633. if(fit_valueB[hn]<0)
  634. fit_flag[hn]=0;
  635. }else{
  636. /* A note: we want to bound/minimize *local*, not global, error */
  637. int lx=info->postlist[ln];
  638. int hx=info->postlist[hn];
  639. int ly=post_Y(fit_valueA,fit_valueB,ln);
  640. int hy=post_Y(fit_valueA,fit_valueB,hn);
  641. if(inspect_error(lx,hx,ly,hy,logmask,logmdct,info)){
  642. /* outside error bounds/begin search area. Split it. */
  643. int ly0=-200;
  644. int ly1=-200;
  645. int hy0=-200;
  646. int hy1=-200;
  647. int lmse=fit_line(fits+lsortpos,sortpos-lsortpos,&ly0,&ly1);
  648. int hmse=fit_line(fits+sortpos,hsortpos-sortpos,&hy0,&hy1);
  649. /* the boundary/sparsity cases are the hard part. They
  650. don't happen often given that we use the full mask
  651. curve (weighted) now, but when they do happen they
  652. can go boom. Pay them detailed attention */
  653. /* cases for a segment:
  654. >=0) normal fit (>=2 unique points)
  655. -1) one point on x0;
  656. one point on x1; <-- disallowed by fit_line
  657. -2) one point in between x0 and x1
  658. -3) no points */
  659. switch(lmse){
  660. case -2:
  661. /* no points in the low segment */
  662. break;
  663. case -1:
  664. ly0=fits[lsortpos].edgey0;
  665. break;
  666. /*default:
  667. break;*/
  668. }
  669. switch(hmse){
  670. case -2:
  671. /* no points in the hi segment */
  672. break;
  673. case -1:
  674. hy0=fits[sortpos].edgey0;
  675. break;
  676. }
  677. /* store new edge values */
  678. fit_valueB[ln]=ly0;
  679. if(ln==0 && ly0>=0)fit_valueA[ln]=ly0;
  680. fit_valueA[i]=ly1;
  681. fit_valueB[i]=hy0;
  682. fit_valueA[hn]=hy1;
  683. if(hn==1 && hy1>=0)fit_valueB[hn]=hy1;
  684. if(ly0<0 && fit_valueA[ln]<0)
  685. fit_flag[ln]=0;
  686. if(hy1<0 && fit_valueB[hn]<0)
  687. fit_flag[hn]=0;
  688. if(ly1>=0 || hy0>=0){
  689. /* store new neighbor values */
  690. for(j=sortpos-1;j>=0;j--)
  691. if(hineighbor[j]==hn)
  692. hineighbor[j]=i;
  693. else
  694. break;
  695. for(j=sortpos+1;j<posts;j++)
  696. if(loneighbor[j]==ln)
  697. loneighbor[j]=i;
  698. else
  699. break;
  700. /* store flag (set) */
  701. fit_flag[i]=1;
  702. }
  703. }
  704. }
  705. }
  706. }
  707. }
  708. /* quantize values to multiplier spec */
  709. switch(info->mult){
  710. case 1: /* 1024 -> 256 */
  711. for(i=0;i<posts;i++)
  712. if(fit_flag[i])
  713. fit_valueA[i]=post_Y(fit_valueA,fit_valueB,i)>>2;
  714. break;
  715. case 2: /* 1024 -> 128 */
  716. for(i=0;i<posts;i++)
  717. if(fit_flag[i])
  718. fit_valueA[i]=post_Y(fit_valueA,fit_valueB,i)>>3;
  719. break;
  720. case 3: /* 1024 -> 86 */
  721. for(i=0;i<posts;i++)
  722. if(fit_flag[i])
  723. fit_valueA[i]=post_Y(fit_valueA,fit_valueB,i)/12;
  724. break;
  725. case 4: /* 1024 -> 64 */
  726. for(i=0;i<posts;i++)
  727. if(fit_flag[i])
  728. fit_valueA[i]=post_Y(fit_valueA,fit_valueB,i)>>4;
  729. break;
  730. }
  731. /* find prediction values for each post and subtract them */
  732. for(i=2;i<posts;i++){
  733. int sp=look->reverse_index[i];
  734. int ln=look->loneighbor[i-2];
  735. int hn=look->hineighbor[i-2];
  736. int x0=info->postlist[ln];
  737. int x1=info->postlist[hn];
  738. int y0=fit_valueA[ln];
  739. int y1=fit_valueA[hn];
  740. int predicted=render_point(x0,x1,y0,y1,info->postlist[i]);
  741. if(fit_flag[i]){
  742. int headroom=(look->quant_q-predicted<predicted?
  743. look->quant_q-predicted:predicted);
  744. int val=fit_valueA[i]-predicted;
  745. /* at this point the 'deviation' value is in the range +/- max
  746. range, but the real, unique range can always be mapped to
  747. only [0-maxrange). So we want to wrap the deviation into
  748. this limited range, but do it in the way that least screws
  749. an essentially gaussian probability distribution. */
  750. if(val<0)
  751. if(val<-headroom)
  752. val=headroom-val-1;
  753. else
  754. val=-1-(val<<1);
  755. else
  756. if(val>=headroom)
  757. val= val+headroom;
  758. else
  759. val<<=1;
  760. fit_valueB[i]=val;
  761. /* unroll the neighbor arrays */
  762. for(j=sp+1;j<posts;j++)
  763. if(loneighbor[j]==i)
  764. loneighbor[j]=loneighbor[sp];
  765. else
  766. break;
  767. for(j=sp-1;j>=0;j--)
  768. if(hineighbor[j]==i)
  769. hineighbor[j]=hineighbor[sp];
  770. else
  771. break;
  772. }else{
  773. fit_valueA[i]=predicted;
  774. fit_valueB[i]=0;
  775. }
  776. if(fit_valueB[i]==0)
  777. fit_valueA[i]|=0x8000;
  778. else{
  779. fit_valueA[look->loneighbor[i-2]]&=0x7fff;
  780. fit_valueA[look->hineighbor[i-2]]&=0x7fff;
  781. }
  782. }
  783. /* we have everything we need. pack it out */
  784. /* mark nontrivial floor */
  785. if(writeflag){
  786. oggpack_write(&vb->opb,1,1);
  787. /* beginning/end post */
  788. look->frames++;
  789. look->postbits+=ilog(look->quant_q-1)*2;
  790. oggpack_write(&vb->opb,fit_valueA[0],ilog(look->quant_q-1));
  791. oggpack_write(&vb->opb,fit_valueA[1],ilog(look->quant_q-1));
  792. /* partition by partition */
  793. for(i=0,j=2;i<info->partitions;i++){
  794. int class=info->partitionclass[i];
  795. int cdim=info->class_dim[class];
  796. int csubbits=info->class_subs[class];
  797. int csub=1<<csubbits;
  798. int bookas[8]={0,0,0,0,0,0,0,0};
  799. int cval=0;
  800. int cshift=0;
  801. /* generate the partition's first stage cascade value */
  802. if(csubbits){
  803. int maxval[8];
  804. for(k=0;k<csub;k++){
  805. int booknum=info->class_subbook[class][k];
  806. if(booknum<0){
  807. maxval[k]=1;
  808. }else{
  809. maxval[k]=sbooks[info->class_subbook[class][k]]->entries;
  810. }
  811. }
  812. for(k=0;k<cdim;k++){
  813. for(l=0;l<csub;l++){
  814. int val=fit_valueB[j+k];
  815. if(val<maxval[l]){
  816. bookas[k]=l;
  817. break;
  818. }
  819. }
  820. cval|= bookas[k]<<cshift;
  821. cshift+=csubbits;
  822. }
  823. /* write it */
  824. look->phrasebits+=
  825. vorbis_book_encode(books+info->class_book[class],cval,&vb->opb);
  826. #ifdef TRAIN_FLOOR1
  827. {
  828. FILE *of;
  829. char buffer[80];
  830. sprintf(buffer,"line_%dx%ld_class%d.vqd",
  831. vb->pcmend/2,posts-2,class);
  832. of=fopen(buffer,"a");
  833. fprintf(of,"%d\n",cval);
  834. fclose(of);
  835. }
  836. #endif
  837. }
  838. /* write post values */
  839. for(k=0;k<cdim;k++){
  840. int book=info->class_subbook[class][bookas[k]];
  841. if(book>=0){
  842. /* hack to allow training with 'bad' books */
  843. if(fit_valueB[j+k]<(books+book)->entries)
  844. look->postbits+=vorbis_book_encode(books+book,
  845. fit_valueB[j+k],&vb->opb);
  846. /*else
  847. fprintf(stderr,"+!");*/
  848. #ifdef TRAIN_FLOOR1
  849. {
  850. FILE *of;
  851. char buffer[80];
  852. sprintf(buffer,"line_%dx%ld_%dsub%d.vqd",
  853. vb->pcmend/2,posts-2,class,bookas[k]);
  854. of=fopen(buffer,"a");
  855. fprintf(of,"%d\n",fit_valueB[j+k]);
  856. fclose(of);
  857. }
  858. #endif
  859. }
  860. }
  861. j+=cdim;
  862. }
  863. }
  864. {
  865. /* generate quantized floor equivalent to what we'd unpack in decode */
  866. int hx;
  867. int lx=0;
  868. int ly=fit_valueA[0]*info->mult;
  869. for(j=1;j<posts;j++){
  870. int current=look->forward_index[j];
  871. if(!(fit_valueA[current]&0x8000)){
  872. int hy=(fit_valueA[current]&0x7fff)*info->mult;
  873. hx=info->postlist[current];
  874. render_line0(lx,hx,ly,hy,codedflr);
  875. lx=hx;
  876. ly=hy;
  877. }
  878. }
  879. for(j=lx;j<vb->pcmend/2;j++)codedflr[j]=codedflr[j-1]; /* be certain */
  880. /* use it to create residue vector. Eliminate mdct elements
  881. that were below the error training attenuation relative to
  882. the original mask. This avoids portions of the floor fit
  883. that were considered 'unused' in fitting from being used in
  884. coding residue if the unfit values are significantly below
  885. the original input mask */
  886. for(j=0;j<n;j++)
  887. if(logmdct[j]+info->twofitatten<logmask[j])
  888. mdct[j]=0.f;
  889. for(j=n;j<vb->pcmend/2;j++)mdct[j]=0.f;
  890. }
  891. }else{
  892. if(writeflag)oggpack_write(&vb->opb,0,1);
  893. memset(codedflr,0,n*sizeof(*codedflr));
  894. memset(mdct,0,n*sizeof(*mdct));
  895. }
  896. seq++;
  897. return(nonzero);
  898. }
  899. static void *floor1_inverse1(vorbis_block *vb,vorbis_look_floor *in){
  900. vorbis_look_floor1 *look=(vorbis_look_floor1 *)in;
  901. vorbis_info_floor1 *info=look->vi;
  902. int i,j,k;
  903. codebook *books=((backend_lookup_state *)(vb->vd->backend_state))->
  904. fullbooks;
  905. /* unpack wrapped/predicted values from stream */
  906. if(oggpack_read(&vb->opb,1)==1){
  907. int *fit_value=_vorbis_block_alloc(vb,(look->posts)*sizeof(*fit_value));
  908. fit_value[0]=oggpack_read(&vb->opb,ilog(look->quant_q-1));
  909. fit_value[1]=oggpack_read(&vb->opb,ilog(look->quant_q-1));
  910. /* partition by partition */
  911. /* partition by partition */
  912. for(i=0,j=2;i<info->partitions;i++){
  913. int class=info->partitionclass[i];
  914. int cdim=info->class_dim[class];
  915. int csubbits=info->class_subs[class];
  916. int csub=1<<csubbits;
  917. int cval=0;
  918. /* decode the partition's first stage cascade value */
  919. if(csubbits){
  920. cval=vorbis_book_decode(books+info->class_book[class],&vb->opb);
  921. if(cval==-1)goto eop;
  922. }
  923. for(k=0;k<cdim;k++){
  924. int book=info->class_subbook[class][cval&(csub-1)];
  925. cval>>=csubbits;
  926. if(book>=0){
  927. if((fit_value[j+k]=vorbis_book_decode(books+book,&vb->opb))==-1)
  928. goto eop;
  929. }else{
  930. fit_value[j+k]=0;
  931. }
  932. }
  933. j+=cdim;
  934. }
  935. /* unwrap positive values and reconsitute via linear interpolation */
  936. for(i=2;i<look->posts;i++){
  937. int predicted=render_point(info->postlist[look->loneighbor[i-2]],
  938. info->postlist[look->hineighbor[i-2]],
  939. fit_value[look->loneighbor[i-2]],
  940. fit_value[look->hineighbor[i-2]],
  941. info->postlist[i]);
  942. int hiroom=look->quant_q-predicted;
  943. int loroom=predicted;
  944. int room=(hiroom<loroom?hiroom:loroom)<<1;
  945. int val=fit_value[i];
  946. if(val){
  947. if(val>=room){
  948. if(hiroom>loroom){
  949. val = val-loroom;
  950. }else{
  951. val = -1-(val-hiroom);
  952. }
  953. }else{
  954. if(val&1){
  955. val= -((val+1)>>1);
  956. }else{
  957. val>>=1;
  958. }
  959. }
  960. fit_value[i]=val+predicted;
  961. fit_value[look->loneighbor[i-2]]&=0x7fff;
  962. fit_value[look->hineighbor[i-2]]&=0x7fff;
  963. }else{
  964. fit_value[i]=predicted|0x8000;
  965. }
  966. }
  967. return(fit_value);
  968. }
  969. eop:
  970. return(NULL);
  971. }
  972. static int floor1_inverse2(vorbis_block *vb,vorbis_look_floor *in,void *memo,
  973. float *out){
  974. vorbis_look_floor1 *look=(vorbis_look_floor1 *)in;
  975. vorbis_info_floor1 *info=look->vi;
  976. codec_setup_info *ci=vb->vd->vi->codec_setup;
  977. int n=ci->blocksizes[vb->mode]/2;
  978. int j;
  979. if(memo){
  980. /* render the lines */
  981. int *fit_value=(int *)memo;
  982. int hx;
  983. int lx=0;
  984. int ly=fit_value[0]*info->mult;
  985. for(j=1;j<look->posts;j++){
  986. int current=look->forward_index[j];
  987. int hy=fit_value[current]&0x7fff;
  988. if(hy==fit_value[current]){
  989. hy*=info->mult;
  990. hx=info->postlist[current];
  991. render_line(lx,hx,ly,hy,out);
  992. lx=hx;
  993. ly=hy;
  994. }
  995. }
  996. for(j=hx;j<n;j++)out[j]*=ly; /* be certain */
  997. return(1);
  998. }
  999. memset(out,0,sizeof(*out)*n);
  1000. return(0);
  1001. }
  1002. /* export hooks */
  1003. vorbis_func_floor floor1_exportbundle={
  1004. &floor1_pack,&floor1_unpack,&floor1_look,&floor1_copy_info,&floor1_free_info,
  1005. &floor1_free_look,&floor1_forward,&floor1_inverse1,&floor1_inverse2
  1006. };