参考博文
H264标准协议基础2
分析264协议中sps、pps的rbsp编码解码过程
void CheckZeroByteNonVCL(NALU_t *nalu, int * ret)
{
int CheckZeroByte=0;
//This function deals only with non-VCL NAL units
if(nalu->nal_unit_type>=1&&nalu->nal_unit_type<=5)
return;
//for SPS and PPS, zero_byte shall exist
if(nalu->nal_unit_type==NALU_TYPE_SPS || nalu->nal_unit_type==NALU_TYPE_PPS)
CheckZeroByte=1;
//check the possibility of the current NALU to be the start of a new access unit, according to 7.4.1.2.3
if(nalu->nal_unit_type==NALU_TYPE_AUD || nalu->nal_unit_type==NALU_TYPE_SPS ||
nalu->nal_unit_type==NALU_TYPE_PPS || nalu->nal_unit_type==NALU_TYPE_SEI ||
(nalu->nal_unit_type>=13 && nalu->nal_unit_type<=18))
{
if(LastAccessUnitExists)
{
LastAccessUnitExists=0; //deliver the last access unit to decoder
NALUCount=0;
}
}
NALUCount++;
//for the first NAL unit in an access unit, zero_byte shall exists
if(NALUCount==1)
CheckZeroByte=1;
if(CheckZeroByte && nalu->startcodeprefix_len==3)
{
printf("warning: zero_byte shall exist\n");
//because it is not a very serious problem, we may not indicate an error by setting ret to -1
//*ret=-1;
}
}
int EBSPtoRBSP(byte *streamBuffer, int end_bytepos, int begin_bytepos)
{
int i, j, count;
count = 0;
if(end_bytepos < begin_bytepos)
return end_bytepos;
j = begin_bytepos;
for(i = begin_bytepos; i < end_bytepos; i++)
{ //starting from begin_bytepos to avoid header information
if(count == ZEROBYTES_SHORTSTARTCODE && streamBuffer[i] == 0x03)
{
i++;
count = 0;
}
streamBuffer[j] = streamBuffer[i];
if(streamBuffer[i] == 0x00)
count++;
else
count = 0;
j++;
}
return j;
}
解析SPS的过程入下代码
int InterpretSPS (DataPartition *p, seq_parameter_set_rbsp_t *sps)
{
unsigned i;
int reserved_zero;
unsigned n_ScalingList;
Bitstream *s = p->bitstream;
assert (p != NULL);
assert (p->bitstream != NULL);
assert (p->bitstream->streamBuffer != 0);
assert (sps != NULL);
UsedBits = 0;
sps->profile_idc = u_v (8, "SPS: profile_idc" , s);
sps->constrained_set0_flag = u_1 ( "SPS: constrained_set0_flag" , s);
sps->constrained_set1_flag = u_1 ( "SPS: constrained_set1_flag" , s);
sps->constrained_set2_flag = u_1 ( "SPS: constrained_set2_flag" , s);
reserved_zero = u_v (5, "SPS: reserved_zero_5bits" , s);
assert (reserved_zero==0);
sps->level_idc = u_v (8, "SPS: level_idc" , s);
sps->seq_parameter_set_id = ue_v ("SPS: seq_parameter_set_id" , s);
sps->chroma_format_idc = 1;
sps->bit_depth_luma_minus8 = 0;
sps->bit_depth_chroma_minus8 = 0;
sps->lossless_qpprime_flag = 0;
sps->separate_colour_plane_flag = 0;
if((sps->profile_idc==100 ) ||
(sps->profile_idc==110) ||
(sps->profile_idc==122) ||
(sps->profile_idc==244) ||
(sps->profile_idc==44)
#if (MVC_EXTENSION_ENABLE)
|| (sps->profile_idc==118)
|| (sps->profile_idc==128)
#endif
)
{
sps->chroma_format_idc = ue_v ("SPS: chroma_format_idc" , s);
if(sps->chroma_format_idc == 3)
{
sps->separate_colour_plane_flag = u_1 ("SPS: separate_colour_plane_flag" , s);
}
sps->bit_depth_luma_minus8 = ue_v ("SPS: bit_depth_luma_minus8" , s);
sps->bit_depth_chroma_minus8 = ue_v ("SPS: bit_depth_chroma_minus8" , s);
//checking;
if((sps->bit_depth_luma_minus8+8 > sizeof(byte)*8) || (sps->bit_depth_chroma_minus8+8> sizeof(byte)*8))
error ("Source picture has higher bit depth than imgpel data type. \nPlease recompile with larger data type for imgpel.", 500);
sps->lossless_qpprime_flag = u_1 ("SPS: lossless_qpprime_y_zero_flag" , s);
sps->seq_scaling_matrix_present_flag = u_1 ( "SPS: seq_scaling_matrix_present_flag" , s);
if(sps->seq_scaling_matrix_present_flag)
{
n_ScalingList = (sps->chroma_format_idc != 3) ? 8 : 12;
for(i=0; iseq_scaling_list_present_flag[i] = u_1 ( "SPS: seq_scaling_list_present_flag" , s);
if(sps->seq_scaling_list_present_flag[i])
{
if(i<6)
Scaling_List(sps->ScalingList4x4[i], 16, &sps->UseDefaultScalingMatrix4x4Flag[i], s);
else
Scaling_List(sps->ScalingList8x8[i-6], 64, &sps->UseDefaultScalingMatrix8x8Flag[i-6], s);
}
}
}
}
sps->log2_max_frame_num_minus4 = ue_v ("SPS: log2_max_frame_num_minus4" , s);
sps->pic_order_cnt_type = ue_v ("SPS: pic_order_cnt_type" , s);
if (sps->pic_order_cnt_type == 0)
sps->log2_max_pic_order_cnt_lsb_minus4 = ue_v ("SPS: log2_max_pic_order_cnt_lsb_minus4" , s);
else if (sps->pic_order_cnt_type == 1)
{
sps->delta_pic_order_always_zero_flag = u_1 ("SPS: delta_pic_order_always_zero_flag" , s);
sps->offset_for_non_ref_pic = se_v ("SPS: offset_for_non_ref_pic" , s);
sps->offset_for_top_to_bottom_field = se_v ("SPS: offset_for_top_to_bottom_field" , s);
sps->num_ref_frames_in_pic_order_cnt_cycle = ue_v ("SPS: num_ref_frames_in_pic_order_cnt_cycle" , s);
for(i=0; inum_ref_frames_in_pic_order_cnt_cycle; i++)
sps->offset_for_ref_frame[i] = se_v ("SPS: offset_for_ref_frame[i]" , s);
}
sps->num_ref_frames = ue_v ("SPS: num_ref_frames" , s);
sps->gaps_in_frame_num_value_allowed_flag = u_1 ("SPS: gaps_in_frame_num_value_allowed_flag" , s);
sps->pic_width_in_mbs_minus1 = ue_v ("SPS: pic_width_in_mbs_minus1" , s);
sps->pic_height_in_map_units_minus1 = ue_v ("SPS: pic_height_in_map_units_minus1" , s);
sps->frame_mbs_only_flag = u_1 ("SPS: frame_mbs_only_flag" , s);
if (!sps->frame_mbs_only_flag)
{
sps->mb_adaptive_frame_field_flag = u_1 ("SPS: mb_adaptive_frame_field_flag" , s);
}
sps->direct_8x8_inference_flag = u_1 ("SPS: direct_8x8_inference_flag" , s);
sps->frame_cropping_flag = u_1 ("SPS: frame_cropping_flag" , s);
if (sps->frame_cropping_flag)
{
sps->frame_cropping_rect_left_offset = ue_v ("SPS: frame_cropping_rect_left_offset" , s);
sps->frame_cropping_rect_right_offset = ue_v ("SPS: frame_cropping_rect_right_offset" , s);
sps->frame_cropping_rect_top_offset = ue_v ("SPS: frame_cropping_rect_top_offset" , s);
sps->frame_cropping_rect_bottom_offset = ue_v ("SPS: frame_cropping_rect_bottom_offset" , s);
}
sps->vui_parameters_present_flag = u_1 ("SPS: vui_parameters_present_flag" , s);
if (sps->vui_parameters_present_flag)
{
printf ("VUI sequence parameters present but not supported, ignored, proceeding to next NALU\n");
}
sps->Valid = TRUE;
return UsedBits;
}
详细代码可参考
https://download.csdn.net/download/zhiyanzhai563/11854356