bit_reader_inl_utils.h (5930B)
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_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 <assert.h> 24 #include <string.h> // for memcpy 25 26 #include "src/dsp/cpu.h" 27 #include "src/dsp/dsp.h" 28 #include "src/utils/bit_reader_utils.h" 29 #include "src/utils/endian_inl_utils.h" 30 #include "src/utils/utils.h" 31 #include "src/webp/types.h" 32 33 #ifdef __cplusplus 34 extern "C" { 35 #endif 36 37 //------------------------------------------------------------------------------ 38 // Derived type lbit_t = natural type for memory I/O 39 40 #if (BITS > 32) 41 typedef uint64_t lbit_t; 42 #elif (BITS > 16) 43 typedef uint32_t lbit_t; 44 #elif (BITS > 8) 45 typedef uint16_t lbit_t; 46 #else 47 typedef uint8_t lbit_t; 48 #endif 49 50 extern const uint8_t kVP8Log2Range[128]; 51 extern const uint8_t kVP8NewRange[128]; 52 53 // special case for the tail byte-reading 54 void VP8LoadFinalBytes(VP8BitReader* const br); 55 56 //------------------------------------------------------------------------------ 57 // Inlined critical functions 58 59 // makes sure br->value has at least BITS bits worth of data 60 static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE 61 void VP8LoadNewBytes(VP8BitReader* WEBP_RESTRICT const br) { 62 assert(br != NULL && br->buf != NULL); 63 // Read 'BITS' bits at a time if possible. 64 if (br->buf < br->buf_max) { 65 // convert memory type to register type (with some zero'ing!) 66 bit_t bits; 67 #if defined(WEBP_USE_MIPS32) 68 // This is needed because of un-aligned read. 69 lbit_t in_bits; 70 lbit_t* p_buf = (lbit_t*)br->buf; 71 __asm__ volatile( 72 ".set push \n\t" 73 ".set at \n\t" 74 ".set macro \n\t" 75 "ulw %[in_bits], 0(%[p_buf]) \n\t" 76 ".set pop \n\t" 77 : [in_bits]"=r"(in_bits) 78 : [p_buf]"r"(p_buf) 79 : "memory", "at" 80 ); 81 #else 82 lbit_t in_bits; 83 memcpy(&in_bits, br->buf, sizeof(in_bits)); 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* WEBP_RESTRICT const br, 111 int prob, const char label[]) { 112 // Don't move this declaration! It makes a big speed difference to store 113 // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't 114 // alter br->range value. 115 range_t range = br->range; 116 if (br->bits < 0) { 117 VP8LoadNewBytes(br); 118 } 119 { 120 const int pos = br->bits; 121 const range_t split = (range * prob) >> 8; 122 const range_t value = (range_t)(br->value >> pos); 123 const int bit = (value > split); 124 if (bit) { 125 range -= split; 126 br->value -= (bit_t)(split + 1) << pos; 127 } else { 128 range = split + 1; 129 } 130 { 131 const int shift = 7 ^ BitsLog2Floor(range); 132 range <<= shift; 133 br->bits -= shift; 134 } 135 br->range = range - 1; 136 BT_TRACK(br); 137 return bit; 138 } 139 } 140 141 // simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here) 142 static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE 143 int VP8GetSigned(VP8BitReader* WEBP_RESTRICT const br, int v, 144 const char label[]) { 145 if (br->bits < 0) { 146 VP8LoadNewBytes(br); 147 } 148 { 149 const int pos = br->bits; 150 const range_t split = br->range >> 1; 151 const range_t value = (range_t)(br->value >> pos); 152 const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0 153 br->bits -= 1; 154 br->range += (range_t)mask; 155 br->range |= 1; 156 br->value -= (bit_t)((split + 1) & (uint32_t)mask) << pos; 157 BT_TRACK(br); 158 return (v ^ mask) - mask; 159 } 160 } 161 162 static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* WEBP_RESTRICT const br, 163 int prob, const char label[]) { 164 // Don't move this declaration! It makes a big speed difference to store 165 // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't 166 // alter br->range value. 167 range_t range = br->range; 168 if (br->bits < 0) { 169 VP8LoadNewBytes(br); 170 } 171 { 172 const int pos = br->bits; 173 const range_t split = (range * prob) >> 8; 174 const range_t value = (range_t)(br->value >> pos); 175 int bit; // Don't use 'const int bit = (value > split);", it's slower. 176 if (value > split) { 177 range -= split + 1; 178 br->value -= (bit_t)(split + 1) << pos; 179 bit = 1; 180 } else { 181 range = split; 182 bit = 0; 183 } 184 if (range <= (range_t)0x7e) { 185 const int shift = kVP8Log2Range[range]; 186 range = kVP8NewRange[range]; 187 br->bits -= shift; 188 } 189 br->range = range; 190 BT_TRACK(br); 191 return bit; 192 } 193 } 194 195 #ifdef __cplusplus 196 } // extern "C" 197 #endif 198 199 #endif // WEBP_UTILS_BIT_READER_INL_UTILS_H_