bit_reader.c 6.4 KB

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