Zoltan Szabadka | 79e99af | 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 | // A (forgetful) hash table to the data seen by the compressor, to |
| 16 | // help create backward references to previous data. |
| 17 | |
| 18 | #ifndef BROTLI_ENC_HASH_H_ |
| 19 | #define BROTLI_ENC_HASH_H_ |
| 20 | |
| 21 | #include <stddef.h> |
| 22 | #include <stdint.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <algorithm> |
| 26 | |
| 27 | #include "./fast_log.h" |
| 28 | #include "./find_match_length.h" |
| 29 | #include "./port.h" |
| 30 | |
| 31 | namespace brotli { |
| 32 | |
| 33 | // kHashMul32 multiplier has these properties: |
| 34 | // * The multiplier must be odd. Otherwise we may lose the highest bit. |
| 35 | // * No long streaks of 1s or 0s. |
| 36 | // * There is no effort to ensure that it is a prime, the oddity is enough |
| 37 | // for this use. |
| 38 | // * The number has been tuned heuristically against compression benchmarks. |
| 39 | static const uint32_t kHashMul32 = 0x1e35a7bd; |
| 40 | |
| 41 | inline uint32_t Hash3Bytes(const uint8_t *data, const int bits) { |
| 42 | uint32_t h = (BROTLI_UNALIGNED_LOAD32(data) & 0xffffff) * kHashMul32; |
| 43 | // The higher bits contain more mixture from the multiplication, |
| 44 | // so we take our results from there. |
| 45 | return h >> (32 - bits); |
| 46 | } |
| 47 | |
| 48 | // Usually, we always choose the longest backward reference. This function |
| 49 | // allows for the exception of that rule. |
| 50 | // |
| 51 | // If we choose a backward reference that is further away, it will |
| 52 | // usually be coded with more bits. We approximate this by assuming |
| 53 | // log2(distance). If the distance can be expressed in terms of the |
| 54 | // last four distances, we use some heuristic constants to estimate |
| 55 | // the bits cost. For the first up to four literals we use the bit |
| 56 | // cost of the literals from the literal cost model, after that we |
| 57 | // use the average bit cost of the cost model. |
| 58 | // |
| 59 | // This function is used to sometimes discard a longer backward reference |
| 60 | // when it is not much longer and the bit cost for encoding it is more |
| 61 | // than the saved literals. |
| 62 | inline double BackwardReferenceScore(double average_cost, |
| 63 | double start_cost4, |
| 64 | double start_cost3, |
| 65 | double start_cost2, |
| 66 | int copy_length, |
| 67 | int backward_reference_offset, |
| 68 | int last_distance1, |
| 69 | int last_distance2, |
| 70 | int last_distance3, |
| 71 | int last_distance4) { |
| 72 | double retval = 0; |
| 73 | switch (copy_length) { |
| 74 | case 2: retval = start_cost2; break; |
| 75 | case 3: retval = start_cost3; break; |
| 76 | default: retval = start_cost4 + (copy_length - 4) * average_cost; break; |
| 77 | } |
| 78 | int diff_last1 = abs(backward_reference_offset - last_distance1); |
| 79 | int diff_last2 = abs(backward_reference_offset - last_distance2); |
| 80 | if (diff_last1 == 0) { |
| 81 | retval += 0.6; |
| 82 | } else if (diff_last1 < 4) { |
| 83 | retval -= 0.9 + 0.03 * diff_last1; |
| 84 | } else if (diff_last2 < 4) { |
| 85 | retval -= 0.95 + 0.1 * diff_last2; |
| 86 | } else if (backward_reference_offset == last_distance3) { |
| 87 | retval -= 1.17; |
| 88 | } else if (backward_reference_offset == last_distance4) { |
| 89 | retval -= 1.27; |
| 90 | } else { |
| 91 | retval -= 1.20 * Log2Floor(backward_reference_offset); |
| 92 | } |
| 93 | return retval; |
| 94 | } |
| 95 | |
| 96 | // A (forgetful) hash table to the data seen by the compressor, to |
| 97 | // help create backward references to previous data. |
| 98 | // |
| 99 | // This is a hash map of fixed size (kBucketSize) to a ring buffer of |
| 100 | // fixed size (kBlockSize). The ring buffer contains the last kBlockSize |
| 101 | // index positions of the given hash key in the compressed data. |
| 102 | template <int kBucketBits, int kBlockBits> |
| 103 | class HashLongestMatch { |
| 104 | public: |
| 105 | HashLongestMatch() |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 106 | : last_distance1_(4), |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 107 | last_distance2_(11), |
| 108 | last_distance3_(15), |
| 109 | last_distance4_(16), |
| 110 | insert_length_(0), |
| 111 | average_cost_(5.4) { |
| 112 | Reset(); |
| 113 | } |
| 114 | void Reset() { |
| 115 | std::fill(&num_[0], &num_[sizeof(num_) / sizeof(num_[0])], 0); |
| 116 | } |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 117 | |
| 118 | // Look at 3 bytes at data. |
| 119 | // Compute a hash from these, and store the value of ix at that position. |
| 120 | inline void Store(const uint8_t *data, const int ix) { |
| 121 | const uint32_t key = Hash3Bytes(data, kBucketBits); |
| 122 | const int minor_ix = num_[key] & kBlockMask; |
| 123 | buckets_[key][minor_ix] = ix; |
| 124 | ++num_[key]; |
| 125 | } |
| 126 | |
| 127 | // Store hashes for a range of data. |
| 128 | void StoreHashes(const uint8_t *data, size_t len, int startix, int mask) { |
| 129 | for (int p = 0; p < len; ++p) { |
| 130 | Store(&data[p & mask], startix + p); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Find a longest backward match of &data[cur_ix] up to the length of |
| 135 | // max_length. |
| 136 | // |
| 137 | // Does not look for matches longer than max_length. |
| 138 | // Does not look for matches further away than max_backward. |
| 139 | // Writes the best found match length into best_len_out. |
| 140 | // Writes the index (&data[index]) offset from the start of the best match |
| 141 | // into best_distance_out. |
| 142 | // Write the score of the best match into best_score_out. |
| 143 | bool FindLongestMatch(const uint8_t * __restrict data, |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 144 | const float * __restrict literal_cost, |
| 145 | const size_t ring_buffer_mask, |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 146 | const uint32_t cur_ix, |
| 147 | uint32_t max_length, |
| 148 | const uint32_t max_backward, |
| 149 | size_t * __restrict best_len_out, |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame^] | 150 | size_t * __restrict best_len_code_out, |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 151 | size_t * __restrict best_distance_out, |
| 152 | double * __restrict best_score_out) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 153 | const size_t cur_ix_masked = cur_ix & ring_buffer_mask; |
| 154 | const double start_cost4 = literal_cost == NULL ? 20 : |
| 155 | literal_cost[cur_ix_masked] + |
| 156 | literal_cost[(cur_ix + 1) & ring_buffer_mask] + |
| 157 | literal_cost[(cur_ix + 2) & ring_buffer_mask] + |
| 158 | literal_cost[(cur_ix + 3) & ring_buffer_mask]; |
| 159 | const double start_cost3 = literal_cost == NULL ? 15 : |
| 160 | literal_cost[cur_ix_masked] + |
| 161 | literal_cost[(cur_ix + 1) & ring_buffer_mask] + |
| 162 | literal_cost[(cur_ix + 2) & ring_buffer_mask] + 0.3; |
| 163 | double start_cost2 = literal_cost == NULL ? 10 : |
| 164 | literal_cost[cur_ix_masked] + |
| 165 | literal_cost[(cur_ix + 1) & ring_buffer_mask] + 1.2; |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 166 | bool match_found = false; |
| 167 | // Don't accept a short copy from far away. |
| 168 | double best_score = 8.25; |
| 169 | if (insert_length_ < 4) { |
| 170 | double cost_diff[4] = { 0.20, 0.09, 0.05, 0.03 }; |
| 171 | best_score += cost_diff[insert_length_]; |
| 172 | } |
| 173 | size_t best_len = *best_len_out; |
| 174 | *best_len_out = 0; |
| 175 | size_t best_ix = 1; |
| 176 | // Try last distance first. |
| 177 | for (int i = 0; i < 16; ++i) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 178 | size_t prev_ix = cur_ix; |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 179 | switch(i) { |
| 180 | case 0: prev_ix -= last_distance1_; break; |
| 181 | case 1: prev_ix -= last_distance2_; break; |
| 182 | case 2: prev_ix -= last_distance3_; break; |
| 183 | case 3: prev_ix -= last_distance4_; break; |
| 184 | |
| 185 | case 4: prev_ix -= last_distance1_ - 1; break; |
| 186 | case 5: prev_ix -= last_distance1_ + 1; break; |
| 187 | case 6: prev_ix -= last_distance1_ - 2; break; |
| 188 | case 7: prev_ix -= last_distance1_ + 2; break; |
| 189 | case 8: prev_ix -= last_distance1_ - 3; break; |
| 190 | case 9: prev_ix -= last_distance1_ + 3; break; |
| 191 | |
| 192 | case 10: prev_ix -= last_distance2_ - 1; break; |
| 193 | case 11: prev_ix -= last_distance2_ + 1; break; |
| 194 | case 12: prev_ix -= last_distance2_ - 2; break; |
| 195 | case 13: prev_ix -= last_distance2_ + 2; break; |
| 196 | case 14: prev_ix -= last_distance2_ - 3; break; |
| 197 | case 15: prev_ix -= last_distance2_ + 3; break; |
| 198 | } |
| 199 | if (prev_ix >= cur_ix) { |
| 200 | continue; |
| 201 | } |
| 202 | const size_t backward = cur_ix - prev_ix; |
| 203 | if (PREDICT_FALSE(backward > max_backward)) { |
| 204 | continue; |
| 205 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 206 | prev_ix &= ring_buffer_mask; |
| 207 | if (data[cur_ix_masked + best_len] != data[prev_ix + best_len]) { |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 208 | continue; |
| 209 | } |
| 210 | const size_t len = |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 211 | FindMatchLengthWithLimit(&data[prev_ix], &data[cur_ix_masked], |
| 212 | max_length); |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 213 | if (len >= 3 || (len == 2 && i < 2)) { |
| 214 | // Comparing for >= 2 does not change the semantics, but just saves for |
| 215 | // a few unnecessary binary logarithms in backward reference score, |
| 216 | // since we are not interested in such short matches. |
| 217 | const double score = BackwardReferenceScore(average_cost_, |
| 218 | start_cost4, |
| 219 | start_cost3, |
| 220 | start_cost2, |
| 221 | len, backward, |
| 222 | last_distance1_, |
| 223 | last_distance2_, |
| 224 | last_distance3_, |
| 225 | last_distance4_); |
| 226 | if (best_score < score) { |
| 227 | best_score = score; |
| 228 | best_len = len; |
| 229 | best_ix = backward; |
| 230 | *best_len_out = best_len; |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame^] | 231 | *best_len_code_out = best_len; |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 232 | *best_distance_out = best_ix; |
| 233 | *best_score_out = best_score; |
| 234 | match_found = true; |
| 235 | } |
| 236 | } |
| 237 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 238 | const uint32_t key = Hash3Bytes(&data[cur_ix_masked], kBucketBits); |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame^] | 239 | const int * __restrict const bucket = &buckets_[key][0]; |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 240 | const int down = (num_[key] > kBlockSize) ? (num_[key] - kBlockSize) : 0; |
| 241 | int stop = int(cur_ix) - 64; |
| 242 | if (stop < 0) { stop = 0; } |
| 243 | |
| 244 | start_cost2 -= 1.0; |
| 245 | for (int i = cur_ix - 1; i > stop; --i) { |
| 246 | size_t prev_ix = i; |
| 247 | const size_t backward = cur_ix - prev_ix; |
| 248 | if (PREDICT_FALSE(backward > max_backward)) { |
| 249 | break; |
| 250 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 251 | prev_ix &= ring_buffer_mask; |
| 252 | if (data[cur_ix_masked] != data[prev_ix] || |
| 253 | data[cur_ix_masked + 1] != data[prev_ix + 1]) { |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 254 | continue; |
| 255 | } |
| 256 | int len = 2; |
| 257 | const double score = start_cost2 - 1.70 * Log2Floor(backward); |
| 258 | |
| 259 | if (best_score < score) { |
| 260 | best_score = score; |
| 261 | best_len = len; |
| 262 | best_ix = backward; |
| 263 | *best_len_out = best_len; |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame^] | 264 | *best_len_code_out = best_len; |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 265 | *best_distance_out = best_ix; |
| 266 | match_found = true; |
| 267 | } |
| 268 | } |
| 269 | for (int i = num_[key] - 1; i >= down; --i) { |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame^] | 270 | int prev_ix = bucket[i & kBlockMask]; |
| 271 | if (prev_ix < 0) { |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 272 | continue; |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame^] | 273 | } else { |
| 274 | const size_t backward = cur_ix - prev_ix; |
| 275 | if (PREDICT_FALSE(backward > max_backward)) { |
| 276 | break; |
| 277 | } |
| 278 | prev_ix &= ring_buffer_mask; |
| 279 | if (data[cur_ix_masked + best_len] != data[prev_ix + best_len]) { |
| 280 | continue; |
| 281 | } |
| 282 | const size_t len = |
| 283 | FindMatchLengthWithLimit(&data[prev_ix], &data[cur_ix_masked], |
| 284 | max_length); |
| 285 | if (len >= 3) { |
| 286 | // Comparing for >= 3 does not change the semantics, but just saves |
| 287 | // for a few unnecessary binary logarithms in backward reference |
| 288 | // score, since we are not interested in such short matches. |
| 289 | const double score = BackwardReferenceScore(average_cost_, |
| 290 | start_cost4, |
| 291 | start_cost3, |
| 292 | start_cost2, |
| 293 | len, backward, |
| 294 | last_distance1_, |
| 295 | last_distance2_, |
| 296 | last_distance3_, |
| 297 | last_distance4_); |
| 298 | if (best_score < score) { |
| 299 | best_score = score; |
| 300 | best_len = len; |
| 301 | best_ix = backward; |
| 302 | *best_len_out = best_len; |
| 303 | *best_len_code_out = best_len; |
| 304 | *best_distance_out = best_ix; |
| 305 | *best_score_out = best_score; |
| 306 | match_found = true; |
| 307 | } |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | } |
| 311 | return match_found; |
| 312 | } |
| 313 | |
| 314 | void set_last_distance(int v) { |
| 315 | if (last_distance1_ != v) { |
| 316 | last_distance4_ = last_distance3_; |
| 317 | last_distance3_ = last_distance2_; |
| 318 | last_distance2_ = last_distance1_; |
| 319 | last_distance1_ = v; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | int last_distance() const { return last_distance1_; } |
| 324 | |
| 325 | void set_insert_length(int v) { insert_length_ = v; } |
| 326 | |
| 327 | void set_average_cost(double v) { average_cost_ = v; } |
| 328 | |
| 329 | private: |
| 330 | // Number of hash buckets. |
| 331 | static const uint32_t kBucketSize = 1 << kBucketBits; |
| 332 | |
| 333 | // Only kBlockSize newest backward references are kept, |
| 334 | // and the older are forgotten. |
| 335 | static const uint32_t kBlockSize = 1 << kBlockBits; |
| 336 | |
| 337 | // Mask for accessing entries in a block (in a ringbuffer manner). |
| 338 | static const uint32_t kBlockMask = (1 << kBlockBits) - 1; |
| 339 | |
| 340 | // Number of entries in a particular bucket. |
| 341 | uint16_t num_[kBucketSize]; |
| 342 | |
| 343 | // Buckets containing kBlockSize of backward references. |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame^] | 344 | int buckets_[kBucketSize][kBlockSize]; |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 345 | |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 346 | int last_distance1_; |
| 347 | int last_distance2_; |
| 348 | int last_distance3_; |
| 349 | int last_distance4_; |
| 350 | |
| 351 | // Cost adjustment for how many literals we are planning to insert |
| 352 | // anyway. |
| 353 | int insert_length_; |
| 354 | |
| 355 | double average_cost_; |
| 356 | }; |
| 357 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 358 | typedef HashLongestMatch<13, 11> Hasher; |
| 359 | |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 360 | } // namespace brotli |
| 361 | |
| 362 | #endif // BROTLI_ENC_HASH_H_ |