nuclear@1: /******************************************************************** nuclear@1: * * nuclear@1: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * nuclear@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * nuclear@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * nuclear@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * nuclear@1: * * nuclear@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * nuclear@1: * by the Xiph.Org Foundation http://www.xiph.org/ * nuclear@1: * * nuclear@1: ******************************************************************** nuclear@1: nuclear@1: function: basic codebook pack/unpack/code/decode operations nuclear@1: last mod: $Id: codebook.c 18183 2012-02-03 20:51:27Z xiphmont $ nuclear@1: nuclear@1: ********************************************************************/ nuclear@1: nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include "vorbis/codec.h" nuclear@1: #include "codebook.h" nuclear@1: #include "scales.h" nuclear@1: #include "misc.h" nuclear@1: #include "os.h" nuclear@1: nuclear@1: /* packs the given codebook into the bitstream **************************/ nuclear@1: nuclear@1: int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){ nuclear@1: long i,j; nuclear@1: int ordered=0; nuclear@1: nuclear@1: /* first the basic parameters */ nuclear@1: oggpack_write(opb,0x564342,24); nuclear@1: oggpack_write(opb,c->dim,16); nuclear@1: oggpack_write(opb,c->entries,24); nuclear@1: nuclear@1: /* pack the codewords. There are two packings; length ordered and nuclear@1: length random. Decide between the two now. */ nuclear@1: nuclear@1: for(i=1;ientries;i++) nuclear@1: if(c->lengthlist[i-1]==0 || c->lengthlist[i]lengthlist[i-1])break; nuclear@1: if(i==c->entries)ordered=1; nuclear@1: nuclear@1: if(ordered){ nuclear@1: /* length ordered. We only need to say how many codewords of nuclear@1: each length. The actual codewords are generated nuclear@1: deterministically */ nuclear@1: nuclear@1: long count=0; nuclear@1: oggpack_write(opb,1,1); /* ordered */ nuclear@1: oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */ nuclear@1: nuclear@1: for(i=1;ientries;i++){ nuclear@1: long this=c->lengthlist[i]; nuclear@1: long last=c->lengthlist[i-1]; nuclear@1: if(this>last){ nuclear@1: for(j=last;jentries-count)); nuclear@1: count=i; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: oggpack_write(opb,i-count,_ilog(c->entries-count)); nuclear@1: nuclear@1: }else{ nuclear@1: /* length random. Again, we don't code the codeword itself, just nuclear@1: the length. This time, though, we have to encode each length */ nuclear@1: oggpack_write(opb,0,1); /* unordered */ nuclear@1: nuclear@1: /* algortihmic mapping has use for 'unused entries', which we tag nuclear@1: here. The algorithmic mapping happens as usual, but the unused nuclear@1: entry has no codeword. */ nuclear@1: for(i=0;ientries;i++) nuclear@1: if(c->lengthlist[i]==0)break; nuclear@1: nuclear@1: if(i==c->entries){ nuclear@1: oggpack_write(opb,0,1); /* no unused entries */ nuclear@1: for(i=0;ientries;i++) nuclear@1: oggpack_write(opb,c->lengthlist[i]-1,5); nuclear@1: }else{ nuclear@1: oggpack_write(opb,1,1); /* we have unused entries; thus we tag */ nuclear@1: for(i=0;ientries;i++){ nuclear@1: if(c->lengthlist[i]==0){ nuclear@1: oggpack_write(opb,0,1); nuclear@1: }else{ nuclear@1: oggpack_write(opb,1,1); nuclear@1: oggpack_write(opb,c->lengthlist[i]-1,5); nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* is the entry number the desired return value, or do we have a nuclear@1: mapping? If we have a mapping, what type? */ nuclear@1: oggpack_write(opb,c->maptype,4); nuclear@1: switch(c->maptype){ nuclear@1: case 0: nuclear@1: /* no mapping */ nuclear@1: break; nuclear@1: case 1:case 2: nuclear@1: /* implicitly populated value mapping */ nuclear@1: /* explicitly populated value mapping */ nuclear@1: nuclear@1: if(!c->quantlist){ nuclear@1: /* no quantlist? error */ nuclear@1: return(-1); nuclear@1: } nuclear@1: nuclear@1: /* values that define the dequantization */ nuclear@1: oggpack_write(opb,c->q_min,32); nuclear@1: oggpack_write(opb,c->q_delta,32); nuclear@1: oggpack_write(opb,c->q_quant-1,4); nuclear@1: oggpack_write(opb,c->q_sequencep,1); nuclear@1: nuclear@1: { nuclear@1: int quantvals; nuclear@1: switch(c->maptype){ nuclear@1: case 1: nuclear@1: /* a single column of (c->entries/c->dim) quantized values for nuclear@1: building a full value list algorithmically (square lattice) */ nuclear@1: quantvals=_book_maptype1_quantvals(c); nuclear@1: break; nuclear@1: case 2: nuclear@1: /* every value (c->entries*c->dim total) specified explicitly */ nuclear@1: quantvals=c->entries*c->dim; nuclear@1: break; nuclear@1: default: /* NOT_REACHABLE */ nuclear@1: quantvals=-1; nuclear@1: } nuclear@1: nuclear@1: /* quantized values */ nuclear@1: for(i=0;iquantlist[i]),c->q_quant); nuclear@1: nuclear@1: } nuclear@1: break; nuclear@1: default: nuclear@1: /* error case; we don't have any other map types now */ nuclear@1: return(-1); nuclear@1: } nuclear@1: nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: /* unpacks a codebook from the packet buffer into the codebook struct, nuclear@1: readies the codebook auxiliary structures for decode *************/ nuclear@1: static_codebook *vorbis_staticbook_unpack(oggpack_buffer *opb){ nuclear@1: long i,j; nuclear@1: static_codebook *s=_ogg_calloc(1,sizeof(*s)); nuclear@1: s->allocedp=1; nuclear@1: nuclear@1: /* make sure alignment is correct */ nuclear@1: if(oggpack_read(opb,24)!=0x564342)goto _eofout; nuclear@1: nuclear@1: /* first the basic parameters */ nuclear@1: s->dim=oggpack_read(opb,16); nuclear@1: s->entries=oggpack_read(opb,24); nuclear@1: if(s->entries==-1)goto _eofout; nuclear@1: nuclear@1: if(_ilog(s->dim)+_ilog(s->entries)>24)goto _eofout; nuclear@1: nuclear@1: /* codeword ordering.... length ordered or unordered? */ nuclear@1: switch((int)oggpack_read(opb,1)){ nuclear@1: case 0:{ nuclear@1: long unused; nuclear@1: /* allocated but unused entries? */ nuclear@1: unused=oggpack_read(opb,1); nuclear@1: if((s->entries*(unused?1:5)+7)>>3>opb->storage-oggpack_bytes(opb)) nuclear@1: goto _eofout; nuclear@1: /* unordered */ nuclear@1: s->lengthlist=_ogg_malloc(sizeof(*s->lengthlist)*s->entries); nuclear@1: nuclear@1: /* allocated but unused entries? */ nuclear@1: if(unused){ nuclear@1: /* yes, unused entries */ nuclear@1: nuclear@1: for(i=0;ientries;i++){ nuclear@1: if(oggpack_read(opb,1)){ nuclear@1: long num=oggpack_read(opb,5); nuclear@1: if(num==-1)goto _eofout; nuclear@1: s->lengthlist[i]=num+1; nuclear@1: }else nuclear@1: s->lengthlist[i]=0; nuclear@1: } nuclear@1: }else{ nuclear@1: /* all entries used; no tagging */ nuclear@1: for(i=0;ientries;i++){ nuclear@1: long num=oggpack_read(opb,5); nuclear@1: if(num==-1)goto _eofout; nuclear@1: s->lengthlist[i]=num+1; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: break; nuclear@1: } nuclear@1: case 1: nuclear@1: /* ordered */ nuclear@1: { nuclear@1: long length=oggpack_read(opb,5)+1; nuclear@1: if(length==0)goto _eofout; nuclear@1: s->lengthlist=_ogg_malloc(sizeof(*s->lengthlist)*s->entries); nuclear@1: nuclear@1: for(i=0;ientries;){ nuclear@1: long num=oggpack_read(opb,_ilog(s->entries-i)); nuclear@1: if(num==-1)goto _eofout; nuclear@1: if(length>32 || num>s->entries-i || nuclear@1: (num>0 && (num-1)>>(length-1)>1)){ nuclear@1: goto _errout; nuclear@1: } nuclear@1: if(length>32)goto _errout; nuclear@1: for(j=0;jlengthlist[i]=length; nuclear@1: length++; nuclear@1: } nuclear@1: } nuclear@1: break; nuclear@1: default: nuclear@1: /* EOF */ nuclear@1: goto _eofout; nuclear@1: } nuclear@1: nuclear@1: /* Do we have a mapping to unpack? */ nuclear@1: switch((s->maptype=oggpack_read(opb,4))){ nuclear@1: case 0: nuclear@1: /* no mapping */ nuclear@1: break; nuclear@1: case 1: case 2: nuclear@1: /* implicitly populated value mapping */ nuclear@1: /* explicitly populated value mapping */ nuclear@1: nuclear@1: s->q_min=oggpack_read(opb,32); nuclear@1: s->q_delta=oggpack_read(opb,32); nuclear@1: s->q_quant=oggpack_read(opb,4)+1; nuclear@1: s->q_sequencep=oggpack_read(opb,1); nuclear@1: if(s->q_sequencep==-1)goto _eofout; nuclear@1: nuclear@1: { nuclear@1: int quantvals=0; nuclear@1: switch(s->maptype){ nuclear@1: case 1: nuclear@1: quantvals=(s->dim==0?0:_book_maptype1_quantvals(s)); nuclear@1: break; nuclear@1: case 2: nuclear@1: quantvals=s->entries*s->dim; nuclear@1: break; nuclear@1: } nuclear@1: nuclear@1: /* quantized values */ nuclear@1: if(((quantvals*s->q_quant+7)>>3)>opb->storage-oggpack_bytes(opb)) nuclear@1: goto _eofout; nuclear@1: s->quantlist=_ogg_malloc(sizeof(*s->quantlist)*quantvals); nuclear@1: for(i=0;iquantlist[i]=oggpack_read(opb,s->q_quant); nuclear@1: nuclear@1: if(quantvals&&s->quantlist[quantvals-1]==-1)goto _eofout; nuclear@1: } nuclear@1: break; nuclear@1: default: nuclear@1: goto _errout; nuclear@1: } nuclear@1: nuclear@1: /* all set */ nuclear@1: return(s); nuclear@1: nuclear@1: _errout: nuclear@1: _eofout: nuclear@1: vorbis_staticbook_destroy(s); nuclear@1: return(NULL); nuclear@1: } nuclear@1: nuclear@1: /* returns the number of bits ************************************************/ nuclear@1: int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){ nuclear@1: if(a<0 || a>=book->c->entries)return(0); nuclear@1: oggpack_write(b,book->codelist[a],book->c->lengthlist[a]); nuclear@1: return(book->c->lengthlist[a]); nuclear@1: } nuclear@1: nuclear@1: /* the 'eliminate the decode tree' optimization actually requires the nuclear@1: codewords to be MSb first, not LSb. This is an annoying inelegancy nuclear@1: (and one of the first places where carefully thought out design nuclear@1: turned out to be wrong; Vorbis II and future Ogg codecs should go nuclear@1: to an MSb bitpacker), but not actually the huge hit it appears to nuclear@1: be. The first-stage decode table catches most words so that nuclear@1: bitreverse is not in the main execution path. */ nuclear@1: nuclear@1: static ogg_uint32_t bitreverse(ogg_uint32_t x){ nuclear@1: x= ((x>>16)&0x0000ffff) | ((x<<16)&0xffff0000); nuclear@1: x= ((x>> 8)&0x00ff00ff) | ((x<< 8)&0xff00ff00); nuclear@1: x= ((x>> 4)&0x0f0f0f0f) | ((x<< 4)&0xf0f0f0f0); nuclear@1: x= ((x>> 2)&0x33333333) | ((x<< 2)&0xcccccccc); nuclear@1: return((x>> 1)&0x55555555) | ((x<< 1)&0xaaaaaaaa); nuclear@1: } nuclear@1: nuclear@1: STIN long decode_packed_entry_number(codebook *book, oggpack_buffer *b){ nuclear@1: int read=book->dec_maxlength; nuclear@1: long lo,hi; nuclear@1: long lok = oggpack_look(b,book->dec_firsttablen); nuclear@1: nuclear@1: if (lok >= 0) { nuclear@1: long entry = book->dec_firsttable[lok]; nuclear@1: if(entry&0x80000000UL){ nuclear@1: lo=(entry>>15)&0x7fff; nuclear@1: hi=book->used_entries-(entry&0x7fff); nuclear@1: }else{ nuclear@1: oggpack_adv(b, book->dec_codelengths[entry-1]); nuclear@1: return(entry-1); nuclear@1: } nuclear@1: }else{ nuclear@1: lo=0; nuclear@1: hi=book->used_entries; nuclear@1: } nuclear@1: nuclear@1: lok = oggpack_look(b, read); nuclear@1: nuclear@1: while(lok<0 && read>1) nuclear@1: lok = oggpack_look(b, --read); nuclear@1: if(lok<0)return -1; nuclear@1: nuclear@1: /* bisect search for the codeword in the ordered list */ nuclear@1: { nuclear@1: ogg_uint32_t testword=bitreverse((ogg_uint32_t)lok); nuclear@1: nuclear@1: while(hi-lo>1){ nuclear@1: long p=(hi-lo)>>1; nuclear@1: long test=book->codelist[lo+p]>testword; nuclear@1: lo+=p&(test-1); nuclear@1: hi-=p&(-test); nuclear@1: } nuclear@1: nuclear@1: if(book->dec_codelengths[lo]<=read){ nuclear@1: oggpack_adv(b, book->dec_codelengths[lo]); nuclear@1: return(lo); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: oggpack_adv(b, read); nuclear@1: nuclear@1: return(-1); nuclear@1: } nuclear@1: nuclear@1: /* Decode side is specced and easier, because we don't need to find nuclear@1: matches using different criteria; we simply read and map. There are nuclear@1: two things we need to do 'depending': nuclear@1: nuclear@1: We may need to support interleave. We don't really, but it's nuclear@1: convenient to do it here rather than rebuild the vector later. nuclear@1: nuclear@1: Cascades may be additive or multiplicitive; this is not inherent in nuclear@1: the codebook, but set in the code using the codebook. Like nuclear@1: interleaving, it's easiest to do it here. nuclear@1: addmul==0 -> declarative (set the value) nuclear@1: addmul==1 -> additive nuclear@1: addmul==2 -> multiplicitive */ nuclear@1: nuclear@1: /* returns the [original, not compacted] entry number or -1 on eof *********/ nuclear@1: long vorbis_book_decode(codebook *book, oggpack_buffer *b){ nuclear@1: if(book->used_entries>0){ nuclear@1: long packed_entry=decode_packed_entry_number(book,b); nuclear@1: if(packed_entry>=0) nuclear@1: return(book->dec_index[packed_entry]); nuclear@1: } nuclear@1: nuclear@1: /* if there's no dec_index, the codebook unpacking isn't collapsed */ nuclear@1: return(-1); nuclear@1: } nuclear@1: nuclear@1: /* returns 0 on OK or -1 on eof *************************************/ nuclear@1: /* decode vector / dim granularity gaurding is done in the upper layer */ nuclear@1: long vorbis_book_decodevs_add(codebook *book,float *a,oggpack_buffer *b,int n){ nuclear@1: if(book->used_entries>0){ nuclear@1: int step=n/book->dim; nuclear@1: long *entry = alloca(sizeof(*entry)*step); nuclear@1: float **t = alloca(sizeof(*t)*step); nuclear@1: int i,j,o; nuclear@1: nuclear@1: for (i = 0; i < step; i++) { nuclear@1: entry[i]=decode_packed_entry_number(book,b); nuclear@1: if(entry[i]==-1)return(-1); nuclear@1: t[i] = book->valuelist+entry[i]*book->dim; nuclear@1: } nuclear@1: for(i=0,o=0;idim;i++,o+=step) nuclear@1: for (j=0;jused_entries>0){ nuclear@1: int i,j,entry; nuclear@1: float *t; nuclear@1: nuclear@1: if(book->dim>8){ nuclear@1: for(i=0;ivaluelist+entry*book->dim; nuclear@1: for (j=0;jdim;) nuclear@1: a[i++]+=t[j++]; nuclear@1: } nuclear@1: }else{ nuclear@1: for(i=0;ivaluelist+entry*book->dim; nuclear@1: j=0; nuclear@1: switch((int)book->dim){ nuclear@1: case 8: nuclear@1: a[i++]+=t[j++]; nuclear@1: case 7: nuclear@1: a[i++]+=t[j++]; nuclear@1: case 6: nuclear@1: a[i++]+=t[j++]; nuclear@1: case 5: nuclear@1: a[i++]+=t[j++]; nuclear@1: case 4: nuclear@1: a[i++]+=t[j++]; nuclear@1: case 3: nuclear@1: a[i++]+=t[j++]; nuclear@1: case 2: nuclear@1: a[i++]+=t[j++]; nuclear@1: case 1: nuclear@1: a[i++]+=t[j++]; nuclear@1: case 0: nuclear@1: break; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: /* unlike the others, we guard against n not being an integer number nuclear@1: of internally rather than in the upper layer (called only by nuclear@1: floor0) */ nuclear@1: long vorbis_book_decodev_set(codebook *book,float *a,oggpack_buffer *b,int n){ nuclear@1: if(book->used_entries>0){ nuclear@1: int i,j,entry; nuclear@1: float *t; nuclear@1: nuclear@1: for(i=0;ivaluelist+entry*book->dim; nuclear@1: for (j=0;idim;){ nuclear@1: a[i++]=t[j++]; nuclear@1: } nuclear@1: } nuclear@1: }else{ nuclear@1: int i; nuclear@1: nuclear@1: for(i=0;iused_entries>0){ nuclear@1: for(i=offset/ch;i<(offset+n)/ch;){ nuclear@1: entry = decode_packed_entry_number(book,b); nuclear@1: if(entry==-1)return(-1); nuclear@1: { nuclear@1: const float *t = book->valuelist+entry*book->dim; nuclear@1: for (j=0;jdim;j++){ nuclear@1: a[chptr++][i]+=t[j]; nuclear@1: if(chptr==ch){ nuclear@1: chptr=0; nuclear@1: i++; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: return(0); nuclear@1: }