解码H.264 NALU流C#

解码H.264 NALU流C#,c#,video-streaming,h.264,decoder,C#,Video Streaming,H.264,Decoder,1-现场博世IP摄像机流 2流格式为H.264 AVC 3-NALU按照“ISO/IEC 14496-15高级视频编码(AVC)文件格式”中的描述打包在流项目中,即单个NALU由网络字节顺序的4字节长度字段分隔 private unsafe byte[] PostProcessPayload(Guid codingType, byte[] dataBuffer) { if ((codingType == VsdkGuids.CodingType_H264) &&

1-现场博世IP摄像机流

2流格式为H.264 AVC

3-NALU按照“ISO/IEC 14496-15高级视频编码(AVC)文件格式”中的描述打包在流项目中,即单个NALU由网络字节顺序的4字节长度字段分隔

 private unsafe byte[] PostProcessPayload(Guid codingType, byte[] dataBuffer)
    {
        if ((codingType == VsdkGuids.CodingType_H264) && m_convertH264StreamItems)
        {
            //
            // "This stream item consists of one or multiple H.264 Network Abstraction Layer Units 
            // (NALUs) ITU-T Rec. H.264 / ISO/IEC 14496-10 Advanced Video Coding belonging to the same timestamp.

            // It is guaranteed that each item including an IDR picture will also include the corresponding picture 
            // parameter set and sequence parameter set NALUs. 

            //The NALUs are packed in the stream item as described in 
            // "ISO/IEC 14496-15 Advanced Video Coding (AVC) file format", i.e. single NALUs are separated 
            // by a 4 byte length field in network byte order. 
            // Since the frame boundary is implicitly communicated (each stream item represents a frame), 
            // frames usually do not carry an explicit access unit delimiter at the end. 
            // In case interlaced video is received, two fields are packet into a single stream item."
            //

            // The following code section replaces the 4 byte NALU lengths with NALU separators and appends an access delimiter at frame end.

            // The data buffer that contains the H.264 frame must be allocated with enough headroom (6 bytes after the last NALU) 
            // for the access delimiter. When such a processed payload data is written into a file of type *.h264, the VLC player for example is able 
            // to play that file (in this Complete C# Sample, connect a device proxy, set the property ConvertH264StreamItems of 
            // a video datastream to "true", add the device proxy to the end nodes, start this video stream, and stop it after a while. Then simply 
            // rename the written DataStream_{unique identifier}_Video.dat file to *.h264 and play this file with VLC).
            int remainingLength = dataBuffer.Length - H264_ACCESS_DELIMITER.Length;
            fixed (byte* pDataBuffer = &dataBuffer[0], pDelimiterBuffer = &H264_ACCESS_DELIMITER[0], pSeparatorBuffer = &H264_NALU_SEPARATOR[0])
            {
                byte* pData = pDataBuffer;
                while (remainingLength > H264_NALU_SEPARATOR.Length)
                {
                    int naluLength = System.Net.IPAddress.NetworkToHostOrder(*(Int32*)pData);

                    // replace NALU length with NALU separator
                    for (int i = 0; i < H264_NALU_SEPARATOR.Length; i++)
                        pData[i] = pSeparatorBuffer[i];

                    // goto next NALU
                    int offsetToNextNalu = H264_NALU_SEPARATOR.Length + naluLength;
                    pData += offsetToNextNalu;
                    remainingLength -= offsetToNextNalu;
                }

                if (remainingLength != 0)
                    Common.Log("Warning: Inconsistency in postprocessing of H.264 stream item, remaining length {0} is not zero", remainingLength);

                // append access delimiter after last NALU, there will be no access violation here because the buffer had been allocated with enough headroom
                for (int i = 0; i < H264_ACCESS_DELIMITER.Length; i++)
                    pData[i] = pDelimiterBuffer[i];
            }
        }
        return dataBuffer;
    }
private不安全字节[]后处理有效负载(Guid编码类型,字节[]数据缓冲)
{
if((codingType==VsdkGuids.codingType_H264)和&m_converth264流项目)
{
//
//“此流项由一个或多个H.264网络抽象层单元组成
//(NALUs)属于同一时间戳的ITU-T Rec.H.264/ISO/IEC 14496-10高级视频编码。
//保证包括IDR图片的每个项目也包括相应的图片
//参数集和序列参数集。
//NALU打包在流项目中,如中所述
//“ISO/IEC 14496-15高级视频编码(AVC)文件格式”,即单个NALU分开
//通过网络字节顺序的4字节长度字段。
//由于帧边界是隐式通信的(每个流项表示一个帧),
//帧的末尾通常没有显式的访问单元分隔符。
//在接收到隔行扫描视频的情况下,将两个字段分组到单个流项目中。”
//
//下面的代码部分使用NALU分隔符替换4字节的NALU长度,并在帧末尾附加一个访问分隔符。
//必须为包含H.264帧的数据缓冲区分配足够的净空(最后一个NALU后6个字节)
//对于访问定界符。当这样一个处理后的有效负载数据写入*.h264类型的文件时,例如VLC播放器就可以
//要播放该文件(在这个完整的C#示例中,连接设备代理,设置ConvertH264StreamItems的属性
//将视频数据流设置为“true”,将设备代理添加到终端节点,启动此视频流,并在一段时间后停止。然后
//将写入的数据流{unique identifier}_Video.dat文件重命名为*.h264,并使用VLC播放该文件)。
int remainingLength=dataBuffer.Length-H264_ACCESS_DELIMITER.Length;
已修复(字节*pDataBuffer=&dataBuffer[0],pDelimiterBuffer=&H264\u访问\u分隔符[0],pSeparatorBuffer=&H264\u NALU\u分隔符[0])
{
字节*pData=pDataBuffer;
while(剩余长度>H264纳鲁分隔符长度)
{
int-nalulelength=System.Net.IPAddress.NetworkToHostOrder(*(Int32*)pData);
//用NALU分隔符替换NALU长度
对于(int i=0;i
如何从C#中的
dataBuffer
获取帧图像