codebook.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: basic codebook pack/unpack/code/decode operations
  14. last mod: $Id: codebook.c,v 1.11.4.5 2000/04/21 16:35:38 xiphmont Exp $
  15. ********************************************************************/
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include "vorbis/codec.h"
  20. #include "vorbis/codebook.h"
  21. #include "bitwise.h"
  22. #include "scales.h"
  23. #include "sharedbook.h"
  24. #include "bookinternal.h"
  25. /* packs the given codebook into the bitstream **************************/
  26. int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){
  27. long i,j;
  28. int ordered=0;
  29. /* first the basic parameters */
  30. _oggpack_write(opb,0x564342,24);
  31. _oggpack_write(opb,c->dim,16);
  32. _oggpack_write(opb,c->entries,24);
  33. /* pack the codewords. There are two packings; length ordered and
  34. length random. Decide between the two now. */
  35. for(i=1;i<c->entries;i++)
  36. if(c->lengthlist[i]<c->lengthlist[i-1])break;
  37. if(i==c->entries)ordered=1;
  38. if(ordered){
  39. /* length ordered. We only need to say how many codewords of
  40. each length. The actual codewords are generated
  41. deterministically */
  42. long count=0;
  43. _oggpack_write(opb,1,1); /* ordered */
  44. _oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */
  45. for(i=1;i<c->entries;i++){
  46. long this=c->lengthlist[i];
  47. long last=c->lengthlist[i-1];
  48. if(this>last){
  49. for(j=last;j<this;j++){
  50. _oggpack_write(opb,i-count,_ilog(c->entries-count));
  51. count=i;
  52. }
  53. }
  54. }
  55. _oggpack_write(opb,i-count,_ilog(c->entries-count));
  56. }else{
  57. /* length random. Again, we don't code the codeword itself, just
  58. the length. This time, though, we have to encode each length */
  59. _oggpack_write(opb,0,1); /* unordered */
  60. /* algortihmic mapping has use for 'unused entries', which we tag
  61. here. The algorithmic mapping happens as usual, but the unused
  62. entry has no codeword. */
  63. for(i=0;i<c->entries;i++)
  64. if(c->lengthlist[i]==0)break;
  65. if(i==c->entries){
  66. _oggpack_write(opb,0,1); /* no unused entries */
  67. for(i=0;i<c->entries;i++)
  68. _oggpack_write(opb,c->lengthlist[i]-1,5);
  69. }else{
  70. _oggpack_write(opb,1,1); /* we have unused entries; thus we tag */
  71. for(i=0;i<c->entries;i++){
  72. if(c->lengthlist[i]==0){
  73. _oggpack_write(opb,0,1);
  74. }else{
  75. _oggpack_write(opb,1,1);
  76. _oggpack_write(opb,c->lengthlist[i]-1,5);
  77. }
  78. }
  79. }
  80. }
  81. /* is the entry number the desired return value, or do we have a
  82. mapping? If we have a mapping, what type? */
  83. _oggpack_write(opb,c->maptype,4);
  84. switch(c->maptype){
  85. case 0:
  86. /* no mapping */
  87. break;
  88. case 1:case 2:
  89. /* implicitly populated value mapping */
  90. /* explicitly populated value mapping */
  91. if(!c->quantlist){
  92. /* no quantlist? error */
  93. return(-1);
  94. }
  95. /* values that define the dequantization */
  96. _oggpack_write(opb,c->q_min,32);
  97. _oggpack_write(opb,c->q_delta,32);
  98. _oggpack_write(opb,c->q_quant-1,4);
  99. _oggpack_write(opb,c->q_sequencep,1);
  100. {
  101. int quantvals;
  102. switch(c->maptype){
  103. case 1:
  104. /* a single column of (c->entries/c->dim) quantized values for
  105. building a full value list algorithmically (square lattice) */
  106. quantvals=_book_maptype1_quantvals(c);
  107. break;
  108. case 2:
  109. /* every value (c->entries*c->dim total) specified explicitly */
  110. quantvals=c->entries*c->dim;
  111. break;
  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. int vorbis_staticbook_unpack(oggpack_buffer *opb,static_codebook *s){
  127. long i,j;
  128. memset(s,0,sizeof(static_codebook));
  129. /* make sure alignment is correct */
  130. if(_oggpack_read(opb,24)!=0x564342)goto _eofout;
  131. /* first the basic parameters */
  132. s->dim=_oggpack_read(opb,16);
  133. s->entries=_oggpack_read(opb,24);
  134. if(s->entries==-1)goto _eofout;
  135. /* codeword ordering.... length ordered or unordered? */
  136. switch(_oggpack_read(opb,1)){
  137. case 0:
  138. /* unordered */
  139. s->lengthlist=malloc(sizeof(long)*s->entries);
  140. /* allocated but unused entries? */
  141. if(_oggpack_read(opb,1)){
  142. /* yes, unused entries */
  143. for(i=0;i<s->entries;i++){
  144. if(_oggpack_read(opb,1)){
  145. long num=_oggpack_read(opb,5);
  146. if(num==-1)goto _eofout;
  147. s->lengthlist[i]=num+1;
  148. }else
  149. s->lengthlist[i]=0;
  150. }
  151. }else{
  152. /* all entries used; no tagging */
  153. for(i=0;i<s->entries;i++){
  154. long num=_oggpack_read(opb,5);
  155. if(num==-1)goto _eofout;
  156. s->lengthlist[i]=num+1;
  157. }
  158. }
  159. break;
  160. case 1:
  161. /* ordered */
  162. {
  163. long length=_oggpack_read(opb,5)+1;
  164. s->lengthlist=malloc(sizeof(long)*s->entries);
  165. for(i=0;i<s->entries;){
  166. long num=_oggpack_read(opb,_ilog(s->entries-i));
  167. if(num==-1)goto _eofout;
  168. for(j=0;j<num;j++,i++)
  169. s->lengthlist[i]=length;
  170. length++;
  171. }
  172. }
  173. break;
  174. default:
  175. /* EOF */
  176. return(-1);
  177. }
  178. /* Do we have a mapping to unpack? */
  179. switch((s->maptype=_oggpack_read(opb,4))){
  180. case 0:
  181. /* no mapping */
  182. break;
  183. case 1: case 2:
  184. /* implicitly populated value mapping */
  185. /* explicitly populated value mapping */
  186. s->q_min=_oggpack_read(opb,32);
  187. s->q_delta=_oggpack_read(opb,32);
  188. s->q_quant=_oggpack_read(opb,4)+1;
  189. s->q_sequencep=_oggpack_read(opb,1);
  190. {
  191. int quantvals;
  192. switch(s->maptype){
  193. case 1:
  194. quantvals=_book_maptype1_quantvals(s);
  195. break;
  196. case 2:
  197. quantvals=s->entries*s->dim;
  198. break;
  199. }
  200. /* quantized values */
  201. s->quantlist=malloc(sizeof(double)*quantvals);
  202. for(i=0;i<quantvals;i++)
  203. s->quantlist[i]=_oggpack_read(opb,s->q_quant);
  204. if(s->quantlist[quantvals-1]==-1)goto _eofout;
  205. }
  206. break;
  207. default:
  208. goto _errout;
  209. }
  210. /* all set */
  211. return(0);
  212. _errout:
  213. _eofout:
  214. vorbis_staticbook_clear(s);
  215. return(-1);
  216. }
  217. /* returns the number of bits ************************************************/
  218. int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){
  219. _oggpack_write(b,book->codelist[a],book->c->lengthlist[a]);
  220. return(book->c->lengthlist[a]);
  221. }
  222. /* One the encode side, our vector writers are each designed for a
  223. specific purpose, and the encoder is not flexible without modification:
  224. The LSP vector coder uses a single stage nearest-match with no
  225. interleave, so no step and no error return. This is specced by floor0
  226. and doesn't change.
  227. Residue0 encoding interleaves, uses multiple stages, and each stage
  228. peels of a specific amount of resolution from a lattice (thus we want
  229. to match by threshhold, not nearest match). Residue doesn't *have* to
  230. be encoded that way, but to change it, one will need to add more
  231. infrastructure on the encode side (decode side is specced and simpler) */
  232. /* floor0 LSP (single stage, non interleaved, nearest match) */
  233. /* returns the number of bits and *modifies a* to the quantization value *****/
  234. int vorbis_book_encodev(codebook *book,double *a,oggpack_buffer *b){
  235. int dim=book->dim,k;
  236. int best=_best(book,a,1);
  237. for(k=0;k<dim;k++)
  238. a[k]=(book->valuelist+best*dim)[k];
  239. return(vorbis_book_encode(book,best,b));
  240. }
  241. /* res0 (multistage, interleave, lattice) */
  242. /* returns the number of bits and *modifies a* to the remainder value ********/
  243. int vorbis_book_encodevs(codebook *book,double *a,oggpack_buffer *b,
  244. int step,int addmul){
  245. int best=vorbis_book_besterror(book,a,step,addmul);
  246. return(vorbis_book_encode(book,best,b));
  247. }
  248. /* Decode side is specced and easier, because we don't need to find
  249. matches using different criteria; we simply read and map. There are
  250. two things we need to do 'depending':
  251. We may need to support interleave. We don't really, but it's
  252. convenient to do it here rather than rebuild the vector later.
  253. Cascades may be additive or multiplicitive; this is not inherent in
  254. the codebook, but set in the code using the codebook. Like
  255. interleaving, it's easiest to do it here.
  256. stage==0 -> declarative (set the value)
  257. stage==1 -> additive
  258. stage==2 -> multiplicitive */
  259. /* returns the entry number or -1 on eof *************************************/
  260. long vorbis_book_decode(codebook *book, oggpack_buffer *b){
  261. long ptr=0;
  262. decode_aux *t=book->decode_tree;
  263. do{
  264. switch(_oggpack_read1(b)){
  265. case 0:
  266. ptr=t->ptr0[ptr];
  267. break;
  268. case 1:
  269. ptr=t->ptr1[ptr];
  270. break;
  271. case -1:
  272. return(-1);
  273. }
  274. }while(ptr>0);
  275. return(-ptr);
  276. }
  277. /* returns the entry number or -1 on eof *************************************/
  278. long vorbis_book_decodevs(codebook *book,double *a,oggpack_buffer *b,
  279. int step,int addmul){
  280. long entry=vorbis_book_decode(book,b);
  281. int i,o;
  282. if(entry==-1)return(-1);
  283. switch(addmul){
  284. case -1:
  285. for(i=0,o=0;i<book->dim;i++,o+=step)
  286. a[o]=(book->valuelist+entry*book->dim)[i];
  287. break;
  288. case 0:
  289. for(i=0,o=0;i<book->dim;i++,o+=step)
  290. a[o]+=(book->valuelist+entry*book->dim)[i];
  291. break;
  292. case 1:
  293. for(i=0,o=0;i<book->dim;i++,o+=step)
  294. a[o]*=(book->valuelist+entry*book->dim)[i];
  295. break;
  296. }
  297. return(entry);
  298. }
  299. #ifdef _V_SELFTEST
  300. /* Simple enough; pack a few candidate codebooks, unpack them. Code a
  301. number of vectors through (keeping track of the quantized values),
  302. and decode using the unpacked book. quantized version of in should
  303. exactly equal out */
  304. #include <stdio.h>
  305. #include "vorbis/book/lsp20_0.vqh"
  306. #include "vorbis/book/lsp32_0.vqh"
  307. #include "vorbis/book/res0_1a.vqh"
  308. #define TESTSIZE 40
  309. double test1[TESTSIZE]={
  310. 0.105939,
  311. 0.215373,
  312. 0.429117,
  313. 0.587974,
  314. 0.181173,
  315. 0.296583,
  316. 0.515707,
  317. 0.715261,
  318. 0.162327,
  319. 0.263834,
  320. 0.342876,
  321. 0.406025,
  322. 0.103571,
  323. 0.223561,
  324. 0.368513,
  325. 0.540313,
  326. 0.136672,
  327. 0.395882,
  328. 0.587183,
  329. 0.652476,
  330. 0.114338,
  331. 0.417300,
  332. 0.525486,
  333. 0.698679,
  334. 0.147492,
  335. 0.324481,
  336. 0.643089,
  337. 0.757582,
  338. 0.139556,
  339. 0.215795,
  340. 0.324559,
  341. 0.399387,
  342. 0.120236,
  343. 0.267420,
  344. 0.446940,
  345. 0.608760,
  346. 0.115587,
  347. 0.287234,
  348. 0.571081,
  349. 0.708603,
  350. };
  351. double test2[TESTSIZE]={
  352. 0.088654,
  353. 0.165742,
  354. 0.279013,
  355. 0.395894,
  356. 0.110812,
  357. 0.218422,
  358. 0.283423,
  359. 0.371719,
  360. 0.136985,
  361. 0.186066,
  362. 0.309814,
  363. 0.381521,
  364. 0.123925,
  365. 0.211707,
  366. 0.314771,
  367. 0.433026,
  368. 0.088619,
  369. 0.192276,
  370. 0.277568,
  371. 0.343509,
  372. 0.068400,
  373. 0.132901,
  374. 0.223999,
  375. 0.302538,
  376. 0.202159,
  377. 0.306131,
  378. 0.360362,
  379. 0.416066,
  380. 0.072591,
  381. 0.178019,
  382. 0.304315,
  383. 0.376516,
  384. 0.094336,
  385. 0.188401,
  386. 0.325119,
  387. 0.390264,
  388. 0.091636,
  389. 0.223099,
  390. 0.282899,
  391. 0.375124,
  392. };
  393. double test3[TESTSIZE]={
  394. 0,1,-2,3,4,-5,6,7,8,9,
  395. 8,-2,7,-1,4,6,8,3,1,-9,
  396. 10,11,12,13,14,15,26,17,18,19,
  397. 30,-25,-30,-1,-5,-32,4,3,-2,0};
  398. static_codebook *testlist[]={&_vq_book_lsp20_0,
  399. &_vq_book_lsp32_0,
  400. &_vq_book_res0_1a,NULL};
  401. double *testvec[]={test1,test2,test3};
  402. int main(){
  403. oggpack_buffer write;
  404. oggpack_buffer read;
  405. long ptr=0,i;
  406. _oggpack_writeinit(&write);
  407. fprintf(stderr,"Testing codebook abstraction...:\n");
  408. while(testlist[ptr]){
  409. codebook c;
  410. static_codebook s;
  411. double *qv=alloca(sizeof(double)*TESTSIZE);
  412. double *iv=alloca(sizeof(double)*TESTSIZE);
  413. memcpy(qv,testvec[ptr],sizeof(double)*TESTSIZE);
  414. memset(iv,0,sizeof(double)*TESTSIZE);
  415. fprintf(stderr,"\tpacking/coding %ld... ",ptr);
  416. /* pack the codebook, write the testvector */
  417. _oggpack_reset(&write);
  418. vorbis_book_init_encode(&c,testlist[ptr]); /* get it into memory
  419. we can write */
  420. vorbis_staticbook_pack(testlist[ptr],&write);
  421. fprintf(stderr,"Codebook size %ld bytes... ",_oggpack_bytes(&write));
  422. for(i=0;i<TESTSIZE;i+=c.dim)
  423. vorbis_book_encodev(&c,qv+i,&write);
  424. vorbis_book_clear(&c);
  425. fprintf(stderr,"OK.\n");
  426. fprintf(stderr,"\tunpacking/decoding %ld... ",ptr);
  427. /* transfer the write data to a read buffer and unpack/read */
  428. _oggpack_readinit(&read,_oggpack_buffer(&write),_oggpack_bytes(&write));
  429. if(vorbis_staticbook_unpack(&read,&s)){
  430. fprintf(stderr,"Error unpacking codebook.\n");
  431. exit(1);
  432. }
  433. if(vorbis_book_init_decode(&c,&s)){
  434. fprintf(stderr,"Error initializing codebook.\n");
  435. exit(1);
  436. }
  437. for(i=0;i<TESTSIZE;i+=c.dim)
  438. if(vorbis_book_decodevs(&c,iv+i,&read,1,-1)==-1){
  439. fprintf(stderr,"Error reading codebook test data (EOP).\n");
  440. exit(1);
  441. }
  442. for(i=0;i<TESTSIZE;i++)
  443. if(fabs(qv[i]-iv[i])>.000001){
  444. fprintf(stderr,"read (%g) != written (%g) at position (%ld)\n",
  445. iv[i],qv[i],i);
  446. exit(1);
  447. }
  448. fprintf(stderr,"OK\n");
  449. ptr++;
  450. }
  451. /* The above is the trivial stuff; now try unquantizing a log scale codebook */
  452. exit(0);
  453. }
  454. #endif