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/box_definitions.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/media/base/bit_reader.h"
|
|
|
|
#include "packager/media/formats/mp4/box_buffer.h"
|
|
|
|
#include "packager/media/formats/mp4/rcheck.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
namespace {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t kFourCCSize = 4;
|
2013-11-27 01:52:13 +00:00
|
|
|
// Additional 32-bit size. We don't support 64-bit size.
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t kBoxSize = kFourCCSize + sizeof(uint32_t);
|
2013-11-27 01:52:13 +00:00
|
|
|
// Additional 1-byte version and 3-byte flags.
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t kFullBoxSize = kBoxSize + 4;
|
2013-11-27 01:52:13 +00:00
|
|
|
|
2014-10-15 21:56:12 +00:00
|
|
|
// Key Id size as defined in CENC spec.
|
|
|
|
const uint32_t kCencKeyIdSize = 16;
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
// 9 uint32_t in big endian formatted array.
|
|
|
|
const uint8_t kUnityMatrix[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0};
|
2013-11-27 01:52:13 +00:00
|
|
|
|
|
|
|
// Default entries for HandlerReference box.
|
|
|
|
const char kVideoHandlerName[] = "VideoHandler";
|
|
|
|
const char kAudioHandlerName[] = "SoundHandler";
|
|
|
|
|
|
|
|
// Default values for VideoSampleEntry box.
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t kVideoResolution = 0x00480000; // 72 dpi.
|
|
|
|
const uint16_t kVideoFrameCount = 1;
|
|
|
|
const uint16_t kVideoDepth = 0x0018;
|
2013-11-27 01:52:13 +00:00
|
|
|
|
2014-10-07 21:41:40 +00:00
|
|
|
// Utility functions to check if the 64bit integers can fit in 32bit integer.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool IsFitIn32Bits(uint64_t a) {
|
2014-09-30 23:52:58 +00:00
|
|
|
return a <= std::numeric_limits<uint32_t>::max();
|
2014-09-30 21:52:21 +00:00
|
|
|
}
|
2014-10-07 21:41:40 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool IsFitIn32Bits(int64_t a) {
|
2014-09-30 23:52:58 +00:00
|
|
|
return a <= std::numeric_limits<int32_t>::max() &&
|
|
|
|
a >= std::numeric_limits<int32_t>::min();
|
2014-09-30 21:52:21 +00:00
|
|
|
}
|
2014-10-07 21:41:40 +00:00
|
|
|
|
|
|
|
template <typename T1, typename T2>
|
|
|
|
bool IsFitIn32Bits(T1 a1, T2 a2) {
|
|
|
|
return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
|
2013-11-27 01:52:13 +00:00
|
|
|
}
|
2014-10-07 21:41:40 +00:00
|
|
|
|
|
|
|
template <typename T1, typename T2, typename T3>
|
|
|
|
bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
|
|
|
|
return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
|
2013-11-27 01:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-09-24 01:35:40 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
|
2013-09-24 01:35:40 +00:00
|
|
|
FileType::~FileType() {}
|
|
|
|
FourCC FileType::BoxType() const { return FOURCC_FTYP; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool FileType::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteFourCC(&major_brand) &&
|
|
|
|
buffer->ReadWriteUInt32(&minor_version));
|
|
|
|
size_t num_brands;
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
num_brands = (buffer->Size() - buffer->Pos()) / sizeof(FourCC);
|
|
|
|
compatible_brands.resize(num_brands);
|
|
|
|
} else {
|
|
|
|
num_brands = compatible_brands.size();
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < num_brands; ++i)
|
|
|
|
RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t FileType::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + kFourCCSize + sizeof(minor_version) +
|
|
|
|
kFourCCSize * compatible_brands.size();
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
SegmentType::SegmentType() {}
|
|
|
|
SegmentType::~SegmentType() {}
|
|
|
|
FourCC SegmentType::BoxType() const { return FOURCC_STYP; }
|
|
|
|
|
|
|
|
bool SegmentType::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
return FileType::ReadWrite(buffer);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SegmentType::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
return FileType::ComputeSize();
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
|
|
|
|
ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
|
|
|
|
FourCC ProtectionSystemSpecificHeader::BoxType() const { return FOURCC_PSSH; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool ProtectionSystemSpecificHeader::ReadWrite(BoxBuffer* buffer) {
|
2014-04-18 22:00:30 +00:00
|
|
|
if (!buffer->Reading() && !raw_box.empty()) {
|
|
|
|
// Write the raw box directly.
|
|
|
|
buffer->writer()->AppendVector(raw_box);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t size = data.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteVector(&system_id, 16) &&
|
|
|
|
buffer->ReadWriteUInt32(&size) &&
|
|
|
|
buffer->ReadWriteVector(&data, size));
|
|
|
|
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
// Copy the entire box, including the header, for passing to EME as
|
|
|
|
// initData.
|
|
|
|
DCHECK(raw_box.empty());
|
|
|
|
BoxReader* reader = buffer->reader();
|
|
|
|
DCHECK(reader);
|
|
|
|
raw_box.assign(reader->data(), reader->data() + reader->size());
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t ProtectionSystemSpecificHeader::ComputeSize() {
|
|
|
|
if (!raw_box.empty()) {
|
2014-04-18 22:00:30 +00:00
|
|
|
atom_size = raw_box.size();
|
2014-09-30 21:52:21 +00:00
|
|
|
} else {
|
|
|
|
atom_size =
|
|
|
|
kFullBoxSize + system_id.size() + sizeof(uint32_t) + data.size();
|
|
|
|
}
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
|
|
|
|
SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
|
|
|
|
FourCC SampleAuxiliaryInformationOffset::BoxType() const { return FOURCC_SAIO; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool SampleAuxiliaryInformationOffset::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer));
|
|
|
|
if (flags & 1)
|
|
|
|
RCHECK(buffer->IgnoreBytes(8)); // aux_info_type and parameter.
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t count = offsets.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&count));
|
2013-09-24 01:35:40 +00:00
|
|
|
offsets.resize(count);
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
|
|
|
|
for (uint32_t i = 0; i < count; ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt64NBytes(&offsets[i], num_bytes));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SampleAuxiliaryInformationOffset::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
// This box is optional. Skip it if it is empty.
|
|
|
|
atom_size = 0;
|
|
|
|
if (offsets.size() != 0) {
|
2014-09-30 21:52:21 +00:00
|
|
|
size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
|
|
|
|
atom_size = kFullBoxSize + sizeof(uint32_t) + num_bytes * offsets.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
}
|
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
2013-11-27 01:52:13 +00:00
|
|
|
|
|
|
|
SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
|
|
|
|
: default_sample_info_size(0), sample_count(0) {}
|
2013-09-24 01:35:40 +00:00
|
|
|
SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
|
|
|
|
FourCC SampleAuxiliaryInformationSize::BoxType() const { return FOURCC_SAIZ; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool SampleAuxiliaryInformationSize::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer));
|
|
|
|
if (flags & 1)
|
|
|
|
RCHECK(buffer->IgnoreBytes(8));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
|
|
|
|
buffer->ReadWriteUInt32(&sample_count));
|
2013-09-24 01:35:40 +00:00
|
|
|
if (default_sample_info_size == 0)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SampleAuxiliaryInformationSize::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
// This box is optional. Skip it if it is empty.
|
|
|
|
atom_size = 0;
|
|
|
|
if (sample_count != 0) {
|
|
|
|
atom_size = kFullBoxSize + sizeof(default_sample_info_size) +
|
|
|
|
sizeof(sample_count) +
|
|
|
|
(default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
|
|
|
|
}
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
|
|
|
|
OriginalFormat::~OriginalFormat() {}
|
|
|
|
FourCC OriginalFormat::BoxType() const { return FOURCC_FRMA; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool OriginalFormat::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
return Box::ReadWrite(buffer) && buffer->ReadWriteFourCC(&format);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t OriginalFormat::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + kFourCCSize;
|
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
|
|
|
|
SchemeType::~SchemeType() {}
|
|
|
|
FourCC SchemeType::BoxType() const { return FOURCC_SCHM; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool SchemeType::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteFourCC(&type) &&
|
|
|
|
buffer->ReadWriteUInt32(&version));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SchemeType::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kFullBoxSize + kFourCCSize + sizeof(version);
|
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
2013-11-27 01:52:13 +00:00
|
|
|
|
|
|
|
TrackEncryption::TrackEncryption()
|
|
|
|
: is_encrypted(false), default_iv_size(0), default_kid(16, 0) {}
|
2013-09-24 01:35:40 +00:00
|
|
|
TrackEncryption::~TrackEncryption() {}
|
|
|
|
FourCC TrackEncryption::BoxType() const { return FOURCC_TENC; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool TrackEncryption::ReadWrite(BoxBuffer* buffer) {
|
2014-10-15 21:56:12 +00:00
|
|
|
if (!buffer->Reading()) {
|
|
|
|
if (default_kid.size() != kCencKeyIdSize) {
|
|
|
|
LOG(WARNING) << "CENC defines key id length of " << kCencKeyIdSize
|
|
|
|
<< " bytes; got " << default_kid.size()
|
|
|
|
<< ". Resized accordingly.";
|
|
|
|
default_kid.resize(kCencKeyIdSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t flag = is_encrypted ? 1 : 0;
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->IgnoreBytes(2) && // reserved.
|
|
|
|
buffer->ReadWriteUInt8(&flag) &&
|
|
|
|
buffer->ReadWriteUInt8(&default_iv_size) &&
|
2014-10-15 21:56:12 +00:00
|
|
|
buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
|
2013-11-27 01:52:13 +00:00
|
|
|
if (buffer->Reading()) {
|
|
|
|
is_encrypted = (flag != 0);
|
|
|
|
if (is_encrypted) {
|
|
|
|
RCHECK(default_iv_size == 8 || default_iv_size == 16);
|
|
|
|
} else {
|
|
|
|
RCHECK(default_iv_size == 0);
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t TrackEncryption::ComputeSize() {
|
2014-10-15 21:56:12 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(uint32_t) + kCencKeyIdSize;
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
SchemeInfo::SchemeInfo() {}
|
|
|
|
SchemeInfo::~SchemeInfo() {}
|
|
|
|
FourCC SchemeInfo::BoxType() const { return FOURCC_SCHI; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool SchemeInfo::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) && buffer->PrepareChildren() &&
|
|
|
|
buffer->ReadWriteChild(&track_encryption));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SchemeInfo::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + track_encryption.ComputeSize();
|
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ProtectionSchemeInfo::ProtectionSchemeInfo() {}
|
|
|
|
ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
|
|
|
|
FourCC ProtectionSchemeInfo::BoxType() const { return FOURCC_SINF; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool ProtectionSchemeInfo::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) &&
|
|
|
|
buffer->PrepareChildren() &&
|
|
|
|
buffer->ReadWriteChild(&format) &&
|
|
|
|
buffer->ReadWriteChild(&type));
|
2013-09-24 01:35:40 +00:00
|
|
|
if (type.type == FOURCC_CENC)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteChild(&info));
|
2013-09-24 01:35:40 +00:00
|
|
|
// Other protection schemes are silently ignored. Since the protection scheme
|
|
|
|
// type can't be determined until this box is opened, we return 'true' for
|
|
|
|
// non-CENC protection scheme types. It is the parent box's responsibility to
|
|
|
|
// ensure that this scheme type is a supported one.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t ProtectionSchemeInfo::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
// Skip sinf box if it is not initialized.
|
|
|
|
atom_size = 0;
|
|
|
|
if (format.format != FOURCC_NULL) {
|
|
|
|
atom_size = kBoxSize + format.ComputeSize() + type.ComputeSize() +
|
|
|
|
info.ComputeSize();
|
|
|
|
}
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
MovieHeader::MovieHeader()
|
|
|
|
: creation_time(0),
|
|
|
|
modification_time(0),
|
|
|
|
timescale(0),
|
|
|
|
duration(0),
|
2013-11-27 01:52:13 +00:00
|
|
|
rate(1 << 16),
|
|
|
|
volume(1 << 8),
|
2013-09-24 01:35:40 +00:00
|
|
|
next_track_id(0) {}
|
|
|
|
MovieHeader::~MovieHeader() {}
|
|
|
|
FourCC MovieHeader::BoxType() const { return FOURCC_MVHD; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool MovieHeader::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer));
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt64NBytes(&creation_time, num_bytes) &&
|
|
|
|
buffer->ReadWriteUInt64NBytes(&modification_time, num_bytes) &&
|
|
|
|
buffer->ReadWriteUInt32(×cale) &&
|
|
|
|
buffer->ReadWriteUInt64NBytes(&duration, num_bytes));
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> matrix(kUnityMatrix,
|
|
|
|
kUnityMatrix + arraysize(kUnityMatrix));
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteInt32(&rate) &&
|
|
|
|
buffer->ReadWriteInt16(&volume) &&
|
|
|
|
buffer->IgnoreBytes(10) && // reserved
|
|
|
|
buffer->ReadWriteVector(&matrix, matrix.size()) &&
|
|
|
|
buffer->IgnoreBytes(24) && // predefined zero
|
|
|
|
buffer->ReadWriteUInt32(&next_track_id));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t MovieHeader::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
|
2014-09-30 21:52:21 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(uint32_t) * (1 + version) * 3 +
|
2013-11-27 01:52:13 +00:00
|
|
|
sizeof(timescale) + sizeof(rate) + sizeof(volume) +
|
|
|
|
sizeof(next_track_id) + sizeof(kUnityMatrix) + 10 +
|
|
|
|
24; // 10 bytes reserved, 24 bytes predefined.
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
TrackHeader::TrackHeader()
|
|
|
|
: creation_time(0),
|
|
|
|
modification_time(0),
|
|
|
|
track_id(0),
|
|
|
|
duration(0),
|
2013-11-27 01:52:13 +00:00
|
|
|
layer(0),
|
|
|
|
alternate_group(0),
|
2013-09-24 01:35:40 +00:00
|
|
|
volume(-1),
|
|
|
|
width(0),
|
2013-11-27 01:52:13 +00:00
|
|
|
height(0) {
|
|
|
|
flags = kTrackEnabled | kTrackInMovie;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
TrackHeader::~TrackHeader() {}
|
|
|
|
FourCC TrackHeader::BoxType() const { return FOURCC_TKHD; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool TrackHeader::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer));
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt64NBytes(&creation_time, num_bytes) &&
|
|
|
|
buffer->ReadWriteUInt64NBytes(&modification_time, num_bytes) &&
|
|
|
|
buffer->ReadWriteUInt32(&track_id) &&
|
|
|
|
buffer->IgnoreBytes(4) && // reserved
|
|
|
|
buffer->ReadWriteUInt64NBytes(&duration, num_bytes));
|
|
|
|
|
|
|
|
if (!buffer->Reading()) {
|
|
|
|
// Set default value for volume, if track is audio, 0x100 else 0.
|
|
|
|
if (volume == -1)
|
|
|
|
volume = (width != 0 && height != 0) ? 0 : 0x100;
|
|
|
|
// Convert integer to 16.16 fix point.
|
|
|
|
width <<= 16;
|
|
|
|
height <<= 16;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> matrix(kUnityMatrix,
|
|
|
|
kUnityMatrix + arraysize(kUnityMatrix));
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->IgnoreBytes(8) && // reserved
|
|
|
|
buffer->ReadWriteInt16(&layer) &&
|
|
|
|
buffer->ReadWriteInt16(&alternate_group) &&
|
|
|
|
buffer->ReadWriteInt16(&volume) &&
|
|
|
|
buffer->IgnoreBytes(2) && // reserved
|
|
|
|
buffer->ReadWriteVector(&matrix, matrix.size()) &&
|
|
|
|
buffer->ReadWriteUInt32(&width) &&
|
|
|
|
buffer->ReadWriteUInt32(&height));
|
|
|
|
// Convert 16.16 fixed point to integer.
|
2013-09-24 01:35:40 +00:00
|
|
|
width >>= 16;
|
|
|
|
height >>= 16;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t TrackHeader::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
|
|
|
|
atom_size = kFullBoxSize + sizeof(track_id) +
|
2014-09-30 21:52:21 +00:00
|
|
|
sizeof(uint32_t) * (1 + version) * 3 + sizeof(layer) +
|
2013-11-27 01:52:13 +00:00
|
|
|
sizeof(alternate_group) + sizeof(volume) + sizeof(width) +
|
|
|
|
sizeof(height) + sizeof(kUnityMatrix) + 14; // 14 bytes reserved.
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
SampleDescription::SampleDescription() : type(kInvalid) {}
|
|
|
|
SampleDescription::~SampleDescription() {}
|
|
|
|
FourCC SampleDescription::BoxType() const { return FOURCC_STSD; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool SampleDescription::ReadWrite(BoxBuffer* buffer) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t count = 0;
|
2013-11-27 01:52:13 +00:00
|
|
|
if (type == kVideo)
|
|
|
|
count = video_entries.size();
|
|
|
|
else
|
|
|
|
count = audio_entries.size();
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&count));
|
|
|
|
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
BoxReader* reader = buffer->reader();
|
|
|
|
DCHECK(reader);
|
|
|
|
video_entries.clear();
|
|
|
|
audio_entries.clear();
|
|
|
|
// Note: this value is preset before scanning begins. See comments in the
|
|
|
|
// Parse(Media*) function.
|
|
|
|
if (type == kVideo) {
|
|
|
|
RCHECK(reader->ReadAllChildren(&video_entries));
|
|
|
|
RCHECK(video_entries.size() == count);
|
|
|
|
} else if (type == kAudio) {
|
|
|
|
RCHECK(reader->ReadAllChildren(&audio_entries));
|
|
|
|
RCHECK(audio_entries.size() == count);
|
|
|
|
}
|
|
|
|
} else {
|
2014-03-21 17:26:49 +00:00
|
|
|
DCHECK_LT(0u, count);
|
2013-11-27 01:52:13 +00:00
|
|
|
if (type == kVideo) {
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(video_entries[i].ReadWrite(buffer));
|
|
|
|
} else if (type == kAudio) {
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(audio_entries[i].ReadWrite(buffer));
|
|
|
|
} else {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SampleDescription::ComputeSize() {
|
|
|
|
atom_size = kFullBoxSize + sizeof(uint32_t);
|
2013-09-24 01:35:40 +00:00
|
|
|
if (type == kVideo) {
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < video_entries.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size += video_entries[i].ComputeSize();
|
2013-09-24 01:35:40 +00:00
|
|
|
} else if (type == kAudio) {
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < audio_entries.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size += audio_entries[i].ComputeSize();
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 17:37:58 +00:00
|
|
|
DecodingTimeToSample::DecodingTimeToSample() {}
|
|
|
|
DecodingTimeToSample::~DecodingTimeToSample() {}
|
|
|
|
FourCC DecodingTimeToSample::BoxType() const { return FOURCC_STTS; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool DecodingTimeToSample::ReadWrite(BoxBuffer* buffer) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t count = decoding_time.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&count));
|
2013-10-08 17:37:58 +00:00
|
|
|
|
|
|
|
decoding_time.resize(count);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
|
|
|
|
buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
|
2013-10-08 17:37:58 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t DecodingTimeToSample::ComputeSize() {
|
|
|
|
atom_size = kFullBoxSize + sizeof(uint32_t) +
|
2013-11-27 01:52:13 +00:00
|
|
|
sizeof(DecodingTime) * decoding_time.size();
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-10-08 17:37:58 +00:00
|
|
|
CompositionTimeToSample::CompositionTimeToSample() {}
|
|
|
|
CompositionTimeToSample::~CompositionTimeToSample() {}
|
|
|
|
FourCC CompositionTimeToSample::BoxType() const { return FOURCC_CTTS; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool CompositionTimeToSample::ReadWrite(BoxBuffer* buffer) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t count = composition_offset.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&count));
|
2013-10-08 17:37:58 +00:00
|
|
|
|
|
|
|
composition_offset.resize(count);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count) &&
|
|
|
|
buffer->ReadWriteInt32(&composition_offset[i].sample_offset));
|
2013-10-08 17:37:58 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t CompositionTimeToSample::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
// Version 1 to support signed offset.
|
|
|
|
version = 1;
|
|
|
|
// This box is optional. Skip it if it is empty.
|
|
|
|
atom_size = 0;
|
|
|
|
if (!composition_offset.empty()) {
|
2014-09-30 21:52:21 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(uint32_t) +
|
2013-11-27 01:52:13 +00:00
|
|
|
sizeof(CompositionOffset) * composition_offset.size();
|
|
|
|
}
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-10-08 17:37:58 +00:00
|
|
|
SampleToChunk::SampleToChunk() {}
|
|
|
|
SampleToChunk::~SampleToChunk() {}
|
|
|
|
FourCC SampleToChunk::BoxType() const { return FOURCC_STSC; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool SampleToChunk::ReadWrite(BoxBuffer* buffer) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t count = chunk_info.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&count));
|
2013-10-08 17:37:58 +00:00
|
|
|
|
|
|
|
chunk_info.resize(count);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
|
|
|
|
buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
|
|
|
|
buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
|
2013-10-08 17:37:58 +00:00
|
|
|
// first_chunk values are always increasing.
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
|
2013-10-08 17:37:58 +00:00
|
|
|
: chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SampleToChunk::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size =
|
2014-09-30 21:52:21 +00:00
|
|
|
kFullBoxSize + sizeof(uint32_t) + sizeof(ChunkInfo) * chunk_info.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-10-08 17:37:58 +00:00
|
|
|
SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
|
|
|
|
SampleSize::~SampleSize() {}
|
|
|
|
FourCC SampleSize::BoxType() const { return FOURCC_STSZ; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool SampleSize::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&sample_size) &&
|
|
|
|
buffer->ReadWriteUInt32(&sample_count));
|
2013-10-08 17:37:58 +00:00
|
|
|
|
|
|
|
if (sample_size == 0) {
|
2013-11-27 01:52:13 +00:00
|
|
|
if (buffer->Reading())
|
|
|
|
sizes.resize(sample_count);
|
|
|
|
else
|
|
|
|
DCHECK(sample_count == sizes.size());
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < sample_count; ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
|
2013-10-08 17:37:58 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SampleSize::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(sample_size) + sizeof(sample_count) +
|
2014-09-30 21:52:21 +00:00
|
|
|
(sample_size == 0 ? sizeof(uint32_t) * sizes.size() : 0);
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompactSampleSize::CompactSampleSize() : field_size(0) {}
|
2013-10-08 17:37:58 +00:00
|
|
|
CompactSampleSize::~CompactSampleSize() {}
|
|
|
|
FourCC CompactSampleSize::BoxType() const { return FOURCC_STZ2; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool CompactSampleSize::ReadWrite(BoxBuffer* buffer) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t sample_count = sizes.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->IgnoreBytes(3) &&
|
|
|
|
buffer->ReadWriteUInt8(&field_size) &&
|
|
|
|
buffer->ReadWriteUInt32(&sample_count));
|
2013-10-08 17:37:58 +00:00
|
|
|
|
|
|
|
// Reserve one more entry if field size is 4 bits.
|
2014-01-16 00:52:07 +00:00
|
|
|
sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
|
2013-10-08 17:37:58 +00:00
|
|
|
switch (field_size) {
|
|
|
|
case 4:
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < sample_count; i += 2) {
|
2013-11-27 01:52:13 +00:00
|
|
|
if (buffer->Reading()) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t size = 0;
|
2014-01-16 00:52:07 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt8(&size));
|
2013-11-27 01:52:13 +00:00
|
|
|
sizes[i] = size >> 4;
|
2014-01-16 00:52:07 +00:00
|
|
|
sizes[i + 1] = size & 0x0F;
|
|
|
|
} else {
|
2014-03-21 17:26:49 +00:00
|
|
|
DCHECK_LT(sizes[i], 16u);
|
|
|
|
DCHECK_LT(sizes[i + 1], 16u);
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t size = (sizes[i] << 4) | sizes[i + 1];
|
2014-01-16 00:52:07 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt8(&size));
|
2013-11-27 01:52:13 +00:00
|
|
|
}
|
2013-10-08 17:37:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < sample_count; ++i) {
|
|
|
|
uint8_t size = sizes[i];
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt8(&size));
|
2013-10-08 17:37:58 +00:00
|
|
|
sizes[i] = size;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16:
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < sample_count; ++i) {
|
|
|
|
uint16_t size = sizes[i];
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt16(&size));
|
2013-10-08 17:37:58 +00:00
|
|
|
sizes[i] = size;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
RCHECK(false);
|
|
|
|
}
|
|
|
|
sizes.resize(sample_count);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t CompactSampleSize::ComputeSize() {
|
|
|
|
atom_size = kFullBoxSize + sizeof(uint32_t) + sizeof(uint32_t) +
|
2013-11-27 01:52:13 +00:00
|
|
|
(field_size * sizes.size() + 7) / 8;
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-10-08 17:37:58 +00:00
|
|
|
ChunkOffset::ChunkOffset() {}
|
|
|
|
ChunkOffset::~ChunkOffset() {}
|
|
|
|
FourCC ChunkOffset::BoxType() const { return FOURCC_STCO; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool ChunkOffset::ReadWrite(BoxBuffer* buffer) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t count = offsets.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&count));
|
2013-10-08 17:37:58 +00:00
|
|
|
|
|
|
|
offsets.resize(count);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i)
|
|
|
|
RCHECK(buffer->ReadWriteUInt64NBytes(&offsets[i], sizeof(uint32_t)));
|
2013-10-08 17:37:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t ChunkOffset::ComputeSize() {
|
|
|
|
atom_size =
|
|
|
|
kFullBoxSize + sizeof(uint32_t) + sizeof(uint32_t) * offsets.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-10-08 17:37:58 +00:00
|
|
|
ChunkLargeOffset::ChunkLargeOffset() {}
|
|
|
|
ChunkLargeOffset::~ChunkLargeOffset() {}
|
|
|
|
FourCC ChunkLargeOffset::BoxType() const { return FOURCC_CO64; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool ChunkLargeOffset::ReadWrite(BoxBuffer* buffer) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t count = offsets.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
|
|
|
|
if (!buffer->Reading()) {
|
|
|
|
// Switch to ChunkOffset box if it is able to fit in 32 bits offset.
|
|
|
|
if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
|
|
|
|
ChunkOffset stco;
|
|
|
|
stco.offsets.swap(offsets);
|
|
|
|
DCHECK(buffer->writer());
|
|
|
|
stco.Write(buffer->writer());
|
|
|
|
stco.offsets.swap(offsets);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&count));
|
2013-10-08 17:37:58 +00:00
|
|
|
|
|
|
|
offsets.resize(count);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
|
2013-10-08 17:37:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t ChunkLargeOffset::ComputeSize() {
|
|
|
|
uint32_t count = offsets.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
int use_large_offset =
|
|
|
|
(count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
|
|
|
|
atom_size = kFullBoxSize + sizeof(count) +
|
2014-09-30 21:52:21 +00:00
|
|
|
sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-10-08 17:37:58 +00:00
|
|
|
SyncSample::SyncSample() {}
|
|
|
|
SyncSample::~SyncSample() {}
|
|
|
|
FourCC SyncSample::BoxType() const { return FOURCC_STSS; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool SyncSample::ReadWrite(BoxBuffer* buffer) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t count = sample_number.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&count));
|
2013-10-08 17:37:58 +00:00
|
|
|
|
|
|
|
sample_number.resize(count);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
|
2013-10-08 17:37:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SyncSample::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
// Sync sample box is optional. Skip it if it is empty.
|
|
|
|
atom_size = 0;
|
|
|
|
if (!sample_number.empty()) {
|
2014-09-30 21:52:21 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(uint32_t) +
|
|
|
|
sizeof(uint32_t) * sample_number.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
}
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
SampleTable::SampleTable() {}
|
|
|
|
SampleTable::~SampleTable() {}
|
|
|
|
FourCC SampleTable::BoxType() const { return FOURCC_STBL; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool SampleTable::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) &&
|
|
|
|
buffer->PrepareChildren() &&
|
|
|
|
buffer->ReadWriteChild(&description) &&
|
|
|
|
buffer->ReadWriteChild(&decoding_time_to_sample) &&
|
|
|
|
buffer->TryReadWriteChild(&composition_time_to_sample) &&
|
|
|
|
buffer->ReadWriteChild(&sample_to_chunk));
|
|
|
|
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
BoxReader* reader = buffer->reader();
|
|
|
|
DCHECK(reader);
|
|
|
|
|
|
|
|
// Either SampleSize or CompactSampleSize must present.
|
|
|
|
if (reader->ChildExist(&sample_size)) {
|
|
|
|
RCHECK(reader->ReadChild(&sample_size));
|
|
|
|
} else {
|
|
|
|
CompactSampleSize compact_sample_size;
|
|
|
|
RCHECK(reader->ReadChild(&compact_sample_size));
|
|
|
|
sample_size.sample_size = 0;
|
|
|
|
sample_size.sample_count = compact_sample_size.sizes.size();
|
|
|
|
sample_size.sizes.swap(compact_sample_size.sizes);
|
|
|
|
}
|
2013-10-08 17:37:58 +00:00
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
// Either ChunkOffset or ChunkLargeOffset must present.
|
|
|
|
if (reader->ChildExist(&chunk_large_offset)) {
|
|
|
|
RCHECK(reader->ReadChild(&chunk_large_offset));
|
|
|
|
} else {
|
|
|
|
ChunkOffset chunk_offset;
|
|
|
|
RCHECK(reader->ReadChild(&chunk_offset));
|
|
|
|
chunk_large_offset.offsets.swap(chunk_offset.offsets);
|
|
|
|
}
|
2013-10-08 17:37:58 +00:00
|
|
|
} else {
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(sample_size.ReadWrite(buffer) &&
|
|
|
|
chunk_large_offset.ReadWrite(buffer));
|
2013-10-08 17:37:58 +00:00
|
|
|
}
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->TryReadWriteChild(&sync_sample));
|
2013-10-08 17:37:58 +00:00
|
|
|
return true;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SampleTable::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + description.ComputeSize() +
|
|
|
|
decoding_time_to_sample.ComputeSize() +
|
|
|
|
composition_time_to_sample.ComputeSize() +
|
|
|
|
sample_to_chunk.ComputeSize() + sample_size.ComputeSize() +
|
|
|
|
chunk_large_offset.ComputeSize() + sync_sample.ComputeSize();
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
EditList::EditList() {}
|
|
|
|
EditList::~EditList() {}
|
|
|
|
FourCC EditList::BoxType() const { return FOURCC_ELST; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool EditList::ReadWrite(BoxBuffer* buffer) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t count = edits.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(FullBox::ReadWrite(buffer) && buffer->ReadWriteUInt32(&count));
|
|
|
|
edits.resize(count);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(
|
|
|
|
buffer->ReadWriteUInt64NBytes(&edits[i].segment_duration, num_bytes) &&
|
|
|
|
buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
|
|
|
|
buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
|
|
|
|
buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
2013-11-27 01:52:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t EditList::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
// EditList box is optional. Skip it if it is empty.
|
|
|
|
atom_size = 0;
|
|
|
|
if (edits.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
version = 0;
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < edits.size(); ++i) {
|
2013-11-27 01:52:13 +00:00
|
|
|
if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
|
|
|
|
version = 1;
|
|
|
|
break;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(uint32_t) +
|
|
|
|
(sizeof(uint32_t) * (1 + version) * 2 + sizeof(int16_t) * 2) *
|
|
|
|
edits.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Edit::Edit() {}
|
|
|
|
Edit::~Edit() {}
|
|
|
|
FourCC Edit::BoxType() const { return FOURCC_EDTS; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool Edit::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
return Box::ReadWrite(buffer) &&
|
|
|
|
buffer->PrepareChildren() &&
|
|
|
|
buffer->ReadWriteChild(&list);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t Edit::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
// Edit box is optional. Skip it if it is empty.
|
|
|
|
atom_size = 0;
|
|
|
|
if (!list.edits.empty())
|
|
|
|
atom_size = kBoxSize + list.ComputeSize();
|
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HandlerReference::HandlerReference() : type(kInvalid) {}
|
|
|
|
HandlerReference::~HandlerReference() {}
|
|
|
|
FourCC HandlerReference::BoxType() const { return FOURCC_HDLR; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool HandlerReference::ReadWrite(BoxBuffer* buffer) {
|
2014-03-21 17:26:49 +00:00
|
|
|
FourCC hdlr_type = FOURCC_NULL;
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> handler_name;
|
2013-11-27 01:52:13 +00:00
|
|
|
if (!buffer->Reading()) {
|
|
|
|
if (type == kVideo) {
|
|
|
|
hdlr_type = FOURCC_VIDE;
|
|
|
|
handler_name.assign(kVideoHandlerName,
|
|
|
|
kVideoHandlerName + arraysize(kVideoHandlerName));
|
|
|
|
} else if (type == kAudio) {
|
|
|
|
hdlr_type = FOURCC_SOUN;
|
|
|
|
handler_name.assign(kAudioHandlerName,
|
|
|
|
kAudioHandlerName + arraysize(kAudioHandlerName));
|
|
|
|
} else {
|
|
|
|
NOTIMPLEMENTED();
|
2014-03-21 17:26:49 +00:00
|
|
|
return false;
|
2013-11-27 01:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->IgnoreBytes(4) && // predefined.
|
|
|
|
buffer->ReadWriteFourCC(&hdlr_type));
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
// Note: for reading, remaining fields in box ignored.
|
|
|
|
if (hdlr_type == FOURCC_VIDE) {
|
|
|
|
type = kVideo;
|
|
|
|
} else if (hdlr_type == FOURCC_SOUN) {
|
|
|
|
type = kAudio;
|
|
|
|
} else {
|
|
|
|
type = kInvalid;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
} else {
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->IgnoreBytes(12) && // reserved.
|
|
|
|
buffer->ReadWriteVector(&handler_name, handler_name.size()));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t HandlerReference::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size =
|
|
|
|
kFullBoxSize + kFourCCSize + 16 + // 16 bytes Reserved
|
|
|
|
(type == kVideo ? sizeof(kVideoHandlerName) : sizeof(kAudioHandlerName));
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
AVCDecoderConfigurationRecord::AVCDecoderConfigurationRecord()
|
|
|
|
: version(0),
|
|
|
|
profile_indication(0),
|
|
|
|
profile_compatibility(0),
|
|
|
|
avc_level(0),
|
|
|
|
length_size(0) {}
|
|
|
|
|
|
|
|
AVCDecoderConfigurationRecord::~AVCDecoderConfigurationRecord() {}
|
|
|
|
FourCC AVCDecoderConfigurationRecord::BoxType() const { return FOURCC_AVCC; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool AVCDecoderConfigurationRecord::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer));
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
RCHECK(buffer->ReadWriteVector(&data, buffer->Size() - buffer->Pos()));
|
|
|
|
BufferReader buffer_reader(&data[0], data.size());
|
|
|
|
return ParseData(&buffer_reader);
|
|
|
|
} else {
|
|
|
|
RCHECK(buffer->ReadWriteVector(&data, data.size()));
|
|
|
|
}
|
|
|
|
return true;
|
2013-10-14 20:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AVCDecoderConfigurationRecord::ParseData(BufferReader* reader) {
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(reader->Read1(&version) && version == 1 &&
|
|
|
|
reader->Read1(&profile_indication) &&
|
|
|
|
reader->Read1(&profile_compatibility) &&
|
|
|
|
reader->Read1(&avc_level));
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t length_size_minus_one;
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(reader->Read1(&length_size_minus_one) &&
|
|
|
|
(length_size_minus_one & 0xfc) == 0xfc);
|
|
|
|
length_size = (length_size_minus_one & 0x3) + 1;
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t num_sps;
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(reader->Read1(&num_sps) && (num_sps & 0xe0) == 0xe0);
|
|
|
|
num_sps &= 0x1f;
|
|
|
|
|
|
|
|
sps_list.resize(num_sps);
|
|
|
|
for (int i = 0; i < num_sps; i++) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint16_t sps_length;
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(reader->Read2(&sps_length) &&
|
2013-11-27 01:52:13 +00:00
|
|
|
reader->ReadToVector(&sps_list[i], sps_length));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t num_pps;
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(reader->Read1(&num_pps));
|
|
|
|
|
|
|
|
pps_list.resize(num_pps);
|
|
|
|
for (int i = 0; i < num_pps; i++) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint16_t pps_length;
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(reader->Read2(&pps_length) &&
|
2013-11-27 01:52:13 +00:00
|
|
|
reader->ReadToVector(&pps_list[i], pps_length));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t AVCDecoderConfigurationRecord::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = 0;
|
|
|
|
if (!data.empty())
|
|
|
|
atom_size = kBoxSize + data.size();
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
PixelAspectRatioBox::PixelAspectRatioBox() : h_spacing(0), v_spacing(0) {}
|
2013-09-24 01:35:40 +00:00
|
|
|
PixelAspectRatioBox::~PixelAspectRatioBox() {}
|
|
|
|
FourCC PixelAspectRatioBox::BoxType() const { return FOURCC_PASP; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool PixelAspectRatioBox::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&h_spacing) &&
|
|
|
|
buffer->ReadWriteUInt32(&v_spacing));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t PixelAspectRatioBox::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
// This box is optional. Skip it if it is not initialized.
|
|
|
|
atom_size = 0;
|
|
|
|
if (h_spacing != 0 || v_spacing != 0) {
|
|
|
|
// Both values must be positive.
|
|
|
|
DCHECK(h_spacing != 0 && v_spacing != 0);
|
|
|
|
atom_size = kBoxSize + sizeof(h_spacing) + sizeof(v_spacing);
|
|
|
|
}
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
VideoSampleEntry::VideoSampleEntry()
|
2013-11-27 01:52:13 +00:00
|
|
|
: format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
VideoSampleEntry::~VideoSampleEntry() {}
|
|
|
|
FourCC VideoSampleEntry::BoxType() const {
|
2013-11-27 01:52:13 +00:00
|
|
|
LOG(ERROR) << "VideoSampleEntry should be parsed according to the "
|
|
|
|
<< "handler type recovered in its Media ancestor.";
|
2013-09-24 01:35:40 +00:00
|
|
|
return FOURCC_NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool VideoSampleEntry::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
DCHECK(buffer->reader());
|
|
|
|
format = buffer->reader()->type();
|
|
|
|
} else {
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&atom_size) &&
|
|
|
|
buffer->ReadWriteFourCC(&format));
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t video_resolution = kVideoResolution;
|
|
|
|
uint16_t video_frame_count = kVideoFrameCount;
|
|
|
|
uint16_t video_depth = kVideoDepth;
|
|
|
|
int16_t predefined = -1;
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->IgnoreBytes(6) && // reserved.
|
|
|
|
buffer->ReadWriteUInt16(&data_reference_index) &&
|
|
|
|
buffer->IgnoreBytes(16) && // predefined 0.
|
|
|
|
buffer->ReadWriteUInt16(&width) &&
|
|
|
|
buffer->ReadWriteUInt16(&height) &&
|
|
|
|
buffer->ReadWriteUInt32(&video_resolution) &&
|
|
|
|
buffer->ReadWriteUInt32(&video_resolution) &&
|
|
|
|
buffer->IgnoreBytes(4) && // reserved.
|
|
|
|
buffer->ReadWriteUInt16(&video_frame_count) &&
|
|
|
|
buffer->IgnoreBytes(32) && // comparessor_name.
|
|
|
|
buffer->ReadWriteUInt16(&video_depth) &&
|
|
|
|
buffer->ReadWriteInt16(&predefined));
|
|
|
|
|
|
|
|
RCHECK(buffer->PrepareChildren() &&
|
|
|
|
buffer->TryReadWriteChild(&pixel_aspect));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
if (format == FOURCC_ENCV) {
|
2013-11-27 01:52:13 +00:00
|
|
|
if (buffer->Reading()) {
|
|
|
|
// Continue scanning until a recognized protection scheme is found,
|
|
|
|
// or until we run out of protection schemes.
|
|
|
|
while (sinf.type.type != FOURCC_CENC) {
|
|
|
|
if (!buffer->ReadWriteChild(&sinf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
RCHECK(buffer->ReadWriteChild(&sinf));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format == FOURCC_AVC1 ||
|
|
|
|
(format == FOURCC_ENCV && sinf.format.format == FOURCC_AVC1)) {
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteChild(&avcc));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t VideoSampleEntry::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + sizeof(data_reference_index) + sizeof(width) +
|
|
|
|
sizeof(height) + sizeof(kVideoResolution) * 2 +
|
|
|
|
sizeof(kVideoFrameCount) + sizeof(kVideoDepth) +
|
|
|
|
pixel_aspect.ComputeSize() + sinf.ComputeSize() +
|
|
|
|
avcc.ComputeSize() + 32 + // 32 bytes comparessor_name.
|
|
|
|
6 + 4 + 16 + 2; // 6 + 4 bytes reserved, 16 + 2 bytes predefined.
|
|
|
|
return atom_size;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
|
2013-09-24 01:35:40 +00:00
|
|
|
ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
|
2013-11-27 01:52:13 +00:00
|
|
|
FourCC ElementaryStreamDescriptor::BoxType() const { return FOURCC_ESDS; }
|
|
|
|
|
|
|
|
bool ElementaryStreamDescriptor::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer));
|
|
|
|
if (buffer->Reading()) {
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> data;
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteVector(&data, buffer->Size() - buffer->Pos()));
|
|
|
|
RCHECK(es_descriptor.Parse(data));
|
2014-01-10 00:21:06 +00:00
|
|
|
if (es_descriptor.IsAAC()) {
|
|
|
|
RCHECK(aac_audio_specific_config.Parse(
|
|
|
|
es_descriptor.decoder_specific_info()));
|
|
|
|
}
|
2013-11-27 01:52:13 +00:00
|
|
|
} else {
|
|
|
|
DCHECK(buffer->writer());
|
|
|
|
es_descriptor.Write(buffer->writer());
|
|
|
|
}
|
|
|
|
return true;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t ElementaryStreamDescriptor::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
// This box is optional. Skip it if not initialized.
|
|
|
|
atom_size = 0;
|
|
|
|
if (es_descriptor.object_type() != kForbidden)
|
|
|
|
atom_size = kFullBoxSize + es_descriptor.ComputeSize();
|
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioSampleEntry::AudioSampleEntry()
|
|
|
|
: format(FOURCC_NULL),
|
2013-11-27 01:52:13 +00:00
|
|
|
data_reference_index(1),
|
|
|
|
channelcount(2),
|
|
|
|
samplesize(16),
|
2013-09-24 01:35:40 +00:00
|
|
|
samplerate(0) {}
|
|
|
|
|
|
|
|
AudioSampleEntry::~AudioSampleEntry() {}
|
|
|
|
|
|
|
|
FourCC AudioSampleEntry::BoxType() const {
|
2013-11-27 01:52:13 +00:00
|
|
|
LOG(ERROR) << "AudioSampleEntry should be parsed according to the "
|
|
|
|
<< "handler type recovered in its Media ancestor.";
|
2013-09-24 01:35:40 +00:00
|
|
|
return FOURCC_NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool AudioSampleEntry::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
DCHECK(buffer->reader());
|
|
|
|
format = buffer->reader()->type();
|
|
|
|
} else {
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&atom_size) &&
|
|
|
|
buffer->ReadWriteFourCC(&format));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert from integer to 16.16 fixed point for writing.
|
|
|
|
samplerate <<= 16;
|
|
|
|
RCHECK(buffer->IgnoreBytes(6) && // reserved.
|
|
|
|
buffer->ReadWriteUInt16(&data_reference_index) &&
|
|
|
|
buffer->IgnoreBytes(8) && // reserved.
|
|
|
|
buffer->ReadWriteUInt16(&channelcount) &&
|
|
|
|
buffer->ReadWriteUInt16(&samplesize) &&
|
|
|
|
buffer->IgnoreBytes(4) && // predefined.
|
|
|
|
buffer->ReadWriteUInt32(&samplerate));
|
|
|
|
// Convert from 16.16 fixed point to integer.
|
2013-09-24 01:35:40 +00:00
|
|
|
samplerate >>= 16;
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->PrepareChildren());
|
2013-09-24 01:35:40 +00:00
|
|
|
if (format == FOURCC_ENCA) {
|
2013-11-27 01:52:13 +00:00
|
|
|
if (buffer->Reading()) {
|
|
|
|
// Continue scanning until a recognized protection scheme is found,
|
|
|
|
// or until we run out of protection schemes.
|
|
|
|
while (sinf.type.type != FOURCC_CENC) {
|
|
|
|
if (!buffer->ReadWriteChild(&sinf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
RCHECK(buffer->ReadWriteChild(&sinf));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ESDS is not valid in case of EAC3.
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->TryReadWriteChild(&esds));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t AudioSampleEntry::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + sizeof(data_reference_index) + sizeof(channelcount) +
|
|
|
|
sizeof(samplesize) + sizeof(samplerate) + sinf.ComputeSize() +
|
|
|
|
esds.ComputeSize() + 6 + 8 + // 6 + 8 bytes reserved.
|
|
|
|
4; // 4 bytes predefined.
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
MediaHeader::MediaHeader()
|
2013-11-27 01:52:13 +00:00
|
|
|
: creation_time(0), modification_time(0), timescale(0), duration(0) {
|
|
|
|
language[0] = 0;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
MediaHeader::~MediaHeader() {}
|
|
|
|
FourCC MediaHeader::BoxType() const { return FOURCC_MDHD; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool MediaHeader::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer));
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt64NBytes(&creation_time, num_bytes) &&
|
|
|
|
buffer->ReadWriteUInt64NBytes(&modification_time, num_bytes) &&
|
|
|
|
buffer->ReadWriteUInt32(×cale) &&
|
|
|
|
buffer->ReadWriteUInt64NBytes(&duration, num_bytes));
|
|
|
|
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
// Read language codes into temp first then use BitReader to read the
|
|
|
|
// values. ISO-639-2/T language code: unsigned int(5)[3] language (2 bytes).
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> temp;
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteVector(&temp, 2));
|
|
|
|
|
|
|
|
BitReader bit_reader(&temp[0], 2);
|
|
|
|
bit_reader.SkipBits(1);
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
CHECK(bit_reader.ReadBits(5, &language[i]));
|
|
|
|
language[i] += 0x60;
|
|
|
|
}
|
|
|
|
language[3] = '\0';
|
2013-09-24 01:35:40 +00:00
|
|
|
} else {
|
2013-11-27 01:52:13 +00:00
|
|
|
// Set up default language if it is not set.
|
|
|
|
const char kUndefinedLanguage[] = "und";
|
|
|
|
if (language[0] == 0)
|
|
|
|
strcpy(language, kUndefinedLanguage);
|
|
|
|
|
|
|
|
// Lang format: bit(1) pad, unsigned int(5)[3] language.
|
2014-09-30 21:52:21 +00:00
|
|
|
uint16_t lang = 0;
|
2013-11-27 01:52:13 +00:00
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
lang |= (language[i] - 0x60) << ((2 - i) * 5);
|
|
|
|
RCHECK(buffer->ReadWriteUInt16(&lang));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
2013-10-14 20:55:48 +00:00
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->IgnoreBytes(2)); // predefined.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t MediaHeader::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
|
|
|
|
atom_size = kFullBoxSize + sizeof(timescale) +
|
2014-09-30 21:52:21 +00:00
|
|
|
sizeof(uint32_t) * (1 + version) * 3 + 2 + // 2 bytes language.
|
|
|
|
2; // 2 bytes predefined.
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoMediaHeader::VideoMediaHeader()
|
|
|
|
: graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t kVideoMediaHeaderFlags = 1;
|
2013-11-27 01:52:13 +00:00
|
|
|
flags = kVideoMediaHeaderFlags;
|
|
|
|
}
|
|
|
|
VideoMediaHeader::~VideoMediaHeader() {}
|
|
|
|
FourCC VideoMediaHeader::BoxType() const { return FOURCC_VMHD; }
|
|
|
|
bool VideoMediaHeader::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt16(&graphicsmode) &&
|
|
|
|
buffer->ReadWriteUInt16(&opcolor_red) &&
|
|
|
|
buffer->ReadWriteUInt16(&opcolor_green) &&
|
|
|
|
buffer->ReadWriteUInt16(&opcolor_blue));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t VideoMediaHeader::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(graphicsmode) + sizeof(opcolor_red) +
|
|
|
|
sizeof(opcolor_green) + sizeof(opcolor_blue);
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
SoundMediaHeader::SoundMediaHeader() : balance(0) {}
|
|
|
|
SoundMediaHeader::~SoundMediaHeader() {}
|
|
|
|
FourCC SoundMediaHeader::BoxType() const { return FOURCC_SMHD; }
|
|
|
|
bool SoundMediaHeader::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt16(&balance) &&
|
|
|
|
buffer->IgnoreBytes(2)); // reserved.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SoundMediaHeader::ComputeSize() {
|
|
|
|
atom_size = kFullBoxSize + sizeof(balance) + sizeof(uint16_t);
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
|
|
|
}
|
2013-10-14 20:55:48 +00:00
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
DataEntryUrl::DataEntryUrl() {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t kDataEntryUrlFlags = 1;
|
2013-11-27 01:52:13 +00:00
|
|
|
flags = kDataEntryUrlFlags;
|
|
|
|
}
|
|
|
|
DataEntryUrl::~DataEntryUrl() {}
|
|
|
|
FourCC DataEntryUrl::BoxType() const { return FOURCC_URL; }
|
|
|
|
bool DataEntryUrl::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer));
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
RCHECK(buffer->ReadWriteVector(&location, buffer->Size() - buffer->Pos()));
|
|
|
|
} else {
|
|
|
|
RCHECK(buffer->ReadWriteVector(&location, location.size()));
|
2013-10-14 20:55:48 +00:00
|
|
|
}
|
2013-11-27 01:52:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-10-14 20:55:48 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t DataEntryUrl::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + sizeof(flags) + location.size();
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataReference::DataReference() {
|
|
|
|
// Default 1 entry.
|
|
|
|
data_entry.resize(1);
|
|
|
|
}
|
|
|
|
DataReference::~DataReference() {}
|
|
|
|
FourCC DataReference::BoxType() const { return FOURCC_DREF; }
|
|
|
|
bool DataReference::ReadWrite(BoxBuffer* buffer) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t entry_count = data_entry.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&entry_count));
|
|
|
|
data_entry.resize(entry_count);
|
|
|
|
RCHECK(buffer->PrepareChildren());
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < entry_count; ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteChild(&data_entry[i]));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t DataReference::ComputeSize() {
|
|
|
|
uint32_t count = data_entry.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(count);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size += data_entry[i].ComputeSize();
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataInformation::DataInformation() {}
|
|
|
|
DataInformation::~DataInformation() {}
|
|
|
|
FourCC DataInformation::BoxType() const { return FOURCC_DINF; }
|
|
|
|
|
|
|
|
bool DataInformation::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
return Box::ReadWrite(buffer) &&
|
|
|
|
buffer->PrepareChildren() &&
|
|
|
|
buffer->ReadWriteChild(&dref);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t DataInformation::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + dref.ComputeSize();
|
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaInformation::MediaInformation() {}
|
|
|
|
MediaInformation::~MediaInformation() {}
|
|
|
|
FourCC MediaInformation::BoxType() const { return FOURCC_MINF; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool MediaInformation::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) &&
|
|
|
|
buffer->PrepareChildren() &&
|
|
|
|
buffer->ReadWriteChild(&dinf) &&
|
|
|
|
buffer->ReadWriteChild(&sample_table));
|
|
|
|
if (sample_table.description.type == kVideo)
|
|
|
|
RCHECK(buffer->ReadWriteChild(&vmhd));
|
|
|
|
else if (sample_table.description.type == kAudio)
|
|
|
|
RCHECK(buffer->ReadWriteChild(&smhd));
|
|
|
|
else
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
// Hint is not supported for now.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t MediaInformation::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + dinf.ComputeSize() + sample_table.ComputeSize();
|
|
|
|
if (sample_table.description.type == kVideo)
|
|
|
|
atom_size += vmhd.ComputeSize();
|
|
|
|
else if (sample_table.description.type == kAudio)
|
|
|
|
atom_size += smhd.ComputeSize();
|
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Media::Media() {}
|
|
|
|
Media::~Media() {}
|
|
|
|
FourCC Media::BoxType() const { return FOURCC_MDIA; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool Media::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) &&
|
|
|
|
buffer->PrepareChildren() &&
|
|
|
|
buffer->ReadWriteChild(&header) &&
|
|
|
|
buffer->ReadWriteChild(&handler));
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
// Maddeningly, the HandlerReference box specifies how to parse the
|
|
|
|
// SampleDescription box, making the latter the only box (of those that we
|
|
|
|
// support) which cannot be parsed correctly on its own (or even with
|
|
|
|
// information from its strict ancestor tree). We thus copy the handler type
|
|
|
|
// to the sample description box *before* parsing it to provide this
|
|
|
|
// information while parsing.
|
|
|
|
information.sample_table.description.type = handler.type;
|
|
|
|
} else {
|
|
|
|
DCHECK_EQ(information.sample_table.description.type, handler.type);
|
|
|
|
}
|
|
|
|
RCHECK(buffer->ReadWriteChild(&information));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t Media::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + header.ComputeSize() + handler.ComputeSize() +
|
|
|
|
information.ComputeSize();
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
Track::Track() {}
|
|
|
|
Track::~Track() {}
|
|
|
|
FourCC Track::BoxType() const { return FOURCC_TRAK; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool Track::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) &&
|
|
|
|
buffer->PrepareChildren() &&
|
|
|
|
buffer->ReadWriteChild(&header) &&
|
|
|
|
buffer->ReadWriteChild(&media) &&
|
|
|
|
buffer->TryReadWriteChild(&edit));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t Track::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + header.ComputeSize() + media.ComputeSize() +
|
|
|
|
edit.ComputeSize();
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
|
|
|
|
MovieExtendsHeader::~MovieExtendsHeader() {}
|
|
|
|
FourCC MovieExtendsHeader::BoxType() const { return FOURCC_MEHD; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool MovieExtendsHeader::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer));
|
2014-09-30 21:52:21 +00:00
|
|
|
size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt64NBytes(&fragment_duration, num_bytes));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t MovieExtendsHeader::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = 0;
|
|
|
|
// This box is optional. Skip it if it is not used.
|
|
|
|
if (fragment_duration != 0) {
|
|
|
|
version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
|
2014-09-30 21:52:21 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(uint32_t) * (1 + version);
|
2013-11-27 01:52:13 +00:00
|
|
|
}
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
TrackExtends::TrackExtends()
|
|
|
|
: track_id(0),
|
|
|
|
default_sample_description_index(0),
|
|
|
|
default_sample_duration(0),
|
|
|
|
default_sample_size(0),
|
|
|
|
default_sample_flags(0) {}
|
|
|
|
TrackExtends::~TrackExtends() {}
|
|
|
|
FourCC TrackExtends::BoxType() const { return FOURCC_TREX; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool TrackExtends::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&track_id) &&
|
|
|
|
buffer->ReadWriteUInt32(&default_sample_description_index) &&
|
|
|
|
buffer->ReadWriteUInt32(&default_sample_duration) &&
|
|
|
|
buffer->ReadWriteUInt32(&default_sample_size) &&
|
|
|
|
buffer->ReadWriteUInt32(&default_sample_flags));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t TrackExtends::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(track_id) +
|
|
|
|
sizeof(default_sample_description_index) +
|
|
|
|
sizeof(default_sample_duration) + sizeof(default_sample_size) +
|
|
|
|
sizeof(default_sample_flags);
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
MovieExtends::MovieExtends() {}
|
|
|
|
MovieExtends::~MovieExtends() {}
|
|
|
|
FourCC MovieExtends::BoxType() const { return FOURCC_MVEX; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool MovieExtends::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) &&
|
|
|
|
buffer->PrepareChildren() &&
|
|
|
|
buffer->TryReadWriteChild(&header));
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
DCHECK(buffer->reader());
|
|
|
|
RCHECK(buffer->reader()->ReadChildren(&tracks));
|
|
|
|
} else {
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < tracks.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(tracks[i].ReadWrite(buffer));
|
|
|
|
}
|
|
|
|
return true;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t MovieExtends::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
// This box is optional. Skip it if it does not contain any track.
|
|
|
|
atom_size = 0;
|
|
|
|
if (tracks.size() != 0) {
|
|
|
|
atom_size = kBoxSize + header.ComputeSize();
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < tracks.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size += tracks[i].ComputeSize();
|
|
|
|
}
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Movie::Movie() {}
|
2013-09-24 01:35:40 +00:00
|
|
|
Movie::~Movie() {}
|
|
|
|
FourCC Movie::BoxType() const { return FOURCC_MOOV; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool Movie::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) &&
|
|
|
|
buffer->PrepareChildren() &&
|
|
|
|
buffer->ReadWriteChild(&header) &&
|
|
|
|
buffer->TryReadWriteChild(&extends));
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
BoxReader* reader = buffer->reader();
|
|
|
|
DCHECK(reader);
|
|
|
|
RCHECK(reader->ReadChildren(&tracks) &&
|
|
|
|
reader->TryReadChildren(&pssh));
|
|
|
|
} else {
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < tracks.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(tracks[i].ReadWrite(buffer));
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < pssh.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(pssh[i].ReadWrite(buffer));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t Movie::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + header.ComputeSize() + extends.ComputeSize();
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < tracks.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size += tracks[i].ComputeSize();
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < pssh.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size += pssh[i].ComputeSize();
|
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
|
|
|
|
TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
|
|
|
|
FourCC TrackFragmentDecodeTime::BoxType() const { return FOURCC_TFDT; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool TrackFragmentDecodeTime::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer));
|
2014-09-30 21:52:21 +00:00
|
|
|
size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt64NBytes(&decode_time, num_bytes));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t TrackFragmentDecodeTime::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
version = IsFitIn32Bits(decode_time) ? 0 : 1;
|
2014-09-30 21:52:21 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(uint32_t) * (1 + version);
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
|
|
|
|
MovieFragmentHeader::~MovieFragmentHeader() {}
|
|
|
|
FourCC MovieFragmentHeader::BoxType() const { return FOURCC_MFHD; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool MovieFragmentHeader::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
return FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&sequence_number);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t MovieFragmentHeader::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(sequence_number);
|
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TrackFragmentHeader::TrackFragmentHeader()
|
|
|
|
: track_id(0),
|
|
|
|
sample_description_index(0),
|
|
|
|
default_sample_duration(0),
|
|
|
|
default_sample_size(0),
|
2013-11-27 01:52:13 +00:00
|
|
|
default_sample_flags(0) {}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
TrackFragmentHeader::~TrackFragmentHeader() {}
|
|
|
|
FourCC TrackFragmentHeader::BoxType() const { return FOURCC_TFHD; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool TrackFragmentHeader::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&track_id));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
// Media Source specific: reject tracks that set 'base-data-offset-present'.
|
|
|
|
// Although the Media Source requires that 'default-base-is-moof' (14496-12
|
|
|
|
// Amendment 2) be set, we omit this check as many otherwise-valid files in
|
|
|
|
// the wild don't set it.
|
|
|
|
//
|
2013-11-27 01:52:13 +00:00
|
|
|
// RCHECK((flags & kDefaultBaseIsMoofMask) &&
|
|
|
|
// !(flags & kBaseDataOffsetPresentMask));
|
2014-03-26 22:09:43 +00:00
|
|
|
if (flags & kDataOffsetPresentMask) {
|
|
|
|
NOTIMPLEMENTED() << " base-data-offset-present is not supported.";
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
if (flags & kSampleDescriptionIndexPresentMask) {
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
|
|
|
|
} else if (buffer->Reading()) {
|
2013-09-24 01:35:40 +00:00
|
|
|
sample_description_index = 0;
|
|
|
|
}
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
if (flags & kDefaultSampleDurationPresentMask) {
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
|
|
|
|
} else if (buffer->Reading()) {
|
2013-09-24 01:35:40 +00:00
|
|
|
default_sample_duration = 0;
|
|
|
|
}
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
if (flags & kDefaultSampleSizePresentMask) {
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
|
|
|
|
} else if (buffer->Reading()) {
|
2013-09-24 01:35:40 +00:00
|
|
|
default_sample_size = 0;
|
|
|
|
}
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
if (flags & kDefaultSampleFlagsPresentMask)
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t TrackFragmentHeader::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(track_id);
|
|
|
|
if (flags & kSampleDescriptionIndexPresentMask)
|
|
|
|
atom_size += sizeof(sample_description_index);
|
|
|
|
if (flags & kDefaultSampleDurationPresentMask)
|
|
|
|
atom_size += sizeof(default_sample_duration);
|
|
|
|
if (flags & kDefaultSampleSizePresentMask)
|
|
|
|
atom_size += sizeof(default_sample_size);
|
|
|
|
if (flags & kDefaultSampleFlagsPresentMask)
|
|
|
|
atom_size += sizeof(default_sample_flags);
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
|
2013-09-24 01:35:40 +00:00
|
|
|
TrackFragmentRun::~TrackFragmentRun() {}
|
|
|
|
FourCC TrackFragmentRun::BoxType() const { return FOURCC_TRUN; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool TrackFragmentRun::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&sample_count));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
|
|
|
|
bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
|
|
|
|
bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
|
|
|
|
bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
|
|
|
|
bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
|
|
|
|
bool sample_composition_time_offsets_present =
|
|
|
|
(flags & kSampleCompTimeOffsetsPresentMask) != 0;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
if (data_offset_present) {
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&data_offset));
|
2013-09-24 01:35:40 +00:00
|
|
|
} else {
|
2014-03-26 22:09:43 +00:00
|
|
|
// NOTE: If the data-offset is not present, then the data for this run
|
2014-03-19 00:09:50 +00:00
|
|
|
// starts immediately after the data of the previous run, or at the
|
|
|
|
// base-data-offset defined by the track fragment header if this is the
|
|
|
|
// first run in a track fragment. If the data-offset is present, it is
|
|
|
|
// relative to the base-data-offset established in the track fragment
|
|
|
|
// header.
|
2013-11-27 01:52:13 +00:00
|
|
|
NOTIMPLEMENTED();
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t first_sample_flags;
|
2013-11-27 01:52:13 +00:00
|
|
|
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
if (first_sample_flags_present)
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
|
|
|
|
|
|
|
|
if (sample_duration_present)
|
|
|
|
sample_durations.resize(sample_count);
|
|
|
|
if (sample_size_present)
|
|
|
|
sample_sizes.resize(sample_count);
|
|
|
|
if (sample_flags_present)
|
|
|
|
sample_flags.resize(sample_count);
|
|
|
|
if (sample_composition_time_offsets_present)
|
|
|
|
sample_composition_time_offsets.resize(sample_count);
|
|
|
|
} else {
|
|
|
|
if (first_sample_flags_present) {
|
|
|
|
first_sample_flags = sample_flags[0];
|
|
|
|
DCHECK(sample_flags.size() == 1);
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sample_duration_present)
|
|
|
|
DCHECK(sample_durations.size() == sample_count);
|
|
|
|
if (sample_size_present)
|
|
|
|
DCHECK(sample_sizes.size() == sample_count);
|
|
|
|
if (sample_flags_present)
|
|
|
|
DCHECK(sample_flags.size() == sample_count);
|
|
|
|
if (sample_composition_time_offsets_present)
|
|
|
|
DCHECK(sample_composition_time_offsets.size() == sample_count);
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < sample_count; ++i) {
|
2013-09-24 01:35:40 +00:00
|
|
|
if (sample_duration_present)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
|
2013-09-24 01:35:40 +00:00
|
|
|
if (sample_size_present)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
|
2013-09-24 01:35:40 +00:00
|
|
|
if (sample_flags_present)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
|
2013-09-24 01:35:40 +00:00
|
|
|
if (sample_composition_time_offsets_present)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->ReadWriteInt32(&sample_composition_time_offsets[i]));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
if (buffer->Reading()) {
|
|
|
|
if (first_sample_flags_present) {
|
|
|
|
if (sample_flags.size() == 0) {
|
|
|
|
sample_flags.push_back(first_sample_flags);
|
|
|
|
} else {
|
|
|
|
sample_flags[0] = first_sample_flags;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t TrackFragmentRun::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
version = 1; // Version 1 to support signed offset.
|
|
|
|
atom_size = kFullBoxSize + sizeof(sample_count);
|
|
|
|
if (flags & kDataOffsetPresentMask)
|
|
|
|
atom_size += sizeof(data_offset);
|
|
|
|
if (flags & kFirstSampleFlagsPresentMask)
|
2014-09-30 21:52:21 +00:00
|
|
|
atom_size += sizeof(uint32_t);
|
|
|
|
uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
|
|
|
|
(flags & kSampleSizePresentMask ? 1 : 0) +
|
|
|
|
(flags & kSampleFlagsPresentMask ? 1 : 0) +
|
|
|
|
(flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
|
|
|
|
atom_size += fields * sizeof(uint32_t) * sample_count;
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2014-03-24 21:09:58 +00:00
|
|
|
SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
|
|
|
|
SampleToGroup::~SampleToGroup() {}
|
|
|
|
FourCC SampleToGroup::BoxType() const { return FOURCC_SBGP; }
|
|
|
|
|
|
|
|
bool SampleToGroup::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&grouping_type));
|
|
|
|
if (version == 1)
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
|
|
|
|
|
|
|
|
if (grouping_type != FOURCC_SEIG) {
|
|
|
|
DCHECK(buffer->Reading());
|
|
|
|
DLOG(WARNING) << "Sample group '" << grouping_type << "' is not supported.";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t count = entries.size();
|
2014-03-24 21:09:58 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&count));
|
|
|
|
entries.resize(count);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-03-24 21:09:58 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
|
|
|
|
buffer->ReadWriteUInt32(&entries[i].group_description_index));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SampleToGroup::ComputeSize() {
|
2014-03-24 21:09:58 +00:00
|
|
|
// This box is optional. Skip it if it is not used.
|
|
|
|
atom_size = 0;
|
|
|
|
if (!entries.empty()) {
|
|
|
|
atom_size = kFullBoxSize + sizeof(grouping_type) +
|
|
|
|
(version == 1 ? sizeof(grouping_type_parameter) : 0) +
|
2014-09-30 21:52:21 +00:00
|
|
|
sizeof(uint32_t) + entries.size() * sizeof(entries[0]);
|
2014-03-24 21:09:58 +00:00
|
|
|
}
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
|
|
|
|
: is_encrypted(false), iv_size(0) {
|
|
|
|
}
|
|
|
|
CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
|
|
|
|
|
|
|
|
SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
|
|
|
|
SampleGroupDescription::~SampleGroupDescription() {}
|
|
|
|
FourCC SampleGroupDescription::BoxType() const { return FOURCC_SGPD; }
|
|
|
|
|
|
|
|
bool SampleGroupDescription::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&grouping_type));
|
|
|
|
|
|
|
|
if (grouping_type != FOURCC_SEIG) {
|
|
|
|
DCHECK(buffer->Reading());
|
|
|
|
DLOG(WARNING) << "Sample group '" << grouping_type << "' is not supported.";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-15 21:56:12 +00:00
|
|
|
const size_t kEntrySize = sizeof(uint32_t) + kCencKeyIdSize;
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t default_length = 0;
|
2014-03-24 21:09:58 +00:00
|
|
|
if (version == 1) {
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&default_length));
|
2014-05-07 21:57:39 +00:00
|
|
|
RCHECK(default_length == 0 || default_length >= kEntrySize);
|
2014-03-24 21:09:58 +00:00
|
|
|
} else {
|
|
|
|
default_length = kEntrySize;
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&default_length));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t count = entries.size();
|
2014-03-24 21:09:58 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&count));
|
|
|
|
entries.resize(count);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-03-24 21:09:58 +00:00
|
|
|
if (version == 1) {
|
|
|
|
if (buffer->Reading() && default_length == 0) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t description_length = 0;
|
2014-03-24 21:09:58 +00:00
|
|
|
RCHECK(buffer->ReadWriteUInt32(&description_length));
|
2014-05-07 21:57:39 +00:00
|
|
|
RCHECK(description_length >= kEntrySize);
|
2014-03-24 21:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 21:56:12 +00:00
|
|
|
if (!buffer->Reading()) {
|
|
|
|
if (entries[i].key_id.size() != kCencKeyIdSize) {
|
|
|
|
LOG(WARNING) << "CENC defines key id length of " << kCencKeyIdSize
|
|
|
|
<< " bytes; got " << entries[i].key_id.size()
|
|
|
|
<< ". Resized accordingly.";
|
|
|
|
entries[i].key_id.resize(kCencKeyIdSize);
|
|
|
|
}
|
|
|
|
}
|
2014-03-24 21:09:58 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t flag = entries[i].is_encrypted ? 1 : 0;
|
2014-03-24 21:09:58 +00:00
|
|
|
RCHECK(buffer->IgnoreBytes(2) && // reserved.
|
|
|
|
buffer->ReadWriteUInt8(&flag) &&
|
|
|
|
buffer->ReadWriteUInt8(&entries[i].iv_size) &&
|
2014-10-15 21:56:12 +00:00
|
|
|
buffer->ReadWriteVector(&entries[i].key_id, kCencKeyIdSize));
|
2014-03-24 21:09:58 +00:00
|
|
|
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
entries[i].is_encrypted = (flag != 0);
|
|
|
|
if (entries[i].is_encrypted) {
|
|
|
|
RCHECK(entries[i].iv_size == 8 || entries[i].iv_size == 16);
|
|
|
|
} else {
|
|
|
|
RCHECK(entries[i].iv_size == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SampleGroupDescription::ComputeSize() {
|
2014-05-07 21:57:39 +00:00
|
|
|
// Version 0 is obsoleted, so always generate version 1 box.
|
|
|
|
version = 1;
|
2014-03-24 21:09:58 +00:00
|
|
|
// This box is optional. Skip it if it is not used.
|
|
|
|
atom_size = 0;
|
|
|
|
if (!entries.empty()) {
|
2014-10-15 21:56:12 +00:00
|
|
|
const size_t kEntrySize = sizeof(uint32_t) + kCencKeyIdSize;
|
2014-03-24 21:09:58 +00:00
|
|
|
atom_size = kFullBoxSize + sizeof(grouping_type) +
|
2014-09-30 21:52:21 +00:00
|
|
|
(version == 1 ? sizeof(uint32_t) : 0) + sizeof(uint32_t) +
|
|
|
|
entries.size() * kEntrySize;
|
2014-03-24 21:09:58 +00:00
|
|
|
}
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
TrackFragment::TrackFragment() {}
|
|
|
|
TrackFragment::~TrackFragment() {}
|
|
|
|
FourCC TrackFragment::BoxType() const { return FOURCC_TRAF; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool TrackFragment::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) &&
|
|
|
|
buffer->PrepareChildren() &&
|
|
|
|
buffer->ReadWriteChild(&header) &&
|
2013-09-24 01:35:40 +00:00
|
|
|
// Media Source specific: 'tfdt' required
|
2013-11-27 01:52:13 +00:00
|
|
|
buffer->ReadWriteChild(&decode_time));
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
DCHECK(buffer->reader());
|
|
|
|
RCHECK(buffer->reader()->TryReadChildren(&runs));
|
2014-03-24 21:09:58 +00:00
|
|
|
|
2014-05-07 21:57:39 +00:00
|
|
|
// There could be multiple SampleGroupDescription and SampleToGroup boxes
|
|
|
|
// with different grouping types. For common encryption, the relevant
|
|
|
|
// grouping type is 'seig'. Continue reading until 'seig' is found, or
|
|
|
|
// until running out of child boxes.
|
2014-03-24 21:09:58 +00:00
|
|
|
while (sample_to_group.grouping_type != FOURCC_SEIG &&
|
|
|
|
buffer->reader()->ChildExist(&sample_to_group)) {
|
|
|
|
RCHECK(buffer->reader()->ReadChild(&sample_to_group));
|
|
|
|
}
|
|
|
|
while (sample_group_description.grouping_type != FOURCC_SEIG &&
|
|
|
|
buffer->reader()->ChildExist(&sample_group_description)) {
|
|
|
|
RCHECK(buffer->reader()->ReadChild(&sample_group_description));
|
|
|
|
}
|
2013-11-27 01:52:13 +00:00
|
|
|
} else {
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < runs.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(runs[i].ReadWrite(buffer));
|
2014-03-24 21:09:58 +00:00
|
|
|
RCHECK(buffer->TryReadWriteChild(&sample_to_group) &&
|
|
|
|
buffer->TryReadWriteChild(&sample_group_description));
|
2013-11-27 01:52:13 +00:00
|
|
|
}
|
|
|
|
return buffer->TryReadWriteChild(&auxiliary_size) &&
|
|
|
|
buffer->TryReadWriteChild(&auxiliary_offset);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t TrackFragment::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + header.ComputeSize() + decode_time.ComputeSize() +
|
2014-03-24 21:09:58 +00:00
|
|
|
sample_to_group.ComputeSize() +
|
|
|
|
sample_group_description.ComputeSize() +
|
2013-11-27 01:52:13 +00:00
|
|
|
auxiliary_size.ComputeSize() + auxiliary_offset.ComputeSize();
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < runs.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size += runs[i].ComputeSize();
|
|
|
|
return atom_size;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MovieFragment::MovieFragment() {}
|
|
|
|
MovieFragment::~MovieFragment() {}
|
|
|
|
FourCC MovieFragment::BoxType() const { return FOURCC_MOOF; }
|
|
|
|
|
2013-11-27 01:52:13 +00:00
|
|
|
bool MovieFragment::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWrite(buffer) &&
|
|
|
|
buffer->PrepareChildren() &&
|
|
|
|
buffer->ReadWriteChild(&header));
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
BoxReader* reader = buffer->reader();
|
|
|
|
DCHECK(reader);
|
|
|
|
RCHECK(reader->ReadChildren(&tracks) &&
|
|
|
|
reader->TryReadChildren(&pssh));
|
|
|
|
} else {
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < tracks.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(tracks[i].ReadWrite(buffer));
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < pssh.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(pssh[i].ReadWrite(buffer));
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t MovieFragment::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size = kBoxSize + header.ComputeSize();
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < tracks.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size += tracks[i].ComputeSize();
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < pssh.size(); ++i)
|
2013-11-27 01:52:13 +00:00
|
|
|
atom_size += pssh[i].ComputeSize();
|
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
SegmentIndex::SegmentIndex()
|
|
|
|
: reference_id(0),
|
|
|
|
timescale(0),
|
|
|
|
earliest_presentation_time(0),
|
|
|
|
first_offset(0) {}
|
|
|
|
SegmentIndex::~SegmentIndex() {}
|
|
|
|
FourCC SegmentIndex::BoxType() const { return FOURCC_SIDX; }
|
|
|
|
|
|
|
|
bool SegmentIndex::ReadWrite(BoxBuffer* buffer) {
|
|
|
|
RCHECK(FullBox::ReadWrite(buffer) &&
|
|
|
|
buffer->ReadWriteUInt32(&reference_id) &&
|
|
|
|
buffer->ReadWriteUInt32(×cale));
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
size_t num_bytes = (version == 1) ? sizeof(uint64_t) : sizeof(uint32_t);
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(
|
|
|
|
buffer->ReadWriteUInt64NBytes(&earliest_presentation_time, num_bytes) &&
|
|
|
|
buffer->ReadWriteUInt64NBytes(&first_offset, num_bytes));
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint16_t reference_count = references.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
RCHECK(buffer->IgnoreBytes(2) && // reserved.
|
|
|
|
buffer->ReadWriteUInt16(&reference_count));
|
|
|
|
references.resize(reference_count);
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t reference_type_size;
|
|
|
|
uint32_t sap;
|
|
|
|
for (uint32_t i = 0; i < reference_count; ++i) {
|
2013-11-27 01:52:13 +00:00
|
|
|
if (!buffer->Reading()) {
|
|
|
|
reference_type_size = references[i].referenced_size;
|
|
|
|
if (references[i].reference_type)
|
|
|
|
reference_type_size |= (1 << 31);
|
|
|
|
sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
|
|
|
|
if (references[i].starts_with_sap)
|
|
|
|
sap |= (1 << 31);
|
|
|
|
}
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
|
|
|
|
buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
|
|
|
|
buffer->ReadWriteUInt32(&sap));
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
references[i].reference_type = (reference_type_size >> 31) ? true : false;
|
|
|
|
references[i].referenced_size = reference_type_size & ~(1 << 31);
|
|
|
|
references[i].starts_with_sap = (sap >> 31) ? true : false;
|
|
|
|
references[i].sap_type =
|
|
|
|
static_cast<SegmentReference::SAPType>((sap >> 28) & 0x07);
|
|
|
|
references[i].sap_delta_time = sap & ~(0xF << 28);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t SegmentIndex::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
|
|
|
|
atom_size = kFullBoxSize + sizeof(reference_id) + sizeof(timescale) +
|
2014-09-30 21:52:21 +00:00
|
|
|
sizeof(uint32_t) * (1 + version) * 2 + 2 * sizeof(uint16_t) +
|
|
|
|
3 * sizeof(uint32_t) * references.size();
|
2013-11-27 01:52:13 +00:00
|
|
|
return atom_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaData::MediaData() : data_size(0) {}
|
|
|
|
MediaData::~MediaData() {}
|
|
|
|
FourCC MediaData::BoxType() const { return FOURCC_MDAT; }
|
|
|
|
|
|
|
|
void MediaData::Write(BufferWriter* buffer) {
|
|
|
|
buffer->AppendInt(ComputeSize());
|
2014-09-30 21:52:21 +00:00
|
|
|
buffer->AppendInt(static_cast<uint32_t>(BoxType()));
|
2013-11-27 01:52:13 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t MediaData::ComputeSize() {
|
2013-11-27 01:52:13 +00:00
|
|
|
return kBoxSize + data_size;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|