bit_reader.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 non-inlined methods
  11. //
  12. // Author: Skal ([email protected])
  13. #ifdef HAVE_CONFIG_H
  14. #include "webp/config.h"
  15. #endif
  16. #include "./bit_reader_inl.h"
  17. #include "../utils/utils.h"
  18. //------------------------------------------------------------------------------
  19. // VP8BitReader
  20. void VP8BitReaderSetBuffer(VP8BitReader* const br,
  21. const uint8_t* const start,
  22. size_t size) {
  23. br->buf_ = start;
  24. br->buf_end_ = start + size;
  25. br->buf_max_ =
  26. (size >= sizeof(lbit_t)) ? start + size - sizeof(lbit_t) + 1
  27. : start;
  28. }
  29. void VP8InitBitReader(VP8BitReader* const br,
  30. const uint8_t* const start, size_t size) {
  31. assert(br != NULL);
  32. assert(start != NULL);
  33. assert(size < (1u << 31)); // limit ensured by format and upstream checks
  34. br->range_ = 255 - 1;
  35. br->value_ = 0;
  36. br->bits_ = -8; // to load the very first 8bits
  37. br->eof_ = 0;
  38. VP8BitReaderSetBuffer(br, start, size);
  39. #ifdef JAVASCRIPT_ENABLED // html5 required aligned reads
  40. while(((uintptr_t)br->buf_ & 1) != 0 && !br->eof_)
  41. VP8LoadFinalBytes(br);
  42. #else
  43. VP8LoadNewBytes(br);
  44. #endif
  45. }
  46. void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset) {
  47. if (br->buf_ != NULL) {
  48. br->buf_ += offset;
  49. br->buf_end_ += offset;
  50. br->buf_max_ += offset;
  51. }
  52. }
  53. const uint8_t kVP8Log2Range[128] = {
  54. 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  55. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  56. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  57. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  58. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  59. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  60. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  61. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  62. 0
  63. };
  64. // range = ((range - 1) << kVP8Log2Range[range]) + 1
  65. const uint8_t kVP8NewRange[128] = {
  66. 127, 127, 191, 127, 159, 191, 223, 127,
  67. 143, 159, 175, 191, 207, 223, 239, 127,
  68. 135, 143, 151, 159, 167, 175, 183, 191,
  69. 199, 207, 215, 223, 231, 239, 247, 127,
  70. 131, 135, 139, 143, 147, 151, 155, 159,
  71. 163, 167, 171, 175, 179, 183, 187, 191,
  72. 195, 199, 203, 207, 211, 215, 219, 223,
  73. 227, 231, 235, 239, 243, 247, 251, 127,
  74. 129, 131, 133, 135, 137, 139, 141, 143,
  75. 145, 147, 149, 151, 153, 155, 157, 159,
  76. 161, 163, 165, 167, 169, 171, 173, 175,
  77. 177, 179, 181, 183, 185, 187, 189, 191,
  78. 193, 195, 197, 199, 201, 203, 205, 207,
  79. 209, 211, 213, 215, 217, 219, 221, 223,
  80. 225, 227, 229, 231, 233, 235, 237, 239,
  81. 241, 243, 245, 247, 249, 251, 253, 127
  82. };
  83. void VP8LoadFinalBytes(VP8BitReader* const br) {
  84. assert(br != NULL && br->buf_ != NULL);
  85. // Only read 8bits at a time
  86. if (br->buf_ < br->buf_end_) {
  87. br->bits_ += 8;
  88. br->value_ = (bit_t)(*br->buf_++) | (br->value_ << 8);
  89. } else if (!br->eof_) {
  90. br->value_ <<= 8;
  91. br->bits_ += 8;
  92. br->eof_ = 1;
  93. } else {
  94. br->bits_ = 0; // This is to avoid undefined behaviour with shifts.
  95. }
  96. }
  97. //------------------------------------------------------------------------------
  98. // Higher-level calls
  99. uint32_t VP8GetValue(VP8BitReader* const br, int bits) {
  100. uint32_t v = 0;
  101. while (bits-- > 0) {
  102. v |= VP8GetBit(br, 0x80) << bits;
  103. }
  104. return v;
  105. }
  106. int32_t VP8GetSignedValue(VP8BitReader* const br, int bits) {
  107. const int value = VP8GetValue(br, bits);
  108. return VP8Get(br) ? -value : value;
  109. }
  110. //------------------------------------------------------------------------------
  111. // VP8LBitReader
  112. #define VP8L_LOG8_WBITS 4 // Number of bytes needed to store VP8L_WBITS bits.
  113. #if defined(__arm__) || defined(_M_ARM) || defined(__aarch64__) || \
  114. defined(__i386__) || defined(_M_IX86) || \
  115. defined(__x86_64__) || defined(_M_X64)
  116. #define VP8L_USE_FAST_LOAD
  117. #endif
  118. static const uint32_t kBitMask[VP8L_MAX_NUM_BIT_READ + 1] = {
  119. 0,
  120. 0x000001, 0x000003, 0x000007, 0x00000f,
  121. 0x00001f, 0x00003f, 0x00007f, 0x0000ff,
  122. 0x0001ff, 0x0003ff, 0x0007ff, 0x000fff,
  123. 0x001fff, 0x003fff, 0x007fff, 0x00ffff,
  124. 0x01ffff, 0x03ffff, 0x07ffff, 0x0fffff,
  125. 0x1fffff, 0x3fffff, 0x7fffff, 0xffffff
  126. };
  127. void VP8LInitBitReader(VP8LBitReader* const br, const uint8_t* const start,
  128. size_t length) {
  129. size_t i;
  130. vp8l_val_t value = 0;
  131. assert(br != NULL);
  132. assert(start != NULL);
  133. assert(length < 0xfffffff8u); // can't happen with a RIFF chunk.
  134. br->len_ = length;
  135. br->val_ = 0;
  136. br->bit_pos_ = 0;
  137. br->eos_ = 0;
  138. if (length > sizeof(br->val_)) {
  139. length = sizeof(br->val_);
  140. }
  141. for (i = 0; i < length; ++i) {
  142. value |= (vp8l_val_t)start[i] << (8 * i);
  143. }
  144. br->val_ = value;
  145. br->pos_ = length;
  146. br->buf_ = start;
  147. }
  148. void VP8LBitReaderSetBuffer(VP8LBitReader* const br,
  149. const uint8_t* const buf, size_t len) {
  150. assert(br != NULL);
  151. assert(buf != NULL);
  152. assert(len < 0xfffffff8u); // can't happen with a RIFF chunk.
  153. br->buf_ = buf;
  154. br->len_ = len;
  155. // pos_ > len_ should be considered a param error.
  156. br->eos_ = (br->pos_ > br->len_) || VP8LIsEndOfStream(br);
  157. }
  158. static void VP8LSetEndOfStream(VP8LBitReader* const br) {
  159. br->eos_ = 1;
  160. br->bit_pos_ = 0; // To avoid undefined behaviour with shifts.
  161. }
  162. // If not at EOS, reload up to VP8L_LBITS byte-by-byte
  163. static void ShiftBytes(VP8LBitReader* const br) {
  164. while (br->bit_pos_ >= 8 && br->pos_ < br->len_) {
  165. br->val_ >>= 8;
  166. br->val_ |= ((vp8l_val_t)br->buf_[br->pos_]) << (VP8L_LBITS - 8);
  167. ++br->pos_;
  168. br->bit_pos_ -= 8;
  169. }
  170. if (VP8LIsEndOfStream(br)) {
  171. VP8LSetEndOfStream(br);
  172. }
  173. }
  174. void VP8LDoFillBitWindow(VP8LBitReader* const br) {
  175. assert(br->bit_pos_ >= VP8L_WBITS);
  176. #if defined(VP8L_USE_FAST_LOAD)
  177. if (br->pos_ + sizeof(br->val_) < br->len_) {
  178. br->val_ >>= VP8L_WBITS;
  179. br->bit_pos_ -= VP8L_WBITS;
  180. br->val_ |= (vp8l_val_t)HToLE32(WebPMemToUint32(br->buf_ + br->pos_)) <<
  181. (VP8L_LBITS - VP8L_WBITS);
  182. br->pos_ += VP8L_LOG8_WBITS;
  183. return;
  184. }
  185. #endif
  186. ShiftBytes(br); // Slow path.
  187. }
  188. uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits) {
  189. assert(n_bits >= 0);
  190. // Flag an error if end_of_stream or n_bits is more than allowed limit.
  191. if (!br->eos_ && n_bits <= VP8L_MAX_NUM_BIT_READ) {
  192. const uint32_t val = VP8LPrefetchBits(br) & kBitMask[n_bits];
  193. const int new_bits = br->bit_pos_ + n_bits;
  194. br->bit_pos_ = new_bits;
  195. ShiftBytes(br);
  196. return val;
  197. } else {
  198. VP8LSetEndOfStream(br);
  199. return 0;
  200. }
  201. }
  202. //------------------------------------------------------------------------------