📄 stdquantizer.java
字号:
// Input and output arrays are the same (for "in place" quant.) outarr = (int[])cblk.getData(); } else { // Source data is float // Can not use 'cblk' to get float data, use 'infblk' infblk = (CBlkWTDataFloat) src.getNextInternCodeBlock(c,infblk); if (infblk == null) { // Release buffer from infblk: this enables to garbage collect // the big buffer when we are done with last code-block of // component. this.infblk.setData(null); return null; // No more code-blocks in current tile for comp. } this.infblk = infblk; // Save local cache infarr = (float[])infblk.getData(); // Get output data array and check that there is memory to put the // quantized coeffs in outarr = (int[]) cblk.getData(); if (outarr == null || outarr.length < infblk.w*infblk.h) { outarr = new int[infblk.w*infblk.h]; cblk.setData(outarr); } cblk.m = infblk.m; cblk.n = infblk.n; cblk.sb = infblk.sb; cblk.ulx = infblk.ulx; cblk.uly = infblk.uly; cblk.w = infblk.w; cblk.h = infblk.h; cblk.wmseScaling = infblk.wmseScaling; cblk.offset = 0; cblk.scanw = cblk.w; } // Cache width, height and subband of code-block w = cblk.w; h = cblk.h; sb = cblk.sb; if(isReversible(tIdx,c)) { // Reversible only for int data cblk.magbits = g-1+src.getNomRangeBits(c)+sb.anGainExp; shiftBits = 31-cblk.magbits; // Update the convertFactor field cblk.convertFactor = (1<<shiftBits); // Since we used getNextCodeBlock() to get the int data then // 'offset' is 0 and 'scanw' is the width of the code-block The // input and output arrays are the same (i.e. "in place") for(j=w*h-1; j>=0; j--){ tmp = (outarr[j]<<shiftBits); outarr[j] = ((tmp < 0) ? (1<<31)|(-tmp) : tmp); } } else{ // Non-reversible, use step size float baseStep = ((Float)qsss.getTileCompVal(tIdx,c)).floatValue(); // Calculate magnitude bits and quantization step size if(isDerived(tIdx,c)){ cblk.magbits = g-1+sb.level- (int)Math.floor(Math.log(baseStep)/log2); stepUDR = baseStep/(1<<sb.level); } else{ cblk.magbits = g-1-(int)Math.floor(Math.log(baseStep/ (sb.l2Norm*(1<<sb.anGainExp)))/ log2); stepUDR = baseStep/(sb.l2Norm*(1<<sb.anGainExp)); } shiftBits = 31-cblk.magbits; // Calculate step that decoder will get and use that one. stepUDR = convertFromExpMantissa(convertToExpMantissa(stepUDR)); invstep = 1.0f/((1L<<(src.getNomRangeBits(c)+sb.anGainExp))* stepUDR); // Normalize to magnitude bits (output fractional point) invstep *= (1<<(shiftBits-src.getFixedPoint(c))); // Update convertFactor and stepSize fields cblk.convertFactor = invstep; cblk.stepSize = ((1L<<(src.getNomRangeBits(c)+sb.anGainExp))* stepUDR); if(intq){ // Quantizing int data // Since we used getNextCodeBlock() to get the int data then // 'offset' is 0 and 'scanw' is the width of the code-block // The input and output arrays are the same (i.e. "in place") for (j=w*h-1; j>=0; j--) { tmp = (int)(outarr[j]*invstep); outarr[j] = ((tmp < 0) ? (1<<31)|(-tmp) : tmp); } } else { // Quantizing float data for (j=w*h-1, k = infblk.offset+(h-1)*infblk.scanw+w-1, jmin = w*(h-1); j>=0; jmin -= w) { for (; j>=jmin; k--, j--) { tmp = (int)(infarr[k]*invstep); outarr[j] = ((tmp < 0) ? (1<<31)|(-tmp) : tmp); } // Jump to beggining of previous line in input k -= infblk.scanw - w; } } } // Return the quantized code-block return cblk; } /** * Calculates the parameters of the SubbandAn objects that depend on the * Quantizer. The 'stepWMSE' field is calculated for each subband which is * a leaf in the tree rooted at 'sb', for the specified component. The * subband tree 'sb' must be the one for the component 'n'. * * @param sb The root of the subband tree. * * @param c The component index * * @see SubbandAn#stepWMSE * */ protected void calcSbParams(SubbandAn sb,int c){ float baseStep; if(sb.stepWMSE>0f) // parameters already calculated return; if(!sb.isNode){ if(isReversible(tIdx,c)){ sb.stepWMSE = (float) Math.pow(2,-(src.getNomRangeBits(c)<<1))* sb.l2Norm*sb.l2Norm; } else{ baseStep = ((Float)qsss.getTileCompVal(tIdx,c)).floatValue(); if(isDerived(tIdx,c)){ sb.stepWMSE = baseStep*baseStep* (float)Math.pow(2,(sb.anGainExp-sb.level)<<1)* sb.l2Norm*sb.l2Norm; } else{ sb.stepWMSE = baseStep*baseStep; } } } else{ calcSbParams((SubbandAn)sb.getLL(),c); calcSbParams((SubbandAn)sb.getHL(),c); calcSbParams((SubbandAn)sb.getLH(),c); calcSbParams((SubbandAn)sb.getHH(),c); sb.stepWMSE = 1f; // Signal that we already calculated this branch } } /** * Converts the floating point value to its exponent-mantissa * representation. The mantissa occupies the 11 least significant bits * (bits 10-0), and the exponent the previous 5 bits (bits 15-11). * * @param step The quantization step, normalized to a dynamic range of 1. * * @return The exponent mantissa representation of the step. * */ public static int convertToExpMantissa(float step) { int exp; exp = (int)Math.ceil(-Math.log(step)/log2); if (exp>QSTEP_MAX_EXPONENT) { // If step size is too small for exponent representation, use the // minimum, which is exponent QSTEP_MAX_EXPONENT and mantissa 0. return (QSTEP_MAX_EXPONENT<<QSTEP_MANTISSA_BITS); } // NOTE: this formula does not support more than 5 bits for the // exponent, otherwise (-1<<exp) might overflow (the - is used to be // able to represent 2**31) return (exp<<QSTEP_MANTISSA_BITS) | ((int)((-step*(-1<<exp)-1f)*(1<<QSTEP_MANTISSA_BITS)+0.5f)); } /** * Converts the exponent-mantissa representation to its floating-point * value. The mantissa occupies the 11 least significant bits (bits 10-0), * and the exponent the previous 5 bits (bits 15-11). * * @param ems The exponent-mantissa representation of the step. * * @return The floating point representation of the step, normalized to a * dynamic range of 1. * */ private static float convertFromExpMantissa(int ems) { // NOTE: this formula does not support more than 5 bits for the // exponent, otherwise (-1<<exp) might overflow (the - is used to be // able to represent 2**31) return (-1f-((float)(ems&QSTEP_MAX_MANTISSA)) / ((float)(1<<QSTEP_MANTISSA_BITS))) / (float)(-1<<((ems>>QSTEP_MANTISSA_BITS)&QSTEP_MAX_EXPONENT)); } /** * Returns the maximum number of magnitude bits in any subband of the * current tile. * * @param c the component number * * @return The maximum number of magnitude bits in all subbands of the * current tile. * */ public int getMaxMagBits(int c){ Subband sb = getSubbandTree(tIdx,c); if(isReversible(tIdx,c)){ return getMaxMagBitsRev(sb,c); } else{ if(isDerived(tIdx,c)){ return getMaxMagBitsDerived(sb,tIdx,c); } else { return getMaxMagBitsExpounded(sb,tIdx,c); } } } /** * Returns the maximum number of magnitude bits in any subband of the * current tile if reversible quantization is used * * @param sb The root of the subband tree of the current tile * * @param c the component number * * @return The highest number of magnitude bit-planes * */ private int getMaxMagBitsRev(Subband sb, int c){ int tmp,max=0; int g = ((Integer)gbs.getTileCompVal(tIdx,c)).intValue(); if(!sb.isNode) return g-1+src.getNomRangeBits(c)+sb.anGainExp; max=getMaxMagBitsRev(sb.getLL(),c); tmp=getMaxMagBitsRev(sb.getLH(),c); if(tmp>max) max=tmp; tmp=getMaxMagBitsRev(sb.getHL(),c); if(tmp>max) max=tmp; tmp=getMaxMagBitsRev(sb.getHH(),c); if(tmp>max) max=tmp; return max; } /** * Returns the maximum number of magnitude bits in any subband in the * given tile-component if derived quantization is used * * @param sb The root of the subband tree of the tile-component * * @param t Tile index * * @param c Component index * * @return The highest number of magnitude bit-planes * */ private int getMaxMagBitsDerived(Subband sb,int t,int c){ int tmp,max=0; int g = ((Integer)gbs.getTileCompVal(t,c)).intValue(); if(!sb.isNode){ float baseStep = ((Float)qsss.getTileCompVal(t,c)).floatValue(); return g-1+sb.level-(int)Math.floor(Math.log(baseStep)/log2); } max=getMaxMagBitsDerived(sb.getLL(),t,c); tmp=getMaxMagBitsDerived(sb.getLH(),t,c); if(tmp>max) max=tmp; tmp=getMaxMagBitsDerived(sb.getHL(),t,c); if(tmp>max) max=tmp; tmp=getMaxMagBitsDerived(sb.getHH(),t,c); if(tmp>max) max=tmp; return max; } /** * Returns the maximum number of magnitude bits in any subband in the * given tile-component if expounded quantization is used * * @param sb The root of the subband tree of the tile-component * * @param t Tile index * * @param c Component index * * @return The highest number of magnitude bit-planes * */ private int getMaxMagBitsExpounded(Subband sb,int t,int c){ int tmp,max=0; int g = ((Integer)gbs.getTileCompVal(t,c)).intValue(); if(!sb.isNode){ float baseStep = ((Float)qsss.getTileCompVal(t,c)).floatValue(); return g-1- (int)Math.floor(Math.log(baseStep/ (((SubbandAn)sb).l2Norm*(1<<sb.anGainExp)))/ log2); } max=getMaxMagBitsExpounded(sb.getLL(),t,c); tmp=getMaxMagBitsExpounded(sb.getLH(),t,c); if(tmp>max) max=tmp; tmp=getMaxMagBitsExpounded(sb.getHL(),t,c); if(tmp>max) max=tmp; tmp=getMaxMagBitsExpounded(sb.getHH(),t,c); if(tmp>max) max=tmp; return max; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -