bit_reader_inl.h 5.0 KB

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