winlin

update the ts_info parse the PMT and PES header

1 # the listen ports, split by space. 1 # the listen ports, split by space.
2 -listen 1937; 2 +listen 1935;
3 # the default chunk size is 128, max is 65536, 3 # the default chunk size is 128, max is 65536,
4 # some client does not support chunk size change, 4 # some client does not support chunk size change,
5 # however, most clients supports it and it can improve 5 # however, most clients supports it and it can improve
1 -/**  
2 -g++ -o ts_info ts_info.cpp -g -O0 -ansi  
3 -*/  
4 -#if 1  
5 -#include <sys/types.h>  
6 -#include <sys/stat.h>  
7 -#include <fcntl.h>  
8 -#include <unistd.h>  
9 -#include <stdio.h>  
10 -#include <string.h>  
11 -#include <assert.h>  
12 -  
13 -#define trace(msg, ...) printf(msg"\n", ##__VA_ARGS__);  
14 -#define srs_freep(p) delete p; p = NULL  
15 -#define srs_freepa(p) delete[] p; p = NULL  
16 -#define srs_assert(p) assert(p)  
17 -  
18 -#endif  
19 -/**  
20 -ISO/IEC 13818-1:2000(E)  
21 -Introduction  
22 - Intro. 1 Transport Stream  
23 - Intro. 2 Program Stream  
24 - Intro. 4 Packetized Elementary Stream  
25 -SECTION 2 ¨C TECHNICAL ELEMENTS  
26 - 2.4 Transport Stream bitstream requirements  
27 - 2.4.1 Transport Stream coding structure and parameters  
28 - 2.4.2 Transport Stream system target decoder  
29 - 2.4.3 Specification of the Transport Stream syntax and semantics  
30 - 2.4.3.1 Transport Stream  
31 - 2.4.3.2 Transport Stream packet layer  
32 - 2.4.3.3 Semantic definition of fields in Transport Stream packet layer  
33 - 2.4.3.5 Semantic definition of fields in adaptation field  
34 - 2.4.3.6 PES packet  
35 - 2.4.3.7 Semantic definition of fields in PES packet  
36 - 2.4.4 Program specific information  
37 - 2.4.4.5 Semantic definition of fields in program association section  
38 - 2.4.4.6 Conditional access Table  
39 - 2.5 Program Stream bitstream requirements  
40 - 2.6 Program and program element descriptors  
41 - 2.7 Restrictions on the multiplexed stream semantics  
42 -Annex A ¨C CRC Decoder Model  
43 -*/  
44 -#if 1  
45 -// Transport Stream packets are 188 bytes in length.  
46 -#define TS_PACKET_SIZE 188  
47 -  
48 -// Program Association Table(see Table 2-25).  
49 -#define PID_PAT 0x00  
50 -// Conditional Access Table (see Table 2-27).  
51 -#define PID_CAT 0x01  
52 -// Transport Stream Description Table  
53 -#define PID_TSDT 0x02  
54 -// null packets (see Table 2-3)  
55 -#define PID_NULL 0x01FFF  
56 -  
57 -/*adaptation_field_control*/  
58 -// No adaptation_field, payload only  
59 -#define AFC_PAYLOAD_ONLY 0x01  
60 -// Adaptation_field only, no payload  
61 -#define AFC_ADAPTION_ONLY 0x02  
62 -// Adaptation_field followed by payload  
63 -#define AFC_BOTH 0x03  
64 -#endif  
65 -  
66 -struct TSPacket  
67 -{  
68 - // 4B ts packet header.  
69 - struct Header  
70 - {  
71 - // 1B  
72 - int8_t sync_byte; //8bits  
73 - // 2B  
74 - int8_t transport_error_indicator; //1bit  
75 - int8_t payload_unit_start_indicator; //1bit  
76 - int8_t transport_priority; //1bit  
77 - u_int16_t pid; //13bits  
78 - // 1B  
79 - int8_t transport_scrambling_control; //2bits  
80 - int8_t adaption_field_control; //2bits  
81 - u_int8_t continuity_counter; //4bits  
82 -  
83 - int get_size()  
84 - {  
85 - return 4;  
86 - }  
87 -  
88 - int demux(TSPacket* ppkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)  
89 - {  
90 - int ret = 0;  
91 -  
92 - // ts packet header.  
93 - sync_byte = *p++;  
94 - if (sync_byte != 0x47) {  
95 - trace("ts+sync_bytes invalid sync_bytes: %#x, expect is 0x47", sync_byte);  
96 - return -1;  
97 - }  
98 -  
99 - pid = 0;  
100 - ((char*)&pid)[1] = *p++;  
101 - ((char*)&pid)[0] = *p++;  
102 -  
103 - transport_error_indicator = (pid >> 15) & 0x01;  
104 - payload_unit_start_indicator = (pid >> 14) & 0x01;  
105 - transport_priority = (pid >> 13) & 0x01;  
106 - pid &= 0x1FFF;  
107 -  
108 - continuity_counter = *p++;  
109 -  
110 - transport_scrambling_control = (continuity_counter >> 6) & 0x03;  
111 - adaption_field_control = (continuity_counter >> 4) & 0x03;  
112 - continuity_counter &= 0x0F;  
113 -  
114 - trace("ts+header sync: %#x error: %d unit_start: %d priotiry: %d pid: %d scrambling: %d adaption: %d counter: %d",  
115 - sync_byte, transport_error_indicator, payload_unit_start_indicator, transport_priority, pid,  
116 - transport_scrambling_control, adaption_field_control, continuity_counter);  
117 -  
118 - return ret;  
119 - }  
120 - } *header;  
121 -  
122 - // variant ts packet adation field.  
123 - struct AdaptionField  
124 - {  
125 - // 1B  
126 - u_int8_t adaption_field_length; //8bits  
127 - // 1B  
128 - int8_t discontinuity_indicator; //1bit  
129 - int8_t random_access_indicator; //1bit  
130 - int8_t elementary_stream_priority_indicator; //1bit  
131 - int8_t PCR_flag; //1bit  
132 - int8_t OPCR_flag; //1bit  
133 - int8_t splicing_point_flag; //1bit  
134 - int8_t transport_private_data_flag; //1bit  
135 - int8_t adaptation_field_extension_flag; //1bit  
136 -  
137 - // if PCR_flag, 6B  
138 - int64_t program_clock_reference_base; //33bits  
139 - //6bits reserved.  
140 - int16_t program_clock_reference_extension; //9bits  
141 -  
142 - // if OPCR_flag, 6B  
143 - int64_t original_program_clock_reference_base; //33bits  
144 - //6bits reserved.  
145 - int16_t original_program_clock_reference_extension; //9bits  
146 -  
147 - // if splicing_point_flag, 1B  
148 - int8_t splice_countdown; //8bits  
149 -  
150 - // if transport_private_data_flag, 1+p[0] B  
151 - u_int8_t transport_private_data_length; //8bits  
152 - char* transport_private_data; //[transport_private_data_length]bytes  
153 -  
154 - // if adaptation_field_extension_flag, 2+x bytes  
155 - u_int8_t adaptation_field_extension_length; //8bits  
156 - int8_t ltw_flag; //1bit  
157 - int8_t piecewise_rate_flag; //1bit  
158 - int8_t seamless_splice_flag; //1bit  
159 - //5bits reserved  
160 - // if ltw_flag, 2B  
161 - int8_t ltw_valid_flag; //1bit  
162 - int16_t ltw_offset; //15bits  
163 - // if piecewise_rate_flag, 3B  
164 - //2bits reserved  
165 - int32_t piecewise_rate; //22bits  
166 - // if seamless_splice_flag, 5B  
167 - int8_t splice_type; //4bits  
168 - int8_t DTS_next_AU0; //3bits  
169 - int8_t marker_bit0; //1bit  
170 - int16_t DTS_next_AU1; //15bits  
171 - int8_t marker_bit1; //1bit  
172 - int16_t DTS_next_AU2; //15bits  
173 - int8_t marker_bit2; //1bit  
174 - // left bytes.  
175 - char* af_ext_reserved;  
176 -  
177 - // left bytes.  
178 - char* af_reserved;  
179 -  
180 - // user defined data size.  
181 - int __user_size;  
182 -  
183 - AdaptionField()  
184 - {  
185 - transport_private_data = NULL;  
186 - af_ext_reserved = NULL;  
187 - af_reserved = NULL;  
188 -  
189 - __user_size = 0;  
190 - }  
191 -  
192 - virtual ~AdaptionField()  
193 - {  
194 - srs_freepa(transport_private_data);  
195 - srs_freepa(af_ext_reserved);  
196 - srs_freepa(af_reserved);  
197 - }  
198 -  
199 - int get_size()  
200 - {  
201 - return __user_size;  
202 - }  
203 -  
204 - int demux(TSPacket* ppkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)  
205 - {  
206 - int ret = 0;  
207 -  
208 - adaption_field_length = *p++;  
209 - u_int8_t* pos_af = p;  
210 - __user_size = 1 + adaption_field_length;  
211 -  
212 - if (adaption_field_length <= 0) {  
213 - trace("ts+af empty af decoded.");  
214 - return ret;  
215 - }  
216 -  
217 - int8_t value = *p++;  
218 -  
219 - discontinuity_indicator = (value >> 7) & 0x01;  
220 - random_access_indicator = (value >> 6) & 0x01;  
221 - elementary_stream_priority_indicator = (value >> 5) & 0x01;  
222 - PCR_flag = (value >> 4) & 0x01;  
223 - OPCR_flag = (value >> 3) & 0x01;  
224 - splicing_point_flag = (value >> 2) & 0x01;  
225 - transport_private_data_flag = (value >> 1) & 0x01;  
226 - adaptation_field_extension_flag = (value >> 0) & 0x01;  
227 -  
228 - trace("ts+af af flags parsed, discontinuity: %d random: %d priority: %d PCR: %d OPCR: %d slicing: %d private: %d extension: %d",  
229 - discontinuity_indicator, random_access_indicator, elementary_stream_priority_indicator, PCR_flag, OPCR_flag, splicing_point_flag,  
230 - transport_private_data_flag, adaptation_field_extension_flag);  
231 -  
232 - char* pp = NULL;  
233 - if (PCR_flag) {  
234 - pp = (char*)&program_clock_reference_base;  
235 - pp[5] = *p++;  
236 - pp[4] = *p++;  
237 - pp[3] = *p++;  
238 - pp[2] = *p++;  
239 - pp[1] = *p++;  
240 - pp[0] = *p++;  
241 -  
242 - program_clock_reference_extension = program_clock_reference_base & 0x1F;  
243 - program_clock_reference_base = (program_clock_reference_base >> 9) & 0x1FFFFFFFF;  
244 - }  
245 - if (OPCR_flag) {  
246 - pp = (char*)&original_program_clock_reference_base;  
247 - pp[5] = *p++;  
248 - pp[4] = *p++;  
249 - pp[3] = *p++;  
250 - pp[2] = *p++;  
251 - pp[1] = *p++;  
252 - pp[0] = *p++;  
253 -  
254 - original_program_clock_reference_extension = original_program_clock_reference_base & 0x1F;  
255 - original_program_clock_reference_base = (original_program_clock_reference_base >> 9) & 0x1FFFFFFFF;  
256 - }  
257 - if (splicing_point_flag) {  
258 - splice_countdown = *p++;  
259 - }  
260 - if (transport_private_data_flag) {  
261 - transport_private_data_length = *p++;  
262 - transport_private_data = new char[transport_private_data_length];  
263 - for (int i = 0; i < transport_private_data_length; i++) {  
264 - transport_private_data[i] = *p++;  
265 - }  
266 - }  
267 - if (adaptation_field_extension_flag) {  
268 - adaptation_field_extension_length = *p++;  
269 - u_int8_t* pos_af_ext = p;  
270 -  
271 - ltw_flag = *p++;  
272 -  
273 - piecewise_rate_flag = (ltw_flag >> 6) & 0x01;  
274 - seamless_splice_flag = (ltw_flag >> 5) & 0x01;  
275 - ltw_flag = (ltw_flag >> 7) & 0x01;  
276 -  
277 - if (ltw_flag) {  
278 - pp = (char*)&ltw_offset;  
279 - pp[1] = *p++;  
280 - pp[0] = *p++;  
281 -  
282 - ltw_valid_flag = (ltw_offset >> 15) &0x01;  
283 - ltw_offset &= 0x7FFF;  
284 - }  
285 - if (piecewise_rate_flag) {  
286 - pp = (char*)&piecewise_rate;  
287 - pp[2] = *p++;  
288 - pp[1] = *p++;  
289 - pp[0] = *p++;  
290 -  
291 - piecewise_rate &= 0x3FFFFF;  
292 - }  
293 - if (seamless_splice_flag) {  
294 - // 1B  
295 - marker_bit0 = *p++;  
296 -  
297 - splice_type = (marker_bit0 >> 4) & 0x0F;  
298 - DTS_next_AU0 = (marker_bit0 >> 1) & 0x07;  
299 - marker_bit0 &= 0x01;  
300 -  
301 - // 2B  
302 - pp = (char*)&DTS_next_AU1;  
303 - pp[1] = *p++;  
304 - pp[0] = *p++;  
305 -  
306 - marker_bit1 = DTS_next_AU1 & 0x01;  
307 - DTS_next_AU1 = (DTS_next_AU1 >> 1) & 0x7FFF;  
308 -  
309 - // 2B  
310 - pp = (char*)&DTS_next_AU2;  
311 - pp[1] = *p++;  
312 - pp[0] = *p++;  
313 -  
314 - marker_bit2 = DTS_next_AU2 & 0x01;  
315 - DTS_next_AU2 = (DTS_next_AU2 >> 1) & 0x7FFF;  
316 - }  
317 -  
318 - // af_ext_reserved  
319 - int ext_size = adaptation_field_extension_length - (p - pos_af_ext);  
320 - if (ext_size > 0) {  
321 - af_ext_reserved = new char[ext_size];  
322 - memcpy(af_ext_reserved, p, ext_size);  
323 - p += ext_size;  
324 - }  
325 - }  
326 -  
327 - // af_reserved  
328 - int af_size = adaption_field_length - (p - pos_af);  
329 - if (af_size > 0) {  
330 - af_reserved = new char[af_size];  
331 - memcpy(af_reserved, p, af_size);  
332 - p += af_size;  
333 - }  
334 -  
335 - return ret;  
336 - }  
337 - } *adaption_field;  
338 -  
339 - // variant ts packet payload.  
340 - // PES packet or PSI table.  
341 - struct Payload  
342 - {  
343 - /**  
344 - * the size of payload(payload plush the 1byte pointer_field).  
345 - */  
346 - int size;  
347 - int pointer_field_size;  
348 -  
349 - /**  
350 - * the actually parsed type.  
351 - */  
352 - enum Type  
353 - {  
354 - TypeUnknown=-1,  
355 - TypeReserved, // TypeReserved, nothing parsed, used reserved.  
356 - TypePAT, //TypePAT, PAT parsed, in pat field.  
357 - } type;  
358 -  
359 - /**  
360 - * 2.4.4.2 Semantics definition of fields in pointer syntax  
361 - */  
362 - u_int8_t pointer_field;  
363 -  
364 - /**  
365 - * if not parsed, store data in this field.  
366 - */  
367 - struct Reserved  
368 - {  
369 - int size;  
370 - char* bytes;  
371 -  
372 - Reserved()  
373 - {  
374 - size = 0;  
375 - bytes = NULL;  
376 - }  
377 -  
378 - virtual ~Reserved()  
379 - {  
380 - srs_freepa(bytes);  
381 - }  
382 -  
383 - int demux(TSPacket* ppkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)  
384 - {  
385 - int ret = 0;  
386 -  
387 - size = ppkt->payload->size - ppkt->payload->pointer_field_size;  
388 -  
389 - // not parsed bytes.  
390 - if (size > 0) {  
391 - bytes = new char[size];  
392 - memcpy(bytes, p, size);  
393 - p += size;  
394 - }  
395 -  
396 - return ret;  
397 - }  
398 - } *reserved;  
399 -  
400 - /**  
401 - * 2.4.4.3 Program association Table. page 61.  
402 - */  
403 - struct PAT {  
404 - // 1B  
405 - u_int8_t table_id; //8bits  
406 -  
407 - // 2B  
408 - int8_t section_syntax_indicator; //1bit  
409 - int8_t const0_value; //1bit  
410 - // 2bits reserved.  
411 - u_int16_t section_length; //12bits  
412 -  
413 - // 2B  
414 - u_int16_t transport_stream_id; //16bits  
415 -  
416 - // 1B  
417 - // 2bits reerverd.  
418 - int8_t version_number; //5bits  
419 - int8_t current_next_indicator; //1bit  
420 -  
421 - // 1B  
422 - u_int8_t section_number; //8bits  
423 -  
424 - // 1B  
425 - u_int8_t last_section_number; //8bits  
426 -  
427 - // multiple 4B program data.  
428 - // program_number 16bits  
429 - // reserved 2bits  
430 - // 13bits data: 0x1FFF  
431 - // if program_number program_map_PID 13bits  
432 - // else network_PID 13bytes.  
433 - int program_size;  
434 - int32_t* programs; //32bits  
435 -  
436 - // 4B  
437 - int32_t CRC_32; //32bits  
438 -  
439 - PAT()  
440 - {  
441 - programs = NULL;  
442 - }  
443 -  
444 - virtual ~PAT()  
445 - {  
446 - srs_freepa(programs);  
447 - }  
448 -  
449 - int get_program(int index)  
450 - {  
451 - srs_assert(index < program_size);  
452 - return programs[index] & 0x1FFF;  
453 - }  
454 -  
455 - int demux(TSPacket* ppkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)  
456 - {  
457 - int ret = 0;  
458 -  
459 - table_id = *p++;  
460 -  
461 - char* pp = (char*)&section_length;  
462 - pp[1] = *p++;  
463 - pp[0] = *p++;  
464 - u_int8_t* pos = p;  
465 -  
466 - section_syntax_indicator = (section_length >> 15) & 0x01;  
467 - const0_value = (section_length >> 14) & 0x01;  
468 - section_length &= 0x0FFF;  
469 -  
470 - pp = (char*)&transport_stream_id;  
471 - pp[1] = *p++;  
472 - pp[0] = *p++;  
473 -  
474 - current_next_indicator = *p++;  
475 - version_number = (current_next_indicator >> 1) & 0x1F;  
476 - current_next_indicator &= 0x01;  
477 -  
478 - section_number = *p++;  
479 - last_section_number = *p++;  
480 -  
481 - // 4 is crc size.  
482 - int program_bytes = section_length - 4 - (p - pos);  
483 - program_size = program_bytes / 4;  
484 - if (program_size > 0) {  
485 - programs = new int32_t[program_size];  
486 - for (int i = 0; i < program_size; i++) {  
487 - pp = (char*)&programs[i];  
488 - pp[3] = *p++;  
489 - pp[2] = *p++;  
490 - pp[1] = *p++;  
491 - pp[0] = *p++;  
492 - }  
493 - }  
494 -  
495 - pp = (char*)&CRC_32;  
496 - pp[3] = *p++;  
497 - pp[2] = *p++;  
498 - pp[1] = *p++;  
499 - pp[0] = *p++;  
500 -  
501 - return ret;  
502 - }  
503 - } *pat;  
504 -  
505 - /**  
506 - * 2.4.3.6 PES packet. page 49.  
507 - */  
508 -  
509 - Payload()  
510 - {  
511 - size = 0;  
512 - pointer_field_size = 0;  
513 -  
514 - type = TypeUnknown;  
515 - reserved = NULL;  
516 - pat = NULL;  
517 - }  
518 -  
519 - virtual ~Payload()  
520 - {  
521 - srs_freep(reserved);  
522 - srs_freep(pat);  
523 - }  
524 -  
525 - int demux(TSPacket* ppkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)  
526 - {  
527 - int ret = 0;  
528 -  
529 - if (ppkt->header->payload_unit_start_indicator) {  
530 - pointer_field = *p++;  
531 - pointer_field_size = 1;  
532 - }  
533 -  
534 - if (ppkt->header->pid == PID_PAT) {  
535 - type = TypePAT;  
536 - pat = new PAT();  
537 - return pat->demux(ppkt, start, last, p);  
538 - }  
539 -  
540 - // not parsed bytes.  
541 - type = TypeReserved;  
542 - reserved = new Reserved();  
543 - if ((ret = reserved->demux(ppkt, start, last, p)) != 0) {  
544 - return ret;  
545 - }  
546 -  
547 - return ret;  
548 - }  
549 - } *payload;  
550 -  
551 - TSPacket()  
552 - {  
553 - header = new Header();  
554 - adaption_field = new AdaptionField();  
555 - payload = new Payload();  
556 - }  
557 -  
558 - virtual ~TSPacket()  
559 - {  
560 - srs_freep(header);  
561 - srs_freep(adaption_field);  
562 - srs_freep(payload);  
563 - }  
564 -  
565 - int demux(u_int8_t* start, u_int8_t* last, u_int8_t*& p)  
566 - {  
567 - int ret = 0;  
568 -  
569 - if ((ret = header->demux(this, start, last, p)) != 0) {  
570 - return ret;  
571 - }  
572 -  
573 - if (header->adaption_field_control == AFC_ADAPTION_ONLY || header->adaption_field_control == AFC_BOTH) {  
574 - if ((ret = adaption_field->demux(this, start, last, p)) != 0) {  
575 - trace("ts+header af(adaption field) decode error. ret=%d", ret);  
576 - return ret;  
577 - }  
578 - trace("ts+header af(adaption field) decoded.");  
579 - }  
580 -  
581 - // calc the user defined data size for payload.  
582 - payload->size = TS_PACKET_SIZE - header->get_size() - adaption_field->get_size();  
583 -  
584 - if (header->adaption_field_control == AFC_PAYLOAD_ONLY || header->adaption_field_control == AFC_BOTH) {  
585 - if ((ret = payload->demux(this, start, last, p)) != 0) {  
586 - trace("ts+header payload decode error. ret=%d", ret);  
587 - return ret;  
588 - }  
589 - trace("ts+header payload decoded.");  
590 - }  
591 -  
592 - trace("ts+header parsed finished. parsed: %d left: %d header: %d payload: %d(%d+%d)",  
593 - (int)(p - start), (int)(last - p), header->get_size(), payload->size, payload->pointer_field_size,  
594 - payload->size - payload->pointer_field_size);  
595 -  
596 - return finish();  
597 - }  
598 -  
599 - int finish()  
600 - {  
601 - return 0;  
602 - }  
603 -};  
604 -  
605 -int main(int /*argc*/, char** /*argv*/)  
606 -{  
607 - const char* file = "livestream-1347.ts";  
608 - //file = "nginx-rtmp-hls/livestream-1347-currupt.ts";  
609 - int fd = open(file, O_RDONLY);  
610 -  
611 - int ret = 0;  
612 - trace("demuxer+read packet count offset T+0 T+1 T+2 T+3 T+x T+L2 T+L1 T+L0");  
613 - for (int i = 0, offset = 0; ; i++) {  
614 - u_int8_t ts_packet[TS_PACKET_SIZE];  
615 - memset(ts_packet, 0, sizeof(ts_packet));  
616 -  
617 - int nread = read(fd, ts_packet, sizeof(ts_packet));  
618 - if (nread == 0) {  
619 - trace("demuxer+read got EOF, read completed, offset: %07d.", offset);  
620 - break;  
621 - }  
622 - if (nread != TS_PACKET_SIZE) {  
623 - trace("demuxer+read error to read ts packet. nread=%d", nread);  
624 - break;  
625 - }  
626 - trace("demuxer+read packet %04d %07d 0x%02x 0x%02x 0x%02x 0x%02x ... 0x%02x 0x%02x 0x%02x",  
627 - i, offset, ts_packet[0], ts_packet[1], ts_packet[2], ts_packet[3],  
628 - ts_packet[TS_PACKET_SIZE - 3], ts_packet[TS_PACKET_SIZE - 2], ts_packet[TS_PACKET_SIZE - 1]);  
629 -  
630 - u_int8_t* p = ts_packet;  
631 - u_int8_t* start = ts_packet;  
632 - u_int8_t* last = ts_packet + TS_PACKET_SIZE;  
633 -  
634 - TSPacket pkt;  
635 - if ((ret = pkt.demux(start, last, p)) != 0) {  
636 - trace("demuxer+read decode ts packet error. ret=%d", ret);  
637 - return ret;  
638 - }  
639 -  
640 - offset += nread;  
641 - }  
642 -  
643 - close(fd);  
644 - return ret;  
645 -}  
646 - 1 +/**
  2 +g++ -o ts_info ts_info.cpp -g -O0 -ansi
  3 +*/
  4 +#if 1
  5 +#include <sys/types.h>
  6 +#include <sys/stat.h>
  7 +#include <fcntl.h>
  8 +#include <unistd.h>
  9 +#include <stdio.h>
  10 +#include <string.h>
  11 +#include <assert.h>
  12 +
  13 +#include <vector>
  14 +
  15 +#define trace(msg, ...) printf(msg"\n", ##__VA_ARGS__);
  16 +#define srs_freep(p) delete p; p = NULL
  17 +#define srs_freepa(p) delete[] p; p = NULL
  18 +#define srs_assert(p) assert(p)
  19 +
  20 +#endif
  21 +/**
  22 +ISO/IEC 13818-1:2000(E)
  23 +Introduction
  24 + Intro. 1 Transport Stream
  25 + Intro. 2 Program Stream
  26 + Intro. 4 Packetized Elementary Stream
  27 +SECTION 2 ¨C TECHNICAL ELEMENTS
  28 + 2.4 Transport Stream bitstream requirements
  29 + 2.4.1 Transport Stream coding structure and parameters
  30 + 2.4.2 Transport Stream system target decoder
  31 + 2.4.3 Specification of the Transport Stream syntax and semantics
  32 + 2.4.3.1 Transport Stream
  33 + 2.4.3.2 Transport Stream packet layer
  34 + 2.4.3.3 Semantic definition of fields in Transport Stream packet layer
  35 + 2.4.3.5 Semantic definition of fields in adaptation field
  36 + 2.4.3.6 PES packet
  37 + 2.4.3.7 Semantic definition of fields in PES packet
  38 + 2.4.4 Program specific information
  39 + 2.4.4.5 Semantic definition of fields in program association section
  40 + 2.4.4.6 Conditional access Table
  41 + 2.5 Program Stream bitstream requirements
  42 + 2.6 Program and program element descriptors
  43 + 2.7 Restrictions on the multiplexed stream semantics
  44 +Annex A ¨C CRC Decoder Model
  45 +*/
  46 +#if 1
  47 +// Transport Stream packets are 188 bytes in length.
  48 +#define TS_PACKET_SIZE 188
  49 +
  50 +// Program Association Table(see Table 2-25).
  51 +#define PID_PAT 0x00
  52 +// Conditional Access Table (see Table 2-27).
  53 +#define PID_CAT 0x01
  54 +// Transport Stream Description Table
  55 +#define PID_TSDT 0x02
  56 +// null packets (see Table 2-3)
  57 +#define PID_NULL 0x01FFF
  58 +
  59 +/*adaptation_field_control*/
  60 +// No adaptation_field, payload only
  61 +#define AFC_PAYLOAD_ONLY 0x01
  62 +// Adaptation_field only, no payload
  63 +#define AFC_ADAPTION_ONLY 0x02
  64 +// Adaptation_field followed by payload
  65 +#define AFC_BOTH 0x03
  66 +#endif
  67 +
  68 +// Table 2-29 – Stream type assignments. page 66.
  69 +enum TSStreamType
  70 +{
  71 + /*defined by ffmpeg*/
  72 + TSStreamTypeVideoMpeg1 = 0x01,
  73 + TSStreamTypeVideoMpeg2 = 0x02,
  74 + TSStreamTypeAudioMpeg1 = 0x03,
  75 + TSStreamTypeAudioMpeg2 = 0x04,
  76 + TSStreamTypePrivateSection = 0x05,
  77 + TSStreamTypePrivateData = 0x06,
  78 + TSStreamTypeAudioAAC = 0x0f,
  79 + TSStreamTypeVideoMpeg4 = 0x10,
  80 + TSStreamTypeVideoH264 = 0x1b,
  81 + TSStreamTypeAudioAC3 = 0x81,
  82 + TSStreamTypeAudioDTS = 0x8a,
  83 +};
  84 +
  85 +/**
  86 +* the actually parsed type.
  87 +*/
  88 +enum TSPidType
  89 +{
  90 + TSPidTypeReserved = 0, // TSPidTypeReserved, nothing parsed, used reserved.
  91 +
  92 + TSPidTypePAT, // Program associtate table
  93 + TSPidTypePMT, // Program map table.
  94 +
  95 + TSPidTypeVideo,
  96 + TSPidTypeAudio,
  97 +};
  98 +
  99 +// forward declares.
  100 +class TSHeader;
  101 +class TSAdaptionField;
  102 +class TSPayload;
  103 +class TSPayloadReserved;
  104 +class TSPayloadPAT;
  105 +class TSPayloadPMT;
  106 +class TSPayloadPES;
  107 +class TSContext;
  108 +
  109 +// TSPacket declares.
  110 +class TSPacket
  111 +{
  112 +public:
  113 + TSHeader* header;
  114 + TSAdaptionField* adaption_field;
  115 + TSPayload* payload;
  116 +
  117 + TSPacket();
  118 + virtual ~TSPacket();
  119 + int demux(TSContext* ctx, u_int8_t* start, u_int8_t* last, u_int8_t*& p);
  120 + int finish();
  121 +};
  122 +
  123 +// TSHeader declares.
  124 +class TSHeader
  125 +{
  126 +public:
  127 + // 1B
  128 + int8_t sync_byte; //8bits
  129 + // 2B
  130 + int8_t transport_error_indicator; //1bit
  131 + int8_t payload_unit_start_indicator; //1bit
  132 + int8_t transport_priority; //1bit
  133 + u_int16_t pid; //13bits
  134 + // 1B
  135 + int8_t transport_scrambling_control; //2bits
  136 + int8_t adaption_field_control; //2bits
  137 + u_int8_t continuity_counter; //4bits
  138 +
  139 + TSHeader();
  140 + virtual ~TSHeader();
  141 + int get_size();
  142 + int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p);
  143 +};
  144 +
  145 +// variant ts packet adation field. page 40.
  146 +class TSAdaptionField
  147 +{
  148 +public:
  149 + // 1B
  150 + u_int8_t adaption_field_length; //8bits
  151 + // 1B
  152 + int8_t discontinuity_indicator; //1bit
  153 + int8_t random_access_indicator; //1bit
  154 + int8_t elementary_stream_priority_indicator; //1bit
  155 + int8_t PCR_flag; //1bit
  156 + int8_t OPCR_flag; //1bit
  157 + int8_t splicing_point_flag; //1bit
  158 + int8_t transport_private_data_flag; //1bit
  159 + int8_t adaptation_field_extension_flag; //1bit
  160 +
  161 + // if PCR_flag, 6B
  162 + int64_t program_clock_reference_base; //33bits
  163 + //6bits reserved.
  164 + int16_t program_clock_reference_extension; //9bits
  165 +
  166 + // if OPCR_flag, 6B
  167 + int64_t original_program_clock_reference_base; //33bits
  168 + //6bits reserved.
  169 + int16_t original_program_clock_reference_extension; //9bits
  170 +
  171 + // if splicing_point_flag, 1B
  172 + int8_t splice_countdown; //8bits
  173 +
  174 + // if transport_private_data_flag, 1+p[0] B
  175 + u_int8_t transport_private_data_length; //8bits
  176 + char* transport_private_data; //[transport_private_data_length]bytes
  177 +
  178 + // if adaptation_field_extension_flag, 2+x bytes
  179 + u_int8_t adaptation_field_extension_length; //8bits
  180 + int8_t ltw_flag; //1bit
  181 + int8_t piecewise_rate_flag; //1bit
  182 + int8_t seamless_splice_flag; //1bit
  183 + //5bits reserved
  184 + // if ltw_flag, 2B
  185 + int8_t ltw_valid_flag; //1bit
  186 + int16_t ltw_offset; //15bits
  187 + // if piecewise_rate_flag, 3B
  188 + //2bits reserved
  189 + int32_t piecewise_rate; //22bits
  190 + // if seamless_splice_flag, 5B
  191 + int8_t splice_type; //4bits
  192 + int8_t DTS_next_AU0; //3bits
  193 + int8_t marker_bit0; //1bit
  194 + int16_t DTS_next_AU1; //15bits
  195 + int8_t marker_bit1; //1bit
  196 + int16_t DTS_next_AU2; //15bits
  197 + int8_t marker_bit2; //1bit
  198 + // left bytes.
  199 + char* af_ext_reserved;
  200 +
  201 + // left bytes.
  202 + char* af_reserved;
  203 +
  204 + // user defined data size.
  205 + int __user_size;
  206 +
  207 + TSAdaptionField();
  208 + virtual ~TSAdaptionField();
  209 + int get_size();
  210 + int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p);
  211 +};
  212 +
  213 +// variant ts packet payload.
  214 +// PES packet or PSI table.
  215 +// TSPayloadPAT: page 61.
  216 +class TSPayload
  217 +{
  218 +public:
  219 + /**
  220 + * the size of payload(payload plush the 1byte pointer_field).
  221 + */
  222 + int size;
  223 + int pointer_field_size;
  224 +
  225 + TSPidType type;
  226 +
  227 + /**
  228 + * 2.4.4.2 Semantics definition of fields in pointer syntax
  229 + */
  230 + u_int8_t pointer_field;
  231 +
  232 + TSPayloadReserved* reserved;
  233 + TSPayloadPAT* pat;
  234 + TSPayloadPMT* pmt;
  235 + TSPayloadPES* pes;
  236 +
  237 + /**
  238 + * 2.4.3.6 PES packet. page 49.
  239 + */
  240 +
  241 + TSPayload();
  242 + virtual ~TSPayload();;
  243 + void read_pointer_field(TSPacket* pkt, u_int8_t*& p);
  244 + int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p);
  245 +};
  246 +
  247 +
  248 +/**
  249 +* 2.4.4.3 Program association Table. page 61.
  250 +*/
  251 +class TSPayloadPAT
  252 +{
  253 +public:
  254 + // 1B
  255 + u_int8_t table_id; //8bits
  256 +
  257 + // 2B
  258 + int8_t section_syntax_indicator; //1bit
  259 + int8_t const0_value; //1bit
  260 + // 2bits reserved.
  261 + u_int16_t section_length; //12bits
  262 +
  263 + // 2B
  264 + u_int16_t transport_stream_id; //16bits
  265 +
  266 + // 1B
  267 + // 2bits reerverd.
  268 + int8_t version_number; //5bits
  269 + int8_t current_next_indicator; //1bit
  270 +
  271 + // 1B
  272 + u_int8_t section_number; //8bits
  273 +
  274 + // 1B
  275 + u_int8_t last_section_number; //8bits
  276 +
  277 + // multiple 4B program data.
  278 + // program_number 16bits
  279 + // reserved 2bits
  280 + // 13bits data: 0x1FFF
  281 + // if program_number program_map_PID 13bits
  282 + // else network_PID 13bytes.
  283 + int program_size;
  284 + int32_t* programs; //32bits
  285 +
  286 + // 4B
  287 + int32_t CRC_32; //32bits
  288 +
  289 + TSPayloadPAT();
  290 + virtual ~TSPayloadPAT();
  291 + int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p);
  292 +};
  293 +
  294 +class TSPMTESInfo
  295 +{
  296 +public:
  297 + // 1B
  298 + u_int8_t stream_type; //8bits
  299 +
  300 + // 2B
  301 + // 3bits reserved
  302 + int16_t elementary_PID; //13bits
  303 +
  304 + // 2B
  305 + // 4bits reserved
  306 + int16_t ES_info_length; //12bits
  307 +
  308 + char* ES_info; //[ES_info_length] bytes.
  309 +
  310 + TSPMTESInfo();
  311 + virtual ~TSPMTESInfo();
  312 +};
  313 +
  314 +
  315 +/**
  316 +* 2.4.4.8 Program Map Table. page 64.
  317 +*/
  318 +class TSPayloadPMT
  319 +{
  320 +public:
  321 + // 1B
  322 + u_int8_t table_id; //8bits
  323 +
  324 + // 2B
  325 + int8_t section_syntax_indicator; //1bit
  326 + int8_t const0_value; //1bit
  327 + // 2bits reserved.
  328 + u_int16_t section_length; //12bits
  329 +
  330 + // 2B
  331 + u_int16_t program_number; //16bits
  332 +
  333 + // 1B
  334 + // 2bits reerverd.
  335 + int8_t version_number; //5bits
  336 + int8_t current_next_indicator; //1bit
  337 +
  338 + // 1B
  339 + u_int8_t section_number; //8bits
  340 +
  341 + // 1B
  342 + u_int8_t last_section_number; //8bits
  343 +
  344 + // 2B
  345 + // 2bits reserved.
  346 + int16_t PCR_PID; //16bits
  347 +
  348 + // 2B
  349 + // 4bits reserved.
  350 + int16_t program_info_length; //12bits
  351 + char* program_info_desc; //[program_info_length]bytes
  352 +
  353 + // array of TSPMTESInfo.
  354 + std::vector<TSPMTESInfo*> ES_info;
  355 +
  356 + // 4B
  357 + int32_t CRC_32; //32bits
  358 +
  359 + TSPayloadPMT();
  360 + virtual ~TSPayloadPMT();
  361 + int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p);
  362 +};
  363 +
  364 +
  365 +/**
  366 +* 2.4.3.7 Semantic definition of fields in PES packet. page 49.
  367 +*/
  368 +class TSPayloadPES
  369 +{
  370 +public:
  371 + // 3B
  372 + int32_t packet_start_code_prefix; //24bits
  373 + // 1B
  374 + u_int8_t stream_id; //8bits
  375 + // 2B
  376 + u_int16_t PES_packet_length; //16bits
  377 +
  378 + TSPayloadPES();
  379 + virtual ~TSPayloadPES();
  380 + int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p);
  381 +};
  382 +
  383 +class TSPayloadReserved
  384 +{
  385 +public:
  386 + int size;
  387 + char* bytes;
  388 +
  389 + TSPayloadReserved();
  390 + virtual ~TSPayloadReserved();
  391 + int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p);
  392 +};
  393 +
  394 +struct TSPid
  395 +{
  396 + TSPidType type;
  397 + int16_t pid;
  398 +};
  399 +
  400 +// ts context
  401 +class TSContext
  402 +{
  403 +public:
  404 + /**
  405 + * consumed pids.
  406 + */
  407 + int pid_size;
  408 + TSPid* pids;
  409 +
  410 + TSContext();
  411 + virtual ~TSContext();
  412 + bool exists(int16_t pid);
  413 + TSPid* get(int16_t pid);
  414 + void push(TSPidType type, int16_t pid);
  415 +};
  416 +
  417 +TSContext::TSContext()
  418 +{
  419 + pid_size = 0;
  420 + pids = NULL;
  421 +}
  422 +
  423 +TSContext::~TSContext()
  424 +{
  425 + srs_freepa(pids);
  426 +}
  427 +
  428 +bool TSContext::exists(int16_t pid)
  429 +{
  430 + for (int i = 0; i < pid_size; i++) {
  431 + if (pid == pids[i].pid) {
  432 + return true;
  433 + }
  434 + }
  435 +
  436 + return false;
  437 +}
  438 +
  439 +TSPid* TSContext::get(int16_t pid)
  440 +{
  441 + for (int i = 0; i < pid_size; i++) {
  442 + if (pid == pids[i].pid) {
  443 + return &pids[i];
  444 + }
  445 + }
  446 +
  447 + return NULL;
  448 +}
  449 +
  450 +void TSContext::push(TSPidType type, int16_t pid)
  451 +{
  452 + if (exists(pid)) {
  453 + return;
  454 + }
  455 +
  456 + TSPid* p = new TSPid[pid_size + 1];
  457 + memcpy(p, pids, sizeof(TSPid) * pid_size);
  458 +
  459 + p[pid_size] = (TSPid){type, pid};
  460 + pid_size++;
  461 +
  462 + srs_freepa(pids);
  463 + pids = p;
  464 +}
  465 +
  466 +TSAdaptionField::TSAdaptionField()
  467 +{
  468 + transport_private_data = NULL;
  469 + af_ext_reserved = NULL;
  470 + af_reserved = NULL;
  471 +
  472 + __user_size = 0;
  473 +}
  474 +
  475 +TSAdaptionField::~TSAdaptionField()
  476 +{
  477 + srs_freepa(transport_private_data);
  478 + srs_freepa(af_ext_reserved);
  479 + srs_freepa(af_reserved);
  480 +}
  481 +
  482 +int TSAdaptionField::get_size()
  483 +{
  484 + return __user_size;
  485 +}
  486 +
  487 +int TSAdaptionField::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)
  488 +{
  489 + int ret = 0;
  490 +
  491 + adaption_field_length = *p++;
  492 + u_int8_t* pos_af = p;
  493 + __user_size = 1 + adaption_field_length;
  494 +
  495 + if (adaption_field_length <= 0) {
  496 + trace("ts+af empty af decoded.");
  497 + return ret;
  498 + }
  499 +
  500 + int8_t value = *p++;
  501 +
  502 + discontinuity_indicator = (value >> 7) & 0x01;
  503 + random_access_indicator = (value >> 6) & 0x01;
  504 + elementary_stream_priority_indicator = (value >> 5) & 0x01;
  505 + PCR_flag = (value >> 4) & 0x01;
  506 + OPCR_flag = (value >> 3) & 0x01;
  507 + splicing_point_flag = (value >> 2) & 0x01;
  508 + transport_private_data_flag = (value >> 1) & 0x01;
  509 + adaptation_field_extension_flag = (value >> 0) & 0x01;
  510 +
  511 + trace("ts+af af flags parsed, discontinuity: %d random: %d priority: %d PCR: %d OPCR: %d slicing: %d private: %d extension: %d",
  512 + discontinuity_indicator, random_access_indicator, elementary_stream_priority_indicator, PCR_flag, OPCR_flag, splicing_point_flag,
  513 + transport_private_data_flag, adaptation_field_extension_flag);
  514 +
  515 + char* pp = NULL;
  516 + if (PCR_flag) {
  517 + pp = (char*)&program_clock_reference_base;
  518 + pp[5] = *p++;
  519 + pp[4] = *p++;
  520 + pp[3] = *p++;
  521 + pp[2] = *p++;
  522 + pp[1] = *p++;
  523 + pp[0] = *p++;
  524 +
  525 + program_clock_reference_extension = program_clock_reference_base & 0x1F;
  526 + program_clock_reference_base = (program_clock_reference_base >> 9) & 0x1FFFFFFFF;
  527 + }
  528 + if (OPCR_flag) {
  529 + pp = (char*)&original_program_clock_reference_base;
  530 + pp[5] = *p++;
  531 + pp[4] = *p++;
  532 + pp[3] = *p++;
  533 + pp[2] = *p++;
  534 + pp[1] = *p++;
  535 + pp[0] = *p++;
  536 +
  537 + original_program_clock_reference_extension = original_program_clock_reference_base & 0x1F;
  538 + original_program_clock_reference_base = (original_program_clock_reference_base >> 9) & 0x1FFFFFFFF;
  539 + }
  540 + if (splicing_point_flag) {
  541 + splice_countdown = *p++;
  542 + }
  543 + if (transport_private_data_flag) {
  544 + transport_private_data_length = *p++;
  545 + transport_private_data = new char[transport_private_data_length];
  546 + for (int i = 0; i < transport_private_data_length; i++) {
  547 + transport_private_data[i] = *p++;
  548 + }
  549 + }
  550 + if (adaptation_field_extension_flag) {
  551 + adaptation_field_extension_length = *p++;
  552 + u_int8_t* pos_af_ext = p;
  553 +
  554 + ltw_flag = *p++;
  555 +
  556 + piecewise_rate_flag = (ltw_flag >> 6) & 0x01;
  557 + seamless_splice_flag = (ltw_flag >> 5) & 0x01;
  558 + ltw_flag = (ltw_flag >> 7) & 0x01;
  559 +
  560 + if (ltw_flag) {
  561 + pp = (char*)&ltw_offset;
  562 + pp[1] = *p++;
  563 + pp[0] = *p++;
  564 +
  565 + ltw_valid_flag = (ltw_offset >> 15) &0x01;
  566 + ltw_offset &= 0x7FFF;
  567 + }
  568 + if (piecewise_rate_flag) {
  569 + pp = (char*)&piecewise_rate;
  570 + pp[2] = *p++;
  571 + pp[1] = *p++;
  572 + pp[0] = *p++;
  573 +
  574 + piecewise_rate &= 0x3FFFFF;
  575 + }
  576 + if (seamless_splice_flag) {
  577 + // 1B
  578 + marker_bit0 = *p++;
  579 +
  580 + splice_type = (marker_bit0 >> 4) & 0x0F;
  581 + DTS_next_AU0 = (marker_bit0 >> 1) & 0x07;
  582 + marker_bit0 &= 0x01;
  583 +
  584 + // 2B
  585 + pp = (char*)&DTS_next_AU1;
  586 + pp[1] = *p++;
  587 + pp[0] = *p++;
  588 +
  589 + marker_bit1 = DTS_next_AU1 & 0x01;
  590 + DTS_next_AU1 = (DTS_next_AU1 >> 1) & 0x7FFF;
  591 +
  592 + // 2B
  593 + pp = (char*)&DTS_next_AU2;
  594 + pp[1] = *p++;
  595 + pp[0] = *p++;
  596 +
  597 + marker_bit2 = DTS_next_AU2 & 0x01;
  598 + DTS_next_AU2 = (DTS_next_AU2 >> 1) & 0x7FFF;
  599 + }
  600 +
  601 + // af_ext_reserved
  602 + int ext_size = adaptation_field_extension_length - (p - pos_af_ext);
  603 + if (ext_size > 0) {
  604 + af_ext_reserved = new char[ext_size];
  605 + memcpy(af_ext_reserved, p, ext_size);
  606 + p += ext_size;
  607 + }
  608 + }
  609 +
  610 + // af_reserved
  611 + int af_size = adaption_field_length - (p - pos_af);
  612 + if (af_size > 0) {
  613 + af_reserved = new char[af_size];
  614 + memcpy(af_reserved, p, af_size);
  615 + p += af_size;
  616 + }
  617 +
  618 + return ret;
  619 +}
  620 +
  621 +TSPayloadReserved::TSPayloadReserved()
  622 +{
  623 + size = 0;
  624 + bytes = NULL;
  625 +}
  626 +
  627 +TSPayloadReserved::~TSPayloadReserved()
  628 +{
  629 + srs_freepa(bytes);
  630 +}
  631 +
  632 +int TSPayloadReserved::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)
  633 +{
  634 + int ret = 0;
  635 +
  636 + size = pkt->payload->size - pkt->payload->pointer_field_size;
  637 +
  638 + // not parsed bytes.
  639 + if (size > 0) {
  640 + bytes = new char[size];
  641 + memcpy(bytes, p, size);
  642 + p += size;
  643 + }
  644 +
  645 + return ret;
  646 +}
  647 +
  648 +TSPayloadPAT::TSPayloadPAT()
  649 +{
  650 + programs = NULL;
  651 +}
  652 +
  653 +TSPayloadPAT::~TSPayloadPAT()
  654 +{
  655 + srs_freepa(programs);
  656 +}
  657 +
  658 +int TSPayloadPAT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)
  659 +{
  660 + int ret = 0;
  661 +
  662 + table_id = *p++;
  663 +
  664 + char* pp = (char*)&section_length;
  665 + pp[1] = *p++;
  666 + pp[0] = *p++;
  667 + u_int8_t* pos = p;
  668 +
  669 + section_syntax_indicator = (section_length >> 15) & 0x01;
  670 + const0_value = (section_length >> 14) & 0x01;
  671 + section_length &= 0x0FFF;
  672 +
  673 + pp = (char*)&transport_stream_id;
  674 + pp[1] = *p++;
  675 + pp[0] = *p++;
  676 +
  677 + current_next_indicator = *p++;
  678 + version_number = (current_next_indicator >> 1) & 0x1F;
  679 + current_next_indicator &= 0x01;
  680 +
  681 + section_number = *p++;
  682 + last_section_number = *p++;
  683 +
  684 + // 4 is crc size.
  685 + int program_bytes = section_length - 4 - (p - pos);
  686 + program_size = program_bytes / 4;
  687 + if (program_size > 0) {
  688 + programs = new int32_t[program_size];
  689 + for (int i = 0; i < program_size; i++) {
  690 + pp = (char*)&programs[i];
  691 + pp[3] = *p++;
  692 + pp[2] = *p++;
  693 + pp[1] = *p++;
  694 + pp[0] = *p++;
  695 +
  696 + int16_t pid = programs[i] & 0x1FFF;
  697 + ctx->push(TSPidTypePMT, pid);
  698 + }
  699 + }
  700 +
  701 + pp = (char*)&CRC_32;
  702 + pp[3] = *p++;
  703 + pp[2] = *p++;
  704 + pp[1] = *p++;
  705 + pp[0] = *p++;
  706 +
  707 + return ret;
  708 +}
  709 +
  710 +TSPMTESInfo::TSPMTESInfo()
  711 +{
  712 + ES_info_length = 0;
  713 + ES_info = NULL;
  714 +}
  715 +
  716 +TSPMTESInfo::~TSPMTESInfo()
  717 +{
  718 + srs_freepa(ES_info);
  719 +}
  720 +
  721 +TSPayloadPMT::TSPayloadPMT()
  722 +{
  723 + program_info_length = 0;
  724 + program_info_desc = NULL;
  725 +}
  726 +
  727 +TSPayloadPMT::~TSPayloadPMT()
  728 +{
  729 + srs_freepa(program_info_desc);
  730 +
  731 + for (std::vector<TSPMTESInfo*>::iterator it = ES_info.begin(); it != ES_info.end(); ++it) {
  732 + TSPMTESInfo* info = *it;
  733 + srs_freep(info);
  734 + }
  735 + ES_info.clear();
  736 +}
  737 +
  738 +int TSPayloadPMT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)
  739 +{
  740 + int ret = 0;
  741 +
  742 + table_id = *p++;
  743 +
  744 + char* pp = (char*)&section_length;
  745 + pp[1] = *p++;
  746 + pp[0] = *p++;
  747 + u_int8_t* pos = p;
  748 +
  749 + section_syntax_indicator = (section_length >> 15) & 0x01;
  750 + const0_value = (section_length >> 14) & 0x01;
  751 + section_length &= 0x0FFF;
  752 +
  753 + pp = (char*)&program_number;
  754 + pp[1] = *p++;
  755 + pp[0] = *p++;
  756 +
  757 + current_next_indicator = *p++;
  758 + version_number = (current_next_indicator >> 1) & 0x1F;
  759 + current_next_indicator &= 0x01;
  760 +
  761 + section_number = *p++;
  762 + last_section_number = *p++;
  763 +
  764 + pp = (char*)&PCR_PID;
  765 + pp[1] = *p++;
  766 + pp[0] = *p++;
  767 +
  768 + PCR_PID &= 0x1FFF;
  769 +
  770 + pp = (char*)&program_info_length;
  771 + pp[1] = *p++;
  772 + pp[0] = *p++;
  773 +
  774 + program_info_length &= 0xFFF;
  775 +
  776 + if (program_info_length > 0) {
  777 + program_info_desc = new char[program_info_length];
  778 + memcpy(program_info_desc, p, program_info_length);
  779 + p += program_info_length;
  780 + }
  781 +
  782 + // [section_length] - 4(CRC) - 9B - [program_info_length]
  783 + int ES_bytes = section_length - 4 - 9 - program_info_length;
  784 + while (ES_bytes > 0) {
  785 + TSPMTESInfo* info = new TSPMTESInfo();
  786 +
  787 + info->stream_type = *p++;
  788 + ES_bytes--;
  789 +
  790 + pp = (char*)&info->elementary_PID;
  791 + pp[1] = *p++;
  792 + pp[0] = *p++;
  793 + ES_bytes -= 2;
  794 +
  795 + info->elementary_PID &= 0x1FFF;
  796 +
  797 + pp = (char*)&info->ES_info_length;
  798 + pp[1] = *p++;
  799 + pp[0] = *p++;
  800 + ES_bytes -= 2;
  801 +
  802 + info->ES_info_length &= 0x0FFF;
  803 +
  804 + if (info->ES_info_length > 0) {
  805 + info->ES_info = new char[info->ES_info_length];
  806 + memcpy(info->ES_info, p, info->ES_info_length);
  807 +
  808 + p += info->ES_info_length;
  809 + ES_bytes -= info->ES_info_length;
  810 + }
  811 +
  812 + ES_info.push_back(info);
  813 +
  814 + // TODO: support more video type.
  815 + if (info->stream_type == TSStreamTypeVideoH264) {
  816 + ctx->push(TSPidTypeVideo, info->elementary_PID);
  817 + }
  818 + // TODO: support more audio type.
  819 + if (info->stream_type == TSStreamTypeAudioAAC) {
  820 + ctx->push(TSPidTypeAudio, info->elementary_PID);
  821 + }
  822 + }
  823 +
  824 + pp = (char*)&CRC_32;
  825 + pp[3] = *p++;
  826 + pp[2] = *p++;
  827 + pp[1] = *p++;
  828 + pp[0] = *p++;
  829 +
  830 + return ret;
  831 +}
  832 +
  833 +TSPayloadPES::TSPayloadPES()
  834 +{
  835 +}
  836 +
  837 +TSPayloadPES::~TSPayloadPES()
  838 +{
  839 +}
  840 +
  841 +int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)
  842 +{
  843 + int ret = 0;
  844 +
  845 + char* pp = (char*)&packet_start_code_prefix;
  846 + pp[2] = *p++;
  847 + pp[1] = *p++;
  848 + pp[0] = *p++;
  849 +
  850 + packet_start_code_prefix &= 0xFFFFFF;
  851 +
  852 + stream_id = *p++;
  853 +
  854 + pp = (char*)&PES_packet_length;
  855 + pp[1] = *p++;
  856 + pp[0] = *p++;
  857 +
  858 + return ret;
  859 +}
  860 +
  861 +/**
  862 +* 2.4.3.6 PES packet. page 49.
  863 +*/
  864 +
  865 +TSPayload::TSPayload()
  866 +{
  867 + size = 0;
  868 + pointer_field_size = 0;
  869 +
  870 + type = TSPidTypeReserved;
  871 + reserved = NULL;
  872 + pat = NULL;
  873 + pmt = NULL;
  874 + pes = NULL;
  875 +}
  876 +
  877 +TSPayload::~TSPayload()
  878 +{
  879 + srs_freep(reserved);
  880 + srs_freep(pat);
  881 + srs_freep(pmt);
  882 + srs_freep(pes);
  883 +}
  884 +
  885 +void TSPayload::read_pointer_field(TSPacket* pkt, u_int8_t*& p)
  886 +{
  887 + if (pkt->header->payload_unit_start_indicator) {
  888 + pointer_field = *p++;
  889 + pointer_field_size = 1;
  890 + }
  891 +}
  892 +
  893 +int TSPayload::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)
  894 +{
  895 + int ret = 0;
  896 +
  897 + if (pkt->header->pid == PID_PAT) {
  898 + read_pointer_field(pkt, p);
  899 +
  900 + type = TSPidTypePAT;
  901 + pat = new TSPayloadPAT();
  902 + return pat->demux(ctx, pkt, start, last, p);
  903 + }
  904 +
  905 + TSPid* pid = ctx->get(pkt->header->pid);
  906 + if (pid && pid->type == TSPidTypePMT) {
  907 + read_pointer_field(pkt, p);
  908 +
  909 + type = pid->type;
  910 + pmt = new TSPayloadPMT();
  911 + return pmt->demux(ctx, pkt, start, last, p);
  912 + }
  913 + if (pid && (pid->type == TSPidTypeVideo || pid->type == TSPidTypeAudio)) {
  914 + type = pid->type;
  915 + pes = new TSPayloadPES();
  916 + return pes->demux(ctx, pkt, start, last, p);
  917 + }
  918 +
  919 + // not parsed bytes.
  920 + type = TSPidTypeReserved;
  921 + reserved = new TSPayloadReserved();
  922 + if ((ret = reserved->demux(ctx, pkt, start, last, p)) != 0) {
  923 + return ret;
  924 + }
  925 +
  926 + return ret;
  927 +}
  928 +
  929 +TSPacket::TSPacket()
  930 +{
  931 + header = new TSHeader();
  932 + adaption_field = new TSAdaptionField();
  933 + payload = new TSPayload();
  934 +}
  935 +
  936 +TSPacket::~TSPacket()
  937 +{
  938 + srs_freep(header);
  939 + srs_freep(adaption_field);
  940 + srs_freep(payload);
  941 +}
  942 +
  943 +int TSPacket::demux(TSContext* ctx, u_int8_t* start, u_int8_t* last, u_int8_t*& p)
  944 +{
  945 + int ret = 0;
  946 +
  947 + if ((ret = header->demux(ctx, this, start, last, p)) != 0) {
  948 + return ret;
  949 + }
  950 +
  951 + if (header->adaption_field_control == AFC_ADAPTION_ONLY || header->adaption_field_control == AFC_BOTH) {
  952 + if ((ret = adaption_field->demux(ctx, this, start, last, p)) != 0) {
  953 + trace("ts+header af(adaption field) decode error. ret=%d", ret);
  954 + return ret;
  955 + }
  956 + trace("ts+header af(adaption field) decoded.");
  957 + }
  958 +
  959 + // calc the user defined data size for payload.
  960 + payload->size = TS_PACKET_SIZE - header->get_size() - adaption_field->get_size();
  961 +
  962 + if (header->adaption_field_control == AFC_PAYLOAD_ONLY || header->adaption_field_control == AFC_BOTH) {
  963 + if ((ret = payload->demux(ctx, this, start, last, p)) != 0) {
  964 + trace("ts+header payload decode error. ret=%d", ret);
  965 + return ret;
  966 + }
  967 + trace("ts+header payload decoded.");
  968 + }
  969 +
  970 + trace("ts+header parsed finished. parsed: %d left: %d header: %d payload: %d(%d+%d)",
  971 + (int)(p - start), (int)(last - p), header->get_size(), payload->size, payload->pointer_field_size,
  972 + payload->size - payload->pointer_field_size);
  973 +
  974 + return finish();
  975 +}
  976 +
  977 +int TSPacket::finish()
  978 +{
  979 + return 0;
  980 +}
  981 +
  982 +TSHeader::TSHeader()
  983 +{
  984 +}
  985 +
  986 +TSHeader::~TSHeader()
  987 +{
  988 +}
  989 +
  990 +int TSHeader::get_size()
  991 +{
  992 + return 4;
  993 +}
  994 +
  995 +int TSHeader::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)
  996 +{
  997 + int ret = 0;
  998 +
  999 + // ts packet header.
  1000 + sync_byte = *p++;
  1001 + if (sync_byte != 0x47) {
  1002 + trace("ts+sync_bytes invalid sync_bytes: %#x, expect is 0x47", sync_byte);
  1003 + return -1;
  1004 + }
  1005 +
  1006 + pid = 0;
  1007 + ((char*)&pid)[1] = *p++;
  1008 + ((char*)&pid)[0] = *p++;
  1009 +
  1010 + transport_error_indicator = (pid >> 15) & 0x01;
  1011 + payload_unit_start_indicator = (pid >> 14) & 0x01;
  1012 + transport_priority = (pid >> 13) & 0x01;
  1013 + pid &= 0x1FFF;
  1014 +
  1015 + ctx->push(TSPidTypePAT, pid);
  1016 +
  1017 + continuity_counter = *p++;
  1018 +
  1019 + transport_scrambling_control = (continuity_counter >> 6) & 0x03;
  1020 + adaption_field_control = (continuity_counter >> 4) & 0x03;
  1021 + continuity_counter &= 0x0F;
  1022 +
  1023 + trace("ts+header sync: %#x error: %d unit_start: %d priotiry: %d pid: %d scrambling: %d adaption: %d counter: %d",
  1024 + sync_byte, transport_error_indicator, payload_unit_start_indicator, transport_priority, pid,
  1025 + transport_scrambling_control, adaption_field_control, continuity_counter);
  1026 +
  1027 + return ret;
  1028 +}
  1029 +
  1030 +int main(int /*argc*/, char** /*argv*/)
  1031 +{
  1032 + const char* file = "livestream-1347.ts";
  1033 + //file = "nginx-rtmp-hls/livestream-1347-currupt.ts";
  1034 + int fd = open(file, O_RDONLY);
  1035 +
  1036 + int ret = 0;
  1037 + trace("demuxer+read packet count offset T+0 T+1 T+2 T+3 T+x T+L2 T+L1 T+L0");
  1038 +
  1039 + TSContext ctx;
  1040 + for (int i = 0, offset = 0; ; i++) {
  1041 + u_int8_t ts_packet[TS_PACKET_SIZE];
  1042 + memset(ts_packet, 0, sizeof(ts_packet));
  1043 +
  1044 + int nread = read(fd, ts_packet, sizeof(ts_packet));
  1045 + if (nread == 0) {
  1046 + trace("demuxer+read got EOF, read completed, offset: %07d.", offset);
  1047 + break;
  1048 + }
  1049 + if (nread != TS_PACKET_SIZE) {
  1050 + trace("demuxer+read error to read ts packet. nread=%d", nread);
  1051 + break;
  1052 + }
  1053 + trace("demuxer+read packet %04d %07d 0x%02x 0x%02x 0x%02x 0x%02x ... 0x%02x 0x%02x 0x%02x",
  1054 + i, offset, ts_packet[0], ts_packet[1], ts_packet[2], ts_packet[3],
  1055 + ts_packet[TS_PACKET_SIZE - 3], ts_packet[TS_PACKET_SIZE - 2], ts_packet[TS_PACKET_SIZE - 1]);
  1056 +
  1057 + u_int8_t* p = ts_packet;
  1058 + u_int8_t* start = ts_packet;
  1059 + u_int8_t* last = ts_packet + TS_PACKET_SIZE;
  1060 +
  1061 + TSPacket pkt;
  1062 + if ((ret = pkt.demux(&ctx, start, last, p)) != 0) {
  1063 + trace("demuxer+read decode ts packet error. ret=%d", ret);
  1064 + return ret;
  1065 + }
  1066 +
  1067 + offset += nread;
  1068 + }
  1069 +
  1070 + close(fd);
  1071 + return ret;
  1072 +}
  1073 +
@@ -41,7 +41,9 @@ file @@ -41,7 +41,9 @@ file
41 ..\core\srs_core_pithy_print.hpp, 41 ..\core\srs_core_pithy_print.hpp,
42 ..\core\srs_core_pithy_print.cpp, 42 ..\core\srs_core_pithy_print.cpp,
43 ..\core\srs_core_log.hpp, 43 ..\core\srs_core_log.hpp,
44 - ..\core\srs_core_log.cpp; 44 + ..\core\srs_core_log.cpp,
  45 + research readonly separator,
  46 + ..\..\research\ts_info.cpp;
45 mainconfig 47 mainconfig
46 "" = "MAIN"; 48 "" = "MAIN";
47 49