winlin

update the ts_info, parse the PES packet

@@ -440,15 +440,51 @@ public: @@ -440,15 +440,51 @@ public:
440 // 1B 440 // 1B
441 u_int8_t PES_header_data_length; //8bits 441 u_int8_t PES_header_data_length; //8bits
442 442
443 - /**  
444 - * logic data.  
445 - */  
446 - // generated by PTS_DTS_flags  
447 int64_t pts; // 33bits 443 int64_t pts; // 33bits
448 int64_t dts; // 33bits 444 int64_t dts; // 33bits
  445 +
  446 + int16_t ESCR_extension; //9bits
  447 + int64_t ESCR_base; //33bits
  448 + int32_t ES_rate; //22bits
  449 +
  450 + int8_t trick_mode_control; //3bits
  451 + int8_t trick_mode_value; //5bits
  452 +
  453 + int8_t additional_copy_info; //7bits
  454 + int16_t previous_PES_packet_CRC; //16bits
  455 +
  456 + int8_t PES_private_data_flag; //1bit
  457 + int8_t pack_header_field_flag; //1bit
  458 + int8_t program_packet_sequence_counter_flag; //1bit
  459 + int8_t P_STD_buffer_flag; //1bit
  460 + // reserved 3bits
  461 + int8_t PES_extension_flag_2; //1bit
  462 +
  463 + // 16B
  464 + char* PES_private_data; //128bits
  465 +
  466 + int8_t pack_field_length; //8bits
  467 + char* pack_field; //[pack_field_length] bytes
  468 +
  469 + int8_t program_packet_sequence_counter; //7bits
  470 + int8_t MPEG1_MPEG2_identifier; //1bit
  471 + int8_t original_stuff_length; //6bits
  472 +
  473 + int8_t P_STD_buffer_scale; //1bit
  474 + int16_t P_STD_buffer_size; //13bits
  475 +
  476 + int8_t PES_extension_field_length; //7bits
  477 + char* PES_extension_field; //[PES_extension_field_length] bytes
  478 +
  479 + int stuffing_size;
  480 + char* stuffing_byte;
  481 +
  482 + char* PES_packet_data_byte; //[PES_packet_length] bytes
449 483
450 TSPayloadPES(); 484 TSPayloadPES();
451 virtual ~TSPayloadPES(); 485 virtual ~TSPayloadPES();
  486 + int64_t decode_33bits_int(u_int8_t*& p, int64_t& temp);
  487 + int64_t decode_33bits_int(int64_t& temp);
