staticcodebook.java

来自「java ogg player library. for play back o」· Java 代码 · 共 589 行 · 第 1/2 页

JAVA
589
字号
/* JOrbis * Copyright (C) 2000 ymnk, JCraft,Inc. *   * Written by: 2000 ymnk<ymnk@jcraft.com> *    * Many thanks to  *   Monty <monty@xiph.org> and  *   The XIPHOPHORUS Company http://www.xiph.org/ . * JOrbis has been based on their awesome works, Vorbis codec. *    * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License * as published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version.    * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU Library General Public License for more details. *  * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */package com.jcraft.jorbis;import com.jcraft.jogg.*;class StaticCodeBook{  int   dim;            // codebook dimensions (elements per vector)  int   entries;        // codebook entries  int[] lengthlist;     // codeword lengths in bits  // mapping  int   maptype;        // 0=none			// 1=implicitly populated values from map column 			// 2=listed arbitrary values  // The below does a linear, single monotonic sequence mapping.  int   q_min;       // packed 32 bit float; quant value 0 maps to minval  int   q_delta;     // packed 32 bit float; val 1 - val 0 == delta  int   q_quant;     // bits: 0 < quant <= 16  int   q_sequencep; // bitflag  // additional information for log (dB) mapping; the linear mapping  // is assumed to actually be values in dB.  encodebias is used to  // assign an error weight to 0 dB. We have two additional flags:  // zeroflag indicates if entry zero is to represent -Inf dB; negflag  // indicates if we're to represent negative linear values in a  // mirror of the positive mapping.  int[] quantlist;  // map == 1: (int)(entries/dim) element column map                    // map == 2: list of dim*entries quantized entry vals  // encode helpers  EncodeAuxNearestMatch nearest_tree;  EncodeAuxThreshMatch  thresh_tree;  StaticCodeBook(){}  StaticCodeBook(int dim, int entries, int[] lengthlist,		 int maptype, int q_min, int q_delta, 		 int q_quant, int q_sequencep, int[] quantlist,		 //EncodeAuxNearestmatch nearest_tree,		 Object nearest_tree,		 // EncodeAuxThreshmatch thresh_tree,		 Object thresh_tree		 ){    this();    this.dim=dim; this.entries=entries; this.lengthlist=lengthlist;    this.maptype=maptype; this.q_min=q_min; this.q_delta=q_delta;    this.q_quant=q_quant; this.q_sequencep=q_sequencep;     this.quantlist=quantlist;  }  int pack(Buffer opb){    int i;    boolean ordered=false;    opb.write(0x564342,24);    opb.write(dim, 16);    opb.write(entries, 24);    // pack the codewords.  There are two packings; length ordered and    // length random.  Decide between the two now.      for(i=1;i<entries;i++){      if(lengthlist[i]<lengthlist[i-1])break;    }    if(i==entries)ordered=true;      if(ordered){      // length ordered.  We only need to say how many codewords of      // each length.  The actual codewords are generated      // deterministically      int count=0;      opb.write(1,1);               // ordered      opb.write(lengthlist[0]-1,5); // 1 to 32      for(i=1;i<entries;i++){	int _this=lengthlist[i];	int _last=lengthlist[i-1];	if(_this>_last){	  for(int j=_last;j<_this;j++){	    opb.write(i-count,ilog(entries-count));	    count=i;	  }	}      }      opb.write(i-count,ilog(entries-count));    }    else{      // length random.  Again, we don't code the codeword itself, just      // the length.  This time, though, we have to encode each length      opb.write(0,1);   // unordered          // algortihmic mapping has use for 'unused entries', which we tag      // here.  The algorithmic mapping happens as usual, but the unused      // entry has no codeword.      for(i=0;i<entries;i++){	if(lengthlist[i]==0)break;      }      if(i==entries){	opb.write(0,1); // no unused entries	for(i=0;i<entries;i++){	  opb.write(lengthlist[i]-1,5);	}      }      else{	opb.write(1,1); // we have unused entries; thus we tag	for(i=0;i<entries;i++){	  if(lengthlist[i]==0){	    opb.write(0,1);	  }	  else{	    opb.write(1,1);	    opb.write(lengthlist[i]-1,5);	  }	}      }    }    // is the entry number the desired return value, or do we have a    // mapping? If we have a mapping, what type?    opb.write(maptype,4);    switch(maptype){    case 0:      // no mapping      break;    case 1:    case 2:      // implicitly populated value mapping      // explicitly populated value mapping      if(quantlist==null){	// no quantlist?  error	return(-1);      }          // values that define the dequantization      opb.write(q_min,32);      opb.write(q_delta,32);      opb.write(q_quant-1,4);      opb.write(q_sequencep,1);          {	int quantvals=0;	switch(maptype){	case 1:	  // a single column of (c->entries/c->dim) quantized values for	  // building a full value list algorithmically (square lattice)	  quantvals=maptype1_quantvals();	  break;	case 2:	  // every value (c->entries*c->dim total) specified explicitly	  quantvals=entries*dim;	  break;	}	// quantized values	for(i=0;i<quantvals;i++){	  opb.write(Math.abs(quantlist[i]),q_quant);	}      }      break;    default:      // error case; we don't have any other map types now      return(-1);    }    return(0);  }/**/  // unpacks a codebook from the packet buffer into the codebook struct,  // readies the codebook auxiliary structures for decode  int unpack(Buffer opb){    int i;    //memset(s,0,sizeof(static_codebook));    // make sure alignment is correct    if(opb.read(24)!=0x564342){//    goto _eofout;      clear();      return(-1);     }    // first the basic parameters    dim=opb.read(16);    entries=opb.read(24);    if(entries==-1){//    goto _eofout;      clear();      return(-1);     }    // codeword ordering.... length ordered or unordered?    switch(opb.read(1)){    case 0:      // unordered      lengthlist=new int[entries];      // allocated but unused entries?      if(opb.read(1)!=0){	// yes, unused entries	for(i=0;i<entries;i++){	  if(opb.read(1)!=0){	    int num=opb.read(5);	    if(num==-1){//            goto _eofout;	      clear();	      return(-1); 	    }	    lengthlist[i]=num+1;	  }	  else{	    lengthlist[i]=0;	  }	}      }      else{	// all entries used; no tagging	for(i=0;i<entries;i++){	  int num=opb.read(5);	  if(num==-1){//          goto _eofout;	    clear();	    return(-1); 	  }	  lengthlist[i]=num+1;	}      }      break;    case 1:      // ordered      {	int length=opb.read(5)+1;	lengthlist=new int[entries];	for(i=0;i<entries;){	  int num=opb.read(ilog(entries-i));	  if(num==-1){//          goto _eofout;	    clear();	    return(-1); 	  }	  for(int j=0;j<num;j++,i++){	    lengthlist[i]=length;	  }	  length++;	}      }      break;    default:      // EOF      return(-1);    }      // Do we have a mapping to unpack?    switch((maptype=opb.read(4))){    case 0:      // no mapping      break;    case 1:    case 2:      // implicitly populated value mapping      // explicitly populated value mapping      q_min=opb.read(32);      q_delta=opb.read(32);      q_quant=opb.read(4)+1;      q_sequencep=opb.read(1);      {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?