C# Class CSJ2K.j2k.entropy.encoder.EBCOTRateAllocator

This implements the EBCOT post compression rate allocation algorithm. This algorithm finds the most suitable truncation points for the set of code-blocks, for each layer target bitrate. It works by first collecting the rate distortion info from all code-blocks, in all tiles and all components, and then running the rate-allocation on the whole image at once, for each layer.

This implementation also provides some timing features. They can be enabled by setting the 'DO_TIMING' constant of this class to true and recompiling. The timing uses the 'System.currentTimeMillis()' Java API call, which returns wall clock time, not the actual CPU time used. The timing results will be printed on the message output. Since the times reported are wall clock times and not CPU usage times they can not be added to find the total used time (i.e. some time might be counted in several places). When timing is disabled ('DO_TIMING' is false) there is no penalty if the compiler performs some basic optimizations. Even if not the penalty should be negligeable.

Inheritance: PostCompRateAllocator
Show file Open project: cureos/csj2k

Public Methods

Method Description
EBCOTRateAllocator ( CodedCBlkDataSrcEnc src, CSJ2K.j2k.entropy.encoder.LayersInfo lyrs, CSJ2K.j2k.codestream.writer.CodestreamWriter writer, CSJ2K.j2k.encoder.EncoderSpecs encSpec, CSJ2K.j2k.util.ParameterList pl ) : System

Initializes the EBCOT rate allocator of entropy coded data. The layout of layers, and their bitrate constraints, is specified by the 'lyrs' parameter.

initialize ( ) : void

Initializes the layers array. This must be called after the main header has been entirely written or simulated, so as to take its overhead into account. This method will get all the code-blocks and then initialize the target bitrates for each layer, according to the specifications.

runAndWrite ( ) : void

Runs the rate allocation algorithm and writes the data to the bit stream writer object provided to the constructor.

writeCompPosResLy ( int t, int rs, int re, int cs, int ce, int lys, int lye ) : void

Write a piece of bit stream according to the COMP_POS_RES_LY_PROG progression mode and between given bounds

writeLyResCompPos ( int t, int rs, int re, int cs, int ce, int lys, int lye ) : void

Write a piece of bit stream according to the LY_RES_COMP_POS_PROG progression mode and between given bounds

writePosCompResLy ( int t, int rs, int re, int cs, int ce, int lys, int lye ) : void

Write a piece of bit stream according to the COMP_POS_RES_LY_PROG progression mode and between given bounds

writeResLyCompPos ( int t, int rs, int re, int cs, int ce, int lys, int lye ) : void

Write a piece of bit stream according to the RES_LY_COMP_POS_PROG progression mode and between given bounds

writeResPosCompLy ( int t, int rs, int re, int cs, int ce, int lys, int lye ) : void

Write a piece of bit stream according to the RES_POS_COMP_LY_PROG progression mode and between given bounds

Private Methods

Method Description
buildAndWriteLayers ( ) : void

This method builds all the bit stream layers and then writes them to the output bit stream. Firstly it builds all the layers by computing the threshold according to the layer target bit-rate, and then it writes the layer bit streams according to the progressive type.

estimateLayerThreshold ( int targetBytes, EBCOTLayer lastLayer ) : float

This function attempts to estimate a rate-distortion slope threshold which will achieve a target number of code bytes close the `targetBytes' value.

findTruncIndices ( int layerIdx, int compIdx, int lvlIdx, int tileIdx, SubbandAn subb, float fthresh, int precinctIdx ) : void

This function finds the new truncation points indices for a packet. It does so by including the data from the code-blocks in the component, resolution level and tile, associated with a R-D slope which is larger than or equal to 'fthresh'.

getAllCodeBlocks ( ) : void

This method gets all the coded code-blocks from the EBCOT entropy coder for every component and every tile. Each coded code-block is stored in a 5D array according to the component, the resolution level, the tile, the subband it belongs and its position in the subband.

For each code-block, the valid slopes are computed and converted into the mantissa-exponent representation.

getLimitedSIndexFromSlope ( float slope ) : int

Returns the index of a slope for the summary table, limiting to the admissible values. The index is calculated as RD_SUMMARY_OFF plus the maximum exponent, base 2, that yields a value not larger than the slope itself.

If the value to return is lower than 0, 0 is returned. If it is larger than the maximum table index, then the maximum is returned.

getSlopeFromSIndex ( int index ) : float

Returns the minimum slope value associated with a summary table index. This minimum slope is just 2^(index-RD_SUMMARY_OFF).

optimizeBitstreamLayer ( int layerIdx, float fmaxt, int maxBytes, int prevBytes ) : float

This function implements the rate-distortion optimization algorithm. It saves the state of any previously generated bit-stream layers and then simulate the formation of a new layer in the bit stream as often as necessary to find the smallest rate-distortion threshold such that the total number of bytes required to represent the layer does not exceed `maxBytes' minus `prevBytes'. It then restores the state of any previously generated bit-stream layers and returns the threshold.

