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/base/rcheck.h"
13 #include "packager/media/formats/mp4/box_buffer.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;
45 const size_t kInvalidIvSize = 1;
49 bool IsIvSizeValid(uint8_t per_sample_iv_size) {
50 return per_sample_iv_size == 0 || per_sample_iv_size == 8 ||
51 per_sample_iv_size == 16;
68 const uint8_t kDdtsExtraData[] = {0xe4, 0x7c, 0, 4, 0, 0x0f, 0};
71 const uint32_t kID3v2HeaderSize = 10;
72 const char kID3v2Identifier[] =
"ID3";
73 const uint16_t kID3v2Version = 0x0400;
76 bool IsFitIn32Bits(uint64_t a) {
77 return a <= std::numeric_limits<uint32_t>::max();
80 bool IsFitIn32Bits(int64_t a) {
81 return a <= std::numeric_limits<int32_t>::max() &&
82 a >= std::numeric_limits<int32_t>::min();
85 template <
typename T1,
typename T2>
86 bool IsFitIn32Bits(T1 a1, T2 a2) {
87 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
90 template <
typename T1,
typename T2,
typename T3>
91 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
92 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
103 TrackType FourCCToTrackType(FourCC fourcc) {
116 FourCC TrackTypeToFourCC(TrackType track_type) {
117 switch (track_type) {
129 bool IsProtectionSchemeSupported(FourCC scheme) {
130 return scheme == FOURCC_cenc || scheme == FOURCC_cens ||
131 scheme == FOURCC_cbc1 || scheme == FOURCC_cbcs;
136 FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
137 FileType::~FileType() {}
140 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
142 buffer->ReadWriteFourCC(&major_brand) &&
143 buffer->ReadWriteUInt32(&minor_version));
146 RCHECK(buffer->
BytesLeft() %
sizeof(FourCC) == 0);
147 num_brands = buffer->
BytesLeft() /
sizeof(FourCC);
148 compatible_brands.resize(num_brands);
150 num_brands = compatible_brands.size();
152 for (
size_t i = 0; i < num_brands; ++i)
153 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
157 uint32_t FileType::ComputeSizeInternal() {
158 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
159 kFourCCSize * compatible_brands.size();
164 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
165 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
168 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
172 raw_box.assign(reader->data(), reader->data() + reader->size());
174 DCHECK(!raw_box.empty());
175 buffer->
writer()->AppendVector(raw_box);
181 uint32_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
182 return raw_box.size();
185 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
186 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
189 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
194 uint32_t count = offsets.size();
195 RCHECK(buffer->ReadWriteUInt32(&count));
196 offsets.resize(count);
198 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
199 for (uint32_t i = 0; i < count; ++i)
204 uint32_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
206 if (offsets.size() == 0)
208 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
209 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
212 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
213 : default_sample_info_size(0), sample_count(0) {}
214 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
217 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
222 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
223 buffer->ReadWriteUInt32(&sample_count));
224 if (default_sample_info_size == 0)
225 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
229 uint32_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
231 if (sample_count == 0)
233 return HeaderSize() +
sizeof(default_sample_info_size) +
234 sizeof(sample_count) +
235 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
238 SampleEncryptionEntry::SampleEncryptionEntry() {}
239 SampleEncryptionEntry::~SampleEncryptionEntry() {}
244 DCHECK(IsIvSizeValid(iv_size));
247 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
249 if (!has_subsamples) {
254 uint16_t subsample_count =
static_cast<uint16_t
>(subsamples.size());
255 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
256 RCHECK(subsample_count > 0);
257 subsamples.resize(subsample_count);
258 for (
auto& subsample : subsamples) {
259 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
260 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
268 DCHECK(IsIvSizeValid(iv_size));
271 initialization_vector.resize(iv_size);
272 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
274 if (!has_subsamples) {
279 uint16_t subsample_count;
280 RCHECK(reader->Read2(&subsample_count));
281 RCHECK(subsample_count > 0);
282 subsamples.resize(subsample_count);
283 for (
auto& subsample : subsamples) {
284 RCHECK(reader->Read2(&subsample.clear_bytes) &&
285 reader->Read4(&subsample.cipher_bytes));
291 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
292 const uint16_t subsample_count =
static_cast<uint16_t
>(subsamples.size());
293 return initialization_vector.size() +
294 (subsample_count > 0 ? (
sizeof(subsample_count) +
295 subsample_entry_size * subsample_count)
301 for (uint32_t i = 0; i < subsamples.size(); ++i)
302 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
306 SampleEncryption::SampleEncryption() : iv_size(kInvalidIvSize) {}
307 SampleEncryption::~SampleEncryption() {}
310 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
315 if (buffer->
Reading() && iv_size == kInvalidIvSize) {
321 if (!IsIvSizeValid(iv_size)) {
323 <<
"IV_size can only be 8 or 16 or 0 for constant iv, but seeing "
328 uint32_t sample_count = sample_encryption_entries.size();
329 RCHECK(buffer->ReadWriteUInt32(&sample_count));
331 sample_encryption_entries.resize(sample_count);
332 for (
auto& sample_encryption_entry : sample_encryption_entries) {
333 RCHECK(sample_encryption_entry.ReadWrite(
334 iv_size, (flags & kUseSubsampleEncryption) != 0, buffer) != 0);
339 uint32_t SampleEncryption::ComputeSizeInternal() {
340 const uint32_t sample_count = sample_encryption_entries.size();
341 if (sample_count == 0) {
346 DCHECK(IsIvSizeValid(iv_size));
348 if (flags & kUseSubsampleEncryption) {
349 for (
const SampleEncryptionEntry& sample_encryption_entry :
350 sample_encryption_entries) {
351 box_size += sample_encryption_entry.ComputeSize();
354 box_size += sample_count * iv_size;
361 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
362 DCHECK(IsIvSizeValid(iv_size));
366 uint32_t sample_count = 0;
367 RCHECK(reader.Read4(&sample_count));
369 sample_encryption_entries->resize(sample_count);
370 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
371 RCHECK(sample_encryption_entry.ParseFromBuffer(
372 iv_size, (flags & kUseSubsampleEncryption) != 0, &reader) != 0);
377 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
378 OriginalFormat::~OriginalFormat() {}
381 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
385 uint32_t OriginalFormat::ComputeSizeInternal() {
389 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
390 SchemeType::~SchemeType() {}
393 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
395 buffer->ReadWriteFourCC(&type) &&
396 buffer->ReadWriteUInt32(&version));
400 uint32_t SchemeType::ComputeSizeInternal() {
401 return HeaderSize() + kFourCCSize +
sizeof(version);
404 TrackEncryption::TrackEncryption()
405 : default_is_protected(0),
406 default_per_sample_iv_size(0),
408 default_crypt_byte_block(0),
409 default_skip_byte_block(0) {}
410 TrackEncryption::~TrackEncryption() {}
413 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
415 if (default_kid.size() != kCencKeyIdSize) {
416 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
417 <<
" bytes; got " << default_kid.size()
418 <<
". Resized accordingly.";
419 default_kid.resize(kCencKeyIdSize);
421 RCHECK(default_crypt_byte_block < 16 && default_skip_byte_block < 16);
422 if (default_crypt_byte_block != 0 && default_skip_byte_block != 0) {
431 uint8_t pattern = default_crypt_byte_block << 4 | default_skip_byte_block;
432 RCHECK(buffer->ReadWriteUInt8(&pattern));
433 default_crypt_byte_block = pattern >> 4;
434 default_skip_byte_block = pattern & 0x0F;
436 RCHECK(buffer->ReadWriteUInt8(&default_is_protected) &&
437 buffer->ReadWriteUInt8(&default_per_sample_iv_size) &&
438 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
440 if (default_is_protected == 1) {
441 if (default_per_sample_iv_size == 0) {
442 uint8_t default_constant_iv_size =
443 static_cast<uint8_t
>(default_constant_iv.size());
444 RCHECK(buffer->ReadWriteUInt8(&default_constant_iv_size));
445 RCHECK(default_constant_iv_size == 8 || default_constant_iv_size == 16);
446 RCHECK(buffer->ReadWriteVector(&default_constant_iv,
447 default_constant_iv_size));
449 RCHECK(default_per_sample_iv_size == 8 ||
450 default_per_sample_iv_size == 16);
451 RCHECK(default_constant_iv.empty());
456 RCHECK(default_is_protected == 0);
457 RCHECK(default_per_sample_iv_size == 0);
458 RCHECK(default_constant_iv.empty());
463 uint32_t TrackEncryption::ComputeSizeInternal() {
464 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize +
465 (default_constant_iv.empty() ? 0 : (
sizeof(uint8_t) +
466 default_constant_iv.size()));
469 SchemeInfo::SchemeInfo() {}
470 SchemeInfo::~SchemeInfo() {}
473 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
479 uint32_t SchemeInfo::ComputeSizeInternal() {
483 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
484 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
487 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
492 if (IsProtectionSchemeSupported(type.type)) {
495 DLOG(WARNING) <<
"Ignore unsupported protection scheme: "
496 << FourCCToString(type.type);
505 uint32_t ProtectionSchemeInfo::ComputeSizeInternal() {
507 if (format.format == FOURCC_NULL)
513 MovieHeader::MovieHeader()
515 modification_time(0),
521 MovieHeader::~MovieHeader() {}
524 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
527 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
530 buffer->ReadWriteUInt32(×cale) &&
533 std::vector<uint8_t> matrix(kUnityMatrix,
534 kUnityMatrix + arraysize(kUnityMatrix));
535 RCHECK(buffer->ReadWriteInt32(&rate) &&
536 buffer->ReadWriteInt16(&volume) &&
538 buffer->ReadWriteVector(&matrix, matrix.size()) &&
540 buffer->ReadWriteUInt32(&next_track_id));
544 uint32_t MovieHeader::ComputeSizeInternal() {
545 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
546 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
547 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
548 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
552 TrackHeader::TrackHeader()
554 modification_time(0),
562 flags = kTrackEnabled | kTrackInMovie;
564 TrackHeader::~TrackHeader() {}
567 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
570 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
573 buffer->ReadWriteUInt32(&track_id) &&
580 volume = (width != 0 && height != 0) ? 0 : 0x100;
582 std::vector<uint8_t> matrix(kUnityMatrix,
583 kUnityMatrix + arraysize(kUnityMatrix));
585 buffer->ReadWriteInt16(&layer) &&
586 buffer->ReadWriteInt16(&alternate_group) &&
587 buffer->ReadWriteInt16(&volume) &&
589 buffer->ReadWriteVector(&matrix, matrix.size()) &&
590 buffer->ReadWriteUInt32(&width) &&
591 buffer->ReadWriteUInt32(&height));
595 uint32_t TrackHeader::ComputeSizeInternal() {
596 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
598 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
599 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
600 sizeof(height) +
sizeof(kUnityMatrix) + 14;
603 SampleDescription::SampleDescription() : type(kInvalid) {}
604 SampleDescription::~SampleDescription() {}
607 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
611 count = video_entries.size();
614 count = audio_entries.size();
617 count = text_entries.size();
620 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
621 <<
" is not handled. Skipping.";
624 buffer->ReadWriteUInt32(&count));
627 BoxReader* reader = buffer->
reader();
629 video_entries.clear();
630 audio_entries.clear();
633 if (type == kVideo) {
634 RCHECK(reader->ReadAllChildren(&video_entries));
635 RCHECK(video_entries.size() == count);
636 }
else if (type == kAudio) {
637 RCHECK(reader->ReadAllChildren(&audio_entries));
638 RCHECK(audio_entries.size() == count);
639 }
else if (type == kText) {
640 RCHECK(reader->ReadAllChildren(&text_entries));
641 RCHECK(text_entries.size() == count);
644 DCHECK_LT(0u, count);
645 if (type == kVideo) {
646 for (uint32_t i = 0; i < count; ++i)
648 }
else if (type == kAudio) {
649 for (uint32_t i = 0; i < count; ++i)
651 }
else if (type == kText) {
652 for (uint32_t i = 0; i < count; ++i)
661 uint32_t SampleDescription::ComputeSizeInternal() {
662 uint32_t box_size =
HeaderSize() +
sizeof(uint32_t);
663 if (type == kVideo) {
664 for (uint32_t i = 0; i < video_entries.size(); ++i)
666 }
else if (type == kAudio) {
667 for (uint32_t i = 0; i < audio_entries.size(); ++i)
669 }
else if (type == kText) {
670 for (uint32_t i = 0; i < text_entries.size(); ++i)
676 DecodingTimeToSample::DecodingTimeToSample() {}
677 DecodingTimeToSample::~DecodingTimeToSample() {}
680 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
681 uint32_t count = decoding_time.size();
683 buffer->ReadWriteUInt32(&count));
685 decoding_time.resize(count);
686 for (uint32_t i = 0; i < count; ++i) {
687 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
688 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
693 uint32_t DecodingTimeToSample::ComputeSizeInternal() {
695 sizeof(DecodingTime) * decoding_time.size();
698 CompositionTimeToSample::CompositionTimeToSample() {}
699 CompositionTimeToSample::~CompositionTimeToSample() {}
702 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
703 uint32_t count = composition_offset.size();
709 for (uint32_t i = 0; i < count; ++i) {
710 if (composition_offset[i].sample_offset < 0) {
718 buffer->ReadWriteUInt32(&count));
720 composition_offset.resize(count);
721 for (uint32_t i = 0; i < count; ++i) {
722 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
725 uint32_t sample_offset = composition_offset[i].sample_offset;
726 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
727 composition_offset[i].sample_offset = sample_offset;
729 int32_t sample_offset = composition_offset[i].sample_offset;
730 RCHECK(buffer->ReadWriteInt32(&sample_offset));
731 composition_offset[i].sample_offset = sample_offset;
737 uint32_t CompositionTimeToSample::ComputeSizeInternal() {
739 if (composition_offset.empty())
744 const uint32_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
746 kCompositionOffsetSize * composition_offset.size();
749 SampleToChunk::SampleToChunk() {}
750 SampleToChunk::~SampleToChunk() {}
753 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
754 uint32_t count = chunk_info.size();
756 buffer->ReadWriteUInt32(&count));
758 chunk_info.resize(count);
759 for (uint32_t i = 0; i < count; ++i) {
760 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
761 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
762 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
764 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
765 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
770 uint32_t SampleToChunk::ComputeSizeInternal() {
772 sizeof(ChunkInfo) * chunk_info.size();
775 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
776 SampleSize::~SampleSize() {}
779 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
781 buffer->ReadWriteUInt32(&sample_size) &&
782 buffer->ReadWriteUInt32(&sample_count));
784 if (sample_size == 0) {
786 sizes.resize(sample_count);
788 DCHECK(sample_count == sizes.size());
789 for (uint32_t i = 0; i < sample_count; ++i)
790 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
795 uint32_t SampleSize::ComputeSizeInternal() {
796 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
797 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
800 CompactSampleSize::CompactSampleSize() : field_size(0) {}
801 CompactSampleSize::~CompactSampleSize() {}
804 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
805 uint32_t sample_count = sizes.size();
808 buffer->ReadWriteUInt8(&field_size) &&
809 buffer->ReadWriteUInt32(&sample_count));
812 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
813 switch (field_size) {
815 for (uint32_t i = 0; i < sample_count; i += 2) {
818 RCHECK(buffer->ReadWriteUInt8(&size));
819 sizes[i] = size >> 4;
820 sizes[i + 1] = size & 0x0F;
822 DCHECK_LT(sizes[i], 16u);
823 DCHECK_LT(sizes[i + 1], 16u);
824 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
825 RCHECK(buffer->ReadWriteUInt8(&size));
830 for (uint32_t i = 0; i < sample_count; ++i) {
831 uint8_t size = sizes[i];
832 RCHECK(buffer->ReadWriteUInt8(&size));
837 for (uint32_t i = 0; i < sample_count; ++i) {
838 uint16_t size = sizes[i];
839 RCHECK(buffer->ReadWriteUInt16(&size));
846 sizes.resize(sample_count);
850 uint32_t CompactSampleSize::ComputeSizeInternal() {
851 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
852 (field_size * sizes.size() + 7) / 8;
855 ChunkOffset::ChunkOffset() {}
856 ChunkOffset::~ChunkOffset() {}
859 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
860 uint32_t count = offsets.size();
862 buffer->ReadWriteUInt32(&count));
864 offsets.resize(count);
865 for (uint32_t i = 0; i < count; ++i)
870 uint32_t ChunkOffset::ComputeSizeInternal() {
871 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
874 ChunkLargeOffset::ChunkLargeOffset() {}
875 ChunkLargeOffset::~ChunkLargeOffset() {}
878 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
879 uint32_t count = offsets.size();
883 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
885 stco.offsets.swap(offsets);
888 stco.offsets.swap(offsets);
894 buffer->ReadWriteUInt32(&count));
896 offsets.resize(count);
897 for (uint32_t i = 0; i < count; ++i)
898 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
902 uint32_t ChunkLargeOffset::ComputeSizeInternal() {
903 uint32_t count = offsets.size();
904 int use_large_offset =
905 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
907 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
910 SyncSample::SyncSample() {}
911 SyncSample::~SyncSample() {}
914 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
915 uint32_t count = sample_number.size();
917 buffer->ReadWriteUInt32(&count));
919 sample_number.resize(count);
920 for (uint32_t i = 0; i < count; ++i)
921 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
925 uint32_t SyncSample::ComputeSizeInternal() {
927 if (sample_number.empty())
930 sizeof(uint32_t) * sample_number.size();
933 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
935 per_sample_iv_size(0),
937 skip_byte_block(0) {}
938 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
940 bool CencSampleEncryptionInfoEntry::ReadWrite(BoxBuffer* buffer) {
941 if (!buffer->Reading()) {
942 if (key_id.size() != kCencKeyIdSize) {
943 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
944 <<
" bytes; got " << key_id.size()
945 <<
". Resized accordingly.";
946 key_id.resize(kCencKeyIdSize);
948 RCHECK(crypt_byte_block < 16 && skip_byte_block < 16);
951 RCHECK(buffer->IgnoreBytes(1));
953 uint8_t pattern = crypt_byte_block << 4 | skip_byte_block;
954 RCHECK(buffer->ReadWriteUInt8(&pattern));
955 crypt_byte_block = pattern >> 4;
956 skip_byte_block = pattern & 0x0F;
958 RCHECK(buffer->ReadWriteUInt8(&is_protected) &&
959 buffer->ReadWriteUInt8(&per_sample_iv_size) &&
960 buffer->ReadWriteVector(&key_id, kCencKeyIdSize));
962 if (is_protected == 1) {
963 if (per_sample_iv_size == 0) {
964 uint8_t constant_iv_size =
static_cast<uint8_t
>(constant_iv.size());
965 RCHECK(buffer->ReadWriteUInt8(&constant_iv_size));
966 RCHECK(constant_iv_size == 8 || constant_iv_size == 16);
967 RCHECK(buffer->ReadWriteVector(&constant_iv, constant_iv_size));
969 RCHECK(per_sample_iv_size == 8 || per_sample_iv_size == 16);
970 DCHECK(constant_iv.empty());
975 RCHECK(is_protected == 0);
976 RCHECK(per_sample_iv_size == 0);
981 uint32_t CencSampleEncryptionInfoEntry::ComputeSize()
const {
982 return sizeof(uint32_t) + kCencKeyIdSize +
983 (constant_iv.empty() ? 0 : (
sizeof(uint8_t) + constant_iv.size()));
986 AudioRollRecoveryEntry::AudioRollRecoveryEntry(): roll_distance(0) {}
987 AudioRollRecoveryEntry::~AudioRollRecoveryEntry() {}
989 bool AudioRollRecoveryEntry::ReadWrite(BoxBuffer* buffer) {
990 RCHECK(buffer->ReadWriteInt16(&roll_distance));
994 uint32_t AudioRollRecoveryEntry::ComputeSize()
const {
995 return sizeof(roll_distance);
998 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
999 SampleGroupDescription::~SampleGroupDescription() {}
1002 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
1004 buffer->ReadWriteUInt32(&grouping_type));
1006 switch (grouping_type) {
1008 return ReadWriteEntries(buffer, &cenc_sample_encryption_info_entries);
1010 return ReadWriteEntries(buffer, &audio_roll_recovery_entries);
1013 DLOG(WARNING) <<
"Ignore unsupported sample group: "
1014 << FourCCToString(static_cast<FourCC>(grouping_type));
1019 template <
typename T>
1020 bool SampleGroupDescription::ReadWriteEntries(BoxBuffer* buffer,
1021 std::vector<T>* entries) {
1022 uint32_t default_length = 0;
1023 if (!buffer->Reading()) {
1024 DCHECK(!entries->empty());
1025 default_length = (*entries)[0].ComputeSize();
1026 DCHECK_NE(default_length, 0u);
1029 RCHECK(buffer->ReadWriteUInt32(&default_length));
1031 NOTIMPLEMENTED() <<
"Unsupported SampleGroupDescriptionBox 'sgpd' version "
1032 <<
static_cast<int>(version);
1036 uint32_t count = entries->size();
1037 RCHECK(buffer->ReadWriteUInt32(&count));
1039 entries->resize(count);
1041 for (T& entry : *entries) {
1043 uint32_t description_length = default_length;
1044 if (buffer->Reading() && default_length == 0)
1045 RCHECK(buffer->ReadWriteUInt32(&description_length));
1046 RCHECK(entry.ReadWrite(buffer));
1047 RCHECK(entry.ComputeSize() == description_length);
1049 RCHECK(entry.ReadWrite(buffer));
1055 uint32_t SampleGroupDescription::ComputeSizeInternal() {
1058 size_t entries_size = 0;
1059 switch (grouping_type) {
1061 for (
const auto& entry : cenc_sample_encryption_info_entries)
1062 entries_size += entry.ComputeSize();
1065 for (
const auto& entry : audio_roll_recovery_entries)
1066 entries_size += entry.ComputeSize();
1070 if (entries_size == 0)
1072 return HeaderSize() +
sizeof(grouping_type) +
1073 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
1077 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
1078 SampleToGroup::~SampleToGroup() {}
1081 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
1083 buffer->ReadWriteUInt32(&grouping_type));
1085 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
1087 if (grouping_type != FOURCC_seig && grouping_type != FOURCC_roll) {
1089 DLOG(WARNING) <<
"Ignore unsupported sample group: "
1090 << FourCCToString(static_cast<FourCC>(grouping_type));
1094 uint32_t count = entries.size();
1095 RCHECK(buffer->ReadWriteUInt32(&count));
1096 entries.resize(count);
1097 for (uint32_t i = 0; i < count; ++i) {
1098 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
1099 buffer->ReadWriteUInt32(&entries[i].group_description_index));
1104 uint32_t SampleToGroup::ComputeSizeInternal() {
1106 if (entries.empty())
1108 return HeaderSize() +
sizeof(grouping_type) +
1109 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
1110 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
1113 SampleTable::SampleTable() {}
1114 SampleTable::~SampleTable() {}
1117 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
1131 RCHECK(reader->
ReadChild(&sample_size));
1133 CompactSampleSize compact_sample_size;
1134 RCHECK(reader->
ReadChild(&compact_sample_size));
1135 sample_size.sample_size = 0;
1136 sample_size.sample_count = compact_sample_size.sizes.size();
1137 sample_size.sizes.swap(compact_sample_size.sizes);
1141 if (reader->
ChildExist(&chunk_large_offset)) {
1142 RCHECK(reader->
ReadChild(&chunk_large_offset));
1144 ChunkOffset chunk_offset;
1145 RCHECK(reader->
ReadChild(&chunk_offset));
1146 chunk_large_offset.offsets.swap(chunk_offset.offsets);
1157 for (
auto& sample_group_description : sample_group_descriptions)
1159 for (
auto& sample_to_group : sample_to_groups)
1165 uint32_t SampleTable::ComputeSizeInternal() {
1172 for (
auto& sample_group_description : sample_group_descriptions)
1173 box_size += sample_group_description.ComputeSize();
1174 for (
auto& sample_to_group : sample_to_groups)
1175 box_size += sample_to_group.ComputeSize();
1179 EditList::EditList() {}
1180 EditList::~EditList() {}
1183 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
1184 uint32_t count = edits.size();
1186 edits.resize(count);
1188 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1189 for (uint32_t i = 0; i < count; ++i) {
1192 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
1193 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
1194 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
1199 uint32_t EditList::ComputeSizeInternal() {
1205 for (uint32_t i = 0; i < edits.size(); ++i) {
1206 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
1212 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
1220 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
1226 uint32_t Edit::ComputeSizeInternal() {
1228 if (list.edits.empty())
1233 HandlerReference::HandlerReference() : handler_type(FOURCC_NULL) {}
1234 HandlerReference::~HandlerReference() {}
1237 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1238 std::vector<uint8_t> handler_name;
1240 switch (handler_type) {
1242 handler_name.assign(kVideoHandlerName,
1243 kVideoHandlerName + arraysize(kVideoHandlerName));
1246 handler_name.assign(kAudioHandlerName,
1247 kAudioHandlerName + arraysize(kAudioHandlerName));
1250 handler_name.assign(kTextHandlerName,
1251 kTextHandlerName + arraysize(kTextHandlerName));
1262 buffer->ReadWriteFourCC(&handler_type));
1265 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1270 uint32_t HandlerReference::ComputeSizeInternal() {
1271 uint32_t box_size =
HeaderSize() + kFourCCSize + 16;
1272 switch (handler_type) {
1274 box_size +=
sizeof(kVideoHandlerName);
1277 box_size +=
sizeof(kAudioHandlerName);
1280 box_size +=
sizeof(kTextHandlerName);
1290 bool Language::ReadWrite(BoxBuffer* buffer) {
1291 if (buffer->Reading()) {
1294 std::vector<uint8_t> temp;
1295 RCHECK(buffer->ReadWriteVector(&temp, 2));
1297 BitReader bit_reader(&temp[0], 2);
1298 bit_reader.SkipBits(1);
1300 for (
int i = 0; i < 3; ++i) {
1301 CHECK(bit_reader.ReadBits(5, &language[i]));
1302 language[i] += 0x60;
1304 code.assign(language, 3);
1307 const char kUndefinedLanguage[] =
"und";
1309 code = kUndefinedLanguage;
1310 DCHECK_EQ(code.size(), 3u);
1314 for (
int i = 0; i < 3; ++i)
1315 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1316 RCHECK(buffer->ReadWriteUInt16(&lang));
1321 uint32_t Language::ComputeSize()
const {
1326 bool PrivFrame::ReadWrite(BoxBuffer* buffer) {
1327 FourCC fourcc = FOURCC_PRIV;
1328 RCHECK(buffer->ReadWriteFourCC(&fourcc));
1329 if (fourcc != FOURCC_PRIV) {
1330 VLOG(1) <<
"Skip unrecognized id3 frame during read: "
1331 << FourCCToString(fourcc);
1335 uint32_t frame_size = owner.size() + 1 + value.size();
1339 DCHECK_LT(frame_size, 0x7Fu);
1341 RCHECK(buffer->ReadWriteUInt32(&frame_size) &&
1342 buffer->ReadWriteUInt16(&flags));
1344 if (buffer->Reading()) {
1346 RCHECK(buffer->ReadWriteString(&str, frame_size));
1348 size_t pos = str.find(
'\0');
1349 RCHECK(pos < str.size());
1350 owner = str.substr(0, pos);
1351 value = str.substr(pos + 1);
1354 RCHECK(buffer->ReadWriteString(&owner, owner.size()) &&
1355 buffer->ReadWriteUInt8(&byte) &&
1356 buffer->ReadWriteString(&value, value.size()));
1361 uint32_t PrivFrame::ComputeSize()
const {
1362 if (owner.empty() && value.empty())
1364 const uint32_t kFourCCSize = 4;
1365 return kFourCCSize +
sizeof(uint32_t) +
sizeof(uint16_t) + owner.size() + 1 +
1374 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1376 language.ReadWrite(buffer));
1379 std::string id3v2_identifier = kID3v2Identifier;
1380 uint16_t version = kID3v2Version;
1386 DCHECK_LT(data_size, 0x7Fu);
1388 RCHECK(buffer->
ReadWriteString(&id3v2_identifier, id3v2_identifier.size()) &&
1389 buffer->ReadWriteUInt16(&version) &&
1390 buffer->ReadWriteUInt8(&flags) &&
1391 buffer->ReadWriteUInt32(&data_size));
1397 uint32_t ID3v2::ComputeSizeInternal() {
1400 return private_frame_size == 0 ? 0 :
HeaderSize() + language.ComputeSize() +
1405 Metadata::Metadata() {}
1406 Metadata::~Metadata() {}
1412 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1420 uint32_t Metadata::ComputeSizeInternal() {
1423 return id3v2_size == 0 ? 0
1427 CodecConfiguration::CodecConfiguration() : box_type(FOURCC_NULL) {}
1428 CodecConfiguration::~CodecConfiguration() {}
1436 bool CodecConfiguration::ReadWriteInternal(
BoxBuffer* buffer) {
1437 DCHECK_NE(box_type, FOURCC_NULL);
1442 if (box_type == FOURCC_vpcC) {
1443 uint32_t version_flags = 0;
1444 RCHECK(buffer->ReadWriteUInt32(&version_flags));
1445 RCHECK(version_flags == 0);
1449 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1451 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1456 uint32_t CodecConfiguration::ComputeSizeInternal() {
1459 DCHECK_NE(box_type, FOURCC_NULL);
1460 return HeaderSize() + (box_type == FOURCC_vpcC ? 4 : 0) + data.size();
1463 PixelAspectRatio::PixelAspectRatio() : h_spacing(0), v_spacing(0) {}
1464 PixelAspectRatio::~PixelAspectRatio() {}
1467 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1469 buffer->ReadWriteUInt32(&h_spacing) &&
1470 buffer->ReadWriteUInt32(&v_spacing));
1474 uint32_t PixelAspectRatio::ComputeSizeInternal() {
1476 if (h_spacing == 0 && v_spacing == 0)
1479 DCHECK(h_spacing != 0 && v_spacing != 0);
1480 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1483 VideoSampleEntry::VideoSampleEntry()
1484 : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
1486 VideoSampleEntry::~VideoSampleEntry() {}
1488 if (format == FOURCC_NULL) {
1489 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
1490 <<
"handler type recovered in its Media ancestor.";
1495 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1496 std::vector<uint8_t> compressor_name;
1498 DCHECK(buffer->
reader());
1499 format = buffer->
reader()->type();
1503 const FourCC actual_format = GetActualFormat();
1504 switch (actual_format) {
1506 compressor_name.assign(
1508 kAvcCompressorName + arraysize(kAvcCompressorName));
1512 compressor_name.assign(
1513 kHevcCompressorName,
1514 kHevcCompressorName + arraysize(kHevcCompressorName));
1519 compressor_name.assign(
1521 kVpcCompressorName + arraysize(kVpcCompressorName));
1524 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1527 compressor_name.resize(kCompressorNameSize);
1530 uint32_t video_resolution = kVideoResolution;
1531 uint16_t video_frame_count = kVideoFrameCount;
1532 uint16_t video_depth = kVideoDepth;
1533 int16_t predefined = -1;
1535 buffer->ReadWriteUInt16(&data_reference_index) &&
1537 buffer->ReadWriteUInt16(&width) &&
1538 buffer->ReadWriteUInt16(&height) &&
1539 buffer->ReadWriteUInt32(&video_resolution) &&
1540 buffer->ReadWriteUInt32(&video_resolution) &&
1542 buffer->ReadWriteUInt16(&video_frame_count) &&
1543 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1544 buffer->ReadWriteUInt16(&video_depth) &&
1545 buffer->ReadWriteInt16(&predefined));
1549 if (format == FOURCC_encv) {
1553 while (!IsProtectionSchemeSupported(sinf.type.type))
1556 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1561 const FourCC actual_format = GetActualFormat();
1563 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1565 DCHECK_EQ(codec_configuration.box_type,
1566 GetCodecConfigurationBoxType(actual_format));
1568 DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1575 uint32_t VideoSampleEntry::ComputeSizeInternal() {
1576 const FourCC actual_format = GetActualFormat();
1577 if (actual_format == FOURCC_NULL)
1579 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1580 DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1581 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1582 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1583 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1585 codec_configuration.
ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1589 FourCC VideoSampleEntry::GetCodecConfigurationBoxType(FourCC format)
const {
1601 LOG(ERROR) << FourCCToString(format) <<
" is not supported.";
1606 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1607 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1610 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1613 std::vector<uint8_t> data;
1614 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1615 RCHECK(es_descriptor.Parse(data));
1616 if (es_descriptor.
IsAAC()) {
1617 RCHECK(aac_audio_specific_config.
Parse(
1618 es_descriptor.decoder_specific_info()));
1621 DCHECK(buffer->
writer());
1622 es_descriptor.Write(buffer->
writer());
1627 uint32_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1629 if (es_descriptor.object_type() == kForbidden)
1631 return HeaderSize() + es_descriptor.ComputeSize();
1634 DTSSpecific::DTSSpecific()
1635 : sampling_frequency(0),
1638 pcm_sample_depth(0) {}
1639 DTSSpecific::~DTSSpecific() {}
1642 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1644 buffer->ReadWriteUInt32(&sampling_frequency) &&
1645 buffer->ReadWriteUInt32(&max_bitrate) &&
1646 buffer->ReadWriteUInt32(&avg_bitrate) &&
1647 buffer->ReadWriteUInt8(&pcm_sample_depth));
1650 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
BytesLeft()));
1652 if (extra_data.empty()) {
1653 extra_data.assign(kDdtsExtraData,
1654 kDdtsExtraData +
sizeof(kDdtsExtraData));
1656 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1661 uint32_t DTSSpecific::ComputeSizeInternal() {
1663 if (sampling_frequency == 0)
1665 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1666 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1667 sizeof(kDdtsExtraData);
1670 AC3Specific::AC3Specific() {}
1671 AC3Specific::~AC3Specific() {}
1675 bool AC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1677 buffer->ReadWriteVector(
1682 uint32_t AC3Specific::ComputeSizeInternal() {
1689 EC3Specific::EC3Specific() {}
1690 EC3Specific::~EC3Specific() {}
1694 bool EC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1697 RCHECK(buffer->ReadWriteVector(&data, size));
1701 uint32_t EC3Specific::ComputeSizeInternal() {
1708 OpusSpecific::OpusSpecific() : preskip(0) {}
1709 OpusSpecific::~OpusSpecific() {}
1713 bool OpusSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1716 std::vector<uint8_t> data;
1717 const int kMinOpusSpecificBoxDataSize = 11;
1718 RCHECK(buffer->
BytesLeft() >= kMinOpusSpecificBoxDataSize);
1719 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1720 preskip = data[2] + (data[3] << 8);
1725 writer.AppendInt(FOURCC_Head);
1727 const uint8_t kOpusIdentificationHeaderVersion = 1;
1728 data[0] = kOpusIdentificationHeaderVersion;
1729 writer.AppendVector(data);
1730 writer.SwapBuffer(&opus_identification_header);
1734 const size_t kOpusMagicSignatureSize = 8u;
1735 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1738 const uint8_t kOpusSpecificBoxVersion = 0;
1740 buffer->
writer()->AppendArray(
1741 &opus_identification_header[kOpusMagicSignatureSize + 1],
1742 opus_identification_header.size() - kOpusMagicSignatureSize - 1);
1747 uint32_t OpusSpecific::ComputeSizeInternal() {
1749 if (opus_identification_header.empty())
1753 const size_t kOpusMagicSignatureSize = 8u;
1754 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1755 return HeaderSize() + opus_identification_header.size() -
1756 kOpusMagicSignatureSize;
1759 AudioSampleEntry::AudioSampleEntry()
1760 : format(FOURCC_NULL),
1761 data_reference_index(1),
1766 AudioSampleEntry::~AudioSampleEntry() {}
1769 if (format == FOURCC_NULL) {
1770 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1771 <<
"handler type recovered in its Media ancestor.";
1776 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1778 DCHECK(buffer->
reader());
1779 format = buffer->
reader()->type();
1787 buffer->ReadWriteUInt16(&data_reference_index) &&
1789 buffer->ReadWriteUInt16(&channelcount) &&
1790 buffer->ReadWriteUInt16(&samplesize) &&
1792 buffer->ReadWriteUInt32(&samplerate));
1797 if (format == FOURCC_enca) {
1801 while (!IsProtectionSchemeSupported(sinf.type.type))
1804 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1817 uint32_t AudioSampleEntry::ComputeSizeInternal() {
1818 if (GetActualFormat() == FOURCC_NULL)
1820 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1821 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1828 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1829 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1835 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1842 uint32_t WebVTTConfigurationBox::ComputeSizeInternal() {
1846 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1847 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1853 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1857 : source_label.size());
1860 uint32_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1861 if (source_label.empty())
1866 TextSampleEntry::TextSampleEntry() : format(FOURCC_NULL) {}
1867 TextSampleEntry::~TextSampleEntry() {}
1870 if (format == FOURCC_NULL) {
1871 LOG(ERROR) <<
"TextSampleEntry should be parsed according to the "
1872 <<
"handler type recovered in its Media ancestor.";
1877 bool TextSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1879 DCHECK(buffer->
reader());
1880 format = buffer->
reader()->type();
1885 buffer->ReadWriteUInt16(&data_reference_index));
1887 if (format == FOURCC_wvtt) {
1896 uint32_t TextSampleEntry::ComputeSizeInternal() {
1898 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1902 MediaHeader::MediaHeader()
1903 : creation_time(0), modification_time(0), timescale(0), duration(0) {}
1904 MediaHeader::~MediaHeader() {}
1907 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1910 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1913 buffer->ReadWriteUInt32(×cale) &&
1915 language.ReadWrite(buffer) &&
1920 uint32_t MediaHeader::ComputeSizeInternal() {
1921 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1923 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1927 VideoMediaHeader::VideoMediaHeader()
1928 : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1929 const uint32_t kVideoMediaHeaderFlags = 1;
1930 flags = kVideoMediaHeaderFlags;
1932 VideoMediaHeader::~VideoMediaHeader() {}
1934 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1936 buffer->ReadWriteUInt16(&graphicsmode) &&
1937 buffer->ReadWriteUInt16(&opcolor_red) &&
1938 buffer->ReadWriteUInt16(&opcolor_green) &&
1939 buffer->ReadWriteUInt16(&opcolor_blue));
1943 uint32_t VideoMediaHeader::ComputeSizeInternal() {
1944 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1945 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1948 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1949 SoundMediaHeader::~SoundMediaHeader() {}
1951 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1953 buffer->ReadWriteUInt16(&balance) &&
1958 uint32_t SoundMediaHeader::ComputeSizeInternal() {
1959 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1962 SubtitleMediaHeader::SubtitleMediaHeader() {}
1963 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1967 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1971 uint32_t SubtitleMediaHeader::ComputeSizeInternal() {
1975 DataEntryUrl::DataEntryUrl() {
1976 const uint32_t kDataEntryUrlFlags = 1;
1977 flags = kDataEntryUrlFlags;
1979 DataEntryUrl::~DataEntryUrl() {}
1981 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
1984 RCHECK(buffer->ReadWriteVector(&location, buffer->
BytesLeft()));
1986 RCHECK(buffer->ReadWriteVector(&location, location.size()));
1991 uint32_t DataEntryUrl::ComputeSizeInternal() {
1995 DataReference::DataReference() {
1997 data_entry.resize(1);
1999 DataReference::~DataReference() {}
2001 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
2002 uint32_t entry_count = data_entry.size();
2004 buffer->ReadWriteUInt32(&entry_count));
2005 data_entry.resize(entry_count);
2007 for (uint32_t i = 0; i < entry_count; ++i)
2012 uint32_t DataReference::ComputeSizeInternal() {
2013 uint32_t count = data_entry.size();
2014 uint32_t box_size =
HeaderSize() +
sizeof(count);
2015 for (uint32_t i = 0; i < count; ++i)
2020 DataInformation::DataInformation() {}
2021 DataInformation::~DataInformation() {}
2024 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2030 uint32_t DataInformation::ComputeSizeInternal() {
2034 MediaInformation::MediaInformation() {}
2035 MediaInformation::~MediaInformation() {}
2038 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2043 switch (sample_table.description.type) {
2060 uint32_t MediaInformation::ComputeSizeInternal() {
2063 switch (sample_table.description.type) {
2083 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
2095 information.sample_table.description.type =
2096 FourCCToTrackType(handler.handler_type);
2098 handler.handler_type =
2099 TrackTypeToFourCC(information.sample_table.description.type);
2100 RCHECK(handler.handler_type != FOURCC_NULL);
2107 uint32_t Media::ComputeSizeInternal() {
2108 handler.handler_type =
2109 TrackTypeToFourCC(information.sample_table.description.type);
2118 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
2128 uint32_t Track::ComputeSizeInternal() {
2133 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
2134 MovieExtendsHeader::~MovieExtendsHeader() {}
2137 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2139 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2144 uint32_t MovieExtendsHeader::ComputeSizeInternal() {
2146 if (fragment_duration == 0)
2148 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
2149 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2152 TrackExtends::TrackExtends()
2154 default_sample_description_index(0),
2155 default_sample_duration(0),
2156 default_sample_size(0),
2157 default_sample_flags(0) {}
2158 TrackExtends::~TrackExtends() {}
2161 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2163 buffer->ReadWriteUInt32(&track_id) &&
2164 buffer->ReadWriteUInt32(&default_sample_description_index) &&
2165 buffer->ReadWriteUInt32(&default_sample_duration) &&
2166 buffer->ReadWriteUInt32(&default_sample_size) &&
2167 buffer->ReadWriteUInt32(&default_sample_flags));
2171 uint32_t TrackExtends::ComputeSizeInternal() {
2173 sizeof(default_sample_description_index) +
2174 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
2175 sizeof(default_sample_flags);
2178 MovieExtends::MovieExtends() {}
2179 MovieExtends::~MovieExtends() {}
2182 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2187 DCHECK(buffer->
reader());
2190 for (uint32_t i = 0; i < tracks.size(); ++i)
2196 uint32_t MovieExtends::ComputeSizeInternal() {
2198 if (tracks.size() == 0)
2201 for (uint32_t i = 0; i < tracks.size(); ++i)
2210 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
2222 for (uint32_t i = 0; i < tracks.size(); ++i)
2224 for (uint32_t i = 0; i < pssh.size(); ++i)
2230 uint32_t Movie::ComputeSizeInternal() {
2233 for (uint32_t i = 0; i < tracks.size(); ++i)
2235 for (uint32_t i = 0; i < pssh.size(); ++i)
2240 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
2241 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
2244 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
2246 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2251 uint32_t TrackFragmentDecodeTime::ComputeSizeInternal() {
2252 version = IsFitIn32Bits(decode_time) ? 0 : 1;
2253 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2256 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
2257 MovieFragmentHeader::~MovieFragmentHeader() {}
2260 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2262 buffer->ReadWriteUInt32(&sequence_number);
2265 uint32_t MovieFragmentHeader::ComputeSizeInternal() {
2266 return HeaderSize() +
sizeof(sequence_number);
2269 TrackFragmentHeader::TrackFragmentHeader()
2271 sample_description_index(0),
2272 default_sample_duration(0),
2273 default_sample_size(0),
2274 default_sample_flags(0) {}
2276 TrackFragmentHeader::~TrackFragmentHeader() {}
2279 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2281 buffer->ReadWriteUInt32(&track_id));
2283 if (flags & kBaseDataOffsetPresentMask) {
2288 uint64_t base_data_offset;
2289 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
2290 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
2291 "default-base-is-moof.";
2294 if (flags & kSampleDescriptionIndexPresentMask) {
2295 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
2296 }
else if (buffer->
Reading()) {
2297 sample_description_index = 0;
2300 if (flags & kDefaultSampleDurationPresentMask) {
2301 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2302 }
else if (buffer->
Reading()) {
2303 default_sample_duration = 0;
2306 if (flags & kDefaultSampleSizePresentMask) {
2307 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2308 }
else if (buffer->
Reading()) {
2309 default_sample_size = 0;
2312 if (flags & kDefaultSampleFlagsPresentMask)
2313 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2317 uint32_t TrackFragmentHeader::ComputeSizeInternal() {
2318 uint32_t box_size =
HeaderSize() +
sizeof(track_id);
2319 if (flags & kSampleDescriptionIndexPresentMask)
2320 box_size +=
sizeof(sample_description_index);
2321 if (flags & kDefaultSampleDurationPresentMask)
2322 box_size +=
sizeof(default_sample_duration);
2323 if (flags & kDefaultSampleSizePresentMask)
2324 box_size +=
sizeof(default_sample_size);
2325 if (flags & kDefaultSampleFlagsPresentMask)
2326 box_size +=
sizeof(default_sample_flags);
2330 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
2331 TrackFragmentRun::~TrackFragmentRun() {}
2334 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
2340 if (flags & kSampleCompTimeOffsetsPresentMask) {
2341 for (uint32_t i = 0; i < sample_count; ++i) {
2342 if (sample_composition_time_offsets[i] < 0) {
2351 buffer->ReadWriteUInt32(&sample_count));
2353 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2354 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2355 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2356 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2357 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2358 bool sample_composition_time_offsets_present =
2359 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2361 if (data_offset_present) {
2362 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2373 uint32_t first_sample_flags(0);
2376 if (first_sample_flags_present)
2377 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2379 if (sample_duration_present)
2380 sample_durations.resize(sample_count);
2381 if (sample_size_present)
2382 sample_sizes.resize(sample_count);
2383 if (sample_flags_present)
2384 sample_flags.resize(sample_count);
2385 if (sample_composition_time_offsets_present)
2386 sample_composition_time_offsets.resize(sample_count);
2388 if (first_sample_flags_present) {
2389 first_sample_flags = sample_flags[0];
2390 DCHECK(sample_flags.size() == 1);
2391 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2394 if (sample_duration_present)
2395 DCHECK(sample_durations.size() == sample_count);
2396 if (sample_size_present)
2397 DCHECK(sample_sizes.size() == sample_count);
2398 if (sample_flags_present)
2399 DCHECK(sample_flags.size() == sample_count);
2400 if (sample_composition_time_offsets_present)
2401 DCHECK(sample_composition_time_offsets.size() == sample_count);
2404 for (uint32_t i = 0; i < sample_count; ++i) {
2405 if (sample_duration_present)
2406 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2407 if (sample_size_present)
2408 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2409 if (sample_flags_present)
2410 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2412 if (sample_composition_time_offsets_present) {
2414 uint32_t sample_offset = sample_composition_time_offsets[i];
2415 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2416 sample_composition_time_offsets[i] = sample_offset;
2418 int32_t sample_offset = sample_composition_time_offsets[i];
2419 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2420 sample_composition_time_offsets[i] = sample_offset;
2426 if (first_sample_flags_present) {
2427 if (sample_flags.size() == 0) {
2428 sample_flags.push_back(first_sample_flags);
2430 sample_flags[0] = first_sample_flags;
2437 uint32_t TrackFragmentRun::ComputeSizeInternal() {
2438 uint32_t box_size =
HeaderSize() +
sizeof(sample_count);
2439 if (flags & kDataOffsetPresentMask)
2440 box_size +=
sizeof(data_offset);
2441 if (flags & kFirstSampleFlagsPresentMask)
2442 box_size +=
sizeof(uint32_t);
2443 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2444 (flags & kSampleSizePresentMask ? 1 : 0) +
2445 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2446 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2447 box_size += fields *
sizeof(uint32_t) * sample_count;
2451 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2452 TrackFragment::~TrackFragment() {}
2455 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2460 DCHECK(buffer->
reader());
2462 if (!decode_time_absent)
2468 if (!decode_time_absent)
2470 for (uint32_t i = 0; i < runs.size(); ++i)
2472 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2474 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2482 uint32_t TrackFragment::ComputeSizeInternal() {
2487 for (uint32_t i = 0; i < runs.size(); ++i)
2489 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2490 box_size += sample_group_descriptions[i].
ComputeSize();
2491 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2496 MovieFragment::MovieFragment() {}
2497 MovieFragment::~MovieFragment() {}
2500 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2510 for (uint32_t i = 0; i < tracks.size(); ++i)
2512 for (uint32_t i = 0; i < pssh.size(); ++i)
2518 uint32_t MovieFragment::ComputeSizeInternal() {
2520 for (uint32_t i = 0; i < tracks.size(); ++i)
2522 for (uint32_t i = 0; i < pssh.size(); ++i)
2527 SegmentIndex::SegmentIndex()
2530 earliest_presentation_time(0),
2532 SegmentIndex::~SegmentIndex() {}
2535 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2537 buffer->ReadWriteUInt32(&reference_id) &&
2538 buffer->ReadWriteUInt32(×cale));
2540 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2545 uint16_t reference_count =
static_cast<uint16_t
>(references.size());
2547 buffer->ReadWriteUInt16(&reference_count));
2548 references.resize(reference_count);
2550 uint32_t reference_type_size;
2552 for (uint32_t i = 0; i < reference_count; ++i) {
2554 reference_type_size = references[i].referenced_size;
2555 if (references[i].reference_type)
2556 reference_type_size |= (1 << 31);
2557 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2558 if (references[i].starts_with_sap)
2561 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2562 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2563 buffer->ReadWriteUInt32(&sap));
2565 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2566 references[i].referenced_size = reference_type_size & ~(1 << 31);
2567 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2568 references[i].sap_type =
2569 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2570 references[i].sap_delta_time = sap & ~(0xF << 28);
2576 uint32_t SegmentIndex::ComputeSizeInternal() {
2577 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2578 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2579 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2580 3 *
sizeof(uint32_t) * references.size();
2583 MediaData::MediaData() : data_size(0) {}
2584 MediaData::~MediaData() {}
2587 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2588 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2592 uint32_t MediaData::ComputeSizeInternal() {
2596 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2597 CueSourceIDBox::~CueSourceIDBox() {}
2601 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2606 uint32_t CueSourceIDBox::ComputeSizeInternal() {
2607 if (source_id == kCueSourceIdNotSet)
2612 CueTimeBox::CueTimeBox() {}
2613 CueTimeBox::~CueTimeBox() {}
2619 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2626 uint32_t CueTimeBox::ComputeSizeInternal() {
2627 if (cue_current_time.empty())
2629 return HeaderSize() + cue_current_time.size();
2632 CueIDBox::CueIDBox() {}
2633 CueIDBox::~CueIDBox() {}
2639 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2645 uint32_t CueIDBox::ComputeSizeInternal() {
2651 CueSettingsBox::CueSettingsBox() {}
2652 CueSettingsBox::~CueSettingsBox() {}
2658 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2664 uint32_t CueSettingsBox::ComputeSizeInternal() {
2665 if (settings.empty())
2670 CuePayloadBox::CuePayloadBox() {}
2671 CuePayloadBox::~CuePayloadBox() {}
2677 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2683 uint32_t CuePayloadBox::ComputeSizeInternal() {
2687 VTTEmptyCueBox::VTTEmptyCueBox() {}
2688 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2694 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2698 uint32_t VTTEmptyCueBox::ComputeSizeInternal() {
2702 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2703 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2709 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2712 &cue_additional_text,
2716 uint32_t VTTAdditionalTextBox::ComputeSizeInternal() {
2717 return HeaderSize() + cue_additional_text.size();
2720 VTTCueBox::VTTCueBox() {}
2721 VTTCueBox::~VTTCueBox() {}
2727 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2738 uint32_t VTTCueBox::ComputeSizeInternal() {
FourCC BoxType() const override
bool ParseFromBuffer(uint8_t iv_size, bool has_subsamples, BufferReader *reader)
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
uint32_t GetTotalSizeOfSubsamples() const
bool ReadWrite(uint8_t iv_size, bool has_subsamples, BoxBuffer *buffer)
FourCC BoxType() const override
uint32_t ComputeSize() const