Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libwebp/src/utils/bit_reader_inl_utils.h
9912 views
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 ([email protected])
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
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 uint8_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_UBSAN_IGNORE_UNDEF WEBP_INLINE
58
void VP8LoadNewBytes(VP8BitReader* WEBP_RESTRICT 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!
107
static WEBP_INLINE int VP8GetBit(VP8BitReader* WEBP_RESTRICT 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)
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; // -1 or 0
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
// Don't move this declaration! It makes a big speed difference to store
162
// 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
163
// alter br->range_ value.
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; // Don't use 'const int bit = (value > split);", it's slower.
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
} // extern "C"
194
#endif
195
196
#endif // WEBP_UTILS_BIT_READER_INL_UTILS_H_
197
198