2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/codecs/es_descriptor.h>
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/bit_reader.h>
|
|
|
|
#include <packager/media/base/buffer_writer.h>
|
|
|
|
#include <packager/media/base/rcheck.h>
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace media {
|
2013-11-27 02:23:30 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// ISO/IEC 14496-1:2004 Section 7.2.6.6 Table 6: StreamType values.
|
|
|
|
enum StreamType {
|
|
|
|
kForbiddenStreamType = 0x00,
|
|
|
|
kObjectDescriptorStreamType = 0x01,
|
|
|
|
kClockReferenceStreamType = 0x02,
|
|
|
|
kSceneDescriptionStreamType = 0x03,
|
|
|
|
kVisualStreamType = 0x04,
|
|
|
|
kAudioStreamType = 0x05,
|
|
|
|
kMPEG7StreamType = 0x06,
|
|
|
|
kIPMPStreamType = 0x07,
|
|
|
|
kObjectContentInfoStreamType = 0x08,
|
|
|
|
kMPEGJStreamType = 0x09,
|
|
|
|
kInteractionStream = 0x0A,
|
|
|
|
kIPMPToolStreamType = 0x0B,
|
|
|
|
};
|
|
|
|
|
|
|
|
// ISO/IEC 14496-1:2004 Section 7.3.2.3 Table 12: ISO SL Config Descriptor.
|
|
|
|
enum SLPredefinedTags {
|
|
|
|
kSLPredefinedNull = 0x01,
|
|
|
|
kSLPredefinedMP4 = 0x02,
|
|
|
|
};
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
// The elementary stream size is specific by up to 4 bytes.
|
|
|
|
// The MSB of a byte indicates if there are more bytes for the size.
|
2019-01-04 23:49:57 +00:00
|
|
|
bool ReadDescriptorSize(BitReader* reader, size_t* size) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t msb;
|
|
|
|
uint8_t byte;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
*size = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 4; ++i) {
|
|
|
|
RCHECK(reader->ReadBits(1, &msb));
|
|
|
|
RCHECK(reader->ReadBits(7, &byte));
|
|
|
|
*size = (*size << 7) + byte;
|
|
|
|
|
|
|
|
if (msb == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
void WriteDescriptorSize(size_t size, BufferWriter* writer) {
|
|
|
|
std::vector<uint8_t> size_bytes;
|
|
|
|
while (size > 0) {
|
|
|
|
uint8_t byte = (size & 0x7F);
|
|
|
|
size >>= 7;
|
|
|
|
if (!size_bytes.empty())
|
|
|
|
byte |= 0x80;
|
|
|
|
size_bytes.push_back(byte);
|
|
|
|
}
|
|
|
|
for (auto iter = size_bytes.rbegin(); iter != size_bytes.rend(); iter++)
|
|
|
|
writer->AppendInt(*iter);
|
|
|
|
}
|
2013-11-27 02:23:30 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
size_t CountDescriptorSize(size_t size) {
|
|
|
|
size_t num_bytes = 0;
|
|
|
|
while (size > 0) {
|
|
|
|
num_bytes++;
|
|
|
|
size >>= 7;
|
|
|
|
}
|
|
|
|
return num_bytes;
|
|
|
|
}
|
2013-11-27 02:23:30 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
} // namespace
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
bool BaseDescriptor::Parse(const std::vector<uint8_t>& data) {
|
|
|
|
BitReader reader(data.data(), data.size());
|
|
|
|
return Read(&reader);
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
bool BaseDescriptor::Read(BitReader* reader) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t tag;
|
2019-01-04 23:49:57 +00:00
|
|
|
RCHECK(reader->ReadBits(8, &tag));
|
|
|
|
if (tag != static_cast<uint8_t>(tag_)) {
|
|
|
|
LOG(ERROR) << "Expecting tag " << static_cast<int>(tag_) << ", but seeing "
|
|
|
|
<< static_cast<int>(tag);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RCHECK(ReadDescriptorSize(reader, &data_size_));
|
|
|
|
return ReadData(reader);
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
void BaseDescriptor::Write(BufferWriter* writer) {
|
|
|
|
// Compute and update descriptor size.
|
|
|
|
size_t size = ComputeSize();
|
|
|
|
size_t buffer_size_before_write = writer->Size();
|
|
|
|
|
|
|
|
WriteInternal(writer);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
DCHECK_EQ(size, writer->Size() - buffer_size_before_write);
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
size_t BaseDescriptor::ComputeSize() {
|
|
|
|
data_size_ = ComputeDataSize();
|
|
|
|
return 1 + CountDescriptorSize(data_size_) + data_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseDescriptor::WriteHeader(BufferWriter* writer) {
|
|
|
|
writer->AppendInt(static_cast<uint8_t>(tag_));
|
|
|
|
WriteDescriptorSize(data_size_, writer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DecoderSpecificInfoDescriptor::ReadData(BitReader* reader) {
|
|
|
|
data_.resize(data_size());
|
|
|
|
for (uint8_t& data_entry : data_)
|
|
|
|
RCHECK(reader->ReadBits(8, &data_entry));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
void DecoderSpecificInfoDescriptor::WriteInternal(BufferWriter* writer) {
|
|
|
|
WriteHeader(writer);
|
|
|
|
writer->AppendVector(data_);
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
size_t DecoderSpecificInfoDescriptor::ComputeDataSize() {
|
|
|
|
return data_.size();
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
bool DecoderConfigDescriptor::ReadData(BitReader* reader) {
|
2018-05-17 00:53:52 +00:00
|
|
|
const size_t start_pos = reader->bit_position();
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(reader->ReadBits(8, &object_type_));
|
2019-01-04 23:49:57 +00:00
|
|
|
|
|
|
|
int stream_type;
|
|
|
|
RCHECK(reader->ReadBits(6, &stream_type));
|
|
|
|
if (stream_type != kAudioStreamType) {
|
|
|
|
LOG(ERROR) << "Seeing non audio stream type " << stream_type;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCHECK(reader->SkipBits(2)); // Skip |upStream| and |reserved|.
|
|
|
|
RCHECK(reader->ReadBits(24, &buffer_size_db_));
|
2016-01-04 18:57:05 +00:00
|
|
|
RCHECK(reader->ReadBits(32, &max_bitrate_));
|
|
|
|
RCHECK(reader->ReadBits(32, &avg_bitrate_));
|
2018-05-17 00:53:52 +00:00
|
|
|
const size_t fields_bits = reader->bit_position() - start_pos;
|
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
const size_t kBitsInByte = 8;
|
|
|
|
const bool has_child_tags = data_size() * kBitsInByte > fields_bits;
|
|
|
|
decoder_specific_info_descriptor_ = DecoderSpecificInfoDescriptor();
|
2018-05-17 00:53:52 +00:00
|
|
|
if (has_child_tags)
|
2019-01-04 23:49:57 +00:00
|
|
|
RCHECK(decoder_specific_info_descriptor_.Read(reader));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
void DecoderConfigDescriptor::WriteInternal(BufferWriter* writer) {
|
|
|
|
WriteHeader(writer);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
writer->AppendInt(static_cast<uint8_t>(object_type_));
|
|
|
|
// 6 bit stream type. The last bit is reserved with 1.
|
|
|
|
const uint8_t stream_type = (kAudioStreamType << 2) | 1;
|
|
|
|
writer->AppendInt(stream_type);
|
|
|
|
writer->AppendNBytes(buffer_size_db_, 3);
|
|
|
|
writer->AppendInt(max_bitrate_);
|
|
|
|
writer->AppendInt(avg_bitrate_);
|
2020-06-07 00:18:00 +00:00
|
|
|
|
|
|
|
if (!decoder_specific_info_descriptor_.data().empty())
|
|
|
|
decoder_specific_info_descriptor_.Write(writer);
|
2019-01-04 23:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t DecoderConfigDescriptor::ComputeDataSize() {
|
|
|
|
// object_type (1 byte), stream_type (1 byte), decoding_buffer_size (3 bytes),
|
|
|
|
// max_bitrate (4 bytes), avg_bitrate (4 bytes).
|
|
|
|
const size_t data_size_without_children = 1 + 1 + 3 + 4 + 4;
|
2020-06-07 00:18:00 +00:00
|
|
|
if (decoder_specific_info_descriptor_.data().empty())
|
|
|
|
return data_size_without_children;
|
2019-01-04 23:49:57 +00:00
|
|
|
return data_size_without_children +
|
|
|
|
decoder_specific_info_descriptor_.ComputeSize();
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2022-12-16 00:53:36 +00:00
|
|
|
bool SLConfigDescriptor::ReadData(BitReader*) {
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
void SLConfigDescriptor::WriteInternal(BufferWriter* writer) {
|
|
|
|
WriteHeader(writer);
|
|
|
|
writer->AppendInt(static_cast<uint8_t>(kSLPredefinedMP4));
|
|
|
|
}
|
2013-11-27 02:23:30 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
size_t SLConfigDescriptor::ComputeDataSize() {
|
|
|
|
return 1;
|
|
|
|
}
|
2013-11-27 02:23:30 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
bool ESDescriptor::ReadData(BitReader* reader) {
|
|
|
|
bool stream_dependency_flag;
|
|
|
|
bool url_flag;
|
|
|
|
bool ocr_stream_flag;
|
|
|
|
RCHECK(reader->ReadBits(16, &esid_));
|
|
|
|
RCHECK(reader->ReadBits(1, &stream_dependency_flag));
|
|
|
|
RCHECK(reader->ReadBits(1, &url_flag));
|
|
|
|
RCHECK(!url_flag); // We don't support url flag
|
|
|
|
RCHECK(reader->ReadBits(1, &ocr_stream_flag));
|
|
|
|
RCHECK(reader->SkipBits(5)); // streamPriority
|
2013-11-27 02:23:30 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
if (stream_dependency_flag)
|
|
|
|
RCHECK(reader->SkipBits(16)); // dependsOn_ES_ID
|
|
|
|
if (ocr_stream_flag)
|
|
|
|
RCHECK(reader->SkipBits(16)); // OCR_ES_Id
|
2013-11-27 02:23:30 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
return decoder_config_descriptor_.Read(reader);
|
|
|
|
// Skip the parsing of |sl_config_descriptor_| intentionally as we do not care
|
|
|
|
// about the data.
|
|
|
|
}
|
2013-11-27 02:23:30 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
void ESDescriptor::WriteInternal(BufferWriter* writer) {
|
|
|
|
WriteHeader(writer);
|
2013-11-27 02:23:30 +00:00
|
|
|
|
2020-07-15 21:14:15 +00:00
|
|
|
// According to ISO/IEC 14496-14:2018 Section 4.1.2,
|
|
|
|
// ES_ID is set to 0 when stored
|
|
|
|
const uint16_t kEsid = 0;
|
|
|
|
writer->AppendInt(kEsid);
|
2019-01-04 23:49:57 +00:00
|
|
|
const uint8_t kNoEsFlags = 0;
|
|
|
|
writer->AppendInt(kNoEsFlags);
|
|
|
|
|
|
|
|
decoder_config_descriptor_.Write(writer);
|
|
|
|
sl_config_descriptor_.Write(writer);
|
2013-11-27 02:23:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
size_t ESDescriptor::ComputeDataSize() {
|
|
|
|
// esid (2 bytes), es_flags (1 byte).
|
|
|
|
const size_t data_size_without_children = 2 + 1;
|
|
|
|
return data_size_without_children + decoder_config_descriptor_.ComputeSize() +
|
|
|
|
sl_config_descriptor_.ComputeSize();
|
2013-11-27 02:23:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|