forked from hegdi/libldacdec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_reader.c
110 lines (93 loc) · 2.34 KB
/
bit_reader.c
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
#include "bit_reader.h"
#include "utility.h"
static int PeekIntFallback(BitReaderCxt* br, int bitCount);
void InitBitReaderCxt(BitReaderCxt* br, const void * buffer)
{
br->Buffer = buffer;
br->Position = 0;
}
uint32_t ReadInt(BitReaderCxt* br, const int bits)
{
const uint32_t value = PeekInt(br, bits);
br->Position += bits;
return value;
}
int32_t ReadSignedInt(BitReaderCxt* br, const int bits)
{
const int value = PeekInt(br, bits);
br->Position += bits;
return SignExtend32(value, bits);
}
int ReadOffsetBinary(BitReaderCxt* br, const int bits)
{
const int offset = 1 << (bits - 1);
const int value = PeekInt(br, bits) - offset;
br->Position += bits;
return value;
}
uint32_t PeekInt(BitReaderCxt* br, const int bits)
{
const unsigned int byteIndex = br->Position / 8;
const unsigned int bitIndex = br->Position % 8;
const uint8_t* buffer = br->Buffer;
if (bits <= 9)
{
uint32_t value = buffer[byteIndex] << 8 | buffer[byteIndex + 1];
value &= 0xFFFF >> bitIndex;
value >>= 16 - bits - bitIndex;
return value;
}
if (bits <= 17)
{
uint32_t value = buffer[byteIndex] << 16 | buffer[byteIndex + 1] << 8 | buffer[byteIndex + 2];
value &= 0xFFFFFF >> bitIndex;
value >>= 24 - bits - bitIndex;
return value;
}
if (bits <= 25)
{
uint32_t value = buffer[byteIndex] << 24
| buffer[byteIndex + 1] << 16
| buffer[byteIndex + 2] << 8
| buffer[byteIndex + 3];
value &= (int)(0xFFFFFFFF >> bitIndex);
value >>= 32 - bits - bitIndex;
return value;
}
return PeekIntFallback(br, bits);
}
void AlignPosition(BitReaderCxt* br, const unsigned int multiple)
{
const int position = br->Position;
if (position % multiple == 0)
{
return;
}
br->Position = position + multiple - position % multiple;
}
static int PeekIntFallback(BitReaderCxt* br, int bitCount)
{
int value = 0;
int byteIndex = br->Position / 8;
int bitIndex = br->Position % 8;
const unsigned char* buffer = br->Buffer;
while (bitCount > 0)
{
if (bitIndex >= 8)
{
bitIndex = 0;
byteIndex++;
}
int bitsToRead = bitCount;
if (bitsToRead > 8 - bitIndex)
{
bitsToRead = 8 - bitIndex;
}
const int mask = 0xFF >> bitIndex;
const int currentByte = (mask & buffer[byteIndex]) >> (8 - bitIndex - bitsToRead);
value = (value << bitsToRead) | currentByte;
bitIndex += bitsToRead;
bitCount -= bitsToRead;
}
return value;
}