staticcodebook.java
来自「java ogg player library. for play back o」· Java 代码 · 共 589 行 · 第 1/2 页
JAVA
589 行
int quantvals=0; switch(maptype){ case 1: quantvals=maptype1_quantvals(); break; case 2: quantvals=entries*dim; break; } // quantized values quantlist=new int[quantvals]; for(i=0;i<quantvals;i++){ quantlist[i]=opb.read(q_quant); } if(quantlist[quantvals-1]==-1){// goto _eofout; clear(); return(-1); } } break; default:// goto _eofout; clear(); return(-1); } // all set return(0);// _errout:// _eofout:// vorbis_staticbook_clear(s);// return(-1); } // there might be a straightforward one-line way to do the below // that's portable and totally safe against roundoff, but I haven't // thought of it. Therefore, we opt on the side of caution private int maptype1_quantvals(){ int vals=(int)(Math.floor(Math.pow(entries,1./dim))); // the above *should* be reliable, but we'll not assume that FP is // ever reliable when bitstream sync is at stake; verify via integer // means that vals really is the greatest value of dim for which // vals^b->bim <= b->entries // treat the above as an initial guess while(true){ int acc=1; int acc1=1; for(int i=0;i<dim;i++){ acc*=vals; acc1*=vals+1; } if(acc<=entries && acc1>entries){ return(vals); } else{ if(acc>entries){ vals--; } else{ vals++; } } } } void clear(){// if(quantlist!=null)free(b->quantlist);// if(lengthlist!=null)free(b->lengthlist);// if(nearest_tree!=null){// free(b->nearest_tree->ptr0);// free(b->nearest_tree->ptr1);// free(b->nearest_tree->p);// free(b->nearest_tree->q);// memset(b->nearest_tree,0,sizeof(encode_aux_nearestmatch));// free(b->nearest_tree);// }// if(thresh_tree!=null){// free(b->thresh_tree->quantthresh);// free(b->thresh_tree->quantmap);// memset(b->thresh_tree,0,sizeof(encode_aux_threshmatch));// free(b->thresh_tree);// }// memset(b,0,sizeof(static_codebook)); } // unpack the quantized list of values for encode/decode // we need to deal with two map types: in map type 1, the values are // generated algorithmically (each column of the vector counts through // the values in the quant vector). in map type 2, all the values came // in in an explicit list. Both value lists must be unpacked float[] unquantize(){ if(maptype==1 || maptype==2){ int quantvals; float mindel=float32_unpack(q_min); float delta=float32_unpack(q_delta); float[] r=new float[entries*dim]; //System.err.println("q_min="+q_min+", mindel="+mindel); // maptype 1 and 2 both use a quantized value vector, but // different sizes switch(maptype){ case 1: // most of the time, entries%dimensions == 0, but we need to be // well defined. We define that the possible vales at each // scalar is values == entries/dim. If entries%dim != 0, we'll // have 'too few' values (values*dim<entries), which means that // we'll have 'left over' entries; left over entries use zeroed // values (and are wasted). So don't generate codebooks like that quantvals=maptype1_quantvals(); for(int j=0;j<entries;j++){ float last=0.f; int indexdiv=1; for(int k=0;k<dim;k++){ int index=(j/indexdiv)%quantvals; float val=quantlist[index]; val=Math.abs(val)*delta+mindel+last; if(q_sequencep!=0)last=val; r[j*dim+k]=val; indexdiv*=quantvals; } } break; case 2: for(int j=0;j<entries;j++){ float last=0.f; for(int k=0;k<dim;k++){ float val=quantlist[j*dim+k];//if((j*dim+k)==0){System.err.println(" | 0 -> "+val+" | ");} val=Math.abs(val)*delta+mindel+last; if(q_sequencep!=0)last=val; r[j*dim+k]=val;//if((j*dim+k)==0){System.err.println(" $ r[0] -> "+r[0]+" | ");} } }//System.err.println("\nr[0]="+r[0]); } return(r); } return(null); } private static int ilog(int v){ int ret=0; while(v!=0){ ret++; v>>>=1; } return(ret); } // 32 bit float (not IEEE; nonnormalized mantissa + // biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm // Why not IEEE? It's just not that important here. static final int VQ_FEXP=10; static final int VQ_FMAN=21; static final int VQ_FEXP_BIAS=768; // bias toward values smaller than 1. // doesn't currently guard under/overflow static long float32_pack(float val){ int sign=0; int exp; int mant; if(val<0){ sign=0x80000000; val= -val; } exp=(int)Math.floor(Math.log(val)/Math.log(2)); mant=(int)Math.rint(Math.pow(val,(VQ_FMAN-1)-exp)); exp=(exp+VQ_FEXP_BIAS)<<VQ_FMAN; return(sign|exp|mant); } static float float32_unpack(int val){ float mant=val&0x1fffff; float sign=val&0x80000000; float exp =(val&0x7fe00000)>>>VQ_FMAN;//System.err.println("mant="+mant+", sign="+sign+", exp="+exp); //if(sign!=0.0)mant= -mant; if((val&0x80000000)!=0)mant= -mant;//System.err.println("mant="+mant); return(ldexp(mant,((int)exp)-(VQ_FMAN-1)-VQ_FEXP_BIAS)); } static float ldexp(float foo, int e){ return (float)(foo*Math.pow(2, e)); }/* // TEST // Unit tests of the dequantizer; this stuff will be OK // cross-platform, I simply want to be sure that special mapping cases // actually work properly; a bug could go unnoticed for a while // cases: // // no mapping // full, explicit mapping // algorithmic mapping // // nonsequential // sequential static int[] full_quantlist1={0,1,2,3, 4,5,6,7, 8,3,6,1}; static int[] partial_quantlist1={0,7,2}; // no mapping static StaticCodeBook test1=new StaticCodeBook(4,16,null, 0,0,0,0,0, null,null,null); static float[] test1_result=null; // linear, full mapping, nonsequential static StaticCodeBook test2=new StaticCodeBook(4,3,null, 2,-533200896,1611661312,4,0, full_quantlist1, null, null); static float[] test2_result={-3,-2,-1,0, 1,2,3,4, 5,0,3,-2}; // linear, full mapping, sequential static StaticCodeBook test3=new StaticCodeBook(4,3,null, 2, -533200896,1611661312,4,1, full_quantlist1,null, null); static float[] test3_result={-3,-5,-6,-6, 1,3,6,10, 5,5,8,6}; // linear, algorithmic mapping, nonsequential static StaticCodeBook test4=new StaticCodeBook(3,27,null, 1,-533200896,1611661312,4,0, partial_quantlist1,null,null); static float[] test4_result={-3,-3,-3, 4,-3,-3, -1,-3,-3, -3, 4,-3, 4, 4,-3, -1, 4,-3, -3,-1,-3, 4,-1,-3, -1,-1,-3, -3,-3, 4, 4,-3, 4, -1,-3, 4, -3, 4, 4, 4, 4, 4, -1, 4, 4, -3,-1, 4, 4,-1, 4, -1,-1, 4, -3,-3,-1, 4,-3,-1, -1,-3,-1, -3, 4,-1, 4, 4,-1, -1, 4,-1, -3,-1,-1, 4,-1,-1, -1,-1,-1}; // linear, algorithmic mapping, sequential static StaticCodeBook test5=new StaticCodeBook(3,27,null, 1,-533200896,1611661312,4,1, partial_quantlist1,null,null); static float[] test5_result={-3,-6,-9, 4, 1,-2, -1,-4,-7, -3, 1,-2, 4, 8, 5, -1, 3, 0, -3,-4,-7, 4, 3, 0, -1,-2,-5, -3,-6,-2, 4, 1, 5, -1,-4, 0, -3, 1, 5, 4, 8,12, -1, 3, 7, -3,-4, 0, 4, 3, 7, -1,-2, 2, -3,-6,-7, 4, 1, 0, -1,-4,-5, -3, 1, 0, 4, 8, 7, -1, 3, 2, -3,-4,-5, 4, 3, 2, -1,-2,-3}; void run_test(float[] comp){ float[] out=unquantize(); if(comp!=null){ if(out==null){ System.err.println("_book_unquantize incorrectly returned NULL"); System.exit(1); } for(int i=0;i<entries*dim;i++){ if(Math.abs(out[i]-comp[i])>.0001){ System.err.println("disagreement in unquantized and reference data:\nposition "+i+": "+out[i]+" != "+comp[i]); System.exit(1); } } } else{ if(out!=null){ System.err.println("_book_unquantize returned a value array:\n correct result should have been NULL"); System.exit(1); } } } public static void main(String[] arg){ // run the nine dequant tests, and compare to the hand-rolled results System.err.print("Dequant test 1... "); test1.run_test(test1_result); System.err.print("OK\nDequant test 2... "); test2.run_test(test2_result); System.err.print("OK\nDequant test 3... "); test3.run_test(test3_result); System.err.print("OK\nDequant test 4... "); test4.run_test(test4_result); System.err.print("OK\nDequant test 5... "); test5.run_test(test5_result); System.err.print("OK\n\n"); }*/}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?