NALU type 7-序列参数集 SPS

第一个字节开始就表示数据内容,没有具体的长度信息,其长度根据具体里面的内容是不同的
参考7.3.2.1.1 Sequence parameter set data syntax
在H.264的各种语法元素中,SPS中的信息至关重要。如果其中的数据丢失或出现错误,那么解码过程很可能会失败。
SPS即Sequence Paramater Set,又称作序列参数集。SPS中保存了一组编码视频序列(Coded video sequence)的全局参数。所谓的编码视频序列即原始视频的一帧一帧的像素数据经过编码之后的结构组成的序列。而每一帧的编码后数据所依赖的参数保存于图像参数集中。一般情况SPS和PPS的NAL Unit通常位于整个码流的起始位置。但在某些特殊情况下,在码流中间也可能出现这两种结构。
image.png

profile_idc
level_idc
profile_idc和level_idc是指比特流所遵守的配置和级别。
主要用于告知解码器需要什么样的能力才能进行解码,比如最大帧率等。详见附录A。

profile_idc:本视频编码时遵循的profile,profile分为baseline,main,extended等,主要用来规定编码时是否采用某些特性,比如baseline profile就规定了只能使用I、P slice进行编码。

level_idc:本视频准循的level,level主要规定了每秒最多能处理多少个宏块,最大的帧大小,最大的解码缓存,最大比特率等这些性能相关的东西,如果是硬解码,则比较容易出现由于视频level太高而不能解码的情况。
constraint_set0_flag 比特流是否遵从基准简表,简表具体规定参考规范中附件A.2
constraint_set1_flag 比特流是否遵从主要简表
constraint_set2_flag 比特流是否遵从扩展简表
constraint_set3_flag
constraint_set4_flag
constraint_set5_flag
详见规范中的说明
seq_parameter_set_id 本SPS的ID,这个ID主要是给PPS使用的
chroma_format_idc YUV格式,见下面表中描述
与亮度取样对应的色度取样。可取值包括:0,1,2,3。当chroma_format_idc不存在时,应推断其值为1(4:2:0的色度格式)。
chroma_format_idc = 0,表示 色彩为单色
chroma_format_idc = 1,表示 YUV 4:2:0
chroma_format_idc = 2,表示 YUV 4:2:2
chroma_format_idc = 3,表示 YUV 4:4:4
separate_colour_plane_flag YUV 444格式专用
bit_depth_luma_minus8 指定了亮度矩阵的比特深度以及亮度量化参数范围偏移量;此处取0,表示每个亮度像素用8为表示,QpBdOffset参数为0;

BitDepthY = 8 + bit_depth_luma_minus8
QpBdOffsetY = 6 bit_depth_luma_minus8
| | bit_depth_chroma_minus8 | 与bit_depth_luma_minus8类似,只不过是针对色度的 | | log2_max_frame_num_minus4
pic_order_cnt_type
log2_max_pic_order_cnt_lsb_minus4
delta_pic_order_always_zero_flag
offset_for_non_ref_pic
offset_for_top_to_bottom_field
num_ref_frames_in_pic_order_cnt_cycle
| 用于求MaxFrameNum
image.png
这些值都用于计算POC | | max_num_ref_frames | 参考帧最多能有多少个 | | gaps_in_frame_num_value_allowed_falg | 由于码流在传输过程中可能出现丢包的情况,从而导致中间有帧缺失,如果制定了这个标记,则会在解码时对丢帧的情况进行调整,否则就当作意外丢失处理。 | | pic_width_in_mbs_minus1 | 图片宽度(宏块为单位)-1
可用于计算图像的宽
这里宏块的大小需要在level_idc所规定的范围之内
if (pSps->iMbWidth
pSps->iMbWidth
> (8 pSLevelLimits->uiMaxFS))
{
if ((pSps->iMbWidth
pSps->iMbWidth)
> (8 pSMaxLevelLimits->uiMaxFS))
{
Log ( “the pic_width_in_mbs exceeds the level limits!”);
}
else
{
Log (“the pic_width_in_mbs exceeds the level limits!”);
}
} | | pic_height_in_map_units_minus1 | 图片高度(宏块为单位)-1
可用于计算图片的高

这里宏块的大小需要在level_idc所规定的范围之内
if (pSps->iMbHeight
pSps->iMbHeight
> (8 pSLevelLimits->uiMaxFS))
{
if ((pSps->iMbHeight
pSps->iMbHeight)
> (8 pSMaxLevelLimits->uiMaxFS))
{
Log ( “the pic_width_in_mbs exceeds the level limits!”);
}
else
{
Log (“the pic_width_in_mbs exceeds the level limits!”);
}
}

uint64_t uiTmp64 = pSps->iMbWidth
pSps->iMbHeight;
if (uiTmp64 > pSLevelLimits->uiMaxFS)
{
if (uiTmp64 > pSMaxLevelLimits->uiMaxFS)
{
Log (“the total count of mb exceeds the level limits!”);
}
else
{
Log (“the total count of mb exceeds the level limits!”);
}
} | | frame_mbs_only_flag | 表示视频序列中的编码图像的类型
为0时表示编码图像为编码场
为1时表示编码图像为只包含帧宏块的编码帧
openh264只支持该值为1
该值影响图像高度的计算
frame_mbs_only_flag = 0,(pic_height_in_map_units_minus1 + 1)表示以宏块为单位的一场的高度;

