pt.tumba.ngram.compression
Class ArithDecoder

java.lang.Object
  extended by pt.tumba.ngram.compression.ArithDecoder

public final class ArithDecoder
extends java.lang.Object

Performs arithmetic decoding, converting bit input into cumulative probability interval output. Returns probabilities as integer counts low, high and total, with the range being [low/total,high/total).

Author:
Bruno Martins
See Also:
ArithEncoder, BitInput

Field Summary
private  int _bufferedBits
          Number of bits that have been buffered.
private  boolean _endOfStream
          Value will be true if the end of stream has been reached.
protected  long _high
          The high bound on the current interval for coding.
private  BitInput _in
          Input stream from which to read bits.
protected  long _low
          The low bound on the current interval for coding.
private  long _value
          Current bits for decoding.
protected static int CODE_VALUE_BITS
          Precision of coding, expressed in number of bits used for arithmetic before shifting out partial results.
protected static long FIRST_QUARTER
          1/4 of the largest possible value plus one.
protected static long HALF
          1/2 of the largest possible value; 2 * FIRST_QUARTER
protected static long THIRD_QUARTER
          3/4 of the largest possible value; 3 * FIRST_QUARTER
protected static long TOP_VALUE
          The largest possible interval value.
 
Constructor Summary
ArithDecoder(BitInput in)
          Construct an arithmetic decoder that reads from the given bit input.
ArithDecoder(java.io.InputStream in)
          Construct an arithmetic decoder that reads from the given input stream.
 
Method Summary
private  void bufferBit()
          Reads a bit from the underlying bit input stream and buffers it.
 void close()
          Closes underlying bit output.
 boolean endOfStream()
          Returns true if the end of stream has been reached and there are no more symbols to decode.
 int getCurrentSymbolCount(int totalCount)
          Returns a count for the current symbol that will be between the low and high counts for the symbol in the model given the total count.
 void removeSymbolFromStream(int[] counts)
          Removes a symbol from the input stream that was coded with counts { low, high, total }.
 void removeSymbolFromStream(long lowCount, long highCount, long totalCount)
          Removes a symbol from the input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_low

protected long _low
The low bound on the current interval for coding. Initialized to zero.


_high

protected long _high
The high bound on the current interval for coding. Initialized to top value possible.


CODE_VALUE_BITS

protected static final int CODE_VALUE_BITS
Precision of coding, expressed in number of bits used for arithmetic before shifting out partial results.

See Also:
Constant Field Values

TOP_VALUE

protected static final long TOP_VALUE
The largest possible interval value. All 1s.

See Also:
Constant Field Values

FIRST_QUARTER

protected static final long FIRST_QUARTER
1/4 of the largest possible value plus one.

See Also:
Constant Field Values

HALF

protected static final long HALF
1/2 of the largest possible value; 2 * FIRST_QUARTER

See Also:
Constant Field Values

THIRD_QUARTER

protected static final long THIRD_QUARTER
3/4 of the largest possible value; 3 * FIRST_QUARTER

See Also:
Constant Field Values

_in

private final BitInput _in
Input stream from which to read bits.


_value

private long _value
Current bits for decoding.


_endOfStream

private boolean _endOfStream
Value will be true if the end of stream has been reached.


_bufferedBits

private int _bufferedBits
Number of bits that have been buffered.

Constructor Detail

ArithDecoder

public ArithDecoder(BitInput in)
             throws java.io.IOException
Construct an arithmetic decoder that reads from the given bit input.

Parameters:
in - Bit input from which to read bits.
Throws:
java.io.IOException - If there is an exception buffering from the bit input stream.

ArithDecoder

public ArithDecoder(java.io.InputStream in)
             throws java.io.IOException
Construct an arithmetic decoder that reads from the given input stream.

Parameters:
in - Input stream from which to read.
Throws:
java.io.IOException - If there is an exception buffering from input stream.
Method Detail

endOfStream

public boolean endOfStream()
Returns true if the end of stream has been reached and there are no more symbols to decode.

Returns:
true if the end of stream has been reached.

getCurrentSymbolCount

public int getCurrentSymbolCount(int totalCount)
Returns a count for the current symbol that will be between the low and high counts for the symbol in the model given the total count. Once symbol is retrieved, the model is used to compute the actual low, high and total counts and removeSymbolFromStream(int[]) is called.

Parameters:
totalCount - The current total count for the model.
Returns:
A count that is in the range above or equal to the low count and less than the high count of the next symbol decoded.

removeSymbolFromStream

public void removeSymbolFromStream(int[] counts)
                            throws java.io.IOException
Removes a symbol from the input stream that was coded with counts { low, high, total }. Called after getCurrentSymbolCount(int).

Parameters:
counts - Array of low, high and total count used to code the symbol.
Throws:
java.io.IOException - If there is an exception in buffering input from the underlying input stream.
See Also:
removeSymbolFromStream(long,long,long)

removeSymbolFromStream

public void removeSymbolFromStream(long lowCount,
                                   long highCount,
                                   long totalCount)
                            throws java.io.IOException
Removes a symbol from the input stream. Called after getCurrentSymbolCount(int).

Parameters:
lowCount - Cumulative count for symbols indexed below symbol to be removed.
highCount - lowCount plus count for this symbol.
totalCount - Total count for all symbols seen.
Throws:
java.io.IOException - If there is an exception in buffering input from the underlying input stream.

close

public void close()
           throws java.io.IOException
Closes underlying bit output.

Throws:
java.io.IOException - If there is an underlying I/O exception in the bit input.

bufferBit

private void bufferBit()
                throws java.io.IOException
Reads a bit from the underlying bit input stream and buffers it.

Throws:
java.io.IOException - If there is an IOException buffering from the underlying bit stream.