codebook.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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-2007 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: basic codebook pack/unpack/code/decode operations
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include <ogg/ogg.h>
  19. #include "vorbis/codec.h"
  20. #include "codebook.h"
  21. #include "scales.h"
  22. #include "misc.h"
  23. #include "os.h"
  24. /* packs the given codebook into the bitstream **************************/
  25. int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){
  26. long i,j;
  27. int ordered=0;
  28. /* first the basic parameters */
  29. oggpack_write(opb,0x564342,24);
  30. oggpack_write(opb,c->dim,16);
  31. oggpack_write(opb,c->entries,24);
  32. /* pack the codewords. There are two packings; length ordered and
  33. length random. Decide between the two now. */
  34. for(i=1;i<c->entries;i++)
  35. if(c->lengthlist[i-1]==0 || c->lengthlist[i]<c->lengthlist[i-1])break;
  36. if(i==c->entries)ordered=1;
  37. if(ordered){
  38. /* length ordered. We only need to say how many codewords of
  39. each length. The actual codewords are generated
  40. deterministically */
  41. long count=0;
  42. oggpack_write(opb,1,1); /* ordered */
  43. oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */
  44. for(i=1;i<c->entries;i++){
  45. long this=c->lengthlist[i];
  46. long last=c->lengthlist[i-1];
  47. if(this>last){
  48. for(j=last;j<this;j++){
  49. oggpack_write(opb,i-count,_ilog(c->entries-count));
  50. count=i;
  51. }
  52. }
  53. }
  54. oggpack_write(opb,i-count,_ilog(c->entries-count));
  55. }else{
  56. /* length random. Again, we don't code the codeword itself, just
  57. the length. This time, though, we have to encode each length */
  58. oggpack_write(opb,0,1); /* unordered */
  59. /* algortihmic mapping has use for 'unused entries', which we tag
  60. here. The algorithmic mapping happens as usual, but the unused
  61. entry has no codeword. */
  62. for(i=0;i<c->entries;i++)
  63. if(c->lengthlist[i]==0)break;
  64. if(i==c->entries){
  65. oggpack_write(opb,0,1); /* no unused entries */
  66. for(i=0;i<c->entries;i++)
  67. oggpack_write(opb,c->lengthlist[i]-1,5);
  68. }else{
  69. oggpack_write(opb,1,1); /* we have unused entries; thus we tag */
  70. for(i=0;i<c->entries;i++){
  71. if(c->lengthlist[i]==0){
  72. oggpack_write(opb,0,1);
  73. }else{
  74. oggpack_write(opb,1,1);
  75. oggpack_write(opb,c->lengthlist[i]-1,5);
  76. }
  77. }
  78. }
  79. }
  80. /* is the entry number the desired return value, or do we have a
  81. mapping? If we have a mapping, what type? */
  82. oggpack_write(opb,c->maptype,4);
  83. switch(c->maptype){
  84. case 0:
  85. /* no mapping */
  86. break;
  87. case 1:case 2:
  88. /* implicitly populated value mapping */
  89. /* explicitly populated value mapping */
  90. if(!c->quantlist){
  91. /* no quantlist? error */
  92. return(-1);
  93. }
  94. /* values that define the dequantization */
  95. oggpack_write(opb,c->q_min,32);
  96. oggpack_write(opb,c->q_delta,32);
  97. oggpack_write(opb,c->q_quant-1,4);
  98. oggpack_write(opb,c->q_sequencep,1);
  99. {
  100. int quantvals;
  101. switch(c->maptype){
  102. case 1:
  103. /* a single column of (c->entries/c->dim) quantized values for
  104. building a full value list algorithmically (square lattice) */
  105. quantvals=_book_maptype1_quantvals(c);
  106. break;
  107. case 2:
  108. /* every value (c->entries*c->dim total) specified explicitly */
  109. quantvals=c->entries*c->dim;
  110. break;
  111. default: /* NOT_REACHABLE */
  112. quantvals=-1;
  113. }
  114. /* quantized values */
  115. for(i=0;i<quantvals;i++)
  116. oggpack_write(opb,labs(c->quantlist[i]),c->q_quant);
  117. }
  118. break;
  119. default:
  120. /* error case; we don't have any other map types now */
  121. return(-1);
  122. }
  123. return(0);
  124. }
  125. /* unpacks a codebook from the packet buffer into the codebook struct,
  126. readies the codebook auxiliary structures for decode *************/
  127. int vorbis_staticbook_unpack(oggpack_buffer *opb,static_codebook *s){
  128. long i,j;
  129. memset(s,0,sizeof(*s));
  130. s->allocedp=1;
  131. /* make sure alignment is correct */
  132. if(oggpack_read(opb,24)!=0x564342)goto _eofout;
  133. /* first the basic parameters */
  134. s->dim=oggpack_read(opb,16);
  135. s->entries=oggpack_read(opb,24);
  136. if(s->entries==-1)goto _eofout;
  137. if(_ilog(s->dim)+_ilog(s->entries)>24)goto _eofout;
  138. /* codeword ordering.... length ordered or unordered? */
  139. switch((int)oggpack_read(opb,1)){
  140. case 0:
  141. /* unordered */
  142. s->lengthlist=_ogg_malloc(sizeof(*s->lengthlist)*s->entries);
  143. /* allocated but unused entries? */
  144. if(oggpack_read(opb,1)){
  145. /* yes, unused entries */
  146. for(i=0;i<s->entries;i++){
  147. if(oggpack_read(opb,1)){
  148. long num=oggpack_read(opb,5);
  149. if(num==-1)goto _eofout;
  150. s->lengthlist[i]=num+1;
  151. }else
  152. s->lengthlist[i]=0;
  153. }
  154. }else{
  155. /* all entries used; no tagging */
  156. for(i=0;i<s->entries;i++){
  157. long num=oggpack_read(opb,5);
  158. if(num==-1)goto _eofout;
  159. s->lengthlist[i]=num+1;
  160. }
  161. }
  162. break;
  163. case 1:
  164. /* ordered */
  165. {
  166. long length=oggpack_read(opb,5)+1;
  167. s->lengthlist=_ogg_malloc(sizeof(*s->lengthlist)*s->entries);
  168. for(i=0;i<s->entries;){
  169. long num=oggpack_read(opb,_ilog(s->entries-i));
  170. if(num==-1)goto _eofout;
  171. for(j=0;j<num && i<s->entries;j++,i++)
  172. s->lengthlist[i]=length;
  173. length++;
  174. }
  175. }
  176. break;
  177. default:
  178. /* EOF */
  179. return(-1);
  180. }
  181. /* Do we have a mapping to unpack? */
  182. switch((s->maptype=oggpack_read(opb,4))){
  183. case 0:
  184. /* no mapping */
  185. break;
  186. case 1: case 2:
  187. /* implicitly populated value mapping */
  188. /* explicitly populated value mapping */
  189. s->q_min=oggpack_read(opb,32);
  190. s->q_delta=oggpack_read(opb,32);
  191. s->q_quant=oggpack_read(opb,4)+1;
  192. s->q_sequencep=oggpack_read(opb,1);
  193. {
  194. int quantvals=0;
  195. switch(s->maptype){
  196. case 1:
  197. quantvals=(s->dim==0?0:_book_maptype1_quantvals(s));
  198. break;
  199. case 2:
  200. quantvals=s->entries*s->dim;
  201. break;
  202. }
  203. /* quantized values */
  204. s->quantlist=_ogg_malloc(sizeof(*s->quantlist)*quantvals);
  205. for(i=0;i<quantvals;i++)
  206. s->quantlist[i]=oggpack_read(opb,s->q_quant);
  207. if(quantvals&&s->quantlist[quantvals-1]==-1)goto _eofout;
  208. }
  209. break;
  210. default:
  211. goto _errout;
  212. }
  213. /* all set */
  214. return(0);
  215. _errout:
  216. _eofout:
  217. vorbis_staticbook_clear(s);
  218. return(-1);
  219. }
  220. /* returns the number of bits ************************************************/
  221. int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){
  222. if(a<0 || a>=book->c->entries)return(0);
  223. oggpack_write(b,book->codelist[a],book->c->lengthlist[a]);
  224. return(book->c->lengthlist[a]);
  225. }
  226. /* One the encode side, our vector writers are each designed for a
  227. specific purpose, and the encoder is not flexible without modification:
  228. The LSP vector coder uses a single stage nearest-match with no
  229. interleave, so no step and no error return. This is specced by floor0
  230. and doesn't change.
  231. Residue0 encoding interleaves, uses multiple stages, and each stage
  232. peels of a specific amount of resolution from a lattice (thus we want
  233. to match by threshold, not nearest match). Residue doesn't *have* to
  234. be encoded that way, but to change it, one will need to add more
  235. infrastructure on the encode side (decode side is specced and simpler) */
  236. /* floor0 LSP (single stage, non interleaved, nearest match) */
  237. /* returns entry number and *modifies a* to the quantization value *****/
  238. int vorbis_book_errorv(codebook *book,float *a){
  239. int dim=book->dim,k;
  240. int best=_best(book,a,1);
  241. for(k=0;k<dim;k++)
  242. a[k]=(book->valuelist+best*dim)[k];
  243. return(best);
  244. }
  245. /* returns the number of bits and *modifies a* to the quantization value *****/
  246. int vorbis_book_encodev(codebook *book,int best,float *a,oggpack_buffer *b){
  247. int k,dim=book->dim;
  248. for(k=0;k<dim;k++)
  249. a[k]=(book->valuelist+best*dim)[k];
  250. return(vorbis_book_encode(book,best,b));
  251. }
  252. /* the 'eliminate the decode tree' optimization actually requires the
  253. codewords to be MSb first, not LSb. This is an annoying inelegancy
  254. (and one of the first places where carefully thought out design
  255. turned out to be wrong; Vorbis II and future Ogg codecs should go
  256. to an MSb bitpacker), but not actually the huge hit it appears to
  257. be. The first-stage decode table catches most words so that
  258. bitreverse is not in the main execution path. */
  259. static ogg_uint32_t bitreverse(ogg_uint32_t x){
  260. x= ((x>>16)&0x0000ffff) | ((x<<16)&0xffff0000);
  261. x= ((x>> 8)&0x00ff00ff) | ((x<< 8)&0xff00ff00);
  262. x= ((x>> 4)&0x0f0f0f0f) | ((x<< 4)&0xf0f0f0f0);
  263. x= ((x>> 2)&0x33333333) | ((x<< 2)&0xcccccccc);
  264. return((x>> 1)&0x55555555) | ((x<< 1)&0xaaaaaaaa);
  265. }
  266. STIN long decode_packed_entry_number(codebook *book, oggpack_buffer *b){
  267. int read=book->dec_maxlength;
  268. long lo,hi;
  269. long lok = oggpack_look(b,book->dec_firsttablen);
  270. if (lok >= 0) {
  271. long entry = book->dec_firsttable[lok];
  272. if(entry&0x80000000UL){
  273. lo=(entry>>15)&0x7fff;
  274. hi=book->used_entries-(entry&0x7fff);
  275. }else{
  276. oggpack_adv(b, book->dec_codelengths[entry-1]);
  277. return(entry-1);
  278. }
  279. }else{
  280. lo=0;
  281. hi=book->used_entries;
  282. }
  283. lok = oggpack_look(b, read);
  284. while(lok<0 && read>1)
  285. lok = oggpack_look(b, --read);
  286. if(lok<0)return -1;
  287. /* bisect search for the codeword in the ordered list */
  288. {
  289. ogg_uint32_t testword=bitreverse((ogg_uint32_t)lok);
  290. while(hi-lo>1){
  291. long p=(hi-lo)>>1;
  292. long test=book->codelist[lo+p]>testword;
  293. lo+=p&(test-1);
  294. hi-=p&(-test);
  295. }
  296. if(book->dec_codelengths[lo]<=read){
  297. oggpack_adv(b, book->dec_codelengths[lo]);
  298. return(lo);
  299. }
  300. }
  301. oggpack_adv(b, read);
  302. return(-1);
  303. }
  304. /* Decode side is specced and easier, because we don't need to find
  305. matches using different criteria; we simply read and map. There are
  306. two things we need to do 'depending':
  307. We may need to support interleave. We don't really, but it's
  308. convenient to do it here rather than rebuild the vector later.
  309. Cascades may be additive or multiplicitive; this is not inherent in
  310. the codebook, but set in the code using the codebook. Like
  311. interleaving, it's easiest to do it here.
  312. addmul==0 -> declarative (set the value)
  313. addmul==1 -> additive
  314. addmul==2 -> multiplicitive */
  315. /* returns the [original, not compacted] entry number or -1 on eof *********/
  316. long vorbis_book_decode(codebook *book, oggpack_buffer *b){
  317. if(book->used_entries>0){
  318. long packed_entry=decode_packed_entry_number(book,b);
  319. if(packed_entry>=0)
  320. return(book->dec_index[packed_entry]);
  321. }
  322. /* if there's no dec_index, the codebook unpacking isn't collapsed */
  323. return(-1);
  324. }
  325. /* returns 0 on OK or -1 on eof *************************************/
  326. long vorbis_book_decodevs_add(codebook *book,float *a,oggpack_buffer *b,int n){
  327. if(book->used_entries>0){
  328. int step=n/book->dim;
  329. long *entry = alloca(sizeof(*entry)*step);
  330. float **t = alloca(sizeof(*t)*step);
  331. int i,j,o;
  332. for (i = 0; i < step; i++) {
  333. entry[i]=decode_packed_entry_number(book,b);
  334. if(entry[i]==-1)return(-1);
  335. t[i] = book->valuelist+entry[i]*book->dim;
  336. }
  337. for(i=0,o=0;i<book->dim;i++,o+=step)
  338. for (j=0;j<step;j++)
  339. a[o+j]+=t[j][i];
  340. }
  341. return(0);
  342. }
  343. long vorbis_book_decodev_add(codebook *book,float *a,oggpack_buffer *b,int n){
  344. if(book->used_entries>0){
  345. int i,j,entry;
  346. float *t;
  347. if(book->dim>8){
  348. for(i=0;i<n;){
  349. entry = decode_packed_entry_number(book,b);
  350. if(entry==-1)return(-1);
  351. t = book->valuelist+entry*book->dim;
  352. for (j=0;j<book->dim;)
  353. a[i++]+=t[j++];
  354. }
  355. }else{
  356. for(i=0;i<n;){
  357. entry = decode_packed_entry_number(book,b);
  358. if(entry==-1)return(-1);
  359. t = book->valuelist+entry*book->dim;
  360. j=0;
  361. switch((int)book->dim){
  362. case 8:
  363. a[i++]+=t[j++];
  364. case 7:
  365. a[i++]+=t[j++];
  366. case 6:
  367. a[i++]+=t[j++];
  368. case 5:
  369. a[i++]+=t[j++];
  370. case 4:
  371. a[i++]+=t[j++];
  372. case 3:
  373. a[i++]+=t[j++];
  374. case 2:
  375. a[i++]+=t[j++];
  376. case 1:
  377. a[i++]+=t[j++];
  378. case 0:
  379. break;
  380. }
  381. }
  382. }
  383. }
  384. return(0);
  385. }
  386. long vorbis_book_decodev_set(codebook *book,float *a,oggpack_buffer *b,int n){
  387. if(book->used_entries>0){
  388. int i,j,entry;
  389. float *t;
  390. for(i=0;i<n;){
  391. entry = decode_packed_entry_number(book,b);
  392. if(entry==-1)return(-1);
  393. t = book->valuelist+entry*book->dim;
  394. for (j=0;j<book->dim;)
  395. a[i++]=t[j++];
  396. }
  397. }else{
  398. int i,j;
  399. for(i=0;i<n;){
  400. for (j=0;j<book->dim;)
  401. a[i++]=0.f;
  402. }
  403. }
  404. return(0);
  405. }
  406. long vorbis_book_decodevv_add(codebook *book,float **a,long offset,int ch,
  407. oggpack_buffer *b,int n){
  408. long i,j,entry;
  409. int chptr=0;
  410. if(book->used_entries>0){
  411. for(i=offset/ch;i<(offset+n)/ch;){
  412. entry = decode_packed_entry_number(book,b);
  413. if(entry==-1)return(-1);
  414. {
  415. const float *t = book->valuelist+entry*book->dim;
  416. for (j=0;j<book->dim;j++){
  417. a[chptr++][i]+=t[j];
  418. if(chptr==ch){
  419. chptr=0;
  420. i++;
  421. }
  422. }
  423. }
  424. }
  425. }
  426. return(0);
  427. }
  428. #ifdef _V_SELFTEST
  429. /* Simple enough; pack a few candidate codebooks, unpack them. Code a
  430. number of vectors through (keeping track of the quantized values),
  431. and decode using the unpacked book. quantized version of in should
  432. exactly equal out */
  433. #include <stdio.h>
  434. #include "vorbis/book/lsp20_0.vqh"
  435. #include "vorbis/book/res0a_13.vqh"
  436. #define TESTSIZE 40
  437. float test1[TESTSIZE]={
  438. 0.105939f,
  439. 0.215373f,
  440. 0.429117f,
  441. 0.587974f,
  442. 0.181173f,
  443. 0.296583f,
  444. 0.515707f,
  445. 0.715261f,
  446. 0.162327f,
  447. 0.263834f,
  448. 0.342876f,
  449. 0.406025f,
  450. 0.103571f,
  451. 0.223561f,
  452. 0.368513f,
  453. 0.540313f,
  454. 0.136672f,
  455. 0.395882f,
  456. 0.587183f,
  457. 0.652476f,
  458. 0.114338f,
  459. 0.417300f,
  460. 0.525486f,
  461. 0.698679f,
  462. 0.147492f,
  463. 0.324481f,
  464. 0.643089f,
  465. 0.757582f,
  466. 0.139556f,
  467. 0.215795f,
  468. 0.324559f,
  469. 0.399387f,
  470. 0.120236f,
  471. 0.267420f,
  472. 0.446940f,
  473. 0.608760f,
  474. 0.115587f,
  475. 0.287234f,
  476. 0.571081f,
  477. 0.708603f,
  478. };
  479. float test3[TESTSIZE]={
  480. 0,1,-2,3,4,-5,6,7,8,9,
  481. 8,-2,7,-1,4,6,8,3,1,-9,
  482. 10,11,12,13,14,15,26,17,18,19,
  483. 30,-25,-30,-1,-5,-32,4,3,-2,0};
  484. static_codebook *testlist[]={&_vq_book_lsp20_0,
  485. &_vq_book_res0a_13,NULL};
  486. float *testvec[]={test1,test3};
  487. int main(){
  488. oggpack_buffer write;
  489. oggpack_buffer read;
  490. long ptr=0,i;
  491. oggpack_writeinit(&write);
  492. fprintf(stderr,"Testing codebook abstraction...:\n");
  493. while(testlist[ptr]){
  494. codebook c;
  495. static_codebook s;
  496. float *qv=alloca(sizeof(*qv)*TESTSIZE);
  497. float *iv=alloca(sizeof(*iv)*TESTSIZE);
  498. memcpy(qv,testvec[ptr],sizeof(*qv)*TESTSIZE);
  499. memset(iv,0,sizeof(*iv)*TESTSIZE);
  500. fprintf(stderr,"\tpacking/coding %ld... ",ptr);
  501. /* pack the codebook, write the testvector */
  502. oggpack_reset(&write);
  503. vorbis_book_init_encode(&c,testlist[ptr]); /* get it into memory
  504. we can write */
  505. vorbis_staticbook_pack(testlist[ptr],&write);
  506. fprintf(stderr,"Codebook size %ld bytes... ",oggpack_bytes(&write));
  507. for(i=0;i<TESTSIZE;i+=c.dim){
  508. int best=_best(&c,qv+i,1);
  509. vorbis_book_encodev(&c,best,qv+i,&write);
  510. }
  511. vorbis_book_clear(&c);
  512. fprintf(stderr,"OK.\n");
  513. fprintf(stderr,"\tunpacking/decoding %ld... ",ptr);
  514. /* transfer the write data to a read buffer and unpack/read */
  515. oggpack_readinit(&read,oggpack_get_buffer(&write),oggpack_bytes(&write));
  516. if(vorbis_staticbook_unpack(&read,&s)){
  517. fprintf(stderr,"Error unpacking codebook.\n");
  518. exit(1);
  519. }
  520. if(vorbis_book_init_decode(&c,&s)){
  521. fprintf(stderr,"Error initializing codebook.\n");
  522. exit(1);
  523. }
  524. for(i=0;i<TESTSIZE;i+=c.dim)
  525. if(vorbis_book_decodev_set(&c,iv+i,&read,c.dim)==-1){
  526. fprintf(stderr,"Error reading codebook test data (EOP).\n");
  527. exit(1);
  528. }
  529. for(i=0;i<TESTSIZE;i++)
  530. if(fabs(qv[i]-iv[i])>.000001){
  531. fprintf(stderr,"read (%g) != written (%g) at position (%ld)\n",
  532. iv[i],qv[i],i);
  533. exit(1);
  534. }
  535. fprintf(stderr,"OK\n");
  536. ptr++;
  537. }
  538. /* The above is the trivial stuff; now try unquantizing a log scale codebook */
  539. exit(0);
  540. }
  541. #endif