public MSF.AudioBlock ParseAudioBlock(int offset, Stream _stream) { _stream.Seek(offset, SeekOrigin.Begin); byte[] tmp = new byte[4]; MSF.AudioBlock ab = new MSF.AudioBlock(); _stream.Read(tmp, 0, 1); ab.AudioFrameNum = (uint)BitTools.MaskBits(tmp, 0, 8); ab.AudioBlockHeaderLength = ab.AudioFrameNum * 5 + 1; uint addr = (uint)offset + ab.AudioBlockHeaderLength + CRC_Size; for (int i = 0; i < ab.AudioFrameNum; i++) { //读音频参数集 MSF.AudioBlock.AudioFrame f = new MSF.AudioBlock.AudioFrame(); _stream.Read(tmp, 0, 2); f.RelativeStartAddr = addr; f.Length = (uint)BitTools.MaskBits(tmp, 0, 16); // skip 音频流编号 和 保留 _stream.Seek(1, SeekOrigin.Current); // 读相对时间 _stream.Read(tmp, 0, 2); f.RelativeTime = BitTools.MaskBits(tmp, 0, 16) * timeUint; // copy 音频流 //f._as = BitTools.CopyToNewStream(_stream, (long)f.RelativeStartAddr, (int)f.Length); BitTools.CopyFromStreamToBytesArray(_stream, (long)f.RelativeStartAddr, (int)f.Length, ref f._data); ab.aFrames.Add(f); //下一段音频地址 addr += f.Length; } return(ab); }
public MSF.VideoBlock ParseVideoBlock(int offset, Stream _stream) { _stream.Seek(offset, SeekOrigin.Begin); byte[] tmp = new byte[4]; MSF.VideoBlock vb = new MSF.VideoBlock(); _stream.Read(tmp, 0, 2); vb.VideoBlockHeaderLength = (uint)BitTools.MaskBits(tmp, 0, 12); //计算有多少个视频帧, 每个视频参数集 5byte vb.VideoFrameNum = vb.VideoBlockHeaderLength / 5; uint addr = (uint)offset + vb.VideoBlockHeaderLength + CRC_Size; for (int i = 0; i < vb.VideoFrameNum; i++) { MSF.VideoBlock.VideoFrame f = new MSF.VideoBlock.VideoFrame(); //本段视频的开始地址 f.RelativeStartAddr = addr; // 视频段长度 _stream.Read(tmp, 0, 2); f.Length = (uint)BitTools.MaskBits(tmp, 0, 16); // 帧类型,I帧 or P帧 _stream.Read(tmp, 0, 1); f.FrameMode = (uint)BitTools.MaskBits(tmp, 0, 3); if (vb.FirstIFrameIdx == -1 && f.FrameMode == 0) { vb.FirstIFrameIdx = i; } // 相对播放时间 if (BitTools.MaskBits(tmp, 7, 1) == 1) //相对时间提示 { _stream.Read(tmp, 0, 2); f.RelativeTime = BitTools.MaskBits(tmp, 0, 16) * timeUint; } // 本段视频流 //f._vs = BitTools.CopyToNewStream(_stream, (long)f.RelativeStartAddr, (int)f.Length); BitTools.CopyFromStreamToBytesArray(_stream, (long)f.RelativeStartAddr, (int)f.Length, ref f._data); vb.vFrames.Add(f); //下一段视频的开始地址 addr += f.Length; } ParseNALHeader(vb); return(vb); }