codebook.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
  4. * *
  5. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  6. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  8. * *
  9. * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
  10. * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: basic codebook pack/unpack/code/decode operations
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include "ogg.h"
  19. #include "ivorbiscodec.h"
  20. #include "codebook.h"
  21. #include "misc.h"
  22. #include "os.h"
  23. /**** pack/unpack helpers ******************************************/
  24. int _ilog(unsigned int v){
  25. int ret=0;
  26. while(v){
  27. ret++;
  28. v>>=1;
  29. }
  30. return(ret);
  31. }
  32. static ogg_uint32_t decpack(long entry,long used_entry,long quantvals,
  33. codebook *b,oggpack_buffer *opb,int maptype){
  34. ogg_uint32_t ret=0;
  35. int j;
  36. switch(b->dec_type){
  37. case 0:
  38. return (ogg_uint32_t)entry;
  39. case 1:
  40. if(maptype==1){
  41. /* vals are already read into temporary column vector here */
  42. for(j=0;j<b->dim;j++){
  43. ogg_uint32_t off=entry%quantvals;
  44. entry/=quantvals;
  45. ret|=((ogg_uint16_t *)(b->q_val))[off]<<(b->q_bits*j);
  46. }
  47. }else{
  48. for(j=0;j<b->dim;j++)
  49. ret|=oggpack_read(opb,b->q_bits)<<(b->q_bits*j);
  50. }
  51. return ret;
  52. case 2:
  53. for(j=0;j<b->dim;j++){
  54. ogg_uint32_t off=entry%quantvals;
  55. entry/=quantvals;
  56. ret|=off<<(b->q_pack*j);
  57. }
  58. return ret;
  59. case 3:
  60. return (ogg_uint32_t)used_entry;
  61. }
  62. return 0; /* silence compiler */
  63. }
  64. /* 32 bit float (not IEEE; nonnormalized mantissa +
  65. biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm
  66. Why not IEEE? It's just not that important here. */
  67. static ogg_int32_t _float32_unpack(long val,int *point){
  68. long mant=val&0x1fffff;
  69. int sign=val&0x80000000;
  70. *point=((val&0x7fe00000L)>>21)-788;
  71. if(mant){
  72. while(!(mant&0x40000000)){
  73. mant<<=1;
  74. *point-=1;
  75. }
  76. if(sign)mant= -mant;
  77. }else{
  78. *point=-9999;
  79. }
  80. return mant;
  81. }
  82. /* choose the smallest supported node size that fits our decode table.
  83. Legal bytewidths are 1/1 1/2 2/2 2/4 4/4 */
  84. static int _determine_node_bytes(long used, int leafwidth){
  85. /* special case small books to size 4 to avoid multiple special
  86. cases in repack */
  87. if(used<2)
  88. return 4;
  89. if(leafwidth==3)leafwidth=4;
  90. if(_ilog(3*used-6)+1 <= leafwidth*4)
  91. return leafwidth/2?leafwidth/2:1;
  92. return leafwidth;
  93. }
  94. /* convenience/clarity; leaves are specified as multiple of node word
  95. size (1 or 2) */
  96. static int _determine_leaf_words(int nodeb, int leafwidth){
  97. if(leafwidth>nodeb)return 2;
  98. return 1;
  99. }
  100. /* given a list of word lengths, number of used entries, and byte
  101. width of a leaf, generate the decode table */
  102. static int _make_words(char *l,long n,ogg_uint32_t *r,long quantvals,
  103. codebook *b, oggpack_buffer *opb,int maptype){
  104. long i,j,count=0;
  105. long top=0;
  106. ogg_uint32_t marker[33];
  107. if(n<2){
  108. r[0]=0x80000000;
  109. }else{
  110. memset(marker,0,sizeof(marker));
  111. for(i=0;i<n;i++){
  112. long length=l[i];
  113. if(length){
  114. ogg_uint32_t entry=marker[length];
  115. long chase=0;
  116. if(count && !entry)return -1; /* overpopulated tree! */
  117. /* chase the tree as far as it's already populated, fill in past */
  118. for(j=0;j<length-1;j++){
  119. int bit=(entry>>(length-j-1))&1;
  120. if(chase>=top){
  121. top++;
  122. r[chase*2]=top;
  123. r[chase*2+1]=0;
  124. }else
  125. if(!r[chase*2+bit])
  126. r[chase*2+bit]=top;
  127. chase=r[chase*2+bit];
  128. }
  129. {
  130. int bit=(entry>>(length-j-1))&1;
  131. if(chase>=top){
  132. top++;
  133. r[chase*2+1]=0;
  134. }
  135. r[chase*2+bit]= decpack(i,count++,quantvals,b,opb,maptype) |
  136. 0x80000000;
  137. }
  138. /* Look to see if the next shorter marker points to the node
  139. above. if so, update it and repeat. */
  140. for(j=length;j>0;j--){
  141. if(marker[j]&1){
  142. marker[j]=marker[j-1]<<1;
  143. break;
  144. }
  145. marker[j]++;
  146. }
  147. /* prune the tree; the implicit invariant says all the longer
  148. markers were dangling from our just-taken node. Dangle them
  149. from our *new* node. */
  150. for(j=length+1;j<33;j++)
  151. if((marker[j]>>1) == entry){
  152. entry=marker[j];
  153. marker[j]=marker[j-1]<<1;
  154. }else
  155. break;
  156. }
  157. }
  158. }
  159. return 0;
  160. }
  161. static int _make_decode_table(codebook *s,char *lengthlist,long quantvals,
  162. oggpack_buffer *opb,int maptype){
  163. int i;
  164. ogg_uint32_t *work;
  165. if(s->dec_nodeb==4){
  166. s->dec_table=_ogg_malloc((s->used_entries*2+1)*sizeof(*work));
  167. /* +1 (rather than -2) is to accommodate 0 and 1 sized books,
  168. which are specialcased to nodeb==4 */
  169. if(_make_words(lengthlist,s->entries,
  170. s->dec_table,quantvals,s,opb,maptype))return 1;
  171. return 0;
  172. }
  173. work=alloca((s->used_entries*2-2)*sizeof(*work));
  174. if(_make_words(lengthlist,s->entries,work,quantvals,s,opb,maptype))return 1;
  175. s->dec_table=_ogg_malloc((s->used_entries*(s->dec_leafw+1)-2)*
  176. s->dec_nodeb);
  177. if(s->dec_leafw==1){
  178. switch(s->dec_nodeb){
  179. case 1:
  180. for(i=0;i<s->used_entries*2-2;i++)
  181. ((unsigned char *)s->dec_table)[i]=
  182. ((work[i] & 0x80000000UL) >> 24) | work[i];
  183. break;
  184. case 2:
  185. for(i=0;i<s->used_entries*2-2;i++)
  186. ((ogg_uint16_t *)s->dec_table)[i]=
  187. ((work[i] & 0x80000000UL) >> 16) | work[i];
  188. break;
  189. }
  190. }else{
  191. /* more complex; we have to do a two-pass repack that updates the
  192. node indexing. */
  193. long top=s->used_entries*3-2;
  194. if(s->dec_nodeb==1){
  195. unsigned char *out=(unsigned char *)s->dec_table;
  196. for(i=s->used_entries*2-4;i>=0;i-=2){
  197. if(work[i]&0x80000000UL){
  198. if(work[i+1]&0x80000000UL){
  199. top-=4;
  200. out[top]=(work[i]>>8 & 0x7f)|0x80;
  201. out[top+1]=(work[i+1]>>8 & 0x7f)|0x80;
  202. out[top+2]=work[i] & 0xff;
  203. out[top+3]=work[i+1] & 0xff;
  204. }else{
  205. top-=3;
  206. out[top]=(work[i]>>8 & 0x7f)|0x80;
  207. out[top+1]=work[work[i+1]*2];
  208. out[top+2]=work[i] & 0xff;
  209. }
  210. }else{
  211. if(work[i+1]&0x80000000UL){
  212. top-=3;
  213. out[top]=work[work[i]*2];
  214. out[top+1]=(work[i+1]>>8 & 0x7f)|0x80;
  215. out[top+2]=work[i+1] & 0xff;
  216. }else{
  217. top-=2;
  218. out[top]=work[work[i]*2];
  219. out[top+1]=work[work[i+1]*2];
  220. }
  221. }
  222. work[i]=top;
  223. }
  224. }else{
  225. ogg_uint16_t *out=(ogg_uint16_t *)s->dec_table;
  226. for(i=s->used_entries*2-4;i>=0;i-=2){
  227. if(work[i]&0x80000000UL){
  228. if(work[i+1]&0x80000000UL){
  229. top-=4;
  230. out[top]=(work[i]>>16 & 0x7fff)|0x8000;
  231. out[top+1]=(work[i+1]>>16 & 0x7fff)|0x8000;
  232. out[top+2]=work[i] & 0xffff;
  233. out[top+3]=work[i+1] & 0xffff;
  234. }else{
  235. top-=3;
  236. out[top]=(work[i]>>16 & 0x7fff)|0x8000;
  237. out[top+1]=work[work[i+1]*2];
  238. out[top+2]=work[i] & 0xffff;
  239. }
  240. }else{
  241. if(work[i+1]&0x80000000UL){
  242. top-=3;
  243. out[top]=work[work[i]*2];
  244. out[top+1]=(work[i+1]>>16 & 0x7fff)|0x8000;
  245. out[top+2]=work[i+1] & 0xffff;
  246. }else{
  247. top-=2;
  248. out[top]=work[work[i]*2];
  249. out[top+1]=work[work[i+1]*2];
  250. }
  251. }
  252. work[i]=top;
  253. }
  254. }
  255. }
  256. return 0;
  257. }
  258. /* most of the time, entries%dimensions == 0, but we need to be
  259. well defined. We define that the possible vales at each
  260. scalar is values == entries/dim. If entries%dim != 0, we'll
  261. have 'too few' values (values*dim<entries), which means that
  262. we'll have 'left over' entries; left over entries use zeroed
  263. values (and are wasted). So don't generate codebooks like
  264. that */
  265. /* there might be a straightforward one-line way to do the below
  266. that's portable and totally safe against roundoff, but I haven't
  267. thought of it. Therefore, we opt on the side of caution */
  268. long _book_maptype1_quantvals(codebook *b){
  269. /* get us a starting hint, we'll polish it below */
  270. int bits=_ilog(b->entries);
  271. int vals=b->entries>>((bits-1)*(b->dim-1)/b->dim);
  272. while(1){
  273. long acc=1;
  274. long acc1=1;
  275. int i;
  276. for(i=0;i<b->dim;i++){
  277. acc*=vals;
  278. acc1*=vals+1;
  279. }
  280. if(acc<=b->entries && acc1>b->entries){
  281. return(vals);
  282. }else{
  283. if(acc>b->entries){
  284. vals--;
  285. }else{
  286. vals++;
  287. }
  288. }
  289. }
  290. }
  291. void vorbis_book_clear(codebook *b){
  292. /* static book is not cleared; we're likely called on the lookup and
  293. the static codebook belongs to the info struct */
  294. if(b->q_val)_ogg_free(b->q_val);
  295. if(b->dec_table)_ogg_free(b->dec_table);
  296. memset(b,0,sizeof(*b));
  297. }
  298. int vorbis_book_unpack(oggpack_buffer *opb,codebook *s){
  299. char *lengthlist=NULL;
  300. int quantvals=0;
  301. long i,j;
  302. int maptype;
  303. memset(s,0,sizeof(*s));
  304. /* make sure alignment is correct */
  305. if(oggpack_read(opb,24)!=0x564342)goto _eofout;
  306. /* first the basic parameters */
  307. s->dim=oggpack_read(opb,16);
  308. s->entries=oggpack_read(opb,24);
  309. if(s->entries==-1)goto _eofout;
  310. /* codeword ordering.... length ordered or unordered? */
  311. switch((int)oggpack_read(opb,1)){
  312. case 0:
  313. /* unordered */
  314. lengthlist=(char *)alloca(sizeof(*lengthlist)*s->entries);
  315. /* allocated but unused entries? */
  316. if(oggpack_read(opb,1)){
  317. /* yes, unused entries */
  318. for(i=0;i<s->entries;i++){
  319. if(oggpack_read(opb,1)){
  320. long num=oggpack_read(opb,5);
  321. if(num==-1)goto _eofout;
  322. lengthlist[i]=num+1;
  323. s->used_entries++;
  324. if(num+1>s->dec_maxlength)s->dec_maxlength=num+1;
  325. }else
  326. lengthlist[i]=0;
  327. }
  328. }else{
  329. /* all entries used; no tagging */
  330. s->used_entries=s->entries;
  331. for(i=0;i<s->entries;i++){
  332. long num=oggpack_read(opb,5);
  333. if(num==-1)goto _eofout;
  334. lengthlist[i]=num+1;
  335. if(num+1>s->dec_maxlength)s->dec_maxlength=num+1;
  336. }
  337. }
  338. break;
  339. case 1:
  340. /* ordered */
  341. {
  342. long length=oggpack_read(opb,5)+1;
  343. s->used_entries=s->entries;
  344. lengthlist=(char *)alloca(sizeof(*lengthlist)*s->entries);
  345. for(i=0;i<s->entries;){
  346. long num=oggpack_read(opb,_ilog(s->entries-i));
  347. if(num==-1)goto _eofout;
  348. for(j=0;j<num && i<s->entries;j++,i++)
  349. lengthlist[i]=length;
  350. s->dec_maxlength=length;
  351. length++;
  352. }
  353. }
  354. break;
  355. default:
  356. /* EOF */
  357. goto _eofout;
  358. }
  359. /* Do we have a mapping to unpack? */
  360. if((maptype=oggpack_read(opb,4))>0){
  361. s->q_min=_float32_unpack(oggpack_read(opb,32),&s->q_minp);
  362. s->q_del=_float32_unpack(oggpack_read(opb,32),&s->q_delp);
  363. s->q_bits=oggpack_read(opb,4)+1;
  364. s->q_seq=oggpack_read(opb,1);
  365. s->q_del>>=s->q_bits;
  366. s->q_delp+=s->q_bits;
  367. }
  368. switch(maptype){
  369. case 0:
  370. /* no mapping; decode type 0 */
  371. /* how many bytes for the indexing? */
  372. /* this is the correct boundary here; we lose one bit to
  373. node/leaf mark */
  374. s->dec_nodeb=_determine_node_bytes(s->used_entries,_ilog(s->entries)/8+1);
  375. s->dec_leafw=_determine_leaf_words(s->dec_nodeb,_ilog(s->entries)/8+1);
  376. s->dec_type=0;
  377. if(_make_decode_table(s,lengthlist,quantvals,opb,maptype)) goto _errout;
  378. break;
  379. case 1:
  380. /* mapping type 1; implicit values by lattice position */
  381. quantvals=_book_maptype1_quantvals(s);
  382. /* dec_type choices here are 1,2; 3 doesn't make sense */
  383. {
  384. /* packed values */
  385. long total1=(s->q_bits*s->dim+8)/8; /* remember flag bit */
  386. /* vector of column offsets; remember flag bit */
  387. long total2=(_ilog(quantvals-1)*s->dim+8)/8+(s->q_bits+7)/8;
  388. if(total1<=4 && total1<=total2){
  389. /* use dec_type 1: vector of packed values */
  390. /* need quantized values before */
  391. s->q_val=alloca(sizeof(ogg_uint16_t)*quantvals);
  392. for(i=0;i<quantvals;i++)
  393. ((ogg_uint16_t *)s->q_val)[i]=oggpack_read(opb,s->q_bits);
  394. if(oggpack_eop(opb)){
  395. s->q_val=0; /* cleanup must not free alloca memory */
  396. goto _eofout;
  397. }
  398. s->dec_type=1;
  399. s->dec_nodeb=_determine_node_bytes(s->used_entries,
  400. (s->q_bits*s->dim+8)/8);
  401. s->dec_leafw=_determine_leaf_words(s->dec_nodeb,
  402. (s->q_bits*s->dim+8)/8);
  403. if(_make_decode_table(s,lengthlist,quantvals,opb,maptype)){
  404. s->q_val=0; /* cleanup must not free alloca memory */
  405. goto _errout;
  406. }
  407. s->q_val=0; /* about to go out of scope; _make_decode_table
  408. was using it */
  409. }else{
  410. /* use dec_type 2: packed vector of column offsets */
  411. /* need quantized values before */
  412. if(s->q_bits<=8){
  413. s->q_val=_ogg_malloc(quantvals);
  414. for(i=0;i<quantvals;i++)
  415. ((unsigned char *)s->q_val)[i]=oggpack_read(opb,s->q_bits);
  416. }else{
  417. s->q_val=_ogg_malloc(quantvals*2);
  418. for(i=0;i<quantvals;i++)
  419. ((ogg_uint16_t *)s->q_val)[i]=oggpack_read(opb,s->q_bits);
  420. }
  421. if(oggpack_eop(opb))goto _eofout;
  422. s->q_pack=_ilog(quantvals-1);
  423. s->dec_type=2;
  424. s->dec_nodeb=_determine_node_bytes(s->used_entries,
  425. (_ilog(quantvals-1)*s->dim+8)/8);
  426. s->dec_leafw=_determine_leaf_words(s->dec_nodeb,
  427. (_ilog(quantvals-1)*s->dim+8)/8);
  428. if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
  429. }
  430. }
  431. break;
  432. case 2:
  433. /* mapping type 2; explicit array of values */
  434. quantvals=s->entries*s->dim;
  435. /* dec_type choices here are 1,3; 2 is not possible */
  436. if( (s->q_bits*s->dim+8)/8 <=4){ /* remember flag bit */
  437. /* use dec_type 1: vector of packed values */
  438. s->dec_type=1;
  439. s->dec_nodeb=_determine_node_bytes(s->used_entries,(s->q_bits*s->dim+8)/8);
  440. s->dec_leafw=_determine_leaf_words(s->dec_nodeb,(s->q_bits*s->dim+8)/8);
  441. if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
  442. }else{
  443. /* use dec_type 3: scalar offset into packed value array */
  444. s->dec_type=3;
  445. s->dec_nodeb=_determine_node_bytes(s->used_entries,_ilog(s->used_entries-1)/8+1);
  446. s->dec_leafw=_determine_leaf_words(s->dec_nodeb,_ilog(s->used_entries-1)/8+1);
  447. if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
  448. /* get the vals & pack them */
  449. s->q_pack=(s->q_bits+7)/8*s->dim;
  450. s->q_val=_ogg_malloc(s->q_pack*s->used_entries);
  451. if(s->q_bits<=8){
  452. for(i=0;i<s->used_entries*s->dim;i++)
  453. ((unsigned char *)(s->q_val))[i]=oggpack_read(opb,s->q_bits);
  454. }else{
  455. for(i=0;i<s->used_entries*s->dim;i++)
  456. ((ogg_uint16_t *)(s->q_val))[i]=oggpack_read(opb,s->q_bits);
  457. }
  458. }
  459. break;
  460. default:
  461. goto _errout;
  462. }
  463. if(oggpack_eop(opb))goto _eofout;
  464. return 0;
  465. _errout:
  466. _eofout:
  467. vorbis_book_clear(s);
  468. return -1;
  469. }
  470. static inline ogg_uint32_t decode_packed_entry_number(codebook *book,
  471. oggpack_buffer *b){
  472. ogg_uint32_t chase=0;
  473. int read=book->dec_maxlength;
  474. long lok = oggpack_look(b,read),i;
  475. while(lok<0 && read>1)
  476. lok = oggpack_look(b, --read);
  477. if(lok<0){
  478. oggpack_adv(b,1); /* force eop */
  479. return -1;
  480. }
  481. /* chase the tree with the bits we got */
  482. if(book->dec_nodeb==1){
  483. if(book->dec_leafw==1){
  484. /* 8/8 */
  485. unsigned char *t=(unsigned char *)book->dec_table;
  486. for(i=0;i<read;i++){
  487. chase=t[chase*2+((lok>>i)&1)];
  488. if(chase&0x80UL)break;
  489. }
  490. chase&=0x7fUL;
  491. }else{
  492. /* 8/16 */
  493. unsigned char *t=(unsigned char *)book->dec_table;
  494. for(i=0;i<read;i++){
  495. int bit=(lok>>i)&1;
  496. int next=t[chase+bit];
  497. if(next&0x80){
  498. chase= (next<<8) | t[chase+bit+1+(!bit || t[chase]&0x80)];
  499. break;
  500. }
  501. chase=next;
  502. }
  503. chase&=0x7fffUL;
  504. }
  505. }else{
  506. if(book->dec_nodeb==2){
  507. if(book->dec_leafw==1){
  508. /* 16/16 */
  509. for(i=0;i<read;i++){
  510. chase=((ogg_uint16_t *)(book->dec_table))[chase*2+((lok>>i)&1)];
  511. if(chase&0x8000UL)break;
  512. }
  513. chase&=0x7fffUL;
  514. }else{
  515. /* 16/32 */
  516. ogg_uint16_t *t=(ogg_uint16_t *)book->dec_table;
  517. for(i=0;i<read;i++){
  518. int bit=(lok>>i)&1;
  519. int next=t[chase+bit];
  520. if(next&0x8000){
  521. chase= (next<<16) | t[chase+bit+1+(!bit || t[chase]&0x8000)];
  522. break;
  523. }
  524. chase=next;
  525. }
  526. chase&=0x7fffffffUL;
  527. }
  528. }else{
  529. for(i=0;i<read;i++){
  530. chase=((ogg_uint32_t *)(book->dec_table))[chase*2+((lok>>i)&1)];
  531. if(chase&0x80000000UL)break;
  532. }
  533. chase&=0x7fffffffUL;
  534. }
  535. }
  536. if(i<read){
  537. oggpack_adv(b,i+1);
  538. return chase;
  539. }
  540. oggpack_adv(b,read+1);
  541. return(-1);
  542. }
  543. /* returns the [original, not compacted] entry number or -1 on eof *********/
  544. long vorbis_book_decode(codebook *book, oggpack_buffer *b){
  545. if(book->dec_type)return -1;
  546. return decode_packed_entry_number(book,b);
  547. }
  548. int decode_map(codebook *s, oggpack_buffer *b, ogg_int32_t *v, int point){
  549. ogg_uint32_t entry = decode_packed_entry_number(s,b);
  550. int i;
  551. if(oggpack_eop(b))return(-1);
  552. /* according to decode type */
  553. switch(s->dec_type){
  554. case 1:{
  555. /* packed vector of values */
  556. int mask=(1<<s->q_bits)-1;
  557. for(i=0;i<s->dim;i++){
  558. v[i]=entry&mask;
  559. entry>>=s->q_bits;
  560. }
  561. break;
  562. }
  563. case 2:{
  564. /* packed vector of column offsets */
  565. int mask=(1<<s->q_pack)-1;
  566. for(i=0;i<s->dim;i++){
  567. if(s->q_bits<=8)
  568. v[i]=((unsigned char *)(s->q_val))[entry&mask];
  569. else
  570. v[i]=((ogg_uint16_t *)(s->q_val))[entry&mask];
  571. entry>>=s->q_pack;
  572. }
  573. break;
  574. }
  575. case 3:{
  576. /* offset into array */
  577. void *ptr=(char*)s->q_val+entry*s->q_pack;
  578. if(s->q_bits<=8){
  579. for(i=0;i<s->dim;i++)
  580. v[i]=((unsigned char *)ptr)[i];
  581. }else{
  582. for(i=0;i<s->dim;i++)
  583. v[i]=((ogg_uint16_t *)ptr)[i];
  584. }
  585. break;
  586. }
  587. default:
  588. return -1;
  589. }
  590. /* we have the unpacked multiplicands; compute final vals */
  591. {
  592. int shiftM=point-s->q_delp;
  593. ogg_int32_t add=point-s->q_minp;
  594. if(add>0)
  595. add= s->q_min >> add;
  596. else
  597. add= s->q_min << -add;
  598. if(shiftM>0)
  599. for(i=0;i<s->dim;i++)
  600. v[i]= add + ((v[i] * s->q_del) >> shiftM);
  601. else
  602. for(i=0;i<s->dim;i++)
  603. v[i]= add + ((v[i] * s->q_del) << -shiftM);
  604. if(s->q_seq)
  605. for(i=1;i<s->dim;i++)
  606. v[i]+=v[i-1];
  607. }
  608. return 0;
  609. }
  610. /* returns 0 on OK or -1 on eof *************************************/
  611. /* decode vector / dim granularity guarding is done in the upper layer */
  612. long vorbis_book_decodevs_add(codebook *book,ogg_int32_t *a,
  613. oggpack_buffer *b,int n,int point){
  614. if(book->used_entries>0){
  615. int step=n/book->dim;
  616. ogg_int32_t *v = (ogg_int32_t *)alloca(sizeof(*v)*book->dim);
  617. int i,j,o;
  618. for (j=0;j<step;j++){
  619. if(decode_map(book,b,v,point))return -1;
  620. for(i=0,o=j;i<book->dim;i++,o+=step)
  621. a[o]+=v[i];
  622. }
  623. }
  624. return 0;
  625. }
  626. /* decode vector / dim granularity guarding is done in the upper layer */
  627. long vorbis_book_decodev_add(codebook *book,ogg_int32_t *a,
  628. oggpack_buffer *b,int n,int point){
  629. if(book->used_entries>0){
  630. ogg_int32_t *v = (ogg_int32_t *)alloca(sizeof(*v)*book->dim);
  631. int i,j;
  632. for(i=0;i<n;){
  633. if(decode_map(book,b,v,point))return -1;
  634. for (j=0;i<n && j<book->dim;j++)
  635. a[i++]+=v[j];
  636. }
  637. }
  638. return 0;
  639. }
  640. /* unlike the others, we guard against n not being an integer number
  641. * of <dim> internally rather than in the upper layer (called only by
  642. * floor0) */
  643. long vorbis_book_decodev_set(codebook *book,ogg_int32_t *a,
  644. oggpack_buffer *b,int n,int point){
  645. if(book->used_entries>0){
  646. ogg_int32_t *v = (ogg_int32_t *)alloca(sizeof(*v)*book->dim);
  647. int i,j;
  648. for(i=0;i<n;){
  649. if(decode_map(book,b,v,point))return -1;
  650. for (j=0;i<n && j<book->dim;j++)
  651. a[i++]=v[j];
  652. }
  653. }else{
  654. int i;
  655. for(i=0;i<n;){
  656. a[i++]=0;
  657. }
  658. }
  659. return 0;
  660. }
  661. /* decode vector / dim granularity guarding is done in the upper layer */
  662. long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,
  663. long offset,int ch,
  664. oggpack_buffer *b,int n,int point){
  665. if(book->used_entries>0){
  666. ogg_int32_t *v = (ogg_int32_t *)alloca(sizeof(*v)*book->dim);
  667. long i,j;
  668. int chptr=0;
  669. long m=offset+n;
  670. for(i=offset;i<m;){
  671. if(decode_map(book,b,v,point))return -1;
  672. for (j=0;i<m && j<book->dim;j++){
  673. a[chptr++][i]+=v[j];
  674. if(chptr==ch){
  675. chptr=0;
  676. i++;
  677. }
  678. }
  679. }
  680. }
  681. return 0;
  682. }