-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitStream.h
320 lines (266 loc) · 9.39 KB
/
BitStream.h
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <utility>
#ifndef MEDIENINFO_BITSTREAM_H
#define MEDIENINFO_BITSTREAM_H
#include <utility>
#include <string>
#include <new> // std::bad_alloc
#include <assert.h>
#include <cstring>
#include <iostream>
#include <fstream>
#define _write_segment_ref(bitstream, segment) bitstream.writeBytes(&segment, sizeof(segment))
class BitStreamSeb {
private:
uint8_t* streamStart;
const std::string fileName;
uint64_t position = 0; // byte position
uint8_t position_bit = 0; // inner bit position
uint64_t size;
uint64_t size_bits;
const int width;
const int height;
public:
BitStreamSeb(std::string fileName, const unsigned int width, const unsigned int height) :
width(width), height(height), fileName(std::move(fileName))
{
size = 1024 + width * height * 24; // byte approximation for memory usage
size_bits = size << 3; // times 8
streamStart = static_cast<uint8_t *>(malloc(size));
if(streamStart == nullptr)
throw std::bad_alloc();
}
~BitStreamSeb() {
free(streamStart);
}
void writeBytes(const void* bytes, const size_t len) {
assert((position + static_cast<uint64_t>(len)) < size);
assert(position_bit == 0);
std::memcpy(streamStart + position, bytes, len);
position += static_cast<uint64_t>(len);
}
inline void writeByteAligned(const uint8_t b) {
assert((position + 1) < size);
assert(position_bit == 0);
*(streamStart + position) = b;
++position;
}
void appendU16(const uint16_t data, uint8_t amount) {
if(amount > 8) {
//const int over = amount - 8;
//const uint16_t shifted = data >> over;
//appendBit(static_cast<const uint8_t>(shifted), 8);
amount -= 8;
appendBit(static_cast<const uint8_t>(data >> amount), 8);
}
appendBit(static_cast<const uint8_t>(data) & ones_u8[amount], amount);
}
/**
* Write up to 8 bits to the stream
*/
void appendBit(const uint8_t data, uint8_t amount) {
assert(amount > 0);
assert(amount <= 8);
assert((data & ~ones(amount)) == 0); // check all unimportant bytes are zero
assert(size_bits > ((position << 3) + position_bit + amount));
// there's still a partial byte to write to
if(position_bit != 0) {
const auto pad = 8 - position_bit;
if(amount > pad) {
// we have more bits to write than the partial byte can fit
// we need to write $pad bits and therefore need to crop ($amount - $pad) bits
// since this is the last use in this block we can directly subtract the written amount
amount -= pad;
const uint8_t append = data >> amount;
const auto pos = streamStart + position;
*pos |= append;
++position; // increase the position by one since we filled the byte
position_bit = 0; // set the bit position to zero
if(*pos == 0xFF) // make sure we dont write a 0xFF byte w/o a trailing zero
{
++position;
*(pos + 1) = 0x00;
}
}
else {
// we have exactly as much or less bits than the byte can fit
// so we shift our data to start at the first unused byte (this shift could be zero if amount == pad)
const uint8_t append = data << (pad - amount);
const auto pos = streamStart + position;
*(pos) |= append;
assert((amount + position_bit) <= 8); // it can't be >8 because this would be handled by the other if case
if(amount == pad) { // we exactly filled this byte
// this was usually &= 0x07 or &= 0b00000111, but position bit can only be =8, since >8 will be
// handled by the other if
position_bit = 0;
++position; // update the position since we filled this byte
if(*pos == 0xFF) // make sure we dont write a 0xFF byte w/o a trailing zero
{
++position;
*(pos + 1) = 0x00;
}
}
else {
position_bit = amount + position_bit;
}
// exit, since we've wrote everything
return;
}
}
if(amount == 8) {
// this is the simplest case - write exactly one byte
*(streamStart + position) = data;
++position;
if(data == 0xFF) {
*(streamStart + position) = 0;
++position;
}
}
else { // amount < 8
// if we start a new byte with less than 8 bits to write, we need to shift the first important bit to the
// rightmost position
const uint8_t append = data << (8 - amount);
*(streamStart + position) = append;
position_bit += amount;
}
}
/**
* Fill a partially filled byte with ones.
*/
void fillByte() {
if (position_bit != 0) {
const uint8_t pad = 8 - position_bit;
const uint8_t append = ones(pad);
*(streamStart + position) |= append;
++position;
position_bit = 0;
}
}
/**
* Save the stream to disk
*/
void writeOut() {
fillByte();
auto file = std::fstream(fileName, std::ios::out | std::ios::binary);
file.write((char*)streamStart, position);
file.close();
}
/**
* Return an uint64_t with the rightmost $amount bytes set to one
*/
static constexpr uint64_t ones(const int amount) {
assert(amount < 63 && amount >= 0);
return (static_cast<uint64_t>(1) << amount) - 1;
}
static constexpr std::array<uint8_t, 9> ones_u8 {
0,
1,
0b00000011,
0b00000111,
0b00001111,
0b00011111,
0b00111111,
0b01111111,
0b11111111,
};
};
class BitStreamDeinzer {
private:
uint8_t* streamStart;
const std::string fileName;
uint64_t position = 0; // byte position
uint8_t position_bit = 0; // inner bit position
uint64_t size;
uint64_t buffer = 0;
const int width;
const int height;
public:
BitStreamDeinzer(std::string fileName, const unsigned int width, const unsigned int height) :
width(width), height(height), fileName(std::move(fileName))
{
size = 1024 + width * height * 24; // byte approximation for memory usage
streamStart = static_cast<uint8_t *>(malloc(size));
if(streamStart == nullptr)
throw std::bad_alloc();
}
~BitStreamDeinzer() {
free(streamStart);
}
void writeBytes(const void* bytes, const size_t len) {
assert((position + static_cast<uint64_t>(len)) < size);
assert(position_bit == 0);
std::memcpy(streamStart + position, bytes, len);
position += static_cast<uint64_t>(len);
}
inline void writeByteAligned(const uint8_t b) {
assert((position + 1) < size);
writeByteAlignedUnsafe(b);
}
void appendU16(uint16_t data, const uint8_t amount) {
assert(amount > 0);
assert(amount <= 16);
//assert((data & ~ones(amount)) == 0); // check all unimportant bytes are zero
assert(size > ((position << 3) + position_bit + amount) / 8);
data &= ~ones(amount);
buffer <<= amount;
buffer |= data;
position_bit += amount;
while(position_bit >= 8) {
uint64_t tmp = (buffer >> (position_bit - 8));
uint8_t tbuffer = static_cast<uint8_t>(tmp);
assert((tmp & ones(8)) == tbuffer); // check all unimportant bytes are zero
writeByteAlignedUnsafe(tbuffer);
position_bit -= 8;
}
}
/**
* Write up to 8 bits to the stream
*/
void appendBit(const uint8_t data, const uint8_t amount) {
appendU16(data, amount);
}
/**
* Fill a partially filled byte with ones.
*/
void fillByte() {
if(position_bit == 0)
return;
appendBit(static_cast<const uint8_t>(ones(8 - position_bit)), 8 - position_bit);
assert(position_bit == 0);
}
/**
* Save the stream to disk
*/
void writeOut() {
fillByte();
auto file = std::fstream(fileName, std::ios::out | std::ios::binary);
file.write((char*)streamStart, position);
file.close();
}
private:
inline void writeByteAlignedUnsafe(const uint8_t b) {
assert((position + 1) < size);
*(streamStart + position) = b;
++position;
}
public:
/**
* Return an uint64_t with the rightmost $amount bytes set to one
*/
static constexpr uint64_t ones(const int amount) {
assert(amount < 63 && amount >= 0);
return (static_cast<uint64_t>(1) << amount) - 1;
}
static constexpr std::array<uint8_t, 9> ones_u8 {
0,
1,
0b00000011,
0b00000111,
0b00001111,
0b00011111,
0b00111111,
0b01111111,
0b11111111,
};
};
typedef BitStreamSeb BitStream;
#endif //MEDIENINFO_BITSTREAM_H