452 int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p); 488 int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p);
453 }; 489 };
454 490
@@ -537,10 +573,38 @@ void TSContext::push(TSPidType type, int16_t pid) @@ -537,10 +573,38 @@ void TSContext::push(TSPidType type, int16_t pid)
537 573
538 TSAdaptionField::TSAdaptionField() 574 TSAdaptionField::TSAdaptionField()
539 { 575 {
  576 + adaption_field_length = 0;
  577 + discontinuity_indicator = 0;
  578 + random_access_indicator = 0;
  579 + elementary_stream_priority_indicator = 0;
  580 + PCR_flag = 0;
  581 + OPCR_flag = 0;
  582 + splicing_point_flag = 0;
  583 + transport_private_data_flag = 0;
  584 + adaptation_field_extension_flag = 0;
  585 + program_clock_reference_base = 0;
  586 + program_clock_reference_extension = 0;
  587 + original_program_clock_reference_base = 0;
  588 + original_program_clock_reference_extension = 0;
  589 + splice_countdown = 0;
  590 + transport_private_data_length = 0;
540 transport_private_data = NULL; 591 transport_private_data = NULL;
  592 + adaptation_field_extension_length = 0;
  593 + ltw_flag = 0;
  594 + piecewise_rate_flag = 0;
  595 + seamless_splice_flag = 0;
  596 + ltw_valid_flag = 0;
  597 + ltw_offset = 0;
  598 + piecewise_rate = 0;
  599 + splice_type = 0;
  600 + DTS_next_AU0 = 0;
  601 + marker_bit0 = 0;
  602 + DTS_next_AU1 = 0;
  603 + marker_bit1 = 0;
  604 + DTS_next_AU2 = 0;
  605 + marker_bit2 = 0;
541 af_ext_reserved = NULL; 606 af_ext_reserved = NULL;
542 af_reserved = NULL; 607 af_reserved = NULL;
543 -  
544 __user_size = 0; 608 __user_size = 0;
545 } 609 }
546 610
@@ -719,7 +783,18 @@ int TSPayloadReserved::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_i @@ -719,7 +783,18 @@ int TSPayloadReserved::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_i
719 783
720 TSPayloadPAT::TSPayloadPAT() 784 TSPayloadPAT::TSPayloadPAT()
721 { 785 {
  786 + table_id = 0;
  787 + section_syntax_indicator = 0;
  788 + const0_value = 0;
  789 + section_length = 0;
  790 + transport_stream_id = 0;
  791 + version_number = 0;
  792 + current_next_indicator = 0;
  793 + section_number = 0;
  794 + last_section_number = 0;
  795 + program_size = 0;
722 programs = NULL; 796 programs = NULL;
  797 + CRC_32 = 0;
723 } 798 }
724 799
725 TSPayloadPAT::~TSPayloadPAT() 800 TSPayloadPAT::~TSPayloadPAT()
@@ -781,7 +856,9 @@ int TSPayloadPAT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t @@ -781,7 +856,9 @@ int TSPayloadPAT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t
781 856
782 TSPMTESInfo::TSPMTESInfo() 857 TSPMTESInfo::TSPMTESInfo()
783 { 858 {
784 - ES_info_length = 0; 859 + stream_type = 0;
  860 + elementary_PID = 0;
  861 + ES_info_length = 0;
785 ES_info = NULL; 862 ES_info = NULL;
786 } 863 }
787 864
@@ -792,8 +869,19 @@ TSPMTESInfo::~TSPMTESInfo() @@ -792,8 +869,19 @@ TSPMTESInfo::~TSPMTESInfo()
792 869
793 TSPayloadPMT::TSPayloadPMT() 870 TSPayloadPMT::TSPayloadPMT()
794 { 871 {
795 - program_info_length = 0;  
796 - program_info_desc = NULL; 872 + table_id = 0;
  873 + section_syntax_indicator = 0;
  874 + const0_value = 0;
  875 + section_length = 0;
  876 + program_number = 0;
  877 + version_number = 0;
  878 + current_next_indicator = 0;
  879 + section_number = 0;
  880 + last_section_number = 0;
  881 + PCR_PID = 0;
  882 + program_info_length = 0;
  883 + program_info_desc = NULL;
  884 + CRC_32 = 0;
797 } 885 }
798 886
799 TSPayloadPMT::~TSPayloadPMT() 887 TSPayloadPMT::~TSPayloadPMT()
@@ -904,11 +992,92 @@ int TSPayloadPMT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t @@ -904,11 +992,92 @@ int TSPayloadPMT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t
904 992
905 TSPayloadPES::TSPayloadPES() 993 TSPayloadPES::TSPayloadPES()
906 { 994 {
907 - pts = dts = 0; 995 + packet_start_code_prefix = 0;
  996 + stream_id = 0;
  997 + PES_packet_length = 0;
  998 + PES_scrambling_control = 0;
  999 + PES_priority = 0;
  1000 + data_alignment_indicator = 0;
  1001 + copyright = 0;
  1002 + original_or_copy = 0;
  1003 + PTS_DTS_flags = 0;
  1004 + ESCR_flag = 0;
  1005 + ES_rate_flag = 0;
  1006 + DSM_trick_mode_flag = 0;
  1007 + additional_copy_info_flag = 0;
  1008 + PES_CRC_flag = 0;
  1009 + PES_extension_flag = 0;
  1010 + PES_header_data_length = 0;
  1011 + pts = 0;
  1012 + dts = 0;
  1013 + ESCR_extension = 0;
  1014 + ESCR_base = 0;
  1015 + ES_rate = 0;
  1016 + trick_mode_control = 0;
  1017 + trick_mode_value = 0;
  1018 + additional_copy_info = 0;
  1019 + previous_PES_packet_CRC = 0;
  1020 + PES_private_data_flag = 0;
  1021 + pack_header_field_flag = 0;
  1022 + program_packet_sequence_counter_flag = 0;
  1023 + P_STD_buffer_flag = 0;
  1024 + PES_extension_flag_2 = 0;
  1025 + PES_private_data = NULL;
  1026 + pack_field_length = 0;
  1027 + pack_field = NULL;
  1028 + program_packet_sequence_counter = 0;
  1029 + MPEG1_MPEG2_identifier = 0;
  1030 + original_stuff_length = 0;
  1031 + P_STD_buffer_scale = 0;
  1032 + P_STD_buffer_size = 0;
  1033 + PES_extension_field_length = 0;
  1034 + PES_extension_field = NULL;
  1035 + stuffing_size = 0;
  1036 + stuffing_byte = NULL;
  1037 + PES_packet_data_byte = NULL;
908 } 1038 }
909 1039
910 TSPayloadPES::~TSPayloadPES() 1040 TSPayloadPES::~TSPayloadPES()
911 { 1041 {
  1042 + srs_freepa(PES_private_data);
  1043 + srs_freepa(pack_field);
  1044 + srs_freepa(PES_extension_field);
  1045 + srs_freepa(stuffing_byte);
  1046 + srs_freepa(PES_packet_data_byte);
  1047 +}
  1048 +
  1049 +int64_t TSPayloadPES::decode_33bits_int(u_int8_t*& p, int64_t& temp)
  1050 +{
  1051 + char* pp = (char*)&temp;
  1052 + pp[4] = *p++;
  1053 + pp[3] = *p++;
  1054 + pp[2] = *p++;
  1055 + pp[1] = *p++;
  1056 + pp[0] = *p++;
  1057 +
  1058 + return decode_33bits_int(temp);
  1059 +}
  1060 +
  1061 +int64_t TSPayloadPES::decode_33bits_int(int64_t& temp)
  1062 +{
  1063 + int64_t ret = 0;
  1064 +
  1065 + // marker_bit 1bit
  1066 + temp = temp >> 1;
  1067 + // PTS [14..0] 15bits
  1068 + ret |= temp & 0x3fff;
  1069 + // marker_bit 1bit
  1070 + temp = temp >> 1;
  1071 + // PTS [29..15] 15bits, 15zero, 29-15+1one
  1072 + ret |= temp & 0x3fff8000;
  1073 + // marker_bit 1bit
  1074 + temp = temp >> 1;
  1075 + // PTS [32..30] 3bits
  1076 + ret |= temp & 0x1c0000000;
  1077 +
  1078 + temp = temp >> 33;
  1079 +
  1080 + return ret;
912 } 1081 }
913 1082
914 int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p) 1083 int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p)
@@ -957,10 +1126,28 @@ int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t @@ -957,10 +1126,28 @@ int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t
957 PES_extension_flag &= 0x01; 1126 PES_extension_flag &= 0x01;
958 1127
959 PES_header_data_length = *p++; 1128 PES_header_data_length = *p++;
  1129 + u_int8_t* pos = p;
