bit_reader.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. /* Bit reading helpers */
  6. #ifndef BROTLI_DEC_BIT_READER_H_
  7. #define BROTLI_DEC_BIT_READER_H_
  8. #include <string.h> /* memcpy */
  9. #include "../common/platform.h"
  10. #include <brotli/types.h>
  11. #if defined(__cplusplus) || defined(c_plusplus)
  12. extern "C" {
  13. #endif
  14. #define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1)
  15. static const uint32_t kBitMask[33] = { 0x00000000,
  16. 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
  17. 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
  18. 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
  19. 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
  20. 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
  21. 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
  22. 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
  23. 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
  24. };
  25. static BROTLI_INLINE uint32_t BitMask(uint32_t n) {
  26. if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
  27. /* Masking with this expression turns to a single
  28. "Unsigned Bit Field Extract" UBFX instruction on ARM. */
  29. return ~((0xFFFFFFFFu) << n);
  30. } else {
  31. return kBitMask[n];
  32. }
  33. }
  34. typedef struct {
  35. brotli_reg_t val_; /* pre-fetched bits */
  36. uint32_t bit_pos_; /* current bit-reading position in val_ */
  37. const uint8_t* next_in; /* the byte we're reading from */
  38. size_t avail_in;
  39. } BrotliBitReader;
  40. typedef struct {
  41. brotli_reg_t val_;
  42. uint32_t bit_pos_;
  43. const uint8_t* next_in;
  44. size_t avail_in;
  45. } BrotliBitReaderState;
  46. /* Initializes the BrotliBitReader fields. */
  47. BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* const br);
  48. /* Ensures that accumulator is not empty.
  49. May consume up to sizeof(brotli_reg_t) - 1 bytes of input.
  50. Returns BROTLI_FALSE if data is required but there is no input available.
  51. For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned
  52. reading. */
  53. BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br);
  54. static BROTLI_INLINE void BrotliBitReaderSaveState(
  55. BrotliBitReader* const from, BrotliBitReaderState* to) {
  56. to->val_ = from->val_;
  57. to->bit_pos_ = from->bit_pos_;
  58. to->next_in = from->next_in;
  59. to->avail_in = from->avail_in;
  60. }
  61. static BROTLI_INLINE void BrotliBitReaderRestoreState(
  62. BrotliBitReader* const to, BrotliBitReaderState* from) {
  63. to->val_ = from->val_;
  64. to->bit_pos_ = from->bit_pos_;
  65. to->next_in = from->next_in;
  66. to->avail_in = from->avail_in;
  67. }
  68. static BROTLI_INLINE uint32_t BrotliGetAvailableBits(
  69. const BrotliBitReader* br) {
  70. return (BROTLI_64_BITS ? 64 : 32) - br->bit_pos_;
  71. }
  72. /* Returns amount of unread bytes the bit reader still has buffered from the
  73. BrotliInput, including whole bytes in br->val_. */
  74. static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
  75. return br->avail_in + (BrotliGetAvailableBits(br) >> 3);
  76. }
  77. /* Checks if there is at least |num| bytes left in the input ring-buffer
  78. (excluding the bits remaining in br->val_). */
  79. static BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount(
  80. BrotliBitReader* const br, size_t num) {
  81. return TO_BROTLI_BOOL(br->avail_in >= num);
  82. }
  83. /* Guarantees that there are at least |n_bits| + 1 bits in accumulator.
  84. Precondition: accumulator contains at least 1 bit.
  85. |n_bits| should be in the range [1..24] for regular build. For portable
  86. non-64-bit little-endian build only 16 bits are safe to request. */
  87. static BROTLI_INLINE void BrotliFillBitWindow(
  88. BrotliBitReader* const br, uint32_t n_bits) {
  89. #if (BROTLI_64_BITS)
  90. if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) {
  91. if (br->bit_pos_ >= 56) {
  92. br->val_ >>= 56;
  93. br->bit_pos_ ^= 56; /* here same as -= 56 because of the if condition */
  94. br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 8;
  95. br->avail_in -= 7;
  96. br->next_in += 7;
  97. }
  98. } else if (
  99. !BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 16)) {
  100. if (br->bit_pos_ >= 48) {
  101. br->val_ >>= 48;
  102. br->bit_pos_ ^= 48; /* here same as -= 48 because of the if condition */
  103. br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 16;
  104. br->avail_in -= 6;
  105. br->next_in += 6;
  106. }
  107. } else {
  108. if (br->bit_pos_ >= 32) {
  109. br->val_ >>= 32;
  110. br->bit_pos_ ^= 32; /* here same as -= 32 because of the if condition */
  111. br->val_ |= ((uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in)) << 32;
  112. br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
  113. br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
  114. }
  115. }
  116. #else
  117. if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) {
  118. if (br->bit_pos_ >= 24) {
  119. br->val_ >>= 24;
  120. br->bit_pos_ ^= 24; /* here same as -= 24 because of the if condition */
  121. br->val_ |= BROTLI_UNALIGNED_LOAD32LE(br->next_in) << 8;
  122. br->avail_in -= 3;
  123. br->next_in += 3;
  124. }
  125. } else {
  126. if (br->bit_pos_ >= 16) {
  127. br->val_ >>= 16;
  128. br->bit_pos_ ^= 16; /* here same as -= 16 because of the if condition */
  129. br->val_ |= ((uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in)) << 16;
  130. br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
  131. br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
  132. }
  133. }
  134. #endif
  135. }
  136. /* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no
  137. more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */
  138. static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {
  139. BrotliFillBitWindow(br, 17);
  140. }
  141. /* Tries to pull one byte of input to accumulator.
  142. Returns BROTLI_FALSE if there is no input available. */
  143. static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
  144. if (br->avail_in == 0) {
  145. return BROTLI_FALSE;
  146. }
  147. br->val_ >>= 8;
  148. #if (BROTLI_64_BITS)
  149. br->val_ |= ((uint64_t)*br->next_in) << 56;
  150. #else
  151. br->val_ |= ((uint32_t)*br->next_in) << 24;
  152. #endif
  153. br->bit_pos_ -= 8;
  154. --br->avail_in;
  155. ++br->next_in;
  156. return BROTLI_TRUE;
  157. }
  158. /* Returns currently available bits.
  159. The number of valid bits could be calculated by BrotliGetAvailableBits. */
  160. static BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked(
  161. BrotliBitReader* const br) {
  162. return br->val_ >> br->bit_pos_;
  163. }
  164. /* Like BrotliGetBits, but does not mask the result.
  165. The result contains at least 16 valid bits. */
  166. static BROTLI_INLINE uint32_t BrotliGet16BitsUnmasked(
  167. BrotliBitReader* const br) {
  168. BrotliFillBitWindow(br, 16);
  169. return (uint32_t)BrotliGetBitsUnmasked(br);
  170. }
  171. /* Returns the specified number of bits from |br| without advancing bit
  172. position. */
  173. static BROTLI_INLINE uint32_t BrotliGetBits(
  174. BrotliBitReader* const br, uint32_t n_bits) {
  175. BrotliFillBitWindow(br, n_bits);
  176. return (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
  177. }
  178. /* Tries to peek the specified amount of bits. Returns BROTLI_FALSE, if there
  179. is not enough input. */
  180. static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits(
  181. BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
  182. while (BrotliGetAvailableBits(br) < n_bits) {
  183. if (!BrotliPullByte(br)) {
  184. return BROTLI_FALSE;
  185. }
  186. }
  187. *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
  188. return BROTLI_TRUE;
  189. }
  190. /* Advances the bit pos by |n_bits|. */
  191. static BROTLI_INLINE void BrotliDropBits(
  192. BrotliBitReader* const br, uint32_t n_bits) {
  193. br->bit_pos_ += n_bits;
  194. }
  195. static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
  196. uint32_t unused_bytes = BrotliGetAvailableBits(br) >> 3;
  197. uint32_t unused_bits = unused_bytes << 3;
  198. br->avail_in += unused_bytes;
  199. br->next_in -= unused_bytes;
  200. if (unused_bits == sizeof(br->val_) << 3) {
  201. br->val_ = 0;
  202. } else {
  203. br->val_ <<= unused_bits;
  204. }
  205. br->bit_pos_ += unused_bits;
  206. }
  207. /* Reads the specified number of bits from |br| and advances the bit pos.
  208. Precondition: accumulator MUST contain at least |n_bits|. */
  209. static BROTLI_INLINE void BrotliTakeBits(
  210. BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
  211. *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
  212. BROTLI_LOG(("[BrotliReadBits] %d %d %d val: %6x\n",
  213. (int)br->avail_in, (int)br->bit_pos_, (int)n_bits, (int)*val));
  214. BrotliDropBits(br, n_bits);
  215. }
  216. /* Reads the specified number of bits from |br| and advances the bit pos.
  217. Assumes that there is enough input to perform BrotliFillBitWindow. */
  218. static BROTLI_INLINE uint32_t BrotliReadBits(
  219. BrotliBitReader* const br, uint32_t n_bits) {
  220. if (BROTLI_64_BITS || (n_bits <= 16)) {
  221. uint32_t val;
  222. BrotliFillBitWindow(br, n_bits);
  223. BrotliTakeBits(br, n_bits, &val);
  224. return val;
  225. } else {
  226. uint32_t low_val;
  227. uint32_t high_val;
  228. BrotliFillBitWindow(br, 16);
  229. BrotliTakeBits(br, 16, &low_val);
  230. BrotliFillBitWindow(br, 8);
  231. BrotliTakeBits(br, n_bits - 16, &high_val);
  232. return low_val | (high_val << 16);
  233. }
  234. }
  235. /* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there
  236. is not enough input. |n_bits| MUST be positive. */
  237. static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(
  238. BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
  239. while (BrotliGetAvailableBits(br) < n_bits) {
  240. if (!BrotliPullByte(br)) {
  241. return BROTLI_FALSE;
  242. }
  243. }
  244. BrotliTakeBits(br, n_bits, val);
  245. return BROTLI_TRUE;
  246. }
  247. /* Advances the bit reader position to the next byte boundary and verifies
  248. that any skipped bits are set to zero. */
  249. static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
  250. uint32_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
  251. uint32_t pad_bits = 0;
  252. if (pad_bits_count != 0) {
  253. BrotliTakeBits(br, pad_bits_count, &pad_bits);
  254. }
  255. return TO_BROTLI_BOOL(pad_bits == 0);
  256. }
  257. /* Copies remaining input bytes stored in the bit reader to the output. Value
  258. |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be
  259. warmed up again after this. */
  260. static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest,
  261. BrotliBitReader* br, size_t num) {
  262. while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
  263. *dest = (uint8_t)BrotliGetBitsUnmasked(br);
  264. BrotliDropBits(br, 8);
  265. ++dest;
  266. --num;
  267. }
  268. memcpy(dest, br->next_in, num);
  269. br->avail_in -= num;
  270. br->next_in += num;
  271. }
  272. #if defined(__cplusplus) || defined(c_plusplus)
  273. } /* extern "C" */
  274. #endif
  275. #endif /* BROTLI_DEC_BIT_READER_H_ */