codebook.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/ogg.h"
  19. #include "ivorbiscodec.h"
  20. #include "codebook.h"
  21. #include "misc.h"
  22. /* unpacks a codebook from the packet buffer into the codebook struct,
  23. readies the codebook auxiliary structures for decode *************/
  24. static_codebook *vorbis_staticbook_unpack(oggpack_buffer *opb){
  25. long i,j;
  26. static_codebook *s=_ogg_calloc(1,sizeof(*s));
  27. /* make sure alignment is correct */
  28. if(oggpack_read(opb,24)!=0x564342)goto _eofout;
  29. /* first the basic parameters */
  30. s->dim=oggpack_read(opb,16);
  31. s->entries=oggpack_read(opb,24);
  32. if(s->entries==-1)goto _eofout;
  33. if(_ilog(s->dim)+_ilog(s->entries)>24)goto _eofout;
  34. /* codeword ordering.... length ordered or unordered? */
  35. switch((int)oggpack_read(opb,1)){
  36. case 0:{
  37. long unused;
  38. /* allocated but unused entries? */
  39. unused=oggpack_read(opb,1);
  40. if((s->entries*(unused?1:5)+7)>>3>opb->storage-oggpack_bytes(opb))
  41. goto _eofout;
  42. /* unordered */
  43. s->lengthlist=(long *)_ogg_malloc(sizeof(*s->lengthlist)*s->entries);
  44. /* allocated but unused entries? */
  45. if(unused){
  46. /* yes, unused entries */
  47. for(i=0;i<s->entries;i++){
  48. if(oggpack_read(opb,1)){
  49. long num=oggpack_read(opb,5);
  50. if(num==-1)goto _eofout;
  51. s->lengthlist[i]=num+1;
  52. }else
  53. s->lengthlist[i]=0;
  54. }
  55. }else{
  56. /* all entries used; no tagging */
  57. for(i=0;i<s->entries;i++){
  58. long num=oggpack_read(opb,5);
  59. if(num==-1)goto _eofout;
  60. s->lengthlist[i]=num+1;
  61. }
  62. }
  63. break;
  64. }
  65. case 1:
  66. /* ordered */
  67. {
  68. long length=oggpack_read(opb,5)+1;
  69. if(length==0)goto _eofout;
  70. s->lengthlist=(long *)_ogg_malloc(sizeof(*s->lengthlist)*s->entries);
  71. for(i=0;i<s->entries;){
  72. long num=oggpack_read(opb,_ilog(s->entries-i));
  73. if(num==-1)goto _eofout;
  74. if(length>32 || num>s->entries-i ||
  75. (num>0 && (num-1)>>(length>>1)>>((length+1)>>1))>0){
  76. goto _errout;
  77. }
  78. for(j=0;j<num;j++,i++)
  79. s->lengthlist[i]=length;
  80. length++;
  81. }
  82. }
  83. break;
  84. default:
  85. /* EOF */
  86. goto _eofout;
  87. }
  88. /* Do we have a mapping to unpack? */
  89. switch((s->maptype=oggpack_read(opb,4))){
  90. case 0:
  91. /* no mapping */
  92. break;
  93. case 1: case 2:
  94. /* implicitly populated value mapping */
  95. /* explicitly populated value mapping */
  96. s->q_min=oggpack_read(opb,32);
  97. s->q_delta=oggpack_read(opb,32);
  98. s->q_quant=oggpack_read(opb,4)+1;
  99. s->q_sequencep=oggpack_read(opb,1);
  100. if(s->q_sequencep==-1)goto _eofout;
  101. {
  102. int quantvals=0;
  103. switch(s->maptype){
  104. case 1:
  105. quantvals=(s->dim==0?0:_book_maptype1_quantvals(s));
  106. break;
  107. case 2:
  108. quantvals=s->entries*s->dim;
  109. break;
  110. }
  111. /* quantized values */
  112. if((quantvals*s->q_quant+7)>>3>opb->storage-oggpack_bytes(opb))
  113. goto _eofout;
  114. s->quantlist=(long *)_ogg_malloc(sizeof(*s->quantlist)*quantvals);
  115. for(i=0;i<quantvals;i++)
  116. s->quantlist[i]=oggpack_read(opb,s->q_quant);
  117. if(quantvals&&s->quantlist[quantvals-1]==-1)goto _eofout;
  118. }
  119. break;
  120. default:
  121. goto _errout;
  122. }
  123. /* all set */
  124. return(s);
  125. _errout:
  126. _eofout:
  127. vorbis_staticbook_destroy(s);
  128. return(NULL);
  129. }
  130. /* the 'eliminate the decode tree' optimization actually requires the
  131. codewords to be MSb first, not LSb. This is an annoying inelegancy
  132. (and one of the first places where carefully thought out design
  133. turned out to be wrong; Vorbis II and future Ogg codecs should go
  134. to an MSb bitpacker), but not actually the huge hit it appears to
  135. be. The first-stage decode table catches most words so that
  136. bitreverse is not in the main execution path. */
  137. static ogg_uint32_t bitreverse(ogg_uint32_t x){
  138. x= ((x>>16)&0x0000ffff) | ((x<<16)&0xffff0000);
  139. x= ((x>> 8)&0x00ff00ff) | ((x<< 8)&0xff00ff00);
  140. x= ((x>> 4)&0x0f0f0f0f) | ((x<< 4)&0xf0f0f0f0);
  141. x= ((x>> 2)&0x33333333) | ((x<< 2)&0xcccccccc);
  142. return((x>> 1)&0x55555555) | ((x<< 1)&0xaaaaaaaa);
  143. }
  144. STIN long decode_packed_entry_number(codebook *book,
  145. oggpack_buffer *b){
  146. int read=book->dec_maxlength;
  147. long lo,hi;
  148. long lok = oggpack_look(b,book->dec_firsttablen);
  149. if (lok >= 0) {
  150. long entry = book->dec_firsttable[lok];
  151. if(entry&0x80000000UL){
  152. lo=(entry>>15)&0x7fff;
  153. hi=book->used_entries-(entry&0x7fff);
  154. }else{
  155. oggpack_adv(b, book->dec_codelengths[entry-1]);
  156. return(entry-1);
  157. }
  158. }else{
  159. lo=0;
  160. hi=book->used_entries;
  161. }
  162. lok = oggpack_look(b, read);
  163. while(lok<0 && read>1)
  164. lok = oggpack_look(b, --read);
  165. if(lok<0){
  166. oggpack_adv(b,1); /* force eop */
  167. return -1;
  168. }
  169. /* bisect search for the codeword in the ordered list */
  170. {
  171. ogg_uint32_t testword=bitreverse((ogg_uint32_t)lok);
  172. while(hi-lo>1){
  173. long p=(hi-lo)>>1;
  174. long test=book->codelist[lo+p]>testword;
  175. lo+=p&(test-1);
  176. hi-=p&(-test);
  177. }
  178. if(book->dec_codelengths[lo]<=read){
  179. oggpack_adv(b, book->dec_codelengths[lo]);
  180. return(lo);
  181. }
  182. }
  183. oggpack_adv(b, read+1);
  184. return(-1);
  185. }
  186. /* Decode side is specced and easier, because we don't need to find
  187. matches using different criteria; we simply read and map. There are
  188. two things we need to do 'depending':
  189. We may need to support interleave. We don't really, but it's
  190. convenient to do it here rather than rebuild the vector later.
  191. Cascades may be additive or multiplicitive; this is not inherent in
  192. the codebook, but set in the code using the codebook. Like
  193. interleaving, it's easiest to do it here.
  194. addmul==0 -> declarative (set the value)
  195. addmul==1 -> additive
  196. addmul==2 -> multiplicitive */
  197. /* returns the [original, not compacted] entry number or -1 on eof *********/
  198. long vorbis_book_decode(codebook *book, oggpack_buffer *b){
  199. if(book->used_entries>0){
  200. long packed_entry=decode_packed_entry_number(book,b);
  201. if(packed_entry>=0)
  202. return(book->dec_index[packed_entry]);
  203. }
  204. /* if there's no dec_index, the codebook unpacking isn't collapsed */
  205. return(-1);
  206. }
  207. /* returns 0 on OK or -1 on eof *************************************/
  208. /* decode vector / dim granularity gaurding is done in the upper layer */
  209. long vorbis_book_decodevs_add(codebook *book,ogg_int32_t *a,
  210. oggpack_buffer *b,int n,int point){
  211. if(book->used_entries>0){
  212. int step=n/book->dim;
  213. long *entry = (long *)alloca(sizeof(*entry)*step);
  214. ogg_int32_t **t = (ogg_int32_t **)alloca(sizeof(*t)*step);
  215. int i,j,o;
  216. int shift=point-book->binarypoint;
  217. if(shift>=0){
  218. for (i = 0; i < step; i++) {
  219. entry[i]=decode_packed_entry_number(book,b);
  220. if(entry[i]==-1)return(-1);
  221. t[i] = book->valuelist+entry[i]*book->dim;
  222. }
  223. for(i=0,o=0;i<book->dim;i++,o+=step)
  224. for (j=0;j<step;j++)
  225. a[o+j]+=t[j][i]>>shift;
  226. }else{
  227. for (i = 0; i < step; i++) {
  228. entry[i]=decode_packed_entry_number(book,b);
  229. if(entry[i]==-1)return(-1);
  230. t[i] = book->valuelist+entry[i]*book->dim;
  231. }
  232. for(i=0,o=0;i<book->dim;i++,o+=step)
  233. for (j=0;j<step;j++)
  234. a[o+j]+=t[j][i]<<-shift;
  235. }
  236. }
  237. return(0);
  238. }
  239. /* decode vector / dim granularity gaurding is done in the upper layer */
  240. long vorbis_book_decodev_add(codebook *book,ogg_int32_t *a,
  241. oggpack_buffer *b,int n,int point){
  242. if(book->used_entries>0){
  243. int i,j,entry;
  244. ogg_int32_t *t;
  245. int shift=point-book->binarypoint;
  246. if(shift>=0){
  247. for(i=0;i<n;){
  248. entry = decode_packed_entry_number(book,b);
  249. if(entry==-1)return(-1);
  250. t = book->valuelist+entry*book->dim;
  251. for (j=0;j<book->dim;)
  252. a[i++]+=t[j++]>>shift;
  253. }
  254. }else{
  255. for(i=0;i<n;){
  256. entry = decode_packed_entry_number(book,b);
  257. if(entry==-1)return(-1);
  258. t = book->valuelist+entry*book->dim;
  259. for (j=0;j<book->dim;)
  260. a[i++]+=t[j++]<<-shift;
  261. }
  262. }
  263. }
  264. return(0);
  265. }
  266. /* unlike the others, we guard against n not being an integer number
  267. of <dim> internally rather than in the upper layer (called only by
  268. floor0) */
  269. long vorbis_book_decodev_set(codebook *book,ogg_int32_t *a,
  270. oggpack_buffer *b,int n,int point){
  271. if(book->used_entries>0){
  272. int i,j,entry;
  273. ogg_int32_t *t;
  274. int shift=point-book->binarypoint;
  275. if(shift>=0){
  276. for(i=0;i<n;){
  277. entry = decode_packed_entry_number(book,b);
  278. if(entry==-1)return(-1);
  279. t = book->valuelist+entry*book->dim;
  280. for (j=0;i<n && j<book->dim;){
  281. a[i++]=t[j++]>>shift;
  282. }
  283. }
  284. }else{
  285. for(i=0;i<n;){
  286. entry = decode_packed_entry_number(book,b);
  287. if(entry==-1)return(-1);
  288. t = book->valuelist+entry*book->dim;
  289. for (j=0;i<n && j<book->dim;){
  290. a[i++]=t[j++]<<-shift;
  291. }
  292. }
  293. }
  294. }else{
  295. int i,j;
  296. for(i=0;i<n;){
  297. a[i++]=0;
  298. }
  299. }
  300. return(0);
  301. }
  302. /* decode vector / dim granularity gaurding is done in the upper layer */
  303. long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,\
  304. long offset,int ch,
  305. oggpack_buffer *b,int n,int point){
  306. if(book->used_entries>0){
  307. long i,j,entry;
  308. int chptr=0;
  309. int shift=point-book->binarypoint;
  310. if(shift>=0){
  311. for(i=offset;i<offset+n;){
  312. entry = decode_packed_entry_number(book,b);
  313. if(entry==-1)return(-1);
  314. {
  315. const ogg_int32_t *t = book->valuelist+entry*book->dim;
  316. for (j=0;j<book->dim;j++){
  317. a[chptr++][i]+=t[j]>>shift;
  318. if(chptr==ch){
  319. chptr=0;
  320. i++;
  321. }
  322. }
  323. }
  324. }
  325. }else{
  326. for(i=offset;i<offset+n;){
  327. entry = decode_packed_entry_number(book,b);
  328. if(entry==-1)return(-1);
  329. {
  330. const ogg_int32_t *t = book->valuelist+entry*book->dim;
  331. for (j=0;j<book->dim;j++){
  332. a[chptr++][i]+=t[j]<<-shift;
  333. if(chptr==ch){
  334. chptr=0;
  335. i++;
  336. }
  337. }
  338. }
  339. }
  340. }
  341. }
  342. return(0);
  343. }