Top Description Fields Constructors Methods
io.netty5.handler.codec.compression

pack-priv final Class Bzip2HuffmanStageEncoder

extends Object
Class Inheritance
Imports
io.netty5.buffer.Buffer, java.util.Arrays

An encoder for the Bzip2 Huffman encoding stage.

Field Summary

Modifier and TypeField and Description
private static final int
HUFFMAN_HIGH_SYMBOL_COST

Used in initial Huffman table generation.

private final int[][]
huffmanCodeLengths

The Canonical Huffman code lengths for each table.

private final int[][]
huffmanMergedCodeSymbols

Merged code symbols for each table.

private final int
mtfAlphabetSize

The number of unique values in the mtfBlock array.

private final char[]
mtfBlock

The output of the Move To Front Transform and Run Length Encoding[2] stages.

private final int
mtfLength

The actual number of values contained in the mtfBlock array.

private final int[]
mtfSymbolFrequencies

The global frequencies of values within the mtfBlock array.

private final byte[]
selectors

The selectors for each segment.

private final Bzip2BitWriter
writer

The Bzip2BitWriter to which the Huffman tables and data is written.

Constructor Summary

AccessConstructor and Description
pack-priv
Bzip2HuffmanStageEncoder(final Bzip2BitWriter
The Bzip2BitWriter which provides bit-level writes
writer
,
final char[]
The MTF block data
mtfBlock
,
final int
The actual length of the MTF block
mtfLength
,
final int
The size of the MTF block's alphabet
mtfAlphabetSize
,
final int[]
The frequencies the MTF block's symbols
mtfSymbolFrequencies
)

Method Summary

Modifier and TypeMethod and Description
private void
assignHuffmanCodeSymbols()

Assigns Canonical Huffman codes based on the calculated lengths.

pack-priv void
encode(Buffer out)

Encodes and writes the block data.

private static void
generateHuffmanCodeLengths(final int
The total number of symbols
alphabetSize
,
final int[]
The frequencies of the symbols
symbolFrequencies
,
final int[]
The array to which the generated code lengths should be written
codeLengths
)

Generate a Huffman code length table for a given list of symbol frequencies.

private void
generateHuffmanOptimisationSeeds()

Generate initial Huffman code length tables, giving each table a different low cost section of the alphabet that is roughly equal in overall cumulative frequency.

private void
optimiseSelectorsAndHuffmanTables(final boolean
If true, write out the (final) chosen selectors
storeSelectors
)

Co-optimise the selector list and the alternative Huffman table code lengths.

private static int

Returns:

The selected table count
selectTableCount
(final int
The length to select a table count for
mtfLength
)

Selects an appropriate table count for a given MTF length.

private void
writeBlockData(Buffer out)

Writes out the encoded block data.

private void
writeSelectorsAndHuffmanTables(Buffer out)

Write out the selector list and Huffman tables.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

HUFFMAN_HIGH_SYMBOL_COSTback to summary
private static final int HUFFMAN_HIGH_SYMBOL_COST

Used in initial Huffman table generation.

huffmanCodeLengthsback to summary
private final int[][] huffmanCodeLengths

The Canonical Huffman code lengths for each table.

huffmanMergedCodeSymbolsback to summary
private final int[][] huffmanMergedCodeSymbols

Merged code symbols for each table. The value at each position is ((code length << 24) | code).

mtfAlphabetSizeback to summary
private final int mtfAlphabetSize

The number of unique values in the mtfBlock array.

mtfBlockback to summary
private final char[] mtfBlock

The output of the Move To Front Transform and Run Length Encoding[2] stages.

mtfLengthback to summary
private final int mtfLength

The actual number of values contained in the mtfBlock array.

mtfSymbolFrequenciesback to summary
private final int[] mtfSymbolFrequencies

The global frequencies of values within the mtfBlock array.

selectorsback to summary
private final byte[] selectors

The selectors for each segment.

writerback to summary
private final Bzip2BitWriter writer

The Bzip2BitWriter to which the Huffman tables and data is written.

Constructor Detail

Bzip2HuffmanStageEncoderback to summary
pack-priv Bzip2HuffmanStageEncoder(final Bzip2BitWriter writer, final char[] mtfBlock, final int mtfLength, final int mtfAlphabetSize, final int[] mtfSymbolFrequencies)
Parameters
writer:Bzip2BitWriter

The Bzip2BitWriter which provides bit-level writes

mtfBlock:char[]

The MTF block data

mtfLength:int

The actual length of the MTF block

mtfAlphabetSize:int

The size of the MTF block's alphabet

mtfSymbolFrequencies:int[]

The frequencies the MTF block's symbols

Method Detail

assignHuffmanCodeSymbolsback to summary
private void assignHuffmanCodeSymbols()

Assigns Canonical Huffman codes based on the calculated lengths.

encodeback to summary
pack-priv void encode(Buffer out)

Encodes and writes the block data.

generateHuffmanCodeLengthsback to summary
private static void generateHuffmanCodeLengths(final int alphabetSize, final int[] symbolFrequencies, final int[] codeLengths)

Generate a Huffman code length table for a given list of symbol frequencies.

Parameters
alphabetSize:int

The total number of symbols

symbolFrequencies:int[]

The frequencies of the symbols

codeLengths:int[]

The array to which the generated code lengths should be written

generateHuffmanOptimisationSeedsback to summary
private void generateHuffmanOptimisationSeeds()

Generate initial Huffman code length tables, giving each table a different low cost section of the alphabet that is roughly equal in overall cumulative frequency. Note that the initial tables are invalid for actual Huffman code generation, and only serve as the seed for later iterative optimisation in optimiseSelectorsAndHuffmanTables(boolean).

optimiseSelectorsAndHuffmanTablesback to summary
private void optimiseSelectorsAndHuffmanTables(final boolean storeSelectors)

Co-optimise the selector list and the alternative Huffman table code lengths. This method is called repeatedly in the hope that the total encoded size of the selectors, the Huffman code lengths and the block data encoded with them will converge towards a minimum.
If the data is highly incompressible, it is possible that the total encoded size will instead diverge (increase) slightly.

Parameters
storeSelectors:boolean

If true, write out the (final) chosen selectors

selectTableCountback to summary
private static int selectTableCount(final int mtfLength)

Selects an appropriate table count for a given MTF length.

Parameters
mtfLength:int

The length to select a table count for

Returns:int

The selected table count

writeBlockDataback to summary
private void writeBlockData(Buffer out)

Writes out the encoded block data.

writeSelectorsAndHuffmanTablesback to summary
private void writeSelectorsAndHuffmanTables(Buffer out)

Write out the selector list and Huffman tables.