DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
hevc_decoder_configuration.cc
1 // Copyright 2015 Google Inc. 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/filters/hevc_decoder_configuration.h"
8 
9 #include "packager/base/strings/string_number_conversions.h"
10 #include "packager/base/strings/string_util.h"
11 #include "packager/media/base/buffer_reader.h"
12 #include "packager/media/formats/mp4/rcheck.h"
13 
14 namespace edash_packager {
15 namespace media {
16 
17 namespace {
18 
19 // ISO/IEC 14496-15:2014 Annex E.
20 std::string GeneralProfileSpaceAsString(uint8_t general_profile_space) {
21  switch (general_profile_space) {
22  case 0:
23  return "";
24  case 1:
25  return "A";
26  case 2:
27  return "B";
28  case 3:
29  return "C";
30  default:
31  LOG(WARNING) << "Unexpected general_profile_space "
32  << general_profile_space;
33  return "";
34  }
35 }
36 
37 std::string TrimLeadingZeros(const std::string& str) {
38  DCHECK_GT(str.size(), 0u);
39  for (size_t i = 0; i < str.size(); ++i) {
40  if (str[i] == '0') continue;
41  return str.substr(i);
42  }
43  return "0";
44 }
45 
46 // Encode the 32 bits input, but in reverse bit order, i.e. bit [31] as the most
47 // significant bit, followed by, bit [30], and down to bit [0] as the least
48 // significant bit, where bits [i] for i in the range of 0 to 31, inclusive, are
49 // specified in ISO/IEC 23008‐2, encoded in hexadecimal (leading zeroes may be
50 // omitted).
51 std::string ReverseBitsAndHexEncode(uint32_t x) {
52  x = ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1);
53  x = ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2);
54  x = ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4);
55  const uint8_t bytes[] = {static_cast<uint8_t>(x & 0xFF),
56  static_cast<uint8_t>((x >> 8) & 0xFF),
57  static_cast<uint8_t>((x >> 16) & 0xFF),
58  static_cast<uint8_t>((x >> 24) & 0xFF)};
59  return TrimLeadingZeros(base::HexEncode(bytes, arraysize(bytes)));
60 }
61 
62 std::string CodecAsString(VideoCodec codec) {
63  switch (codec) {
64  case kCodecHEV1:
65  return "hev1";
66  case kCodecHVC1:
67  return "hvc1";
68  default:
69  LOG(WARNING) << "Unknown codec: " << codec;
70  return std::string();
71  }
72 }
73 
74 } // namespace
75 
76 HEVCDecoderConfiguration::HEVCDecoderConfiguration()
77  : version_(0),
78  general_profile_space_(0),
79  general_tier_flag_(false),
80  general_profile_idc_(0),
81  general_profile_compatibility_flags_(0),
82  general_level_idc_(0),
83  length_size_(0) {}
84 
85 HEVCDecoderConfiguration::~HEVCDecoderConfiguration() {}
86 
87 bool HEVCDecoderConfiguration::Parse(const std::vector<uint8_t>& data) {
88  BufferReader reader(data.data(), data.size());
89 
90  uint8_t profile_indication = 0;
91  uint8_t length_size_minus_one = 0;
92  uint8_t num_of_arrays = 0;
93  RCHECK(reader.Read1(&version_) && version_ == 1 &&
94  reader.Read1(&profile_indication) &&
95  reader.Read4(&general_profile_compatibility_flags_) &&
96  reader.ReadToVector(&general_constraint_indicator_flags_, 6) &&
97  reader.Read1(&general_level_idc_) &&
98  reader.SkipBytes(8) && // Skip uninterested fields.
99  reader.Read1(&length_size_minus_one) &&
100  reader.Read1(&num_of_arrays));
101 
102  general_profile_space_ = profile_indication >> 6;
103  RCHECK(general_profile_space_ <= 3u);
104  general_tier_flag_ = ((profile_indication >> 5) & 1) == 1;
105  general_profile_idc_ = profile_indication & 0x1f;
106 
107  length_size_ = (length_size_minus_one & 0x3) + 1;
108 
109  // TODO(kqyang): Parse SPS to get resolutions.
110  return true;
111 }
112 
113 std::string HEVCDecoderConfiguration::GetCodecString(VideoCodec codec) const {
114  // ISO/IEC 14496-15:2014 Annex E.
115  std::vector<std::string> fields;
116  fields.push_back(CodecAsString(codec));
117  fields.push_back(GeneralProfileSpaceAsString(general_profile_space_) +
118  base::IntToString(general_profile_idc_));
119  fields.push_back(
120  ReverseBitsAndHexEncode(general_profile_compatibility_flags_));
121  fields.push_back((general_tier_flag_ ? "H" : "L") +
122  base::IntToString(general_level_idc_));
123 
124  // Remove trailing bytes that are zero.
125  std::vector<uint8_t> constraints = general_constraint_indicator_flags_;
126  size_t size = constraints.size();
127  for (; size > 0; --size) {
128  if (constraints[size - 1] != 0) break;
129  }
130  constraints.resize(size);
131  for (uint8_t constraint : constraints)
132  fields.push_back(TrimLeadingZeros(base::HexEncode(&constraint, 1)));
133 
134  return base::JoinString(fields, ".");
135 }
136 
137 } // namespace media
138 } // namespace edash_packager
bool Parse(const std::vector< uint8_t > &data)