blob: db986e5f20cf18462d3aa9953500f9e77623bd03 [file] [log] [blame]
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +02001// Copyright 2010 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Entropy encoding (Huffman) utilities.
16
17#include "./entropy_encode.h"
18
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +020019#include <algorithm>
20#include <limits>
21#include <vector>
Roderick Sheeterc23cb1e2013-12-12 10:43:05 -080022#include <cstdlib>
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +020023
24#include "./histogram.h"
Zoltan Szabadka4a7024d2015-10-01 12:08:14 +020025#include "./types.h"
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +020026
27namespace brotli {
28
29namespace {
30
31struct HuffmanTree {
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +020032 HuffmanTree(int count, int16_t left, int16_t right)
33 : total_count_(count),
34 index_left_(left),
35 index_right_or_value_(right) {
36 }
37 int total_count_;
38 int16_t index_left_;
39 int16_t index_right_or_value_;
40};
41
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +020042// Sort the root nodes, least popular first.
Zoltan Szabadka98539222015-04-23 16:20:29 +020043bool SortHuffmanTree(const HuffmanTree &v0, const HuffmanTree &v1) {
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +020044 return v0.total_count_ < v1.total_count_;
45}
46
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +020047void SetDepth(const HuffmanTree &p,
48 HuffmanTree *pool,
49 uint8_t *depth,
50 int level) {
51 if (p.index_left_ >= 0) {
52 ++level;
53 SetDepth(pool[p.index_left_], pool, depth, level);
54 SetDepth(pool[p.index_right_or_value_], pool, depth, level);
55 } else {
56 depth[p.index_right_or_value_] = level;
57 }
58}
59
60} // namespace
61
62// This function will create a Huffman tree.
63//
64// The catch here is that the tree cannot be arbitrarily deep.
65// Brotli specifies a maximum depth of 15 bits for "code trees"
66// and 7 bits for "code length code trees."
67//
68// count_limit is the value that is to be faked as the minimum value
69// and this minimum value is raised until the tree matches the
70// maximum length requirement.
71//
72// This algorithm is not of excellent performance for very long data blocks,
73// especially when population counts are longer than 2**tree_limit, but
74// we are not planning to use this with extremely long blocks.
75//
76// See http://en.wikipedia.org/wiki/Huffman_coding
77void CreateHuffmanTree(const int *data,
78 const int length,
79 const int tree_limit,
80 uint8_t *depth) {
81 // For block sizes below 64 kB, we never need to do a second iteration
82 // of this loop. Probably all of our block sizes will be smaller than
83 // that, so this loop is mostly of academic interest. If we actually
84 // would need this, we would be better off with the Katajainen algorithm.
85 for (int count_limit = 1; ; count_limit *= 2) {
86 std::vector<HuffmanTree> tree;
87 tree.reserve(2 * length + 1);
88
Zoltan Szabadka98539222015-04-23 16:20:29 +020089 for (int i = length - 1; i >= 0; --i) {
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +020090 if (data[i]) {
91 const int count = std::max(data[i], count_limit);
92 tree.push_back(HuffmanTree(count, -1, i));
93 }
94 }
95
96 const int n = tree.size();
97 if (n == 1) {
98 depth[tree[0].index_right_or_value_] = 1; // Only one element.
99 break;
100 }
101
Zoltan Szabadka98539222015-04-23 16:20:29 +0200102 std::stable_sort(tree.begin(), tree.end(), SortHuffmanTree);
103
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200104 // The nodes are:
105 // [0, n): the sorted leaf nodes that we start with.
106 // [n]: we add a sentinel here.
107 // [n + 1, 2n): new parent nodes are added here, starting from
108 // (n+1). These are naturally in ascending order.
109 // [2n]: we add a sentinel at the end as well.
110 // There will be (2n+1) elements at the end.
111 const HuffmanTree sentinel(std::numeric_limits<int>::max(), -1, -1);
112 tree.push_back(sentinel);
113 tree.push_back(sentinel);
114
115 int i = 0; // Points to the next leaf node.
116 int j = n + 1; // Points to the next non-leaf node.
117 for (int k = n - 1; k > 0; --k) {
118 int left, right;
119 if (tree[i].total_count_ <= tree[j].total_count_) {
120 left = i;
121 ++i;
122 } else {
123 left = j;
124 ++j;
125 }
126 if (tree[i].total_count_ <= tree[j].total_count_) {
127 right = i;
128 ++i;
129 } else {
130 right = j;
131 ++j;
132 }
133
134 // The sentinel node becomes the parent node.
135 int j_end = tree.size() - 1;
136 tree[j_end].total_count_ =
137 tree[left].total_count_ + tree[right].total_count_;
138 tree[j_end].index_left_ = left;
139 tree[j_end].index_right_or_value_ = right;
140
141 // Add back the last sentinel node.
142 tree.push_back(sentinel);
143 }
144 SetDepth(tree[2 * n - 1], &tree[0], depth, 0);
145
146 // We need to pack the Huffman tree in tree_limit bits.
147 // If this was not successful, add fake entities to the lowest values
148 // and retry.
149 if (*std::max_element(&depth[0], &depth[length]) <= tree_limit) {
150 break;
151 }
152 }
153}
154
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200155void Reverse(std::vector<uint8_t>* v, int start, int end) {
Zoltan Szabadka60c24c02013-12-12 13:18:04 +0100156 --end;
157 while (start < end) {
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200158 int tmp = (*v)[start];
159 (*v)[start] = (*v)[end];
160 (*v)[end] = tmp;
Zoltan Szabadka60c24c02013-12-12 13:18:04 +0100161 ++start;
162 --end;
163 }
164}
165
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200166void WriteHuffmanTreeRepetitions(
167 const int previous_value,
168 const int value,
169 int repetitions,
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200170 std::vector<uint8_t> *tree,
171 std::vector<uint8_t> *extra_bits_data) {
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200172 if (previous_value != value) {
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200173 tree->push_back(value);
174 extra_bits_data->push_back(0);
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200175 --repetitions;
176 }
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100177 if (repetitions == 7) {
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200178 tree->push_back(value);
179 extra_bits_data->push_back(0);
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100180 --repetitions;
181 }
Zoltan Szabadka60c24c02013-12-12 13:18:04 +0100182 if (repetitions < 3) {
183 for (int i = 0; i < repetitions; ++i) {
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200184 tree->push_back(value);
185 extra_bits_data->push_back(0);
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200186 }
Zoltan Szabadka60c24c02013-12-12 13:18:04 +0100187 } else {
188 repetitions -= 3;
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200189 int start = tree->size();
Zoltan Szabadka60c24c02013-12-12 13:18:04 +0100190 while (repetitions >= 0) {
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200191 tree->push_back(16);
192 extra_bits_data->push_back(repetitions & 0x3);
Zoltan Szabadka60c24c02013-12-12 13:18:04 +0100193 repetitions >>= 2;
194 --repetitions;
195 }
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200196 Reverse(tree, start, tree->size());
197 Reverse(extra_bits_data, start, tree->size());
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200198 }
199}
200
201void WriteHuffmanTreeRepetitionsZeros(
202 int repetitions,
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200203 std::vector<uint8_t> *tree,
204 std::vector<uint8_t> *extra_bits_data) {
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100205 if (repetitions == 11) {
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200206 tree->push_back(0);
207 extra_bits_data->push_back(0);
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100208 --repetitions;
209 }
Zoltan Szabadka60c24c02013-12-12 13:18:04 +0100210 if (repetitions < 3) {
211 for (int i = 0; i < repetitions; ++i) {
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200212 tree->push_back(0);
213 extra_bits_data->push_back(0);
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200214 }
Zoltan Szabadka60c24c02013-12-12 13:18:04 +0100215 } else {
216 repetitions -= 3;
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200217 int start = tree->size();
Zoltan Szabadka60c24c02013-12-12 13:18:04 +0100218 while (repetitions >= 0) {
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200219 tree->push_back(17);
220 extra_bits_data->push_back(repetitions & 0x7);
Zoltan Szabadka60c24c02013-12-12 13:18:04 +0100221 repetitions >>= 3;
222 --repetitions;
223 }
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200224 Reverse(tree, start, tree->size());
225 Reverse(extra_bits_data, start, tree->size());
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200226 }
227}
228
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200229int OptimizeHuffmanCountsForRle(int length, int* counts) {
Zoltan Szabadka98539222015-04-23 16:20:29 +0200230 int nonzero_count = 0;
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200231 int stride;
232 int limit;
233 int sum;
234 uint8_t* good_for_rle;
235 // Let's make the Huffman code more compatible with rle encoding.
236 int i;
Zoltan Szabadka98539222015-04-23 16:20:29 +0200237 for (i = 0; i < length; i++) {
238 if (counts[i]) {
239 ++nonzero_count;
240 }
241 }
242 if (nonzero_count < 16) {
243 return 1;
244 }
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200245 for (; length >= 0; --length) {
246 if (length == 0) {
247 return 1; // All zeros.
248 }
249 if (counts[length - 1] != 0) {
250 // Now counts[0..length - 1] does not have trailing zeros.
251 break;
252 }
253 }
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100254 {
255 int nonzeros = 0;
256 int smallest_nonzero = 1 << 30;
257 for (i = 0; i < length; ++i) {
258 if (counts[i] != 0) {
259 ++nonzeros;
260 if (smallest_nonzero > counts[i]) {
261 smallest_nonzero = counts[i];
262 }
263 }
264 }
265 if (nonzeros < 5) {
266 // Small histogram will model it well.
267 return 1;
268 }
269 int zeros = length - nonzeros;
270 if (smallest_nonzero < 4) {
271 if (zeros < 6) {
272 for (i = 1; i < length - 1; ++i) {
273 if (counts[i - 1] != 0 && counts[i] == 0 && counts[i + 1] != 0) {
274 counts[i] = 1;
275 }
276 }
277 }
278 }
279 if (nonzeros < 28) {
280 return 1;
281 }
282 }
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200283 // 2) Let's mark all population counts that already can be encoded
284 // with an rle code.
285 good_for_rle = (uint8_t*)calloc(length, 1);
286 if (good_for_rle == NULL) {
287 return 0;
288 }
289 {
290 // Let's not spoil any of the existing good rle codes.
291 // Mark any seq of 0's that is longer as 5 as a good_for_rle.
292 // Mark any seq of non-0's that is longer as 7 as a good_for_rle.
293 int symbol = counts[0];
294 int stride = 0;
295 for (i = 0; i < length + 1; ++i) {
296 if (i == length || counts[i] != symbol) {
297 if ((symbol == 0 && stride >= 5) ||
298 (symbol != 0 && stride >= 7)) {
299 int k;
300 for (k = 0; k < stride; ++k) {
301 good_for_rle[i - k - 1] = 1;
302 }
303 }
304 stride = 1;
305 if (i != length) {
306 symbol = counts[i];
307 }
308 } else {
309 ++stride;
310 }
311 }
312 }
313 // 3) Let's replace those population counts that lead to more rle codes.
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100314 // Math here is in 24.8 fixed point representation.
315 const int streak_limit = 1240;
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200316 stride = 0;
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100317 limit = 256 * (counts[0] + counts[1] + counts[2]) / 3 + 420;
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200318 sum = 0;
319 for (i = 0; i < length + 1; ++i) {
320 if (i == length || good_for_rle[i] ||
321 (i != 0 && good_for_rle[i - 1]) ||
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100322 abs(256 * counts[i] - limit) >= streak_limit) {
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200323 if (stride >= 4 || (stride >= 3 && sum == 0)) {
324 int k;
325 // The stride must end, collapse what we have, if we have enough (4).
326 int count = (sum + stride / 2) / stride;
327 if (count < 1) {
328 count = 1;
329 }
330 if (sum == 0) {
331 // Don't make an all zeros stride to be upgraded to ones.
332 count = 0;
333 }
334 for (k = 0; k < stride; ++k) {
335 // We don't want to change value at counts[i],
336 // that is already belonging to the next stride. Thus - 1.
337 counts[i - k - 1] = count;
338 }
339 }
340 stride = 0;
341 sum = 0;
Zoltan Szabadkac6b9c7c2013-11-15 19:02:17 +0100342 if (i < length - 2) {
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200343 // All interesting strides have a count of at least 4,
344 // at least when non-zeros.
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100345 limit = 256 * (counts[i] + counts[i + 1] + counts[i + 2]) / 3 + 420;
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200346 } else if (i < length) {
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100347 limit = 256 * counts[i];
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200348 } else {
349 limit = 0;
350 }
351 }
352 ++stride;
353 if (i != length) {
354 sum += counts[i];
355 if (stride >= 4) {
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100356 limit = (256 * sum + stride / 2) / stride;
357 }
358 if (stride == 4) {
359 limit += 120;
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200360 }
361 }
362 }
363 free(good_for_rle);
364 return 1;
365}
366
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100367static void DecideOverRleUse(const uint8_t* depth, const int length,
368 bool *use_rle_for_non_zero,
369 bool *use_rle_for_zero) {
370 int total_reps_zero = 0;
371 int total_reps_non_zero = 0;
372 int count_reps_zero = 0;
373 int count_reps_non_zero = 0;
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200374 for (uint32_t i = 0; i < length;) {
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100375 const int value = depth[i];
376 int reps = 1;
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200377 for (uint32_t k = i + 1; k < length && depth[k] == value; ++k) {
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100378 ++reps;
379 }
380 if (reps >= 3 && value == 0) {
381 total_reps_zero += reps;
382 ++count_reps_zero;
383 }
384 if (reps >= 4 && value != 0) {
385 total_reps_non_zero += reps;
386 ++count_reps_non_zero;
387 }
388 i += reps;
389 }
390 total_reps_non_zero -= count_reps_non_zero * 2;
391 total_reps_zero -= count_reps_zero * 2;
392 *use_rle_for_non_zero = total_reps_non_zero > 2;
393 *use_rle_for_zero = total_reps_zero > 2;
394}
395
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200396void WriteHuffmanTree(const uint8_t* depth,
397 uint32_t length,
398 std::vector<uint8_t> *tree,
399 std::vector<uint8_t> *extra_bits_data) {
Zoltan Szabadkac6b9c7c2013-11-15 19:02:17 +0100400 int previous_value = 8;
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100401
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200402 // Throw away trailing zeros.
403 int new_length = length;
404 for (int i = 0; i < length; ++i) {
405 if (depth[length - i - 1] == 0) {
406 --new_length;
407 } else {
408 break;
409 }
410 }
411
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100412 // First gather statistics on if it is a good idea to do rle.
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200413 bool use_rle_for_non_zero = false;
414 bool use_rle_for_zero = false;
415 if (length > 50) {
416 // Find rle coding for longer codes.
417 // Shorter codes seem not to benefit from rle.
418 DecideOverRleUse(depth, new_length,
419 &use_rle_for_non_zero, &use_rle_for_zero);
420 }
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100421
422 // Actual rle coding.
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200423 for (uint32_t i = 0; i < new_length;) {
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200424 const int value = depth[i];
425 int reps = 1;
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200426 if ((value != 0 && use_rle_for_non_zero) ||
427 (value == 0 && use_rle_for_zero)) {
428 for (uint32_t k = i + 1; k < new_length && depth[k] == value; ++k) {
429 ++reps;
Zoltan Szabadka0454ab42014-02-14 15:04:23 +0100430 }
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200431 }
432 if (value == 0) {
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200433 WriteHuffmanTreeRepetitionsZeros(reps, tree, extra_bits_data);
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200434 } else {
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200435 WriteHuffmanTreeRepetitions(previous_value,
436 value, reps, tree, extra_bits_data);
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200437 previous_value = value;
438 }
439 i += reps;
440 }
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200441}
442
443namespace {
444
445uint16_t ReverseBits(int num_bits, uint16_t bits) {
446 static const size_t kLut[16] = { // Pre-reversed 4-bit values.
447 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
448 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
449 };
450 size_t retval = kLut[bits & 0xf];
451 for (int i = 4; i < num_bits; i += 4) {
452 retval <<= 4;
453 bits >>= 4;
454 retval |= kLut[bits & 0xf];
455 }
456 retval >>= (-num_bits & 0x3);
457 return retval;
458}
459
460} // namespace
461
462void ConvertBitDepthsToSymbols(const uint8_t *depth, int len, uint16_t *bits) {
463 // In Brotli, all bit depths are [1..15]
464 // 0 bit depth means that the symbol does not exist.
465 const int kMaxBits = 16; // 0..15 are values for bits
466 uint16_t bl_count[kMaxBits] = { 0 };
467 {
468 for (int i = 0; i < len; ++i) {
469 ++bl_count[depth[i]];
470 }
471 bl_count[0] = 0;
472 }
473 uint16_t next_code[kMaxBits];
474 next_code[0] = 0;
475 {
476 int code = 0;
477 for (int bits = 1; bits < kMaxBits; ++bits) {
478 code = (code + bl_count[bits - 1]) << 1;
479 next_code[bits] = code;
480 }
481 }
482 for (int i = 0; i < len; ++i) {
483 if (depth[i]) {
484 bits[i] = ReverseBits(depth[i], next_code[depth[i]]++);
485 }
486 }
487}
488
489} // namespace brotli