5 #include "packager/media/formats/mp4/box_definitions.h" 9 #include "packager/base/logging.h" 10 #include "packager/media/base/bit_reader.h" 11 #include "packager/media/base/macros.h" 12 #include "packager/media/base/rcheck.h" 13 #include "packager/media/formats/mp4/box_buffer.h" 16 const uint32_t kFourCCSize = 4;
19 const uint32_t kCencKeyIdSize = 16;
22 const uint8_t kUnityMatrix[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
24 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0};
27 const char kVideoHandlerName[] =
"VideoHandler";
28 const char kAudioHandlerName[] =
"SoundHandler";
29 const char kTextHandlerName[] =
"TextHandler";
32 const uint32_t kVideoResolution = 0x00480000;
33 const uint16_t kVideoFrameCount = 1;
34 const uint16_t kVideoDepth = 0x0018;
36 const uint32_t kCompressorNameSize = 32u;
37 const char kAvcCompressorName[] =
"\012AVC Coding";
38 const char kHevcCompressorName[] =
"\013HEVC Coding";
39 const char kVpcCompressorName[] =
"\012VPC Coding";
44 bool IsIvSizeValid(uint8_t per_sample_iv_size) {
45 return per_sample_iv_size == 0 || per_sample_iv_size == 8 ||
46 per_sample_iv_size == 16;
63 const uint8_t kDdtsExtraData[] = {0xe4, 0x7c, 0, 4, 0, 0x0f, 0};
66 bool IsFitIn32Bits(uint64_t a) {
67 return a <= std::numeric_limits<uint32_t>::max();
70 bool IsFitIn32Bits(int64_t a) {
71 return a <= std::numeric_limits<int32_t>::max() &&
72 a >= std::numeric_limits<int32_t>::min();
75 template <
typename T1,
typename T2>
76 bool IsFitIn32Bits(T1 a1, T2 a2) {
77 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
80 template <
typename T1,
typename T2,
typename T3>
81 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
82 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
93 TrackType FourCCToTrackType(FourCC fourcc) {
106 FourCC TrackTypeToFourCC(TrackType track_type) {
107 switch (track_type) {
119 bool IsProtectionSchemeSupported(FourCC scheme) {
120 return scheme == FOURCC_cenc || scheme == FOURCC_cens ||
121 scheme == FOURCC_cbc1 || scheme == FOURCC_cbcs;
126 FileType::FileType() =
default;
127 FileType::~FileType() =
default;
133 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
135 buffer->ReadWriteFourCC(&major_brand) &&
136 buffer->ReadWriteUInt32(&minor_version));
139 RCHECK(buffer->
BytesLeft() %
sizeof(FourCC) == 0);
140 num_brands = buffer->
BytesLeft() /
sizeof(FourCC);
141 compatible_brands.resize(num_brands);
143 num_brands = compatible_brands.size();
145 for (
size_t i = 0; i < num_brands; ++i)
146 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
150 size_t FileType::ComputeSizeInternal() {
151 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
152 kFourCCSize * compatible_brands.size();
159 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() =
default;
160 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() =
default;
166 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
170 raw_box.assign(reader->data(), reader->data() + reader->size());
172 DCHECK(!raw_box.empty());
173 buffer->
writer()->AppendVector(raw_box);
179 size_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
180 return raw_box.size();
183 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() =
default;
184 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() =
default;
190 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
195 uint32_t count =
static_cast<uint32_t
>(offsets.size());
196 RCHECK(buffer->ReadWriteUInt32(&count));
197 offsets.resize(count);
199 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
200 for (uint32_t i = 0; i < count; ++i)
205 size_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
207 if (offsets.size() == 0)
209 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
210 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
213 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize() =
default;
214 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() =
default;
220 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
225 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
226 buffer->ReadWriteUInt32(&sample_count));
227 if (default_sample_info_size == 0)
228 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
232 size_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
234 if (sample_count == 0)
236 return HeaderSize() +
sizeof(default_sample_info_size) +
237 sizeof(sample_count) +
238 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
244 DCHECK(IsIvSizeValid(iv_size));
247 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
249 if (!has_subsamples) {
254 uint16_t subsample_count =
static_cast<uint16_t
>(subsamples.size());
255 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
256 RCHECK(subsample_count > 0);
257 subsamples.resize(subsample_count);
258 for (
auto& subsample : subsamples) {
259 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
260 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
268 DCHECK(IsIvSizeValid(iv_size));
271 initialization_vector.resize(iv_size);
272 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
274 if (!has_subsamples) {
279 uint16_t subsample_count;
280 RCHECK(reader->Read2(&subsample_count));
281 RCHECK(subsample_count > 0);
282 subsamples.resize(subsample_count);
283 for (
auto& subsample : subsamples) {
284 RCHECK(reader->Read2(&subsample.clear_bytes) &&
285 reader->Read4(&subsample.cipher_bytes));
291 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
292 const uint16_t subsample_count =
static_cast<uint16_t
>(subsamples.size());
293 return static_cast<uint32_t
>(
294 initialization_vector.size() +
296 ? (
sizeof(subsample_count) + subsample_entry_size * subsample_count)
302 for (uint32_t i = 0; i < subsamples.size(); ++i)
303 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
307 SampleEncryption::SampleEncryption() =
default;
308 SampleEncryption::~SampleEncryption() =
default;
314 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
319 if (buffer->
Reading() && iv_size == SampleEncryption::kInvalidIvSize) {
321 buffer->ReadWriteVector(&sample_encryption_data, buffer->
BytesLeft()));
325 if (!IsIvSizeValid(iv_size)) {
327 <<
"IV_size can only be 8 or 16 or 0 for constant iv, but seeing " 332 uint32_t sample_count =
333 static_cast<uint32_t
>(sample_encryption_entries.size());
334 RCHECK(buffer->ReadWriteUInt32(&sample_count));
336 sample_encryption_entries.resize(sample_count);
337 for (
auto& sample_encryption_entry : sample_encryption_entries) {
338 RCHECK(sample_encryption_entry.ReadWrite(
339 iv_size, (flags & kUseSubsampleEncryption) != 0, buffer) != 0);
344 size_t SampleEncryption::ComputeSizeInternal() {
345 const uint32_t sample_count =
346 static_cast<uint32_t
>(sample_encryption_entries.size());
347 if (sample_count == 0) {
352 DCHECK(IsIvSizeValid(iv_size));
354 if (flags & kUseSubsampleEncryption) {
356 sample_encryption_entries) {
357 box_size += sample_encryption_entry.ComputeSize();
360 box_size += sample_count * iv_size;
367 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
368 DCHECK(IsIvSizeValid(iv_size));
371 sample_encryption_data.size());
372 uint32_t sample_count = 0;
373 RCHECK(reader.Read4(&sample_count));
375 sample_encryption_entries->resize(sample_count);
376 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
377 RCHECK(sample_encryption_entry.ParseFromBuffer(
378 iv_size, (flags & kUseSubsampleEncryption) != 0, &reader) != 0);
383 OriginalFormat::OriginalFormat() =
default;
384 OriginalFormat::~OriginalFormat() =
default;
390 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
394 size_t OriginalFormat::ComputeSizeInternal() {
398 SchemeType::SchemeType() =
default;
399 SchemeType::~SchemeType() =
default;
405 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
407 buffer->ReadWriteUInt32(&version));
411 size_t SchemeType::ComputeSizeInternal() {
412 return HeaderSize() + kFourCCSize +
sizeof(version);
415 TrackEncryption::TrackEncryption() =
default;
416 TrackEncryption::~TrackEncryption() =
default;
422 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
424 if (default_kid.size() != kCencKeyIdSize) {
425 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
426 <<
" bytes; got " << default_kid.size()
427 <<
". Resized accordingly.";
428 default_kid.resize(kCencKeyIdSize);
430 RCHECK(default_crypt_byte_block < 16 && default_skip_byte_block < 16);
431 if (default_crypt_byte_block != 0 && default_skip_byte_block != 0) {
440 uint8_t pattern = default_crypt_byte_block << 4 | default_skip_byte_block;
441 RCHECK(buffer->ReadWriteUInt8(&pattern));
442 default_crypt_byte_block = pattern >> 4;
443 default_skip_byte_block = pattern & 0x0F;
445 RCHECK(buffer->ReadWriteUInt8(&default_is_protected) &&
446 buffer->ReadWriteUInt8(&default_per_sample_iv_size) &&
447 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
449 if (default_is_protected == 1) {
450 if (default_per_sample_iv_size == 0) {
451 uint8_t default_constant_iv_size =
452 static_cast<uint8_t
>(default_constant_iv.size());
453 RCHECK(buffer->ReadWriteUInt8(&default_constant_iv_size));
454 RCHECK(default_constant_iv_size == 8 || default_constant_iv_size == 16);
455 RCHECK(buffer->ReadWriteVector(&default_constant_iv,
456 default_constant_iv_size));
458 RCHECK(default_per_sample_iv_size == 8 ||
459 default_per_sample_iv_size == 16);
460 RCHECK(default_constant_iv.empty());
465 RCHECK(default_is_protected == 0);
466 RCHECK(default_per_sample_iv_size == 0);
467 RCHECK(default_constant_iv.empty());
472 size_t TrackEncryption::ComputeSizeInternal() {
473 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize +
474 (default_constant_iv.empty()
476 : (
sizeof(uint8_t) + default_constant_iv.size()));
479 SchemeInfo::SchemeInfo() =
default;
480 SchemeInfo::~SchemeInfo() =
default;
486 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
492 size_t SchemeInfo::ComputeSizeInternal() {
493 return HeaderSize() + track_encryption.ComputeSize();
496 ProtectionSchemeInfo::ProtectionSchemeInfo() =
default;
497 ProtectionSchemeInfo::~ProtectionSchemeInfo() =
default;
503 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
506 if (IsProtectionSchemeSupported(type.type)) {
509 DLOG(WARNING) <<
"Ignore unsupported protection scheme: " 510 << FourCCToString(type.type);
519 size_t ProtectionSchemeInfo::ComputeSizeInternal() {
521 if (format.format == FOURCC_NULL)
523 return HeaderSize() + format.ComputeSize() + type.ComputeSize() +
527 MovieHeader::MovieHeader() =
default;
528 MovieHeader::~MovieHeader() =
default;
534 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
537 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
540 buffer->ReadWriteUInt32(×cale) &&
543 std::vector<uint8_t> matrix(kUnityMatrix,
544 kUnityMatrix + arraysize(kUnityMatrix));
545 RCHECK(buffer->ReadWriteInt32(&rate) && buffer->ReadWriteInt16(&volume) &&
547 buffer->ReadWriteVector(&matrix, matrix.size()) &&
549 buffer->ReadWriteUInt32(&next_track_id));
553 size_t MovieHeader::ComputeSizeInternal() {
554 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
555 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
556 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
557 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
561 TrackHeader::TrackHeader() {
562 flags = kTrackEnabled | kTrackInMovie | kTrackInPreview;
565 TrackHeader::~TrackHeader() =
default;
571 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
574 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
577 buffer->ReadWriteUInt32(&track_id) &&
584 volume = (width != 0 && height != 0) ? 0 : 0x100;
586 std::vector<uint8_t> matrix(kUnityMatrix,
587 kUnityMatrix + arraysize(kUnityMatrix));
589 buffer->ReadWriteInt16(&layer) &&
590 buffer->ReadWriteInt16(&alternate_group) &&
591 buffer->ReadWriteInt16(&volume) &&
593 buffer->ReadWriteVector(&matrix, matrix.size()) &&
594 buffer->ReadWriteUInt32(&width) && buffer->ReadWriteUInt32(&height));
598 size_t TrackHeader::ComputeSizeInternal() {
599 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
601 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
602 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
603 sizeof(height) +
sizeof(kUnityMatrix) + 14;
606 SampleDescription::SampleDescription() =
default;
607 SampleDescription::~SampleDescription() =
default;
613 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
617 count =
static_cast<uint32_t
>(video_entries.size());
620 count =
static_cast<uint32_t
>(audio_entries.size());
623 count =
static_cast<uint32_t
>(text_entries.size());
626 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
627 <<
" is not handled. Skipping.";
634 video_entries.clear();
635 audio_entries.clear();
638 if (type == kVideo) {
640 RCHECK(video_entries.size() == count);
641 }
else if (type == kAudio) {
643 RCHECK(audio_entries.size() == count);
644 }
else if (type == kText) {
646 RCHECK(text_entries.size() == count);
649 DCHECK_LT(0u, count);
650 if (type == kVideo) {
651 for (uint32_t i = 0; i < count; ++i)
653 }
else if (type == kAudio) {
654 for (uint32_t i = 0; i < count; ++i)
656 }
else if (type == kText) {
657 for (uint32_t i = 0; i < count; ++i)
666 size_t SampleDescription::ComputeSizeInternal() {
668 if (type == kVideo) {
669 for (uint32_t i = 0; i < video_entries.size(); ++i)
671 }
else if (type == kAudio) {
672 for (uint32_t i = 0; i < audio_entries.size(); ++i)
674 }
else if (type == kText) {
675 for (uint32_t i = 0; i < text_entries.size(); ++i)
681 DecodingTimeToSample::DecodingTimeToSample() =
default;
682 DecodingTimeToSample::~DecodingTimeToSample() =
default;
688 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
689 uint32_t count =
static_cast<uint32_t
>(decoding_time.size());
692 decoding_time.resize(count);
693 for (uint32_t i = 0; i < count; ++i) {
694 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
695 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
700 size_t DecodingTimeToSample::ComputeSizeInternal() {
705 CompositionTimeToSample::CompositionTimeToSample() =
default;
706 CompositionTimeToSample::~CompositionTimeToSample() =
default;
712 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
713 uint32_t count =
static_cast<uint32_t
>(composition_offset.size());
719 for (uint32_t i = 0; i < count; ++i) {
720 if (composition_offset[i].sample_offset < 0) {
729 composition_offset.resize(count);
730 for (uint32_t i = 0; i < count; ++i) {
731 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
734 uint32_t sample_offset = composition_offset[i].sample_offset;
735 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
736 composition_offset[i].sample_offset = sample_offset;
738 int32_t sample_offset = composition_offset[i].sample_offset;
739 RCHECK(buffer->ReadWriteInt32(&sample_offset));
740 composition_offset[i].sample_offset = sample_offset;
746 size_t CompositionTimeToSample::ComputeSizeInternal() {
748 if (composition_offset.empty())
753 const size_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
755 kCompositionOffsetSize * composition_offset.size();
758 SampleToChunk::SampleToChunk() =
default;
759 SampleToChunk::~SampleToChunk() =
default;
765 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
766 uint32_t count =
static_cast<uint32_t
>(chunk_info.size());
769 chunk_info.resize(count);
770 for (uint32_t i = 0; i < count; ++i) {
771 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
772 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
773 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
775 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
776 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
781 size_t SampleToChunk::ComputeSizeInternal() {
786 SampleSize::SampleSize() =
default;
787 SampleSize::~SampleSize() =
default;
793 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
795 buffer->ReadWriteUInt32(&sample_size) &&
796 buffer->ReadWriteUInt32(&sample_count));
798 if (sample_size == 0) {
800 sizes.resize(sample_count);
802 DCHECK(sample_count == sizes.size());
803 for (uint32_t i = 0; i < sample_count; ++i)
804 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
809 size_t SampleSize::ComputeSizeInternal() {
810 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
811 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
814 CompactSampleSize::CompactSampleSize() =
default;
815 CompactSampleSize::~CompactSampleSize() =
default;
821 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
822 uint32_t sample_count =
static_cast<uint32_t
>(sizes.size());
824 buffer->ReadWriteUInt8(&field_size) &&
825 buffer->ReadWriteUInt32(&sample_count));
828 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
829 switch (field_size) {
831 for (uint32_t i = 0; i < sample_count; i += 2) {
834 RCHECK(buffer->ReadWriteUInt8(&size));
835 sizes[i] = size >> 4;
836 sizes[i + 1] = size & 0x0F;
838 DCHECK_LT(sizes[i], 16u);
839 DCHECK_LT(sizes[i + 1], 16u);
840 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
841 RCHECK(buffer->ReadWriteUInt8(&size));
846 for (uint32_t i = 0; i < sample_count; ++i) {
847 uint8_t size = sizes[i];
848 RCHECK(buffer->ReadWriteUInt8(&size));
853 for (uint32_t i = 0; i < sample_count; ++i) {
854 uint16_t size = sizes[i];
855 RCHECK(buffer->ReadWriteUInt16(&size));
862 sizes.resize(sample_count);
866 size_t CompactSampleSize::ComputeSizeInternal() {
867 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
868 (field_size * sizes.size() + 7) / 8;
871 ChunkOffset::ChunkOffset() =
default;
872 ChunkOffset::~ChunkOffset() =
default;
878 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
879 uint32_t count =
static_cast<uint32_t
>(offsets.size());
882 offsets.resize(count);
883 for (uint32_t i = 0; i < count; ++i)
888 size_t ChunkOffset::ComputeSizeInternal() {
889 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
892 ChunkLargeOffset::ChunkLargeOffset() =
default;
893 ChunkLargeOffset::~ChunkLargeOffset() =
default;
899 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
900 uint32_t count =
static_cast<uint32_t
>(offsets.size());
904 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
906 stco.offsets.swap(offsets);
909 stco.offsets.swap(offsets);
916 offsets.resize(count);
917 for (uint32_t i = 0; i < count; ++i)
918 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
922 size_t ChunkLargeOffset::ComputeSizeInternal() {
923 uint32_t count =
static_cast<uint32_t
>(offsets.size());
924 int use_large_offset =
925 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
927 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
930 SyncSample::SyncSample() =
default;
931 SyncSample::~SyncSample() =
default;
937 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
938 uint32_t count =
static_cast<uint32_t
>(sample_number.size());
941 sample_number.resize(count);
942 for (uint32_t i = 0; i < count; ++i)
943 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
947 size_t SyncSample::ComputeSizeInternal() {
949 if (sample_number.empty())
952 sizeof(uint32_t) * sample_number.size();
955 bool CencSampleEncryptionInfoEntry::ReadWrite(
BoxBuffer* buffer) {
957 if (key_id.size() != kCencKeyIdSize) {
958 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
959 <<
" bytes; got " << key_id.size()
960 <<
". Resized accordingly.";
961 key_id.resize(kCencKeyIdSize);
963 RCHECK(crypt_byte_block < 16 && skip_byte_block < 16);
968 uint8_t pattern = crypt_byte_block << 4 | skip_byte_block;
969 RCHECK(buffer->ReadWriteUInt8(&pattern));
970 crypt_byte_block = pattern >> 4;
971 skip_byte_block = pattern & 0x0F;
973 RCHECK(buffer->ReadWriteUInt8(&is_protected) &&
974 buffer->ReadWriteUInt8(&per_sample_iv_size) &&
975 buffer->ReadWriteVector(&key_id, kCencKeyIdSize));
977 if (is_protected == 1) {
978 if (per_sample_iv_size == 0) {
979 uint8_t constant_iv_size =
static_cast<uint8_t
>(constant_iv.size());
980 RCHECK(buffer->ReadWriteUInt8(&constant_iv_size));
981 RCHECK(constant_iv_size == 8 || constant_iv_size == 16);
982 RCHECK(buffer->ReadWriteVector(&constant_iv, constant_iv_size));
984 RCHECK(per_sample_iv_size == 8 || per_sample_iv_size == 16);
985 DCHECK(constant_iv.empty());
990 RCHECK(is_protected == 0);
991 RCHECK(per_sample_iv_size == 0);
996 uint32_t CencSampleEncryptionInfoEntry::ComputeSize()
const {
997 return static_cast<uint32_t
>(
998 sizeof(uint32_t) + kCencKeyIdSize +
999 (constant_iv.empty() ? 0 : (
sizeof(uint8_t) + constant_iv.size())));
1002 bool AudioRollRecoveryEntry::ReadWrite(
BoxBuffer* buffer) {
1003 RCHECK(buffer->ReadWriteInt16(&roll_distance));
1007 uint32_t AudioRollRecoveryEntry::ComputeSize()
const {
1008 return sizeof(roll_distance);
1011 SampleGroupDescription::SampleGroupDescription() =
default;
1012 SampleGroupDescription::~SampleGroupDescription() =
default;
1018 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
1020 buffer->ReadWriteUInt32(&grouping_type));
1022 switch (grouping_type) {
1024 return ReadWriteEntries(buffer, &cenc_sample_encryption_info_entries);
1026 return ReadWriteEntries(buffer, &audio_roll_recovery_entries);
1029 DLOG(WARNING) <<
"Ignore unsupported sample group: " 1030 << FourCCToString(static_cast<FourCC>(grouping_type));
1035 template <
typename T>
1036 bool SampleGroupDescription::ReadWriteEntries(
BoxBuffer* buffer,
1037 std::vector<T>* entries) {
1038 uint32_t default_length = 0;
1040 DCHECK(!entries->empty());
1041 default_length = (*entries)[0].ComputeSize();
1042 DCHECK_NE(default_length, 0u);
1045 RCHECK(buffer->ReadWriteUInt32(&default_length));
1047 NOTIMPLEMENTED() <<
"Unsupported SampleGroupDescriptionBox 'sgpd' version " 1048 <<
static_cast<int>(version);
1052 uint32_t count =
static_cast<uint32_t
>(entries->size());
1053 RCHECK(buffer->ReadWriteUInt32(&count));
1055 entries->resize(count);
1057 for (T& entry : *entries) {
1059 uint32_t description_length = default_length;
1060 if (buffer->
Reading() && default_length == 0)
1061 RCHECK(buffer->ReadWriteUInt32(&description_length));
1062 RCHECK(entry.ReadWrite(buffer));
1063 RCHECK(entry.ComputeSize() == description_length);
1065 RCHECK(entry.ReadWrite(buffer));
1071 size_t SampleGroupDescription::ComputeSizeInternal() {
1074 size_t entries_size = 0;
1075 switch (grouping_type) {
1077 for (
const auto& entry : cenc_sample_encryption_info_entries)
1078 entries_size += entry.ComputeSize();
1081 for (
const auto& entry : audio_roll_recovery_entries)
1082 entries_size += entry.ComputeSize();
1086 if (entries_size == 0)
1088 return HeaderSize() +
sizeof(grouping_type) +
1089 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
1093 SampleToGroup::SampleToGroup() =
default;
1094 SampleToGroup::~SampleToGroup() =
default;
1100 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
1102 buffer->ReadWriteUInt32(&grouping_type));
1104 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
1106 if (grouping_type != FOURCC_seig && grouping_type != FOURCC_roll) {
1108 DLOG(WARNING) <<
"Ignore unsupported sample group: " 1109 << FourCCToString(static_cast<FourCC>(grouping_type));
1113 uint32_t count =
static_cast<uint32_t
>(entries.size());
1114 RCHECK(buffer->ReadWriteUInt32(&count));
1115 entries.resize(count);
1116 for (uint32_t i = 0; i < count; ++i) {
1117 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
1118 buffer->ReadWriteUInt32(&entries[i].group_description_index));
1123 size_t SampleToGroup::ComputeSizeInternal() {
1125 if (entries.empty())
1127 return HeaderSize() +
sizeof(grouping_type) +
1128 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
1129 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
1132 SampleTable::SampleTable() =
default;
1133 SampleTable::~SampleTable() =
default;
1139 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
1152 RCHECK(reader->
ReadChild(&sample_size));
1155 RCHECK(reader->
ReadChild(&compact_sample_size));
1156 sample_size.sample_size = 0;
1157 sample_size.sample_count =
1158 static_cast<uint32_t
>(compact_sample_size.sizes.size());
1159 sample_size.sizes.swap(compact_sample_size.sizes);
1163 if (reader->
ChildExist(&chunk_large_offset)) {
1164 RCHECK(reader->
ReadChild(&chunk_large_offset));
1167 RCHECK(reader->
ReadChild(&chunk_offset));
1168 chunk_large_offset.offsets.swap(chunk_offset.offsets);
1179 for (
auto& sample_group_description : sample_group_descriptions)
1181 for (
auto& sample_to_group : sample_to_groups)
1187 size_t SampleTable::ComputeSizeInternal() {
1189 decoding_time_to_sample.ComputeSize() +
1190 composition_time_to_sample.ComputeSize() +
1191 sample_to_chunk.ComputeSize() + sample_size.ComputeSize() +
1192 chunk_large_offset.ComputeSize() +
1193 sync_sample.ComputeSize();
1194 for (
auto& sample_group_description : sample_group_descriptions)
1195 box_size += sample_group_description.ComputeSize();
1196 for (
auto& sample_to_group : sample_to_groups)
1197 box_size += sample_to_group.ComputeSize();
1201 EditList::EditList() =
default;
1202 EditList::~EditList() =
default;
1208 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
1209 uint32_t count =
static_cast<uint32_t
>(edits.size());
1211 edits.resize(count);
1213 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1214 for (uint32_t i = 0; i < count; ++i) {
1217 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
1218 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
1219 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
1224 size_t EditList::ComputeSizeInternal() {
1230 for (uint32_t i = 0; i < edits.size(); ++i) {
1231 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
1237 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
1241 Edit::Edit() =
default;
1242 Edit::~Edit() =
default;
1248 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
1253 size_t Edit::ComputeSizeInternal() {
1255 if (list.edits.empty())
1260 HandlerReference::HandlerReference() =
default;
1261 HandlerReference::~HandlerReference() =
default;
1267 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1268 std::vector<uint8_t> handler_name;
1270 switch (handler_type) {
1272 handler_name.assign(kVideoHandlerName,
1273 kVideoHandlerName + arraysize(kVideoHandlerName));
1276 handler_name.assign(kAudioHandlerName,
1277 kAudioHandlerName + arraysize(kAudioHandlerName));
1280 handler_name.assign(kTextHandlerName,
1281 kTextHandlerName + arraysize(kTextHandlerName));
1292 buffer->ReadWriteFourCC(&handler_type));
1295 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1300 size_t HandlerReference::ComputeSizeInternal() {
1302 switch (handler_type) {
1304 box_size +=
sizeof(kVideoHandlerName);
1307 box_size +=
sizeof(kAudioHandlerName);
1310 box_size +=
sizeof(kTextHandlerName);
1320 bool Language::ReadWrite(
BoxBuffer* buffer) {
1324 std::vector<uint8_t> temp;
1325 RCHECK(buffer->ReadWriteVector(&temp, 2));
1328 bit_reader.SkipBits(1);
1330 for (
int i = 0; i < 3; ++i) {
1331 CHECK(bit_reader.ReadBits(5, &language[i]));
1332 language[i] += 0x60;
1334 code.assign(language, 3);
1337 const char kUndefinedLanguage[] =
"und";
1339 code = kUndefinedLanguage;
1340 DCHECK_EQ(code.size(), 3u);
1344 for (
int i = 0; i < 3; ++i)
1345 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1346 RCHECK(buffer->ReadWriteUInt16(&lang));
1351 uint32_t Language::ComputeSize()
const {
1356 ID3v2::ID3v2() =
default;
1357 ID3v2::~ID3v2() =
default;
1363 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1365 buffer->ReadWriteVector(&id3v2_data, buffer->
Reading()
1367 : id3v2_data.size()));
1371 size_t ID3v2::ComputeSizeInternal() {
1373 return id3v2_data.size() == 0
1375 :
HeaderSize() + language.ComputeSize() + id3v2_data.size();
1378 Metadata::Metadata() =
default;
1379 Metadata::~Metadata() =
default;
1385 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1391 size_t Metadata::ComputeSizeInternal() {
1392 size_t id3v2_size = id3v2.ComputeSize();
1394 return id3v2_size == 0 ? 0
1395 :
HeaderSize() + handler.ComputeSize() + id3v2_size;
1398 CodecConfiguration::CodecConfiguration() =
default;
1399 CodecConfiguration::~CodecConfiguration() =
default;
1407 bool CodecConfiguration::ReadWriteInternal(
BoxBuffer* buffer) {
1408 DCHECK_NE(box_type, FOURCC_NULL);
1413 if (box_type == FOURCC_vpcC) {
1415 uint8_t vpcc_version = 1;
1416 uint32_t version_flags = vpcc_version << 24;
1417 RCHECK(buffer->ReadWriteUInt32(&version_flags));
1418 vpcc_version = version_flags >> 24;
1419 RCHECK(vpcc_version == 1);
1423 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1425 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1430 size_t CodecConfiguration::ComputeSizeInternal() {
1433 DCHECK_NE(box_type, FOURCC_NULL);
1434 return HeaderSize() + (box_type == FOURCC_vpcC ? 4 : 0) + data.size();
1437 PixelAspectRatio::PixelAspectRatio() =
default;
1438 PixelAspectRatio::~PixelAspectRatio() =
default;
1444 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1446 buffer->ReadWriteUInt32(&h_spacing) &&
1447 buffer->ReadWriteUInt32(&v_spacing));
1451 size_t PixelAspectRatio::ComputeSizeInternal() {
1453 if (h_spacing == 0 && v_spacing == 0)
1456 DCHECK(h_spacing != 0 && v_spacing != 0);
1457 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1460 VideoSampleEntry::VideoSampleEntry() =
default;
1461 VideoSampleEntry::~VideoSampleEntry() =
default;
1464 if (format == FOURCC_NULL) {
1465 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the " 1466 <<
"handler type recovered in its Media ancestor.";
1471 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1472 std::vector<uint8_t> compressor_name;
1474 DCHECK(buffer->
reader());
1475 format = buffer->
reader()->type();
1479 const FourCC actual_format = GetActualFormat();
1480 switch (actual_format) {
1483 compressor_name.assign(
1485 kAvcCompressorName + arraysize(kAvcCompressorName));
1489 compressor_name.assign(
1490 kHevcCompressorName,
1491 kHevcCompressorName + arraysize(kHevcCompressorName));
1496 compressor_name.assign(
1498 kVpcCompressorName + arraysize(kVpcCompressorName));
1501 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1504 compressor_name.resize(kCompressorNameSize);
1507 uint32_t video_resolution = kVideoResolution;
1508 uint16_t video_frame_count = kVideoFrameCount;
1509 uint16_t video_depth = kVideoDepth;
1510 int16_t predefined = -1;
1512 buffer->ReadWriteUInt16(&data_reference_index) &&
1514 buffer->ReadWriteUInt16(&width) && buffer->ReadWriteUInt16(&height) &&
1515 buffer->ReadWriteUInt32(&video_resolution) &&
1516 buffer->ReadWriteUInt32(&video_resolution) &&
1518 buffer->ReadWriteUInt16(&video_frame_count) &&
1519 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1520 buffer->ReadWriteUInt16(&video_depth) &&
1521 buffer->ReadWriteInt16(&predefined));
1528 if (format == FOURCC_encv && buffer->
Reading()) {
1531 while (!IsProtectionSchemeSupported(sinf.type.type))
1535 const FourCC actual_format = GetActualFormat();
1537 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1539 DCHECK_EQ(codec_configuration.box_type,
1540 GetCodecConfigurationBoxType(actual_format));
1542 if (codec_configuration.box_type == FOURCC_NULL)
1551 if (format == FOURCC_encv && !buffer->
Reading()) {
1552 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1558 size_t VideoSampleEntry::ComputeSizeInternal() {
1559 const FourCC actual_format = GetActualFormat();
1560 if (actual_format == FOURCC_NULL)
1562 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1563 DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1564 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1565 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1566 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1567 pixel_aspect.ComputeSize() + sinf.ComputeSize() +
1568 codec_configuration.ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1572 FourCC VideoSampleEntry::GetCodecConfigurationBoxType(FourCC format)
const {
1585 LOG(ERROR) << FourCCToString(format) <<
" is not supported.";
1590 ElementaryStreamDescriptor::ElementaryStreamDescriptor() =
default;
1591 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() =
default;
1597 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1600 std::vector<uint8_t> data;
1601 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1602 RCHECK(es_descriptor.Parse(data));
1603 if (es_descriptor.IsAAC()) {
1604 RCHECK(aac_audio_specific_config.Parse(
1605 es_descriptor.decoder_specific_info()));
1608 DCHECK(buffer->
writer());
1609 es_descriptor.Write(buffer->
writer());
1614 size_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1616 if (es_descriptor.object_type() == ObjectType::kForbidden)
1618 return HeaderSize() + es_descriptor.ComputeSize();
1621 DTSSpecific::DTSSpecific() =
default;
1622 DTSSpecific::~DTSSpecific() =
default;
1629 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1631 buffer->ReadWriteUInt32(&sampling_frequency) &&
1632 buffer->ReadWriteUInt32(&max_bitrate) &&
1633 buffer->ReadWriteUInt32(&avg_bitrate) &&
1634 buffer->ReadWriteUInt8(&pcm_sample_depth));
1637 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
BytesLeft()));
1639 if (extra_data.empty()) {
1640 extra_data.assign(kDdtsExtraData,
1641 kDdtsExtraData +
sizeof(kDdtsExtraData));
1643 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1648 size_t DTSSpecific::ComputeSizeInternal() {
1650 if (sampling_frequency == 0)
1652 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1653 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1654 sizeof(kDdtsExtraData);
1657 AC3Specific::AC3Specific() =
default;
1658 AC3Specific::~AC3Specific() =
default;
1664 bool AC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1666 buffer->ReadWriteVector(
1671 size_t AC3Specific::ComputeSizeInternal() {
1678 EC3Specific::EC3Specific() =
default;
1679 EC3Specific::~EC3Specific() =
default;
1685 bool EC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1688 RCHECK(buffer->ReadWriteVector(&data, size));
1692 size_t EC3Specific::ComputeSizeInternal() {
1699 OpusSpecific::OpusSpecific() =
default;
1700 OpusSpecific::~OpusSpecific() =
default;
1706 bool OpusSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1709 std::vector<uint8_t> data;
1710 const int kMinOpusSpecificBoxDataSize = 11;
1711 RCHECK(buffer->
BytesLeft() >= kMinOpusSpecificBoxDataSize);
1712 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1713 preskip = data[2] + (data[3] << 8);
1718 writer.AppendInt(FOURCC_Head);
1720 const uint8_t kOpusIdentificationHeaderVersion = 1;
1721 data[0] = kOpusIdentificationHeaderVersion;
1722 writer.AppendVector(data);
1723 writer.SwapBuffer(&opus_identification_header);
1727 const size_t kOpusMagicSignatureSize = 8u;
1728 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1731 const uint8_t kOpusSpecificBoxVersion = 0;
1733 buffer->
writer()->AppendArray(
1734 &opus_identification_header[kOpusMagicSignatureSize + 1],
1735 opus_identification_header.size() - kOpusMagicSignatureSize - 1);
1740 size_t OpusSpecific::ComputeSizeInternal() {
1742 if (opus_identification_header.empty())
1746 const size_t kOpusMagicSignatureSize = 8u;
1747 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1748 return HeaderSize() + opus_identification_header.size() -
1749 kOpusMagicSignatureSize;
1752 FlacSpecific::FlacSpecific() =
default;
1753 FlacSpecific::~FlacSpecific() =
default;
1759 bool FlacSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1762 RCHECK(buffer->ReadWriteVector(&data, size));
1766 size_t FlacSpecific::ComputeSizeInternal() {
1773 AudioSampleEntry::AudioSampleEntry() =
default;
1774 AudioSampleEntry::~AudioSampleEntry() =
default;
1777 if (format == FOURCC_NULL) {
1778 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the " 1779 <<
"handler type recovered in its Media ancestor.";
1784 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1786 DCHECK(buffer->
reader());
1787 format = buffer->
reader()->type();
1795 buffer->ReadWriteUInt16(&data_reference_index) &&
1797 buffer->ReadWriteUInt16(&channelcount) &&
1798 buffer->ReadWriteUInt16(&samplesize) &&
1800 buffer->ReadWriteUInt32(&samplerate));
1816 if (format == FOURCC_enca) {
1820 while (!IsProtectionSchemeSupported(sinf.type.type))
1823 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1830 size_t AudioSampleEntry::ComputeSizeInternal() {
1831 if (GetActualFormat() == FOURCC_NULL)
1833 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1834 sizeof(samplesize) +
sizeof(samplerate) + sinf.ComputeSize() +
1835 esds.ComputeSize() + ddts.ComputeSize() + dac3.ComputeSize() +
1836 dec3.ComputeSize() + dops.ComputeSize() + dfla.ComputeSize() +
1842 WebVTTConfigurationBox::WebVTTConfigurationBox() =
default;
1843 WebVTTConfigurationBox::~WebVTTConfigurationBox() =
default;
1849 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1855 size_t WebVTTConfigurationBox::ComputeSizeInternal() {
1859 WebVTTSourceLabelBox::WebVTTSourceLabelBox() =
default;
1860 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() =
default;
1866 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1870 : source_label.size());
1873 size_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1874 if (source_label.empty())
1879 TextSampleEntry::TextSampleEntry() =
default;
1880 TextSampleEntry::~TextSampleEntry() =
default;
1883 if (format == FOURCC_NULL) {
1884 LOG(ERROR) <<
"TextSampleEntry should be parsed according to the " 1885 <<
"handler type recovered in its Media ancestor.";
1890 bool TextSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1892 DCHECK(buffer->
reader());
1893 format = buffer->
reader()->type();
1898 buffer->ReadWriteUInt16(&data_reference_index));
1900 if (format == FOURCC_wvtt) {
1908 size_t TextSampleEntry::ComputeSizeInternal() {
1910 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1911 config.ComputeSize() + label.ComputeSize();
1914 MediaHeader::MediaHeader() =
default;
1915 MediaHeader::~MediaHeader() =
default;
1921 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1924 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1927 buffer->ReadWriteUInt32(×cale) &&
1929 language.ReadWrite(buffer) &&
1935 size_t MediaHeader::ComputeSizeInternal() {
1936 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1938 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1942 VideoMediaHeader::VideoMediaHeader() {
1943 const uint32_t kVideoMediaHeaderFlags = 1;
1944 flags = kVideoMediaHeaderFlags;
1947 VideoMediaHeader::~VideoMediaHeader() =
default;
1952 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1954 buffer->ReadWriteUInt16(&graphicsmode) &&
1955 buffer->ReadWriteUInt16(&opcolor_red) &&
1956 buffer->ReadWriteUInt16(&opcolor_green) &&
1957 buffer->ReadWriteUInt16(&opcolor_blue));
1961 size_t VideoMediaHeader::ComputeSizeInternal() {
1962 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1963 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1966 SoundMediaHeader::SoundMediaHeader() =
default;
1967 SoundMediaHeader::~SoundMediaHeader() =
default;
1973 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1979 size_t SoundMediaHeader::ComputeSizeInternal() {
1980 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1983 SubtitleMediaHeader::SubtitleMediaHeader() =
default;
1984 SubtitleMediaHeader::~SubtitleMediaHeader() =
default;
1990 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1994 size_t SubtitleMediaHeader::ComputeSizeInternal() {
1998 DataEntryUrl::DataEntryUrl() {
1999 const uint32_t kDataEntryUrlFlags = 1;
2000 flags = kDataEntryUrlFlags;
2003 DataEntryUrl::~DataEntryUrl() =
default;
2008 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
2011 RCHECK(buffer->ReadWriteVector(&location, buffer->
BytesLeft()));
2013 RCHECK(buffer->ReadWriteVector(&location, location.size()));
2018 size_t DataEntryUrl::ComputeSizeInternal() {
2022 DataReference::DataReference() =
default;
2023 DataReference::~DataReference() =
default;
2028 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
2029 uint32_t entry_count =
static_cast<uint32_t
>(data_entry.size());
2031 buffer->ReadWriteUInt32(&entry_count));
2032 data_entry.resize(entry_count);
2034 for (uint32_t i = 0; i < entry_count; ++i)
2039 size_t DataReference::ComputeSizeInternal() {
2040 uint32_t count =
static_cast<uint32_t
>(data_entry.size());
2042 for (uint32_t i = 0; i < count; ++i)
2047 DataInformation::DataInformation() =
default;
2048 DataInformation::~DataInformation() =
default;
2054 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2059 size_t DataInformation::ComputeSizeInternal() {
2063 MediaInformation::MediaInformation() =
default;
2064 MediaInformation::~MediaInformation() =
default;
2070 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2074 switch (sample_table.description.type) {
2091 size_t MediaInformation::ComputeSizeInternal() {
2093 HeaderSize() + dinf.ComputeSize() + sample_table.ComputeSize();
2094 switch (sample_table.description.type) {
2096 box_size += vmhd.ComputeSize();
2099 box_size += smhd.ComputeSize();
2102 box_size += sthd.ComputeSize();
2110 Media::Media() =
default;
2111 Media::~Media() =
default;
2117 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
2128 information.sample_table.description.type =
2129 FourCCToTrackType(handler.handler_type);
2131 handler.handler_type =
2132 TrackTypeToFourCC(information.sample_table.description.type);
2133 RCHECK(handler.handler_type != FOURCC_NULL);
2140 size_t Media::ComputeSizeInternal() {
2141 handler.handler_type =
2142 TrackTypeToFourCC(information.sample_table.description.type);
2143 return HeaderSize() + header.ComputeSize() + handler.ComputeSize() +
2144 information.ComputeSize();
2147 Track::Track() =
default;
2148 Track::~Track() =
default;
2154 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
2162 size_t Track::ComputeSizeInternal() {
2163 return HeaderSize() + header.ComputeSize() + media.ComputeSize() +
2167 MovieExtendsHeader::MovieExtendsHeader() =
default;
2168 MovieExtendsHeader::~MovieExtendsHeader() =
default;
2174 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2176 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2181 size_t MovieExtendsHeader::ComputeSizeInternal() {
2183 if (fragment_duration == 0)
2185 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
2186 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2189 TrackExtends::TrackExtends() =
default;
2190 TrackExtends::~TrackExtends() =
default;
2196 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2198 buffer->ReadWriteUInt32(&track_id) &&
2199 buffer->ReadWriteUInt32(&default_sample_description_index) &&
2200 buffer->ReadWriteUInt32(&default_sample_duration) &&
2201 buffer->ReadWriteUInt32(&default_sample_size) &&
2202 buffer->ReadWriteUInt32(&default_sample_flags));
2206 size_t TrackExtends::ComputeSizeInternal() {
2208 sizeof(default_sample_description_index) +
2209 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
2210 sizeof(default_sample_flags);
2213 MovieExtends::MovieExtends() =
default;
2214 MovieExtends::~MovieExtends() =
default;
2220 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2224 DCHECK(buffer->
reader());
2227 for (uint32_t i = 0; i < tracks.size(); ++i)
2233 size_t MovieExtends::ComputeSizeInternal() {
2235 if (tracks.size() == 0)
2238 for (uint32_t i = 0; i < tracks.size(); ++i)
2243 Movie::Movie() =
default;
2244 Movie::~Movie() =
default;
2250 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
2266 for (uint32_t i = 0; i < tracks.size(); ++i)
2269 for (uint32_t i = 0; i < pssh.size(); ++i)
2275 size_t Movie::ComputeSizeInternal() {
2277 metadata.ComputeSize() + extends.ComputeSize();
2278 for (uint32_t i = 0; i < tracks.size(); ++i)
2280 for (uint32_t i = 0; i < pssh.size(); ++i)
2285 TrackFragmentDecodeTime::TrackFragmentDecodeTime() =
default;
2286 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() =
default;
2292 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
2294 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2299 size_t TrackFragmentDecodeTime::ComputeSizeInternal() {
2300 version = IsFitIn32Bits(decode_time) ? 0 : 1;
2301 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2304 MovieFragmentHeader::MovieFragmentHeader() =
default;
2305 MovieFragmentHeader::~MovieFragmentHeader() =
default;
2311 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2313 buffer->ReadWriteUInt32(&sequence_number);
2316 size_t MovieFragmentHeader::ComputeSizeInternal() {
2317 return HeaderSize() +
sizeof(sequence_number);
2320 TrackFragmentHeader::TrackFragmentHeader() =
default;
2321 TrackFragmentHeader::~TrackFragmentHeader() =
default;
2327 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2330 if (flags & kBaseDataOffsetPresentMask) {
2335 uint64_t base_data_offset;
2336 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
2337 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes " 2338 "default-base-is-moof.";
2341 if (flags & kSampleDescriptionIndexPresentMask) {
2342 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
2343 }
else if (buffer->
Reading()) {
2344 sample_description_index = 0;
2347 if (flags & kDefaultSampleDurationPresentMask) {
2348 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2349 }
else if (buffer->
Reading()) {
2350 default_sample_duration = 0;
2353 if (flags & kDefaultSampleSizePresentMask) {
2354 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2355 }
else if (buffer->
Reading()) {
2356 default_sample_size = 0;
2359 if (flags & kDefaultSampleFlagsPresentMask)
2360 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2364 size_t TrackFragmentHeader::ComputeSizeInternal() {
2366 if (flags & kSampleDescriptionIndexPresentMask)
2367 box_size +=
sizeof(sample_description_index);
2368 if (flags & kDefaultSampleDurationPresentMask)
2369 box_size +=
sizeof(default_sample_duration);
2370 if (flags & kDefaultSampleSizePresentMask)
2371 box_size +=
sizeof(default_sample_size);
2372 if (flags & kDefaultSampleFlagsPresentMask)
2373 box_size +=
sizeof(default_sample_flags);
2377 TrackFragmentRun::TrackFragmentRun() =
default;
2378 TrackFragmentRun::~TrackFragmentRun() =
default;
2384 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
2390 if (flags & kSampleCompTimeOffsetsPresentMask) {
2391 for (uint32_t i = 0; i < sample_count; ++i) {
2392 if (sample_composition_time_offsets[i] < 0) {
2401 buffer->ReadWriteUInt32(&sample_count));
2403 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2404 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2405 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2406 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2407 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2408 bool sample_composition_time_offsets_present =
2409 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2411 if (data_offset_present) {
2412 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2423 uint32_t first_sample_flags(0);
2426 if (first_sample_flags_present)
2427 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2429 if (sample_duration_present)
2430 sample_durations.resize(sample_count);
2431 if (sample_size_present)
2432 sample_sizes.resize(sample_count);
2433 if (sample_flags_present)
2434 sample_flags.resize(sample_count);
2435 if (sample_composition_time_offsets_present)
2436 sample_composition_time_offsets.resize(sample_count);
2438 if (first_sample_flags_present) {
2439 first_sample_flags = sample_flags[0];
2440 DCHECK(sample_flags.size() == 1);
2441 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2444 if (sample_duration_present)
2445 DCHECK(sample_durations.size() == sample_count);
2446 if (sample_size_present)
2447 DCHECK(sample_sizes.size() == sample_count);
2448 if (sample_flags_present)
2449 DCHECK(sample_flags.size() == sample_count);
2450 if (sample_composition_time_offsets_present)
2451 DCHECK(sample_composition_time_offsets.size() == sample_count);
2454 for (uint32_t i = 0; i < sample_count; ++i) {
2455 if (sample_duration_present)
2456 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2457 if (sample_size_present)
2458 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2459 if (sample_flags_present)
2460 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2462 if (sample_composition_time_offsets_present) {
2464 uint32_t sample_offset = sample_composition_time_offsets[i];
2465 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2466 sample_composition_time_offsets[i] = sample_offset;
2468 int32_t sample_offset = sample_composition_time_offsets[i];
2469 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2470 sample_composition_time_offsets[i] = sample_offset;
2476 if (first_sample_flags_present) {
2477 if (sample_flags.size() == 0) {
2478 sample_flags.push_back(first_sample_flags);
2480 sample_flags[0] = first_sample_flags;
2487 size_t TrackFragmentRun::ComputeSizeInternal() {
2489 if (flags & kDataOffsetPresentMask)
2490 box_size +=
sizeof(data_offset);
2491 if (flags & kFirstSampleFlagsPresentMask)
2492 box_size +=
sizeof(uint32_t);
2493 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2494 (flags & kSampleSizePresentMask ? 1 : 0) +
2495 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2496 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2497 box_size += fields *
sizeof(uint32_t) * sample_count;
2501 TrackFragment::TrackFragment() =
default;
2502 TrackFragment::~TrackFragment() =
default;
2508 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2512 DCHECK(buffer->
reader());
2514 if (!decode_time_absent)
2520 if (!decode_time_absent)
2522 for (uint32_t i = 0; i < runs.size(); ++i)
2524 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2526 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2534 size_t TrackFragment::ComputeSizeInternal() {
2536 decode_time.ComputeSize() + auxiliary_size.ComputeSize() +
2537 auxiliary_offset.ComputeSize() +
2538 sample_encryption.ComputeSize();
2539 for (uint32_t i = 0; i < runs.size(); ++i)
2541 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2542 box_size += sample_group_descriptions[i].
ComputeSize();
2543 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2548 MovieFragment::MovieFragment() =
default;
2549 MovieFragment::~MovieFragment() =
default;
2555 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2563 for (uint32_t i = 0; i < tracks.size(); ++i)
2565 for (uint32_t i = 0; i < pssh.size(); ++i)
2571 size_t MovieFragment::ComputeSizeInternal() {
2573 for (uint32_t i = 0; i < tracks.size(); ++i)
2575 for (uint32_t i = 0; i < pssh.size(); ++i)
2580 SegmentIndex::SegmentIndex() =
default;
2581 SegmentIndex::~SegmentIndex() =
default;
2587 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2589 buffer->ReadWriteUInt32(&reference_id) &&
2590 buffer->ReadWriteUInt32(×cale));
2592 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2597 uint16_t reference_count =
static_cast<uint16_t
>(references.size());
2599 buffer->ReadWriteUInt16(&reference_count));
2600 references.resize(reference_count);
2602 uint32_t reference_type_size;
2604 for (uint32_t i = 0; i < reference_count; ++i) {
2606 reference_type_size = references[i].referenced_size;
2607 if (references[i].reference_type)
2608 reference_type_size |= (1 << 31);
2609 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2610 if (references[i].starts_with_sap)
2613 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2614 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2615 buffer->ReadWriteUInt32(&sap));
2617 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2618 references[i].referenced_size = reference_type_size & ~(1 << 31);
2619 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2620 references[i].sap_type =
2621 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2622 references[i].sap_delta_time = sap & ~(0xF << 28);
2628 size_t SegmentIndex::ComputeSizeInternal() {
2629 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2630 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2631 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2632 3 *
sizeof(uint32_t) * references.size();
2635 MediaData::MediaData() =
default;
2636 MediaData::~MediaData() =
default;
2642 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2643 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2647 size_t MediaData::ComputeSizeInternal() {
2651 CueSourceIDBox::CueSourceIDBox() =
default;
2652 CueSourceIDBox::~CueSourceIDBox() =
default;
2658 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2663 size_t CueSourceIDBox::ComputeSizeInternal() {
2664 if (source_id == kCueSourceIdNotSet)
2669 CueTimeBox::CueTimeBox() =
default;
2670 CueTimeBox::~CueTimeBox() =
default;
2676 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2683 size_t CueTimeBox::ComputeSizeInternal() {
2684 if (cue_current_time.empty())
2686 return HeaderSize() + cue_current_time.size();
2689 CueIDBox::CueIDBox() =
default;
2690 CueIDBox::~CueIDBox() =
default;
2696 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2702 size_t CueIDBox::ComputeSizeInternal() {
2708 CueSettingsBox::CueSettingsBox() =
default;
2709 CueSettingsBox::~CueSettingsBox() =
default;
2715 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2721 size_t CueSettingsBox::ComputeSizeInternal() {
2722 if (settings.empty())
2727 CuePayloadBox::CuePayloadBox() =
default;
2728 CuePayloadBox::~CuePayloadBox() =
default;
2734 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2740 size_t CuePayloadBox::ComputeSizeInternal() {
2744 VTTEmptyCueBox::VTTEmptyCueBox() =
default;
2745 VTTEmptyCueBox::~VTTEmptyCueBox() =
default;
2751 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2755 size_t VTTEmptyCueBox::ComputeSizeInternal() {
2759 VTTAdditionalTextBox::VTTAdditionalTextBox() =
default;
2760 VTTAdditionalTextBox::~VTTAdditionalTextBox() =
default;
2766 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2769 &cue_additional_text,
2773 size_t VTTAdditionalTextBox::ComputeSizeInternal() {
2774 return HeaderSize() + cue_additional_text.size();
2777 VTTCueBox::VTTCueBox() =
default;
2778 VTTCueBox::~VTTCueBox() =
default;
2784 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2794 size_t VTTCueBox::ComputeSizeInternal() {
2795 return HeaderSize() + cue_source_id.ComputeSize() + cue_id.ComputeSize() +
2796 cue_time.ComputeSize() + cue_settings.ComputeSize() +
2797 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