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