jj2000.j2k.entropy.decoder
Class MQDecoder

java.lang.Object
  |
  +--jj2000.j2k.entropy.decoder.MQDecoder

public class MQDecoder
extends java.lang.Object

This class implements the MQ arithmetic decoder. It is implemented using the software conventions decoder for better performance (i.e. execution time performance). The initial states for each context of the MQ-coder are specified in the constructor.


Field Summary
(package private)  int a
          The current interval
(package private)  int b
          The last byte read
(package private)  int c
          The current bit code
(package private)  int cT
          The bit code counter
(package private)  int[] I
          The current index of each context
(package private)  ByteInputBuffer in
          The ByteInputBuffer used to read the compressed bit stream.
(package private)  int[] initStates
          The initial state of each context
(package private)  boolean markerFound
          Flag indicating if a marker has been found
(package private)  int[] mPS
          The current most probable signal for each context
(package private) static int[] nLPS
          The indexes of the next LPS
(package private) static int[] nMPS
          The indexes of the next MPS
(package private) static int[] qe
          The data structures containing the probabilities for the LPS
(package private) static int[] switchLM
          Whether LPS and MPS should be switched
 
Constructor Summary
MQDecoder(ByteInputBuffer iStream, int nrOfContexts, int[] initStates)
          Instantiates a new MQ-decoder, with the specified number of contexts and initial states.
 
Method Summary
private  void byteIn()
          This function gets one byte of compressed bits from the in-stream.
 boolean checkPredTerm()
          Checks for past errors in the decoding process using the predictable error resilient termination.
 int decodeSymbol(int context)
          Arithmetically decodes one symbol from the bit stream with the given context and returns its decoded value.
 void decodeSymbols(int[] bits, int[] cX, int n)
          This function performs the arithmetic decoding.
 boolean fastDecodeSymbols(int[] bits, int ctxt, int n)
          Decodes 'n' symbols from the bit stream using the same context 'ctxt'.
 ByteInputBuffer getByteInputBuffer()
          Returns the underlying 'ByteInputBuffer' from where the MQ coded input bytes are read.
 int getNumCtxts()
          Returns the number of contexts in the arithmetic coder.
private  void init()
          Initializes the state of the MQ coder, without modifying the current context states.
 void nextSegment(byte[] buf, int off, int len)
          Resets the MQ decoder to start a new segment.
 void resetCtxt(int c)
          Resets a context to the original probability distribution.
 void resetCtxts()
          Resets a context to the original probability distribution.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

qe

static final int[] qe
The data structures containing the probabilities for the LPS


nMPS

static final int[] nMPS
The indexes of the next MPS


nLPS

static final int[] nLPS
The indexes of the next LPS


switchLM

static final int[] switchLM
Whether LPS and MPS should be switched


in

ByteInputBuffer in
The ByteInputBuffer used to read the compressed bit stream.


mPS

int[] mPS
The current most probable signal for each context


I

int[] I
The current index of each context


c

int c
The current bit code


cT

int cT
The bit code counter


a

int a
The current interval


b

int b
The last byte read


markerFound

boolean markerFound
Flag indicating if a marker has been found


initStates

final int[] initStates
The initial state of each context

Constructor Detail

MQDecoder

public MQDecoder(ByteInputBuffer iStream,
                 int nrOfContexts,
                 int[] initStates)
Instantiates a new MQ-decoder, with the specified number of contexts and initial states. The compressed bytestream is read from the 'iStream' object.

Parameters:
iStream - the stream that contains the coded bits
nrOfContexts - The number of contexts used
initStates - The initial state for each context. A reference is kept to this array to reinitialize the contexts whenever 'reset()' or 'resetCtxts()' is called.
Method Detail

fastDecodeSymbols

public final boolean fastDecodeSymbols(int[] bits,
                                       int ctxt,
                                       int n)
