-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitStream.h
144 lines (118 loc) · 2.54 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
#pragma once
namespace GApi
{
namespace Media
{
class BitStream
{
public:
class IO
{
public:
virtual bool Read(void* buffer, size_t size) = 0;
virtual void Skip(size_t size) = 0;
};
private:
IO &io_;
uint8_t _bits; // octet
size_t _offsetBits;
size_t _readBits;
public:
BitStream(IO& io) :
io_(io),
_bits(0),
_offsetBits(0),
_readBits(0)
{}
//Number of bits read
size_t GetReadBits()
{
return _readBits;
}
bool Read(void* buffer, size_t size)
{
assert(_offsetBits == 0);
return io_.Read(buffer, size);
}
template<class T = uint8_t>
T ReadBit(size_t n = 1)
{
T value = 0;
if (_offsetBits < n)
{
uint8_t readByte = (uint8_t)ceil((double)(n-_offsetBits) / 8.0);
uint8_t readBits = readByte * 8;
T bytes = 0;
io_.Read(&bytes, readByte);
value = (T)(_bits << (n - _offsetBits));
value |= (T)(bytes >> (_offsetBits+(readBits-n)));
bytes <<= (n - _offsetBits);
bytes &= 0xff;
_bits = (uint8_t)(bytes >> (n - _offsetBits));
_offsetBits = readBits - (n - _offsetBits);
}
else
{
value = (T)(_bits >> (_offsetBits - n));
_bits <<= (8 - (_offsetBits - n));
_bits >>= (8 - (_offsetBits - n));
_offsetBits -= n;
}
if (n > 8)
{
size_t count = n / 8;
size_t remain = n % 8;
T temp = value;
for (size_t i = 0; i < count; ++i)
{
value = (value << 8) | (0xff & (temp >> (i * 8)));
}
if (remain > 0)
{
value |= (value << 8) | (0xff & (temp >> remain));
}
}
_readBits += n;
return value;
}
void SkipBit(size_t n = 1)
{
assert(false && "not implementation");
}
void AlignBits()
{
if (_offsetBits > 0)
{
uint8_t temp;
io_.Read(&temp, 1);
}
_offsetBits = 0;
_bits = 0;
_readBits = 0;
}
template<typename T, bool eglomb>
T ReadBit() { assert(0); return 0; }
template<>
uint32_t ReadBit<uint32_t, true>()
{
int32_t leadingZeroBits = -1;
for (uint8_t b = 0; !b; leadingZeroBits++)
{
b = ReadBit();
}
uint32_t codeNum = 0;
if (leadingZeroBits > 0)
{
codeNum = (uint32_t)std::pow(2, leadingZeroBits) - 1 + ReadBit<int32_t>(leadingZeroBits);
}
return codeNum;
};
template<>
int32_t ReadBit<int32_t, true>()
{
uint32_t codeNum = ReadBit<uint32_t, true>();
return (int32_t)std::pow(-1.0, codeNum + 1) * (int32_t)std::ceil(codeNum / 2.0);
};
};
}
}