Class Bzip2HuffmanStageEncoder
java.lang.Object
io.netty.handler.codec.compression.Bzip2HuffmanStageEncoder
An encoder for the Bzip2 Huffman encoding stage.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
Used in initial Huffman table generation.private final int[][]
The Canonical Huffman code lengths for each table.private final int[][]
Merged code symbols for each table.private final int
The number of unique values in themtfBlock
array.private final char[]
The output of the Move To Front Transform and Run Length Encoding[2] stages.private final int
The actual number of values contained in themtfBlock
array.private final int[]
The global frequencies of values within themtfBlock
array.private final byte[]
The selectors for each segment.private final Bzip2BitWriter
TheBzip2BitWriter
to which the Huffman tables and data is written. -
Constructor Summary
ConstructorsConstructorDescriptionBzip2HuffmanStageEncoder
(Bzip2BitWriter writer, char[] mtfBlock, int mtfLength, int mtfAlphabetSize, int[] mtfSymbolFrequencies) -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Assigns Canonical Huffman codes based on the calculated lengths.(package private) void
Encodes and writes the block data.private static void
generateHuffmanCodeLengths
(int alphabetSize, int[] symbolFrequencies, int[] codeLengths) Generate a Huffman code length table for a given list of symbol frequencies.private void
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
(boolean storeSelectors) Co-optimise the selector list and the alternative Huffman table code lengths.private static int
selectTableCount
(int mtfLength) Selects an appropriate table count for a given MTF length.private void
writeBlockData
(ByteBuf out) Writes out the encoded block data.private void
Write out the selector list and Huffman tables.
-
Field Details
-
HUFFMAN_HIGH_SYMBOL_COST
private static final int HUFFMAN_HIGH_SYMBOL_COSTUsed in initial Huffman table generation.- See Also:
-
writer
TheBzip2BitWriter
to which the Huffman tables and data is written. -
mtfBlock
private final char[] mtfBlockThe output of the Move To Front Transform and Run Length Encoding[2] stages. -
mtfLength
private final int mtfLengthThe actual number of values contained in themtfBlock
array. -
mtfAlphabetSize
private final int mtfAlphabetSizeThe number of unique values in themtfBlock
array. -
mtfSymbolFrequencies
private final int[] mtfSymbolFrequenciesThe global frequencies of values within themtfBlock
array. -
huffmanCodeLengths
private final int[][] huffmanCodeLengthsThe Canonical Huffman code lengths for each table. -
huffmanMergedCodeSymbols
private final int[][] huffmanMergedCodeSymbolsMerged code symbols for each table. The value at each position is ((code length invalid input: '<'invalid input: '<' 24) | code). -
selectors
private final byte[] selectorsThe selectors for each segment.
-
-
Constructor Details
-
Bzip2HuffmanStageEncoder
Bzip2HuffmanStageEncoder(Bzip2BitWriter writer, char[] mtfBlock, int mtfLength, int mtfAlphabetSize, int[] mtfSymbolFrequencies) - Parameters:
writer
- TheBzip2BitWriter
which provides bit-level writesmtfBlock
- The MTF block datamtfLength
- The actual length of the MTF blockmtfAlphabetSize
- The size of the MTF block's alphabetmtfSymbolFrequencies
- The frequencies the MTF block's symbols
-
-
Method Details
-
selectTableCount
private static int selectTableCount(int mtfLength) Selects an appropriate table count for a given MTF length.- Parameters:
mtfLength
- The length to select a table count for- Returns:
- The selected table count
-
generateHuffmanCodeLengths
private static void generateHuffmanCodeLengths(int alphabetSize, int[] symbolFrequencies, int[] codeLengths) Generate a Huffman code length table for a given list of symbol frequencies.- Parameters:
alphabetSize
- The total number of symbolssymbolFrequencies
- The frequencies of the symbolscodeLengths
- The array to which the generated code lengths should be written
-
generateHuffmanOptimisationSeeds
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 inoptimiseSelectorsAndHuffmanTables(boolean)
. -
optimiseSelectorsAndHuffmanTables
private void optimiseSelectorsAndHuffmanTables(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
- Iftrue
, write out the (final) chosen selectors
-
assignHuffmanCodeSymbols
private void assignHuffmanCodeSymbols()Assigns Canonical Huffman codes based on the calculated lengths. -
writeSelectorsAndHuffmanTables
Write out the selector list and Huffman tables. -
writeBlockData
Writes out the encoded block data. -
encode
Encodes and writes the block data.
-