winlin

refine the h264 parser, one ts message should parsed to one rtmp/flv message.

@@ -355,12 +355,20 @@ int SrsMpegtsOverUdp::on_ts_video(SrsTsMessage* msg, SrsStream* avs) @@ -355,12 +355,20 @@ int SrsMpegtsOverUdp::on_ts_video(SrsTsMessage* msg, SrsStream* avs)
355 if ((ret = avc->annexb_demux(avs, &frame, &frame_size)) != ERROR_SUCCESS) { 355 if ((ret = avc->annexb_demux(avs, &frame, &frame_size)) != ERROR_SUCCESS) {
356 return ret; 356 return ret;
357 } 357 }
358 -  
359 - // ignore invalid frame,  
360 - // * atleast 1bytes for SPS to decode the type  
361 - // * ignore the auth bytes '09f0'  
362 - if (frame_size <= 2) {  
363 - continue; 358 +
  359 + // 5bits, 7.3.1 NAL unit syntax,
  360 + // H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
  361 + // 7: SPS, 8: PPS, 5: I Frame, 1: P Frame
  362 + SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(frame[0] & 0x1f);
  363 +
  364 + // ignore the nalu type sps(7), pps(8), aud(9)
  365 + switch (nal_unit_type) {
  366 + case SrsAvcNaluTypeSPS:
  367 + case SrsAvcNaluTypePPS:
  368 + case SrsAvcNaluTypeAccessUnitDelimiter:
  369 + continue;
  370 + default:
  371 + break;
364 } 372 }
365 373
366 // for sps 374 // for sps
@@ -402,6 +410,7 @@ int SrsMpegtsOverUdp::on_ts_video(SrsTsMessage* msg, SrsStream* avs) @@ -402,6 +410,7 @@ int SrsMpegtsOverUdp::on_ts_video(SrsTsMessage* msg, SrsStream* avs)
402 } 410 }
403 411
404 // ibp frame. 412 // ibp frame.
  413 + // TODO: FIXME: we should group all frames to a rtmp/flv message from one ts message.
