9#ifndef BROTLI_DEC_BIT_READER_H_
10#define BROTLI_DEC_BIT_READER_H_
16#include "../common/constants.h"
17#include "../common/platform.h"
19#if defined(__cplusplus) || defined(c_plusplus)
23#define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1)
26#define BROTLI_FAST_INPUT_SLACK 28
28BROTLI_INTERNAL
extern const brotli_reg_t kBrotliBitMask[33];
30static BROTLI_INLINE brotli_reg_t BitMask(brotli_reg_t n) {
31 if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
34 return ~(~((brotli_reg_t)0) << n);
36 return kBrotliBitMask[n];
42 brotli_reg_t bit_pos_;
43 const uint8_t* next_in;
44 const uint8_t* guard_in;
45 const uint8_t* last_in;
50 brotli_reg_t bit_pos_;
51 const uint8_t* next_in;
68BROTLI_INTERNAL BROTLI_NOINLINE
BROTLI_BOOL BrotliSafeReadBits32Slow(
71static BROTLI_INLINE
size_t
73 return (
size_t)(br->last_in - br->next_in);
76static BROTLI_INLINE
void BrotliBitReaderSaveState(
78 to->val_ = from->val_;
79 to->bit_pos_ = from->bit_pos_;
80 to->next_in = from->next_in;
81 to->avail_in = BrotliBitReaderGetAvailIn(from);
84static BROTLI_INLINE
void BrotliBitReaderSetInput(
86 br->next_in = next_in;
87 br->last_in = (avail_in == 0) ? next_in : (next_in + avail_in);
88 if (avail_in + 1 > BROTLI_FAST_INPUT_SLACK) {
89 br->guard_in = next_in + (avail_in + 1 - BROTLI_FAST_INPUT_SLACK);
91 br->guard_in = next_in;
95static BROTLI_INLINE
void BrotliBitReaderRestoreState(
97 to->val_ = from->val_;
98 to->bit_pos_ = from->bit_pos_;
99 to->next_in = from->next_in;
100 BrotliBitReaderSetInput(to, from->next_in, from->avail_in);
103static BROTLI_INLINE brotli_reg_t BrotliGetAvailableBits(
111static BROTLI_INLINE
size_t BrotliGetRemainingBytes(
BrotliBitReader* br) {
113 size_t avail_in = BrotliBitReaderGetAvailIn(br);
114 if (avail_in > kCap)
return kCap;
115 return avail_in + (BrotliGetAvailableBits(br) >> 3);
120static BROTLI_INLINE
BROTLI_BOOL BrotliCheckInputAmount(
126static BROTLI_INLINE brotli_reg_t BrotliBitReaderLoadBits(brotli_reg_t val,
127 brotli_reg_t new_bits,
129 brotli_reg_t offset) {
131 !((val >> offset) & ~new_bits & ~(~((brotli_reg_t)0) << count)));
133 return val | (new_bits << offset);
140static BROTLI_INLINE
void BrotliFillBitWindow(
143 if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
145 brotli_reg_t bit_pos = br->bit_pos_;
147 br->val_ = BrotliBitReaderLoadBits(br->val_,
148 BROTLI_UNALIGNED_LOAD64LE(br->next_in), 56, bit_pos);
149 br->bit_pos_ = bit_pos + 56;
152 }
else if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
154 brotli_reg_t bit_pos = br->bit_pos_;
156 br->val_ = BrotliBitReaderLoadBits(br->val_,
157 BROTLI_UNALIGNED_LOAD64LE(br->next_in), 48, bit_pos);
158 br->bit_pos_ = bit_pos + 48;
162 brotli_reg_t bit_pos = br->bit_pos_;
164 br->val_ = BrotliBitReaderLoadBits(br->val_,
165 (uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in), 32, bit_pos);
166 br->bit_pos_ = bit_pos + 32;
167 br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
171 if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
173 brotli_reg_t bit_pos = br->bit_pos_;
175 br->val_ = BrotliBitReaderLoadBits(br->val_,
176 BROTLI_UNALIGNED_LOAD32LE(br->next_in), 24, bit_pos);
177 br->bit_pos_ = bit_pos + 24;
181 brotli_reg_t bit_pos = br->bit_pos_;
183 br->val_ = BrotliBitReaderLoadBits(br->val_,
184 (uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in), 16, bit_pos);
185 br->bit_pos_ = bit_pos + 16;
186 br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
194static BROTLI_INLINE
void BrotliFillBitWindow16(
BrotliBitReader*
const br) {
195 BrotliFillBitWindow(br, 17);
201 if (br->next_in == br->last_in) {
204 br->val_ = BrotliBitReaderLoadBits(br->val_,
205 (brotli_reg_t)*br->next_in, 8, br->bit_pos_);
213static BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked(
220static BROTLI_INLINE brotli_reg_t BrotliGet16BitsUnmasked(
222 BrotliFillBitWindow(br, 16);
223 return (brotli_reg_t)BrotliGetBitsUnmasked(br);
228static BROTLI_INLINE brotli_reg_t BrotliGetBits(
230 BrotliFillBitWindow(br, n_bits);
231 return BrotliGetBitsUnmasked(br) & BitMask(n_bits);
238 while (BrotliGetAvailableBits(br) < n_bits) {
239 if (!BrotliPullByte(br)) {
243 *val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);
248static BROTLI_INLINE
void BrotliDropBits(
250 br->bit_pos_ -= n_bits;
257static BROTLI_INLINE
void BrotliBitReaderNormalize(
BrotliBitReader* br) {
259 if (br->bit_pos_ < (
sizeof(brotli_reg_t) << 3u)) {
260 br->val_ &= (((brotli_reg_t)1) << br->bit_pos_) - 1;
265 brotli_reg_t unused_bytes = BrotliGetAvailableBits(br) >> 3;
266 brotli_reg_t unused_bits = unused_bytes << 3;
268 (unused_bytes == 0) ? br->next_in : (br->next_in - unused_bytes);
269 br->bit_pos_ -= unused_bits;
270 BrotliBitReaderNormalize(br);
278 *val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);
279 BROTLI_LOG((
"[BrotliTakeBits] %d %d %d val: %6x\n",
280 (
int)BrotliBitReaderGetAvailIn(br), (
int)br->bit_pos_,
281 (
int)n_bits, (
int)*val));
282 BrotliDropBits(br, n_bits);
288static BROTLI_INLINE brotli_reg_t BrotliReadBits24(
290 BROTLI_DCHECK(n_bits <= 24);
291 if (BROTLI_64_BITS || (n_bits <= 16)) {
293 BrotliFillBitWindow(br, n_bits);
294 BrotliTakeBits(br, n_bits, &val);
297 brotli_reg_t low_val;
298 brotli_reg_t high_val;
299 BrotliFillBitWindow(br, 16);
300 BrotliTakeBits(br, 16, &low_val);
301 BrotliFillBitWindow(br, 8);
302 BrotliTakeBits(br, n_bits - 16, &high_val);
303 return low_val | (high_val << 16);
308static BROTLI_INLINE brotli_reg_t BrotliReadBits32(
310 BROTLI_DCHECK(n_bits <= 32);
311 if (BROTLI_64_BITS || (n_bits <= 16)) {
313 BrotliFillBitWindow(br, n_bits);
314 BrotliTakeBits(br, n_bits, &val);
317 brotli_reg_t low_val;
318 brotli_reg_t high_val;
319 BrotliFillBitWindow(br, 16);
320 BrotliTakeBits(br, 16, &low_val);
321 BrotliFillBitWindow(br, 16);
322 BrotliTakeBits(br, n_bits - 16, &high_val);
323 return low_val | (high_val << 16);
330static BROTLI_INLINE
BROTLI_BOOL BrotliSafeReadBits(
332 BROTLI_DCHECK(n_bits <= 24);
333 while (BrotliGetAvailableBits(br) < n_bits) {
334 if (!BrotliPullByte(br)) {
338 BrotliTakeBits(br, n_bits, val);
343static BROTLI_INLINE
BROTLI_BOOL BrotliSafeReadBits32(
345 BROTLI_DCHECK(n_bits <= 32);
346 if (BROTLI_64_BITS || (n_bits <= 24)) {
347 while (BrotliGetAvailableBits(br) < n_bits) {
348 if (!BrotliPullByte(br)) {
352 BrotliTakeBits(br, n_bits, val);
355 return BrotliSafeReadBits32Slow(br, n_bits, val);
362 brotli_reg_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
363 brotli_reg_t pad_bits = 0;
364 if (pad_bits_count != 0) {
365 BrotliTakeBits(br, pad_bits_count, &pad_bits);
367 BrotliBitReaderNormalize(br);
371static BROTLI_INLINE
void BrotliDropBytes(
BrotliBitReader* br,
size_t num) {
373 BROTLI_DCHECK(br->bit_pos_ == 0);
374 BROTLI_DCHECK(br->val_ == 0);
381static BROTLI_INLINE
void BrotliCopyBytes(uint8_t* dest,
383 while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
384 *dest = (uint8_t)BrotliGetBitsUnmasked(br);
385 BrotliDropBits(br, 8);
389 BrotliBitReaderNormalize(br);
391 memcpy(dest, br->next_in, num);
392 BrotliDropBytes(br, num);
396BROTLI_UNUSED_FUNCTION
void BrotliBitReaderSuppressUnusedFunctions(
void) {
397 BROTLI_UNUSED(&BrotliBitReaderSuppressUnusedFunctions);
399 BROTLI_UNUSED(&BrotliBitReaderGetAvailIn);
400 BROTLI_UNUSED(&BrotliBitReaderLoadBits);
401 BROTLI_UNUSED(&BrotliBitReaderRestoreState);
402 BROTLI_UNUSED(&BrotliBitReaderSaveState);
403 BROTLI_UNUSED(&BrotliBitReaderSetInput);
404 BROTLI_UNUSED(&BrotliBitReaderUnload);
405 BROTLI_UNUSED(&BrotliCheckInputAmount);
406 BROTLI_UNUSED(&BrotliCopyBytes);
407 BROTLI_UNUSED(&BrotliFillBitWindow16);
408 BROTLI_UNUSED(&BrotliGet16BitsUnmasked);
409 BROTLI_UNUSED(&BrotliGetBits);
410 BROTLI_UNUSED(&BrotliGetRemainingBytes);
411 BROTLI_UNUSED(&BrotliJumpToByteBoundary);
412 BROTLI_UNUSED(&BrotliReadBits24);
413 BROTLI_UNUSED(&BrotliReadBits32);
414 BROTLI_UNUSED(&BrotliSafeGetBits);
415 BROTLI_UNUSED(&BrotliSafeReadBits);
416 BROTLI_UNUSED(&BrotliSafeReadBits32);
419#if defined(__cplusplus) || defined(c_plusplus)
#define BROTLI_LARGE_MAX_WBITS
The maximum supported large brotli window bits by the encoder and decoder.
Definition: constants.h:58
Definition: bit_reader.h:48
Definition: bit_reader.h:40
Common types used in decoder and encoder API.
#define BROTLI_TRUE
Portable true replacement.
Definition: types.h:51
#define BROTLI_FALSE
Portable false replacement.
Definition: types.h:53
#define TO_BROTLI_BOOL(X)
bool to BROTLI_BOOL conversion macros.
Definition: types.h:55
#define BROTLI_BOOL
A portable bool replacement.
Definition: types.h:49