h265_sps_parser.h (5094B)
1 /* 2 * Copyright (c) 2023 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef COMMON_VIDEO_H265_H265_SPS_PARSER_H_ 12 #define COMMON_VIDEO_H265_H265_SPS_PARSER_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <optional> 17 #include <vector> 18 19 #include "api/array_view.h" 20 #include "rtc_base/bitstream_reader.h" 21 #include "rtc_base/system/rtc_export.h" 22 23 namespace webrtc { 24 25 // For explanations of each struct and its members, see H.265 specification 26 // at http://www.itu.int/rec/T-REC-H.265. 27 enum { 28 kMaxLongTermRefPicSets = 32, // 7.4.3.2.1 29 kMaxShortTermRefPicSets = 64, // 7.4.3.2.1 30 kMaxSubLayers = 7, // 7.4.3.1 & 7.4.3.2.1 [v|s]ps_max_sub_layers_minus1 + 1 31 }; 32 33 enum H265ProfileIdc { 34 kProfileIdcMain = 1, 35 kProfileIdcMain10 = 2, 36 kProfileIdcMainStill = 3, 37 kProfileIdcRangeExtensions = 4, 38 kProfileIdcHighThroughput = 5, 39 kProfileIdcMultiviewMain = 6, 40 kProfileIdcScalableMain = 7, 41 kProfileIdc3dMain = 8, 42 kProfileIdcScreenContentCoding = 9, 43 kProfileIdcScalableRangeExtensions = 10, 44 kProfileIdcHighThroughputScreenContentCoding = 11, 45 }; 46 47 // A class for parsing out sequence parameter set (SPS) data from an H265 NALU. 48 class RTC_EXPORT H265SpsParser { 49 public: 50 struct ProfileTierLevel { 51 ProfileTierLevel(); 52 // Syntax elements. 53 int general_profile_idc = 0; 54 int general_level_idc = 0; // 30x the actual level. 55 uint32_t general_profile_compatibility_flags = 0; 56 bool general_progressive_source_flag = false; 57 bool general_interlaced_source_flag = false; 58 bool general_non_packed_constraint_flag = false; 59 bool general_frame_only_constraint_flag = false; 60 bool general_one_picture_only_constraint_flag = false; 61 }; 62 63 struct ShortTermRefPicSet { 64 ShortTermRefPicSet(); 65 66 // Syntax elements. 67 uint32_t num_negative_pics = 0; 68 uint32_t num_positive_pics = 0; 69 uint32_t delta_poc_s0[kMaxShortTermRefPicSets] = {}; 70 uint32_t used_by_curr_pic_s0[kMaxShortTermRefPicSets] = {}; 71 uint32_t delta_poc_s1[kMaxShortTermRefPicSets] = {}; 72 uint32_t used_by_curr_pic_s1[kMaxShortTermRefPicSets] = {}; 73 74 // Calculated fields. 75 uint32_t num_delta_pocs = 0; 76 }; 77 78 // The parsed state of the SPS. Only some select values are stored. 79 // Add more as they are actually needed. 80 struct RTC_EXPORT SpsState { 81 SpsState() = default; 82 83 uint32_t sps_max_sub_layers_minus1 = 0; 84 uint32_t chroma_format_idc = 0; 85 uint32_t separate_colour_plane_flag = 0; 86 uint32_t pic_width_in_luma_samples = 0; 87 uint32_t pic_height_in_luma_samples = 0; 88 uint32_t log2_max_pic_order_cnt_lsb_minus4 = 0; 89 uint32_t sps_max_dec_pic_buffering_minus1[kMaxSubLayers] = {}; 90 uint32_t log2_min_luma_coding_block_size_minus3 = 0; 91 uint32_t log2_diff_max_min_luma_coding_block_size = 0; 92 uint32_t sample_adaptive_offset_enabled_flag = 0; 93 uint32_t num_short_term_ref_pic_sets = 0; 94 std::vector<H265SpsParser::ShortTermRefPicSet> short_term_ref_pic_set; 95 uint32_t long_term_ref_pics_present_flag = 0; 96 uint32_t num_long_term_ref_pics_sps = 0; 97 std::vector<uint32_t> used_by_curr_pic_lt_sps_flag; 98 uint32_t sps_temporal_mvp_enabled_flag = 0; 99 uint32_t width = 0; 100 uint32_t height = 0; 101 uint32_t sps_id = 0; 102 uint32_t vps_id = 0; 103 uint32_t pic_width_in_ctbs_y = 0; 104 uint32_t pic_height_in_ctbs_y = 0; 105 uint32_t bit_depth_luma_minus8 = 0; 106 }; 107 108 // Unpack RBSP and parse SPS state from the supplied buffer. 109 static std::optional<SpsState> ParseSps(ArrayView<const uint8_t> data); 110 // TODO: bugs.webrtc.org/42225170 - Deprecate. 111 static inline std::optional<SpsState> ParseSps(const uint8_t* data, 112 size_t length) { 113 return ParseSps(MakeArrayView(data, length)); 114 } 115 116 static bool ParseScalingListData(BitstreamReader& reader); 117 118 static std::optional<ShortTermRefPicSet> ParseShortTermRefPicSet( 119 uint32_t st_rps_idx, 120 uint32_t num_short_term_ref_pic_sets, 121 const std::vector<ShortTermRefPicSet>& ref_pic_sets, 122 uint32_t sps_max_dec_pic_buffering_minus1, 123 BitstreamReader& reader); 124 125 static std::optional<H265SpsParser::ProfileTierLevel> ParseProfileTierLevel( 126 bool profile_present, 127 int max_num_sub_layers_minus1, 128 BitstreamReader& reader); 129 130 protected: 131 // Parse the SPS state, for a bit buffer where RBSP decoding has already been 132 // performed. 133 static std::optional<SpsState> ParseSpsInternal( 134 ArrayView<const uint8_t> buffer); 135 136 // From Table A.8 - General tier and level limits. 137 static int GetMaxLumaPs(int general_level_idc); 138 // From A.4.2 - Profile-specific level limits for the video profiles. 139 static size_t GetDpbMaxPicBuf(int general_profile_idc); 140 }; 141 142 } // namespace webrtc 143 #endif // COMMON_VIDEO_H265_H265_SPS_PARSER_H_