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 num_brands = (buffer->
Size() - buffer->
Pos()) /
sizeof(FourCC);
138 compatible_brands.resize(num_brands);
140 num_brands = compatible_brands.size();
142 for (
size_t i = 0; i < num_brands; ++i)
143 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
147 uint32_t FileType::ComputeSizeInternal() {
148 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
149 kFourCCSize * compatible_brands.size();
154 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
155 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
158 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
159 if (!buffer->
Reading() && !raw_box.empty()) {
161 buffer->
writer()->AppendVector(raw_box);
165 uint32_t size = data.size();
167 buffer->ReadWriteVector(&system_id, 16) &&
168 buffer->ReadWriteUInt32(&size) &&
169 buffer->ReadWriteVector(&data, size));
174 DCHECK(raw_box.empty());
175 BoxReader* reader = buffer->
reader();
177 raw_box.assign(reader->data(), reader->data() + reader->size());
182 uint32_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
183 if (!raw_box.empty()) {
184 return raw_box.size();
186 return HeaderSize() + system_id.size() +
sizeof(uint32_t) + data.size();
190 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
191 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
194 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
199 uint32_t count = offsets.size();
200 RCHECK(buffer->ReadWriteUInt32(&count));
201 offsets.resize(count);
203 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
204 for (uint32_t i = 0; i < count; ++i)
209 uint32_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
211 if (offsets.size() == 0)
213 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
214 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
217 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
218 : default_sample_info_size(0), sample_count(0) {}
219 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
222 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
227 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
228 buffer->ReadWriteUInt32(&sample_count));
229 if (default_sample_info_size == 0)
230 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
234 uint32_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
236 if (sample_count == 0)
238 return HeaderSize() +
sizeof(default_sample_info_size) +
239 sizeof(sample_count) +
240 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
243 SampleEncryptionEntry::SampleEncryptionEntry() {}
244 SampleEncryptionEntry::~SampleEncryptionEntry() {}
249 DCHECK(IsIvSizeValid(iv_size));
252 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
254 if (!has_subsamples) {
259 uint16_t subsample_count = subsamples.size();
260 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
261 RCHECK(subsample_count > 0);
262 subsamples.resize(subsample_count);
263 for (
auto& subsample : subsamples) {
264 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
265 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
273 DCHECK(IsIvSizeValid(iv_size));
276 initialization_vector.resize(iv_size);
277 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
279 if (!has_subsamples) {
284 uint16_t subsample_count;
285 RCHECK(reader->Read2(&subsample_count));
286 RCHECK(subsample_count > 0);
287 subsamples.resize(subsample_count);
288 for (
auto& subsample : subsamples) {
289 RCHECK(reader->Read2(&subsample.clear_bytes) &&
290 reader->Read4(&subsample.cipher_bytes));
296 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
297 const uint16_t subsample_count = subsamples.size();
298 return initialization_vector.size() +
299 (subsample_count > 0 ? (
sizeof(subsample_count) +
300 subsample_entry_size * subsample_count)
306 for (uint32_t i = 0; i < subsamples.size(); ++i)
307 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
311 SampleEncryption::SampleEncryption() : iv_size(0) {}
312 SampleEncryption::~SampleEncryption() {}
315 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
320 if (buffer->
Reading() && iv_size == 0) {
322 buffer->
Size() - buffer->
Pos()));
326 if (!IsIvSizeValid(iv_size)) {
327 LOG(ERROR) <<
"IV_size can only be 8 or 16, but seeing " << iv_size;
331 uint32_t sample_count = sample_encryption_entries.size();
332 RCHECK(buffer->ReadWriteUInt32(&sample_count));
334 sample_encryption_entries.resize(sample_count);
335 for (
auto& sample_encryption_entry : sample_encryption_entries) {
336 RCHECK(sample_encryption_entry.ReadWrite(
337 iv_size, flags & kUseSubsampleEncryption, buffer));
342 uint32_t SampleEncryption::ComputeSizeInternal() {
343 const uint32_t sample_count = sample_encryption_entries.size();
344 if (sample_count == 0) {
349 DCHECK(IsIvSizeValid(iv_size));
351 if (flags & kUseSubsampleEncryption) {
352 for (
const SampleEncryptionEntry& sample_encryption_entry :
353 sample_encryption_entries) {
354 box_size += sample_encryption_entry.ComputeSize();
357 box_size += sample_count * iv_size;
364 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
365 DCHECK(IsIvSizeValid(iv_size));
369 uint32_t sample_count = 0;
370 RCHECK(reader.Read4(&sample_count));
372 sample_encryption_entries->resize(sample_count);
373 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
374 RCHECK(sample_encryption_entry.ParseFromBuffer(
375 iv_size, flags & kUseSubsampleEncryption, &reader));
380 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
381 OriginalFormat::~OriginalFormat() {}
384 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
388 uint32_t OriginalFormat::ComputeSizeInternal() {
392 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
393 SchemeType::~SchemeType() {}
396 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
398 buffer->ReadWriteFourCC(&type) &&
399 buffer->ReadWriteUInt32(&version));
403 uint32_t SchemeType::ComputeSizeInternal() {
404 return HeaderSize() + kFourCCSize +
sizeof(version);
407 TrackEncryption::TrackEncryption()
408 : is_encrypted(false), default_iv_size(0), default_kid(16, 0) {}
409 TrackEncryption::~TrackEncryption() {}
412 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
414 if (default_kid.size() != kCencKeyIdSize) {
415 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
416 <<
" bytes; got " << default_kid.size()
417 <<
". Resized accordingly.";
418 default_kid.resize(kCencKeyIdSize);
422 uint8_t flag = is_encrypted ? 1 : 0;
425 buffer->ReadWriteUInt8(&flag) &&
426 buffer->ReadWriteUInt8(&default_iv_size) &&
427 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
429 is_encrypted = (flag != 0);
431 RCHECK(default_iv_size == 8 || default_iv_size == 16);
433 RCHECK(default_iv_size == 0);
439 uint32_t TrackEncryption::ComputeSizeInternal() {
440 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize;
443 SchemeInfo::SchemeInfo() {}
444 SchemeInfo::~SchemeInfo() {}
447 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
453 uint32_t SchemeInfo::ComputeSizeInternal() {
457 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
458 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
461 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
466 if (type.type == FOURCC_CENC)
475 uint32_t ProtectionSchemeInfo::ComputeSizeInternal() {
477 if (format.format == FOURCC_NULL)
483 MovieHeader::MovieHeader()
485 modification_time(0),
491 MovieHeader::~MovieHeader() {}
494 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
497 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
500 buffer->ReadWriteUInt32(×cale) &&
503 std::vector<uint8_t> matrix(kUnityMatrix,
504 kUnityMatrix + arraysize(kUnityMatrix));
505 RCHECK(buffer->ReadWriteInt32(&rate) &&
506 buffer->ReadWriteInt16(&volume) &&
508 buffer->ReadWriteVector(&matrix, matrix.size()) &&
510 buffer->ReadWriteUInt32(&next_track_id));
514 uint32_t MovieHeader::ComputeSizeInternal() {
515 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
516 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
517 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
518 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
522 TrackHeader::TrackHeader()
524 modification_time(0),
532 flags = kTrackEnabled | kTrackInMovie;
534 TrackHeader::~TrackHeader() {}
537 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
540 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
543 buffer->ReadWriteUInt32(&track_id) &&
550 volume = (width != 0 && height != 0) ? 0 : 0x100;
552 std::vector<uint8_t> matrix(kUnityMatrix,
553 kUnityMatrix + arraysize(kUnityMatrix));
555 buffer->ReadWriteInt16(&layer) &&
556 buffer->ReadWriteInt16(&alternate_group) &&
557 buffer->ReadWriteInt16(&volume) &&
559 buffer->ReadWriteVector(&matrix, matrix.size()) &&
560 buffer->ReadWriteUInt32(&width) &&
561 buffer->ReadWriteUInt32(&height));
565 uint32_t TrackHeader::ComputeSizeInternal() {
566 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
568 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
569 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
570 sizeof(height) +
sizeof(kUnityMatrix) + 14;
573 SampleDescription::SampleDescription() : type(kInvalid) {}
574 SampleDescription::~SampleDescription() {}
577 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
581 count = video_entries.size();
584 count = audio_entries.size();
587 count = wvtt_entries.size();
590 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
591 <<
" is not handled. Skipping.";
594 buffer->ReadWriteUInt32(&count));
597 BoxReader* reader = buffer->
reader();
599 video_entries.clear();
600 audio_entries.clear();
603 if (type == kVideo) {
604 RCHECK(reader->ReadAllChildren(&video_entries));
605 RCHECK(video_entries.size() == count);
606 }
else if (type == kAudio) {
607 RCHECK(reader->ReadAllChildren(&audio_entries));
608 RCHECK(audio_entries.size() == count);
609 }
else if (type == kText) {
610 RCHECK(reader->ReadAllChildren(&wvtt_entries));
611 RCHECK(wvtt_entries.size() == count);
614 DCHECK_LT(0u, count);
615 if (type == kVideo) {
616 for (uint32_t i = 0; i < count; ++i)
618 }
else if (type == kAudio) {
619 for (uint32_t i = 0; i < count; ++i)
621 }
else if (type == kText) {
622 for (uint32_t i = 0; i < count; ++i)
631 uint32_t SampleDescription::ComputeSizeInternal() {
632 uint32_t box_size =
HeaderSize() +
sizeof(uint32_t);
633 if (type == kVideo) {
634 for (uint32_t i = 0; i < video_entries.size(); ++i)
636 }
else if (type == kAudio) {
637 for (uint32_t i = 0; i < audio_entries.size(); ++i)
643 DecodingTimeToSample::DecodingTimeToSample() {}
644 DecodingTimeToSample::~DecodingTimeToSample() {}
647 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
648 uint32_t count = decoding_time.size();
650 buffer->ReadWriteUInt32(&count));
652 decoding_time.resize(count);
653 for (uint32_t i = 0; i < count; ++i) {
654 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
655 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
660 uint32_t DecodingTimeToSample::ComputeSizeInternal() {
662 sizeof(DecodingTime) * decoding_time.size();
665 CompositionTimeToSample::CompositionTimeToSample() {}
666 CompositionTimeToSample::~CompositionTimeToSample() {}
669 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
670 uint32_t count = composition_offset.size();
676 for (uint32_t i = 0; i < count; ++i) {
677 if (composition_offset[i].sample_offset < 0) {
685 buffer->ReadWriteUInt32(&count));
687 composition_offset.resize(count);
688 for (uint32_t i = 0; i < count; ++i) {
689 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
692 uint32_t sample_offset = composition_offset[i].sample_offset;
693 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
694 composition_offset[i].sample_offset = sample_offset;
696 int32_t sample_offset = composition_offset[i].sample_offset;
697 RCHECK(buffer->ReadWriteInt32(&sample_offset));
698 composition_offset[i].sample_offset = sample_offset;
704 uint32_t CompositionTimeToSample::ComputeSizeInternal() {
706 if (composition_offset.empty())
711 const uint32_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
713 kCompositionOffsetSize * composition_offset.size();
716 SampleToChunk::SampleToChunk() {}
717 SampleToChunk::~SampleToChunk() {}
720 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
721 uint32_t count = chunk_info.size();
723 buffer->ReadWriteUInt32(&count));
725 chunk_info.resize(count);
726 for (uint32_t i = 0; i < count; ++i) {
727 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
728 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
729 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
731 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
732 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
737 uint32_t SampleToChunk::ComputeSizeInternal() {
739 sizeof(ChunkInfo) * chunk_info.size();
742 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
743 SampleSize::~SampleSize() {}
746 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
748 buffer->ReadWriteUInt32(&sample_size) &&
749 buffer->ReadWriteUInt32(&sample_count));
751 if (sample_size == 0) {
753 sizes.resize(sample_count);
755 DCHECK(sample_count == sizes.size());
756 for (uint32_t i = 0; i < sample_count; ++i)
757 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
762 uint32_t SampleSize::ComputeSizeInternal() {
763 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
764 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
767 CompactSampleSize::CompactSampleSize() : field_size(0) {}
768 CompactSampleSize::~CompactSampleSize() {}
771 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
772 uint32_t sample_count = sizes.size();
775 buffer->ReadWriteUInt8(&field_size) &&
776 buffer->ReadWriteUInt32(&sample_count));
779 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
780 switch (field_size) {
782 for (uint32_t i = 0; i < sample_count; i += 2) {
785 RCHECK(buffer->ReadWriteUInt8(&size));
786 sizes[i] = size >> 4;
787 sizes[i + 1] = size & 0x0F;
789 DCHECK_LT(sizes[i], 16u);
790 DCHECK_LT(sizes[i + 1], 16u);
791 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
792 RCHECK(buffer->ReadWriteUInt8(&size));
797 for (uint32_t i = 0; i < sample_count; ++i) {
798 uint8_t size = sizes[i];
799 RCHECK(buffer->ReadWriteUInt8(&size));
804 for (uint32_t i = 0; i < sample_count; ++i) {
805 uint16_t size = sizes[i];
806 RCHECK(buffer->ReadWriteUInt16(&size));
813 sizes.resize(sample_count);
817 uint32_t CompactSampleSize::ComputeSizeInternal() {
818 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
819 (field_size * sizes.size() + 7) / 8;
822 ChunkOffset::ChunkOffset() {}
823 ChunkOffset::~ChunkOffset() {}
826 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
827 uint32_t count = offsets.size();
829 buffer->ReadWriteUInt32(&count));
831 offsets.resize(count);
832 for (uint32_t i = 0; i < count; ++i)
837 uint32_t ChunkOffset::ComputeSizeInternal() {
838 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
841 ChunkLargeOffset::ChunkLargeOffset() {}
842 ChunkLargeOffset::~ChunkLargeOffset() {}
845 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
846 uint32_t count = offsets.size();
850 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
852 stco.offsets.swap(offsets);
855 stco.offsets.swap(offsets);
861 buffer->ReadWriteUInt32(&count));
863 offsets.resize(count);
864 for (uint32_t i = 0; i < count; ++i)
865 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
869 uint32_t ChunkLargeOffset::ComputeSizeInternal() {
870 uint32_t count = offsets.size();
871 int use_large_offset =
872 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
874 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
877 SyncSample::SyncSample() {}
878 SyncSample::~SyncSample() {}
881 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
882 uint32_t count = sample_number.size();
884 buffer->ReadWriteUInt32(&count));
886 sample_number.resize(count);
887 for (uint32_t i = 0; i < count; ++i)
888 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
892 uint32_t SyncSample::ComputeSizeInternal() {
894 if (sample_number.empty())
897 sizeof(uint32_t) * sample_number.size();
900 SampleTable::SampleTable() {}
901 SampleTable::~SampleTable() {}
904 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
920 CompactSampleSize compact_sample_size;
921 RCHECK(reader->
ReadChild(&compact_sample_size));
922 sample_size.sample_size = 0;
923 sample_size.sample_count = compact_sample_size.sizes.size();
924 sample_size.sizes.swap(compact_sample_size.sizes);
928 if (reader->
ChildExist(&chunk_large_offset)) {
929 RCHECK(reader->
ReadChild(&chunk_large_offset));
931 ChunkOffset chunk_offset;
932 RCHECK(reader->
ReadChild(&chunk_offset));
933 chunk_large_offset.offsets.swap(chunk_offset.offsets);
943 uint32_t SampleTable::ComputeSizeInternal() {
951 EditList::EditList() {}
952 EditList::~EditList() {}
955 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
956 uint32_t count = edits.size();
960 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
961 for (uint32_t i = 0; i < count; ++i) {
964 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
965 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
966 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
971 uint32_t EditList::ComputeSizeInternal() {
977 for (uint32_t i = 0; i < edits.size(); ++i) {
978 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
984 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
992 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
998 uint32_t Edit::ComputeSizeInternal() {
1000 if (list.edits.empty())
1005 HandlerReference::HandlerReference() : handler_type(FOURCC_NULL) {}
1006 HandlerReference::~HandlerReference() {}
1009 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1010 std::vector<uint8_t> handler_name;
1012 switch (handler_type) {
1014 handler_name.assign(kVideoHandlerName,
1015 kVideoHandlerName + arraysize(kVideoHandlerName));
1018 handler_name.assign(kAudioHandlerName,
1019 kAudioHandlerName + arraysize(kAudioHandlerName));
1022 handler_name.assign(kTextHandlerName,
1023 kTextHandlerName + arraysize(kTextHandlerName));
1034 buffer->ReadWriteFourCC(&handler_type));
1037 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1042 uint32_t HandlerReference::ComputeSizeInternal() {
1043 uint32_t box_size =
HeaderSize() + kFourCCSize + 16;
1044 switch (handler_type) {
1046 box_size +=
sizeof(kVideoHandlerName);
1049 box_size +=
sizeof(kAudioHandlerName);
1052 box_size +=
sizeof(kTextHandlerName);
1062 bool Language::ReadWrite(BoxBuffer* buffer) {
1063 if (buffer->Reading()) {
1066 std::vector<uint8_t> temp;
1067 RCHECK(buffer->ReadWriteVector(&temp, 2));
1069 BitReader bit_reader(&temp[0], 2);
1070 bit_reader.SkipBits(1);
1072 for (
int i = 0; i < 3; ++i) {
1073 CHECK(bit_reader.ReadBits(5, &language[i]));
1074 language[i] += 0x60;
1076 code.assign(language, 3);
1079 const char kUndefinedLanguage[] =
"und";
1081 code = kUndefinedLanguage;
1082 DCHECK_EQ(code.size(), 3u);
1086 for (
int i = 0; i < 3; ++i)
1087 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1088 RCHECK(buffer->ReadWriteUInt16(&lang));
1093 uint32_t Language::ComputeSize()
const {
1098 bool PrivFrame::ReadWrite(BoxBuffer* buffer) {
1099 FourCC fourcc = FOURCC_PRIV;
1100 RCHECK(buffer->ReadWriteFourCC(&fourcc));
1101 if (fourcc != FOURCC_PRIV) {
1102 VLOG(1) <<
"Skip unrecognized id3 frame during read: "
1103 << FourCCToString(fourcc);
1107 uint32_t frame_size = owner.size() + 1 + value.size();
1111 DCHECK_LT(frame_size, 0x7Fu);
1113 RCHECK(buffer->ReadWriteUInt32(&frame_size) &&
1114 buffer->ReadWriteUInt16(&flags));
1116 if (buffer->Reading()) {
1118 RCHECK(buffer->ReadWriteString(&str, frame_size));
1120 size_t pos = str.find(
'\0');
1121 RCHECK(pos < str.size());
1122 owner = str.substr(0, pos);
1123 value = str.substr(pos + 1);
1126 RCHECK(buffer->ReadWriteString(&owner, owner.size()) &&
1127 buffer->ReadWriteUInt8(&byte) &&
1128 buffer->ReadWriteString(&value, value.size()));
1133 uint32_t PrivFrame::ComputeSize()
const {
1134 if (owner.empty() && value.empty())
1136 const uint32_t kFourCCSize = 4;
1137 return kFourCCSize +
sizeof(uint32_t) +
sizeof(uint16_t) + owner.size() + 1 +
1146 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1148 language.ReadWrite(buffer));
1151 std::string id3v2_identifier = kID3v2Identifier;
1152 uint16_t version = kID3v2Version;
1158 DCHECK_LT(data_size, 0x7Fu);
1160 RCHECK(buffer->
ReadWriteString(&id3v2_identifier, id3v2_identifier.size()) &&
1161 buffer->ReadWriteUInt16(&version) &&
1162 buffer->ReadWriteUInt8(&flags) &&
1163 buffer->ReadWriteUInt32(&data_size));
1169 uint32_t ID3v2::ComputeSizeInternal() {
1172 return private_frame_size == 0 ? 0 :
HeaderSize() + language.ComputeSize() +
1177 Metadata::Metadata() {}
1178 Metadata::~Metadata() {}
1184 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1192 uint32_t Metadata::ComputeSizeInternal() {
1195 return id3v2_size == 0 ? 0
1199 CodecConfigurationRecord::CodecConfigurationRecord() : box_type(FOURCC_NULL) {}
1200 CodecConfigurationRecord::~CodecConfigurationRecord() {}
1207 bool CodecConfigurationRecord::ReadWriteInternal(
BoxBuffer* buffer) {
1210 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1212 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1217 uint32_t CodecConfigurationRecord::ComputeSizeInternal() {
1223 PixelAspectRatio::PixelAspectRatio() : h_spacing(0), v_spacing(0) {}
1224 PixelAspectRatio::~PixelAspectRatio() {}
1227 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1229 buffer->ReadWriteUInt32(&h_spacing) &&
1230 buffer->ReadWriteUInt32(&v_spacing));
1234 uint32_t PixelAspectRatio::ComputeSizeInternal() {
1236 if (h_spacing == 0 && v_spacing == 0)
1239 DCHECK(h_spacing != 0 && v_spacing != 0);
1240 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1243 VideoSampleEntry::VideoSampleEntry()
1244 : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
1246 VideoSampleEntry::~VideoSampleEntry() {}
1248 if (format == FOURCC_NULL) {
1249 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
1250 <<
"handler type recovered in its Media ancestor.";
1255 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1256 std::vector<uint8_t> compressor_name;
1258 DCHECK(buffer->
reader());
1259 format = buffer->
reader()->type();
1263 const FourCC actual_format = GetActualFormat();
1264 switch (actual_format) {
1266 compressor_name.assign(
1268 kAvcCompressorName + arraysize(kAvcCompressorName));
1272 compressor_name.assign(
1273 kHevcCompressorName,
1274 kHevcCompressorName + arraysize(kHevcCompressorName));
1279 compressor_name.assign(
1281 kVpcCompressorName + arraysize(kVpcCompressorName));
1284 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1287 compressor_name.resize(kCompressorNameSize);
1290 uint32_t video_resolution = kVideoResolution;
1291 uint16_t video_frame_count = kVideoFrameCount;
1292 uint16_t video_depth = kVideoDepth;
1293 int16_t predefined = -1;
1295 buffer->ReadWriteUInt16(&data_reference_index) &&
1297 buffer->ReadWriteUInt16(&width) &&
1298 buffer->ReadWriteUInt16(&height) &&
1299 buffer->ReadWriteUInt32(&video_resolution) &&
1300 buffer->ReadWriteUInt32(&video_resolution) &&
1302 buffer->ReadWriteUInt16(&video_frame_count) &&
1303 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1304 buffer->ReadWriteUInt16(&video_depth) &&
1305 buffer->ReadWriteInt16(&predefined));
1309 if (format == FOURCC_ENCV) {
1313 while (sinf.type.type != FOURCC_CENC) {
1322 const FourCC actual_format = GetActualFormat();
1323 switch (actual_format) {
1325 codec_config_record.box_type = FOURCC_AVCC;
1329 codec_config_record.box_type = FOURCC_HVCC;
1334 codec_config_record.box_type = FOURCC_VPCC;
1337 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1345 uint32_t VideoSampleEntry::ComputeSizeInternal() {
1346 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1347 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1348 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1350 codec_config_record.
ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1354 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1355 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1358 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1361 std::vector<uint8_t> data;
1362 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1363 RCHECK(es_descriptor.Parse(data));
1364 if (es_descriptor.
IsAAC()) {
1365 RCHECK(aac_audio_specific_config.
Parse(
1366 es_descriptor.decoder_specific_info()));
1369 DCHECK(buffer->
writer());
1370 es_descriptor.Write(buffer->
writer());
1375 uint32_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1377 if (es_descriptor.object_type() == kForbidden)
1379 return HeaderSize() + es_descriptor.ComputeSize();
1382 DTSSpecific::DTSSpecific()
1383 : sampling_frequency(0),
1386 pcm_sample_depth(0) {}
1387 DTSSpecific::~DTSSpecific() {}
1390 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1392 buffer->ReadWriteUInt32(&sampling_frequency) &&
1393 buffer->ReadWriteUInt32(&max_bitrate) &&
1394 buffer->ReadWriteUInt32(&avg_bitrate) &&
1395 buffer->ReadWriteUInt8(&pcm_sample_depth));
1398 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
Size() - buffer->
Pos()));
1400 if (extra_data.empty()) {
1401 extra_data.assign(kDdtsExtraData,
1402 kDdtsExtraData +
sizeof(kDdtsExtraData));
1404 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1409 uint32_t DTSSpecific::ComputeSizeInternal() {
1411 if (sampling_frequency == 0)
1413 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1414 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1415 sizeof(kDdtsExtraData);
1418 AudioSampleEntry::AudioSampleEntry()
1419 : format(FOURCC_NULL),
1420 data_reference_index(1),
1425 AudioSampleEntry::~AudioSampleEntry() {}
1428 if (format == FOURCC_NULL) {
1429 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1430 <<
"handler type recovered in its Media ancestor.";
1435 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1437 DCHECK(buffer->
reader());
1438 format = buffer->
reader()->type();
1446 buffer->ReadWriteUInt16(&data_reference_index) &&
1448 buffer->ReadWriteUInt16(&channelcount) &&
1449 buffer->ReadWriteUInt16(&samplesize) &&
1451 buffer->ReadWriteUInt32(&samplerate));
1456 if (format == FOURCC_ENCA) {
1460 while (sinf.type.type != FOURCC_CENC) {
1474 uint32_t AudioSampleEntry::ComputeSizeInternal() {
1475 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1476 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1482 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1483 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1489 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1496 uint32_t WebVTTConfigurationBox::ComputeSizeInternal() {
1500 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1501 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1507 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1511 : source_label.size());
1514 uint32_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1515 if (source_label.empty())
1520 WVTTSampleEntry::WVTTSampleEntry() {}
1521 WVTTSampleEntry::~WVTTSampleEntry() {}
1527 bool WVTTSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1531 buffer->ReadWriteUInt16(&data_reference_index) &&
1538 uint32_t WVTTSampleEntry::ComputeSizeInternal() {
1540 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1544 MediaHeader::MediaHeader()
1545 : creation_time(0), modification_time(0), timescale(0), duration(0) {}
1546 MediaHeader::~MediaHeader() {}
1549 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1552 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1555 buffer->ReadWriteUInt32(×cale) &&
1557 language.ReadWrite(buffer) &&
1562 uint32_t MediaHeader::ComputeSizeInternal() {
1563 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1565 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1569 VideoMediaHeader::VideoMediaHeader()
1570 : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1571 const uint32_t kVideoMediaHeaderFlags = 1;
1572 flags = kVideoMediaHeaderFlags;
1574 VideoMediaHeader::~VideoMediaHeader() {}
1576 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1578 buffer->ReadWriteUInt16(&graphicsmode) &&
1579 buffer->ReadWriteUInt16(&opcolor_red) &&
1580 buffer->ReadWriteUInt16(&opcolor_green) &&
1581 buffer->ReadWriteUInt16(&opcolor_blue));
1585 uint32_t VideoMediaHeader::ComputeSizeInternal() {
1586 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1587 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1590 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1591 SoundMediaHeader::~SoundMediaHeader() {}
1593 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1595 buffer->ReadWriteUInt16(&balance) &&
1600 uint32_t SoundMediaHeader::ComputeSizeInternal() {
1601 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1604 SubtitleMediaHeader::SubtitleMediaHeader() {}
1605 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1609 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1613 uint32_t SubtitleMediaHeader::ComputeSizeInternal() {
1617 DataEntryUrl::DataEntryUrl() {
1618 const uint32_t kDataEntryUrlFlags = 1;
1619 flags = kDataEntryUrlFlags;
1621 DataEntryUrl::~DataEntryUrl() {}
1623 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
1626 RCHECK(buffer->ReadWriteVector(&location, buffer->
Size() - buffer->
Pos()));
1628 RCHECK(buffer->ReadWriteVector(&location, location.size()));
1633 uint32_t DataEntryUrl::ComputeSizeInternal() {
1637 DataReference::DataReference() {
1639 data_entry.resize(1);
1641 DataReference::~DataReference() {}
1643 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
1644 uint32_t entry_count = data_entry.size();
1646 buffer->ReadWriteUInt32(&entry_count));
1647 data_entry.resize(entry_count);
1649 for (uint32_t i = 0; i < entry_count; ++i)
1654 uint32_t DataReference::ComputeSizeInternal() {
1655 uint32_t count = data_entry.size();
1656 uint32_t box_size =
HeaderSize() +
sizeof(count);
1657 for (uint32_t i = 0; i < count; ++i)
1662 DataInformation::DataInformation() {}
1663 DataInformation::~DataInformation() {}
1666 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1672 uint32_t DataInformation::ComputeSizeInternal() {
1676 MediaInformation::MediaInformation() {}
1677 MediaInformation::~MediaInformation() {}
1680 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1685 switch (sample_table.description.type) {
1702 uint32_t MediaInformation::ComputeSizeInternal() {
1705 switch (sample_table.description.type) {
1725 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
1737 information.sample_table.description.type =
1738 FourCCToTrackType(handler.handler_type);
1740 handler.handler_type =
1741 TrackTypeToFourCC(information.sample_table.description.type);
1742 RCHECK(handler.handler_type != FOURCC_NULL);
1749 uint32_t Media::ComputeSizeInternal() {
1750 handler.handler_type =
1751 TrackTypeToFourCC(information.sample_table.description.type);
1760 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
1770 uint32_t Track::ComputeSizeInternal() {
1775 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
1776 MovieExtendsHeader::~MovieExtendsHeader() {}
1779 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1781 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1786 uint32_t MovieExtendsHeader::ComputeSizeInternal() {
1788 if (fragment_duration == 0)
1790 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
1791 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1794 TrackExtends::TrackExtends()
1796 default_sample_description_index(0),
1797 default_sample_duration(0),
1798 default_sample_size(0),
1799 default_sample_flags(0) {}
1800 TrackExtends::~TrackExtends() {}
1803 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1805 buffer->ReadWriteUInt32(&track_id) &&
1806 buffer->ReadWriteUInt32(&default_sample_description_index) &&
1807 buffer->ReadWriteUInt32(&default_sample_duration) &&
1808 buffer->ReadWriteUInt32(&default_sample_size) &&
1809 buffer->ReadWriteUInt32(&default_sample_flags));
1813 uint32_t TrackExtends::ComputeSizeInternal() {
1815 sizeof(default_sample_description_index) +
1816 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
1817 sizeof(default_sample_flags);
1820 MovieExtends::MovieExtends() {}
1821 MovieExtends::~MovieExtends() {}
1824 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1829 DCHECK(buffer->
reader());
1832 for (uint32_t i = 0; i < tracks.size(); ++i)
1838 uint32_t MovieExtends::ComputeSizeInternal() {
1840 if (tracks.size() == 0)
1843 for (uint32_t i = 0; i < tracks.size(); ++i)
1852 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
1864 for (uint32_t i = 0; i < tracks.size(); ++i)
1866 for (uint32_t i = 0; i < pssh.size(); ++i)
1872 uint32_t Movie::ComputeSizeInternal() {
1875 for (uint32_t i = 0; i < tracks.size(); ++i)
1877 for (uint32_t i = 0; i < pssh.size(); ++i)
1882 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
1883 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
1886 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
1888 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1893 uint32_t TrackFragmentDecodeTime::ComputeSizeInternal() {
1894 version = IsFitIn32Bits(decode_time) ? 0 : 1;
1895 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1898 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
1899 MovieFragmentHeader::~MovieFragmentHeader() {}
1902 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1904 buffer->ReadWriteUInt32(&sequence_number);
1907 uint32_t MovieFragmentHeader::ComputeSizeInternal() {
1908 return HeaderSize() +
sizeof(sequence_number);
1911 TrackFragmentHeader::TrackFragmentHeader()
1913 sample_description_index(0),
1914 default_sample_duration(0),
1915 default_sample_size(0),
1916 default_sample_flags(0) {}
1918 TrackFragmentHeader::~TrackFragmentHeader() {}
1921 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1923 buffer->ReadWriteUInt32(&track_id));
1925 if (flags & kBaseDataOffsetPresentMask) {
1930 uint64_t base_data_offset;
1931 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
1932 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
1933 "default-base-is-moof.";
1936 if (flags & kSampleDescriptionIndexPresentMask) {
1937 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
1938 }
else if (buffer->
Reading()) {
1939 sample_description_index = 0;
1942 if (flags & kDefaultSampleDurationPresentMask) {
1943 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
1944 }
else if (buffer->
Reading()) {
1945 default_sample_duration = 0;
1948 if (flags & kDefaultSampleSizePresentMask) {
1949 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
1950 }
else if (buffer->
Reading()) {
1951 default_sample_size = 0;
1954 if (flags & kDefaultSampleFlagsPresentMask)
1955 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
1959 uint32_t TrackFragmentHeader::ComputeSizeInternal() {
1960 uint32_t box_size =
HeaderSize() +
sizeof(track_id);
1961 if (flags & kSampleDescriptionIndexPresentMask)
1962 box_size +=
sizeof(sample_description_index);
1963 if (flags & kDefaultSampleDurationPresentMask)
1964 box_size +=
sizeof(default_sample_duration);
1965 if (flags & kDefaultSampleSizePresentMask)
1966 box_size +=
sizeof(default_sample_size);
1967 if (flags & kDefaultSampleFlagsPresentMask)
1968 box_size +=
sizeof(default_sample_flags);
1972 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
1973 TrackFragmentRun::~TrackFragmentRun() {}
1976 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
1982 if (flags & kSampleCompTimeOffsetsPresentMask) {
1983 for (uint32_t i = 0; i < sample_count; ++i) {
1984 if (sample_composition_time_offsets[i] < 0) {
1993 buffer->ReadWriteUInt32(&sample_count));
1995 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
1996 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
1997 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
1998 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
1999 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2000 bool sample_composition_time_offsets_present =
2001 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2003 if (data_offset_present) {
2004 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2015 uint32_t first_sample_flags;
2018 if (first_sample_flags_present)
2019 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2021 if (sample_duration_present)
2022 sample_durations.resize(sample_count);
2023 if (sample_size_present)
2024 sample_sizes.resize(sample_count);
2025 if (sample_flags_present)
2026 sample_flags.resize(sample_count);
2027 if (sample_composition_time_offsets_present)
2028 sample_composition_time_offsets.resize(sample_count);
2030 if (first_sample_flags_present) {
2031 first_sample_flags = sample_flags[0];
2032 DCHECK(sample_flags.size() == 1);
2033 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2036 if (sample_duration_present)
2037 DCHECK(sample_durations.size() == sample_count);
2038 if (sample_size_present)
2039 DCHECK(sample_sizes.size() == sample_count);
2040 if (sample_flags_present)
2041 DCHECK(sample_flags.size() == sample_count);
2042 if (sample_composition_time_offsets_present)
2043 DCHECK(sample_composition_time_offsets.size() == sample_count);
2046 for (uint32_t i = 0; i < sample_count; ++i) {
2047 if (sample_duration_present)
2048 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2049 if (sample_size_present)
2050 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2051 if (sample_flags_present)
2052 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2054 if (sample_composition_time_offsets_present) {
2056 uint32_t sample_offset = sample_composition_time_offsets[i];
2057 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2058 sample_composition_time_offsets[i] = sample_offset;
2060 int32_t sample_offset = sample_composition_time_offsets[i];
2061 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2062 sample_composition_time_offsets[i] = sample_offset;
2068 if (first_sample_flags_present) {
2069 if (sample_flags.size() == 0) {
2070 sample_flags.push_back(first_sample_flags);
2072 sample_flags[0] = first_sample_flags;
2079 uint32_t TrackFragmentRun::ComputeSizeInternal() {
2080 uint32_t box_size =
HeaderSize() +
sizeof(sample_count);
2081 if (flags & kDataOffsetPresentMask)
2082 box_size +=
sizeof(data_offset);
2083 if (flags & kFirstSampleFlagsPresentMask)
2084 box_size +=
sizeof(uint32_t);
2085 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2086 (flags & kSampleSizePresentMask ? 1 : 0) +
2087 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2088 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2089 box_size += fields *
sizeof(uint32_t) * sample_count;
2093 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
2094 SampleToGroup::~SampleToGroup() {}
2097 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
2099 buffer->ReadWriteUInt32(&grouping_type));
2101 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
2103 if (grouping_type != FOURCC_SEIG) {
2105 DLOG(WARNING) <<
"Sample group "
2106 << FourCCToString(static_cast<FourCC>(grouping_type))
2107 <<
" is not supported.";
2111 uint32_t count = entries.size();
2112 RCHECK(buffer->ReadWriteUInt32(&count));
2113 entries.resize(count);
2114 for (uint32_t i = 0; i < count; ++i) {
2115 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
2116 buffer->ReadWriteUInt32(&entries[i].group_description_index));
2121 uint32_t SampleToGroup::ComputeSizeInternal() {
2123 if (entries.empty())
2125 return HeaderSize() +
sizeof(grouping_type) +
2126 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
2127 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
2130 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
2131 : is_encrypted(false), iv_size(0) {
2133 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
2135 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
2136 SampleGroupDescription::~SampleGroupDescription() {}
2139 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
2141 buffer->ReadWriteUInt32(&grouping_type));
2143 if (grouping_type != FOURCC_SEIG) {
2145 DLOG(WARNING) <<
"Sample group '" << grouping_type <<
"' is not supported.";
2149 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
2150 uint32_t default_length = 0;
2153 RCHECK(buffer->ReadWriteUInt32(&default_length));
2154 RCHECK(default_length == 0 || default_length >= kEntrySize);
2156 default_length = kEntrySize;
2157 RCHECK(buffer->ReadWriteUInt32(&default_length));
2161 uint32_t count = entries.size();
2162 RCHECK(buffer->ReadWriteUInt32(&count));
2163 entries.resize(count);
2164 for (uint32_t i = 0; i < count; ++i) {
2166 if (buffer->
Reading() && default_length == 0) {
2167 uint32_t description_length = 0;
2168 RCHECK(buffer->ReadWriteUInt32(&description_length));
2169 RCHECK(description_length >= kEntrySize);
2174 if (entries[i].key_id.size() != kCencKeyIdSize) {
2175 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
2176 <<
" bytes; got " << entries[i].key_id.size()
2177 <<
". Resized accordingly.";
2178 entries[i].key_id.resize(kCencKeyIdSize);
2182 uint8_t flag = entries[i].is_encrypted ? 1 : 0;
2184 buffer->ReadWriteUInt8(&flag) &&
2185 buffer->ReadWriteUInt8(&entries[i].iv_size) &&
2186 buffer->ReadWriteVector(&entries[i].key_id, kCencKeyIdSize));
2189 entries[i].is_encrypted = (flag != 0);
2190 if (entries[i].is_encrypted) {
2191 RCHECK(entries[i].iv_size == 8 || entries[i].iv_size == 16);
2193 RCHECK(entries[i].iv_size == 0);
2200 uint32_t SampleGroupDescription::ComputeSizeInternal() {
2204 if (entries.empty())
2206 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
2207 return HeaderSize() +
sizeof(grouping_type) +
2208 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
2209 entries.size() * kEntrySize;
2212 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2213 TrackFragment::~TrackFragment() {}
2216 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2221 DCHECK(buffer->
reader());
2223 if (!decode_time_absent)
2231 while (sample_to_group.grouping_type != FOURCC_SEIG &&
2235 while (sample_group_description.grouping_type != FOURCC_SEIG &&
2240 if (!decode_time_absent)
2242 for (uint32_t i = 0; i < runs.size(); ++i)
2252 uint32_t TrackFragment::ComputeSizeInternal() {
2258 for (uint32_t i = 0; i < runs.size(); ++i)
2263 MovieFragment::MovieFragment() {}
2264 MovieFragment::~MovieFragment() {}
2267 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2277 for (uint32_t i = 0; i < tracks.size(); ++i)
2279 for (uint32_t i = 0; i < pssh.size(); ++i)
2285 uint32_t MovieFragment::ComputeSizeInternal() {
2287 for (uint32_t i = 0; i < tracks.size(); ++i)
2289 for (uint32_t i = 0; i < pssh.size(); ++i)
2294 SegmentIndex::SegmentIndex()
2297 earliest_presentation_time(0),
2299 SegmentIndex::~SegmentIndex() {}
2302 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2304 buffer->ReadWriteUInt32(&reference_id) &&
2305 buffer->ReadWriteUInt32(×cale));
2307 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2312 uint16_t reference_count = references.size();
2314 buffer->ReadWriteUInt16(&reference_count));
2315 references.resize(reference_count);
2317 uint32_t reference_type_size;
2319 for (uint32_t i = 0; i < reference_count; ++i) {
2321 reference_type_size = references[i].referenced_size;
2322 if (references[i].reference_type)
2323 reference_type_size |= (1 << 31);
2324 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2325 if (references[i].starts_with_sap)
2328 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2329 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2330 buffer->ReadWriteUInt32(&sap));
2332 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2333 references[i].referenced_size = reference_type_size & ~(1 << 31);
2334 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2335 references[i].sap_type =
2336 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2337 references[i].sap_delta_time = sap & ~(0xF << 28);
2343 uint32_t SegmentIndex::ComputeSizeInternal() {
2344 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2345 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2346 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2347 3 *
sizeof(uint32_t) * references.size();
2350 MediaData::MediaData() : data_size(0) {}
2351 MediaData::~MediaData() {}
2354 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2355 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2359 uint32_t MediaData::ComputeSizeInternal() {
2363 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2364 CueSourceIDBox::~CueSourceIDBox() {}
2368 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2373 uint32_t CueSourceIDBox::ComputeSizeInternal() {
2374 if (source_id == kCueSourceIdNotSet)
2379 CueTimeBox::CueTimeBox() {}
2380 CueTimeBox::~CueTimeBox() {}
2386 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2393 uint32_t CueTimeBox::ComputeSizeInternal() {
2394 if (cue_current_time.empty())
2396 return HeaderSize() + cue_current_time.size();
2399 CueIDBox::CueIDBox() {}
2400 CueIDBox::~CueIDBox() {}
2406 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2412 uint32_t CueIDBox::ComputeSizeInternal() {
2418 CueSettingsBox::CueSettingsBox() {}
2419 CueSettingsBox::~CueSettingsBox() {}
2425 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2431 uint32_t CueSettingsBox::ComputeSizeInternal() {
2432 if (settings.empty())
2437 CuePayloadBox::CuePayloadBox() {}
2438 CuePayloadBox::~CuePayloadBox() {}
2444 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2450 uint32_t CuePayloadBox::ComputeSizeInternal() {
2454 VTTEmptyCueBox::VTTEmptyCueBox() {}
2455 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2461 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2465 uint32_t VTTEmptyCueBox::ComputeSizeInternal() {
2469 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2470 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2476 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2479 &cue_additional_text,
2483 uint32_t VTTAdditionalTextBox::ComputeSizeInternal() {
2484 return HeaderSize() + cue_additional_text.size();
2487 VTTCueBox::VTTCueBox() {}
2488 VTTCueBox::~VTTCueBox() {}
2494 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2505 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