5 #include "packager/media/formats/mp4/box_definitions.h"
9 #include "packager/base/logging.h"
10 #include "packager/media/base/bit_reader.h"
11 #include "packager/media/base/macros.h"
12 #include "packager/media/formats/mp4/box_buffer.h"
13 #include "packager/media/formats/mp4/rcheck.h"
16 const uint32_t kFourCCSize = 4;
19 const uint32_t kCencKeyIdSize = 16;
22 const uint8_t kUnityMatrix[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
24 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0};
27 const char kVideoHandlerName[] =
"VideoHandler";
28 const char kAudioHandlerName[] =
"SoundHandler";
29 const char kTextHandlerName[] =
"TextHandler";
32 const uint32_t kVideoResolution = 0x00480000;
33 const uint16_t kVideoFrameCount = 1;
34 const uint16_t kVideoDepth = 0x0018;
36 const uint32_t kCompressorNameSize = 32u;
37 const char kAvcCompressorName[] =
"\012AVC Coding";
38 const char kHevcCompressorName[] =
"\013HEVC Coding";
39 const char kVpcCompressorName[] =
"\012VPC Coding";
43 const int kCueSourceIdNotSet = -1;
47 bool IsIvSizeValid(
size_t iv_size) {
48 return iv_size == 8 || iv_size == 16;
65 const uint8_t kDdtsExtraData[] = {0xe4, 0x7c, 0, 4, 0, 0x0f, 0};
68 const uint32_t kID3v2HeaderSize = 10;
69 const char kID3v2Identifier[] =
"ID3";
70 const uint16_t kID3v2Version = 0x0400;
73 bool IsFitIn32Bits(uint64_t a) {
74 return a <= std::numeric_limits<uint32_t>::max();
77 bool IsFitIn32Bits(int64_t a) {
78 return a <= std::numeric_limits<int32_t>::max() &&
79 a >= std::numeric_limits<int32_t>::min();
82 template <
typename T1,
typename T2>
83 bool IsFitIn32Bits(T1 a1, T2 a2) {
84 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
87 template <
typename T1,
typename T2,
typename T3>
88 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
89 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
94 namespace edash_packager {
100 TrackType FourCCToTrackType(FourCC fourcc) {
113 FourCC TrackTypeToFourCC(TrackType track_type) {
114 switch (track_type) {
128 FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
129 FileType::~FileType() {}
132 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
134 buffer->ReadWriteFourCC(&major_brand) &&
135 buffer->ReadWriteUInt32(&minor_version));
138 RCHECK(buffer->
BytesLeft() %
sizeof(FourCC) == 0);
139 num_brands = buffer->
BytesLeft() /
sizeof(FourCC);
140 compatible_brands.resize(num_brands);
142 num_brands = compatible_brands.size();
144 for (
size_t i = 0; i < num_brands; ++i)
145 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
149 uint32_t FileType::ComputeSizeInternal() {
150 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
151 kFourCCSize * compatible_brands.size();
156 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
157 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
160 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
161 if (!buffer->
Reading() && !raw_box.empty()) {
163 buffer->
writer()->AppendVector(raw_box);
167 uint32_t size = data.size();
169 buffer->ReadWriteVector(&system_id, 16) &&
170 buffer->ReadWriteUInt32(&size) &&
171 buffer->ReadWriteVector(&data, size));
176 DCHECK(raw_box.empty());
177 BoxReader* reader = buffer->
reader();
179 raw_box.assign(reader->data(), reader->data() + reader->size());
184 uint32_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
185 if (!raw_box.empty()) {
186 return raw_box.size();
188 return HeaderSize() + system_id.size() +
sizeof(uint32_t) + data.size();
192 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
193 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
196 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
201 uint32_t count = offsets.size();
202 RCHECK(buffer->ReadWriteUInt32(&count));
203 offsets.resize(count);
205 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
206 for (uint32_t i = 0; i < count; ++i)
211 uint32_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
213 if (offsets.size() == 0)
215 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
216 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
219 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
220 : default_sample_info_size(0), sample_count(0) {}
221 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
224 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
229 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
230 buffer->ReadWriteUInt32(&sample_count));
231 if (default_sample_info_size == 0)
232 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
236 uint32_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
238 if (sample_count == 0)
240 return HeaderSize() +
sizeof(default_sample_info_size) +
241 sizeof(sample_count) +
242 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
245 SampleEncryptionEntry::SampleEncryptionEntry() {}
246 SampleEncryptionEntry::~SampleEncryptionEntry() {}
251 DCHECK(IsIvSizeValid(iv_size));
254 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
256 if (!has_subsamples) {
261 uint16_t subsample_count = subsamples.size();
262 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
263 RCHECK(subsample_count > 0);
264 subsamples.resize(subsample_count);
265 for (
auto& subsample : subsamples) {
266 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
267 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
275 DCHECK(IsIvSizeValid(iv_size));
278 initialization_vector.resize(iv_size);
279 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
281 if (!has_subsamples) {
286 uint16_t subsample_count;
287 RCHECK(reader->Read2(&subsample_count));
288 RCHECK(subsample_count > 0);
289 subsamples.resize(subsample_count);
290 for (
auto& subsample : subsamples) {
291 RCHECK(reader->Read2(&subsample.clear_bytes) &&
292 reader->Read4(&subsample.cipher_bytes));
298 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
299 const uint16_t subsample_count = subsamples.size();
300 return initialization_vector.size() +
301 (subsample_count > 0 ? (
sizeof(subsample_count) +
302 subsample_entry_size * subsample_count)
308 for (uint32_t i = 0; i < subsamples.size(); ++i)
309 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
313 SampleEncryption::SampleEncryption() : iv_size(0) {}
314 SampleEncryption::~SampleEncryption() {}
317 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
322 if (buffer->
Reading() && iv_size == 0) {
328 if (!IsIvSizeValid(iv_size)) {
329 LOG(ERROR) <<
"IV_size can only be 8 or 16, but seeing " << iv_size;
333 uint32_t sample_count = sample_encryption_entries.size();
334 RCHECK(buffer->ReadWriteUInt32(&sample_count));
336 sample_encryption_entries.resize(sample_count);
337 for (
auto& sample_encryption_entry : sample_encryption_entries) {
338 RCHECK(sample_encryption_entry.ReadWrite(
339 iv_size, flags & kUseSubsampleEncryption, buffer));
344 uint32_t SampleEncryption::ComputeSizeInternal() {
345 const uint32_t sample_count = sample_encryption_entries.size();
346 if (sample_count == 0) {
351 DCHECK(IsIvSizeValid(iv_size));
353 if (flags & kUseSubsampleEncryption) {
354 for (
const SampleEncryptionEntry& sample_encryption_entry :
355 sample_encryption_entries) {
356 box_size += sample_encryption_entry.ComputeSize();
359 box_size += sample_count * iv_size;
366 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
367 DCHECK(IsIvSizeValid(iv_size));
371 uint32_t sample_count = 0;
372 RCHECK(reader.Read4(&sample_count));
374 sample_encryption_entries->resize(sample_count);
375 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
376 RCHECK(sample_encryption_entry.ParseFromBuffer(
377 iv_size, flags & kUseSubsampleEncryption, &reader));
382 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
383 OriginalFormat::~OriginalFormat() {}
386 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
390 uint32_t OriginalFormat::ComputeSizeInternal() {
394 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
395 SchemeType::~SchemeType() {}
398 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
400 buffer->ReadWriteFourCC(&type) &&
401 buffer->ReadWriteUInt32(&version));
405 uint32_t SchemeType::ComputeSizeInternal() {
406 return HeaderSize() + kFourCCSize +
sizeof(version);
409 TrackEncryption::TrackEncryption()
410 : is_encrypted(false), default_iv_size(0), default_kid(16, 0) {}
411 TrackEncryption::~TrackEncryption() {}
414 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
416 if (default_kid.size() != kCencKeyIdSize) {
417 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
418 <<
" bytes; got " << default_kid.size()
419 <<
". Resized accordingly.";
420 default_kid.resize(kCencKeyIdSize);
424 uint8_t flag = is_encrypted ? 1 : 0;
427 buffer->ReadWriteUInt8(&flag) &&
428 buffer->ReadWriteUInt8(&default_iv_size) &&
429 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
431 is_encrypted = (flag != 0);
433 RCHECK(default_iv_size == 8 || default_iv_size == 16);
435 RCHECK(default_iv_size == 0);
441 uint32_t TrackEncryption::ComputeSizeInternal() {
442 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize;
445 SchemeInfo::SchemeInfo() {}
446 SchemeInfo::~SchemeInfo() {}
449 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
455 uint32_t SchemeInfo::ComputeSizeInternal() {
459 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
460 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
463 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
468 if (type.type == FOURCC_CENC)
477 uint32_t ProtectionSchemeInfo::ComputeSizeInternal() {
479 if (format.format == FOURCC_NULL)
485 MovieHeader::MovieHeader()
487 modification_time(0),
493 MovieHeader::~MovieHeader() {}
496 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
499 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
502 buffer->ReadWriteUInt32(×cale) &&
505 std::vector<uint8_t> matrix(kUnityMatrix,
506 kUnityMatrix + arraysize(kUnityMatrix));
507 RCHECK(buffer->ReadWriteInt32(&rate) &&
508 buffer->ReadWriteInt16(&volume) &&
510 buffer->ReadWriteVector(&matrix, matrix.size()) &&
512 buffer->ReadWriteUInt32(&next_track_id));
516 uint32_t MovieHeader::ComputeSizeInternal() {
517 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
518 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
519 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
520 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
524 TrackHeader::TrackHeader()
526 modification_time(0),
534 flags = kTrackEnabled | kTrackInMovie;
536 TrackHeader::~TrackHeader() {}
539 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
542 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
545 buffer->ReadWriteUInt32(&track_id) &&
552 volume = (width != 0 && height != 0) ? 0 : 0x100;
554 std::vector<uint8_t> matrix(kUnityMatrix,
555 kUnityMatrix + arraysize(kUnityMatrix));
557 buffer->ReadWriteInt16(&layer) &&
558 buffer->ReadWriteInt16(&alternate_group) &&
559 buffer->ReadWriteInt16(&volume) &&
561 buffer->ReadWriteVector(&matrix, matrix.size()) &&
562 buffer->ReadWriteUInt32(&width) &&
563 buffer->ReadWriteUInt32(&height));
567 uint32_t TrackHeader::ComputeSizeInternal() {
568 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
570 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
571 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
572 sizeof(height) +
sizeof(kUnityMatrix) + 14;
575 SampleDescription::SampleDescription() : type(kInvalid) {}
576 SampleDescription::~SampleDescription() {}
579 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
583 count = video_entries.size();
586 count = audio_entries.size();
589 count = text_entries.size();
592 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
593 <<
" is not handled. Skipping.";
596 buffer->ReadWriteUInt32(&count));
599 BoxReader* reader = buffer->
reader();
601 video_entries.clear();
602 audio_entries.clear();
605 if (type == kVideo) {
606 RCHECK(reader->ReadAllChildren(&video_entries));
607 RCHECK(video_entries.size() == count);
608 }
else if (type == kAudio) {
609 RCHECK(reader->ReadAllChildren(&audio_entries));
610 RCHECK(audio_entries.size() == count);
611 }
else if (type == kText) {
612 RCHECK(reader->ReadAllChildren(&text_entries));
613 RCHECK(text_entries.size() == count);
616 DCHECK_LT(0u, count);
617 if (type == kVideo) {
618 for (uint32_t i = 0; i < count; ++i)
620 }
else if (type == kAudio) {
621 for (uint32_t i = 0; i < count; ++i)
623 }
else if (type == kText) {
624 for (uint32_t i = 0; i < count; ++i)
633 uint32_t SampleDescription::ComputeSizeInternal() {
634 uint32_t box_size =
HeaderSize() +
sizeof(uint32_t);
635 if (type == kVideo) {
636 for (uint32_t i = 0; i < video_entries.size(); ++i)
638 }
else if (type == kAudio) {
639 for (uint32_t i = 0; i < audio_entries.size(); ++i)
641 }
else if (type == kText) {
642 for (uint32_t i = 0; i < text_entries.size(); ++i)
648 DecodingTimeToSample::DecodingTimeToSample() {}
649 DecodingTimeToSample::~DecodingTimeToSample() {}
652 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
653 uint32_t count = decoding_time.size();
655 buffer->ReadWriteUInt32(&count));
657 decoding_time.resize(count);
658 for (uint32_t i = 0; i < count; ++i) {
659 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
660 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
665 uint32_t DecodingTimeToSample::ComputeSizeInternal() {
667 sizeof(DecodingTime) * decoding_time.size();
670 CompositionTimeToSample::CompositionTimeToSample() {}
671 CompositionTimeToSample::~CompositionTimeToSample() {}
674 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
675 uint32_t count = composition_offset.size();
681 for (uint32_t i = 0; i < count; ++i) {
682 if (composition_offset[i].sample_offset < 0) {
690 buffer->ReadWriteUInt32(&count));
692 composition_offset.resize(count);
693 for (uint32_t i = 0; i < count; ++i) {
694 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
697 uint32_t sample_offset = composition_offset[i].sample_offset;
698 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
699 composition_offset[i].sample_offset = sample_offset;
701 int32_t sample_offset = composition_offset[i].sample_offset;
702 RCHECK(buffer->ReadWriteInt32(&sample_offset));
703 composition_offset[i].sample_offset = sample_offset;
709 uint32_t CompositionTimeToSample::ComputeSizeInternal() {
711 if (composition_offset.empty())
716 const uint32_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
718 kCompositionOffsetSize * composition_offset.size();
721 SampleToChunk::SampleToChunk() {}
722 SampleToChunk::~SampleToChunk() {}
725 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
726 uint32_t count = chunk_info.size();
728 buffer->ReadWriteUInt32(&count));
730 chunk_info.resize(count);
731 for (uint32_t i = 0; i < count; ++i) {
732 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
733 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
734 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
736 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
737 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
742 uint32_t SampleToChunk::ComputeSizeInternal() {
744 sizeof(ChunkInfo) * chunk_info.size();
747 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
748 SampleSize::~SampleSize() {}
751 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
753 buffer->ReadWriteUInt32(&sample_size) &&
754 buffer->ReadWriteUInt32(&sample_count));
756 if (sample_size == 0) {
758 sizes.resize(sample_count);
760 DCHECK(sample_count == sizes.size());
761 for (uint32_t i = 0; i < sample_count; ++i)
762 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
767 uint32_t SampleSize::ComputeSizeInternal() {
768 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
769 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
772 CompactSampleSize::CompactSampleSize() : field_size(0) {}
773 CompactSampleSize::~CompactSampleSize() {}
776 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
777 uint32_t sample_count = sizes.size();
780 buffer->ReadWriteUInt8(&field_size) &&
781 buffer->ReadWriteUInt32(&sample_count));
784 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
785 switch (field_size) {
787 for (uint32_t i = 0; i < sample_count; i += 2) {
790 RCHECK(buffer->ReadWriteUInt8(&size));
791 sizes[i] = size >> 4;
792 sizes[i + 1] = size & 0x0F;
794 DCHECK_LT(sizes[i], 16u);
795 DCHECK_LT(sizes[i + 1], 16u);
796 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
797 RCHECK(buffer->ReadWriteUInt8(&size));
802 for (uint32_t i = 0; i < sample_count; ++i) {
803 uint8_t size = sizes[i];
804 RCHECK(buffer->ReadWriteUInt8(&size));
809 for (uint32_t i = 0; i < sample_count; ++i) {
810 uint16_t size = sizes[i];
811 RCHECK(buffer->ReadWriteUInt16(&size));
818 sizes.resize(sample_count);
822 uint32_t CompactSampleSize::ComputeSizeInternal() {
823 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
824 (field_size * sizes.size() + 7) / 8;
827 ChunkOffset::ChunkOffset() {}
828 ChunkOffset::~ChunkOffset() {}
831 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
832 uint32_t count = offsets.size();
834 buffer->ReadWriteUInt32(&count));
836 offsets.resize(count);
837 for (uint32_t i = 0; i < count; ++i)
842 uint32_t ChunkOffset::ComputeSizeInternal() {
843 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
846 ChunkLargeOffset::ChunkLargeOffset() {}
847 ChunkLargeOffset::~ChunkLargeOffset() {}
850 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
851 uint32_t count = offsets.size();
855 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
857 stco.offsets.swap(offsets);
860 stco.offsets.swap(offsets);
866 buffer->ReadWriteUInt32(&count));
868 offsets.resize(count);
869 for (uint32_t i = 0; i < count; ++i)
870 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
874 uint32_t ChunkLargeOffset::ComputeSizeInternal() {
875 uint32_t count = offsets.size();
876 int use_large_offset =
877 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
879 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
882 SyncSample::SyncSample() {}
883 SyncSample::~SyncSample() {}
886 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
887 uint32_t count = sample_number.size();
889 buffer->ReadWriteUInt32(&count));
891 sample_number.resize(count);
892 for (uint32_t i = 0; i < count; ++i)
893 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
897 uint32_t SyncSample::ComputeSizeInternal() {
899 if (sample_number.empty())
902 sizeof(uint32_t) * sample_number.size();
905 SampleTable::SampleTable() {}
906 SampleTable::~SampleTable() {}
909 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
925 CompactSampleSize compact_sample_size;
926 RCHECK(reader->
ReadChild(&compact_sample_size));
927 sample_size.sample_size = 0;
928 sample_size.sample_count = compact_sample_size.sizes.size();
929 sample_size.sizes.swap(compact_sample_size.sizes);
933 if (reader->
ChildExist(&chunk_large_offset)) {
934 RCHECK(reader->
ReadChild(&chunk_large_offset));
936 ChunkOffset chunk_offset;
937 RCHECK(reader->
ReadChild(&chunk_offset));
938 chunk_large_offset.offsets.swap(chunk_offset.offsets);
948 uint32_t SampleTable::ComputeSizeInternal() {
956 EditList::EditList() {}
957 EditList::~EditList() {}
960 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
961 uint32_t count = edits.size();
965 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
966 for (uint32_t i = 0; i < count; ++i) {
969 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
970 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
971 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
976 uint32_t EditList::ComputeSizeInternal() {
982 for (uint32_t i = 0; i < edits.size(); ++i) {
983 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
989 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
997 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
1003 uint32_t Edit::ComputeSizeInternal() {
1005 if (list.edits.empty())
1010 HandlerReference::HandlerReference() : handler_type(FOURCC_NULL) {}
1011 HandlerReference::~HandlerReference() {}
1014 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1015 std::vector<uint8_t> handler_name;
1017 switch (handler_type) {
1019 handler_name.assign(kVideoHandlerName,
1020 kVideoHandlerName + arraysize(kVideoHandlerName));
1023 handler_name.assign(kAudioHandlerName,
1024 kAudioHandlerName + arraysize(kAudioHandlerName));
1027 handler_name.assign(kTextHandlerName,
1028 kTextHandlerName + arraysize(kTextHandlerName));
1039 buffer->ReadWriteFourCC(&handler_type));
1042 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1047 uint32_t HandlerReference::ComputeSizeInternal() {
1048 uint32_t box_size =
HeaderSize() + kFourCCSize + 16;
1049 switch (handler_type) {
1051 box_size +=
sizeof(kVideoHandlerName);
1054 box_size +=
sizeof(kAudioHandlerName);
1057 box_size +=
sizeof(kTextHandlerName);
1067 bool Language::ReadWrite(BoxBuffer* buffer) {
1068 if (buffer->Reading()) {
1071 std::vector<uint8_t> temp;
1072 RCHECK(buffer->ReadWriteVector(&temp, 2));
1074 BitReader bit_reader(&temp[0], 2);
1075 bit_reader.SkipBits(1);
1077 for (
int i = 0; i < 3; ++i) {
1078 CHECK(bit_reader.ReadBits(5, &language[i]));
1079 language[i] += 0x60;
1081 code.assign(language, 3);
1084 const char kUndefinedLanguage[] =
"und";
1086 code = kUndefinedLanguage;
1087 DCHECK_EQ(code.size(), 3u);
1091 for (
int i = 0; i < 3; ++i)
1092 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1093 RCHECK(buffer->ReadWriteUInt16(&lang));
1098 uint32_t Language::ComputeSize()
const {
1103 bool PrivFrame::ReadWrite(BoxBuffer* buffer) {
1104 FourCC fourcc = FOURCC_PRIV;
1105 RCHECK(buffer->ReadWriteFourCC(&fourcc));
1106 if (fourcc != FOURCC_PRIV) {
1107 VLOG(1) <<
"Skip unrecognized id3 frame during read: "
1108 << FourCCToString(fourcc);
1112 uint32_t frame_size = owner.size() + 1 + value.size();
1116 DCHECK_LT(frame_size, 0x7Fu);
1118 RCHECK(buffer->ReadWriteUInt32(&frame_size) &&
1119 buffer->ReadWriteUInt16(&flags));
1121 if (buffer->Reading()) {
1123 RCHECK(buffer->ReadWriteString(&str, frame_size));
1125 size_t pos = str.find(
'\0');
1126 RCHECK(pos < str.size());
1127 owner = str.substr(0, pos);
1128 value = str.substr(pos + 1);
1131 RCHECK(buffer->ReadWriteString(&owner, owner.size()) &&
1132 buffer->ReadWriteUInt8(&byte) &&
1133 buffer->ReadWriteString(&value, value.size()));
1138 uint32_t PrivFrame::ComputeSize()
const {
1139 if (owner.empty() && value.empty())
1141 const uint32_t kFourCCSize = 4;
1142 return kFourCCSize +
sizeof(uint32_t) +
sizeof(uint16_t) + owner.size() + 1 +
1151 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1153 language.ReadWrite(buffer));
1156 std::string id3v2_identifier = kID3v2Identifier;
1157 uint16_t version = kID3v2Version;
1163 DCHECK_LT(data_size, 0x7Fu);
1165 RCHECK(buffer->
ReadWriteString(&id3v2_identifier, id3v2_identifier.size()) &&
1166 buffer->ReadWriteUInt16(&version) &&
1167 buffer->ReadWriteUInt8(&flags) &&
1168 buffer->ReadWriteUInt32(&data_size));
1174 uint32_t ID3v2::ComputeSizeInternal() {
1177 return private_frame_size == 0 ? 0 :
HeaderSize() + language.ComputeSize() +
1182 Metadata::Metadata() {}
1183 Metadata::~Metadata() {}
1189 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1197 uint32_t Metadata::ComputeSizeInternal() {
1200 return id3v2_size == 0 ? 0
1204 CodecConfigurationRecord::CodecConfigurationRecord() : box_type(FOURCC_NULL) {}
1205 CodecConfigurationRecord::~CodecConfigurationRecord() {}
1212 bool CodecConfigurationRecord::ReadWriteInternal(
BoxBuffer* buffer) {
1215 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1217 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1222 uint32_t CodecConfigurationRecord::ComputeSizeInternal() {
1228 PixelAspectRatio::PixelAspectRatio() : h_spacing(0), v_spacing(0) {}
1229 PixelAspectRatio::~PixelAspectRatio() {}
1232 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1234 buffer->ReadWriteUInt32(&h_spacing) &&
1235 buffer->ReadWriteUInt32(&v_spacing));
1239 uint32_t PixelAspectRatio::ComputeSizeInternal() {
1241 if (h_spacing == 0 && v_spacing == 0)
1244 DCHECK(h_spacing != 0 && v_spacing != 0);
1245 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1248 VideoSampleEntry::VideoSampleEntry()
1249 : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
1251 VideoSampleEntry::~VideoSampleEntry() {}
1253 if (format == FOURCC_NULL) {
1254 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
1255 <<
"handler type recovered in its Media ancestor.";
1260 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1261 std::vector<uint8_t> compressor_name;
1263 DCHECK(buffer->
reader());
1264 format = buffer->
reader()->type();
1268 const FourCC actual_format = GetActualFormat();
1269 switch (actual_format) {
1271 compressor_name.assign(
1273 kAvcCompressorName + arraysize(kAvcCompressorName));
1277 compressor_name.assign(
1278 kHevcCompressorName,
1279 kHevcCompressorName + arraysize(kHevcCompressorName));
1284 compressor_name.assign(
1286 kVpcCompressorName + arraysize(kVpcCompressorName));
1289 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1292 compressor_name.resize(kCompressorNameSize);
1295 uint32_t video_resolution = kVideoResolution;
1296 uint16_t video_frame_count = kVideoFrameCount;
1297 uint16_t video_depth = kVideoDepth;
1298 int16_t predefined = -1;
1300 buffer->ReadWriteUInt16(&data_reference_index) &&
1302 buffer->ReadWriteUInt16(&width) &&
1303 buffer->ReadWriteUInt16(&height) &&
1304 buffer->ReadWriteUInt32(&video_resolution) &&
1305 buffer->ReadWriteUInt32(&video_resolution) &&
1307 buffer->ReadWriteUInt16(&video_frame_count) &&
1308 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1309 buffer->ReadWriteUInt16(&video_depth) &&
1310 buffer->ReadWriteInt16(&predefined));
1314 if (format == FOURCC_ENCV) {
1318 while (sinf.type.type != FOURCC_CENC) {
1327 const FourCC actual_format = GetActualFormat();
1328 switch (actual_format) {
1330 codec_config_record.box_type = FOURCC_AVCC;
1334 codec_config_record.box_type = FOURCC_HVCC;
1339 codec_config_record.box_type = FOURCC_VPCC;
1342 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1350 uint32_t VideoSampleEntry::ComputeSizeInternal() {
1351 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1352 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1353 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1355 codec_config_record.
ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1359 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1360 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1363 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1366 std::vector<uint8_t> data;
1367 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1368 RCHECK(es_descriptor.Parse(data));
1369 if (es_descriptor.
IsAAC()) {
1370 RCHECK(aac_audio_specific_config.
Parse(
1371 es_descriptor.decoder_specific_info()));
1374 DCHECK(buffer->
writer());
1375 es_descriptor.Write(buffer->
writer());
1380 uint32_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1382 if (es_descriptor.object_type() == kForbidden)
1384 return HeaderSize() + es_descriptor.ComputeSize();
1387 DTSSpecific::DTSSpecific()
1388 : sampling_frequency(0),
1391 pcm_sample_depth(0) {}
1392 DTSSpecific::~DTSSpecific() {}
1395 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1397 buffer->ReadWriteUInt32(&sampling_frequency) &&
1398 buffer->ReadWriteUInt32(&max_bitrate) &&
1399 buffer->ReadWriteUInt32(&avg_bitrate) &&
1400 buffer->ReadWriteUInt8(&pcm_sample_depth));
1403 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
BytesLeft()));
1405 if (extra_data.empty()) {
1406 extra_data.assign(kDdtsExtraData,
1407 kDdtsExtraData +
sizeof(kDdtsExtraData));
1409 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1414 uint32_t DTSSpecific::ComputeSizeInternal() {
1416 if (sampling_frequency == 0)
1418 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1419 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1420 sizeof(kDdtsExtraData);
1423 AC3Specific::AC3Specific() {}
1424 AC3Specific::~AC3Specific() {}
1428 bool AC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1430 buffer->ReadWriteVector(
1435 uint32_t AC3Specific::ComputeSizeInternal() {
1442 EC3Specific::EC3Specific() {}
1443 EC3Specific::~EC3Specific() {}
1447 bool EC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1450 RCHECK(buffer->ReadWriteVector(&data, size));
1454 uint32_t EC3Specific::ComputeSizeInternal() {
1461 AudioSampleEntry::AudioSampleEntry()
1462 : format(FOURCC_NULL),
1463 data_reference_index(1),
1468 AudioSampleEntry::~AudioSampleEntry() {}
1471 if (format == FOURCC_NULL) {
1472 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1473 <<
"handler type recovered in its Media ancestor.";
1478 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1480 DCHECK(buffer->
reader());
1481 format = buffer->
reader()->type();
1489 buffer->ReadWriteUInt16(&data_reference_index) &&
1491 buffer->ReadWriteUInt16(&channelcount) &&
1492 buffer->ReadWriteUInt16(&samplesize) &&
1494 buffer->ReadWriteUInt32(&samplerate));
1499 if (format == FOURCC_ENCA) {
1503 while (sinf.type.type != FOURCC_CENC) {
1519 uint32_t AudioSampleEntry::ComputeSizeInternal() {
1520 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1521 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1528 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1529 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1535 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1542 uint32_t WebVTTConfigurationBox::ComputeSizeInternal() {
1546 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1547 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1553 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1557 : source_label.size());
1560 uint32_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1561 if (source_label.empty())
1566 TextSampleEntry::TextSampleEntry() : format(FOURCC_NULL) {}
1567 TextSampleEntry::~TextSampleEntry() {}
1570 if (format == FOURCC_NULL) {
1571 LOG(ERROR) <<
"TextSampleEntry should be parsed according to the "
1572 <<
"handler type recovered in its Media ancestor.";
1577 bool TextSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1579 DCHECK(buffer->
reader());
1580 format = buffer->
reader()->type();
1585 buffer->ReadWriteUInt16(&data_reference_index));
1587 if (format == FOURCC_wvtt) {
1596 uint32_t TextSampleEntry::ComputeSizeInternal() {
1598 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1602 MediaHeader::MediaHeader()
1603 : creation_time(0), modification_time(0), timescale(0), duration(0) {}
1604 MediaHeader::~MediaHeader() {}
1607 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1610 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1613 buffer->ReadWriteUInt32(×cale) &&
1615 language.ReadWrite(buffer) &&
1620 uint32_t MediaHeader::ComputeSizeInternal() {
1621 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1623 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1627 VideoMediaHeader::VideoMediaHeader()
1628 : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1629 const uint32_t kVideoMediaHeaderFlags = 1;
1630 flags = kVideoMediaHeaderFlags;
1632 VideoMediaHeader::~VideoMediaHeader() {}
1634 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1636 buffer->ReadWriteUInt16(&graphicsmode) &&
1637 buffer->ReadWriteUInt16(&opcolor_red) &&
1638 buffer->ReadWriteUInt16(&opcolor_green) &&
1639 buffer->ReadWriteUInt16(&opcolor_blue));
1643 uint32_t VideoMediaHeader::ComputeSizeInternal() {
1644 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1645 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1648 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1649 SoundMediaHeader::~SoundMediaHeader() {}
1651 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1653 buffer->ReadWriteUInt16(&balance) &&
1658 uint32_t SoundMediaHeader::ComputeSizeInternal() {
1659 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1662 SubtitleMediaHeader::SubtitleMediaHeader() {}
1663 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1667 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1671 uint32_t SubtitleMediaHeader::ComputeSizeInternal() {
1675 DataEntryUrl::DataEntryUrl() {
1676 const uint32_t kDataEntryUrlFlags = 1;
1677 flags = kDataEntryUrlFlags;
1679 DataEntryUrl::~DataEntryUrl() {}
1681 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
1684 RCHECK(buffer->ReadWriteVector(&location, buffer->
BytesLeft()));
1686 RCHECK(buffer->ReadWriteVector(&location, location.size()));
1691 uint32_t DataEntryUrl::ComputeSizeInternal() {
1695 DataReference::DataReference() {
1697 data_entry.resize(1);
1699 DataReference::~DataReference() {}
1701 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
1702 uint32_t entry_count = data_entry.size();
1704 buffer->ReadWriteUInt32(&entry_count));
1705 data_entry.resize(entry_count);
1707 for (uint32_t i = 0; i < entry_count; ++i)
1712 uint32_t DataReference::ComputeSizeInternal() {
1713 uint32_t count = data_entry.size();
1714 uint32_t box_size =
HeaderSize() +
sizeof(count);
1715 for (uint32_t i = 0; i < count; ++i)
1720 DataInformation::DataInformation() {}
1721 DataInformation::~DataInformation() {}
1724 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1730 uint32_t DataInformation::ComputeSizeInternal() {
1734 MediaInformation::MediaInformation() {}
1735 MediaInformation::~MediaInformation() {}
1738 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1743 switch (sample_table.description.type) {
1760 uint32_t MediaInformation::ComputeSizeInternal() {
1763 switch (sample_table.description.type) {
1783 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
1795 information.sample_table.description.type =
1796 FourCCToTrackType(handler.handler_type);
1798 handler.handler_type =
1799 TrackTypeToFourCC(information.sample_table.description.type);
1800 RCHECK(handler.handler_type != FOURCC_NULL);
1807 uint32_t Media::ComputeSizeInternal() {
1808 handler.handler_type =
1809 TrackTypeToFourCC(information.sample_table.description.type);
1818 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
1828 uint32_t Track::ComputeSizeInternal() {
1833 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
1834 MovieExtendsHeader::~MovieExtendsHeader() {}
1837 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1839 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1844 uint32_t MovieExtendsHeader::ComputeSizeInternal() {
1846 if (fragment_duration == 0)
1848 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
1849 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1852 TrackExtends::TrackExtends()
1854 default_sample_description_index(0),
1855 default_sample_duration(0),
1856 default_sample_size(0),
1857 default_sample_flags(0) {}
1858 TrackExtends::~TrackExtends() {}
1861 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1863 buffer->ReadWriteUInt32(&track_id) &&
1864 buffer->ReadWriteUInt32(&default_sample_description_index) &&
1865 buffer->ReadWriteUInt32(&default_sample_duration) &&
1866 buffer->ReadWriteUInt32(&default_sample_size) &&
1867 buffer->ReadWriteUInt32(&default_sample_flags));
1871 uint32_t TrackExtends::ComputeSizeInternal() {
1873 sizeof(default_sample_description_index) +
1874 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
1875 sizeof(default_sample_flags);
1878 MovieExtends::MovieExtends() {}
1879 MovieExtends::~MovieExtends() {}
1882 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1887 DCHECK(buffer->
reader());
1890 for (uint32_t i = 0; i < tracks.size(); ++i)
1896 uint32_t MovieExtends::ComputeSizeInternal() {
1898 if (tracks.size() == 0)
1901 for (uint32_t i = 0; i < tracks.size(); ++i)
1910 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
1922 for (uint32_t i = 0; i < tracks.size(); ++i)
1924 for (uint32_t i = 0; i < pssh.size(); ++i)
1930 uint32_t Movie::ComputeSizeInternal() {
1933 for (uint32_t i = 0; i < tracks.size(); ++i)
1935 for (uint32_t i = 0; i < pssh.size(); ++i)
1940 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
1941 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
1944 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
1946 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1951 uint32_t TrackFragmentDecodeTime::ComputeSizeInternal() {
1952 version = IsFitIn32Bits(decode_time) ? 0 : 1;
1953 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1956 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
1957 MovieFragmentHeader::~MovieFragmentHeader() {}
1960 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1962 buffer->ReadWriteUInt32(&sequence_number);
1965 uint32_t MovieFragmentHeader::ComputeSizeInternal() {
1966 return HeaderSize() +
sizeof(sequence_number);
1969 TrackFragmentHeader::TrackFragmentHeader()
1971 sample_description_index(0),
1972 default_sample_duration(0),
1973 default_sample_size(0),
1974 default_sample_flags(0) {}
1976 TrackFragmentHeader::~TrackFragmentHeader() {}
1979 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1981 buffer->ReadWriteUInt32(&track_id));
1983 if (flags & kBaseDataOffsetPresentMask) {
1988 uint64_t base_data_offset;
1989 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
1990 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
1991 "default-base-is-moof.";
1994 if (flags & kSampleDescriptionIndexPresentMask) {
1995 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
1996 }
else if (buffer->
Reading()) {
1997 sample_description_index = 0;
2000 if (flags & kDefaultSampleDurationPresentMask) {
2001 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2002 }
else if (buffer->
Reading()) {
2003 default_sample_duration = 0;
2006 if (flags & kDefaultSampleSizePresentMask) {
2007 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2008 }
else if (buffer->
Reading()) {
2009 default_sample_size = 0;
2012 if (flags & kDefaultSampleFlagsPresentMask)
2013 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2017 uint32_t TrackFragmentHeader::ComputeSizeInternal() {
2018 uint32_t box_size =
HeaderSize() +
sizeof(track_id);
2019 if (flags & kSampleDescriptionIndexPresentMask)
2020 box_size +=
sizeof(sample_description_index);
2021 if (flags & kDefaultSampleDurationPresentMask)
2022 box_size +=
sizeof(default_sample_duration);
2023 if (flags & kDefaultSampleSizePresentMask)
2024 box_size +=
sizeof(default_sample_size);
2025 if (flags & kDefaultSampleFlagsPresentMask)
2026 box_size +=
sizeof(default_sample_flags);
2030 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
2031 TrackFragmentRun::~TrackFragmentRun() {}
2034 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
2040 if (flags & kSampleCompTimeOffsetsPresentMask) {
2041 for (uint32_t i = 0; i < sample_count; ++i) {
2042 if (sample_composition_time_offsets[i] < 0) {
2051 buffer->ReadWriteUInt32(&sample_count));
2053 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2054 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2055 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2056 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2057 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2058 bool sample_composition_time_offsets_present =
2059 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2061 if (data_offset_present) {
2062 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2073 uint32_t first_sample_flags;
2076 if (first_sample_flags_present)
2077 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2079 if (sample_duration_present)
2080 sample_durations.resize(sample_count);
2081 if (sample_size_present)
2082 sample_sizes.resize(sample_count);
2083 if (sample_flags_present)
2084 sample_flags.resize(sample_count);
2085 if (sample_composition_time_offsets_present)
2086 sample_composition_time_offsets.resize(sample_count);
2088 if (first_sample_flags_present) {
2089 first_sample_flags = sample_flags[0];
2090 DCHECK(sample_flags.size() == 1);
2091 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2094 if (sample_duration_present)
2095 DCHECK(sample_durations.size() == sample_count);
2096 if (sample_size_present)
2097 DCHECK(sample_sizes.size() == sample_count);
2098 if (sample_flags_present)
2099 DCHECK(sample_flags.size() == sample_count);
2100 if (sample_composition_time_offsets_present)
2101 DCHECK(sample_composition_time_offsets.size() == sample_count);
2104 for (uint32_t i = 0; i < sample_count; ++i) {
2105 if (sample_duration_present)
2106 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2107 if (sample_size_present)
2108 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2109 if (sample_flags_present)
2110 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2112 if (sample_composition_time_offsets_present) {
2114 uint32_t sample_offset = sample_composition_time_offsets[i];
2115 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2116 sample_composition_time_offsets[i] = sample_offset;
2118 int32_t sample_offset = sample_composition_time_offsets[i];
2119 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2120 sample_composition_time_offsets[i] = sample_offset;
2126 if (first_sample_flags_present) {
2127 if (sample_flags.size() == 0) {
2128 sample_flags.push_back(first_sample_flags);
2130 sample_flags[0] = first_sample_flags;
2137 uint32_t TrackFragmentRun::ComputeSizeInternal() {
2138 uint32_t box_size =
HeaderSize() +
sizeof(sample_count);
2139 if (flags & kDataOffsetPresentMask)
2140 box_size +=
sizeof(data_offset);
2141 if (flags & kFirstSampleFlagsPresentMask)
2142 box_size +=
sizeof(uint32_t);
2143 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2144 (flags & kSampleSizePresentMask ? 1 : 0) +
2145 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2146 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2147 box_size += fields *
sizeof(uint32_t) * sample_count;
2151 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
2152 SampleToGroup::~SampleToGroup() {}
2155 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
2157 buffer->ReadWriteUInt32(&grouping_type));
2159 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
2161 if (grouping_type != FOURCC_SEIG) {
2163 DLOG(WARNING) <<
"Sample group "
2164 << FourCCToString(static_cast<FourCC>(grouping_type))
2165 <<
" is not supported.";
2169 uint32_t count = entries.size();
2170 RCHECK(buffer->ReadWriteUInt32(&count));
2171 entries.resize(count);
2172 for (uint32_t i = 0; i < count; ++i) {
2173 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
2174 buffer->ReadWriteUInt32(&entries[i].group_description_index));
2179 uint32_t SampleToGroup::ComputeSizeInternal() {
2181 if (entries.empty())
2183 return HeaderSize() +
sizeof(grouping_type) +
2184 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
2185 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
2188 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
2189 : is_encrypted(false), iv_size(0) {
2191 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
2193 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
2194 SampleGroupDescription::~SampleGroupDescription() {}
2197 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
2199 buffer->ReadWriteUInt32(&grouping_type));
2201 if (grouping_type != FOURCC_SEIG) {
2203 DLOG(WARNING) <<
"Sample group '" << grouping_type <<
"' is not supported.";
2207 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
2208 uint32_t default_length = 0;
2211 RCHECK(buffer->ReadWriteUInt32(&default_length));
2212 RCHECK(default_length == 0 || default_length >= kEntrySize);
2214 default_length = kEntrySize;
2215 RCHECK(buffer->ReadWriteUInt32(&default_length));
2219 uint32_t count = entries.size();
2220 RCHECK(buffer->ReadWriteUInt32(&count));
2221 entries.resize(count);
2222 for (uint32_t i = 0; i < count; ++i) {
2224 if (buffer->
Reading() && default_length == 0) {
2225 uint32_t description_length = 0;
2226 RCHECK(buffer->ReadWriteUInt32(&description_length));
2227 RCHECK(description_length >= kEntrySize);
2232 if (entries[i].key_id.size() != kCencKeyIdSize) {
2233 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
2234 <<
" bytes; got " << entries[i].key_id.size()
2235 <<
". Resized accordingly.";
2236 entries[i].key_id.resize(kCencKeyIdSize);
2240 uint8_t flag = entries[i].is_encrypted ? 1 : 0;
2242 buffer->ReadWriteUInt8(&flag) &&
2243 buffer->ReadWriteUInt8(&entries[i].iv_size) &&
2244 buffer->ReadWriteVector(&entries[i].key_id, kCencKeyIdSize));
2247 entries[i].is_encrypted = (flag != 0);
2248 if (entries[i].is_encrypted) {
2249 RCHECK(entries[i].iv_size == 8 || entries[i].iv_size == 16);
2251 RCHECK(entries[i].iv_size == 0);
2258 uint32_t SampleGroupDescription::ComputeSizeInternal() {
2262 if (entries.empty())
2264 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
2265 return HeaderSize() +
sizeof(grouping_type) +
2266 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
2267 entries.size() * kEntrySize;
2270 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2271 TrackFragment::~TrackFragment() {}
2274 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2279 DCHECK(buffer->
reader());
2281 if (!decode_time_absent)
2289 while (sample_to_group.grouping_type != FOURCC_SEIG &&
2293 while (sample_group_description.grouping_type != FOURCC_SEIG &&
2298 if (!decode_time_absent)
2300 for (uint32_t i = 0; i < runs.size(); ++i)
2310 uint32_t TrackFragment::ComputeSizeInternal() {
2316 for (uint32_t i = 0; i < runs.size(); ++i)
2321 MovieFragment::MovieFragment() {}
2322 MovieFragment::~MovieFragment() {}
2325 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2335 for (uint32_t i = 0; i < tracks.size(); ++i)
2337 for (uint32_t i = 0; i < pssh.size(); ++i)
2343 uint32_t MovieFragment::ComputeSizeInternal() {
2345 for (uint32_t i = 0; i < tracks.size(); ++i)
2347 for (uint32_t i = 0; i < pssh.size(); ++i)
2352 SegmentIndex::SegmentIndex()
2355 earliest_presentation_time(0),
2357 SegmentIndex::~SegmentIndex() {}
2360 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2362 buffer->ReadWriteUInt32(&reference_id) &&
2363 buffer->ReadWriteUInt32(×cale));
2365 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2370 uint16_t reference_count = references.size();
2372 buffer->ReadWriteUInt16(&reference_count));
2373 references.resize(reference_count);
2375 uint32_t reference_type_size;
2377 for (uint32_t i = 0; i < reference_count; ++i) {
2379 reference_type_size = references[i].referenced_size;
2380 if (references[i].reference_type)
2381 reference_type_size |= (1 << 31);
2382 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2383 if (references[i].starts_with_sap)
2386 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2387 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2388 buffer->ReadWriteUInt32(&sap));
2390 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2391 references[i].referenced_size = reference_type_size & ~(1 << 31);
2392 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2393 references[i].sap_type =
2394 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2395 references[i].sap_delta_time = sap & ~(0xF << 28);
2401 uint32_t SegmentIndex::ComputeSizeInternal() {
2402 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2403 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2404 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2405 3 *
sizeof(uint32_t) * references.size();
2408 MediaData::MediaData() : data_size(0) {}
2409 MediaData::~MediaData() {}
2412 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2413 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2417 uint32_t MediaData::ComputeSizeInternal() {
2421 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2422 CueSourceIDBox::~CueSourceIDBox() {}
2426 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2431 uint32_t CueSourceIDBox::ComputeSizeInternal() {
2432 if (source_id == kCueSourceIdNotSet)
2437 CueTimeBox::CueTimeBox() {}
2438 CueTimeBox::~CueTimeBox() {}
2444 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2451 uint32_t CueTimeBox::ComputeSizeInternal() {
2452 if (cue_current_time.empty())
2454 return HeaderSize() + cue_current_time.size();
2457 CueIDBox::CueIDBox() {}
2458 CueIDBox::~CueIDBox() {}
2464 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2470 uint32_t CueIDBox::ComputeSizeInternal() {
2476 CueSettingsBox::CueSettingsBox() {}
2477 CueSettingsBox::~CueSettingsBox() {}
2483 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2489 uint32_t CueSettingsBox::ComputeSizeInternal() {
2490 if (settings.empty())
2495 CuePayloadBox::CuePayloadBox() {}
2496 CuePayloadBox::~CuePayloadBox() {}
2502 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2508 uint32_t CuePayloadBox::ComputeSizeInternal() {
2512 VTTEmptyCueBox::VTTEmptyCueBox() {}
2513 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2519 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2523 uint32_t VTTEmptyCueBox::ComputeSizeInternal() {
2527 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2528 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2534 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2537 &cue_additional_text,
2541 uint32_t VTTAdditionalTextBox::ComputeSizeInternal() {
2542 return HeaderSize() + cue_additional_text.size();
2545 VTTCueBox::VTTCueBox() {}
2546 VTTCueBox::~VTTCueBox() {}
2552 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2563 uint32_t VTTCueBox::ComputeSizeInternal() {
uint32_t ComputeSize() const
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)
FourCC BoxType() const override
bool ReadWrite(uint8_t iv_size, bool has_subsamples, BoxBuffer *buffer)
uint32_t GetTotalSizeOfSubsamples() const