|
1 |
| -#include "common.h" |
| 1 | +#include "PR/ultratypes.h" |
| 2 | +#include "sys/bitstream.h" |
2 | 3 |
|
3 |
| -#pragma GLOBAL_ASM("asm/nonmatchings/bitstream/func_8000B8B0.s") |
| 4 | +BitStream *bitstream_init(BitStream *stream, u8 *data, s32 bitLength, s32 capacity) { |
| 5 | + stream->byteLength = bitLength >> 3; |
| 6 | + if (bitLength & 7) { |
| 7 | + // need one extra byte to hold bits |
| 8 | + stream->byteLength += 1; |
| 9 | + } |
4 | 10 |
|
5 |
| -#pragma GLOBAL_ASM("asm/nonmatchings/bitstream/func_8000B8E4.s") |
| 11 | + stream->bitLength = bitLength; |
| 12 | + stream->capacity = capacity; |
| 13 | + stream->data = data; |
| 14 | + stream->bitPos = 0; |
6 | 15 |
|
7 |
| -#pragma GLOBAL_ASM("asm/nonmatchings/bitstream/func_8000B970.s") |
| 16 | + return stream; |
| 17 | +} |
8 | 18 |
|
9 |
| -#pragma GLOBAL_ASM("asm/nonmatchings/bitstream/func_8000BA20.s") |
| 19 | +u32 bitstream_read(BitStream *stream, u8 n) { |
| 20 | + u32 data; |
| 21 | + s32 i; |
| 22 | + s32 byteIdx; |
| 23 | + s32 bitIdx; |
| 24 | + u8 bit; |
10 | 25 |
|
11 |
| -#pragma GLOBAL_ASM("asm/nonmatchings/bitstream/func_8000BA50.s") |
| 26 | + data = 0; |
| 27 | + i = 0; |
| 28 | + |
| 29 | + if (stream->bitPos < stream->bitLength) {} |
| 30 | + |
| 31 | + while (n != 0 && stream->bitPos < stream->bitLength) { |
| 32 | + byteIdx = stream->bitPos >> 3; |
| 33 | + bitIdx = stream->bitPos & 7; |
| 34 | + |
| 35 | + bit = (stream->data[byteIdx] >> bitIdx) & 1; |
| 36 | + data |= bit << i; |
| 37 | + |
| 38 | + i++; |
| 39 | + stream->bitPos++; |
| 40 | + n--; |
| 41 | + } |
| 42 | + |
| 43 | + return data; |
| 44 | +} |
| 45 | + |
| 46 | +void bitstream_write(BitStream *stream, u32 data, u8 n) { |
| 47 | + s32 i; |
| 48 | + s32 byteIdx; |
| 49 | + s32 bitIdx; |
| 50 | + s32 bitPos; |
| 51 | + s32 capacity; |
| 52 | + u8 bit; |
| 53 | + |
| 54 | + if (stream->capacity) {} |
| 55 | + if (stream->bitPos) {} |
| 56 | + |
| 57 | + i = 0; |
| 58 | + |
| 59 | + while (n != 0 && stream->bitPos < stream->capacity) { |
| 60 | + byteIdx = stream->bitPos >> 3; |
| 61 | + bitIdx = stream->bitPos & 7; |
| 62 | + |
| 63 | + bit = (data >> i) & 1; |
| 64 | + stream->data[byteIdx] |= bit << bitIdx; |
| 65 | + |
| 66 | + i++; |
| 67 | + |
| 68 | + stream->bitPos++; |
| 69 | + if (stream->bitLength < stream->bitPos) { |
| 70 | + stream->bitLength += 1; |
| 71 | + } |
| 72 | + |
| 73 | + n--; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void bitstream_append(BitStream *stream, u32 data, u8 n) { |
| 78 | + s32 len; |
| 79 | + |
| 80 | + len = stream->bitLength; |
| 81 | + |
| 82 | + stream->bitPos = len; |
| 83 | + |
| 84 | + bitstream_write(stream, data, n); |
| 85 | +} |
| 86 | + |
| 87 | +void bitstream_set_pos(BitStream *stream, s32 bitPos) { |
| 88 | + stream->bitPos = bitPos; |
| 89 | +} |
0 commit comments