DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
avc_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/avc_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/filters/h264_parser.h"
13 #include "packager/media/formats/mp4/rcheck.h"
14 
15 namespace edash_packager {
16 namespace media {
17 
18 AVCDecoderConfiguration::AVCDecoderConfiguration()
19  : version_(0),
20  profile_indication_(0),
21  profile_compatibility_(0),
22  avc_level_(0),
23  length_size_(0) {}
24 
25 AVCDecoderConfiguration::~AVCDecoderConfiguration() {}
26 
27 bool AVCDecoderConfiguration::Parse(const std::vector<uint8_t>& data) {
28  BufferReader reader(data.data(), data.size());
29  RCHECK(reader.Read1(&version_) && version_ == 1 &&
30  reader.Read1(&profile_indication_) &&
31  reader.Read1(&profile_compatibility_) && reader.Read1(&avc_level_));
32 
33  uint8_t length_size_minus_one;
34  RCHECK(reader.Read1(&length_size_minus_one));
35  length_size_ = (length_size_minus_one & 0x3) + 1;
36 
37  uint8_t num_sps;
38  RCHECK(reader.Read1(&num_sps));
39  num_sps &= 0x1f;
40  if (num_sps < 1) {
41  LOG(ERROR) << "No SPS found.";
42  return false;
43  }
44 
45  uint16_t sps_length = 0;
46  RCHECK(reader.Read2(&sps_length));
47 
48  H264Parser parser;
49  int sps_id = 0;
50  Nalu nalu;
51  RCHECK(nalu.InitializeFromH264(reader.data() + reader.pos(), sps_length));
52  RCHECK(parser.ParseSPS(nalu, &sps_id) == H264Parser::kOk);
53  return ExtractResolutionFromSps(*parser.GetSPS(sps_id), &coded_width_,
54  &coded_height_, &pixel_width_,
55  &pixel_height_);
56  // It is unlikely to have more than one SPS in practice. Also there's
57  // no way to change the {coded,pixel}_{width,height} dynamically from
58  // VideoStreamInfo. So skip the rest (if there are any).
59 }
60 
62  return GetCodecString(profile_indication_, profile_compatibility_,
63  avc_level_);
64 }
65 
67  uint8_t profile_indication,
68  uint8_t profile_compatibility,
69  uint8_t avc_level) {
70  const uint8_t bytes[] = {profile_indication, profile_compatibility,
71  avc_level};
72  return "avc1." +
73  base::StringToLowerASCII(base::HexEncode(bytes, arraysize(bytes)));
74 }
75 
76 } // namespace media
77 } // namespace edash_packager
bool Parse(const std::vector< uint8_t > &data)