Class RangeCoder
java.lang.Object
htsjdk.samtools.cram.compression.range.RangeCoder
Arithmetic range coder used by the CRAM 3.1 Range (adaptive arithmetic) codec and FQZComp quality
score codec. Implements both encoding and decoding using a 32-bit range with carry propagation
for output byte generation.
The range coder maintains a probability interval [low, low+range) and narrows it for each symbol based on cumulative and symbol frequencies. When the range becomes too small (< 2^24), it renormalizes by shifting out the top byte.
Encoding output is written to an internal byte[] buffer (set via setOutput(byte[], int))
rather than a ByteBuffer, eliminating bounds checking and position tracking overhead
in the hot encoding loop.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintReturn the current write position in the output buffer.protected voidrangeDecode(ByteBuffer inBuffer, int cumulativeFrequency, int symbolFrequency) Update the decoder state after a symbol has been decoded.voidrangeDecodeStart(ByteBuffer inBuffer) Initialize the decoder by reading the first 5 bytes of the compressed stream into the code register.protected voidrangeEncode(int cumulativeFrequency, int symbolFrequency, int totalFrequency) Encode a symbol by narrowing the range interval and emitting output bytes as needed.voidFlush the encoder state by emitting the final 5 bytes.protected intrangeGetFrequency(int totalFrequency) Compute the scaled frequency for symbol lookup during decoding.voidsetOutput(byte[] buf, int pos) Set the output buffer for encoding.
-
Constructor Details
-
RangeCoder
public RangeCoder()
-
-
Method Details
-
setOutput
public void setOutput(byte[] buf, int pos) Set the output buffer for encoding. Must be called before any encode operations.- Parameters:
buf- the byte array to write compressed output topos- the starting write position in the buffer
-
getOutputPosition
public int getOutputPosition()Return the current write position in the output buffer. Call after encoding is complete to determine how many bytes were written. -
rangeDecodeStart
Initialize the decoder by reading the first 5 bytes of the compressed stream into the code register. Must be called before any calls toByteModel.modelDecode(ByteBuffer, RangeCoder).- Parameters:
inBuffer- the compressed input stream
-
rangeDecode
Update the decoder state after a symbol has been decoded.- Parameters:
inBuffer- the compressed input stream (for renormalization reads)cumulativeFrequency- cumulative frequency of symbols before the decoded symbolsymbolFrequency- frequency of the decoded symbol
-
rangeGetFrequency
protected int rangeGetFrequency(int totalFrequency) Compute the scaled frequency for symbol lookup during decoding.- Parameters:
totalFrequency- the sum of all symbol frequencies- Returns:
- the scaled frequency value used to identify the decoded symbol
-
rangeEncode
protected void rangeEncode(int cumulativeFrequency, int symbolFrequency, int totalFrequency) Encode a symbol by narrowing the range interval and emitting output bytes as needed. Output is written to the internal byte[] buffer (set viasetOutput(byte[], int)).- Parameters:
cumulativeFrequency- cumulative frequency of all symbols before this onesymbolFrequency- frequency of the symbol being encodedtotalFrequency- sum of all symbol frequencies
-
rangeEncodeEnd
public void rangeEncodeEnd()Flush the encoder state by emitting the final 5 bytes. Must be called after all symbols have been encoded to produce a valid compressed stream.
-