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 bool IsFitIn32Bits(uint64_t a) {
68 return a <= std::numeric_limits<uint32_t>::max();
71 bool IsFitIn32Bits(int64_t a) {
72 return a <= std::numeric_limits<int32_t>::max() &&
73 a >= std::numeric_limits<int32_t>::min();
76 template <
typename T1,
typename T2>
77 bool IsFitIn32Bits(T1 a1, T2 a2) {
78 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
81 template <
typename T1,
typename T2,
typename T3>
82 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
83 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
88 namespace edash_packager {
92 FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
93 FileType::~FileType() {}
96 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
98 buffer->ReadWriteFourCC(&major_brand) &&
99 buffer->ReadWriteUInt32(&minor_version));
102 num_brands = (buffer->
Size() - buffer->
Pos()) /
sizeof(FourCC);
103 compatible_brands.resize(num_brands);
105 num_brands = compatible_brands.size();
107 for (
size_t i = 0; i < num_brands; ++i)
108 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
112 uint32_t FileType::ComputeSizeInternal() {
113 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
114 kFourCCSize * compatible_brands.size();
119 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
120 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
123 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
124 if (!buffer->
Reading() && !raw_box.empty()) {
126 buffer->
writer()->AppendVector(raw_box);
130 uint32_t size = data.size();
132 buffer->ReadWriteVector(&system_id, 16) &&
133 buffer->ReadWriteUInt32(&size) &&
134 buffer->ReadWriteVector(&data, size));
139 DCHECK(raw_box.empty());
140 BoxReader* reader = buffer->
reader();
142 raw_box.assign(reader->data(), reader->data() + reader->size());
147 uint32_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
148 if (!raw_box.empty()) {
149 return raw_box.size();
151 return HeaderSize() + system_id.size() +
sizeof(uint32_t) + data.size();
155 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
156 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
159 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
164 uint32_t count = offsets.size();
165 RCHECK(buffer->ReadWriteUInt32(&count));
166 offsets.resize(count);
168 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
169 for (uint32_t i = 0; i < count; ++i)
174 uint32_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
176 if (offsets.size() == 0)
178 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
179 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
182 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
183 : default_sample_info_size(0), sample_count(0) {}
184 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
187 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
192 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
193 buffer->ReadWriteUInt32(&sample_count));
194 if (default_sample_info_size == 0)
195 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
199 uint32_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
201 if (sample_count == 0)
203 return HeaderSize() +
sizeof(default_sample_info_size) +
204 sizeof(sample_count) +
205 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
208 SampleEncryptionEntry::SampleEncryptionEntry() {}
209 SampleEncryptionEntry::~SampleEncryptionEntry() {}
214 DCHECK(IsIvSizeValid(iv_size));
217 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
219 if (!has_subsamples) {
224 uint16_t subsample_count = subsamples.size();
225 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
226 RCHECK(subsample_count > 0);
227 subsamples.resize(subsample_count);
228 for (
auto& subsample : subsamples) {
229 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
230 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
238 DCHECK(IsIvSizeValid(iv_size));
241 initialization_vector.resize(iv_size);
242 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
244 if (!has_subsamples) {
249 uint16_t subsample_count;
250 RCHECK(reader->Read2(&subsample_count));
251 RCHECK(subsample_count > 0);
252 subsamples.resize(subsample_count);
253 for (
auto& subsample : subsamples) {
254 RCHECK(reader->Read2(&subsample.clear_bytes) &&
255 reader->Read4(&subsample.cipher_bytes));
261 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
262 const uint16_t subsample_count = subsamples.size();
263 return initialization_vector.size() +
264 (subsample_count > 0 ? (
sizeof(subsample_count) +
265 subsample_entry_size * subsample_count)
271 for (uint32_t i = 0; i < subsamples.size(); ++i)
272 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
276 SampleEncryption::SampleEncryption() : iv_size(0) {}
277 SampleEncryption::~SampleEncryption() {}
280 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
285 if (buffer->
Reading() && iv_size == 0) {
287 buffer->
Size() - buffer->
Pos()));
291 if (!IsIvSizeValid(iv_size)) {
292 LOG(ERROR) <<
"IV_size can only be 8 or 16, but seeing " << iv_size;
296 uint32_t sample_count = sample_encryption_entries.size();
297 RCHECK(buffer->ReadWriteUInt32(&sample_count));
299 sample_encryption_entries.resize(sample_count);
300 for (
auto& sample_encryption_entry : sample_encryption_entries) {
301 RCHECK(sample_encryption_entry.ReadWrite(
302 iv_size, flags & kUseSubsampleEncryption, buffer));
307 uint32_t SampleEncryption::ComputeSizeInternal() {
308 const uint32_t sample_count = sample_encryption_entries.size();
309 if (sample_count == 0) {
314 DCHECK(IsIvSizeValid(iv_size));
316 if (flags & kUseSubsampleEncryption) {
317 for (
const SampleEncryptionEntry& sample_encryption_entry :
318 sample_encryption_entries) {
319 box_size += sample_encryption_entry.ComputeSize();
322 box_size += sample_count * iv_size;
329 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
330 DCHECK(IsIvSizeValid(iv_size));
334 uint32_t sample_count = 0;
335 RCHECK(reader.Read4(&sample_count));
337 sample_encryption_entries->resize(sample_count);
338 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
339 RCHECK(sample_encryption_entry.ParseFromBuffer(
340 iv_size, flags & kUseSubsampleEncryption, &reader));
345 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
346 OriginalFormat::~OriginalFormat() {}
349 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
353 uint32_t OriginalFormat::ComputeSizeInternal() {
357 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
358 SchemeType::~SchemeType() {}
361 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
363 buffer->ReadWriteFourCC(&type) &&
364 buffer->ReadWriteUInt32(&version));
368 uint32_t SchemeType::ComputeSizeInternal() {
369 return HeaderSize() + kFourCCSize +
sizeof(version);
372 TrackEncryption::TrackEncryption()
373 : is_encrypted(false), default_iv_size(0), default_kid(16, 0) {}
374 TrackEncryption::~TrackEncryption() {}
377 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
379 if (default_kid.size() != kCencKeyIdSize) {
380 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
381 <<
" bytes; got " << default_kid.size()
382 <<
". Resized accordingly.";
383 default_kid.resize(kCencKeyIdSize);
387 uint8_t flag = is_encrypted ? 1 : 0;
390 buffer->ReadWriteUInt8(&flag) &&
391 buffer->ReadWriteUInt8(&default_iv_size) &&
392 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
394 is_encrypted = (flag != 0);
396 RCHECK(default_iv_size == 8 || default_iv_size == 16);
398 RCHECK(default_iv_size == 0);
404 uint32_t TrackEncryption::ComputeSizeInternal() {
405 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize;
408 SchemeInfo::SchemeInfo() {}
409 SchemeInfo::~SchemeInfo() {}
412 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
418 uint32_t SchemeInfo::ComputeSizeInternal() {
422 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
423 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
426 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
431 if (type.type == FOURCC_CENC)
440 uint32_t ProtectionSchemeInfo::ComputeSizeInternal() {
442 if (format.format == FOURCC_NULL)
448 MovieHeader::MovieHeader()
450 modification_time(0),
456 MovieHeader::~MovieHeader() {}
459 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
462 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
465 buffer->ReadWriteUInt32(×cale) &&
468 std::vector<uint8_t> matrix(kUnityMatrix,
469 kUnityMatrix + arraysize(kUnityMatrix));
470 RCHECK(buffer->ReadWriteInt32(&rate) &&
471 buffer->ReadWriteInt16(&volume) &&
473 buffer->ReadWriteVector(&matrix, matrix.size()) &&
475 buffer->ReadWriteUInt32(&next_track_id));
479 uint32_t MovieHeader::ComputeSizeInternal() {
480 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
481 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
482 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
483 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
487 TrackHeader::TrackHeader()
489 modification_time(0),
497 flags = kTrackEnabled | kTrackInMovie;
499 TrackHeader::~TrackHeader() {}
502 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
505 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
508 buffer->ReadWriteUInt32(&track_id) &&
515 volume = (width != 0 && height != 0) ? 0 : 0x100;
517 std::vector<uint8_t> matrix(kUnityMatrix,
518 kUnityMatrix + arraysize(kUnityMatrix));
520 buffer->ReadWriteInt16(&layer) &&
521 buffer->ReadWriteInt16(&alternate_group) &&
522 buffer->ReadWriteInt16(&volume) &&
524 buffer->ReadWriteVector(&matrix, matrix.size()) &&
525 buffer->ReadWriteUInt32(&width) &&
526 buffer->ReadWriteUInt32(&height));
530 uint32_t TrackHeader::ComputeSizeInternal() {
531 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
533 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
534 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
535 sizeof(height) +
sizeof(kUnityMatrix) + 14;
538 SampleDescription::SampleDescription() : type(kInvalid) {}
539 SampleDescription::~SampleDescription() {}
542 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
546 count = video_entries.size();
549 count = audio_entries.size();
552 count = wvtt_entries.size();
555 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
556 <<
" is not handled. Skipping.";
559 buffer->ReadWriteUInt32(&count));
562 BoxReader* reader = buffer->
reader();
564 video_entries.clear();
565 audio_entries.clear();
568 if (type == kVideo) {
569 RCHECK(reader->ReadAllChildren(&video_entries));
570 RCHECK(video_entries.size() == count);
571 }
else if (type == kAudio) {
572 RCHECK(reader->ReadAllChildren(&audio_entries));
573 RCHECK(audio_entries.size() == count);
574 }
else if (type == kText) {
575 RCHECK(reader->ReadAllChildren(&wvtt_entries));
576 RCHECK(wvtt_entries.size() == count);
579 DCHECK_LT(0u, count);
580 if (type == kVideo) {
581 for (uint32_t i = 0; i < count; ++i)
583 }
else if (type == kAudio) {
584 for (uint32_t i = 0; i < count; ++i)
586 }
else if (type == kText) {
587 for (uint32_t i = 0; i < count; ++i)
596 uint32_t SampleDescription::ComputeSizeInternal() {
597 uint32_t box_size =
HeaderSize() +
sizeof(uint32_t);
598 if (type == kVideo) {
599 for (uint32_t i = 0; i < video_entries.size(); ++i)
601 }
else if (type == kAudio) {
602 for (uint32_t i = 0; i < audio_entries.size(); ++i)
608 DecodingTimeToSample::DecodingTimeToSample() {}
609 DecodingTimeToSample::~DecodingTimeToSample() {}
612 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
613 uint32_t count = decoding_time.size();
615 buffer->ReadWriteUInt32(&count));
617 decoding_time.resize(count);
618 for (uint32_t i = 0; i < count; ++i) {
619 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
620 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
625 uint32_t DecodingTimeToSample::ComputeSizeInternal() {
627 sizeof(DecodingTime) * decoding_time.size();
630 CompositionTimeToSample::CompositionTimeToSample() {}
631 CompositionTimeToSample::~CompositionTimeToSample() {}
634 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
635 uint32_t count = composition_offset.size();
641 for (uint32_t i = 0; i < count; ++i) {
642 if (composition_offset[i].sample_offset < 0) {
650 buffer->ReadWriteUInt32(&count));
652 composition_offset.resize(count);
653 for (uint32_t i = 0; i < count; ++i) {
654 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
657 uint32_t sample_offset = composition_offset[i].sample_offset;
658 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
659 composition_offset[i].sample_offset = sample_offset;
661 int32_t sample_offset = composition_offset[i].sample_offset;
662 RCHECK(buffer->ReadWriteInt32(&sample_offset));
663 composition_offset[i].sample_offset = sample_offset;
669 uint32_t CompositionTimeToSample::ComputeSizeInternal() {
671 if (composition_offset.empty())
676 const uint32_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
678 kCompositionOffsetSize * composition_offset.size();
681 SampleToChunk::SampleToChunk() {}
682 SampleToChunk::~SampleToChunk() {}
685 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
686 uint32_t count = chunk_info.size();
688 buffer->ReadWriteUInt32(&count));
690 chunk_info.resize(count);
691 for (uint32_t i = 0; i < count; ++i) {
692 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
693 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
694 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
696 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
697 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
702 uint32_t SampleToChunk::ComputeSizeInternal() {
704 sizeof(ChunkInfo) * chunk_info.size();
707 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
708 SampleSize::~SampleSize() {}
711 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
713 buffer->ReadWriteUInt32(&sample_size) &&
714 buffer->ReadWriteUInt32(&sample_count));
716 if (sample_size == 0) {
718 sizes.resize(sample_count);
720 DCHECK(sample_count == sizes.size());
721 for (uint32_t i = 0; i < sample_count; ++i)
722 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
727 uint32_t SampleSize::ComputeSizeInternal() {
728 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
729 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
732 CompactSampleSize::CompactSampleSize() : field_size(0) {}
733 CompactSampleSize::~CompactSampleSize() {}
736 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
737 uint32_t sample_count = sizes.size();
740 buffer->ReadWriteUInt8(&field_size) &&
741 buffer->ReadWriteUInt32(&sample_count));
744 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
745 switch (field_size) {
747 for (uint32_t i = 0; i < sample_count; i += 2) {
750 RCHECK(buffer->ReadWriteUInt8(&size));
751 sizes[i] = size >> 4;
752 sizes[i + 1] = size & 0x0F;
754 DCHECK_LT(sizes[i], 16u);
755 DCHECK_LT(sizes[i + 1], 16u);
756 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
757 RCHECK(buffer->ReadWriteUInt8(&size));
762 for (uint32_t i = 0; i < sample_count; ++i) {
763 uint8_t size = sizes[i];
764 RCHECK(buffer->ReadWriteUInt8(&size));
769 for (uint32_t i = 0; i < sample_count; ++i) {
770 uint16_t size = sizes[i];
771 RCHECK(buffer->ReadWriteUInt16(&size));
778 sizes.resize(sample_count);
782 uint32_t CompactSampleSize::ComputeSizeInternal() {
783 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
784 (field_size * sizes.size() + 7) / 8;
787 ChunkOffset::ChunkOffset() {}
788 ChunkOffset::~ChunkOffset() {}
791 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
792 uint32_t count = offsets.size();
794 buffer->ReadWriteUInt32(&count));
796 offsets.resize(count);
797 for (uint32_t i = 0; i < count; ++i)
802 uint32_t ChunkOffset::ComputeSizeInternal() {
803 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
806 ChunkLargeOffset::ChunkLargeOffset() {}
807 ChunkLargeOffset::~ChunkLargeOffset() {}
810 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
811 uint32_t count = offsets.size();
815 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
817 stco.offsets.swap(offsets);
820 stco.offsets.swap(offsets);
826 buffer->ReadWriteUInt32(&count));
828 offsets.resize(count);
829 for (uint32_t i = 0; i < count; ++i)
830 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
834 uint32_t ChunkLargeOffset::ComputeSizeInternal() {
835 uint32_t count = offsets.size();
836 int use_large_offset =
837 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
839 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
842 SyncSample::SyncSample() {}
843 SyncSample::~SyncSample() {}
846 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
847 uint32_t count = sample_number.size();
849 buffer->ReadWriteUInt32(&count));
851 sample_number.resize(count);
852 for (uint32_t i = 0; i < count; ++i)
853 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
857 uint32_t SyncSample::ComputeSizeInternal() {
859 if (sample_number.empty())
862 sizeof(uint32_t) * sample_number.size();
865 SampleTable::SampleTable() {}
866 SampleTable::~SampleTable() {}
869 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
885 CompactSampleSize compact_sample_size;
886 RCHECK(reader->
ReadChild(&compact_sample_size));
887 sample_size.sample_size = 0;
888 sample_size.sample_count = compact_sample_size.sizes.size();
889 sample_size.sizes.swap(compact_sample_size.sizes);
893 if (reader->
ChildExist(&chunk_large_offset)) {
894 RCHECK(reader->
ReadChild(&chunk_large_offset));
896 ChunkOffset chunk_offset;
897 RCHECK(reader->
ReadChild(&chunk_offset));
898 chunk_large_offset.offsets.swap(chunk_offset.offsets);
908 uint32_t SampleTable::ComputeSizeInternal() {
916 EditList::EditList() {}
917 EditList::~EditList() {}
920 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
921 uint32_t count = edits.size();
925 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
926 for (uint32_t i = 0; i < count; ++i) {
929 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
930 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
931 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
936 uint32_t EditList::ComputeSizeInternal() {
942 for (uint32_t i = 0; i < edits.size(); ++i) {
943 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
949 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
957 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
963 uint32_t Edit::ComputeSizeInternal() {
965 if (list.edits.empty())
970 HandlerReference::HandlerReference() : type(kInvalid) {}
971 HandlerReference::~HandlerReference() {}
974 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
975 FourCC hdlr_type = FOURCC_NULL;
976 std::vector<uint8_t> handler_name;
978 if (type == kVideo) {
979 hdlr_type = FOURCC_VIDE;
980 handler_name.assign(kVideoHandlerName,
981 kVideoHandlerName + arraysize(kVideoHandlerName));
982 }
else if (type == kAudio) {
983 hdlr_type = FOURCC_SOUN;
984 handler_name.assign(kAudioHandlerName,
985 kAudioHandlerName + arraysize(kAudioHandlerName));
986 }
else if (type == kText) {
987 hdlr_type = FOURCC_TEXT;
988 handler_name.assign(kTextHandlerName,
989 kTextHandlerName + arraysize(kTextHandlerName));
997 buffer->ReadWriteFourCC(&hdlr_type));
1000 if (hdlr_type == FOURCC_VIDE) {
1002 }
else if (hdlr_type == FOURCC_SOUN) {
1009 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1014 uint32_t HandlerReference::ComputeSizeInternal() {
1015 uint32_t box_size =
HeaderSize() + kFourCCSize + 16;
1016 if (type == kVideo) {
1017 box_size +=
sizeof(kVideoHandlerName);
1018 }
else if (type == kAudio) {
1019 box_size +=
sizeof(kAudioHandlerName);
1021 box_size +=
sizeof(kTextHandlerName);
1026 CodecConfigurationRecord::CodecConfigurationRecord() : box_type(FOURCC_NULL) {}
1027 CodecConfigurationRecord::~CodecConfigurationRecord() {}
1034 bool CodecConfigurationRecord::ReadWriteInternal(
BoxBuffer* buffer) {
1037 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1039 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1044 uint32_t CodecConfigurationRecord::ComputeSizeInternal() {
1050 PixelAspectRatio::PixelAspectRatio() : h_spacing(0), v_spacing(0) {}
1051 PixelAspectRatio::~PixelAspectRatio() {}
1054 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1056 buffer->ReadWriteUInt32(&h_spacing) &&
1057 buffer->ReadWriteUInt32(&v_spacing));
1061 uint32_t PixelAspectRatio::ComputeSizeInternal() {
1063 if (h_spacing == 0 && v_spacing == 0)
1066 DCHECK(h_spacing != 0 && v_spacing != 0);
1067 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1070 VideoSampleEntry::VideoSampleEntry()
1071 : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
1073 VideoSampleEntry::~VideoSampleEntry() {}
1075 if (format == FOURCC_NULL) {
1076 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
1077 <<
"handler type recovered in its Media ancestor.";
1082 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1083 std::vector<uint8_t> compressor_name;
1085 DCHECK(buffer->
reader());
1086 format = buffer->
reader()->type();
1090 const FourCC actual_format = GetActualFormat();
1091 switch (actual_format) {
1093 compressor_name.assign(
1095 kAvcCompressorName + arraysize(kAvcCompressorName));
1099 compressor_name.assign(
1100 kHevcCompressorName,
1101 kHevcCompressorName + arraysize(kHevcCompressorName));
1106 compressor_name.assign(
1108 kVpcCompressorName + arraysize(kVpcCompressorName));
1111 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1114 compressor_name.resize(kCompressorNameSize);
1117 uint32_t video_resolution = kVideoResolution;
1118 uint16_t video_frame_count = kVideoFrameCount;
1119 uint16_t video_depth = kVideoDepth;
1120 int16_t predefined = -1;
1122 buffer->ReadWriteUInt16(&data_reference_index) &&
1124 buffer->ReadWriteUInt16(&width) &&
1125 buffer->ReadWriteUInt16(&height) &&
1126 buffer->ReadWriteUInt32(&video_resolution) &&
1127 buffer->ReadWriteUInt32(&video_resolution) &&
1129 buffer->ReadWriteUInt16(&video_frame_count) &&
1130 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1131 buffer->ReadWriteUInt16(&video_depth) &&
1132 buffer->ReadWriteInt16(&predefined));
1136 if (format == FOURCC_ENCV) {
1140 while (sinf.type.type != FOURCC_CENC) {
1149 const FourCC actual_format = GetActualFormat();
1150 switch (actual_format) {
1152 codec_config_record.box_type = FOURCC_AVCC;
1156 codec_config_record.box_type = FOURCC_HVCC;
1161 codec_config_record.box_type = FOURCC_VPCC;
1164 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1172 uint32_t VideoSampleEntry::ComputeSizeInternal() {
1173 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1174 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1175 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1177 codec_config_record.
ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1181 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1182 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1185 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1188 std::vector<uint8_t> data;
1189 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1190 RCHECK(es_descriptor.Parse(data));
1191 if (es_descriptor.
IsAAC()) {
1192 RCHECK(aac_audio_specific_config.
Parse(
1193 es_descriptor.decoder_specific_info()));
1196 DCHECK(buffer->
writer());
1197 es_descriptor.Write(buffer->
writer());
1202 uint32_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1204 if (es_descriptor.object_type() == kForbidden)
1206 return HeaderSize() + es_descriptor.ComputeSize();
1209 DTSSpecific::DTSSpecific()
1210 : sampling_frequency(0),
1213 pcm_sample_depth(0) {}
1214 DTSSpecific::~DTSSpecific() {}
1217 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1219 buffer->ReadWriteUInt32(&sampling_frequency) &&
1220 buffer->ReadWriteUInt32(&max_bitrate) &&
1221 buffer->ReadWriteUInt32(&avg_bitrate) &&
1222 buffer->ReadWriteUInt8(&pcm_sample_depth));
1225 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
Size() - buffer->
Pos()));
1227 if (extra_data.empty()) {
1228 extra_data.assign(kDdtsExtraData,
1229 kDdtsExtraData +
sizeof(kDdtsExtraData));
1231 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1236 uint32_t DTSSpecific::ComputeSizeInternal() {
1238 if (sampling_frequency == 0)
1240 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1241 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1242 sizeof(kDdtsExtraData);
1245 AudioSampleEntry::AudioSampleEntry()
1246 : format(FOURCC_NULL),
1247 data_reference_index(1),
1252 AudioSampleEntry::~AudioSampleEntry() {}
1255 if (format == FOURCC_NULL) {
1256 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1257 <<
"handler type recovered in its Media ancestor.";
1262 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1264 DCHECK(buffer->
reader());
1265 format = buffer->
reader()->type();
1273 buffer->ReadWriteUInt16(&data_reference_index) &&
1275 buffer->ReadWriteUInt16(&channelcount) &&
1276 buffer->ReadWriteUInt16(&samplesize) &&
1278 buffer->ReadWriteUInt32(&samplerate));
1283 if (format == FOURCC_ENCA) {
1287 while (sinf.type.type != FOURCC_CENC) {
1301 uint32_t AudioSampleEntry::ComputeSizeInternal() {
1302 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1303 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1309 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1310 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1316 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1323 uint32_t WebVTTConfigurationBox::ComputeSizeInternal() {
1327 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1328 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1334 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1338 : source_label.size());
1341 uint32_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1342 if (source_label.empty())
1347 WVTTSampleEntry::WVTTSampleEntry() {}
1348 WVTTSampleEntry::~WVTTSampleEntry() {}
1354 bool WVTTSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1358 buffer->ReadWriteUInt16(&data_reference_index) &&
1365 uint32_t WVTTSampleEntry::ComputeSizeInternal() {
1367 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1371 MediaHeader::MediaHeader()
1372 : creation_time(0), modification_time(0), timescale(0), duration(0) {
1375 MediaHeader::~MediaHeader() {}
1378 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1381 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1384 buffer->ReadWriteUInt32(×cale) &&
1390 std::vector<uint8_t> temp;
1391 RCHECK(buffer->ReadWriteVector(&temp, 2));
1394 bit_reader.SkipBits(1);
1395 for (
int i = 0; i < 3; ++i) {
1396 CHECK(bit_reader.ReadBits(5, &language[i]));
1397 language[i] += 0x60;
1402 const char kUndefinedLanguage[] =
"und";
1403 if (language[0] == 0)
1404 strcpy(language, kUndefinedLanguage);
1408 for (
int i = 0; i < 3; ++i)
1409 lang |= (language[i] - 0x60) << ((2 - i) * 5);
1410 RCHECK(buffer->ReadWriteUInt16(&lang));
1417 uint32_t MediaHeader::ComputeSizeInternal() {
1418 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1420 sizeof(uint32_t) * (1 + version) * 3 + 2 +
1424 VideoMediaHeader::VideoMediaHeader()
1425 : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1426 const uint32_t kVideoMediaHeaderFlags = 1;
1427 flags = kVideoMediaHeaderFlags;
1429 VideoMediaHeader::~VideoMediaHeader() {}
1431 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1433 buffer->ReadWriteUInt16(&graphicsmode) &&
1434 buffer->ReadWriteUInt16(&opcolor_red) &&
1435 buffer->ReadWriteUInt16(&opcolor_green) &&
1436 buffer->ReadWriteUInt16(&opcolor_blue));
1440 uint32_t VideoMediaHeader::ComputeSizeInternal() {
1441 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1442 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1445 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1446 SoundMediaHeader::~SoundMediaHeader() {}
1448 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1450 buffer->ReadWriteUInt16(&balance) &&
1455 uint32_t SoundMediaHeader::ComputeSizeInternal() {
1456 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1459 SubtitleMediaHeader::SubtitleMediaHeader() {}
1460 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1464 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1468 uint32_t SubtitleMediaHeader::ComputeSizeInternal() {
1472 DataEntryUrl::DataEntryUrl() {
1473 const uint32_t kDataEntryUrlFlags = 1;
1474 flags = kDataEntryUrlFlags;
1476 DataEntryUrl::~DataEntryUrl() {}
1478 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
1481 RCHECK(buffer->ReadWriteVector(&location, buffer->
Size() - buffer->
Pos()));
1483 RCHECK(buffer->ReadWriteVector(&location, location.size()));
1488 uint32_t DataEntryUrl::ComputeSizeInternal() {
1492 DataReference::DataReference() {
1494 data_entry.resize(1);
1496 DataReference::~DataReference() {}
1498 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
1499 uint32_t entry_count = data_entry.size();
1501 buffer->ReadWriteUInt32(&entry_count));
1502 data_entry.resize(entry_count);
1504 for (uint32_t i = 0; i < entry_count; ++i)
1509 uint32_t DataReference::ComputeSizeInternal() {
1510 uint32_t count = data_entry.size();
1511 uint32_t box_size =
HeaderSize() +
sizeof(count);
1512 for (uint32_t i = 0; i < count; ++i)
1517 DataInformation::DataInformation() {}
1518 DataInformation::~DataInformation() {}
1521 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1527 uint32_t DataInformation::ComputeSizeInternal() {
1531 MediaInformation::MediaInformation() {}
1532 MediaInformation::~MediaInformation() {}
1535 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1540 switch (sample_table.description.type) {
1557 uint32_t MediaInformation::ComputeSizeInternal() {
1560 switch (sample_table.description.type) {
1580 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
1592 information.sample_table.description.type = handler.type;
1594 DCHECK_EQ(information.sample_table.description.type, handler.type);
1600 uint32_t Media::ComputeSizeInternal() {
1609 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
1619 uint32_t Track::ComputeSizeInternal() {
1624 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
1625 MovieExtendsHeader::~MovieExtendsHeader() {}
1628 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1630 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1635 uint32_t MovieExtendsHeader::ComputeSizeInternal() {
1637 if (fragment_duration == 0)
1639 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
1640 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1643 TrackExtends::TrackExtends()
1645 default_sample_description_index(0),
1646 default_sample_duration(0),
1647 default_sample_size(0),
1648 default_sample_flags(0) {}
1649 TrackExtends::~TrackExtends() {}
1652 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1654 buffer->ReadWriteUInt32(&track_id) &&
1655 buffer->ReadWriteUInt32(&default_sample_description_index) &&
1656 buffer->ReadWriteUInt32(&default_sample_duration) &&
1657 buffer->ReadWriteUInt32(&default_sample_size) &&
1658 buffer->ReadWriteUInt32(&default_sample_flags));
1662 uint32_t TrackExtends::ComputeSizeInternal() {
1664 sizeof(default_sample_description_index) +
1665 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
1666 sizeof(default_sample_flags);
1669 MovieExtends::MovieExtends() {}
1670 MovieExtends::~MovieExtends() {}
1673 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1678 DCHECK(buffer->
reader());
1681 for (uint32_t i = 0; i < tracks.size(); ++i)
1687 uint32_t MovieExtends::ComputeSizeInternal() {
1689 if (tracks.size() == 0)
1692 for (uint32_t i = 0; i < tracks.size(); ++i)
1701 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
1712 for (uint32_t i = 0; i < tracks.size(); ++i)
1714 for (uint32_t i = 0; i < pssh.size(); ++i)
1720 uint32_t Movie::ComputeSizeInternal() {
1723 for (uint32_t i = 0; i < tracks.size(); ++i)
1725 for (uint32_t i = 0; i < pssh.size(); ++i)
1730 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
1731 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
1734 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
1736 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1741 uint32_t TrackFragmentDecodeTime::ComputeSizeInternal() {
1742 version = IsFitIn32Bits(decode_time) ? 0 : 1;
1743 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1746 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
1747 MovieFragmentHeader::~MovieFragmentHeader() {}
1750 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1752 buffer->ReadWriteUInt32(&sequence_number);
1755 uint32_t MovieFragmentHeader::ComputeSizeInternal() {
1756 return HeaderSize() +
sizeof(sequence_number);
1759 TrackFragmentHeader::TrackFragmentHeader()
1761 sample_description_index(0),
1762 default_sample_duration(0),
1763 default_sample_size(0),
1764 default_sample_flags(0) {}
1766 TrackFragmentHeader::~TrackFragmentHeader() {}
1769 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1771 buffer->ReadWriteUInt32(&track_id));
1773 if (flags & kBaseDataOffsetPresentMask) {
1778 uint64_t base_data_offset;
1779 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
1780 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
1781 "default-base-is-moof.";
1784 if (flags & kSampleDescriptionIndexPresentMask) {
1785 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
1786 }
else if (buffer->
Reading()) {
1787 sample_description_index = 0;
1790 if (flags & kDefaultSampleDurationPresentMask) {
1791 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
1792 }
else if (buffer->
Reading()) {
1793 default_sample_duration = 0;
1796 if (flags & kDefaultSampleSizePresentMask) {
1797 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
1798 }
else if (buffer->
Reading()) {
1799 default_sample_size = 0;
1802 if (flags & kDefaultSampleFlagsPresentMask)
1803 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
1807 uint32_t TrackFragmentHeader::ComputeSizeInternal() {
1808 uint32_t box_size =
HeaderSize() +
sizeof(track_id);
1809 if (flags & kSampleDescriptionIndexPresentMask)
1810 box_size +=
sizeof(sample_description_index);
1811 if (flags & kDefaultSampleDurationPresentMask)
1812 box_size +=
sizeof(default_sample_duration);
1813 if (flags & kDefaultSampleSizePresentMask)
1814 box_size +=
sizeof(default_sample_size);
1815 if (flags & kDefaultSampleFlagsPresentMask)
1816 box_size +=
sizeof(default_sample_flags);
1820 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
1821 TrackFragmentRun::~TrackFragmentRun() {}
1824 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
1830 if (flags & kSampleCompTimeOffsetsPresentMask) {
1831 for (uint32_t i = 0; i < sample_count; ++i) {
1832 if (sample_composition_time_offsets[i] < 0) {
1841 buffer->ReadWriteUInt32(&sample_count));
1843 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
1844 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
1845 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
1846 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
1847 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
1848 bool sample_composition_time_offsets_present =
1849 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
1851 if (data_offset_present) {
1852 RCHECK(buffer->ReadWriteUInt32(&data_offset));
1863 uint32_t first_sample_flags;
1866 if (first_sample_flags_present)
1867 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
1869 if (sample_duration_present)
1870 sample_durations.resize(sample_count);
1871 if (sample_size_present)
1872 sample_sizes.resize(sample_count);
1873 if (sample_flags_present)
1874 sample_flags.resize(sample_count);
1875 if (sample_composition_time_offsets_present)
1876 sample_composition_time_offsets.resize(sample_count);
1878 if (first_sample_flags_present) {
1879 first_sample_flags = sample_flags[0];
1880 DCHECK(sample_flags.size() == 1);
1881 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
1884 if (sample_duration_present)
1885 DCHECK(sample_durations.size() == sample_count);
1886 if (sample_size_present)
1887 DCHECK(sample_sizes.size() == sample_count);
1888 if (sample_flags_present)
1889 DCHECK(sample_flags.size() == sample_count);
1890 if (sample_composition_time_offsets_present)
1891 DCHECK(sample_composition_time_offsets.size() == sample_count);
1894 for (uint32_t i = 0; i < sample_count; ++i) {
1895 if (sample_duration_present)
1896 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
1897 if (sample_size_present)
1898 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
1899 if (sample_flags_present)
1900 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
1902 if (sample_composition_time_offsets_present) {
1904 uint32_t sample_offset = sample_composition_time_offsets[i];
1905 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
1906 sample_composition_time_offsets[i] = sample_offset;
1908 int32_t sample_offset = sample_composition_time_offsets[i];
1909 RCHECK(buffer->ReadWriteInt32(&sample_offset));
1910 sample_composition_time_offsets[i] = sample_offset;
1916 if (first_sample_flags_present) {
1917 if (sample_flags.size() == 0) {
1918 sample_flags.push_back(first_sample_flags);
1920 sample_flags[0] = first_sample_flags;
1927 uint32_t TrackFragmentRun::ComputeSizeInternal() {
1928 uint32_t box_size =
HeaderSize() +
sizeof(sample_count);
1929 if (flags & kDataOffsetPresentMask)
1930 box_size +=
sizeof(data_offset);
1931 if (flags & kFirstSampleFlagsPresentMask)
1932 box_size +=
sizeof(uint32_t);
1933 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
1934 (flags & kSampleSizePresentMask ? 1 : 0) +
1935 (flags & kSampleFlagsPresentMask ? 1 : 0) +
1936 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
1937 box_size += fields *
sizeof(uint32_t) * sample_count;
1941 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
1942 SampleToGroup::~SampleToGroup() {}
1945 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
1947 buffer->ReadWriteUInt32(&grouping_type));
1949 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
1951 if (grouping_type != FOURCC_SEIG) {
1953 DLOG(WARNING) <<
"Sample group "
1954 << FourCCToString(static_cast<FourCC>(grouping_type))
1955 <<
" is not supported.";
1959 uint32_t count = entries.size();
1960 RCHECK(buffer->ReadWriteUInt32(&count));
1961 entries.resize(count);
1962 for (uint32_t i = 0; i < count; ++i) {
1963 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
1964 buffer->ReadWriteUInt32(&entries[i].group_description_index));
1969 uint32_t SampleToGroup::ComputeSizeInternal() {
1971 if (entries.empty())
1973 return HeaderSize() +
sizeof(grouping_type) +
1974 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
1975 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
1978 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
1979 : is_encrypted(false), iv_size(0) {
1981 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
1983 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
1984 SampleGroupDescription::~SampleGroupDescription() {}
1987 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
1989 buffer->ReadWriteUInt32(&grouping_type));
1991 if (grouping_type != FOURCC_SEIG) {
1993 DLOG(WARNING) <<
"Sample group '" << grouping_type <<
"' is not supported.";
1997 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
1998 uint32_t default_length = 0;
2001 RCHECK(buffer->ReadWriteUInt32(&default_length));
2002 RCHECK(default_length == 0 || default_length >= kEntrySize);
2004 default_length = kEntrySize;
2005 RCHECK(buffer->ReadWriteUInt32(&default_length));
2009 uint32_t count = entries.size();
2010 RCHECK(buffer->ReadWriteUInt32(&count));
2011 entries.resize(count);
2012 for (uint32_t i = 0; i < count; ++i) {
2014 if (buffer->
Reading() && default_length == 0) {
2015 uint32_t description_length = 0;
2016 RCHECK(buffer->ReadWriteUInt32(&description_length));
2017 RCHECK(description_length >= kEntrySize);
2022 if (entries[i].key_id.size() != kCencKeyIdSize) {
2023 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
2024 <<
" bytes; got " << entries[i].key_id.size()
2025 <<
". Resized accordingly.";
2026 entries[i].key_id.resize(kCencKeyIdSize);
2030 uint8_t flag = entries[i].is_encrypted ? 1 : 0;
2032 buffer->ReadWriteUInt8(&flag) &&
2033 buffer->ReadWriteUInt8(&entries[i].iv_size) &&
2034 buffer->ReadWriteVector(&entries[i].key_id, kCencKeyIdSize));
2037 entries[i].is_encrypted = (flag != 0);
2038 if (entries[i].is_encrypted) {
2039 RCHECK(entries[i].iv_size == 8 || entries[i].iv_size == 16);
2041 RCHECK(entries[i].iv_size == 0);
2048 uint32_t SampleGroupDescription::ComputeSizeInternal() {
2052 if (entries.empty())
2054 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
2055 return HeaderSize() +
sizeof(grouping_type) +
2056 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
2057 entries.size() * kEntrySize;
2060 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2061 TrackFragment::~TrackFragment() {}
2064 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2069 DCHECK(buffer->
reader());
2071 if (!decode_time_absent)
2079 while (sample_to_group.grouping_type != FOURCC_SEIG &&
2083 while (sample_group_description.grouping_type != FOURCC_SEIG &&
2088 if (!decode_time_absent)
2090 for (uint32_t i = 0; i < runs.size(); ++i)
2100 uint32_t TrackFragment::ComputeSizeInternal() {
2106 for (uint32_t i = 0; i < runs.size(); ++i)
2111 MovieFragment::MovieFragment() {}
2112 MovieFragment::~MovieFragment() {}
2115 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2125 for (uint32_t i = 0; i < tracks.size(); ++i)
2127 for (uint32_t i = 0; i < pssh.size(); ++i)
2133 uint32_t MovieFragment::ComputeSizeInternal() {
2135 for (uint32_t i = 0; i < tracks.size(); ++i)
2137 for (uint32_t i = 0; i < pssh.size(); ++i)
2142 SegmentIndex::SegmentIndex()
2145 earliest_presentation_time(0),
2147 SegmentIndex::~SegmentIndex() {}
2150 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2152 buffer->ReadWriteUInt32(&reference_id) &&
2153 buffer->ReadWriteUInt32(×cale));
2155 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2160 uint16_t reference_count = references.size();
2162 buffer->ReadWriteUInt16(&reference_count));
2163 references.resize(reference_count);
2165 uint32_t reference_type_size;
2167 for (uint32_t i = 0; i < reference_count; ++i) {
2169 reference_type_size = references[i].referenced_size;
2170 if (references[i].reference_type)
2171 reference_type_size |= (1 << 31);
2172 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2173 if (references[i].starts_with_sap)
2176 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2177 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2178 buffer->ReadWriteUInt32(&sap));
2180 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2181 references[i].referenced_size = reference_type_size & ~(1 << 31);
2182 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2183 references[i].sap_type =
2184 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2185 references[i].sap_delta_time = sap & ~(0xF << 28);
2191 uint32_t SegmentIndex::ComputeSizeInternal() {
2192 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2193 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2194 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2195 3 *
sizeof(uint32_t) * references.size();
2198 MediaData::MediaData() : data_size(0) {}
2199 MediaData::~MediaData() {}
2202 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2203 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2207 uint32_t MediaData::ComputeSizeInternal() {
2211 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2212 CueSourceIDBox::~CueSourceIDBox() {}
2216 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2221 uint32_t CueSourceIDBox::ComputeSizeInternal() {
2222 if (source_id == kCueSourceIdNotSet)
2227 CueTimeBox::CueTimeBox() {}
2228 CueTimeBox::~CueTimeBox() {}
2234 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2241 uint32_t CueTimeBox::ComputeSizeInternal() {
2242 if (cue_current_time.empty())
2244 return HeaderSize() + cue_current_time.size();
2247 CueIDBox::CueIDBox() {}
2248 CueIDBox::~CueIDBox() {}
2254 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2260 uint32_t CueIDBox::ComputeSizeInternal() {
2266 CueSettingsBox::CueSettingsBox() {}
2267 CueSettingsBox::~CueSettingsBox() {}
2273 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2279 uint32_t CueSettingsBox::ComputeSizeInternal() {
2280 if (settings.empty())
2285 CuePayloadBox::CuePayloadBox() {}
2286 CuePayloadBox::~CuePayloadBox() {}
2292 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2298 uint32_t CuePayloadBox::ComputeSizeInternal() {
2302 VTTEmptyCueBox::VTTEmptyCueBox() {}
2303 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2309 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2313 uint32_t VTTEmptyCueBox::ComputeSizeInternal() {
2317 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2318 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2324 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2327 &cue_additional_text,
2331 uint32_t VTTAdditionalTextBox::ComputeSizeInternal() {
2332 return HeaderSize() + cue_additional_text.size();
2335 VTTCueBox::VTTCueBox() {}
2336 VTTCueBox::~VTTCueBox() {}
2342 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2353 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