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') continue;
46 return str.substr(i);
47 }
48 return "0";
49}
50
51// Encode the 32 bits input, but in reverse bit order, i.e. bit [31] as the most
52// significant bit, followed by, bit [30], and down to bit [0] as the least
53// significant bit, where bits [i] for i in the range of 0 to 31, inclusive, are
54// specified in ISO/IEC 23008‐2, encoded in hexadecimal (leading zeroes may be
55// omitted).
56std::string ReverseBitsAndHexEncode(uint32_t x) {
57 x = ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1);
58 x = ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2);
59 x = ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4);
60 const uint8_t bytes[] = {static_cast<uint8_t>(x & 0xFF),
61 static_cast<uint8_t>((x >> 8) & 0xFF),
62 static_cast<uint8_t>((x >> 16) & 0xFF),
63 static_cast<uint8_t>((x >> 24) & 0xFF)};
64 return TrimLeadingZeros(absl::BytesToHexString(
65 byte_array_to_string_view(bytes, std::size(bytes))));
66}
67
68} // namespace
69
70HEVCDecoderConfigurationRecord::HEVCDecoderConfigurationRecord() = default;
71
72HEVCDecoderConfigurationRecord::~HEVCDecoderConfigurationRecord() = default;
73
74bool HEVCDecoderConfigurationRecord::ParseInternal() {
75 BufferReader reader(data(), data_size());
76
77 uint8_t profile_indication = 0;
78 uint8_t length_size_minus_one = 0;
79 uint8_t num_of_arrays = 0;
80 if (!layered_) {
81 RCHECK(reader.Read1(&version_) && version_ == 1 &&
82 reader.Read1(&profile_indication) &&
83 reader.Read4(&general_profile_compatibility_flags_) &&
84 reader.ReadToVector(&general_constraint_indicator_flags_, 6) &&
85 reader.Read1(&general_level_idc_) &&
86 reader.SkipBytes(8) && // Skip uninterested fields.
87 reader.Read1(&length_size_minus_one) &&
88 reader.Read1(&num_of_arrays));
89
90 general_profile_space_ = profile_indication >> 6;
91 RCHECK(general_profile_space_ <= 3u);
92 general_tier_flag_ = ((profile_indication >> 5) & 1) == 1;
93 general_profile_idc_ = profile_indication & 0x1f;
94 } else {
95 RCHECK(reader.Read1(&version_) && version_ == 1 &&
96 reader.SkipBytes(3) && // Skip uninterested fields.
97 reader.Read1(&length_size_minus_one) &&
98 reader.Read1(&num_of_arrays));
99 }
100 if ((length_size_minus_one & 0x3) == 2) {
101 LOG(ERROR) << "Invalid NALU length size.";
102 return false;
103 }
104 set_nalu_length_size((length_size_minus_one & 0x3) + 1);
105
106 if (parser_ == nullptr) {
107 if (internal_parser_used_ == true)
108 parser_ = &internal_parser_;
109 else {
110 LOG(ERROR) << "Internal parser is not used, but parser_ is not set!";
111 return false;
112 }
113 }
114
115 for (int i = 0; i < num_of_arrays; i++) {
116 uint8_t nal_unit_type;
117 uint16_t num_nalus;
118 RCHECK(reader.Read1(&nal_unit_type));
119 nal_unit_type &= 0x3f;
120 RCHECK(reader.Read2(&num_nalus));
121 for (int j = 0; j < num_nalus; j++) {
122 uint16_t nalu_length;
123 RCHECK(reader.Read2(&nalu_length));
124 uint64_t nalu_offset = reader.pos();
125 RCHECK(reader.SkipBytes(nalu_length));
126
127 Nalu nalu;
128 RCHECK(nalu.Initialize(Nalu::kH265, data() + nalu_offset, nalu_length));
129 RCHECK(nalu.type() == nal_unit_type);
130 AddNalu(nalu);
131
132 if (nalu.type() == Nalu::H265_VPS) {
133 int vps_id = 0;
134 RCHECK(parser_->ParseVps(nalu, &vps_id) == H265Parser::kOk);
135 } else if (nalu.type() == Nalu::H265_SPS) {
136 int sps_id = 0;
137 RCHECK(parser_->ParseSps(nalu, &sps_id) == H265Parser::kOk);
138 const H265Sps* sps = parser_->GetSps(sps_id);
139 if (!layered_) {
141 sps->vui_parameters.transfer_characteristics);
142 set_color_primaries(sps->vui_parameters.color_primaries);
143 set_matrix_coefficients(sps->vui_parameters.matrix_coefficients);
144 } else {
145 // Get profile/tier/level info from the SPS/VPS.
146 const int* general_profile_tier_level_data =
147 sps->general_profile_tier_level_data;
148 general_profile_space_ =
149 (general_profile_tier_level_data[0] & 0xFF) >> 6;
150 RCHECK(general_profile_space_ <= 3u);
151 general_tier_flag_ =
152 ((general_profile_tier_level_data[0] & 0x3F) >> 5) == 1;
153 general_profile_idc_ = general_profile_tier_level_data[0] & 0x1F;
154 general_profile_compatibility_flags_ =
155 ((general_profile_tier_level_data[1] & 0xFF) << 24) |
156 ((general_profile_tier_level_data[2] & 0xFF) << 16) |
157 ((general_profile_tier_level_data[3] & 0xFF) << 8) |
158 (general_profile_tier_level_data[4] & 0xFF);
159 general_constraint_indicator_flags_.resize(6);
160 for (int k = 0; k < 6; ++k) {
161 general_constraint_indicator_flags_[i] =
162 general_profile_tier_level_data[5 + i] & 0xFF;
163 }
164 general_level_idc_ = general_profile_tier_level_data[11] & 0xFF;
165 }
166 }
167 }
168 }
169
170 // TODO(kqyang): Parse SPS to get resolutions.
171 return true;
172}
173
175 FourCC codec_fourcc) const {
176 // ISO/IEC 14496-15:2014 Annex E.
177 std::vector<std::string> fields;
178 fields.push_back(FourCCToString(codec_fourcc));
179 fields.push_back(GeneralProfileSpaceAsString(general_profile_space_) +
180 absl::StrFormat("%d", general_profile_idc_));
181 fields.push_back(
182 ReverseBitsAndHexEncode(general_profile_compatibility_flags_));
183 fields.push_back((general_tier_flag_ ? "H" : "L") +
184 absl::StrFormat("%d", general_level_idc_));
185
186 // Remove trailing bytes that are zero.
187 std::vector<uint8_t> constraints = general_constraint_indicator_flags_;
188 size_t size = constraints.size();
189 for (; size > 0; --size) {
190 if (constraints[size - 1] != 0) break;
191 }
192 constraints.resize(size);
193 for (uint8_t constraint : constraints)
194 fields.push_back(TrimLeadingZeros(
195 absl::BytesToHexString(byte_array_to_string_view(&constraint, 1))));
196
197 return absl::StrJoin(fields, ".");
198}
199
200bool HEVCDecoderConfigurationRecord::ParseLHEVCConfig(
201 const std::vector<uint8_t>& data) {
202 layered_ = true;
203 return Parse(data.data(), data.size());
204}
205
206} // namespace media
207} // 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.