| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef WEBP_UTILS_BIT_READER_INL_UTILS_H_ |
| 17 |
#define WEBP_UTILS_BIT_READER_INL_UTILS_H_ |
| 18 |
|
| 19 |
#ifdef HAVE_CONFIG_H |
| 20 |
#include "src/webp/config.h" |
| 21 |
#endif |
| 22 |
|
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include "src/dsp/dsp.h" |
| 26 |
#include "src/utils/bit_reader_utils.h" |
| 27 |
#include "src/utils/endian_inl_utils.h" |
| 28 |
#include "src/utils/utils.h" |
| 29 |
|
| 30 |
#ifdef __cplusplus |
| 31 |
extern "C" { |
| 32 |
#endif |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#if (BITS > 32) |
| 38 |
typedef uint64_t lbit_t; |
| 39 |
#elif (BITS > 16) |
| 40 |
typedef uint32_t lbit_t; |
| 41 |
#elif (BITS > 8) |
| 42 |
typedef uint16_t lbit_t; |
| 43 |
#else |
| 44 |
typedef uint8_t lbit_t; |
| 45 |
#endif |
| 46 |
|
| 47 |
extern const uint8_t kVP8Log2Range[128]; |
| 48 |
extern const uint8_t kVP8NewRange[128]; |
| 49 |
|
| 50 |
|
| 51 |
void VP8LoadFinalBytes(VP8BitReader* const br); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE |
| 58 |
void VP8LoadNewBytes(VP8BitReader* WEBP_RESTRICT const br) { |
| 59 |
assert(br != NULL && br->buf_ != NULL); |
| 60 |
|
| 61 |
if (br->buf_ < br->buf_max_) { |
| 62 |
|
| 63 |
bit_t bits; |
| 64 |
#if defined(WEBP_USE_MIPS32) |
| 65 |
|
| 66 |
lbit_t in_bits; |
| 67 |
lbit_t* p_buf_ = (lbit_t*)br->buf_; |
| 68 |
__asm__ volatile( |
| 69 |
".set push \n\t" |
| 70 |
".set at \n\t" |
| 71 |
".set macro \n\t" |
| 72 |
"ulw %[in_bits], 0(%[p_buf_]) \n\t" |
| 73 |
".set pop \n\t" |
| 74 |
: [in_bits]"=r"(in_bits) |
| 75 |
: [p_buf_]"r"(p_buf_) |
| 76 |
: "memory", "at" |
| 77 |
); |
| 78 |
#else |
| 79 |
lbit_t in_bits; |
| 80 |
memcpy(&in_bits, br->buf_, sizeof(in_bits)); |
| 81 |
#endif |
| 82 |
br->buf_ += BITS >> 3; |
| 83 |
#if !defined(WORDS_BIGENDIAN) |
| 84 |
#if (BITS > 32) |
| 85 |
bits = BSwap64(in_bits); |
| 86 |
bits >>= 64 - BITS; |
| 87 |
#elif (BITS >= 24) |
| 88 |
bits = BSwap32(in_bits); |
| 89 |
bits >>= (32 - BITS); |
| 90 |
#elif (BITS == 16) |
| 91 |
bits = BSwap16(in_bits); |
| 92 |
#else |
| 93 |
bits = (bit_t)in_bits; |
| 94 |
#endif |
| 95 |
#else |
| 96 |
bits = (bit_t)in_bits; |
| 97 |
if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS); |
| 98 |
#endif |
| 99 |
br->value_ = bits | (br->value_ << BITS); |
| 100 |
br->bits_ += BITS; |
| 101 |
} else { |
| 102 |
VP8LoadFinalBytes(br); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
static WEBP_INLINE int VP8GetBit(VP8BitReader* WEBP_RESTRICT const br, |
| 108 |
int prob, const char label[]) { |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
range_t range = br->range_; |
| 113 |
if (br->bits_ < 0) { |
| 114 |
VP8LoadNewBytes(br); |
| 115 |
} |
| 116 |
{ |
| 117 |
const int pos = br->bits_; |
| 118 |
const range_t split = (range * prob) >> 8; |
| 119 |
const range_t value = (range_t)(br->value_ >> pos); |
| 120 |
const int bit = (value > split); |
| 121 |
if (bit) { |
| 122 |
range -= split; |
| 123 |
br->value_ -= (bit_t)(split + 1) << pos; |
| 124 |
} else { |
| 125 |
range = split + 1; |
| 126 |
} |
| 127 |
{ |
| 128 |
const int shift = 7 ^ BitsLog2Floor(range); |
| 129 |
range <<= shift; |
| 130 |
br->bits_ -= shift; |
| 131 |
} |
| 132 |
br->range_ = range - 1; |
| 133 |
BT_TRACK(br); |
| 134 |
return bit; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE |
| 140 |
int VP8GetSigned(VP8BitReader* WEBP_RESTRICT const br, int v, |
| 141 |
const char label[]) { |
| 142 |
if (br->bits_ < 0) { |
| 143 |
VP8LoadNewBytes(br); |
| 144 |
} |
| 145 |
{ |
| 146 |
const int pos = br->bits_; |
| 147 |
const range_t split = br->range_ >> 1; |
| 148 |
const range_t value = (range_t)(br->value_ >> pos); |
| 149 |
const int32_t mask = (int32_t)(split - value) >> 31; |
| 150 |
br->bits_ -= 1; |
| 151 |
br->range_ += (range_t)mask; |
| 152 |
br->range_ |= 1; |
| 153 |
br->value_ -= (bit_t)((split + 1) & (uint32_t)mask) << pos; |
| 154 |
BT_TRACK(br); |
| 155 |
return (v ^ mask) - mask; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* WEBP_RESTRICT const br, |
| 160 |
int prob, const char label[]) { |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
range_t range = br->range_; |
| 165 |
if (br->bits_ < 0) { |
| 166 |
VP8LoadNewBytes(br); |
| 167 |
} |
| 168 |
{ |
| 169 |
const int pos = br->bits_; |
| 170 |
const range_t split = (range * prob) >> 8; |
| 171 |
const range_t value = (range_t)(br->value_ >> pos); |
| 172 |
int bit; |
| 173 |
if (value > split) { |
| 174 |
range -= split + 1; |
| 175 |
br->value_ -= (bit_t)(split + 1) << pos; |
| 176 |
bit = 1; |
| 177 |
} else { |
| 178 |
range = split; |
| 179 |
bit = 0; |
| 180 |
} |
| 181 |
if (range <= (range_t)0x7e) { |
| 182 |
const int shift = kVP8Log2Range[range]; |
| 183 |
range = kVP8NewRange[range]; |
| 184 |
br->bits_ -= shift; |
| 185 |
} |
| 186 |
br->range_ = range; |
| 187 |
BT_TRACK(br); |
| 188 |
return bit; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
#ifdef __cplusplus |
| 193 |
} |
| 194 |
#endif |
| 195 |
|
| 196 |
#endif |