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 kAv1CompressorName[] =
"\012AOM Coding";
38 const char kAvcCompressorName[] =
"\012AVC Coding";
39 const char kDolbyVisionCompressorName[] =
"\013DOVI Coding";
40 const char kHevcCompressorName[] =
"\013HEVC Coding";
41 const char kVpcCompressorName[] =
"\012VPC Coding";
46 bool IsIvSizeValid(uint8_t per_sample_iv_size) {
47 return per_sample_iv_size == 0 || per_sample_iv_size == 8 ||
48 per_sample_iv_size == 16;
65 const uint8_t kDdtsExtraData[] = {0xe4, 0x7c, 0, 4, 0, 0x0f, 0};
68 bool IsFitIn32Bits(uint64_t a) {
69 return a <= std::numeric_limits<uint32_t>::max();
72 bool IsFitIn32Bits(int64_t a) {
73 return a <= std::numeric_limits<int32_t>::max() &&
74 a >= std::numeric_limits<int32_t>::min();
77 template <
typename T1,
typename T2>
78 bool IsFitIn32Bits(T1 a1, T2 a2) {
79 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
82 template <
typename T1,
typename T2,
typename T3>
83 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
84 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
95 TrackType FourCCToTrackType(FourCC fourcc) {
108 FourCC TrackTypeToFourCC(TrackType track_type) {
109 switch (track_type) {
121 bool IsProtectionSchemeSupported(FourCC scheme) {
122 return scheme == FOURCC_cenc || scheme == FOURCC_cens ||
123 scheme == FOURCC_cbc1 || scheme == FOURCC_cbcs;
128 FileType::FileType() =
default;
129 FileType::~FileType() =
default;
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 size_t FileType::ComputeSizeInternal() {
153 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
154 kFourCCSize * compatible_brands.size();
161 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() =
default;
162 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() =
default;
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 size_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
182 return raw_box.size();
185 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() =
default;
186 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() =
default;
192 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
197 uint32_t count =
static_cast<uint32_t
>(offsets.size());
198 RCHECK(buffer->ReadWriteUInt32(&count));
199 offsets.resize(count);
201 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
202 for (uint32_t i = 0; i < count; ++i)
207 size_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
209 if (offsets.size() == 0)
211 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
212 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
215 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize() =
default;
216 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() =
default;
222 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
227 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
228 buffer->ReadWriteUInt32(&sample_count));
229 if (default_sample_info_size == 0)
230 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
234 size_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
236 if (sample_count == 0)
238 return HeaderSize() +
sizeof(default_sample_info_size) +
239 sizeof(sample_count) +
240 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
246 DCHECK(IsIvSizeValid(iv_size));
249 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
251 if (!has_subsamples) {
256 uint16_t subsample_count =
static_cast<uint16_t
>(subsamples.size());
257 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
258 RCHECK(subsample_count > 0);
259 subsamples.resize(subsample_count);
260 for (
auto& subsample : subsamples) {
261 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
262 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
270 DCHECK(IsIvSizeValid(iv_size));
273 initialization_vector.resize(iv_size);
274 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
276 if (!has_subsamples) {
281 uint16_t subsample_count;
282 RCHECK(reader->Read2(&subsample_count));
283 RCHECK(subsample_count > 0);
284 subsamples.resize(subsample_count);
285 for (
auto& subsample : subsamples) {
286 RCHECK(reader->Read2(&subsample.clear_bytes) &&
287 reader->Read4(&subsample.cipher_bytes));
293 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
294 const uint16_t subsample_count =
static_cast<uint16_t
>(subsamples.size());
295 return static_cast<uint32_t
>(
296 initialization_vector.size() +
298 ? (
sizeof(subsample_count) + subsample_entry_size * subsample_count)
304 for (uint32_t i = 0; i < subsamples.size(); ++i)
305 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
309 SampleEncryption::SampleEncryption() =
default;
310 SampleEncryption::~SampleEncryption() =
default;
316 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
321 if (buffer->
Reading() && iv_size == SampleEncryption::kInvalidIvSize) {
323 buffer->ReadWriteVector(&sample_encryption_data, buffer->
BytesLeft()));
327 if (!IsIvSizeValid(iv_size)) {
329 <<
"IV_size can only be 8 or 16 or 0 for constant iv, but seeing " 334 uint32_t sample_count =
335 static_cast<uint32_t
>(sample_encryption_entries.size());
336 RCHECK(buffer->ReadWriteUInt32(&sample_count));
338 sample_encryption_entries.resize(sample_count);
339 for (
auto& sample_encryption_entry : sample_encryption_entries) {
340 RCHECK(sample_encryption_entry.ReadWrite(
341 iv_size, (flags & kUseSubsampleEncryption) != 0, buffer) != 0);
346 size_t SampleEncryption::ComputeSizeInternal() {
347 const uint32_t sample_count =
348 static_cast<uint32_t
>(sample_encryption_entries.size());
349 if (sample_count == 0) {
354 DCHECK(IsIvSizeValid(iv_size));
356 if (flags & kUseSubsampleEncryption) {
358 sample_encryption_entries) {
359 box_size += sample_encryption_entry.ComputeSize();
362 box_size += sample_count * iv_size;
369 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
370 DCHECK(IsIvSizeValid(iv_size));
373 sample_encryption_data.size());
374 uint32_t sample_count = 0;
375 RCHECK(reader.Read4(&sample_count));
377 sample_encryption_entries->resize(sample_count);
378 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
379 RCHECK(sample_encryption_entry.ParseFromBuffer(
380 iv_size, (flags & kUseSubsampleEncryption) != 0, &reader) != 0);
385 OriginalFormat::OriginalFormat() =
default;
386 OriginalFormat::~OriginalFormat() =
default;
392 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
396 size_t OriginalFormat::ComputeSizeInternal() {
400 SchemeType::SchemeType() =
default;
401 SchemeType::~SchemeType() =
default;
407 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
409 buffer->ReadWriteUInt32(&version));
413 size_t SchemeType::ComputeSizeInternal() {
414 return HeaderSize() + kFourCCSize +
sizeof(version);
417 TrackEncryption::TrackEncryption() =
default;
418 TrackEncryption::~TrackEncryption() =
default;
424 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
426 if (default_kid.size() != kCencKeyIdSize) {
427 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
428 <<
" bytes; got " << default_kid.size()
429 <<
". Resized accordingly.";
430 default_kid.resize(kCencKeyIdSize);
432 RCHECK(default_crypt_byte_block < 16 && default_skip_byte_block < 16);
433 if (default_crypt_byte_block != 0 && default_skip_byte_block != 0) {
442 uint8_t pattern = default_crypt_byte_block << 4 | default_skip_byte_block;
443 RCHECK(buffer->ReadWriteUInt8(&pattern));
444 default_crypt_byte_block = pattern >> 4;
445 default_skip_byte_block = pattern & 0x0F;
447 RCHECK(buffer->ReadWriteUInt8(&default_is_protected) &&
448 buffer->ReadWriteUInt8(&default_per_sample_iv_size) &&
449 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
451 if (default_is_protected == 1) {
452 if (default_per_sample_iv_size == 0) {
453 uint8_t default_constant_iv_size =
454 static_cast<uint8_t
>(default_constant_iv.size());
455 RCHECK(buffer->ReadWriteUInt8(&default_constant_iv_size));
456 RCHECK(default_constant_iv_size == 8 || default_constant_iv_size == 16);
457 RCHECK(buffer->ReadWriteVector(&default_constant_iv,
458 default_constant_iv_size));
460 RCHECK(default_per_sample_iv_size == 8 ||
461 default_per_sample_iv_size == 16);
462 RCHECK(default_constant_iv.empty());
467 RCHECK(default_is_protected == 0);
468 RCHECK(default_per_sample_iv_size == 0);
469 RCHECK(default_constant_iv.empty());
474 size_t TrackEncryption::ComputeSizeInternal() {
475 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize +
476 (default_constant_iv.empty()
478 : (
sizeof(uint8_t) + default_constant_iv.size()));
481 SchemeInfo::SchemeInfo() =
default;
482 SchemeInfo::~SchemeInfo() =
default;
488 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
494 size_t SchemeInfo::ComputeSizeInternal() {
495 return HeaderSize() + track_encryption.ComputeSize();
498 ProtectionSchemeInfo::ProtectionSchemeInfo() =
default;
499 ProtectionSchemeInfo::~ProtectionSchemeInfo() =
default;
505 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
508 if (IsProtectionSchemeSupported(type.type)) {
511 DLOG(WARNING) <<
"Ignore unsupported protection scheme: " 512 << FourCCToString(type.type);
521 size_t ProtectionSchemeInfo::ComputeSizeInternal() {
523 if (format.format == FOURCC_NULL)
525 return HeaderSize() + format.ComputeSize() + type.ComputeSize() +
529 MovieHeader::MovieHeader() =
default;
530 MovieHeader::~MovieHeader() =
default;
536 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
539 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
542 buffer->ReadWriteUInt32(×cale) &&
545 std::vector<uint8_t> matrix(kUnityMatrix,
546 kUnityMatrix + arraysize(kUnityMatrix));
547 RCHECK(buffer->ReadWriteInt32(&rate) && buffer->ReadWriteInt16(&volume) &&
549 buffer->ReadWriteVector(&matrix, matrix.size()) &&
551 buffer->ReadWriteUInt32(&next_track_id));
555 size_t MovieHeader::ComputeSizeInternal() {
556 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
557 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
558 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
559 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
563 TrackHeader::TrackHeader() {
564 flags = kTrackEnabled | kTrackInMovie | kTrackInPreview;
567 TrackHeader::~TrackHeader() =
default;
573 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
576 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
579 buffer->ReadWriteUInt32(&track_id) &&
586 volume = (width != 0 && height != 0) ? 0 : 0x100;
588 std::vector<uint8_t> matrix(kUnityMatrix,
589 kUnityMatrix + arraysize(kUnityMatrix));
591 buffer->ReadWriteInt16(&layer) &&
592 buffer->ReadWriteInt16(&alternate_group) &&
593 buffer->ReadWriteInt16(&volume) &&
595 buffer->ReadWriteVector(&matrix, matrix.size()) &&
596 buffer->ReadWriteUInt32(&width) && buffer->ReadWriteUInt32(&height));
600 size_t TrackHeader::ComputeSizeInternal() {
601 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
603 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
604 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
605 sizeof(height) +
sizeof(kUnityMatrix) + 14;
608 SampleDescription::SampleDescription() =
default;
609 SampleDescription::~SampleDescription() =
default;
615 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
619 count =
static_cast<uint32_t
>(video_entries.size());
622 count =
static_cast<uint32_t
>(audio_entries.size());
625 count =
static_cast<uint32_t
>(text_entries.size());
628 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
629 <<
" is not handled. Skipping.";
636 video_entries.clear();
637 audio_entries.clear();
640 if (type == kVideo) {
642 RCHECK(video_entries.size() == count);
643 }
else if (type == kAudio) {
645 RCHECK(audio_entries.size() == count);
646 }
else if (type == kText) {
648 RCHECK(text_entries.size() == count);
651 DCHECK_LT(0u, count);
652 if (type == kVideo) {
653 for (uint32_t i = 0; i < count; ++i)
655 }
else if (type == kAudio) {
656 for (uint32_t i = 0; i < count; ++i)
658 }
else if (type == kText) {
659 for (uint32_t i = 0; i < count; ++i)
668 size_t SampleDescription::ComputeSizeInternal() {
670 if (type == kVideo) {
671 for (uint32_t i = 0; i < video_entries.size(); ++i)
673 }
else if (type == kAudio) {
674 for (uint32_t i = 0; i < audio_entries.size(); ++i)
676 }
else if (type == kText) {
677 for (uint32_t i = 0; i < text_entries.size(); ++i)
683 DecodingTimeToSample::DecodingTimeToSample() =
default;
684 DecodingTimeToSample::~DecodingTimeToSample() =
default;
690 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
691 uint32_t count =
static_cast<uint32_t
>(decoding_time.size());
694 decoding_time.resize(count);
695 for (uint32_t i = 0; i < count; ++i) {
696 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
697 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
702 size_t DecodingTimeToSample::ComputeSizeInternal() {
707 CompositionTimeToSample::CompositionTimeToSample() =
default;
708 CompositionTimeToSample::~CompositionTimeToSample() =
default;
714 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
715 uint32_t count =
static_cast<uint32_t
>(composition_offset.size());
721 for (uint32_t i = 0; i < count; ++i) {
722 if (composition_offset[i].sample_offset < 0) {
731 composition_offset.resize(count);
732 for (uint32_t i = 0; i < count; ++i) {
733 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
736 uint32_t sample_offset = composition_offset[i].sample_offset;
737 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
738 composition_offset[i].sample_offset = sample_offset;
740 int32_t sample_offset = composition_offset[i].sample_offset;
741 RCHECK(buffer->ReadWriteInt32(&sample_offset));
742 composition_offset[i].sample_offset = sample_offset;
748 size_t CompositionTimeToSample::ComputeSizeInternal() {
750 if (composition_offset.empty())
755 const size_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
757 kCompositionOffsetSize * composition_offset.size();
760 SampleToChunk::SampleToChunk() =
default;
761 SampleToChunk::~SampleToChunk() =
default;
767 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
768 uint32_t count =
static_cast<uint32_t
>(chunk_info.size());
771 chunk_info.resize(count);
772 for (uint32_t i = 0; i < count; ++i) {
773 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
774 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
775 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
777 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
778 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
783 size_t SampleToChunk::ComputeSizeInternal() {
788 SampleSize::SampleSize() =
default;
789 SampleSize::~SampleSize() =
default;
795 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
797 buffer->ReadWriteUInt32(&sample_size) &&
798 buffer->ReadWriteUInt32(&sample_count));
800 if (sample_size == 0) {
802 sizes.resize(sample_count);
804 DCHECK(sample_count == sizes.size());
805 for (uint32_t i = 0; i < sample_count; ++i)
806 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
811 size_t SampleSize::ComputeSizeInternal() {
812 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
813 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
816 CompactSampleSize::CompactSampleSize() =
default;
817 CompactSampleSize::~CompactSampleSize() =
default;
823 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
824 uint32_t sample_count =
static_cast<uint32_t
>(sizes.size());
826 buffer->ReadWriteUInt8(&field_size) &&
827 buffer->ReadWriteUInt32(&sample_count));
830 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
831 switch (field_size) {
833 for (uint32_t i = 0; i < sample_count; i += 2) {
836 RCHECK(buffer->ReadWriteUInt8(&size));
837 sizes[i] = size >> 4;
838 sizes[i + 1] = size & 0x0F;
840 DCHECK_LT(sizes[i], 16u);
841 DCHECK_LT(sizes[i + 1], 16u);
842 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
843 RCHECK(buffer->ReadWriteUInt8(&size));
848 for (uint32_t i = 0; i < sample_count; ++i) {
849 uint8_t size = sizes[i];
850 RCHECK(buffer->ReadWriteUInt8(&size));
855 for (uint32_t i = 0; i < sample_count; ++i) {
856 uint16_t size = sizes[i];
857 RCHECK(buffer->ReadWriteUInt16(&size));
864 sizes.resize(sample_count);
868 size_t CompactSampleSize::ComputeSizeInternal() {
869 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
870 (field_size * sizes.size() + 7) / 8;
873 ChunkOffset::ChunkOffset() =
default;
874 ChunkOffset::~ChunkOffset() =
default;
880 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
881 uint32_t count =
static_cast<uint32_t
>(offsets.size());
884 offsets.resize(count);
885 for (uint32_t i = 0; i < count; ++i)
890 size_t ChunkOffset::ComputeSizeInternal() {
891 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
894 ChunkLargeOffset::ChunkLargeOffset() =
default;
895 ChunkLargeOffset::~ChunkLargeOffset() =
default;
901 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
902 uint32_t count =
static_cast<uint32_t
>(offsets.size());
906 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
908 stco.offsets.swap(offsets);
911 stco.offsets.swap(offsets);
918 offsets.resize(count);
919 for (uint32_t i = 0; i < count; ++i)
920 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
924 size_t ChunkLargeOffset::ComputeSizeInternal() {
925 uint32_t count =
static_cast<uint32_t
>(offsets.size());
926 int use_large_offset =
927 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
929 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
932 SyncSample::SyncSample() =
default;
933 SyncSample::~SyncSample() =
default;
939 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
940 uint32_t count =
static_cast<uint32_t
>(sample_number.size());
943 sample_number.resize(count);
944 for (uint32_t i = 0; i < count; ++i)
945 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
949 size_t SyncSample::ComputeSizeInternal() {
951 if (sample_number.empty())
954 sizeof(uint32_t) * sample_number.size();
957 bool CencSampleEncryptionInfoEntry::ReadWrite(
BoxBuffer* buffer) {
959 if (key_id.size() != kCencKeyIdSize) {
960 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
961 <<
" bytes; got " << key_id.size()
962 <<
". Resized accordingly.";
963 key_id.resize(kCencKeyIdSize);
965 RCHECK(crypt_byte_block < 16 && skip_byte_block < 16);
970 uint8_t pattern = crypt_byte_block << 4 | skip_byte_block;
971 RCHECK(buffer->ReadWriteUInt8(&pattern));
972 crypt_byte_block = pattern >> 4;
973 skip_byte_block = pattern & 0x0F;
975 RCHECK(buffer->ReadWriteUInt8(&is_protected) &&
976 buffer->ReadWriteUInt8(&per_sample_iv_size) &&
977 buffer->ReadWriteVector(&key_id, kCencKeyIdSize));
979 if (is_protected == 1) {
980 if (per_sample_iv_size == 0) {
981 uint8_t constant_iv_size =
static_cast<uint8_t
>(constant_iv.size());
982 RCHECK(buffer->ReadWriteUInt8(&constant_iv_size));
983 RCHECK(constant_iv_size == 8 || constant_iv_size == 16);
984 RCHECK(buffer->ReadWriteVector(&constant_iv, constant_iv_size));
986 RCHECK(per_sample_iv_size == 8 || per_sample_iv_size == 16);
987 DCHECK(constant_iv.empty());
992 RCHECK(is_protected == 0);
993 RCHECK(per_sample_iv_size == 0);
998 uint32_t CencSampleEncryptionInfoEntry::ComputeSize()
const {
999 return static_cast<uint32_t
>(
1000 sizeof(uint32_t) + kCencKeyIdSize +
1001 (constant_iv.empty() ? 0 : (
sizeof(uint8_t) + constant_iv.size())));
1004 bool AudioRollRecoveryEntry::ReadWrite(
BoxBuffer* buffer) {
1005 RCHECK(buffer->ReadWriteInt16(&roll_distance));
1009 uint32_t AudioRollRecoveryEntry::ComputeSize()
const {
1010 return sizeof(roll_distance);
1013 SampleGroupDescription::SampleGroupDescription() =
default;
1014 SampleGroupDescription::~SampleGroupDescription() =
default;
1020 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
1022 buffer->ReadWriteUInt32(&grouping_type));
1024 switch (grouping_type) {
1026 return ReadWriteEntries(buffer, &cenc_sample_encryption_info_entries);
1028 return ReadWriteEntries(buffer, &audio_roll_recovery_entries);
1031 DLOG(WARNING) <<
"Ignore unsupported sample group: " 1032 << FourCCToString(static_cast<FourCC>(grouping_type));
1037 template <
typename T>
1038 bool SampleGroupDescription::ReadWriteEntries(
BoxBuffer* buffer,
1039 std::vector<T>* entries) {
1040 uint32_t default_length = 0;
1042 DCHECK(!entries->empty());
1043 default_length = (*entries)[0].ComputeSize();
1044 DCHECK_NE(default_length, 0u);
1047 RCHECK(buffer->ReadWriteUInt32(&default_length));
1049 NOTIMPLEMENTED() <<
"Unsupported SampleGroupDescriptionBox 'sgpd' version " 1050 <<
static_cast<int>(version);
1054 uint32_t count =
static_cast<uint32_t
>(entries->size());
1055 RCHECK(buffer->ReadWriteUInt32(&count));
1057 entries->resize(count);
1059 for (T& entry : *entries) {
1061 uint32_t description_length = default_length;
1062 if (buffer->
Reading() && default_length == 0)
1063 RCHECK(buffer->ReadWriteUInt32(&description_length));
1064 RCHECK(entry.ReadWrite(buffer));
1065 RCHECK(entry.ComputeSize() == description_length);
1067 RCHECK(entry.ReadWrite(buffer));
1073 size_t SampleGroupDescription::ComputeSizeInternal() {
1076 size_t entries_size = 0;
1077 switch (grouping_type) {
1079 for (
const auto& entry : cenc_sample_encryption_info_entries)
1080 entries_size += entry.ComputeSize();
1083 for (
const auto& entry : audio_roll_recovery_entries)
1084 entries_size += entry.ComputeSize();
1088 if (entries_size == 0)
1090 return HeaderSize() +
sizeof(grouping_type) +
1091 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
1095 SampleToGroup::SampleToGroup() =
default;
1096 SampleToGroup::~SampleToGroup() =
default;
1102 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
1104 buffer->ReadWriteUInt32(&grouping_type));
1106 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
1108 if (grouping_type != FOURCC_seig && grouping_type != FOURCC_roll) {
1110 DLOG(WARNING) <<
"Ignore unsupported sample group: " 1111 << FourCCToString(static_cast<FourCC>(grouping_type));
1115 uint32_t count =
static_cast<uint32_t
>(entries.size());
1116 RCHECK(buffer->ReadWriteUInt32(&count));
1117 entries.resize(count);
1118 for (uint32_t i = 0; i < count; ++i) {
1119 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
1120 buffer->ReadWriteUInt32(&entries[i].group_description_index));
1125 size_t SampleToGroup::ComputeSizeInternal() {
1127 if (entries.empty())
1129 return HeaderSize() +
sizeof(grouping_type) +
1130 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
1131 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
1134 SampleTable::SampleTable() =
default;
1135 SampleTable::~SampleTable() =
default;
1141 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
1154 RCHECK(reader->
ReadChild(&sample_size));
1157 RCHECK(reader->
ReadChild(&compact_sample_size));
1158 sample_size.sample_size = 0;
1159 sample_size.sample_count =
1160 static_cast<uint32_t
>(compact_sample_size.sizes.size());
1161 sample_size.sizes.swap(compact_sample_size.sizes);
1165 if (reader->
ChildExist(&chunk_large_offset)) {
1166 RCHECK(reader->
ReadChild(&chunk_large_offset));
1169 RCHECK(reader->
ReadChild(&chunk_offset));
1170 chunk_large_offset.offsets.swap(chunk_offset.offsets);
1181 for (
auto& sample_group_description : sample_group_descriptions)
1183 for (
auto& sample_to_group : sample_to_groups)
1189 size_t SampleTable::ComputeSizeInternal() {
1191 decoding_time_to_sample.ComputeSize() +
1192 composition_time_to_sample.ComputeSize() +
1193 sample_to_chunk.ComputeSize() + sample_size.ComputeSize() +
1194 chunk_large_offset.ComputeSize() +
1195 sync_sample.ComputeSize();
1196 for (
auto& sample_group_description : sample_group_descriptions)
1197 box_size += sample_group_description.ComputeSize();
1198 for (
auto& sample_to_group : sample_to_groups)
1199 box_size += sample_to_group.ComputeSize();
1203 EditList::EditList() =
default;
1204 EditList::~EditList() =
default;
1210 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
1211 uint32_t count =
static_cast<uint32_t
>(edits.size());
1213 edits.resize(count);
1215 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1216 for (uint32_t i = 0; i < count; ++i) {
1219 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
1220 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
1221 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
1226 size_t EditList::ComputeSizeInternal() {
1232 for (uint32_t i = 0; i < edits.size(); ++i) {
1233 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
1239 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
1243 Edit::Edit() =
default;
1244 Edit::~Edit() =
default;
1250 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
1255 size_t Edit::ComputeSizeInternal() {
1257 if (list.edits.empty())
1262 HandlerReference::HandlerReference() =
default;
1263 HandlerReference::~HandlerReference() =
default;
1269 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1270 std::vector<uint8_t> handler_name;
1272 switch (handler_type) {
1274 handler_name.assign(kVideoHandlerName,
1275 kVideoHandlerName + arraysize(kVideoHandlerName));
1278 handler_name.assign(kAudioHandlerName,
1279 kAudioHandlerName + arraysize(kAudioHandlerName));
1282 handler_name.assign(kTextHandlerName,
1283 kTextHandlerName + arraysize(kTextHandlerName));
1294 buffer->ReadWriteFourCC(&handler_type));
1297 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1302 size_t HandlerReference::ComputeSizeInternal() {
1304 switch (handler_type) {
1306 box_size +=
sizeof(kVideoHandlerName);
1309 box_size +=
sizeof(kAudioHandlerName);
1312 box_size +=
sizeof(kTextHandlerName);
1322 bool Language::ReadWrite(
BoxBuffer* buffer) {
1326 std::vector<uint8_t> temp;
1327 RCHECK(buffer->ReadWriteVector(&temp, 2));
1330 bit_reader.SkipBits(1);
1332 for (
int i = 0; i < 3; ++i) {
1333 CHECK(bit_reader.ReadBits(5, &language[i]));
1334 language[i] += 0x60;
1336 code.assign(language, 3);
1339 const char kUndefinedLanguage[] =
"und";
1341 code = kUndefinedLanguage;
1342 DCHECK_EQ(code.size(), 3u);
1346 for (
int i = 0; i < 3; ++i)
1347 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1348 RCHECK(buffer->ReadWriteUInt16(&lang));
1353 uint32_t Language::ComputeSize()
const {
1358 ID3v2::ID3v2() =
default;
1359 ID3v2::~ID3v2() =
default;
1365 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1367 buffer->ReadWriteVector(&id3v2_data, buffer->
Reading()
1369 : id3v2_data.size()));
1373 size_t ID3v2::ComputeSizeInternal() {
1375 return id3v2_data.size() == 0
1377 :
HeaderSize() + language.ComputeSize() + id3v2_data.size();
1380 Metadata::Metadata() =
default;
1381 Metadata::~Metadata() =
default;
1387 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1393 size_t Metadata::ComputeSizeInternal() {
1394 size_t id3v2_size = id3v2.ComputeSize();
1396 return id3v2_size == 0 ? 0
1397 :
HeaderSize() + handler.ComputeSize() + id3v2_size;
1400 CodecConfiguration::CodecConfiguration() =
default;
1401 CodecConfiguration::~CodecConfiguration() =
default;
1409 bool CodecConfiguration::ReadWriteInternal(
BoxBuffer* buffer) {
1410 DCHECK_NE(box_type, FOURCC_NULL);
1415 if (box_type == FOURCC_vpcC) {
1417 uint8_t vpcc_version = 1;
1418 uint32_t version_flags = vpcc_version << 24;
1419 RCHECK(buffer->ReadWriteUInt32(&version_flags));
1420 vpcc_version = version_flags >> 24;
1421 RCHECK(vpcc_version == 1);
1425 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1427 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1432 size_t CodecConfiguration::ComputeSizeInternal() {
1435 DCHECK_NE(box_type, FOURCC_NULL);
1436 return HeaderSize() + (box_type == FOURCC_vpcC ? 4 : 0) + data.size();
1439 PixelAspectRatio::PixelAspectRatio() =
default;
1440 PixelAspectRatio::~PixelAspectRatio() =
default;
1446 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1448 buffer->ReadWriteUInt32(&h_spacing) &&
1449 buffer->ReadWriteUInt32(&v_spacing));
1453 size_t PixelAspectRatio::ComputeSizeInternal() {
1455 if (h_spacing == 0 && v_spacing == 0)
1458 DCHECK(h_spacing != 0 && v_spacing != 0);
1459 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1462 VideoSampleEntry::VideoSampleEntry() =
default;
1463 VideoSampleEntry::~VideoSampleEntry() =
default;
1466 if (format == FOURCC_NULL) {
1467 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the " 1468 <<
"handler type recovered in its Media ancestor.";
1473 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1474 std::vector<uint8_t> compressor_name;
1476 DCHECK(buffer->
reader());
1477 format = buffer->
reader()->type();
1481 const FourCC actual_format = GetActualFormat();
1482 switch (actual_format) {
1484 compressor_name.assign(std::begin(kAv1CompressorName),
1485 std::end(kAv1CompressorName));
1489 compressor_name.assign(std::begin(kAvcCompressorName),
1490 std::end(kAvcCompressorName));
1494 compressor_name.assign(std::begin(kDolbyVisionCompressorName),
1495 std::end(kDolbyVisionCompressorName));
1499 compressor_name.assign(std::begin(kHevcCompressorName),
1500 std::end(kHevcCompressorName));
1504 compressor_name.assign(std::begin(kVpcCompressorName),
1505 std::end(kVpcCompressorName));
1508 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1511 compressor_name.resize(kCompressorNameSize);
1514 uint32_t video_resolution = kVideoResolution;
1515 uint16_t video_frame_count = kVideoFrameCount;
1516 uint16_t video_depth = kVideoDepth;
1517 int16_t predefined = -1;
1519 buffer->ReadWriteUInt16(&data_reference_index) &&
1521 buffer->ReadWriteUInt16(&width) && buffer->ReadWriteUInt16(&height) &&
1522 buffer->ReadWriteUInt32(&video_resolution) &&
1523 buffer->ReadWriteUInt32(&video_resolution) &&
1525 buffer->ReadWriteUInt16(&video_frame_count) &&
1526 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1527 buffer->ReadWriteUInt16(&video_depth) &&
1528 buffer->ReadWriteInt16(&predefined));
1535 if (format == FOURCC_encv && buffer->
Reading()) {
1538 while (!IsProtectionSchemeSupported(sinf.type.type))
1542 const FourCC actual_format = GetActualFormat();
1544 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1546 DCHECK_EQ(codec_configuration.box_type,
1547 GetCodecConfigurationBoxType(actual_format));
1549 if (codec_configuration.box_type == FOURCC_NULL)
1555 extra_codec_configs.clear();
1557 const bool is_hevc =
1558 actual_format == FOURCC_dvhe || actual_format == FOURCC_dvh1 ||
1559 actual_format == FOURCC_hev1 || actual_format == FOURCC_hvc1;
1561 for (FourCC fourcc : {FOURCC_dvcC, FOURCC_dvvC, FOURCC_hvcE}) {
1563 dv_box.box_type = fourcc;
1565 if (!dv_box.data.empty())
1566 extra_codec_configs.push_back(std::move(dv_box));
1579 if (format == FOURCC_encv && !buffer->
Reading()) {
1580 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1586 size_t VideoSampleEntry::ComputeSizeInternal() {
1587 const FourCC actual_format = GetActualFormat();
1588 if (actual_format == FOURCC_NULL)
1590 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1591 DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1592 size_t size =
HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1593 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1594 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1595 pixel_aspect.ComputeSize() + sinf.ComputeSize() +
1596 codec_configuration.ComputeSize() + kCompressorNameSize + 6 +
1599 size += codec_config.ComputeSize();
1603 FourCC VideoSampleEntry::GetCodecConfigurationBoxType(FourCC format)
const {
1619 LOG(ERROR) << FourCCToString(format) <<
" is not supported.";
1624 std::vector<uint8_t> VideoSampleEntry::ExtraCodecConfigsAsVector()
const {
1627 codec_config.Write(&buffer);
1628 return std::vector<uint8_t>(buffer.
Buffer(), buffer.
Buffer() + buffer.Size());
1631 bool VideoSampleEntry::ParseExtraCodecConfigsVector(
1632 const std::vector<uint8_t>& data) {
1633 extra_codec_configs.clear();
1635 while (pos < data.size()) {
1637 std::unique_ptr<BoxReader> box_reader(
1639 RCHECK(!err && box_reader);
1642 codec_config.box_type = box_reader->type();
1643 RCHECK(codec_config.
Parse(box_reader.get()));
1644 extra_codec_configs.push_back(std::move(codec_config));
1646 pos += box_reader->pos();
1651 ElementaryStreamDescriptor::ElementaryStreamDescriptor() =
default;
1652 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() =
default;
1658 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1661 std::vector<uint8_t> data;
1662 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1663 RCHECK(es_descriptor.Parse(data));
1664 if (es_descriptor.decoder_config_descriptor().IsAAC()) {
1665 RCHECK(aac_audio_specific_config.Parse(
1666 es_descriptor.decoder_config_descriptor()
1667 .decoder_specific_info_descriptor()
1671 DCHECK(buffer->
writer());
1672 es_descriptor.Write(buffer->
writer());
1677 size_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1679 if (es_descriptor.decoder_config_descriptor().object_type() ==
1680 ObjectType::kForbidden) {
1683 return HeaderSize() + es_descriptor.ComputeSize();
1686 DTSSpecific::DTSSpecific() =
default;
1687 DTSSpecific::~DTSSpecific() =
default;
1694 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1696 buffer->ReadWriteUInt32(&sampling_frequency) &&
1697 buffer->ReadWriteUInt32(&max_bitrate) &&
1698 buffer->ReadWriteUInt32(&avg_bitrate) &&
1699 buffer->ReadWriteUInt8(&pcm_sample_depth));
1702 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
BytesLeft()));
1704 if (extra_data.empty()) {
1705 extra_data.assign(kDdtsExtraData,
1706 kDdtsExtraData +
sizeof(kDdtsExtraData));
1708 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1713 size_t DTSSpecific::ComputeSizeInternal() {
1715 if (sampling_frequency == 0)
1717 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1718 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1719 sizeof(kDdtsExtraData);
1722 AC3Specific::AC3Specific() =
default;
1723 AC3Specific::~AC3Specific() =
default;
1729 bool AC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1731 buffer->ReadWriteVector(
1736 size_t AC3Specific::ComputeSizeInternal() {
1743 EC3Specific::EC3Specific() =
default;
1744 EC3Specific::~EC3Specific() =
default;
1750 bool EC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1753 RCHECK(buffer->ReadWriteVector(&data, size));
1757 size_t EC3Specific::ComputeSizeInternal() {
1764 OpusSpecific::OpusSpecific() =
default;
1765 OpusSpecific::~OpusSpecific() =
default;
1771 bool OpusSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1774 std::vector<uint8_t> data;
1775 const int kMinOpusSpecificBoxDataSize = 11;
1776 RCHECK(buffer->
BytesLeft() >= kMinOpusSpecificBoxDataSize);
1777 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1778 preskip = data[2] + (data[3] << 8);
1783 writer.AppendInt(FOURCC_Head);
1785 const uint8_t kOpusIdentificationHeaderVersion = 1;
1786 data[0] = kOpusIdentificationHeaderVersion;
1787 writer.AppendVector(data);
1788 writer.SwapBuffer(&opus_identification_header);
1792 const size_t kOpusMagicSignatureSize = 8u;
1793 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1796 const uint8_t kOpusSpecificBoxVersion = 0;
1798 buffer->
writer()->AppendArray(
1799 &opus_identification_header[kOpusMagicSignatureSize + 1],
1800 opus_identification_header.size() - kOpusMagicSignatureSize - 1);
1805 size_t OpusSpecific::ComputeSizeInternal() {
1807 if (opus_identification_header.empty())
1811 const size_t kOpusMagicSignatureSize = 8u;
1812 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1813 return HeaderSize() + opus_identification_header.size() -
1814 kOpusMagicSignatureSize;
1817 FlacSpecific::FlacSpecific() =
default;
1818 FlacSpecific::~FlacSpecific() =
default;
1824 bool FlacSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1827 RCHECK(buffer->ReadWriteVector(&data, size));
1831 size_t FlacSpecific::ComputeSizeInternal() {
1838 AudioSampleEntry::AudioSampleEntry() =
default;
1839 AudioSampleEntry::~AudioSampleEntry() =
default;
1842 if (format == FOURCC_NULL) {
1843 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the " 1844 <<
"handler type recovered in its Media ancestor.";
1849 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1851 DCHECK(buffer->
reader());
1852 format = buffer->
reader()->type();
1860 buffer->ReadWriteUInt16(&data_reference_index) &&
1862 buffer->ReadWriteUInt16(&channelcount) &&
1863 buffer->ReadWriteUInt16(&samplesize) &&
1865 buffer->ReadWriteUInt32(&samplerate));
1881 if (format == FOURCC_enca) {
1885 while (!IsProtectionSchemeSupported(sinf.type.type))
1888 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1895 size_t AudioSampleEntry::ComputeSizeInternal() {
1896 if (GetActualFormat() == FOURCC_NULL)
1898 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1899 sizeof(samplesize) +
sizeof(samplerate) + sinf.ComputeSize() +
1900 esds.ComputeSize() + ddts.ComputeSize() + dac3.ComputeSize() +
1901 dec3.ComputeSize() + dops.ComputeSize() + dfla.ComputeSize() +
1907 WebVTTConfigurationBox::WebVTTConfigurationBox() =
default;
1908 WebVTTConfigurationBox::~WebVTTConfigurationBox() =
default;
1914 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1920 size_t WebVTTConfigurationBox::ComputeSizeInternal() {
1924 WebVTTSourceLabelBox::WebVTTSourceLabelBox() =
default;
1925 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() =
default;
1931 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1935 : source_label.size());
1938 size_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1939 if (source_label.empty())
1944 TextSampleEntry::TextSampleEntry() =
default;
1945 TextSampleEntry::~TextSampleEntry() =
default;
1948 if (format == FOURCC_NULL) {
1949 LOG(ERROR) <<
"TextSampleEntry should be parsed according to the " 1950 <<
"handler type recovered in its Media ancestor.";
1955 bool TextSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1957 DCHECK(buffer->
reader());
1958 format = buffer->
reader()->type();
1963 buffer->ReadWriteUInt16(&data_reference_index));
1965 if (format == FOURCC_wvtt) {
1973 size_t TextSampleEntry::ComputeSizeInternal() {
1975 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1976 config.ComputeSize() + label.ComputeSize();
1979 MediaHeader::MediaHeader() =
default;
1980 MediaHeader::~MediaHeader() =
default;
1986 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1989 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1992 buffer->ReadWriteUInt32(×cale) &&
1994 language.ReadWrite(buffer) &&
2000 size_t MediaHeader::ComputeSizeInternal() {
2001 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
2003 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
2007 VideoMediaHeader::VideoMediaHeader() {
2008 const uint32_t kVideoMediaHeaderFlags = 1;
2009 flags = kVideoMediaHeaderFlags;
2012 VideoMediaHeader::~VideoMediaHeader() =
default;
2017 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2019 buffer->ReadWriteUInt16(&graphicsmode) &&
2020 buffer->ReadWriteUInt16(&opcolor_red) &&
2021 buffer->ReadWriteUInt16(&opcolor_green) &&
2022 buffer->ReadWriteUInt16(&opcolor_blue));
2026 size_t VideoMediaHeader::ComputeSizeInternal() {
2027 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
2028 sizeof(opcolor_green) +
sizeof(opcolor_blue);
2031 SoundMediaHeader::SoundMediaHeader() =
default;
2032 SoundMediaHeader::~SoundMediaHeader() =
default;
2038 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2044 size_t SoundMediaHeader::ComputeSizeInternal() {
2045 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
2048 SubtitleMediaHeader::SubtitleMediaHeader() =
default;
2049 SubtitleMediaHeader::~SubtitleMediaHeader() =
default;
2055 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2059 size_t SubtitleMediaHeader::ComputeSizeInternal() {
2063 DataEntryUrl::DataEntryUrl() {
2064 const uint32_t kDataEntryUrlFlags = 1;
2065 flags = kDataEntryUrlFlags;
2068 DataEntryUrl::~DataEntryUrl() =
default;
2073 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
2076 RCHECK(buffer->ReadWriteVector(&location, buffer->
BytesLeft()));
2078 RCHECK(buffer->ReadWriteVector(&location, location.size()));
2083 size_t DataEntryUrl::ComputeSizeInternal() {
2087 DataReference::DataReference() =
default;
2088 DataReference::~DataReference() =
default;
2093 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
2094 uint32_t entry_count =
static_cast<uint32_t
>(data_entry.size());
2096 buffer->ReadWriteUInt32(&entry_count));
2097 data_entry.resize(entry_count);
2099 for (uint32_t i = 0; i < entry_count; ++i)
2104 size_t DataReference::ComputeSizeInternal() {
2105 uint32_t count =
static_cast<uint32_t
>(data_entry.size());
2107 for (uint32_t i = 0; i < count; ++i)
2112 DataInformation::DataInformation() =
default;
2113 DataInformation::~DataInformation() =
default;
2119 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2124 size_t DataInformation::ComputeSizeInternal() {
2128 MediaInformation::MediaInformation() =
default;
2129 MediaInformation::~MediaInformation() =
default;
2135 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2139 switch (sample_table.description.type) {
2156 size_t MediaInformation::ComputeSizeInternal() {
2158 HeaderSize() + dinf.ComputeSize() + sample_table.ComputeSize();
2159 switch (sample_table.description.type) {
2161 box_size += vmhd.ComputeSize();
2164 box_size += smhd.ComputeSize();
2167 box_size += sthd.ComputeSize();
2175 Media::Media() =
default;
2176 Media::~Media() =
default;
2182 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
2193 information.sample_table.description.type =
2194 FourCCToTrackType(handler.handler_type);
2196 handler.handler_type =
2197 TrackTypeToFourCC(information.sample_table.description.type);
2198 RCHECK(handler.handler_type != FOURCC_NULL);
2205 size_t Media::ComputeSizeInternal() {
2206 handler.handler_type =
2207 TrackTypeToFourCC(information.sample_table.description.type);
2208 return HeaderSize() + header.ComputeSize() + handler.ComputeSize() +
2209 information.ComputeSize();
2212 Track::Track() =
default;
2213 Track::~Track() =
default;
2219 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
2227 size_t Track::ComputeSizeInternal() {
2228 return HeaderSize() + header.ComputeSize() + media.ComputeSize() +
2232 MovieExtendsHeader::MovieExtendsHeader() =
default;
2233 MovieExtendsHeader::~MovieExtendsHeader() =
default;
2239 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2241 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2246 size_t MovieExtendsHeader::ComputeSizeInternal() {
2248 if (fragment_duration == 0)
2250 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
2251 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2254 TrackExtends::TrackExtends() =
default;
2255 TrackExtends::~TrackExtends() =
default;
2261 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2263 buffer->ReadWriteUInt32(&track_id) &&
2264 buffer->ReadWriteUInt32(&default_sample_description_index) &&
2265 buffer->ReadWriteUInt32(&default_sample_duration) &&
2266 buffer->ReadWriteUInt32(&default_sample_size) &&
2267 buffer->ReadWriteUInt32(&default_sample_flags));
2271 size_t TrackExtends::ComputeSizeInternal() {
2273 sizeof(default_sample_description_index) +
2274 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
2275 sizeof(default_sample_flags);
2278 MovieExtends::MovieExtends() =
default;
2279 MovieExtends::~MovieExtends() =
default;
2285 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2289 DCHECK(buffer->
reader());
2292 for (uint32_t i = 0; i < tracks.size(); ++i)
2298 size_t MovieExtends::ComputeSizeInternal() {
2300 if (tracks.size() == 0)
2303 for (uint32_t i = 0; i < tracks.size(); ++i)
2308 Movie::Movie() =
default;
2309 Movie::~Movie() =
default;
2315 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
2331 for (uint32_t i = 0; i < tracks.size(); ++i)
2334 for (uint32_t i = 0; i < pssh.size(); ++i)
2340 size_t Movie::ComputeSizeInternal() {
2342 metadata.ComputeSize() + extends.ComputeSize();
2343 for (uint32_t i = 0; i < tracks.size(); ++i)
2345 for (uint32_t i = 0; i < pssh.size(); ++i)
2350 TrackFragmentDecodeTime::TrackFragmentDecodeTime() =
default;
2351 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() =
default;
2357 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
2359 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2364 size_t TrackFragmentDecodeTime::ComputeSizeInternal() {
2365 version = IsFitIn32Bits(decode_time) ? 0 : 1;
2366 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2369 MovieFragmentHeader::MovieFragmentHeader() =
default;
2370 MovieFragmentHeader::~MovieFragmentHeader() =
default;
2376 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2378 buffer->ReadWriteUInt32(&sequence_number);
2381 size_t MovieFragmentHeader::ComputeSizeInternal() {
2382 return HeaderSize() +
sizeof(sequence_number);
2385 TrackFragmentHeader::TrackFragmentHeader() =
default;
2386 TrackFragmentHeader::~TrackFragmentHeader() =
default;
2392 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2395 if (flags & kBaseDataOffsetPresentMask) {
2400 uint64_t base_data_offset;
2401 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
2402 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes " 2403 "default-base-is-moof.";
2406 if (flags & kSampleDescriptionIndexPresentMask) {
2407 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
2408 }
else if (buffer->
Reading()) {
2409 sample_description_index = 0;
2412 if (flags & kDefaultSampleDurationPresentMask) {
2413 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2414 }
else if (buffer->
Reading()) {
2415 default_sample_duration = 0;
2418 if (flags & kDefaultSampleSizePresentMask) {
2419 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2420 }
else if (buffer->
Reading()) {
2421 default_sample_size = 0;
2424 if (flags & kDefaultSampleFlagsPresentMask)
2425 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2429 size_t TrackFragmentHeader::ComputeSizeInternal() {
2431 if (flags & kSampleDescriptionIndexPresentMask)
2432 box_size +=
sizeof(sample_description_index);
2433 if (flags & kDefaultSampleDurationPresentMask)
2434 box_size +=
sizeof(default_sample_duration);
2435 if (flags & kDefaultSampleSizePresentMask)
2436 box_size +=
sizeof(default_sample_size);
2437 if (flags & kDefaultSampleFlagsPresentMask)
2438 box_size +=
sizeof(default_sample_flags);
2442 TrackFragmentRun::TrackFragmentRun() =
default;
2443 TrackFragmentRun::~TrackFragmentRun() =
default;
2449 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
2455 if (flags & kSampleCompTimeOffsetsPresentMask) {
2456 for (uint32_t i = 0; i < sample_count; ++i) {
2457 if (sample_composition_time_offsets[i] < 0) {
2466 buffer->ReadWriteUInt32(&sample_count));
2468 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2469 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2470 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2471 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2472 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2473 bool sample_composition_time_offsets_present =
2474 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2476 if (data_offset_present) {
2477 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2488 uint32_t first_sample_flags(0);
2491 if (first_sample_flags_present)
2492 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2494 if (sample_duration_present)
2495 sample_durations.resize(sample_count);
2496 if (sample_size_present)
2497 sample_sizes.resize(sample_count);
2498 if (sample_flags_present)
2499 sample_flags.resize(sample_count);
2500 if (sample_composition_time_offsets_present)
2501 sample_composition_time_offsets.resize(sample_count);
2503 if (first_sample_flags_present) {
2504 first_sample_flags = sample_flags[0];
2505 DCHECK(sample_flags.size() == 1);
2506 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2509 if (sample_duration_present)
2510 DCHECK(sample_durations.size() == sample_count);
2511 if (sample_size_present)
2512 DCHECK(sample_sizes.size() == sample_count);
2513 if (sample_flags_present)
2514 DCHECK(sample_flags.size() == sample_count);
2515 if (sample_composition_time_offsets_present)
2516 DCHECK(sample_composition_time_offsets.size() == sample_count);
2519 for (uint32_t i = 0; i < sample_count; ++i) {
2520 if (sample_duration_present)
2521 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2522 if (sample_size_present)
2523 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2524 if (sample_flags_present)
2525 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2527 if (sample_composition_time_offsets_present) {
2529 uint32_t sample_offset = sample_composition_time_offsets[i];
2530 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2531 sample_composition_time_offsets[i] = sample_offset;
2533 int32_t sample_offset = sample_composition_time_offsets[i];
2534 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2535 sample_composition_time_offsets[i] = sample_offset;
2541 if (first_sample_flags_present) {
2542 if (sample_flags.size() == 0) {
2543 sample_flags.push_back(first_sample_flags);
2545 sample_flags[0] = first_sample_flags;
2552 size_t TrackFragmentRun::ComputeSizeInternal() {
2554 if (flags & kDataOffsetPresentMask)
2555 box_size +=
sizeof(data_offset);
2556 if (flags & kFirstSampleFlagsPresentMask)
2557 box_size +=
sizeof(uint32_t);
2558 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2559 (flags & kSampleSizePresentMask ? 1 : 0) +
2560 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2561 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2562 box_size += fields *
sizeof(uint32_t) * sample_count;
2566 TrackFragment::TrackFragment() =
default;
2567 TrackFragment::~TrackFragment() =
default;
2573 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2577 DCHECK(buffer->
reader());
2579 if (!decode_time_absent)
2585 if (!decode_time_absent)
2587 for (uint32_t i = 0; i < runs.size(); ++i)
2589 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2591 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2599 size_t TrackFragment::ComputeSizeInternal() {
2601 decode_time.ComputeSize() + auxiliary_size.ComputeSize() +
2602 auxiliary_offset.ComputeSize() +
2603 sample_encryption.ComputeSize();
2604 for (uint32_t i = 0; i < runs.size(); ++i)
2606 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2607 box_size += sample_group_descriptions[i].
ComputeSize();
2608 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2613 MovieFragment::MovieFragment() =
default;
2614 MovieFragment::~MovieFragment() =
default;
2620 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2628 for (uint32_t i = 0; i < tracks.size(); ++i)
2630 for (uint32_t i = 0; i < pssh.size(); ++i)
2636 size_t MovieFragment::ComputeSizeInternal() {
2638 for (uint32_t i = 0; i < tracks.size(); ++i)
2640 for (uint32_t i = 0; i < pssh.size(); ++i)
2645 SegmentIndex::SegmentIndex() =
default;
2646 SegmentIndex::~SegmentIndex() =
default;
2652 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2654 buffer->ReadWriteUInt32(&reference_id) &&
2655 buffer->ReadWriteUInt32(×cale));
2657 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2662 uint16_t reference_count =
static_cast<uint16_t
>(references.size());
2664 buffer->ReadWriteUInt16(&reference_count));
2665 references.resize(reference_count);
2667 uint32_t reference_type_size;
2669 for (uint32_t i = 0; i < reference_count; ++i) {
2671 reference_type_size = references[i].referenced_size;
2672 if (references[i].reference_type)
2673 reference_type_size |= (1 << 31);
2674 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2675 if (references[i].starts_with_sap)
2678 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2679 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2680 buffer->ReadWriteUInt32(&sap));
2682 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2683 references[i].referenced_size = reference_type_size & ~(1 << 31);
2684 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2685 references[i].sap_type =
2686 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2687 references[i].sap_delta_time = sap & ~(0xF << 28);
2693 size_t SegmentIndex::ComputeSizeInternal() {
2694 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2695 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2696 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2697 3 *
sizeof(uint32_t) * references.size();
2700 MediaData::MediaData() =
default;
2701 MediaData::~MediaData() =
default;
2707 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2708 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2712 size_t MediaData::ComputeSizeInternal() {
2716 CueSourceIDBox::CueSourceIDBox() =
default;
2717 CueSourceIDBox::~CueSourceIDBox() =
default;
2723 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2728 size_t CueSourceIDBox::ComputeSizeInternal() {
2729 if (source_id == kCueSourceIdNotSet)
2734 CueTimeBox::CueTimeBox() =
default;
2735 CueTimeBox::~CueTimeBox() =
default;
2741 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2748 size_t CueTimeBox::ComputeSizeInternal() {
2749 if (cue_current_time.empty())
2751 return HeaderSize() + cue_current_time.size();
2754 CueIDBox::CueIDBox() =
default;
2755 CueIDBox::~CueIDBox() =
default;
2761 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2767 size_t CueIDBox::ComputeSizeInternal() {
2773 CueSettingsBox::CueSettingsBox() =
default;
2774 CueSettingsBox::~CueSettingsBox() =
default;
2780 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2786 size_t CueSettingsBox::ComputeSizeInternal() {
2787 if (settings.empty())
2792 CuePayloadBox::CuePayloadBox() =
default;
2793 CuePayloadBox::~CuePayloadBox() =
default;
2799 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2805 size_t CuePayloadBox::ComputeSizeInternal() {
2809 VTTEmptyCueBox::VTTEmptyCueBox() =
default;
2810 VTTEmptyCueBox::~VTTEmptyCueBox() =
default;
2816 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2820 size_t VTTEmptyCueBox::ComputeSizeInternal() {
2824 VTTAdditionalTextBox::VTTAdditionalTextBox() =
default;
2825 VTTAdditionalTextBox::~VTTAdditionalTextBox() =
default;
2831 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2834 &cue_additional_text,
2838 size_t VTTAdditionalTextBox::ComputeSizeInternal() {
2839 return HeaderSize() + cue_additional_text.size();
2842 VTTCueBox::VTTCueBox() =
default;
2843 VTTCueBox::~VTTCueBox() =
default;
2849 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2859 size_t VTTCueBox::ComputeSizeInternal() {
2860 return HeaderSize() + cue_source_id.ComputeSize() + cue_id.ComputeSize() +
2861 cue_time.ComputeSize() + cue_settings.ComputeSize() +
2862 cue_payload.ComputeSize();
FourCC BoxType() const override
bool ParseFromBuffer(uint8_t iv_size, bool has_subsamples, BufferReader *reader)
uint32_t GetTotalSizeOfSubsamples() const
uint32_t ComputeSize() const
FourCC BoxType() const override
FourCC BoxType() const override
All the methods that are virtual are virtual for mocking.
FourCC BoxType() const override
bool ReadWrite(uint8_t iv_size, bool has_subsamples, BoxBuffer *buffer)
FourCC BoxType() const override