codebook.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
  6. * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis 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.18.2.4 2000/11/04 06:43:49 xiphmont Exp $
  15. ********************************************************************/
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include <ogg/ogg.h>
  20. #include "vorbis/codec.h"
  21. #include "codebook.h"
  22. #include "scales.h"
  23. #include "misc.h"
  24. #include "os.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. 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. /* codeword ordering.... length ordered or unordered? */
  137. switch(oggpack_read(opb,1)){
  138. case 0:
  139. /* unordered */
  140. s->lengthlist=_ogg_malloc(sizeof(long)*s->entries);
  141. /* allocated but unused entries? */
  142. if(oggpack_read(opb,1)){
  143. /* yes, unused entries */
  144. for(i=0;i<s->entries;i++){
  145. if(oggpack_read(opb,1)){
  146. long num=oggpack_read(opb,5);
  147. if(num==-1)goto _eofout;
  148. s->lengthlist[i]=num+1;
  149. }else
  150. s->lengthlist[i]=0;
  151. }
  152. }else{
  153. /* all entries used; no tagging */
  154. for(i=0;i<s->entries;i++){
  155. long num=oggpack_read(opb,5);
  156. if(num==-1)goto _eofout;
  157. s->lengthlist[i]=num+1;
  158. }
  159. }
  160. break;
  161. case 1:
  162. /* ordered */
  163. {
  164. long length=oggpack_read(opb,5)+1;
  165. s->lengthlist=_ogg_malloc(sizeof(long)*s->entries);
  166. for(i=0;i<s->entries;){
  167. long num=oggpack_read(opb,_ilog(s->entries-i));
  168. if(num==-1)goto _eofout;
  169. for(j=0;j<num;j++,i++)
  170. s->lengthlist[i]=length;
  171. length++;
  172. }
  173. }
  174. break;
  175. default:
  176. /* EOF */
  177. return(-1);
  178. }
  179. /* Do we have a mapping to unpack? */
  180. switch((s->maptype=oggpack_read(opb,4))){
  181. case 0:
  182. /* no mapping */
  183. break;
  184. case 1: case 2:
  185. /* implicitly populated value mapping */
  186. /* explicitly populated value mapping */
  187. s->q_min=oggpack_read(opb,32);
  188. s->q_delta=oggpack_read(opb,32);
  189. s->q_quant=oggpack_read(opb,4)+1;
  190. s->q_sequencep=oggpack_read(opb,1);
  191. {
  192. int quantvals;
  193. switch(s->maptype){
  194. case 1:
  195. quantvals=_book_maptype1_quantvals(s);
  196. break;
  197. case 2:
  198. quantvals=s->entries*s->dim;
  199. break;
  200. }
  201. /* quantized values */
  202. s->quantlist=_ogg_malloc(sizeof(float)*quantvals);
  203. for(i=0;i<quantvals;i++)
  204. s->quantlist[i]=oggpack_read(opb,s->q_quant);
  205. if(s->quantlist[quantvals-1]==-1)goto _eofout;
  206. }
  207. break;
  208. default:
  209. goto _errout;
  210. }
  211. /* all set */
  212. return(0);
  213. _errout:
  214. _eofout:
  215. vorbis_staticbook_clear(s);
  216. return(-1);
  217. }
  218. /* returns the number of bits ************************************************/
  219. int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){
  220. oggpack_write(b,book->codelist[a],book->c->lengthlist[a]);
  221. return(book->c->lengthlist[a]);
  222. }
  223. /* One the encode side, our vector writers are each designed for a
  224. specific purpose, and the encoder is not flexible without modification:
  225. The LSP vector coder uses a single stage nearest-match with no
  226. interleave, so no step and no error return. This is specced by floor0
  227. and doesn't change.
  228. Residue0 encoding interleaves, uses multiple stages, and each stage
  229. peels of a specific amount of resolution from a lattice (thus we want
  230. to match by threshold, not nearest match). Residue doesn't *have* to
  231. be encoded that way, but to change it, one will need to add more
  232. infrastructure on the encode side (decode side is specced and simpler) */
  233. /* floor0 LSP (single stage, non interleaved, nearest match) */
  234. /* returns entry number and *modifies a* to the quantization value *****/
  235. int vorbis_book_errorv(codebook *book,float *a){
  236. int dim=book->dim,k;
  237. int best=_best(book,a,1);
  238. for(k=0;k<dim;k++)
  239. a[k]=(book->valuelist+best*dim)[k];
  240. return(best);
  241. }
  242. /* returns the number of bits and *modifies a* to the quantization value *****/
  243. int vorbis_book_encodev(codebook *book,int best,float *a,oggpack_buffer *b){
  244. int k,dim=book->dim;
  245. for(k=0;k<dim;k++)
  246. a[k]=(book->valuelist+best*dim)[k];
  247. return(vorbis_book_encode(book,best,b));
  248. }
  249. /* res0 (multistage, interleave, lattice) */
  250. /* returns the number of bits and *modifies a* to the remainder value ********/
  251. int vorbis_book_encodevs(codebook *book,float *a,oggpack_buffer *b,
  252. int step,int addmul){
  253. int best=vorbis_book_besterror(book,a,step,addmul);
  254. return(vorbis_book_encode(book,best,b));
  255. }
  256. /* Decode side is specced and easier, because we don't need to find
  257. matches using different criteria; we simply read and map. There are
  258. two things we need to do 'depending':
  259. We may need to support interleave. We don't really, but it's
  260. convenient to do it here rather than rebuild the vector later.
  261. Cascades may be additive or multiplicitive; this is not inherent in
  262. the codebook, but set in the code using the codebook. Like
  263. interleaving, it's easiest to do it here.
  264. addmul==0 -> declarative (set the value)
  265. addmul==1 -> additive
  266. addmul==2 -> multiplicitive */
  267. /* returns the entry number or -1 on eof *************************************/
  268. long vorbis_book_decode(codebook *book, oggpack_buffer *b){
  269. long ptr=0;
  270. decode_aux *t=book->decode_tree;
  271. int lok = oggpack_look(b, t->tabn);
  272. if (lok >= 0) {
  273. ptr = t->tab[lok];
  274. oggpack_adv(b, t->tabl[lok]);
  275. if (ptr <= 0)
  276. return -ptr;
  277. }
  278. do{
  279. switch(oggpack_read1(b)){
  280. case 0:
  281. ptr=t->ptr0[ptr];
  282. break;
  283. case 1:
  284. ptr=t->ptr1[ptr];
  285. break;
  286. case -1:
  287. return(-1);
  288. }
  289. }while(ptr>0);
  290. return(-ptr);
  291. }
  292. /* returns the entry number or -1 on eof *************************************/
  293. long vorbis_book_decodevs(codebook *book,float *a,oggpack_buffer *b,
  294. int step,int addmul){
  295. long entry=vorbis_book_decode(book,b);
  296. int i,o;
  297. float *t;
  298. if(entry==-1)return(-1);
  299. t = book->valuelist+entry*book->dim;
  300. switch(addmul){
  301. case -1:
  302. for(i=0,o=0;i<book->dim-3;i+=4,o+=4*step) {
  303. a[o]=t[i];
  304. a[o+step]=t[i+1];
  305. a[o+2*step]=t[i+2];
  306. a[o+3*step]=t[i+3];
  307. }
  308. for(;i<book->dim;i++,o+=step)
  309. a[o]=t[i];
  310. break;
  311. case 0:
  312. for(i=0,o=0;i<book->dim-3;i+=4,o+=4*step) {
  313. a[o]+=t[i];
  314. a[o+step]+=t[i+1];
  315. a[o+2*step]+=t[i+2];
  316. a[o+3*step]+=t[i+3];
  317. }
  318. for(;i<book->dim;i++,o+=step)
  319. a[o]+=t[i];
  320. break;
  321. case 1:
  322. for(i=0,o=0;i<book->dim-3;i+=4,o+=4*step) {
  323. a[o]*=t[i];
  324. a[o+step]*=t[i+1];
  325. a[o+2*step]*=t[i+2];
  326. a[o+3*step]*=t[i+3];
  327. }
  328. for(;i<book->dim;i++,o+=step)
  329. a[o]*=t[i];
  330. break;
  331. }
  332. return(entry);
  333. }
  334. /* returns 0 on OK or -1 on eof *************************************/
  335. long s_vorbis_book_decodevs(codebook *book,float *a,oggpack_buffer *b,
  336. int step,int addmul){
  337. long *entry = alloca(sizeof(long)*step);
  338. float **t = alloca(sizeof(float *)*step);
  339. int i,j,o;
  340. for (i = 0; i < step; i++) {
  341. entry[i]=vorbis_book_decode(book,b);
  342. if(entry[i]==-1)return(-1);
  343. t[i] = book->valuelist+entry[i]*book->dim;
  344. }
  345. switch(addmul){
  346. case -1:
  347. for(i=0,o=0;i<book->dim;i++,o+=step)
  348. for (j=0;j<step;j++)
  349. a[o+j]=t[j][i];
  350. break;
  351. case 0:
  352. for(i=0,o=0;i<book->dim;i++,o+=step)
  353. for (j=0;j<step;j++)
  354. a[o+j]+=t[j][i];
  355. break;
  356. case 1:
  357. for(i=0,o=0;i<book->dim;i++,o+=step)
  358. for (j=0;j<step;j++)
  359. a[o+j]*=t[j][i];
  360. break;
  361. }
  362. return(0);
  363. }
  364. #ifdef _V_SELFTEST
  365. /* Simple enough; pack a few candidate codebooks, unpack them. Code a
  366. number of vectors through (keeping track of the quantized values),
  367. and decode using the unpacked book. quantized version of in should
  368. exactly equal out */
  369. #include <stdio.h>
  370. #include "vorbis/book/lsp20_0.vqh"
  371. #include "vorbis/book/res0a_13.vqh"
  372. #define TESTSIZE 40
  373. float test1[TESTSIZE]={
  374. 0.105939,
  375. 0.215373,
  376. 0.429117,
  377. 0.587974,
  378. 0.181173,
  379. 0.296583,
  380. 0.515707,
  381. 0.715261,
  382. 0.162327,
  383. 0.263834,
  384. 0.342876,
  385. 0.406025,
  386. 0.103571,
  387. 0.223561,
  388. 0.368513,
  389. 0.540313,
  390. 0.136672,
  391. 0.395882,
  392. 0.587183,
  393. 0.652476,
  394. 0.114338,
  395. 0.417300,
  396. 0.525486,
  397. 0.698679,
  398. 0.147492,
  399. 0.324481,
  400. 0.643089,
  401. 0.757582,
  402. 0.139556,
  403. 0.215795,
  404. 0.324559,
  405. 0.399387,
  406. 0.120236,
  407. 0.267420,
  408. 0.446940,
  409. 0.608760,
  410. 0.115587,
  411. 0.287234,
  412. 0.571081,
  413. 0.708603,
  414. };
  415. float test3[TESTSIZE]={
  416. 0,1,-2,3,4,-5,6,7,8,9,
  417. 8,-2,7,-1,4,6,8,3,1,-9,
  418. 10,11,12,13,14,15,26,17,18,19,
  419. 30,-25,-30,-1,-5,-32,4,3,-2,0};
  420. static_codebook *testlist[]={&_vq_book_lsp20_0,
  421. &_vq_book_res0a_13,NULL};
  422. float *testvec[]={test1,test3};
  423. int main(){
  424. oggpack_buffer write;
  425. oggpack_buffer read;
  426. long ptr=0,i;
  427. oggpack_writeinit(&write);
  428. fprintf(stderr,"Testing codebook abstraction...:\n");
  429. while(testlist[ptr]){
  430. codebook c;
  431. static_codebook s;
  432. float *qv=alloca(sizeof(float)*TESTSIZE);
  433. float *iv=alloca(sizeof(float)*TESTSIZE);
  434. memcpy(qv,testvec[ptr],sizeof(float)*TESTSIZE);
  435. memset(iv,0,sizeof(float)*TESTSIZE);
  436. fprintf(stderr,"\tpacking/coding %ld... ",ptr);
  437. /* pack the codebook, write the testvector */
  438. oggpack_reset(&write);
  439. vorbis_book_init_encode(&c,testlist[ptr]); /* get it into memory
  440. we can write */
  441. vorbis_staticbook_pack(testlist[ptr],&write);
  442. fprintf(stderr,"Codebook size %ld bytes... ",oggpack_bytes(&write));
  443. for(i=0;i<TESTSIZE;i+=c.dim){
  444. int best=_best(&c,qv+i,1);
  445. vorbis_book_encodev(&c,best,qv+i,&write);
  446. }
  447. vorbis_book_clear(&c);
  448. fprintf(stderr,"OK.\n");
  449. fprintf(stderr,"\tunpacking/decoding %ld... ",ptr);
  450. /* transfer the write data to a read buffer and unpack/read */
  451. oggpack_readinit(&read,oggpack_get_buffer(&write),oggpack_bytes(&write));
  452. if(vorbis_staticbook_unpack(&read,&s)){
  453. fprintf(stderr,"Error unpacking codebook.\n");
  454. exit(1);
  455. }
  456. if(vorbis_book_init_decode(&c,&s)){
  457. fprintf(stderr,"Error initializing codebook.\n");
  458. exit(1);
  459. }
  460. for(i=0;i<TESTSIZE;i+=c.dim)
  461. if(vorbis_book_decodevs(&c,iv+i,&read,1,-1)==-1){
  462. fprintf(stderr,"Error reading codebook test data (EOP).\n");
  463. exit(1);
  464. }
  465. for(i=0;i<TESTSIZE;i++)
  466. if(fabs(qv[i]-iv[i])>.000001){
  467. fprintf(stderr,"read (%g) != written (%g) at position (%ld)\n",
  468. iv[i],qv[i],i);
  469. exit(1);
  470. }
  471. fprintf(stderr,"OK\n");
  472. ptr++;
  473. }
  474. /* The above is the trivial stuff; now try unquantizing a log scale codebook */
  475. exit(0);
  476. }
  477. #endif