OLD | NEW |
1 /* Copyright 2013 Google Inc. All Rights Reserved. | 1 /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | 2 |
3 Distributed under MIT license. | 3 Distributed under MIT license. |
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 */ | 5 */ |
6 | 6 |
7 // Functions for encoding of integers into prefix codes the amount of extra | 7 /* Functions for encoding of integers into prefix codes the amount of extra |
8 // bits, and the actual values of the extra bits. | 8 bits, and the actual values of the extra bits. */ |
9 | 9 |
10 #ifndef BROTLI_ENC_PREFIX_H_ | 10 #ifndef BROTLI_ENC_PREFIX_H_ |
11 #define BROTLI_ENC_PREFIX_H_ | 11 #define BROTLI_ENC_PREFIX_H_ |
12 | 12 |
| 13 #include "../common/constants.h" |
| 14 #include <brotli/port.h> |
| 15 #include <brotli/types.h> |
13 #include "./fast_log.h" | 16 #include "./fast_log.h" |
14 #include "./types.h" | |
15 | 17 |
16 namespace brotli { | 18 #if defined(__cplusplus) || defined(c_plusplus) |
| 19 extern "C" { |
| 20 #endif |
17 | 21 |
18 static const uint32_t kNumInsertLenPrefixes = 24; | 22 /* Here distance_code is an intermediate code, i.e. one of the special codes or |
19 static const uint32_t kNumCopyLenPrefixes = 24; | 23 the actual distance increased by BROTLI_NUM_DISTANCE_SHORT_CODES - 1. */ |
20 static const uint32_t kNumCommandPrefixes = 704; | 24 static BROTLI_INLINE void PrefixEncodeCopyDistance(size_t distance_code, |
21 static const uint32_t kNumBlockLenPrefixes = 26; | 25 size_t num_direct_codes, |
22 static const uint32_t kNumDistanceShortCodes = 16; | 26 size_t postfix_bits, |
23 static const uint32_t kNumDistancePrefixes = 520; | 27 uint16_t* code, |
24 | 28 uint32_t* extra_bits) { |
25 // Represents the range of values belonging to a prefix code: | 29 if (distance_code < BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes) { |
26 // [offset, offset + 2^nbits) | 30 *code = (uint16_t)distance_code; |
27 struct PrefixCodeRange { | 31 *extra_bits = 0; |
28 uint32_t offset; | 32 return; |
29 uint32_t nbits; | 33 } else { |
30 }; | 34 size_t dist = ((size_t)1 << (postfix_bits + 2u)) + |
31 | 35 (distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES - num_direct_codes); |
32 static const PrefixCodeRange kBlockLengthPrefixCode[kNumBlockLenPrefixes] = { | 36 size_t bucket = Log2FloorNonZero(dist) - 1; |
33 { 1, 2}, { 5, 2}, { 9, 2}, { 13, 2}, | 37 size_t postfix_mask = (1u << postfix_bits) - 1; |
34 { 17, 3}, { 25, 3}, { 33, 3}, { 41, 3}, | 38 size_t postfix = dist & postfix_mask; |
35 { 49, 4}, { 65, 4}, { 81, 4}, { 97, 4}, | 39 size_t prefix = (dist >> bucket) & 1; |
36 { 113, 5}, { 145, 5}, { 177, 5}, { 209, 5}, | 40 size_t offset = (2 + prefix) << bucket; |
37 { 241, 6}, { 305, 6}, { 369, 7}, { 497, 8}, | 41 size_t nbits = bucket - postfix_bits; |
38 { 753, 9}, { 1265, 10}, {2289, 11}, {4337, 12}, | 42 *code = (uint16_t)( |
39 {8433, 13}, {16625, 24} | 43 (BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes + |
40 }; | 44 ((2 * (nbits - 1) + prefix) << postfix_bits) + postfix)); |
41 | 45 *extra_bits = (uint32_t)( |
42 inline void GetBlockLengthPrefixCode(uint32_t len, uint32_t* code, | 46 (nbits << 24) | ((dist - offset) >> postfix_bits)); |
43 uint32_t* n_extra, uint32_t* extra) { | |
44 *code = 0; | |
45 while (*code < 25 && len >= kBlockLengthPrefixCode[*code + 1].offset) { | |
46 ++(*code); | |
47 } | 47 } |
48 *n_extra = kBlockLengthPrefixCode[*code].nbits; | |
49 *extra = len - kBlockLengthPrefixCode[*code].offset; | |
50 } | 48 } |
51 | 49 |
52 inline void PrefixEncodeCopyDistance(size_t distance_code, | 50 #if defined(__cplusplus) || defined(c_plusplus) |
53 size_t num_direct_codes, | 51 } /* extern "C" */ |
54 size_t postfix_bits, | 52 #endif |
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 | 53 |
77 } // namespace brotli | 54 #endif /* BROTLI_ENC_PREFIX_H_ */ |
78 | |
79 #endif // BROTLI_ENC_PREFIX_H_ | |
OLD | NEW |