960 1130
  1131 + int64_t temp = 0;
961 if (PTS_DTS_flags == 0x2) { 1132 if (PTS_DTS_flags == 0x2) {
962 - int64_t temp = 0; 1133 + pts = decode_33bits_int(p, temp);
  1134 + // '0010' 4bits
  1135 + //int8_t const4bits = temp & 0x0F;
  1136 + }
  1137 +
  1138 + if (PTS_DTS_flags == 0x3) {
  1139 + pts = decode_33bits_int(p, temp);
  1140 + // '0011' 4bits
  1141 + //int8_t const4bits = temp & 0x0F;
  1142 +
  1143 + dts = decode_33bits_int(p, temp);
  1144 + // '0001' 4bits
  1145 + //int8_t const4bits = temp & 0x0F;
  1146 + }
  1147 +
  1148 + if (ESCR_flag) {
963 pp = (char*)&temp; 1149 pp = (char*)&temp;
  1150 + pp[5] = *p++;
964 pp[4] = *p++; 1151 pp[4] = *p++;
965 pp[3] = *p++; 1152 pp[3] = *p++;
966 pp[2] = *p++; 1153 pp[2] = *p++;
@@ -969,70 +1156,111 @@ int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t @@ -969,70 +1156,111 @@ int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t
969 1156
970 // marker_bit 1bit 1157 // marker_bit 1bit
971 temp = temp >> 1; 1158 temp = temp >> 1;
972 - // PTS [14..0] 15bits  
973 - pts |= temp & 0x3FFF;  
974 - // marker_bit 1bit  
975 - temp = temp >> 1;  
976 - // PTS [29..15] 15bits  
977 - pts |= temp & 0x3fff8000;  
978 - // marker_bit 1bit  
979 - temp = temp >> 1;  
980 - // PTS [32..30] 3bits  
981 - pts |= temp & 0x1c0000000;  
982 - // '0010' 4bits  
983 - //int8_t const4bits = (temp >> 32) & 0x0F; 1159 + // ESCR_extension 9bits
  1160 + ESCR_extension = temp & 0x1f;
  1161 + temp = temp >> 9;
  1162 +
  1163 + ESCR_base = decode_33bits_int(temp);
  1164 +
  1165 + // reserved 2bits
  1166 + //int8_t reserved2bits = temp & 0x03;
984 } 1167 }
985 -  
986 - if (PTS_DTS_flags == 0x3) {  
987 - int64_t temp = 0;  
988 -  
989 - pp = (char*)&temp;  
990 - pp[4] = *p++;  
991 - pp[3] = *p++; 1168 +
  1169 + if (ES_rate_flag) {
  1170 + pp = (char*)&ES_rate;
992 pp[2] = *p++; 1171 pp[2] = *p++;
993 pp[1] = *p++; 1172 pp[1] = *p++;
994 pp[0] = *p++; 1173 pp[0] = *p++;
995 1174
996 - // marker_bit 1bit  
997 - temp = temp >> 1;  
998 - // PTS [14..0] 15bits  
999 - pts |= temp & 0x3FFF;  
1000 - // marker_bit 1bit  
1001 - temp = temp >> 1;  
1002 - // PTS [29..15] 15bits  
1003 - pts |= temp & 0x3fff8000;  
1004 - // marker_bit 1bit  
1005 - temp = temp >> 1;  
1006 - // PTS [32..30] 3bits  
1007 - pts |= temp & 0x1c0000000;  
1008 - // '0010' 4bits  
1009 - //int8_t const4bits = (temp >> 32) & 0x0F; 1175 + ES_rate = ES_rate >> 1;
  1176 + ES_rate &= 0x3FFFFF;
  1177 + }
1010 1178
1011 - pp = (char*)&temp;  
1012 - pp[4] = *p++;  
1013 - pp[3] = *p++;  
1014 - pp[2] = *p++; 1179 + if (DSM_trick_mode_flag) {
  1180 + trick_mode_control = *p++;
  1181 +
  1182 + trick_mode_value = trick_mode_control & 0x1f;
  1183 + trick_mode_control = (trick_mode_control >> 5) & 0x03;
  1184 + }
  1185 +
  1186 + if (additional_copy_info_flag) {
  1187 + additional_copy_info = *p++;
  1188 + additional_copy_info &= 0x7f;
  1189 + }
  1190 +
  1191 + if (PES_CRC_flag) {
  1192 + pp = (char*)&previous_PES_packet_CRC;
1015 pp[1] = *p++; 1193 pp[1] = *p++;
1016 pp[0] = *p++; 1194 pp[0] = *p++;
  1195 + }
  1196 +
  1197 + if (PES_extension_flag) {
  1198 + PES_extension_flag_2 = *p++;
1017 1199
1018 - // marker_bit 1bit  
1019 - temp = temp >> 1;  
1020 - // PTS [14..0] 15bits  
1021 - dts |= temp & 0x3FFF;  
1022 - // marker_bit 1bit  
1023 - temp = temp >> 1;  
1024 - // PTS [29..15] 15bits  
1025 - dts |= temp & 0x3fff8000;  
1026 - // marker_bit 1bit  
1027 - temp = temp >> 1;  
1028 - // PTS [32..30] 3bits  
1029 - dts |= temp & 0x1c0000000;  
1030 - // '0010' 4bits  
1031 - //int8_t const4bits = (temp >> 32) & 0x0F; 1200 + PES_private_data_flag = (PES_extension_flag_2 >> 7) & 0x01;
  1201 + pack_header_field_flag = (PES_extension_flag_2 >> 6) & 0x01;
  1202 + program_packet_sequence_counter_flag = (PES_extension_flag_2 >> 5) & 0x01;
  1203 + P_STD_buffer_flag = (PES_extension_flag_2 >> 4) & 0x01;
  1204 + PES_extension_flag_2 &= PES_extension_flag_2 & 0x01;
  1205 +
  1206 + if (PES_private_data_flag) {
  1207 + PES_private_data = new char[16];
  1208 + memcpy(PES_private_data, p, 16);
  1209 + p += 16;
  1210 + }
  1211 +
  1212 + if (pack_header_field_flag) {
  1213 + pack_field_length = *p++;
  1214 + if (pack_field_length > 0) {
  1215 + pack_field = new char[pack_field_length];
  1216 + memcpy(pack_field, p, pack_field_length);
  1217 + p += pack_field_length;
  1218 + }
  1219 + }
  1220 +
  1221 + if (program_packet_sequence_counter_flag) {
  1222 + program_packet_sequence_counter = *p++;
  1223 + program_packet_sequence_counter &= 0x7f;
  1224 +
  1225 + original_stuff_length = *p++;
  1226 + MPEG1_MPEG2_identifier = (original_stuff_length >> 6) & 0x01;
  1227 + original_stuff_length &= 0x3f;
  1228 + }
  1229 +
  1230 + if (P_STD_buffer_flag) {
  1231 + pp = (char*)&P_STD_buffer_size;
  1232 + pp[1] = *p++;
  1233 + pp[0] = *p++;
  1234 +
  1235 + // '01'
  1236 + //int8_t const2bits = (P_STD_buffer_scale >>14) & 0x03;
  1237 +
  1238 + P_STD_buffer_scale = (P_STD_buffer_scale >>13) & 0x01;
  1239 + P_STD_buffer_size &= 0x1FFF;
  1240 + }
  1241 +
  1242 + if (PES_extension_flag_2) {
  1243 + PES_extension_field_length = *p++;
  1244 + PES_extension_field_length &= 0x07;
  1245 +
  1246 + if (PES_extension_field_length > 0) {
  1247 + PES_extension_field = new char[PES_extension_field_length];
  1248 + memcpy(PES_extension_field, p, PES_extension_field_length);
  1249 + p += PES_extension_field_length;
  1250 + }
  1251 + }
1032 } 1252 }
1033 1253
1034 - if (ESCR_flag) { 1254 + // stuffing_byte
  1255 + int stuffing_size = PES_header_data_length - (p - pos);
  1256 + if (stuffing_size > 0) {
  1257 + stuffing_byte = new char[stuffing_size];
  1258 + memcpy(stuffing_byte, p, stuffing_size);
  1259 + p += stuffing_size;
1035 } 1260 }
  1261 +
  1262 + // PES_packet_data_byte
  1263 + PES_packet_data_byte = new char[PES_packet_length];
