-
Notifications
You must be signed in to change notification settings - Fork 12
/
tsparser.c
executable file
·621 lines (510 loc) · 15.7 KB
/
tsparser.c
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
* MpegtTS Basic Parser
* Copyright (c) jeoliva, All rights reserved.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
*License along with this library.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include "bitreader.h"
#include "tsparser.h"
// Parse a TS packet
void parseTSPacket(TSParser *parser, ABitReader *bitReader)
{
uint32_t sync_byte;
uint32_t transport_error_indicator;
uint32_t payload_unit_start_indicator;
uint32_t transport_priority;
uint32_t pid;
uint32_t transport_scrambling_control;
uint32_t adaptation_field_control;
uint32_t continuity_counter;
sync_byte = getBits(bitReader, 8);
transport_error_indicator = getBits(bitReader, 1);
if (transport_error_indicator != 0)
{
printf("Packet with Error. Transport Error indicator: %u\n", transport_error_indicator);
}
payload_unit_start_indicator = getBits(bitReader, 1);
printf("Payload unit start indicator: %u\n", payload_unit_start_indicator);
transport_priority = getBits(bitReader, 1);
printf("Transport Priority: %u\n", transport_priority);
pid = getBits(bitReader, 13);
printf("PID: 0x%04x\n", pid);
transport_scrambling_control = getBits(bitReader, 2);
printf("Transport Scrambling Control: %u\n", transport_scrambling_control);
adaptation_field_control = getBits(bitReader, 2);
printf("Adaptation field control: %u\n", adaptation_field_control);
continuity_counter = getBits(bitReader, 4);
printf("Continuity Counter: %u\n", continuity_counter);
if (adaptation_field_control == 2 || adaptation_field_control == 3)
{
parseAdaptationField(parser, bitReader);
}
if (adaptation_field_control == 1 || adaptation_field_control == 3)
{
parseProgramId(parser, bitReader, pid, payload_unit_start_indicator);
}
}
// Parse adaptation field
void parseAdaptationField(TSParser *parser, ABitReader *bitReader)
{
uint32_t adaptation_field_length = getBits(bitReader, 8);
printf("Adaptation field length: %u\n", adaptation_field_length);
if (adaptation_field_length > 0)
{
skipBits(bitReader, adaptation_field_length * 8);
}
}
// Parse program Id
void parseProgramId(TSParser *parser, ABitReader *bitReader, uint32_t pid, uint32_t payload_unit_start_indicator)
{
int i, handled = 0;
TSPointersListItem *listItem;
TSProgram *pProgram;
if (pid == 0)
{
if (payload_unit_start_indicator)
{
uint32_t skip = getBits(bitReader, 8);
skipBits(bitReader, skip * 8);
}
parseProgramAssociationTable(parser, bitReader);
return;
}
for (listItem = parser->mPrograms.mHead; listItem != NULL; listItem = listItem->mNext)
{
pProgram = (TSProgram *)listItem->mData;
if (pid == pProgram->mProgramMapPID)
{
if (payload_unit_start_indicator)
{
uint32_t skip = getBits(bitReader, 8);
skipBits(bitReader, skip * 8);
}
parseProgramMap(parser, pProgram, bitReader);
handled = 1;
break;
}
else
{
TSStream *pStream = getStreamByPID(pProgram, pid);
if (pStream != NULL)
{
parseStream(pStream, payload_unit_start_indicator, bitReader);
handled = 1;
break;
}
}
}
if (!handled)
{
printf("PID 0x%04x not handled.\n", pid);
}
}
// Parse Program Association table
void parseProgramAssociationTable(TSParser *parser, ABitReader *bitReader)
{
size_t i;
uint32_t table_id = getBits(bitReader, 8);
printf(" table_id = %u\n", table_id);
uint32_t section_syntax_indicator = getBits(bitReader, 1);
printf(" section_syntax_indicator = %u\n", section_syntax_indicator);
getBits(bitReader, 1);
printf(" reserved = %u\n", getBits(bitReader, 2));
uint32_t section_length = getBits(bitReader, 12);
printf(" section_length = %u\n", section_length);
printf(" transport_stream_id = %u\n", getBits(bitReader, 16));
printf(" reserved = %u\n", getBits(bitReader, 2));
printf(" version_number = %u\n", getBits(bitReader, 5));
printf(" current_next_indicator = %u\n", getBits(bitReader, 1));
printf(" section_number = %u\n", getBits(bitReader, 8));
printf(" last_section_number = %u\n", getBits(bitReader, 8));
size_t numProgramBytes = (section_length - 5 /* header */ - 4 /* crc */);
printf(" numProgramBytes = %ld\n", numProgramBytes);
for (i = 0; i < numProgramBytes / 4; ++i)
{
uint32_t program_number = getBits(bitReader, 16);
printf(" program_number = %u\n", program_number);
printf(" reserved = %u\n", getBits(bitReader, 3));
if (program_number == 0)
{
printf(" network_PID = 0x%04x\n", getBits(bitReader, 13));
}
else
{
unsigned programMapPID = getBits(bitReader, 13);
printf(" program_map_PID = 0x%04x\n", programMapPID);
addProgram(parser, programMapPID);
}
}
printf(" CRC = 0x%08x\n", getBits(bitReader, 32));
}
// Parse Stream
void parseStream(TSStream *stream, uint32_t payload_unit_start_indicator, ABitReader *bitReader)
{
size_t payloadSizeBits;
if (payload_unit_start_indicator)
{
if (stream->mPayloadStarted)
{
flushStreamData(stream);
}
stream->mPayloadStarted = 1;
}
if (!stream->mPayloadStarted)
{
return;
}
payloadSizeBits = numBitsLeft(bitReader);
memcpy(stream->mBuffer + stream->mBufferSize, getBitReaderData(bitReader), payloadSizeBits / 8);
stream->mBufferSize += (payloadSizeBits / 8);
}
// Flush stream data
void flushStreamData(TSStream *stream)
{
ABitReader bitReader;
initABitReader(&bitReader, (uint8_t *)stream->mBuffer, stream->mBufferSize);
parsePES(stream, &bitReader);
stream->mBufferSize = 0;
}
// Parse a PES packet
void parsePES(TSStream *stream, ABitReader *bitReader)
{
uint32_t packet_startcode_prefix = getBits(bitReader, 24);
uint32_t stream_id = getBits(bitReader, 8);
uint32_t PES_packet_length = getBits(bitReader, 16);
if (stream_id != 0xbc // program_stream_map
&& stream_id != 0xbe // padding_stream
&& stream_id != 0xbf // private_stream_2
&& stream_id != 0xf0 // ECM
&& stream_id != 0xf1 // EMM
&& stream_id != 0xff // program_stream_directory
&& stream_id != 0xf2 // DSMCC
&& stream_id != 0xf8) // H.222.1 type E
{
uint32_t PTS_DTS_flags;
uint32_t ESCR_flag;
uint32_t ES_rate_flag;
uint32_t DSM_trick_mode_flag;
uint32_t additional_copy_info_flag;
uint32_t PES_header_data_length;
uint32_t optional_bytes_remaining;
uint64_t PTS = 0, DTS = 0;
skipBits(bitReader, 8);
PTS_DTS_flags = getBits(bitReader, 2);
ESCR_flag = getBits(bitReader, 1);
ES_rate_flag = getBits(bitReader, 1);
DSM_trick_mode_flag = getBits(bitReader, 1);
additional_copy_info_flag = getBits(bitReader, 1);
skipBits(bitReader, 2);
PES_header_data_length = getBits(bitReader, 8);
optional_bytes_remaining = PES_header_data_length;
if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3)
{
skipBits(bitReader, 4);
PTS = parseTSTimestamp(bitReader);
skipBits(bitReader, 1);
optional_bytes_remaining -= 5;
if (PTS_DTS_flags == 3)
{
skipBits(bitReader, 4);
DTS = parseTSTimestamp(bitReader);
skipBits(bitReader, 1);
optional_bytes_remaining -= 5;
}
}
if (ESCR_flag)
{
skipBits(bitReader, 2);
uint64_t ESCR = parseTSTimestamp(bitReader);
skipBits(bitReader, 11);
optional_bytes_remaining -= 6;
}
if (ES_rate_flag)
{
skipBits(bitReader, 24);
optional_bytes_remaining -= 3;
}
skipBits(bitReader, optional_bytes_remaining * 8);
// ES data follows.
if (PES_packet_length != 0)
{
uint32_t dataLength = PES_packet_length - 3 - PES_header_data_length;
// Signaling we have payload data
onPayloadData(stream, PTS_DTS_flags, PTS, DTS, getBitReaderData(bitReader), dataLength);
skipBits(bitReader, dataLength * 8);
}
else
{
size_t payloadSizeBits;
// Signaling we have payload data
onPayloadData(stream, PTS_DTS_flags, PTS, DTS, getBitReaderData(bitReader), numBitsLeft(bitReader) / 8);
payloadSizeBits = numBitsLeft(bitReader);
}
}
else if (stream_id == 0xbe)
{ // padding_stream
skipBits(bitReader, PES_packet_length * 8);
}
else
{
skipBits(bitReader, PES_packet_length * 8);
}
}
int64_t parseTSTimestamp(ABitReader *bitReader)
{
int64_t result = ((uint64_t)getBits(bitReader, 3)) << 30;
skipBits(bitReader, 1);
result |= ((uint64_t)getBits(bitReader, 15)) << 15;
skipBits(bitReader, 1);
result |= getBits(bitReader, 15);
return result;
}
// convert PTS to timestamp
int64_t convertPTSToTimestamp(TSStream *stream, uint64_t PTS)
{
if (!stream->mProgram->mFirstPTSValid)
{
stream->mProgram->mFirstPTSValid = 1;
stream->mProgram->mFirstPTS = PTS;
PTS = 0;
}
else if (PTS < stream->mProgram->mFirstPTS)
{
PTS = 0;
}
else
{
PTS -= stream->mProgram->mFirstPTS;
}
return (PTS * 100) / 9;
}
// Function called when we have payload content
void onPayloadData(TSStream *stream, uint32_t PTS_DTS_flag, uint64_t PTS, uint64_t DTS, uint8_t *data, size_t size)
{
int64_t timeUs = convertPTSToTimestamp(stream, PTS);
if (stream->mStreamType == TS_STREAM_VIDEO)
{
printf("Payload Data!!!! Video (%02x), PTS: %lld, DTS:%lld, Size: %ld\n", stream->mStreamType, PTS, DTS, size);
}
else if (stream->mStreamType == TS_STREAM_AUDIO)
{
printf("Payload Data!!!! Audio (%02x), PTS: %lld, DTS:%lld, Size: %ld\n", stream->mStreamType, PTS, DTS, size);
}
}
// Add a new program to the list of programs
void addProgram(TSParser *parser, uint32_t programMapPID)
{
TSProgram *program = (TSProgram *)malloc(sizeof(TSProgram));
memset(program, 0, sizeof(TSProgram));
program->mProgramMapPID = programMapPID;
addItemToList(&parser->mPrograms, program);
}
// Add a new stream to the specified program
void addStream(TSProgram *program, uint32_t elementaryPID, uint32_t streamType)
{
TSStream *stream = (TSStream *)malloc(sizeof(TSStream));
memset(stream, 0, sizeof(TSStream));
stream->mProgram = program;
stream->mElementaryPID = elementaryPID;
stream->mStreamType = streamType;
printf("Add Stream. PID: %d, Stream Type: %d (%X)\n", elementaryPID, streamType, streamType);
addItemToList(&program->mStreams, stream);
}
// Add an item to a linked list
void addItemToList(TSPointersList *list, void *data)
{
TSPointersListItem *item = (TSPointersListItem *)malloc(sizeof(TSPointersListItem));
item->mData = data;
item->mNext = NULL;
if (list->mTail != NULL)
{
list->mTail->mNext = item;
list->mTail = item;
}
else
{
list->mHead = list->mTail = item;
}
}
// Get Stream object given its id
TSStream *getStreamByPID(TSProgram *program, uint32_t pid)
{
TSPointersListItem *listItem;
TSStream *stream;
for (listItem = program->mStreams.mHead; listItem != NULL; listItem = listItem->mNext)
{
stream = (TSStream *)listItem->mData;
if (stream != NULL && stream->mElementaryPID == pid)
{
return stream;
}
}
return NULL;
}
// Get a Program object given its id
TSProgram *getProgramByPID(TSParser *parser, uint32_t pid)
{
TSPointersListItem *listItem;
TSProgram *program;
for (listItem = parser->mPrograms.mHead; listItem != NULL; listItem = listItem->mNext)
{
program = (TSProgram *)listItem->mData;
if (program != NULL && program->mProgramMapPID == pid)
{
return program;
}
}
return NULL;
}
// Parse program map
void parseProgramMap(TSParser *parser, TSProgram *program, ABitReader *bitReader)
{
uint32_t section_syntax_indicator;
uint32_t table_id;
uint32_t section_length;
uint32_t program_info_length;
size_t infoBytesRemaining;
uint32_t streamType;
uint32_t elementaryPID;
uint32_t ES_info_length;
uint32_t info_bytes_remaining;
table_id = getBits(bitReader, 8);
printf("****** PROGRAM MAP *****\n");
printf(" table_id = %u\n", table_id);
section_syntax_indicator = getBits(bitReader, 1);
printf(" section_syntax_indicator = %u\n", section_syntax_indicator);
// Reserved
skipBits(bitReader, 3);
section_length = getBits(bitReader, 12);
printf(" section_length = %u\n", section_length);
printf(" program_number = %u\n", getBits(bitReader, 16));
printf(" reserved = %u\n", getBits(bitReader, 2));
printf(" version_number = %u\n", getBits(bitReader, 5));
printf(" current_next_indicator = %u\n", getBits(bitReader, 1));
printf(" section_number = %u\n", getBits(bitReader, 8));
printf(" last_section_number = %u\n", getBits(bitReader, 8));
printf(" reserved = %u\n", getBits(bitReader, 3));
printf(" PCR_PID = 0x%04x\n", getBits(bitReader, 13));
printf(" reserved = %u\n", getBits(bitReader, 4));
program_info_length = getBits(bitReader, 12);
printf(" program_info_length = %u\n", program_info_length);
skipBits(bitReader, program_info_length * 8); // skip descriptors
// infoBytesRemaining is the number of bytes that make up the
// variable length section of ES_infos. It does not include the
// final CRC.
infoBytesRemaining = section_length - 9 - program_info_length - 4;
while (infoBytesRemaining > 0)
{
streamType = getBits(bitReader, 8);
printf(" stream_type = 0x%02x\n", streamType);
printf(" reserved = %u\n", getBits(bitReader, 3));
elementaryPID = getBits(bitReader, 13);
printf(" elementary_PID = 0x%04x\n", elementaryPID);
printf(" reserved = %u\n", getBits(bitReader, 4));
ES_info_length = getBits(bitReader, 12);
printf(" ES_info_length = %u\n", ES_info_length);
info_bytes_remaining = ES_info_length;
while (info_bytes_remaining >= 2)
{
uint32_t descLength;
printf(" tag = 0x%02x\n", getBits(bitReader, 8));
descLength = getBits(bitReader, 8);
printf(" len = %u\n", descLength);
skipBits(bitReader, descLength * 8);
info_bytes_remaining -= descLength + 2;
}
if (getStreamByPID(program, elementaryPID) == NULL)
addStream(program, elementaryPID, streamType);
infoBytesRemaining -= 5 + ES_info_length;
}
printf(" CRC = 0x%08x\n", getBits(bitReader, 32));
printf("****** PROGRAM MAP *****\n");
}
// Free program resources
void freeProgramResources(TSProgram *program)
{
// Free Streams
TSPointersListItem *item;
TSStream *pStream;
while (program->mStreams.mHead != NULL)
{
item = program->mStreams.mHead;
program->mStreams.mHead = item->mNext;
if (item->mData != NULL)
{
pStream = (TSStream *)item->mData;
free(pStream);
}
free(item);
}
}
// Free parser resources
void freeParserResources(TSParser *parser)
{
// Free Programs
TSPointersListItem *item;
TSProgram *pProgram;
while (parser->mPrograms.mHead != NULL)
{
item = parser->mPrograms.mHead;
parser->mPrograms.mHead = item->mNext;
if (item->mData != NULL)
{
pProgram = (TSProgram *)item->mData;
freeProgramResources(pProgram);
free(pProgram);
}
free(item);
}
}
// Signal a discontinuity
void signalDiscontinuity(TSParser *parser, int isSeek)
{
TSPointersListItem *item;
TSProgram *pProgram;
while (parser->mPrograms.mHead != NULL)
{
item = parser->mPrograms.mHead;
parser->mPrograms.mHead = item->mNext;
if (item->mData != NULL)
{
pProgram = (TSProgram *)item->mData;
signalDiscontinuityToProgram(pProgram, isSeek);
}
}
}
// Signal a discontinuity to a program
void signalDiscontinuityToProgram(TSProgram *program, int isSeek)
{
TSPointersListItem *item;
TSStream *pStream;
while (program->mStreams.mHead != NULL)
{
item = program->mStreams.mHead;
program->mStreams.mHead = item->mNext;
if (item->mData != NULL)
{
pStream = (TSStream *)item->mData;
signalDiscontinuityToStream(pStream, isSeek);
}
}
}
// Signal a discontinuity to a stream
void signalDiscontinuityToStream(TSStream *stream, int isSeek)
{
stream->mPayloadStarted = 0;
stream->mBufferSize = 0;
}