codebook.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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-2015 *
  9. * by the Xiph.Org Foundation https://xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: basic codebook pack/unpack/code/decode operations
  13. ********************************************************************/
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <math.h>
  17. #include <ogg/ogg.h>
  18. #include "vorbis/codec.h"
  19. #include "codebook.h"
  20. #include "scales.h"
  21. #include "misc.h"
  22. #include "os.h"
  23. /* packs the given codebook into the bitstream **************************/
  24. int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){
  25. long i,j;
  26. int ordered=0;
  27. /* first the basic parameters */
  28. oggpack_write(opb,0x564342,24);
  29. oggpack_write(opb,c->dim,16);
  30. oggpack_write(opb,c->entries,24);
  31. /* pack the codewords. There are two packings; length ordered and
  32. length random. Decide between the two now. */
  33. for(i=1;i<c->entries;i++)
  34. if(c->lengthlist[i-1]==0 || c->lengthlist[i]<c->lengthlist[i-1])break;
  35. if(i==c->entries)ordered=1;
  36. if(ordered){
  37. /* length ordered. We only need to say how many codewords of
  38. each length. The actual codewords are generated
  39. deterministically */
  40. long count=0;
  41. oggpack_write(opb,1,1); /* ordered */
  42. oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */
  43. for(i=1;i<c->entries;i++){
  44. char this=c->lengthlist[i];
  45. char last=c->lengthlist[i-1];
  46. if(this>last){
  47. for(j=last;j<this;j++){
  48. oggpack_write(opb,i-count,ov_ilog(c->entries-count));
  49. count=i;
  50. }
  51. }
  52. }
  53. oggpack_write(opb,i-count,ov_ilog(c->entries-count));
  54. }else{
  55. /* length random. Again, we don't code the codeword itself, just
  56. the length. This time, though, we have to encode each length */
  57. oggpack_write(opb,0,1); /* unordered */
  58. /* algortihmic mapping has use for 'unused entries', which we tag
  59. here. The algorithmic mapping happens as usual, but the unused
  60. entry has no codeword. */
  61. for(i=0;i<c->entries;i++)
  62. if(c->lengthlist[i]==0)break;
  63. if(i==c->entries){
  64. oggpack_write(opb,0,1); /* no unused entries */
  65. for(i=0;i<c->entries;i++)
  66. oggpack_write(opb,c->lengthlist[i]-1,5);
  67. }else{
  68. oggpack_write(opb,1,1); /* we have unused entries; thus we tag */
  69. for(i=0;i<c->entries;i++){
  70. if(c->lengthlist[i]==0){
  71. oggpack_write(opb,0,1);
  72. }else{
  73. oggpack_write(opb,1,1);
  74. oggpack_write(opb,c->lengthlist[i]-1,5);
  75. }
  76. }
  77. }
  78. }
  79. /* is the entry number the desired return value, or do we have a
  80. mapping? If we have a mapping, what type? */
  81. oggpack_write(opb,c->maptype,4);
  82. switch(c->maptype){
  83. case 0:
  84. /* no mapping */
  85. break;
  86. case 1:case 2:
  87. /* implicitly populated value mapping */
  88. /* explicitly populated value mapping */
  89. if(!c->quantlist){
  90. /* no quantlist? error */
  91. return(-1);
  92. }
  93. /* values that define the dequantization */
  94. oggpack_write(opb,c->q_min,32);
  95. oggpack_write(opb,c->q_delta,32);
  96. oggpack_write(opb,c->q_quant-1,4);
  97. oggpack_write(opb,c->q_sequencep,1);
  98. {
  99. int quantvals;
  100. switch(c->maptype){
  101. case 1:
  102. /* a single column of (c->entries/c->dim) quantized values for
  103. building a full value list algorithmically (square lattice) */
  104. quantvals=_book_maptype1_quantvals(c);
  105. break;
  106. case 2:
  107. /* every value (c->entries*c->dim total) specified explicitly */
  108. quantvals=c->entries*c->dim;
  109. break;
  110. default: /* NOT_REACHABLE */
  111. quantvals=-1;
  112. }
  113. /* quantized values */
  114. for(i=0;i<quantvals;i++)
  115. oggpack_write(opb,labs(c->quantlist[i]),c->q_quant);
  116. }
  117. break;
  118. default:
  119. /* error case; we don't have any other map types now */
  120. return(-1);
  121. }
  122. return(0);
  123. }
  124. /* unpacks a codebook from the packet buffer into the codebook struct,
  125. readies the codebook auxiliary structures for decode *************/
  126. static_codebook *vorbis_staticbook_unpack(oggpack_buffer *opb){
  127. long i,j;
  128. static_codebook *s=_ogg_calloc(1,sizeof(*s));
  129. s->allocedp=1;
  130. /* make sure alignment is correct */
  131. if(oggpack_read(opb,24)!=0x564342)goto _eofout;
  132. /* first the basic parameters */
  133. s->dim=oggpack_read(opb,16);
  134. s->entries=oggpack_read(opb,24);
  135. if(s->entries==-1)goto _eofout;
  136. if(ov_ilog(s->dim)+ov_ilog(s->entries)>24)goto _eofout;
  137. /* codeword ordering.... length ordered or unordered? */
  138. switch((int)oggpack_read(opb,1)){
  139. case 0:{
  140. long unused;
  141. /* allocated but unused entries? */
  142. unused=oggpack_read(opb,1);
  143. if((s->entries*(unused?1:5)+7)>>3>opb->storage-oggpack_bytes(opb))
  144. goto _eofout;
  145. /* unordered */
  146. s->lengthlist=_ogg_malloc(sizeof(*s->lengthlist)*s->entries);
  147. /* allocated but unused entries? */
  148. if(unused){
  149. /* yes, unused entries */
  150. for(i=0;i<s->entries;i++){
  151. if(oggpack_read(opb,1)){
  152. long num=oggpack_read(opb,5);
  153. if(num==-1)goto _eofout;
  154. s->lengthlist[i]=num+1;
  155. }else
  156. s->lengthlist[i]=0;
  157. }
  158. }else{
  159. /* all entries used; no tagging */
  160. for(i=0;i<s->entries;i++){
  161. long num=oggpack_read(opb,5);
  162. if(num==-1)goto _eofout;
  163. s->lengthlist[i]=num+1;
  164. }
  165. }
  166. break;
  167. }
  168. case 1:
  169. /* ordered */
  170. {
  171. long length=oggpack_read(opb,5)+1;
  172. if(length==0)goto _eofout;
  173. s->lengthlist=_ogg_malloc(sizeof(*s->lengthlist)*s->entries);
  174. for(i=0;i<s->entries;){
  175. long num=oggpack_read(opb,ov_ilog(s->entries-i));
  176. if(num==-1)goto _eofout;
  177. if(length>32 || num>s->entries-i ||
  178. (num>0 && (num-1)>>(length-1)>1)){
  179. goto _errout;
  180. }
  181. if(length>32)goto _errout;
  182. for(j=0;j<num;j++,i++)
  183. s->lengthlist[i]=length;
  184. length++;
  185. }
  186. }
  187. break;
  188. default:
  189. /* EOF */
  190. goto _eofout;
  191. }
  192. /* Do we have a mapping to unpack? */
  193. switch((s->maptype=oggpack_read(opb,4))){
  194. case 0:
  195. /* no mapping */
  196. break;
  197. case 1: case 2:
  198. /* implicitly populated value mapping */
  199. /* explicitly populated value mapping */
  200. s->q_min=oggpack_read(opb,32);
  201. s->q_delta=oggpack_read(opb,32);
  202. s->q_quant=oggpack_read(opb,4)+1;
  203. s->q_sequencep=oggpack_read(opb,1);
  204. if(s->q_sequencep==-1)goto _eofout;
  205. {
  206. int quantvals=0;
  207. switch(s->maptype){
  208. case 1:
  209. quantvals=(s->dim==0?0:_book_maptype1_quantvals(s));
  210. break;
  211. case 2:
  212. quantvals=s->entries*s->dim;
  213. break;
  214. }
  215. /* quantized values */
  216. if(((quantvals*s->q_quant+7)>>3)>opb->storage-oggpack_bytes(opb))
  217. goto _eofout;
  218. s->quantlist=_ogg_malloc(sizeof(*s->quantlist)*quantvals);
  219. for(i=0;i<quantvals;i++)
  220. s->quantlist[i]=oggpack_read(opb,s->q_quant);
  221. if(quantvals&&s->quantlist[quantvals-1]==-1)goto _eofout;
  222. }
  223. break;
  224. default:
  225. goto _errout;
  226. }
  227. /* all set */
  228. return(s);
  229. _errout:
  230. _eofout:
  231. vorbis_staticbook_destroy(s);
  232. return(NULL);
  233. }
  234. /* returns the number of bits ************************************************/
  235. int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){
  236. if(a<0 || a>=book->c->entries)return(0);
  237. oggpack_write(b,book->codelist[a],book->c->lengthlist[a]);
  238. return(book->c->lengthlist[a]);
  239. }
  240. /* the 'eliminate the decode tree' optimization actually requires the
  241. codewords to be MSb first, not LSb. This is an annoying inelegancy
  242. (and one of the first places where carefully thought out design
  243. turned out to be wrong; Vorbis II and future Ogg codecs should go
  244. to an MSb bitpacker), but not actually the huge hit it appears to
  245. be. The first-stage decode table catches most words so that
  246. bitreverse is not in the main execution path. */
  247. static ogg_uint32_t bitreverse(ogg_uint32_t x){
  248. x= ((x>>16)&0x0000ffff) | ((x<<16)&0xffff0000);
  249. x= ((x>> 8)&0x00ff00ff) | ((x<< 8)&0xff00ff00);
  250. x= ((x>> 4)&0x0f0f0f0f) | ((x<< 4)&0xf0f0f0f0);
  251. x= ((x>> 2)&0x33333333) | ((x<< 2)&0xcccccccc);
  252. return((x>> 1)&0x55555555) | ((x<< 1)&0xaaaaaaaa);
  253. }
  254. STIN long decode_packed_entry_number(codebook *book, oggpack_buffer *b){
  255. int read=book->dec_maxlength;
  256. long lo,hi;
  257. long lok = oggpack_look(b,book->dec_firsttablen);
  258. if (lok >= 0) {
  259. long entry = book->dec_firsttable[lok];
  260. if(entry&0x80000000UL){
  261. lo=(entry>>15)&0x7fff;
  262. hi=book->used_entries-(entry&0x7fff);
  263. }else{
  264. oggpack_adv(b, book->dec_codelengths[entry-1]);
  265. return(entry-1);
  266. }
  267. }else{
  268. lo=0;
  269. hi=book->used_entries;
  270. }
  271. /* Single entry codebooks use a firsttablen of 1 and a
  272. dec_maxlength of 1. If a single-entry codebook gets here (due to
  273. failure to read one bit above), the next look attempt will also
  274. fail and we'll correctly kick out instead of trying to walk the
  275. underformed tree */
  276. lok = oggpack_look(b, read);
  277. while(lok<0 && read>1)
  278. lok = oggpack_look(b, --read);
  279. if(lok<0)return -1;
  280. /* bisect search for the codeword in the ordered list */
  281. {
  282. ogg_uint32_t testword=bitreverse((ogg_uint32_t)lok);
  283. while(hi-lo>1){
  284. long p=(hi-lo)>>1;
  285. long test=book->codelist[lo+p]>testword;
  286. lo+=p&(test-1);
  287. hi-=p&(-test);
  288. }
  289. if(book->dec_codelengths[lo]<=read){
  290. oggpack_adv(b, book->dec_codelengths[lo]);
  291. return(lo);
  292. }
  293. }
  294. oggpack_adv(b, read);
  295. return(-1);
  296. }
  297. /* Decode side is specced and easier, because we don't need to find
  298. matches using different criteria; we simply read and map. There are
  299. two things we need to do 'depending':
  300. We may need to support interleave. We don't really, but it's
  301. convenient to do it here rather than rebuild the vector later.
  302. Cascades may be additive or multiplicitive; this is not inherent in
  303. the codebook, but set in the code using the codebook. Like
  304. interleaving, it's easiest to do it here.
  305. addmul==0 -> declarative (set the value)
  306. addmul==1 -> additive
  307. addmul==2 -> multiplicitive */
  308. /* returns the [original, not compacted] entry number or -1 on eof *********/
  309. long vorbis_book_decode(codebook *book, oggpack_buffer *b){
  310. if(book->used_entries>0){
  311. long packed_entry=decode_packed_entry_number(book,b);
  312. if(packed_entry>=0)
  313. return(book->dec_index[packed_entry]);
  314. }
  315. /* if there's no dec_index, the codebook unpacking isn't collapsed */
  316. return(-1);
  317. }
  318. /* returns 0 on OK or -1 on eof *************************************/
  319. /* decode vector / dim granularity gaurding is done in the upper layer */
  320. long vorbis_book_decodevs_add(codebook *book,float *a,oggpack_buffer *b,int n){
  321. if(book->used_entries>0){
  322. int step=n/book->dim;
  323. long *entry = alloca(sizeof(*entry)*step);
  324. float **t = alloca(sizeof(*t)*step);
  325. int i,j,o;
  326. for (i = 0; i < step; i++) {
  327. entry[i]=decode_packed_entry_number(book,b);
  328. if(entry[i]==-1)return(-1);
  329. t[i] = book->valuelist+entry[i]*book->dim;
  330. }
  331. for(i=0,o=0;i<book->dim;i++,o+=step)
  332. for (j=0;o+j<n && j<step;j++)
  333. a[o+j]+=t[j][i];
  334. }
  335. return(0);
  336. }
  337. /* decode vector / dim granularity gaurding is done in the upper layer */
  338. long vorbis_book_decodev_add(codebook *book,float *a,oggpack_buffer *b,int n){
  339. if(book->used_entries>0){
  340. int i,j,entry;
  341. float *t;
  342. for(i=0;i<n;){
  343. entry = decode_packed_entry_number(book,b);
  344. if(entry==-1)return(-1);
  345. t = book->valuelist+entry*book->dim;
  346. for(j=0;i<n && j<book->dim;)
  347. a[i++]+=t[j++];
  348. }
  349. }
  350. return(0);
  351. }
  352. /* unlike the others, we guard against n not being an integer number
  353. of <dim> internally rather than in the upper layer (called only by
  354. floor0) */
  355. long vorbis_book_decodev_set(codebook *book,float *a,oggpack_buffer *b,int n){
  356. if(book->used_entries>0){
  357. int i,j,entry;
  358. float *t;
  359. for(i=0;i<n;){
  360. entry = decode_packed_entry_number(book,b);
  361. if(entry==-1)return(-1);
  362. t = book->valuelist+entry*book->dim;
  363. for (j=0;i<n && j<book->dim;){
  364. a[i++]=t[j++];
  365. }
  366. }
  367. }else{
  368. int i;
  369. for(i=0;i<n;){
  370. a[i++]=0.f;
  371. }
  372. }
  373. return(0);
  374. }
  375. long vorbis_book_decodevv_add(codebook *book,float **a,long offset,int ch,
  376. oggpack_buffer *b,int n){
  377. long i,j,entry;
  378. int chptr=0;
  379. if(book->used_entries>0){
  380. int m=(offset+n)/ch;
  381. for(i=offset/ch;i<m;){
  382. entry = decode_packed_entry_number(book,b);
  383. if(entry==-1)return(-1);
  384. {
  385. const float *t = book->valuelist+entry*book->dim;
  386. for (j=0;i<m && j<book->dim;j++){
  387. a[chptr++][i]+=t[j];
  388. if(chptr==ch){
  389. chptr=0;
  390. i++;
  391. }
  392. }
  393. }
  394. }
  395. }
  396. return(0);
  397. }