| 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 // This class models a sequence of literals and a backward reference copy. |
| 8 |
| 9 #ifndef BROTLI_ENC_COMMAND_H_ |
| 10 #define BROTLI_ENC_COMMAND_H_ |
| 11 |
| 12 #include "./fast_log.h" |
| 13 #include "./prefix.h" |
| 14 #include "./types.h" |
| 15 |
| 16 namespace brotli { |
| 17 |
| 18 static uint32_t kInsBase[] = { 0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26, 34, 50, |
| 19 66, 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594 }; |
| 20 static uint32_t kInsExtra[] = { 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, |
| 21 5, 5, 6, 7, 8, 9, 10, 12, 14, 24 }; |
| 22 static uint32_t kCopyBase[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 18, 22, 30, |
| 23 38, 54, 70, 102, 134, 198, 326, 582, 1094, 2118 }; |
| 24 static uint32_t kCopyExtra[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, |
| 25 4, 4, 5, 5, 6, 7, 8, 9, 10, 24 }; |
| 26 |
| 27 static inline uint16_t GetInsertLengthCode(size_t insertlen) { |
| 28 if (insertlen < 6) { |
| 29 return static_cast<uint16_t>(insertlen); |
| 30 } else if (insertlen < 130) { |
| 31 insertlen -= 2; |
| 32 uint32_t nbits = Log2FloorNonZero(insertlen) - 1u; |
| 33 return static_cast<uint16_t>((nbits << 1) + (insertlen >> nbits) + 2); |
| 34 } else if (insertlen < 2114) { |
| 35 return static_cast<uint16_t>(Log2FloorNonZero(insertlen - 66) + 10); |
| 36 } else if (insertlen < 6210) { |
| 37 return 21u; |
| 38 } else if (insertlen < 22594) { |
| 39 return 22u; |
| 40 } else { |
| 41 return 23u; |
| 42 } |
| 43 } |
| 44 |
| 45 static inline uint16_t GetCopyLengthCode(size_t copylen) { |
| 46 if (copylen < 10) { |
| 47 return static_cast<uint16_t>(copylen - 2); |
| 48 } else if (copylen < 134) { |
| 49 copylen -= 6; |
| 50 uint32_t nbits = Log2FloorNonZero(copylen) - 1u; |
| 51 return static_cast<uint16_t>((nbits << 1) + (copylen >> nbits) + 4); |
| 52 } else if (copylen < 2118) { |
| 53 return static_cast<uint16_t>(Log2FloorNonZero(copylen - 70) + 12); |
| 54 } else { |
| 55 return 23u; |
| 56 } |
| 57 } |
| 58 |
| 59 static inline uint16_t CombineLengthCodes( |
| 60 uint16_t inscode, uint16_t copycode, bool use_last_distance) { |
| 61 uint16_t bits64 = |
| 62 static_cast<uint16_t>((copycode & 0x7u) | ((inscode & 0x7u) << 3)); |
| 63 if (use_last_distance && inscode < 8 && copycode < 16) { |
| 64 return (copycode < 8) ? bits64 : (bits64 | 64); |
| 65 } else { |
| 66 // "To convert an insert-and-copy length code to an insert length code and |
| 67 // a copy length code, the following table can be used" |
| 68 static const uint16_t cells[9] = { 128u, 192u, 384u, 256u, 320u, 512u, |
| 69 448u, 576u, 640u }; |
| 70 return cells[(copycode >> 3) + 3 * (inscode >> 3)] | bits64; |
| 71 } |
| 72 } |
| 73 |
| 74 static inline void GetLengthCode(size_t insertlen, size_t copylen, |
| 75 bool use_last_distance, |
| 76 uint16_t* code) { |
| 77 uint16_t inscode = GetInsertLengthCode(insertlen); |
| 78 uint16_t copycode = GetCopyLengthCode(copylen); |
| 79 *code = CombineLengthCodes(inscode, copycode, use_last_distance); |
| 80 } |
| 81 |
| 82 static inline uint32_t GetInsertBase(uint16_t inscode) { |
| 83 return kInsBase[inscode]; |
| 84 } |
| 85 |
| 86 static inline uint32_t GetInsertExtra(uint16_t inscode) { |
| 87 return kInsExtra[inscode]; |
| 88 } |
| 89 |
| 90 static inline uint32_t GetCopyBase(uint16_t copycode) { |
| 91 return kCopyBase[copycode]; |
| 92 } |
| 93 |
| 94 static inline uint32_t GetCopyExtra(uint16_t copycode) { |
| 95 return kCopyExtra[copycode]; |
| 96 } |
| 97 |
| 98 struct Command { |
| 99 // distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. |
| 100 Command(size_t insertlen, size_t copylen, size_t copylen_code, |
| 101 size_t distance_code) |
| 102 : insert_len_(static_cast<uint32_t>(insertlen)) { |
| 103 copy_len_ = static_cast<uint32_t>( |
| 104 copylen | ((copylen_code ^ copylen) << 24)); |
| 105 // The distance prefix and extra bits are stored in this Command as if |
| 106 // npostfix and ndirect were 0, they are only recomputed later after the |
| 107 // clustering if needed. |
| 108 PrefixEncodeCopyDistance(distance_code, 0, 0, &dist_prefix_, &dist_extra_); |
| 109 GetLengthCode(insertlen, copylen_code, dist_prefix_ == 0, |
| 110 &cmd_prefix_); |
| 111 } |
| 112 |
| 113 explicit Command(size_t insertlen) |
| 114 : insert_len_(static_cast<uint32_t>(insertlen)) |
| 115 , copy_len_(4 << 24), dist_extra_(0), dist_prefix_(16) { |
| 116 GetLengthCode(insertlen, 4, dist_prefix_ == 0, &cmd_prefix_); |
| 117 } |
| 118 |
| 119 uint32_t DistanceCode(void) const { |
| 120 if (dist_prefix_ < 16) { |
| 121 return dist_prefix_; |
| 122 } |
| 123 uint32_t nbits = dist_extra_ >> 24; |
| 124 uint32_t extra = dist_extra_ & 0xffffff; |
| 125 uint32_t prefix = dist_prefix_ - 12 - 2 * nbits; |
| 126 return (prefix << nbits) + extra + 12; |
| 127 } |
| 128 |
| 129 uint32_t DistanceContext(void) const { |
| 130 uint32_t r = cmd_prefix_ >> 6; |
| 131 uint32_t c = cmd_prefix_ & 7; |
| 132 if ((r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2)) { |
| 133 return c; |
| 134 } |
| 135 return 3; |
| 136 } |
| 137 |
| 138 inline uint32_t copy_len(void) const { |
| 139 return copy_len_ & 0xFFFFFF; |
| 140 } |
| 141 |
| 142 inline uint32_t copy_len_code(void) const { |
| 143 return (copy_len_ & 0xFFFFFF) ^ (copy_len_ >> 24); |
| 144 } |
| 145 |
| 146 uint32_t insert_len_; |
| 147 /* Stores copy_len in low 24 bits and copy_len XOR copy_code in high 8 bit. */ |
| 148 uint32_t copy_len_; |
| 149 uint32_t dist_extra_; |
| 150 uint16_t cmd_prefix_; |
| 151 uint16_t dist_prefix_; |
| 152 }; |
| 153 |
| 154 } // namespace brotli |
| 155 |
| 156 #endif // BROTLI_ENC_COMMAND_H_ |
| OLD | NEW |