private void SPS_PPS(byte[] data) { bool spsStartCode = data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x00 && data[3] == 0x01 && (data[4] == 0x67 || data[4] == 0x27); if (spsStartCode) { this.IsKeyFrame = 1; } else { this.IsKeyFrame = 0; } if (initialized) { return; } int nal_unit_type = data[4]; if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1) { if (nal_unit_type == 0x67 || (nal_unit_type & 0x0F) == 7) { var sps_pps = SliceHeader.GetSPS_PPS(data); if (sps_pps == null) { return; } var sps = mp4parser.h264.model.SeqParameterSet.Read(new MemoryStream(sps_pps[0], 1, sps_pps[0].Length - 1)); var pps = mp4parser.h264.model.PictureParameterSet.read(new MemoryStream(sps_pps[1], 1, sps_pps[1].Length - 1)); Width = (ushort)((sps.pic_width_in_mbs_minus1 + 1) * 16 - 2 * sps.frame_crop_left_offset - 2 * sps.frame_crop_right_offset); Height = (ushort)((sps.pic_height_in_map_units_minus1 + 1) * 16 - 2 * sps.frame_crop_top_offset - 2 * sps.frame_crop_bottom_offset); this.SPS = sps_pps[0]; this.PPS = sps_pps[1]; initialized = true; } } }
private MediaFrame CreateVideoMediaFrame(byte[] data, long tick) { int keyFrameFlagOffset = 0; if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1 && data[4] == 0x09) { keyFrameFlagOffset = 6; var tmp = new byte[data.Length - 6]; Array.Copy(data, 6, tmp, 0, tmp.Length - 6); data = tmp; keyFrameFlagOffset = 0; } if (data[keyFrameFlagOffset + 4] == 0x67 || data[keyFrameFlagOffset + 4] == 0x27) { if (this.SPS == null) { var tdata = data; if (keyFrameFlagOffset > 0) { tdata = new byte[data.Length - keyFrameFlagOffset]; Array.Copy(data, keyFrameFlagOffset, tdata, 0, tdata.Length); } var sps_pps = SliceHeader.GetSPS_PPS(tdata); var sps = com.googlecode.mp4parser.h264.model.SeqParameterSet.read(new MemoryStream(sps_pps[0], 1, sps_pps[0].Length - 1)); var pps = com.googlecode.mp4parser.h264.model.PictureParameterSet.read(new MemoryStream(sps_pps[1], 1, sps_pps[1].Length - 1)); Width = (sps.pic_width_in_mbs_minus1 + 1) * 16 - 2 * sps.frame_crop_left_offset - 2 * sps.frame_crop_right_offset; Height = (sps.pic_height_in_map_units_minus1 + 1) * 16 - 2 * sps.frame_crop_top_offset - 2 * sps.frame_crop_bottom_offset; this.SPS = sps_pps[0]; this.PPS = sps_pps[1]; } var mf = new MediaFrame() { nIsAudio = 0, nIsKeyFrame = 1, Data = data, nSize = data.Length, nWidth = Width, nHeight = Height, nSPSLen = (short)this.SPS.Length, nPPSLen = (short)this.PPS.Length, nTimetick = tick, nOffset = 0, nEncoder = MediaFrame.H264Encoder, nEx = 1, }; mf.MediaFrameVersion = (byte)(mf.nIsKeyFrame == 1 ? 1 : 0); VideoFrameCount++; return(mf); } else { if (this.SPS != null) { var mf = new MediaFrame() { nIsAudio = 0, nIsKeyFrame = 0, Data = data, nSize = data.Length, nWidth = Width, nHeight = Height, nSPSLen = (short)this.SPS.Length, nPPSLen = (short)this.PPS.Length, nTimetick = tick, nOffset = 0, nEncoder = MediaFrame.H264Encoder, nEx = 1, }; mf.MediaFrameVersion = (byte)(mf.nIsKeyFrame == 1 ? 1 : 0); VideoFrameCount++; return(mf); } } return(null); }
private MediaFrame CreateVideoMediaFrame(byte[] data, long tick) { int keyFrameFlagOffset = 0; if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1 && data[4] == 0x09) { keyFrameFlagOffset = 6; } if (data[keyFrameFlagOffset + 4] == 0x67 || data[keyFrameFlagOffset + 4] == 0x68 | data[keyFrameFlagOffset + 4] == 0x27) {//sps ,这里sps及pps及载荷会被放置到不同的pes包里 try { #region if (this.SPS == null || this.PPS == null) { var tdata = data; if (keyFrameFlagOffset > 0) { tdata = new byte[data.Length - keyFrameFlagOffset]; Array.Copy(data, keyFrameFlagOffset, tdata, 0, tdata.Length); } var sps_pps = SliceHeader.GetSPS_PPS(tdata); var sps = mp4parser.h264.model.SeqParameterSet.Read(new MemoryStream(sps_pps[0], 1, sps_pps[0].Length - 1)); var pps = mp4parser.h264.model.PictureParameterSet.read(new MemoryStream(sps_pps[1], 1, sps_pps[1].Length - 1)); Width = (sps.pic_width_in_mbs_minus1 + 1) * 16 - 2 * sps.frame_crop_left_offset - 2 * sps.frame_crop_right_offset; Height = (sps.pic_height_in_map_units_minus1 + 1) * 16 - 2 * sps.frame_crop_top_offset - 2 * sps.frame_crop_bottom_offset; this.SPS = sps_pps[0]; this.PPS = sps_pps[1]; } #endregion } catch { if (this.SPS == null && data[keyFrameFlagOffset + 4] == 0x67) { SPS = data.Skip(4).ToArray(); } if (this.PPS == null && data[keyFrameFlagOffset + 4] == 0x68) { PPS = data.Skip(4).ToArray(); } if (this.SPS != null && this.PPS != null) { var sps = mp4parser.h264.model.SeqParameterSet.Read(new MemoryStream(this.SPS, 1, this.SPS.Length - 1)); // var pps = mp4parser.h264.model.PictureParameterSet.read(new MemoryStream(this.PPS, 1, this.PPS.Length - 1)); Width = (sps.pic_width_in_mbs_minus1 + 1) * 16 - 2 * sps.frame_crop_left_offset - 2 * sps.frame_crop_right_offset; Height = (sps.pic_height_in_map_units_minus1 + 1) * 16 - 2 * sps.frame_crop_top_offset - 2 * sps.frame_crop_bottom_offset; } return(null); } if (data[keyFrameFlagOffset + 4] == 0x67 && this.SPS != null && data.Length == this.SPS.Length + 4) { return(null); } if (data[keyFrameFlagOffset + 4] == 0x68 && this.PPS != null && data.Length == this.PPS.Length + 4) { return(null); } } if (this.SPS != null && this.PPS != null) { if (data[keyFrameFlagOffset + 4] == 0x65) { var h264 = new MemoryStream(); h264.Write(new byte[] { 0, 0, 0, 1 }, 0, 4); h264.Write(this.SPS, 0, this.SPS.Length); h264.Write(new byte[] { 0, 0, 0, 1 }, 0, 4); h264.Write(this.PPS, 0, this.PPS.Length); // h264.Write(new byte[] { 0, 0, 0, 1 }, 0, 4); h264.Write(data, 0, data.Length); var mf = new MediaFrame() { IsAudio = 0, IsKeyFrame = 1, Size = (int)h264.Length, Width = Width, Height = Height, SPSLen = SPS.Length, PPSLen = PPS.Length, NTimetick = tick, Offset = 0, Encoder = MediaFrame.H264Encoder, Ex = 1, }; mf.SetData(h264.ToArray()); mf.MediaFrameVersion = (byte)(mf.IsKeyFrame == 1 ? 1 : 0); VideoFrameCount++; return(mf); } else { if (this.SPS != null) { var mf = new MediaFrame() { IsAudio = 0, IsKeyFrame = 0, Size = data.Length, Width = Width, Height = Height, SPSLen = (short)this.SPS.Length, PPSLen = (short)this.PPS.Length, NTimetick = tick, Offset = 0, Encoder = MediaFrame.H264Encoder, Ex = 1, }; mf.SetData(data); mf.MediaFrameVersion = (byte)(mf.IsKeyFrame == 1 ? 1 : 0); VideoFrameCount++; return(mf); } } } return(null); }