| OLD | NEW |
| (Empty) | |
| 1 /* Copyright 2013 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 // Functions for encoding of integers into prefix codes the amount of extra |
| 8 // bits, and the actual values of the extra bits. |
| 9 |
| 10 #ifndef BROTLI_ENC_PREFIX_H_ |
| 11 #define BROTLI_ENC_PREFIX_H_ |
| 12 |
| 13 #include "./fast_log.h" |
| 14 #include "./types.h" |
| 15 |
| 16 namespace brotli { |
| 17 |
| 18 static const uint32_t kNumInsertLenPrefixes = 24; |
| 19 static const uint32_t kNumCopyLenPrefixes = 24; |
| 20 static const uint32_t kNumCommandPrefixes = 704; |
| 21 static const uint32_t kNumBlockLenPrefixes = 26; |
| 22 static const uint32_t kNumDistanceShortCodes = 16; |
| 23 static const uint32_t kNumDistancePrefixes = 520; |
| 24 |
| 25 // Represents the range of values belonging to a prefix code: |
| 26 // [offset, offset + 2^nbits) |
| 27 struct PrefixCodeRange { |
| 28 uint32_t offset; |
| 29 uint32_t nbits; |
| 30 }; |
| 31 |
| 32 static const PrefixCodeRange kBlockLengthPrefixCode[kNumBlockLenPrefixes] = { |
| 33 { 1, 2}, { 5, 2}, { 9, 2}, { 13, 2}, |
| 34 { 17, 3}, { 25, 3}, { 33, 3}, { 41, 3}, |
| 35 { 49, 4}, { 65, 4}, { 81, 4}, { 97, 4}, |
| 36 { 113, 5}, { 145, 5}, { 177, 5}, { 209, 5}, |
| 37 { 241, 6}, { 305, 6}, { 369, 7}, { 497, 8}, |
| 38 { 753, 9}, { 1265, 10}, {2289, 11}, {4337, 12}, |
| 39 {8433, 13}, {16625, 24} |
| 40 }; |
| 41 |
| 42 inline void GetBlockLengthPrefixCode(uint32_t len, uint32_t* code, |
| 43 uint32_t* n_extra, uint32_t* extra) { |
| 44 *code = 0; |
| 45 while (*code < 25 && len >= kBlockLengthPrefixCode[*code + 1].offset) { |
| 46 ++(*code); |
| 47 } |
| 48 *n_extra = kBlockLengthPrefixCode[*code].nbits; |
| 49 *extra = len - kBlockLengthPrefixCode[*code].offset; |
| 50 } |
| 51 |
| 52 inline void PrefixEncodeCopyDistance(size_t distance_code, |
| 53 size_t num_direct_codes, |
| 54 size_t postfix_bits, |
| 55 uint16_t* code, |
| 56 uint32_t* extra_bits) { |
| 57 if (distance_code < kNumDistanceShortCodes + num_direct_codes) { |
| 58 *code = static_cast<uint16_t>(distance_code); |
| 59 *extra_bits = 0; |
| 60 return; |
| 61 } |
| 62 distance_code -= kNumDistanceShortCodes + num_direct_codes; /* >= 0 */ |
| 63 distance_code += (1u << (postfix_bits + 2u)); /* > 0 */ |
| 64 size_t bucket = Log2FloorNonZero(distance_code) - 1; |
| 65 size_t postfix_mask = (1 << postfix_bits) - 1; |
| 66 size_t postfix = distance_code & postfix_mask; |
| 67 size_t prefix = (distance_code >> bucket) & 1; |
| 68 size_t offset = (2 + prefix) << bucket; |
| 69 size_t nbits = bucket - postfix_bits; |
| 70 *code = static_cast<uint16_t>( |
| 71 (kNumDistanceShortCodes + num_direct_codes + |
| 72 ((2 * (nbits - 1) + prefix) << postfix_bits) + postfix)); |
| 73 *extra_bits = static_cast<uint32_t>( |
| 74 (nbits << 24) | ((distance_code - offset) >> postfix_bits)); |
| 75 } |
| 76 |
| 77 } // namespace brotli |
| 78 |
| 79 #endif // BROTLI_ENC_PREFIX_H_ |
| OLD | NEW |