-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathBitstreamReader.h
More file actions
289 lines (262 loc) · 7.87 KB
/
BitstreamReader.h
File metadata and controls
289 lines (262 loc) · 7.87 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
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#ifndef BITSTREAMREADER_H
#define BITSTREAMREADER_H
/// @file BitstreamReader.h
/// @author Matthias Richter
/// @since 2019-06-05
/// @brief Utility class to provide bitstream access to an underlying resource
#include <type_traits>
#include <bitset>
namespace o2
{
namespace algorithm
{
/// @class BitStreamReader
/// @brief Utility class to provide bitstream access to an underlying resource
///
/// Allows to access bits of variable length, supports integral types and also
/// bitsets as target type. At the moment, the access is in direction MSB -> LSB.
///
/// BitstreamReader<uint8_t> reader(start, end);
/// while (reader.good() && not reader.eof()) {
/// // get an 8 bit value from the stream, moves the position
/// uint8_t ivalue;
/// reader.get(ivalue);
///
/// // get a 13 bit bitset without moving the position
/// std::bitset<13> value;
/// reader.peek(value, value.size());
/// // e.g. use 7 bits of the data
/// value >>= value.size() - 7;
/// // move position by the specific number of bits
/// reader.seek(7);
/// }
template <typename BufferType>
class BitstreamReader
{
public:
using self_type = BitstreamReader<BufferType>;
// for the moment we simply use pointers, but with some traits this can be extended to
// containers
using value_type = BufferType;
using iterator = const value_type*;
static constexpr size_t value_size = sizeof(value_type) * 8;
BitstreamReader() = delete;
BitstreamReader(iterator start, iterator end)
: mStart(start), mEnd(end), mCurrent(mStart), mBitPosition(value_size)
{
}
~BitstreamReader() = default;
/// Check reader's state
/// @return true if not in error state
bool good() const
{
return mBitPosition > 0;
}
/// Indicates end of data
/// @return true if end of resource is reached
bool eof() const
{
return mCurrent == mEnd && mBitPosition > 0;
}
/// Reset the reader, start over at beginning
void reset()
{
mCurrent = mStart;
mBitPosition = value_size;
}
/// Get the next N bits without moving the read position
/// if bitlength is smaller than the size of data type, result is aligned to LSB
/// TODO: this also works nicely for bitsets, but then the bitlength has to be specified
/// as template parameter, want to do a specific overload, but needs more work to catch
/// all cases.
/// @param v target variable passed by reference
/// @return number of poked bits
template <typename T, size_t N = sizeof(T) * 8>
size_t peek(T& v)
{
static_assert(N <= sizeof(T) * 8);
return peek<T, false>(v, N);
}
/// Get the next n bits without moving the read position
/// if bitlength is smaller than the size of data type, result is aligned to LSB
/// @param v target variable passed by reference
/// @param bitlength number of bits to read
/// @return number of poked bits
template <typename T>
size_t peek(T& v, size_t bitlength)
{
return peek<T, true>(v, bitlength);
}
/// Move read position
/// @param bitlength move count in number of bits
void seek(size_t bitlength)
{
while (good() && bitlength > 0 && mCurrent != mEnd) {
if (bitlength >= mBitPosition) {
bitlength -= mBitPosition;
mBitPosition = 0;
} else {
mBitPosition -= bitlength;
bitlength = 0;
}
if (mBitPosition == 0) {
mCurrent++;
mBitPosition = value_size;
}
}
if (bitlength > 0) {
mBitPosition = 0;
}
}
/// Get the next n bits and move the read position
template <typename T, size_t N = sizeof(T) * 8>
T get()
{
T result;
peek<T, N>(result);
seek(N);
return result;
}
/// Get the next n and move the read position
template <typename T>
T get(size_t bitlength = sizeof(T) * 8)
{
T result;
peek<T>(result, bitlength);
seek(bitlength);
return result;
}
/// @class Bits
/// @brief Helper class to get value of specified type which holds the number used bits
///
/// The class holds both the extracted value access via peek method and the number of used
/// bits. The reader will be incremented when the object is destroyed.
/// The number of bits can be adjusted by using markUsed method
template <typename FieldType, size_t N = sizeof(FieldType) * 8, typename ParentType = self_type>
class Bits
{
public:
using field_type = FieldType;
static_assert(N <= sizeof(FieldType) * 8);
Bits()
: mParent(nullptr), mData(0), mLength(0)
{
}
Bits(ParentType* parent, FieldType&& data)
: mParent(parent), mData(std::move(data)), mLength(N)
{
}
Bits(Bits&& other)
: mParent(other.mParent), mData(std::move(other.mData)), mLength(other.mLength)
{
other.mParent = nullptr;
other.mLength = 0;
}
~Bits()
{
if (mParent) {
mParent->seek(mLength);
}
}
auto& operator=(Bits<FieldType, N, ParentType>&& other)
{
mParent = other.mParent;
mData = std::move(other.mData);
mLength = other.mLength;
other.mParent = nullptr;
other.mLength = 0;
return *this;
}
FieldType& operator*()
{
return mData;
}
void markUsed(size_t length)
{
mLength = length;
}
private:
ParentType* mParent;
FieldType mData;
size_t mLength;
};
/// Read an integral value from the stream
template <typename T, std::enable_if_t<std::is_integral<T>::value, int> = 0>
self_type& operator>>(T& target)
{
target = get<T>();
return *this;
}
/// Read a bitstream value from the stream
template <size_t N>
self_type& operator>>(std::bitset<N>& target)
{
target = get<std::bitset<N>, N>();
return *this;
}
/// Read a Bits object from the stream
template <typename T>
self_type& operator>>(Bits<T>& target)
{
T bitfield;
peek<T>(bitfield);
target = std::move(Bits<T>(this, std::move(bitfield)));
return *this;
}
private:
/// The internal peek method
template <typename T, bool RuntimeCheck>
size_t peek(T& result, size_t bitlength)
{
if constexpr (RuntimeCheck) {
// the runtime check is disabled if bitlength is derived at compile time
if (bitlength > sizeof(T) * 8) {
throw std::length_error(std::string("requested bit length ") + std::to_string(bitlength) + " does not fit size of result data type " + std::to_string(sizeof(T) * 8));
}
}
result = 0;
size_t bitsToWrite = bitlength;
auto current = mCurrent;
auto bitsAvailable = mBitPosition;
while (bitsToWrite > 0 && current != mEnd) {
// extract available bits
value_type mask = ~value_type(0) >> (value_size - bitsAvailable);
if (bitsToWrite >= bitsAvailable) {
T value = (*current & mask) << (bitsToWrite - bitsAvailable);
result |= value;
bitsToWrite -= bitsAvailable;
bitsAvailable = 0;
} else {
value_type value = (*current & mask) >> (bitsAvailable - bitsToWrite);
result |= value;
bitsAvailable -= bitsToWrite;
bitsToWrite = 0;
}
if (bitsAvailable == 0) {
current++;
bitsAvailable = value_size;
}
}
return bitlength - bitsToWrite;
}
/// start of resource
iterator mStart;
/// end of resource
iterator mEnd;
/// current position in resource
iterator mCurrent;
/// bit position in current element
size_t mBitPosition;
};
} // namespace algorithm
} // namespace o2
#endif