codebook.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /************************************************************************
  2. * Copyright (C) 2002-2009, Xiph.org Foundation
  3. * Copyright (C) 2010, Robin Watts for Pinknoise Productions Ltd
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the names of the Xiph.org Foundation nor Pinknoise
  17. * Productions Ltd nor the names of its contributors may be used to
  18. * endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ************************************************************************
  33. function: basic codebook pack/unpack/code/decode operations
  34. ************************************************************************/
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <math.h>
  38. #include <limits.h>
  39. // #include <log/log.h>
  40. #include "ogg.h"
  41. #include "ivorbiscodec.h"
  42. #include "codebook.h"
  43. #include "misc.h"
  44. #include "os.h"
  45. #define MARKER_SIZE 33
  46. /**** pack/unpack helpers ******************************************/
  47. int _ilog(unsigned int v){
  48. int ret=0;
  49. while(v){
  50. ret++;
  51. v>>=1;
  52. }
  53. return(ret);
  54. }
  55. static ogg_uint32_t decpack(long entry,long used_entry,long quantvals,
  56. codebook *b,oggpack_buffer *opb,int maptype){
  57. ogg_uint32_t ret=0;
  58. int j;
  59. switch(b->dec_type){
  60. case 0:
  61. return (ogg_uint32_t)entry;
  62. case 1:
  63. if(maptype==1){
  64. /* vals are already read into temporary column vector here */
  65. for(j=0;j<b->dim;j++){
  66. ogg_uint32_t off=entry%quantvals;
  67. entry/=quantvals;
  68. ret|=((ogg_uint16_t *)(b->q_val))[off]<<(b->q_bits*j);
  69. }
  70. }else{
  71. for(j=0;j<b->dim;j++)
  72. ret|=oggpack_read(opb,b->q_bits)<<(b->q_bits*j);
  73. }
  74. return ret;
  75. case 2:
  76. for(j=0;j<b->dim;j++){
  77. ogg_uint32_t off=entry%quantvals;
  78. entry/=quantvals;
  79. ret|=off<<(b->q_pack*j);
  80. }
  81. return ret;
  82. case 3:
  83. return (ogg_uint32_t)used_entry;
  84. }
  85. return 0; /* silence compiler */
  86. }
  87. /* 32 bit float (not IEEE; nonnormalized mantissa +
  88. biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm
  89. Why not IEEE? It's just not that important here. */
  90. static ogg_int32_t _float32_unpack(long val,int *point){
  91. long mant=val&0x1fffff;
  92. int sign=val&0x80000000;
  93. *point=((val&0x7fe00000L)>>21)-788;
  94. if(mant){
  95. while(!(mant&0x40000000)){
  96. mant<<=1;
  97. *point-=1;
  98. }
  99. if(sign)mant= -mant;
  100. }else{
  101. *point=-9999;
  102. }
  103. return mant;
  104. }
  105. /* choose the smallest supported node size that fits our decode table.
  106. Legal bytewidths are 1/1 1/2 2/2 2/4 4/4 */
  107. static int _determine_node_bytes(long used, int leafwidth){
  108. /* special case small books to size 4 to avoid multiple special
  109. cases in repack */
  110. if(used<2)
  111. return 4;
  112. if(leafwidth==3)leafwidth=4;
  113. if(_ilog(3*used-6)+1 <= leafwidth*4)
  114. return leafwidth/2?leafwidth/2:1;
  115. return leafwidth;
  116. }
  117. /* convenience/clarity; leaves are specified as multiple of node word
  118. size (1 or 2) */
  119. static int _determine_leaf_words(int nodeb, int leafwidth){
  120. if(leafwidth>nodeb)return 2;
  121. return 1;
  122. }
  123. /* given a list of word lengths, number of used entries, and byte
  124. width of a leaf, generate the decode table */
  125. static int _make_words(char *l,long n,ogg_uint32_t *r,long quantvals,
  126. codebook *b, oggpack_buffer *opb,int maptype){
  127. long i,j,count=0;
  128. long top=0;
  129. ogg_uint32_t marker[MARKER_SIZE];
  130. if (n<1)
  131. return 1;
  132. if(n<2){
  133. r[0]=0x80000000;
  134. }else{
  135. memset(marker,0,sizeof(marker));
  136. for(i=0;i<n;i++){
  137. long length=l[i];
  138. if(length){
  139. if (length < 0 || length >= MARKER_SIZE) {
  140. //cjh ALOGE("b/23881715");
  141. return 1;
  142. }
  143. ogg_uint32_t entry=marker[length];
  144. long chase=0;
  145. if(count && !entry)return -1; /* overpopulated tree! */
  146. /* chase the tree as far as it's already populated, fill in past */
  147. for(j=0;j<length-1;j++){
  148. int bit=(entry>>(length-j-1))&1;
  149. if(chase>=top){
  150. if (chase < 0 || chase >= n) return 1;
  151. top++;
  152. r[chase*2]=top;
  153. r[chase*2+1]=0;
  154. }else
  155. if (chase < 0 || chase >= n || chase*2+bit > n*2+1) return 1;
  156. if(!r[chase*2+bit])
  157. r[chase*2+bit]=top;
  158. chase=r[chase*2+bit];
  159. if (chase < 0 || chase >= n) return 1;
  160. }
  161. {
  162. int bit=(entry>>(length-j-1))&1;
  163. if(chase>=top){
  164. top++;
  165. r[chase*2+1]=0;
  166. }
  167. r[chase*2+bit]= decpack(i,count++,quantvals,b,opb,maptype) |
  168. 0x80000000;
  169. }
  170. /* Look to see if the next shorter marker points to the node
  171. above. if so, update it and repeat. */
  172. for(j=length;j>0;j--){
  173. if(marker[j]&1){
  174. marker[j]=marker[j-1]<<1;
  175. break;
  176. }
  177. marker[j]++;
  178. }
  179. /* prune the tree; the implicit invariant says all the longer
  180. markers were dangling from our just-taken node. Dangle them
  181. from our *new* node. */
  182. for(j=length+1;j<MARKER_SIZE;j++)
  183. if((marker[j]>>1) == entry){
  184. entry=marker[j];
  185. marker[j]=marker[j-1]<<1;
  186. }else
  187. break;
  188. }
  189. }
  190. }
  191. // following sanity check copied from libvorbis
  192. /* sanity check the huffman tree; an underpopulated tree must be
  193. rejected. The only exception is the one-node pseudo-nil tree,
  194. which appears to be underpopulated because the tree doesn't
  195. really exist; there's only one possible 'codeword' or zero bits,
  196. but the above tree-gen code doesn't mark that. */
  197. if(b->used_entries != 1){
  198. for(i=1;i<MARKER_SIZE;i++)
  199. if(marker[i] & (0xffffffffUL>>(32-i))){
  200. return 1;
  201. }
  202. }
  203. return 0;
  204. }
  205. static int _make_decode_table(codebook *s,char *lengthlist,long quantvals,
  206. oggpack_buffer *opb,int maptype){
  207. int i;
  208. ogg_uint32_t *work;
  209. if (!lengthlist) return 1;
  210. if(s->dec_nodeb==4){
  211. /* Over-allocate by using s->entries instead of used_entries.
  212. * This means that we can use s->entries to enforce size in
  213. * _make_words without messing up length list looping.
  214. * This probably wastes a bit of space, but it shouldn't
  215. * impact behavior or size too much.
  216. */
  217. s->dec_table=_ogg_malloc((s->entries*2+1)*sizeof(*work));
  218. if (!s->dec_table) return 1;
  219. /* +1 (rather than -2) is to accommodate 0 and 1 sized books,
  220. which are specialcased to nodeb==4 */
  221. if(_make_words(lengthlist,s->entries,
  222. s->dec_table,quantvals,s,opb,maptype))return 1;
  223. return 0;
  224. }
  225. if (s->used_entries > INT_MAX/2 ||
  226. s->used_entries*2 > INT_MAX/((long) sizeof(*work)) - 1) return 1;
  227. /* Overallocate as above */
  228. work=calloc((s->entries*2+1),sizeof(*work));
  229. if (!work) return 1;
  230. if(_make_words(lengthlist,s->entries,work,quantvals,s,opb,maptype)) goto error_out;
  231. if (s->used_entries > INT_MAX/(s->dec_leafw+1)) goto error_out;
  232. if (s->dec_nodeb && s->used_entries * (s->dec_leafw+1) > INT_MAX/s->dec_nodeb) goto error_out;
  233. s->dec_table=_ogg_malloc((s->used_entries*(s->dec_leafw+1)-2)*
  234. s->dec_nodeb);
  235. if (!s->dec_table) goto error_out;
  236. if(s->dec_leafw==1){
  237. switch(s->dec_nodeb){
  238. case 1:
  239. for(i=0;i<s->used_entries*2-2;i++)
  240. ((unsigned char *)s->dec_table)[i]=(unsigned char)
  241. (((work[i] & 0x80000000UL) >> 24) | work[i]);
  242. break;
  243. case 2:
  244. for(i=0;i<s->used_entries*2-2;i++)
  245. ((ogg_uint16_t *)s->dec_table)[i]=(ogg_uint16_t)
  246. (((work[i] & 0x80000000UL) >> 16) | work[i]);
  247. break;
  248. }
  249. }else{
  250. /* more complex; we have to do a two-pass repack that updates the
  251. node indexing. */
  252. long top=s->used_entries*3-2;
  253. if(s->dec_nodeb==1){
  254. unsigned char *out=(unsigned char *)s->dec_table;
  255. for(i=s->used_entries*2-4;i>=0;i-=2){
  256. if(work[i]&0x80000000UL){
  257. if(work[i+1]&0x80000000UL){
  258. top-=4;
  259. out[top]=(work[i]>>8 & 0x7f)|0x80;
  260. out[top+1]=(work[i+1]>>8 & 0x7f)|0x80;
  261. out[top+2]=work[i] & 0xff;
  262. out[top+3]=work[i+1] & 0xff;
  263. }else{
  264. top-=3;
  265. out[top]=(work[i]>>8 & 0x7f)|0x80;
  266. out[top+1]=work[work[i+1]*2];
  267. out[top+2]=work[i] & 0xff;
  268. }
  269. }else{
  270. if(work[i+1]&0x80000000UL){
  271. top-=3;
  272. out[top]=work[work[i]*2];
  273. out[top+1]=(work[i+1]>>8 & 0x7f)|0x80;
  274. out[top+2]=work[i+1] & 0xff;
  275. }else{
  276. top-=2;
  277. out[top]=work[work[i]*2];
  278. out[top+1]=work[work[i+1]*2];
  279. }
  280. }
  281. work[i]=top;
  282. }
  283. }else{
  284. ogg_uint16_t *out=(ogg_uint16_t *)s->dec_table;
  285. for(i=s->used_entries*2-4;i>=0;i-=2){
  286. if(work[i]&0x80000000UL){
  287. if(work[i+1]&0x80000000UL){
  288. top-=4;
  289. out[top]=(work[i]>>16 & 0x7fff)|0x8000;
  290. out[top+1]=(work[i+1]>>16 & 0x7fff)|0x8000;
  291. out[top+2]=work[i] & 0xffff;
  292. out[top+3]=work[i+1] & 0xffff;
  293. }else{
  294. top-=3;
  295. out[top]=(work[i]>>16 & 0x7fff)|0x8000;
  296. out[top+1]=work[work[i+1]*2];
  297. out[top+2]=work[i] & 0xffff;
  298. }
  299. }else{
  300. if(work[i+1]&0x80000000UL){
  301. top-=3;
  302. out[top]=work[work[i]*2];
  303. out[top+1]=(work[i+1]>>16 & 0x7fff)|0x8000;
  304. out[top+2]=work[i+1] & 0xffff;
  305. }else{
  306. top-=2;
  307. out[top]=work[work[i]*2];
  308. out[top+1]=work[work[i+1]*2];
  309. }
  310. }
  311. work[i]=top;
  312. }
  313. }
  314. }
  315. free(work);
  316. return 0;
  317. error_out:
  318. free(work);
  319. return 1;
  320. }
  321. /* most of the time, entries%dimensions == 0, but we need to be
  322. well defined. We define that the possible vales at each
  323. scalar is values == entries/dim. If entries%dim != 0, we'll
  324. have 'too few' values (values*dim<entries), which means that
  325. we'll have 'left over' entries; left over entries use zeroed
  326. values (and are wasted). So don't generate codebooks like
  327. that */
  328. /* there might be a straightforward one-line way to do the below
  329. that's portable and totally safe against roundoff, but I haven't
  330. thought of it. Therefore, we opt on the side of caution */
  331. long _book_maptype1_quantvals(codebook *b){
  332. /* get us a starting hint, we'll polish it below */
  333. int bits=_ilog(b->entries);
  334. int vals=b->entries>>((bits-1)*(b->dim-1)/b->dim);
  335. while(1){
  336. long acc=1;
  337. long acc1=1;
  338. int i;
  339. for(i=0;i<b->dim;i++){
  340. acc*=vals;
  341. acc1*=vals+1;
  342. }
  343. if(acc<=b->entries && acc1>b->entries){
  344. return(vals);
  345. }else{
  346. if(acc>b->entries){
  347. vals--;
  348. }else{
  349. vals++;
  350. }
  351. }
  352. }
  353. }
  354. void vorbis_book_clear(codebook *b){
  355. /* static book is not cleared; we're likely called on the lookup and
  356. the static codebook belongs to the info struct */
  357. if(b->q_val)_ogg_free(b->q_val);
  358. if(b->dec_table)_ogg_free(b->dec_table);
  359. if(b->dec_buf)_ogg_free(b->dec_buf);
  360. memset(b,0,sizeof(*b));
  361. }
  362. int vorbis_book_unpack(oggpack_buffer *opb,codebook *s){
  363. char *lengthlist=NULL;
  364. int quantvals=0;
  365. long i,j;
  366. int maptype;
  367. memset(s,0,sizeof(*s));
  368. /* make sure alignment is correct */
  369. if(oggpack_read(opb,24)!=0x564342)goto _eofout;
  370. /* first the basic parameters */
  371. s->dim=oggpack_read(opb,16);
  372. s->dec_buf=_ogg_malloc(sizeof(ogg_int32_t)*s->dim);
  373. if (s->dec_buf == NULL)
  374. goto _errout;
  375. s->entries=oggpack_read(opb,24);
  376. if(s->entries<=0)goto _eofout;
  377. if(s->dim<=0)goto _eofout;
  378. if(_ilog(s->dim)+_ilog(s->entries)>24)goto _eofout;
  379. if (s->dim > INT_MAX/s->entries) goto _eofout;
  380. /* codeword ordering.... length ordered or unordered? */
  381. switch((int)oggpack_read(opb,1)){
  382. case 0:
  383. /* unordered */
  384. lengthlist=(char *)calloc(s->entries, sizeof(*lengthlist));
  385. if(!lengthlist) goto _eofout;
  386. /* allocated but unused entries? */
  387. if(oggpack_read(opb,1)){
  388. /* yes, unused entries */
  389. for(i=0;i<s->entries;i++){
  390. if(oggpack_read(opb,1)){
  391. long num=oggpack_read(opb,5);
  392. if(num==-1)goto _eofout;
  393. lengthlist[i]=(char)(num+1);
  394. s->used_entries++;
  395. if(num+1>s->dec_maxlength)s->dec_maxlength=num+1;
  396. }else
  397. lengthlist[i]=0;
  398. }
  399. }else{
  400. /* all entries used; no tagging */
  401. s->used_entries=s->entries;
  402. for(i=0;i<s->entries;i++){
  403. long num=oggpack_read(opb,5);
  404. if(num==-1)goto _eofout;
  405. lengthlist[i]=(char)(num+1);
  406. if(num+1>s->dec_maxlength)s->dec_maxlength=num+1;
  407. }
  408. }
  409. break;
  410. case 1:
  411. /* ordered */
  412. {
  413. long length=oggpack_read(opb,5)+1;
  414. s->used_entries=s->entries;
  415. lengthlist=(char *)calloc(s->entries, sizeof(*lengthlist));
  416. if (!lengthlist) goto _eofout;
  417. for(i=0;i<s->entries;){
  418. long num=oggpack_read(opb,_ilog(s->entries-i));
  419. if(num<0)goto _eofout;
  420. for(j=0;j<num && i<s->entries;j++,i++)
  421. lengthlist[i]=(char)length;
  422. s->dec_maxlength=length;
  423. length++;
  424. }
  425. }
  426. break;
  427. default:
  428. /* EOF */
  429. goto _eofout;
  430. }
  431. /* Do we have a mapping to unpack? */
  432. if((maptype=oggpack_read(opb,4))>0){
  433. s->q_min=_float32_unpack(oggpack_read(opb,32),&s->q_minp);
  434. s->q_del=_float32_unpack(oggpack_read(opb,32),&s->q_delp);
  435. s->q_bits=oggpack_read(opb,4)+1;
  436. s->q_seq=oggpack_read(opb,1);
  437. s->q_del>>=s->q_bits;
  438. s->q_delp+=s->q_bits;
  439. }
  440. switch(maptype){
  441. case 0:
  442. /* no mapping; decode type 0 */
  443. /* how many bytes for the indexing? */
  444. /* this is the correct boundary here; we lose one bit to
  445. node/leaf mark */
  446. s->dec_nodeb=_determine_node_bytes(s->used_entries,_ilog(s->entries)/8+1);
  447. s->dec_leafw=_determine_leaf_words(s->dec_nodeb,_ilog(s->entries)/8+1);
  448. s->dec_type=0;
  449. if(_make_decode_table(s,lengthlist,quantvals,opb,maptype)) goto _errout;
  450. break;
  451. case 1:
  452. /* mapping type 1; implicit values by lattice position */
  453. quantvals=_book_maptype1_quantvals(s);
  454. /* dec_type choices here are 1,2; 3 doesn't make sense */
  455. {
  456. /* packed values */
  457. long total1=(s->q_bits*s->dim+8)/8; /* remember flag bit */
  458. if (s->dim > (INT_MAX-8)/s->q_bits) goto _eofout;
  459. /* vector of column offsets; remember flag bit */
  460. long total2=(_ilog(quantvals-1)*s->dim+8)/8+(s->q_bits+7)/8;
  461. if(total1<=4 && total1<=total2){
  462. /* use dec_type 1: vector of packed values */
  463. /* need quantized values before */
  464. s->q_val=calloc(sizeof(ogg_uint16_t), quantvals);
  465. if (!s->q_val) goto _eofout;
  466. for(i=0;i<quantvals;i++)
  467. ((ogg_uint16_t *)s->q_val)[i]=(ogg_uint16_t)oggpack_read(opb,s->q_bits);
  468. if(oggpack_eop(opb)){
  469. goto _eofout;
  470. }
  471. s->dec_type=1;
  472. s->dec_nodeb=_determine_node_bytes(s->used_entries,
  473. (s->q_bits*s->dim+8)/8);
  474. s->dec_leafw=_determine_leaf_words(s->dec_nodeb,
  475. (s->q_bits*s->dim+8)/8);
  476. if(_make_decode_table(s,lengthlist,quantvals,opb,maptype)){
  477. goto _errout;
  478. }
  479. free(s->q_val);
  480. s->q_val=0;
  481. }else{
  482. /* use dec_type 2: packed vector of column offsets */
  483. /* need quantized values before */
  484. if(s->q_bits<=8){
  485. s->q_val=_ogg_malloc(quantvals);
  486. if (!s->q_val) goto _eofout;
  487. for(i=0;i<quantvals;i++)
  488. ((unsigned char *)s->q_val)[i]=(unsigned char)oggpack_read(opb,s->q_bits);
  489. }else{
  490. s->q_val=_ogg_malloc(quantvals*2);
  491. if (!s->q_val) goto _eofout;
  492. for(i=0;i<quantvals;i++)
  493. ((ogg_uint16_t *)s->q_val)[i]=(ogg_uint16_t)oggpack_read(opb,s->q_bits);
  494. }
  495. if(oggpack_eop(opb))goto _eofout;
  496. s->q_pack=_ilog(quantvals-1);
  497. s->dec_type=2;
  498. s->dec_nodeb=_determine_node_bytes(s->used_entries,
  499. (_ilog(quantvals-1)*s->dim+8)/8);
  500. s->dec_leafw=_determine_leaf_words(s->dec_nodeb,
  501. (_ilog(quantvals-1)*s->dim+8)/8);
  502. if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
  503. }
  504. }
  505. break;
  506. case 2:
  507. /* mapping type 2; explicit array of values */
  508. quantvals=s->entries*s->dim;
  509. /* dec_type choices here are 1,3; 2 is not possible */
  510. if( (s->q_bits*s->dim+8)/8 <=4){ /* remember flag bit */
  511. /* use dec_type 1: vector of packed values */
  512. s->dec_type=1;
  513. s->dec_nodeb=_determine_node_bytes(s->used_entries,(s->q_bits*s->dim+8)/8);
  514. s->dec_leafw=_determine_leaf_words(s->dec_nodeb,(s->q_bits*s->dim+8)/8);
  515. if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
  516. }else{
  517. /* use dec_type 3: scalar offset into packed value array */
  518. s->dec_type=3;
  519. s->dec_nodeb=_determine_node_bytes(s->used_entries,_ilog(s->used_entries-1)/8+1);
  520. s->dec_leafw=_determine_leaf_words(s->dec_nodeb,_ilog(s->used_entries-1)/8+1);
  521. if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
  522. /* get the vals & pack them */
  523. s->q_pack=(s->q_bits+7)/8*s->dim;
  524. s->q_val=_ogg_malloc(s->q_pack*s->used_entries);
  525. if(s->q_bits<=8){
  526. for(i=0;i<s->used_entries*s->dim;i++)
  527. ((unsigned char *)(s->q_val))[i]=(unsigned char)oggpack_read(opb,s->q_bits);
  528. }else{
  529. for(i=0;i<s->used_entries*s->dim;i++)
  530. ((ogg_uint16_t *)(s->q_val))[i]=(ogg_uint16_t)oggpack_read(opb,s->q_bits);
  531. }
  532. }
  533. break;
  534. default:
  535. goto _errout;
  536. }
  537. if (s->dec_nodeb==1)
  538. if (s->dec_leafw == 1)
  539. s->dec_method = 0;
  540. else
  541. s->dec_method = 1;
  542. else if (s->dec_nodeb==2)
  543. if (s->dec_leafw == 1)
  544. s->dec_method = 2;
  545. else
  546. s->dec_method = 3;
  547. else
  548. s->dec_method = 4;
  549. if(oggpack_eop(opb))goto _eofout;
  550. free(lengthlist);
  551. return 0;
  552. _errout:
  553. _eofout:
  554. vorbis_book_clear(s);
  555. free(lengthlist);
  556. free(s->q_val);
  557. return -1;
  558. }
  559. #ifndef ONLY_C
  560. ogg_uint32_t decode_packed_entry_number(codebook *book,
  561. oggpack_buffer *b);
  562. #else
  563. static inline ogg_uint32_t decode_packed_entry_number(codebook *book,
  564. oggpack_buffer *b){
  565. ogg_uint32_t chase=0;
  566. int read=book->dec_maxlength;
  567. long lok = oggpack_look(b,read),i;
  568. while(lok<0 && read>1)
  569. lok = oggpack_look(b, --read);
  570. if(lok<0){
  571. oggpack_adv(b,1); /* force eop */
  572. return -1;
  573. }
  574. /* chase the tree with the bits we got */
  575. switch (book->dec_method)
  576. {
  577. case 0:
  578. {
  579. /* book->dec_nodeb==1, book->dec_leafw==1 */
  580. /* 8/8 - Used */
  581. unsigned char *t=(unsigned char *)book->dec_table;
  582. for(i=0;i<read;i++){
  583. chase=t[chase*2+((lok>>i)&1)];
  584. if(chase&0x80UL)break;
  585. }
  586. chase&=0x7fUL;
  587. break;
  588. }
  589. case 1:
  590. {
  591. /* book->dec_nodeb==1, book->dec_leafw!=1 */
  592. /* 8/16 - Used by infile2 */
  593. unsigned char *t=(unsigned char *)book->dec_table;
  594. for(i=0;i<read;i++){
  595. int bit=(lok>>i)&1;
  596. int next=t[chase+bit];
  597. if(next&0x80){
  598. chase= (next<<8) | t[chase+bit+1+(!bit || t[chase]&0x80)];
  599. break;
  600. }
  601. chase=next;
  602. }
  603. //chase&=0x7fffUL;
  604. chase&=~0x8000UL;
  605. break;
  606. }
  607. case 2:
  608. {
  609. /* book->dec_nodeb==2, book->dec_leafw==1 */
  610. /* 16/16 - Used */
  611. for(i=0;i<read;i++){
  612. chase=((ogg_uint16_t *)(book->dec_table))[chase*2+((lok>>i)&1)];
  613. if(chase&0x8000UL)break;
  614. }
  615. //chase&=0x7fffUL;
  616. chase&=~0x8000UL;
  617. break;
  618. }
  619. case 3:
  620. {
  621. /* book->dec_nodeb==2, book->dec_leafw!=1 */
  622. /* 16/32 - Used by infile2 */
  623. ogg_uint16_t *t=(ogg_uint16_t *)book->dec_table;
  624. for(i=0;i<read;i++){
  625. int bit=(lok>>i)&1;
  626. int next=t[chase+bit];
  627. if(next&0x8000){
  628. chase= (next<<16) | t[chase+bit+1+(!bit || t[chase]&0x8000)];
  629. break;
  630. }
  631. chase=next;
  632. }
  633. //chase&=0x7fffffffUL;
  634. chase&=~0x80000000UL;
  635. break;
  636. }
  637. case 4:
  638. {
  639. //Output("32/32");
  640. for(i=0;i<read;i++){
  641. chase=((ogg_uint32_t *)(book->dec_table))[chase*2+((lok>>i)&1)];
  642. if(chase&0x80000000UL)break;
  643. }
  644. //chase&=0x7fffffffUL;
  645. chase&=~0x80000000UL;
  646. break;
  647. }
  648. }
  649. if(i<read){
  650. oggpack_adv(b,i+1);
  651. return chase;
  652. }
  653. oggpack_adv(b,read+1);
  654. return(-1);
  655. }
  656. #endif
  657. /* returns the [original, not compacted] entry number or -1 on eof *********/
  658. long vorbis_book_decode(codebook *book, oggpack_buffer *b){
  659. if(book->dec_type)return -1;
  660. return decode_packed_entry_number(book,b);
  661. }
  662. #ifndef ONLY_C
  663. int decode_map(codebook *s, oggpack_buffer *b, ogg_int32_t *v, int point);
  664. #else
  665. static int decode_map(codebook *s, oggpack_buffer *b, ogg_int32_t *v, int point){
  666. ogg_uint32_t entry = decode_packed_entry_number(s,b);
  667. int i;
  668. if(oggpack_eop(b))return(-1);
  669. /* 1 used by test file 0 */
  670. /* according to decode type */
  671. switch(s->dec_type){
  672. case 1:{
  673. /* packed vector of values */
  674. int mask=(1<<s->q_bits)-1;
  675. for(i=0;i<s->dim;i++){
  676. v[i]=entry&mask;
  677. entry>>=s->q_bits;
  678. }
  679. break;
  680. }
  681. case 2:{
  682. /* packed vector of column offsets */
  683. int mask=(1<<s->q_pack)-1;
  684. for(i=0;i<s->dim;i++){
  685. if(s->q_bits<=8)
  686. v[i]=((unsigned char *)(s->q_val))[entry&mask];
  687. else
  688. v[i]=((ogg_uint16_t *)(s->q_val))[entry&mask];
  689. entry>>=s->q_pack;
  690. }
  691. break;
  692. }
  693. case 3:{
  694. /* offset into array */
  695. void *ptr=((char *)s->q_val)+entry*s->q_pack;
  696. if(s->q_bits<=8){
  697. for(i=0;i<s->dim;i++)
  698. v[i]=((unsigned char *)ptr)[i];
  699. }else{
  700. for(i=0;i<s->dim;i++)
  701. v[i]=((ogg_uint16_t *)ptr)[i];
  702. }
  703. break;
  704. }
  705. default:
  706. return -1;
  707. }
  708. /* we have the unpacked multiplicands; compute final vals */
  709. {
  710. int shiftM = point-s->q_delp;
  711. ogg_int32_t add = point-s->q_minp;
  712. int mul = s->q_del;
  713. if(add>0)
  714. add= s->q_min >> add;
  715. else
  716. add= s->q_min << -add;
  717. if (shiftM<0)
  718. {
  719. mul <<= -shiftM;
  720. shiftM = 0;
  721. }
  722. add <<= shiftM;
  723. for(i=0;i<s->dim;i++)
  724. v[i]= ((add + v[i] * mul) >> shiftM);
  725. if(s->q_seq)
  726. for(i=1;i<s->dim;i++)
  727. v[i]+=v[i-1];
  728. }
  729. return 0;
  730. }
  731. #endif
  732. /* returns 0 on OK or -1 on eof *************************************/
  733. long vorbis_book_decodevs_add(codebook *book,ogg_int32_t *a,
  734. oggpack_buffer *b,int n,int point){
  735. if(book->used_entries>0){
  736. int step=n/book->dim;
  737. ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
  738. int i,j,o;
  739. if (!v) return -1;
  740. for (j=0;j<step;j++){
  741. if(decode_map(book,b,v,point))return -1;
  742. for(i=0,o=j;i<book->dim;i++,o+=step)
  743. a[o]+=v[i];
  744. }
  745. }
  746. return 0;
  747. }
  748. long vorbis_book_decodev_add(codebook *book,ogg_int32_t *a,
  749. oggpack_buffer *b,int n,int point){
  750. if(book->used_entries>0){
  751. ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
  752. int i,j;
  753. if (!v) return -1;
  754. for(i=0;i<n;){
  755. if(decode_map(book,b,v,point))return -1;
  756. for (j=0;j<book->dim;j++)
  757. a[i++]+=v[j];
  758. }
  759. }
  760. return 0;
  761. }
  762. long vorbis_book_decodev_set(codebook *book,ogg_int32_t *a,
  763. oggpack_buffer *b,int n,int point){
  764. if(book->used_entries>0){
  765. ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
  766. int i,j;
  767. if (!v) return -1;
  768. for(i=0;i<n;){
  769. if(decode_map(book,b,v,point))return -1;
  770. for (j=0;j<book->dim;j++)
  771. a[i++]=v[j];
  772. }
  773. }else{
  774. int i,j;
  775. for(i=0;i<n;){
  776. for (j=0;j<book->dim;j++)
  777. a[i++]=0;
  778. }
  779. }
  780. return 0;
  781. }
  782. #ifndef ONLY_C
  783. long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,
  784. long offset,int ch,
  785. oggpack_buffer *b,int n,int point);
  786. #else
  787. long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,
  788. long offset,int ch,
  789. oggpack_buffer *b,int n,int point){
  790. if(book->used_entries>0){
  791. ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
  792. long i,j;
  793. int chptr=0;
  794. if (!v) return -1;
  795. for(i=offset;i<offset+n;){
  796. if(decode_map(book,b,v,point))return -1;
  797. for (j=0;j<book->dim;j++){
  798. a[chptr++][i]+=v[j];
  799. if(chptr==ch){
  800. chptr=0;
  801. i++;
  802. }
  803. }
  804. }
  805. }
  806. return 0;
  807. }
  808. #endif