/src/ghostpdl/brotli/c/enc/entropy_encode.c
Line | Count | Source |
1 | | /* Copyright 2010 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 | | /* Entropy encoding (Huffman) utilities. */ |
8 | | |
9 | | #include "entropy_encode.h" |
10 | | |
11 | | #include "../common/constants.h" |
12 | | #include "../common/platform.h" |
13 | | |
14 | | #if defined(__cplusplus) || defined(c_plusplus) |
15 | | extern "C" { |
16 | | #endif |
17 | | |
18 | | const BROTLI_MODEL("small") size_t kBrotliShellGaps[] = {132, 57, 23, 10, 4, 1}; |
19 | | |
20 | | BROTLI_BOOL BrotliSetDepth( |
21 | 0 | int p0, HuffmanTree* pool, uint8_t* depth, int max_depth) { |
22 | 0 | int stack[16]; |
23 | 0 | int level = 0; |
24 | 0 | int p = p0; |
25 | 0 | BROTLI_DCHECK(max_depth <= 15); |
26 | 0 | stack[0] = -1; |
27 | 0 | while (BROTLI_TRUE) { |
28 | 0 | if (pool[p].index_left_ >= 0) { |
29 | 0 | level++; |
30 | 0 | if (level > max_depth) return BROTLI_FALSE; |
31 | 0 | stack[level] = pool[p].index_right_or_value_; |
32 | 0 | p = pool[p].index_left_; |
33 | 0 | continue; |
34 | 0 | } else { |
35 | 0 | depth[pool[p].index_right_or_value_] = (uint8_t)level; |
36 | 0 | } |
37 | 0 | while (level >= 0 && stack[level] == -1) level--; |
38 | 0 | if (level < 0) return BROTLI_TRUE; |
39 | 0 | p = stack[level]; |
40 | 0 | stack[level] = -1; |
41 | 0 | } |
42 | 0 | } |
43 | | |
44 | | /* Sort the root nodes, least popular first. */ |
45 | | static BROTLI_INLINE BROTLI_BOOL SortHuffmanTree( |
46 | 0 | const HuffmanTree* v0, const HuffmanTree* v1) { |
47 | 0 | if (v0->total_count_ != v1->total_count_) { |
48 | 0 | return TO_BROTLI_BOOL(v0->total_count_ < v1->total_count_); |
49 | 0 | } |
50 | 0 | return TO_BROTLI_BOOL(v0->index_right_or_value_ > v1->index_right_or_value_); |
51 | 0 | } |
52 | | |
53 | | /* This function will create a Huffman tree. |
54 | | |
55 | | The catch here is that the tree cannot be arbitrarily deep. |
56 | | Brotli specifies a maximum depth of 15 bits for "code trees" |
57 | | and 7 bits for "code length code trees." |
58 | | |
59 | | count_limit is the value that is to be faked as the minimum value |
60 | | and this minimum value is raised until the tree matches the |
61 | | maximum length requirement. |
62 | | |
63 | | This algorithm is not of excellent performance for very long data blocks, |
64 | | especially when population counts are longer than 2**tree_limit, but |
65 | | we are not planning to use this with extremely long blocks. |
66 | | |
67 | | See http://en.wikipedia.org/wiki/Huffman_coding */ |
68 | | void BrotliCreateHuffmanTree(const uint32_t* data, |
69 | | const size_t length, |
70 | | const int tree_limit, |
71 | | HuffmanTree* tree, |
72 | 0 | uint8_t* depth) { |
73 | 0 | uint32_t count_limit; |
74 | 0 | HuffmanTree sentinel; |
75 | 0 | InitHuffmanTree(&sentinel, BROTLI_UINT32_MAX, -1, -1); |
76 | | /* For block sizes below 64 kB, we never need to do a second iteration |
77 | | of this loop. Probably all of our block sizes will be smaller than |
78 | | that, so this loop is mostly of academic interest. If we actually |
79 | | would need this, we would be better off with the Katajainen algorithm. */ |
80 | 0 | for (count_limit = 1; ; count_limit *= 2) { |
81 | 0 | size_t n = 0; |
82 | 0 | size_t i; |
83 | 0 | size_t j; |
84 | 0 | size_t k; |
85 | 0 | for (i = length; i != 0;) { |
86 | 0 | --i; |
87 | 0 | if (data[i]) { |
88 | 0 | const uint32_t count = BROTLI_MAX(uint32_t, data[i], count_limit); |
89 | 0 | InitHuffmanTree(&tree[n++], count, -1, (int16_t)i); |
90 | 0 | } |
91 | 0 | } |
92 | |
|
93 | 0 | if (n == 1) { |
94 | 0 | depth[tree[0].index_right_or_value_] = 1; /* Only one element. */ |
95 | 0 | break; |
96 | 0 | } |
97 | | |
98 | 0 | SortHuffmanTreeItems(tree, n, SortHuffmanTree); |
99 | | |
100 | | /* The nodes are: |
101 | | [0, n): the sorted leaf nodes that we start with. |
102 | | [n]: we add a sentinel here. |
103 | | [n + 1, 2n): new parent nodes are added here, starting from |
104 | | (n+1). These are naturally in ascending order. |
105 | | [2n]: we add a sentinel at the end as well. |
106 | | There will be (2n+1) elements at the end. */ |
107 | 0 | tree[n] = sentinel; |
108 | 0 | tree[n + 1] = sentinel; |
109 | |
|
110 | 0 | i = 0; /* Points to the next leaf node. */ |
111 | 0 | j = n + 1; /* Points to the next non-leaf node. */ |
112 | 0 | for (k = n - 1; k != 0; --k) { |
113 | 0 | size_t left, right; |
114 | 0 | if (tree[i].total_count_ <= tree[j].total_count_) { |
115 | 0 | left = i; |
116 | 0 | ++i; |
117 | 0 | } else { |
118 | 0 | left = j; |
119 | 0 | ++j; |
120 | 0 | } |
121 | 0 | if (tree[i].total_count_ <= tree[j].total_count_) { |
122 | 0 | right = i; |
123 | 0 | ++i; |
124 | 0 | } else { |
125 | 0 | right = j; |
126 | 0 | ++j; |
127 | 0 | } |
128 | |
|
129 | 0 | { |
130 | | /* The sentinel node becomes the parent node. */ |
131 | 0 | size_t j_end = 2 * n - k; |
132 | 0 | tree[j_end].total_count_ = |
133 | 0 | tree[left].total_count_ + tree[right].total_count_; |
134 | 0 | tree[j_end].index_left_ = (int16_t)left; |
135 | 0 | tree[j_end].index_right_or_value_ = (int16_t)right; |
136 | | |
137 | | /* Add back the last sentinel node. */ |
138 | 0 | tree[j_end + 1] = sentinel; |
139 | 0 | } |
140 | 0 | } |
141 | 0 | if (BrotliSetDepth((int)(2 * n - 1), &tree[0], depth, tree_limit)) { |
142 | | /* We need to pack the Huffman tree in tree_limit bits. If this was not |
143 | | successful, add fake entities to the lowest values and retry. */ |
144 | 0 | break; |
145 | 0 | } |
146 | 0 | } |
147 | 0 | } |
148 | | |
149 | 0 | static void Reverse(uint8_t* v, size_t start, size_t end) { |
150 | 0 | --end; |
151 | 0 | while (start < end) { |
152 | 0 | uint8_t tmp = v[start]; |
153 | 0 | v[start] = v[end]; |
154 | 0 | v[end] = tmp; |
155 | 0 | ++start; |
156 | 0 | --end; |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | static void BrotliWriteHuffmanTreeRepetitions( |
161 | | const uint8_t previous_value, |
162 | | const uint8_t value, |
163 | | size_t repetitions, |
164 | | size_t* tree_size, |
165 | | uint8_t* tree, |
166 | 0 | uint8_t* extra_bits_data) { |
167 | 0 | BROTLI_DCHECK(repetitions > 0); |
168 | 0 | if (previous_value != value) { |
169 | 0 | tree[*tree_size] = value; |
170 | 0 | extra_bits_data[*tree_size] = 0; |
171 | 0 | ++(*tree_size); |
172 | 0 | --repetitions; |
173 | 0 | } |
174 | 0 | if (repetitions == 7) { |
175 | 0 | tree[*tree_size] = value; |
176 | 0 | extra_bits_data[*tree_size] = 0; |
177 | 0 | ++(*tree_size); |
178 | 0 | --repetitions; |
179 | 0 | } |
180 | 0 | if (repetitions < 3) { |
181 | 0 | size_t i; |
182 | 0 | for (i = 0; i < repetitions; ++i) { |
183 | 0 | tree[*tree_size] = value; |
184 | 0 | extra_bits_data[*tree_size] = 0; |
185 | 0 | ++(*tree_size); |
186 | 0 | } |
187 | 0 | } else { |
188 | 0 | size_t start = *tree_size; |
189 | 0 | repetitions -= 3; |
190 | 0 | while (BROTLI_TRUE) { |
191 | 0 | tree[*tree_size] = BROTLI_REPEAT_PREVIOUS_CODE_LENGTH; |
192 | 0 | extra_bits_data[*tree_size] = repetitions & 0x3; |
193 | 0 | ++(*tree_size); |
194 | 0 | repetitions >>= 2; |
195 | 0 | if (repetitions == 0) { |
196 | 0 | break; |
197 | 0 | } |
198 | 0 | --repetitions; |
199 | 0 | } |
200 | 0 | Reverse(tree, start, *tree_size); |
201 | 0 | Reverse(extra_bits_data, start, *tree_size); |
202 | 0 | } |
203 | 0 | } |
204 | | |
205 | | static void BrotliWriteHuffmanTreeRepetitionsZeros( |
206 | | size_t repetitions, |
207 | | size_t* tree_size, |
208 | | uint8_t* tree, |
209 | 0 | uint8_t* extra_bits_data) { |
210 | 0 | if (repetitions == 11) { |
211 | 0 | tree[*tree_size] = 0; |
212 | 0 | extra_bits_data[*tree_size] = 0; |
213 | 0 | ++(*tree_size); |
214 | 0 | --repetitions; |
215 | 0 | } |
216 | 0 | if (repetitions < 3) { |
217 | 0 | size_t i; |
218 | 0 | for (i = 0; i < repetitions; ++i) { |
219 | 0 | tree[*tree_size] = 0; |
220 | 0 | extra_bits_data[*tree_size] = 0; |
221 | 0 | ++(*tree_size); |
222 | 0 | } |
223 | 0 | } else { |
224 | 0 | size_t start = *tree_size; |
225 | 0 | repetitions -= 3; |
226 | 0 | while (BROTLI_TRUE) { |
227 | 0 | tree[*tree_size] = BROTLI_REPEAT_ZERO_CODE_LENGTH; |
228 | 0 | extra_bits_data[*tree_size] = repetitions & 0x7; |
229 | 0 | ++(*tree_size); |
230 | 0 | repetitions >>= 3; |
231 | 0 | if (repetitions == 0) { |
232 | 0 | break; |
233 | 0 | } |
234 | 0 | --repetitions; |
235 | 0 | } |
236 | 0 | Reverse(tree, start, *tree_size); |
237 | 0 | Reverse(extra_bits_data, start, *tree_size); |
238 | 0 | } |
239 | 0 | } |
240 | | |
241 | | void BrotliOptimizeHuffmanCountsForRle(size_t length, uint32_t* counts, |
242 | 0 | uint8_t* good_for_rle) { |
243 | 0 | size_t nonzero_count = 0; |
244 | 0 | size_t stride; |
245 | 0 | size_t limit; |
246 | 0 | size_t sum; |
247 | 0 | const size_t streak_limit = 1240; |
248 | | /* Let's make the Huffman code more compatible with RLE encoding. */ |
249 | 0 | size_t i; |
250 | 0 | for (i = 0; i < length; i++) { |
251 | 0 | if (counts[i]) { |
252 | 0 | ++nonzero_count; |
253 | 0 | } |
254 | 0 | } |
255 | 0 | if (nonzero_count < 16) { |
256 | 0 | return; |
257 | 0 | } |
258 | 0 | while (length != 0 && counts[length - 1] == 0) { |
259 | 0 | --length; |
260 | 0 | } |
261 | 0 | if (length == 0) { |
262 | 0 | return; /* All zeros. */ |
263 | 0 | } |
264 | | /* Now counts[0..length - 1] does not have trailing zeros. */ |
265 | 0 | { |
266 | 0 | size_t nonzeros = 0; |
267 | 0 | uint32_t smallest_nonzero = 1 << 30; |
268 | 0 | for (i = 0; i < length; ++i) { |
269 | 0 | if (counts[i] != 0) { |
270 | 0 | ++nonzeros; |
271 | 0 | if (smallest_nonzero > counts[i]) { |
272 | 0 | smallest_nonzero = counts[i]; |
273 | 0 | } |
274 | 0 | } |
275 | 0 | } |
276 | 0 | if (nonzeros < 5) { |
277 | | /* Small histogram will model it well. */ |
278 | 0 | return; |
279 | 0 | } |
280 | 0 | if (smallest_nonzero < 4) { |
281 | 0 | size_t zeros = length - nonzeros; |
282 | 0 | if (zeros < 6) { |
283 | 0 | for (i = 1; i < length - 1; ++i) { |
284 | 0 | if (counts[i - 1] != 0 && counts[i] == 0 && counts[i + 1] != 0) { |
285 | 0 | counts[i] = 1; |
286 | 0 | } |
287 | 0 | } |
288 | 0 | } |
289 | 0 | } |
290 | 0 | if (nonzeros < 28) { |
291 | 0 | return; |
292 | 0 | } |
293 | 0 | } |
294 | | /* 2) Let's mark all population counts that already can be encoded |
295 | | with an RLE code. */ |
296 | 0 | memset(good_for_rle, 0, length); |
297 | 0 | { |
298 | | /* Let's not spoil any of the existing good RLE codes. |
299 | | Mark any seq of 0's that is longer as 5 as a good_for_rle. |
300 | | Mark any seq of non-0's that is longer as 7 as a good_for_rle. */ |
301 | 0 | uint32_t symbol = counts[0]; |
302 | 0 | size_t step = 0; |
303 | 0 | for (i = 0; i <= length; ++i) { |
304 | 0 | if (i == length || counts[i] != symbol) { |
305 | 0 | if ((symbol == 0 && step >= 5) || |
306 | 0 | (symbol != 0 && step >= 7)) { |
307 | 0 | size_t k; |
308 | 0 | for (k = 0; k < step; ++k) { |
309 | 0 | good_for_rle[i - k - 1] = 1; |
310 | 0 | } |
311 | 0 | } |
312 | 0 | step = 1; |
313 | 0 | if (i != length) { |
314 | 0 | symbol = counts[i]; |
315 | 0 | } |
316 | 0 | } else { |
317 | 0 | ++step; |
318 | 0 | } |
319 | 0 | } |
320 | 0 | } |
321 | | /* 3) Let's replace those population counts that lead to more RLE codes. |
322 | | Math here is in 24.8 fixed point representation. */ |
323 | 0 | stride = 0; |
324 | 0 | limit = 256 * (counts[0] + counts[1] + counts[2]) / 3 + 420; |
325 | 0 | sum = 0; |
326 | 0 | for (i = 0; i <= length; ++i) { |
327 | 0 | if (i == length || good_for_rle[i] || |
328 | 0 | (i != 0 && good_for_rle[i - 1]) || |
329 | 0 | (256 * counts[i] - limit + streak_limit) >= 2 * streak_limit) { |
330 | 0 | if (stride >= 4 || (stride >= 3 && sum == 0)) { |
331 | 0 | size_t k; |
332 | | /* The stride must end, collapse what we have, if we have enough (4). */ |
333 | 0 | size_t count = (sum + stride / 2) / stride; |
334 | 0 | if (count == 0) { |
335 | 0 | count = 1; |
336 | 0 | } |
337 | 0 | if (sum == 0) { |
338 | | /* Don't make an all zeros stride to be upgraded to ones. */ |
339 | 0 | count = 0; |
340 | 0 | } |
341 | 0 | for (k = 0; k < stride; ++k) { |
342 | | /* We don't want to change value at counts[i], |
343 | | that is already belonging to the next stride. Thus - 1. */ |
344 | 0 | counts[i - k - 1] = (uint32_t)count; |
345 | 0 | } |
346 | 0 | } |
347 | 0 | stride = 0; |
348 | 0 | sum = 0; |
349 | 0 | if (i < length - 2) { |
350 | | /* All interesting strides have a count of at least 4, */ |
351 | | /* at least when non-zeros. */ |
352 | 0 | limit = 256 * (counts[i] + counts[i + 1] + counts[i + 2]) / 3 + 420; |
353 | 0 | } else if (i < length) { |
354 | 0 | limit = 256 * counts[i]; |
355 | 0 | } else { |
356 | 0 | limit = 0; |
357 | 0 | } |
358 | 0 | } |
359 | 0 | ++stride; |
360 | 0 | if (i != length) { |
361 | 0 | sum += counts[i]; |
362 | 0 | if (stride >= 4) { |
363 | 0 | limit = (256 * sum + stride / 2) / stride; |
364 | 0 | } |
365 | 0 | if (stride == 4) { |
366 | 0 | limit += 120; |
367 | 0 | } |
368 | 0 | } |
369 | 0 | } |
370 | 0 | } |
371 | | |
372 | | static void DecideOverRleUse(const uint8_t* depth, const size_t length, |
373 | | BROTLI_BOOL* use_rle_for_non_zero, |
374 | 0 | BROTLI_BOOL* use_rle_for_zero) { |
375 | 0 | size_t total_reps_zero = 0; |
376 | 0 | size_t total_reps_non_zero = 0; |
377 | 0 | size_t count_reps_zero = 1; |
378 | 0 | size_t count_reps_non_zero = 1; |
379 | 0 | size_t i; |
380 | 0 | for (i = 0; i < length;) { |
381 | 0 | const uint8_t value = depth[i]; |
382 | 0 | size_t reps = 1; |
383 | 0 | size_t k; |
384 | 0 | for (k = i + 1; k < length && depth[k] == value; ++k) { |
385 | 0 | ++reps; |
386 | 0 | } |
387 | 0 | if (reps >= 3 && value == 0) { |
388 | 0 | total_reps_zero += reps; |
389 | 0 | ++count_reps_zero; |
390 | 0 | } |
391 | 0 | if (reps >= 4 && value != 0) { |
392 | 0 | total_reps_non_zero += reps; |
393 | 0 | ++count_reps_non_zero; |
394 | 0 | } |
395 | 0 | i += reps; |
396 | 0 | } |
397 | 0 | *use_rle_for_non_zero = |
398 | 0 | TO_BROTLI_BOOL(total_reps_non_zero > count_reps_non_zero * 2); |
399 | 0 | *use_rle_for_zero = TO_BROTLI_BOOL(total_reps_zero > count_reps_zero * 2); |
400 | 0 | } |
401 | | |
402 | | void BrotliWriteHuffmanTree(const uint8_t* depth, |
403 | | size_t length, |
404 | | size_t* tree_size, |
405 | | uint8_t* tree, |
406 | 0 | uint8_t* extra_bits_data) { |
407 | 0 | uint8_t previous_value = BROTLI_INITIAL_REPEATED_CODE_LENGTH; |
408 | 0 | size_t i; |
409 | 0 | BROTLI_BOOL use_rle_for_non_zero = BROTLI_FALSE; |
410 | 0 | BROTLI_BOOL use_rle_for_zero = BROTLI_FALSE; |
411 | | |
412 | | /* Throw away trailing zeros. */ |
413 | 0 | size_t new_length = length; |
414 | 0 | for (i = 0; i < length; ++i) { |
415 | 0 | if (depth[length - i - 1] == 0) { |
416 | 0 | --new_length; |
417 | 0 | } else { |
418 | 0 | break; |
419 | 0 | } |
420 | 0 | } |
421 | | |
422 | | /* First gather statistics on if it is a good idea to do RLE. */ |
423 | 0 | if (length > 50) { |
424 | | /* Find RLE coding for longer codes. |
425 | | Shorter codes seem not to benefit from RLE. */ |
426 | 0 | DecideOverRleUse(depth, new_length, |
427 | 0 | &use_rle_for_non_zero, &use_rle_for_zero); |
428 | 0 | } |
429 | | |
430 | | /* Actual RLE coding. */ |
431 | 0 | for (i = 0; i < new_length;) { |
432 | 0 | const uint8_t value = depth[i]; |
433 | 0 | size_t reps = 1; |
434 | 0 | if ((value != 0 && use_rle_for_non_zero) || |
435 | 0 | (value == 0 && use_rle_for_zero)) { |
436 | 0 | size_t k; |
437 | 0 | for (k = i + 1; k < new_length && depth[k] == value; ++k) { |
438 | 0 | ++reps; |
439 | 0 | } |
440 | 0 | } |
441 | 0 | if (value == 0) { |
442 | 0 | BrotliWriteHuffmanTreeRepetitionsZeros( |
443 | 0 | reps, tree_size, tree, extra_bits_data); |
444 | 0 | } else { |
445 | 0 | BrotliWriteHuffmanTreeRepetitions(previous_value, |
446 | 0 | value, reps, tree_size, |
447 | 0 | tree, extra_bits_data); |
448 | 0 | previous_value = value; |
449 | 0 | } |
450 | 0 | i += reps; |
451 | 0 | } |
452 | 0 | } |
453 | | |
454 | 0 | static uint16_t BrotliReverseBits(size_t num_bits, uint16_t bits) { |
455 | 0 | static const size_t BROTLI_MODEL("small") kLut[16] = |
456 | 0 | { /* Pre-reversed 4-bit values. */ |
457 | 0 | 0x00, 0x08, 0x04, 0x0C, 0x02, 0x0A, 0x06, 0x0E, |
458 | 0 | 0x01, 0x09, 0x05, 0x0D, 0x03, 0x0B, 0x07, 0x0F |
459 | 0 | }; |
460 | 0 | size_t retval = kLut[bits & 0x0F]; |
461 | 0 | size_t i; |
462 | 0 | for (i = 4; i < num_bits; i += 4) { |
463 | 0 | retval <<= 4; |
464 | 0 | bits = (uint16_t)(bits >> 4); |
465 | 0 | retval |= kLut[bits & 0x0F]; |
466 | 0 | } |
467 | 0 | retval >>= ((0 - num_bits) & 0x03); |
468 | 0 | return (uint16_t)retval; |
469 | 0 | } |
470 | | |
471 | | /* 0..15 are values for bits */ |
472 | 0 | #define MAX_HUFFMAN_BITS 16 |
473 | | |
474 | | void BrotliConvertBitDepthsToSymbols(const uint8_t* depth, |
475 | | size_t len, |
476 | 0 | uint16_t* bits) { |
477 | | /* In Brotli, all bit depths are [1..15] |
478 | | 0 bit depth means that the symbol does not exist. */ |
479 | 0 | uint16_t bl_count[MAX_HUFFMAN_BITS] = { 0 }; |
480 | 0 | uint16_t next_code[MAX_HUFFMAN_BITS]; |
481 | 0 | size_t i; |
482 | 0 | int code = 0; |
483 | 0 | for (i = 0; i < len; ++i) { |
484 | 0 | ++bl_count[depth[i]]; |
485 | 0 | } |
486 | 0 | bl_count[0] = 0; |
487 | 0 | next_code[0] = 0; |
488 | 0 | for (i = 1; i < MAX_HUFFMAN_BITS; ++i) { |
489 | 0 | code = (code + bl_count[i - 1]) << 1; |
490 | 0 | next_code[i] = (uint16_t)code; |
491 | 0 | } |
492 | 0 | for (i = 0; i < len; ++i) { |
493 | 0 | if (depth[i]) { |
494 | 0 | bits[i] = BrotliReverseBits(depth[i], next_code[depth[i]]++); |
495 | 0 | } |
496 | 0 | } |
497 | 0 | } |
498 | | |
499 | | #if defined(__cplusplus) || defined(c_plusplus) |
500 | | } /* extern "C" */ |
501 | | #endif |