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/formats/mp4/box_buffer.h"
12 #include "packager/media/formats/mp4/rcheck.h"
15 const uint32_t kFourCCSize = 4;
18 const uint32_t kCencKeyIdSize = 16;
21 const uint8_t kUnityMatrix[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
23 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0};
26 const char kVideoHandlerName[] =
"VideoHandler";
27 const char kAudioHandlerName[] =
"SoundHandler";
28 const char kTextHandlerName[] =
"TextHandler";
31 const uint32_t kVideoResolution = 0x00480000;
32 const uint16_t kVideoFrameCount = 1;
33 const uint16_t kVideoDepth = 0x0018;
35 const uint32_t kCompressorNameSize = 32u;
36 const char kAvcCompressorName[] =
"\012AVC Coding";
37 const char kHevcCompressorName[] =
"\013HEVC Coding";
38 const char kVpcCompressorName[] =
"\012VPC Coding";
42 const int kCueSourceIdNotSet = -1;
46 bool IsIvSizeValid(
size_t iv_size) {
47 return iv_size == 8 || iv_size == 16;
64 const uint8_t kDdtsExtraData[] = {0xe4, 0x7c, 0, 4, 0, 0x0f, 0};
67 const uint32_t kID3v2HeaderSize = 10;
68 const char kID3v2Identifier[] =
"ID3";
69 const uint16_t kID3v2Version = 0x0400;
72 bool IsFitIn32Bits(uint64_t a) {
73 return a <= std::numeric_limits<uint32_t>::max();
76 bool IsFitIn32Bits(int64_t a) {
77 return a <= std::numeric_limits<int32_t>::max() &&
78 a >= std::numeric_limits<int32_t>::min();
81 template <
typename T1,
typename T2>
82 bool IsFitIn32Bits(T1 a1, T2 a2) {
83 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
86 template <
typename T1,
typename T2,
typename T3>
87 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
88 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
93 namespace edash_packager {
99 TrackType FourCCToTrackType(FourCC fourcc) {
112 FourCC TrackTypeToFourCC(TrackType track_type) {
113 switch (track_type) {
127 FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
128 FileType::~FileType() {}
131 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
133 buffer->ReadWriteFourCC(&major_brand) &&
134 buffer->ReadWriteUInt32(&minor_version));
137 RCHECK(buffer->
BytesLeft() %
sizeof(FourCC) == 0);
138 num_brands = buffer->
BytesLeft() /
sizeof(FourCC);
139 compatible_brands.resize(num_brands);
141 num_brands = compatible_brands.size();
143 for (
size_t i = 0; i < num_brands; ++i)
144 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
148 uint32_t FileType::ComputeSizeInternal() {
149 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
150 kFourCCSize * compatible_brands.size();
155 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
156 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
159 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
160 if (!buffer->
Reading() && !raw_box.empty()) {
162 buffer->
writer()->AppendVector(raw_box);
166 uint32_t size = data.size();
168 buffer->ReadWriteVector(&system_id, 16) &&
169 buffer->ReadWriteUInt32(&size) &&
170 buffer->ReadWriteVector(&data, size));
175 DCHECK(raw_box.empty());
176 BoxReader* reader = buffer->
reader();
178 raw_box.assign(reader->data(), reader->data() + reader->size());
183 uint32_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
184 if (!raw_box.empty()) {
185 return raw_box.size();
187 return HeaderSize() + system_id.size() +
sizeof(uint32_t) + data.size();
191 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
192 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
195 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
200 uint32_t count = offsets.size();
201 RCHECK(buffer->ReadWriteUInt32(&count));
202 offsets.resize(count);
204 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
205 for (uint32_t i = 0; i < count; ++i)
210 uint32_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
212 if (offsets.size() == 0)
214 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
215 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
218 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
219 : default_sample_info_size(0), sample_count(0) {}
220 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
223 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
228 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
229 buffer->ReadWriteUInt32(&sample_count));
230 if (default_sample_info_size == 0)
231 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
235 uint32_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
237 if (sample_count == 0)
239 return HeaderSize() +
sizeof(default_sample_info_size) +
240 sizeof(sample_count) +
241 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
244 SampleEncryptionEntry::SampleEncryptionEntry() {}
245 SampleEncryptionEntry::~SampleEncryptionEntry() {}
250 DCHECK(IsIvSizeValid(iv_size));
253 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
255 if (!has_subsamples) {
260 uint16_t subsample_count = subsamples.size();
261 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
262 RCHECK(subsample_count > 0);
263 subsamples.resize(subsample_count);
264 for (
auto& subsample : subsamples) {
265 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
266 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
274 DCHECK(IsIvSizeValid(iv_size));
277 initialization_vector.resize(iv_size);
278 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
280 if (!has_subsamples) {
285 uint16_t subsample_count;
286 RCHECK(reader->Read2(&subsample_count));
287 RCHECK(subsample_count > 0);
288 subsamples.resize(subsample_count);
289 for (
auto& subsample : subsamples) {
290 RCHECK(reader->Read2(&subsample.clear_bytes) &&
291 reader->Read4(&subsample.cipher_bytes));
297 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
298 const uint16_t subsample_count = subsamples.size();
299 return initialization_vector.size() +
300 (subsample_count > 0 ? (
sizeof(subsample_count) +
301 subsample_entry_size * subsample_count)
307 for (uint32_t i = 0; i < subsamples.size(); ++i)
308 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
312 SampleEncryption::SampleEncryption() : iv_size(0) {}
313 SampleEncryption::~SampleEncryption() {}
316 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
321 if (buffer->
Reading() && iv_size == 0) {
327 if (!IsIvSizeValid(iv_size)) {
328 LOG(ERROR) <<
"IV_size can only be 8 or 16, but seeing " << iv_size;
332 uint32_t sample_count = sample_encryption_entries.size();
333 RCHECK(buffer->ReadWriteUInt32(&sample_count));
335 sample_encryption_entries.resize(sample_count);
336 for (
auto& sample_encryption_entry : sample_encryption_entries) {
337 RCHECK(sample_encryption_entry.ReadWrite(
338 iv_size, flags & kUseSubsampleEncryption, buffer));
343 uint32_t SampleEncryption::ComputeSizeInternal() {
344 const uint32_t sample_count = sample_encryption_entries.size();
345 if (sample_count == 0) {
350 DCHECK(IsIvSizeValid(iv_size));
352 if (flags & kUseSubsampleEncryption) {
353 for (
const SampleEncryptionEntry& sample_encryption_entry :
354 sample_encryption_entries) {
355 box_size += sample_encryption_entry.ComputeSize();
358 box_size += sample_count * iv_size;
365 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
366 DCHECK(IsIvSizeValid(iv_size));
370 uint32_t sample_count = 0;
371 RCHECK(reader.Read4(&sample_count));
373 sample_encryption_entries->resize(sample_count);
374 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
375 RCHECK(sample_encryption_entry.ParseFromBuffer(
376 iv_size, flags & kUseSubsampleEncryption, &reader));
381 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
382 OriginalFormat::~OriginalFormat() {}
385 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
389 uint32_t OriginalFormat::ComputeSizeInternal() {
393 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
394 SchemeType::~SchemeType() {}
397 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
399 buffer->ReadWriteFourCC(&type) &&
400 buffer->ReadWriteUInt32(&version));
404 uint32_t SchemeType::ComputeSizeInternal() {
405 return HeaderSize() + kFourCCSize +
sizeof(version);
408 TrackEncryption::TrackEncryption()
409 : is_encrypted(false), default_iv_size(0), default_kid(16, 0) {}
410 TrackEncryption::~TrackEncryption() {}
413 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
415 if (default_kid.size() != kCencKeyIdSize) {
416 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
417 <<
" bytes; got " << default_kid.size()
418 <<
". Resized accordingly.";
419 default_kid.resize(kCencKeyIdSize);
423 uint8_t flag = is_encrypted ? 1 : 0;
426 buffer->ReadWriteUInt8(&flag) &&
427 buffer->ReadWriteUInt8(&default_iv_size) &&
428 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
430 is_encrypted = (flag != 0);
432 RCHECK(default_iv_size == 8 || default_iv_size == 16);
434 RCHECK(default_iv_size == 0);
440 uint32_t TrackEncryption::ComputeSizeInternal() {
441 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize;
444 SchemeInfo::SchemeInfo() {}
445 SchemeInfo::~SchemeInfo() {}
448 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
454 uint32_t SchemeInfo::ComputeSizeInternal() {
458 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
459 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
462 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
467 if (type.type == FOURCC_CENC)
476 uint32_t ProtectionSchemeInfo::ComputeSizeInternal() {
478 if (format.format == FOURCC_NULL)
484 MovieHeader::MovieHeader()
486 modification_time(0),
492 MovieHeader::~MovieHeader() {}
495 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
498 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
501 buffer->ReadWriteUInt32(×cale) &&
504 std::vector<uint8_t> matrix(kUnityMatrix,
505 kUnityMatrix + arraysize(kUnityMatrix));
506 RCHECK(buffer->ReadWriteInt32(&rate) &&
507 buffer->ReadWriteInt16(&volume) &&
509 buffer->ReadWriteVector(&matrix, matrix.size()) &&
511 buffer->ReadWriteUInt32(&next_track_id));
515 uint32_t MovieHeader::ComputeSizeInternal() {
516 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
517 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
518 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
519 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
523 TrackHeader::TrackHeader()
525 modification_time(0),
533 flags = kTrackEnabled | kTrackInMovie;
535 TrackHeader::~TrackHeader() {}
538 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
541 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
544 buffer->ReadWriteUInt32(&track_id) &&
551 volume = (width != 0 && height != 0) ? 0 : 0x100;
553 std::vector<uint8_t> matrix(kUnityMatrix,
554 kUnityMatrix + arraysize(kUnityMatrix));
556 buffer->ReadWriteInt16(&layer) &&
557 buffer->ReadWriteInt16(&alternate_group) &&
558 buffer->ReadWriteInt16(&volume) &&
560 buffer->ReadWriteVector(&matrix, matrix.size()) &&
561 buffer->ReadWriteUInt32(&width) &&
562 buffer->ReadWriteUInt32(&height));
566 uint32_t TrackHeader::ComputeSizeInternal() {
567 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
569 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
570 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
571 sizeof(height) +
sizeof(kUnityMatrix) + 14;
574 SampleDescription::SampleDescription() : type(kInvalid) {}
575 SampleDescription::~SampleDescription() {}
578 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
582 count = video_entries.size();
585 count = audio_entries.size();
588 count = wvtt_entries.size();
591 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
592 <<
" is not handled. Skipping.";
595 buffer->ReadWriteUInt32(&count));
598 BoxReader* reader = buffer->
reader();
600 video_entries.clear();
601 audio_entries.clear();
604 if (type == kVideo) {
605 RCHECK(reader->ReadAllChildren(&video_entries));
606 RCHECK(video_entries.size() == count);
607 }
else if (type == kAudio) {
608 RCHECK(reader->ReadAllChildren(&audio_entries));
609 RCHECK(audio_entries.size() == count);
610 }
else if (type == kText) {
611 RCHECK(reader->ReadAllChildren(&wvtt_entries));
612 RCHECK(wvtt_entries.size() == count);
615 DCHECK_LT(0u, count);
616 if (type == kVideo) {
617 for (uint32_t i = 0; i < count; ++i)
619 }
else if (type == kAudio) {
620 for (uint32_t i = 0; i < count; ++i)
622 }
else if (type == kText) {
623 for (uint32_t i = 0; i < count; ++i)
632 uint32_t SampleDescription::ComputeSizeInternal() {
633 uint32_t box_size =
HeaderSize() +
sizeof(uint32_t);
634 if (type == kVideo) {
635 for (uint32_t i = 0; i < video_entries.size(); ++i)
637 }
else if (type == kAudio) {
638 for (uint32_t i = 0; i < audio_entries.size(); ++i)
644 DecodingTimeToSample::DecodingTimeToSample() {}
645 DecodingTimeToSample::~DecodingTimeToSample() {}
648 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
649 uint32_t count = decoding_time.size();
651 buffer->ReadWriteUInt32(&count));
653 decoding_time.resize(count);
654 for (uint32_t i = 0; i < count; ++i) {
655 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
656 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
661 uint32_t DecodingTimeToSample::ComputeSizeInternal() {
663 sizeof(DecodingTime) * decoding_time.size();
666 CompositionTimeToSample::CompositionTimeToSample() {}
667 CompositionTimeToSample::~CompositionTimeToSample() {}
670 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
671 uint32_t count = composition_offset.size();
677 for (uint32_t i = 0; i < count; ++i) {
678 if (composition_offset[i].sample_offset < 0) {
686 buffer->ReadWriteUInt32(&count));
688 composition_offset.resize(count);
689 for (uint32_t i = 0; i < count; ++i) {
690 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
693 uint32_t sample_offset = composition_offset[i].sample_offset;
694 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
695 composition_offset[i].sample_offset = sample_offset;
697 int32_t sample_offset = composition_offset[i].sample_offset;
698 RCHECK(buffer->ReadWriteInt32(&sample_offset));
699 composition_offset[i].sample_offset = sample_offset;
705 uint32_t CompositionTimeToSample::ComputeSizeInternal() {
707 if (composition_offset.empty())
712 const uint32_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
714 kCompositionOffsetSize * composition_offset.size();
717 SampleToChunk::SampleToChunk() {}
718 SampleToChunk::~SampleToChunk() {}
721 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
722 uint32_t count = chunk_info.size();
724 buffer->ReadWriteUInt32(&count));
726 chunk_info.resize(count);
727 for (uint32_t i = 0; i < count; ++i) {
728 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
729 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
730 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
732 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
733 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
738 uint32_t SampleToChunk::ComputeSizeInternal() {
740 sizeof(ChunkInfo) * chunk_info.size();
743 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
744 SampleSize::~SampleSize() {}
747 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
749 buffer->ReadWriteUInt32(&sample_size) &&
750 buffer->ReadWriteUInt32(&sample_count));
752 if (sample_size == 0) {
754 sizes.resize(sample_count);
756 DCHECK(sample_count == sizes.size());
757 for (uint32_t i = 0; i < sample_count; ++i)
758 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
763 uint32_t SampleSize::ComputeSizeInternal() {
764 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
765 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
768 CompactSampleSize::CompactSampleSize() : field_size(0) {}
769 CompactSampleSize::~CompactSampleSize() {}
772 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
773 uint32_t sample_count = sizes.size();
776 buffer->ReadWriteUInt8(&field_size) &&
777 buffer->ReadWriteUInt32(&sample_count));
780 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
781 switch (field_size) {
783 for (uint32_t i = 0; i < sample_count; i += 2) {
786 RCHECK(buffer->ReadWriteUInt8(&size));
787 sizes[i] = size >> 4;
788 sizes[i + 1] = size & 0x0F;
790 DCHECK_LT(sizes[i], 16u);
791 DCHECK_LT(sizes[i + 1], 16u);
792 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
793 RCHECK(buffer->ReadWriteUInt8(&size));
798 for (uint32_t i = 0; i < sample_count; ++i) {
799 uint8_t size = sizes[i];
800 RCHECK(buffer->ReadWriteUInt8(&size));
805 for (uint32_t i = 0; i < sample_count; ++i) {
806 uint16_t size = sizes[i];
807 RCHECK(buffer->ReadWriteUInt16(&size));
814 sizes.resize(sample_count);
818 uint32_t CompactSampleSize::ComputeSizeInternal() {
819 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
820 (field_size * sizes.size() + 7) / 8;
823 ChunkOffset::ChunkOffset() {}
824 ChunkOffset::~ChunkOffset() {}
827 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
828 uint32_t count = offsets.size();
830 buffer->ReadWriteUInt32(&count));
832 offsets.resize(count);
833 for (uint32_t i = 0; i < count; ++i)
838 uint32_t ChunkOffset::ComputeSizeInternal() {
839 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
842 ChunkLargeOffset::ChunkLargeOffset() {}
843 ChunkLargeOffset::~ChunkLargeOffset() {}
846 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
847 uint32_t count = offsets.size();
851 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
853 stco.offsets.swap(offsets);
856 stco.offsets.swap(offsets);
862 buffer->ReadWriteUInt32(&count));
864 offsets.resize(count);
865 for (uint32_t i = 0; i < count; ++i)
866 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
870 uint32_t ChunkLargeOffset::ComputeSizeInternal() {
871 uint32_t count = offsets.size();
872 int use_large_offset =
873 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
875 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
878 SyncSample::SyncSample() {}
879 SyncSample::~SyncSample() {}
882 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
883 uint32_t count = sample_number.size();
885 buffer->ReadWriteUInt32(&count));
887 sample_number.resize(count);
888 for (uint32_t i = 0; i < count; ++i)
889 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
893 uint32_t SyncSample::ComputeSizeInternal() {
895 if (sample_number.empty())
898 sizeof(uint32_t) * sample_number.size();
901 SampleTable::SampleTable() {}
902 SampleTable::~SampleTable() {}
905 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
921 CompactSampleSize compact_sample_size;
922 RCHECK(reader->
ReadChild(&compact_sample_size));
923 sample_size.sample_size = 0;
924 sample_size.sample_count = compact_sample_size.sizes.size();
925 sample_size.sizes.swap(compact_sample_size.sizes);
929 if (reader->
ChildExist(&chunk_large_offset)) {
930 RCHECK(reader->
ReadChild(&chunk_large_offset));
932 ChunkOffset chunk_offset;
933 RCHECK(reader->
ReadChild(&chunk_offset));
934 chunk_large_offset.offsets.swap(chunk_offset.offsets);
944 uint32_t SampleTable::ComputeSizeInternal() {
952 EditList::EditList() {}
953 EditList::~EditList() {}
956 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
957 uint32_t count = edits.size();
961 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
962 for (uint32_t i = 0; i < count; ++i) {
965 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
966 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
967 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
972 uint32_t EditList::ComputeSizeInternal() {
978 for (uint32_t i = 0; i < edits.size(); ++i) {
979 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
985 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
993 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
999 uint32_t Edit::ComputeSizeInternal() {
1001 if (list.edits.empty())
1006 HandlerReference::HandlerReference() : handler_type(FOURCC_NULL) {}
1007 HandlerReference::~HandlerReference() {}
1010 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1011 std::vector<uint8_t> handler_name;
1013 switch (handler_type) {
1015 handler_name.assign(kVideoHandlerName,
1016 kVideoHandlerName + arraysize(kVideoHandlerName));
1019 handler_name.assign(kAudioHandlerName,
1020 kAudioHandlerName + arraysize(kAudioHandlerName));
1023 handler_name.assign(kTextHandlerName,
1024 kTextHandlerName + arraysize(kTextHandlerName));
1035 buffer->ReadWriteFourCC(&handler_type));
1038 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1043 uint32_t HandlerReference::ComputeSizeInternal() {
1044 uint32_t box_size =
HeaderSize() + kFourCCSize + 16;
1045 switch (handler_type) {
1047 box_size +=
sizeof(kVideoHandlerName);
1050 box_size +=
sizeof(kAudioHandlerName);
1053 box_size +=
sizeof(kTextHandlerName);
1063 bool Language::ReadWrite(BoxBuffer* buffer) {
1064 if (buffer->Reading()) {
1067 std::vector<uint8_t> temp;
1068 RCHECK(buffer->ReadWriteVector(&temp, 2));
1070 BitReader bit_reader(&temp[0], 2);
1071 bit_reader.SkipBits(1);
1073 for (
int i = 0; i < 3; ++i) {
1074 CHECK(bit_reader.ReadBits(5, &language[i]));
1075 language[i] += 0x60;
1077 code.assign(language, 3);
1080 const char kUndefinedLanguage[] =
"und";
1082 code = kUndefinedLanguage;
1083 DCHECK_EQ(code.size(), 3u);
1087 for (
int i = 0; i < 3; ++i)
1088 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1089 RCHECK(buffer->ReadWriteUInt16(&lang));
1094 uint32_t Language::ComputeSize()
const {
1099 bool PrivFrame::ReadWrite(BoxBuffer* buffer) {
1100 FourCC fourcc = FOURCC_PRIV;
1101 RCHECK(buffer->ReadWriteFourCC(&fourcc));
1102 if (fourcc != FOURCC_PRIV) {
1103 VLOG(1) <<
"Skip unrecognized id3 frame during read: "
1104 << FourCCToString(fourcc);
1108 uint32_t frame_size = owner.size() + 1 + value.size();
1112 DCHECK_LT(frame_size, 0x7Fu);
1114 RCHECK(buffer->ReadWriteUInt32(&frame_size) &&
1115 buffer->ReadWriteUInt16(&flags));
1117 if (buffer->Reading()) {
1119 RCHECK(buffer->ReadWriteString(&str, frame_size));
1121 size_t pos = str.find(
'\0');
1122 RCHECK(pos < str.size());
1123 owner = str.substr(0, pos);
1124 value = str.substr(pos + 1);
1127 RCHECK(buffer->ReadWriteString(&owner, owner.size()) &&
1128 buffer->ReadWriteUInt8(&byte) &&
1129 buffer->ReadWriteString(&value, value.size()));
1134 uint32_t PrivFrame::ComputeSize()
const {
1135 if (owner.empty() && value.empty())
1137 const uint32_t kFourCCSize = 4;
1138 return kFourCCSize +
sizeof(uint32_t) +
sizeof(uint16_t) + owner.size() + 1 +
1147 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1149 language.ReadWrite(buffer));
1152 std::string id3v2_identifier = kID3v2Identifier;
1153 uint16_t version = kID3v2Version;
1159 DCHECK_LT(data_size, 0x7Fu);
1161 RCHECK(buffer->
ReadWriteString(&id3v2_identifier, id3v2_identifier.size()) &&
1162 buffer->ReadWriteUInt16(&version) &&
1163 buffer->ReadWriteUInt8(&flags) &&
1164 buffer->ReadWriteUInt32(&data_size));
1170 uint32_t ID3v2::ComputeSizeInternal() {
1173 return private_frame_size == 0 ? 0 :
HeaderSize() + language.ComputeSize() +
1178 Metadata::Metadata() {}
1179 Metadata::~Metadata() {}
1185 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1193 uint32_t Metadata::ComputeSizeInternal() {
1196 return id3v2_size == 0 ? 0
1200 CodecConfigurationRecord::CodecConfigurationRecord() : box_type(FOURCC_NULL) {}
1201 CodecConfigurationRecord::~CodecConfigurationRecord() {}
1208 bool CodecConfigurationRecord::ReadWriteInternal(
BoxBuffer* buffer) {
1211 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1213 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1218 uint32_t CodecConfigurationRecord::ComputeSizeInternal() {
1224 PixelAspectRatio::PixelAspectRatio() : h_spacing(0), v_spacing(0) {}
1225 PixelAspectRatio::~PixelAspectRatio() {}
1228 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1230 buffer->ReadWriteUInt32(&h_spacing) &&
1231 buffer->ReadWriteUInt32(&v_spacing));
1235 uint32_t PixelAspectRatio::ComputeSizeInternal() {
1237 if (h_spacing == 0 && v_spacing == 0)
1240 DCHECK(h_spacing != 0 && v_spacing != 0);
1241 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1244 VideoSampleEntry::VideoSampleEntry()
1245 : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
1247 VideoSampleEntry::~VideoSampleEntry() {}
1249 if (format == FOURCC_NULL) {
1250 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
1251 <<
"handler type recovered in its Media ancestor.";
1256 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1257 std::vector<uint8_t> compressor_name;
1259 DCHECK(buffer->
reader());
1260 format = buffer->
reader()->type();
1264 const FourCC actual_format = GetActualFormat();
1265 switch (actual_format) {
1267 compressor_name.assign(
1269 kAvcCompressorName + arraysize(kAvcCompressorName));
1273 compressor_name.assign(
1274 kHevcCompressorName,
1275 kHevcCompressorName + arraysize(kHevcCompressorName));
1280 compressor_name.assign(
1282 kVpcCompressorName + arraysize(kVpcCompressorName));
1285 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1288 compressor_name.resize(kCompressorNameSize);
1291 uint32_t video_resolution = kVideoResolution;
1292 uint16_t video_frame_count = kVideoFrameCount;
1293 uint16_t video_depth = kVideoDepth;
1294 int16_t predefined = -1;
1296 buffer->ReadWriteUInt16(&data_reference_index) &&
1298 buffer->ReadWriteUInt16(&width) &&
1299 buffer->ReadWriteUInt16(&height) &&
1300 buffer->ReadWriteUInt32(&video_resolution) &&
1301 buffer->ReadWriteUInt32(&video_resolution) &&
1303 buffer->ReadWriteUInt16(&video_frame_count) &&
1304 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1305 buffer->ReadWriteUInt16(&video_depth) &&
1306 buffer->ReadWriteInt16(&predefined));
1310 if (format == FOURCC_ENCV) {
1314 while (sinf.type.type != FOURCC_CENC) {
1323 const FourCC actual_format = GetActualFormat();
1324 switch (actual_format) {
1326 codec_config_record.box_type = FOURCC_AVCC;
1330 codec_config_record.box_type = FOURCC_HVCC;
1335 codec_config_record.box_type = FOURCC_VPCC;
1338 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1346 uint32_t VideoSampleEntry::ComputeSizeInternal() {
1347 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1348 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1349 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1351 codec_config_record.
ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1355 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1356 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1359 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1362 std::vector<uint8_t> data;
1363 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1364 RCHECK(es_descriptor.Parse(data));
1365 if (es_descriptor.
IsAAC()) {
1366 RCHECK(aac_audio_specific_config.
Parse(
1367 es_descriptor.decoder_specific_info()));
1370 DCHECK(buffer->
writer());
1371 es_descriptor.Write(buffer->
writer());
1376 uint32_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1378 if (es_descriptor.object_type() == kForbidden)
1380 return HeaderSize() + es_descriptor.ComputeSize();
1383 DTSSpecific::DTSSpecific()
1384 : sampling_frequency(0),
1387 pcm_sample_depth(0) {}
1388 DTSSpecific::~DTSSpecific() {}
1391 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1393 buffer->ReadWriteUInt32(&sampling_frequency) &&
1394 buffer->ReadWriteUInt32(&max_bitrate) &&
1395 buffer->ReadWriteUInt32(&avg_bitrate) &&
1396 buffer->ReadWriteUInt8(&pcm_sample_depth));
1399 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
BytesLeft()));
1401 if (extra_data.empty()) {
1402 extra_data.assign(kDdtsExtraData,
1403 kDdtsExtraData +
sizeof(kDdtsExtraData));
1405 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1410 uint32_t DTSSpecific::ComputeSizeInternal() {
1412 if (sampling_frequency == 0)
1414 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1415 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1416 sizeof(kDdtsExtraData);
1419 AC3Specific::AC3Specific() {}
1420 AC3Specific::~AC3Specific() {}
1424 bool AC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1426 buffer->ReadWriteVector(
1431 uint32_t AC3Specific::ComputeSizeInternal() {
1438 AudioSampleEntry::AudioSampleEntry()
1439 : format(FOURCC_NULL),
1440 data_reference_index(1),
1445 AudioSampleEntry::~AudioSampleEntry() {}
1448 if (format == FOURCC_NULL) {
1449 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1450 <<
"handler type recovered in its Media ancestor.";
1455 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1457 DCHECK(buffer->
reader());
1458 format = buffer->
reader()->type();
1466 buffer->ReadWriteUInt16(&data_reference_index) &&
1468 buffer->ReadWriteUInt16(&channelcount) &&
1469 buffer->ReadWriteUInt16(&samplesize) &&
1471 buffer->ReadWriteUInt32(&samplerate));
1476 if (format == FOURCC_ENCA) {
1480 while (sinf.type.type != FOURCC_CENC) {
1495 uint32_t AudioSampleEntry::ComputeSizeInternal() {
1496 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1497 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1503 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1504 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1510 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1517 uint32_t WebVTTConfigurationBox::ComputeSizeInternal() {
1521 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1522 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1528 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1532 : source_label.size());
1535 uint32_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1536 if (source_label.empty())
1541 WVTTSampleEntry::WVTTSampleEntry() {}
1542 WVTTSampleEntry::~WVTTSampleEntry() {}
1548 bool WVTTSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1552 buffer->ReadWriteUInt16(&data_reference_index) &&
1559 uint32_t WVTTSampleEntry::ComputeSizeInternal() {
1561 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1565 MediaHeader::MediaHeader()
1566 : creation_time(0), modification_time(0), timescale(0), duration(0) {}
1567 MediaHeader::~MediaHeader() {}
1570 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1573 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1576 buffer->ReadWriteUInt32(×cale) &&
1578 language.ReadWrite(buffer) &&
1583 uint32_t MediaHeader::ComputeSizeInternal() {
1584 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1586 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1590 VideoMediaHeader::VideoMediaHeader()
1591 : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1592 const uint32_t kVideoMediaHeaderFlags = 1;
1593 flags = kVideoMediaHeaderFlags;
1595 VideoMediaHeader::~VideoMediaHeader() {}
1597 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1599 buffer->ReadWriteUInt16(&graphicsmode) &&
1600 buffer->ReadWriteUInt16(&opcolor_red) &&
1601 buffer->ReadWriteUInt16(&opcolor_green) &&
1602 buffer->ReadWriteUInt16(&opcolor_blue));
1606 uint32_t VideoMediaHeader::ComputeSizeInternal() {
1607 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1608 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1611 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1612 SoundMediaHeader::~SoundMediaHeader() {}
1614 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1616 buffer->ReadWriteUInt16(&balance) &&
1621 uint32_t SoundMediaHeader::ComputeSizeInternal() {
1622 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1625 SubtitleMediaHeader::SubtitleMediaHeader() {}
1626 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1630 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1634 uint32_t SubtitleMediaHeader::ComputeSizeInternal() {
1638 DataEntryUrl::DataEntryUrl() {
1639 const uint32_t kDataEntryUrlFlags = 1;
1640 flags = kDataEntryUrlFlags;
1642 DataEntryUrl::~DataEntryUrl() {}
1644 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
1647 RCHECK(buffer->ReadWriteVector(&location, buffer->
BytesLeft()));
1649 RCHECK(buffer->ReadWriteVector(&location, location.size()));
1654 uint32_t DataEntryUrl::ComputeSizeInternal() {
1658 DataReference::DataReference() {
1660 data_entry.resize(1);
1662 DataReference::~DataReference() {}
1664 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
1665 uint32_t entry_count = data_entry.size();
1667 buffer->ReadWriteUInt32(&entry_count));
1668 data_entry.resize(entry_count);
1670 for (uint32_t i = 0; i < entry_count; ++i)
1675 uint32_t DataReference::ComputeSizeInternal() {
1676 uint32_t count = data_entry.size();
1677 uint32_t box_size =
HeaderSize() +
sizeof(count);
1678 for (uint32_t i = 0; i < count; ++i)
1683 DataInformation::DataInformation() {}
1684 DataInformation::~DataInformation() {}
1687 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1693 uint32_t DataInformation::ComputeSizeInternal() {
1697 MediaInformation::MediaInformation() {}
1698 MediaInformation::~MediaInformation() {}
1701 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1706 switch (sample_table.description.type) {
1723 uint32_t MediaInformation::ComputeSizeInternal() {
1726 switch (sample_table.description.type) {
1746 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
1758 information.sample_table.description.type =
1759 FourCCToTrackType(handler.handler_type);
1761 handler.handler_type =
1762 TrackTypeToFourCC(information.sample_table.description.type);
1763 RCHECK(handler.handler_type != FOURCC_NULL);
1770 uint32_t Media::ComputeSizeInternal() {
1771 handler.handler_type =
1772 TrackTypeToFourCC(information.sample_table.description.type);
1781 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
1791 uint32_t Track::ComputeSizeInternal() {
1796 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
1797 MovieExtendsHeader::~MovieExtendsHeader() {}
1800 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1802 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1807 uint32_t MovieExtendsHeader::ComputeSizeInternal() {
1809 if (fragment_duration == 0)
1811 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
1812 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1815 TrackExtends::TrackExtends()
1817 default_sample_description_index(0),
1818 default_sample_duration(0),
1819 default_sample_size(0),
1820 default_sample_flags(0) {}
1821 TrackExtends::~TrackExtends() {}
1824 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1826 buffer->ReadWriteUInt32(&track_id) &&
1827 buffer->ReadWriteUInt32(&default_sample_description_index) &&
1828 buffer->ReadWriteUInt32(&default_sample_duration) &&
1829 buffer->ReadWriteUInt32(&default_sample_size) &&
1830 buffer->ReadWriteUInt32(&default_sample_flags));
1834 uint32_t TrackExtends::ComputeSizeInternal() {
1836 sizeof(default_sample_description_index) +
1837 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
1838 sizeof(default_sample_flags);
1841 MovieExtends::MovieExtends() {}
1842 MovieExtends::~MovieExtends() {}
1845 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1850 DCHECK(buffer->
reader());
1853 for (uint32_t i = 0; i < tracks.size(); ++i)
1859 uint32_t MovieExtends::ComputeSizeInternal() {
1861 if (tracks.size() == 0)
1864 for (uint32_t i = 0; i < tracks.size(); ++i)
1873 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
1885 for (uint32_t i = 0; i < tracks.size(); ++i)
1887 for (uint32_t i = 0; i < pssh.size(); ++i)
1893 uint32_t Movie::ComputeSizeInternal() {
1896 for (uint32_t i = 0; i < tracks.size(); ++i)
1898 for (uint32_t i = 0; i < pssh.size(); ++i)
1903 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
1904 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
1907 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
1909 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1914 uint32_t TrackFragmentDecodeTime::ComputeSizeInternal() {
1915 version = IsFitIn32Bits(decode_time) ? 0 : 1;
1916 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1919 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
1920 MovieFragmentHeader::~MovieFragmentHeader() {}
1923 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1925 buffer->ReadWriteUInt32(&sequence_number);
1928 uint32_t MovieFragmentHeader::ComputeSizeInternal() {
1929 return HeaderSize() +
sizeof(sequence_number);
1932 TrackFragmentHeader::TrackFragmentHeader()
1934 sample_description_index(0),
1935 default_sample_duration(0),
1936 default_sample_size(0),
1937 default_sample_flags(0) {}
1939 TrackFragmentHeader::~TrackFragmentHeader() {}
1942 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1944 buffer->ReadWriteUInt32(&track_id));
1946 if (flags & kBaseDataOffsetPresentMask) {
1951 uint64_t base_data_offset;
1952 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
1953 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
1954 "default-base-is-moof.";
1957 if (flags & kSampleDescriptionIndexPresentMask) {
1958 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
1959 }
else if (buffer->
Reading()) {
1960 sample_description_index = 0;
1963 if (flags & kDefaultSampleDurationPresentMask) {
1964 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
1965 }
else if (buffer->
Reading()) {
1966 default_sample_duration = 0;
1969 if (flags & kDefaultSampleSizePresentMask) {
1970 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
1971 }
else if (buffer->
Reading()) {
1972 default_sample_size = 0;
1975 if (flags & kDefaultSampleFlagsPresentMask)
1976 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
1980 uint32_t TrackFragmentHeader::ComputeSizeInternal() {
1981 uint32_t box_size =
HeaderSize() +
sizeof(track_id);
1982 if (flags & kSampleDescriptionIndexPresentMask)
1983 box_size +=
sizeof(sample_description_index);
1984 if (flags & kDefaultSampleDurationPresentMask)
1985 box_size +=
sizeof(default_sample_duration);
1986 if (flags & kDefaultSampleSizePresentMask)
1987 box_size +=
sizeof(default_sample_size);
1988 if (flags & kDefaultSampleFlagsPresentMask)
1989 box_size +=
sizeof(default_sample_flags);
1993 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
1994 TrackFragmentRun::~TrackFragmentRun() {}
1997 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
2003 if (flags & kSampleCompTimeOffsetsPresentMask) {
2004 for (uint32_t i = 0; i < sample_count; ++i) {
2005 if (sample_composition_time_offsets[i] < 0) {
2014 buffer->ReadWriteUInt32(&sample_count));
2016 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2017 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2018 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2019 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2020 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2021 bool sample_composition_time_offsets_present =
2022 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2024 if (data_offset_present) {
2025 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2036 uint32_t first_sample_flags;
2039 if (first_sample_flags_present)
2040 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2042 if (sample_duration_present)
2043 sample_durations.resize(sample_count);
2044 if (sample_size_present)
2045 sample_sizes.resize(sample_count);
2046 if (sample_flags_present)
2047 sample_flags.resize(sample_count);
2048 if (sample_composition_time_offsets_present)
2049 sample_composition_time_offsets.resize(sample_count);
2051 if (first_sample_flags_present) {
2052 first_sample_flags = sample_flags[0];
2053 DCHECK(sample_flags.size() == 1);
2054 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2057 if (sample_duration_present)
2058 DCHECK(sample_durations.size() == sample_count);
2059 if (sample_size_present)
2060 DCHECK(sample_sizes.size() == sample_count);
2061 if (sample_flags_present)
2062 DCHECK(sample_flags.size() == sample_count);
2063 if (sample_composition_time_offsets_present)
2064 DCHECK(sample_composition_time_offsets.size() == sample_count);
2067 for (uint32_t i = 0; i < sample_count; ++i) {
2068 if (sample_duration_present)
2069 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2070 if (sample_size_present)
2071 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2072 if (sample_flags_present)
2073 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2075 if (sample_composition_time_offsets_present) {
2077 uint32_t sample_offset = sample_composition_time_offsets[i];
2078 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2079 sample_composition_time_offsets[i] = sample_offset;
2081 int32_t sample_offset = sample_composition_time_offsets[i];
2082 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2083 sample_composition_time_offsets[i] = sample_offset;
2089 if (first_sample_flags_present) {
2090 if (sample_flags.size() == 0) {
2091 sample_flags.push_back(first_sample_flags);
2093 sample_flags[0] = first_sample_flags;
2100 uint32_t TrackFragmentRun::ComputeSizeInternal() {
2101 uint32_t box_size =
HeaderSize() +
sizeof(sample_count);
2102 if (flags & kDataOffsetPresentMask)
2103 box_size +=
sizeof(data_offset);
2104 if (flags & kFirstSampleFlagsPresentMask)
2105 box_size +=
sizeof(uint32_t);
2106 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2107 (flags & kSampleSizePresentMask ? 1 : 0) +
2108 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2109 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2110 box_size += fields *
sizeof(uint32_t) * sample_count;
2114 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
2115 SampleToGroup::~SampleToGroup() {}
2118 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
2120 buffer->ReadWriteUInt32(&grouping_type));
2122 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
2124 if (grouping_type != FOURCC_SEIG) {
2126 DLOG(WARNING) <<
"Sample group "
2127 << FourCCToString(static_cast<FourCC>(grouping_type))
2128 <<
" is not supported.";
2132 uint32_t count = entries.size();
2133 RCHECK(buffer->ReadWriteUInt32(&count));
2134 entries.resize(count);
2135 for (uint32_t i = 0; i < count; ++i) {
2136 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
2137 buffer->ReadWriteUInt32(&entries[i].group_description_index));
2142 uint32_t SampleToGroup::ComputeSizeInternal() {
2144 if (entries.empty())
2146 return HeaderSize() +
sizeof(grouping_type) +
2147 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
2148 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
2151 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
2152 : is_encrypted(false), iv_size(0) {
2154 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
2156 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
2157 SampleGroupDescription::~SampleGroupDescription() {}
2160 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
2162 buffer->ReadWriteUInt32(&grouping_type));
2164 if (grouping_type != FOURCC_SEIG) {
2166 DLOG(WARNING) <<
"Sample group '" << grouping_type <<
"' is not supported.";
2170 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
2171 uint32_t default_length = 0;
2174 RCHECK(buffer->ReadWriteUInt32(&default_length));
2175 RCHECK(default_length == 0 || default_length >= kEntrySize);
2177 default_length = kEntrySize;
2178 RCHECK(buffer->ReadWriteUInt32(&default_length));
2182 uint32_t count = entries.size();
2183 RCHECK(buffer->ReadWriteUInt32(&count));
2184 entries.resize(count);
2185 for (uint32_t i = 0; i < count; ++i) {
2187 if (buffer->
Reading() && default_length == 0) {
2188 uint32_t description_length = 0;
2189 RCHECK(buffer->ReadWriteUInt32(&description_length));
2190 RCHECK(description_length >= kEntrySize);
2195 if (entries[i].key_id.size() != kCencKeyIdSize) {
2196 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
2197 <<
" bytes; got " << entries[i].key_id.size()
2198 <<
". Resized accordingly.";
2199 entries[i].key_id.resize(kCencKeyIdSize);
2203 uint8_t flag = entries[i].is_encrypted ? 1 : 0;
2205 buffer->ReadWriteUInt8(&flag) &&
2206 buffer->ReadWriteUInt8(&entries[i].iv_size) &&
2207 buffer->ReadWriteVector(&entries[i].key_id, kCencKeyIdSize));
2210 entries[i].is_encrypted = (flag != 0);
2211 if (entries[i].is_encrypted) {
2212 RCHECK(entries[i].iv_size == 8 || entries[i].iv_size == 16);
2214 RCHECK(entries[i].iv_size == 0);
2221 uint32_t SampleGroupDescription::ComputeSizeInternal() {
2225 if (entries.empty())
2227 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
2228 return HeaderSize() +
sizeof(grouping_type) +
2229 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
2230 entries.size() * kEntrySize;
2233 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2234 TrackFragment::~TrackFragment() {}
2237 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2242 DCHECK(buffer->
reader());
2244 if (!decode_time_absent)
2252 while (sample_to_group.grouping_type != FOURCC_SEIG &&
2256 while (sample_group_description.grouping_type != FOURCC_SEIG &&
2261 if (!decode_time_absent)
2263 for (uint32_t i = 0; i < runs.size(); ++i)
2273 uint32_t TrackFragment::ComputeSizeInternal() {
2279 for (uint32_t i = 0; i < runs.size(); ++i)
2284 MovieFragment::MovieFragment() {}
2285 MovieFragment::~MovieFragment() {}
2288 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2298 for (uint32_t i = 0; i < tracks.size(); ++i)
2300 for (uint32_t i = 0; i < pssh.size(); ++i)
2306 uint32_t MovieFragment::ComputeSizeInternal() {
2308 for (uint32_t i = 0; i < tracks.size(); ++i)
2310 for (uint32_t i = 0; i < pssh.size(); ++i)
2315 SegmentIndex::SegmentIndex()
2318 earliest_presentation_time(0),
2320 SegmentIndex::~SegmentIndex() {}
2323 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2325 buffer->ReadWriteUInt32(&reference_id) &&
2326 buffer->ReadWriteUInt32(×cale));
2328 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2333 uint16_t reference_count = references.size();
2335 buffer->ReadWriteUInt16(&reference_count));
2336 references.resize(reference_count);
2338 uint32_t reference_type_size;
2340 for (uint32_t i = 0; i < reference_count; ++i) {
2342 reference_type_size = references[i].referenced_size;
2343 if (references[i].reference_type)
2344 reference_type_size |= (1 << 31);
2345 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2346 if (references[i].starts_with_sap)
2349 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2350 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2351 buffer->ReadWriteUInt32(&sap));
2353 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2354 references[i].referenced_size = reference_type_size & ~(1 << 31);
2355 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2356 references[i].sap_type =
2357 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2358 references[i].sap_delta_time = sap & ~(0xF << 28);
2364 uint32_t SegmentIndex::ComputeSizeInternal() {
2365 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2366 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2367 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2368 3 *
sizeof(uint32_t) * references.size();
2371 MediaData::MediaData() : data_size(0) {}
2372 MediaData::~MediaData() {}
2375 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2376 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2380 uint32_t MediaData::ComputeSizeInternal() {
2384 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2385 CueSourceIDBox::~CueSourceIDBox() {}
2389 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2394 uint32_t CueSourceIDBox::ComputeSizeInternal() {
2395 if (source_id == kCueSourceIdNotSet)
2400 CueTimeBox::CueTimeBox() {}
2401 CueTimeBox::~CueTimeBox() {}
2407 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2414 uint32_t CueTimeBox::ComputeSizeInternal() {
2415 if (cue_current_time.empty())
2417 return HeaderSize() + cue_current_time.size();
2420 CueIDBox::CueIDBox() {}
2421 CueIDBox::~CueIDBox() {}
2427 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2433 uint32_t CueIDBox::ComputeSizeInternal() {
2439 CueSettingsBox::CueSettingsBox() {}
2440 CueSettingsBox::~CueSettingsBox() {}
2446 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2452 uint32_t CueSettingsBox::ComputeSizeInternal() {
2453 if (settings.empty())
2458 CuePayloadBox::CuePayloadBox() {}
2459 CuePayloadBox::~CuePayloadBox() {}
2465 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2471 uint32_t CuePayloadBox::ComputeSizeInternal() {
2475 VTTEmptyCueBox::VTTEmptyCueBox() {}
2476 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2482 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2486 uint32_t VTTEmptyCueBox::ComputeSizeInternal() {
2490 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2491 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2497 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2500 &cue_additional_text,
2504 uint32_t VTTAdditionalTextBox::ComputeSizeInternal() {
2505 return HeaderSize() + cue_additional_text.size();
2508 VTTCueBox::VTTCueBox() {}
2509 VTTCueBox::~VTTCueBox() {}
2515 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2526 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