Shaka Packager SDK
Loading...
Searching...
No Matches
hevc_decoder_configuration_record.cc
1// Copyright 2015 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#include <packager/media/codecs/hevc_decoder_configuration_record.h>
8
9#include <absl/log/check.h>
10#include <absl/strings/escaping.h>
11#include <absl/strings/str_format.h>
12#include <absl/strings/str_join.h>
13
14#include <packager/media/base/buffer_reader.h>
15#include <packager/media/base/rcheck.h>
16#include <packager/media/codecs/h265_parser.h>
17#include <packager/utils/bytes_to_string_view.h>
18
19namespace shaka {
20namespace media {
21
22namespace {
23
24// ISO/IEC 14496-15:2014 Annex E.
25std::string GeneralProfileSpaceAsString(uint8_t general_profile_space) {
26 switch (general_profile_space) {
27 case 0:
28 return "";
29 case 1:
30 return "A";
31 case 2:
32 return "B";
33 case 3:
34 return "C";
35 default:
36 LOG(WARNING) << "Unexpected general_profile_space "
37 << general_profile_space;
38 return "";
39 }
40}
41
42std::string TrimLeadingZeros(const std::string& str) {
43 DCHECK_GT(str.size(), 0u);
44 for (size_t i = 0; i < str.size(); ++i) {
45 if (str[i] == '0')
46 continue;
47 return str.substr(i);
48 }
49 return "0";
50}
51
52// Encode the 32 bits input, but in reverse bit order, i.e. bit [31] as the most
53// significant bit, followed by, bit [30], and down to bit [0] as the least
54// significant bit, where bits [i] for i in the range of 0 to 31, inclusive, are
55// specified in ISO/IEC 23008‐2, encoded in hexadecimal (leading zeroes may be
56// omitted).
57std::string ReverseBitsAndHexEncode(uint32_t x) {
58 x = ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1);
59 x = ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2);
60 x = ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4);
61 const uint8_t bytes[] = {static_cast<uint8_t>(x & 0xFF),
62 static_cast<uint8_t>((x >> 8) & 0xFF),
63 static_cast<uint8_t>((x >> 16) & 0xFF),
64 static_cast<uint8_t>((x >> 24) & 0xFF)};
65 return TrimLeadingZeros(absl::BytesToHexString(
66 byte_array_to_string_view(bytes, std::size(bytes))));
67}
68
69} // namespace
70
71HEVCDecoderConfigurationRecord::HEVCDecoderConfigurationRecord() = default;
72
73HEVCDecoderConfigurationRecord::~HEVCDecoderConfigurationRecord() = default;
74
75bool HEVCDecoderConfigurationRecord::ParseInternal() {
76 BufferReader reader(data(), data_size());
77
78 uint8_t profile_indication = 0;
79 uint8_t length_size_minus_one = 0;
80 uint8_t num_of_arrays = 0;
81 if (!layered_) {
82 RCHECK(reader.Read1(&version_) && version_ == 1 &&
83 reader.Read1(&profile_indication) &&
84 reader.Read4(&general_profile_compatibility_flags_) &&
85 reader.ReadToVector(&general_constraint_indicator_flags_, 6) &&
86 reader.Read1(&general_level_idc_) &&
87 reader.SkipBytes(8) && // Skip uninterested fields.
88 reader.Read1(&length_size_minus_one) &&
89 reader.Read1(&num_of_arrays));
90
91 general_profile_space_ = profile_indication >> 6;
92 RCHECK(general_profile_space_ <= 3u);
93 general_tier_flag_ = ((profile_indication >> 5) & 1) == 1;
94 general_profile_idc_ = profile_indication & 0x1f;
95 } else {
96 RCHECK(reader.Read1(&version_) && version_ == 1 &&
97 reader.SkipBytes(3) && // Skip uninterested fields.
98 reader.Read1(&length_size_minus_one) &&
99 reader.Read1(&num_of_arrays));
100 }
101 if ((length_size_minus_one & 0x3) == 2) {
102 LOG(ERROR) << "Invalid NALU length size.";
103 return false;
104 }
105 set_nalu_length_size((length_size_minus_one & 0x3) + 1);
106
107 if (parser_ == nullptr) {
108 if (internal_parser_used_ == true)
109 parser_ = &internal_parser_;
110 else {
111 LOG(ERROR) << "Internal parser is not used, but parser_ is not set!";
112 return false;
113 }
114 }
115
116 for (int i = 0; i < num_of_arrays; i++) {
117 uint8_t nal_unit_type;
118 uint16_t num_nalus;
119 RCHECK(reader.Read1(&nal_unit_type));
120 nal_unit_type &= 0x3f;
121 RCHECK(reader.Read2(&num_nalus));
122 for (int j = 0; j < num_nalus; j++) {
123 uint16_t nalu_length;
124 RCHECK(reader.Read2(&nalu_length));
125 uint64_t nalu_offset = reader.pos();
126 RCHECK(reader.SkipBytes(nalu_length));
127
128 Nalu nalu;
129 RCHECK(nalu.Initialize(Nalu::kH265, data() + nalu_offset, nalu_length));
130 RCHECK(nalu.type() == nal_unit_type);
131 AddNalu(nalu);
132
133 if (nalu.type() == Nalu::H265_VPS) {
134 int vps_id = 0;
135 RCHECK(parser_->ParseVps(nalu, &vps_id) == H265Parser::kOk);
136 } else if (nalu.type() == Nalu::H265_SPS) {
137 int sps_id = 0;
138 RCHECK(parser_->ParseSps(nalu, &sps_id) == H265Parser::kOk);
139 const H265Sps* sps = parser_->GetSps(sps_id);
140 if (!layered_) {
142 sps->vui_parameters.transfer_characteristics);
143 set_color_primaries(sps->vui_parameters.color_primaries);
144 set_matrix_coefficients(sps->vui_parameters.matrix_coefficients);
145 } else {
146 // Get profile/tier/level info from the SPS/VPS.
147 const int* general_profile_tier_level_data =
148 sps->general_profile_tier_level_data;
149 general_profile_space_ =
150 (general_profile_tier_level_data[0] & 0xFF) >> 6;
151 RCHECK(general_profile_space_ <= 3u);
152 general_tier_flag_ =
153 ((general_profile_tier_level_data[0] & 0x3F) >> 5) == 1;
154 general_profile_idc_ = general_profile_tier_level_data[0] & 0x1F;
155 general_profile_compatibility_flags_ =
156 ((general_profile_tier_level_data[1] & 0xFF) << 24) |
157 ((general_profile_tier_level_data[2] & 0xFF) << 16) |
158 ((general_profile_tier_level_data[3] & 0xFF) << 8) |
159 (general_profile_tier_level_data[4] & 0xFF);
160 general_constraint_indicator_flags_.resize(6);
161 for (int k = 0; k < 6; ++k) {
162 general_constraint_indicator_flags_[i] =
163 general_profile_tier_level_data[5 + i] & 0xFF;
164 }
165 general_level_idc_ = general_profile_tier_level_data[11] & 0xFF;
166 }
167 }
168 }
169 }
170
171 // TODO(kqyang): Parse SPS to get resolutions.
172 return true;
173}
174
176 FourCC codec_fourcc) const {
177 // ISO/IEC 14496-15:2014 Annex E.
178 std::vector<std::string> fields;
179 fields.push_back(FourCCToString(codec_fourcc));
180 fields.push_back(GeneralProfileSpaceAsString(general_profile_space_) +
181 absl::StrFormat("%d", general_profile_idc_));
182 fields.push_back(
183 ReverseBitsAndHexEncode(general_profile_compatibility_flags_));
184 fields.push_back((general_tier_flag_ ? "H" : "L") +
185 absl::StrFormat("%d", general_level_idc_));
186
187 // Remove trailing bytes that are zero.
188 std::vector<uint8_t> constraints = general_constraint_indicator_flags_;
189 size_t size = constraints.size();
190 for (; size > 0; --size) {
191 if (constraints[size - 1] != 0)
192 break;
193 }
194 constraints.resize(size);
195 for (uint8_t constraint : constraints)
196 fields.push_back(TrimLeadingZeros(
197 absl::BytesToHexString(byte_array_to_string_view(&constraint, 1))));
198
199 return absl::StrJoin(fields, ".");
200}
201
202bool HEVCDecoderConfigurationRecord::ParseLHEVCConfig(
203 const std::vector<uint8_t>& data) {
204 layered_ = true;
205 return Parse(data.data(), data.size());
206}
207
208} // namespace media
209} // namespace shaka
void set_matrix_coefficients(uint8_t matrix_coefficients)
Sets the matrix coeffs.
void AddNalu(const Nalu &nalu)
Adds the given Nalu to the configuration.
void set_color_primaries(uint8_t color_primaries)
Sets the colour primaries.
void set_transfer_characteristics(uint8_t transfer_characteristics)
Sets the transfer characteristics.
bool Parse(const std::vector< uint8_t > &data)
void set_nalu_length_size(uint8_t nalu_length_size)
Sets the size of the NAL unit length field.
Result ParseSps(const Nalu &nalu, int *sps_id)
Result ParseVps(const Nalu &nalu, int *vps_id)
const H265Sps * GetSps(int sps_id)
All the methods that are virtual are virtual for mocking.
std::string_view byte_array_to_string_view(const uint8_t *bytes, size_t bytes_size)
Convert byte array to string_view.