OLD | NEW |
1 /* Copyright 2013 Google Inc. All Rights Reserved. | 1 /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | 2 |
3 Distributed under MIT license. | 3 Distributed under MIT license. |
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 */ | 5 */ |
6 | 6 |
7 /* Utilities for building Huffman decoding tables. */ | 7 /* Utilities for building Huffman decoding tables. */ |
8 | 8 |
9 #include "./huffman.h" | 9 #include "./huffman.h" |
10 | 10 |
11 #include <string.h> /* memcpy, memset */ | 11 #include <string.h> /* memcpy, memset */ |
12 | 12 |
| 13 #include "../common/constants.h" |
| 14 #include <brotli/types.h> |
13 #include "./port.h" | 15 #include "./port.h" |
14 #include "./types.h" | |
15 | 16 |
16 #if defined(__cplusplus) || defined(c_plusplus) | 17 #if defined(__cplusplus) || defined(c_plusplus) |
17 extern "C" { | 18 extern "C" { |
18 #endif | 19 #endif |
19 | 20 |
20 #define BROTLI_REVERSE_BITS_MAX 8 | 21 #define BROTLI_REVERSE_BITS_MAX 8 |
21 | 22 |
22 #ifdef BROTLI_RBIT | 23 #ifdef BROTLI_RBIT |
23 #define BROTLI_REVERSE_BITS_BASE (32 - BROTLI_REVERSE_BITS_MAX) | 24 #define BROTLI_REVERSE_BITS_BASE \ |
| 25 ((sizeof(reg_t) << 3) - BROTLI_REVERSE_BITS_MAX) |
24 #else | 26 #else |
25 #define BROTLI_REVERSE_BITS_BASE 0 | 27 #define BROTLI_REVERSE_BITS_BASE 0 |
26 static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = { | 28 static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = { |
27 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, | 29 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, |
28 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, | 30 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, |
29 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, | 31 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, |
30 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, | 32 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, |
31 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, | 33 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, |
32 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, | 34 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, |
33 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, | 35 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, |
(...skipping 19 matching lines...) Expand all Loading... |
53 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, | 55 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, |
54 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, | 56 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, |
55 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, | 57 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, |
56 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, | 58 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, |
57 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, | 59 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, |
58 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF | 60 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF |
59 }; | 61 }; |
60 #endif /* BROTLI_RBIT */ | 62 #endif /* BROTLI_RBIT */ |
61 | 63 |
62 #define BROTLI_REVERSE_BITS_LOWEST \ | 64 #define BROTLI_REVERSE_BITS_LOWEST \ |
63 (1U << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE)) | 65 ((reg_t)1 << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE)) |
64 | 66 |
65 /* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX), | 67 /* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX), |
66 where reverse(value, len) is the bit-wise reversal of the len least | 68 where reverse(value, len) is the bit-wise reversal of the len least |
67 significant bits of value. */ | 69 significant bits of value. */ |
68 static BROTLI_INLINE uint32_t BrotliReverseBits(uint32_t num) { | 70 static BROTLI_INLINE reg_t BrotliReverseBits(reg_t num) { |
69 #ifdef BROTLI_RBIT | 71 #ifdef BROTLI_RBIT |
70 return BROTLI_RBIT(num); | 72 return BROTLI_RBIT(num); |
71 #else | 73 #else |
72 return kReverseBits[num]; | 74 return kReverseBits[num]; |
73 #endif | 75 #endif |
74 } | 76 } |
75 | 77 |
76 /* Stores code in table[0], table[step], table[2*step], ..., table[end] */ | 78 /* Stores code in table[0], table[step], table[2*step], ..., table[end] */ |
77 /* Assumes that end is an integer multiple of step */ | 79 /* Assumes that end is an integer multiple of step */ |
78 static BROTLI_INLINE void ReplicateValue(HuffmanCode* table, | 80 static BROTLI_INLINE void ReplicateValue(HuffmanCode* table, |
(...skipping 16 matching lines...) Expand all Loading... |
95 if (left <= 0) break; | 97 if (left <= 0) break; |
96 ++len; | 98 ++len; |
97 left <<= 1; | 99 left <<= 1; |
98 } | 100 } |
99 return len - root_bits; | 101 return len - root_bits; |
100 } | 102 } |
101 | 103 |
102 void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, | 104 void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, |
103 const uint8_t* const code_lengths, | 105 const uint8_t* const code_lengths, |
104 uint16_t* count) { | 106 uint16_t* count) { |
105 HuffmanCode code; /* current table entry */ | 107 HuffmanCode code; /* current table entry */ |
106 int symbol; /* symbol index in original or sorted table */ | 108 int symbol; /* symbol index in original or sorted table */ |
107 uint32_t key; /* prefix code */ | 109 reg_t key; /* prefix code */ |
108 uint32_t key_step; /* prefix code addend */ | 110 reg_t key_step; /* prefix code addend */ |
109 int step; /* step size to replicate values in current table */ | 111 int step; /* step size to replicate values in current table */ |
110 int table_size; /* size of current table */ | 112 int table_size; /* size of current table */ |
111 int sorted[18]; /* symbols sorted by code length */ | 113 int sorted[BROTLI_CODE_LENGTH_CODES]; /* symbols sorted by code length */ |
112 /* offsets in sorted table for each length */ | 114 /* offsets in sorted table for each length */ |
113 int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1]; | 115 int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1]; |
114 int bits; | 116 int bits; |
115 int bits_count; | 117 int bits_count; |
116 BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <= | 118 BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <= |
117 BROTLI_REVERSE_BITS_MAX); | 119 BROTLI_REVERSE_BITS_MAX); |
118 | 120 |
119 /* generate offsets into sorted symbol table by code length */ | 121 /* generate offsets into sorted symbol table by code length */ |
120 symbol = -1; | 122 symbol = -1; |
121 bits = 1; | 123 bits = 1; |
122 BROTLI_REPEAT(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH, { | 124 BROTLI_REPEAT(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH, { |
123 symbol += count[bits]; | 125 symbol += count[bits]; |
124 offset[bits] = symbol; | 126 offset[bits] = symbol; |
125 bits++; | 127 bits++; |
126 }); | 128 }); |
127 /* Symbols with code length 0 are placed after all other symbols. */ | 129 /* Symbols with code length 0 are placed after all other symbols. */ |
128 offset[0] = 17; | 130 offset[0] = BROTLI_CODE_LENGTH_CODES - 1; |
129 | 131 |
130 /* sort symbols by length, by symbol order within each length */ | 132 /* sort symbols by length, by symbol order within each length */ |
131 symbol = 18; | 133 symbol = BROTLI_CODE_LENGTH_CODES; |
132 do { | 134 do { |
133 BROTLI_REPEAT(6, { | 135 BROTLI_REPEAT(6, { |
134 symbol--; | 136 symbol--; |
135 sorted[offset[code_lengths[symbol]]--] = symbol; | 137 sorted[offset[code_lengths[symbol]]--] = symbol; |
136 }); | 138 }); |
137 } while (symbol != 0); | 139 } while (symbol != 0); |
138 | 140 |
139 table_size = 1 << BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH; | 141 table_size = 1 << BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH; |
140 | 142 |
141 /* Special case: all symbols but one have 0 code length. */ | 143 /* Special case: all symbols but one have 0 code length. */ |
142 if (offset[0] == 0) { | 144 if (offset[0] == 0) { |
143 code.bits = 0; | 145 code.bits = 0; |
144 code.value = (uint16_t)sorted[0]; | 146 code.value = (uint16_t)sorted[0]; |
145 for (key = 0; key < (uint32_t)table_size; ++key) { | 147 for (key = 0; key < (reg_t)table_size; ++key) { |
146 table[key] = code; | 148 table[key] = code; |
147 } | 149 } |
148 return; | 150 return; |
149 } | 151 } |
150 | 152 |
151 /* fill in table */ | 153 /* fill in table */ |
152 key = 0; | 154 key = 0; |
153 key_step = BROTLI_REVERSE_BITS_LOWEST; | 155 key_step = BROTLI_REVERSE_BITS_LOWEST; |
154 symbol = 0; | 156 symbol = 0; |
155 bits = 1; | 157 bits = 1; |
156 step = 2; | 158 step = 2; |
157 do { | 159 do { |
158 code.bits = (uint8_t)bits; | 160 code.bits = (uint8_t)bits; |
159 for (bits_count = count[bits]; bits_count != 0; --bits_count) { | 161 for (bits_count = count[bits]; bits_count != 0; --bits_count) { |
160 code.value = (uint16_t)sorted[symbol++]; | 162 code.value = (uint16_t)sorted[symbol++]; |
161 ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code); | 163 ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code); |
162 key += key_step; | 164 key += key_step; |
163 } | 165 } |
164 step <<= 1; | 166 step <<= 1; |
165 key_step >>= 1; | 167 key_step >>= 1; |
166 } while (++bits <= BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH); | 168 } while (++bits <= BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH); |
167 } | 169 } |
168 | 170 |
169 uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, | 171 uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, |
170 int root_bits, | 172 int root_bits, |
171 const uint16_t* const symbol_lists, | 173 const uint16_t* const symbol_lists, |
172 uint16_t* count) { | 174 uint16_t* count) { |
173 HuffmanCode code; /* current table entry */ | 175 HuffmanCode code; /* current table entry */ |
174 HuffmanCode* table; /* next available space in table */ | 176 HuffmanCode* table; /* next available space in table */ |
175 int len; /* current code length */ | 177 int len; /* current code length */ |
176 int symbol; /* symbol index in original or sorted table */ | 178 int symbol; /* symbol index in original or sorted table */ |
177 uint32_t key; /* prefix code */ | 179 reg_t key; /* prefix code */ |
178 uint32_t key_step; /* prefix code addend */ | 180 reg_t key_step; /* prefix code addend */ |
179 uint32_t sub_key; /* 2nd level table prefix code */ | 181 reg_t sub_key; /* 2nd level table prefix code */ |
180 uint32_t sub_key_step; /* 2nd level table prefix code addend */ | 182 reg_t sub_key_step; /* 2nd level table prefix code addend */ |
181 int step; /* step size to replicate values in current table */ | 183 int step; /* step size to replicate values in current table */ |
182 int table_bits; /* key length of current table */ | 184 int table_bits; /* key length of current table */ |
183 int table_size; /* size of current table */ | 185 int table_size; /* size of current table */ |
184 int total_size; /* sum of root table size and 2nd level table sizes */ | 186 int total_size; /* sum of root table size and 2nd level table sizes */ |
185 int max_length = -1; | 187 int max_length = -1; |
186 int bits; | 188 int bits; |
187 int bits_count; | 189 int bits_count; |
188 | 190 |
189 BROTLI_DCHECK(root_bits <= BROTLI_REVERSE_BITS_MAX); | 191 BROTLI_DCHECK(root_bits <= BROTLI_REVERSE_BITS_MAX); |
190 BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH - root_bits <= | 192 BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH - root_bits <= |
191 BROTLI_REVERSE_BITS_MAX); | 193 BROTLI_REVERSE_BITS_MAX); |
192 | 194 |
193 while (symbol_lists[max_length] == 0xFFFF) max_length--; | 195 while (symbol_lists[max_length] == 0xFFFF) max_length--; |
194 max_length += BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1; | 196 max_length += BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 memcpy(&table[table_size], &table[0], | 349 memcpy(&table[table_size], &table[0], |
348 (size_t)table_size * sizeof(table[0])); | 350 (size_t)table_size * sizeof(table[0])); |
349 table_size <<= 1; | 351 table_size <<= 1; |
350 } | 352 } |
351 return goal_size; | 353 return goal_size; |
352 } | 354 } |
353 | 355 |
354 #if defined(__cplusplus) || defined(c_plusplus) | 356 #if defined(__cplusplus) || defined(c_plusplus) |
355 } /* extern "C" */ | 357 } /* extern "C" */ |
356 #endif | 358 #endif |
OLD | NEW |