DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
vp_codec_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/vp_codec_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/bit_reader.h"
12 #include "packager/media/base/buffer_writer.h"
13 #include "packager/base/strings/stringprintf.h"
14 #include "packager/media/formats/mp4/rcheck.h"
15 
16 namespace edash_packager {
17 namespace media {
18 namespace {
19 
20 std::string VPCodecAsString(VideoCodec codec) {
21  switch (codec) {
22  case kCodecVP8:
23  return "vp08";
24  case kCodecVP9:
25  return "vp09";
26  case kCodecVP10:
27  return "vp10";
28  default:
29  LOG(WARNING) << "Unknown VP codec: " << codec;
30  return std::string();
31  }
32 }
33 
34 } // namespace
35 
36 VPCodecConfiguration::VPCodecConfiguration()
37  : profile_(0),
38  level_(0),
39  bit_depth_(0),
40  color_space_(0),
41  chroma_subsampling_(0),
42  transfer_function_(0),
43  video_full_range_flag_(false) {}
44 
45 VPCodecConfiguration::VPCodecConfiguration(
46  uint8_t profile,
47  uint8_t level,
48  uint8_t bit_depth,
49  uint8_t color_space,
50  uint8_t chroma_subsampling,
51  uint8_t transfer_function,
52  bool video_full_range_flag,
53  const std::vector<uint8_t>& codec_initialization_data)
54  : profile_(profile),
55  level_(level),
56  bit_depth_(bit_depth),
57  color_space_(color_space),
58  chroma_subsampling_(chroma_subsampling),
59  transfer_function_(transfer_function),
60  video_full_range_flag_(video_full_range_flag),
61  codec_initialization_data_(codec_initialization_data) {}
62 
63 VPCodecConfiguration::~VPCodecConfiguration(){};
64 
65 bool VPCodecConfiguration::Parse(const std::vector<uint8_t>& data) {
66  BitReader reader(vector_as_array(&data), data.size());
67  RCHECK(reader.ReadBits(8, &profile_));
68  RCHECK(reader.ReadBits(8, &level_));
69  RCHECK(reader.ReadBits(4, &bit_depth_));
70  RCHECK(reader.ReadBits(4, &color_space_));
71  RCHECK(reader.ReadBits(4, &chroma_subsampling_));
72  RCHECK(reader.ReadBits(3, &transfer_function_));
73  RCHECK(reader.ReadBits(1, &video_full_range_flag_));
74  uint16_t codec_initialization_data_size = 0;
75  RCHECK(reader.ReadBits(16, &codec_initialization_data_size));
76  RCHECK(reader.bits_available() >= codec_initialization_data_size * 8);
77  const size_t header_size = data.size() - reader.bits_available() / 8;
78  codec_initialization_data_.assign(
79  data.begin() + header_size,
80  data.begin() + header_size + codec_initialization_data_size);
81  return true;
82 }
83 
84 void VPCodecConfiguration::Write(std::vector<uint8_t>* data) const {
85  BufferWriter writer;
86  writer.AppendInt(profile_);
87  writer.AppendInt(level_);
88  uint8_t bit_depth_color_space = (bit_depth_ << 4) | color_space_;
89  writer.AppendInt(bit_depth_color_space);
90  uint8_t chroma = (chroma_subsampling_ << 4) | (transfer_function_ << 1) |
91  (video_full_range_flag_ ? 1 : 0);
92  writer.AppendInt(chroma);
93  uint16_t codec_initialization_data_size = codec_initialization_data_.size();
94  writer.AppendInt(codec_initialization_data_size);
95  writer.AppendVector(codec_initialization_data_);
96  writer.SwapBuffer(data);
97 }
98 
99 std::string VPCodecConfiguration::GetCodecString(VideoCodec codec) const {
100  const std::string fields[] = {
101  base::IntToString(profile_),
102  base::IntToString(level_),
103  base::IntToString(bit_depth_),
104  base::IntToString(color_space_),
105  base::IntToString(chroma_subsampling_),
106  base::IntToString(transfer_function_),
107  (video_full_range_flag_ ? "01" : "00"),
108  };
109 
110  std::string codec_string = VPCodecAsString(codec);
111  for (const std::string& field : fields) {
112  // Make sure every field is at least 2-chars wide. The space will be
113  // replaced with '0' afterwards.
114  base::StringAppendF(&codec_string, ".%2s", field.c_str());
115  }
116  base::ReplaceChars(codec_string, " ", "0", &codec_string);
117  return codec_string;
118 }
119 
120 } // namespace media
121 } // namespace edash_packager
void Write(std::vector< uint8_t > *data) const
A class to read bit streams.
Definition: bit_reader.h:17
std::string GetCodecString(VideoCodec codec) const
bool Parse(const std::vector< uint8_t > &data)