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) {
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;
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;
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;
170 decoder_specific_info_descriptor_.Write(writer);
173 size_t DecoderConfigDescriptor::ComputeDataSize() {
176 const size_t data_size_without_children = 1 + 1 + 3 + 4 + 4;
177 return data_size_without_children +
178 decoder_specific_info_descriptor_.ComputeSize();
181 bool SLConfigDescriptor::ReadData(
BitReader* reader) {
185 void SLConfigDescriptor::WriteInternal(
BufferWriter* writer) {
187 writer->
AppendInt(static_cast<uint8_t>(kSLPredefinedMP4));
190 size_t SLConfigDescriptor::ComputeDataSize() {
194 bool ESDescriptor::ReadData(
BitReader* reader) {
195 bool stream_dependency_flag;
197 bool ocr_stream_flag;
198 RCHECK(reader->
ReadBits(16, &esid_));
199 RCHECK(reader->
ReadBits(1, &stream_dependency_flag));
200 RCHECK(reader->
ReadBits(1, &url_flag));
202 RCHECK(reader->
ReadBits(1, &ocr_stream_flag));
205 if (stream_dependency_flag)
210 return decoder_config_descriptor_.Read(reader);
215 void ESDescriptor::WriteInternal(
BufferWriter* writer) {
219 const uint8_t kNoEsFlags = 0;
222 decoder_config_descriptor_.Write(writer);
223 sl_config_descriptor_.Write(writer);
226 size_t ESDescriptor::ComputeDataSize() {
228 const size_t data_size_without_children = 2 + 1;
229 return data_size_without_children + decoder_config_descriptor_.ComputeSize() +
230 sl_config_descriptor_.ComputeSize();
All the methods that are virtual are virtual for mocking.