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/stl_util.h"
10 #include "packager/base/strings/string_number_conversions.h"
11 #include "packager/base/strings/string_util.h"
12 #include "packager/media/base/buffer_reader.h"
13 #include "packager/media/filters/h264_parser.h"
14 #include "packager/media/formats/mp4/rcheck.h"
15 
16 namespace edash_packager {
17 namespace media {
18 
19 AVCDecoderConfiguration::AVCDecoderConfiguration()
20  : version_(0),
21  profile_indication_(0),
22  profile_compatibility_(0),
23  avc_level_(0),
24  length_size_(0) {}
25 
26 AVCDecoderConfiguration::~AVCDecoderConfiguration() {}
27 
28 bool AVCDecoderConfiguration::Parse(const std::vector<uint8_t>& data) {
29  BufferReader reader(vector_as_array(&data), data.size());
30  RCHECK(reader.Read1(&version_) && version_ == 1 &&
31  reader.Read1(&profile_indication_) &&
32  reader.Read1(&profile_compatibility_) && reader.Read1(&avc_level_));
33 
34  uint8_t length_size_minus_one;
35  RCHECK(reader.Read1(&length_size_minus_one));
36  length_size_ = (length_size_minus_one & 0x3) + 1;
37 
38  uint8_t num_sps;
39  RCHECK(reader.Read1(&num_sps));
40  num_sps &= 0x1f;
41  if (num_sps < 1) {
42  LOG(ERROR) << "No SPS found.";
43  return false;
44  }
45 
46  uint16_t sps_length = 0;
47  RCHECK(reader.Read2(&sps_length));
48 
49  H264Parser parser;
50  int sps_id = 0;
51  RCHECK(parser.ParseSPSFromArray(reader.data() + reader.pos(), sps_length,
52  &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)