Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 1 | // 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 Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <limits> |
| 21 | #include <vector> |
Roderick Sheeter | c23cb1e | 2013-12-12 10:43:05 -0800 | [diff] [blame] | 22 | #include <cstdlib> |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 23 | |
| 24 | #include "./histogram.h" |
Zoltan Szabadka | 4a7024d | 2015-10-01 12:08:14 +0200 | [diff] [blame] | 25 | #include "./types.h" |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 26 | |
| 27 | namespace brotli { |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | struct HuffmanTree { |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 32 | 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 Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 42 | // Sort the root nodes, least popular first. |
Zoltan Szabadka | 9853922 | 2015-04-23 16:20:29 +0200 | [diff] [blame] | 43 | bool SortHuffmanTree(const HuffmanTree &v0, const HuffmanTree &v1) { |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 44 | return v0.total_count_ < v1.total_count_; |
| 45 | } |
| 46 | |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 47 | void 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 |
| 77 | void 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 Szabadka | 9853922 | 2015-04-23 16:20:29 +0200 | [diff] [blame] | 89 | for (int i = length - 1; i >= 0; --i) { |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 90 | 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 Szabadka | 9853922 | 2015-04-23 16:20:29 +0200 | [diff] [blame] | 102 | std::stable_sort(tree.begin(), tree.end(), SortHuffmanTree); |
| 103 | |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 104 | // 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 Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 155 | void Reverse(std::vector<uint8_t>* v, int start, int end) { |
Zoltan Szabadka | 60c24c0 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 156 | --end; |
| 157 | while (start < end) { |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 158 | int tmp = (*v)[start]; |
| 159 | (*v)[start] = (*v)[end]; |
| 160 | (*v)[end] = tmp; |
Zoltan Szabadka | 60c24c0 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 161 | ++start; |
| 162 | --end; |
| 163 | } |
| 164 | } |
| 165 | |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 166 | void WriteHuffmanTreeRepetitions( |
| 167 | const int previous_value, |
| 168 | const int value, |
| 169 | int repetitions, |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 170 | std::vector<uint8_t> *tree, |
| 171 | std::vector<uint8_t> *extra_bits_data) { |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 172 | if (previous_value != value) { |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 173 | tree->push_back(value); |
| 174 | extra_bits_data->push_back(0); |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 175 | --repetitions; |
| 176 | } |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 177 | if (repetitions == 7) { |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 178 | tree->push_back(value); |
| 179 | extra_bits_data->push_back(0); |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 180 | --repetitions; |
| 181 | } |
Zoltan Szabadka | 60c24c0 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 182 | if (repetitions < 3) { |
| 183 | for (int i = 0; i < repetitions; ++i) { |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 184 | tree->push_back(value); |
| 185 | extra_bits_data->push_back(0); |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 186 | } |
Zoltan Szabadka | 60c24c0 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 187 | } else { |
| 188 | repetitions -= 3; |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 189 | int start = tree->size(); |
Zoltan Szabadka | 60c24c0 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 190 | while (repetitions >= 0) { |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 191 | tree->push_back(16); |
| 192 | extra_bits_data->push_back(repetitions & 0x3); |
Zoltan Szabadka | 60c24c0 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 193 | repetitions >>= 2; |
| 194 | --repetitions; |
| 195 | } |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 196 | Reverse(tree, start, tree->size()); |
| 197 | Reverse(extra_bits_data, start, tree->size()); |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | void WriteHuffmanTreeRepetitionsZeros( |
| 202 | int repetitions, |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 203 | std::vector<uint8_t> *tree, |
| 204 | std::vector<uint8_t> *extra_bits_data) { |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 205 | if (repetitions == 11) { |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 206 | tree->push_back(0); |
| 207 | extra_bits_data->push_back(0); |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 208 | --repetitions; |
| 209 | } |
Zoltan Szabadka | 60c24c0 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 210 | if (repetitions < 3) { |
| 211 | for (int i = 0; i < repetitions; ++i) { |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 212 | tree->push_back(0); |
| 213 | extra_bits_data->push_back(0); |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 214 | } |
Zoltan Szabadka | 60c24c0 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 215 | } else { |
| 216 | repetitions -= 3; |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 217 | int start = tree->size(); |
Zoltan Szabadka | 60c24c0 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 218 | while (repetitions >= 0) { |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 219 | tree->push_back(17); |
| 220 | extra_bits_data->push_back(repetitions & 0x7); |
Zoltan Szabadka | 60c24c0 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 221 | repetitions >>= 3; |
| 222 | --repetitions; |
| 223 | } |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 224 | Reverse(tree, start, tree->size()); |
| 225 | Reverse(extra_bits_data, start, tree->size()); |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 229 | int OptimizeHuffmanCountsForRle(int length, int* counts) { |
Zoltan Szabadka | 9853922 | 2015-04-23 16:20:29 +0200 | [diff] [blame] | 230 | int nonzero_count = 0; |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 231 | 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 Szabadka | 9853922 | 2015-04-23 16:20:29 +0200 | [diff] [blame] | 237 | 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 Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 245 | 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 Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 254 | { |
| 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 Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 283 | // 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 Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 314 | // Math here is in 24.8 fixed point representation. |
| 315 | const int streak_limit = 1240; |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 316 | stride = 0; |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 317 | limit = 256 * (counts[0] + counts[1] + counts[2]) / 3 + 420; |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 318 | 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 Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 322 | abs(256 * counts[i] - limit) >= streak_limit) { |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 323 | 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 Szabadka | c6b9c7c | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 342 | if (i < length - 2) { |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 343 | // All interesting strides have a count of at least 4, |
| 344 | // at least when non-zeros. |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 345 | limit = 256 * (counts[i] + counts[i + 1] + counts[i + 2]) / 3 + 420; |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 346 | } else if (i < length) { |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 347 | limit = 256 * counts[i]; |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 348 | } else { |
| 349 | limit = 0; |
| 350 | } |
| 351 | } |
| 352 | ++stride; |
| 353 | if (i != length) { |
| 354 | sum += counts[i]; |
| 355 | if (stride >= 4) { |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 356 | limit = (256 * sum + stride / 2) / stride; |
| 357 | } |
| 358 | if (stride == 4) { |
| 359 | limit += 120; |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | free(good_for_rle); |
| 364 | return 1; |
| 365 | } |
| 366 | |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 367 | static 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 Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 374 | for (uint32_t i = 0; i < length;) { |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 375 | const int value = depth[i]; |
| 376 | int reps = 1; |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 377 | for (uint32_t k = i + 1; k < length && depth[k] == value; ++k) { |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 378 | ++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 Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 396 | void WriteHuffmanTree(const uint8_t* depth, |
| 397 | uint32_t length, |
| 398 | std::vector<uint8_t> *tree, |
| 399 | std::vector<uint8_t> *extra_bits_data) { |
Zoltan Szabadka | c6b9c7c | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 400 | int previous_value = 8; |
Zoltan Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 401 | |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 402 | // 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 Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 412 | // First gather statistics on if it is a good idea to do rle. |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 413 | 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 Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 421 | |
| 422 | // Actual rle coding. |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 423 | for (uint32_t i = 0; i < new_length;) { |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 424 | const int value = depth[i]; |
| 425 | int reps = 1; |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 426 | 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 Szabadka | 0454ab4 | 2014-02-14 15:04:23 +0100 | [diff] [blame] | 430 | } |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 431 | } |
| 432 | if (value == 0) { |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 433 | WriteHuffmanTreeRepetitionsZeros(reps, tree, extra_bits_data); |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 434 | } else { |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 435 | WriteHuffmanTreeRepetitions(previous_value, |
| 436 | value, reps, tree, extra_bits_data); |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 437 | previous_value = value; |
| 438 | } |
| 439 | i += reps; |
| 440 | } |
Zoltan Szabadka | c66e4e3 | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | namespace { |
| 444 | |
| 445 | uint16_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 | |
| 462 | void 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 |