5 #include "packager/media/codecs/es_descriptor.h"
7 #include "packager/media/base/bit_reader.h"
8 #include "packager/media/base/buffer_writer.h"
9 #include "packager/media/base/rcheck.h"
17 kForbiddenStreamType = 0x00,
18 kObjectDescriptorStreamType = 0x01,
19 kClockReferenceStreamType = 0x02,
20 kSceneDescriptionStreamType = 0x03,
21 kVisualStreamType = 0x04,
22 kAudioStreamType = 0x05,
23 kMPEG7StreamType = 0x06,
24 kIPMPStreamType = 0x07,
25 kObjectContentInfoStreamType = 0x08,
26 kMPEGJStreamType = 0x09,
27 kInteractionStream = 0x0A,
28 kIPMPToolStreamType = 0x0B,
32 enum SLPredefinedTags {
33 kSLPredefinedNull = 0x01,
34 kSLPredefinedMP4 = 0x02,
39 bool ReadESSize(BitReader* reader, uint32_t* size) {
45 for (
size_t i = 0; i < 4; ++i) {
46 RCHECK(reader->ReadBits(1, &msb));
47 RCHECK(reader->ReadBits(7, &byte));
48 *size = (*size << 7) + byte;
59 const size_t kHeaderSize = 2;
60 const size_t kMaxDecoderSpecificInfoSize = 64;
61 const uint32_t kUnknownBitrate = 0;
65 ESDescriptor::ESDescriptor()
67 object_type_(kForbidden),
68 max_bitrate_(kUnknownBitrate),
69 avg_bitrate_(kUnknownBitrate) {}
71 ESDescriptor::~ESDescriptor() {}
73 bool ESDescriptor::Parse(
const std::vector<uint8_t>& data) {
74 BitReader reader(&data[0], data.size());
77 uint8_t stream_dependency_flag;
79 uint8_t ocr_stream_flag;
82 RCHECK(reader.ReadBits(8, &tag));
83 RCHECK(tag == kESDescrTag);
84 RCHECK(ReadESSize(&reader, &size));
86 RCHECK(reader.ReadBits(16, &esid_));
87 RCHECK(reader.ReadBits(1, &stream_dependency_flag));
88 RCHECK(reader.ReadBits(1, &url_flag));
90 RCHECK(reader.ReadBits(1, &ocr_stream_flag));
91 RCHECK(reader.ReadBits(5, &dummy));
93 if (stream_dependency_flag)
94 RCHECK(reader.ReadBits(16, &dummy));
96 RCHECK(reader.ReadBits(16, &dummy));
98 RCHECK(ParseDecoderConfigDescriptor(&reader));
103 bool ESDescriptor::ParseDecoderConfigDescriptor(BitReader* reader) {
108 RCHECK(reader->ReadBits(8, &tag));
109 RCHECK(tag == kDecoderConfigDescrTag);
110 RCHECK(ReadESSize(reader, &size));
112 RCHECK(reader->ReadBits(8, &object_type_));
113 RCHECK(reader->ReadBits(32, &dummy));
114 RCHECK(reader->ReadBits(32, &max_bitrate_));
115 RCHECK(reader->ReadBits(32, &avg_bitrate_));
116 RCHECK(ParseDecoderSpecificInfo(reader));
121 bool ESDescriptor::ParseDecoderSpecificInfo(BitReader* reader) {
126 RCHECK(reader->ReadBits(8, &tag));
127 RCHECK(tag == kDecoderSpecificInfoTag);
128 RCHECK(ReadESSize(reader, &size));
130 decoder_specific_info_.resize(size);
131 for (uint32_t i = 0; i < size; ++i)
132 RCHECK(reader->ReadBits(8, &decoder_specific_info_[i]));
136 void ESDescriptor::Write(BufferWriter* writer)
const {
138 CHECK_LT(decoder_specific_info_.size(), kMaxDecoderSpecificInfoSize);
140 const std::vector<uint8_t> kEmptyDecodingBufferSize(3, 0);
141 const uint8_t kNoEsFlags = 0;
143 const uint8_t decoder_specific_info_size =
144 static_cast<uint8_t
>(decoder_specific_info_.size());
147 const uint8_t stream_type = (kAudioStreamType << 2) | 1;
148 const uint8_t decoder_config_size =
149 static_cast<uint8_t
>(decoder_specific_info_size + kHeaderSize +
151 sizeof(stream_type) +
152 kEmptyDecodingBufferSize.size() +
153 sizeof(kUnknownBitrate) * 2);
155 const uint8_t sl_config_size =
sizeof(uint8_t);
156 const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
157 kHeaderSize +
sizeof(esid_) +
sizeof(kNoEsFlags);
159 writer->AppendInt(static_cast<uint8_t>(kESDescrTag));
160 writer->AppendInt(es_size);
161 writer->AppendInt(esid_);
162 writer->AppendInt(kNoEsFlags);
164 writer->AppendInt(static_cast<uint8_t>(kDecoderConfigDescrTag));
165 writer->AppendInt(decoder_config_size);
166 writer->AppendInt(static_cast<uint8_t>(object_type_));
167 writer->AppendInt(stream_type);
168 writer->AppendVector(kEmptyDecodingBufferSize);
169 writer->AppendInt(max_bitrate_);
170 writer->AppendInt(avg_bitrate_);
172 writer->AppendInt(static_cast<uint8_t>(kDecoderSpecificInfoTag));
173 writer->AppendInt(decoder_specific_info_size);
174 writer->AppendVector(decoder_specific_info_);
176 writer->AppendInt(static_cast<uint8_t>(kSLConfigTag));
177 writer->AppendInt(sl_config_size);
178 writer->AppendInt(static_cast<uint8_t>(kSLPredefinedMP4));
181 size_t ESDescriptor::ComputeSize()
const {
183 const uint8_t decoder_specific_info_size =
184 static_cast<uint8_t
>(decoder_specific_info_.size());
185 const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
186 sizeof(uint8_t) * 5 +
187 sizeof(uint32_t) * 2;
188 const uint8_t sl_config_size =
sizeof(uint8_t);
189 const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
190 kHeaderSize +
sizeof(esid_) +
sizeof(uint8_t);
191 return es_size + kHeaderSize;