Method Details

EBCOTRateAllocator() public method

Initializes the EBCOT rate allocator of entropy coded data. The layout of layers, and their bitrate constraints, is specified by the 'lyrs' parameter.
public EBCOTRateAllocator ( CodedCBlkDataSrcEnc src, CSJ2K.j2k.entropy.encoder.LayersInfo lyrs, CSJ2K.j2k.codestream.writer.CodestreamWriter writer, CSJ2K.j2k.encoder.EncoderSpecs encSpec, CSJ2K.j2k.util.ParameterList pl ) : System
src CodedCBlkDataSrcEnc The source of entropy coded data. /// ///
lyrs CSJ2K.j2k.entropy.encoder.LayersInfo The layers layout specification. /// ///
writer CSJ2K.j2k.codestream.writer.CodestreamWriter The bit stream writer. /// ///
encSpec CSJ2K.j2k.encoder.EncoderSpecs
pl CSJ2K.j2k.util.ParameterList
return System

initialize() public method

Initializes the layers array. This must be called after the main header has been entirely written or simulated, so as to take its overhead into account. This method will get all the code-blocks and then initialize the target bitrates for each layer, according to the specifications.
public initialize ( ) : void
return void

runAndWrite() public method

Runs the rate allocation algorithm and writes the data to the bit stream writer object provided to the constructor.
public runAndWrite ( ) : void
return void

writeCompPosResLy() public method

Write a piece of bit stream according to the COMP_POS_RES_LY_PROG progression mode and between given bounds
public writeCompPosResLy ( int t, int rs, int re, int cs, int ce, int lys, int lye ) : void
t int Tile index. /// ///
rs int First resolution level index. /// ///
re int Last resolution level index. /// ///
cs int First component index. /// ///
ce int Last component index. /// ///
lys int First layer index for each component and resolution. /// ///
lye int Index of the last layer. /// ///
return void

writeLyResCompPos() public method

Write a piece of bit stream according to the LY_RES_COMP_POS_PROG progression mode and between given bounds
public writeLyResCompPos ( int t, int rs, int re, int cs, int ce, int lys, int lye ) : void
t int Tile index. /// ///
rs int First resolution level index. /// ///
re int Last resolution level index. /// ///
cs int First component index. /// ///
ce int Last component index. /// ///
lys int First layer index for each component and resolution. /// ///
lye int Index of the last layer. /// ///
return void

writePosCompResLy() public method

Write a piece of bit stream according to the COMP_POS_RES_LY_PROG progression mode and between given bounds
public writePosCompResLy ( int t, int rs, int re, int cs, int ce, int lys, int lye ) : void
t int Tile index. /// ///
rs int First resolution level index. /// ///
re int Last resolution level index. /// ///
cs int First component index. /// ///
ce int Last component index. /// ///
lys int First layer index for each component and resolution. /// ///
lye int Index of the last layer. /// ///
return void

writeResLyCompPos() public method

Write a piece of bit stream according to the RES_LY_COMP_POS_PROG progression mode and between given bounds
public writeResLyCompPos ( int t, int rs, int re, int cs, int ce, int lys, int lye ) : void
t int Tile index. /// ///
rs int First resolution level index. /// ///
re int Last resolution level index. /// ///
cs int First component index. /// ///
ce int Last component index. /// ///
lys int First layer index for each component and resolution. /// ///
lye int Index of the last layer. /// ///
return void

writeResPosCompLy() public method

Write a piece of bit stream according to the RES_POS_COMP_LY_PROG progression mode and between given bounds
public writeResPosCompLy ( int t, int rs, int re, int cs, int ce, int lys, int lye ) : void
t int Tile index. /// ///
rs int First resolution level index. /// ///
re int Last resolution level index. /// ///
cs int First component index. /// ///
ce int Last component index. /// ///
lys int First layer index for each component and resolution. /// ///
lye int Last layer index. /// ///
return void