1、H.264/H.265简介
在H.264/H.265视频编码标准中,整个系统框架被分为了两个层面:视频编码层面(VCL)和网络抽象层面(NAL)。
其中,前者负责有效表示视频数据的内容,而后者则负责格式化数据并提供头信息,以保证数据适合各种信道和存储介质上的传输。
NAL单元是NAL的基本语法结构。
H264 NAL包含一个字节的头信息和一系列来自VCL的称为原始字节序列载荷(RBSP)的字节流。
H265 NAL包含两个字节的头信息和一系列来自VCL的称为原始字节序列载荷(RBSP)的字节流。
如果NALU对应的Slice为一帧的开始,则用4字节表示,即0x00000001;或者用3字节表示,0x000001。
1.1、h264 nal header
+---------------+
|0|1|2|3|4|5|6|7|
+-+-+-+-+-+-+-+-+
|F|NRI| Type |
+---------------+
nalu->forbidden_bit = ((nalu->buf) >> 7) & 1;
nalu->nal_reference_idc = (NalRefIdc) (((nalu->buf) >> 5) & 3);
nalu->nal_unit_type = (NaluType) ((*(nalu->buf)) & 0x1f);
- F (1 bit):forbidden_zero_bit。
- NRI (2 bit):nal_ref_idc。0值表示解码时可丢弃,非0值表示解码时不可丢弃。例如SPS PPS不可丢弃故为1,SEI可丢弃故为0。
- Type (5 bit):nal_unit_type。

1.2、h265 nal header
+---------------+---------------+
|0|1|2|3|4|5|6|7|0|1|2|3|4|5|6|7|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|F| Type | LayerId | TID |
+-------------+-----------------+
forbidden_zero_bit = (pData[0]>>7) & 0x1
nalu.m_nalUnitType = (pData[0]>>1) & 0x3F
nalu.m_nuhLayerId = (pData[0] & 0x1 << 5) + (pData[1]>>3) & 0x1F
nalu.m_temporalId = (pData[1] & 0x7) - 1
- F (1bit): forbidden_zero_bit.
- Type (6bit): nal_unit_type. 表示当前NAL的类型
- LayerId (6bit): nuh_layer_id. 编码层级信息
- TID (3bit): nuh_temporal_id_plus1. 时间分级信息

2、h264 rtp 打包

2.1、单包打包模式
去掉0x00000001,然后直接把NAL单元(头+数据)填入就好。
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|F|NRI| Type | |
+-+-+-+-+-+-+-+-+ |
| |
| Bytes 2..n of a single NAL unit |
| |
| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| :...OPTIONAL RTP padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2.2、分片打包模式
分片Fragmentation Units (FUs),可分为FUA和FUB两者模式,如下仅介绍FUA。
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| FU indicator | FU header | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| |
| FU payload |
| |
| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| :...OPTIONAL RTP padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
第一个字节FU indicator:填入NAL单元头,并修改type值为28(表示FUA分片)。
+---------------+
|0|1|2|3|4|5|6|7|
+-+-+-+-+-+-+-+-+
|F|NRI| Type |
+---------------+

第二个字节FU header:S为1表示第一个分片;E为1表示最后一个分片;R必须为0;Type表示实际的原本的nal type类型。
+---------------+
|0|1|2|3|4|5|6|7|
+-+-+-+-+-+-+-+-+
|S|E|R| Type |
+---------------+
3、h265 rtp 打包

3.1、单包打包模式
PayloadHdr 直接把NAL单元头填入就好。

3.2、分片打包模式

第一字节PayloadHdr:先拷贝NAL单元头,但需要把Type换成49。
第二字节FU header:S为1表示第一个分片;E为1表示最后一个分片;FuType表示实际的原本的nal type类型。
+---------------+
|0|1|2|3|4|5|6|7|
+-+-+-+-+-+-+-+-+
|S|E| FuType |
+---------------+
网友评论