winlin

for #250, parse the PES ts packet payload.

@@ -408,6 +408,12 @@ SrsTsContext::SrsTsContext() @@ -408,6 +408,12 @@ SrsTsContext::SrsTsContext()
408 408
409 SrsTsContext::~SrsTsContext() 409 SrsTsContext::~SrsTsContext()
410 { 410 {
  411 + std::map<int, SrsTsChannel*>::iterator it;
  412 + for (it = pids.begin(); it != pids.end(); ++it) {
  413 + SrsTsChannel* channel = it->second;
  414 + srs_freep(channel);
  415 + }
  416 + pids.clear();
411 } 417 }
412 418
413 int SrsTsContext::decode(SrsStream* stream) 419 int SrsTsContext::decode(SrsStream* stream)
@@ -429,17 +435,28 @@ int SrsTsContext::decode(SrsStream* stream) @@ -429,17 +435,28 @@ int SrsTsContext::decode(SrsStream* stream)
429 return ret; 435 return ret;
430 } 436 }
431 437
432 -SrsTsPidApply SrsTsContext::get(int pid) 438 +SrsTsChannel* SrsTsContext::get(int pid)
433 { 439 {
434 if (pids.find(pid) == pids.end()) { 440 if (pids.find(pid) == pids.end()) {
435 - return SrsTsPidApplyReserved; 441 + return NULL;
436 } 442 }
437 return pids[pid]; 443 return pids[pid];
438 } 444 }
439 445
440 -void SrsTsContext::set(int pid, SrsTsPidApply apply_pid) 446 +void SrsTsContext::set(int pid, SrsTsPidApply apply_pid, SrsTsStream stream)
441 { 447 {
442 - pids[pid] = apply_pid; 448 + SrsTsChannel* channel = NULL;
  449 +
  450 + if (pids.find(pid) == pids.end()) {
  451 + channel = new SrsTsChannel();
  452 + pids[pid] = channel;
  453 + } else {
  454 + channel = pids[pid];
  455 + }
  456 +
  457 + channel->pid = pid;
  458 + channel->apply = apply_pid;
  459 + channel->stream = stream;
443 } 460 }
444 461
445 SrsTsPacket::SrsTsPacket(SrsTsContext* c) 462 SrsTsPacket::SrsTsPacket(SrsTsContext* c)
@@ -523,11 +540,15 @@ int SrsTsPacket::decode(SrsStream* stream) @@ -523,11 +540,15 @@ int SrsTsPacket::decode(SrsStream* stream)
523 srs_freep(payload); 540 srs_freep(payload);
524 payload = new SrsTsPayloadPAT(this); 541 payload = new SrsTsPayloadPAT(this);
525 } else { 542 } else {
526 - SrsTsPidApply apply_pid = context->get(pid);  
527 - if (apply_pid == SrsTsPidApplyPMT) { 543 + SrsTsChannel* channel = context->get(pid);
  544 + if (channel && channel->apply == SrsTsPidApplyPMT) {
528 // 2.4.4.8 Program Map Table 545 // 2.4.4.8 Program Map Table
529 srs_freep(payload); 546 srs_freep(payload);
530 payload = new SrsTsPayloadPMT(this); 547 payload = new SrsTsPayloadPMT(this);
  548 + } else if (channel && (channel->apply == SrsTsPidApplyVideo || channel->apply == SrsTsPidApplyAudio)) {
  549 + // 2.4.3.6 PES packet
  550 + srs_freep(payload);
  551 + payload = new SrsTsPayloadPES(this);
531 } else { 552 } else {
532 // left bytes as reserved. 553 // left bytes as reserved.
533 stream->skip(nb_payload); 554 stream->skip(nb_payload);
@@ -793,6 +814,31 @@ SrsTsPayload::~SrsTsPayload() @@ -793,6 +814,31 @@ SrsTsPayload::~SrsTsPayload()
793 { 814 {
794 } 815 }
795 816
  817 +SrsTsPayloadPES::SrsTsPayloadPES(SrsTsPacket* p) : SrsTsPayload(p)
  818 +{
  819 + PES_private_data = NULL;
  820 + pack_field = NULL;
  821 + PES_extension_field = NULL;
  822 + nb_stuffings = 0;
  823 + nb_bytes = 0;
  824 + bytes = NULL;
  825 + nb_paddings = 0;
  826 +}
  827 +
  828 +SrsTsPayloadPES::~SrsTsPayloadPES()
  829 +{
  830 + srs_freep(PES_private_data);
  831 + srs_freep(pack_field);
  832 + srs_freep(PES_extension_field);
  833 + srs_freep(bytes);
  834 +}
  835 +
  836 +int SrsTsPayloadPES::decode(SrsStream* stream)
  837 +{
  838 + int ret = ERROR_SUCCESS;
  839 + return ret;
  840 +}
  841 +
796 SrsTsPayloadPSI::SrsTsPayloadPSI(SrsTsPacket* p) : SrsTsPayload(p) 842 SrsTsPayloadPSI::SrsTsPayloadPSI(SrsTsPacket* p) : SrsTsPayload(p)
797 { 843 {
798 pointer_field = 0; 844 pointer_field = 0;
@@ -879,7 +925,19 @@ int SrsTsPayloadPSI::decode(SrsStream* stream) @@ -879,7 +925,19 @@ int SrsTsPayloadPSI::decode(SrsStream* stream)
879 925
880 // consume left stuffings 926 // consume left stuffings
881 if (!stream->empty()) { 927 if (!stream->empty()) {
882 - stream->skip(stream->size() - stream->pos()); 928 + int nb_stuffings = stream->size() - stream->pos();
  929 + char* stuffing = stream->data() + stream->pos();
  930 +
  931 + // all stuffing must be 0xff.
  932 + // TODO: FIXME: maybe need to remove the following.
  933 + for (int i = 0; i < nb_stuffings; i++) {
  934 + if ((u_int8_t)stuffing[i] != 0xff) {
  935 + srs_warn("ts: stuff is not 0xff, actual=%#x", stuffing[i]);
  936 + break;
  937 + }
  938 + }
  939 +
  940 + stream->skip(nb_stuffings);
883 } 941 }
884 942
885 return ret; 943 return ret;
@@ -1049,7 +1107,7 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream) @@ -1049,7 +1107,7 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream)
1049 return ret; 1107 return ret;
1050 } 1108 }
1051 1109
1052 - info->stream_type = stream->read_1bytes(); 1110 + info->stream_type = (SrsTsStream)stream->read_1bytes();
1053 info->elementary_PID = stream->read_2bytes(); 1111 info->elementary_PID = stream->read_2bytes();
1054 info->ES_info_length = stream->read_2bytes(); 1112 info->ES_info_length = stream->read_2bytes();
1055 1113
@@ -1066,6 +1124,23 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream) @@ -1066,6 +1124,23 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream)
1066 info->ES_info = new char[info->ES_info_length]; 1124 info->ES_info = new char[info->ES_info_length];
1067 stream->read_bytes(info->ES_info, info->ES_info_length); 1125 stream->read_bytes(info->ES_info, info->ES_info_length);
1068 } 1126 }
  1127 +
  1128 + // update the apply pid table
  1129 + switch (info->stream_type) {
  1130 + case SrsTsStreamVideoH264:
  1131 + case SrsTsStreamVideoMpeg4:
  1132 + packet->context->set(info->elementary_PID, SrsTsPidApplyVideo, info->stream_type);
  1133 + break;
  1134 + case SrsTsStreamAudioAAC:
  1135 + case SrsTsStreamAudioAC3:
  1136 + case SrsTsStreamAudioDTS:
  1137 + case SrsTsStreamAudioMp3:
  1138 + packet->context->set(info->elementary_PID, SrsTsPidApplyAudio, info->stream_type);
  1139 + break;
  1140 + default:
  1141 + srs_warn("ts: drop pid=%#x, stream=%#x", info->elementary_PID, info->stream_type);
  1142 + break;
  1143 + }
