bit_reader_inl.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_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 range_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_INLINE void VP8LoadNewBytes(VP8BitReader* const br) {
  48. assert(br != NULL && br->buf_ != NULL);
  49. // Read 'BITS' bits at a time if possible.
  50. if (br->buf_ + sizeof(lbit_t) <= br->buf_end_) {
  51. // convert memory type to register type (with some zero'ing!)
  52. bit_t bits;
  53. #if defined(WEBP_FORCE_ALIGNED)
  54. lbit_t in_bits;
  55. memcpy(&in_bits, br->buf_, sizeof(in_bits));
  56. #elif defined(WEBP_USE_MIPS32)
  57. // This is needed because of un-aligned read.
  58. lbit_t in_bits;
  59. lbit_t* p_buf_ = (lbit_t*)br->buf_;
  60. __asm__ volatile(
  61. ".set push \n\t"
  62. ".set at \n\t"
  63. ".set macro \n\t"
  64. "ulw %[in_bits], 0(%[p_buf_]) \n\t"
  65. ".set pop \n\t"
  66. : [in_bits]"=r"(in_bits)
  67. : [p_buf_]"r"(p_buf_)
  68. : "memory", "at"
  69. );
  70. #else
  71. const lbit_t in_bits = *(const lbit_t*)br->buf_;
  72. #endif
  73. br->buf_ += BITS >> 3;
  74. #if !defined(WORDS_BIGENDIAN)
  75. #if (BITS > 32)
  76. bits = BSwap64(in_bits);
  77. bits >>= 64 - BITS;
  78. #elif (BITS >= 24)
  79. bits = BSwap32(in_bits);
  80. bits >>= (32 - BITS);
  81. #elif (BITS == 16)
  82. bits = BSwap16(in_bits);
  83. #else // BITS == 8
  84. bits = (bit_t)in_bits;
  85. #endif // BITS > 32
  86. #else // WORDS_BIGENDIAN
  87. bits = (bit_t)in_bits;
  88. if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS);
  89. #endif
  90. br->value_ = bits | (br->value_ << BITS);
  91. br->bits_ += BITS;
  92. } else {
  93. VP8LoadFinalBytes(br); // no need to be inlined
  94. }
  95. }
  96. // Read a bit with proba 'prob'. Speed-critical function!
  97. static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) {
  98. // Don't move this declaration! It makes a big speed difference to store
  99. // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
  100. // alter br->range_ value.
  101. range_t range = br->range_;
  102. if (br->bits_ < 0) {
  103. VP8LoadNewBytes(br);
  104. }
  105. {
  106. const int pos = br->bits_;
  107. const range_t split = (range * prob) >> 8;
  108. const range_t value = (range_t)(br->value_ >> pos);
  109. #if defined(__arm__) || defined(_M_ARM) // ARM-specific
  110. const int bit = ((int)(split - value) >> 31) & 1;
  111. if (value > split) {
  112. range -= split + 1;
  113. br->value_ -= (bit_t)(split + 1) << pos;
  114. } else {
  115. range = split;
  116. }
  117. #else // faster version on x86
  118. int bit; // Don't use 'const int bit = (value > split);", it's slower.
  119. if (value > split) {
  120. range -= split + 1;
  121. br->value_ -= (bit_t)(split + 1) << pos;
  122. bit = 1;
  123. } else {
  124. range = split;
  125. bit = 0;
  126. }
  127. #endif
  128. if (range <= (range_t)0x7e) {
  129. const int shift = kVP8Log2Range[range];
  130. range = kVP8NewRange[range];
  131. br->bits_ -= shift;
  132. }
  133. br->range_ = range;
  134. return bit;
  135. }
  136. }
  137. // simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)
  138. static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) {
  139. if (br->bits_ < 0) {
  140. VP8LoadNewBytes(br);
  141. }
  142. {
  143. const int pos = br->bits_;
  144. const range_t split = br->range_ >> 1;
  145. const range_t value = (range_t)(br->value_ >> pos);
  146. const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0
  147. br->bits_ -= 1;
  148. br->range_ += mask;
  149. br->range_ |= 1;
  150. br->value_ -= (bit_t)((split + 1) & mask) << pos;
  151. return (v ^ mask) - mask;
  152. }
  153. }
  154. #ifdef __cplusplus
  155. } // extern "C"
  156. #endif
  157. #endif // WEBP_UTILS_BIT_READER_INL_H_