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 <string.h> // for memcpy
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
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,
108 int prob, const char label[]) {
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 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// simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)
139static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
140int VP8GetSigned(VP8BitReader* const br, int v, const char label[]) {
141 if (br->bits_ < 0) {
142 VP8LoadNewBytes(br);
143 }
144 {
145 const int pos = br->bits_;
146 const range_t split = br->range_ >> 1;
147 const range_t value = (range_t)(br->value_ >> pos);
148 const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0
149 br->bits_ -= 1;
150 br->range_ += mask;
151 br->range_ |= 1;
152 br->value_ -= (bit_t)((split + 1) & mask) << pos;
153 BT_TRACK(br);
154 return (v ^ mask) - mask;
155 }
156}
157
158static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* const br,
159 int prob, const char label[]) {
160 // Don't move this declaration! It makes a big speed difference to store
161 // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
162 // alter br->range_ value.
163 range_t range = br->range_;
164 if (br->bits_ < 0) {
165 VP8LoadNewBytes(br);
166 }
167 {
168 const int pos = br->bits_;
169 const range_t split = (range * prob) >> 8;
170 const range_t value = (range_t)(br->value_ >> pos);
171 int bit; // Don't use 'const int bit = (value > split);", it's slower.
172 if (value > split) {
173 range -= split + 1;
174 br->value_ -= (bit_t)(split + 1) << pos;
175 bit = 1;
176 } else {
177 range = split;
178 bit = 0;
179 }
180 if (range <= (range_t)0x7e) {
181 const int shift = kVP8Log2Range[range];
182 range = kVP8NewRange[range];
183 br->bits_ -= shift;
184 }
185 br->range_ = range;
186 BT_TRACK(br);
187 return bit;
188 }
189}
190
191#ifdef __cplusplus
192} // extern "C"
193#endif
194
195#endif // WEBP_UTILS_BIT_READER_INL_UTILS_H_
196