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