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/formats/mp4/rcheck.h"
11 namespace edash_packager {
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;
66 ESDescriptor::ESDescriptor() : esid_(0), object_type_(kForbidden) {}
68 ESDescriptor::~ESDescriptor() {}
70 bool ESDescriptor::Parse(
const std::vector<uint8_t>& data) {
71 BitReader reader(&data[0], data.size());
74 uint8_t stream_dependency_flag;
76 uint8_t ocr_stream_flag;
79 RCHECK(reader.ReadBits(8, &tag));
80 RCHECK(tag == kESDescrTag);
81 RCHECK(ReadESSize(&reader, &size));
83 RCHECK(reader.ReadBits(16, &esid_));
84 RCHECK(reader.ReadBits(1, &stream_dependency_flag));
85 RCHECK(reader.ReadBits(1, &url_flag));
87 RCHECK(reader.ReadBits(1, &ocr_stream_flag));
88 RCHECK(reader.ReadBits(5, &dummy));
90 if (stream_dependency_flag)
91 RCHECK(reader.ReadBits(16, &dummy));
93 RCHECK(reader.ReadBits(16, &dummy));
95 RCHECK(ParseDecoderConfigDescriptor(&reader));
100 bool ESDescriptor::ParseDecoderConfigDescriptor(BitReader* reader) {
105 RCHECK(reader->ReadBits(8, &tag));
106 RCHECK(tag == kDecoderConfigDescrTag);
107 RCHECK(ReadESSize(reader, &size));
109 RCHECK(reader->ReadBits(8, &object_type_));
110 RCHECK(reader->ReadBits(64, &dummy));
111 RCHECK(reader->ReadBits(32, &dummy));
112 RCHECK(ParseDecoderSpecificInfo(reader));
117 bool ESDescriptor::ParseDecoderSpecificInfo(BitReader* reader) {
122 RCHECK(reader->ReadBits(8, &tag));
123 RCHECK(tag == kDecoderSpecificInfoTag);
124 RCHECK(ReadESSize(reader, &size));
126 decoder_specific_info_.resize(size);
127 for (uint32_t i = 0; i < size; ++i)
128 RCHECK(reader->ReadBits(8, &decoder_specific_info_[i]));
133 void ESDescriptor::Write(BufferWriter* writer)
const {
135 CHECK_LT(decoder_specific_info_.size(), kMaxDecoderSpecificInfoSize);
137 const std::vector<uint8_t> kEmptyDecodingBufferSize(3, 0);
138 const uint32_t kUnknownBitrate = 0;
139 const uint8_t kNoEsFlags = 0;
141 const uint8_t decoder_specific_info_size = decoder_specific_info_.size();
144 const uint8_t stream_type = (kAudioStreamType << 2) | 1;
145 const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
147 sizeof(stream_type) +
148 kEmptyDecodingBufferSize.size() +
149 sizeof(kUnknownBitrate) * 2;
151 const uint8_t sl_config_size =
sizeof(uint8_t);
152 const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
153 kHeaderSize +
sizeof(esid_) +
sizeof(kNoEsFlags);
155 writer->AppendInt(static_cast<uint8_t>(kESDescrTag));
156 writer->AppendInt(es_size);
157 writer->AppendInt(esid_);
158 writer->AppendInt(kNoEsFlags);
160 writer->AppendInt(static_cast<uint8_t>(kDecoderConfigDescrTag));
161 writer->AppendInt(decoder_config_size);
162 writer->AppendInt(static_cast<uint8_t>(object_type_));
163 writer->AppendInt(stream_type);
164 writer->AppendVector(kEmptyDecodingBufferSize);
165 writer->AppendInt(kUnknownBitrate);
166 writer->AppendInt(kUnknownBitrate);
168 writer->AppendInt(static_cast<uint8_t>(kDecoderSpecificInfoTag));
169 writer->AppendInt(decoder_specific_info_size);
170 writer->AppendVector(decoder_specific_info_);
172 writer->AppendInt(static_cast<uint8_t>(kSLConfigTag));
173 writer->AppendInt(sl_config_size);
174 writer->AppendInt(static_cast<uint8_t>(kSLPredefinedMP4));
177 size_t ESDescriptor::ComputeSize()
const {
179 const uint8_t decoder_specific_info_size = decoder_specific_info_.size();
180 const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
181 sizeof(uint8_t) * 5 +
182 sizeof(uint32_t) * 2;
183 const uint8_t sl_config_size =
sizeof(uint8_t);
184 const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
185 kHeaderSize +
sizeof(esid_) +
sizeof(uint8_t);
186 return es_size + kHeaderSize;