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