5 #include "packager/media/formats/mp4/box_definitions.h"
7 #include <gflags/gflags.h>
11 #include "packager/base/logging.h"
12 #include "packager/media/base/bit_reader.h"
13 #include "packager/media/base/macros.h"
14 #include "packager/media/base/rcheck.h"
15 #include "packager/media/formats/mp4/box_buffer.h"
17 DEFINE_bool(mvex_before_trak,
19 "Android MediaExtractor requires mvex to be written before trak. "
20 "Set the flag to true to comply with the requirement.");
23 const uint32_t kFourCCSize = 4;
26 const uint32_t kCencKeyIdSize = 16;
29 const uint8_t kUnityMatrix[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
31 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0};
34 const char kVideoHandlerName[] =
"VideoHandler";
35 const char kAudioHandlerName[] =
"SoundHandler";
36 const char kTextHandlerName[] =
"TextHandler";
37 const char kSubtitleHandlerName[] =
"SubtitleHandler";
40 const uint32_t kVideoResolution = 0x00480000;
41 const uint16_t kVideoFrameCount = 1;
42 const uint16_t kVideoDepth = 0x0018;
44 const uint32_t kCompressorNameSize = 32u;
45 const char kAv1CompressorName[] =
"\012AOM Coding";
46 const char kAvcCompressorName[] =
"\012AVC Coding";
47 const char kDolbyVisionCompressorName[] =
"\013DOVI Coding";
48 const char kHevcCompressorName[] =
"\013HEVC Coding";
49 const char kVpcCompressorName[] =
"\012VPC Coding";
54 bool IsIvSizeValid(uint8_t per_sample_iv_size) {
55 return per_sample_iv_size == 0 || per_sample_iv_size == 8 ||
56 per_sample_iv_size == 16;
73 const uint8_t kDdtsExtraData[] = {0xe4, 0x7c, 0, 4, 0, 0x0f, 0};
76 bool IsFitIn32Bits(uint64_t a) {
77 return a <= std::numeric_limits<uint32_t>::max();
80 bool IsFitIn32Bits(int64_t a) {
81 return a <= std::numeric_limits<int32_t>::max() &&
82 a >= std::numeric_limits<int32_t>::min();
85 template <
typename T1,
typename T2>
86 bool IsFitIn32Bits(T1 a1, T2 a2) {
87 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
90 template <
typename T1,
typename T2,
typename T3>
91 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
92 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
103 TrackType FourCCToTrackType(FourCC fourcc) {
118 FourCC TrackTypeToFourCC(TrackType track_type) {
119 switch (track_type) {
133 bool IsProtectionSchemeSupported(FourCC scheme) {
134 return scheme == FOURCC_cenc || scheme == FOURCC_cens ||
135 scheme == FOURCC_cbc1 || scheme == FOURCC_cbcs;
140 FileType::FileType() =
default;
141 FileType::~FileType() =
default;
147 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
149 buffer->ReadWriteFourCC(&major_brand) &&
150 buffer->ReadWriteUInt32(&minor_version));
153 RCHECK(buffer->
BytesLeft() %
sizeof(FourCC) == 0);
154 num_brands = buffer->
BytesLeft() /
sizeof(FourCC);
155 compatible_brands.resize(num_brands);
157 num_brands = compatible_brands.size();
159 for (
size_t i = 0; i < num_brands; ++i)
160 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
164 size_t FileType::ComputeSizeInternal() {
165 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
166 kFourCCSize * compatible_brands.size();
173 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() =
default;
174 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() =
default;
180 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
184 raw_box.assign(reader->data(), reader->data() + reader->size());
186 DCHECK(!raw_box.empty());
187 buffer->
writer()->AppendVector(raw_box);
193 size_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
194 return raw_box.size();
197 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() =
default;
198 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() =
default;
204 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
209 uint32_t count =
static_cast<uint32_t
>(offsets.size());
210 RCHECK(buffer->ReadWriteUInt32(&count));
211 offsets.resize(count);
213 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
214 for (uint32_t i = 0; i < count; ++i)
219 size_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
221 if (offsets.size() == 0)
223 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
224 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
227 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize() =
default;
228 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() =
default;
234 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
239 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
240 buffer->ReadWriteUInt32(&sample_count));
241 if (default_sample_info_size == 0)
242 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
246 size_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
248 if (sample_count == 0)
250 return HeaderSize() +
sizeof(default_sample_info_size) +
251 sizeof(sample_count) +
252 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
258 DCHECK(IsIvSizeValid(iv_size));
261 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
263 if (!has_subsamples) {
268 uint16_t subsample_count =
static_cast<uint16_t
>(subsamples.size());
269 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
270 RCHECK(subsample_count > 0);
271 subsamples.resize(subsample_count);
272 for (
auto& subsample : subsamples) {
273 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
274 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
282 DCHECK(IsIvSizeValid(iv_size));
285 initialization_vector.resize(iv_size);
286 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
288 if (!has_subsamples) {
293 uint16_t subsample_count;
294 RCHECK(reader->Read2(&subsample_count));
295 RCHECK(subsample_count > 0);
296 subsamples.resize(subsample_count);
297 for (
auto& subsample : subsamples) {
298 RCHECK(reader->Read2(&subsample.clear_bytes) &&
299 reader->Read4(&subsample.cipher_bytes));
305 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
306 const uint16_t subsample_count =
static_cast<uint16_t
>(subsamples.size());
307 return static_cast<uint32_t
>(
308 initialization_vector.size() +
310 ? (
sizeof(subsample_count) + subsample_entry_size * subsample_count)
316 for (uint32_t i = 0; i < subsamples.size(); ++i)
317 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
321 SampleEncryption::SampleEncryption() =
default;
322 SampleEncryption::~SampleEncryption() =
default;
328 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
333 if (buffer->
Reading() && iv_size == SampleEncryption::kInvalidIvSize) {
339 if (!IsIvSizeValid(iv_size)) {
341 <<
"IV_size can only be 8 or 16 or 0 for constant iv, but seeing "
346 uint32_t sample_count =
347 static_cast<uint32_t
>(sample_encryption_entries.size());
348 RCHECK(buffer->ReadWriteUInt32(&sample_count));
350 sample_encryption_entries.resize(sample_count);
351 for (
auto& sample_encryption_entry : sample_encryption_entries) {
352 RCHECK(sample_encryption_entry.ReadWrite(
353 iv_size, (flags & kUseSubsampleEncryption) != 0, buffer) != 0);
358 size_t SampleEncryption::ComputeSizeInternal() {
359 const uint32_t sample_count =
360 static_cast<uint32_t
>(sample_encryption_entries.size());
361 if (sample_count == 0) {
366 DCHECK(IsIvSizeValid(iv_size));
368 if (flags & kUseSubsampleEncryption) {
369 for (
const SampleEncryptionEntry& sample_encryption_entry :
370 sample_encryption_entries) {
371 box_size += sample_encryption_entry.ComputeSize();
381 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
382 DCHECK(IsIvSizeValid(iv_size));
386 uint32_t sample_count = 0;
387 RCHECK(reader.Read4(&sample_count));
389 sample_encryption_entries->resize(sample_count);
390 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
391 RCHECK(sample_encryption_entry.ParseFromBuffer(
392 iv_size, (flags & kUseSubsampleEncryption) != 0, &reader) != 0);
397 OriginalFormat::OriginalFormat() =
default;
398 OriginalFormat::~OriginalFormat() =
default;
404 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
408 size_t OriginalFormat::ComputeSizeInternal() {
412 SchemeType::SchemeType() =
default;
413 SchemeType::~SchemeType() =
default;
419 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
421 buffer->ReadWriteUInt32(&version));
425 size_t SchemeType::ComputeSizeInternal() {
426 return HeaderSize() + kFourCCSize +
sizeof(version);
429 TrackEncryption::TrackEncryption() =
default;
430 TrackEncryption::~TrackEncryption() =
default;
436 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
438 if (default_kid.size() != kCencKeyIdSize) {
439 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
440 <<
" bytes; got " << default_kid.size()
441 <<
". Resized accordingly.";
442 default_kid.resize(kCencKeyIdSize);
444 RCHECK(default_crypt_byte_block < 16 && default_skip_byte_block < 16);
445 if (default_crypt_byte_block != 0 && default_skip_byte_block != 0) {
454 uint8_t pattern = default_crypt_byte_block << 4 | default_skip_byte_block;
455 RCHECK(buffer->ReadWriteUInt8(&pattern));
456 default_crypt_byte_block = pattern >> 4;
457 default_skip_byte_block = pattern & 0x0F;
459 RCHECK(buffer->ReadWriteUInt8(&default_is_protected) &&
460 buffer->ReadWriteUInt8(&default_per_sample_iv_size) &&
461 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
463 if (default_is_protected == 1) {
464 if (default_per_sample_iv_size == 0) {
465 uint8_t default_constant_iv_size =
466 static_cast<uint8_t
>(default_constant_iv.size());
467 RCHECK(buffer->ReadWriteUInt8(&default_constant_iv_size));
468 RCHECK(default_constant_iv_size == 8 || default_constant_iv_size == 16);
469 RCHECK(buffer->ReadWriteVector(&default_constant_iv,
470 default_constant_iv_size));
472 RCHECK(default_per_sample_iv_size == 8 ||
473 default_per_sample_iv_size == 16);
474 RCHECK(default_constant_iv.empty());
479 RCHECK(default_is_protected == 0);
480 RCHECK(default_per_sample_iv_size == 0);
481 RCHECK(default_constant_iv.empty());
486 size_t TrackEncryption::ComputeSizeInternal() {
487 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize +
488 (default_constant_iv.empty()
490 : (
sizeof(uint8_t) + default_constant_iv.size()));
493 SchemeInfo::SchemeInfo() =
default;
494 SchemeInfo::~SchemeInfo() =
default;
500 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
506 size_t SchemeInfo::ComputeSizeInternal() {
510 ProtectionSchemeInfo::ProtectionSchemeInfo() =
default;
511 ProtectionSchemeInfo::~ProtectionSchemeInfo() =
default;
517 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
520 if (IsProtectionSchemeSupported(type.type)) {
523 DLOG(WARNING) <<
"Ignore unsupported protection scheme: "
524 << FourCCToString(type.type);
533 size_t ProtectionSchemeInfo::ComputeSizeInternal() {
535 if (format.format == FOURCC_NULL)
541 MovieHeader::MovieHeader() =
default;
542 MovieHeader::~MovieHeader() =
default;
548 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
551 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
554 buffer->ReadWriteUInt32(×cale) &&
557 std::vector<uint8_t> matrix(kUnityMatrix,
558 kUnityMatrix + arraysize(kUnityMatrix));
559 RCHECK(buffer->ReadWriteInt32(&rate) && buffer->ReadWriteInt16(&volume) &&
561 buffer->ReadWriteVector(&matrix, matrix.size()) &&
563 buffer->ReadWriteUInt32(&next_track_id));
567 size_t MovieHeader::ComputeSizeInternal() {
568 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
569 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
570 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
571 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
575 TrackHeader::TrackHeader() {
576 flags = kTrackEnabled | kTrackInMovie | kTrackInPreview;
579 TrackHeader::~TrackHeader() =
default;
585 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
588 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
591 buffer->ReadWriteUInt32(&track_id) &&
598 volume = (width != 0 && height != 0) ? 0 : 0x100;
600 std::vector<uint8_t> matrix(kUnityMatrix,
601 kUnityMatrix + arraysize(kUnityMatrix));
603 buffer->ReadWriteInt16(&layer) &&
604 buffer->ReadWriteInt16(&alternate_group) &&
605 buffer->ReadWriteInt16(&volume) &&
607 buffer->ReadWriteVector(&matrix, matrix.size()) &&
608 buffer->ReadWriteUInt32(&width) && buffer->ReadWriteUInt32(&height));
612 size_t TrackHeader::ComputeSizeInternal() {
613 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
615 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
616 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
617 sizeof(height) +
sizeof(kUnityMatrix) + 14;
620 SampleDescription::SampleDescription() =
default;
621 SampleDescription::~SampleDescription() =
default;
627 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
631 count =
static_cast<uint32_t
>(video_entries.size());
634 count =
static_cast<uint32_t
>(audio_entries.size());
638 count =
static_cast<uint32_t
>(text_entries.size());
641 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
642 <<
" is not handled. Skipping.";
647 BoxReader* reader = buffer->
reader();
649 video_entries.clear();
650 audio_entries.clear();
653 if (type == kVideo) {
654 RCHECK(reader->ReadAllChildren(&video_entries));
655 RCHECK(video_entries.size() == count);
656 }
else if (type == kAudio) {
657 RCHECK(reader->ReadAllChildren(&audio_entries));
658 RCHECK(audio_entries.size() == count);
659 }
else if (type == kText || type == kSubtitle) {
660 RCHECK(reader->ReadAllChildren(&text_entries));
661 RCHECK(text_entries.size() == count);
664 DCHECK_LT(0u, count);
665 if (type == kVideo) {
666 for (uint32_t i = 0; i < count; ++i)
668 }
else if (type == kAudio) {
669 for (uint32_t i = 0; i < count; ++i)
671 }
else if (type == kText || type == kSubtitle) {
672 for (uint32_t i = 0; i < count; ++i)
681 size_t SampleDescription::ComputeSizeInternal() {
683 if (type == kVideo) {
684 for (uint32_t i = 0; i < video_entries.size(); ++i)
686 }
else if (type == kAudio) {
687 for (uint32_t i = 0; i < audio_entries.size(); ++i)
689 }
else if (type == kText || type == kSubtitle) {
690 for (uint32_t i = 0; i < text_entries.size(); ++i)
696 DecodingTimeToSample::DecodingTimeToSample() =
default;
697 DecodingTimeToSample::~DecodingTimeToSample() =
default;
703 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
704 uint32_t count =
static_cast<uint32_t
>(decoding_time.size());
707 decoding_time.resize(count);
708 for (uint32_t i = 0; i < count; ++i) {
709 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
710 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
715 size_t DecodingTimeToSample::ComputeSizeInternal() {
717 sizeof(DecodingTime) * decoding_time.size();
720 CompositionTimeToSample::CompositionTimeToSample() =
default;
721 CompositionTimeToSample::~CompositionTimeToSample() =
default;
727 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
728 uint32_t count =
static_cast<uint32_t
>(composition_offset.size());
734 for (uint32_t i = 0; i < count; ++i) {
735 if (composition_offset[i].sample_offset < 0) {
744 composition_offset.resize(count);
745 for (uint32_t i = 0; i < count; ++i) {
746 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
749 uint32_t sample_offset = composition_offset[i].sample_offset;
750 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
751 composition_offset[i].sample_offset = sample_offset;
753 int32_t sample_offset = composition_offset[i].sample_offset;
754 RCHECK(buffer->ReadWriteInt32(&sample_offset));
755 composition_offset[i].sample_offset = sample_offset;
761 size_t CompositionTimeToSample::ComputeSizeInternal() {
763 if (composition_offset.empty())
768 const size_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
770 kCompositionOffsetSize * composition_offset.size();
773 SampleToChunk::SampleToChunk() =
default;
774 SampleToChunk::~SampleToChunk() =
default;
780 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
781 uint32_t count =
static_cast<uint32_t
>(chunk_info.size());
784 chunk_info.resize(count);
785 for (uint32_t i = 0; i < count; ++i) {
786 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
787 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
788 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
790 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
791 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
796 size_t SampleToChunk::ComputeSizeInternal() {
798 sizeof(ChunkInfo) * chunk_info.size();
801 SampleSize::SampleSize() =
default;
802 SampleSize::~SampleSize() =
default;
808 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
810 buffer->ReadWriteUInt32(&sample_size) &&
811 buffer->ReadWriteUInt32(&sample_count));
813 if (sample_size == 0) {
815 sizes.resize(sample_count);
817 DCHECK(sample_count == sizes.size());
818 for (uint32_t i = 0; i < sample_count; ++i)
819 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
824 size_t SampleSize::ComputeSizeInternal() {
825 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
826 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
829 CompactSampleSize::CompactSampleSize() =
default;
830 CompactSampleSize::~CompactSampleSize() =
default;
836 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
837 uint32_t sample_count =
static_cast<uint32_t
>(sizes.size());
839 buffer->ReadWriteUInt8(&field_size) &&
840 buffer->ReadWriteUInt32(&sample_count));
843 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
844 switch (field_size) {
846 for (uint32_t i = 0; i < sample_count; i += 2) {
849 RCHECK(buffer->ReadWriteUInt8(&size));
850 sizes[i] = size >> 4;
851 sizes[i + 1] = size & 0x0F;
853 DCHECK_LT(sizes[i], 16u);
854 DCHECK_LT(sizes[i + 1], 16u);
855 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
856 RCHECK(buffer->ReadWriteUInt8(&size));
861 for (uint32_t i = 0; i < sample_count; ++i) {
862 uint8_t size = sizes[i];
863 RCHECK(buffer->ReadWriteUInt8(&size));
868 for (uint32_t i = 0; i < sample_count; ++i) {
869 uint16_t size = sizes[i];
870 RCHECK(buffer->ReadWriteUInt16(&size));
877 sizes.resize(sample_count);
881 size_t CompactSampleSize::ComputeSizeInternal() {
882 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
883 (field_size * sizes.size() + 7) / 8;
886 ChunkOffset::ChunkOffset() =
default;
887 ChunkOffset::~ChunkOffset() =
default;
893 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
894 uint32_t count =
static_cast<uint32_t
>(offsets.size());
897 offsets.resize(count);
898 for (uint32_t i = 0; i < count; ++i)
903 size_t ChunkOffset::ComputeSizeInternal() {
904 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
907 ChunkLargeOffset::ChunkLargeOffset() =
default;
908 ChunkLargeOffset::~ChunkLargeOffset() =
default;
914 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
915 uint32_t count =
static_cast<uint32_t
>(offsets.size());
919 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
921 stco.offsets.swap(offsets);
924 stco.offsets.swap(offsets);
931 offsets.resize(count);
932 for (uint32_t i = 0; i < count; ++i)
933 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
937 size_t ChunkLargeOffset::ComputeSizeInternal() {
938 uint32_t count =
static_cast<uint32_t
>(offsets.size());
939 int use_large_offset =
940 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
942 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
945 SyncSample::SyncSample() =
default;
946 SyncSample::~SyncSample() =
default;
952 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
953 uint32_t count =
static_cast<uint32_t
>(sample_number.size());
956 sample_number.resize(count);
957 for (uint32_t i = 0; i < count; ++i)
958 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
962 size_t SyncSample::ComputeSizeInternal() {
964 if (sample_number.empty())
967 sizeof(uint32_t) * sample_number.size();
970 bool CencSampleEncryptionInfoEntry::ReadWrite(BoxBuffer* buffer) {
971 if (!buffer->Reading()) {
972 if (key_id.size() != kCencKeyIdSize) {
973 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
974 <<
" bytes; got " << key_id.size()
975 <<
". Resized accordingly.";
976 key_id.resize(kCencKeyIdSize);
978 RCHECK(crypt_byte_block < 16 && skip_byte_block < 16);
981 RCHECK(buffer->IgnoreBytes(1));
983 uint8_t pattern = crypt_byte_block << 4 | skip_byte_block;
984 RCHECK(buffer->ReadWriteUInt8(&pattern));
985 crypt_byte_block = pattern >> 4;
986 skip_byte_block = pattern & 0x0F;
988 RCHECK(buffer->ReadWriteUInt8(&is_protected) &&
989 buffer->ReadWriteUInt8(&per_sample_iv_size) &&
990 buffer->ReadWriteVector(&key_id, kCencKeyIdSize));
992 if (is_protected == 1) {
993 if (per_sample_iv_size == 0) {
994 uint8_t constant_iv_size =
static_cast<uint8_t
>(constant_iv.size());
995 RCHECK(buffer->ReadWriteUInt8(&constant_iv_size));
996 RCHECK(constant_iv_size == 8 || constant_iv_size == 16);
997 RCHECK(buffer->ReadWriteVector(&constant_iv, constant_iv_size));
999 RCHECK(per_sample_iv_size == 8 || per_sample_iv_size == 16);
1000 DCHECK(constant_iv.empty());
1005 RCHECK(is_protected == 0);
1006 RCHECK(per_sample_iv_size == 0);
1011 uint32_t CencSampleEncryptionInfoEntry::ComputeSize()
const {
1012 return static_cast<uint32_t
>(
1013 sizeof(uint32_t) + kCencKeyIdSize +
1014 (constant_iv.empty() ? 0 : (
sizeof(uint8_t) + constant_iv.size())));
1017 bool AudioRollRecoveryEntry::ReadWrite(BoxBuffer* buffer) {
1018 RCHECK(buffer->ReadWriteInt16(&roll_distance));
1022 uint32_t AudioRollRecoveryEntry::ComputeSize()
const {
1023 return sizeof(roll_distance);
1026 SampleGroupDescription::SampleGroupDescription() =
default;
1027 SampleGroupDescription::~SampleGroupDescription() =
default;
1033 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
1035 buffer->ReadWriteUInt32(&grouping_type));
1037 switch (grouping_type) {
1039 return ReadWriteEntries(buffer, &cenc_sample_encryption_info_entries);
1041 return ReadWriteEntries(buffer, &audio_roll_recovery_entries);
1044 DLOG(WARNING) <<
"Ignore unsupported sample group: "
1045 << FourCCToString(
static_cast<FourCC
>(grouping_type));
1050 template <
typename T>
1051 bool SampleGroupDescription::ReadWriteEntries(BoxBuffer* buffer,
1052 std::vector<T>* entries) {
1053 uint32_t default_length = 0;
1054 if (!buffer->Reading()) {
1055 DCHECK(!entries->empty());
1056 default_length = (*entries)[0].ComputeSize();
1057 DCHECK_NE(default_length, 0u);
1060 RCHECK(buffer->ReadWriteUInt32(&default_length));
1062 NOTIMPLEMENTED() <<
"Unsupported SampleGroupDescriptionBox 'sgpd' version "
1063 <<
static_cast<int>(version);
1067 uint32_t count =
static_cast<uint32_t
>(entries->size());
1068 RCHECK(buffer->ReadWriteUInt32(&count));
1069 if (buffer->Reading()) {
1075 entries->resize(count);
1077 for (T& entry : *entries) {
1079 uint32_t description_length = default_length;
1080 if (buffer->Reading() && default_length == 0)
1081 RCHECK(buffer->ReadWriteUInt32(&description_length));
1082 RCHECK(entry.ReadWrite(buffer));
1083 RCHECK(entry.ComputeSize() == description_length);
1085 RCHECK(entry.ReadWrite(buffer));
1091 size_t SampleGroupDescription::ComputeSizeInternal() {
1094 size_t entries_size = 0;
1095 switch (grouping_type) {
1097 for (
const auto& entry : cenc_sample_encryption_info_entries)
1098 entries_size += entry.ComputeSize();
1101 for (
const auto& entry : audio_roll_recovery_entries)
1102 entries_size += entry.ComputeSize();
1106 if (entries_size == 0)
1108 return HeaderSize() +
sizeof(grouping_type) +
1109 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
1113 SampleToGroup::SampleToGroup() =
default;
1114 SampleToGroup::~SampleToGroup() =
default;
1120 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
1122 buffer->ReadWriteUInt32(&grouping_type));
1124 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
1126 if (grouping_type != FOURCC_seig && grouping_type != FOURCC_roll) {
1128 DLOG(WARNING) <<
"Ignore unsupported sample group: "
1129 << FourCCToString(
static_cast<FourCC
>(grouping_type));
1133 uint32_t count =
static_cast<uint32_t
>(entries.size());
1134 RCHECK(buffer->ReadWriteUInt32(&count));
1135 entries.resize(count);
1136 for (uint32_t i = 0; i < count; ++i) {
1137 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
1138 buffer->ReadWriteUInt32(&entries[i].group_description_index));
1143 size_t SampleToGroup::ComputeSizeInternal() {
1145 if (entries.empty())
1147 return HeaderSize() +
sizeof(grouping_type) +
1148 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
1149 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
1152 SampleTable::SampleTable() =
default;
1153 SampleTable::~SampleTable() =
default;
1159 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
1172 RCHECK(reader->
ReadChild(&sample_size));
1174 CompactSampleSize compact_sample_size;
1175 RCHECK(reader->
ReadChild(&compact_sample_size));
1176 sample_size.sample_size = 0;
1177 sample_size.sample_count =
1178 static_cast<uint32_t
>(compact_sample_size.sizes.size());
1179 sample_size.sizes.swap(compact_sample_size.sizes);
1183 if (reader->
ChildExist(&chunk_large_offset)) {
1184 RCHECK(reader->
ReadChild(&chunk_large_offset));
1186 ChunkOffset chunk_offset;
1187 RCHECK(reader->
ReadChild(&chunk_offset));
1188 chunk_large_offset.offsets.swap(chunk_offset.offsets);
1199 for (
auto& sample_group_description : sample_group_descriptions)
1201 for (
auto& sample_to_group : sample_to_groups)
1207 size_t SampleTable::ComputeSizeInternal() {
1214 for (
auto& sample_group_description : sample_group_descriptions)
1215 box_size += sample_group_description.ComputeSize();
1216 for (
auto& sample_to_group : sample_to_groups)
1217 box_size += sample_to_group.ComputeSize();
1221 EditList::EditList() =
default;
1222 EditList::~EditList() =
default;
1228 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
1229 uint32_t count =
static_cast<uint32_t
>(edits.size());
1231 edits.resize(count);
1233 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1234 for (uint32_t i = 0; i < count; ++i) {
1237 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
1238 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
1239 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
1244 size_t EditList::ComputeSizeInternal() {
1250 for (uint32_t i = 0; i < edits.size(); ++i) {
1251 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
1257 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
1261 Edit::Edit() =
default;
1262 Edit::~Edit() =
default;
1268 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
1273 size_t Edit::ComputeSizeInternal() {
1275 if (list.edits.empty())
1280 HandlerReference::HandlerReference() =
default;
1281 HandlerReference::~HandlerReference() =
default;
1287 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1288 std::vector<uint8_t> handler_name;
1290 switch (handler_type) {
1292 handler_name.assign(kVideoHandlerName,
1293 kVideoHandlerName + arraysize(kVideoHandlerName));
1296 handler_name.assign(kAudioHandlerName,
1297 kAudioHandlerName + arraysize(kAudioHandlerName));
1300 handler_name.assign(kTextHandlerName,
1301 kTextHandlerName + arraysize(kTextHandlerName));
1304 handler_name.assign(
1305 kSubtitleHandlerName,
1306 kSubtitleHandlerName + arraysize(kSubtitleHandlerName));
1317 buffer->ReadWriteFourCC(&handler_type));
1320 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1325 size_t HandlerReference::ComputeSizeInternal() {
1327 switch (handler_type) {
1329 box_size +=
sizeof(kVideoHandlerName);
1332 box_size +=
sizeof(kAudioHandlerName);
1335 box_size +=
sizeof(kTextHandlerName);
1338 box_size +=
sizeof(kSubtitleHandlerName);
1348 bool Language::ReadWrite(BoxBuffer* buffer) {
1349 if (buffer->Reading()) {
1352 std::vector<uint8_t> temp;
1353 RCHECK(buffer->ReadWriteVector(&temp, 2));
1355 BitReader bit_reader(&temp[0], 2);
1356 bit_reader.SkipBits(1);
1358 for (
int i = 0; i < 3; ++i) {
1359 CHECK(bit_reader.ReadBits(5, &language[i]));
1360 language[i] += 0x60;
1362 code.assign(language, 3);
1365 const char kUndefinedLanguage[] =
"und";
1367 code = kUndefinedLanguage;
1368 DCHECK_EQ(code.size(), 3u);
1372 for (
int i = 0; i < 3; ++i)
1373 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1374 RCHECK(buffer->ReadWriteUInt16(&lang));
1379 uint32_t Language::ComputeSize()
const {
1384 ID3v2::ID3v2() =
default;
1385 ID3v2::~ID3v2() =
default;
1391 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1393 buffer->ReadWriteVector(&id3v2_data, buffer->
Reading()
1395 : id3v2_data.size()));
1399 size_t ID3v2::ComputeSizeInternal() {
1401 return id3v2_data.size() == 0
1403 :
HeaderSize() + language.ComputeSize() + id3v2_data.size();
1406 Metadata::Metadata() =
default;
1407 Metadata::~Metadata() =
default;
1413 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1419 size_t Metadata::ComputeSizeInternal() {
1422 return id3v2_size == 0 ? 0
1426 CodecConfiguration::CodecConfiguration() =
default;
1427 CodecConfiguration::~CodecConfiguration() =
default;
1435 bool CodecConfiguration::ReadWriteInternal(
BoxBuffer* buffer) {
1436 DCHECK_NE(box_type, FOURCC_NULL);
1441 if (box_type == FOURCC_vpcC) {
1443 uint8_t vpcc_version = 1;
1444 uint32_t version_flags = vpcc_version << 24;
1445 RCHECK(buffer->ReadWriteUInt32(&version_flags));
1446 vpcc_version = version_flags >> 24;
1447 RCHECK(vpcc_version == 1);
1451 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1453 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1458 size_t CodecConfiguration::ComputeSizeInternal() {
1461 DCHECK_NE(box_type, FOURCC_NULL);
1462 return HeaderSize() + (box_type == FOURCC_vpcC ? 4 : 0) + data.size();
1465 PixelAspectRatio::PixelAspectRatio() =
default;
1466 PixelAspectRatio::~PixelAspectRatio() =
default;
1472 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1474 buffer->ReadWriteUInt32(&h_spacing) &&
1475 buffer->ReadWriteUInt32(&v_spacing));
1479 size_t PixelAspectRatio::ComputeSizeInternal() {
1481 if (h_spacing == 0 && v_spacing == 0)
1484 DCHECK(h_spacing != 0 && v_spacing != 0);
1485 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1488 VideoSampleEntry::VideoSampleEntry() =
default;
1489 VideoSampleEntry::~VideoSampleEntry() =
default;
1492 if (format == FOURCC_NULL) {
1493 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
1494 <<
"handler type recovered in its Media ancestor.";
1499 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1500 std::vector<uint8_t> compressor_name;
1502 DCHECK(buffer->
reader());
1503 format = buffer->
reader()->type();
1507 const FourCC actual_format = GetActualFormat();
1508 switch (actual_format) {
1510 compressor_name.assign(std::begin(kAv1CompressorName),
1511 std::end(kAv1CompressorName));
1515 compressor_name.assign(std::begin(kAvcCompressorName),
1516 std::end(kAvcCompressorName));
1520 compressor_name.assign(std::begin(kDolbyVisionCompressorName),
1521 std::end(kDolbyVisionCompressorName));
1525 compressor_name.assign(std::begin(kHevcCompressorName),
1526 std::end(kHevcCompressorName));
1530 compressor_name.assign(std::begin(kVpcCompressorName),
1531 std::end(kVpcCompressorName));
1534 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1537 compressor_name.resize(kCompressorNameSize);
1540 uint32_t video_resolution = kVideoResolution;
1541 uint16_t video_frame_count = kVideoFrameCount;
1542 uint16_t video_depth = kVideoDepth;
1543 int16_t predefined = -1;
1545 buffer->ReadWriteUInt16(&data_reference_index) &&
1547 buffer->ReadWriteUInt16(&width) && buffer->ReadWriteUInt16(&height) &&
1548 buffer->ReadWriteUInt32(&video_resolution) &&
1549 buffer->ReadWriteUInt32(&video_resolution) &&
1551 buffer->ReadWriteUInt16(&video_frame_count) &&
1552 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1553 buffer->ReadWriteUInt16(&video_depth) &&
1554 buffer->ReadWriteInt16(&predefined));
1561 if (format == FOURCC_encv && buffer->
Reading()) {
1564 while (!IsProtectionSchemeSupported(sinf.type.type))
1568 const FourCC actual_format = GetActualFormat();
1570 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1572 DCHECK_EQ(codec_configuration.box_type,
1573 GetCodecConfigurationBoxType(actual_format));
1575 if (codec_configuration.box_type == FOURCC_NULL)
1581 extra_codec_configs.clear();
1583 const bool is_hevc =
1584 actual_format == FOURCC_dvhe || actual_format == FOURCC_dvh1 ||
1585 actual_format == FOURCC_hev1 || actual_format == FOURCC_hvc1;
1587 for (FourCC fourcc : {FOURCC_dvcC, FOURCC_dvvC, FOURCC_hvcE}) {
1588 CodecConfiguration dv_box;
1589 dv_box.box_type = fourcc;
1591 if (!dv_box.data.empty())
1592 extra_codec_configs.push_back(std::move(dv_box));
1596 for (CodecConfiguration& extra_codec_config : extra_codec_configs)
1605 if (format == FOURCC_encv && !buffer->
Reading()) {
1606 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1612 size_t VideoSampleEntry::ComputeSizeInternal() {
1613 const FourCC actual_format = GetActualFormat();
1614 if (actual_format == FOURCC_NULL)
1616 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1617 DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1618 size_t size =
HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1619 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1620 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1622 codec_configuration.
ComputeSize() + kCompressorNameSize + 6 +
1624 for (CodecConfiguration& codec_config : extra_codec_configs)
1625 size += codec_config.ComputeSize();
1629 FourCC VideoSampleEntry::GetCodecConfigurationBoxType(FourCC format)
const {
1645 LOG(ERROR) << FourCCToString(format) <<
" is not supported.";
1650 std::vector<uint8_t> VideoSampleEntry::ExtraCodecConfigsAsVector()
const {
1651 BufferWriter buffer;
1652 for (CodecConfiguration codec_config : extra_codec_configs)
1653 codec_config.Write(&buffer);
1654 return std::vector<uint8_t>(buffer.Buffer(), buffer.Buffer() + buffer.Size());
1657 bool VideoSampleEntry::ParseExtraCodecConfigsVector(
1658 const std::vector<uint8_t>& data) {
1659 extra_codec_configs.clear();
1661 while (pos < data.size()) {
1663 std::unique_ptr<BoxReader> box_reader(
1665 RCHECK(!err && box_reader);
1667 CodecConfiguration codec_config;
1668 codec_config.box_type = box_reader->type();
1669 RCHECK(codec_config.Parse(box_reader.get()));
1670 extra_codec_configs.push_back(std::move(codec_config));
1672 pos += box_reader->pos();
1677 ElementaryStreamDescriptor::ElementaryStreamDescriptor() =
default;
1678 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() =
default;
1684 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1687 std::vector<uint8_t> data;
1688 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1689 RCHECK(es_descriptor.
Parse(data));
1690 if (es_descriptor.decoder_config_descriptor().
IsAAC()) {
1691 RCHECK(aac_audio_specific_config.
Parse(
1692 es_descriptor.decoder_config_descriptor()
1693 .decoder_specific_info_descriptor()
1697 DCHECK(buffer->
writer());
1703 size_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1705 if (es_descriptor.decoder_config_descriptor().object_type() ==
1706 ObjectType::kForbidden) {
1712 DTSSpecific::DTSSpecific() =
default;
1713 DTSSpecific::~DTSSpecific() =
default;
1720 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1722 buffer->ReadWriteUInt32(&sampling_frequency) &&
1723 buffer->ReadWriteUInt32(&max_bitrate) &&
1724 buffer->ReadWriteUInt32(&avg_bitrate) &&
1725 buffer->ReadWriteUInt8(&pcm_sample_depth));
1728 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
BytesLeft()));
1730 if (extra_data.empty()) {
1731 extra_data.assign(kDdtsExtraData,
1732 kDdtsExtraData +
sizeof(kDdtsExtraData));
1734 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1739 size_t DTSSpecific::ComputeSizeInternal() {
1741 if (sampling_frequency == 0)
1743 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1744 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1745 sizeof(kDdtsExtraData);
1748 AC3Specific::AC3Specific() =
default;
1749 AC3Specific::~AC3Specific() =
default;
1755 bool AC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1757 buffer->ReadWriteVector(
1762 size_t AC3Specific::ComputeSizeInternal() {
1769 EC3Specific::EC3Specific() =
default;
1770 EC3Specific::~EC3Specific() =
default;
1776 bool EC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1779 RCHECK(buffer->ReadWriteVector(&data, size));
1783 size_t EC3Specific::ComputeSizeInternal() {
1790 AC4Specific::AC4Specific() =
default;
1791 AC4Specific::~AC4Specific() =
default;
1797 bool AC4Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1800 RCHECK(buffer->ReadWriteVector(&data, size));
1804 size_t AC4Specific::ComputeSizeInternal() {
1811 OpusSpecific::OpusSpecific() =
default;
1812 OpusSpecific::~OpusSpecific() =
default;
1818 bool OpusSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1821 std::vector<uint8_t> data;
1822 const int kMinOpusSpecificBoxDataSize = 11;
1823 RCHECK(buffer->
BytesLeft() >= kMinOpusSpecificBoxDataSize);
1824 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1825 preskip = data[2] + (data[3] << 8);
1830 writer.AppendInt(FOURCC_Head);
1832 const uint8_t kOpusIdentificationHeaderVersion = 1;
1833 data[0] = kOpusIdentificationHeaderVersion;
1834 writer.AppendVector(data);
1835 writer.SwapBuffer(&opus_identification_header);
1839 const size_t kOpusMagicSignatureSize = 8u;
1840 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1843 const uint8_t kOpusSpecificBoxVersion = 0;
1845 buffer->
writer()->AppendArray(
1846 &opus_identification_header[kOpusMagicSignatureSize + 1],
1847 opus_identification_header.size() - kOpusMagicSignatureSize - 1);
1852 size_t OpusSpecific::ComputeSizeInternal() {
1854 if (opus_identification_header.empty())
1858 const size_t kOpusMagicSignatureSize = 8u;
1859 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1860 return HeaderSize() + opus_identification_header.size() -
1861 kOpusMagicSignatureSize;
1864 FlacSpecific::FlacSpecific() =
default;
1865 FlacSpecific::~FlacSpecific() =
default;
1871 bool FlacSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1874 RCHECK(buffer->ReadWriteVector(&data, size));
1878 size_t FlacSpecific::ComputeSizeInternal() {
1885 AudioSampleEntry::AudioSampleEntry() =
default;
1886 AudioSampleEntry::~AudioSampleEntry() =
default;
1889 if (format == FOURCC_NULL) {
1890 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1891 <<
"handler type recovered in its Media ancestor.";
1896 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1898 DCHECK(buffer->
reader());
1899 format = buffer->
reader()->type();
1907 buffer->ReadWriteUInt16(&data_reference_index) &&
1909 buffer->ReadWriteUInt16(&channelcount) &&
1910 buffer->ReadWriteUInt16(&samplesize) &&
1912 buffer->ReadWriteUInt32(&samplerate));
1929 if (format == FOURCC_enca) {
1933 while (!IsProtectionSchemeSupported(sinf.type.type))
1936 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1943 size_t AudioSampleEntry::ComputeSizeInternal() {
1944 if (GetActualFormat() == FOURCC_NULL)
1946 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1947 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1956 WebVTTConfigurationBox::WebVTTConfigurationBox() =
default;
1957 WebVTTConfigurationBox::~WebVTTConfigurationBox() =
default;
1963 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1969 size_t WebVTTConfigurationBox::ComputeSizeInternal() {
1973 WebVTTSourceLabelBox::WebVTTSourceLabelBox() =
default;
1974 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() =
default;
1980 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1984 : source_label.size());
1987 size_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1988 if (source_label.empty())
1993 TextSampleEntry::TextSampleEntry() =
default;
1994 TextSampleEntry::~TextSampleEntry() =
default;
1997 if (format == FOURCC_NULL) {
1998 LOG(ERROR) <<
"TextSampleEntry should be parsed according to the "
1999 <<
"handler type recovered in its Media ancestor.";
2004 bool TextSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
2006 DCHECK(buffer->
reader());
2007 format = buffer->
reader()->type();
2012 buffer->ReadWriteUInt16(&data_reference_index));
2014 if (format == FOURCC_wvtt) {
2018 }
else if (format == FOURCC_stpp) {
2021 RCHECK(buffer->ReadWriteCString(&namespace_) &&
2022 buffer->ReadWriteCString(&schema_location));
2027 size_t TextSampleEntry::ComputeSizeInternal() {
2029 size_t ret =
HeaderSize() + 6 +
sizeof(data_reference_index);
2030 if (format == FOURCC_wvtt) {
2032 }
else if (format == FOURCC_stpp) {
2034 ret += namespace_.size() + schema_location.size() + 2;
2039 MediaHeader::MediaHeader() =
default;
2040 MediaHeader::~MediaHeader() =
default;
2046 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2049 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2052 buffer->ReadWriteUInt32(×cale) &&
2054 language.ReadWrite(buffer) &&
2060 size_t MediaHeader::ComputeSizeInternal() {
2061 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
2063 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
2067 VideoMediaHeader::VideoMediaHeader() {
2068 const uint32_t kVideoMediaHeaderFlags = 1;
2069 flags = kVideoMediaHeaderFlags;
2072 VideoMediaHeader::~VideoMediaHeader() =
default;
2077 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2079 buffer->ReadWriteUInt16(&graphicsmode) &&
2080 buffer->ReadWriteUInt16(&opcolor_red) &&
2081 buffer->ReadWriteUInt16(&opcolor_green) &&
2082 buffer->ReadWriteUInt16(&opcolor_blue));
2086 size_t VideoMediaHeader::ComputeSizeInternal() {
2087 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
2088 sizeof(opcolor_green) +
sizeof(opcolor_blue);
2091 SoundMediaHeader::SoundMediaHeader() =
default;
2092 SoundMediaHeader::~SoundMediaHeader() =
default;
2098 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2104 size_t SoundMediaHeader::ComputeSizeInternal() {
2105 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
2108 NullMediaHeader::NullMediaHeader() =
default;
2109 NullMediaHeader::~NullMediaHeader() =
default;
2115 bool NullMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2119 size_t NullMediaHeader::ComputeSizeInternal() {
2123 SubtitleMediaHeader::SubtitleMediaHeader() =
default;
2124 SubtitleMediaHeader::~SubtitleMediaHeader() =
default;
2130 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2134 size_t SubtitleMediaHeader::ComputeSizeInternal() {
2138 DataEntryUrl::DataEntryUrl() {
2139 const uint32_t kDataEntryUrlFlags = 1;
2140 flags = kDataEntryUrlFlags;
2143 DataEntryUrl::~DataEntryUrl() =
default;
2148 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
2151 RCHECK(buffer->ReadWriteVector(&location, buffer->
BytesLeft()));
2153 RCHECK(buffer->ReadWriteVector(&location, location.size()));
2158 size_t DataEntryUrl::ComputeSizeInternal() {
2162 DataReference::DataReference() =
default;
2163 DataReference::~DataReference() =
default;
2168 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
2169 uint32_t entry_count =
static_cast<uint32_t
>(data_entry.size());
2171 buffer->ReadWriteUInt32(&entry_count));
2172 data_entry.resize(entry_count);
2174 for (uint32_t i = 0; i < entry_count; ++i)
2179 size_t DataReference::ComputeSizeInternal() {
2180 uint32_t count =
static_cast<uint32_t
>(data_entry.size());
2182 for (uint32_t i = 0; i < count; ++i)
2187 DataInformation::DataInformation() =
default;
2188 DataInformation::~DataInformation() =
default;
2194 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2199 size_t DataInformation::ComputeSizeInternal() {
2203 MediaInformation::MediaInformation() =
default;
2204 MediaInformation::~MediaInformation() =
default;
2210 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2214 switch (sample_table.description.type) {
2234 size_t MediaInformation::ComputeSizeInternal() {
2237 switch (sample_table.description.type) {
2256 Media::Media() =
default;
2257 Media::~Media() =
default;
2263 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
2274 information.sample_table.description.type =
2275 FourCCToTrackType(handler.handler_type);
2277 handler.handler_type =
2278 TrackTypeToFourCC(information.sample_table.description.type);
2279 RCHECK(handler.handler_type != FOURCC_NULL);
2286 size_t Media::ComputeSizeInternal() {
2287 handler.handler_type =
2288 TrackTypeToFourCC(information.sample_table.description.type);
2293 Track::Track() =
default;
2294 Track::~Track() =
default;
2300 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
2308 size_t Track::ComputeSizeInternal() {
2313 MovieExtendsHeader::MovieExtendsHeader() =
default;
2314 MovieExtendsHeader::~MovieExtendsHeader() =
default;
2320 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2322 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2327 size_t MovieExtendsHeader::ComputeSizeInternal() {
2329 if (fragment_duration == 0)
2331 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
2332 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2335 TrackExtends::TrackExtends() =
default;
2336 TrackExtends::~TrackExtends() =
default;
2342 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2344 buffer->ReadWriteUInt32(&track_id) &&
2345 buffer->ReadWriteUInt32(&default_sample_description_index) &&
2346 buffer->ReadWriteUInt32(&default_sample_duration) &&
2347 buffer->ReadWriteUInt32(&default_sample_size) &&
2348 buffer->ReadWriteUInt32(&default_sample_flags));
2352 size_t TrackExtends::ComputeSizeInternal() {
2354 sizeof(default_sample_description_index) +
2355 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
2356 sizeof(default_sample_flags);
2359 MovieExtends::MovieExtends() =
default;
2360 MovieExtends::~MovieExtends() =
default;
2366 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2370 DCHECK(buffer->
reader());
2373 for (uint32_t i = 0; i < tracks.size(); ++i)
2379 size_t MovieExtends::ComputeSizeInternal() {
2381 if (tracks.size() == 0)
2384 for (uint32_t i = 0; i < tracks.size(); ++i)
2389 Movie::Movie() =
default;
2390 Movie::~Movie() =
default;
2396 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
2412 if (FLAGS_mvex_before_trak) {
2418 for (uint32_t i = 0; i < tracks.size(); ++i)
2420 if (!FLAGS_mvex_before_trak) {
2423 for (uint32_t i = 0; i < pssh.size(); ++i)
2429 size_t Movie::ComputeSizeInternal() {
2432 for (uint32_t i = 0; i < tracks.size(); ++i)
2434 for (uint32_t i = 0; i < pssh.size(); ++i)
2439 TrackFragmentDecodeTime::TrackFragmentDecodeTime() =
default;
2440 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() =
default;
2446 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
2448 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2453 size_t TrackFragmentDecodeTime::ComputeSizeInternal() {
2454 version = IsFitIn32Bits(decode_time) ? 0 : 1;
2455 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2458 MovieFragmentHeader::MovieFragmentHeader() =
default;
2459 MovieFragmentHeader::~MovieFragmentHeader() =
default;
2465 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2467 buffer->ReadWriteUInt32(&sequence_number);
2470 size_t MovieFragmentHeader::ComputeSizeInternal() {
2471 return HeaderSize() +
sizeof(sequence_number);
2474 TrackFragmentHeader::TrackFragmentHeader() =
default;
2475 TrackFragmentHeader::~TrackFragmentHeader() =
default;
2481 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2484 if (flags & kBaseDataOffsetPresentMask) {
2489 uint64_t base_data_offset;
2490 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
2491 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
2492 "default-base-is-moof.";
2495 if (flags & kSampleDescriptionIndexPresentMask) {
2496 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
2497 }
else if (buffer->
Reading()) {
2498 sample_description_index = 0;
2501 if (flags & kDefaultSampleDurationPresentMask) {
2502 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2503 }
else if (buffer->
Reading()) {
2504 default_sample_duration = 0;
2507 if (flags & kDefaultSampleSizePresentMask) {
2508 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2509 }
else if (buffer->
Reading()) {
2510 default_sample_size = 0;
2513 if (flags & kDefaultSampleFlagsPresentMask)
2514 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2518 size_t TrackFragmentHeader::ComputeSizeInternal() {
2520 if (flags & kSampleDescriptionIndexPresentMask)
2521 box_size +=
sizeof(sample_description_index);
2522 if (flags & kDefaultSampleDurationPresentMask)
2523 box_size +=
sizeof(default_sample_duration);
2524 if (flags & kDefaultSampleSizePresentMask)
2525 box_size +=
sizeof(default_sample_size);
2526 if (flags & kDefaultSampleFlagsPresentMask)
2527 box_size +=
sizeof(default_sample_flags);
2531 TrackFragmentRun::TrackFragmentRun() =
default;
2532 TrackFragmentRun::~TrackFragmentRun() =
default;
2538 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
2544 if (flags & kSampleCompTimeOffsetsPresentMask) {
2545 for (uint32_t i = 0; i < sample_count; ++i) {
2546 if (sample_composition_time_offsets[i] < 0) {
2555 buffer->ReadWriteUInt32(&sample_count));
2557 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2558 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2559 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2560 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2561 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2562 bool sample_composition_time_offsets_present =
2563 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2565 if (data_offset_present) {
2566 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2577 uint32_t first_sample_flags(0);
2580 if (first_sample_flags_present)
2581 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2583 if (sample_duration_present)
2584 sample_durations.resize(sample_count);
2585 if (sample_size_present)
2586 sample_sizes.resize(sample_count);
2587 if (sample_flags_present)
2588 sample_flags.resize(sample_count);
2589 if (sample_composition_time_offsets_present)
2590 sample_composition_time_offsets.resize(sample_count);
2592 if (first_sample_flags_present) {
2593 first_sample_flags = sample_flags[0];
2594 DCHECK(sample_flags.size() == 1);
2595 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2598 if (sample_duration_present)
2599 DCHECK(sample_durations.size() == sample_count);
2600 if (sample_size_present)
2601 DCHECK(sample_sizes.size() == sample_count);
2602 if (sample_flags_present)
2603 DCHECK(sample_flags.size() == sample_count);
2604 if (sample_composition_time_offsets_present)
2605 DCHECK(sample_composition_time_offsets.size() == sample_count);
2608 for (uint32_t i = 0; i < sample_count; ++i) {
2609 if (sample_duration_present)
2610 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2611 if (sample_size_present)
2612 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2613 if (sample_flags_present)
2614 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2616 if (sample_composition_time_offsets_present) {
2618 uint32_t sample_offset = sample_composition_time_offsets[i];
2619 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2620 sample_composition_time_offsets[i] = sample_offset;
2622 int32_t sample_offset = sample_composition_time_offsets[i];
2623 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2624 sample_composition_time_offsets[i] = sample_offset;
2630 if (first_sample_flags_present) {
2631 if (sample_flags.size() == 0) {
2632 sample_flags.push_back(first_sample_flags);
2634 sample_flags[0] = first_sample_flags;
2641 size_t TrackFragmentRun::ComputeSizeInternal() {
2643 if (flags & kDataOffsetPresentMask)
2645 if (flags & kFirstSampleFlagsPresentMask)
2647 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2648 (flags & kSampleSizePresentMask ? 1 : 0) +
2649 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2650 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2651 box_size += fields *
sizeof(uint32_t) * sample_count;
2655 TrackFragment::TrackFragment() =
default;
2656 TrackFragment::~TrackFragment() =
default;
2662 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2666 DCHECK(buffer->
reader());
2668 if (!decode_time_absent)
2674 if (!decode_time_absent)
2676 for (uint32_t i = 0; i < runs.size(); ++i)
2678 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2680 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2688 size_t TrackFragment::ComputeSizeInternal() {
2693 for (uint32_t i = 0; i < runs.size(); ++i)
2695 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2697 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2702 MovieFragment::MovieFragment() =
default;
2703 MovieFragment::~MovieFragment() =
default;
2709 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2717 for (uint32_t i = 0; i < tracks.size(); ++i)
2719 for (uint32_t i = 0; i < pssh.size(); ++i)
2725 size_t MovieFragment::ComputeSizeInternal() {
2727 for (uint32_t i = 0; i < tracks.size(); ++i)
2729 for (uint32_t i = 0; i < pssh.size(); ++i)
2734 SegmentIndex::SegmentIndex() =
default;
2735 SegmentIndex::~SegmentIndex() =
default;
2741 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2743 buffer->ReadWriteUInt32(&reference_id) &&
2744 buffer->ReadWriteUInt32(×cale));
2746 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2751 uint16_t reference_count;
2752 if (references.size() <= std::numeric_limits<uint16_t>::max()) {
2753 reference_count =
static_cast<uint16_t
>(references.size());
2755 reference_count = std::numeric_limits<uint16_t>::max();
2756 LOG(WARNING) <<
"Seeing " << references.size()
2757 <<
" subsegment references, but at most " << reference_count
2758 <<
" references can be stored in 'sidx' box."
2759 <<
" The extra references are truncated.";
2760 LOG(WARNING) <<
"The stream will not play to the end in DASH.";
2761 LOG(WARNING) <<
"A possible workaround is to increase segment duration.";
2764 buffer->ReadWriteUInt16(&reference_count));
2766 references.resize(reference_count);
2768 uint32_t reference_type_size;
2770 for (uint32_t i = 0; i < reference_count; ++i) {
2772 reference_type_size = references[i].referenced_size;
2773 if (references[i].reference_type)
2774 reference_type_size |= (1 << 31);
2775 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2776 if (references[i].starts_with_sap)
2779 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2780 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2781 buffer->ReadWriteUInt32(&sap));
2783 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2784 references[i].referenced_size = reference_type_size & ~(1 << 31);
2785 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2786 references[i].sap_type =
2787 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2788 references[i].sap_delta_time = sap & ~(0xF << 28);
2794 size_t SegmentIndex::ComputeSizeInternal() {
2795 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2796 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2797 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2798 3 *
sizeof(uint32_t) *
2801 static_cast<size_t>(std::numeric_limits<uint16_t>::max()));
2804 MediaData::MediaData() =
default;
2805 MediaData::~MediaData() =
default;
2811 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2812 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2816 size_t MediaData::ComputeSizeInternal() {
2820 CueSourceIDBox::CueSourceIDBox() =
default;
2821 CueSourceIDBox::~CueSourceIDBox() =
default;
2827 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2832 size_t CueSourceIDBox::ComputeSizeInternal() {
2833 if (source_id == kCueSourceIdNotSet)
2838 CueTimeBox::CueTimeBox() =
default;
2839 CueTimeBox::~CueTimeBox() =
default;
2845 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2852 size_t CueTimeBox::ComputeSizeInternal() {
2853 if (cue_current_time.empty())
2855 return HeaderSize() + cue_current_time.size();
2858 CueIDBox::CueIDBox() =
default;
2859 CueIDBox::~CueIDBox() =
default;
2865 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2871 size_t CueIDBox::ComputeSizeInternal() {
2877 CueSettingsBox::CueSettingsBox() =
default;
2878 CueSettingsBox::~CueSettingsBox() =
default;
2884 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2890 size_t CueSettingsBox::ComputeSizeInternal() {
2891 if (settings.empty())
2896 CuePayloadBox::CuePayloadBox() =
default;
2897 CuePayloadBox::~CuePayloadBox() =
default;
2903 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2909 size_t CuePayloadBox::ComputeSizeInternal() {
2913 VTTEmptyCueBox::VTTEmptyCueBox() =
default;
2914 VTTEmptyCueBox::~VTTEmptyCueBox() =
default;
2920 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2924 size_t VTTEmptyCueBox::ComputeSizeInternal() {
2928 VTTAdditionalTextBox::VTTAdditionalTextBox() =
default;
2929 VTTAdditionalTextBox::~VTTAdditionalTextBox() =
default;
2935 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2938 &cue_additional_text,
2942 size_t VTTAdditionalTextBox::ComputeSizeInternal() {
2943 return HeaderSize() + cue_additional_text.size();
2946 VTTCueBox::VTTCueBox() =
default;
2947 VTTCueBox::~VTTCueBox() =
default;
2953 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2963 size_t VTTCueBox::ComputeSizeInternal() {