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#include <string.h> // for memcpy
24
25#include "../dsp/dsp.h"
26#include "./bit_reader_utils.h"
27#include "./endian_inl_utils.h"
28#include "./utils.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34//------------------------------------------------------------------------------
35// Derived type lbit_t = natural type for memory I/O
36
37#if (BITS > 32)
38typedef uint64_t lbit_t;
39#elif (BITS > 16)
40typedef uint32_t lbit_t;
41#elif (BITS > 8)
42typedef uint16_t lbit_t;
43#else
44typedef uint8_t lbit_t;
45#endif
46
47extern const uint8_t kVP8Log2Range[128];
48extern const uint8_t kVP8NewRange[128];
49
50// special case for the tail byte-reading
51void VP8LoadFinalBytes(VP8BitReader* const br);
52
53//------------------------------------------------------------------------------
54// Inlined critical functions
55
56// makes sure br->value_ has at least BITS bits worth of data
57static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE
58void VP8LoadNewBytes(VP8BitReader* const br) {
59 assert(br != NULL && br->buf_ != NULL);
60 // Read 'BITS' bits at a time if possible.
61 if (br->buf_ < br->buf_max_) {
62 // convert memory type to register type (with some zero'ing!)
63 bit_t bits;
64#if defined(WEBP_USE_MIPS32)
65 // This is needed because of un-aligned read.
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 // BITS == 8
93 bits = (bit_t)in_bits;
94#endif // BITS > 32
95#else // WORDS_BIGENDIAN
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); // no need to be inlined
103 }
104}
105
106// Read a bit with proba 'prob'. Speed-critical function!
107static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) {
108 // Don't move this declaration! It makes a big speed difference to store
109 // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
110 // alter br->range_ value.
111 range_t range = br->range_;
112 if (br->bits_ < 0) {
113 VP8LoadNewBytes(br);
114 }
115 {
116 const int pos = br->bits_;
117 const range_t split = (range * prob) >> 8;
118 const range_t value = (range_t)(br->value_ >> pos);
119 const int bit = (value > split);
120 if (bit) {
121 range -= split;
122 br->value_ -= (bit_t)(split + 1) << pos;
123 } else {
124 range = split + 1;
125 }
126 {
127 const int shift = 7 ^ BitsLog2Floor(range);
128 range <<= shift;
129 br->bits_ -= shift;
130 }
131 br->range_ = range - 1;
132 return bit;
133 }
134}
135
136// simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)
137static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
138int 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
155static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* const br, int prob) {
156 // Don't move this declaration! It makes a big speed difference to store
157 // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
158 // alter br->range_ value.
159 range_t range = br->range_;
160 if (br->bits_ < 0) {
161 VP8LoadNewBytes(br);
162 }
163 {
164 const int pos = br->bits_;
165 const range_t split = (range * prob) >> 8;
166 const range_t value = (range_t)(br->value_ >> pos);
167 int bit; // Don't use 'const int bit = (value > split);", it's slower.
168 if (value > split) {
169 range -= split + 1;
170 br->value_ -= (bit_t)(split + 1) << pos;
171 bit = 1;
172 } else {
173 range = split;
174 bit = 0;
175 }
176 if (range <= (range_t)0x7e) {
177 const int shift = kVP8Log2Range[range];
178 range = kVP8NewRange[range];
179 br->bits_ -= shift;
180 }
181 br->range_ = range;
182 return bit;
183 }
184}
185
186#ifdef __cplusplus
187} // extern "C"
188#endif
189
190#endif // WEBP_UTILS_BIT_READER_INL_H_
191