bit_reader.h 12 KB

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