1069 } 1144 }
1070 1145
1071 // update the apply pid table. 1146 // update the apply pid table.
@@ -65,8 +65,8 @@ public: @@ -65,8 +65,8 @@ public:
65 65
66 /** 66 /**
67 * the pid of ts packet, 67 * the pid of ts packet,
68 -* Table 2-3 ¨C PID table, hls-mpeg-ts-iso13818-1.pdf, page 37  
69 -* NOTE ¨C The transport packets with PID values 0x0000, 0x0001, and 0x0010-0x1FFE are allowed to carry a PCR. 68 +* Table 2-3 - PID table, hls-mpeg-ts-iso13818-1.pdf, page 37
  69 +* NOTE - The transport packets with PID values 0x0000, 0x0001, and 0x0010-0x1FFE are allowed to carry a PCR.
70 */ 70 */
71 enum SrsTsPid 71 enum SrsTsPid
72 { 72 {
@@ -88,7 +88,7 @@ enum SrsTsPid @@ -88,7 +88,7 @@ enum SrsTsPid
88 88
89 /** 89 /**
90 * the transport_scrambling_control of ts packet, 90 * the transport_scrambling_control of ts packet,
91 -* Table 2-4 ¨C Scrambling control values, hls-mpeg-ts-iso13818-1.pdf, page 38 91 +* Table 2-4 - Scrambling control values, hls-mpeg-ts-iso13818-1.pdf, page 38
92 */ 92 */
93 enum SrsTsScrambled 93 enum SrsTsScrambled
94 { 94 {
@@ -104,7 +104,7 @@ enum SrsTsScrambled @@ -104,7 +104,7 @@ enum SrsTsScrambled
104 104
105 /** 105 /**
106 * the adaption_field_control of ts packet, 106 * the adaption_field_control of ts packet,
107 -* Table 2-5 ¨C Adaptation field control values, hls-mpeg-ts-iso13818-1.pdf, page 38 107 +* Table 2-5 - Adaptation field control values, hls-mpeg-ts-iso13818-1.pdf, page 38
108 */ 108 */
109 enum SrsTsAdaptationFieldType 109 enum SrsTsAdaptationFieldType
110 { 110 {
@@ -134,12 +134,61 @@ enum SrsTsPidApply @@ -134,12 +134,61 @@ enum SrsTsPidApply
134 }; 134 };
135 135
136 /** 136 /**
  137 +* Table 2-29 - Stream type assignments
  138 +*/
  139 +enum SrsTsStream
  140 +{
  141 + // ITU-T | ISO/IEC Reserved
  142 + SrsTsStreamReserved = 0x00,
  143 + // ISO/IEC 11172 Video
  144 + // ITU-T Rec. H.262 | ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream
  145 + // ISO/IEC 11172 Audio
  146 + // ISO/IEC 13818-3 Audio
  147 + SrsTsStreamAudioMp3 = 0x04,
  148 + // ITU-T Rec. H.222.0 | ISO/IEC 13818-1 private_sections
  149 + // ITU-T Rec. H.222.0 | ISO/IEC 13818-1 PES packets containing private data
  150 + // ISO/IEC 13522 MHEG
  151 + // ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Annex A DSM-CC
  152 + // ITU-T Rec. H.222.1
  153 + // ISO/IEC 13818-6 type A
  154 + // ISO/IEC 13818-6 type B
  155 + // ISO/IEC 13818-6 type C
  156 + // ISO/IEC 13818-6 type D
  157 + // ITU-T Rec. H.222.0 | ISO/IEC 13818-1 auxiliary
  158 + // ISO/IEC 13818-7 Audio with ADTS transport syntax
  159 + SrsTsStreamAudioAAC = 0x0f,
  160 + // ISO/IEC 14496-2 Visual
  161 + SrsTsStreamVideoMpeg4 = 0x10,
  162 + // ISO/IEC 14496-3 Audio with the LATM transport syntax as defined in ISO/IEC 14496-3 / AMD 1
  163 + // ISO/IEC 14496-1 SL-packetized stream or FlexMux stream carried in PES packets
  164 + // ISO/IEC 14496-1 SL-packetized stream or FlexMux stream carried in ISO/IEC14496_sections.
  165 + // ISO/IEC 13818-6 Synchronized Download Protocol
  166 + // ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Reserved
  167 + // 0x15-0x7F
  168 + SrsTsStreamVideoH264 = 0x1b,
  169 + // User Private
  170 + // 0x80-0xFF
  171 + SrsTsStreamAudioAC3 = 0x81,
  172 + SrsTsStreamAudioDTS = 0x8a,
  173 +};
  174 +
  175 +/**
  176 +* the ts channel.
  177 +*/
  178 +struct SrsTsChannel
  179 +{
  180 + int pid;
  181 + SrsTsPidApply apply;
  182 + SrsTsStream stream;
  183 +};
  184 +
  185 +/**
137 * the context of ts, to decode the ts stream. 186 * the context of ts, to decode the ts stream.
138 */ 187 */
139 class SrsTsContext 188 class SrsTsContext
140 { 189 {
141 private: 190 private:
142 - std::map<int, SrsTsPidApply> pids; 191 + std::map<int, SrsTsChannel*> pids;
143 public: 192 public:
144 SrsTsContext(); 193 SrsTsContext();
145 virtual ~SrsTsContext(); 194 virtual ~SrsTsContext();
@@ -152,13 +201,13 @@ public: @@ -152,13 +201,13 @@ public:
152 public: 201 public:
153 /** 202 /**
154 * get the pid apply, the parsed pid. 203 * get the pid apply, the parsed pid.
155 - * @return the apply pid; SrsTsPidApplyReserved for invalid. 204 + * @return the apply channel; NULL for invalid.
156 */ 205 */
157 - virtual SrsTsPidApply get(int pid); 206 + virtual SrsTsChannel* get(int pid);
158 /** 207 /**
159 * set the pid apply, the parsed pid. 208 * set the pid apply, the parsed pid.
160 */ 209 */
161 - virtual void set(int pid, SrsTsPidApply apply_pid); 210 + virtual void set(int pid, SrsTsPidApply apply_pid, SrsTsStream stream = SrsTsStreamReserved);
162 }; 211 };
163 212
164 /** 213 /**
@@ -216,7 +265,7 @@ public: @@ -216,7 +265,7 @@ public:
216 /** 265 /**
217 * The PID is a 13-bit field, indicating the type of the data stored in the packet payload. PID value 0x0000 is 266 * The PID is a 13-bit field, indicating the type of the data stored in the packet payload. PID value 0x0000 is
218 * reserved for the Program Association Table (see Table 2-25). PID value 0x0001 is reserved for the Conditional Access 267 * reserved for the Program Association Table (see Table 2-25). PID value 0x0001 is reserved for the Conditional Access
219 - * Table (see Table 2-27). PID values 0x0002 ¨C 0x000F are reserved. PID value 0x1FFF is reserved for null packets (see 268 + * Table (see Table 2-27). PID values 0x0002 - 0x000F are reserved. PID value 0x1FFF is reserved for null packets (see
220 * Table 2-3). 269 * Table 2-3).
221 */ 270 */
222 SrsTsPid pid; //13bits 271 SrsTsPid pid; //13bits
@@ -269,7 +318,7 @@ public: @@ -269,7 +318,7 @@ public:
269 /** 318 /**
270 * the adaption field of ts packet. 319 * the adaption field of ts packet.
271 * 2.4.3.5 Semantic definition of fields in adaptation field, hls-mpeg-ts-iso13818-1.pdf, page 39 320 * 2.4.3.5 Semantic definition of fields in adaptation field, hls-mpeg-ts-iso13818-1.pdf, page 39
272 -* Table 2-6 ¨C Transport Stream adaptation field, hls-mpeg-ts-iso13818-1.pdf, page 40 321 +* Table 2-6 - Transport Stream adaptation field, hls-mpeg-ts-iso13818-1.pdf, page 40
273 */ 322 */
274 class SrsTsAdaptationField 323 class SrsTsAdaptationField
275 { 324 {
@@ -329,8 +378,8 @@ public: @@ -329,8 +378,8 @@ public:
329 * same PID. 378 * same PID.
330 * 379 *
331 * For the purpose of this clause, an elementary stream access point is defined as follows: 380 * For the purpose of this clause, an elementary stream access point is defined as follows:
332 - * Video ¨C The first byte of a video sequence header.  
333 - * Audio ¨C The first byte of an audio frame. 381 + * Video - The first byte of a video sequence header.
  382 + * Audio - The first byte of an audio frame.
334 * 383 *
335 * After a continuity counter discontinuity in a Transport packet which is designated as containing elementary stream data, 384 * After a continuity counter discontinuity in a Transport packet which is designated as containing elementary stream data,
336 * the first byte of elementary stream data in a Transport Stream packet of the same PID shall be the first byte of an 385 * the first byte of elementary stream data in a Transport Stream packet of the same PID shall be the first byte of an
@@ -454,8 +503,8 @@ public: @@ -454,8 +503,8 @@ public:
454 * being excluded). 503 * being excluded).
455 * 504 *
456 * For the purposes of this subclause, an access point is defined as follows: 505 * For the purposes of this subclause, an access point is defined as follows:
457 - * Video ¨C The first byte of a video_sequence_header.  
458 - * Audio ¨C The first byte of an audio frame. 506 + * Video - The first byte of a video_sequence_header.
  507 + * Audio - The first byte of an audio frame.
459 */ 508 */
460 int8_t splice_countdown; //8bits 509 int8_t splice_countdown; //8bits
461 510
@@ -497,15 +546,15 @@ public: @@ -497,15 +546,15 @@ public:
497 //5bits reserved 546 //5bits reserved
498 // if ltw_flag, 2B 547 // if ltw_flag, 2B
499 /** 548 /**
500 - * (legal time window_valid_flag) ¨C This is a 1-bit field which when set to '1' indicates that the value of the 549 + * (legal time window_valid_flag) - This is a 1-bit field which when set to '1' indicates that the value of the
501 * ltw_offset shall be valid. A value of '0' indicates that the value in the ltw_offset field is undefined. 550 * ltw_offset shall be valid. A value of '0' indicates that the value in the ltw_offset field is undefined.
502 */ 551 */
503 int8_t ltw_valid_flag; //1bit 552 int8_t ltw_valid_flag; //1bit
504 /** 553 /**
505 - * (legal time window offset) ¨C This is a 15-bit field, the value of which is defined only if the ltw_valid flag has 554 + * (legal time window offset) - This is a 15-bit field, the value of which is defined only if the ltw_valid flag has
506 * a value of '1'. When defined, the legal time window offset is in units of (300/fs) seconds, where fs is the system clock 555 * a value of '1'. When defined, the legal time window offset is in units of (300/fs) seconds, where fs is the system clock
507 * frequency of the program that this PID belongs to, and fulfils: 556 * frequency of the program that this PID belongs to, and fulfils:
508 - * offset = t1(i) ¨C t(i) 557 + * offset = t1(i) - t(i)
509 * ltw_offset = offset//1 558 * ltw_offset = offset//1
510 * where i is the index of the first byte of this Transport Stream packet, offset is the value encoded in this field, t(i) is the 559 * where i is the index of the first byte of this Transport Stream packet, offset is the value encoded in this field, t(i) is the
511 * arrival time of byte i in the T-STD, and t1(i) is the upper bound in time of a time interval called the Legal Time Window 560 * arrival time of byte i in the T-STD, and t1(i) is the upper bound in time of a time interval called the Legal Time Window
@@ -532,7 +581,7 @@ public: @@ -532,7 +581,7 @@ public:
532 */ 581 */
533 int8_t splice_type; //4bits 582 int8_t splice_type; //4bits
534 /** 583 /**
535 - * (decoding time stamp next access unit) ¨C This is a 33-bit field, coded in three parts. In the case of 584 + * (decoding time stamp next access unit) - This is a 33-bit field, coded in three parts. In the case of
536 * continuous and periodic decoding through this splicing point it indicates the decoding time of the first access unit 585 * continuous and periodic decoding through this splicing point it indicates the decoding time of the first access unit
537 * following the splicing point. This decoding time is expressed in the time base which is valid in the Transport Stream 586 * following the splicing point. This decoding time is expressed in the time base which is valid in the Transport Stream
538 * packet in which the splice_countdown reaches zero. From the first occurrence of this field onwards, it shall have the 587 * packet in which the splice_countdown reaches zero. From the first occurrence of this field onwards, it shall have the
@@ -613,6 +662,312 @@ public: @@ -613,6 +662,312 @@ public:
613 }; 662 };
614 663
615 /** 664 /**
  665 +* the PES payload of ts packet.
  666 +* 2.4.3.6 PES packet, hls-mpeg-ts-iso13818-1.pdf, page 49
  667 +*/
  668 +class SrsTsPayloadPES : public SrsTsPayload
  669 +{
  670 +public:
  671 + // 3B
  672 + /**
  673 + * The packet_start_code_prefix is a 24-bit code. Together with the stream_id that follows it
  674 + * constitutes a packet start code that identifies the beginning of a packet. The packet_start_code_prefix is the bit string
  675 + * '0000 0000 0000 0000 0000 0001' (0x000001).
  676 + */
  677 + int32_t packet_start_code_prefix; //24bits
  678 + // 1B
  679 + /**
  680 + * In Program Streams, the stream_id specifies the type and number of the elementary stream as defined by the
  681 + * stream_id Table 2-18. In Transport Streams, the stream_id may be set to any valid value which correctly describes the
  682 + * elementary stream type as defined in Table 2-18. In Transport Streams, the elementary stream type is specified in the
  683 + * Program Specific Information as specified in 2.4.4.
  684 + */
  685 + u_int8_t stream_id; //8bits
  686 + // 2B
  687 + /**
  688 + * A 16-bit field specifying the number of bytes in the PES packet following the last byte of the
  689 + * field. A value of 0 indicates that the PES packet length is neither specified nor bounded and is allowed only in
  690 + * PES packets whose payload consists of bytes from a video elementary stream contained in Transport Stream packets.
  691 + */
  692 + u_int16_t PES_packet_length; //16bits
  693 +
  694 + // 1B
  695 + // 2bits const '10'
  696 + /**
  697 + * The 2-bit PES_scrambling_control field indicates the scrambling mode of the PES packet
  698 + * payload. When scrambling is performed at the PES level, the PES packet header, including the optional fields when
  699 + * present, shall not be scrambled (see Table 2-19).
  700 + */
  701 + int8_t PES_scrambling_control; //2bits
  702 + /**
  703 + * This is a 1-bit field indicating the priority of the payload in this PES packet. A '1' indicates a higher
  704 + * priority of the payload of the PES packet payload than a PES packet payload with this field set to '0'. A multiplexor can
  705 + * use the PES_priority bit to prioritize its data within an elementary stream. This field shall not be changed by the transport
  706 + * mechanism.
  707 + */
  708 + int8_t PES_priority; //1bit
  709 + /**
  710 + * This is a 1-bit flag. When set to a value of '1' it indicates that the PES packet header is
  711 + * immediately followed by the video start code or audio syncword indicated in the data_stream_alignment_descriptor
  712 + * in 2.6.10 if this descriptor is present. If set to a value of '1' and the descriptor is not present, alignment as indicated in
  713 + * alignment_type '01' in Table 2-47 and Table 2-48 is required. When set to a value of '0' it is not defined whether any such
  714 + * alignment occurs or not.
  715 + */
  716 + int8_t data_alignment_indicator; //1bit
  717 + /**
  718 + * This is a 1-bit field. When set to '1' it indicates that the material of the associated PES packet payload is
  719 + * protected by copyright. When set to '0' it is not defined whether the material is protected by copyright. A copyright
  720 + * descriptor described in 2.6.24 is associated with the elementary stream which contains this PES packet and the copyright
  721 + * flag is set to '1' if the descriptor applies to the material contained in this PES packet
  722 + */
  723 + int8_t copyright; //1bit
  724 + /**
  725 + * This is a 1-bit field. When set to '1' the contents of the associated PES packet payload is an original.
  726 + * When set to '0' it indicates that the contents of the associated PES packet payload is a copy.
  727 + */
  728 + int8_t original_or_copy; //1bit
  729 +
  730 + // 1B
  731 + /**
  732 + * This is a 2-bit field. When the PTS_DTS_flags field is set to '10', the PTS fields shall be present in
  733 + * the PES packet header. When the PTS_DTS_flags field is set to '11', both the PTS fields and DTS fields shall be present
  734 + * in the PES packet header. When the PTS_DTS_flags field is set to '00' no PTS or DTS fields shall be present in the PES
  735 + * packet header. The value '01' is forbidden.
  736 + */
  737 + int8_t PTS_DTS_flags; //2bits
  738 + /**
  739 + * A 1-bit flag, which when set to '1' indicates that ESCR base and extension fields are present in the PES
  740 + * packet header. When set to '0' it indicates that no ESCR fields are present.
  741 + */
  742 + int8_t ESCR_flag; //1bit
  743 + /**
  744 + * A 1-bit flag, which when set to '1' indicates that the ES_rate field is present in the PES packet header.
  745 + * When set to '0' it indicates that no ES_rate field is present.
  746 + */
  747 + int8_t ES_rate_flag; //1bit
  748 + /**
  749 + * A 1-bit flag, which when set to '1' it indicates the presence of an 8-bit trick mode field. When
  750 + * set to '0' it indicates that this field is not present.
  751 + */
  752 + int8_t DSM_trick_mode_flag; //1bit
  753 + /**
  754 + * A 1-bit flag, which when set to '1' indicates the presence of the additional_copy_info field.
  755 + * When set to '0' it indicates that this field is not present.
  756 + */
  757 + int8_t additional_copy_info_flag; //1bit
  758 + /**
  759 + * A 1-bit flag, which when set to '1' indicates that a CRC field is present in the PES packet. When set to
  760 + * '0' it indicates that this field is not present.
  761 + */
  762 + int8_t PES_CRC_flag; //1bit
  763 + /**
  764 + * A 1-bit flag, which when set to '1' indicates that an extension field exists in this PES packet
  765 + * header. When set to '0' it indicates that this field is not present.
  766 + */
  767 + int8_t PES_extension_flag; //1bit
  768 +
  769 + // 1B
  770 + /**
  771 + * An 8-bit field specifying the total number of bytes occupied by the optional fields and any
  772 + * stuffing bytes contained in this PES packet header. The presence of optional fields is indicated in the byte that precedes
  773 + * the PES_header_data_length field.
  774 + */
  775 + u_int8_t PES_header_data_length; //8bits
  776 +
  777 + // 8B
  778 + /**
  779 + * Presentation times shall be related to decoding times as follows: The PTS is a 33-bit
  780 + * number coded in three separate fields. It indicates the time of presentation, tp n (k), in the system target decoder of a
  781 + * presentation unit k of elementary stream n. The value of PTS is specified in units of the period of the system clock
  782 + * frequency divided by 300 (yielding 90 kHz). The presentation time is derived from the PTS according to equation 2-11
  783 + * below. Refer to 2.7.4 for constraints on the frequency of coding presentation timestamps.
  784 + */
  785 + int64_t pts; // 33bits
  786 + /**
  787 + * The DTS is a 33-bit number coded in three separate fields. It indicates the decoding time,
  788 + * td n (j), in the system target decoder of an access unit j of elementary stream n. The value of DTS is specified in units of
  789 + * the period of the system clock frequency divided by 300 (yielding 90 kHz).
  790 + */
  791 + int64_t dts; // 33bits
  792 +
  793 + // 8B
  794 + /**
  795 + * The elementary stream clock reference is a 42-bit field coded in two parts. The first
  796 + * part, ESCR_base, is a 33-bit field whose value is given by ESCR_base(i), as given in equation 2-14. The second part,
  797 + * ESCR_ext, is a 9-bit field whose value is given by ESCR_ext(i), as given in equation 2-15. The ESCR field indicates the
  798 + * intended time of arrival of the byte containing the last bit of the ESCR_base at the input of the PES-STD for PES streams
  799 + * (refer to 2.5.2.4).
  800 + */
  801 + int16_t ESCR_extension; //9bits
  802 + int64_t ESCR_base; //33bits
  803 + /**
  804 + * The ES_rate field is a 22-bit unsigned integer specifying the rate at which the
  805 + * system target decoder receives bytes of the PES packet in the case of a PES stream. The ES_rate is valid in the PES
  806 + * packet in which it is included and in subsequent PES packets of the same PES stream until a new ES_rate field is
  807 + * encountered. The value of the ES_rate is measured in units of 50 bytes/second. The value 0 is forbidden. The value of the
  808 + * ES_rate is used to define the time of arrival of bytes at the input of a P-STD for PES streams defined in 2.5.2.4. The
  809 + * value encoded in the ES_rate field may vary from PES_packet to PES_packet.
  810 + */
  811 + int32_t ES_rate; //22bits
  812 +
  813 + // 1B
  814 + /**
  815 + * A 3-bit field that indicates which trick mode is applied to the associated video stream. In cases of
  816 + * other types of elementary streams, the meanings of this field and those defined by the following five bits are undefined.
  817 + * For the definition of trick_mode status, refer to the trick mode section of 2.4.2.3.
  818 + */
  819 + int8_t trick_mode_control; //3bits
  820 + int8_t trick_mode_value; //5bits
  821 +
  822 + // 1B
  823 + // 1bit const '1'
  824 + /**
  825 + * This 7-bit field contains private data relating to copyright information.
  826 + */
  827 + int8_t additional_copy_info; //7bits
  828 +
  829 + // 2B
  830 + /**
  831 + * The previous_PES_packet_CRC is a 16-bit field that contains the CRC value that yields
  832 + * a zero output of the 16 registers in the decoder similar to the one defined in Annex A,
  833 + */
  834 + int16_t previous_PES_packet_CRC; //16bits
  835 +
  836 + // 1B
  837 + /**
  838 + * A 1-bit flag which when set to '1' indicates that the PES packet header contains private data.
  839 + * When set to a value of '0' it indicates that private data is not present in the PES header.
  840 + */
  841 + int8_t PES_private_data_flag; //1bit
  842 + /**
  843 + * A 1-bit flag which when set to '1' indicates that an ISO/IEC 11172-1 pack header or a
  844 + * Program Stream pack header is stored in this PES packet header. If this field is in a PES packet that is contained in a
  845 + * Program Stream, then this field shall be set to '0'. In a Transport Stream, when set to the value '0' it indicates that no pack
  846 + * header is present in the PES header.
  847 + */
  848 + int8_t pack_header_field_flag; //1bit
  849 + /**
  850 + * A 1-bit flag which when set to '1' indicates that the
  851 + * program_packet_sequence_counter, MPEG1_MPEG2_identifier, and original_stuff_length fields are present in this
  852 + * PES packet. When set to a value of '0' it indicates that these fields are not present in the PES header.
  853 + */
  854 + int8_t program_packet_sequence_counter_flag; //1bit
  855 + /**
  856 + * A 1-bit flag which when set to '1' indicates that the P-STD_buffer_scale and P-STD_buffer_size
  857 + * are present in the PES packet header. When set to a value of '0' it indicates that these fields are not present in the
  858 + * PES header.
  859 + */
  860 + int8_t P_STD_buffer_flag; //1bit
  861 + // reserved 3bits
  862 + /**
  863 + * A 1-bit field which when set to '1' indicates the presence of the PES_extension_field_length
  864 + * field and associated fields. When set to a value of '0' this indicates that the PES_extension_field_length field and any
  865 + * associated fields are not present.
  866 + */
  867 + int8_t PES_extension_flag_2; //1bit
  868 +
  869 + // 16B
  870 + /**
  871 + * This is a 16-byte field which contains private data. This data, combined with the fields before and
  872 + * after, shall not emulate the packet_start_code_prefix (0x000001).
  873 + */
  874 + char* PES_private_data; //128bits
  875 +
  876 + // (1+x)B
  877 + /**
  878 + * This is an 8-bit field which indicates the length, in bytes, of the pack_header_field().
  879 + */
  880 + int8_t pack_field_length; //8bits
  881 + char* pack_field; //[pack_field_length] bytes
  882 +
  883 + // 2B
  884 + // 1bit const '1'
  885 + /**
  886 + * The program_packet_sequence_counter field is a 7-bit field. It is an optional
  887 + * counter that increments with each successive PES packet from a Program Stream or from an ISO/IEC 11172-1 Stream or
  888 + * the PES packets associated with a single program definition in a Transport Stream, providing functionality similar to a
  889 + * continuity counter (refer to 2.4.3.2). This allows an application to retrieve the original PES packet sequence of a Program
  890 + * Stream or the original packet sequence of the original ISO/IEC 11172-1 stream. The counter will wrap around to 0 after
  891 + * its maximum value. Repetition of PES packets shall not occur. Consequently, no two consecutive PES packets in the
  892 + * program multiplex shall have identical program_packet_sequence_counter values.
  893 + */
  894 + int8_t program_packet_sequence_counter; //7bits
  895 + // 1bit const '1'
  896 + /**
  897 + * A 1-bit flag which when set to '1' indicates that this PES packet carries information from
  898 + * an ISO/IEC 11172-1 stream. When set to '0' it indicates that this PES packet carries information from a Program Stream.
  899 + */
  900 + int8_t MPEG1_MPEG2_identifier; //1bit
  901 + /**
  902 + * This 6-bit field specifies the number of stuffing bytes used in the original ITU-T
  903 + * Rec. H.222.0 | ISO/IEC 13818-1 PES packet header or in the original ISO/IEC 11172-1 packet header.
  904 + */
  905 + int8_t original_stuff_length; //6bits
  906 +
  907 + // 2B
  908 + // 2bits const '01'
  909 + /**
  910 + * The P-STD_buffer_scale is a 1-bit field, the meaning of which is only defined if this PES packet
  911 + * is contained in a Program Stream. It indicates the scaling factor used to interpret the subsequent P-STD_buffer_size field.
  912 + * If the preceding stream_id indicates an audio stream, P-STD_buffer_scale shall have the value '0'. If the preceding
  913 + * stream_id indicates a video stream, P-STD_buffer_scale shall have the value '1'. For all other stream types, the value
  914 + * may be either '1' or '0'.
  915 + */
  916 + int8_t P_STD_buffer_scale; //1bit
  917 + /**
  918 + * The P-STD_buffer_size is a 13-bit unsigned integer, the meaning of which is only defined if this
  919 + * PES packet is contained in a Program Stream. It defines the size of the input buffer, BS n , in the P-STD. If
  920 + * P-STD_buffer_scale has the value '0', then the P-STD_buffer_size measures the buffer size in units of 128 bytes. If
  921 + * P-STD_buffer_scale has the value '1', then the P-STD_buffer_size measures the buffer size in units of 1024 bytes.
  922 + */
  923 + int16_t P_STD_buffer_size; //13bits
  924 +
  925 + // (1+x)B
  926 + // 1bit const '1'
  927 + /**
  928 + * This is a 7-bit field which specifies the length, in bytes, of the data following this field in
  929 + * the PES extension field up to and including any reserved bytes.
  930 + */
  931 + int8_t PES_extension_field_length; //7bits
  932 + char* PES_extension_field; //[PES_extension_field_length] bytes
  933 +
  934 + // NB
  935 + /**
  936 + * This is a fixed 8-bit value equal to '1111 1111' that can be inserted by the encoder, for example to meet
  937 + * the requirements of the channel. It is discarded by the decoder. No more than 32 stuffing bytes shall be present in one
  938 + * PES packet header.
  939 + */
  940 + int nb_stuffings;
  941 +
  942 + // NB
  943 + /**
  944 + * PES_packet_data_bytes shall be contiguous bytes of data from the elementary stream
  945 + * indicated by the packet’s stream_id or PID. When the elementary stream data conforms to ITU-T
  946 + * Rec. H.262 | ISO/IEC 13818-2 or ISO/IEC 13818-3, the PES_packet_data_bytes shall be byte aligned to the bytes of this
  947 + * Recommendation | International Standard. The byte-order of the elementary stream shall be preserved. The number of
  948 + * PES_packet_data_bytes, N, is specified by the PES_packet_length field. N shall be equal to the value indicated in the
  949 + * PES_packet_length minus the number of bytes between the last byte of the PES_packet_length field and the first
  950 + * PES_packet_data_byte.
  951 + *
  952 + * In the case of a private_stream_1, private_stream_2, ECM_stream, or EMM_stream, the contents of the
  953 + * PES_packet_data_byte field are user definable and will not be specified by ITU-T | ISO/IEC in the future.
  954 + */
  955 + int nb_bytes;
  956 + char* bytes;
  957 +
  958 + // NB
  959 + /**
  960 + * This is a fixed 8-bit value equal to '1111 1111'. It is discarded by the decoder.
  961 + */
  962 + int nb_paddings;
  963 +public:
  964 + SrsTsPayloadPES(SrsTsPacket* p);
  965 + virtual ~SrsTsPayloadPES();
  966 +public:
  967 + virtual int decode(SrsStream* stream);
  968 +};
  969 +
  970 +/**
616 * the PSI payload of ts packet. 971 * the PSI payload of ts packet.
617 * 2.4.4 Program specific information, hls-mpeg-ts-iso13818-1.pdf, page 59 972 * 2.4.4 Program specific information, hls-mpeg-ts-iso13818-1.pdf, page 59
618 */ 973 */
@@ -687,7 +1042,7 @@ public: @@ -687,7 +1042,7 @@ public:
687 // reserved 3bits 1042 // reserved 3bits
688 /** 1043 /**
689 * program_map_PID/network_PID 13bits 1044 * program_map_PID/network_PID 13bits
690 - * network_PID ¨C The network_PID is a 13-bit field, which is used only in conjunction with the value of the 1045 + * network_PID - The network_PID is a 13-bit field, which is used only in conjunction with the value of the
691 * program_number set to 0x0000, specifies the PID of the Transport Stream packets which shall contain the Network 1046 * program_number set to 0x0000, specifies the PID of the Transport Stream packets which shall contain the Network
692 * Information Table. The value of the network_PID field is defined by the user, but shall only take values as specified in 1047 * Information Table. The value of the network_PID field is defined by the user, but shall only take values as specified in
693 * Table 2-3. The presence of the network_PID is optional. 1048 * Table 2-3. The presence of the network_PID is optional.
@@ -767,7 +1122,7 @@ public: @@ -767,7 +1122,7 @@ public:
767 * This is an 8-bit field specifying the type of program element carried within the packets with the PID 1122 * This is an 8-bit field specifying the type of program element carried within the packets with the PID
768 * whose value is specified by the elementary_PID. The values of stream_type are specified in Table 2-29. 1123 * whose value is specified by the elementary_PID. The values of stream_type are specified in Table 2-29.
769 */ 1124 */
770 - u_int8_t stream_type; //8bits 1125 + SrsTsStream stream_type; //8bits
771 1126
772 // 2B 1127 // 2B
773 // 3bits reserved 1128 // 3bits reserved