forked from rarten/ooz
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitreader.cpp
244 lines (218 loc) · 5.51 KB
/
bitreader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "bitreader.h"
int BitReader_ReadFluff(BitReader *bits, int num_symbols) {
unsigned long y;
if (num_symbols == 256)
return 0;
int x = 257 - num_symbols;
if (x > num_symbols)
x = num_symbols;
x *= 2;
_BitScanReverse(&y, x - 1);
y += 1;
uint32 v = bits->bits >> (32 - y);
uint32 z = (1 << y) - x;
if ((v >> 1) >= z) {
bits->bits <<= y;
bits->bitpos += y;
return v - z;
} else {
bits->bits <<= (y - 1);
bits->bitpos += (y - 1);
return (v >> 1);
}
}
// Read more bytes to make sure we always have at least 24 bits in |bits|.
void BitReader_Refill(BitReader *bits) {
assert(bits->bitpos <= 24);
while (bits->bitpos > 0) {
bits->bits |= (bits->p < bits->p_end ? *bits->p : 0) << bits->bitpos;
bits->bitpos -= 8;
bits->p++;
}
}
// Read more bytes to make sure we always have at least 24 bits in |bits|,
// used when reading backwards.
void BitReader_RefillBackwards(BitReader *bits) {
assert(bits->bitpos <= 24);
while (bits->bitpos > 0) {
bits->p--;
bits->bits |= (bits->p >= bits->p_end ? *bits->p : 0) << bits->bitpos;
bits->bitpos -= 8;
}
}
// Refill bits then read a single bit.
int BitReader_ReadBit(BitReader *bits) {
int r;
BitReader_Refill(bits);
r = bits->bits >> 31;
bits->bits <<= 1;
bits->bitpos += 1;
return r;
}
int BitReader_ReadBitNoRefill(BitReader *bits) {
int r;
r = bits->bits >> 31;
bits->bits <<= 1;
bits->bitpos += 1;
return r;
}
// Read |n| bits without refilling.
int BitReader_ReadBitsNoRefill(BitReader *bits, int n) {
int r = (bits->bits >> (32 - n));
bits->bits <<= n;
bits->bitpos += n;
return r;
}
// Read |n| bits without refilling, n may be zero.
int BitReader_ReadBitsNoRefillZero(BitReader *bits, int n) {
int r = (bits->bits >> 1 >> (31 - n));
bits->bits <<= n;
bits->bitpos += n;
return r;
}
uint32 BitReader_ReadMoreThan24Bits(BitReader *bits, int n) {
uint32 rv;
if (n <= 24) {
rv = BitReader_ReadBitsNoRefillZero(bits, n);
} else {
rv = BitReader_ReadBitsNoRefill(bits, 24) << (n - 24);
BitReader_Refill(bits);
rv += BitReader_ReadBitsNoRefill(bits, n - 24);
}
BitReader_Refill(bits);
return rv;
}
uint32 BitReader_ReadMoreThan24BitsB(BitReader *bits, int n) {
uint32 rv;
if (n <= 24) {
rv = BitReader_ReadBitsNoRefillZero(bits, n);
} else {
rv = BitReader_ReadBitsNoRefill(bits, 24) << (n - 24);
BitReader_RefillBackwards(bits);
rv += BitReader_ReadBitsNoRefill(bits, n - 24);
}
BitReader_RefillBackwards(bits);
return rv;
}
// Reads a gamma value.
// Assumes bitreader is already filled with at least 23 bits
int BitReader_ReadGamma(BitReader *bits) {
unsigned long bitresult;
int n;
int r;
if (bits->bits != 0) {
_BitScanReverse(&bitresult, bits->bits);
n = 31 - bitresult;
} else {
n = 32;
}
n = 2 * n + 2;
assert(n < 24);
bits->bitpos += n;
r = bits->bits >> (32 - n);
bits->bits <<= n;
return r - 2;
}
// Reads a gamma value with |forced| number of forced bits.
int BitReader_ReadGammaX(BitReader *bits, int forced) {
unsigned long bitresult;
int r;
if (bits->bits != 0) {
_BitScanReverse(&bitresult, bits->bits);
int lz = 31 - bitresult;
assert(lz < 24);
r = (bits->bits >> (31 - lz - forced)) + ((lz - 1) << forced);
bits->bits <<= lz + forced + 1;
bits->bitpos += lz + forced + 1;
return r;
}
return 0;
}
// Reads a offset code parametrized by |v|.
uint32 BitReader_ReadDistance(BitReader *bits, uint32 v) {
uint32 w, m, n, rv;
if (v < 0xF0) {
n = (v >> 4) + 4;
w = _rotl(bits->bits | 1, n);
bits->bitpos += n;
m = (2 << n) - 1;
bits->bits = w & ~m;
rv = ((w & m) << 4) + (v & 0xF) - 248;
} else {
n = v - 0xF0 + 4;
w = _rotl(bits->bits | 1, n);
bits->bitpos += n;
m = (2 << n) - 1;
bits->bits = w & ~m;
rv = 8322816 + ((w & m) << 12);
BitReader_Refill(bits);
rv += (bits->bits >> 20);
bits->bitpos += 12;
bits->bits <<= 12;
}
BitReader_Refill(bits);
return rv;
}
// Reads a offset code parametrized by |v|, backwards.
uint32 BitReader_ReadDistanceB(BitReader *bits, uint32 v) {
uint32 w, m, n, rv;
if (v < 0xF0) {
n = (v >> 4) + 4;
w = _rotl(bits->bits | 1, n);
bits->bitpos += n;
m = (2 << n) - 1;
bits->bits = w & ~m;
rv = ((w & m) << 4) + (v & 0xF) - 248;
} else {
n = v - 0xF0 + 4;
w = _rotl(bits->bits | 1, n);
bits->bitpos += n;
m = (2 << n) - 1;
bits->bits = w & ~m;
rv = 8322816 + ((w & m) << 12);
BitReader_RefillBackwards(bits);
rv += (bits->bits >> (32 - 12));
bits->bitpos += 12;
bits->bits <<= 12;
}
BitReader_RefillBackwards(bits);
return rv;
}
// Reads a length code.
bool BitReader_ReadLength(BitReader *bits, uint32 *v) {
unsigned long bitresult;
int n;
uint32 rv;
_BitScanReverse(&bitresult, bits->bits);
n = 31 - bitresult;
if (n > 12) return false;
bits->bitpos += n;
bits->bits <<= n;
BitReader_Refill(bits);
n += 7;
bits->bitpos += n;
rv = (bits->bits >> (32 - n)) - 64;
bits->bits <<= n;
*v = rv;
BitReader_Refill(bits);
return true;
}
// Reads a length code, backwards.
bool BitReader_ReadLengthB(BitReader *bits, uint32 *v) {
unsigned long bitresult;
int n;
uint32 rv;
_BitScanReverse(&bitresult, bits->bits);
n = 31 - bitresult;
if (n > 12) return false;
bits->bitpos += n;
bits->bits <<= n;
BitReader_RefillBackwards(bits);
n += 7;
bits->bitpos += n;
rv = (bits->bits >> (32 - n)) - 64;
bits->bits <<= n;
*v = rv;
BitReader_RefillBackwards(bits);
return true;
}