5 #include "packager/media/formats/mp4/box_definitions.h"
9 #include "packager/base/logging.h"
10 #include "packager/media/base/bit_reader.h"
11 #include "packager/media/base/macros.h"
12 #include "packager/media/formats/mp4/box_buffer.h"
13 #include "packager/media/formats/mp4/rcheck.h"
16 const uint32_t kFourCCSize = 4;
19 const uint32_t kCencKeyIdSize = 16;
22 const uint8_t kUnityMatrix[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
24 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0};
27 const char kVideoHandlerName[] =
"VideoHandler";
28 const char kAudioHandlerName[] =
"SoundHandler";
29 const char kTextHandlerName[] =
"TextHandler";
32 const uint32_t kVideoResolution = 0x00480000;
33 const uint16_t kVideoFrameCount = 1;
34 const uint16_t kVideoDepth = 0x0018;
36 const uint32_t kCompressorNameSize = 32u;
37 const char kAvcCompressorName[] =
"\012AVC Coding";
38 const char kHevcCompressorName[] =
"\013HEVC Coding";
39 const char kVpcCompressorName[] =
"\012VPC Coding";
43 const int kCueSourceIdNotSet = -1;
47 bool IsIvSizeValid(
size_t iv_size) {
48 return iv_size == 8 || iv_size == 16;
65 const uint8_t kDdtsExtraData[] = {0xe4, 0x7c, 0, 4, 0, 0x0f, 0};
68 const uint32_t kID3v2HeaderSize = 10;
69 const char kID3v2Identifier[] =
"ID3";
70 const uint16_t kID3v2Version = 0x0400;
73 bool IsFitIn32Bits(uint64_t a) {
74 return a <= std::numeric_limits<uint32_t>::max();
77 bool IsFitIn32Bits(int64_t a) {
78 return a <= std::numeric_limits<int32_t>::max() &&
79 a >= std::numeric_limits<int32_t>::min();
82 template <
typename T1,
typename T2>
83 bool IsFitIn32Bits(T1 a1, T2 a2) {
84 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
87 template <
typename T1,
typename T2,
typename T3>
88 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
89 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
94 namespace edash_packager {
100 TrackType FourCCToTrackType(FourCC fourcc) {
113 FourCC TrackTypeToFourCC(TrackType track_type) {
114 switch (track_type) {
128 FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
129 FileType::~FileType() {}
132 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
134 buffer->ReadWriteFourCC(&major_brand) &&
135 buffer->ReadWriteUInt32(&minor_version));
138 RCHECK(buffer->
BytesLeft() %
sizeof(FourCC) == 0);
139 num_brands = buffer->
BytesLeft() /
sizeof(FourCC);
140 compatible_brands.resize(num_brands);
142 num_brands = compatible_brands.size();
144 for (
size_t i = 0; i < num_brands; ++i)
145 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
149 uint32_t FileType::ComputeSizeInternal() {
150 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
151 kFourCCSize * compatible_brands.size();
156 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
157 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
160 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
161 if (!buffer->
Reading() && !raw_box.empty()) {
163 buffer->
writer()->AppendVector(raw_box);
167 uint32_t size = data.size();
169 buffer->ReadWriteVector(&system_id, 16) &&
170 buffer->ReadWriteUInt32(&size) &&
171 buffer->ReadWriteVector(&data, size));
176 DCHECK(raw_box.empty());
177 BoxReader* reader = buffer->
reader();
179 raw_box.assign(reader->data(), reader->data() + reader->size());
184 uint32_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
185 if (!raw_box.empty()) {
186 return raw_box.size();
188 return HeaderSize() + system_id.size() +
sizeof(uint32_t) + data.size();
192 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
193 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
196 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
201 uint32_t count = offsets.size();
202 RCHECK(buffer->ReadWriteUInt32(&count));
203 offsets.resize(count);
205 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
206 for (uint32_t i = 0; i < count; ++i)
211 uint32_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
213 if (offsets.size() == 0)
215 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
216 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
219 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
220 : default_sample_info_size(0), sample_count(0) {}
221 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
224 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
229 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
230 buffer->ReadWriteUInt32(&sample_count));
231 if (default_sample_info_size == 0)
232 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
236 uint32_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
238 if (sample_count == 0)
240 return HeaderSize() +
sizeof(default_sample_info_size) +
241 sizeof(sample_count) +
242 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
245 SampleEncryptionEntry::SampleEncryptionEntry() {}
246 SampleEncryptionEntry::~SampleEncryptionEntry() {}
251 DCHECK(IsIvSizeValid(iv_size));
254 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
256 if (!has_subsamples) {
261 uint16_t subsample_count = subsamples.size();
262 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
263 RCHECK(subsample_count > 0);
264 subsamples.resize(subsample_count);
265 for (
auto& subsample : subsamples) {
266 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
267 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
275 DCHECK(IsIvSizeValid(iv_size));
278 initialization_vector.resize(iv_size);
279 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
281 if (!has_subsamples) {
286 uint16_t subsample_count;
287 RCHECK(reader->Read2(&subsample_count));
288 RCHECK(subsample_count > 0);
289 subsamples.resize(subsample_count);
290 for (
auto& subsample : subsamples) {
291 RCHECK(reader->Read2(&subsample.clear_bytes) &&
292 reader->Read4(&subsample.cipher_bytes));
298 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
299 const uint16_t subsample_count = subsamples.size();
300 return initialization_vector.size() +
301 (subsample_count > 0 ? (
sizeof(subsample_count) +
302 subsample_entry_size * subsample_count)
308 for (uint32_t i = 0; i < subsamples.size(); ++i)
309 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
313 SampleEncryption::SampleEncryption() : iv_size(0) {}
314 SampleEncryption::~SampleEncryption() {}
317 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
322 if (buffer->
Reading() && iv_size == 0) {
328 if (!IsIvSizeValid(iv_size)) {
329 LOG(ERROR) <<
"IV_size can only be 8 or 16, but seeing " << iv_size;
333 uint32_t sample_count = sample_encryption_entries.size();
334 RCHECK(buffer->ReadWriteUInt32(&sample_count));
336 sample_encryption_entries.resize(sample_count);
337 for (
auto& sample_encryption_entry : sample_encryption_entries) {
338 RCHECK(sample_encryption_entry.ReadWrite(
339 iv_size, flags & kUseSubsampleEncryption, buffer));
344 uint32_t SampleEncryption::ComputeSizeInternal() {
345 const uint32_t sample_count = sample_encryption_entries.size();
346 if (sample_count == 0) {
351 DCHECK(IsIvSizeValid(iv_size));
353 if (flags & kUseSubsampleEncryption) {
354 for (
const SampleEncryptionEntry& sample_encryption_entry :
355 sample_encryption_entries) {
356 box_size += sample_encryption_entry.ComputeSize();
359 box_size += sample_count * iv_size;
366 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
367 DCHECK(IsIvSizeValid(iv_size));
371 uint32_t sample_count = 0;
372 RCHECK(reader.Read4(&sample_count));
374 sample_encryption_entries->resize(sample_count);
375 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
376 RCHECK(sample_encryption_entry.ParseFromBuffer(
377 iv_size, flags & kUseSubsampleEncryption, &reader));
382 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
383 OriginalFormat::~OriginalFormat() {}
386 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
390 uint32_t OriginalFormat::ComputeSizeInternal() {
394 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
395 SchemeType::~SchemeType() {}
398 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
400 buffer->ReadWriteFourCC(&type) &&
401 buffer->ReadWriteUInt32(&version));
405 uint32_t SchemeType::ComputeSizeInternal() {
406 return HeaderSize() + kFourCCSize +
sizeof(version);
409 TrackEncryption::TrackEncryption()
410 : is_encrypted(false), default_iv_size(0), default_kid(16, 0) {}
411 TrackEncryption::~TrackEncryption() {}
414 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
416 if (default_kid.size() != kCencKeyIdSize) {
417 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
418 <<
" bytes; got " << default_kid.size()
419 <<
". Resized accordingly.";
420 default_kid.resize(kCencKeyIdSize);
424 uint8_t flag = is_encrypted ? 1 : 0;
427 buffer->ReadWriteUInt8(&flag) &&
428 buffer->ReadWriteUInt8(&default_iv_size) &&
429 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
431 is_encrypted = (flag != 0);
433 RCHECK(default_iv_size == 8 || default_iv_size == 16);
435 RCHECK(default_iv_size == 0);
441 uint32_t TrackEncryption::ComputeSizeInternal() {
442 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize;
445 SchemeInfo::SchemeInfo() {}
446 SchemeInfo::~SchemeInfo() {}
449 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
455 uint32_t SchemeInfo::ComputeSizeInternal() {
459 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
460 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
463 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
468 if (type.type == FOURCC_CENC)
477 uint32_t ProtectionSchemeInfo::ComputeSizeInternal() {
479 if (format.format == FOURCC_NULL)
485 MovieHeader::MovieHeader()
487 modification_time(0),
493 MovieHeader::~MovieHeader() {}
496 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
499 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
502 buffer->ReadWriteUInt32(×cale) &&
505 std::vector<uint8_t> matrix(kUnityMatrix,
506 kUnityMatrix + arraysize(kUnityMatrix));
507 RCHECK(buffer->ReadWriteInt32(&rate) &&
508 buffer->ReadWriteInt16(&volume) &&
510 buffer->ReadWriteVector(&matrix, matrix.size()) &&
512 buffer->ReadWriteUInt32(&next_track_id));
516 uint32_t MovieHeader::ComputeSizeInternal() {
517 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
518 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
519 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
520 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
524 TrackHeader::TrackHeader()
526 modification_time(0),
534 flags = kTrackEnabled | kTrackInMovie;
536 TrackHeader::~TrackHeader() {}
539 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
542 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
545 buffer->ReadWriteUInt32(&track_id) &&
552 volume = (width != 0 && height != 0) ? 0 : 0x100;
554 std::vector<uint8_t> matrix(kUnityMatrix,
555 kUnityMatrix + arraysize(kUnityMatrix));
557 buffer->ReadWriteInt16(&layer) &&
558 buffer->ReadWriteInt16(&alternate_group) &&
559 buffer->ReadWriteInt16(&volume) &&
561 buffer->ReadWriteVector(&matrix, matrix.size()) &&
562 buffer->ReadWriteUInt32(&width) &&
563 buffer->ReadWriteUInt32(&height));
567 uint32_t TrackHeader::ComputeSizeInternal() {
568 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
570 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
571 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
572 sizeof(height) +
sizeof(kUnityMatrix) + 14;
575 SampleDescription::SampleDescription() : type(kInvalid) {}
576 SampleDescription::~SampleDescription() {}
579 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
583 count = video_entries.size();
586 count = audio_entries.size();
589 count = wvtt_entries.size();
592 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
593 <<
" is not handled. Skipping.";
596 buffer->ReadWriteUInt32(&count));
599 BoxReader* reader = buffer->
reader();
601 video_entries.clear();
602 audio_entries.clear();
605 if (type == kVideo) {
606 RCHECK(reader->ReadAllChildren(&video_entries));
607 RCHECK(video_entries.size() == count);
608 }
else if (type == kAudio) {
609 RCHECK(reader->ReadAllChildren(&audio_entries));
610 RCHECK(audio_entries.size() == count);
611 }
else if (type == kText) {
612 RCHECK(reader->ReadAllChildren(&wvtt_entries));
613 RCHECK(wvtt_entries.size() == count);
616 DCHECK_LT(0u, count);
617 if (type == kVideo) {
618 for (uint32_t i = 0; i < count; ++i)
620 }
else if (type == kAudio) {
621 for (uint32_t i = 0; i < count; ++i)
623 }
else if (type == kText) {
624 for (uint32_t i = 0; i < count; ++i)
633 uint32_t SampleDescription::ComputeSizeInternal() {
634 uint32_t box_size =
HeaderSize() +
sizeof(uint32_t);
635 if (type == kVideo) {
636 for (uint32_t i = 0; i < video_entries.size(); ++i)
638 }
else if (type == kAudio) {
639 for (uint32_t i = 0; i < audio_entries.size(); ++i)
645 DecodingTimeToSample::DecodingTimeToSample() {}
646 DecodingTimeToSample::~DecodingTimeToSample() {}
649 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
650 uint32_t count = decoding_time.size();
652 buffer->ReadWriteUInt32(&count));
654 decoding_time.resize(count);
655 for (uint32_t i = 0; i < count; ++i) {
656 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
657 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
662 uint32_t DecodingTimeToSample::ComputeSizeInternal() {
664 sizeof(DecodingTime) * decoding_time.size();
667 CompositionTimeToSample::CompositionTimeToSample() {}
668 CompositionTimeToSample::~CompositionTimeToSample() {}
671 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
672 uint32_t count = composition_offset.size();
678 for (uint32_t i = 0; i < count; ++i) {
679 if (composition_offset[i].sample_offset < 0) {
687 buffer->ReadWriteUInt32(&count));
689 composition_offset.resize(count);
690 for (uint32_t i = 0; i < count; ++i) {
691 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
694 uint32_t sample_offset = composition_offset[i].sample_offset;
695 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
696 composition_offset[i].sample_offset = sample_offset;
698 int32_t sample_offset = composition_offset[i].sample_offset;
699 RCHECK(buffer->ReadWriteInt32(&sample_offset));
700 composition_offset[i].sample_offset = sample_offset;
706 uint32_t CompositionTimeToSample::ComputeSizeInternal() {
708 if (composition_offset.empty())
713 const uint32_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
715 kCompositionOffsetSize * composition_offset.size();
718 SampleToChunk::SampleToChunk() {}
719 SampleToChunk::~SampleToChunk() {}
722 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
723 uint32_t count = chunk_info.size();
725 buffer->ReadWriteUInt32(&count));
727 chunk_info.resize(count);
728 for (uint32_t i = 0; i < count; ++i) {
729 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
730 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
731 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
733 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
734 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
739 uint32_t SampleToChunk::ComputeSizeInternal() {
741 sizeof(ChunkInfo) * chunk_info.size();
744 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
745 SampleSize::~SampleSize() {}
748 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
750 buffer->ReadWriteUInt32(&sample_size) &&
751 buffer->ReadWriteUInt32(&sample_count));
753 if (sample_size == 0) {
755 sizes.resize(sample_count);
757 DCHECK(sample_count == sizes.size());
758 for (uint32_t i = 0; i < sample_count; ++i)
759 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
764 uint32_t SampleSize::ComputeSizeInternal() {
765 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
766 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
769 CompactSampleSize::CompactSampleSize() : field_size(0) {}
770 CompactSampleSize::~CompactSampleSize() {}
773 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
774 uint32_t sample_count = sizes.size();
777 buffer->ReadWriteUInt8(&field_size) &&
778 buffer->ReadWriteUInt32(&sample_count));
781 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
782 switch (field_size) {
784 for (uint32_t i = 0; i < sample_count; i += 2) {
787 RCHECK(buffer->ReadWriteUInt8(&size));
788 sizes[i] = size >> 4;
789 sizes[i + 1] = size & 0x0F;
791 DCHECK_LT(sizes[i], 16u);
792 DCHECK_LT(sizes[i + 1], 16u);
793 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
794 RCHECK(buffer->ReadWriteUInt8(&size));
799 for (uint32_t i = 0; i < sample_count; ++i) {
800 uint8_t size = sizes[i];
801 RCHECK(buffer->ReadWriteUInt8(&size));
806 for (uint32_t i = 0; i < sample_count; ++i) {
807 uint16_t size = sizes[i];
808 RCHECK(buffer->ReadWriteUInt16(&size));
815 sizes.resize(sample_count);
819 uint32_t CompactSampleSize::ComputeSizeInternal() {
820 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
821 (field_size * sizes.size() + 7) / 8;
824 ChunkOffset::ChunkOffset() {}
825 ChunkOffset::~ChunkOffset() {}
828 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
829 uint32_t count = offsets.size();
831 buffer->ReadWriteUInt32(&count));
833 offsets.resize(count);
834 for (uint32_t i = 0; i < count; ++i)
839 uint32_t ChunkOffset::ComputeSizeInternal() {
840 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
843 ChunkLargeOffset::ChunkLargeOffset() {}
844 ChunkLargeOffset::~ChunkLargeOffset() {}
847 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
848 uint32_t count = offsets.size();
852 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
854 stco.offsets.swap(offsets);
857 stco.offsets.swap(offsets);
863 buffer->ReadWriteUInt32(&count));
865 offsets.resize(count);
866 for (uint32_t i = 0; i < count; ++i)
867 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
871 uint32_t ChunkLargeOffset::ComputeSizeInternal() {
872 uint32_t count = offsets.size();
873 int use_large_offset =
874 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
876 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
879 SyncSample::SyncSample() {}
880 SyncSample::~SyncSample() {}
883 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
884 uint32_t count = sample_number.size();
886 buffer->ReadWriteUInt32(&count));
888 sample_number.resize(count);
889 for (uint32_t i = 0; i < count; ++i)
890 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
894 uint32_t SyncSample::ComputeSizeInternal() {
896 if (sample_number.empty())
899 sizeof(uint32_t) * sample_number.size();
902 SampleTable::SampleTable() {}
903 SampleTable::~SampleTable() {}
906 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
922 CompactSampleSize compact_sample_size;
923 RCHECK(reader->
ReadChild(&compact_sample_size));
924 sample_size.sample_size = 0;
925 sample_size.sample_count = compact_sample_size.sizes.size();
926 sample_size.sizes.swap(compact_sample_size.sizes);
930 if (reader->
ChildExist(&chunk_large_offset)) {
931 RCHECK(reader->
ReadChild(&chunk_large_offset));
933 ChunkOffset chunk_offset;
934 RCHECK(reader->
ReadChild(&chunk_offset));
935 chunk_large_offset.offsets.swap(chunk_offset.offsets);
945 uint32_t SampleTable::ComputeSizeInternal() {
953 EditList::EditList() {}
954 EditList::~EditList() {}
957 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
958 uint32_t count = edits.size();
962 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
963 for (uint32_t i = 0; i < count; ++i) {
966 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
967 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
968 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
973 uint32_t EditList::ComputeSizeInternal() {
979 for (uint32_t i = 0; i < edits.size(); ++i) {
980 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
986 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
994 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
1000 uint32_t Edit::ComputeSizeInternal() {
1002 if (list.edits.empty())
1007 HandlerReference::HandlerReference() : handler_type(FOURCC_NULL) {}
1008 HandlerReference::~HandlerReference() {}
1011 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1012 std::vector<uint8_t> handler_name;
1014 switch (handler_type) {
1016 handler_name.assign(kVideoHandlerName,
1017 kVideoHandlerName + arraysize(kVideoHandlerName));
1020 handler_name.assign(kAudioHandlerName,
1021 kAudioHandlerName + arraysize(kAudioHandlerName));
1024 handler_name.assign(kTextHandlerName,
1025 kTextHandlerName + arraysize(kTextHandlerName));
1036 buffer->ReadWriteFourCC(&handler_type));
1039 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1044 uint32_t HandlerReference::ComputeSizeInternal() {
1045 uint32_t box_size =
HeaderSize() + kFourCCSize + 16;
1046 switch (handler_type) {
1048 box_size +=
sizeof(kVideoHandlerName);
1051 box_size +=
sizeof(kAudioHandlerName);
1054 box_size +=
sizeof(kTextHandlerName);
1064 bool Language::ReadWrite(BoxBuffer* buffer) {
1065 if (buffer->Reading()) {
1068 std::vector<uint8_t> temp;
1069 RCHECK(buffer->ReadWriteVector(&temp, 2));
1071 BitReader bit_reader(&temp[0], 2);
1072 bit_reader.SkipBits(1);
1074 for (
int i = 0; i < 3; ++i) {
1075 CHECK(bit_reader.ReadBits(5, &language[i]));
1076 language[i] += 0x60;
1078 code.assign(language, 3);
1081 const char kUndefinedLanguage[] =
"und";
1083 code = kUndefinedLanguage;
1084 DCHECK_EQ(code.size(), 3u);
1088 for (
int i = 0; i < 3; ++i)
1089 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1090 RCHECK(buffer->ReadWriteUInt16(&lang));
1095 uint32_t Language::ComputeSize()
const {
1100 bool PrivFrame::ReadWrite(BoxBuffer* buffer) {
1101 FourCC fourcc = FOURCC_PRIV;
1102 RCHECK(buffer->ReadWriteFourCC(&fourcc));
1103 if (fourcc != FOURCC_PRIV) {
1104 VLOG(1) <<
"Skip unrecognized id3 frame during read: "
1105 << FourCCToString(fourcc);
1109 uint32_t frame_size = owner.size() + 1 + value.size();
1113 DCHECK_LT(frame_size, 0x7Fu);
1115 RCHECK(buffer->ReadWriteUInt32(&frame_size) &&
1116 buffer->ReadWriteUInt16(&flags));
1118 if (buffer->Reading()) {
1120 RCHECK(buffer->ReadWriteString(&str, frame_size));
1122 size_t pos = str.find(
'\0');
1123 RCHECK(pos < str.size());
1124 owner = str.substr(0, pos);
1125 value = str.substr(pos + 1);
1128 RCHECK(buffer->ReadWriteString(&owner, owner.size()) &&
1129 buffer->ReadWriteUInt8(&byte) &&
1130 buffer->ReadWriteString(&value, value.size()));
1135 uint32_t PrivFrame::ComputeSize()
const {
1136 if (owner.empty() && value.empty())
1138 const uint32_t kFourCCSize = 4;
1139 return kFourCCSize +
sizeof(uint32_t) +
sizeof(uint16_t) + owner.size() + 1 +
1148 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1150 language.ReadWrite(buffer));
1153 std::string id3v2_identifier = kID3v2Identifier;
1154 uint16_t version = kID3v2Version;
1160 DCHECK_LT(data_size, 0x7Fu);
1162 RCHECK(buffer->
ReadWriteString(&id3v2_identifier, id3v2_identifier.size()) &&
1163 buffer->ReadWriteUInt16(&version) &&
1164 buffer->ReadWriteUInt8(&flags) &&
1165 buffer->ReadWriteUInt32(&data_size));
1171 uint32_t ID3v2::ComputeSizeInternal() {
1174 return private_frame_size == 0 ? 0 :
HeaderSize() + language.ComputeSize() +
1179 Metadata::Metadata() {}
1180 Metadata::~Metadata() {}
1186 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1194 uint32_t Metadata::ComputeSizeInternal() {
1197 return id3v2_size == 0 ? 0
1201 CodecConfigurationRecord::CodecConfigurationRecord() : box_type(FOURCC_NULL) {}
1202 CodecConfigurationRecord::~CodecConfigurationRecord() {}
1209 bool CodecConfigurationRecord::ReadWriteInternal(
BoxBuffer* buffer) {
1212 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1214 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1219 uint32_t CodecConfigurationRecord::ComputeSizeInternal() {
1225 PixelAspectRatio::PixelAspectRatio() : h_spacing(0), v_spacing(0) {}
1226 PixelAspectRatio::~PixelAspectRatio() {}
1229 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1231 buffer->ReadWriteUInt32(&h_spacing) &&
1232 buffer->ReadWriteUInt32(&v_spacing));
1236 uint32_t PixelAspectRatio::ComputeSizeInternal() {
1238 if (h_spacing == 0 && v_spacing == 0)
1241 DCHECK(h_spacing != 0 && v_spacing != 0);
1242 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1245 VideoSampleEntry::VideoSampleEntry()
1246 : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
1248 VideoSampleEntry::~VideoSampleEntry() {}
1250 if (format == FOURCC_NULL) {
1251 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
1252 <<
"handler type recovered in its Media ancestor.";
1257 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1258 std::vector<uint8_t> compressor_name;
1260 DCHECK(buffer->
reader());
1261 format = buffer->
reader()->type();
1265 const FourCC actual_format = GetActualFormat();
1266 switch (actual_format) {
1268 compressor_name.assign(
1270 kAvcCompressorName + arraysize(kAvcCompressorName));
1274 compressor_name.assign(
1275 kHevcCompressorName,
1276 kHevcCompressorName + arraysize(kHevcCompressorName));
1281 compressor_name.assign(
1283 kVpcCompressorName + arraysize(kVpcCompressorName));
1286 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1289 compressor_name.resize(kCompressorNameSize);
1292 uint32_t video_resolution = kVideoResolution;
1293 uint16_t video_frame_count = kVideoFrameCount;
1294 uint16_t video_depth = kVideoDepth;
1295 int16_t predefined = -1;
1297 buffer->ReadWriteUInt16(&data_reference_index) &&
1299 buffer->ReadWriteUInt16(&width) &&
1300 buffer->ReadWriteUInt16(&height) &&
1301 buffer->ReadWriteUInt32(&video_resolution) &&
1302 buffer->ReadWriteUInt32(&video_resolution) &&
1304 buffer->ReadWriteUInt16(&video_frame_count) &&
1305 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1306 buffer->ReadWriteUInt16(&video_depth) &&
1307 buffer->ReadWriteInt16(&predefined));
1311 if (format == FOURCC_ENCV) {
1315 while (sinf.type.type != FOURCC_CENC) {
1324 const FourCC actual_format = GetActualFormat();
1325 switch (actual_format) {
1327 codec_config_record.box_type = FOURCC_AVCC;
1331 codec_config_record.box_type = FOURCC_HVCC;
1336 codec_config_record.box_type = FOURCC_VPCC;
1339 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1347 uint32_t VideoSampleEntry::ComputeSizeInternal() {
1348 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1349 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1350 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1352 codec_config_record.
ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1356 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1357 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1360 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1363 std::vector<uint8_t> data;
1364 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1365 RCHECK(es_descriptor.Parse(data));
1366 if (es_descriptor.
IsAAC()) {
1367 RCHECK(aac_audio_specific_config.
Parse(
1368 es_descriptor.decoder_specific_info()));
1371 DCHECK(buffer->
writer());
1372 es_descriptor.Write(buffer->
writer());
1377 uint32_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1379 if (es_descriptor.object_type() == kForbidden)
1381 return HeaderSize() + es_descriptor.ComputeSize();
1384 DTSSpecific::DTSSpecific()
1385 : sampling_frequency(0),
1388 pcm_sample_depth(0) {}
1389 DTSSpecific::~DTSSpecific() {}
1392 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1394 buffer->ReadWriteUInt32(&sampling_frequency) &&
1395 buffer->ReadWriteUInt32(&max_bitrate) &&
1396 buffer->ReadWriteUInt32(&avg_bitrate) &&
1397 buffer->ReadWriteUInt8(&pcm_sample_depth));
1400 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
BytesLeft()));
1402 if (extra_data.empty()) {
1403 extra_data.assign(kDdtsExtraData,
1404 kDdtsExtraData +
sizeof(kDdtsExtraData));
1406 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1411 uint32_t DTSSpecific::ComputeSizeInternal() {
1413 if (sampling_frequency == 0)
1415 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1416 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1417 sizeof(kDdtsExtraData);
1420 AC3Specific::AC3Specific() {}
1421 AC3Specific::~AC3Specific() {}
1425 bool AC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1427 buffer->ReadWriteVector(
1432 uint32_t AC3Specific::ComputeSizeInternal() {
1439 EC3Specific::EC3Specific() : number_independent_substreams(0) {}
1440 EC3Specific::~EC3Specific() {}
1444 bool EC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1447 RCHECK(buffer->ReadWriteVector(&data, size));
1452 RCHECK(bit_reader.SkipBits(13) &&
1453 bit_reader.ReadBits(3, &number_independent_substreams));
1457 ++number_independent_substreams;
1459 for (
size_t i = 0; i < number_independent_substreams; ++i) {
1460 RCHECK(bit_reader.ReadBits(2, &substream.sample_rate_code));
1461 RCHECK(bit_reader.ReadBits(5, &substream.bit_stream_identification));
1462 RCHECK(bit_reader.SkipBits(1));
1463 RCHECK(bit_reader.ReadBits(1, &substream.audio_service));
1464 RCHECK(bit_reader.ReadBits(3, &substream.bit_stream_mode));
1465 RCHECK(bit_reader.ReadBits(3, &substream.audio_coding_mode));
1466 RCHECK(bit_reader.ReadBits(1, &substream.lfe_channel_on));
1467 RCHECK(bit_reader.SkipBits(3));
1468 RCHECK(bit_reader.ReadBits(4, &substream.number_dependent_substreams));
1469 if (substream.number_dependent_substreams > 0) {
1470 RCHECK(bit_reader.ReadBits(9, &substream.channel_location));
1472 RCHECK(bit_reader.SkipBits(1));
1474 independent_substreams.push_back(substream);
1480 uint32_t EC3Specific::ComputeSizeInternal() {
1487 AudioSampleEntry::AudioSampleEntry()
1488 : format(FOURCC_NULL),
1489 data_reference_index(1),
1494 AudioSampleEntry::~AudioSampleEntry() {}
1497 if (format == FOURCC_NULL) {
1498 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1499 <<
"handler type recovered in its Media ancestor.";
1504 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1506 DCHECK(buffer->
reader());
1507 format = buffer->
reader()->type();
1515 buffer->ReadWriteUInt16(&data_reference_index) &&
1517 buffer->ReadWriteUInt16(&channelcount) &&
1518 buffer->ReadWriteUInt16(&samplesize) &&
1520 buffer->ReadWriteUInt32(&samplerate));
1525 if (format == FOURCC_ENCA) {
1529 while (sinf.type.type != FOURCC_CENC) {
1545 uint32_t AudioSampleEntry::ComputeSizeInternal() {
1546 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1547 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1554 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1555 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1561 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1568 uint32_t WebVTTConfigurationBox::ComputeSizeInternal() {
1572 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1573 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1579 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1583 : source_label.size());
1586 uint32_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1587 if (source_label.empty())
1592 WVTTSampleEntry::WVTTSampleEntry() {}
1593 WVTTSampleEntry::~WVTTSampleEntry() {}
1599 bool WVTTSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1603 buffer->ReadWriteUInt16(&data_reference_index) &&
1610 uint32_t WVTTSampleEntry::ComputeSizeInternal() {
1612 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1616 MediaHeader::MediaHeader()
1617 : creation_time(0), modification_time(0), timescale(0), duration(0) {}
1618 MediaHeader::~MediaHeader() {}
1621 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1624 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1627 buffer->ReadWriteUInt32(×cale) &&
1629 language.ReadWrite(buffer) &&
1634 uint32_t MediaHeader::ComputeSizeInternal() {
1635 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1637 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1641 VideoMediaHeader::VideoMediaHeader()
1642 : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1643 const uint32_t kVideoMediaHeaderFlags = 1;
1644 flags = kVideoMediaHeaderFlags;
1646 VideoMediaHeader::~VideoMediaHeader() {}
1648 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1650 buffer->ReadWriteUInt16(&graphicsmode) &&
1651 buffer->ReadWriteUInt16(&opcolor_red) &&
1652 buffer->ReadWriteUInt16(&opcolor_green) &&
1653 buffer->ReadWriteUInt16(&opcolor_blue));
1657 uint32_t VideoMediaHeader::ComputeSizeInternal() {
1658 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1659 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1662 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1663 SoundMediaHeader::~SoundMediaHeader() {}
1665 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1667 buffer->ReadWriteUInt16(&balance) &&
1672 uint32_t SoundMediaHeader::ComputeSizeInternal() {
1673 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1676 SubtitleMediaHeader::SubtitleMediaHeader() {}
1677 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1681 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1685 uint32_t SubtitleMediaHeader::ComputeSizeInternal() {
1689 DataEntryUrl::DataEntryUrl() {
1690 const uint32_t kDataEntryUrlFlags = 1;
1691 flags = kDataEntryUrlFlags;
1693 DataEntryUrl::~DataEntryUrl() {}
1695 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
1698 RCHECK(buffer->ReadWriteVector(&location, buffer->
BytesLeft()));
1700 RCHECK(buffer->ReadWriteVector(&location, location.size()));
1705 uint32_t DataEntryUrl::ComputeSizeInternal() {
1709 DataReference::DataReference() {
1711 data_entry.resize(1);
1713 DataReference::~DataReference() {}
1715 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
1716 uint32_t entry_count = data_entry.size();
1718 buffer->ReadWriteUInt32(&entry_count));
1719 data_entry.resize(entry_count);
1721 for (uint32_t i = 0; i < entry_count; ++i)
1726 uint32_t DataReference::ComputeSizeInternal() {
1727 uint32_t count = data_entry.size();
1728 uint32_t box_size =
HeaderSize() +
sizeof(count);
1729 for (uint32_t i = 0; i < count; ++i)
1734 DataInformation::DataInformation() {}
1735 DataInformation::~DataInformation() {}
1738 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1744 uint32_t DataInformation::ComputeSizeInternal() {
1748 MediaInformation::MediaInformation() {}
1749 MediaInformation::~MediaInformation() {}
1752 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1757 switch (sample_table.description.type) {
1774 uint32_t MediaInformation::ComputeSizeInternal() {
1777 switch (sample_table.description.type) {
1797 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
1809 information.sample_table.description.type =
1810 FourCCToTrackType(handler.handler_type);
1812 handler.handler_type =
1813 TrackTypeToFourCC(information.sample_table.description.type);
1814 RCHECK(handler.handler_type != FOURCC_NULL);
1821 uint32_t Media::ComputeSizeInternal() {
1822 handler.handler_type =
1823 TrackTypeToFourCC(information.sample_table.description.type);
1832 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
1842 uint32_t Track::ComputeSizeInternal() {
1847 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
1848 MovieExtendsHeader::~MovieExtendsHeader() {}
1851 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1853 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1858 uint32_t MovieExtendsHeader::ComputeSizeInternal() {
1860 if (fragment_duration == 0)
1862 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
1863 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1866 TrackExtends::TrackExtends()
1868 default_sample_description_index(0),
1869 default_sample_duration(0),
1870 default_sample_size(0),
1871 default_sample_flags(0) {}
1872 TrackExtends::~TrackExtends() {}
1875 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1877 buffer->ReadWriteUInt32(&track_id) &&
1878 buffer->ReadWriteUInt32(&default_sample_description_index) &&
1879 buffer->ReadWriteUInt32(&default_sample_duration) &&
1880 buffer->ReadWriteUInt32(&default_sample_size) &&
1881 buffer->ReadWriteUInt32(&default_sample_flags));
1885 uint32_t TrackExtends::ComputeSizeInternal() {
1887 sizeof(default_sample_description_index) +
1888 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
1889 sizeof(default_sample_flags);
1892 MovieExtends::MovieExtends() {}
1893 MovieExtends::~MovieExtends() {}
1896 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1901 DCHECK(buffer->
reader());
1904 for (uint32_t i = 0; i < tracks.size(); ++i)
1910 uint32_t MovieExtends::ComputeSizeInternal() {
1912 if (tracks.size() == 0)
1915 for (uint32_t i = 0; i < tracks.size(); ++i)
1924 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
1936 for (uint32_t i = 0; i < tracks.size(); ++i)
1938 for (uint32_t i = 0; i < pssh.size(); ++i)
1944 uint32_t Movie::ComputeSizeInternal() {
1947 for (uint32_t i = 0; i < tracks.size(); ++i)
1949 for (uint32_t i = 0; i < pssh.size(); ++i)
1954 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
1955 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
1958 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
1960 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1965 uint32_t TrackFragmentDecodeTime::ComputeSizeInternal() {
1966 version = IsFitIn32Bits(decode_time) ? 0 : 1;
1967 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1970 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
1971 MovieFragmentHeader::~MovieFragmentHeader() {}
1974 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1976 buffer->ReadWriteUInt32(&sequence_number);
1979 uint32_t MovieFragmentHeader::ComputeSizeInternal() {
1980 return HeaderSize() +
sizeof(sequence_number);
1983 TrackFragmentHeader::TrackFragmentHeader()
1985 sample_description_index(0),
1986 default_sample_duration(0),
1987 default_sample_size(0),
1988 default_sample_flags(0) {}
1990 TrackFragmentHeader::~TrackFragmentHeader() {}
1993 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1995 buffer->ReadWriteUInt32(&track_id));
1997 if (flags & kBaseDataOffsetPresentMask) {
2002 uint64_t base_data_offset;
2003 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
2004 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
2005 "default-base-is-moof.";
2008 if (flags & kSampleDescriptionIndexPresentMask) {
2009 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
2010 }
else if (buffer->
Reading()) {
2011 sample_description_index = 0;
2014 if (flags & kDefaultSampleDurationPresentMask) {
2015 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2016 }
else if (buffer->
Reading()) {
2017 default_sample_duration = 0;
2020 if (flags & kDefaultSampleSizePresentMask) {
2021 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2022 }
else if (buffer->
Reading()) {
2023 default_sample_size = 0;
2026 if (flags & kDefaultSampleFlagsPresentMask)
2027 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2031 uint32_t TrackFragmentHeader::ComputeSizeInternal() {
2032 uint32_t box_size =
HeaderSize() +
sizeof(track_id);
2033 if (flags & kSampleDescriptionIndexPresentMask)
2034 box_size +=
sizeof(sample_description_index);
2035 if (flags & kDefaultSampleDurationPresentMask)
2036 box_size +=
sizeof(default_sample_duration);
2037 if (flags & kDefaultSampleSizePresentMask)
2038 box_size +=
sizeof(default_sample_size);
2039 if (flags & kDefaultSampleFlagsPresentMask)
2040 box_size +=
sizeof(default_sample_flags);
2044 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
2045 TrackFragmentRun::~TrackFragmentRun() {}
2048 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
2054 if (flags & kSampleCompTimeOffsetsPresentMask) {
2055 for (uint32_t i = 0; i < sample_count; ++i) {
2056 if (sample_composition_time_offsets[i] < 0) {
2065 buffer->ReadWriteUInt32(&sample_count));
2067 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2068 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2069 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2070 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2071 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2072 bool sample_composition_time_offsets_present =
2073 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2075 if (data_offset_present) {
2076 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2087 uint32_t first_sample_flags;
2090 if (first_sample_flags_present)
2091 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2093 if (sample_duration_present)
2094 sample_durations.resize(sample_count);
2095 if (sample_size_present)
2096 sample_sizes.resize(sample_count);
2097 if (sample_flags_present)
2098 sample_flags.resize(sample_count);
2099 if (sample_composition_time_offsets_present)
2100 sample_composition_time_offsets.resize(sample_count);
2102 if (first_sample_flags_present) {
2103 first_sample_flags = sample_flags[0];
2104 DCHECK(sample_flags.size() == 1);
2105 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2108 if (sample_duration_present)
2109 DCHECK(sample_durations.size() == sample_count);
2110 if (sample_size_present)
2111 DCHECK(sample_sizes.size() == sample_count);
2112 if (sample_flags_present)
2113 DCHECK(sample_flags.size() == sample_count);
2114 if (sample_composition_time_offsets_present)
2115 DCHECK(sample_composition_time_offsets.size() == sample_count);
2118 for (uint32_t i = 0; i < sample_count; ++i) {
2119 if (sample_duration_present)
2120 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2121 if (sample_size_present)
2122 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2123 if (sample_flags_present)
2124 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2126 if (sample_composition_time_offsets_present) {
2128 uint32_t sample_offset = sample_composition_time_offsets[i];
2129 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2130 sample_composition_time_offsets[i] = sample_offset;
2132 int32_t sample_offset = sample_composition_time_offsets[i];
2133 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2134 sample_composition_time_offsets[i] = sample_offset;
2140 if (first_sample_flags_present) {
2141 if (sample_flags.size() == 0) {
2142 sample_flags.push_back(first_sample_flags);
2144 sample_flags[0] = first_sample_flags;
2151 uint32_t TrackFragmentRun::ComputeSizeInternal() {
2152 uint32_t box_size =
HeaderSize() +
sizeof(sample_count);
2153 if (flags & kDataOffsetPresentMask)
2154 box_size +=
sizeof(data_offset);
2155 if (flags & kFirstSampleFlagsPresentMask)
2156 box_size +=
sizeof(uint32_t);
2157 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2158 (flags & kSampleSizePresentMask ? 1 : 0) +
2159 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2160 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2161 box_size += fields *
sizeof(uint32_t) * sample_count;
2165 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
2166 SampleToGroup::~SampleToGroup() {}
2169 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
2171 buffer->ReadWriteUInt32(&grouping_type));
2173 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
2175 if (grouping_type != FOURCC_SEIG) {
2177 DLOG(WARNING) <<
"Sample group "
2178 << FourCCToString(static_cast<FourCC>(grouping_type))
2179 <<
" is not supported.";
2183 uint32_t count = entries.size();
2184 RCHECK(buffer->ReadWriteUInt32(&count));
2185 entries.resize(count);
2186 for (uint32_t i = 0; i < count; ++i) {
2187 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
2188 buffer->ReadWriteUInt32(&entries[i].group_description_index));
2193 uint32_t SampleToGroup::ComputeSizeInternal() {
2195 if (entries.empty())
2197 return HeaderSize() +
sizeof(grouping_type) +
2198 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
2199 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
2202 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
2203 : is_encrypted(false), iv_size(0) {
2205 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
2207 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
2208 SampleGroupDescription::~SampleGroupDescription() {}
2211 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
2213 buffer->ReadWriteUInt32(&grouping_type));
2215 if (grouping_type != FOURCC_SEIG) {
2217 DLOG(WARNING) <<
"Sample group '" << grouping_type <<
"' is not supported.";
2221 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
2222 uint32_t default_length = 0;
2225 RCHECK(buffer->ReadWriteUInt32(&default_length));
2226 RCHECK(default_length == 0 || default_length >= kEntrySize);
2228 default_length = kEntrySize;
2229 RCHECK(buffer->ReadWriteUInt32(&default_length));
2233 uint32_t count = entries.size();
2234 RCHECK(buffer->ReadWriteUInt32(&count));
2235 entries.resize(count);
2236 for (uint32_t i = 0; i < count; ++i) {
2238 if (buffer->
Reading() && default_length == 0) {
2239 uint32_t description_length = 0;
2240 RCHECK(buffer->ReadWriteUInt32(&description_length));
2241 RCHECK(description_length >= kEntrySize);
2246 if (entries[i].key_id.size() != kCencKeyIdSize) {
2247 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
2248 <<
" bytes; got " << entries[i].key_id.size()
2249 <<
". Resized accordingly.";
2250 entries[i].key_id.resize(kCencKeyIdSize);
2254 uint8_t flag = entries[i].is_encrypted ? 1 : 0;
2256 buffer->ReadWriteUInt8(&flag) &&
2257 buffer->ReadWriteUInt8(&entries[i].iv_size) &&
2258 buffer->ReadWriteVector(&entries[i].key_id, kCencKeyIdSize));
2261 entries[i].is_encrypted = (flag != 0);
2262 if (entries[i].is_encrypted) {
2263 RCHECK(entries[i].iv_size == 8 || entries[i].iv_size == 16);
2265 RCHECK(entries[i].iv_size == 0);
2272 uint32_t SampleGroupDescription::ComputeSizeInternal() {
2276 if (entries.empty())
2278 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
2279 return HeaderSize() +
sizeof(grouping_type) +
2280 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
2281 entries.size() * kEntrySize;
2284 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2285 TrackFragment::~TrackFragment() {}
2288 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2293 DCHECK(buffer->
reader());
2295 if (!decode_time_absent)
2303 while (sample_to_group.grouping_type != FOURCC_SEIG &&
2307 while (sample_group_description.grouping_type != FOURCC_SEIG &&
2312 if (!decode_time_absent)
2314 for (uint32_t i = 0; i < runs.size(); ++i)
2324 uint32_t TrackFragment::ComputeSizeInternal() {
2330 for (uint32_t i = 0; i < runs.size(); ++i)
2335 MovieFragment::MovieFragment() {}
2336 MovieFragment::~MovieFragment() {}
2339 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2349 for (uint32_t i = 0; i < tracks.size(); ++i)
2351 for (uint32_t i = 0; i < pssh.size(); ++i)
2357 uint32_t MovieFragment::ComputeSizeInternal() {
2359 for (uint32_t i = 0; i < tracks.size(); ++i)
2361 for (uint32_t i = 0; i < pssh.size(); ++i)
2366 SegmentIndex::SegmentIndex()
2369 earliest_presentation_time(0),
2371 SegmentIndex::~SegmentIndex() {}
2374 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2376 buffer->ReadWriteUInt32(&reference_id) &&
2377 buffer->ReadWriteUInt32(×cale));
2379 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2384 uint16_t reference_count = references.size();
2386 buffer->ReadWriteUInt16(&reference_count));
2387 references.resize(reference_count);
2389 uint32_t reference_type_size;
2391 for (uint32_t i = 0; i < reference_count; ++i) {
2393 reference_type_size = references[i].referenced_size;
2394 if (references[i].reference_type)
2395 reference_type_size |= (1 << 31);
2396 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2397 if (references[i].starts_with_sap)
2400 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2401 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2402 buffer->ReadWriteUInt32(&sap));
2404 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2405 references[i].referenced_size = reference_type_size & ~(1 << 31);
2406 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2407 references[i].sap_type =
2408 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2409 references[i].sap_delta_time = sap & ~(0xF << 28);
2415 uint32_t SegmentIndex::ComputeSizeInternal() {
2416 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2417 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2418 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2419 3 *
sizeof(uint32_t) * references.size();
2422 MediaData::MediaData() : data_size(0) {}
2423 MediaData::~MediaData() {}
2426 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2427 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2431 uint32_t MediaData::ComputeSizeInternal() {
2435 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2436 CueSourceIDBox::~CueSourceIDBox() {}
2440 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2445 uint32_t CueSourceIDBox::ComputeSizeInternal() {
2446 if (source_id == kCueSourceIdNotSet)
2451 CueTimeBox::CueTimeBox() {}
2452 CueTimeBox::~CueTimeBox() {}
2458 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2465 uint32_t CueTimeBox::ComputeSizeInternal() {
2466 if (cue_current_time.empty())
2468 return HeaderSize() + cue_current_time.size();
2471 CueIDBox::CueIDBox() {}
2472 CueIDBox::~CueIDBox() {}
2478 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2484 uint32_t CueIDBox::ComputeSizeInternal() {
2490 CueSettingsBox::CueSettingsBox() {}
2491 CueSettingsBox::~CueSettingsBox() {}
2497 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2503 uint32_t CueSettingsBox::ComputeSizeInternal() {
2504 if (settings.empty())
2509 CuePayloadBox::CuePayloadBox() {}
2510 CuePayloadBox::~CuePayloadBox() {}
2516 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2522 uint32_t CuePayloadBox::ComputeSizeInternal() {
2526 VTTEmptyCueBox::VTTEmptyCueBox() {}
2527 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2533 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2537 uint32_t VTTEmptyCueBox::ComputeSizeInternal() {
2541 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2542 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2548 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2551 &cue_additional_text,
2555 uint32_t VTTAdditionalTextBox::ComputeSizeInternal() {
2556 return HeaderSize() + cue_additional_text.size();
2559 VTTCueBox::VTTCueBox() {}
2560 VTTCueBox::~VTTCueBox() {}
2566 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2577 uint32_t VTTCueBox::ComputeSizeInternal() {
uint32_t ComputeSize() const
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
bool ParseFromBuffer(uint8_t iv_size, bool has_subsamples, BufferReader *reader)
bool ReadWrite(uint8_t iv_size, bool has_subsamples, BoxBuffer *buffer)
uint32_t GetTotalSizeOfSubsamples() const