-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathBitStream.ts
More file actions
321 lines (291 loc) · 8.79 KB
/
Copy pathBitStream.ts
File metadata and controls
321 lines (291 loc) · 8.79 KB
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
321
/*
* Copyright (c) 2026. Paul Higgs
* License: BSD-3-Clause (see LICENSE file)
*
*
* reads bits and bytes from a buffer that may not contain aligned values
* TODO: add writing support
*/
import { DataStream, Endianness } from '#/DataStream';
import type { MultiBufferStream } from './buffer';
class State {
rbyte: number;
rbit: number;
wbyte: number;
wbit: number;
end: number;
read_error: boolean;
write_error: boolean;
constructor() {
this.rbyte = this.rbit = this.wbyte = this.wbit = this.end = 0;
this.read_error = this.write_error = false;
}
readBitOffset(): number {
return 8 * this.rbyte + this.rbit;
}
readByteOffset() {
return this.rbyte;
}
writeBitOffset(): number {
return 8 * this.wbyte + this.wbit;
}
writeByteOffset() {
return this.wbyte;
}
aligned() {
return this.read_error && this.rbit !== 0;
}
}
export class BitStream {
private buffer: Array<number>;
private stream: MultiBufferStream | DataStream;
private state: State;
private endianness: Endianness;
constructor(stream: MultiBufferStream | DataStream, endianness?: Endianness) {
this.state = new State();
this.stream = stream;
this.endianness = endianness ? endianness : Endianness.BIG_ENDIAN;
}
appendUint8(count = 1): void {
for (let i = 0; i < count; i++) this.buffer.push(this.stream.readUint8());
this.state.end = this.state.wbyte = this.buffer.length;
}
extend(bits: number): void {
let count = bits;
while (count > 0) {
this.buffer.push(0);
count -= 8;
}
}
readBit(): number {
//! Read the next bit and advance the read pointer.
if (this.state.read_error || this.endOfRead()) {
this.state.read_error = true;
return 0;
}
const bit: number =
(this.buffer[this.state.rbyte] >>
(this.endianness === Endianness.BIG_ENDIAN ? 7 - this.state.rbit : this.state.rbit)) &
0x01;
if (++this.state.rbit > 7) {
this.state.rbyte++;
this.state.rbit = 0;
}
return bit;
}
peekBit(): number {
//! Read the next bit and but dont advance the read pointer.
if (this.state.read_error || this.endOfRead()) {
this.state.read_error = true;
return 0;
}
const bit: number =
(this.buffer[this.state.rbyte] >>
(this.endianness === Endianness.BIG_ENDIAN ? 7 - this.state.rbit : this.state.rbit)) &
0x01;
return bit;
}
endOfRead(): boolean {
return this.state.rbyte === this.state.wbyte && this.state.rbit === this.state.wbit;
}
getBool(): boolean {
return this.readBit() !== 0;
}
private rdb = function (bytes: number): number {
let i: number, res: number;
// eslint-disable-next-line no-loss-of-precision
const ff = 0xffffffffffffffff;
if (this.state.read_error) return ff;
if (this.state.rbit === 0) {
// Read buffer is byte aligned. Most common case.
if (this.state.rbyte + bytes > this.state.wbyte) {
// Not enough bytes to read.
this.state.read_error = true;
return ff;
} else {
for (res = 0, i = 0; i < bytes; i++) res = (res << 8) + this.buffer[this.state.rbyte + i];
this.state.rbyte += bytes;
return res;
}
} else {
// Read buffer is not byte aligned, use an intermediate aligned buffer.
if (this.currentReadBitOffset() + 8 * bytes > this.currentWriteBitOffset()) {
// Not enough bytes to read.
this.state.read_error = true;
return ff;
} else {
for (res = 0, i = 0; i < bytes; i++) {
if (this.endianness === Endianness.BIG_ENDIAN)
res =
(res << 8) +
((this.buffer[this.state.rbyte] << this.state.rbit) |
(this.buffer[this.state.rbyte + 1] >> (8 - this.state.rbit)));
else
res =
(res << 8) +
((this.buffer[this.state.rbyte] >> this.state.rbit) |
(this.buffer[this.state.rbyte + 1] << (8 - this.state.rbit)));
this.state.rbyte++;
}
return res;
}
}
return ff; // we should never get here!!
};
readUint8() {
return this.rdb(1);
}
readUint16(endianness?: Endianness) {
return (endianness ?? this.endianness) === Endianness.BIG_ENDIAN
? this.GetUInt16BE(this.rdb(2))
: this.GetUInt16LE(this.rdb(2));
}
private GetUInt16BE = function (val: number) {
return this.OSisLittleEndian() ? this.ByteSwap16(val) : val;
};
private GetUInt16LE = function (val: number) {
return this.OSisLittleEndian() ? val : this.ByteSwap16(val);
};
private ByteSwap16 = function (x: number): number {
return (x << 8) | (x >> 8);
};
readUint24(endianness?: Endianness) {
return (endianness ?? this.endianness) === Endianness.BIG_ENDIAN
? this.GetUInt24BE(this.rdb(3))
: this.GetUInt24LE(this.rdb(3));
}
private ByteSwap24 = function (x: number) {
return ((x & 0xff0000) >> 16) | (x & 0xff00) | (x & (0xff << 16));
};
private GetUInt24BE = function (val: number) {
return this.OSisLittleEndian() ? this.ByteSwap24(val) : val;
};
private GetUInt24LE = function (val: number) {
return this.OSisLittleEndian() ? val : this.ByteSwap24(val);
};
readUint32(endianness?: Endianness) {
return (endianness ?? this.endianness) === Endianness.BIG_ENDIAN
? this.GetUInt32BE(this.rdb(4))
: this.GetUInt32LE(this.rdb(4));
}
private ByteSwap32 = function (x: number) {
return (x << 24) | ((x << 8) & 0x00ff0000) | ((x >> 8) & 0x0000ff00) | (x >> 24);
};
private GetUInt32BE = function (val: number) {
return this.OSisLittleEndian() ? this.ByteSwap32(val) : val;
};
private GetUInt32LE = function (val: number) {
return this.OSisLittleEndian() ? val : this.ByteSwap32(val);
};
readBits(bits: number): number {
// No read if read error is already set or not enough bits to read.
if (
this.state.read_error ||
this.currentReadBitOffset() + bits > this.currentWriteBitOffset()
) {
this.state.read_error = true;
return 0;
}
let val = 0;
if (this.endianness === Endianness.BIG_ENDIAN) {
// Read leading bits up to byte boundary
while (bits > 0 && this.state.rbit !== 0) {
val = (val << 1) | this.readBit();
--bits;
}
// Read complete bytes
while (bits > 7) {
val = (val << 8) | this.buffer[this.state.rbyte++];
bits -= 8;
}
// Read trailing bits
while (bits > 0) {
val = (val << 1) | this.readBit();
--bits;
}
} else {
// Little endian decoding
let shift = 0;
// Read leading bits up to byte boundary
while (bits > 0 && this.state.rbit !== 0) {
val |= this.readBit() << shift;
--bits;
shift++;
}
// Read complete bytes
while (bits > 7) {
val |= this.buffer[this.state.rbyte++] << shift;
bits -= 8;
shift += 8;
}
// Read trailing bits
while (bits > 0) {
val |= this.readBit() << shift;
--bits;
shift++;
}
}
return val;
}
skipBits(bits: number): boolean {
if (this.state.read_error) {
// Can't skip bits and bytes if read error is already set.
return false;
}
const rpos = 8 * this.state.rbyte + this.state.rbit + bits;
const wpos = 8 * this.state.wbyte + this.state.wbit;
if (rpos > wpos) {
this.state.rbyte = this.state.wbyte;
this.state.rbit = this.state.wbit;
this.state.read_error = true;
return false;
}
this.state.rbyte = rpos >> 3;
this.state.rbit = rpos & 7;
return true;
}
skipBit(): boolean {
return this.skipBits(1);
}
readUE(): number {
// read in an Unsigned Exp-Golomb code;
if (this.readBit() === 1) return 0;
let zero_count = 1;
while (this.peekBit() === 0) {
this.readBit();
zero_count++;
}
return this.readBits(zero_count + 1) - 1;
}
byte_alignment(): void {
while (!this.state.aligned) this.skipBit();
}
private OSisLittleEndian = function (): boolean {
return this.stream.endianness === Endianness.LITTLE_ENDIAN;
};
currentReadByteOffset(): number {
return this.state.readByteOffset();
}
currentReadBitOffset(): number {
return this.state.readBitOffset();
}
currentWriteByteOffset(): number {
return this.state.writeByteOffset();
}
currentWriteBitOffset(): number {
return this.state.writeBitOffset();
}
bitsRemaining(): number {
return this.currentWriteBitOffset() - this.currentReadBitOffset();
}
writeBitsRemaining(): number {
return 8 * this.buffer.length - this.currentWriteBitOffset();
}
/*
TODO - for near future implementation to support writing AVS3 related boxes that are not byte aligned
writeBit(bit: number): void {
if (this.writeBitsRemaining() < 1)
this.extend(1);
}
*/
}