|
| 1 | +/* |
| 2 | + * Intel License |
| 3 | + */ |
| 4 | + |
| 5 | +#include "webrtc/modules/rtp_rtcp/source/h265_sps_parser.h" |
| 6 | + |
| 7 | +#include "webrtc/base/bitbuffer.h" |
| 8 | +#include "webrtc/base/bytebuffer.h" |
| 9 | +#include "webrtc/base/logging.h" |
| 10 | + |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#define RETURN_FALSE_ON_FAIL(x) \ |
| 14 | + if (!(x)) { \ |
| 15 | + return false; \ |
| 16 | + } |
| 17 | + |
| 18 | +namespace webrtc { |
| 19 | + |
| 20 | +H265SpsParser::H265SpsParser(const uint8_t* sps, size_t byte_length) |
| 21 | + : sps_(sps), byte_length_(byte_length), width_(0), height_(0) { |
| 22 | +} |
| 23 | + |
| 24 | +bool H265SpsParser::Parse() { |
| 25 | + // General note: this is based off the 04/2015 version of the H.265 standard. |
| 26 | + // You can find it on this page: |
| 27 | + // http://www.itu.int/rec/T-REC-H.265 |
| 28 | + |
| 29 | + const char* sps_bytes = reinterpret_cast<const char*>(sps_); |
| 30 | + // First, parse out rbsp, which is basically the source buffer minus emulation |
| 31 | + // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in |
| 32 | + // section 7.3.1.1 of the H.265 standard, similar to H264. |
| 33 | + rtc::ByteBufferWriter rbsp_buffer; |
| 34 | + for (size_t i = 0; i < byte_length_;) { |
| 35 | + // Be careful about over/underflow here. byte_length_ - 3 can underflow, and |
| 36 | + // i + 3 can overflow, but byte_length_ - i can't, because i < byte_length_ |
| 37 | + // above, and that expression will produce the number of bytes left in |
| 38 | + // the stream including the byte at i. |
| 39 | + if (byte_length_ - i >= 3 && sps_[i] == 0 && sps_[i + 1] == 0 && |
| 40 | + sps_[i + 2] == 3) { |
| 41 | + // Two rbsp bytes + the emulation byte. |
| 42 | + rbsp_buffer.WriteBytes(sps_bytes + i, 2); |
| 43 | + i += 3; |
| 44 | + } else { |
| 45 | + // Single rbsp byte. |
| 46 | + rbsp_buffer.WriteBytes(sps_bytes + i, 1); |
| 47 | + i++; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Now, we need to use a bit buffer to parse through the actual HEVC SPS |
| 52 | + // format. See Section 7.3.2.1.1 ("Sequence parameter set data syntax") of the |
| 53 | + // H.265 standard for a complete description. |
| 54 | + // Since we only care about resolution, we ignore the majority of fields, but |
| 55 | + // we still have to actively parse through a lot of the data, since many of |
| 56 | + // the fields have variable size. |
| 57 | + // Unlike H264, for H265, the picture size is indicated by pic_width_in_luma_samples |
| 58 | + // and pic_height_in_luma_samples, if conformance_window_flag !=1; |
| 59 | + // When conformance_window_flag is 1, the width is adjusted with con_win_xx_offset |
| 60 | + // |
| 61 | + rtc::BitBuffer parser(reinterpret_cast<const uint8_t*>(rbsp_buffer.Data()), |
| 62 | + rbsp_buffer.Length()); |
| 63 | + |
| 64 | + // The golomb values we have to read, not just consume. |
| 65 | + uint32_t golomb_ignored; |
| 66 | + |
| 67 | + // separate_colour_plane_flag is optional (assumed 0), but has implications |
| 68 | + // about the ChromaArrayType, which modifies how we treat crop coordinates. |
| 69 | + uint32_t separate_colour_plane_flag = 0; |
| 70 | + // chroma_format_idc will be ChromaArrayType if separate_colour_plane_flag is |
| 71 | + // 0. It defaults to 1, when not specified. |
| 72 | + uint32_t chroma_format_idc = 1; |
| 73 | + |
| 74 | + |
| 75 | + // sps_video_parameter_set_id: u(4) |
| 76 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBits(4)); |
| 77 | + // sps_max_sub_layers_minus1: u(3) |
| 78 | + uint32_t sps_max_sub_layers_minus1 = 0; |
| 79 | + RETURN_FALSE_ON_FAIL(parser.ReadBits(&sps_max_sub_layers_minus1, 3)); |
| 80 | + // sps_temporal_id_nesting_flag: u(1) |
| 81 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBits(1)); |
| 82 | + // profile_tier_level(1, sps_max_sub_layers_minus1). We are acutally not |
| 83 | + // using them, so read/skip over it. |
| 84 | + // general_profile_space+general_tier_flag+general_prfile_idc: u(8) |
| 85 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBytes(1)); |
| 86 | + // general_profile_compatabilitiy_flag[32] |
| 87 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBytes(4)); |
| 88 | + // general_progressive_source_flag + interlaced_source_flag+ non-packed_constraint |
| 89 | + // flag + frame_only_constraint_flag: u(4) |
| 90 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBits(4)); |
| 91 | + // general_profile_idc decided flags or reserved. u(43) |
| 92 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBits(43)); |
| 93 | + // general_inbld_flag or reserved 0: u(1) |
| 94 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBits(1)); |
| 95 | + // general_level_idc: u(8) |
| 96 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBytes(1)); |
| 97 | + // if max_sub_layers_minus1 >=1, read the sublayer profile information |
| 98 | + std::vector<uint32_t> sub_layer_profile_present_flags; |
| 99 | + std::vector<uint32_t> sub_layer_level_present_flags; |
| 100 | + uint32_t sub_layer_profile_present = 0; |
| 101 | + uint32_t sub_layer_level_present = 0; |
| 102 | + for (uint32_t i = 0; i < sps_max_sub_layers_minus1; i++) { |
| 103 | + //sublayer_profile_present_flag and sublayer_level_presnet_flag: u(2) |
| 104 | + RETURN_FALSE_ON_FAIL(parser.ReadBits(&sub_layer_profile_present, 1)); |
| 105 | + RETURN_FALSE_ON_FAIL(parser.ReadBits(&sub_layer_level_present, 1)); |
| 106 | + sub_layer_profile_present_flags.push_back(sub_layer_profile_present); |
| 107 | + sub_layer_level_present_flags.push_back(sub_layer_level_present); |
| 108 | + } |
| 109 | + if (sps_max_sub_layers_minus1 > 0) { |
| 110 | + for (uint32_t j = sps_max_sub_layers_minus1; j < 8; j++) { |
| 111 | + // reserved 2 bits: u(2) |
| 112 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBits(2)); |
| 113 | + } |
| 114 | + } |
| 115 | + for (uint32_t k = 0; k < sps_max_sub_layers_minus1; k++) { |
| 116 | + if(sub_layer_profile_present_flags[k]) {// |
| 117 | + // sub_layer profile_space/tier_flag/profile_idc. ignored. u(8) |
| 118 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBytes(1)); |
| 119 | + // profile_compatability_flag: u(32) |
| 120 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBytes(4)); |
| 121 | + // sub_layer progressive_source_flag/interlaced_source_flag/ |
| 122 | + // non_packed_constraint_flag/frame_only_constraint_flag: u(4) |
| 123 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBits(4)); |
| 124 | + // following 43-bits are profile_idc specific. We simply read/skip it. u(43) |
| 125 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBits(43)); |
| 126 | + // 1-bit profile_idc specific inbld flag. We simply read/skip it. u(1) |
| 127 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBits(1)); |
| 128 | + } |
| 129 | + if (sub_layer_level_present_flags[k]) { |
| 130 | + // sub_layer_level_idc: u(8) |
| 131 | + RETURN_FALSE_ON_FAIL(parser.ConsumeBytes(1)); |
| 132 | + } |
| 133 | + } |
| 134 | + //sps_seq_parameter_set_id: ue(v) |
| 135 | + RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); |
| 136 | + // chrome_format_idc: ue(v) |
| 137 | + RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&chroma_format_idc)); |
| 138 | + if (chroma_format_idc == 3) { |
| 139 | + // seperate_colour_plane_flag: u(1) |
| 140 | + RETURN_FALSE_ON_FAIL(parser.ReadBits(&separate_colour_plane_flag, 1)); |
| 141 | + } |
| 142 | + uint32_t pic_width_in_luma_samples = 0; |
| 143 | + uint32_t pic_height_in_luma_samples = 0; |
| 144 | + // pic_width_in_luma_samples: ue(v) |
| 145 | + RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&pic_width_in_luma_samples)); |
| 146 | + // pic_height_in_luma_samples: ue(v) |
| 147 | + RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&pic_height_in_luma_samples)); |
| 148 | + // conformance_window_flag: u(1) |
| 149 | + uint32_t conformance_window_flag = 0; |
| 150 | + RETURN_FALSE_ON_FAIL(parser.ReadBits(&conformance_window_flag, 1)); |
| 151 | + |
| 152 | + uint32_t conf_win_left_offset = 0; |
| 153 | + uint32_t conf_win_right_offset = 0; |
| 154 | + uint32_t conf_win_top_offset = 0; |
| 155 | + uint32_t conf_win_bottom_offset = 0; |
| 156 | + if (conformance_window_flag) { |
| 157 | + // conf_win_left_offset: ue(v) |
| 158 | + RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&conf_win_left_offset)); |
| 159 | + // conf_win_right_offset: ue(v) |
| 160 | + RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&conf_win_right_offset)); |
| 161 | + // conf_win_top_offset: ue(v) |
| 162 | + RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&conf_win_top_offset)); |
| 163 | + // conf_win_bottom_offset: ue(v) |
| 164 | + RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&conf_win_bottom_offset)); |
| 165 | + } |
| 166 | + |
| 167 | + //For enough to get the resolution information. calcaluate according to HEVC spec 7.4.3.2 |
| 168 | + int width = 0; |
| 169 | + int height = 0; |
| 170 | + |
| 171 | + width = pic_width_in_luma_samples; |
| 172 | + height = pic_height_in_luma_samples; |
| 173 | + |
| 174 | + if (conformance_window_flag) { |
| 175 | + int sub_width_c = ((1 == chroma_format_idc) || (2 == chroma_format_idc)) && |
| 176 | + (0 == separate_colour_plane_flag) ? 2 : 1; |
| 177 | + int sub_height_c = (1 == chroma_format_idc) && (0 == separate_colour_plane_flag) ? 2 : 1; |
| 178 | + //the offset includes the pixel within conformance window. so don't need to +1 as per spec |
| 179 | + width -= sub_width_c*(conf_win_right_offset + conf_win_left_offset); |
| 180 | + height -= sub_height_c*(conf_win_top_offset + conf_win_bottom_offset); |
| 181 | + } |
| 182 | + |
| 183 | + width_ = width; |
| 184 | + height_ = height; |
| 185 | + return true; |
| 186 | + |
| 187 | +} |
| 188 | + |
| 189 | +} // namespace webrtc |
0 commit comments