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 kHevcCompressorName[] =
"\013HEVC Coding";
40 const char kVpcCompressorName[] =
"\012VPC Coding";
45 bool IsIvSizeValid(uint8_t per_sample_iv_size) {
46 return per_sample_iv_size == 0 || per_sample_iv_size == 8 ||
47 per_sample_iv_size == 16;
64 const uint8_t kDdtsExtraData[] = {0xe4, 0x7c, 0, 4, 0, 0x0f, 0};
67 bool IsFitIn32Bits(uint64_t a) {
68 return a <= std::numeric_limits<uint32_t>::max();
71 bool IsFitIn32Bits(int64_t a) {
72 return a <= std::numeric_limits<int32_t>::max() &&
73 a >= std::numeric_limits<int32_t>::min();
76 template <
typename T1,
typename T2>
77 bool IsFitIn32Bits(T1 a1, T2 a2) {
78 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
81 template <
typename T1,
typename T2,
typename T3>
82 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
83 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
94 TrackType FourCCToTrackType(FourCC fourcc) {
107 FourCC TrackTypeToFourCC(TrackType track_type) {
108 switch (track_type) {
120 bool IsProtectionSchemeSupported(FourCC scheme) {
121 return scheme == FOURCC_cenc || scheme == FOURCC_cens ||
122 scheme == FOURCC_cbc1 || scheme == FOURCC_cbcs;
127 FileType::FileType() =
default;
128 FileType::~FileType() =
default;
134 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
136 buffer->ReadWriteFourCC(&major_brand) &&
137 buffer->ReadWriteUInt32(&minor_version));
140 RCHECK(buffer->
BytesLeft() %
sizeof(FourCC) == 0);
141 num_brands = buffer->
BytesLeft() /
sizeof(FourCC);
142 compatible_brands.resize(num_brands);
144 num_brands = compatible_brands.size();
146 for (
size_t i = 0; i < num_brands; ++i)
147 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
151 size_t FileType::ComputeSizeInternal() {
152 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
153 kFourCCSize * compatible_brands.size();
160 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() =
default;
161 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() =
default;
167 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
171 raw_box.assign(reader->data(), reader->data() + reader->size());
173 DCHECK(!raw_box.empty());
174 buffer->
writer()->AppendVector(raw_box);
180 size_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
181 return raw_box.size();
184 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() =
default;
185 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() =
default;
191 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
196 uint32_t count =
static_cast<uint32_t
>(offsets.size());
197 RCHECK(buffer->ReadWriteUInt32(&count));
198 offsets.resize(count);
200 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
201 for (uint32_t i = 0; i < count; ++i)
206 size_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
208 if (offsets.size() == 0)
210 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
211 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
214 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize() =
default;
215 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() =
default;
221 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
226 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
227 buffer->ReadWriteUInt32(&sample_count));
228 if (default_sample_info_size == 0)
229 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
233 size_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
235 if (sample_count == 0)
237 return HeaderSize() +
sizeof(default_sample_info_size) +
238 sizeof(sample_count) +
239 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
245 DCHECK(IsIvSizeValid(iv_size));
248 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
250 if (!has_subsamples) {
255 uint16_t subsample_count =
static_cast<uint16_t
>(subsamples.size());
256 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
257 RCHECK(subsample_count > 0);
258 subsamples.resize(subsample_count);
259 for (
auto& subsample : subsamples) {
260 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
261 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
269 DCHECK(IsIvSizeValid(iv_size));
272 initialization_vector.resize(iv_size);
273 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
275 if (!has_subsamples) {
280 uint16_t subsample_count;
281 RCHECK(reader->Read2(&subsample_count));
282 RCHECK(subsample_count > 0);
283 subsamples.resize(subsample_count);
284 for (
auto& subsample : subsamples) {
285 RCHECK(reader->Read2(&subsample.clear_bytes) &&
286 reader->Read4(&subsample.cipher_bytes));
292 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
293 const uint16_t subsample_count =
static_cast<uint16_t
>(subsamples.size());
294 return static_cast<uint32_t
>(
295 initialization_vector.size() +
297 ? (
sizeof(subsample_count) + subsample_entry_size * subsample_count)
303 for (uint32_t i = 0; i < subsamples.size(); ++i)
304 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
308 SampleEncryption::SampleEncryption() =
default;
309 SampleEncryption::~SampleEncryption() =
default;
315 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
320 if (buffer->
Reading() && iv_size == SampleEncryption::kInvalidIvSize) {
322 buffer->ReadWriteVector(&sample_encryption_data, buffer->
BytesLeft()));
326 if (!IsIvSizeValid(iv_size)) {
328 <<
"IV_size can only be 8 or 16 or 0 for constant iv, but seeing " 333 uint32_t sample_count =
334 static_cast<uint32_t
>(sample_encryption_entries.size());
335 RCHECK(buffer->ReadWriteUInt32(&sample_count));
337 sample_encryption_entries.resize(sample_count);
338 for (
auto& sample_encryption_entry : sample_encryption_entries) {
339 RCHECK(sample_encryption_entry.ReadWrite(
340 iv_size, (flags & kUseSubsampleEncryption) != 0, buffer) != 0);
345 size_t SampleEncryption::ComputeSizeInternal() {
346 const uint32_t sample_count =
347 static_cast<uint32_t
>(sample_encryption_entries.size());
348 if (sample_count == 0) {
353 DCHECK(IsIvSizeValid(iv_size));
355 if (flags & kUseSubsampleEncryption) {
357 sample_encryption_entries) {
358 box_size += sample_encryption_entry.ComputeSize();
361 box_size += sample_count * iv_size;
368 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
369 DCHECK(IsIvSizeValid(iv_size));
372 sample_encryption_data.size());
373 uint32_t sample_count = 0;
374 RCHECK(reader.Read4(&sample_count));
376 sample_encryption_entries->resize(sample_count);
377 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
378 RCHECK(sample_encryption_entry.ParseFromBuffer(
379 iv_size, (flags & kUseSubsampleEncryption) != 0, &reader) != 0);
384 OriginalFormat::OriginalFormat() =
default;
385 OriginalFormat::~OriginalFormat() =
default;
391 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
395 size_t OriginalFormat::ComputeSizeInternal() {
399 SchemeType::SchemeType() =
default;
400 SchemeType::~SchemeType() =
default;
406 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
408 buffer->ReadWriteUInt32(&version));
412 size_t SchemeType::ComputeSizeInternal() {
413 return HeaderSize() + kFourCCSize +
sizeof(version);
416 TrackEncryption::TrackEncryption() =
default;
417 TrackEncryption::~TrackEncryption() =
default;
423 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
425 if (default_kid.size() != kCencKeyIdSize) {
426 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
427 <<
" bytes; got " << default_kid.size()
428 <<
". Resized accordingly.";
429 default_kid.resize(kCencKeyIdSize);
431 RCHECK(default_crypt_byte_block < 16 && default_skip_byte_block < 16);
432 if (default_crypt_byte_block != 0 && default_skip_byte_block != 0) {
441 uint8_t pattern = default_crypt_byte_block << 4 | default_skip_byte_block;
442 RCHECK(buffer->ReadWriteUInt8(&pattern));
443 default_crypt_byte_block = pattern >> 4;
444 default_skip_byte_block = pattern & 0x0F;
446 RCHECK(buffer->ReadWriteUInt8(&default_is_protected) &&
447 buffer->ReadWriteUInt8(&default_per_sample_iv_size) &&
448 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
450 if (default_is_protected == 1) {
451 if (default_per_sample_iv_size == 0) {
452 uint8_t default_constant_iv_size =
453 static_cast<uint8_t
>(default_constant_iv.size());
454 RCHECK(buffer->ReadWriteUInt8(&default_constant_iv_size));
455 RCHECK(default_constant_iv_size == 8 || default_constant_iv_size == 16);
456 RCHECK(buffer->ReadWriteVector(&default_constant_iv,
457 default_constant_iv_size));
459 RCHECK(default_per_sample_iv_size == 8 ||
460 default_per_sample_iv_size == 16);
461 RCHECK(default_constant_iv.empty());
466 RCHECK(default_is_protected == 0);
467 RCHECK(default_per_sample_iv_size == 0);
468 RCHECK(default_constant_iv.empty());
473 size_t TrackEncryption::ComputeSizeInternal() {
474 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize +
475 (default_constant_iv.empty()
477 : (
sizeof(uint8_t) + default_constant_iv.size()));
480 SchemeInfo::SchemeInfo() =
default;
481 SchemeInfo::~SchemeInfo() =
default;
487 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
493 size_t SchemeInfo::ComputeSizeInternal() {
494 return HeaderSize() + track_encryption.ComputeSize();
497 ProtectionSchemeInfo::ProtectionSchemeInfo() =
default;
498 ProtectionSchemeInfo::~ProtectionSchemeInfo() =
default;
504 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
507 if (IsProtectionSchemeSupported(type.type)) {
510 DLOG(WARNING) <<
"Ignore unsupported protection scheme: " 511 << FourCCToString(type.type);
520 size_t ProtectionSchemeInfo::ComputeSizeInternal() {
522 if (format.format == FOURCC_NULL)
524 return HeaderSize() + format.ComputeSize() + type.ComputeSize() +
528 MovieHeader::MovieHeader() =
default;
529 MovieHeader::~MovieHeader() =
default;
535 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
538 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
541 buffer->ReadWriteUInt32(×cale) &&
544 std::vector<uint8_t> matrix(kUnityMatrix,
545 kUnityMatrix + arraysize(kUnityMatrix));
546 RCHECK(buffer->ReadWriteInt32(&rate) && buffer->ReadWriteInt16(&volume) &&
548 buffer->ReadWriteVector(&matrix, matrix.size()) &&
550 buffer->ReadWriteUInt32(&next_track_id));
554 size_t MovieHeader::ComputeSizeInternal() {
555 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
556 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
557 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
558 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
562 TrackHeader::TrackHeader() {
563 flags = kTrackEnabled | kTrackInMovie | kTrackInPreview;
566 TrackHeader::~TrackHeader() =
default;
572 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
575 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
578 buffer->ReadWriteUInt32(&track_id) &&
585 volume = (width != 0 && height != 0) ? 0 : 0x100;
587 std::vector<uint8_t> matrix(kUnityMatrix,
588 kUnityMatrix + arraysize(kUnityMatrix));
590 buffer->ReadWriteInt16(&layer) &&
591 buffer->ReadWriteInt16(&alternate_group) &&
592 buffer->ReadWriteInt16(&volume) &&
594 buffer->ReadWriteVector(&matrix, matrix.size()) &&
595 buffer->ReadWriteUInt32(&width) && buffer->ReadWriteUInt32(&height));
599 size_t TrackHeader::ComputeSizeInternal() {
600 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
602 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
603 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
604 sizeof(height) +
sizeof(kUnityMatrix) + 14;
607 SampleDescription::SampleDescription() =
default;
608 SampleDescription::~SampleDescription() =
default;
614 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
618 count =
static_cast<uint32_t
>(video_entries.size());
621 count =
static_cast<uint32_t
>(audio_entries.size());
624 count =
static_cast<uint32_t
>(text_entries.size());
627 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
628 <<
" is not handled. Skipping.";
635 video_entries.clear();
636 audio_entries.clear();
639 if (type == kVideo) {
641 RCHECK(video_entries.size() == count);
642 }
else if (type == kAudio) {
644 RCHECK(audio_entries.size() == count);
645 }
else if (type == kText) {
647 RCHECK(text_entries.size() == count);
650 DCHECK_LT(0u, count);
651 if (type == kVideo) {
652 for (uint32_t i = 0; i < count; ++i)
654 }
else if (type == kAudio) {
655 for (uint32_t i = 0; i < count; ++i)
657 }
else if (type == kText) {
658 for (uint32_t i = 0; i < count; ++i)
667 size_t SampleDescription::ComputeSizeInternal() {
669 if (type == kVideo) {
670 for (uint32_t i = 0; i < video_entries.size(); ++i)
672 }
else if (type == kAudio) {
673 for (uint32_t i = 0; i < audio_entries.size(); ++i)
675 }
else if (type == kText) {
676 for (uint32_t i = 0; i < text_entries.size(); ++i)
682 DecodingTimeToSample::DecodingTimeToSample() =
default;
683 DecodingTimeToSample::~DecodingTimeToSample() =
default;
689 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
690 uint32_t count =
static_cast<uint32_t
>(decoding_time.size());
693 decoding_time.resize(count);
694 for (uint32_t i = 0; i < count; ++i) {
695 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
696 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
701 size_t DecodingTimeToSample::ComputeSizeInternal() {
706 CompositionTimeToSample::CompositionTimeToSample() =
default;
707 CompositionTimeToSample::~CompositionTimeToSample() =
default;
713 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
714 uint32_t count =
static_cast<uint32_t
>(composition_offset.size());
720 for (uint32_t i = 0; i < count; ++i) {
721 if (composition_offset[i].sample_offset < 0) {
730 composition_offset.resize(count);
731 for (uint32_t i = 0; i < count; ++i) {
732 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
735 uint32_t sample_offset = composition_offset[i].sample_offset;
736 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
737 composition_offset[i].sample_offset = sample_offset;
739 int32_t sample_offset = composition_offset[i].sample_offset;
740 RCHECK(buffer->ReadWriteInt32(&sample_offset));
741 composition_offset[i].sample_offset = sample_offset;
747 size_t CompositionTimeToSample::ComputeSizeInternal() {
749 if (composition_offset.empty())
754 const size_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
756 kCompositionOffsetSize * composition_offset.size();
759 SampleToChunk::SampleToChunk() =
default;
760 SampleToChunk::~SampleToChunk() =
default;
766 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
767 uint32_t count =
static_cast<uint32_t
>(chunk_info.size());
770 chunk_info.resize(count);
771 for (uint32_t i = 0; i < count; ++i) {
772 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
773 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
774 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
776 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
777 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
782 size_t SampleToChunk::ComputeSizeInternal() {
787 SampleSize::SampleSize() =
default;
788 SampleSize::~SampleSize() =
default;
794 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
796 buffer->ReadWriteUInt32(&sample_size) &&
797 buffer->ReadWriteUInt32(&sample_count));
799 if (sample_size == 0) {
801 sizes.resize(sample_count);
803 DCHECK(sample_count == sizes.size());
804 for (uint32_t i = 0; i < sample_count; ++i)
805 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
810 size_t SampleSize::ComputeSizeInternal() {
811 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
812 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
815 CompactSampleSize::CompactSampleSize() =
default;
816 CompactSampleSize::~CompactSampleSize() =
default;
822 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
823 uint32_t sample_count =
static_cast<uint32_t
>(sizes.size());
825 buffer->ReadWriteUInt8(&field_size) &&
826 buffer->ReadWriteUInt32(&sample_count));
829 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
830 switch (field_size) {
832 for (uint32_t i = 0; i < sample_count; i += 2) {
835 RCHECK(buffer->ReadWriteUInt8(&size));
836 sizes[i] = size >> 4;
837 sizes[i + 1] = size & 0x0F;
839 DCHECK_LT(sizes[i], 16u);
840 DCHECK_LT(sizes[i + 1], 16u);
841 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
842 RCHECK(buffer->ReadWriteUInt8(&size));
847 for (uint32_t i = 0; i < sample_count; ++i) {
848 uint8_t size = sizes[i];
849 RCHECK(buffer->ReadWriteUInt8(&size));
854 for (uint32_t i = 0; i < sample_count; ++i) {
855 uint16_t size = sizes[i];
856 RCHECK(buffer->ReadWriteUInt16(&size));
863 sizes.resize(sample_count);
867 size_t CompactSampleSize::ComputeSizeInternal() {
868 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
869 (field_size * sizes.size() + 7) / 8;
872 ChunkOffset::ChunkOffset() =
default;
873 ChunkOffset::~ChunkOffset() =
default;
879 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
880 uint32_t count =
static_cast<uint32_t
>(offsets.size());
883 offsets.resize(count);
884 for (uint32_t i = 0; i < count; ++i)
889 size_t ChunkOffset::ComputeSizeInternal() {
890 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
893 ChunkLargeOffset::ChunkLargeOffset() =
default;
894 ChunkLargeOffset::~ChunkLargeOffset() =
default;
900 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
901 uint32_t count =
static_cast<uint32_t
>(offsets.size());
905 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
907 stco.offsets.swap(offsets);
910 stco.offsets.swap(offsets);
917 offsets.resize(count);
918 for (uint32_t i = 0; i < count; ++i)
919 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
923 size_t ChunkLargeOffset::ComputeSizeInternal() {
924 uint32_t count =
static_cast<uint32_t
>(offsets.size());
925 int use_large_offset =
926 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
928 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
931 SyncSample::SyncSample() =
default;
932 SyncSample::~SyncSample() =
default;
938 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
939 uint32_t count =
static_cast<uint32_t
>(sample_number.size());
942 sample_number.resize(count);
943 for (uint32_t i = 0; i < count; ++i)
944 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
948 size_t SyncSample::ComputeSizeInternal() {
950 if (sample_number.empty())
953 sizeof(uint32_t) * sample_number.size();
956 bool CencSampleEncryptionInfoEntry::ReadWrite(
BoxBuffer* buffer) {
958 if (key_id.size() != kCencKeyIdSize) {
959 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
960 <<
" bytes; got " << key_id.size()
961 <<
". Resized accordingly.";
962 key_id.resize(kCencKeyIdSize);
964 RCHECK(crypt_byte_block < 16 && skip_byte_block < 16);
969 uint8_t pattern = crypt_byte_block << 4 | skip_byte_block;
970 RCHECK(buffer->ReadWriteUInt8(&pattern));
971 crypt_byte_block = pattern >> 4;
972 skip_byte_block = pattern & 0x0F;
974 RCHECK(buffer->ReadWriteUInt8(&is_protected) &&
975 buffer->ReadWriteUInt8(&per_sample_iv_size) &&
976 buffer->ReadWriteVector(&key_id, kCencKeyIdSize));
978 if (is_protected == 1) {
979 if (per_sample_iv_size == 0) {
980 uint8_t constant_iv_size =
static_cast<uint8_t
>(constant_iv.size());
981 RCHECK(buffer->ReadWriteUInt8(&constant_iv_size));
982 RCHECK(constant_iv_size == 8 || constant_iv_size == 16);
983 RCHECK(buffer->ReadWriteVector(&constant_iv, constant_iv_size));
985 RCHECK(per_sample_iv_size == 8 || per_sample_iv_size == 16);
986 DCHECK(constant_iv.empty());
991 RCHECK(is_protected == 0);
992 RCHECK(per_sample_iv_size == 0);
997 uint32_t CencSampleEncryptionInfoEntry::ComputeSize()
const {
998 return static_cast<uint32_t
>(
999 sizeof(uint32_t) + kCencKeyIdSize +
1000 (constant_iv.empty() ? 0 : (
sizeof(uint8_t) + constant_iv.size())));
1003 bool AudioRollRecoveryEntry::ReadWrite(
BoxBuffer* buffer) {
1004 RCHECK(buffer->ReadWriteInt16(&roll_distance));
1008 uint32_t AudioRollRecoveryEntry::ComputeSize()
const {
1009 return sizeof(roll_distance);
1012 SampleGroupDescription::SampleGroupDescription() =
default;
1013 SampleGroupDescription::~SampleGroupDescription() =
default;
1019 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
1021 buffer->ReadWriteUInt32(&grouping_type));
1023 switch (grouping_type) {
1025 return ReadWriteEntries(buffer, &cenc_sample_encryption_info_entries);
1027 return ReadWriteEntries(buffer, &audio_roll_recovery_entries);
1030 DLOG(WARNING) <<
"Ignore unsupported sample group: " 1031 << FourCCToString(static_cast<FourCC>(grouping_type));
1036 template <
typename T>
1037 bool SampleGroupDescription::ReadWriteEntries(
BoxBuffer* buffer,
1038 std::vector<T>* entries) {
1039 uint32_t default_length = 0;
1041 DCHECK(!entries->empty());
1042 default_length = (*entries)[0].ComputeSize();
1043 DCHECK_NE(default_length, 0u);
1046 RCHECK(buffer->ReadWriteUInt32(&default_length));
1048 NOTIMPLEMENTED() <<
"Unsupported SampleGroupDescriptionBox 'sgpd' version " 1049 <<
static_cast<int>(version);
1053 uint32_t count =
static_cast<uint32_t
>(entries->size());
1054 RCHECK(buffer->ReadWriteUInt32(&count));
1056 entries->resize(count);
1058 for (T& entry : *entries) {
1060 uint32_t description_length = default_length;
1061 if (buffer->
Reading() && default_length == 0)
1062 RCHECK(buffer->ReadWriteUInt32(&description_length));
1063 RCHECK(entry.ReadWrite(buffer));
1064 RCHECK(entry.ComputeSize() == description_length);
1066 RCHECK(entry.ReadWrite(buffer));
1072 size_t SampleGroupDescription::ComputeSizeInternal() {
1075 size_t entries_size = 0;
1076 switch (grouping_type) {
1078 for (
const auto& entry : cenc_sample_encryption_info_entries)
1079 entries_size += entry.ComputeSize();
1082 for (
const auto& entry : audio_roll_recovery_entries)
1083 entries_size += entry.ComputeSize();
1087 if (entries_size == 0)
1089 return HeaderSize() +
sizeof(grouping_type) +
1090 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
1094 SampleToGroup::SampleToGroup() =
default;
1095 SampleToGroup::~SampleToGroup() =
default;
1101 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
1103 buffer->ReadWriteUInt32(&grouping_type));
1105 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
1107 if (grouping_type != FOURCC_seig && grouping_type != FOURCC_roll) {
1109 DLOG(WARNING) <<
"Ignore unsupported sample group: " 1110 << FourCCToString(static_cast<FourCC>(grouping_type));
1114 uint32_t count =
static_cast<uint32_t
>(entries.size());
1115 RCHECK(buffer->ReadWriteUInt32(&count));
1116 entries.resize(count);
1117 for (uint32_t i = 0; i < count; ++i) {
1118 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
1119 buffer->ReadWriteUInt32(&entries[i].group_description_index));
1124 size_t SampleToGroup::ComputeSizeInternal() {
1126 if (entries.empty())
1128 return HeaderSize() +
sizeof(grouping_type) +
1129 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
1130 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
1133 SampleTable::SampleTable() =
default;
1134 SampleTable::~SampleTable() =
default;
1140 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
1153 RCHECK(reader->
ReadChild(&sample_size));
1156 RCHECK(reader->
ReadChild(&compact_sample_size));
1157 sample_size.sample_size = 0;
1158 sample_size.sample_count =
1159 static_cast<uint32_t
>(compact_sample_size.sizes.size());
1160 sample_size.sizes.swap(compact_sample_size.sizes);
1164 if (reader->
ChildExist(&chunk_large_offset)) {
1165 RCHECK(reader->
ReadChild(&chunk_large_offset));
1168 RCHECK(reader->
ReadChild(&chunk_offset));
1169 chunk_large_offset.offsets.swap(chunk_offset.offsets);
1180 for (
auto& sample_group_description : sample_group_descriptions)
1182 for (
auto& sample_to_group : sample_to_groups)
1188 size_t SampleTable::ComputeSizeInternal() {
1190 decoding_time_to_sample.ComputeSize() +
1191 composition_time_to_sample.ComputeSize() +
1192 sample_to_chunk.ComputeSize() + sample_size.ComputeSize() +
1193 chunk_large_offset.ComputeSize() +
1194 sync_sample.ComputeSize();
1195 for (
auto& sample_group_description : sample_group_descriptions)
1196 box_size += sample_group_description.ComputeSize();
1197 for (
auto& sample_to_group : sample_to_groups)
1198 box_size += sample_to_group.ComputeSize();
1202 EditList::EditList() =
default;
1203 EditList::~EditList() =
default;
1209 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
1210 uint32_t count =
static_cast<uint32_t
>(edits.size());
1212 edits.resize(count);
1214 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1215 for (uint32_t i = 0; i < count; ++i) {
1218 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
1219 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
1220 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
1225 size_t EditList::ComputeSizeInternal() {
1231 for (uint32_t i = 0; i < edits.size(); ++i) {
1232 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
1238 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
1242 Edit::Edit() =
default;
1243 Edit::~Edit() =
default;
1249 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
1254 size_t Edit::ComputeSizeInternal() {
1256 if (list.edits.empty())
1261 HandlerReference::HandlerReference() =
default;
1262 HandlerReference::~HandlerReference() =
default;
1268 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1269 std::vector<uint8_t> handler_name;
1271 switch (handler_type) {
1273 handler_name.assign(kVideoHandlerName,
1274 kVideoHandlerName + arraysize(kVideoHandlerName));
1277 handler_name.assign(kAudioHandlerName,
1278 kAudioHandlerName + arraysize(kAudioHandlerName));
1281 handler_name.assign(kTextHandlerName,
1282 kTextHandlerName + arraysize(kTextHandlerName));
1293 buffer->ReadWriteFourCC(&handler_type));
1296 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1301 size_t HandlerReference::ComputeSizeInternal() {
1303 switch (handler_type) {
1305 box_size +=
sizeof(kVideoHandlerName);
1308 box_size +=
sizeof(kAudioHandlerName);
1311 box_size +=
sizeof(kTextHandlerName);
1321 bool Language::ReadWrite(
BoxBuffer* buffer) {
1325 std::vector<uint8_t> temp;
1326 RCHECK(buffer->ReadWriteVector(&temp, 2));
1329 bit_reader.SkipBits(1);
1331 for (
int i = 0; i < 3; ++i) {
1332 CHECK(bit_reader.ReadBits(5, &language[i]));
1333 language[i] += 0x60;
1335 code.assign(language, 3);
1338 const char kUndefinedLanguage[] =
"und";
1340 code = kUndefinedLanguage;
1341 DCHECK_EQ(code.size(), 3u);
1345 for (
int i = 0; i < 3; ++i)
1346 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1347 RCHECK(buffer->ReadWriteUInt16(&lang));
1352 uint32_t Language::ComputeSize()
const {
1357 ID3v2::ID3v2() =
default;
1358 ID3v2::~ID3v2() =
default;
1364 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1366 buffer->ReadWriteVector(&id3v2_data, buffer->
Reading()
1368 : id3v2_data.size()));
1372 size_t ID3v2::ComputeSizeInternal() {
1374 return id3v2_data.size() == 0
1376 :
HeaderSize() + language.ComputeSize() + id3v2_data.size();
1379 Metadata::Metadata() =
default;
1380 Metadata::~Metadata() =
default;
1386 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1392 size_t Metadata::ComputeSizeInternal() {
1393 size_t id3v2_size = id3v2.ComputeSize();
1395 return id3v2_size == 0 ? 0
1396 :
HeaderSize() + handler.ComputeSize() + id3v2_size;
1399 CodecConfiguration::CodecConfiguration() =
default;
1400 CodecConfiguration::~CodecConfiguration() =
default;
1408 bool CodecConfiguration::ReadWriteInternal(
BoxBuffer* buffer) {
1409 DCHECK_NE(box_type, FOURCC_NULL);
1414 if (box_type == FOURCC_vpcC) {
1416 uint8_t vpcc_version = 1;
1417 uint32_t version_flags = vpcc_version << 24;
1418 RCHECK(buffer->ReadWriteUInt32(&version_flags));
1419 vpcc_version = version_flags >> 24;
1420 RCHECK(vpcc_version == 1);
1424 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1426 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1431 size_t CodecConfiguration::ComputeSizeInternal() {
1434 DCHECK_NE(box_type, FOURCC_NULL);
1435 return HeaderSize() + (box_type == FOURCC_vpcC ? 4 : 0) + data.size();
1438 PixelAspectRatio::PixelAspectRatio() =
default;
1439 PixelAspectRatio::~PixelAspectRatio() =
default;
1445 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1447 buffer->ReadWriteUInt32(&h_spacing) &&
1448 buffer->ReadWriteUInt32(&v_spacing));
1452 size_t PixelAspectRatio::ComputeSizeInternal() {
1454 if (h_spacing == 0 && v_spacing == 0)
1457 DCHECK(h_spacing != 0 && v_spacing != 0);
1458 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1461 VideoSampleEntry::VideoSampleEntry() =
default;
1462 VideoSampleEntry::~VideoSampleEntry() =
default;
1465 if (format == FOURCC_NULL) {
1466 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the " 1467 <<
"handler type recovered in its Media ancestor.";
1472 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1473 std::vector<uint8_t> compressor_name;
1475 DCHECK(buffer->
reader());
1476 format = buffer->
reader()->type();
1480 const FourCC actual_format = GetActualFormat();
1481 switch (actual_format) {
1483 compressor_name.assign(std::begin(kAv1CompressorName),
1484 std::end(kAv1CompressorName));
1488 compressor_name.assign(std::begin(kAvcCompressorName),
1489 std::end(kAvcCompressorName));
1493 compressor_name.assign(std::begin(kHevcCompressorName),
1494 std::end(kHevcCompressorName));
1498 compressor_name.assign(std::begin(kVpcCompressorName),
1499 std::end(kVpcCompressorName));
1502 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1505 compressor_name.resize(kCompressorNameSize);
1508 uint32_t video_resolution = kVideoResolution;
1509 uint16_t video_frame_count = kVideoFrameCount;
1510 uint16_t video_depth = kVideoDepth;
1511 int16_t predefined = -1;
1513 buffer->ReadWriteUInt16(&data_reference_index) &&
1515 buffer->ReadWriteUInt16(&width) && buffer->ReadWriteUInt16(&height) &&
1516 buffer->ReadWriteUInt32(&video_resolution) &&
1517 buffer->ReadWriteUInt32(&video_resolution) &&
1519 buffer->ReadWriteUInt16(&video_frame_count) &&
1520 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1521 buffer->ReadWriteUInt16(&video_depth) &&
1522 buffer->ReadWriteInt16(&predefined));
1529 if (format == FOURCC_encv && buffer->
Reading()) {
1532 while (!IsProtectionSchemeSupported(sinf.type.type))
1536 const FourCC actual_format = GetActualFormat();
1538 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1540 DCHECK_EQ(codec_configuration.box_type,
1541 GetCodecConfigurationBoxType(actual_format));
1543 if (codec_configuration.box_type == FOURCC_NULL)
1552 if (format == FOURCC_encv && !buffer->
Reading()) {
1553 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1559 size_t VideoSampleEntry::ComputeSizeInternal() {
1560 const FourCC actual_format = GetActualFormat();
1561 if (actual_format == FOURCC_NULL)
1563 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1564 DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1565 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1566 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1567 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1568 pixel_aspect.ComputeSize() + sinf.ComputeSize() +
1569 codec_configuration.ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1573 FourCC VideoSampleEntry::GetCodecConfigurationBoxType(FourCC format)
const {
1587 LOG(ERROR) << FourCCToString(format) <<
" is not supported.";
1592 ElementaryStreamDescriptor::ElementaryStreamDescriptor() =
default;
1593 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() =
default;
1599 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1602 std::vector<uint8_t> data;
1603 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1604 RCHECK(es_descriptor.Parse(data));
1605 if (es_descriptor.IsAAC()) {
1606 RCHECK(aac_audio_specific_config.Parse(
1607 es_descriptor.decoder_specific_info()));
1610 DCHECK(buffer->
writer());
1611 es_descriptor.Write(buffer->
writer());
1616 size_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1618 if (es_descriptor.object_type() == ObjectType::kForbidden)
1620 return HeaderSize() + es_descriptor.ComputeSize();
1623 DTSSpecific::DTSSpecific() =
default;
1624 DTSSpecific::~DTSSpecific() =
default;
1631 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1633 buffer->ReadWriteUInt32(&sampling_frequency) &&
1634 buffer->ReadWriteUInt32(&max_bitrate) &&
1635 buffer->ReadWriteUInt32(&avg_bitrate) &&
1636 buffer->ReadWriteUInt8(&pcm_sample_depth));
1639 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
BytesLeft()));
1641 if (extra_data.empty()) {
1642 extra_data.assign(kDdtsExtraData,
1643 kDdtsExtraData +
sizeof(kDdtsExtraData));
1645 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1650 size_t DTSSpecific::ComputeSizeInternal() {
1652 if (sampling_frequency == 0)
1654 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1655 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1656 sizeof(kDdtsExtraData);
1659 AC3Specific::AC3Specific() =
default;
1660 AC3Specific::~AC3Specific() =
default;
1666 bool AC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1668 buffer->ReadWriteVector(
1673 size_t AC3Specific::ComputeSizeInternal() {
1680 EC3Specific::EC3Specific() =
default;
1681 EC3Specific::~EC3Specific() =
default;
1687 bool EC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1690 RCHECK(buffer->ReadWriteVector(&data, size));
1694 size_t EC3Specific::ComputeSizeInternal() {
1701 OpusSpecific::OpusSpecific() =
default;
1702 OpusSpecific::~OpusSpecific() =
default;
1708 bool OpusSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1711 std::vector<uint8_t> data;
1712 const int kMinOpusSpecificBoxDataSize = 11;
1713 RCHECK(buffer->
BytesLeft() >= kMinOpusSpecificBoxDataSize);
1714 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1715 preskip = data[2] + (data[3] << 8);
1720 writer.AppendInt(FOURCC_Head);
1722 const uint8_t kOpusIdentificationHeaderVersion = 1;
1723 data[0] = kOpusIdentificationHeaderVersion;
1724 writer.AppendVector(data);
1725 writer.SwapBuffer(&opus_identification_header);
1729 const size_t kOpusMagicSignatureSize = 8u;
1730 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1733 const uint8_t kOpusSpecificBoxVersion = 0;
1735 buffer->
writer()->AppendArray(
1736 &opus_identification_header[kOpusMagicSignatureSize + 1],
1737 opus_identification_header.size() - kOpusMagicSignatureSize - 1);
1742 size_t OpusSpecific::ComputeSizeInternal() {
1744 if (opus_identification_header.empty())
1748 const size_t kOpusMagicSignatureSize = 8u;
1749 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1750 return HeaderSize() + opus_identification_header.size() -
1751 kOpusMagicSignatureSize;
1754 FlacSpecific::FlacSpecific() =
default;
1755 FlacSpecific::~FlacSpecific() =
default;
1761 bool FlacSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1764 RCHECK(buffer->ReadWriteVector(&data, size));
1768 size_t FlacSpecific::ComputeSizeInternal() {
1775 AudioSampleEntry::AudioSampleEntry() =
default;
1776 AudioSampleEntry::~AudioSampleEntry() =
default;
1779 if (format == FOURCC_NULL) {
1780 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the " 1781 <<
"handler type recovered in its Media ancestor.";
1786 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1788 DCHECK(buffer->
reader());
1789 format = buffer->
reader()->type();
1797 buffer->ReadWriteUInt16(&data_reference_index) &&
1799 buffer->ReadWriteUInt16(&channelcount) &&
1800 buffer->ReadWriteUInt16(&samplesize) &&
1802 buffer->ReadWriteUInt32(&samplerate));
1818 if (format == FOURCC_enca) {
1822 while (!IsProtectionSchemeSupported(sinf.type.type))
1825 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1832 size_t AudioSampleEntry::ComputeSizeInternal() {
1833 if (GetActualFormat() == FOURCC_NULL)
1835 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1836 sizeof(samplesize) +
sizeof(samplerate) + sinf.ComputeSize() +
1837 esds.ComputeSize() + ddts.ComputeSize() + dac3.ComputeSize() +
1838 dec3.ComputeSize() + dops.ComputeSize() + dfla.ComputeSize() +
1844 WebVTTConfigurationBox::WebVTTConfigurationBox() =
default;
1845 WebVTTConfigurationBox::~WebVTTConfigurationBox() =
default;
1851 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1857 size_t WebVTTConfigurationBox::ComputeSizeInternal() {
1861 WebVTTSourceLabelBox::WebVTTSourceLabelBox() =
default;
1862 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() =
default;
1868 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1872 : source_label.size());
1875 size_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1876 if (source_label.empty())
1881 TextSampleEntry::TextSampleEntry() =
default;
1882 TextSampleEntry::~TextSampleEntry() =
default;
1885 if (format == FOURCC_NULL) {
1886 LOG(ERROR) <<
"TextSampleEntry should be parsed according to the " 1887 <<
"handler type recovered in its Media ancestor.";
1892 bool TextSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1894 DCHECK(buffer->
reader());
1895 format = buffer->
reader()->type();
1900 buffer->ReadWriteUInt16(&data_reference_index));
1902 if (format == FOURCC_wvtt) {
1910 size_t TextSampleEntry::ComputeSizeInternal() {
1912 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1913 config.ComputeSize() + label.ComputeSize();
1916 MediaHeader::MediaHeader() =
default;
1917 MediaHeader::~MediaHeader() =
default;
1923 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1926 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1929 buffer->ReadWriteUInt32(×cale) &&
1931 language.ReadWrite(buffer) &&
1937 size_t MediaHeader::ComputeSizeInternal() {
1938 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1940 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1944 VideoMediaHeader::VideoMediaHeader() {
1945 const uint32_t kVideoMediaHeaderFlags = 1;
1946 flags = kVideoMediaHeaderFlags;
1949 VideoMediaHeader::~VideoMediaHeader() =
default;
1954 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1956 buffer->ReadWriteUInt16(&graphicsmode) &&
1957 buffer->ReadWriteUInt16(&opcolor_red) &&
1958 buffer->ReadWriteUInt16(&opcolor_green) &&
1959 buffer->ReadWriteUInt16(&opcolor_blue));
1963 size_t VideoMediaHeader::ComputeSizeInternal() {
1964 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1965 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1968 SoundMediaHeader::SoundMediaHeader() =
default;
1969 SoundMediaHeader::~SoundMediaHeader() =
default;
1975 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1981 size_t SoundMediaHeader::ComputeSizeInternal() {
1982 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1985 SubtitleMediaHeader::SubtitleMediaHeader() =
default;
1986 SubtitleMediaHeader::~SubtitleMediaHeader() =
default;
1992 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1996 size_t SubtitleMediaHeader::ComputeSizeInternal() {
2000 DataEntryUrl::DataEntryUrl() {
2001 const uint32_t kDataEntryUrlFlags = 1;
2002 flags = kDataEntryUrlFlags;
2005 DataEntryUrl::~DataEntryUrl() =
default;
2010 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
2013 RCHECK(buffer->ReadWriteVector(&location, buffer->
BytesLeft()));
2015 RCHECK(buffer->ReadWriteVector(&location, location.size()));
2020 size_t DataEntryUrl::ComputeSizeInternal() {
2024 DataReference::DataReference() =
default;
2025 DataReference::~DataReference() =
default;
2030 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
2031 uint32_t entry_count =
static_cast<uint32_t
>(data_entry.size());
2033 buffer->ReadWriteUInt32(&entry_count));
2034 data_entry.resize(entry_count);
2036 for (uint32_t i = 0; i < entry_count; ++i)
2041 size_t DataReference::ComputeSizeInternal() {
2042 uint32_t count =
static_cast<uint32_t
>(data_entry.size());
2044 for (uint32_t i = 0; i < count; ++i)
2049 DataInformation::DataInformation() =
default;
2050 DataInformation::~DataInformation() =
default;
2056 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2061 size_t DataInformation::ComputeSizeInternal() {
2065 MediaInformation::MediaInformation() =
default;
2066 MediaInformation::~MediaInformation() =
default;
2072 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2076 switch (sample_table.description.type) {
2093 size_t MediaInformation::ComputeSizeInternal() {
2095 HeaderSize() + dinf.ComputeSize() + sample_table.ComputeSize();
2096 switch (sample_table.description.type) {
2098 box_size += vmhd.ComputeSize();
2101 box_size += smhd.ComputeSize();
2104 box_size += sthd.ComputeSize();
2112 Media::Media() =
default;
2113 Media::~Media() =
default;
2119 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
2130 information.sample_table.description.type =
2131 FourCCToTrackType(handler.handler_type);
2133 handler.handler_type =
2134 TrackTypeToFourCC(information.sample_table.description.type);
2135 RCHECK(handler.handler_type != FOURCC_NULL);
2142 size_t Media::ComputeSizeInternal() {
2143 handler.handler_type =
2144 TrackTypeToFourCC(information.sample_table.description.type);
2145 return HeaderSize() + header.ComputeSize() + handler.ComputeSize() +
2146 information.ComputeSize();
2149 Track::Track() =
default;
2150 Track::~Track() =
default;
2156 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
2164 size_t Track::ComputeSizeInternal() {
2165 return HeaderSize() + header.ComputeSize() + media.ComputeSize() +
2169 MovieExtendsHeader::MovieExtendsHeader() =
default;
2170 MovieExtendsHeader::~MovieExtendsHeader() =
default;
2176 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2178 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2183 size_t MovieExtendsHeader::ComputeSizeInternal() {
2185 if (fragment_duration == 0)
2187 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
2188 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2191 TrackExtends::TrackExtends() =
default;
2192 TrackExtends::~TrackExtends() =
default;
2198 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2200 buffer->ReadWriteUInt32(&track_id) &&
2201 buffer->ReadWriteUInt32(&default_sample_description_index) &&
2202 buffer->ReadWriteUInt32(&default_sample_duration) &&
2203 buffer->ReadWriteUInt32(&default_sample_size) &&
2204 buffer->ReadWriteUInt32(&default_sample_flags));
2208 size_t TrackExtends::ComputeSizeInternal() {
2210 sizeof(default_sample_description_index) +
2211 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
2212 sizeof(default_sample_flags);
2215 MovieExtends::MovieExtends() =
default;
2216 MovieExtends::~MovieExtends() =
default;
2222 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2226 DCHECK(buffer->
reader());
2229 for (uint32_t i = 0; i < tracks.size(); ++i)
2235 size_t MovieExtends::ComputeSizeInternal() {
2237 if (tracks.size() == 0)
2240 for (uint32_t i = 0; i < tracks.size(); ++i)
2245 Movie::Movie() =
default;
2246 Movie::~Movie() =
default;
2252 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
2268 for (uint32_t i = 0; i < tracks.size(); ++i)
2271 for (uint32_t i = 0; i < pssh.size(); ++i)
2277 size_t Movie::ComputeSizeInternal() {
2279 metadata.ComputeSize() + extends.ComputeSize();
2280 for (uint32_t i = 0; i < tracks.size(); ++i)
2282 for (uint32_t i = 0; i < pssh.size(); ++i)
2287 TrackFragmentDecodeTime::TrackFragmentDecodeTime() =
default;
2288 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() =
default;
2294 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
2296 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2301 size_t TrackFragmentDecodeTime::ComputeSizeInternal() {
2302 version = IsFitIn32Bits(decode_time) ? 0 : 1;
2303 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2306 MovieFragmentHeader::MovieFragmentHeader() =
default;
2307 MovieFragmentHeader::~MovieFragmentHeader() =
default;
2313 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2315 buffer->ReadWriteUInt32(&sequence_number);
2318 size_t MovieFragmentHeader::ComputeSizeInternal() {
2319 return HeaderSize() +
sizeof(sequence_number);
2322 TrackFragmentHeader::TrackFragmentHeader() =
default;
2323 TrackFragmentHeader::~TrackFragmentHeader() =
default;
2329 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2332 if (flags & kBaseDataOffsetPresentMask) {
2337 uint64_t base_data_offset;
2338 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
2339 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes " 2340 "default-base-is-moof.";
2343 if (flags & kSampleDescriptionIndexPresentMask) {
2344 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
2345 }
else if (buffer->
Reading()) {
2346 sample_description_index = 0;
2349 if (flags & kDefaultSampleDurationPresentMask) {
2350 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2351 }
else if (buffer->
Reading()) {
2352 default_sample_duration = 0;
2355 if (flags & kDefaultSampleSizePresentMask) {
2356 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2357 }
else if (buffer->
Reading()) {
2358 default_sample_size = 0;
2361 if (flags & kDefaultSampleFlagsPresentMask)
2362 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2366 size_t TrackFragmentHeader::ComputeSizeInternal() {
2368 if (flags & kSampleDescriptionIndexPresentMask)
2369 box_size +=
sizeof(sample_description_index);
2370 if (flags & kDefaultSampleDurationPresentMask)
2371 box_size +=
sizeof(default_sample_duration);
2372 if (flags & kDefaultSampleSizePresentMask)
2373 box_size +=
sizeof(default_sample_size);
2374 if (flags & kDefaultSampleFlagsPresentMask)
2375 box_size +=
sizeof(default_sample_flags);
2379 TrackFragmentRun::TrackFragmentRun() =
default;
2380 TrackFragmentRun::~TrackFragmentRun() =
default;
2386 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
2392 if (flags & kSampleCompTimeOffsetsPresentMask) {
2393 for (uint32_t i = 0; i < sample_count; ++i) {
2394 if (sample_composition_time_offsets[i] < 0) {
2403 buffer->ReadWriteUInt32(&sample_count));
2405 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2406 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2407 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2408 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2409 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2410 bool sample_composition_time_offsets_present =
2411 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2413 if (data_offset_present) {
2414 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2425 uint32_t first_sample_flags(0);
2428 if (first_sample_flags_present)
2429 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2431 if (sample_duration_present)
2432 sample_durations.resize(sample_count);
2433 if (sample_size_present)
2434 sample_sizes.resize(sample_count);
2435 if (sample_flags_present)
2436 sample_flags.resize(sample_count);
2437 if (sample_composition_time_offsets_present)
2438 sample_composition_time_offsets.resize(sample_count);
2440 if (first_sample_flags_present) {
2441 first_sample_flags = sample_flags[0];
2442 DCHECK(sample_flags.size() == 1);
2443 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2446 if (sample_duration_present)
2447 DCHECK(sample_durations.size() == sample_count);
2448 if (sample_size_present)
2449 DCHECK(sample_sizes.size() == sample_count);
2450 if (sample_flags_present)
2451 DCHECK(sample_flags.size() == sample_count);
2452 if (sample_composition_time_offsets_present)
2453 DCHECK(sample_composition_time_offsets.size() == sample_count);
2456 for (uint32_t i = 0; i < sample_count; ++i) {
2457 if (sample_duration_present)
2458 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2459 if (sample_size_present)
2460 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2461 if (sample_flags_present)
2462 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2464 if (sample_composition_time_offsets_present) {
2466 uint32_t sample_offset = sample_composition_time_offsets[i];
2467 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2468 sample_composition_time_offsets[i] = sample_offset;
2470 int32_t sample_offset = sample_composition_time_offsets[i];
2471 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2472 sample_composition_time_offsets[i] = sample_offset;
2478 if (first_sample_flags_present) {
2479 if (sample_flags.size() == 0) {
2480 sample_flags.push_back(first_sample_flags);
2482 sample_flags[0] = first_sample_flags;
2489 size_t TrackFragmentRun::ComputeSizeInternal() {
2491 if (flags & kDataOffsetPresentMask)
2492 box_size +=
sizeof(data_offset);
2493 if (flags & kFirstSampleFlagsPresentMask)
2494 box_size +=
sizeof(uint32_t);
2495 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2496 (flags & kSampleSizePresentMask ? 1 : 0) +
2497 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2498 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2499 box_size += fields *
sizeof(uint32_t) * sample_count;
2503 TrackFragment::TrackFragment() =
default;
2504 TrackFragment::~TrackFragment() =
default;
2510 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2514 DCHECK(buffer->
reader());
2516 if (!decode_time_absent)
2522 if (!decode_time_absent)
2524 for (uint32_t i = 0; i < runs.size(); ++i)
2526 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2528 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2536 size_t TrackFragment::ComputeSizeInternal() {
2538 decode_time.ComputeSize() + auxiliary_size.ComputeSize() +
2539 auxiliary_offset.ComputeSize() +
2540 sample_encryption.ComputeSize();
2541 for (uint32_t i = 0; i < runs.size(); ++i)
2543 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2544 box_size += sample_group_descriptions[i].
ComputeSize();
2545 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2550 MovieFragment::MovieFragment() =
default;
2551 MovieFragment::~MovieFragment() =
default;
2557 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2565 for (uint32_t i = 0; i < tracks.size(); ++i)
2567 for (uint32_t i = 0; i < pssh.size(); ++i)
2573 size_t MovieFragment::ComputeSizeInternal() {
2575 for (uint32_t i = 0; i < tracks.size(); ++i)
2577 for (uint32_t i = 0; i < pssh.size(); ++i)
2582 SegmentIndex::SegmentIndex() =
default;
2583 SegmentIndex::~SegmentIndex() =
default;
2589 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2591 buffer->ReadWriteUInt32(&reference_id) &&
2592 buffer->ReadWriteUInt32(×cale));
2594 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2599 uint16_t reference_count =
static_cast<uint16_t
>(references.size());
2601 buffer->ReadWriteUInt16(&reference_count));
2602 references.resize(reference_count);
2604 uint32_t reference_type_size;
2606 for (uint32_t i = 0; i < reference_count; ++i) {
2608 reference_type_size = references[i].referenced_size;
2609 if (references[i].reference_type)
2610 reference_type_size |= (1 << 31);
2611 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2612 if (references[i].starts_with_sap)
2615 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2616 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2617 buffer->ReadWriteUInt32(&sap));
2619 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2620 references[i].referenced_size = reference_type_size & ~(1 << 31);
2621 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2622 references[i].sap_type =
2623 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2624 references[i].sap_delta_time = sap & ~(0xF << 28);
2630 size_t SegmentIndex::ComputeSizeInternal() {
2631 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2632 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2633 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2634 3 *
sizeof(uint32_t) * references.size();
2637 MediaData::MediaData() =
default;
2638 MediaData::~MediaData() =
default;
2644 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2645 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2649 size_t MediaData::ComputeSizeInternal() {
2653 CueSourceIDBox::CueSourceIDBox() =
default;
2654 CueSourceIDBox::~CueSourceIDBox() =
default;
2660 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2665 size_t CueSourceIDBox::ComputeSizeInternal() {
2666 if (source_id == kCueSourceIdNotSet)
2671 CueTimeBox::CueTimeBox() =
default;
2672 CueTimeBox::~CueTimeBox() =
default;
2678 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2685 size_t CueTimeBox::ComputeSizeInternal() {
2686 if (cue_current_time.empty())
2688 return HeaderSize() + cue_current_time.size();
2691 CueIDBox::CueIDBox() =
default;
2692 CueIDBox::~CueIDBox() =
default;
2698 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2704 size_t CueIDBox::ComputeSizeInternal() {
2710 CueSettingsBox::CueSettingsBox() =
default;
2711 CueSettingsBox::~CueSettingsBox() =
default;
2717 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2723 size_t CueSettingsBox::ComputeSizeInternal() {
2724 if (settings.empty())
2729 CuePayloadBox::CuePayloadBox() =
default;
2730 CuePayloadBox::~CuePayloadBox() =
default;
2736 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2742 size_t CuePayloadBox::ComputeSizeInternal() {
2746 VTTEmptyCueBox::VTTEmptyCueBox() =
default;
2747 VTTEmptyCueBox::~VTTEmptyCueBox() =
default;
2753 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2757 size_t VTTEmptyCueBox::ComputeSizeInternal() {
2761 VTTAdditionalTextBox::VTTAdditionalTextBox() =
default;
2762 VTTAdditionalTextBox::~VTTAdditionalTextBox() =
default;
2768 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2771 &cue_additional_text,
2775 size_t VTTAdditionalTextBox::ComputeSizeInternal() {
2776 return HeaderSize() + cue_additional_text.size();
2779 VTTCueBox::VTTCueBox() =
default;
2780 VTTCueBox::~VTTCueBox() =
default;
2786 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2796 size_t VTTCueBox::ComputeSizeInternal() {
2797 return HeaderSize() + cue_source_id.ComputeSize() + cue_id.ComputeSize() +
2798 cue_time.ComputeSize() + cue_settings.ComputeSize() +
2799 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