-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBitStreamReader.h
executable file
·103 lines (84 loc) · 1.28 KB
/
BitStreamReader.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
#pragma once
class CBitStreamReader
{
public:
CBitStreamReader(unsigned char* dataToRead)
{
position = 0;
m_pabinaryData = dataToRead;
}
virtual ~CBitStreamReader(void)
{
}
void SkipBits(int n)
{
for (int i = 0; i < n; i++)
{
SkipBit();
}
}
int GetBits(int n) // Same as U(int n)
{
int result = 0;
for (int i = 0; i < n; i++)
{
result = result * 2 +GetBit();
}
return result;
}
int U(int n)
{
int result = 0;
for (int i = 0; i < n; i++)
{
result = result * 2 +GetBit();
}
return result;
}
int Uev()
{
return Ev(false);
}
int Sev()
{
return Ev(true);
}
private:
int GetBit()
{
int mask = 1 << (7 - (position & 7));
int index = position >> 3;
position++;
return ((m_pabinaryData[index] & mask) == 0) ? 0 : 1;
}
void SkipBit()
{
position++;
}
int Ev(bool isItSigned)
{
int bitCount = 0;
//std::string expGolomb;
while (GetBit() == 0)
{
//expGolomb += '0';
bitCount++;
}
//expGolomb += "/1";
int result = 1;
for (int i = 0; i < bitCount; i++)
{
int b = GetBit();
//expGolomb += b;
result = result * 2 + b;
}
result--;
if (isItSigned)
{
result = (result + 1) / 2 * (result % 2 == 0 ? -1 : 1);
}
return result;
}
unsigned char* m_pabinaryData;
int position;
};