📄 filebitstreamreaderagent.java
字号:
/* * CVS identifier: * * $Id: FileBitstreamReaderAgent.java,v 1.44 2001/03/02 10:09:13 grosbois Exp $ * * Class: FileBitstreamReaderAgent * * Description: Retrieve code-blocks codewords in the bit stream * * * * COPYRIGHT: * * This software module was originally developed by Rapha雔 Grosbois and * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel * Askel鰂 (Ericsson Radio Systems AB); and Bertrand Berthelot, David * Bouchard, F閘ix Henry, Gerard Mozelle and Patrice Onno (Canon Research * Centre France S.A) in the course of development of the JPEG2000 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This * software module is an implementation of a part of the JPEG 2000 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio * Systems AB and Canon Research Centre France S.A (collectively JJ2000 * Partners) agree not to assert against ISO/IEC and users of the JPEG * 2000 Standard (Users) any of their rights under the copyright, not * including other intellectual property rights, for this software module * with respect to the usage by ISO/IEC and Users of this software module * or modifications thereof for use in hardware or software products * claiming conformance to the JPEG 2000 Standard. Those intending to use * this software module in hardware or software products are advised that * their use may infringe existing patents. The original developers of * this software module, JJ2000 Partners and ISO/IEC assume no liability * for use of this software module or modifications thereof. No license * or right to this software module is granted for non JPEG 2000 Standard * conforming products. JJ2000 Partners have full right to use this * software module for his/her own purpose, assign or donate this * software module to any third party and to inhibit third parties from * using this software module for non JPEG 2000 Standard conforming * products. This copyright notice must be included in all copies or * derivative works of this software module. * * Copyright (c) 1999/2000 JJ2000 Partners. * */package jj2000.j2k.codestream.reader;import jj2000.j2k.quantization.dequantizer.*;import jj2000.j2k.wavelet.synthesis.*;import jj2000.j2k.entropy.decoder.*;import jj2000.j2k.codestream.*;import jj2000.j2k.decoder.*;import jj2000.j2k.entropy.*;import jj2000.j2k.image.*;import jj2000.j2k.util.*;import jj2000.j2k.io.*;import jj2000.j2k.*;import java.util.*;import java.io.*;/** * This class reads the bit stream (with the help of HeaderDecoder for tile * headers and PktDecoder for packets header and body) and retrives location * of all code-block's codewords. * * <P>Note: All tile-parts headers are read by the constructor whereas packets * are processed when decoding related tile (when setTile method is called). * * <P>In parsing mode, the reader simulates a virtual layer-resolution * progressive bit stream with the same truncation points in each code-block, * whereas in truncation mode, only the first bytes are taken into account (it * behaves like if it is a real truncated codestream). * * @see HeaderDecoder * @see PktDecoder * */public class FileBitstreamReaderAgent extends BitstreamReaderAgent implements Markers, ProgressionType, StdEntropyCoderOptions{ /** Reference to the PktDecoder instance */ public PktDecoder pktDec; /** Reference to the ParameterList instance */ private ParameterList pl; /** The RandomAccessIO where to get data from */ private RandomAccessIO in; /** The number of tiles in the image */ private int nt; /** Offset of the first packet in each tile-part in each tile */ private int[][] firstPackOff; /** Number of bytes allocated to each tile. In parsing mode, this number * is related to the tile length in the codestream whereas in truncation * mode all the rate is affected to the first tiles. */ private int[] nBytes; /** Length of each tile-part (written in Psot) */ private int[][] tilePartLen; /** Total length of each tile */ private int[] totTileLen; /** Total length of tiles' header */ private int[] totTileHeadLen; /** First tile part header length*/ private int firstTilePartHeadLen; /** Total length of all tile parts in all tiles */ private double totAllTileLen; /** Length of main header */ private int mainHeadLen; /** Length of all tile-part headers */ private int[][] tilePartHeadLen; /** Length of each packet head found in the tile */ private Vector pktHL; /** True if truncation mode is used. False if parsing mode */ private boolean isTruncMode; /** The number of tile-parts that remain to read */ private int remainingTileParts; /** The number of tile-parts read so far for each tile */ private int[] tilePartsRead; /** Thetotal number of tile-parts read so far */ private int totTilePartsRead=0; /** The number of tile-parts in each tile */ private int[] tileParts; /** The current tile part being used */ private int curTilePart; /** The number of the tile-part in the codestream */ private int[][] tilePartNum; /** Array containing info. for all the code-blocks:<br> * - 1st dim: component index.<br> * - 2nd dim: resolution level index.<br> * - 3rd dim: subband index.<br> * - 4th/5th dim: code-block index (vert. and horiz.).<br> */ private CBlkInfo[][][][][] cbI; /** * Reads all tiles headers and keep offset of their first packet. Finally * the rate allocation method is called. * * @param hd HeaderDecoder of the codestream. * * @param ehs The input stream where to read bit-stream. * * @param decSpec The decoder specifications * * @param pl The ParameterList instance created from the command-line * arguments. * * @see #allocateRate * */ public FileBitstreamReaderAgent(HeaderDecoder hd,RandomAccessIO ehs, DecoderSpecs decSpec,ParameterList pl) throws IOException { super(hd,decSpec); this.pl = pl; String strInfo = "Codestream elements information in bytes "+ "(offset, total length, header length):\n\n"; // Get decoding rate boolean rateInBytes; boolean parsing = pl.getBooleanParameter("parsing"); try{ trate = pl.getFloatParameter("rate"); } catch (NumberFormatException e) { throw new Error("Invalid value in 'rate' option: "+ pl.getParameter("rate")); } catch (IllegalArgumentException e) { throw new Error("'rate' option is missing"); } try{ tnbytes = pl.getIntParameter("nbytes"); } catch (NumberFormatException e) { throw new Error("Invalid value in 'nbytes' option: "+ pl.getParameter("nbytes")); } catch (IllegalArgumentException e) { throw new Error("'nbytes' option is missing"); } // Check that '-rate' and '-nbytes' are not used at the same time ParameterList defaults = pl.getDefaultParameterList(); if(tnbytes != defaults.getFloatParameter("nbytes")) rateInBytes = true; else rateInBytes = false; if(rateInBytes && trate != defaults.getIntParameter("rate")) { throw new Error("Cannot use '-rate' and '-nbytes' options at the "+ "same time."); } if(rateInBytes){ trate = tnbytes*8f/hd.getImgWidth()/hd.getImgHeight(); } else { tnbytes = (int)(trate*hd.getImgWidth()*hd.getImgHeight())/8; } isTruncMode = !pl.getBooleanParameter("parsing"); // initializations nt = ntX * ntY; in = ehs; pktDec = new PktDecoder(decSpec,hd,ehs,this,isTruncMode); tileParts = new int[nt]; totTileLen = new int[nt]; tilePartLen = new int[nt][]; tilePartNum = new int[nt][]; firstPackOff = new int[nt][]; tilePartsRead = new int[nt]; totTileHeadLen = new int[nt]; tilePartHeadLen = new int[nt][]; nBytes = new int[nt]; hd.nTileParts = new int[nt]; this.isTruncMode = isTruncMode; int t=0, pos, tp=0, tptot=0; // Keeps main header's length, takes file format into account int cdstreamStart = hd.initPos; // Codestream offset in the file mainHeadLen = in.getPos() - cdstreamStart; anbytes = mainHeadLen; strInfo += "Main header length : "+cdstreamStart+", "+mainHeadLen+ ", "+mainHeadLen+"\n"; // If cannot even read the first tile-part if(anbytes > tnbytes){ throw new Error("Requested bitrate is too small."); } // Read all tile-part headers from all tiles. int tilePartStart; boolean rateReached = false; int mdl; totAllTileLen = 0; remainingTileParts = nt; // at least as many tile-parts as tiles try{ while(remainingTileParts != 0){ tilePartStart = in.getPos(); // Read tile-part header try{ t = readTilePartHeader(); tp = tilePartsRead[t]; } catch(EOFException e){ firstPackOff[t][tp]=in.length(); throw e; } pos = in.getPos(); // In truncation mode, if target decoding rate is reached in // tile-part header, skips the tile-part and stop reading if(isTruncMode){ if( (pos-cdstreamStart) > tnbytes ){ firstPackOff[t][tp]=in.length(); rateReached = true; break; } } // Set tile part position and header length firstPackOff[t][tp] = pos; tilePartHeadLen[t][tp] = (pos-tilePartStart); strInfo += "Tile-part "+tp+" of tile "+t+" : "+tilePartStart +", "+tilePartLen[t][tp]+", "+tilePartHeadLen[t][tp]+"\n"; // Update length counters totTileLen[t] += tilePartLen[t][tp]; totTileHeadLen[t] += tilePartHeadLen[t][tp]; totAllTileLen += tilePartLen[t][tp]; if(isTruncMode){ if(anbytes+tilePartLen[t][tp] > tnbytes) { anbytes += tilePartHeadLen[t][tp]; rateReached = true; nBytes[t] += (tnbytes-anbytes); break; } else { anbytes += tilePartHeadLen[t][tp]; nBytes[t] += (tilePartLen[t][tp]- tilePartHeadLen[t][tp]); } } else { if(anbytes+tilePartHeadLen[t][tp] > tnbytes) { break; } else { anbytes += tilePartHeadLen[t][tp]; } } // If this is first tile-part, remember header length if(tptot == 0) firstTilePartHeadLen = tilePartHeadLen[t][tp]; // Go to the beginning of next tile part tilePartsRead[t]++; in.seek(tilePartStart+tilePartLen[t][tp]); remainingTileParts--; tptot++; } } catch(EOFException e) { if(pl.getBooleanParameter("cdstr_info")) FacilityManager.getMsgLogger(). printmsg(MsgLogger.INFO,strInfo); FacilityManager.getMsgLogger(). printmsg(MsgLogger.WARNING,"Codestream truncated in tile "+t); // Bit-rate allocation if(!isTruncMode) allocateRate(); // Update 'res' value once all tile-part headers are read if(pl.getParameter("res") == null){ res = decSpec.dls.getMin(); } else{ try { res = pl.getIntParameter("res"); if(res<0){ throw new IllegalArgumentException("Specified negative "+ "resolution level "+
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -