5 #include "packager/media/formats/mp4/box_definitions.h"
9 #include "packager/base/logging.h"
10 #include "packager/media/base/bit_reader.h"
11 #include "packager/media/base/macros.h"
12 #include "packager/media/base/rcheck.h"
13 #include "packager/media/formats/mp4/box_buffer.h"
16 const uint32_t kFourCCSize = 4;
19 const uint32_t kCencKeyIdSize = 16;
22 const uint8_t kUnityMatrix[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
24 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0};
27 const char kVideoHandlerName[] =
"VideoHandler";
28 const char kAudioHandlerName[] =
"SoundHandler";
29 const char kTextHandlerName[] =
"TextHandler";
32 const uint32_t kVideoResolution = 0x00480000;
33 const uint16_t kVideoFrameCount = 1;
34 const uint16_t kVideoDepth = 0x0018;
36 const uint32_t kCompressorNameSize = 32u;
37 const char kAvcCompressorName[] =
"\012AVC Coding";
38 const char kHevcCompressorName[] =
"\013HEVC Coding";
39 const char kVpcCompressorName[] =
"\012VPC Coding";
43 const int kCueSourceIdNotSet = -1;
45 const size_t kInvalidIvSize = 1;
49 bool IsIvSizeValid(uint8_t per_sample_iv_size) {
50 return per_sample_iv_size == 0 || per_sample_iv_size == 8 ||
51 per_sample_iv_size == 16;
68 const uint8_t kDdtsExtraData[] = {0xe4, 0x7c, 0, 4, 0, 0x0f, 0};
71 const uint32_t kID3v2HeaderSize = 10;
72 const char kID3v2Identifier[] =
"ID3";
73 const uint16_t kID3v2Version = 0x0400;
76 bool IsFitIn32Bits(uint64_t a) {
77 return a <= std::numeric_limits<uint32_t>::max();
80 bool IsFitIn32Bits(int64_t a) {
81 return a <= std::numeric_limits<int32_t>::max() &&
82 a >= std::numeric_limits<int32_t>::min();
85 template <
typename T1,
typename T2>
86 bool IsFitIn32Bits(T1 a1, T2 a2) {
87 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
90 template <
typename T1,
typename T2,
typename T3>
91 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
92 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
103 TrackType FourCCToTrackType(FourCC fourcc) {
116 FourCC TrackTypeToFourCC(TrackType track_type) {
117 switch (track_type) {
129 bool IsProtectionSchemeSupported(FourCC scheme) {
130 return scheme == FOURCC_cenc || scheme == FOURCC_cens ||
131 scheme == FOURCC_cbc1 || scheme == FOURCC_cbcs;
136 FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
137 FileType::~FileType() {}
140 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
142 buffer->ReadWriteFourCC(&major_brand) &&
143 buffer->ReadWriteUInt32(&minor_version));
146 RCHECK(buffer->
BytesLeft() %
sizeof(FourCC) == 0);
147 num_brands = buffer->
BytesLeft() /
sizeof(FourCC);
148 compatible_brands.resize(num_brands);
150 num_brands = compatible_brands.size();
152 for (
size_t i = 0; i < num_brands; ++i)
153 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
157 size_t FileType::ComputeSizeInternal() {
158 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
159 kFourCCSize * compatible_brands.size();
164 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
165 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
168 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
172 raw_box.assign(reader->data(), reader->data() + reader->size());
174 DCHECK(!raw_box.empty());
175 buffer->
writer()->AppendVector(raw_box);
181 size_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
182 return raw_box.size();
185 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
186 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
189 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
194 uint32_t count =
static_cast<uint32_t
>(offsets.size());
195 RCHECK(buffer->ReadWriteUInt32(&count));
196 offsets.resize(count);
198 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
199 for (uint32_t i = 0; i < count; ++i)
204 size_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
206 if (offsets.size() == 0)
208 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
209 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
212 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
213 : default_sample_info_size(0), sample_count(0) {}
214 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
217 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
222 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
223 buffer->ReadWriteUInt32(&sample_count));
224 if (default_sample_info_size == 0)
225 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
229 size_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
231 if (sample_count == 0)
233 return HeaderSize() +
sizeof(default_sample_info_size) +
234 sizeof(sample_count) +
235 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
238 SampleEncryptionEntry::SampleEncryptionEntry() {}
239 SampleEncryptionEntry::~SampleEncryptionEntry() {}
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() : iv_size(kInvalidIvSize) {}
308 SampleEncryption::~SampleEncryption() {}
311 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
316 if (buffer->
Reading() && iv_size == kInvalidIvSize) {
322 if (!IsIvSizeValid(iv_size)) {
324 <<
"IV_size can only be 8 or 16 or 0 for constant iv, but seeing "
329 uint32_t sample_count =
330 static_cast<uint32_t
>(sample_encryption_entries.size());
331 RCHECK(buffer->ReadWriteUInt32(&sample_count));
333 sample_encryption_entries.resize(sample_count);
334 for (
auto& sample_encryption_entry : sample_encryption_entries) {
335 RCHECK(sample_encryption_entry.ReadWrite(
336 iv_size, (flags & kUseSubsampleEncryption) != 0, buffer) != 0);
341 size_t SampleEncryption::ComputeSizeInternal() {
342 const uint32_t sample_count =
343 static_cast<uint32_t
>(sample_encryption_entries.size());
344 if (sample_count == 0) {
349 DCHECK(IsIvSizeValid(iv_size));
351 if (flags & kUseSubsampleEncryption) {
352 for (
const SampleEncryptionEntry& sample_encryption_entry :
353 sample_encryption_entries) {
354 box_size += sample_encryption_entry.ComputeSize();
357 box_size += sample_count * iv_size;
364 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
365 DCHECK(IsIvSizeValid(iv_size));
369 uint32_t sample_count = 0;
370 RCHECK(reader.Read4(&sample_count));
372 sample_encryption_entries->resize(sample_count);
373 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
374 RCHECK(sample_encryption_entry.ParseFromBuffer(
375 iv_size, (flags & kUseSubsampleEncryption) != 0, &reader) != 0);
380 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
381 OriginalFormat::~OriginalFormat() {}
384 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
388 size_t OriginalFormat::ComputeSizeInternal() {
392 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
393 SchemeType::~SchemeType() {}
396 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
398 buffer->ReadWriteFourCC(&type) &&
399 buffer->ReadWriteUInt32(&version));
403 size_t SchemeType::ComputeSizeInternal() {
404 return HeaderSize() + kFourCCSize +
sizeof(version);
407 TrackEncryption::TrackEncryption()
408 : default_is_protected(0),
409 default_per_sample_iv_size(0),
411 default_crypt_byte_block(0),
412 default_skip_byte_block(0) {}
413 TrackEncryption::~TrackEncryption() {}
416 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
418 if (default_kid.size() != kCencKeyIdSize) {
419 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
420 <<
" bytes; got " << default_kid.size()
421 <<
". Resized accordingly.";
422 default_kid.resize(kCencKeyIdSize);
424 RCHECK(default_crypt_byte_block < 16 && default_skip_byte_block < 16);
425 if (default_crypt_byte_block != 0 && default_skip_byte_block != 0) {
434 uint8_t pattern = default_crypt_byte_block << 4 | default_skip_byte_block;
435 RCHECK(buffer->ReadWriteUInt8(&pattern));
436 default_crypt_byte_block = pattern >> 4;
437 default_skip_byte_block = pattern & 0x0F;
439 RCHECK(buffer->ReadWriteUInt8(&default_is_protected) &&
440 buffer->ReadWriteUInt8(&default_per_sample_iv_size) &&
441 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
443 if (default_is_protected == 1) {
444 if (default_per_sample_iv_size == 0) {
445 uint8_t default_constant_iv_size =
446 static_cast<uint8_t
>(default_constant_iv.size());
447 RCHECK(buffer->ReadWriteUInt8(&default_constant_iv_size));
448 RCHECK(default_constant_iv_size == 8 || default_constant_iv_size == 16);
449 RCHECK(buffer->ReadWriteVector(&default_constant_iv,
450 default_constant_iv_size));
452 RCHECK(default_per_sample_iv_size == 8 ||
453 default_per_sample_iv_size == 16);
454 RCHECK(default_constant_iv.empty());
459 RCHECK(default_is_protected == 0);
460 RCHECK(default_per_sample_iv_size == 0);
461 RCHECK(default_constant_iv.empty());
466 size_t TrackEncryption::ComputeSizeInternal() {
467 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize +
468 (default_constant_iv.empty() ? 0 : (
sizeof(uint8_t) +
469 default_constant_iv.size()));
472 SchemeInfo::SchemeInfo() {}
473 SchemeInfo::~SchemeInfo() {}
476 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
482 size_t SchemeInfo::ComputeSizeInternal() {
486 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
487 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
490 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
495 if (IsProtectionSchemeSupported(type.type)) {
498 DLOG(WARNING) <<
"Ignore unsupported protection scheme: "
499 << FourCCToString(type.type);
508 size_t ProtectionSchemeInfo::ComputeSizeInternal() {
510 if (format.format == FOURCC_NULL)
516 MovieHeader::MovieHeader()
518 modification_time(0),
524 MovieHeader::~MovieHeader() {}
527 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
530 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
533 buffer->ReadWriteUInt32(×cale) &&
536 std::vector<uint8_t> matrix(kUnityMatrix,
537 kUnityMatrix + arraysize(kUnityMatrix));
538 RCHECK(buffer->ReadWriteInt32(&rate) &&
539 buffer->ReadWriteInt16(&volume) &&
541 buffer->ReadWriteVector(&matrix, matrix.size()) &&
543 buffer->ReadWriteUInt32(&next_track_id));
547 size_t MovieHeader::ComputeSizeInternal() {
548 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
549 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
550 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
551 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
555 TrackHeader::TrackHeader()
557 modification_time(0),
565 flags = kTrackEnabled | kTrackInMovie | kTrackInPreview;
567 TrackHeader::~TrackHeader() {}
570 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
573 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
576 buffer->ReadWriteUInt32(&track_id) &&
583 volume = (width != 0 && height != 0) ? 0 : 0x100;
585 std::vector<uint8_t> matrix(kUnityMatrix,
586 kUnityMatrix + arraysize(kUnityMatrix));
588 buffer->ReadWriteInt16(&layer) &&
589 buffer->ReadWriteInt16(&alternate_group) &&
590 buffer->ReadWriteInt16(&volume) &&
592 buffer->ReadWriteVector(&matrix, matrix.size()) &&
593 buffer->ReadWriteUInt32(&width) &&
594 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() : type(kInvalid) {}
607 SampleDescription::~SampleDescription() {}
610 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
614 count =
static_cast<uint32_t
>(video_entries.size());
617 count =
static_cast<uint32_t
>(audio_entries.size());
620 count =
static_cast<uint32_t
>(text_entries.size());
623 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
624 <<
" is not handled. Skipping.";
627 buffer->ReadWriteUInt32(&count));
630 BoxReader* reader = buffer->
reader();
632 video_entries.clear();
633 audio_entries.clear();
636 if (type == kVideo) {
637 RCHECK(reader->ReadAllChildren(&video_entries));
638 RCHECK(video_entries.size() == count);
639 }
else if (type == kAudio) {
640 RCHECK(reader->ReadAllChildren(&audio_entries));
641 RCHECK(audio_entries.size() == count);
642 }
else if (type == kText) {
643 RCHECK(reader->ReadAllChildren(&text_entries));
644 RCHECK(text_entries.size() == count);
647 DCHECK_LT(0u, count);
648 if (type == kVideo) {
649 for (uint32_t i = 0; i < count; ++i)
651 }
else if (type == kAudio) {
652 for (uint32_t i = 0; i < count; ++i)
654 }
else if (type == kText) {
655 for (uint32_t i = 0; i < count; ++i)
664 size_t SampleDescription::ComputeSizeInternal() {
665 size_t box_size =
HeaderSize() +
sizeof(uint32_t);
666 if (type == kVideo) {
667 for (uint32_t i = 0; i < video_entries.size(); ++i)
669 }
else if (type == kAudio) {
670 for (uint32_t i = 0; i < audio_entries.size(); ++i)
672 }
else if (type == kText) {
673 for (uint32_t i = 0; i < text_entries.size(); ++i)
679 DecodingTimeToSample::DecodingTimeToSample() {}
680 DecodingTimeToSample::~DecodingTimeToSample() {}
683 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
684 uint32_t count =
static_cast<uint32_t
>(decoding_time.size());
686 buffer->ReadWriteUInt32(&count));
688 decoding_time.resize(count);
689 for (uint32_t i = 0; i < count; ++i) {
690 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
691 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
696 size_t DecodingTimeToSample::ComputeSizeInternal() {
698 sizeof(DecodingTime) * decoding_time.size();
701 CompositionTimeToSample::CompositionTimeToSample() {}
702 CompositionTimeToSample::~CompositionTimeToSample() {}
705 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
706 uint32_t count =
static_cast<uint32_t
>(composition_offset.size());
712 for (uint32_t i = 0; i < count; ++i) {
713 if (composition_offset[i].sample_offset < 0) {
721 buffer->ReadWriteUInt32(&count));
723 composition_offset.resize(count);
724 for (uint32_t i = 0; i < count; ++i) {
725 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
728 uint32_t sample_offset = composition_offset[i].sample_offset;
729 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
730 composition_offset[i].sample_offset = sample_offset;
732 int32_t sample_offset = composition_offset[i].sample_offset;
733 RCHECK(buffer->ReadWriteInt32(&sample_offset));
734 composition_offset[i].sample_offset = sample_offset;
740 size_t CompositionTimeToSample::ComputeSizeInternal() {
742 if (composition_offset.empty())
747 const size_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
749 kCompositionOffsetSize * composition_offset.size();
752 SampleToChunk::SampleToChunk() {}
753 SampleToChunk::~SampleToChunk() {}
756 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
757 uint32_t count =
static_cast<uint32_t
>(chunk_info.size());
759 buffer->ReadWriteUInt32(&count));
761 chunk_info.resize(count);
762 for (uint32_t i = 0; i < count; ++i) {
763 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
764 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
765 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
767 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
768 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
773 size_t SampleToChunk::ComputeSizeInternal() {
775 sizeof(ChunkInfo) * chunk_info.size();
778 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
779 SampleSize::~SampleSize() {}
782 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
784 buffer->ReadWriteUInt32(&sample_size) &&
785 buffer->ReadWriteUInt32(&sample_count));
787 if (sample_size == 0) {
789 sizes.resize(sample_count);
791 DCHECK(sample_count == sizes.size());
792 for (uint32_t i = 0; i < sample_count; ++i)
793 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
798 size_t SampleSize::ComputeSizeInternal() {
799 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
800 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
803 CompactSampleSize::CompactSampleSize() : field_size(0) {}
804 CompactSampleSize::~CompactSampleSize() {}
807 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
808 uint32_t sample_count =
static_cast<uint32_t
>(sizes.size());
811 buffer->ReadWriteUInt8(&field_size) &&
812 buffer->ReadWriteUInt32(&sample_count));
815 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
816 switch (field_size) {
818 for (uint32_t i = 0; i < sample_count; i += 2) {
821 RCHECK(buffer->ReadWriteUInt8(&size));
822 sizes[i] = size >> 4;
823 sizes[i + 1] = size & 0x0F;
825 DCHECK_LT(sizes[i], 16u);
826 DCHECK_LT(sizes[i + 1], 16u);
827 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
828 RCHECK(buffer->ReadWriteUInt8(&size));
833 for (uint32_t i = 0; i < sample_count; ++i) {
834 uint8_t size = sizes[i];
835 RCHECK(buffer->ReadWriteUInt8(&size));
840 for (uint32_t i = 0; i < sample_count; ++i) {
841 uint16_t size = sizes[i];
842 RCHECK(buffer->ReadWriteUInt16(&size));
849 sizes.resize(sample_count);
853 size_t CompactSampleSize::ComputeSizeInternal() {
854 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
855 (field_size * sizes.size() + 7) / 8;
858 ChunkOffset::ChunkOffset() {}
859 ChunkOffset::~ChunkOffset() {}
862 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
863 uint32_t count =
static_cast<uint32_t
>(offsets.size());
865 buffer->ReadWriteUInt32(&count));
867 offsets.resize(count);
868 for (uint32_t i = 0; i < count; ++i)
873 size_t ChunkOffset::ComputeSizeInternal() {
874 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
877 ChunkLargeOffset::ChunkLargeOffset() {}
878 ChunkLargeOffset::~ChunkLargeOffset() {}
881 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
882 uint32_t count =
static_cast<uint32_t
>(offsets.size());
886 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
888 stco.offsets.swap(offsets);
891 stco.offsets.swap(offsets);
897 buffer->ReadWriteUInt32(&count));
899 offsets.resize(count);
900 for (uint32_t i = 0; i < count; ++i)
901 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
905 size_t ChunkLargeOffset::ComputeSizeInternal() {
906 uint32_t count =
static_cast<uint32_t
>(offsets.size());
907 int use_large_offset =
908 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
910 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
913 SyncSample::SyncSample() {}
914 SyncSample::~SyncSample() {}
917 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
918 uint32_t count =
static_cast<uint32_t
>(sample_number.size());
920 buffer->ReadWriteUInt32(&count));
922 sample_number.resize(count);
923 for (uint32_t i = 0; i < count; ++i)
924 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
928 size_t SyncSample::ComputeSizeInternal() {
930 if (sample_number.empty())
933 sizeof(uint32_t) * sample_number.size();
936 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
938 per_sample_iv_size(0),
940 skip_byte_block(0) {}
941 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
943 bool CencSampleEncryptionInfoEntry::ReadWrite(BoxBuffer* buffer) {
944 if (!buffer->Reading()) {
945 if (key_id.size() != kCencKeyIdSize) {
946 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
947 <<
" bytes; got " << key_id.size()
948 <<
". Resized accordingly.";
949 key_id.resize(kCencKeyIdSize);
951 RCHECK(crypt_byte_block < 16 && skip_byte_block < 16);
954 RCHECK(buffer->IgnoreBytes(1));
956 uint8_t pattern = crypt_byte_block << 4 | skip_byte_block;
957 RCHECK(buffer->ReadWriteUInt8(&pattern));
958 crypt_byte_block = pattern >> 4;
959 skip_byte_block = pattern & 0x0F;
961 RCHECK(buffer->ReadWriteUInt8(&is_protected) &&
962 buffer->ReadWriteUInt8(&per_sample_iv_size) &&
963 buffer->ReadWriteVector(&key_id, kCencKeyIdSize));
965 if (is_protected == 1) {
966 if (per_sample_iv_size == 0) {
967 uint8_t constant_iv_size =
static_cast<uint8_t
>(constant_iv.size());
968 RCHECK(buffer->ReadWriteUInt8(&constant_iv_size));
969 RCHECK(constant_iv_size == 8 || constant_iv_size == 16);
970 RCHECK(buffer->ReadWriteVector(&constant_iv, constant_iv_size));
972 RCHECK(per_sample_iv_size == 8 || per_sample_iv_size == 16);
973 DCHECK(constant_iv.empty());
978 RCHECK(is_protected == 0);
979 RCHECK(per_sample_iv_size == 0);
984 uint32_t CencSampleEncryptionInfoEntry::ComputeSize()
const {
985 return static_cast<uint32_t
>(
986 sizeof(uint32_t) + kCencKeyIdSize +
987 (constant_iv.empty() ? 0 : (
sizeof(uint8_t) + constant_iv.size())));
990 AudioRollRecoveryEntry::AudioRollRecoveryEntry(): roll_distance(0) {}
991 AudioRollRecoveryEntry::~AudioRollRecoveryEntry() {}
993 bool AudioRollRecoveryEntry::ReadWrite(BoxBuffer* buffer) {
994 RCHECK(buffer->ReadWriteInt16(&roll_distance));
998 uint32_t AudioRollRecoveryEntry::ComputeSize()
const {
999 return sizeof(roll_distance);
1002 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
1003 SampleGroupDescription::~SampleGroupDescription() {}
1006 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
1008 buffer->ReadWriteUInt32(&grouping_type));
1010 switch (grouping_type) {
1012 return ReadWriteEntries(buffer, &cenc_sample_encryption_info_entries);
1014 return ReadWriteEntries(buffer, &audio_roll_recovery_entries);
1017 DLOG(WARNING) <<
"Ignore unsupported sample group: "
1018 << FourCCToString(static_cast<FourCC>(grouping_type));
1023 template <
typename T>
1024 bool SampleGroupDescription::ReadWriteEntries(BoxBuffer* buffer,
1025 std::vector<T>* entries) {
1026 uint32_t default_length = 0;
1027 if (!buffer->Reading()) {
1028 DCHECK(!entries->empty());
1029 default_length = (*entries)[0].ComputeSize();
1030 DCHECK_NE(default_length, 0u);
1033 RCHECK(buffer->ReadWriteUInt32(&default_length));
1035 NOTIMPLEMENTED() <<
"Unsupported SampleGroupDescriptionBox 'sgpd' version "
1036 <<
static_cast<int>(version);
1040 uint32_t count =
static_cast<uint32_t
>(entries->size());
1041 RCHECK(buffer->ReadWriteUInt32(&count));
1043 entries->resize(count);
1045 for (T& entry : *entries) {
1047 uint32_t description_length = default_length;
1048 if (buffer->Reading() && default_length == 0)
1049 RCHECK(buffer->ReadWriteUInt32(&description_length));
1050 RCHECK(entry.ReadWrite(buffer));
1051 RCHECK(entry.ComputeSize() == description_length);
1053 RCHECK(entry.ReadWrite(buffer));
1059 size_t SampleGroupDescription::ComputeSizeInternal() {
1062 size_t entries_size = 0;
1063 switch (grouping_type) {
1065 for (
const auto& entry : cenc_sample_encryption_info_entries)
1066 entries_size += entry.ComputeSize();
1069 for (
const auto& entry : audio_roll_recovery_entries)
1070 entries_size += entry.ComputeSize();
1074 if (entries_size == 0)
1076 return HeaderSize() +
sizeof(grouping_type) +
1077 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
1081 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
1082 SampleToGroup::~SampleToGroup() {}
1085 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
1087 buffer->ReadWriteUInt32(&grouping_type));
1089 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
1091 if (grouping_type != FOURCC_seig && grouping_type != FOURCC_roll) {
1093 DLOG(WARNING) <<
"Ignore unsupported sample group: "
1094 << FourCCToString(static_cast<FourCC>(grouping_type));
1098 uint32_t count =
static_cast<uint32_t
>(entries.size());
1099 RCHECK(buffer->ReadWriteUInt32(&count));
1100 entries.resize(count);
1101 for (uint32_t i = 0; i < count; ++i) {
1102 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
1103 buffer->ReadWriteUInt32(&entries[i].group_description_index));
1108 size_t SampleToGroup::ComputeSizeInternal() {
1110 if (entries.empty())
1112 return HeaderSize() +
sizeof(grouping_type) +
1113 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
1114 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
1117 SampleTable::SampleTable() {}
1118 SampleTable::~SampleTable() {}
1121 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
1135 RCHECK(reader->
ReadChild(&sample_size));
1137 CompactSampleSize compact_sample_size;
1138 RCHECK(reader->
ReadChild(&compact_sample_size));
1139 sample_size.sample_size = 0;
1140 sample_size.sample_count =
1141 static_cast<uint32_t
>(compact_sample_size.sizes.size());
1142 sample_size.sizes.swap(compact_sample_size.sizes);
1146 if (reader->
ChildExist(&chunk_large_offset)) {
1147 RCHECK(reader->
ReadChild(&chunk_large_offset));
1149 ChunkOffset chunk_offset;
1150 RCHECK(reader->
ReadChild(&chunk_offset));
1151 chunk_large_offset.offsets.swap(chunk_offset.offsets);
1162 for (
auto& sample_group_description : sample_group_descriptions)
1164 for (
auto& sample_to_group : sample_to_groups)
1170 size_t SampleTable::ComputeSizeInternal() {
1177 for (
auto& sample_group_description : sample_group_descriptions)
1178 box_size += sample_group_description.ComputeSize();
1179 for (
auto& sample_to_group : sample_to_groups)
1180 box_size += sample_to_group.ComputeSize();
1184 EditList::EditList() {}
1185 EditList::~EditList() {}
1188 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
1189 uint32_t count =
static_cast<uint32_t
>(edits.size());
1191 edits.resize(count);
1193 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1194 for (uint32_t i = 0; i < count; ++i) {
1197 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
1198 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
1199 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
1204 size_t EditList::ComputeSizeInternal() {
1210 for (uint32_t i = 0; i < edits.size(); ++i) {
1211 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
1217 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
1225 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
1231 size_t Edit::ComputeSizeInternal() {
1233 if (list.edits.empty())
1238 HandlerReference::HandlerReference() : handler_type(FOURCC_NULL) {}
1239 HandlerReference::~HandlerReference() {}
1242 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1243 std::vector<uint8_t> handler_name;
1245 switch (handler_type) {
1247 handler_name.assign(kVideoHandlerName,
1248 kVideoHandlerName + arraysize(kVideoHandlerName));
1251 handler_name.assign(kAudioHandlerName,
1252 kAudioHandlerName + arraysize(kAudioHandlerName));
1255 handler_name.assign(kTextHandlerName,
1256 kTextHandlerName + arraysize(kTextHandlerName));
1267 buffer->ReadWriteFourCC(&handler_type));
1270 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1275 size_t HandlerReference::ComputeSizeInternal() {
1276 size_t box_size =
HeaderSize() + kFourCCSize + 16;
1277 switch (handler_type) {
1279 box_size +=
sizeof(kVideoHandlerName);
1282 box_size +=
sizeof(kAudioHandlerName);
1285 box_size +=
sizeof(kTextHandlerName);
1295 bool Language::ReadWrite(BoxBuffer* buffer) {
1296 if (buffer->Reading()) {
1299 std::vector<uint8_t> temp;
1300 RCHECK(buffer->ReadWriteVector(&temp, 2));
1302 BitReader bit_reader(&temp[0], 2);
1303 bit_reader.SkipBits(1);
1305 for (
int i = 0; i < 3; ++i) {
1306 CHECK(bit_reader.ReadBits(5, &language[i]));
1307 language[i] += 0x60;
1309 code.assign(language, 3);
1312 const char kUndefinedLanguage[] =
"und";
1314 code = kUndefinedLanguage;
1315 DCHECK_EQ(code.size(), 3u);
1319 for (
int i = 0; i < 3; ++i)
1320 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1321 RCHECK(buffer->ReadWriteUInt16(&lang));
1326 uint32_t Language::ComputeSize()
const {
1331 bool PrivFrame::ReadWrite(BoxBuffer* buffer) {
1332 FourCC fourcc = FOURCC_PRIV;
1333 RCHECK(buffer->ReadWriteFourCC(&fourcc));
1334 if (fourcc != FOURCC_PRIV) {
1335 VLOG(1) <<
"Skip unrecognized id3 frame during read: "
1336 << FourCCToString(fourcc);
1340 uint32_t frame_size =
static_cast<uint32_t
>(owner.size() + 1 + value.size());
1344 DCHECK_LT(frame_size, 0x7Fu);
1346 RCHECK(buffer->ReadWriteUInt32(&frame_size) &&
1347 buffer->ReadWriteUInt16(&flags));
1349 if (buffer->Reading()) {
1351 RCHECK(buffer->ReadWriteString(&str, frame_size));
1353 size_t pos = str.find(
'\0');
1354 RCHECK(pos < str.size());
1355 owner = str.substr(0, pos);
1356 value = str.substr(pos + 1);
1359 RCHECK(buffer->ReadWriteString(&owner, owner.size()) &&
1360 buffer->ReadWriteUInt8(&byte) &&
1361 buffer->ReadWriteString(&value, value.size()));
1366 uint32_t PrivFrame::ComputeSize()
const {
1367 if (owner.empty() && value.empty())
1369 const uint32_t kFourCCSize = 4;
1370 return kFourCCSize +
1371 static_cast<uint32_t
>(
sizeof(uint32_t) +
sizeof(uint16_t) +
1372 owner.size() + 1 + value.size());
1380 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1382 language.ReadWrite(buffer));
1385 std::string id3v2_identifier = kID3v2Identifier;
1386 uint16_t version = kID3v2Version;
1392 DCHECK_LT(data_size, 0x7Fu);
1394 RCHECK(buffer->
ReadWriteString(&id3v2_identifier, id3v2_identifier.size()) &&
1395 buffer->ReadWriteUInt16(&version) &&
1396 buffer->ReadWriteUInt8(&flags) &&
1397 buffer->ReadWriteUInt32(&data_size));
1403 size_t ID3v2::ComputeSizeInternal() {
1406 return private_frame_size == 0 ? 0 :
HeaderSize() + language.ComputeSize() +
1411 Metadata::Metadata() {}
1412 Metadata::~Metadata() {}
1418 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1426 size_t Metadata::ComputeSizeInternal() {
1429 return id3v2_size == 0 ? 0
1433 CodecConfiguration::CodecConfiguration() : box_type(FOURCC_NULL) {}
1434 CodecConfiguration::~CodecConfiguration() {}
1442 bool CodecConfiguration::ReadWriteInternal(
BoxBuffer* buffer) {
1443 DCHECK_NE(box_type, FOURCC_NULL);
1448 if (box_type == FOURCC_vpcC) {
1450 uint8_t vpcc_version = 1;
1451 uint32_t version_flags = vpcc_version << 24;
1452 RCHECK(buffer->ReadWriteUInt32(&version_flags));
1453 vpcc_version = version_flags >> 24;
1454 RCHECK(vpcc_version == 1);
1458 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1460 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1465 size_t CodecConfiguration::ComputeSizeInternal() {
1468 DCHECK_NE(box_type, FOURCC_NULL);
1469 return HeaderSize() + (box_type == FOURCC_vpcC ? 4 : 0) + data.size();
1472 PixelAspectRatio::PixelAspectRatio() : h_spacing(0), v_spacing(0) {}
1473 PixelAspectRatio::~PixelAspectRatio() {}
1476 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1478 buffer->ReadWriteUInt32(&h_spacing) &&
1479 buffer->ReadWriteUInt32(&v_spacing));
1483 size_t PixelAspectRatio::ComputeSizeInternal() {
1485 if (h_spacing == 0 && v_spacing == 0)
1488 DCHECK(h_spacing != 0 && v_spacing != 0);
1489 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1492 VideoSampleEntry::VideoSampleEntry()
1493 : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
1495 VideoSampleEntry::~VideoSampleEntry() {}
1497 if (format == FOURCC_NULL) {
1498 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
1499 <<
"handler type recovered in its Media ancestor.";
1504 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1505 std::vector<uint8_t> compressor_name;
1507 DCHECK(buffer->
reader());
1508 format = buffer->
reader()->type();
1512 const FourCC actual_format = GetActualFormat();
1513 switch (actual_format) {
1516 compressor_name.assign(
1518 kAvcCompressorName + arraysize(kAvcCompressorName));
1522 compressor_name.assign(
1523 kHevcCompressorName,
1524 kHevcCompressorName + arraysize(kHevcCompressorName));
1529 compressor_name.assign(
1531 kVpcCompressorName + arraysize(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) &&
1548 buffer->ReadWriteUInt16(&height) &&
1549 buffer->ReadWriteUInt32(&video_resolution) &&
1550 buffer->ReadWriteUInt32(&video_resolution) &&
1552 buffer->ReadWriteUInt16(&video_frame_count) &&
1553 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1554 buffer->ReadWriteUInt16(&video_depth) &&
1555 buffer->ReadWriteInt16(&predefined));
1559 if (format == FOURCC_encv) {
1563 while (!IsProtectionSchemeSupported(sinf.type.type))
1566 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1571 const FourCC actual_format = GetActualFormat();
1573 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1575 DCHECK_EQ(codec_configuration.box_type,
1576 GetCodecConfigurationBoxType(actual_format));
1578 DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1585 size_t VideoSampleEntry::ComputeSizeInternal() {
1586 const FourCC actual_format = GetActualFormat();
1587 if (actual_format == FOURCC_NULL)
1589 codec_configuration.box_type = GetCodecConfigurationBoxType(actual_format);
1590 DCHECK_NE(codec_configuration.box_type, FOURCC_NULL);
1591 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1592 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1593 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1595 codec_configuration.
ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1599 FourCC VideoSampleEntry::GetCodecConfigurationBoxType(FourCC format)
const {
1612 LOG(ERROR) << FourCCToString(format) <<
" is not supported.";
1617 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1618 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1621 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1624 std::vector<uint8_t> data;
1625 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1626 RCHECK(es_descriptor.Parse(data));
1627 if (es_descriptor.
IsAAC()) {
1628 RCHECK(aac_audio_specific_config.
Parse(
1629 es_descriptor.decoder_specific_info()));
1632 DCHECK(buffer->
writer());
1633 es_descriptor.Write(buffer->
writer());
1638 size_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1640 if (es_descriptor.object_type() == kForbidden)
1642 return HeaderSize() + es_descriptor.ComputeSize();
1645 DTSSpecific::DTSSpecific()
1646 : sampling_frequency(0),
1649 pcm_sample_depth(0) {}
1650 DTSSpecific::~DTSSpecific() {}
1653 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1655 buffer->ReadWriteUInt32(&sampling_frequency) &&
1656 buffer->ReadWriteUInt32(&max_bitrate) &&
1657 buffer->ReadWriteUInt32(&avg_bitrate) &&
1658 buffer->ReadWriteUInt8(&pcm_sample_depth));
1661 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
BytesLeft()));
1663 if (extra_data.empty()) {
1664 extra_data.assign(kDdtsExtraData,
1665 kDdtsExtraData +
sizeof(kDdtsExtraData));
1667 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1672 size_t DTSSpecific::ComputeSizeInternal() {
1674 if (sampling_frequency == 0)
1676 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1677 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1678 sizeof(kDdtsExtraData);
1681 AC3Specific::AC3Specific() {}
1682 AC3Specific::~AC3Specific() {}
1686 bool AC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1688 buffer->ReadWriteVector(
1693 size_t AC3Specific::ComputeSizeInternal() {
1700 EC3Specific::EC3Specific() {}
1701 EC3Specific::~EC3Specific() {}
1705 bool EC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1708 RCHECK(buffer->ReadWriteVector(&data, size));
1712 size_t EC3Specific::ComputeSizeInternal() {
1719 OpusSpecific::OpusSpecific() : preskip(0) {}
1720 OpusSpecific::~OpusSpecific() {}
1724 bool OpusSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1727 std::vector<uint8_t> data;
1728 const int kMinOpusSpecificBoxDataSize = 11;
1729 RCHECK(buffer->
BytesLeft() >= kMinOpusSpecificBoxDataSize);
1730 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1731 preskip = data[2] + (data[3] << 8);
1736 writer.AppendInt(FOURCC_Head);
1738 const uint8_t kOpusIdentificationHeaderVersion = 1;
1739 data[0] = kOpusIdentificationHeaderVersion;
1740 writer.AppendVector(data);
1741 writer.SwapBuffer(&opus_identification_header);
1745 const size_t kOpusMagicSignatureSize = 8u;
1746 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1749 const uint8_t kOpusSpecificBoxVersion = 0;
1751 buffer->
writer()->AppendArray(
1752 &opus_identification_header[kOpusMagicSignatureSize + 1],
1753 opus_identification_header.size() - kOpusMagicSignatureSize - 1);
1758 size_t OpusSpecific::ComputeSizeInternal() {
1760 if (opus_identification_header.empty())
1764 const size_t kOpusMagicSignatureSize = 8u;
1765 DCHECK_GT(opus_identification_header.size(), kOpusMagicSignatureSize);
1766 return HeaderSize() + opus_identification_header.size() -
1767 kOpusMagicSignatureSize;
1770 AudioSampleEntry::AudioSampleEntry()
1771 : format(FOURCC_NULL),
1772 data_reference_index(1),
1777 AudioSampleEntry::~AudioSampleEntry() {}
1780 if (format == FOURCC_NULL) {
1781 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1782 <<
"handler type recovered in its Media ancestor.";
1787 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1789 DCHECK(buffer->
reader());
1790 format = buffer->
reader()->type();
1798 buffer->ReadWriteUInt16(&data_reference_index) &&
1800 buffer->ReadWriteUInt16(&channelcount) &&
1801 buffer->ReadWriteUInt16(&samplesize) &&
1803 buffer->ReadWriteUInt32(&samplerate));
1808 if (format == FOURCC_enca) {
1812 while (!IsProtectionSchemeSupported(sinf.type.type))
1815 DCHECK(IsProtectionSchemeSupported(sinf.type.type));
1828 size_t AudioSampleEntry::ComputeSizeInternal() {
1829 if (GetActualFormat() == FOURCC_NULL)
1831 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1832 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1839 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1840 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1846 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1853 size_t WebVTTConfigurationBox::ComputeSizeInternal() {
1857 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1858 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1864 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1868 : source_label.size());
1871 size_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1872 if (source_label.empty())
1877 TextSampleEntry::TextSampleEntry() : format(FOURCC_NULL) {}
1878 TextSampleEntry::~TextSampleEntry() {}
1881 if (format == FOURCC_NULL) {
1882 LOG(ERROR) <<
"TextSampleEntry should be parsed according to the "
1883 <<
"handler type recovered in its Media ancestor.";
1888 bool TextSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1890 DCHECK(buffer->
reader());
1891 format = buffer->
reader()->type();
1896 buffer->ReadWriteUInt16(&data_reference_index));
1898 if (format == FOURCC_wvtt) {
1907 size_t TextSampleEntry::ComputeSizeInternal() {
1909 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1913 MediaHeader::MediaHeader()
1914 : creation_time(0), modification_time(0), timescale(0), duration(0) {}
1915 MediaHeader::~MediaHeader() {}
1918 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1921 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1924 buffer->ReadWriteUInt32(×cale) &&
1926 language.ReadWrite(buffer) &&
1931 size_t MediaHeader::ComputeSizeInternal() {
1932 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1934 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1938 VideoMediaHeader::VideoMediaHeader()
1939 : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1940 const uint32_t kVideoMediaHeaderFlags = 1;
1941 flags = kVideoMediaHeaderFlags;
1943 VideoMediaHeader::~VideoMediaHeader() {}
1945 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1947 buffer->ReadWriteUInt16(&graphicsmode) &&
1948 buffer->ReadWriteUInt16(&opcolor_red) &&
1949 buffer->ReadWriteUInt16(&opcolor_green) &&
1950 buffer->ReadWriteUInt16(&opcolor_blue));
1954 size_t VideoMediaHeader::ComputeSizeInternal() {
1955 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1956 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1959 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1960 SoundMediaHeader::~SoundMediaHeader() {}
1962 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1964 buffer->ReadWriteUInt16(&balance) &&
1969 size_t SoundMediaHeader::ComputeSizeInternal() {
1970 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1973 SubtitleMediaHeader::SubtitleMediaHeader() {}
1974 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1978 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1982 size_t SubtitleMediaHeader::ComputeSizeInternal() {
1986 DataEntryUrl::DataEntryUrl() {
1987 const uint32_t kDataEntryUrlFlags = 1;
1988 flags = kDataEntryUrlFlags;
1990 DataEntryUrl::~DataEntryUrl() {}
1992 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
1995 RCHECK(buffer->ReadWriteVector(&location, buffer->
BytesLeft()));
1997 RCHECK(buffer->ReadWriteVector(&location, location.size()));
2002 size_t DataEntryUrl::ComputeSizeInternal() {
2006 DataReference::DataReference() {
2008 data_entry.resize(1);
2010 DataReference::~DataReference() {}
2012 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
2013 uint32_t entry_count =
static_cast<uint32_t
>(data_entry.size());
2015 buffer->ReadWriteUInt32(&entry_count));
2016 data_entry.resize(entry_count);
2018 for (uint32_t i = 0; i < entry_count; ++i)
2023 size_t DataReference::ComputeSizeInternal() {
2024 uint32_t count =
static_cast<uint32_t
>(data_entry.size());
2025 size_t box_size =
HeaderSize() +
sizeof(count);
2026 for (uint32_t i = 0; i < count; ++i)
2031 DataInformation::DataInformation() {}
2032 DataInformation::~DataInformation() {}
2035 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2041 size_t DataInformation::ComputeSizeInternal() {
2045 MediaInformation::MediaInformation() {}
2046 MediaInformation::~MediaInformation() {}
2049 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
2054 switch (sample_table.description.type) {
2071 size_t MediaInformation::ComputeSizeInternal() {
2074 switch (sample_table.description.type) {
2094 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
2106 information.sample_table.description.type =
2107 FourCCToTrackType(handler.handler_type);
2109 handler.handler_type =
2110 TrackTypeToFourCC(information.sample_table.description.type);
2111 RCHECK(handler.handler_type != FOURCC_NULL);
2118 size_t Media::ComputeSizeInternal() {
2119 handler.handler_type =
2120 TrackTypeToFourCC(information.sample_table.description.type);
2129 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
2139 size_t Track::ComputeSizeInternal() {
2144 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
2145 MovieExtendsHeader::~MovieExtendsHeader() {}
2148 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2150 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2155 size_t MovieExtendsHeader::ComputeSizeInternal() {
2157 if (fragment_duration == 0)
2159 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
2160 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2163 TrackExtends::TrackExtends()
2165 default_sample_description_index(0),
2166 default_sample_duration(0),
2167 default_sample_size(0),
2168 default_sample_flags(0) {}
2169 TrackExtends::~TrackExtends() {}
2172 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2174 buffer->ReadWriteUInt32(&track_id) &&
2175 buffer->ReadWriteUInt32(&default_sample_description_index) &&
2176 buffer->ReadWriteUInt32(&default_sample_duration) &&
2177 buffer->ReadWriteUInt32(&default_sample_size) &&
2178 buffer->ReadWriteUInt32(&default_sample_flags));
2182 size_t TrackExtends::ComputeSizeInternal() {
2184 sizeof(default_sample_description_index) +
2185 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
2186 sizeof(default_sample_flags);
2189 MovieExtends::MovieExtends() {}
2190 MovieExtends::~MovieExtends() {}
2193 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
2198 DCHECK(buffer->
reader());
2201 for (uint32_t i = 0; i < tracks.size(); ++i)
2207 size_t MovieExtends::ComputeSizeInternal() {
2209 if (tracks.size() == 0)
2212 for (uint32_t i = 0; i < tracks.size(); ++i)
2221 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
2233 for (uint32_t i = 0; i < tracks.size(); ++i)
2236 for (uint32_t i = 0; i < pssh.size(); ++i)
2242 size_t Movie::ComputeSizeInternal() {
2245 for (uint32_t i = 0; i < tracks.size(); ++i)
2247 for (uint32_t i = 0; i < pssh.size(); ++i)
2252 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
2253 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
2256 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
2258 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2263 size_t TrackFragmentDecodeTime::ComputeSizeInternal() {
2264 version = IsFitIn32Bits(decode_time) ? 0 : 1;
2265 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
2268 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
2269 MovieFragmentHeader::~MovieFragmentHeader() {}
2272 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2274 buffer->ReadWriteUInt32(&sequence_number);
2277 size_t MovieFragmentHeader::ComputeSizeInternal() {
2278 return HeaderSize() +
sizeof(sequence_number);
2281 TrackFragmentHeader::TrackFragmentHeader()
2283 sample_description_index(0),
2284 default_sample_duration(0),
2285 default_sample_size(0),
2286 default_sample_flags(0) {}
2288 TrackFragmentHeader::~TrackFragmentHeader() {}
2291 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
2293 buffer->ReadWriteUInt32(&track_id));
2295 if (flags & kBaseDataOffsetPresentMask) {
2300 uint64_t base_data_offset;
2301 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
2302 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
2303 "default-base-is-moof.";
2306 if (flags & kSampleDescriptionIndexPresentMask) {
2307 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
2308 }
else if (buffer->
Reading()) {
2309 sample_description_index = 0;
2312 if (flags & kDefaultSampleDurationPresentMask) {
2313 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2314 }
else if (buffer->
Reading()) {
2315 default_sample_duration = 0;
2318 if (flags & kDefaultSampleSizePresentMask) {
2319 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2320 }
else if (buffer->
Reading()) {
2321 default_sample_size = 0;
2324 if (flags & kDefaultSampleFlagsPresentMask)
2325 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2329 size_t TrackFragmentHeader::ComputeSizeInternal() {
2330 size_t box_size =
HeaderSize() +
sizeof(track_id);
2331 if (flags & kSampleDescriptionIndexPresentMask)
2332 box_size +=
sizeof(sample_description_index);
2333 if (flags & kDefaultSampleDurationPresentMask)
2334 box_size +=
sizeof(default_sample_duration);
2335 if (flags & kDefaultSampleSizePresentMask)
2336 box_size +=
sizeof(default_sample_size);
2337 if (flags & kDefaultSampleFlagsPresentMask)
2338 box_size +=
sizeof(default_sample_flags);
2342 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
2343 TrackFragmentRun::~TrackFragmentRun() {}
2346 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
2352 if (flags & kSampleCompTimeOffsetsPresentMask) {
2353 for (uint32_t i = 0; i < sample_count; ++i) {
2354 if (sample_composition_time_offsets[i] < 0) {
2363 buffer->ReadWriteUInt32(&sample_count));
2365 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2366 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2367 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2368 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2369 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2370 bool sample_composition_time_offsets_present =
2371 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2373 if (data_offset_present) {
2374 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2385 uint32_t first_sample_flags(0);
2388 if (first_sample_flags_present)
2389 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2391 if (sample_duration_present)
2392 sample_durations.resize(sample_count);
2393 if (sample_size_present)
2394 sample_sizes.resize(sample_count);
2395 if (sample_flags_present)
2396 sample_flags.resize(sample_count);
2397 if (sample_composition_time_offsets_present)
2398 sample_composition_time_offsets.resize(sample_count);
2400 if (first_sample_flags_present) {
2401 first_sample_flags = sample_flags[0];
2402 DCHECK(sample_flags.size() == 1);
2403 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2406 if (sample_duration_present)
2407 DCHECK(sample_durations.size() == sample_count);
2408 if (sample_size_present)
2409 DCHECK(sample_sizes.size() == sample_count);
2410 if (sample_flags_present)
2411 DCHECK(sample_flags.size() == sample_count);
2412 if (sample_composition_time_offsets_present)
2413 DCHECK(sample_composition_time_offsets.size() == sample_count);
2416 for (uint32_t i = 0; i < sample_count; ++i) {
2417 if (sample_duration_present)
2418 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2419 if (sample_size_present)
2420 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2421 if (sample_flags_present)
2422 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2424 if (sample_composition_time_offsets_present) {
2426 uint32_t sample_offset = sample_composition_time_offsets[i];
2427 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2428 sample_composition_time_offsets[i] = sample_offset;
2430 int32_t sample_offset = sample_composition_time_offsets[i];
2431 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2432 sample_composition_time_offsets[i] = sample_offset;
2438 if (first_sample_flags_present) {
2439 if (sample_flags.size() == 0) {
2440 sample_flags.push_back(first_sample_flags);
2442 sample_flags[0] = first_sample_flags;
2449 size_t TrackFragmentRun::ComputeSizeInternal() {
2450 size_t box_size =
HeaderSize() +
sizeof(sample_count);
2451 if (flags & kDataOffsetPresentMask)
2452 box_size +=
sizeof(data_offset);
2453 if (flags & kFirstSampleFlagsPresentMask)
2454 box_size +=
sizeof(uint32_t);
2455 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2456 (flags & kSampleSizePresentMask ? 1 : 0) +
2457 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2458 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2459 box_size += fields *
sizeof(uint32_t) * sample_count;
2463 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2464 TrackFragment::~TrackFragment() {}
2467 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2472 DCHECK(buffer->
reader());
2474 if (!decode_time_absent)
2480 if (!decode_time_absent)
2482 for (uint32_t i = 0; i < runs.size(); ++i)
2484 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2486 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2494 size_t TrackFragment::ComputeSizeInternal() {
2499 for (uint32_t i = 0; i < runs.size(); ++i)
2501 for (uint32_t i = 0; i < sample_group_descriptions.size(); ++i)
2502 box_size += sample_group_descriptions[i].
ComputeSize();
2503 for (uint32_t i = 0; i < sample_to_groups.size(); ++i)
2508 MovieFragment::MovieFragment() {}
2509 MovieFragment::~MovieFragment() {}
2512 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2522 for (uint32_t i = 0; i < tracks.size(); ++i)
2524 for (uint32_t i = 0; i < pssh.size(); ++i)
2530 size_t MovieFragment::ComputeSizeInternal() {
2532 for (uint32_t i = 0; i < tracks.size(); ++i)
2534 for (uint32_t i = 0; i < pssh.size(); ++i)
2539 SegmentIndex::SegmentIndex()
2542 earliest_presentation_time(0),
2544 SegmentIndex::~SegmentIndex() {}
2547 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2549 buffer->ReadWriteUInt32(&reference_id) &&
2550 buffer->ReadWriteUInt32(×cale));
2552 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2557 uint16_t reference_count =
static_cast<uint16_t
>(references.size());
2559 buffer->ReadWriteUInt16(&reference_count));
2560 references.resize(reference_count);
2562 uint32_t reference_type_size;
2564 for (uint32_t i = 0; i < reference_count; ++i) {
2566 reference_type_size = references[i].referenced_size;
2567 if (references[i].reference_type)
2568 reference_type_size |= (1 << 31);
2569 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2570 if (references[i].starts_with_sap)
2573 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2574 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2575 buffer->ReadWriteUInt32(&sap));
2577 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2578 references[i].referenced_size = reference_type_size & ~(1 << 31);
2579 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2580 references[i].sap_type =
2581 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2582 references[i].sap_delta_time = sap & ~(0xF << 28);
2588 size_t SegmentIndex::ComputeSizeInternal() {
2589 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2590 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2591 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2592 3 *
sizeof(uint32_t) * references.size();
2595 MediaData::MediaData() : data_size(0) {}
2596 MediaData::~MediaData() {}
2599 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2600 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2604 size_t MediaData::ComputeSizeInternal() {
2608 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2609 CueSourceIDBox::~CueSourceIDBox() {}
2613 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2618 size_t CueSourceIDBox::ComputeSizeInternal() {
2619 if (source_id == kCueSourceIdNotSet)
2624 CueTimeBox::CueTimeBox() {}
2625 CueTimeBox::~CueTimeBox() {}
2631 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2638 size_t CueTimeBox::ComputeSizeInternal() {
2639 if (cue_current_time.empty())
2641 return HeaderSize() + cue_current_time.size();
2644 CueIDBox::CueIDBox() {}
2645 CueIDBox::~CueIDBox() {}
2651 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2657 size_t CueIDBox::ComputeSizeInternal() {
2663 CueSettingsBox::CueSettingsBox() {}
2664 CueSettingsBox::~CueSettingsBox() {}
2670 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2676 size_t CueSettingsBox::ComputeSizeInternal() {
2677 if (settings.empty())
2682 CuePayloadBox::CuePayloadBox() {}
2683 CuePayloadBox::~CuePayloadBox() {}
2689 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2695 size_t CuePayloadBox::ComputeSizeInternal() {
2699 VTTEmptyCueBox::VTTEmptyCueBox() {}
2700 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2706 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2710 size_t VTTEmptyCueBox::ComputeSizeInternal() {
2714 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2715 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2721 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2724 &cue_additional_text,
2728 size_t VTTAdditionalTextBox::ComputeSizeInternal() {
2729 return HeaderSize() + cue_additional_text.size();
2732 VTTCueBox::VTTCueBox() {}
2733 VTTCueBox::~VTTCueBox() {}
2739 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2750 size_t VTTCueBox::ComputeSizeInternal() {
FourCC BoxType() const override
bool ParseFromBuffer(uint8_t iv_size, bool has_subsamples, BufferReader *reader)
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
uint32_t GetTotalSizeOfSubsamples() const
bool ReadWrite(uint8_t iv_size, bool has_subsamples, BoxBuffer *buffer)
FourCC BoxType() const override
uint32_t ComputeSize() const