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 ReadDescriptorSize(BitReader* reader,
size_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;
57 void WriteDescriptorSize(
size_t size, BufferWriter* writer) {
58 std::vector<uint8_t> size_bytes;
60 uint8_t
byte = (size & 0x7F);
62 if (!size_bytes.empty())
64 size_bytes.push_back(
byte);
66 for (
auto iter = size_bytes.rbegin(); iter != size_bytes.rend(); iter++)
67 writer->AppendInt(*iter);
70 size_t CountDescriptorSize(
size_t size) {
82 BitReader reader(data.data(), data.size());
89 if (tag !=
static_cast<uint8_t
>(tag_)) {
90 LOG(ERROR) <<
"Expecting tag " <<
static_cast<int>(tag_) <<
", but seeing "
91 <<
static_cast<int>(tag);
94 RCHECK(ReadDescriptorSize(reader, &data_size_));
95 return ReadData(reader);
101 size_t buffer_size_before_write = writer->Size();
103 WriteInternal(writer);
105 DCHECK_EQ(size, writer->Size() - buffer_size_before_write);
109 data_size_ = ComputeDataSize();
110 return 1 + CountDescriptorSize(data_size_) + data_size_;
114 writer->
AppendInt(
static_cast<uint8_t
>(tag_));
115 WriteDescriptorSize(data_size_, writer);
118 bool DecoderSpecificInfoDescriptor::ReadData(
BitReader* reader) {
120 for (uint8_t& data_entry : data_)
121 RCHECK(reader->
ReadBits(8, &data_entry));
125 void DecoderSpecificInfoDescriptor::WriteInternal(BufferWriter* writer) {
127 writer->AppendVector(data_);
130 size_t DecoderSpecificInfoDescriptor::ComputeDataSize() {
134 bool DecoderConfigDescriptor::ReadData(BitReader* reader) {
135 const size_t start_pos = reader->bit_position();
136 RCHECK(reader->ReadBits(8, &object_type_));
139 RCHECK(reader->ReadBits(6, &stream_type));
140 if (stream_type != kAudioStreamType) {
141 LOG(ERROR) <<
"Seeing non audio stream type " << stream_type;
145 RCHECK(reader->SkipBits(2));
146 RCHECK(reader->ReadBits(24, &buffer_size_db_));
147 RCHECK(reader->ReadBits(32, &max_bitrate_));
148 RCHECK(reader->ReadBits(32, &avg_bitrate_));
149 const size_t fields_bits = reader->bit_position() - start_pos;
151 const size_t kBitsInByte = 8;
152 const bool has_child_tags =
data_size() * kBitsInByte > fields_bits;
153 decoder_specific_info_descriptor_ = DecoderSpecificInfoDescriptor();
155 RCHECK(decoder_specific_info_descriptor_.
Read(reader));
160 void DecoderConfigDescriptor::WriteInternal(BufferWriter* writer) {
163 writer->AppendInt(
static_cast<uint8_t
>(object_type_));
165 const uint8_t stream_type = (kAudioStreamType << 2) | 1;
166 writer->AppendInt(stream_type);
167 writer->AppendNBytes(buffer_size_db_, 3);
168 writer->AppendInt(max_bitrate_);
169 writer->AppendInt(avg_bitrate_);
171 if (!decoder_specific_info_descriptor_.data().empty())
172 decoder_specific_info_descriptor_.
Write(writer);
175 size_t DecoderConfigDescriptor::ComputeDataSize() {
178 const size_t data_size_without_children = 1 + 1 + 3 + 4 + 4;
179 if (decoder_specific_info_descriptor_.data().empty())
180 return data_size_without_children;
181 return data_size_without_children +
185 bool SLConfigDescriptor::ReadData(BitReader* reader) {
189 void SLConfigDescriptor::WriteInternal(BufferWriter* writer) {
191 writer->AppendInt(
static_cast<uint8_t
>(kSLPredefinedMP4));
194 size_t SLConfigDescriptor::ComputeDataSize() {
198 bool ESDescriptor::ReadData(BitReader* reader) {
199 bool stream_dependency_flag;
201 bool ocr_stream_flag;
202 RCHECK(reader->ReadBits(16, &esid_));
203 RCHECK(reader->ReadBits(1, &stream_dependency_flag));
204 RCHECK(reader->ReadBits(1, &url_flag));
206 RCHECK(reader->ReadBits(1, &ocr_stream_flag));
207 RCHECK(reader->SkipBits(5));
209 if (stream_dependency_flag)
210 RCHECK(reader->SkipBits(16));
212 RCHECK(reader->SkipBits(16));
214 return decoder_config_descriptor_.
Read(reader);
219 void ESDescriptor::WriteInternal(BufferWriter* writer) {
224 const uint16_t kEsid = 0;
225 writer->AppendInt(kEsid);
226 const uint8_t kNoEsFlags = 0;
227 writer->AppendInt(kNoEsFlags);
229 decoder_config_descriptor_.
Write(writer);
230 sl_config_descriptor_.
Write(writer);
233 size_t ESDescriptor::ComputeDataSize() {
235 const size_t data_size_without_children = 2 + 1;
236 return data_size_without_children + decoder_config_descriptor_.
ComputeSize() +
All the methods that are virtual are virtual for mocking.