bit_reader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 <brotli/types.h>
  10. #include "../common/constants.h"
  11. #include "../common/platform.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_UNALIGNED_READ_FAST this function also prepares bit reader for
  44. aligned 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_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
  91. (n_bits <= 8)) {
  92. uint32_t bit_pos = br->bit_pos_;
  93. if (bit_pos >= 56) {
  94. br->val_ =
  95. (br->val_ >> 56) | (BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 8);
  96. br->bit_pos_ =
  97. bit_pos ^ 56; /* here same as -= 56 because of the if condition */
  98. br->avail_in -= 7;
  99. br->next_in += 7;
  100. }
  101. } else if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
  102. (n_bits <= 16)) {
  103. uint32_t bit_pos = br->bit_pos_;
  104. if (bit_pos >= 48) {
  105. br->val_ =
  106. (br->val_ >> 48) | (BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 16);
  107. br->bit_pos_ =
  108. bit_pos ^ 48; /* here same as -= 48 because of the if condition */
  109. br->avail_in -= 6;
  110. br->next_in += 6;
  111. }
  112. } else {
  113. uint32_t bit_pos = br->bit_pos_;
  114. if (bit_pos >= 32) {
  115. br->val_ = (br->val_ >> 32) |
  116. (((uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in)) << 32);
  117. br->bit_pos_ =
  118. bit_pos ^ 32; /* here same as -= 32 because of the if condition */
  119. br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
  120. br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
  121. }
  122. }
  123. #else
  124. if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
  125. (n_bits <= 8)) {
  126. uint32_t bit_pos = br->bit_pos_;
  127. if (bit_pos >= 24) {
  128. br->val_ =
  129. (br->val_ >> 24) | (BROTLI_UNALIGNED_LOAD32LE(br->next_in) << 8);
  130. br->bit_pos_ =
  131. bit_pos ^ 24; /* here same as -= 24 because of the if condition */
  132. br->avail_in -= 3;
  133. br->next_in += 3;
  134. }
  135. } else {
  136. uint32_t bit_pos = br->bit_pos_;
  137. if (bit_pos >= 16) {
  138. br->val_ = (br->val_ >> 16) |
  139. (((uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in)) << 16);
  140. br->bit_pos_ =
  141. bit_pos ^ 16; /* here same as -= 16 because of the if condition */
  142. br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
  143. br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
  144. }
  145. }
  146. #endif
  147. }
  148. /* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no
  149. more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */
  150. static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {
  151. BrotliFillBitWindow(br, 17);
  152. }
  153. /* Tries to pull one byte of input to accumulator.
  154. Returns BROTLI_FALSE if there is no input available. */
  155. static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
  156. if (br->avail_in == 0) {
  157. return BROTLI_FALSE;
  158. }
  159. br->val_ >>= 8;
  160. #if (BROTLI_64_BITS)
  161. br->val_ |= ((uint64_t)*br->next_in) << 56;
  162. #else
  163. br->val_ |= ((uint32_t)*br->next_in) << 24;
  164. #endif
  165. br->bit_pos_ -= 8;
  166. --br->avail_in;
  167. ++br->next_in;
  168. return BROTLI_TRUE;
  169. }
  170. /* Returns currently available bits.
  171. The number of valid bits could be calculated by BrotliGetAvailableBits. */
  172. static BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked(
  173. BrotliBitReader* const br) {
  174. return br->val_ >> br->bit_pos_;
  175. }
  176. /* Like BrotliGetBits, but does not mask the result.
  177. The result contains at least 16 valid bits. */
  178. static BROTLI_INLINE uint32_t BrotliGet16BitsUnmasked(
  179. BrotliBitReader* const br) {
  180. BrotliFillBitWindow(br, 16);
  181. return (uint32_t)BrotliGetBitsUnmasked(br);
  182. }
  183. /* Returns the specified number of bits from |br| without advancing bit
  184. position. */
  185. static BROTLI_INLINE uint32_t BrotliGetBits(
  186. BrotliBitReader* const br, uint32_t n_bits) {
  187. BrotliFillBitWindow(br, n_bits);
  188. return (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
  189. }
  190. /* Tries to peek the specified amount of bits. Returns BROTLI_FALSE, if there
  191. is not enough input. */
  192. static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits(
  193. BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
  194. while (BrotliGetAvailableBits(br) < n_bits) {
  195. if (!BrotliPullByte(br)) {
  196. return BROTLI_FALSE;
  197. }
  198. }
  199. *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
  200. return BROTLI_TRUE;
  201. }
  202. /* Advances the bit pos by |n_bits|. */
  203. static BROTLI_INLINE void BrotliDropBits(
  204. BrotliBitReader* const br, uint32_t n_bits) {
  205. br->bit_pos_ += n_bits;
  206. }
  207. static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
  208. uint32_t unused_bytes = BrotliGetAvailableBits(br) >> 3;
  209. uint32_t unused_bits = unused_bytes << 3;
  210. br->avail_in += unused_bytes;
  211. br->next_in -= unused_bytes;
  212. if (unused_bits == sizeof(br->val_) << 3) {
  213. br->val_ = 0;
  214. } else {
  215. br->val_ <<= unused_bits;
  216. }
  217. br->bit_pos_ += unused_bits;
  218. }
  219. /* Reads the specified number of bits from |br| and advances the bit pos.
  220. Precondition: accumulator MUST contain at least |n_bits|. */
  221. static BROTLI_INLINE void BrotliTakeBits(
  222. BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
  223. *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
  224. BROTLI_LOG(("[BrotliTakeBits] %d %d %d val: %6x\n",
  225. (int)br->avail_in, (int)br->bit_pos_, (int)n_bits, (int)*val));
  226. BrotliDropBits(br, n_bits);
  227. }
  228. /* Reads the specified number of bits from |br| and advances the bit pos.
  229. Assumes that there is enough input to perform BrotliFillBitWindow.
  230. Up to 24 bits are allowed to be requested from this method. */
  231. static BROTLI_INLINE uint32_t BrotliReadBits24(
  232. BrotliBitReader* const br, uint32_t n_bits) {
  233. BROTLI_DCHECK(n_bits <= 24);
  234. if (BROTLI_64_BITS || (n_bits <= 16)) {
  235. uint32_t val;
  236. BrotliFillBitWindow(br, n_bits);
  237. BrotliTakeBits(br, n_bits, &val);
  238. return val;
  239. } else {
  240. uint32_t low_val;
  241. uint32_t high_val;
  242. BrotliFillBitWindow(br, 16);
  243. BrotliTakeBits(br, 16, &low_val);
  244. BrotliFillBitWindow(br, 8);
  245. BrotliTakeBits(br, n_bits - 16, &high_val);
  246. return low_val | (high_val << 16);
  247. }
  248. }
  249. /* Same as BrotliReadBits24, but allows reading up to 32 bits. */
  250. static BROTLI_INLINE uint32_t BrotliReadBits32(
  251. BrotliBitReader* const br, uint32_t n_bits) {
  252. BROTLI_DCHECK(n_bits <= 32);
  253. if (BROTLI_64_BITS || (n_bits <= 16)) {
  254. uint32_t val;
  255. BrotliFillBitWindow(br, n_bits);
  256. BrotliTakeBits(br, n_bits, &val);
  257. return val;
  258. } else {
  259. uint32_t low_val;
  260. uint32_t high_val;
  261. BrotliFillBitWindow(br, 16);
  262. BrotliTakeBits(br, 16, &low_val);
  263. BrotliFillBitWindow(br, 16);
  264. BrotliTakeBits(br, n_bits - 16, &high_val);
  265. return low_val | (high_val << 16);
  266. }
  267. }
  268. /* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there
  269. is not enough input. |n_bits| MUST be positive.
  270. Up to 24 bits are allowed to be requested from this method. */
  271. static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(
  272. BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
  273. BROTLI_DCHECK(n_bits <= 24);
  274. while (BrotliGetAvailableBits(br) < n_bits) {
  275. if (!BrotliPullByte(br)) {
  276. return BROTLI_FALSE;
  277. }
  278. }
  279. BrotliTakeBits(br, n_bits, val);
  280. return BROTLI_TRUE;
  281. }
  282. /* Same as BrotliSafeReadBits, but allows reading up to 32 bits. */
  283. static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits32(
  284. BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
  285. BROTLI_DCHECK(n_bits <= 32);
  286. if (BROTLI_64_BITS || (n_bits <= 24)) {
  287. while (BrotliGetAvailableBits(br) < n_bits) {
  288. if (!BrotliPullByte(br)) {
  289. return BROTLI_FALSE;
  290. }
  291. }
  292. BrotliTakeBits(br, n_bits, val);
  293. return BROTLI_TRUE;
  294. } else {
  295. return BrotliSafeReadBits32Slow(br, n_bits, val);
  296. }
  297. }
  298. /* Advances the bit reader position to the next byte boundary and verifies
  299. that any skipped bits are set to zero. */
  300. static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
  301. uint32_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
  302. uint32_t pad_bits = 0;
  303. if (pad_bits_count != 0) {
  304. BrotliTakeBits(br, pad_bits_count, &pad_bits);
  305. }
  306. return TO_BROTLI_BOOL(pad_bits == 0);
  307. }
  308. static BROTLI_INLINE void BrotliDropBytes(BrotliBitReader* br, size_t num) {
  309. br->avail_in -= num;
  310. br->next_in += num;
  311. }
  312. /* Copies remaining input bytes stored in the bit reader to the output. Value
  313. |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be
  314. warmed up again after this. */
  315. static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest,
  316. BrotliBitReader* br, size_t num) {
  317. while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
  318. *dest = (uint8_t)BrotliGetBitsUnmasked(br);
  319. BrotliDropBits(br, 8);
  320. ++dest;
  321. --num;
  322. }
  323. if (num > 0) {
  324. memcpy(dest, br->next_in, num);
  325. BrotliDropBytes(br, num);
  326. }
  327. }
  328. #if defined(__cplusplus) || defined(c_plusplus)
  329. } /* extern "C" */
  330. #endif
  331. #endif /* BROTLI_DEC_BIT_READER_H_ */