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;
62 const size_t kBitsInByte = 8;
66 ESDescriptor::ESDescriptor()
68 object_type_(ObjectType::kForbidden),
69 max_bitrate_(kUnknownBitrate),
70 avg_bitrate_(kUnknownBitrate) {}
72 ESDescriptor::~ESDescriptor() {}
74 bool ESDescriptor::Parse(
const std::vector<uint8_t>&
data) {
75 BitReader reader(&data[0], data.size());
78 uint8_t stream_dependency_flag;
80 uint8_t ocr_stream_flag;
83 RCHECK(reader.ReadBits(8, &tag));
84 RCHECK(tag == kESDescrTag);
85 RCHECK(ReadESSize(&reader, &size));
87 RCHECK(reader.ReadBits(16, &esid_));
88 RCHECK(reader.ReadBits(1, &stream_dependency_flag));
89 RCHECK(reader.ReadBits(1, &url_flag));
91 RCHECK(reader.ReadBits(1, &ocr_stream_flag));
92 RCHECK(reader.ReadBits(5, &dummy));
94 if (stream_dependency_flag)
95 RCHECK(reader.ReadBits(16, &dummy));
97 RCHECK(reader.ReadBits(16, &dummy));
99 RCHECK(ParseDecoderConfigDescriptor(&reader));
104 bool ESDescriptor::ParseDecoderConfigDescriptor(BitReader* reader) {
109 RCHECK(reader->ReadBits(8, &tag));
110 RCHECK(tag == kDecoderConfigDescrTag);
111 RCHECK(ReadESSize(reader, &size));
113 const size_t start_pos = reader->bit_position();
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 const size_t fields_bits = reader->bit_position() - start_pos;
120 const bool has_child_tags = size * kBitsInByte > fields_bits;
122 RCHECK(ParseDecoderSpecificInfo(reader));
127 bool ESDescriptor::ParseDecoderSpecificInfo(BitReader* reader) {
132 RCHECK(reader->ReadBits(8, &tag));
133 RCHECK(tag == kDecoderSpecificInfoTag);
134 RCHECK(ReadESSize(reader, &size));
136 decoder_specific_info_.resize(size);
137 for (uint32_t i = 0; i < size; ++i)
138 RCHECK(reader->ReadBits(8, &decoder_specific_info_[i]));
142 void ESDescriptor::Write(BufferWriter* writer)
const {
144 CHECK_LT(decoder_specific_info_.size(), kMaxDecoderSpecificInfoSize);
146 const std::vector<uint8_t> kEmptyDecodingBufferSize(3, 0);
147 const uint8_t kNoEsFlags = 0;
149 const uint8_t decoder_specific_info_size =
150 static_cast<uint8_t
>(decoder_specific_info_.size());
153 const uint8_t stream_type = (kAudioStreamType << 2) | 1;
154 const uint8_t decoder_config_size =
155 static_cast<uint8_t
>(decoder_specific_info_size + kHeaderSize +
157 sizeof(stream_type) +
158 kEmptyDecodingBufferSize.size() +
159 sizeof(kUnknownBitrate) * 2);
161 const uint8_t sl_config_size =
sizeof(uint8_t);
162 const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
163 kHeaderSize +
sizeof(esid_) +
sizeof(kNoEsFlags);
165 writer->AppendInt(static_cast<uint8_t>(kESDescrTag));
166 writer->AppendInt(es_size);
167 writer->AppendInt(esid_);
168 writer->AppendInt(kNoEsFlags);
170 writer->AppendInt(static_cast<uint8_t>(kDecoderConfigDescrTag));
171 writer->AppendInt(decoder_config_size);
172 writer->AppendInt(static_cast<uint8_t>(object_type_));
173 writer->AppendInt(stream_type);
174 writer->AppendVector(kEmptyDecodingBufferSize);
175 writer->AppendInt(max_bitrate_);
176 writer->AppendInt(avg_bitrate_);
178 writer->AppendInt(static_cast<uint8_t>(kDecoderSpecificInfoTag));
179 writer->AppendInt(decoder_specific_info_size);
180 writer->AppendVector(decoder_specific_info_);
182 writer->AppendInt(static_cast<uint8_t>(kSLConfigTag));
183 writer->AppendInt(sl_config_size);
184 writer->AppendInt(static_cast<uint8_t>(kSLPredefinedMP4));
187 size_t ESDescriptor::ComputeSize()
const {
189 const uint8_t decoder_specific_info_size =
190 static_cast<uint8_t
>(decoder_specific_info_.size());
191 const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
192 sizeof(uint8_t) * 5 +
193 sizeof(uint32_t) * 2;
194 const uint8_t sl_config_size =
sizeof(uint8_t);
195 const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
196 kHeaderSize +
sizeof(esid_) +
sizeof(uint8_t);
197 return es_size + kHeaderSize;
All the methods that are virtual are virtual for mocking.