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 = decoder_specific_info_.size();
146 const uint8_t stream_type = (kAudioStreamType << 2) | 1;
147 const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
149 sizeof(stream_type) +
150 kEmptyDecodingBufferSize.size() +
151 sizeof(kUnknownBitrate) * 2;
153 const uint8_t sl_config_size =
sizeof(uint8_t);
154 const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
155 kHeaderSize +
sizeof(esid_) +
sizeof(kNoEsFlags);
157 writer->AppendInt(static_cast<uint8_t>(kESDescrTag));
158 writer->AppendInt(es_size);
159 writer->AppendInt(esid_);
160 writer->AppendInt(kNoEsFlags);
162 writer->AppendInt(static_cast<uint8_t>(kDecoderConfigDescrTag));
163 writer->AppendInt(decoder_config_size);
164 writer->AppendInt(static_cast<uint8_t>(object_type_));
165 writer->AppendInt(stream_type);
166 writer->AppendVector(kEmptyDecodingBufferSize);
167 writer->AppendInt(max_bitrate_);
168 writer->AppendInt(avg_bitrate_);
170 writer->AppendInt(static_cast<uint8_t>(kDecoderSpecificInfoTag));
171 writer->AppendInt(decoder_specific_info_size);
172 writer->AppendVector(decoder_specific_info_);
174 writer->AppendInt(static_cast<uint8_t>(kSLConfigTag));
175 writer->AppendInt(sl_config_size);
176 writer->AppendInt(static_cast<uint8_t>(kSLPredefinedMP4));
179 size_t ESDescriptor::ComputeSize()
const {
181 const uint8_t decoder_specific_info_size = decoder_specific_info_.size();
182 const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
183 sizeof(uint8_t) * 5 +
184 sizeof(uint32_t) * 2;
185 const uint8_t sl_config_size =
sizeof(uint8_t);
186 const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
187 kHeaderSize +
sizeof(esid_) +
sizeof(uint8_t);
188 return es_size + kHeaderSize;