1036 } else if (stream_id == PES_program_stream_map 1264 } else if (stream_id == PES_program_stream_map
1037 || stream_id == PES_private_stream_2 1265 || stream_id == PES_private_stream_2
1038 || stream_id == PES_ECM_stream 1266 || stream_id == PES_ECM_stream
@@ -1064,12 +1292,13 @@ TSPayload::TSPayload() @@ -1064,12 +1292,13 @@ TSPayload::TSPayload()
1064 { 1292 {
1065 size = 0; 1293 size = 0;
1066 pointer_field_size = 0; 1294 pointer_field_size = 0;
1067 -  
1068 type = TSPidTypeReserved; 1295 type = TSPidTypeReserved;
  1296 + pointer_field = 0;
1069 reserved = NULL; 1297 reserved = NULL;
1070 pat = NULL; 1298 pat = NULL;
1071 pmt = NULL; 1299 pmt = NULL;
1072 pes = NULL; 1300 pes = NULL;
  1301 +
1073 } 1302 }
1074 1303
1075 TSPayload::~TSPayload() 1304 TSPayload::~TSPayload()
@@ -1179,6 +1408,14 @@ int TSPacket::finish() @@ -1179,6 +1408,14 @@ int TSPacket::finish()
1179 1408
1180 TSHeader::TSHeader() 1409 TSHeader::TSHeader()
1181 { 1410 {
  1411 + sync_byte = 0;
  1412 + transport_error_indicator = 0;
  1413 + payload_unit_start_indicator = 0;
  1414 + transport_priority = 0;
  1415 + pid = 0;
  1416 + transport_scrambling_control = 0;
  1417 + adaption_field_control = 0;
  1418 + continuity_counter = 0;
1182 } 1419 }
1183 1420
1184 TSHeader::~TSHeader() 1421 TSHeader::~TSHeader()