H264 profile 及帧类型介绍
1. h264 profile:
h264 分为三种profile
基准档次:baseline profile;
主要档次:main profile;
扩展档次:extended profile(常见无high profile);
在H.264的SPS中,第一个字节表示profile_idc,根据profile_idc(通过wireshark 抓包可见)的值可以确定码流符合哪一种档次。判断规律为:
profile_idc = 66 → baseline profile;
profile_idc = 77 → main profile;
profile_idc = 88 → extended profile;
在新版的标准中,还包括了High、High 10、High 4:2:2、High 4:4:4、High 10 Intra、High 4:2:2 Intra、High 4:4:4 Intra、CAVLC 4:4:4 Intra等,每一种都由不同的profile_idc表示。
2. 常用帧类型:
SPS: Sequence Paramater Set,又称作序列参数集。其包括图像的分辨率等信息。基中 pic_width_in_mbs_minus1 , pic_height_in_mbs_minus1, 这两个参数分别表示图像的宽和高,为宏块(16x16)为单位的值减1。因此,实际的宽为 (pic_width_in_mbs_minus1 +1)*16 , 高为(pic_height_in_mbs_minus1+1)*16
PPS: 图像参数集Picture Paramater Set ,
I : 关键帧
P: 前向搜索帧/前向预测帧,以I帧预测P帧
B: 双向搜索帧/双向内插帧,由I帧、P帧预测B帧
一段视频必须要有sps, pps 帧,否则无法解码播放。如果视频的开始部分无sps ,pps帧除了用ffplay(支持跳帧),一般的播放器无法解码播放。
3. RTP 传输H264
RTP 包如下所示:
RTSP/1.0 200 OK
CSeq: 4
x-Accept-Dynamic-Rate: 1
Content-Base: rtsp://192.168.3.146:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif/
Cache-Control: must-revalidate
Content-Length: 693
Content-Type: application/sdp
v=0
o=- 2251938965 2251938965 IN IP4 0.0.0.0
s=Media Server
c=IN IP4 0.0.0.0
t=0 0
a=control:*
a=packetization-supported:DH
a=rtppayload-supported:DH
a=range:npt=now-
m=video 0 RTP/AVP 96
a=control:trackID=0
a=framerate:25.000000
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=4D002A;sprop-parameter-sets=Z00AKpWoHgCJ+WbgICAgQAA=,aO48gAA=
a=recvonly
m=audio 0 RTP/AVP 97
a=control:trackID=1
a=rtpmap:97 MPEG4-GENERIC/16000
a=fmtp:97 streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1408
a=recvonly
m=application 0 RTP/AVP 107
a=control:trackID=4
a=rtpmap:107 vnd.onvif.metadata/90000
a=recvonly
用RTP传输H264的时候,根据sdp协议描述,其中有两项:Sequence Parameter Sets (SPS) 和Picture Parameter Set (PPS)需要用到, 那么这两项从哪里获取呢?答案是从H264码流中获取.在H264码流中,都是以"0x00 0x00 0x01"或者"0x00 0x00 0x00 0x01"为开始码的,找到开始码之后,使用开始码之后的第一个字节的低5位判断帧类型是否为7(sps)或者8(pps)即data[4] & 0x1f == 7 || data[4] & 0x1f == 8.然后对获取的nal去掉开始码之后进行base64编码,得到的信息就可以用于sdp.sps和pps需要用逗号分隔开来(上面标记为红色部分的字段)。
NALU的第1个字节是NUAL类型字节,其格式如下
|0 |1 |2 |3 |4 |5 |6 |7 |
|F |NRI|Type |
F: 1 bit forbidden_zero_bit H.264规范声明值设置为1表示语法违例
NRI: 2 bit nal_ref_idc 表示NALU的优先级。0--3,取值越大,该NALU越重要,需要优先保护
00值表示该NALU不用于帧间图像预测重构参考图像,可以丢弃不用冒参考图像完整性风险。
如果NALU是参考帧slice、PPS、SPS时,该值必须大于0
Type: 5 bit nal_unit_type 表示NALU载荷类型,类型值定义如下
0:未定义
1:非IDR图像不采用数据划分片段
2:非IDR图像采用数据划分片段A部分
3:非IDR图像采用数据划分片段B部分
4:非IDR图像采用数据划分片段C部分
5:IDR图像片段
6:补充增强信息 SEI
7:序列参数集 SPS
8:图像参数集 PPS
9:分隔符
10:序列结束符
11:流结束符
12:填充数据
13:序列参数集扩展
14:带前缀的NALU
15:子序列参数集
16-18:保留
19:不采用数据划分的辅助编码图像片段
20:编码片段扩展
21-23:保留
24-31:未定义
4. H264 流分析工具
1. Elecard StreamEye 系列
2. H264BSAnalyzer(开源)
https://github.com/latelee/H264BSAnalyzer
特性:
- support different NAL display, including VPS, SPS, PPS, SEI, AUD, Slice.
- support hex data display for NAL.
- support displaying detail information using cursor up and down.
- support different color for different slice, with frame number.
- auto parse file name.
- support file name suffix:
- H.264 format file: .h264,.h264, .avc
- H.265 format file: .h26, .h265, .hevc
- auto decide format acording file content if no name suffix specify listing above.
- support playing H.264、H.265 bitstream video file.
- pause, stop, play frame by frame for video file.
- support saving for RGB(24bit) and YUV(yuv420p) file, BMP, JPEG (picture) file.
- support saving for AVI, MP4, MOV format file.