bit_reader_inl_utils.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2014 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. // Specific inlined methods for boolean decoder [VP8GetBit() ...]
  11. // This file should be included by the .c sources that actually need to call
  12. // these methods.
  13. //
  14. // Author: Skal (pascal.massimino@gmail.com)
  15. #ifndef WEBP_UTILS_BIT_READER_INL_UTILS_H_
  16. #define WEBP_UTILS_BIT_READER_INL_UTILS_H_
  17. #ifdef HAVE_CONFIG_H
  18. #include "../webp/config.h"
  19. #endif
  20. #include <string.h> // for memcpy
  21. #include "../dsp/dsp.h"
  22. #include "./bit_reader_utils.h"
  23. #include "./endian_inl_utils.h"
  24. #include "./utils.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. //------------------------------------------------------------------------------
  29. // Derived type lbit_t = natural type for memory I/O
  30. #if (BITS > 32)
  31. typedef uint64_t lbit_t;
  32. #elif (BITS > 16)
  33. typedef uint32_t lbit_t;
  34. #elif (BITS > 8)
  35. typedef uint16_t lbit_t;
  36. #else
  37. typedef uint8_t lbit_t;
  38. #endif
  39. extern const uint8_t kVP8Log2Range[128];
  40. extern const uint8_t kVP8NewRange[128];
  41. // special case for the tail byte-reading
  42. void VP8LoadFinalBytes(VP8BitReader* const br);
  43. //------------------------------------------------------------------------------
  44. // Inlined critical functions
  45. // makes sure br->value_ has at least BITS bits worth of data
  46. static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE
  47. void VP8LoadNewBytes(VP8BitReader* WEBP_RESTRICT const br) {
  48. assert(br != NULL && br->buf_ != NULL);
  49. // Read 'BITS' bits at a time if possible.
  50. if (br->buf_ < br->buf_max_) {
  51. // convert memory type to register type (with some zero'ing!)
  52. bit_t bits;
  53. #if defined(WEBP_USE_MIPS32)
  54. // This is needed because of un-aligned read.
  55. lbit_t in_bits;
  56. lbit_t* p_buf_ = (lbit_t*)br->buf_;
  57. __asm__ volatile(
  58. ".set push \n\t"
  59. ".set at \n\t"
  60. ".set macro \n\t"
  61. "ulw %[in_bits], 0(%[p_buf_]) \n\t"
  62. ".set pop \n\t"
  63. : [in_bits]"=r"(in_bits)
  64. : [p_buf_]"r"(p_buf_)
  65. : "memory", "at"
  66. );
  67. #else
  68. lbit_t in_bits;
  69. memcpy(&in_bits, br->buf_, sizeof(in_bits));
  70. #endif
  71. br->buf_ += BITS >> 3;
  72. #if !defined(WORDS_BIGENDIAN)
  73. #if (BITS > 32)
  74. bits = BSwap64(in_bits);
  75. bits >>= 64 - BITS;
  76. #elif (BITS >= 24)
  77. bits = BSwap32(in_bits);
  78. bits >>= (32 - BITS);
  79. #elif (BITS == 16)
  80. bits = BSwap16(in_bits);
  81. #else // BITS == 8
  82. bits = (bit_t)in_bits;
  83. #endif // BITS > 32
  84. #else // WORDS_BIGENDIAN
  85. bits = (bit_t)in_bits;
  86. if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS);
  87. #endif
  88. br->value_ = bits | (br->value_ << BITS);
  89. br->bits_ += BITS;
  90. } else {
  91. VP8LoadFinalBytes(br); // no need to be inlined
  92. }
  93. }
  94. // Read a bit with proba 'prob'. Speed-critical function!
  95. static WEBP_INLINE int VP8GetBit(VP8BitReader* WEBP_RESTRICT const br,
  96. int prob, const char label[]) {
  97. // Don't move this declaration! It makes a big speed difference to store
  98. // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
  99. // alter br->range_ value.
  100. range_t range = br->range_;
  101. if (br->bits_ < 0) {
  102. VP8LoadNewBytes(br);
  103. }
  104. {
  105. const int pos = br->bits_;
  106. const range_t split = (range * prob) >> 8;
  107. const range_t value = (range_t)(br->value_ >> pos);
  108. const int bit = (value > split);
  109. if (bit) {
  110. range -= split;
  111. br->value_ -= (bit_t)(split + 1) << pos;
  112. } else {
  113. range = split + 1;
  114. }
  115. {
  116. const int shift = 7 ^ BitsLog2Floor(range);
  117. range <<= shift;
  118. br->bits_ -= shift;
  119. }
  120. br->range_ = range - 1;
  121. BT_TRACK(br);
  122. return bit;
  123. }
  124. }
  125. // simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)
  126. static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
  127. int VP8GetSigned(VP8BitReader* WEBP_RESTRICT const br, int v,
  128. const char label[]) {
  129. if (br->bits_ < 0) {
  130. VP8LoadNewBytes(br);
  131. }
  132. {
  133. const int pos = br->bits_;
  134. const range_t split = br->range_ >> 1;
  135. const range_t value = (range_t)(br->value_ >> pos);
  136. const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0
  137. br->bits_ -= 1;
  138. br->range_ += mask;
  139. br->range_ |= 1;
  140. br->value_ -= (bit_t)((split + 1) & mask) << pos;
  141. BT_TRACK(br);
  142. return (v ^ mask) - mask;
  143. }
  144. }
  145. static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* WEBP_RESTRICT const br,
  146. int prob, const char label[]) {
  147. // Don't move this declaration! It makes a big speed difference to store
  148. // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
  149. // alter br->range_ value.
  150. range_t range = br->range_;
  151. if (br->bits_ < 0) {
  152. VP8LoadNewBytes(br);
  153. }
  154. {
  155. const int pos = br->bits_;
  156. const range_t split = (range * prob) >> 8;
  157. const range_t value = (range_t)(br->value_ >> pos);
  158. int bit; // Don't use 'const int bit = (value > split);", it's slower.
  159. if (value > split) {
  160. range -= split + 1;
  161. br->value_ -= (bit_t)(split + 1) << pos;
  162. bit = 1;
  163. } else {
  164. range = split;
  165. bit = 0;
  166. }
  167. if (range <= (range_t)0x7e) {
  168. const int shift = kVP8Log2Range[range];
  169. range = kVP8NewRange[range];
  170. br->bits_ -= shift;
  171. }
  172. br->range_ = range;
  173. BT_TRACK(br);
  174. return bit;
  175. }
  176. }
  177. #ifdef __cplusplus
  178. } // extern "C"
  179. #endif
  180. #endif // WEBP_UTILS_BIT_READER_INL_UTILS_H_