bit_reader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/constants.h"
  10. #include "../common/platform.h"
  11. #include <brotli/types.h>
  12. #if defined(__cplusplus) || defined(c_plusplus)
  13. extern "C" {
  14. #endif
  15. #define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1)
  16. BROTLI_INTERNAL extern const uint32_t kBrotliBitMask[33];
  17. static BROTLI_INLINE uint32_t BitMask(uint32_t n) {
  18. if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
  19. /* Masking with this expression turns to a single
  20. "Unsigned Bit Field Extract" UBFX instruction on ARM. */
  21. return ~((0xFFFFFFFFu) << n);
  22. } else {
  23. return kBrotliBitMask[n];
  24. }
  25. }
  26. typedef struct {
  27. brotli_reg_t val_; /* pre-fetched bits */
  28. uint32_t bit_pos_; /* current bit-reading position in val_ */
  29. const uint8_t* next_in; /* the byte we're reading from */
  30. size_t avail_in;
  31. } BrotliBitReader;
  32. typedef struct {
  33. brotli_reg_t val_;
  34. uint32_t bit_pos_;
  35. const uint8_t* next_in;
  36. size_t avail_in;
  37. } BrotliBitReaderState;
  38. /* Initializes the BrotliBitReader fields. */
  39. BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* const br);
  40. /* Ensures that accumulator is not empty.
  41. May consume up to sizeof(brotli_reg_t) - 1 bytes of input.
  42. Returns BROTLI_FALSE if data is required but there is no input available.
  43. For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned
  44. reading. */
  45. BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br);
  46. /* Fallback for BrotliSafeReadBits32. Extracted as noninlined method to unburden
  47. the main code-path. Never called for RFC brotli streams, required only for
  48. "large-window" mode and other extensions. */
  49. BROTLI_INTERNAL BROTLI_NOINLINE BROTLI_BOOL BrotliSafeReadBits32Slow(
  50. BrotliBitReader* const br, uint32_t n_bits, uint32_t* val);
  51. static BROTLI_INLINE void BrotliBitReaderSaveState(
  52. BrotliBitReader* const from, BrotliBitReaderState* to) {
  53. to->val_ = from->val_;
  54. to->bit_pos_ = from->bit_pos_;
  55. to->next_in = from->next_in;
  56. to->avail_in = from->avail_in;
  57. }
  58. static BROTLI_INLINE void BrotliBitReaderRestoreState(
  59. BrotliBitReader* const to, BrotliBitReaderState* from) {
  60. to->val_ = from->val_;
  61. to->bit_pos_ = from->bit_pos_;
  62. to->next_in = from->next_in;
  63. to->avail_in = from->avail_in;
  64. }
  65. static BROTLI_INLINE uint32_t BrotliGetAvailableBits(
  66. const BrotliBitReader* br) {
  67. return (BROTLI_64_BITS ? 64 : 32) - br->bit_pos_;
  68. }
  69. /* Returns amount of unread bytes the bit reader still has buffered from the
  70. BrotliInput, including whole bytes in br->val_. Result is capped with
  71. maximal ring-buffer size (larger number won't be utilized anyway). */
  72. static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
  73. static const size_t kCap = (size_t)1 << BROTLI_LARGE_MAX_WBITS;
  74. if (br->avail_in > kCap) return kCap;
  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(("[BrotliTakeBits] %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. Up to 24 bits are allowed to be requested from this method. */
  219. static BROTLI_INLINE uint32_t BrotliReadBits24(
  220. BrotliBitReader* const br, uint32_t n_bits) {
  221. BROTLI_DCHECK(n_bits <= 24);
  222. if (BROTLI_64_BITS || (n_bits <= 16)) {
  223. uint32_t val;
  224. BrotliFillBitWindow(br, n_bits);
  225. BrotliTakeBits(br, n_bits, &val);
  226. return val;
  227. } else {
  228. uint32_t low_val;
  229. uint32_t high_val;
  230. BrotliFillBitWindow(br, 16);
  231. BrotliTakeBits(br, 16, &low_val);
  232. BrotliFillBitWindow(br, 8);
  233. BrotliTakeBits(br, n_bits - 16, &high_val);
  234. return low_val | (high_val << 16);
  235. }
  236. }
  237. /* Same as BrotliReadBits24, but allows reading up to 32 bits. */
  238. static BROTLI_INLINE uint32_t BrotliReadBits32(
  239. BrotliBitReader* const br, uint32_t n_bits) {
  240. BROTLI_DCHECK(n_bits <= 32);
  241. if (BROTLI_64_BITS || (n_bits <= 16)) {
  242. uint32_t val;
  243. BrotliFillBitWindow(br, n_bits);
  244. BrotliTakeBits(br, n_bits, &val);
  245. return val;
  246. } else {
  247. uint32_t low_val;
  248. uint32_t high_val;
  249. BrotliFillBitWindow(br, 16);
  250. BrotliTakeBits(br, 16, &low_val);
  251. BrotliFillBitWindow(br, 16);
  252. BrotliTakeBits(br, n_bits - 16, &high_val);
  253. return low_val | (high_val << 16);
  254. }
  255. }
  256. /* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there
  257. is not enough input. |n_bits| MUST be positive.
  258. Up to 24 bits are allowed to be requested from this method. */
  259. static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(
  260. BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
  261. BROTLI_DCHECK(n_bits <= 24);
  262. while (BrotliGetAvailableBits(br) < n_bits) {
  263. if (!BrotliPullByte(br)) {
  264. return BROTLI_FALSE;
  265. }
  266. }
  267. BrotliTakeBits(br, n_bits, val);
  268. return BROTLI_TRUE;
  269. }
  270. /* Same as BrotliSafeReadBits, but allows reading up to 32 bits. */
  271. static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits32(
  272. BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
  273. BROTLI_DCHECK(n_bits <= 32);
  274. if (BROTLI_64_BITS || (n_bits <= 24)) {
  275. while (BrotliGetAvailableBits(br) < n_bits) {
  276. if (!BrotliPullByte(br)) {
  277. return BROTLI_FALSE;
  278. }
  279. }
  280. BrotliTakeBits(br, n_bits, val);
  281. return BROTLI_TRUE;
  282. } else {
  283. return BrotliSafeReadBits32Slow(br, n_bits, val);
  284. }
  285. }
  286. /* Advances the bit reader position to the next byte boundary and verifies
  287. that any skipped bits are set to zero. */
  288. static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
  289. uint32_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
  290. uint32_t pad_bits = 0;
  291. if (pad_bits_count != 0) {
  292. BrotliTakeBits(br, pad_bits_count, &pad_bits);
  293. }
  294. return TO_BROTLI_BOOL(pad_bits == 0);
  295. }
  296. /* Copies remaining input bytes stored in the bit reader to the output. Value
  297. |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be
  298. warmed up again after this. */
  299. static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest,
  300. BrotliBitReader* br, size_t num) {
  301. while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
  302. *dest = (uint8_t)BrotliGetBitsUnmasked(br);
  303. BrotliDropBits(br, 8);
  304. ++dest;
  305. --num;
  306. }
  307. memcpy(dest, br->next_in, num);
  308. br->avail_in -= num;
  309. br->next_in += num;
  310. }
  311. #if defined(__cplusplus) || defined(c_plusplus)
  312. } /* extern "C" */
  313. #endif
  314. #endif /* BROTLI_DEC_BIT_READER_H_ */