当前位置:   article > 正文

H264码流解析(三):解析SPS和PPS_sps pps 解析

sps pps 解析

上两篇里面写了划分NALU和指数哥伦布编码,这一篇主要写解析SPS和PPS。

1、下载bs.h头文件

下载bs.h头文件,并在项目中使用

下载地址

2、解析SPS

H264官方文档英文版下载

先看SPS(序列参数集)的结构,再7.3.2.1.1里面包含了SPS结构的介绍

 其中scaling_list函数在7.3.2.1.1.1里,如图所示

vui_parameters函数在E1.1里 ,如图所示

hrd_parameters()函数在E.1.2里面,如图 

具体代码如下,注意点就是我们要将EBSP的数据转变为RBSP的数据才可以解析,不然系欸小会有问题

  1. #define Extended_SAR 255
  2. bs_t* b;
  3. void scaling_list(uint32_t* scalingList, int sizeOfScalingList, uint32_t useDefaultScalingMatrixFlag) {
  4. int lastScale = 8;
  5. int nextScale = 8;
  6. for (int j = 0; j < sizeOfScalingList; j++) {
  7. if (nextScale != 0) {
  8. int delta_scale = bs_read_se(b);
  9. std::cout << "delta_scale:" << delta_scale << std::endl;
  10. nextScale = (lastScale + delta_scale + 256) % 256;
  11. useDefaultScalingMatrixFlag = (j == 0 && nextScale == 0);
  12. }
  13. scalingList[j] = (nextScale == 0) ? lastScale : nextScale;
  14. lastScale = scalingList[j];
  15. }
  16. }
  17. void hrd_parameters() {
  18. std::cout << "==hrd==" << std::endl;
  19. uint32_t cpb_cnt_minus1 = bs_read_ue(b);
  20. std::cout << "cpb_cnt_minus1:" << cpb_cnt_minus1 << std::endl;
  21. std::cout << "bit_rate_scale:" << bs_read_u(b, 4) << std::endl;
  22. std::cout << "cpb_size_scale:" << bs_read_u(b, 4) << std::endl;
  23. uint32_t bit_rate_value_minus1[5];
  24. uint32_t cpb_size_value_minus1[5];
  25. uint32_t cbr_flag[5];
  26. for (int SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; SchedSelIdx++) {
  27. bit_rate_value_minus1[SchedSelIdx] = bs_read_ue(b);
  28. cpb_size_value_minus1[SchedSelIdx] = bs_read_ue(b);
  29. cbr_flag[SchedSelIdx] = bs_read_u1(b);
  30. }
  31. std::cout << "initial_cpb_removal_delay_length_minus1:" << bs_read_u(b, 5) << std::endl;
  32. std::cout << "cpb_removal_delay_length_minus1:" << bs_read_u(b, 5) << std::endl;
  33. std::cout << "dpb_output_delay_length_minus1:" << bs_read_u(b, 5) << std::endl;
  34. std::cout << "time_offset_length:" << bs_read_u(b, 5) << std::endl;
  35. std::cout << "==hrd end==" << std::endl;
  36. }
  37. void vui_parameters() {
  38. std::cout << "==VUI==" << std::endl;
  39. uint32_t aspect_ratio_info_present_flag = bs_read_u1(b);
  40. std::cout << "aspect_ratio_info_present_flag:" << aspect_ratio_info_present_flag << std::endl;
  41. if (aspect_ratio_info_present_flag) {
  42. uint32_t aspect_ratio_idc = bs_read_u8(b);
  43. std::cout << " aspect_ratio_idc:" << aspect_ratio_idc << std::endl;
  44. if (aspect_ratio_idc == Extended_SAR){
  45. std::cout << " sar_width:" << bs_read_u(b,16);
  46. std::cout << " sar_height:" << bs_read_u(b, 16);
  47. }
  48. }
  49. uint32_t overscan_info_present_flag = bs_read_u1(b);
  50. std::cout << "overscan_info_present_flag:" << overscan_info_present_flag << std::endl;
  51. if (overscan_info_present_flag) {
  52. std::cout << " overscan_appropriate_flag:" << bs_read_u1(b) << std::endl;
  53. }
  54. uint32_t video_signal_type_present_flag = bs_read_u1(b);
  55. std::cout << "video_signal_type_present_flag:" << video_signal_type_present_flag << std::endl;
  56. if (video_signal_type_present_flag) {
  57. std::cout << " video_format:" << bs_read_u(b,3) << std::endl;
  58. std::cout << " video_full_range_flag:" << bs_read_u1(b) << std::endl;
  59. uint32_t colour_description_present_flag = bs_read_u1(b);
  60. std::cout << " colour_description_present_flag:" << colour_description_present_flag << std::endl;
  61. if (colour_description_present_flag) {
  62. std::cout << " colour_primaries:" << bs_read_u8(b) << std::endl;
  63. std::cout << " transfer_characteristics:" << bs_read_u8(b) << std::endl;
  64. std::cout << " matrix_coefficients:" << bs_read_u8(b) << std::endl;
  65. }
  66. }
  67. uint32_t chroma_loc_info_present_flag = bs_read_u1(b);
  68. std::cout << "chroma_loc_info_present_flag:" << chroma_loc_info_present_flag << std::endl;
  69. if (chroma_loc_info_present_flag) {
  70. std::cout << " chroma_sample_loc_type_top_field:" << bs_read_ue(b) << std::endl;
  71. std::cout << " chroma_sample_loc_type_bottom_field:" << bs_read_ue(b) << std::endl;
  72. }
  73. uint32_t timing_info_present_flag = bs_read_u1(b);
  74. std::cout << "timing_info_present_flag:" << timing_info_present_flag << std::endl;
  75. if (timing_info_present_flag) {
  76. std::cout << " num_units_in_tick:" << bs_read_u(b, 32) << std::endl;
  77. std::cout << " time_scale:" << bs_read_u(b, 32) << std::endl;
  78. std::cout << " fixed_frame_rate_flag:" << bs_read_u1(b) << std::endl;
  79. }
  80. uint32_t nal_hrd_parameters_present_flag = bs_read_u1(b);
  81. std::cout << "nal_hrd_parameters_present_flag:" << nal_hrd_parameters_present_flag << std::endl;
  82. if (nal_hrd_parameters_present_flag) {
  83. hrd_parameters();
  84. }
  85. uint32_t vcl_hrd_parameters_present_flag = bs_read_u1(b);
  86. std::cout << "vcl_hrd_parameters_present_flag:" << vcl_hrd_parameters_present_flag << std::endl;
  87. if (vcl_hrd_parameters_present_flag) {
  88. hrd_parameters();
  89. }
  90. if (nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag) {
  91. std::cout << " low_delay_hrd_flag:" << bs_read_u1(b) << std::endl;
  92. }
  93. std::cout << "pic_struct_present_flag:" << bs_read_u1(b) << std::endl;
  94. uint32_t bitstream_restriction_flag = bs_read_u1(b);
  95. std::cout << "bitstream_restriction_flag:" << bitstream_restriction_flag << std::endl;
  96. if (bitstream_restriction_flag) {
  97. std::cout << " motion_vectors_over_pic_boundaries_flag:" << bs_read_u1(b) << std::endl;
  98. std::cout << " max_bytes_per_pic_denom:" << bs_read_ue(b) << std::endl;
  99. std::cout << " max_bits_per_mb_denom:" << bs_read_ue(b) << std::endl;
  100. std::cout << " log2_max_mv_length_horizontal:" << bs_read_ue(b) << std::endl;
  101. std::cout << " log2_max_mv_length_vertical:" << bs_read_ue(b) << std::endl;
  102. std::cout << " num_reorder_frames:" << bs_read_ue(b) << std::endl;
  103. std::cout << " max_dec_frame_buffering:" << bs_read_ue(b) << std::endl;
  104. }
  105. std::cout << "==VUI== End" << std::endl;
  106. }
  107. std::vector<uint8_t> EBSP2RBSP(uint8_t* buffer, int len) {
  108. // 00 00 03 去掉03
  109. std::vector<uint8_t> ebsp;
  110. int i = 0;
  111. for (i = 0; i < len-2; ++i) {
  112. if (buffer[i] == 0x00 && buffer[i+1] == 0x00 && buffer[i+2] == 0x03) {
  113. ebsp.push_back(buffer[i++]);
  114. ebsp.push_back(buffer[i++]);
  115. }
  116. else {
  117. ebsp.push_back(buffer[i]);
  118. }
  119. }
  120. for (; i < len; ++i) {
  121. ebsp.push_back(buffer[i]);
  122. }
  123. return ebsp;
  124. }
  125. uint32_t chroma_format_idc; // pps要使用
  126. void ParseSPS(uint8_t* buffer, int len) {
  127. std::vector<uint8_t> ebsp = EBSP2RBSP(buffer, len);
  128. b = bs_new(ebsp.data(), ebsp.size());
  129. std::cout << "forbidden_zero_bit :" << bs_read_u(b, 1) << std::endl;
  130. std::cout << "nal_ref_idc:" << bs_read_u(b, 2) << std::endl;
  131. std::cout << "nal_unit_type:" << bs_read_u(b, 5) << std::endl;
  132. uint32_t profile_idc = bs_read_u8(b);
  133. std::cout << "profile_idc:" << profile_idc << std::endl;
  134. std::cout << "constraint_set0_flag:" << bs_read_u(b, 1) << std::endl;
  135. std::cout << "constraint_set1_flag:" << bs_read_u(b, 1) << std::endl;
  136. std::cout << "constraint_set2_flag:" << bs_read_u(b, 1) << std::endl;
  137. std::cout << "constraint_set3_flag:" << bs_read_u(b, 1) << std::endl;
  138. std::cout << "constraint_set4_flag:" << bs_read_u(b, 1) << std::endl;
  139. std::cout << "constraint_set5_flag:" << bs_read_u(b, 1) << std::endl;
  140. std::cout << "reserved_zero_2bits :" << bs_read_u(b, 2) << std::endl;
  141. std::cout << "level_idc:" << bs_read_u8(b) << std::endl;
  142. std::cout << "seq_parameter_set_id:" << bs_read_ue(b) << std::endl;
  143. if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 ||
  144. profile_idc == 244 || profile_idc == 44 || profile_idc == 83 ||
  145. profile_idc == 86 || profile_idc == 118 || profile_idc == 128) {
  146. chroma_format_idc = bs_read_ue(b);
  147. std::cout << " chroma_format_idc:" << chroma_format_idc << std::endl;
  148. if (chroma_format_idc == 3) {
  149. std::cout << " separate_colour_plane_flag:" << bs_read_u(b, 1) << std::endl;
  150. }
  151. std::cout << " bit_depth_luma_minus8:" << bs_read_ue(b) << std::endl;
  152. std::cout << " bit_depth_chroma_minus8:" << bs_read_ue(b) << std::endl;
  153. std::cout << " qpprime_y_zero_transform_bypass_flag:" << bs_read_u(b, 1) << std::endl;
  154. uint32_t seq_scaling_matrix_present_flag = bs_read_u(b, 1);
  155. std::cout << " seq_scaling_matrix_present_flag:" << seq_scaling_matrix_present_flag << std::endl;
  156. if (seq_scaling_matrix_present_flag) {
  157. uint32_t seq_scaling_list_present_flag[12];
  158. uint32_t* ScalingList4x4[12];
  159. uint32_t UseDefaultScalingMatrix4x4Flag[12];
  160. uint32_t* ScalingList8x8[12];
  161. uint32_t UseDefaultScalingMatrix8x8Flag[12];
  162. for (int i = 0; i < ((chroma_format_idc != 3) ? 8 : 12); i++) {
  163. seq_scaling_list_present_flag[i] = bs_read_u(b, 1);
  164. if (seq_scaling_list_present_flag[i]) {
  165. if (i < 6) {
  166. scaling_list(ScalingList4x4[i], 16,
  167. UseDefaultScalingMatrix4x4Flag[i]);
  168. }
  169. else {
  170. scaling_list(ScalingList8x8[i-6], 64,
  171. UseDefaultScalingMatrix8x8Flag[i-6]);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. std::cout << "log2_max_frame_num_minus4:" << bs_read_ue(b) << std::endl;
  178. uint32_t pic_order_cnt_type = bs_read_ue(b);
  179. std::cout << "pic_order_cnt_type:" << pic_order_cnt_type << std::endl;
  180. if (pic_order_cnt_type == 0) {
  181. std::cout << " log2_max_pic_order_cnt_lsb_minus4:" << bs_read_ue(b) << std::endl;
  182. }
  183. else if (pic_order_cnt_type == 1) {
  184. std::cout << " delta_pic_order_always_zero_flag:" << bs_read_u(b, 1) << std::endl;
  185. std::cout << " offset_for_non_ref_pic:" << bs_read_se(b) << std::endl;
  186. std::cout << " offset_for_top_to_bottom_field:" << bs_read_se(b) << std::endl;
  187. uint32_t num_ref_frames_in_pic_order_cnt_cycle = bs_read_ue(b);
  188. std::cout << " num_ref_frames_in_pic_order_cnt_cycle:" << num_ref_frames_in_pic_order_cnt_cycle << std::endl;
  189. int offset_for_ref_frame[256];
  190. for (int i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) {
  191. offset_for_ref_frame[i] = bs_read_se(b);
  192. }
  193. }
  194. std::cout << "max_num_ref_frames:" << bs_read_ue(b) << std::endl;
  195. std::cout << "gaps_in_frame_num_value_allowed_flag:" << bs_read_u(b,1) << std::endl;
  196. std::cout << "pic_width_in_mbs_minus1:" << bs_read_ue(b) << std::endl;
  197. std::cout << "pic_height_in_map_units_minus1:" << bs_read_ue(b) << std::endl;
  198. uint32_t frame_mbs_only_flag = bs_read_u(b, 1);
  199. std::cout << "frame_mbs_only_flag:" << frame_mbs_only_flag << std::endl;
  200. if (!frame_mbs_only_flag) {
  201. std::cout << " mb_adaptive_frame_field_flag:" << bs_read_u(b, 1) << std::endl;
  202. }
  203. std::cout << "direct_8x8_inference_flag:" << bs_read_u(b, 1) << std::endl;
  204. uint32_t frame_cropping_flag = bs_read_u(b, 1);
  205. std::cout << "frame_cropping_flag:" << frame_cropping_flag << std::endl;
  206. if (frame_cropping_flag) {
  207. std::cout << " frame_crop_left_offset:" << bs_read_ue(b) << std::endl;
  208. std::cout << " frame_crop_right_offset:" << bs_read_ue(b) << std::endl;
  209. std::cout << " frame_crop_top_offset:" << bs_read_ue(b) << std::endl;
  210. std::cout << " frame_crop_bottom_offset:" << bs_read_ue(b) << std::endl;
  211. }
  212. uint32_t vui_parameters_present_flag = bs_read_u(b, 1);
  213. std::cout << "vui_parameters_present_flag:" << vui_parameters_present_flag << std::endl;
  214. if (vui_parameters_present_flag) {
  215. vui_parameters();
  216. }
  217. bs_free(b);
  218. }

 通过解析我们可以发现SPS序列参数集里面包含以下内容:

(1)、profile_idc,指明所需要的profile

(2)、level_idc,指明所需要的level

(3)、sps的id,后面pps根据sps的id找到对应的sps

(4)、max_num_ref_frames用于参考帧的最大数目

(5)、图像的宽pic_width_in_mbs_minus1和高pic_height_in_map_units_minus1,计算方法width = (pic_width_in_mbs_minus1+1)*16 。height = (pic_height_in_map_units_minus1+1)*16

(6)、video_full_range_flag ,表示video range还是full fange

(7)、帧速率,每一帧之间的间隔

(8)、 frame_mbs_only_flag,宏块编码方式,为1表示所有宏块都是用帧编码,为0表示可能使用帧编码也可能使用场编码

(9)、log2_max_pic_order_cnt_lsb_minus4,表示POC的上限

(10)、pic_order_cnt_type,指明poc的编码方式,poc标识图像的播放顺序

等等

具体可以参考下面两篇文章

H264___sps_pps___分析_郭风朴(guofengpu)的Android影音专栏-CSDN博客

H264参数结构三:序列参数集层(SPS) & 图像参数集语义_heanyu的专栏-CSDN博客

3、解析PPS

PPS:图像参数集,具体内容如下

具体代码如下所示

  1. // 一个bit 1,若干个bit 0
  2. void rbsp_trailing_bits() {
  3. int rbsp_stop_one_bit = bs_read_u1(b); // equal to 1
  4. std::cout << "rbsp_stop_one_bit:" << rbsp_stop_one_bit << std::endl;
  5. while (!bs_byte_aligned(b)){
  6. int rbsp_alignment_zero_bit = bs_read_u1(b); // equal to 0
  7. }
  8. }
  9. // 是不是有更多的RBSP数据
  10. uint32_t more_rbsp_data() {
  11. if (bs_eof(b)) { return 0; }
  12. if (bs_peek_u1(b) == 1) { return 0; } // if next bit is 1, we've reached the stop bit
  13. return 1;
  14. }
  15. void ParsePPS(uint8_t* buffer, int len) {
  16. std::cout << "=========================PPS=======================" << std::endl;
  17. std::vector<uint8_t> ebsp = EBSP2RBSP(buffer, len);
  18. // std::cout << "pps buffer len:" << ebsp.size() << std::endl;
  19. b = bs_new(ebsp.data(), ebsp.size());
  20. std::cout << "forbidden_zero_bit :" << bs_read_u(b, 1) << std::endl;
  21. std::cout << "nal_ref_idc:" << bs_read_u(b, 2) << std::endl;
  22. std::cout << "nal_unit_type:" << bs_read_u(b, 5) << std::endl;
  23. std::cout << "pic_parameter_set_id:" << bs_read_ue(b) << std::endl;
  24. std::cout << "seq_parameter_set_id:" << bs_read_ue(b) << std::endl;
  25. std::cout << "entropy_coding_mode_flag:" << bs_read_u1(b) << std::endl;
  26. std::cout << "bottom_field_pic_order_in_frame_present_flag:" << bs_read_u1(b) << std::endl;
  27. uint32_t num_slice_groups_minus1 = bs_read_ue(b);
  28. std::cout << "num_slice_groups_minus1:" << num_slice_groups_minus1 << std::endl;
  29. if (num_slice_groups_minus1 > 0) {
  30. uint32_t slice_group_map_type = bs_read_ue(b);
  31. std::cout << " slice_group_map_type:" << slice_group_map_type << std::endl;
  32. if (slice_group_map_type == 0) {
  33. uint32_t run_length_minus1[8];
  34. for (int iGroup = 0; iGroup <= num_slice_groups_minus1; iGroup++) {
  35. run_length_minus1[iGroup] = bs_read_ue(b);
  36. }
  37. }
  38. else if (slice_group_map_type == 2) {
  39. uint32_t top_left[8];
  40. uint32_t bottom_right[8];
  41. for (int iGroup = 0; iGroup < num_slice_groups_minus1; iGroup++) {
  42. top_left[iGroup] = bs_read_ue(b);
  43. bottom_right[iGroup] = bs_read_ue(b);
  44. }
  45. }
  46. else if (slice_group_map_type == 3 ||
  47. slice_group_map_type == 4 ||
  48. slice_group_map_type == 5) {
  49. std::cout << " slice_group_change_direction_flag:" << bs_read_u1(b) << std::endl;
  50. std::cout << " slice_group_change_rate_minus1:" << bs_read_ue(b) << std::endl;
  51. }
  52. else if (slice_group_map_type == 6) {
  53. uint32_t pic_size_in_map_units_minus1 = bs_read_ue(b);
  54. std::cout << " pic_size_in_map_units_minus1:" << pic_size_in_map_units_minus1 << std::endl;
  55. uint32_t slice_group_id[8];
  56. for (int i = 0; i <= pic_size_in_map_units_minus1; i++) {
  57. slice_group_id[i] = bs_read_ue(b);
  58. }
  59. }
  60. }
  61. std::cout << "num_ref_idx_l0_default_active_minus1:" << bs_read_ue(b) << std::endl;
  62. std::cout << "num_ref_idx_l1_default_active_minus1:" << bs_read_ue(b) << std::endl;
  63. std::cout << "weighted_pred_flag:" << bs_read_u1(b) << std::endl;
  64. std::cout << "weighted_bipred_idc:" << bs_read_u(b,2) << std::endl;
  65. std::cout << "pic_init_qp_minus26:" << bs_read_se(b) << std::endl;
  66. std::cout << "pic_init_qs_minus26:" << bs_read_se(b) << std::endl;
  67. std::cout << "chroma_qp_index_offset:" << bs_read_se(b) << std::endl;
  68. std::cout << "deblocking_filter_control_present_flag:" << bs_read_u1(b) << std::endl;
  69. std::cout << "constrained_intra_pred_flag:" << bs_read_u1(b) << std::endl;
  70. std::cout << "redundant_pic_cnt_present_flag:" << bs_read_u1(b) << std::endl;
  71. if (more_rbsp_data()) {
  72. uint32_t transform_8x8_mode_flag = bs_read_u1(b);
  73. std::cout << " transform_8x8_mode_flag:" << transform_8x8_mode_flag << std::endl;
  74. uint32_t pic_scaling_matrix_present_flag = bs_read_u1(b);
  75. std::cout << " pic_scaling_matrix_present_flag:" << std::endl;
  76. uint32_t pic_scaling_list_present_flag[6];
  77. uint32_t* ScalingList4x4[12];
  78. uint32_t UseDefaultScalingMatrix4x4Flag[12];
  79. uint32_t* ScalingList8x8[12];
  80. uint32_t UseDefaultScalingMatrix8x8Flag[12];
  81. if (pic_scaling_matrix_present_flag) {
  82. for (int i = 0; i < 6 + ((chroma_format_idc != 3) ? 2 : 6) * transform_8x8_mode_flag; i++) {
  83. pic_scaling_list_present_flag[i] = bs_read_u1(b);
  84. if (pic_scaling_list_present_flag[i]) {
  85. if (i < 6) {
  86. scaling_list(ScalingList4x4[i], 16,
  87. UseDefaultScalingMatrix4x4Flag[i]);
  88. }
  89. else {
  90. scaling_list(ScalingList8x8[i - 6], 64,
  91. UseDefaultScalingMatrix8x8Flag[i - 6]);
  92. }
  93. }
  94. }
  95. }
  96. std::cout << " second_chroma_qp_index_offset:" << bs_read_se(b) << std::endl;
  97. }
  98. rbsp_trailing_bits();
  99. bs_free(b);
  100. }

 PPS里面主要包含以下内容

(1)、PPS的id

(2)、SPS的id

(3)、entropy_coding_mode_flag 熵编码的选择,为0表示使用CAVLC,为表示CABAC

(4)、num_slice_groups_minus1,加1表示图像中使用片组的个数

等等

当然,我们也可以不适用bs.h头文件,也可以自己写解析过程,主要就是bs_read_u1,bs_read_u,bs_read_u8,bs_read_ue,bs_read_se这几个函数

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/659653
推荐阅读
相关标签
  

闽ICP备14008679号