Decodes 'n' symbols from the bit stream using the same context 'ctxt'. If possible the MQ-coder speedup mode will be used to speed up decoding. The speedup mode is used if Q (the LPS probability for 'ctxt' is low enough) and the A and C registers permit decoding several MPS symbols without renormalization.

Speedup mode should be used when decoding long runs of MPS with high probability with the same context.

This methiod will return the decoded symbols differently if speedup mode was used or not. If true is returned, then speedup mode was used and the 'n' decoded symbols are all the same and it is returned ain bits[0] only. If false is returned then speedup mode was not used, the decoded symbols are probably not all the same and they are returned in bits[0], bits[1], ... bits[n-1].

Parameters:
bits - The array where to put the decoded symbols. Must be of length 'n' or more.
ctxt - The context to use in decoding the symbols.
n - The number of symbols to decode.
Returns:
True if speedup mode was used, false if not. If speedup mode was used then all the decoded symbols are the same and its value is returned in 'bits[0]' only (not in bits[1], bits[2], etc.).

decodeSymbols

public final void decodeSymbols(int[] bits,
                                int[] cX,
                                int n)
This function performs the arithmetic decoding. The function receives an array in which to put the decoded symbols and an array of contexts with which to decode them.

Each context has a current MPS and an index describing what the current probability is for the LPS. Each bit is decoded and if the probability of the LPS exceeds .5, the MPS and LPS are switched.

Parameters:
bits - The array where to place the decoded symbols. It should be long enough to contain 'n' elements.
cX - The context to use in decoding each symbol.
n - The number of symbols to decode

decodeSymbol

public final int decodeSymbol(int context)
Arithmetically decodes one symbol from the bit stream with the given context and returns its decoded value.

Each context has a current MPS and an index describing what the current probability is for the LPS. Each bit is encoded and if the probability of the LPS exceeds .5, the MPS and LPS are switched.

Parameters:
context - The context to use in decoding the symbol
Returns:
The decoded symbol, 0 or 1.

checkPredTerm

public boolean checkPredTerm()
Checks for past errors in the decoding process using the predictable error resilient termination. This works only if the encoder used the predictable error resilient MQ termination, otherwise it reports wrong results. If an error is detected it means that the MQ bit stream has been wrongly decoded or that the MQ terminated segment length is too long. If no errors are detected it does not necessarily mean that the MQ bit stream has been correctly decoded.

Returns:
True if errors are found, false otherwise.

byteIn

private void byteIn()
This function gets one byte of compressed bits from the in-stream. the byte is added to c. If the byte is 0xFF and the next byte is greater than 0x8F, the byte after 0xFF is a marker.


getNumCtxts

public final int getNumCtxts()
Returns the number of contexts in the arithmetic coder.

Returns:
The number of contexts

resetCtxt

public final void resetCtxt(int c)
Resets a context to the original probability distribution.

Parameters:
c - The number of the context (it starts at 0).

resetCtxts

public final void resetCtxts()
Resets a context to the original probability distribution. The original probability distribution depends on the actual implementation of the arithmetic coder or decoder.


nextSegment

public final void nextSegment(byte[] buf,
                              int off,
                              int len)
Resets the MQ decoder to start a new segment. This is like recreating a new MQDecoder object with new input data.

Parameters:
buf - The byte array containing the MQ encoded data. If null the current byte array is assumed.
off - The index of the first element in 'buf' to be decoded. If negative the byte just after the previous segment is assumed, only valid if 'buf' is null.
len - The number of bytes in 'buf' to be decoded. Any subsequent bytes are taken to be 0xFF.

getByteInputBuffer

public ByteInputBuffer getByteInputBuffer()
Returns the underlying 'ByteInputBuffer' from where the MQ coded input bytes are read.

Returns:
The underlying ByteInputBuffer.

init

private void init()
Initializes the state of the MQ coder, without modifying the current context states. It sets the registers (A,C,B) and the "marker found" state to the initial state, to start the decoding of a new segment.

To have a complete reset of the MQ (as if a new MQDecoder object was created) 'resetCtxts()' should be called after this method.