frame_mbs_only_flag = 1,(pic_height_in_map_units_minus1 + 1)表示以宏块为单位的一帧的高度; | | mb_adaptive_frame_field_flag | 等于 0 表示在一个图像的帧和场宏块之间没有交换。
等于 1 表示在帧和帧内的场宏块之间可能会有交换。 | | direct_8x8_inference_flag | 在进行B Direct预测时,是否子宏块(8x8块)中的4个4x4块共用一个4x4的co-located,请参考h.264直接预测 中的direct_8x8_inference_flag关键字 | | frame_cropping_flag
| 表示宏块中的数据相对于原图是否有填充的标志
用于计算图像宽高 | | frame_crop_left_offset
frame_crop_right_offset
frame_crop_top_offset
frame_crop_bottom_offset | 在frame_cropping_flag为1时表示解码出来的图像,需要截取的偏移量。
用于计算图像宽高 | | vui_parameters_prsent_falg | SPS是否包含vui参数 | | vui_parameters | video usability information见附录E主要包含了视频的比例调整,比特率等信息 |


image.png**

Profile和Level详解

Profile(档次): H.264 规定了四种不同的档次,每个档次支持一组特定的编码功能,并支持一类特定的应用。
(1) 基本档次(Base Line Profile):支持帧内和帧间编码,支持利用基于上下文的适应变长编码(CAVLC)主要应用:可视电话,会议电视,和无线通讯等实时视频通讯领域。
只有I,P Slice;
nal_unit_type不应包括2-4的值;
序列参数集frame_mbs_only_flag = 1;
语法元素chroma_format_idc、bit_depth_luma_minus8、bit_depth_chroma_minus8、qpprime_y_zero_transform_bypass_flag和seq_scaling_matrix_present_flag不能出现在序列参数集中。
图片参数集weighted_pred_flag和weighted_bipred_idc均为0。
图像参数集entropy_coding_mode_flag = 0。
num_slice_groups_minus1取值0-7
语法元素transform_8x8_mode_flag、pic_scaling_matrix_present_flag和second_chroma_qp_index_offset不能出现在图片参数集中。
level_prefix不能大于15。
profile_idc ==66.
(2) 主要档次(Main profile):支持隔行视频,采用B片的帧间编码和采用加权预测的帧间编码,支持利用基于上下文的自适应算术编码(CABAC)主要应用:数字广播电视和数字视频存储
I, P, and B slice
nal_unit_type不应包括2-4的值;
语法元素chroma_format_idc、bit_depth_luma_minus8、bit_depth_chroma_minus8、qpprime_y_zero_transform_bypass_flag和seq_scaling_matrix_present_flag不能出现在序列参数集中。
num_slice_groups_minus1 = 0
redundant_pic_cnt_present_flag = 0
transform_8x8_mode_flag、pic_scaling_matrix_present_flag和second_chroma_qp_index_offset不能出现在图片参数集中
level_prefix应不大于15
profile_idc == 77
(3) 扩展档次(Extended Profile):支持码流之间有效的切换(SP和SI片)、改进误码性能,但不支持隔行视频和CABAC。
direct_8x8_inference_flag =1
chroma_format_idc、bit_depth_luma_minus8、bit_depth_chroma_minus8、qpprime_y_zero_transform_bypass_flag和seq_scaling_matrix_present_flag不能出现在序列参数集中。
entropy_coding_mode_flag = 0
num_slice_groups_minus1在0-7
transform_8x8_mode_flag、pic_scaling_matrix_present_flag和second_chroma_qp_index_offset不能出现在图片参数集中
level_prefix应不大于15
profile_idc = 88
(4) 高级画质(High profile):。在main Profile 的基础上增加了8x8内部预测、自定义量化、无损视频编码和更多的YUV 格式;
I, P, and B slice
nal_unit_type不应包括2-4的值;
num_slice_groups_minus1 = 0
redundant_pic_cnt_present_flag = 0
chroma_format_idc 0-1
bit_depth_luma_minus8 =0
bit_depth_chroma_minus8=0
qpprime_y_zero_transform_bypass_flag = 0
profile_idc is equal to 77 or 100,or constraint_set1_flag is equal to 1.
(5) High 10 profile 高级10位档次
(6) High 4:2:2 profile 高级4:2:2档次
(7) High 4:4:4 profile 高级4:4:4档次
不同档次的相关定义如下图:
NALU type 7-序列参数集 SPS - 图4
Level:级别是用来约束分辨率、帧率和码率的。所有概要文件都使用相同的级别定义集,但是单个实现可能为每个受支持的概要文件支持不同的级别。对于任何给定的概要文件,级别通常对应于解码器处理负载和内存能力。
帧fR =1/172 场 fR = 1/(2172)
常见视频分辨率所配套的级别:
480P(800x480):若帧率低于13.5用2.2,若帧率在13.5至27之间就用3,帧率高于27时用3.1。
720P(1280x720):一般为3.1。
1080P(1920x1080):一般为4
NALU type 7-序列参数集 SPS - 图5
NALU type 7-序列参数集 SPS - 图6
最大存储帧数(max stored frames):也叫最大参考帧数(ref)。
DPB:decoded
picture buffer,解码图片缓存区。在做视频解码时,需要将最近的若干幅参考帧缓存起来,这个缓冲区就叫做DPB。DPB一般以宏块数为单位(DpbMbs),计算公式为:
DpbMbs = ref(参考帧数)
PicWidthInMbs(水平宏块数) * FrameHeightInMbs(垂直宏块数)