| OLD | NEW |
| 1 /* Copyright 2013 Google Inc. All Rights Reserved. | 1 /* Copyright 2013 Google Inc. All Rights Reserved. |
| 2 | 2 |
| 3 Distributed under MIT license. | 3 Distributed under MIT license. |
| 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /* Bit reading helpers */ | 7 /* Bit reading helpers */ |
| 8 | 8 |
| 9 #ifndef BROTLI_DEC_BIT_READER_H_ | 9 #ifndef BROTLI_DEC_BIT_READER_H_ |
| 10 #define BROTLI_DEC_BIT_READER_H_ | 10 #define BROTLI_DEC_BIT_READER_H_ |
| 11 | 11 |
| 12 #include <string.h> /* memcpy */ | 12 #include <string.h> /* memcpy */ |
| 13 | 13 |
| 14 #include "./port.h" | 14 #include "./port.h" |
| 15 #include "./types.h" | 15 #include "./types.h" |
| 16 | 16 |
| 17 #ifdef BROTLI_DECODE_DEBUG | |
| 18 #include <stdio.h> | |
| 19 #endif | |
| 20 | |
| 21 #if defined(__cplusplus) || defined(c_plusplus) | 17 #if defined(__cplusplus) || defined(c_plusplus) |
| 22 extern "C" { | 18 extern "C" { |
| 23 #endif | 19 #endif |
| 24 | 20 |
| 25 #if (BROTLI_64_BITS) | 21 #if (BROTLI_64_BITS) |
| 26 #define BROTLI_SHORT_FILL_BIT_WINDOW_READ 4 | 22 #define BROTLI_SHORT_FILL_BIT_WINDOW_READ 4 |
| 27 typedef uint64_t reg_t; | 23 typedef uint64_t reg_t; |
| 28 #else | 24 #else |
| 29 #define BROTLI_SHORT_FILL_BIT_WINDOW_READ 2 | 25 #define BROTLI_SHORT_FILL_BIT_WINDOW_READ 2 |
| 30 typedef uint32_t reg_t; | 26 typedef uint32_t reg_t; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 59 } BrotliBitReader; | 55 } BrotliBitReader; |
| 60 | 56 |
| 61 typedef struct { | 57 typedef struct { |
| 62 reg_t val_; | 58 reg_t val_; |
| 63 uint32_t bit_pos_; | 59 uint32_t bit_pos_; |
| 64 const uint8_t* next_in; | 60 const uint8_t* next_in; |
| 65 size_t avail_in; | 61 size_t avail_in; |
| 66 } BrotliBitReaderState; | 62 } BrotliBitReaderState; |
| 67 | 63 |
| 68 /* Initializes the bitreader fields. */ | 64 /* Initializes the bitreader fields. */ |
| 69 void BrotliInitBitReader(BrotliBitReader* const br); | 65 BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* const br); |
| 70 | 66 |
| 71 /* Ensures that accumulator is not empty. May consume one byte of input. | 67 /* Ensures that accumulator is not empty. May consume one byte of input. |
| 72 Returns 0 if data is required but there is no input available. | 68 Returns 0 if data is required but there is no input available. |
| 73 For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned | 69 For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned |
| 74 reading. */ | 70 reading. */ |
| 75 int BrotliWarmupBitReader(BrotliBitReader* const br); | 71 BROTLI_INTERNAL int BrotliWarmupBitReader(BrotliBitReader* const br); |
| 76 | 72 |
| 77 static BROTLI_INLINE void BrotliBitReaderSaveState( | 73 static BROTLI_INLINE void BrotliBitReaderSaveState( |
| 78 BrotliBitReader* const from, BrotliBitReaderState* to) { | 74 BrotliBitReader* const from, BrotliBitReaderState* to) { |
| 79 to->val_ = from->val_; | 75 to->val_ = from->val_; |
| 80 to->bit_pos_ = from->bit_pos_; | 76 to->bit_pos_ = from->bit_pos_; |
| 81 to->next_in = from->next_in; | 77 to->next_in = from->next_in; |
| 82 to->avail_in = from->avail_in; | 78 to->avail_in = from->avail_in; |
| 83 } | 79 } |
| 84 | 80 |
| 85 static BROTLI_INLINE void BrotliBitReaderRestoreState( | 81 static BROTLI_INLINE void BrotliBitReaderRestoreState( |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 br->val_ <<= unused_bits; | 287 br->val_ <<= unused_bits; |
| 292 } | 288 } |
| 293 br->bit_pos_ += unused_bits; | 289 br->bit_pos_ += unused_bits; |
| 294 } | 290 } |
| 295 | 291 |
| 296 /* Reads the specified number of bits from br and advances the bit pos. | 292 /* Reads the specified number of bits from br and advances the bit pos. |
| 297 Precondition: accumulator MUST contain at least n_bits. */ | 293 Precondition: accumulator MUST contain at least n_bits. */ |
| 298 static BROTLI_INLINE void BrotliTakeBits( | 294 static BROTLI_INLINE void BrotliTakeBits( |
| 299 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { | 295 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { |
| 300 *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits); | 296 *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits); |
| 301 #ifdef BROTLI_DECODE_DEBUG | 297 BROTLI_LOG(("[BrotliReadBits] %d %d %d val: %6x\n", |
| 302 printf("[BrotliReadBits] %d %d %d val: %6x\n", | 298 (int)br->avail_in, (int)br->bit_pos_, n_bits, (int)*val)); |
| 303 (int)br->avail_in, (int)br->bit_pos_, n_bits, (int)*val); | |
| 304 #endif | |
| 305 BrotliDropBits(br, n_bits); | 299 BrotliDropBits(br, n_bits); |
| 306 } | 300 } |
| 307 | 301 |
| 308 /* Reads the specified number of bits from br and advances the bit pos. | 302 /* Reads the specified number of bits from br and advances the bit pos. |
| 309 Assumes that there is enough input to perform BrotliFillBitWindow. */ | 303 Assumes that there is enough input to perform BrotliFillBitWindow. */ |
| 310 static BROTLI_INLINE uint32_t BrotliReadBits( | 304 static BROTLI_INLINE uint32_t BrotliReadBits( |
| 311 BrotliBitReader* const br, uint32_t n_bits) { | 305 BrotliBitReader* const br, uint32_t n_bits) { |
| 312 if (BROTLI_64_BITS || (n_bits <= 16)) { | 306 if (BROTLI_64_BITS || (n_bits <= 16)) { |
| 313 uint32_t val; | 307 uint32_t val; |
| 314 BrotliFillBitWindow(br, n_bits); | 308 BrotliFillBitWindow(br, n_bits); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 memcpy(dest, br->next_in, num); | 374 memcpy(dest, br->next_in, num); |
| 381 br->avail_in -= num; | 375 br->avail_in -= num; |
| 382 br->next_in += num; | 376 br->next_in += num; |
| 383 } | 377 } |
| 384 | 378 |
| 385 #if defined(__cplusplus) || defined(c_plusplus) | 379 #if defined(__cplusplus) || defined(c_plusplus) |
| 386 } /* extern "C" */ | 380 } /* extern "C" */ |
| 387 #endif | 381 #endif |
| 388 | 382 |
| 389 #endif /* BROTLI_DEC_BIT_READER_H_ */ | 383 #endif /* BROTLI_DEC_BIT_READER_H_ */ |
| OLD | NEW |