Chris@1: /******************************************************************** Chris@1: * * Chris@1: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * Chris@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * Chris@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * Chris@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * Chris@1: * * Chris@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * Chris@1: * by the Xiph.Org Foundation http://www.xiph.org/ * Chris@1: * * Chris@1: ******************************************************************** Chris@1: Chris@1: function: basic shared codebook operations Chris@1: last mod: $Id: sharedbook.c 17030 2010-03-25 06:52:55Z xiphmont $ Chris@1: Chris@1: ********************************************************************/ Chris@1: Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: #include "os.h" Chris@1: #include "misc.h" Chris@1: #include "vorbis/codec.h" Chris@1: #include "codebook.h" Chris@1: #include "scales.h" Chris@1: Chris@1: /**** pack/unpack helpers ******************************************/ Chris@1: int _ilog(unsigned int v){ Chris@1: int ret=0; Chris@1: while(v){ Chris@1: ret++; Chris@1: v>>=1; Chris@1: } Chris@1: return(ret); Chris@1: } Chris@1: Chris@1: /* 32 bit float (not IEEE; nonnormalized mantissa + Chris@1: biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm Chris@1: Why not IEEE? It's just not that important here. */ Chris@1: Chris@1: #define VQ_FEXP 10 Chris@1: #define VQ_FMAN 21 Chris@1: #define VQ_FEXP_BIAS 768 /* bias toward values smaller than 1. */ Chris@1: Chris@1: /* doesn't currently guard under/overflow */ Chris@1: long _float32_pack(float val){ Chris@1: int sign=0; Chris@1: long exp; Chris@1: long mant; Chris@1: if(val<0){ Chris@1: sign=0x80000000; Chris@1: val= -val; Chris@1: } Chris@1: exp= floor(log(val)/log(2.f)+.001); //+epsilon Chris@1: mant=rint(ldexp(val,(VQ_FMAN-1)-exp)); Chris@1: exp=(exp+VQ_FEXP_BIAS)<>VQ_FMAN; Chris@1: if(sign)mant= -mant; Chris@1: return(ldexp(mant,exp-(VQ_FMAN-1)-VQ_FEXP_BIAS)); Chris@1: } Chris@1: Chris@1: /* given a list of word lengths, generate a list of codewords. Works Chris@1: for length ordered or unordered, always assigns the lowest valued Chris@1: codewords first. Extended to handle unused entries (length 0) */ Chris@1: ogg_uint32_t *_make_words(long *l,long n,long sparsecount){ Chris@1: long i,j,count=0; Chris@1: ogg_uint32_t marker[33]; Chris@1: ogg_uint32_t *r=_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r)); Chris@1: memset(marker,0,sizeof(marker)); Chris@1: Chris@1: for(i=0;i0){ Chris@1: ogg_uint32_t entry=marker[length]; Chris@1: Chris@1: /* when we claim a node for an entry, we also claim the nodes Chris@1: below it (pruning off the imagined tree that may have dangled Chris@1: from it) as well as blocking the use of any nodes directly Chris@1: above for leaves */ Chris@1: Chris@1: /* update ourself */ Chris@1: if(length<32 && (entry>>length)){ Chris@1: /* error condition; the lengths must specify an overpopulated tree */ Chris@1: _ogg_free(r); Chris@1: return(NULL); Chris@1: } Chris@1: r[count++]=entry; Chris@1: Chris@1: /* Look to see if the next shorter marker points to the node Chris@1: above. if so, update it and repeat. */ Chris@1: { Chris@1: for(j=length;j>0;j--){ Chris@1: Chris@1: if(marker[j]&1){ Chris@1: /* have to jump branches */ Chris@1: if(j==1) Chris@1: marker[1]++; Chris@1: else Chris@1: marker[j]=marker[j-1]<<1; Chris@1: break; /* invariant says next upper marker would already Chris@1: have been moved if it was on the same path */ Chris@1: } Chris@1: marker[j]++; Chris@1: } Chris@1: } Chris@1: Chris@1: /* prune the tree; the implicit invariant says all the longer Chris@1: markers were dangling from our just-taken node. Dangle them Chris@1: from our *new* node. */ Chris@1: for(j=length+1;j<33;j++) Chris@1: if((marker[j]>>1) == entry){ Chris@1: entry=marker[j]; Chris@1: marker[j]=marker[j-1]<<1; Chris@1: }else Chris@1: break; Chris@1: }else Chris@1: if(sparsecount==0)count++; Chris@1: } Chris@1: Chris@1: /* sanity check the huffman tree; an underpopulated tree must be Chris@1: rejected. The only exception is the one-node pseudo-nil tree, Chris@1: which appears to be underpopulated because the tree doesn't Chris@1: really exist; there's only one possible 'codeword' or zero bits, Chris@1: but the above tree-gen code doesn't mark that. */ Chris@1: if(sparsecount != 1){ Chris@1: for(i=1;i<33;i++) Chris@1: if(marker[i] & (0xffffffffUL>>(32-i))){ Chris@1: _ogg_free(r); Chris@1: return(NULL); Chris@1: } Chris@1: } Chris@1: Chris@1: /* bitreverse the words because our bitwise packer/unpacker is LSb Chris@1: endian */ Chris@1: for(i=0,count=0;i>j)&1; Chris@1: } Chris@1: Chris@1: if(sparsecount){ Chris@1: if(l[i]) Chris@1: r[count++]=temp; Chris@1: }else Chris@1: r[count++]=temp; Chris@1: } Chris@1: Chris@1: return(r); Chris@1: } Chris@1: Chris@1: /* there might be a straightforward one-line way to do the below Chris@1: that's portable and totally safe against roundoff, but I haven't Chris@1: thought of it. Therefore, we opt on the side of caution */ Chris@1: long _book_maptype1_quantvals(const static_codebook *b){ Chris@1: long vals=floor(pow((float)b->entries,1.f/b->dim)); Chris@1: Chris@1: /* the above *should* be reliable, but we'll not assume that FP is Chris@1: ever reliable when bitstream sync is at stake; verify via integer Chris@1: means that vals really is the greatest value of dim for which Chris@1: vals^b->bim <= b->entries */ Chris@1: /* treat the above as an initial guess */ Chris@1: while(1){ Chris@1: long acc=1; Chris@1: long acc1=1; Chris@1: int i; Chris@1: for(i=0;idim;i++){ Chris@1: acc*=vals; Chris@1: acc1*=vals+1; Chris@1: } Chris@1: if(acc<=b->entries && acc1>b->entries){ Chris@1: return(vals); Chris@1: }else{ Chris@1: if(acc>b->entries){ Chris@1: vals--; Chris@1: }else{ Chris@1: vals++; Chris@1: } Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* unpack the quantized list of values for encode/decode ***********/ Chris@1: /* we need to deal with two map types: in map type 1, the values are Chris@1: generated algorithmically (each column of the vector counts through Chris@1: the values in the quant vector). in map type 2, all the values came Chris@1: in in an explicit list. Both value lists must be unpacked */ Chris@1: float *_book_unquantize(const static_codebook *b,int n,int *sparsemap){ Chris@1: long j,k,count=0; Chris@1: if(b->maptype==1 || b->maptype==2){ Chris@1: int quantvals; Chris@1: float mindel=_float32_unpack(b->q_min); Chris@1: float delta=_float32_unpack(b->q_delta); Chris@1: float *r=_ogg_calloc(n*b->dim,sizeof(*r)); Chris@1: Chris@1: /* maptype 1 and 2 both use a quantized value vector, but Chris@1: different sizes */ Chris@1: switch(b->maptype){ Chris@1: case 1: Chris@1: /* most of the time, entries%dimensions == 0, but we need to be Chris@1: well defined. We define that the possible vales at each Chris@1: scalar is values == entries/dim. If entries%dim != 0, we'll Chris@1: have 'too few' values (values*dimentries;j++){ Chris@1: if((sparsemap && b->lengthlist[j]) || !sparsemap){ Chris@1: float last=0.f; Chris@1: int indexdiv=1; Chris@1: for(k=0;kdim;k++){ Chris@1: int index= (j/indexdiv)%quantvals; Chris@1: float val=b->quantlist[index]; Chris@1: val=fabs(val)*delta+mindel+last; Chris@1: if(b->q_sequencep)last=val; Chris@1: if(sparsemap) Chris@1: r[sparsemap[count]*b->dim+k]=val; Chris@1: else Chris@1: r[count*b->dim+k]=val; Chris@1: indexdiv*=quantvals; Chris@1: } Chris@1: count++; Chris@1: } Chris@1: Chris@1: } Chris@1: break; Chris@1: case 2: Chris@1: for(j=0;jentries;j++){ Chris@1: if((sparsemap && b->lengthlist[j]) || !sparsemap){ Chris@1: float last=0.f; Chris@1: Chris@1: for(k=0;kdim;k++){ Chris@1: float val=b->quantlist[j*b->dim+k]; Chris@1: val=fabs(val)*delta+mindel+last; Chris@1: if(b->q_sequencep)last=val; Chris@1: if(sparsemap) Chris@1: r[sparsemap[count]*b->dim+k]=val; Chris@1: else Chris@1: r[count*b->dim+k]=val; Chris@1: } Chris@1: count++; Chris@1: } Chris@1: } Chris@1: break; Chris@1: } Chris@1: Chris@1: return(r); Chris@1: } Chris@1: return(NULL); Chris@1: } Chris@1: Chris@1: void vorbis_staticbook_destroy(static_codebook *b){ Chris@1: if(b->allocedp){ Chris@1: if(b->quantlist)_ogg_free(b->quantlist); Chris@1: if(b->lengthlist)_ogg_free(b->lengthlist); Chris@1: memset(b,0,sizeof(*b)); Chris@1: _ogg_free(b); Chris@1: } /* otherwise, it is in static memory */ Chris@1: } Chris@1: Chris@1: void vorbis_book_clear(codebook *b){ Chris@1: /* static book is not cleared; we're likely called on the lookup and Chris@1: the static codebook belongs to the info struct */ Chris@1: if(b->valuelist)_ogg_free(b->valuelist); Chris@1: if(b->codelist)_ogg_free(b->codelist); Chris@1: Chris@1: if(b->dec_index)_ogg_free(b->dec_index); Chris@1: if(b->dec_codelengths)_ogg_free(b->dec_codelengths); Chris@1: if(b->dec_firsttable)_ogg_free(b->dec_firsttable); Chris@1: Chris@1: memset(b,0,sizeof(*b)); Chris@1: } Chris@1: Chris@1: int vorbis_book_init_encode(codebook *c,const static_codebook *s){ Chris@1: Chris@1: memset(c,0,sizeof(*c)); Chris@1: c->c=s; Chris@1: c->entries=s->entries; Chris@1: c->used_entries=s->entries; Chris@1: c->dim=s->dim; Chris@1: c->codelist=_make_words(s->lengthlist,s->entries,0); Chris@1: //c->valuelist=_book_unquantize(s,s->entries,NULL); Chris@1: c->quantvals=_book_maptype1_quantvals(s); Chris@1: c->minval=(int)rint(_float32_unpack(s->q_min)); Chris@1: c->delta=(int)rint(_float32_unpack(s->q_delta)); Chris@1: Chris@1: return(0); Chris@1: } Chris@1: Chris@1: static ogg_uint32_t bitreverse(ogg_uint32_t x){ Chris@1: x= ((x>>16)&0x0000ffffUL) | ((x<<16)&0xffff0000UL); Chris@1: x= ((x>> 8)&0x00ff00ffUL) | ((x<< 8)&0xff00ff00UL); Chris@1: x= ((x>> 4)&0x0f0f0f0fUL) | ((x<< 4)&0xf0f0f0f0UL); Chris@1: x= ((x>> 2)&0x33333333UL) | ((x<< 2)&0xccccccccUL); Chris@1: return((x>> 1)&0x55555555UL) | ((x<< 1)&0xaaaaaaaaUL); Chris@1: } Chris@1: Chris@1: static int sort32a(const void *a,const void *b){ Chris@1: return ( **(ogg_uint32_t **)a>**(ogg_uint32_t **)b)- Chris@1: ( **(ogg_uint32_t **)a<**(ogg_uint32_t **)b); Chris@1: } Chris@1: Chris@1: /* decode codebook arrangement is more heavily optimized than encode */ Chris@1: int vorbis_book_init_decode(codebook *c,const static_codebook *s){ Chris@1: int i,j,n=0,tabn; Chris@1: int *sortindex; Chris@1: memset(c,0,sizeof(*c)); Chris@1: Chris@1: /* count actually used entries */ Chris@1: for(i=0;ientries;i++) Chris@1: if(s->lengthlist[i]>0) Chris@1: n++; Chris@1: Chris@1: c->entries=s->entries; Chris@1: c->used_entries=n; Chris@1: c->dim=s->dim; Chris@1: Chris@1: if(n>0){ Chris@1: Chris@1: /* two different remappings go on here. Chris@1: Chris@1: First, we collapse the likely sparse codebook down only to Chris@1: actually represented values/words. This collapsing needs to be Chris@1: indexed as map-valueless books are used to encode original entry Chris@1: positions as integers. Chris@1: Chris@1: Second, we reorder all vectors, including the entry index above, Chris@1: by sorted bitreversed codeword to allow treeless decode. */ Chris@1: Chris@1: /* perform sort */ Chris@1: ogg_uint32_t *codes=_make_words(s->lengthlist,s->entries,c->used_entries); Chris@1: ogg_uint32_t **codep=alloca(sizeof(*codep)*n); Chris@1: Chris@1: if(codes==NULL)goto err_out; Chris@1: Chris@1: for(i=0;icodelist=_ogg_malloc(n*sizeof(*c->codelist)); Chris@1: /* the index is a reverse index */ Chris@1: for(i=0;icodelist[sortindex[i]]=codes[i]; Chris@1: _ogg_free(codes); Chris@1: Chris@1: Chris@1: c->valuelist=_book_unquantize(s,n,sortindex); Chris@1: c->dec_index=_ogg_malloc(n*sizeof(*c->dec_index)); Chris@1: Chris@1: for(n=0,i=0;ientries;i++) Chris@1: if(s->lengthlist[i]>0) Chris@1: c->dec_index[sortindex[n++]]=i; Chris@1: Chris@1: c->dec_codelengths=_ogg_malloc(n*sizeof(*c->dec_codelengths)); Chris@1: for(n=0,i=0;ientries;i++) Chris@1: if(s->lengthlist[i]>0) Chris@1: c->dec_codelengths[sortindex[n++]]=s->lengthlist[i]; Chris@1: Chris@1: c->dec_firsttablen=_ilog(c->used_entries)-4; /* this is magic */ Chris@1: if(c->dec_firsttablen<5)c->dec_firsttablen=5; Chris@1: if(c->dec_firsttablen>8)c->dec_firsttablen=8; Chris@1: Chris@1: tabn=1<dec_firsttablen; Chris@1: c->dec_firsttable=_ogg_calloc(tabn,sizeof(*c->dec_firsttable)); Chris@1: c->dec_maxlength=0; Chris@1: Chris@1: for(i=0;idec_maxlengthdec_codelengths[i]) Chris@1: c->dec_maxlength=c->dec_codelengths[i]; Chris@1: if(c->dec_codelengths[i]<=c->dec_firsttablen){ Chris@1: ogg_uint32_t orig=bitreverse(c->codelist[i]); Chris@1: for(j=0;j<(1<<(c->dec_firsttablen-c->dec_codelengths[i]));j++) Chris@1: c->dec_firsttable[orig|(j<dec_codelengths[i])]=i+1; Chris@1: } Chris@1: } Chris@1: Chris@1: /* now fill in 'unused' entries in the firsttable with hi/lo search Chris@1: hints for the non-direct-hits */ Chris@1: { Chris@1: ogg_uint32_t mask=0xfffffffeUL<<(31-c->dec_firsttablen); Chris@1: long lo=0,hi=0; Chris@1: Chris@1: for(i=0;idec_firsttablen); Chris@1: if(c->dec_firsttable[bitreverse(word)]==0){ Chris@1: while((lo+1)codelist[lo+1]<=word)lo++; Chris@1: while( hi=(c->codelist[hi]&mask))hi++; Chris@1: Chris@1: /* we only actually have 15 bits per hint to play with here. Chris@1: In order to overflow gracefully (nothing breaks, efficiency Chris@1: just drops), encode as the difference from the extremes. */ Chris@1: { Chris@1: unsigned long loval=lo; Chris@1: unsigned long hival=n-hi; Chris@1: Chris@1: if(loval>0x7fff)loval=0x7fff; Chris@1: if(hival>0x7fff)hival=0x7fff; Chris@1: c->dec_firsttable[bitreverse(word)]= Chris@1: 0x80000000UL | (loval<<15) | hival; Chris@1: } Chris@1: } Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: return(0); Chris@1: err_out: Chris@1: vorbis_book_clear(c); Chris@1: return(-1); Chris@1: } Chris@1: Chris@1: long vorbis_book_codeword(codebook *book,int entry){ Chris@1: if(book->c) /* only use with encode; decode optimizations are Chris@1: allowed to break this */ Chris@1: return book->codelist[entry]; Chris@1: return -1; Chris@1: } Chris@1: Chris@1: long vorbis_book_codelen(codebook *book,int entry){ Chris@1: if(book->c) /* only use with encode; decode optimizations are Chris@1: allowed to break this */ Chris@1: return book->c->lengthlist[entry]; Chris@1: return -1; Chris@1: } Chris@1: Chris@1: #ifdef _V_SELFTEST Chris@1: Chris@1: /* Unit tests of the dequantizer; this stuff will be OK Chris@1: cross-platform, I simply want to be sure that special mapping cases Chris@1: actually work properly; a bug could go unnoticed for a while */ Chris@1: Chris@1: #include Chris@1: Chris@1: /* cases: Chris@1: Chris@1: no mapping Chris@1: full, explicit mapping Chris@1: algorithmic mapping Chris@1: Chris@1: nonsequential Chris@1: sequential Chris@1: */ Chris@1: Chris@1: static long full_quantlist1[]={0,1,2,3, 4,5,6,7, 8,3,6,1}; Chris@1: static long partial_quantlist1[]={0,7,2}; Chris@1: Chris@1: /* no mapping */ Chris@1: static_codebook test1={ Chris@1: 4,16, Chris@1: NULL, Chris@1: 0, Chris@1: 0,0,0,0, Chris@1: NULL, Chris@1: 0 Chris@1: }; Chris@1: static float *test1_result=NULL; Chris@1: Chris@1: /* linear, full mapping, nonsequential */ Chris@1: static_codebook test2={ Chris@1: 4,3, Chris@1: NULL, Chris@1: 2, Chris@1: -533200896,1611661312,4,0, Chris@1: full_quantlist1, Chris@1: 0 Chris@1: }; Chris@1: static float test2_result[]={-3,-2,-1,0, 1,2,3,4, 5,0,3,-2}; Chris@1: Chris@1: /* linear, full mapping, sequential */ Chris@1: static_codebook test3={ Chris@1: 4,3, Chris@1: NULL, Chris@1: 2, Chris@1: -533200896,1611661312,4,1, Chris@1: full_quantlist1, Chris@1: 0 Chris@1: }; Chris@1: static float test3_result[]={-3,-5,-6,-6, 1,3,6,10, 5,5,8,6}; Chris@1: Chris@1: /* linear, algorithmic mapping, nonsequential */ Chris@1: static_codebook test4={ Chris@1: 3,27, Chris@1: NULL, Chris@1: 1, Chris@1: -533200896,1611661312,4,0, Chris@1: partial_quantlist1, Chris@1: 0 Chris@1: }; Chris@1: static float test4_result[]={-3,-3,-3, 4,-3,-3, -1,-3,-3, Chris@1: -3, 4,-3, 4, 4,-3, -1, 4,-3, Chris@1: -3,-1,-3, 4,-1,-3, -1,-1,-3, Chris@1: -3,-3, 4, 4,-3, 4, -1,-3, 4, Chris@1: -3, 4, 4, 4, 4, 4, -1, 4, 4, Chris@1: -3,-1, 4, 4,-1, 4, -1,-1, 4, Chris@1: -3,-3,-1, 4,-3,-1, -1,-3,-1, Chris@1: -3, 4,-1, 4, 4,-1, -1, 4,-1, Chris@1: -3,-1,-1, 4,-1,-1, -1,-1,-1}; Chris@1: Chris@1: /* linear, algorithmic mapping, sequential */ Chris@1: static_codebook test5={ Chris@1: 3,27, Chris@1: NULL, Chris@1: 1, Chris@1: -533200896,1611661312,4,1, Chris@1: partial_quantlist1, Chris@1: 0 Chris@1: }; Chris@1: static float test5_result[]={-3,-6,-9, 4, 1,-2, -1,-4,-7, Chris@1: -3, 1,-2, 4, 8, 5, -1, 3, 0, Chris@1: -3,-4,-7, 4, 3, 0, -1,-2,-5, Chris@1: -3,-6,-2, 4, 1, 5, -1,-4, 0, Chris@1: -3, 1, 5, 4, 8,12, -1, 3, 7, Chris@1: -3,-4, 0, 4, 3, 7, -1,-2, 2, Chris@1: -3,-6,-7, 4, 1, 0, -1,-4,-5, Chris@1: -3, 1, 0, 4, 8, 7, -1, 3, 2, Chris@1: -3,-4,-5, 4, 3, 2, -1,-2,-3}; Chris@1: Chris@1: void run_test(static_codebook *b,float *comp){ Chris@1: float *out=_book_unquantize(b,b->entries,NULL); Chris@1: int i; Chris@1: Chris@1: if(comp){ Chris@1: if(!out){ Chris@1: fprintf(stderr,"_book_unquantize incorrectly returned NULL\n"); Chris@1: exit(1); Chris@1: } Chris@1: Chris@1: for(i=0;ientries*b->dim;i++) Chris@1: if(fabs(out[i]-comp[i])>.0001){ Chris@1: fprintf(stderr,"disagreement in unquantized and reference data:\n" Chris@1: "position %d, %g != %g\n",i,out[i],comp[i]); Chris@1: exit(1); Chris@1: } Chris@1: Chris@1: }else{ Chris@1: if(out){ Chris@1: fprintf(stderr,"_book_unquantize returned a value array: \n" Chris@1: " correct result should have been NULL\n"); Chris@1: exit(1); Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: int main(){ Chris@1: /* run the nine dequant tests, and compare to the hand-rolled results */ Chris@1: fprintf(stderr,"Dequant test 1... "); Chris@1: run_test(&test1,test1_result); Chris@1: fprintf(stderr,"OK\nDequant test 2... "); Chris@1: run_test(&test2,test2_result); Chris@1: fprintf(stderr,"OK\nDequant test 3... "); Chris@1: run_test(&test3,test3_result); Chris@1: fprintf(stderr,"OK\nDequant test 4... "); Chris@1: run_test(&test4,test4_result); Chris@1: fprintf(stderr,"OK\nDequant test 5... "); Chris@1: run_test(&test5,test5_result); Chris@1: fprintf(stderr,"OK\n\n"); Chris@1: Chris@1: return(0); Chris@1: } Chris@1: Chris@1: #endif