bit_reader.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Boolean decoder
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. // Vikas Arora (vikaas.arora@gmail.com)
  14. #ifndef WEBP_UTILS_BIT_READER_H_
  15. #define WEBP_UTILS_BIT_READER_H_
  16. #include <assert.h>
  17. #ifdef _MSC_VER
  18. #include <stdlib.h> // _byteswap_ulong
  19. #endif
  20. #include "../webp/types.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. // The Boolean decoder needs to maintain infinite precision on the value_ field.
  25. // However, since range_ is only 8bit, we only need an active window of 8 bits
  26. // for value_. Left bits (MSB) gets zeroed and shifted away when value_ falls
  27. // below 128, range_ is updated, and fresh bits read from the bitstream are
  28. // brought in as LSB. To avoid reading the fresh bits one by one (slow), we
  29. // cache BITS of them ahead. The total of (BITS + 8) bits must fit into a
  30. // natural register (with type bit_t). To fetch BITS bits from bitstream we
  31. // use a type lbit_t.
  32. //
  33. // BITS can be any multiple of 8 from 8 to 56 (inclusive).
  34. // Pick values that fit natural register size.
  35. #if defined(__i386__) || defined(_M_IX86) // x86 32bit
  36. #define BITS 24
  37. #elif defined(__x86_64__) || defined(_M_X64) // x86 64bit
  38. #define BITS 56
  39. #elif defined(__arm__) || defined(_M_ARM) // ARM
  40. #define BITS 24
  41. #elif defined(__mips__) // MIPS
  42. #define BITS 24
  43. #else // reasonable default
  44. #define BITS 24 // TODO(skal): test aarch64 and find the proper BITS value.
  45. #endif
  46. //------------------------------------------------------------------------------
  47. // Derived types and constants:
  48. // bit_t = natural register type for storing 'value_' (which is BITS+8 bits)
  49. // range_t = register for 'range_' (which is 8bits only)
  50. #if (BITS > 24)
  51. typedef uint64_t bit_t;
  52. #else
  53. typedef uint32_t bit_t;
  54. #endif
  55. typedef uint32_t range_t;
  56. //------------------------------------------------------------------------------
  57. // Bitreader
  58. typedef struct VP8BitReader VP8BitReader;
  59. struct VP8BitReader {
  60. // boolean decoder (keep the field ordering as is!)
  61. bit_t value_; // current value
  62. range_t range_; // current range minus 1. In [127, 254] interval.
  63. int bits_; // number of valid bits left
  64. // read buffer
  65. const uint8_t* buf_; // next byte to be read
  66. const uint8_t* buf_end_; // end of read buffer
  67. int eof_; // true if input is exhausted
  68. };
  69. // Initialize the bit reader and the boolean decoder.
  70. void VP8InitBitReader(VP8BitReader* const br,
  71. const uint8_t* const start, const uint8_t* const end);
  72. // Update internal pointers to displace the byte buffer by the
  73. // relative offset 'offset'.
  74. void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset);
  75. // return the next value made of 'num_bits' bits
  76. uint32_t VP8GetValue(VP8BitReader* const br, int num_bits);
  77. static WEBP_INLINE uint32_t VP8Get(VP8BitReader* const br) {
  78. return VP8GetValue(br, 1);
  79. }
  80. // return the next value with sign-extension.
  81. int32_t VP8GetSignedValue(VP8BitReader* const br, int num_bits);
  82. // bit_reader_inl.h will implement the following methods:
  83. // static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob)
  84. // static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v)
  85. // and should be included by the .c files that actually need them.
  86. // This is to avoid recompiling the whole library whenever this file is touched,
  87. // and also allowing platform-specific ad-hoc hacks.
  88. // -----------------------------------------------------------------------------
  89. // Bitreader for lossless format
  90. // maximum number of bits (inclusive) the bit-reader can handle:
  91. #define VP8L_MAX_NUM_BIT_READ 24
  92. #define VP8L_LBITS 64 // Number of bits prefetched.
  93. #define VP8L_WBITS 32 // Minimum number of bytes ready after VP8LFillBitWindow.
  94. typedef uint64_t vp8l_val_t; // right now, this bit-reader can only use 64bit.
  95. typedef struct {
  96. vp8l_val_t val_; // pre-fetched bits
  97. const uint8_t* buf_; // input byte buffer
  98. size_t len_; // buffer length
  99. size_t pos_; // byte position in buf_
  100. int bit_pos_; // current bit-reading position in val_
  101. int eos_; // bitstream is finished
  102. int error_; // an error occurred (buffer overflow attempt...)
  103. } VP8LBitReader;
  104. void VP8LInitBitReader(VP8LBitReader* const br,
  105. const uint8_t* const start,
  106. size_t length);
  107. // Sets a new data buffer.
  108. void VP8LBitReaderSetBuffer(VP8LBitReader* const br,
  109. const uint8_t* const buffer, size_t length);
  110. // Reads the specified number of bits from read buffer.
  111. // Flags an error in case end_of_stream or n_bits is more than the allowed limit
  112. // of VP8L_MAX_NUM_BIT_READ (inclusive).
  113. // Flags eos_ if this read attempt is going to cross the read buffer.
  114. uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits);
  115. // Return the prefetched bits, so they can be looked up.
  116. static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) {
  117. return (uint32_t)(br->val_ >> br->bit_pos_);
  118. }
  119. // Returns true if there was an attempt at reading bit past the end of
  120. // the buffer. Doesn't set br->eos_ flag.
  121. static WEBP_INLINE int VP8LIsEndOfStream(const VP8LBitReader* const br) {
  122. assert(br->pos_ <= br->len_);
  123. return (br->pos_ == br->len_) && (br->bit_pos_ > VP8L_LBITS);
  124. }
  125. // For jumping over a number of bits in the bit stream when accessed with
  126. // VP8LPrefetchBits and VP8LFillBitWindow.
  127. static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) {
  128. br->bit_pos_ = val;
  129. br->eos_ = VP8LIsEndOfStream(br);
  130. }
  131. // Advances the read buffer by 4 bytes to make room for reading next 32 bits.
  132. // Speed critical, but infrequent part of the code can be non-inlined.
  133. extern void VP8LDoFillBitWindow(VP8LBitReader* const br);
  134. static WEBP_INLINE void VP8LFillBitWindow(VP8LBitReader* const br) {
  135. if (br->bit_pos_ >= VP8L_WBITS) VP8LDoFillBitWindow(br);
  136. }
  137. #ifdef __cplusplus
  138. } // extern "C"
  139. #endif
  140. #endif /* WEBP_UTILS_BIT_READER_H_ */