int ov_ilog(ogg_uint32_t v){ int ret; for(ret=0;v;ret++)v>>=1; return ret;
}
/* 32 bit float (not IEEE; nonnormalized mantissa + biasedexponent):neeeeeeeeeemmmmmmmmmmmmmmmmmmmmm
Why not IEEE? It's just not that important here. */
/* doesn't currently guard under/overflow */ long _float32_pack(float val){ int sign=0; long exp; long mant; if(val<0){
sign=0x80000000;
val= -val;
}
exp= floor(log(val)/log(2.f)+.001); /* +epsilon */
mant=rint(ldexp(val,(VQ_FMAN-1)-exp));
exp=(exp+VQ_FEXP_BIAS)<<VQ_FMAN;
return(sign|exp|mant);
}
float _float32_unpack(long val){ double mant=val&0x1fffff; int sign=val&0x80000000; long exp =(val&0x7fe00000L)>>VQ_FMAN; if(sign)mant= -mant;
exp=exp-(VQ_FMAN-1)-VQ_FEXP_BIAS; /* clamp excessive exponent values */ if (exp>63){
exp=63;
} if (exp<-63){
exp=-63;
} return(ldexp(mant,exp));
}
/* given a list of word lengths, generate a list of codewords. Works forlengthorderedorunordered,alwaysassignsthelowestvalued
codewords first. Extended to handle unused entries (length 0) */
ogg_uint32_t *_make_words(char *l,long n,long sparsecount){ long i,j,count=0;
ogg_uint32_t marker[33];
ogg_uint32_t *r=_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r));
memset(marker,0,sizeof(marker));
for(i=0;i<n;i++){ long length=l[i]; if(length>0){
ogg_uint32_t entry=marker[length];
/* when we claim a node for an entry, we also claim the nodes belowit(pruningofftheimaginedtreethatmayhavedangled fromit)aswellasblockingtheuseofanynodesdirectly
above for leaves */
/* update ourself */ if(length<32 && (entry>>length)){ /* error condition; the lengths must specify an overpopulated tree */
_ogg_free(r); return(NULL);
}
r[count++]=entry;
/* Look to see if the next shorter marker points to the node
above. if so, update it and repeat. */
{ for(j=length;j>0;j--){
if(marker[j]&1){ /* have to jump branches */ if(j==1)
marker[1]++; else
marker[j]=marker[j-1]<<1; break; /* invariant says next upper marker would already
have been moved if it was on the same path */
}
marker[j]++;
}
}
/* prune the tree; the implicit invariant says all the longer markersweredanglingfromourjust-takennode.Danglethem
from our *new* node. */ for(j=length+1;j<33;j++) if((marker[j]>>1) == entry){
entry=marker[j];
marker[j]=marker[j-1]<<1;
}else break;
}else if(sparsecount==0)count++;
}
/* any underpopulated tree must be rejected. */ /* Single-entry codebooks are a retconned extension to the spec. Theyhaveasinglecodeword'0'oflength1thatresultsinan
underpopulated tree. Shield that case from the underformed tree check. */ if(!(count==1 && marker[2]==2)){ for(i=1;i<33;i++) if(marker[i] & (0xffffffffUL>>(32-i))){
_ogg_free(r); return(NULL);
}
}
/* bitreverse the words because our bitwise packer/unpacker is LSb
endian */ for(i=0,count=0;i<n;i++){
ogg_uint32_t temp=0; for(j=0;j<l[i];j++){
temp<<=1;
temp|=(r[count]>>j)&1;
}
/* there might be a straightforward one-line way to do the below that'sportableandtotallysafeagainstroundoff,butIhaven't
thought of it. Therefore, we opt on the side of caution */ long _book_maptype1_quantvals(const static_codebook *b){ long vals; if(b->entries<1){ return(0);
}
vals=floor(pow((float)b->entries,1.f/b->dim));
/* the above *should* be reliable, but we'll not assume that FP is everreliablewhenbitstreamsyncisatstake;verifyviainteger meansthatvalsreallyisthegreatestvalueofdimforwhich
vals^b->bim <= b->entries */ /* treat the above as an initial guess */ if(vals<1){
vals=1;
} while(1){ long acc=1; long acc1=1; int i; for(i=0;i<b->dim;i++){ if(b->entries/vals<acc)break;
acc*=vals; if(LONG_MAX/(vals+1)<acc1)acc1=LONG_MAX; else acc1*=vals+1;
} if(i>=b->dim && acc<=b->entries && acc1>b->entries){ return(vals);
}else{ if(i<b->dim || acc>b->entries){
vals--;
}else{
vals++;
}
}
}
}
/* unpack the quantized list of values for encode/decode ***********/ /* we need to deal with two map types: in map type 1, the values are generatedalgorithmically(eachcolumnofthevectorcountsthrough thevaluesinthequantvector).inmaptype2,allthevaluescame
in in an explicit list. Both value lists must be unpacked */ float *_book_unquantize(const static_codebook *b,int n,int *sparsemap){ long j,k,count=0; if(b->maptype==1 || b->maptype==2){ int quantvals; float mindel=_float32_unpack(b->q_min); float delta=_float32_unpack(b->q_delta); float *r=_ogg_calloc(n*b->dim,sizeof(*r));
/* maptype 1 and 2 both use a quantized value vector, but
different sizes */ switch(b->maptype){ case1: /* most of the time, entries%dimensions == 0, but we need to be welldefined.Wedefinethatthepossiblevalesateach scalarisvalues==entries/dim.Ifentries%dim!=0,we'll have'toofew'values(values*dim<entries),whichmeansthat we'llhave'leftover'entries;leftoverentriesusezeroed values(andarewasted).Sodon'tgeneratecodebookslike
that */
quantvals=_book_maptype1_quantvals(b); for(j=0;j<b->entries;j++){ if((sparsemap && b->lengthlist[j]) || !sparsemap){ float last=0.f; int indexdiv=1; for(k=0;k<b->dim;k++){ int index= (j/indexdiv)%quantvals; float val=b->quantlist[index];
val=fabs(val)*delta+mindel+last; if(b->q_sequencep)last=val; if(sparsemap)
r[sparsemap[count]*b->dim+k]=val; else
r[count*b->dim+k]=val;
indexdiv*=quantvals;
}
count++;
}
void vorbis_staticbook_destroy(static_codebook *b){ if(b->allocedp){ if(b->quantlist)_ogg_free(b->quantlist); if(b->lengthlist)_ogg_free(b->lengthlist);
memset(b,0,sizeof(*b));
_ogg_free(b);
} /* otherwise, it is in static memory */
}
void vorbis_book_clear(codebook *b){ /* static book is not cleared; we're likely called on the lookup and
the static codebook belongs to the info struct */ if(b->valuelist)_ogg_free(b->valuelist); if(b->codelist)_ogg_free(b->codelist);
/* decode codebook arrangement is more heavily optimized than encode */ int vorbis_book_init_decode(codebook *c,const static_codebook *s){ int i,j,n=0,tabn; int *sortindex=NULL;
ogg_uint32_t *codes=NULL;
ogg_uint32_t **codep=NULL;
memset(c,0,sizeof(*c));
/* count actually used entries and find max length */ for(i=0;i<s->entries;i++) if(s->lengthlist[i]>0)
n++;
c->codelist=_ogg_malloc(n*sizeof(*c->codelist)); if(c->codelist==NULL)goto err_out; /* the index is a reverse index */ for(i=0;i<n;i++){ int position=codep[i]-codes;
sortindex[position]=i;
}
_ogg_free(codep); codep=NULL;
if(n==1 && c->dec_maxlength==1){ /* special case the 'single entry codebook' with a single bit fastpathtable(thatalwaysreturnsentry0)inordertouse
unmodified decode paths. */
c->dec_firsttablen=1;
c->dec_firsttable=_ogg_calloc(2,sizeof(*c->dec_firsttable)); if(c->dec_firsttable==NULL)goto err_out;
c->dec_firsttable[0]=c->dec_firsttable[1]=1;
}else{
c->dec_firsttablen=ov_ilog(c->used_entries)-4; /* this is magic */ if(c->dec_firsttablen<5)c->dec_firsttablen=5; if(c->dec_firsttablen>8)c->dec_firsttablen=8;
/* now fill in 'unused' entries in the firsttable with hi/lo search
hints for the non-direct-hits */
{
ogg_uint32_t mask=0xfffffffeUL<<(31-c->dec_firsttablen); long lo=0,hi=0;
/* we only actually have 15 bits per hint to play with here. Inordertooverflowgracefully(nothingbreaks,efficiency
just drops), encode as the difference from the extremes. */
{ unsignedlong loval=lo; unsignedlong hival=n-hi;
long vorbis_book_codeword(codebook *book,int entry){ if(book->c) /* only use with encode; decode optimizations are
allowed to break this */ return book->codelist[entry]; return -1;
}
long vorbis_book_codelen(codebook *book,int entry){ if(book->c) /* only use with encode; decode optimizations are
allowed to break this */ return book->c->lengthlist[entry]; return -1;
}
#ifdef _V_SELFTEST
/* Unit tests of the dequantizer; this stuff will be OK cross-platform,Isimplywanttobesurethatspecialmappingcases
actually work properly; a bug could go unnoticed for a while */
void run_test(static_codebook *b,float *comp){ float *out=_book_unquantize(b,b->entries,NULL); int i;
if(comp){ if(!out){
fprintf(stderr,"_book_unquantize incorrectly returned NULL\n"); exit(1);
}
for(i=0;i<b->entries*b->dim;i++) if(fabs(out[i]-comp[i])>.0001){
fprintf(stderr,"disagreement in unquantized and reference data:\n" "position %d, %g != %g\n",i,out[i],comp[i]); exit(1);
}
}else{ if(out){
fprintf(stderr,"_book_unquantize returned a value array: \n" " correct result should have been NULL\n"); exit(1);
}
}
_ogg_free(out);
}
int main(){ /* run the nine dequant tests, and compare to the hand-rolled results */
fprintf(stderr,"Dequant test 1... ");
run_test(&test1,test1_result);
fprintf(stderr,"OK\nDequant test 2... ");
run_test(&test2,test2_result);
fprintf(stderr,"OK\nDequant test 3... ");
run_test(&test3,test3_result);
fprintf(stderr,"OK\nDequant test 4... ");
run_test(&test4,test4_result);
fprintf(stderr,"OK\nDequant test 5... ");
run_test(&test5,test5_result);
fprintf(stderr,"OK\n\n");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.