7#include <packager/media/codecs/hevc_decoder_configuration_record.h>
15#include <absl/log/check.h>
16#include <absl/log/log.h>
17#include <absl/strings/escaping.h>
18#include <absl/strings/str_format.h>
19#include <absl/strings/str_join.h>
21#include <packager/media/base/buffer_reader.h>
22#include <packager/media/base/fourccs.h>
23#include <packager/media/base/rcheck.h>
24#include <packager/media/codecs/h265_parser.h>
25#include <packager/utils/bytes_to_string_view.h>
33std::string GeneralProfileSpaceAsString(uint8_t general_profile_space) {
34 switch (general_profile_space) {
44 LOG(WARNING) <<
"Unexpected general_profile_space "
45 << general_profile_space;
50std::string TrimLeadingZeros(
const std::string& str) {
51 DCHECK_GT(str.size(), 0u);
52 for (
size_t i = 0; i < str.size(); ++i) {
65std::string ReverseBitsAndHexEncode(uint32_t x) {
66 x = ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1);
67 x = ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2);
68 x = ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4);
69 const uint8_t bytes[] = {
static_cast<uint8_t
>(x & 0xFF),
70 static_cast<uint8_t
>((x >> 8) & 0xFF),
71 static_cast<uint8_t
>((x >> 16) & 0xFF),
72 static_cast<uint8_t
>((x >> 24) & 0xFF)};
73 return TrimLeadingZeros(absl::BytesToHexString(
79HEVCDecoderConfigurationRecord::HEVCDecoderConfigurationRecord() =
default;
81HEVCDecoderConfigurationRecord::~HEVCDecoderConfigurationRecord() =
default;
83bool HEVCDecoderConfigurationRecord::ParseInternal() {
86 uint8_t profile_indication = 0;
87 uint8_t length_size_minus_one = 0;
88 uint8_t num_of_arrays = 0;
90 RCHECK(reader.Read1(&version_) && version_ == 1 &&
91 reader.Read1(&profile_indication) &&
92 reader.Read4(&general_profile_compatibility_flags_) &&
93 reader.ReadToVector(&general_constraint_indicator_flags_, 6) &&
94 reader.Read1(&general_level_idc_) &&
95 reader.SkipBytes(8) &&
96 reader.Read1(&length_size_minus_one) &&
97 reader.Read1(&num_of_arrays));
99 general_profile_space_ = profile_indication >> 6;
100 RCHECK(general_profile_space_ <= 3u);
101 general_tier_flag_ = ((profile_indication >> 5) & 1) == 1;
102 general_profile_idc_ = profile_indication & 0x1f;
104 RCHECK(reader.Read1(&version_) && version_ == 1 &&
105 reader.SkipBytes(3) &&
106 reader.Read1(&length_size_minus_one) &&
107 reader.Read1(&num_of_arrays));
109 if ((length_size_minus_one & 0x3) == 2) {
110 LOG(ERROR) <<
"Invalid NALU length size.";
115 if (parser_ ==
nullptr) {
116 if (internal_parser_used_ ==
true)
117 parser_ = &internal_parser_;
119 LOG(ERROR) <<
"Internal parser is not used, but parser_ is not set!";
124 for (
int i = 0; i < num_of_arrays; i++) {
125 uint8_t nal_unit_type;
127 RCHECK(reader.Read1(&nal_unit_type));
128 nal_unit_type &= 0x3f;
129 RCHECK(reader.Read2(&num_nalus));
130 for (
int j = 0; j < num_nalus; j++) {
131 uint16_t nalu_length;
132 RCHECK(reader.Read2(&nalu_length));
133 uint64_t nalu_offset = reader.pos();
134 RCHECK(reader.SkipBytes(nalu_length));
137 RCHECK(
nalu.Initialize(Nalu::kH265,
data() + nalu_offset, nalu_length));
138 RCHECK(
nalu.
type() == nal_unit_type);
141 if (
nalu.
type() == Nalu::H265_VPS) {
143 RCHECK(parser_->
ParseVps(
nalu, &vps_id) == H265Parser::kOk);
144 }
else if (
nalu.
type() == Nalu::H265_SPS) {
146 RCHECK(parser_->
ParseSps(
nalu, &sps_id) == H265Parser::kOk);
147 const H265Sps* sps = parser_->
GetSps(sps_id);
150 sps->vui_parameters.transfer_characteristics);
155 const int* general_profile_tier_level_data =
156 sps->general_profile_tier_level_data;
157 general_profile_space_ =
158 (general_profile_tier_level_data[0] & 0xFF) >> 6;
159 RCHECK(general_profile_space_ <= 3u);
161 ((general_profile_tier_level_data[0] & 0x3F) >> 5) == 1;
162 general_profile_idc_ = general_profile_tier_level_data[0] & 0x1F;
163 general_profile_compatibility_flags_ =
164 ((general_profile_tier_level_data[1] & 0xFF) << 24) |
165 ((general_profile_tier_level_data[2] & 0xFF) << 16) |
166 ((general_profile_tier_level_data[3] & 0xFF) << 8) |
167 (general_profile_tier_level_data[4] & 0xFF);
168 general_constraint_indicator_flags_.resize(6);
169 for (
int k = 0; k < 6; ++k) {
170 general_constraint_indicator_flags_[i] =
171 general_profile_tier_level_data[5 + i] & 0xFF;
173 general_level_idc_ = general_profile_tier_level_data[11] & 0xFF;
184 FourCC codec_fourcc)
const {
186 std::vector<std::string> fields;
187 fields.push_back(FourCCToString(codec_fourcc));
188 fields.push_back(GeneralProfileSpaceAsString(general_profile_space_) +
189 absl::StrFormat(
"%d", general_profile_idc_));
191 ReverseBitsAndHexEncode(general_profile_compatibility_flags_));
192 fields.push_back((general_tier_flag_ ?
"H" :
"L") +
193 absl::StrFormat(
"%d", general_level_idc_));
196 std::vector<uint8_t> constraints = general_constraint_indicator_flags_;
197 size_t size = constraints.size();
198 for (; size > 0; --size) {
199 if (constraints[size - 1] != 0)
202 constraints.resize(size);
203 for (uint8_t constraint : constraints)
204 fields.push_back(TrimLeadingZeros(
207 return absl::StrJoin(fields,
".");
210bool HEVCDecoderConfigurationRecord::ParseLHEVCConfig(
211 const std::vector<uint8_t>& data) {
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.