5 #include <packager/media/codecs/es_descriptor.h>
7 #include <absl/log/check.h>
9 #include <packager/media/base/bit_reader.h>
10 #include <packager/media/base/buffer_writer.h>
11 #include <packager/media/base/rcheck.h>
19 kForbiddenStreamType = 0x00,
20 kObjectDescriptorStreamType = 0x01,
21 kClockReferenceStreamType = 0x02,
22 kSceneDescriptionStreamType = 0x03,
23 kVisualStreamType = 0x04,
24 kAudioStreamType = 0x05,
25 kMPEG7StreamType = 0x06,
26 kIPMPStreamType = 0x07,
27 kObjectContentInfoStreamType = 0x08,
28 kMPEGJStreamType = 0x09,
29 kInteractionStream = 0x0A,
30 kIPMPToolStreamType = 0x0B,
34 enum SLPredefinedTags {
35 kSLPredefinedNull = 0x01,
36 kSLPredefinedMP4 = 0x02,
41 bool ReadDescriptorSize(BitReader* reader,
size_t* size) {
47 for (
size_t i = 0; i < 4; ++i) {
48 RCHECK(reader->ReadBits(1, &msb));
49 RCHECK(reader->ReadBits(7, &
byte));
50 *size = (*size << 7) +
byte;
59 void WriteDescriptorSize(
size_t size, BufferWriter* writer) {
60 std::vector<uint8_t> size_bytes;
62 uint8_t
byte = (size & 0x7F);
64 if (!size_bytes.empty())
66 size_bytes.push_back(
byte);
68 for (
auto iter = size_bytes.rbegin(); iter != size_bytes.rend(); iter++)
69 writer->AppendInt(*iter);
72 size_t CountDescriptorSize(
size_t size) {
84 BitReader reader(data.data(), data.size());
91 if (tag !=
static_cast<uint8_t
>(tag_)) {
92 LOG(ERROR) <<
"Expecting tag " <<
static_cast<int>(tag_) <<
", but seeing "
93 <<
static_cast<int>(tag);
96 RCHECK(ReadDescriptorSize(reader, &data_size_));
97 return ReadData(reader);
103 size_t buffer_size_before_write = writer->Size();
105 WriteInternal(writer);
107 DCHECK_EQ(size, writer->Size() - buffer_size_before_write);
111 data_size_ = ComputeDataSize();
112 return 1 + CountDescriptorSize(data_size_) + data_size_;
116 writer->
AppendInt(
static_cast<uint8_t
>(tag_));
117 WriteDescriptorSize(data_size_, writer);
120 bool DecoderSpecificInfoDescriptor::ReadData(
BitReader* reader) {
122 for (uint8_t& data_entry : data_)
123 RCHECK(reader->
ReadBits(8, &data_entry));
127 void DecoderSpecificInfoDescriptor::WriteInternal(BufferWriter* writer) {
129 writer->AppendVector(data_);
132 size_t DecoderSpecificInfoDescriptor::ComputeDataSize() {
136 bool DecoderConfigDescriptor::ReadData(BitReader* reader) {
137 const size_t start_pos = reader->bit_position();
138 RCHECK(reader->ReadBits(8, &object_type_));
141 RCHECK(reader->ReadBits(6, &stream_type));
142 if (stream_type != kAudioStreamType) {
143 LOG(ERROR) <<
"Seeing non audio stream type " << stream_type;
147 RCHECK(reader->SkipBits(2));
148 RCHECK(reader->ReadBits(24, &buffer_size_db_));
149 RCHECK(reader->ReadBits(32, &max_bitrate_));
150 RCHECK(reader->ReadBits(32, &avg_bitrate_));
151 const size_t fields_bits = reader->bit_position() - start_pos;
153 const size_t kBitsInByte = 8;
154 const bool has_child_tags =
data_size() * kBitsInByte > fields_bits;
155 decoder_specific_info_descriptor_ = DecoderSpecificInfoDescriptor();
157 RCHECK(decoder_specific_info_descriptor_.
Read(reader));
162 void DecoderConfigDescriptor::WriteInternal(BufferWriter* writer) {
165 writer->AppendInt(
static_cast<uint8_t
>(object_type_));
167 const uint8_t stream_type = (kAudioStreamType << 2) | 1;
168 writer->AppendInt(stream_type);
169 writer->AppendNBytes(buffer_size_db_, 3);
170 writer->AppendInt(max_bitrate_);
171 writer->AppendInt(avg_bitrate_);
173 if (!decoder_specific_info_descriptor_.data().empty())
174 decoder_specific_info_descriptor_.
Write(writer);
177 size_t DecoderConfigDescriptor::ComputeDataSize() {
180 const size_t data_size_without_children = 1 + 1 + 3 + 4 + 4;
181 if (decoder_specific_info_descriptor_.data().empty())
182 return data_size_without_children;
183 return data_size_without_children +
187 bool SLConfigDescriptor::ReadData(BitReader*) {
191 void SLConfigDescriptor::WriteInternal(BufferWriter* writer) {
193 writer->AppendInt(
static_cast<uint8_t
>(kSLPredefinedMP4));
196 size_t SLConfigDescriptor::ComputeDataSize() {
200 bool ESDescriptor::ReadData(BitReader* reader) {
201 bool stream_dependency_flag;
203 bool ocr_stream_flag;
204 RCHECK(reader->ReadBits(16, &esid_));
205 RCHECK(reader->ReadBits(1, &stream_dependency_flag));
206 RCHECK(reader->ReadBits(1, &url_flag));
208 RCHECK(reader->ReadBits(1, &ocr_stream_flag));
209 RCHECK(reader->SkipBits(5));
211 if (stream_dependency_flag)
212 RCHECK(reader->SkipBits(16));
214 RCHECK(reader->SkipBits(16));
216 return decoder_config_descriptor_.
Read(reader);
221 void ESDescriptor::WriteInternal(BufferWriter* writer) {
226 const uint16_t kEsid = 0;
227 writer->AppendInt(kEsid);
228 const uint8_t kNoEsFlags = 0;
229 writer->AppendInt(kNoEsFlags);
231 decoder_config_descriptor_.
Write(writer);
232 sl_config_descriptor_.
Write(writer);
235 size_t ESDescriptor::ComputeDataSize() {
237 const size_t data_size_without_children = 2 + 1;
238 return data_size_without_children + decoder_config_descriptor_.
ComputeSize() +
All the methods that are virtual are virtual for mocking.