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(
size_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) {
131 FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
132 FileType::~FileType() {}
135 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
137 buffer->ReadWriteFourCC(&major_brand) &&
138 buffer->ReadWriteUInt32(&minor_version));
141 RCHECK(buffer->
BytesLeft() %
sizeof(FourCC) == 0);
142 num_brands = buffer->
BytesLeft() /
sizeof(FourCC);
143 compatible_brands.resize(num_brands);
145 num_brands = compatible_brands.size();
147 for (
size_t i = 0; i < num_brands; ++i)
148 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
152 uint32_t FileType::ComputeSizeInternal() {
153 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
154 kFourCCSize * compatible_brands.size();
159 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
160 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
163 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
167 raw_box.assign(reader->data(), reader->data() + reader->size());
169 DCHECK(!raw_box.empty());
170 buffer->
writer()->AppendVector(raw_box);
176 uint32_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
177 return raw_box.size();
180 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
181 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
184 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
189 uint32_t count = offsets.size();
190 RCHECK(buffer->ReadWriteUInt32(&count));
191 offsets.resize(count);
193 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
194 for (uint32_t i = 0; i < count; ++i)
199 uint32_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
201 if (offsets.size() == 0)
203 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
204 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
207 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
208 : default_sample_info_size(0), sample_count(0) {}
209 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
212 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
217 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
218 buffer->ReadWriteUInt32(&sample_count));
219 if (default_sample_info_size == 0)
220 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
224 uint32_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
226 if (sample_count == 0)
228 return HeaderSize() +
sizeof(default_sample_info_size) +
229 sizeof(sample_count) +
230 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
233 SampleEncryptionEntry::SampleEncryptionEntry() {}
234 SampleEncryptionEntry::~SampleEncryptionEntry() {}
239 DCHECK(IsIvSizeValid(iv_size));
242 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
244 if (!has_subsamples) {
249 uint16_t subsample_count = subsamples.size();
250 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
251 RCHECK(subsample_count > 0);
252 subsamples.resize(subsample_count);
253 for (
auto& subsample : subsamples) {
254 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
255 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
263 DCHECK(IsIvSizeValid(iv_size));
266 initialization_vector.resize(iv_size);
267 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
269 if (!has_subsamples) {
274 uint16_t subsample_count;
275 RCHECK(reader->Read2(&subsample_count));
276 RCHECK(subsample_count > 0);
277 subsamples.resize(subsample_count);
278 for (
auto& subsample : subsamples) {
279 RCHECK(reader->Read2(&subsample.clear_bytes) &&
280 reader->Read4(&subsample.cipher_bytes));
286 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
287 const uint16_t subsample_count = subsamples.size();
288 return initialization_vector.size() +
289 (subsample_count > 0 ? (
sizeof(subsample_count) +
290 subsample_entry_size * subsample_count)
296 for (uint32_t i = 0; i < subsamples.size(); ++i)
297 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
301 SampleEncryption::SampleEncryption() : iv_size(kInvalidIvSize) {}
302 SampleEncryption::~SampleEncryption() {}
305 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
310 if (buffer->
Reading() && iv_size == kInvalidIvSize) {
316 if (!IsIvSizeValid(iv_size)) {
318 <<
"IV_size can only be 8 or 16 or 0 for constant iv, but seeing "
323 uint32_t sample_count = sample_encryption_entries.size();
324 RCHECK(buffer->ReadWriteUInt32(&sample_count));
326 sample_encryption_entries.resize(sample_count);
327 for (
auto& sample_encryption_entry : sample_encryption_entries) {
328 RCHECK(sample_encryption_entry.ReadWrite(
329 iv_size, flags & kUseSubsampleEncryption, buffer));
334 uint32_t SampleEncryption::ComputeSizeInternal() {
335 const uint32_t sample_count = sample_encryption_entries.size();
336 if (sample_count == 0) {
341 DCHECK(IsIvSizeValid(iv_size));
343 if (flags & kUseSubsampleEncryption) {
344 for (
const SampleEncryptionEntry& sample_encryption_entry :
345 sample_encryption_entries) {
346 box_size += sample_encryption_entry.ComputeSize();
349 box_size += sample_count * iv_size;
356 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
357 DCHECK(IsIvSizeValid(iv_size));
361 uint32_t sample_count = 0;
362 RCHECK(reader.Read4(&sample_count));
364 sample_encryption_entries->resize(sample_count);
365 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
366 RCHECK(sample_encryption_entry.ParseFromBuffer(
367 iv_size, flags & kUseSubsampleEncryption, &reader));
372 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
373 OriginalFormat::~OriginalFormat() {}
376 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
380 uint32_t OriginalFormat::ComputeSizeInternal() {
384 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
385 SchemeType::~SchemeType() {}
388 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
390 buffer->ReadWriteFourCC(&type) &&
391 buffer->ReadWriteUInt32(&version));
395 uint32_t SchemeType::ComputeSizeInternal() {
396 return HeaderSize() + kFourCCSize +
sizeof(version);
399 TrackEncryption::TrackEncryption()
400 : default_is_protected(0),
401 default_per_sample_iv_size(0),
403 default_crypt_byte_block(0),
404 default_skip_byte_block(0) {}
405 TrackEncryption::~TrackEncryption() {}
408 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
410 if (default_kid.size() != kCencKeyIdSize) {
411 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
412 <<
" bytes; got " << default_kid.size()
413 <<
". Resized accordingly.";
414 default_kid.resize(kCencKeyIdSize);
416 RCHECK(default_crypt_byte_block < 16 && default_skip_byte_block < 16);
417 if (default_crypt_byte_block != 0 && default_skip_byte_block != 0) {
426 uint8_t pattern = default_crypt_byte_block << 4 | default_skip_byte_block;
427 RCHECK(buffer->ReadWriteUInt8(&pattern));
428 default_crypt_byte_block = pattern >> 4;
429 default_skip_byte_block = pattern & 0x0F;
431 RCHECK(buffer->ReadWriteUInt8(&default_is_protected) &&
432 buffer->ReadWriteUInt8(&default_per_sample_iv_size) &&
433 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
435 if (default_is_protected == 1) {
436 if (default_per_sample_iv_size == 0) {
437 uint8_t default_constant_iv_size = default_constant_iv.size();
438 RCHECK(buffer->ReadWriteUInt8(&default_constant_iv_size));
439 RCHECK(default_constant_iv_size == 8 || default_constant_iv_size == 16);
440 RCHECK(buffer->ReadWriteVector(&default_constant_iv,
441 default_constant_iv_size));
443 RCHECK(default_per_sample_iv_size == 8 ||
444 default_per_sample_iv_size == 16);
445 RCHECK(default_constant_iv.empty());
450 RCHECK(default_is_protected == 0);
451 RCHECK(default_per_sample_iv_size == 0);
452 RCHECK(default_constant_iv.empty());
457 uint32_t TrackEncryption::ComputeSizeInternal() {
458 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize +
459 (default_constant_iv.empty() ? 0 : (
sizeof(uint8_t) +
460 default_constant_iv.size()));
463 SchemeInfo::SchemeInfo() {}
464 SchemeInfo::~SchemeInfo() {}
467 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
473 uint32_t SchemeInfo::ComputeSizeInternal() {
477 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
478 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
481 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
486 RCHECK(type.type == FOURCC_cenc || type.type == FOURCC_cbc1 ||
487 type.type == FOURCC_cens || type.type == FOURCC_cbcs);
496 uint32_t ProtectionSchemeInfo::ComputeSizeInternal() {
498 if (format.format == FOURCC_NULL)
504 MovieHeader::MovieHeader()
506 modification_time(0),
512 MovieHeader::~MovieHeader() {}
515 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
518 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
521 buffer->ReadWriteUInt32(×cale) &&
524 std::vector<uint8_t> matrix(kUnityMatrix,
525 kUnityMatrix + arraysize(kUnityMatrix));
526 RCHECK(buffer->ReadWriteInt32(&rate) &&
527 buffer->ReadWriteInt16(&volume) &&
529 buffer->ReadWriteVector(&matrix, matrix.size()) &&
531 buffer->ReadWriteUInt32(&next_track_id));
535 uint32_t MovieHeader::ComputeSizeInternal() {
536 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
537 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
538 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
539 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
543 TrackHeader::TrackHeader()
545 modification_time(0),
553 flags = kTrackEnabled | kTrackInMovie;
555 TrackHeader::~TrackHeader() {}
558 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
561 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
564 buffer->ReadWriteUInt32(&track_id) &&
571 volume = (width != 0 && height != 0) ? 0 : 0x100;
573 std::vector<uint8_t> matrix(kUnityMatrix,
574 kUnityMatrix + arraysize(kUnityMatrix));
576 buffer->ReadWriteInt16(&layer) &&
577 buffer->ReadWriteInt16(&alternate_group) &&
578 buffer->ReadWriteInt16(&volume) &&
580 buffer->ReadWriteVector(&matrix, matrix.size()) &&
581 buffer->ReadWriteUInt32(&width) &&
582 buffer->ReadWriteUInt32(&height));
586 uint32_t TrackHeader::ComputeSizeInternal() {
587 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
589 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
590 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
591 sizeof(height) +
sizeof(kUnityMatrix) + 14;
594 SampleDescription::SampleDescription() : type(kInvalid) {}
595 SampleDescription::~SampleDescription() {}
598 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
602 count = video_entries.size();
605 count = audio_entries.size();
608 count = text_entries.size();
611 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
612 <<
" is not handled. Skipping.";
615 buffer->ReadWriteUInt32(&count));
618 BoxReader* reader = buffer->
reader();
620 video_entries.clear();
621 audio_entries.clear();
624 if (type == kVideo) {
625 RCHECK(reader->ReadAllChildren(&video_entries));
626 RCHECK(video_entries.size() == count);
627 }
else if (type == kAudio) {
628 RCHECK(reader->ReadAllChildren(&audio_entries));
629 RCHECK(audio_entries.size() == count);
630 }
else if (type == kText) {
631 RCHECK(reader->ReadAllChildren(&text_entries));
632 RCHECK(text_entries.size() == count);
635 DCHECK_LT(0u, count);
636 if (type == kVideo) {
637 for (uint32_t i = 0; i < count; ++i)
639 }
else if (type == kAudio) {
640 for (uint32_t i = 0; i < count; ++i)
642 }
else if (type == kText) {
643 for (uint32_t i = 0; i < count; ++i)
652 uint32_t SampleDescription::ComputeSizeInternal() {
653 uint32_t box_size =
HeaderSize() +
sizeof(uint32_t);
654 if (type == kVideo) {
655 for (uint32_t i = 0; i < video_entries.size(); ++i)
657 }
else if (type == kAudio) {
658 for (uint32_t i = 0; i < audio_entries.size(); ++i)
660 }
else if (type == kText) {
661 for (uint32_t i = 0; i < text_entries.size(); ++i)
667 DecodingTimeToSample::DecodingTimeToSample() {}
668 DecodingTimeToSample::~DecodingTimeToSample() {}
671 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
672 uint32_t count = decoding_time.size();
674 buffer->ReadWriteUInt32(&count));
676 decoding_time.resize(count);
677 for (uint32_t i = 0; i < count; ++i) {
678 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
679 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
684 uint32_t DecodingTimeToSample::ComputeSizeInternal() {
686 sizeof(DecodingTime) * decoding_time.size();
689 CompositionTimeToSample::CompositionTimeToSample() {}
690 CompositionTimeToSample::~CompositionTimeToSample() {}
693 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
694 uint32_t count = composition_offset.size();
700 for (uint32_t i = 0; i < count; ++i) {
701 if (composition_offset[i].sample_offset < 0) {
709 buffer->ReadWriteUInt32(&count));
711 composition_offset.resize(count);
712 for (uint32_t i = 0; i < count; ++i) {
713 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
716 uint32_t sample_offset = composition_offset[i].sample_offset;
717 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
718 composition_offset[i].sample_offset = sample_offset;
720 int32_t sample_offset = composition_offset[i].sample_offset;
721 RCHECK(buffer->ReadWriteInt32(&sample_offset));
722 composition_offset[i].sample_offset = sample_offset;
728 uint32_t CompositionTimeToSample::ComputeSizeInternal() {
730 if (composition_offset.empty())
735 const uint32_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
737 kCompositionOffsetSize * composition_offset.size();
740 SampleToChunk::SampleToChunk() {}
741 SampleToChunk::~SampleToChunk() {}
744 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
745 uint32_t count = chunk_info.size();
747 buffer->ReadWriteUInt32(&count));
749 chunk_info.resize(count);
750 for (uint32_t i = 0; i < count; ++i) {
751 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
752 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
753 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
755 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
756 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
761 uint32_t SampleToChunk::ComputeSizeInternal() {
763 sizeof(ChunkInfo) * chunk_info.size();
766 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
767 SampleSize::~SampleSize() {}
770 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
772 buffer->ReadWriteUInt32(&sample_size) &&
773 buffer->ReadWriteUInt32(&sample_count));
775 if (sample_size == 0) {
777 sizes.resize(sample_count);
779 DCHECK(sample_count == sizes.size());
780 for (uint32_t i = 0; i < sample_count; ++i)
781 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
786 uint32_t SampleSize::ComputeSizeInternal() {
787 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
788 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
791 CompactSampleSize::CompactSampleSize() : field_size(0) {}
792 CompactSampleSize::~CompactSampleSize() {}
795 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
796 uint32_t sample_count = sizes.size();
799 buffer->ReadWriteUInt8(&field_size) &&
800 buffer->ReadWriteUInt32(&sample_count));
803 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
804 switch (field_size) {
806 for (uint32_t i = 0; i < sample_count; i += 2) {
809 RCHECK(buffer->ReadWriteUInt8(&size));
810 sizes[i] = size >> 4;
811 sizes[i + 1] = size & 0x0F;
813 DCHECK_LT(sizes[i], 16u);
814 DCHECK_LT(sizes[i + 1], 16u);
815 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
816 RCHECK(buffer->ReadWriteUInt8(&size));
821 for (uint32_t i = 0; i < sample_count; ++i) {
822 uint8_t size = sizes[i];
823 RCHECK(buffer->ReadWriteUInt8(&size));
828 for (uint32_t i = 0; i < sample_count; ++i) {
829 uint16_t size = sizes[i];
830 RCHECK(buffer->ReadWriteUInt16(&size));
837 sizes.resize(sample_count);
841 uint32_t CompactSampleSize::ComputeSizeInternal() {
842 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
843 (field_size * sizes.size() + 7) / 8;
846 ChunkOffset::ChunkOffset() {}
847 ChunkOffset::~ChunkOffset() {}
850 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
851 uint32_t count = offsets.size();
853 buffer->ReadWriteUInt32(&count));
855 offsets.resize(count);
856 for (uint32_t i = 0; i < count; ++i)
861 uint32_t ChunkOffset::ComputeSizeInternal() {
862 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
865 ChunkLargeOffset::ChunkLargeOffset() {}
866 ChunkLargeOffset::~ChunkLargeOffset() {}
869 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
870 uint32_t count = offsets.size();
874 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
876 stco.offsets.swap(offsets);
879 stco.offsets.swap(offsets);
885 buffer->ReadWriteUInt32(&count));
887 offsets.resize(count);
888 for (uint32_t i = 0; i < count; ++i)
889 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
893 uint32_t ChunkLargeOffset::ComputeSizeInternal() {
894 uint32_t count = offsets.size();
895 int use_large_offset =
896 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
898 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
901 SyncSample::SyncSample() {}
902 SyncSample::~SyncSample() {}
905 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
906 uint32_t count = sample_number.size();
908 buffer->ReadWriteUInt32(&count));
910 sample_number.resize(count);
911 for (uint32_t i = 0; i < count; ++i)
912 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
916 uint32_t SyncSample::ComputeSizeInternal() {
918 if (sample_number.empty())
921 sizeof(uint32_t) * sample_number.size();
924 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
926 per_sample_iv_size(0),
928 skip_byte_block(0) {}
929 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
931 bool CencSampleEncryptionInfoEntry::ReadWrite(BoxBuffer* buffer) {
932 if (!buffer->Reading()) {
933 if (key_id.size() != kCencKeyIdSize) {
934 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
935 <<
" bytes; got " << key_id.size()
936 <<
". Resized accordingly.";
937 key_id.resize(kCencKeyIdSize);
939 RCHECK(crypt_byte_block < 16 && skip_byte_block < 16);
942 RCHECK(buffer->IgnoreBytes(1));
944 uint8_t pattern = crypt_byte_block << 4 | skip_byte_block;
945 RCHECK(buffer->ReadWriteUInt8(&pattern));
946 crypt_byte_block = pattern >> 4;
947 skip_byte_block = pattern & 0x0F;
949 RCHECK(buffer->ReadWriteUInt8(&is_protected) &&
950 buffer->ReadWriteUInt8(&per_sample_iv_size) &&
951 buffer->ReadWriteVector(&key_id, kCencKeyIdSize));
953 if (is_protected == 1) {
954 if (per_sample_iv_size == 0) {
955 uint8_t constant_iv_size = constant_iv.size();
956 RCHECK(buffer->ReadWriteUInt8(&constant_iv_size));
957 RCHECK(constant_iv_size == 8 || constant_iv_size == 16);
958 RCHECK(buffer->ReadWriteVector(&constant_iv, constant_iv_size));
960 RCHECK(per_sample_iv_size == 8 || per_sample_iv_size == 16);
961 DCHECK(constant_iv.empty());
966 RCHECK(is_protected == 0);
967 RCHECK(per_sample_iv_size == 0);
972 uint32_t CencSampleEncryptionInfoEntry::ComputeSize()
const {
973 return sizeof(uint32_t) + kCencKeyIdSize +
974 (constant_iv.empty() ? 0 : (
sizeof(uint8_t) + constant_iv.size()));
977 AudioRollRecoveryEntry::AudioRollRecoveryEntry(): roll_distance(0) {}
978 AudioRollRecoveryEntry::~AudioRollRecoveryEntry() {}
980 bool AudioRollRecoveryEntry::ReadWrite(BoxBuffer* buffer) {
981 RCHECK(buffer->ReadWriteInt16(&roll_distance));
985 uint32_t AudioRollRecoveryEntry::ComputeSize()
const {
986 return sizeof(roll_distance);
989 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
990 SampleGroupDescription::~SampleGroupDescription() {}
993 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
995 buffer->ReadWriteUInt32(&grouping_type));
997 switch (grouping_type) {
999 return ReadWriteEntries(buffer, &cenc_sample_encryption_info_entries);
1001 return ReadWriteEntries(buffer, &audio_roll_recovery_entries);
1004 DLOG(WARNING) <<
"Sample group '" << grouping_type
1005 <<
"' is not supported.";
1010 template <
typename T>
1011 bool SampleGroupDescription::ReadWriteEntries(BoxBuffer* buffer,
1012 std::vector<T>* entries) {
1013 uint32_t default_length = 0;
1014 if (!buffer->Reading()) {
1015 DCHECK(!entries->empty());
1016 default_length = (*entries)[0].ComputeSize();
1017 DCHECK_NE(default_length, 0u);
1020 RCHECK(buffer->ReadWriteUInt32(&default_length));
1022 NOTIMPLEMENTED() <<
"Unsupported SampleGroupDescriptionBox 'sgpd' version "
1023 <<
static_cast<int>(version);
1027 uint32_t count = entries->size();
1028 RCHECK(buffer->ReadWriteUInt32(&count));
1030 entries->resize(count);
1032 for (T& entry : *entries) {
1034 uint32_t description_length = default_length;
1035 if (buffer->Reading() && default_length == 0)
1036 RCHECK(buffer->ReadWriteUInt32(&description_length));
1037 RCHECK(entry.ReadWrite(buffer));
1038 RCHECK(entry.ComputeSize() == description_length);
1040 RCHECK(entry.ReadWrite(buffer));
1046 uint32_t SampleGroupDescription::ComputeSizeInternal() {
1049 size_t entries_size = 0;
1050 switch (grouping_type) {
1052 for (
const auto& entry : cenc_sample_encryption_info_entries)
1053 entries_size += entry.ComputeSize();
1056 for (
const auto& entry : audio_roll_recovery_entries)
1057 entries_size += entry.ComputeSize();
1061 if (entries_size == 0)
1063 return HeaderSize() +
sizeof(grouping_type) +
1064 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
1068 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
1069 SampleToGroup::~SampleToGroup() {}
1072 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
1074 buffer->ReadWriteUInt32(&grouping_type));
1076 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
1078 if (grouping_type != FOURCC_seig && grouping_type != FOURCC_roll) {
1080 DLOG(WARNING) <<
"Sample group "
1081 << FourCCToString(static_cast<FourCC>(grouping_type))
1082 <<
" is not supported.";
1086 uint32_t count = entries.size();
1087 RCHECK(buffer->ReadWriteUInt32(&count));
1088 entries.resize(count);
1089 for (uint32_t i = 0; i < count; ++i) {
1090 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
1091 buffer->ReadWriteUInt32(&entries[i].group_description_index));
1096 uint32_t SampleToGroup::ComputeSizeInternal() {
1098 if (entries.empty())
1100 return HeaderSize() +
sizeof(grouping_type) +
1101 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
1102 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
1105 SampleTable::SampleTable() {}
1106 SampleTable::~SampleTable() {}
1109 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
1123 RCHECK(reader->
ReadChild(&sample_size));
1125 CompactSampleSize compact_sample_size;
1126 RCHECK(reader->
ReadChild(&compact_sample_size));
1127 sample_size.sample_size = 0;
1128 sample_size.sample_count = compact_sample_size.sizes.size();
1129 sample_size.sizes.swap(compact_sample_size.sizes);
1133 if (reader->
ChildExist(&chunk_large_offset)) {
1134 RCHECK(reader->
ReadChild(&chunk_large_offset));
1136 ChunkOffset chunk_offset;
1137 RCHECK(reader->
ReadChild(&chunk_offset));
1138 chunk_large_offset.offsets.swap(chunk_offset.offsets);
1149 for (
auto& sample_group_description : sample_group_descriptions)
1151 for (
auto& sample_to_group : sample_to_groups)
1157 uint32_t SampleTable::ComputeSizeInternal() {
1164 for (
auto& sample_group_description : sample_group_descriptions)
1165 box_size += sample_group_description.ComputeSize();
1166 for (
auto& sample_to_group : sample_to_groups)
1167 box_size += sample_to_group.ComputeSize();
1171 EditList::EditList() {}
1172 EditList::~EditList() {}
1175 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
1176 uint32_t count = edits.size();
1178 edits.resize(count);
1180 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1181 for (uint32_t i = 0; i < count; ++i) {
1184 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
1185 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
1186 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
1191 uint32_t EditList::ComputeSizeInternal() {
1197 for (uint32_t i = 0; i < edits.size(); ++i) {
1198 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
1204 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
1212 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
1218 uint32_t Edit::ComputeSizeInternal() {
1220 if (list.edits.empty())
1225 HandlerReference::HandlerReference() : handler_type(FOURCC_NULL) {}
1226 HandlerReference::~HandlerReference() {}
1229 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1230 std::vector<uint8_t> handler_name;
1232 switch (handler_type) {
1234 handler_name.assign(kVideoHandlerName,
1235 kVideoHandlerName + arraysize(kVideoHandlerName));
1238 handler_name.assign(kAudioHandlerName,
1239 kAudioHandlerName + arraysize(kAudioHandlerName));
1242 handler_name.assign(kTextHandlerName,
1243 kTextHandlerName + arraysize(kTextHandlerName));
1254 buffer->ReadWriteFourCC(&handler_type));
1257 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1262 uint32_t HandlerReference::ComputeSizeInternal() {
1263 uint32_t box_size =
HeaderSize() + kFourCCSize + 16;
1264 switch (handler_type) {
1266 box_size +=
sizeof(kVideoHandlerName);
1269 box_size +=
sizeof(kAudioHandlerName);
1272 box_size +=
sizeof(kTextHandlerName);
1282 bool Language::ReadWrite(BoxBuffer* buffer) {
1283 if (buffer->Reading()) {
1286 std::vector<uint8_t> temp;
1287 RCHECK(buffer->ReadWriteVector(&temp, 2));
1289 BitReader bit_reader(&temp[0], 2);
1290 bit_reader.SkipBits(1);
1292 for (
int i = 0; i < 3; ++i) {
1293 CHECK(bit_reader.ReadBits(5, &language[i]));
1294 language[i] += 0x60;
1296 code.assign(language, 3);
1299 const char kUndefinedLanguage[] =
"und";
1301 code = kUndefinedLanguage;
1302 DCHECK_EQ(code.size(), 3u);
1306 for (
int i = 0; i < 3; ++i)
1307 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1308 RCHECK(buffer->ReadWriteUInt16(&lang));
1313 uint32_t Language::ComputeSize()
const {
1318 bool PrivFrame::ReadWrite(BoxBuffer* buffer) {
1319 FourCC fourcc = FOURCC_PRIV;
1320 RCHECK(buffer->ReadWriteFourCC(&fourcc));
1321 if (fourcc != FOURCC_PRIV) {
1322 VLOG(1) <<
"Skip unrecognized id3 frame during read: "
1323 << FourCCToString(fourcc);
1327 uint32_t frame_size = owner.size() + 1 + value.size();
1331 DCHECK_LT(frame_size, 0x7Fu);
1333 RCHECK(buffer->ReadWriteUInt32(&frame_size) &&
1334 buffer->ReadWriteUInt16(&flags));
1336 if (buffer->Reading()) {
1338 RCHECK(buffer->ReadWriteString(&str, frame_size));
1340 size_t pos = str.find(
'\0');
1341 RCHECK(pos < str.size());
1342 owner = str.substr(0, pos);
1343 value = str.substr(pos + 1);
1346 RCHECK(buffer->ReadWriteString(&owner, owner.size()) &&
1347 buffer->ReadWriteUInt8(&byte) &&
1348 buffer->ReadWriteString(&value, value.size()));
1353 uint32_t PrivFrame::ComputeSize()
const {
1354 if (owner.empty() && value.empty())
1356 const uint32_t kFourCCSize = 4;
1357 return kFourCCSize +
sizeof(uint32_t) +
sizeof(uint16_t) + owner.size() + 1 +
1366 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1368 language.ReadWrite(buffer));
1371 std::string id3v2_identifier = kID3v2Identifier;
1372 uint16_t version = kID3v2Version;
1378 DCHECK_LT(data_size, 0x7Fu);
1380 RCHECK(buffer->
ReadWriteString(&id3v2_identifier, id3v2_identifier.size()) &&
1381 buffer->ReadWriteUInt16(&version) &&
1382 buffer->ReadWriteUInt8(&flags) &&
1383 buffer->ReadWriteUInt32(&data_size));
1389 uint32_t ID3v2::ComputeSizeInternal() {
1392 return private_frame_size == 0 ? 0 :
HeaderSize() + language.ComputeSize() +
1397 Metadata::Metadata() {}
1398 Metadata::~Metadata() {}
1404 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1412 uint32_t Metadata::ComputeSizeInternal() {
1415 return id3v2_size == 0 ? 0
1419 CodecConfigurationRecord::CodecConfigurationRecord() : box_type(FOURCC_NULL) {}
1420 CodecConfigurationRecord::~CodecConfigurationRecord() {}
1427 bool CodecConfigurationRecord::ReadWriteInternal(
BoxBuffer* buffer) {
1430 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1432 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1437 uint32_t CodecConfigurationRecord::ComputeSizeInternal() {
1443 PixelAspectRatio::PixelAspectRatio() : h_spacing(0), v_spacing(0) {}
1444 PixelAspectRatio::~PixelAspectRatio() {}
1447 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1449 buffer->ReadWriteUInt32(&h_spacing) &&
1450 buffer->ReadWriteUInt32(&v_spacing));
1454 uint32_t PixelAspectRatio::ComputeSizeInternal() {
1456 if (h_spacing == 0 && v_spacing == 0)
1459 DCHECK(h_spacing != 0 && v_spacing != 0);
1460 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1463 VideoSampleEntry::VideoSampleEntry()
1464 : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
1466 VideoSampleEntry::~VideoSampleEntry() {}
1468 if (format == FOURCC_NULL) {
1469 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
1470 <<
"handler type recovered in its Media ancestor.";
1475 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1476 std::vector<uint8_t> compressor_name;
1478 DCHECK(buffer->
reader());
1479 format = buffer->
reader()->type();
1483 const FourCC actual_format = GetActualFormat();
1484 switch (actual_format) {
1486 compressor_name.assign(
1488 kAvcCompressorName + arraysize(kAvcCompressorName));
1492 compressor_name.assign(
1493 kHevcCompressorName,
1494 kHevcCompressorName + arraysize(kHevcCompressorName));
1499 compressor_name.assign(
1501 kVpcCompressorName + arraysize(kVpcCompressorName));
1504 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1507 compressor_name.resize(kCompressorNameSize);
1510 uint32_t video_resolution = kVideoResolution;
1511 uint16_t video_frame_count = kVideoFrameCount;
1512 uint16_t video_depth = kVideoDepth;
1513 int16_t predefined = -1;
1515 buffer->ReadWriteUInt16(&data_reference_index) &&
1517 buffer->ReadWriteUInt16(&width) &&
1518 buffer->ReadWriteUInt16(&height) &&
1519 buffer->ReadWriteUInt32(&video_resolution) &&
1520 buffer->ReadWriteUInt32(&video_resolution) &&
1522 buffer->ReadWriteUInt16(&video_frame_count) &&
1523 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1524 buffer->ReadWriteUInt16(&video_depth) &&
1525 buffer->ReadWriteInt16(&predefined));
1529 if (format == FOURCC_encv)
1532 const FourCC actual_format = GetActualFormat();
1533 switch (actual_format) {
1535 codec_config_record.box_type = FOURCC_avcC;
1539 codec_config_record.box_type = FOURCC_hvcC;
1544 codec_config_record.box_type = FOURCC_vpcC;
1547 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1555 uint32_t VideoSampleEntry::ComputeSizeInternal() {
1556 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1557 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1558 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1560 codec_config_record.
ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1564 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1565 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1568 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1571 std::vector<uint8_t> data;
1572 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1573 RCHECK(es_descriptor.Parse(data));
1574 if (es_descriptor.
IsAAC()) {
1575 RCHECK(aac_audio_specific_config.
Parse(
1576 es_descriptor.decoder_specific_info()));
1579 DCHECK(buffer->
writer());
1580 es_descriptor.Write(buffer->
writer());
1585 uint32_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1587 if (es_descriptor.object_type() == kForbidden)
1589 return HeaderSize() + es_descriptor.ComputeSize();
1592 DTSSpecific::DTSSpecific()
1593 : sampling_frequency(0),
1596 pcm_sample_depth(0) {}
1597 DTSSpecific::~DTSSpecific() {}
1600 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1602 buffer->ReadWriteUInt32(&sampling_frequency) &&
1603 buffer->ReadWriteUInt32(&max_bitrate) &&
1604 buffer->ReadWriteUInt32(&avg_bitrate) &&
1605 buffer->ReadWriteUInt8(&pcm_sample_depth));
1608 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
BytesLeft()));
1610 if (extra_data.empty()) {
1611 extra_data.assign(kDdtsExtraData,
1612 kDdtsExtraData +
sizeof(kDdtsExtraData));
1614 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1619 uint32_t DTSSpecific::ComputeSizeInternal() {
1621 if (sampling_frequency == 0)
1623 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1624 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1625 sizeof(kDdtsExtraData);
1628 AC3Specific::AC3Specific() {}
1629 AC3Specific::~AC3Specific() {}
1633 bool AC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1635 buffer->ReadWriteVector(
1640 uint32_t AC3Specific::ComputeSizeInternal() {
1647 EC3Specific::EC3Specific() {}
1648 EC3Specific::~EC3Specific() {}
1652 bool EC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1655 RCHECK(buffer->ReadWriteVector(&data, size));
1659 uint32_t EC3Specific::ComputeSizeInternal() {
1666 OpusSpecific::OpusSpecific() : preskip(0) {}
1667 OpusSpecific::~OpusSpecific() {}
1671 bool OpusSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1674 std::vector<uint8_t> data;
1675 const int kMinOpusSpecificBoxDataSize = 11;
1676 RCHECK(buffer->
BytesLeft() >= kMinOpusSpecificBoxDataSize);
1677 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1678 preskip = data[2] + (data[3] << 8);
1683 writer.AppendInt(FOURCC_Head);
1685 const uint8_t kOpusIdentificationHeaderVersion = 1;
1686 data[0] = kOpusIdentificationHeaderVersion;
1687 writer.AppendVector(data);
1688 writer.SwapBuffer(&opus_identification_header);
1692 const size_t kOpusMagicSignatureSize = 8u;
1693 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1696 const uint8_t kOpusSpecificBoxVersion = 0;
1698 buffer->
writer()->AppendArray(
1699 &opus_identification_header[kOpusMagicSignatureSize + 1],
1700 opus_identification_header.size() - kOpusMagicSignatureSize - 1);
1705 uint32_t OpusSpecific::ComputeSizeInternal() {
1707 if (opus_identification_header.empty())
1711 const size_t kOpusMagicSignatureSize = 8u;
1712 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1713 return HeaderSize() + opus_identification_header.size() -
1714 kOpusMagicSignatureSize;
1717 AudioSampleEntry::AudioSampleEntry()
1718 : format(FOURCC_NULL),
1719 data_reference_index(1),
1724 AudioSampleEntry::~AudioSampleEntry() {}
1727 if (format == FOURCC_NULL) {
1728 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1729 <<
"handler type recovered in its Media ancestor.";
1734 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1736 DCHECK(buffer->
reader());
1737 format = buffer->
reader()->type();
1745 buffer->ReadWriteUInt16(&data_reference_index) &&
1747 buffer->ReadWriteUInt16(&channelcount) &&
1748 buffer->ReadWriteUInt16(&samplesize) &&
1750 buffer->ReadWriteUInt32(&samplerate));
1755 if (format == FOURCC_enca)
1766 uint32_t AudioSampleEntry::ComputeSizeInternal() {
1767 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1768 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1775 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1776 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1782 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1789 uint32_t WebVTTConfigurationBox::ComputeSizeInternal() {
1793 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1794 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1800 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1804 : source_label.size());
1807 uint32_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1808 if (source_label.empty())
1813 TextSampleEntry::TextSampleEntry() : format(FOURCC_NULL) {}
1814 TextSampleEntry::~TextSampleEntry() {}
1817 if (format == FOURCC_NULL) {
1818 LOG(ERROR) <<
"TextSampleEntry should be parsed according to the "
1819 <<
"handler type recovered in its Media ancestor.";
1824 bool TextSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1826 DCHECK(buffer->
reader());
1827 format = buffer->
reader()->type();
1832 buffer->ReadWriteUInt16(&data_reference_index));
1834 if (format == FOURCC_wvtt) {
1843 uint32_t TextSampleEntry::ComputeSizeInternal() {
1845 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1849 MediaHeader::MediaHeader()
1850 : creation_time(0), modification_time(0), timescale(0), duration(0) {}
1851 MediaHeader::~MediaHeader() {}
1854 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1857 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1860 buffer->ReadWriteUInt32(×cale) &&
1862 language.ReadWrite(buffer) &&
1867 uint32_t MediaHeader::ComputeSizeInternal() {
1868 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1870 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1874 VideoMediaHeader::VideoMediaHeader()
1875 : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1876 const uint32_t kVideoMediaHeaderFlags = 1;
1877 flags = kVideoMediaHeaderFlags;
1879 VideoMediaHeader::~VideoMediaHeader() {}
1881 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1883 buffer->ReadWriteUInt16(&graphicsmode) &&
1884 buffer->ReadWriteUInt16(&opcolor_red) &&
1885 buffer->ReadWriteUInt16(&opcolor_green) &&
1886 buffer->ReadWriteUInt16(&opcolor_blue));
1890 uint32_t VideoMediaHeader::ComputeSizeInternal() {
1891 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1892 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1895 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1896 SoundMediaHeader::~SoundMediaHeader() {}
1898 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1900 buffer->ReadWriteUInt16(&balance) &&
1905 uint32_t SoundMediaHeader::ComputeSizeInternal() {
1906 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1909 SubtitleMediaHeader::SubtitleMediaHeader() {}
1910 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1914 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1918 uint32_t SubtitleMediaHeader::ComputeSizeInternal() {
1922 DataEntryUrl::DataEntryUrl() {
1923 const uint32_t kDataEntryUrlFlags = 1;
1924 flags = kDataEntryUrlFlags;
1926 DataEntryUrl::~DataEntryUrl() {}
1928 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
1931 RCHECK(buffer->ReadWriteVector(&location, buffer->
BytesLeft()));
1933 RCHECK(buffer->ReadWriteVector(&location, location.size()));
1938 uint32_t DataEntryUrl::ComputeSizeInternal() {
1942 DataReference::DataReference() {
1944 data_entry.resize(1);
1946 DataReference::~DataReference() {}
1948 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
1949 uint32_t entry_count = data_entry.size();
1951 buffer->ReadWriteUInt32(&entry_count));
1952 data_entry.resize(entry_count);
1954 for (uint32_t i = 0; i < entry_count; ++i)
1959 uint32_t DataReference::ComputeSizeInternal() {
1960 uint32_t count = data_entry.size();
1961 uint32_t box_size =
HeaderSize() +
sizeof(count);
1962 for (uint32_t i = 0; i < count; ++i)
1967 DataInformation::DataInformation() {}
1968 DataInformation::~DataInformation() {}
1971 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1977 uint32_t DataInformation::ComputeSizeInternal() {
1981 MediaInformation::MediaInformation() {}
1982 MediaInformation::~MediaInformation() {}
1985 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1990 switch (sample_table.description.type) {
2007 uint32_t MediaInformation::ComputeSizeInternal() {
2010 switch (sample_table.description.type) {
2030 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
2042 information.sample_table.description.type =
2043 FourCCToTrackType(handler.handler_type);
2045 handler.handler_type =
2046 TrackTypeToFourCC(information.sample_table.description.type);
2047 RCHECK(handler.handler_type != FOURCC_NULL);
2054 uint32_t Media::ComputeSizeInternal() {
2055 handler.handler_type =
2056 TrackTypeToFourCC(information.sample_table.description.type);
2065 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
2075 uint32_t Track::ComputeSizeInternal() {
2080 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
2081 MovieExtendsHeader::~MovieExtendsHeader() {}
2084 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2086 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2091 uint32_t MovieExtendsHeader::ComputeSizeInternal() {
2093 if (fragment_duration == 0)
2095 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
2096 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2099 TrackExtends::TrackExtends()
2101 default_sample_description_index(0),
2102 default_sample_duration(0),
2103 default_sample_size(0),
2104 default_sample_flags(0) {}
2105 TrackExtends::~TrackExtends() {}
2108 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2110 buffer->ReadWriteUInt32(&track_id) &&
2111 buffer->ReadWriteUInt32(&default_sample_description_index) &&
2112 buffer->ReadWriteUInt32(&default_sample_duration) &&
2113 buffer->ReadWriteUInt32(&default_sample_size) &&
2114 buffer->ReadWriteUInt32(&default_sample_flags));
2118 uint32_t TrackExtends::ComputeSizeInternal() {
2120 sizeof(default_sample_description_index) +
2121 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
2122 sizeof(default_sample_flags);
2125 MovieExtends::MovieExtends() {}
2126 MovieExtends::~MovieExtends() {}
2129 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2134 DCHECK(buffer->
reader());
2137 for (uint32_t i = 0; i < tracks.size(); ++i)
2143 uint32_t MovieExtends::ComputeSizeInternal() {
2145 if (tracks.size() == 0)
2148 for (uint32_t i = 0; i < tracks.size(); ++i)
2157 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
2169 for (uint32_t i = 0; i < tracks.size(); ++i)
2171 for (uint32_t i = 0; i < pssh.size(); ++i)
2177 uint32_t Movie::ComputeSizeInternal() {
2180 for (uint32_t i = 0; i < tracks.size(); ++i)
2182 for (uint32_t i = 0; i < pssh.size(); ++i)
2187 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
2188 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
2191 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
2193 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2198 uint32_t TrackFragmentDecodeTime::ComputeSizeInternal() {
2199 version = IsFitIn32Bits(decode_time) ? 0 : 1;
2200 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2203 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
2204 MovieFragmentHeader::~MovieFragmentHeader() {}
2207 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2209 buffer->ReadWriteUInt32(&sequence_number);
2212 uint32_t MovieFragmentHeader::ComputeSizeInternal() {
2213 return HeaderSize() +
sizeof(sequence_number);
2216 TrackFragmentHeader::TrackFragmentHeader()
2218 sample_description_index(0),
2219 default_sample_duration(0),
2220 default_sample_size(0),
2221 default_sample_flags(0) {}
2223 TrackFragmentHeader::~TrackFragmentHeader() {}
2226 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2228 buffer->ReadWriteUInt32(&track_id));
2230 if (flags & kBaseDataOffsetPresentMask) {
2235 uint64_t base_data_offset;
2236 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
2237 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
2238 "default-base-is-moof.";
2241 if (flags & kSampleDescriptionIndexPresentMask) {
2242 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
2243 }
else if (buffer->
Reading()) {
2244 sample_description_index = 0;
2247 if (flags & kDefaultSampleDurationPresentMask) {
2248 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2249 }
else if (buffer->
Reading()) {
2250 default_sample_duration = 0;
2253 if (flags & kDefaultSampleSizePresentMask) {
2254 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2255 }
else if (buffer->
Reading()) {
2256 default_sample_size = 0;
2259 if (flags & kDefaultSampleFlagsPresentMask)
2260 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2264 uint32_t TrackFragmentHeader::ComputeSizeInternal() {
2265 uint32_t box_size =
HeaderSize() +
sizeof(track_id);
2266 if (flags & kSampleDescriptionIndexPresentMask)
2267 box_size +=
sizeof(sample_description_index);
2268 if (flags & kDefaultSampleDurationPresentMask)
2269 box_size +=
sizeof(default_sample_duration);
2270 if (flags & kDefaultSampleSizePresentMask)
2271 box_size +=
sizeof(default_sample_size);
2272 if (flags & kDefaultSampleFlagsPresentMask)
2273 box_size +=
sizeof(default_sample_flags);
2277 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
2278 TrackFragmentRun::~TrackFragmentRun() {}
2281 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
2287 if (flags & kSampleCompTimeOffsetsPresentMask) {
2288 for (uint32_t i = 0; i < sample_count; ++i) {
2289 if (sample_composition_time_offsets[i] < 0) {
2298 buffer->ReadWriteUInt32(&sample_count));
2300 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2301 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2302 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2303 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2304 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2305 bool sample_composition_time_offsets_present =
2306 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2308 if (data_offset_present) {
2309 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2320 uint32_t first_sample_flags;
2323 if (first_sample_flags_present)
2324 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2326 if (sample_duration_present)
2327 sample_durations.resize(sample_count);
2328 if (sample_size_present)
2329 sample_sizes.resize(sample_count);
2330 if (sample_flags_present)
2331 sample_flags.resize(sample_count);
2332 if (sample_composition_time_offsets_present)
2333 sample_composition_time_offsets.resize(sample_count);
2335 if (first_sample_flags_present) {
2336 first_sample_flags = sample_flags[0];
2337 DCHECK(sample_flags.size() == 1);
2338 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2341 if (sample_duration_present)
2342 DCHECK(sample_durations.size() == sample_count);
2343 if (sample_size_present)
2344 DCHECK(sample_sizes.size() == sample_count);
2345 if (sample_flags_present)
2346 DCHECK(sample_flags.size() == sample_count);
2347 if (sample_composition_time_offsets_present)
2348 DCHECK(sample_composition_time_offsets.size() == sample_count);
2351 for (uint32_t i = 0; i < sample_count; ++i) {
2352 if (sample_duration_present)
2353 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2354 if (sample_size_present)
2355 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2356 if (sample_flags_present)
2357 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2359 if (sample_composition_time_offsets_present) {
2361 uint32_t sample_offset = sample_composition_time_offsets[i];
2362 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2363 sample_composition_time_offsets[i] = sample_offset;
2365 int32_t sample_offset = sample_composition_time_offsets[i];
2366 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2367 sample_composition_time_offsets[i] = sample_offset;
2373 if (first_sample_flags_present) {
2374 if (sample_flags.size() == 0) {
2375 sample_flags.push_back(first_sample_flags);
2377 sample_flags[0] = first_sample_flags;
2384 uint32_t TrackFragmentRun::ComputeSizeInternal() {
2385 uint32_t box_size =
HeaderSize() +
sizeof(sample_count);
2386 if (flags & kDataOffsetPresentMask)
2387 box_size +=
sizeof(data_offset);
2388 if (flags & kFirstSampleFlagsPresentMask)
2389 box_size +=
sizeof(uint32_t);
2390 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2391 (flags & kSampleSizePresentMask ? 1 : 0) +
2392 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2393 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2394 box_size += fields *
sizeof(uint32_t) * sample_count;
2398 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2399 TrackFragment::~TrackFragment() {}
2402 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2407 DCHECK(buffer->
reader());
2409 if (!decode_time_absent)
2415 if (!decode_time_absent)
2417 for (uint32_t i = 0; i < runs.size(); ++i)
2419 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2421 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2429 uint32_t TrackFragment::ComputeSizeInternal() {
2434 for (uint32_t i = 0; i < runs.size(); ++i)
2436 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2437 box_size += sample_group_descriptions[i].
ComputeSize();
2438 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2443 MovieFragment::MovieFragment() {}
2444 MovieFragment::~MovieFragment() {}
2447 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2457 for (uint32_t i = 0; i < tracks.size(); ++i)
2459 for (uint32_t i = 0; i < pssh.size(); ++i)
2465 uint32_t MovieFragment::ComputeSizeInternal() {
2467 for (uint32_t i = 0; i < tracks.size(); ++i)
2469 for (uint32_t i = 0; i < pssh.size(); ++i)
2474 SegmentIndex::SegmentIndex()
2477 earliest_presentation_time(0),
2479 SegmentIndex::~SegmentIndex() {}
2482 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2484 buffer->ReadWriteUInt32(&reference_id) &&
2485 buffer->ReadWriteUInt32(×cale));
2487 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2492 uint16_t reference_count = references.size();
2494 buffer->ReadWriteUInt16(&reference_count));
2495 references.resize(reference_count);
2497 uint32_t reference_type_size;
2499 for (uint32_t i = 0; i < reference_count; ++i) {
2501 reference_type_size = references[i].referenced_size;
2502 if (references[i].reference_type)
2503 reference_type_size |= (1 << 31);
2504 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2505 if (references[i].starts_with_sap)
2508 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2509 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2510 buffer->ReadWriteUInt32(&sap));
2512 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2513 references[i].referenced_size = reference_type_size & ~(1 << 31);
2514 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2515 references[i].sap_type =
2516 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2517 references[i].sap_delta_time = sap & ~(0xF << 28);
2523 uint32_t SegmentIndex::ComputeSizeInternal() {
2524 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2525 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2526 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2527 3 *
sizeof(uint32_t) * references.size();
2530 MediaData::MediaData() : data_size(0) {}
2531 MediaData::~MediaData() {}
2534 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2535 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2539 uint32_t MediaData::ComputeSizeInternal() {
2543 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2544 CueSourceIDBox::~CueSourceIDBox() {}
2548 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2553 uint32_t CueSourceIDBox::ComputeSizeInternal() {
2554 if (source_id == kCueSourceIdNotSet)
2559 CueTimeBox::CueTimeBox() {}
2560 CueTimeBox::~CueTimeBox() {}
2566 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2573 uint32_t CueTimeBox::ComputeSizeInternal() {
2574 if (cue_current_time.empty())
2576 return HeaderSize() + cue_current_time.size();
2579 CueIDBox::CueIDBox() {}
2580 CueIDBox::~CueIDBox() {}
2586 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2592 uint32_t CueIDBox::ComputeSizeInternal() {
2598 CueSettingsBox::CueSettingsBox() {}
2599 CueSettingsBox::~CueSettingsBox() {}
2605 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2611 uint32_t CueSettingsBox::ComputeSizeInternal() {
2612 if (settings.empty())
2617 CuePayloadBox::CuePayloadBox() {}
2618 CuePayloadBox::~CuePayloadBox() {}
2624 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2630 uint32_t CuePayloadBox::ComputeSizeInternal() {
2634 VTTEmptyCueBox::VTTEmptyCueBox() {}
2635 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2641 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2645 uint32_t VTTEmptyCueBox::ComputeSizeInternal() {
2649 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2650 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2656 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2659 &cue_additional_text,
2663 uint32_t VTTAdditionalTextBox::ComputeSizeInternal() {
2664 return HeaderSize() + cue_additional_text.size();
2667 VTTCueBox::VTTCueBox() {}
2668 VTTCueBox::~VTTCueBox() {}
2674 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2685 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