405 srs_info("mpegts: demux avc ibp frame size=%d, dts=%d", ibpframe_size, dts); 414 srs_info("mpegts: demux avc ibp frame size=%d, dts=%d", ibpframe_size, dts);
406 if ((ret = write_h264_ipb_frame(frame, frame_size, dts, pts)) != ERROR_SUCCESS) { 415 if ((ret = write_h264_ipb_frame(frame, frame_size, dts, pts)) != ERROR_SUCCESS) {
407 return ret; 416 return ret;
@@ -458,10 +467,20 @@ int SrsMpegtsOverUdp::write_h264_ipb_frame(char* frame, int frame_size, u_int32_ @@ -458,10 +467,20 @@ int SrsMpegtsOverUdp::write_h264_ipb_frame(char* frame, int frame_size, u_int32_
458 if (!h264_sps_pps_sent) { 467 if (!h264_sps_pps_sent) {
459 return ERROR_H264_DROP_BEFORE_SPS_PPS; 468 return ERROR_H264_DROP_BEFORE_SPS_PPS;
460 } 469 }
  470 +
  471 + // 5bits, 7.3.1 NAL unit syntax,
  472 + // H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
  473 + // 7: SPS, 8: PPS, 5: I Frame, 1: P Frame
  474 + SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(frame[0] & 0x1f);
  475 +
  476 + // for IDR frame, the frame is keyframe.
  477 + SrsCodecVideoAVCFrame frame_type = SrsCodecVideoAVCFrameInterFrame;
  478 + if (nal_unit_type == SrsAvcNaluTypeIDR) {
  479 + frame_type = SrsCodecVideoAVCFrameKeyFrame;
  480 + }
461 481
462 std::string ibp; 482 std::string ibp;
463 - int8_t frame_type;  
464 - if ((ret = avc->mux_ipb_frame(frame, frame_size, dts, pts, ibp, frame_type)) != ERROR_SUCCESS) { 483 + if ((ret = avc->mux_ipb_frame(frame, frame_size, ibp)) != ERROR_SUCCESS) {
465 return ret; 484 return ret;
466 } 485 }
467 486
@@ -579,10 +579,20 @@ int SrsRtspConn::write_h264_sps_pps(u_int32_t dts, u_int32_t pts) @@ -579,10 +579,20 @@ int SrsRtspConn::write_h264_sps_pps(u_int32_t dts, u_int32_t pts)
579 int SrsRtspConn::write_h264_ipb_frame(char* frame, int frame_size, u_int32_t dts, u_int32_t pts) 579 int SrsRtspConn::write_h264_ipb_frame(char* frame, int frame_size, u_int32_t dts, u_int32_t pts)
580 { 580 {
581 int ret = ERROR_SUCCESS; 581 int ret = ERROR_SUCCESS;
  582 +
  583 + // 5bits, 7.3.1 NAL unit syntax,
  584 + // H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
  585 + // 7: SPS, 8: PPS, 5: I Frame, 1: P Frame
  586 + SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(frame[0] & 0x1f);
  587 +
  588 + // for IDR frame, the frame is keyframe.
  589 + SrsCodecVideoAVCFrame frame_type = SrsCodecVideoAVCFrameInterFrame;
  590 + if (nal_unit_type == SrsAvcNaluTypeIDR) {
  591 + frame_type = SrsCodecVideoAVCFrameKeyFrame;
  592 + }
582 593
583 std::string ibp; 594 std::string ibp;
584 - int8_t frame_type;  
585 - if ((ret = avc->mux_ipb_frame(frame, frame_size, dts, pts, ibp, frame_type)) != ERROR_SUCCESS) { 595 + if ((ret = avc->mux_ipb_frame(frame, frame_size, ibp)) != ERROR_SUCCESS) {
586 return ret; 596 return ret;
587 } 597 }
588 598
@@ -929,7 +929,7 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp) @@ -929,7 +929,7 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
929 return ret; 929 return ret;
930 } 930 }
931 931
932 - int64_t seq_parameter_set_id = -1; 932 + int32_t seq_parameter_set_id = -1;
933 if ((ret = srs_avc_nalu_read_uev(&bs, seq_parameter_set_id)) != ERROR_SUCCESS) { 933 if ((ret = srs_avc_nalu_read_uev(&bs, seq_parameter_set_id)) != ERROR_SUCCESS) {
934 return ret; 934 return ret;
935 } 935 }
@@ -944,7 +944,7 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp) @@ -944,7 +944,7 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
944 || profile_idc == 44 || profile_idc == 83 || profile_idc == 86 || profile_idc == 118 944 || profile_idc == 44 || profile_idc == 83 || profile_idc == 86 || profile_idc == 118
945 || profile_idc == 128 945 || profile_idc == 128
946 ) { 946 ) {
947 - int64_t chroma_format_idc = -1; 947 + int32_t chroma_format_idc = -1;
948 if ((ret = srs_avc_nalu_read_uev(&bs, chroma_format_idc)) != ERROR_SUCCESS) { 948 if ((ret = srs_avc_nalu_read_uev(&bs, chroma_format_idc)) != ERROR_SUCCESS) {
949 return ret; 949 return ret;
950 } 950 }
@@ -955,12 +955,12 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp) @@ -955,12 +955,12 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
955 } 955 }
956 } 956 }
957 957
958 - int64_t bit_depth_luma_minus8 = -1; 958 + int32_t bit_depth_luma_minus8 = -1;
959 if ((ret = srs_avc_nalu_read_uev(&bs, bit_depth_luma_minus8)) != ERROR_SUCCESS) { 959 if ((ret = srs_avc_nalu_read_uev(&bs, bit_depth_luma_minus8)) != ERROR_SUCCESS) {
960 return ret; 960 return ret;
961 } 961 }
962 962
963 - int64_t bit_depth_chroma_minus8 = -1; 963 + int32_t bit_depth_chroma_minus8 = -1;
964 if ((ret = srs_avc_nalu_read_uev(&bs, bit_depth_chroma_minus8)) != ERROR_SUCCESS) { 964 if ((ret = srs_avc_nalu_read_uev(&bs, bit_depth_chroma_minus8)) != ERROR_SUCCESS) {
965 return ret; 965 return ret;
966 } 966 }
@@ -981,18 +981,18 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp) @@ -981,18 +981,18 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
981 } 981 }
982 } 982 }
983 983
984 - int64_t log2_max_frame_num_minus4 = -1; 984 + int32_t log2_max_frame_num_minus4 = -1;
985 if ((ret = srs_avc_nalu_read_uev(&bs, log2_max_frame_num_minus4)) != ERROR_SUCCESS) { 985 if ((ret = srs_avc_nalu_read_uev(&bs, log2_max_frame_num_minus4)) != ERROR_SUCCESS) {
986 return ret; 986 return ret;
987 } 987 }
988 988
989 - int64_t pic_order_cnt_type = -1; 989 + int32_t pic_order_cnt_type = -1;
990 if ((ret = srs_avc_nalu_read_uev(&bs, pic_order_cnt_type)) != ERROR_SUCCESS) { 990 if ((ret = srs_avc_nalu_read_uev(&bs, pic_order_cnt_type)) != ERROR_SUCCESS) {
991 return ret; 991 return ret;
992 } 992 }
993 993
994 if (pic_order_cnt_type == 0) { 994 if (pic_order_cnt_type == 0) {
995 - int64_t log2_max_pic_order_cnt_lsb_minus4 = -1; 995 + int32_t log2_max_pic_order_cnt_lsb_minus4 = -1;
996 if ((ret = srs_avc_nalu_read_uev(&bs, log2_max_pic_order_cnt_lsb_minus4)) != ERROR_SUCCESS) { 996 if ((ret = srs_avc_nalu_read_uev(&bs, log2_max_pic_order_cnt_lsb_minus4)) != ERROR_SUCCESS) {
997 return ret; 997 return ret;
998 } 998 }
@@ -1002,17 +1002,17 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp) @@ -1002,17 +1002,17 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
1002 return ret; 1002 return ret;
1003 } 1003 }
1004 1004
1005 - int64_t offset_for_non_ref_pic = -1; 1005 + int32_t offset_for_non_ref_pic = -1;
1006 if ((ret = srs_avc_nalu_read_uev(&bs, offset_for_non_ref_pic)) != ERROR_SUCCESS) { 1006 if ((ret = srs_avc_nalu_read_uev(&bs, offset_for_non_ref_pic)) != ERROR_SUCCESS) {
1007 return ret; 1007 return ret;
1008 } 1008 }
1009 1009
1010 - int64_t offset_for_top_to_bottom_field = -1; 1010 + int32_t offset_for_top_to_bottom_field = -1;
1011 if ((ret = srs_avc_nalu_read_uev(&bs, offset_for_top_to_bottom_field)) != ERROR_SUCCESS) { 1011 if ((ret = srs_avc_nalu_read_uev(&bs, offset_for_top_to_bottom_field)) != ERROR_SUCCESS) {
1012 return ret; 1012 return ret;
1013 } 1013 }
1014 1014
1015 - int64_t num_ref_frames_in_pic_order_cnt_cycle = -1; 1015 + int32_t num_ref_frames_in_pic_order_cnt_cycle = -1;
1016 if ((ret = srs_avc_nalu_read_uev(&bs, num_ref_frames_in_pic_order_cnt_cycle)) != ERROR_SUCCESS) { 1016 if ((ret = srs_avc_nalu_read_uev(&bs, num_ref_frames_in_pic_order_cnt_cycle)) != ERROR_SUCCESS) {
1017 return ret; 1017 return ret;
1018 } 1018 }
@@ -1023,7 +1023,7 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp) @@ -1023,7 +1023,7 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
1023 } 1023 }
1024 } 1024 }
1025 1025
1026 - int64_t max_num_ref_frames = -1; 1026 + int32_t max_num_ref_frames = -1;
1027 if ((ret = srs_avc_nalu_read_uev(&bs, max_num_ref_frames)) != ERROR_SUCCESS) { 1027 if ((ret = srs_avc_nalu_read_uev(&bs, max_num_ref_frames)) != ERROR_SUCCESS) {
1028 return ret; 1028 return ret;
1029 } 1029 }
@@ -1033,12 +1033,12 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp) @@ -1033,12 +1033,12 @@ int SrsAvcAacCodec::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
1033 return ret; 1033 return ret;
1034 } 1034 }
1035 1035
1036 - int64_t pic_width_in_mbs_minus1 = -1; 1036 + int32_t pic_width_in_mbs_minus1 = -1;
1037 if ((ret = srs_avc_nalu_read_uev(&bs, pic_width_in_mbs_minus1)) != ERROR_SUCCESS) { 1037 if ((ret = srs_avc_nalu_read_uev(&bs, pic_width_in_mbs_minus1)) != ERROR_SUCCESS) {
1038 return ret; 1038 return ret;
1039 } 1039 }
1040 1040
1041 - int64_t pic_height_in_map_units_minus1 = -1; 1041 + int32_t pic_height_in_map_units_minus1 = -1;
1042 if ((ret = srs_avc_nalu_read_uev(&bs, pic_height_in_map_units_minus1)) != ERROR_SUCCESS) { 1042 if ((ret = srs_avc_nalu_read_uev(&bs, pic_height_in_map_units_minus1)) != ERROR_SUCCESS) {
1043 return ret; 1043 return ret;
1044 } 1044 }
@@ -46,7 +46,7 @@ using namespace std; @@ -46,7 +46,7 @@ using namespace std;
46 // @see SRS_SYS_TIME_RESOLUTION_MS_TIMES 46 // @see SRS_SYS_TIME_RESOLUTION_MS_TIMES
47 #define SYS_TIME_RESOLUTION_US 300*1000 47 #define SYS_TIME_RESOLUTION_US 300*1000
48 48
49 -int srs_avc_nalu_read_uev(SrsBitStream* stream, int64_t& v) 49 +int srs_avc_nalu_read_uev(SrsBitStream* stream, int32_t& v)
50 { 50 {
51 int ret = ERROR_SUCCESS; 51 int ret = ERROR_SUCCESS;
52 52
@@ -67,7 +67,7 @@ int srs_avc_nalu_read_uev(SrsBitStream* stream, int64_t& v) @@ -67,7 +67,7 @@ int srs_avc_nalu_read_uev(SrsBitStream* stream, int64_t& v)
67 b = stream->read_bit(); 67 b = stream->read_bit();
68 } 68 }
69 69
70 - if (leadingZeroBits >= 64) { 70 + if (leadingZeroBits >= 31) {
71 return ERROR_AVC_NALU_UEV; 71 return ERROR_AVC_NALU_UEV;
72 } 72 }
73 73
@@ -40,7 +40,7 @@ class SrsBitStream; @@ -40,7 +40,7 @@ class SrsBitStream;
40 #define srs_max(a, b) (((a) < (b))? (b) : (a)) 40 #define srs_max(a, b) (((a) < (b))? (b) : (a))
41 41
42 // read nalu uev. 42 // read nalu uev.
43 -extern int srs_avc_nalu_read_uev(SrsBitStream* stream, int64_t& v); 43 +extern int srs_avc_nalu_read_uev(SrsBitStream* stream, int32_t& v);
44 extern int srs_avc_nalu_read_bit(SrsBitStream* stream, int8_t& v); 44 extern int srs_avc_nalu_read_bit(SrsBitStream* stream, int8_t& v);
45 45
46 // get current system time in ms, use cache to avoid performance problem 46 // get current system time in ms, use cache to avoid performance problem
@@ -1243,10 +1243,20 @@ int srs_write_h264_ipb_frame(Context* context, @@ -1243,10 +1243,20 @@ int srs_write_h264_ipb_frame(Context* context,
1243 if (!context->h264_sps_pps_sent) { 1243 if (!context->h264_sps_pps_sent) {
1244 return ERROR_H264_DROP_BEFORE_SPS_PPS; 1244 return ERROR_H264_DROP_BEFORE_SPS_PPS;
1245 } 1245 }
1246 - 1246 +
  1247 + // 5bits, 7.3.1 NAL unit syntax,
  1248 + // H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
  1249 + // 7: SPS, 8: PPS, 5: I Frame, 1: P Frame
  1250 + SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(frame[0] & 0x1f);
  1251 +
  1252 + // for IDR frame, the frame is keyframe.
  1253 + SrsCodecVideoAVCFrame frame_type = SrsCodecVideoAVCFrameInterFrame;
  1254 + if (nal_unit_type == SrsAvcNaluTypeIDR) {
  1255 + frame_type = SrsCodecVideoAVCFrameKeyFrame;
  1256 + }
  1257 +
1247 std::string ibp; 1258 std::string ibp;
1248 - int8_t frame_type;  
1249 - if ((ret = context->avc_raw.mux_ipb_frame(frame, frame_size, dts, pts, ibp, frame_type)) != ERROR_SUCCESS) { 1259 + if ((ret = context->avc_raw.mux_ipb_frame(frame, frame_size, ibp)) != ERROR_SUCCESS) {
1250 return ret; 1260 return ret;
1251 } 1261 }
1252 1262
@@ -226,15 +226,10 @@ int SrsRawH264Stream::mux_sequence_header(string sps, string pps, u_int32_t dts, @@ -226,15 +226,10 @@ int SrsRawH264Stream::mux_sequence_header(string sps, string pps, u_int32_t dts,
226 return ret; 226 return ret;
227 } 227 }
228 228
229 -int SrsRawH264Stream::mux_ipb_frame(char* frame, int nb_frame, u_int32_t dts, u_int32_t pts, string& ibp, int8_t& frame_type) 229 +int SrsRawH264Stream::mux_ipb_frame(char* frame, int nb_frame, string& ibp)
230 { 230 {
231 int ret = ERROR_SUCCESS; 231 int ret = ERROR_SUCCESS;
232 232
233 - // 5bits, 7.3.1 NAL unit syntax,  
234 - // H.264-AVC-ISO_IEC_14496-10.pdf, page 44.  
235 - // 7: SPS, 8: PPS, 5: I Frame, 1: P Frame  
236 - u_int8_t nal_unit_type = (char)frame[0] & 0x1f;  
237 -  
238 // 4bytes size of nalu: 233 // 4bytes size of nalu:
239 // NALUnitLength 234 // NALUnitLength
240 // Nbytes of nalu. 235 // Nbytes of nalu.
@@ -260,12 +255,6 @@ int SrsRawH264Stream::mux_ipb_frame(char* frame, int nb_frame, u_int32_t dts, u_ @@ -260,12 +255,6 @@ int SrsRawH264Stream::mux_ipb_frame(char* frame, int nb_frame, u_int32_t dts, u_
260 // NALUnit 255 // NALUnit
261 stream.write_bytes(frame, nb_frame); 256 stream.write_bytes(frame, nb_frame);
262 257
263 - // send out h264 packet.  
264 - frame_type = SrsCodecVideoAVCFrameInterFrame;  
265 - if (nal_unit_type != 1) {  
266 - frame_type = SrsCodecVideoAVCFrameKeyFrame;  
267 - }  
268 -  
269 ibp = ""; 258 ibp = "";
270 ibp.append(packet, nb_packet); 259 ibp.append(packet, nb_packet);
271 260
@@ -281,7 +270,7 @@ int SrsRawH264Stream::mux_avc2flv(string video, int8_t frame_type, int8_t avc_pa @@ -281,7 +270,7 @@ int SrsRawH264Stream::mux_avc2flv(string video, int8_t frame_type, int8_t avc_pa
281 // 1bytes, AVCPacketType 270 // 1bytes, AVCPacketType
282 // 3bytes, CompositionTime, the cts. 271 // 3bytes, CompositionTime, the cts.
283 // @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78 272 // @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78
284 - int size = video.length() + 5; 273 + int size = (int)video.length() + 5;
285 char* data = new char[size]; 274 char* data = new char[size];
286 char* p = data; 275 char* p = data;
287 276
@@ -76,7 +76,7 @@ public: @@ -76,7 +76,7 @@ public:
76 * @param ibp output the packet. 76 * @param ibp output the packet.
77 * @param frame_type output the frame type. 77 * @param frame_type output the frame type.
78 */ 78 */
79 - virtual int mux_ipb_frame(char* frame, int nb_frame, u_int32_t dts, u_int32_t pts, std::string& ibp, int8_t& frame_type); 79 + virtual int mux_ipb_frame(char* frame, int nb_frame, std::string& ibp);
80 /** 80 /**
81 * mux the avc video packet to flv video packet. 81 * mux the avc video packet to flv video packet.
82 * @param frame_type, SrsCodecVideoAVCFrameKeyFrame or SrsCodecVideoAVCFrameInterFrame. 82 * @param frame_type, SrsCodecVideoAVCFrameKeyFrame or SrsCodecVideoAVCFrameInterFrame.