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.
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/es_descriptor.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/bit_reader.h"
|
|
|
|
#include "packager/media/base/buffer_writer.h"
|
2016-04-06 23:21:45 +00:00
|
|
|
#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.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadESSize(BitReader* reader, uint32_t* size) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-27 02:23:30 +00:00
|
|
|
// Descryptor Header Size: 1 byte tag and 1 byte size (we don't support
|
|
|
|
// multi-bytes size for now).
|
|
|
|
const size_t kHeaderSize = 2;
|
|
|
|
const size_t kMaxDecoderSpecificInfoSize = 64;
|
2016-01-04 18:57:05 +00:00
|
|
|
const uint32_t kUnknownBitrate = 0;
|
2013-11-27 02:23:30 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
namespace mp4 {
|
|
|
|
|
2016-01-04 18:57:05 +00:00
|
|
|
ESDescriptor::ESDescriptor()
|
|
|
|
: esid_(0),
|
|
|
|
object_type_(kForbidden),
|
|
|
|
max_bitrate_(kUnknownBitrate),
|
|
|
|
avg_bitrate_(kUnknownBitrate) {}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
ESDescriptor::~ESDescriptor() {}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ESDescriptor::Parse(const std::vector<uint8_t>& data) {
|
2013-09-24 01:35:40 +00:00
|
|
|
BitReader reader(&data[0], data.size());
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t tag;
|
|
|
|
uint32_t size;
|
|
|
|
uint8_t stream_dependency_flag;
|
|
|
|
uint8_t url_flag;
|
|
|
|
uint8_t ocr_stream_flag;
|
|
|
|
uint16_t dummy;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
RCHECK(reader.ReadBits(8, &tag));
|
|
|
|
RCHECK(tag == kESDescrTag);
|
|
|
|
RCHECK(ReadESSize(&reader, &size));
|
|
|
|
|
2013-11-27 02:23:30 +00:00
|
|
|
RCHECK(reader.ReadBits(16, &esid_)); // ES_ID
|
2013-09-24 01:35:40 +00:00
|
|
|
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.ReadBits(5, &dummy)); // streamPriority
|
|
|
|
|
|
|
|
if (stream_dependency_flag)
|
|
|
|
RCHECK(reader.ReadBits(16, &dummy)); // dependsOn_ES_ID
|
|
|
|
if (ocr_stream_flag)
|
|
|
|
RCHECK(reader.ReadBits(16, &dummy)); // OCR_ES_Id
|
|
|
|
|
|
|
|
RCHECK(ParseDecoderConfigDescriptor(&reader));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ESDescriptor::ParseDecoderConfigDescriptor(BitReader* reader) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t tag;
|
|
|
|
uint32_t size;
|
2016-01-04 18:57:05 +00:00
|
|
|
uint32_t dummy;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
RCHECK(reader->ReadBits(8, &tag));
|
|
|
|
RCHECK(tag == kDecoderConfigDescrTag);
|
|
|
|
RCHECK(ReadESSize(reader, &size));
|
|
|
|
|
|
|
|
RCHECK(reader->ReadBits(8, &object_type_));
|
|
|
|
RCHECK(reader->ReadBits(32, &dummy));
|
2016-01-04 18:57:05 +00:00
|
|
|
RCHECK(reader->ReadBits(32, &max_bitrate_));
|
|
|
|
RCHECK(reader->ReadBits(32, &avg_bitrate_));
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(ParseDecoderSpecificInfo(reader));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ESDescriptor::ParseDecoderSpecificInfo(BitReader* reader) {
|
2013-11-27 02:23:30 +00:00
|
|
|
DCHECK(reader);
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t tag;
|
|
|
|
uint32_t size;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
RCHECK(reader->ReadBits(8, &tag));
|
|
|
|
RCHECK(tag == kDecoderSpecificInfoTag);
|
|
|
|
RCHECK(ReadESSize(reader, &size));
|
|
|
|
|
|
|
|
decoder_specific_info_.resize(size);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < size; ++i)
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(reader->ReadBits(8, &decoder_specific_info_[i]));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-27 02:23:30 +00:00
|
|
|
void ESDescriptor::Write(BufferWriter* writer) const {
|
|
|
|
DCHECK(writer);
|
|
|
|
CHECK_LT(decoder_specific_info_.size(), kMaxDecoderSpecificInfoSize);
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const std::vector<uint8_t> kEmptyDecodingBufferSize(3, 0);
|
|
|
|
const uint8_t kNoEsFlags = 0;
|
2013-11-27 02:23:30 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t decoder_specific_info_size = decoder_specific_info_.size();
|
2013-11-27 02:23:30 +00:00
|
|
|
|
|
|
|
// 6 bit stream type. The last bit is reserved with 1.
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t stream_type = (kAudioStreamType << 2) | 1;
|
|
|
|
const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
|
|
|
|
sizeof(uint8_t) + // object_type_.
|
|
|
|
sizeof(stream_type) +
|
|
|
|
kEmptyDecodingBufferSize.size() +
|
|
|
|
sizeof(kUnknownBitrate) * 2;
|
|
|
|
|
|
|
|
const uint8_t sl_config_size = sizeof(uint8_t); // predefined.
|
|
|
|
const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
|
|
|
|
kHeaderSize + sizeof(esid_) + sizeof(kNoEsFlags);
|
|
|
|
|
|
|
|
writer->AppendInt(static_cast<uint8_t>(kESDescrTag));
|
2013-11-27 02:23:30 +00:00
|
|
|
writer->AppendInt(es_size);
|
|
|
|
writer->AppendInt(esid_);
|
|
|
|
writer->AppendInt(kNoEsFlags);
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
writer->AppendInt(static_cast<uint8_t>(kDecoderConfigDescrTag));
|
2013-11-27 02:23:30 +00:00
|
|
|
writer->AppendInt(decoder_config_size);
|
2014-09-30 21:52:21 +00:00
|
|
|
writer->AppendInt(static_cast<uint8_t>(object_type_));
|
2013-11-27 02:23:30 +00:00
|
|
|
writer->AppendInt(stream_type);
|
|
|
|
writer->AppendVector(kEmptyDecodingBufferSize);
|
2016-01-04 18:57:05 +00:00
|
|
|
writer->AppendInt(max_bitrate_);
|
|
|
|
writer->AppendInt(avg_bitrate_);
|
2013-11-27 02:23:30 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
writer->AppendInt(static_cast<uint8_t>(kDecoderSpecificInfoTag));
|
2013-11-27 02:23:30 +00:00
|
|
|
writer->AppendInt(decoder_specific_info_size);
|
|
|
|
writer->AppendVector(decoder_specific_info_);
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
writer->AppendInt(static_cast<uint8_t>(kSLConfigTag));
|
2013-11-27 02:23:30 +00:00
|
|
|
writer->AppendInt(sl_config_size);
|
2014-09-30 21:52:21 +00:00
|
|
|
writer->AppendInt(static_cast<uint8_t>(kSLPredefinedMP4));
|
2013-11-27 02:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t ESDescriptor::ComputeSize() const {
|
|
|
|
// A bit magical. Refer to ESDescriptor::Write for details.
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t decoder_specific_info_size = decoder_specific_info_.size();
|
|
|
|
const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
|
|
|
|
sizeof(uint8_t) * 5 +
|
|
|
|
sizeof(uint32_t) * 2;
|
|
|
|
const uint8_t sl_config_size = sizeof(uint8_t);
|
|
|
|
const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
|
|
|
|
kHeaderSize + sizeof(esid_) + sizeof(uint8_t);
|
2013-11-27 02:23:30 +00:00
|
|
|
return es_size + kHeaderSize;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
} // namespace mp4
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|