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