command.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* This class models a sequence of literals and a backward reference copy. */
  6. #ifndef BROTLI_ENC_COMMAND_H_
  7. #define BROTLI_ENC_COMMAND_H_
  8. #include "../common/constants.h"
  9. #include "../common/platform.h"
  10. #include <brotli/types.h>
  11. #include "./fast_log.h"
  12. #include "./params.h"
  13. #include "./prefix.h"
  14. #if defined(__cplusplus) || defined(c_plusplus)
  15. extern "C" {
  16. #endif
  17. static uint32_t kInsBase[] = { 0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26, 34, 50,
  18. 66, 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594 };
  19. static uint32_t kInsExtra[] = { 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
  20. 5, 5, 6, 7, 8, 9, 10, 12, 14, 24 };
  21. static uint32_t kCopyBase[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 18, 22, 30,
  22. 38, 54, 70, 102, 134, 198, 326, 582, 1094, 2118 };
  23. static uint32_t kCopyExtra[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
  24. 4, 4, 5, 5, 6, 7, 8, 9, 10, 24 };
  25. static BROTLI_INLINE uint16_t GetInsertLengthCode(size_t insertlen) {
  26. if (insertlen < 6) {
  27. return (uint16_t)insertlen;
  28. } else if (insertlen < 130) {
  29. uint32_t nbits = Log2FloorNonZero(insertlen - 2) - 1u;
  30. return (uint16_t)((nbits << 1) + ((insertlen - 2) >> nbits) + 2);
  31. } else if (insertlen < 2114) {
  32. return (uint16_t)(Log2FloorNonZero(insertlen - 66) + 10);
  33. } else if (insertlen < 6210) {
  34. return 21u;
  35. } else if (insertlen < 22594) {
  36. return 22u;
  37. } else {
  38. return 23u;
  39. }
  40. }
  41. static BROTLI_INLINE uint16_t GetCopyLengthCode(size_t copylen) {
  42. if (copylen < 10) {
  43. return (uint16_t)(copylen - 2);
  44. } else if (copylen < 134) {
  45. uint32_t nbits = Log2FloorNonZero(copylen - 6) - 1u;
  46. return (uint16_t)((nbits << 1) + ((copylen - 6) >> nbits) + 4);
  47. } else if (copylen < 2118) {
  48. return (uint16_t)(Log2FloorNonZero(copylen - 70) + 12);
  49. } else {
  50. return 23u;
  51. }
  52. }
  53. static BROTLI_INLINE uint16_t CombineLengthCodes(
  54. uint16_t inscode, uint16_t copycode, BROTLI_BOOL use_last_distance) {
  55. uint16_t bits64 =
  56. (uint16_t)((copycode & 0x7u) | ((inscode & 0x7u) << 3u));
  57. if (use_last_distance && inscode < 8u && copycode < 16u) {
  58. return (copycode < 8u) ? bits64 : (bits64 | 64u);
  59. } else {
  60. /* Specification: 5 Encoding of ... (last table) */
  61. /* offset = 2 * index, where index is in range [0..8] */
  62. uint32_t offset = 2u * ((copycode >> 3u) + 3u * (inscode >> 3u));
  63. /* All values in specification are K * 64,
  64. where K = [2, 3, 6, 4, 5, 8, 7, 9, 10],
  65. i + 1 = [1, 2, 3, 4, 5, 6, 7, 8, 9],
  66. K - i - 1 = [1, 1, 3, 0, 0, 2, 0, 1, 2] = D.
  67. All values in D require only 2 bits to encode.
  68. Magic constant is shifted 6 bits left, to avoid final multiplication. */
  69. offset = (offset << 5u) + 0x40u + ((0x520D40u >> offset) & 0xC0u);
  70. return (uint16_t)(offset | bits64);
  71. }
  72. }
  73. static BROTLI_INLINE void GetLengthCode(size_t insertlen, size_t copylen,
  74. BROTLI_BOOL use_last_distance,
  75. uint16_t* code) {
  76. uint16_t inscode = GetInsertLengthCode(insertlen);
  77. uint16_t copycode = GetCopyLengthCode(copylen);
  78. *code = CombineLengthCodes(inscode, copycode, use_last_distance);
  79. }
  80. static BROTLI_INLINE uint32_t GetInsertBase(uint16_t inscode) {
  81. return kInsBase[inscode];
  82. }
  83. static BROTLI_INLINE uint32_t GetInsertExtra(uint16_t inscode) {
  84. return kInsExtra[inscode];
  85. }
  86. static BROTLI_INLINE uint32_t GetCopyBase(uint16_t copycode) {
  87. return kCopyBase[copycode];
  88. }
  89. static BROTLI_INLINE uint32_t GetCopyExtra(uint16_t copycode) {
  90. return kCopyExtra[copycode];
  91. }
  92. typedef struct Command {
  93. uint32_t insert_len_;
  94. /* Stores copy_len in low 25 bits and copy_code - copy_len in high 7 bit. */
  95. uint32_t copy_len_;
  96. /* Stores distance extra bits. */
  97. uint32_t dist_extra_;
  98. uint16_t cmd_prefix_;
  99. /* Stores distance code in low 10 bits
  100. and number of extra bits in high 6 bits. */
  101. uint16_t dist_prefix_;
  102. } Command;
  103. /* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */
  104. static BROTLI_INLINE void InitCommand(Command* self,
  105. const BrotliDistanceParams* dist, size_t insertlen,
  106. size_t copylen, int copylen_code_delta, size_t distance_code) {
  107. /* Don't rely on signed int representation, use honest casts. */
  108. uint32_t delta = (uint8_t)((int8_t)copylen_code_delta);
  109. self->insert_len_ = (uint32_t)insertlen;
  110. self->copy_len_ = (uint32_t)(copylen | (delta << 25));
  111. /* The distance prefix and extra bits are stored in this Command as if
  112. npostfix and ndirect were 0, they are only recomputed later after the
  113. clustering if needed. */
  114. PrefixEncodeCopyDistance(
  115. distance_code, dist->num_direct_distance_codes,
  116. dist->distance_postfix_bits, &self->dist_prefix_, &self->dist_extra_);
  117. GetLengthCode(
  118. insertlen, (size_t)((int)copylen + copylen_code_delta),
  119. TO_BROTLI_BOOL((self->dist_prefix_ & 0x3FF) == 0), &self->cmd_prefix_);
  120. }
  121. static BROTLI_INLINE void InitInsertCommand(Command* self, size_t insertlen) {
  122. self->insert_len_ = (uint32_t)insertlen;
  123. self->copy_len_ = 4 << 25;
  124. self->dist_extra_ = 0;
  125. self->dist_prefix_ = BROTLI_NUM_DISTANCE_SHORT_CODES;
  126. GetLengthCode(insertlen, 4, BROTLI_FALSE, &self->cmd_prefix_);
  127. }
  128. static BROTLI_INLINE uint32_t CommandRestoreDistanceCode(
  129. const Command* self, const BrotliDistanceParams* dist) {
  130. if ((self->dist_prefix_ & 0x3FFu) <
  131. BROTLI_NUM_DISTANCE_SHORT_CODES + dist->num_direct_distance_codes) {
  132. return self->dist_prefix_ & 0x3FFu;
  133. } else {
  134. uint32_t dcode = self->dist_prefix_ & 0x3FFu;
  135. uint32_t nbits = self->dist_prefix_ >> 10;
  136. uint32_t extra = self->dist_extra_;
  137. uint32_t postfix_mask = (1U << dist->distance_postfix_bits) - 1U;
  138. uint32_t hcode = (dcode - dist->num_direct_distance_codes -
  139. BROTLI_NUM_DISTANCE_SHORT_CODES) >>
  140. dist->distance_postfix_bits;
  141. uint32_t lcode = (dcode - dist->num_direct_distance_codes -
  142. BROTLI_NUM_DISTANCE_SHORT_CODES) & postfix_mask;
  143. uint32_t offset = ((2U + (hcode & 1U)) << nbits) - 4U;
  144. return ((offset + extra) << dist->distance_postfix_bits) + lcode +
  145. dist->num_direct_distance_codes + BROTLI_NUM_DISTANCE_SHORT_CODES;
  146. }
  147. }
  148. static BROTLI_INLINE uint32_t CommandDistanceContext(const Command* self) {
  149. uint32_t r = self->cmd_prefix_ >> 6;
  150. uint32_t c = self->cmd_prefix_ & 7;
  151. if ((r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2)) {
  152. return c;
  153. }
  154. return 3;
  155. }
  156. static BROTLI_INLINE uint32_t CommandCopyLen(const Command* self) {
  157. return self->copy_len_ & 0x1FFFFFF;
  158. }
  159. static BROTLI_INLINE uint32_t CommandCopyLenCode(const Command* self) {
  160. uint32_t modifier = self->copy_len_ >> 25;
  161. int32_t delta = (int8_t)((uint8_t)(modifier | ((modifier & 0x40) << 1)));
  162. return (uint32_t)((int32_t)(self->copy_len_ & 0x1FFFFFF) + delta);
  163. }
  164. #if defined(__cplusplus) || defined(c_plusplus)
  165. } /* extern "C" */
  166. #endif
  167. #endif /* BROTLI_ENC_COMMAND_H_ */