floor1.c 30 KB

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