5 #include "packager/media/formats/mp4/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;
67 ESDescriptor::ESDescriptor()
69 object_type_(kForbidden),
70 max_bitrate_(kUnknownBitrate),
71 avg_bitrate_(kUnknownBitrate) {}
73 ESDescriptor::~ESDescriptor() {}
75 bool ESDescriptor::Parse(
const std::vector<uint8_t>& data) {
76 BitReader reader(&data[0], data.size());
79 uint8_t stream_dependency_flag;
81 uint8_t ocr_stream_flag;
84 RCHECK(reader.ReadBits(8, &tag));
85 RCHECK(tag == kESDescrTag);
86 RCHECK(ReadESSize(&reader, &size));
88 RCHECK(reader.ReadBits(16, &esid_));
89 RCHECK(reader.ReadBits(1, &stream_dependency_flag));
90 RCHECK(reader.ReadBits(1, &url_flag));
92 RCHECK(reader.ReadBits(1, &ocr_stream_flag));
93 RCHECK(reader.ReadBits(5, &dummy));
95 if (stream_dependency_flag)
96 RCHECK(reader.ReadBits(16, &dummy));
98 RCHECK(reader.ReadBits(16, &dummy));
100 RCHECK(ParseDecoderConfigDescriptor(&reader));
105 bool ESDescriptor::ParseDecoderConfigDescriptor(BitReader* reader) {
110 RCHECK(reader->ReadBits(8, &tag));
111 RCHECK(tag == kDecoderConfigDescrTag);
112 RCHECK(ReadESSize(reader, &size));
114 RCHECK(reader->ReadBits(8, &object_type_));
115 RCHECK(reader->ReadBits(32, &dummy));
116 RCHECK(reader->ReadBits(32, &max_bitrate_));
117 RCHECK(reader->ReadBits(32, &avg_bitrate_));
118 RCHECK(ParseDecoderSpecificInfo(reader));
123 bool ESDescriptor::ParseDecoderSpecificInfo(BitReader* reader) {
128 RCHECK(reader->ReadBits(8, &tag));
129 RCHECK(tag == kDecoderSpecificInfoTag);
130 RCHECK(ReadESSize(reader, &size));
132 decoder_specific_info_.resize(size);
133 for (uint32_t i = 0; i < size; ++i)
134 RCHECK(reader->ReadBits(8, &decoder_specific_info_[i]));
138 void ESDescriptor::Write(BufferWriter* writer)
const {
140 CHECK_LT(decoder_specific_info_.size(), kMaxDecoderSpecificInfoSize);
142 const std::vector<uint8_t> kEmptyDecodingBufferSize(3, 0);
143 const uint8_t kNoEsFlags = 0;
145 const uint8_t decoder_specific_info_size = decoder_specific_info_.size();
148 const uint8_t stream_type = (kAudioStreamType << 2) | 1;
149 const uint8_t decoder_config_size = 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 = decoder_specific_info_.size();
184 const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
185 sizeof(uint8_t) * 5 +
186 sizeof(uint32_t) * 2;
187 const uint8_t sl_config_size =
sizeof(uint8_t);
188 const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
189 kHeaderSize +
sizeof(esid_) +
sizeof(uint8_t);
190 return es_size + kHeaderSize;