我正在尝试解析 RTP AVC 视频流以为 H264 解码器做准备。
这是 Wireshark 捕获的我的数据包流 首先我试图找到 IDR 切片、SPS 和 PPS 参数,所以这就是 https://dl.dropboxusercontent.com/u/76817805/frame.pcapng
接下来我正在执行以下操作:
1)找到 PPS 和 SPS 数据并将它们复制到 NAL 单元到具有 [0x00 0x00 0x01] 开始序列的解包缓冲区中。
[00 00 01 | SPS][00 00 01 | [PPS]
2)对于以[0x7C 0x85](起始位= 1)开头的数据包,我正在重建第一个NAL heder(我的情况为0x65)并将0x7C 0x85后面的数据复制到带有启动序列的解包缓冲区中。
[00 00 01 65 | 视频数据……]
3) 对于以 [0x7C 0x05] 开头的数据包,我将除了 2 个第一个字节之外的数据复制到解包缓冲区中。
[.....视频数据.....]
4) 对于以 [0x7C 0x45] (停止位 = 1) 开头的数据包,我将除 2 个第一个字节之外的数据复制到解包缓冲区中。 [.....视频数据(最后一个字节)]
5)对于未分段的数据包,我只是将数据复制到解包缓冲区中并启动序列。
[00 00 01 | 纳鲁]
所以在解析示例视频流的最后,我得到了这个二进制文件 https://dl.dropboxusercontent.com/u/76817805/raw.264,但它无法正确解码。![在此处输入图像描述][1]
任何人都可以帮助我并找出我的算法中的错误吗?我究竟做错了什么?非常感谢大家。
UInt32 parseRTP( Uint8 * buf, int inputDataLen, Uint32 curAdr)
{
int result_len = 0;
// filter zero bytes at the end of packet
for (i = inputDataLen-1; i>0; i--)
{
if (buf[i] == 0x00) inputDataLen--;
else break;
}
// get NAL type
nal = buf[0];
type = (nal & 0x1f);
if ((buf[0] == 0x7C) && (buf[1] == 0x85)) IFrameisOK = 1; // Start of I frame
if (type == 6)
return 0;
if (type == 7) // new SPS
{
memcpy((void*)sps, start_sequence, sizeof(start_sequence));
memcpy((void*)(sps + sizeof(start_sequence)), buf, inputDataLen);
sps_len = inputDataLen + sizeof(start_sequence);
SPSisOK = 1;
return 0;
}
if (type == 8) // new PPS
{
memcpy((void*)pps, start_sequence, sizeof(start_sequence));
memcpy((void*)(pps + sizeof(start_sequence)), buf, inputDataLen);
pps_len = inputDataLen + sizeof(start_sequence);
PPSisOK = 1;
return 0;
}
if (SPSisOK == 1 && PPSisOK == 1)
{
if (IFrameisOK == 0) return 0; // wait I-frame
/* Simplify the case.
These are all the nal types used internally by the h264 codec
*/
if (type >= 1 && type <= 23) type = 1;
switch (type)
{
case 0: // undefined;
break;
case 1:
// copy start sequence
memcpy((void*)curAdr, start_sequence, sizeof(start_sequence));
curAdr += sizeof(start_sequence);
// copy data
memcpy((void*)curAdr, buf, inputDataLen);
curAdr += inputDataLen;
result_len = sizeof(start_sequence) + inputDataLen;
break;
case 24: // STAP-A (one packet, multiple nals) not used in this project
break;
case 25: // STAP-B
case 26: // MTAP-16
case 27: // MTAP-24
case 29: // FU-B
//not used in this project
break;
case 28: // FU-A (fragmented nal)
inputDataLen -= 2; // delete 2 first bytes for fragmented units
//skip the fu_indicator
buf++;
Uint8 fu_indicator = nal;
Uint8 fu_header = *buf; // read the fu_header.
Uint8 start_bit = fu_header >> 7;
Uint8 reconstructed_nal;
Uint8 nal_type = (fu_header & 0x1f);
/* reconstruct this packet's true nal; only the
data follows..*/
reconstructed_nal = fu_indicator & (0xe0);
/*the original nal forbidden bit and NRI are stored in this
packet's nal*/
reconstructed_nal |= nal_type;
// skip the fu_header...
buf++;
if(start_bit)
{
if (NEED_CONFIGS)
{
// copy SPS and PPS first
memcpy((void*)curAdr, sps, sps_len);
curAdr += sps_len;
memcpy((void*)curAdr, pps, pps_len);
curAdr += pps_len;
}
// copy in the start sequence
memcpy((void*)curAdr, start_sequence, sizeof(start_sequence));
curAdr += sizeof(start_sequence);
// copy reconstructed nal
memcpy((void*)curAdr,&reconstructed_nal, sizeof(reconstructed_nal));
curAdr += sizeof(reconstructed_nal);
// copy payload
memcpy((void*)curAdr,buf, inputDataLen);
curAdr += inputDataLen;
if (NEED_CONFIGS)
{
result_len = (sps_len + pps_len + sizeof(start_sequence) + sizeof(reconstructed_nal) + inputDataLen);
NEED_CONFIGS = 0;
}
else
{
result_len += (sizeof(start_sequence) + sizeof(reconstructed_nal) + inputDataLen);
}
}
else
{
memcpy((void*)curAdr,buf, inputDataLen);
curAdr += inputDataLen;
result_len = inputDataLen;
}
break;
default:
break;
}
return result_len;
}
else
{
return 0;
}
}