1 /* Copyright 2015 Google Inc. All Rights Reserved. 2 3 Distributed under MIT license. 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 5 */ 6 7 package org.brotli.dec; 8 9 /** 10 * Utilities for building Huffman decoding tables. 11 */ 12 final class Huffman { 13 14 private static final int MAX_LENGTH = 15; 15 16 /** 17 * Returns reverse(reverse(key, len) + 1, len). 18 * 19 * <p> reverse(key, len) is the bit-wise reversal of the len least significant bits of key. 20 */ getNextKey(int key, int len)21 private static int getNextKey(int key, int len) { 22 int step = 1 << (len - 1); 23 while ((key & step) != 0) { 24 step >>= 1; 25 } 26 return (key & (step - 1)) + step; 27 } 28 29 /** 30 * Stores {@code item} in {@code table[0], table[step], table[2 * step] .., table[end]}. 31 * 32 * <p> Assumes that end is an integer multiple of step. 33 */ replicateValue(int[] table, int offset, int step, int end, int item)34 private static void replicateValue(int[] table, int offset, int step, int end, int item) { 35 do { 36 end -= step; 37 table[offset + end] = item; 38 } while (end > 0); 39 } 40 41 /** 42 * @param count histogram of bit lengths for the remaining symbols, 43 * @param len code length of the next processed symbol. 44 * @return table width of the next 2nd level table. 45 */ nextTableBitSize(int[] count, int len, int rootBits)46 private static int nextTableBitSize(int[] count, int len, int rootBits) { 47 int left = 1 << (len - rootBits); 48 while (len < MAX_LENGTH) { 49 left -= count[len]; 50 if (left <= 0) { 51 break; 52 } 53 len++; 54 left <<= 1; 55 } 56 return len - rootBits; 57 } 58 59 /** 60 * Builds Huffman lookup table assuming code lengths are in symbol order. 61 */ buildHuffmanTable(int[] rootTable, int tableOffset, int rootBits, int[] codeLengths, int codeLengthsSize)62 static void buildHuffmanTable(int[] rootTable, int tableOffset, int rootBits, int[] codeLengths, 63 int codeLengthsSize) { 64 int key; // Reversed prefix code. 65 int[] sorted = new int[codeLengthsSize]; // Symbols sorted by code length. 66 // TODO: fill with zeroes? 67 int[] count = new int[MAX_LENGTH + 1]; // Number of codes of each length. 68 int[] offset = new int[MAX_LENGTH + 1]; // Offsets in sorted table for each length. 69 int symbol; 70 71 // Build histogram of code lengths. 72 for (symbol = 0; symbol < codeLengthsSize; symbol++) { 73 count[codeLengths[symbol]]++; 74 } 75 76 // Generate offsets into sorted symbol table by code length. 77 offset[1] = 0; 78 for (int len = 1; len < MAX_LENGTH; len++) { 79 offset[len + 1] = offset[len] + count[len]; 80 } 81 82 // Sort symbols by length, by symbol order within each length. 83 for (symbol = 0; symbol < codeLengthsSize; symbol++) { 84 if (codeLengths[symbol] != 0) { 85 sorted[offset[codeLengths[symbol]]++] = symbol; 86 } 87 } 88 89 int tableBits = rootBits; 90 int tableSize = 1 << tableBits; 91 int totalSize = tableSize; 92 93 // Special case code with only one value. 94 if (offset[MAX_LENGTH] == 1) { 95 for (key = 0; key < totalSize; key++) { 96 rootTable[tableOffset + key] = sorted[0]; 97 } 98 return; 99 } 100 101 // Fill in root table. 102 key = 0; 103 symbol = 0; 104 for (int len = 1, step = 2; len <= rootBits; len++, step <<= 1) { 105 for (; count[len] > 0; count[len]--) { 106 replicateValue(rootTable, tableOffset + key, step, tableSize, len << 16 | sorted[symbol++]); 107 key = getNextKey(key, len); 108 } 109 } 110 111 // Fill in 2nd level tables and add pointers to root table. 112 int mask = totalSize - 1; 113 int low = -1; 114 int currentOffset = tableOffset; 115 for (int len = rootBits + 1, step = 2; len <= MAX_LENGTH; len++, step <<= 1) { 116 for (; count[len] > 0; count[len]--) { 117 if ((key & mask) != low) { 118 currentOffset += tableSize; 119 tableBits = nextTableBitSize(count, len, rootBits); 120 tableSize = 1 << tableBits; 121 totalSize += tableSize; 122 low = key & mask; 123 rootTable[tableOffset + low] = 124 (tableBits + rootBits) << 16 | (currentOffset - tableOffset - low); 125 } 126 replicateValue(rootTable, currentOffset + (key >> rootBits), step, tableSize, 127 (len - rootBits) << 16 | sorted[symbol++]); 128 key = getNextKey(key, len); 129 } 130 } 131 } 132 } 133