forked from chikuzen/MPEG2DecPlus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetbit.cpp
463 lines (408 loc) · 15 KB
/
getbit.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
461
462
/*
* Copyright (C) Chia-chen Kuo - April 2001
*
* This file is part of DVD2AVI, a free MPEG-2 decoder
*
* DVD2AVI is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* DVD2AVI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include "MPEG2Decoder.h"
void CMPEG2Decoder::Initialize_Buffer()
{
Rdptr = Rdbfr + BUFFER_SIZE;
Rdmax = Rdptr;
buffer_invalid = (uint8_t*)(UINTPTR_MAX);
if (SystemStream_Flag)
{
if (Rdptr >= Rdmax)
Next_Packet();
CurrentBfr = *Rdptr++ << 24;
if (Rdptr >= Rdmax)
Next_Packet();
CurrentBfr += *Rdptr++ << 16;
if (Rdptr >= Rdmax)
Next_Packet();
CurrentBfr += *Rdptr++ << 8;
if (Rdptr >= Rdmax)
Next_Packet();
CurrentBfr += *Rdptr++;
Fill_Next();
}
else
{
Fill_Buffer();
CurrentBfr = (*Rdptr << 24) + (*(Rdptr + 1) << 16) + (*(Rdptr + 2) << 8) + *(Rdptr + 3);
Rdptr += 4;
Fill_Next();
}
BitsLeft = 32;
}
struct transport_packet {
// 1 byte
uint8_t sync_byte; // 8 bslbf
// 2 bytes
uint8_t transport_error_indicator;// 1 bslbf
uint8_t payload_unit_start_indicator;// 1 bslbf
uint8_t transport_priority; // 1 bslbf
uint16_t pid; // 13 uimsbf
// 1 byte
uint8_t transport_scrambling_control;// 2 bslbf
uint8_t adaptation_field_control;// 2 bslbf
uint8_t continuity_counter;// 4 uimsbf
// VVV (only valid if adaptation_field_control != 1)
// 1 byte
uint8_t adaptation_field_length; // 8 uimsbf
// VVV (only valid if adaptation_field_length != 0)
// 1 byte
uint8_t discontinuity_indicator; // 1 bslbf
uint8_t random_access_indicator; // 1 bslbf
uint8_t elementary_stream_priority_indicator; // 1 bslbf
uint8_t PCR_flag; // 1 bslbf
uint8_t OPCR_flag; // 1 bslbf
uint8_t splicing_point_flag; // 1 bslbf
uint8_t transport_private_data_flag; // 1 bslbf
uint8_t adaptation_field_extension_flag; // 1 bslbf
/*
if(adaptation_field_control=='10' || adaptation_field_control=='11'){
adaptation_field()
}
if(adaptation_field_control=='01' || adaptation_field_control=='11') {
for (i=0;i<N;i++){
data_byte 8 bslbf
}
}
*/
};
#define SKIP_TRANSPORT_PACKET_BYTES( bytes_to_skip ) \
{ Rdptr += (bytes_to_skip); Packet_Length -= (bytes_to_skip); }
void CMPEG2Decoder::Next_Transport_Packet()
{
int Packet_Length; // bytes remaining in MPEG-2 transport packet
int Packet_Header_Length;
uint32_t code;
transport_packet tp = { 0 };
for (;;)
{
// 0) initialize some temp variables
Packet_Length = TransportPacketSize; // total length of an MPEG-2 transport packet
if (TransportPacketSize == 192)
{
//Get_Byte();
//Get_Byte();
//Get_Byte();
//Get_Byte();
Rdptr += 4;
Packet_Length -= 4;
}
// 1) Search for a sync byte. Gives some protection against emulation.
for (;;)
{
if ((tp.sync_byte = Get_Byte()) != 0x47)
continue;
if (Rdptr - Rdbfr > TransportPacketSize)
{
if (Rdptr[-(TransportPacketSize + 1)] == 0x47)
break;
}
else if (Rdbfr + Read - Rdptr > TransportPacketSize - static_cast<int64_t>(1))
{
if (Rdptr[+(TransportPacketSize - 1)] == 0x47)
break;
}
else
{
// We can't check so just accept this sync byte.
break;
}
}
--Packet_Length; // decrement the sync_byte;
// 2) get pid, transport_error_indicator, payload_unit_start_indicator
code = Get_Short();
Packet_Length = Packet_Length - 2; // decrement the two bytes we just got;
tp.pid = code & 0x1FFF; // bits [12:0]
tp.transport_error_indicator = (code >> 15) & 0x01; // bit#15
tp.payload_unit_start_indicator = (code >> 14) & 0x01; // bit#14
tp.transport_priority = (code >> 13) & 0x01; // bit#13
// 3) get other fields
code = Get_Byte();
--Packet_Length; // decrement the 1 byte we just got;
tp.transport_scrambling_control = (code >> 6) & 0x03;// 2 bslbf
tp.adaptation_field_control = (code >> 4) & 0x03;// 2 bslbf
tp.continuity_counter = code & 0x0F;// 4 uimsbf
// 4) check for early-exit conditions ... (possibly skip packet)
// we don't care about the continuity counter
// if ( tp.continuity_counter != previous_continuity_counter ) ...
if (tp.transport_error_indicator ||
(tp.adaptation_field_control == 0))
{
// skip remaining bytes in current packet
SKIP_TRANSPORT_PACKET_BYTES(Packet_Length)
continue; // abort, and circle back to top of 'for() loop'
}
// 5) check
if (tp.adaptation_field_control == 2 || tp.adaptation_field_control == 3)
{
// adaptation field is present
tp.adaptation_field_length = Get_Byte(); // 8-bits
--Packet_Length; // decrement the 1 byte we just got;
if (tp.adaptation_field_length != 0) // end of field already?
{
// if we made it this far, we no longer need to decrement
// Packet_Length. We took care of it up there!
code = Get_Byte();
--Packet_Length; // decrement the 1 byte we just got;
tp.discontinuity_indicator = (code >> 7) & 0x01; // 1 bslbf
tp.random_access_indicator = (code >> 6) & 0x01; // 1 bslbf
tp.elementary_stream_priority_indicator = (code >> 5) & 0x01; // 1 bslbf
tp.PCR_flag = (code >> 4) & 0x01; // 1 bslbf
tp.OPCR_flag = (code >> 3) & 0x01; // 1 bslbf
tp.splicing_point_flag = (code >> 2) & 0x01; // 1 bslbf
tp.transport_private_data_flag = (code >> 1) & 0x01; // 1 bslbf
tp.adaptation_field_extension_flag = (code >> 0) & 0x01; // 1 bslbf
// skip the remainder of the adaptation_field
SKIP_TRANSPORT_PACKET_BYTES(tp.adaptation_field_length - 1)
} // if ( tp.adaptation_field_length != 0 )
} // if ( tp.adaptation_field_control != 1 )
// we've processed the header, so now just the payload is left...
// video
if (tp.pid == MPEG2_Transport_VideoPID && Packet_Length > 0)
{
#if 0
code = Get_Short();
code = (code & 0xffff) << 16 | Get_Short();
Packet_Length = Packet_Length - 4; // remove these two bytes
// Packet start?
if (code < 0x000001E0 || code > 0x000001EF)
if (!tp.payload_unit_start_indicator)
{
// No, move the buffer-pointer back.
Rdptr -= 4;
Packet_Length = Packet_Length + 4; // restore these four bytes
}
else
#endif
if (tp.payload_unit_start_indicator)
{
// YES, pull out PTS
//Get_Short();
//Get_Short();
//Get_Short(); // MPEG2-PES total Packet_Length
//Get_Byte(); // skip a byte
Rdptr += 7;
code = Get_Byte();
Packet_Header_Length = Get_Byte();
Packet_Length = Packet_Length - 9; // compensate the bytes we extracted
// get PTS, and skip rest of PES-header
if (code >= 0x80 && Packet_Header_Length > 4) // Extension_flag ?
{
// Skip PES_PTS
//Get_Short();
//Get_Short();
Rdptr += 4;
Get_Byte();
Packet_Length = Packet_Length - 5;
SKIP_TRANSPORT_PACKET_BYTES(Packet_Header_Length - static_cast<int64_t>(5))
}
else
SKIP_TRANSPORT_PACKET_BYTES(Packet_Header_Length)
}
Rdmax = Rdptr + Packet_Length;
if (TransportPacketSize == 204)
Rdmax -= 16;
return;
}
// fall through case
// skip the remainder of the adaptation_field
SKIP_TRANSPORT_PACKET_BYTES(Packet_Length)
} // for
}
// PVA packet data structure.
struct pva_packet {
uint16_t sync_byte;
uint8_t stream_id;
uint8_t counter;
uint8_t reserved;
uint8_t flags;
uint16_t length;
};
// PVA transport stream parser.
void CMPEG2Decoder::Next_PVA_Packet()
{
uint32_t Packet_Length;
pva_packet pva;
uint32_t PTS;
for (;;)
{
// Search for a good sync.
while (true)
{
// Sync word is 0x4156.
if (Get_Byte() != 0x41) continue;
if (Get_Byte() != 0x56)
{
// This byte might be a 0x41, so back up by one.
Rdptr--;
continue;
}
// To protect against emulation of the sync word,
// also check that the stream says audio or video.
pva.stream_id = Get_Byte();
if (pva.stream_id != 0x01 && pva.stream_id != 0x02)
{
// This byte might be a 0x41, so back up by one.
Rdptr--;
continue;
}
break;
}
// Pick up the remaining packet header fields.
pva.counter = Get_Byte();
pva.reserved = Get_Byte();
pva.flags = Get_Byte();
pva.length = Get_Byte() << 8;
pva.length |= Get_Byte();
Packet_Length = pva.length;
// Any payload?
if (Packet_Length == 0 || pva.reserved != 0x55)
continue; // No, try the next packet.
// Check stream id for video.
if (pva.stream_id == 1)
{
// This is a video packet.
// Extract the PTS if it exists.
if (pva.flags & 0x10)
{
// The spec is unclear about the significance of the prebytes field.
// It appears to be safe to ignore it.
PTS = (int)((Get_Byte() << 24) | (Get_Byte() << 16) | (Get_Byte() << 8) | Get_Byte());
Packet_Length -= 4;
}
// Deliver the video to the ES parsing layer.
Rdmax = Rdptr + Packet_Length;
return;
}
// Not an video packet or an audio packet to be demultiplexed. Keep looking.
SKIP_TRANSPORT_PACKET_BYTES(Packet_Length);
}
}
void CMPEG2Decoder::Next_Packet()
{
if (SystemStream_Flag == 2) // MPEG-2 transport packet?
{
Next_Transport_Packet();
return;
}
else if (SystemStream_Flag == 3) // PVA packet?
{
Next_PVA_Packet();
return;
}
uint32_t code, Packet_Length, Packet_Header_Length;
static int stream_type;
while (true) {
code = Get_Short();
code = (code << 16) + Get_Short();
// remove system layer byte stuffing
while ((code & 0xffffff00) != 0x00000100) {
if (Fault_Flag == OUT_OF_BITS)
return;
code = (code << 8) | Get_Byte();
}
if (code == PACK_START_CODE) {
if ((Get_Byte() & 0xf0) == 0x20) {
Rdptr += 7; // MPEG1 program stream
stream_type = MPEG1_PROGRAM_STREAM;
}
else {
Rdptr += 8; // MPEG2 program stream
stream_type = MPEG2_PROGRAM_STREAM;
}
}
else if ((code & 0xfffffff0) == VIDEO_ELEMENTARY_STREAM) {
Packet_Length = Get_Short();
Rdmax = Rdptr + Packet_Length;
if (stream_type == MPEG1_PROGRAM_STREAM) {
// MPEG1 program stream.
Packet_Header_Length = 0;
// Stuffing bytes.
do {
code = Get_Byte();
Packet_Header_Length += 1;
} while (code == 0xff);
if ((code & 0xc0) == 0x40) {
// STD bytes.
Get_Byte();
code = Get_Byte();
Packet_Header_Length += 2;
}
if ((code & 0xf0) == 0x20) {
// PTS bytes.
Get_Short();
Get_Short();
Packet_Header_Length += 4;
}
else if ((code & 0xf0) == 0x30) {
// PTS/DTS bytes.
Get_Short();
Get_Short();
Get_Short();
Get_Short();
Get_Byte();
Packet_Header_Length += 9;
}
return;
}
else {
// MPEG2 program stream.
code = Get_Byte();
if ((code & 0xc0) == 0x80)
{
//code = Get_Byte();
++Rdptr;
Packet_Header_Length = Get_Byte();
Rdptr += Packet_Header_Length;
return;
}
else
Rdptr += Packet_Length - 1;
}
}
else if (code >= SYSTEM_START_CODE)
{
code = Get_Short();
Rdptr += code;
}
}
}
void CMPEG2Decoder::Next_File()
{
if (File_Flag < static_cast<int>(Infile.size() - 1)) {
File_Flag++;
}
else {
File_Flag = 0;
}
// Even if we ran out of files, we reread the first one, just so
// the decoder at least processes valid data until it detects the
// fault flag and exits.
_lseeki64(Infile[File_Flag], 0, SEEK_SET);
int bytes = _read(Infile[File_Flag], Rdbfr + Read, BUFFER_SIZE - Read);
if (Read + static_cast<int64_t>(bytes) == BUFFER_SIZE)
// The whole buffer has valid data.
buffer_invalid = (uint8_t*)(UINTPTR_MAX);
else
// Point to the first invalid buffer location.
buffer_invalid = Rdbfr + Read + bytes;
}