-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathH26xBinaryReader.cpp
460 lines (419 loc) · 14.9 KB
/
H26xBinaryReader.cpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "H26xBinaryReader.h"
#include <cassert>
#include <cstdint>
#include <exception>
#include <stdexcept>
#include <string>
#include "H26xUltis.h"
namespace Mmp
{
namespace Codec
{
// Quick Note:
//
// See also : ISO 14496/10(2020) - 7.2 Specification of syntax functions, categories, and descriptors
//
// - ae(v): context-adaptive arithmetic entropy-coded syntax element. The parsing process for this descriptor is specified
// in subclause 9.3.
// - b(8): byte having any pattern of bit string (8 bits). The parsing process for this descriptor is specified by the return
// value of the function read_bits( 8 ).
// - ce(v): context-adaptive variable-length entropy-coded syntax element with the left bit first. The parsing process for
// this descriptor is specified in subclause 9.2.
// - f(n): fixed-pattern bit string using n bits written (from left to right) with the left bit first. The parsing process for this
// descriptor is specified by the return value of the function read_bits( n ).
// - i(n): signed integer using n bits. When n is "v" in the syntax table, the number of bits varies in a manner dependent
// on the value of other syntax elements. The parsing process for this descriptor is specified by the return value of the
// function read_bits( n ) interpreted as a two's complement integer representation with most significant bit written first.
// - me(v): mapped Exp-Golomb-coded syntax element with the left bit first. The parsing process for this descriptor is
// specified in subclause 9.1.
// - se(v): signed integer Exp-Golomb-coded syntax element with the left bit first. The parsing process for this descriptor
// is specified in subclause 9.1.
// - te(v): truncated Exp-Golomb-coded syntax element with left bit first. The parsing process for this descriptor is
// specified in subclause 9.1.
// - u(n): unsigned integer using n bits. When n is "v" in the syntax table, the number of bits varies in a manner
// dependent on the value of other syntax elements. The parsing process for this descriptor is specified by the return
// value of the function read_bits( n ) interpreted as a binary representation of an unsigned integer with most significant
// bit written first.
// - ue(v): unsigned integer Exp-Golomb-coded syntax element with the left bit first. The parsing process for this
// descriptor is specified in subclause 9.1.
//
static uint8_t kLeftAndLookUp[8] = {0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01};
static uint8_t kRightAndLookUp[8] = {0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF};
H26xBinaryReader::H26xBinaryReader(AbstractH26xByteReader::ptr reader)
{
_rbspEndByte = 0;
_curBitPos = 8;
_curValue = 0;
_reader = reader;
_inNalUnit = false;
_zeroCount = 0;
}
H26xBinaryReader::~H26xBinaryReader()
{
}
void H26xBinaryReader::UE(uint32_t& value)
{
// See also : ISO 14496/10(2020) - 9.1 Parsing process for Exp-Golomb codes
int32_t leadingZeroBits = -1;
uint64_t tmp = 0;
for (uint8_t b = 0; b == 0; leadingZeroBits++)
{
U(1, b);
assert(b == 0 || b == 1);
}
U(leadingZeroBits, tmp);
value = (uint32_t)((1 << leadingZeroBits) - 1 + tmp);
}
void H26xBinaryReader::SE(int32_t& value)
{
// See also : ISO 14496/10(2020) - Table 9-3 – Assignment of syntax element to codeNum for signed Exp-Golomb coded syntax elements se(v)}
uint32_t codeNum = 0;
UE(codeNum);
if (codeNum % 2 == 0)
{
value = -static_cast<int32_t>(codeNum >> 1);
}
else
{
value = static_cast<int32_t>((codeNum >> 1) + 1);
}
}
#define MMP_U_OPERATION(bits, value) value = 0;\
bool firstFlag = false;\
do\
{\
ReadOneByteAuto();\
size_t readBits = bits <= (size_t)(8 - _curBitPos)? bits : (size_t)(8 - (uint8_t)_curBitPos);\
bits = (size_t)(bits - readBits);\
value <<= readBits;\
if (readBits < 8 && !firstFlag)\
{\
value |= (_curValue & kLeftAndLookUp[_curBitPos]) >> (8 - _curBitPos - readBits);\
}\
else if (readBits < 8)\
{\
value |= (_curValue & kRightAndLookUp[readBits - 1]) >> (8 - _curBitPos - readBits);\
}\
else\
{\
value |= _curValue;\
}\
_curBitPos = _curBitPos + (uint8_t)readBits;\
firstFlag = true;\
} while (bits != 0);
#define MMP_U_PRED_OPERATION(bits, value) uint8_t curBitPos = _curBitPos;\
size_t curPosByte = curBitPos == 8 ? _reader->Tell() : _reader->Tell() - 1;\
MMP_U_OPERATION(bits, value);\
_reader->Seek(curPosByte);\
if (curBitPos != 8)\
{\
ReadOneByteAuto(true);\
}\
_curBitPos = curBitPos;
#define MMP_I_OPERATION(bits, value) MMP_U_OPERATION(bits, value)
#define MMP_I_PRED_OPERATION(bits, value) MMP_U_PRED_OPERATION(bits, value)
void H26xBinaryReader::U(size_t bits, uint64_t& value)
{
if (bits > 64)
{
throw std::out_of_range(std::string());
}
MMP_U_OPERATION(bits, value);
}
void H26xBinaryReader::U(size_t bits, uint32_t& value, bool probe)
{
if (bits > 32)
{
throw std::out_of_range(std::string());
}
if (!probe)
{
MMP_U_OPERATION(bits, value);
}
else
{
MMP_U_PRED_OPERATION(bits, value);
}
}
void H26xBinaryReader::U(size_t bits, uint16_t& value)
{
if (bits > 16)
{
throw std::out_of_range(std::string());
}
MMP_U_OPERATION(bits, value);
}
void H26xBinaryReader::U(size_t bits, uint8_t& value, bool probe)
{
if (bits > 8)
{
throw std::out_of_range(std::string());
}
if (!probe)
{
MMP_U_OPERATION(bits, value);
}
else
{
MMP_U_PRED_OPERATION(bits, value);
}
}
void H26xBinaryReader::I(size_t bits, int64_t& value)
{
if (bits > 64)
{
throw std::out_of_range(std::string());
}
MMP_I_OPERATION(bits, value);
}
void H26xBinaryReader::I(size_t bits, int32_t& value)
{
if (bits > 32)
{
throw std::out_of_range(std::string());
}
MMP_I_OPERATION(bits, value);
}
void H26xBinaryReader::I(size_t bits, int16_t& value)
{
if (bits > 16)
{
throw std::out_of_range(std::string());
}
MMP_I_OPERATION(bits, value);
}
void H26xBinaryReader::I(size_t bits, int8_t& value)
{
if (bits > 8)
{
throw std::out_of_range(std::string());
}
MMP_I_OPERATION(bits, value);
}
#undef MMP_I_PRED_OPERATION
#undef MMP_I_OPERATION
#undef MMP_U_PRED_OPERATION
#undef MMP_U_OPERATION
void H26xBinaryReader::B8(uint8_t& value)
{
U(8, value);
}
void H26xBinaryReader::Skip(size_t bits)
{
if (bits + _curBitPos < 8) // 不需要跳转至下一个字节
{
_curBitPos = uint8_t(bits + _curBitPos);
return;
}
else // 需要跳转的 bits 先用消费当前 byte 剩余的 bits
{
bits = bits - (8 - _curBitPos);
_curBitPos = 8;
}
size_t skipByte = bits / 8; // 计算需要跳转的字节数
if (skipByte)
{
_reader->Seek(_reader->Tell() + skipByte);
}
ReadOneByteAuto(true);
_curBitPos = bits % 8;
}
void H26xBinaryReader::MoveNextByte()
{
_curBitPos = 8;
ReadOneByteAuto();
}
bool H26xBinaryReader::Eof()
{
return _reader->Eof() && _curBitPos == 8;
}
void H26xBinaryReader::BeginNalUnit()
{
_inNalUnit = true;
}
void H26xBinaryReader::EndNalUnit()
{
_inNalUnit = false;
}
size_t H26xBinaryReader::CurBits()
{
return _reader->Tell() * 8 + (_curBitPos % 8);
}
bool H26xBinaryReader::more_rbsp_data()
{
// Hint :
// more_rbsp_data( ) is specified as follows:
// - If there is no more data in the RBSP, the return value of more_rbsp_data( ) is equal to FALSE.
// - Otherwise, the RBSP data is searched for the last (least significant, right-most) bit equal to 1 that is present in the
// RBSP. Given the position of this bit, which is the first bit (rbsp_stop_one_bit) of the rbsp_trailing_bits( ) syntax
// structure, the following applies:
// - If there is more data in an RBSP before the rbsp_trailing_bits( ) syntax structure, the return value of
// more_rbsp_data( ) is equal to TRUE.
// - Otherwise, the return value of more_rbsp_data( ) is equal to FALSE.
//
// Hint : 寻找下一个 RBSP 起点 NAL START CODE (0x000003),
// 根据 ISO 描述, 在 rbsp_trailing_bits() 后可能存在几个字节的 zero, 此部分不算做 RBSP 范畴内
//
if (_rbspEndByte > _reader->Tell()) // cache hint
{
// * 可能最后一个字节完全是 rbsp_trailing_bits (1 rbsp_stop_one_bit + 7 rbsp_alignment_zero_bit)
if (_curBitPos == 8 && (_rbspEndByte == _reader->Tell() + 1))
{
ReadOneByteAuto();
if (_curValue == 0x80)
{
return false;
}
}
else
{
return true;
}
}
else if (_reader->Tell() == _rbspEndByte) // reach end of rbsp
{
return _curBitPos == 8 ? false : true;
}
else // update _rbspEndByte and try once again
{
bool inNalUnit = _inNalUnit;
_inNalUnit = false;
uint8_t curBitPos = _curBitPos;
size_t curPosByte = curBitPos == 8 ? _reader->Tell() : _reader->Tell() - 1;
// 1 - 寻找下一个 RBSP 起点 NAL START CODE (0x000003)
{
try
{
uint32_t next_24_bits = 0;
U(24, next_24_bits, true);
while (next_24_bits != 0x000001)
{
if ((next_24_bits & 0xFFFF) == 0)
{
Skip(8);
}
else if ((next_24_bits & 0xFF) == 0)
{
Skip(16);
}
else
{
Skip(32);
}
_rbspEndByte = _reader->Tell();
U(24, next_24_bits, true);
}
}
catch (...)
{
// Hint : 剩余长度不足 3 时可以进入此异常
_rbspEndByte = _reader->Tell();
}
}
// 2 - 移除 rbsp_trailing_bits() 后的几个 zero byte (,如果存在的话)
{
try
{
uint8_t zeroByte = 0;
do
{
U(8, zeroByte, true);
if (zeroByte == 0)
{
_reader->Seek(_reader->Tell() - 1);
_rbspEndByte = _reader->Tell();
}
} while (zeroByte == 0);
}
catch (...)
{
// Hint : 无可读数据进入此异常
// nothing to do
}
}
_reader->Seek(curPosByte);
if (curBitPos != 8)
{
ReadOneByteAuto(true);
}
_curBitPos = curBitPos;
_inNalUnit = inNalUnit;
return more_rbsp_data();
}
return true;
}
void H26xBinaryReader::rbsp_trailing_bits()
{
// See also : ISO 14496/10(2020) - 7.3.2.11 RBSP trailing bits syntax
uint8_t rbsp_stop_one_bit = 0;
uint8_t rbsp_alignment_zero_bit;
U(1, rbsp_stop_one_bit);
assert(rbsp_stop_one_bit == 1);
while (!(_curBitPos == 0 || _curBitPos == 8) && more_data_in_byte_stream())
{
U(1, rbsp_alignment_zero_bit);
// assert(rbsp_alignment_zero_bit == 0);
}
}
bool H26xBinaryReader::more_data_in_byte_stream()
{
// Hint :
// more_data_in_byte_stream( ), which is used only in the byte stream NAL unit syntax structure specified in Annex B, is
// specified as follows:
// - If more data follow in the byte stream, the return value of more_data_in_byte_stream( ) is equal to TRUE.
// - Otherwise, the return value of more_data_in_byte_stream( ) is equal to FALSE.
return !(_reader->Eof() && _curBitPos == 8);
}
void H26xBinaryReader::byte_alignment()
{
assert(false);
// TODO
}
bool H26xBinaryReader::End()
{
assert(false);
return true;
}
bool H26xBinaryReader::ReadBytes(size_t byte, uint8_t* value)
{
size_t readByte = _reader->Read(value, byte);
if (readByte != byte)
{
throw std::out_of_range("");
}
return readByte == byte;
}
void H26xBinaryReader::ReadOneByteAuto(bool force)
{
if (_curBitPos == 8 || force)
{
ReadBytes(1, &_curValue);
// Hint : 0x0000030x -> 0x00000x
// The RBSP data is searched for byte-aligned bits of the following binary patterns:
// '00000000 00000000 000000xx' (where xx represents any 2 bit pattern: 00, 01, 10, or 11),
// and a byte equal to 0x03 is inserted to replace these bit patterns with the patterns:
// '00000000 00000000 00000011 000000xx',
// and finally, when the last byte of the RBSP data is equal to 0x00 (which can only occur when the RBSP ends in a
// cabac_zero_word), a final byte equal to 0x03 is appended to the end of the data. The last zero byte of a byte-aligned
// three-byte sequence 0x000000 in the RBSP (which is replaced by the four-byte sequence 0x00000300) is taken into
// account when searching the RBSP data for the next occurrence of byte-aligned bits with the binary patterns
// specified above.
//
if (_inNalUnit && _zeroCount == 2 && _curValue == 3)
{
ReadBytes(1, &_curValue);
_zeroCount = 0;
}
if (_curValue == 0)
{
_zeroCount = (_zeroCount + 1) % 3;
}
else
{
_zeroCount = 0;
}
_curBitPos = 0;
}
}
} // namespace Codec
} // namespace Mmp