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(
size_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);
97 namespace edash_packager {
103 TrackType FourCCToTrackType(FourCC fourcc) {
116 FourCC TrackTypeToFourCC(TrackType track_type) {
117 switch (track_type) {
131 FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
132 FileType::~FileType() {}
135 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
137 buffer->ReadWriteFourCC(&major_brand) &&
138 buffer->ReadWriteUInt32(&minor_version));
141 RCHECK(buffer->
BytesLeft() %
sizeof(FourCC) == 0);
142 num_brands = buffer->
BytesLeft() /
sizeof(FourCC);
143 compatible_brands.resize(num_brands);
145 num_brands = compatible_brands.size();
147 for (
size_t i = 0; i < num_brands; ++i)
148 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
152 uint32_t FileType::ComputeSizeInternal() {
153 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
154 kFourCCSize * compatible_brands.size();
159 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
160 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
163 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
167 raw_box.assign(reader->data(), reader->data() + reader->size());
169 DCHECK(!raw_box.empty());
170 buffer->
writer()->AppendVector(raw_box);
176 uint32_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
177 return raw_box.size();
180 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
181 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
184 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
189 uint32_t count = offsets.size();
190 RCHECK(buffer->ReadWriteUInt32(&count));
191 offsets.resize(count);
193 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
194 for (uint32_t i = 0; i < count; ++i)
199 uint32_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
201 if (offsets.size() == 0)
203 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
204 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
207 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
208 : default_sample_info_size(0), sample_count(0) {}
209 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
212 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
217 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
218 buffer->ReadWriteUInt32(&sample_count));
219 if (default_sample_info_size == 0)
220 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
224 uint32_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
226 if (sample_count == 0)
228 return HeaderSize() +
sizeof(default_sample_info_size) +
229 sizeof(sample_count) +
230 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
233 SampleEncryptionEntry::SampleEncryptionEntry() {}
234 SampleEncryptionEntry::~SampleEncryptionEntry() {}
239 DCHECK(IsIvSizeValid(iv_size));
242 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
244 if (!has_subsamples) {
249 uint16_t subsample_count = subsamples.size();
250 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
251 RCHECK(subsample_count > 0);
252 subsamples.resize(subsample_count);
253 for (
auto& subsample : subsamples) {
254 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
255 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
263 DCHECK(IsIvSizeValid(iv_size));
266 initialization_vector.resize(iv_size);
267 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
269 if (!has_subsamples) {
274 uint16_t subsample_count;
275 RCHECK(reader->Read2(&subsample_count));
276 RCHECK(subsample_count > 0);
277 subsamples.resize(subsample_count);
278 for (
auto& subsample : subsamples) {
279 RCHECK(reader->Read2(&subsample.clear_bytes) &&
280 reader->Read4(&subsample.cipher_bytes));
286 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
287 const uint16_t subsample_count = subsamples.size();
288 return initialization_vector.size() +
289 (subsample_count > 0 ? (
sizeof(subsample_count) +
290 subsample_entry_size * subsample_count)
296 for (uint32_t i = 0; i < subsamples.size(); ++i)
297 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
301 SampleEncryption::SampleEncryption() : iv_size(kInvalidIvSize) {}
302 SampleEncryption::~SampleEncryption() {}
305 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
310 if (buffer->
Reading() && iv_size == kInvalidIvSize) {
316 if (!IsIvSizeValid(iv_size)) {
318 <<
"IV_size can only be 8 or 16 or 0 for constant iv, but seeing "
323 uint32_t sample_count = sample_encryption_entries.size();
324 RCHECK(buffer->ReadWriteUInt32(&sample_count));
326 sample_encryption_entries.resize(sample_count);
327 for (
auto& sample_encryption_entry : sample_encryption_entries) {
328 RCHECK(sample_encryption_entry.ReadWrite(
329 iv_size, flags & kUseSubsampleEncryption, buffer));
334 uint32_t SampleEncryption::ComputeSizeInternal() {
335 const uint32_t sample_count = sample_encryption_entries.size();
336 if (sample_count == 0) {
341 DCHECK(IsIvSizeValid(iv_size));
343 if (flags & kUseSubsampleEncryption) {
344 for (
const SampleEncryptionEntry& sample_encryption_entry :
345 sample_encryption_entries) {
346 box_size += sample_encryption_entry.ComputeSize();
349 box_size += sample_count * iv_size;
356 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
357 DCHECK(IsIvSizeValid(iv_size));
361 uint32_t sample_count = 0;
362 RCHECK(reader.Read4(&sample_count));
364 sample_encryption_entries->resize(sample_count);
365 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
366 RCHECK(sample_encryption_entry.ParseFromBuffer(
367 iv_size, flags & kUseSubsampleEncryption, &reader));
372 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
373 OriginalFormat::~OriginalFormat() {}
376 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
380 uint32_t OriginalFormat::ComputeSizeInternal() {
384 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
385 SchemeType::~SchemeType() {}
388 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
390 buffer->ReadWriteFourCC(&type) &&
391 buffer->ReadWriteUInt32(&version));
395 uint32_t SchemeType::ComputeSizeInternal() {
396 return HeaderSize() + kFourCCSize +
sizeof(version);
399 TrackEncryption::TrackEncryption()
400 : default_is_protected(0),
401 default_per_sample_iv_size(0),
403 default_crypt_byte_block(0),
404 default_skip_byte_block(0) {}
405 TrackEncryption::~TrackEncryption() {}
408 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
410 if (default_kid.size() != kCencKeyIdSize) {
411 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
412 <<
" bytes; got " << default_kid.size()
413 <<
". Resized accordingly.";
414 default_kid.resize(kCencKeyIdSize);
416 RCHECK(default_crypt_byte_block < 16 && default_skip_byte_block < 16);
417 if (default_crypt_byte_block != 0 && default_skip_byte_block != 0) {
426 uint8_t pattern = default_crypt_byte_block << 4 | default_skip_byte_block;
427 RCHECK(buffer->ReadWriteUInt8(&pattern));
428 default_crypt_byte_block = pattern >> 4;
429 default_skip_byte_block = pattern & 0x0F;
431 RCHECK(buffer->ReadWriteUInt8(&default_is_protected) &&
432 buffer->ReadWriteUInt8(&default_per_sample_iv_size) &&
433 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
435 if (default_is_protected == 1) {
436 if (default_per_sample_iv_size == 0) {
437 uint8_t default_constant_iv_size = default_constant_iv.size();
438 RCHECK(buffer->ReadWriteUInt8(&default_constant_iv_size));
439 RCHECK(default_constant_iv_size == 8 || default_constant_iv_size == 16);
440 RCHECK(buffer->ReadWriteVector(&default_constant_iv,
441 default_constant_iv_size));
443 RCHECK(default_per_sample_iv_size == 8 ||
444 default_per_sample_iv_size == 16);
445 RCHECK(default_constant_iv.empty());
450 RCHECK(default_is_protected == 0);
451 RCHECK(default_per_sample_iv_size == 0);
456 uint32_t TrackEncryption::ComputeSizeInternal() {
457 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize +
458 (default_constant_iv.empty() ? 0 : (
sizeof(uint8_t) +
459 default_constant_iv.size()));
462 SchemeInfo::SchemeInfo() {}
463 SchemeInfo::~SchemeInfo() {}
466 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
472 uint32_t SchemeInfo::ComputeSizeInternal() {
476 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
477 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
480 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
485 if (type.type == FOURCC_cenc || type.type == FOURCC_cbc1)
494 uint32_t ProtectionSchemeInfo::ComputeSizeInternal() {
496 if (format.format == FOURCC_NULL)
502 MovieHeader::MovieHeader()
504 modification_time(0),
510 MovieHeader::~MovieHeader() {}
513 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
516 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
519 buffer->ReadWriteUInt32(×cale) &&
522 std::vector<uint8_t> matrix(kUnityMatrix,
523 kUnityMatrix + arraysize(kUnityMatrix));
524 RCHECK(buffer->ReadWriteInt32(&rate) &&
525 buffer->ReadWriteInt16(&volume) &&
527 buffer->ReadWriteVector(&matrix, matrix.size()) &&
529 buffer->ReadWriteUInt32(&next_track_id));
533 uint32_t MovieHeader::ComputeSizeInternal() {
534 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
535 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
536 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
537 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
541 TrackHeader::TrackHeader()
543 modification_time(0),
551 flags = kTrackEnabled | kTrackInMovie;
553 TrackHeader::~TrackHeader() {}
556 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
559 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
562 buffer->ReadWriteUInt32(&track_id) &&
569 volume = (width != 0 && height != 0) ? 0 : 0x100;
571 std::vector<uint8_t> matrix(kUnityMatrix,
572 kUnityMatrix + arraysize(kUnityMatrix));
574 buffer->ReadWriteInt16(&layer) &&
575 buffer->ReadWriteInt16(&alternate_group) &&
576 buffer->ReadWriteInt16(&volume) &&
578 buffer->ReadWriteVector(&matrix, matrix.size()) &&
579 buffer->ReadWriteUInt32(&width) &&
580 buffer->ReadWriteUInt32(&height));
584 uint32_t TrackHeader::ComputeSizeInternal() {
585 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
587 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
588 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
589 sizeof(height) +
sizeof(kUnityMatrix) + 14;
592 SampleDescription::SampleDescription() : type(kInvalid) {}
593 SampleDescription::~SampleDescription() {}
596 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
600 count = video_entries.size();
603 count = audio_entries.size();
606 count = text_entries.size();
609 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
610 <<
" is not handled. Skipping.";
613 buffer->ReadWriteUInt32(&count));
616 BoxReader* reader = buffer->
reader();
618 video_entries.clear();
619 audio_entries.clear();
622 if (type == kVideo) {
623 RCHECK(reader->ReadAllChildren(&video_entries));
624 RCHECK(video_entries.size() == count);
625 }
else if (type == kAudio) {
626 RCHECK(reader->ReadAllChildren(&audio_entries));
627 RCHECK(audio_entries.size() == count);
628 }
else if (type == kText) {
629 RCHECK(reader->ReadAllChildren(&text_entries));
630 RCHECK(text_entries.size() == count);
633 DCHECK_LT(0u, count);
634 if (type == kVideo) {
635 for (uint32_t i = 0; i < count; ++i)
637 }
else if (type == kAudio) {
638 for (uint32_t i = 0; i < count; ++i)
640 }
else if (type == kText) {
641 for (uint32_t i = 0; i < count; ++i)
650 uint32_t SampleDescription::ComputeSizeInternal() {
651 uint32_t box_size =
HeaderSize() +
sizeof(uint32_t);
652 if (type == kVideo) {
653 for (uint32_t i = 0; i < video_entries.size(); ++i)
655 }
else if (type == kAudio) {
656 for (uint32_t i = 0; i < audio_entries.size(); ++i)
658 }
else if (type == kText) {
659 for (uint32_t i = 0; i < text_entries.size(); ++i)
665 DecodingTimeToSample::DecodingTimeToSample() {}
666 DecodingTimeToSample::~DecodingTimeToSample() {}
669 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
670 uint32_t count = decoding_time.size();
672 buffer->ReadWriteUInt32(&count));
674 decoding_time.resize(count);
675 for (uint32_t i = 0; i < count; ++i) {
676 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
677 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
682 uint32_t DecodingTimeToSample::ComputeSizeInternal() {
684 sizeof(DecodingTime) * decoding_time.size();
687 CompositionTimeToSample::CompositionTimeToSample() {}
688 CompositionTimeToSample::~CompositionTimeToSample() {}
691 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
692 uint32_t count = composition_offset.size();
698 for (uint32_t i = 0; i < count; ++i) {
699 if (composition_offset[i].sample_offset < 0) {
707 buffer->ReadWriteUInt32(&count));
709 composition_offset.resize(count);
710 for (uint32_t i = 0; i < count; ++i) {
711 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
714 uint32_t sample_offset = composition_offset[i].sample_offset;
715 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
716 composition_offset[i].sample_offset = sample_offset;
718 int32_t sample_offset = composition_offset[i].sample_offset;
719 RCHECK(buffer->ReadWriteInt32(&sample_offset));
720 composition_offset[i].sample_offset = sample_offset;
726 uint32_t CompositionTimeToSample::ComputeSizeInternal() {
728 if (composition_offset.empty())
733 const uint32_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
735 kCompositionOffsetSize * composition_offset.size();
738 SampleToChunk::SampleToChunk() {}
739 SampleToChunk::~SampleToChunk() {}
742 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
743 uint32_t count = chunk_info.size();
745 buffer->ReadWriteUInt32(&count));
747 chunk_info.resize(count);
748 for (uint32_t i = 0; i < count; ++i) {
749 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
750 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
751 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
753 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
754 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
759 uint32_t SampleToChunk::ComputeSizeInternal() {
761 sizeof(ChunkInfo) * chunk_info.size();
764 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
765 SampleSize::~SampleSize() {}
768 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
770 buffer->ReadWriteUInt32(&sample_size) &&
771 buffer->ReadWriteUInt32(&sample_count));
773 if (sample_size == 0) {
775 sizes.resize(sample_count);
777 DCHECK(sample_count == sizes.size());
778 for (uint32_t i = 0; i < sample_count; ++i)
779 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
784 uint32_t SampleSize::ComputeSizeInternal() {
785 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
786 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
789 CompactSampleSize::CompactSampleSize() : field_size(0) {}
790 CompactSampleSize::~CompactSampleSize() {}
793 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
794 uint32_t sample_count = sizes.size();
797 buffer->ReadWriteUInt8(&field_size) &&
798 buffer->ReadWriteUInt32(&sample_count));
801 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
802 switch (field_size) {
804 for (uint32_t i = 0; i < sample_count; i += 2) {
807 RCHECK(buffer->ReadWriteUInt8(&size));
808 sizes[i] = size >> 4;
809 sizes[i + 1] = size & 0x0F;
811 DCHECK_LT(sizes[i], 16u);
812 DCHECK_LT(sizes[i + 1], 16u);
813 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
814 RCHECK(buffer->ReadWriteUInt8(&size));
819 for (uint32_t i = 0; i < sample_count; ++i) {
820 uint8_t size = sizes[i];
821 RCHECK(buffer->ReadWriteUInt8(&size));
826 for (uint32_t i = 0; i < sample_count; ++i) {
827 uint16_t size = sizes[i];
828 RCHECK(buffer->ReadWriteUInt16(&size));
835 sizes.resize(sample_count);
839 uint32_t CompactSampleSize::ComputeSizeInternal() {
840 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
841 (field_size * sizes.size() + 7) / 8;
844 ChunkOffset::ChunkOffset() {}
845 ChunkOffset::~ChunkOffset() {}
848 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
849 uint32_t count = offsets.size();
851 buffer->ReadWriteUInt32(&count));
853 offsets.resize(count);
854 for (uint32_t i = 0; i < count; ++i)
859 uint32_t ChunkOffset::ComputeSizeInternal() {
860 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
863 ChunkLargeOffset::ChunkLargeOffset() {}
864 ChunkLargeOffset::~ChunkLargeOffset() {}
867 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
868 uint32_t count = offsets.size();
872 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
874 stco.offsets.swap(offsets);
877 stco.offsets.swap(offsets);
883 buffer->ReadWriteUInt32(&count));
885 offsets.resize(count);
886 for (uint32_t i = 0; i < count; ++i)
887 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
891 uint32_t ChunkLargeOffset::ComputeSizeInternal() {
892 uint32_t count = offsets.size();
893 int use_large_offset =
894 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
896 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
899 SyncSample::SyncSample() {}
900 SyncSample::~SyncSample() {}
903 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
904 uint32_t count = sample_number.size();
906 buffer->ReadWriteUInt32(&count));
908 sample_number.resize(count);
909 for (uint32_t i = 0; i < count; ++i)
910 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
914 uint32_t SyncSample::ComputeSizeInternal() {
916 if (sample_number.empty())
919 sizeof(uint32_t) * sample_number.size();
922 SampleTable::SampleTable() {}
923 SampleTable::~SampleTable() {}
926 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
942 CompactSampleSize compact_sample_size;
943 RCHECK(reader->
ReadChild(&compact_sample_size));
944 sample_size.sample_size = 0;
945 sample_size.sample_count = compact_sample_size.sizes.size();
946 sample_size.sizes.swap(compact_sample_size.sizes);
950 if (reader->
ChildExist(&chunk_large_offset)) {
951 RCHECK(reader->
ReadChild(&chunk_large_offset));
953 ChunkOffset chunk_offset;
954 RCHECK(reader->
ReadChild(&chunk_offset));
955 chunk_large_offset.offsets.swap(chunk_offset.offsets);
965 uint32_t SampleTable::ComputeSizeInternal() {
973 EditList::EditList() {}
974 EditList::~EditList() {}
977 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
978 uint32_t count = edits.size();
982 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
983 for (uint32_t i = 0; i < count; ++i) {
986 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
987 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
988 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
993 uint32_t EditList::ComputeSizeInternal() {
999 for (uint32_t i = 0; i < edits.size(); ++i) {
1000 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
1006 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
1014 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
1020 uint32_t Edit::ComputeSizeInternal() {
1022 if (list.edits.empty())
1027 HandlerReference::HandlerReference() : handler_type(FOURCC_NULL) {}
1028 HandlerReference::~HandlerReference() {}
1031 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
1032 std::vector<uint8_t> handler_name;
1034 switch (handler_type) {
1036 handler_name.assign(kVideoHandlerName,
1037 kVideoHandlerName + arraysize(kVideoHandlerName));
1040 handler_name.assign(kAudioHandlerName,
1041 kAudioHandlerName + arraysize(kAudioHandlerName));
1044 handler_name.assign(kTextHandlerName,
1045 kTextHandlerName + arraysize(kTextHandlerName));
1056 buffer->ReadWriteFourCC(&handler_type));
1059 buffer->ReadWriteVector(&handler_name, handler_name.size()));
1064 uint32_t HandlerReference::ComputeSizeInternal() {
1065 uint32_t box_size =
HeaderSize() + kFourCCSize + 16;
1066 switch (handler_type) {
1068 box_size +=
sizeof(kVideoHandlerName);
1071 box_size +=
sizeof(kAudioHandlerName);
1074 box_size +=
sizeof(kTextHandlerName);
1084 bool Language::ReadWrite(BoxBuffer* buffer) {
1085 if (buffer->Reading()) {
1088 std::vector<uint8_t> temp;
1089 RCHECK(buffer->ReadWriteVector(&temp, 2));
1091 BitReader bit_reader(&temp[0], 2);
1092 bit_reader.SkipBits(1);
1094 for (
int i = 0; i < 3; ++i) {
1095 CHECK(bit_reader.ReadBits(5, &language[i]));
1096 language[i] += 0x60;
1098 code.assign(language, 3);
1101 const char kUndefinedLanguage[] =
"und";
1103 code = kUndefinedLanguage;
1104 DCHECK_EQ(code.size(), 3u);
1108 for (
int i = 0; i < 3; ++i)
1109 lang |= (code[i] - 0x60) << ((2 - i) * 5);
1110 RCHECK(buffer->ReadWriteUInt16(&lang));
1115 uint32_t Language::ComputeSize()
const {
1120 bool PrivFrame::ReadWrite(BoxBuffer* buffer) {
1121 FourCC fourcc = FOURCC_PRIV;
1122 RCHECK(buffer->ReadWriteFourCC(&fourcc));
1123 if (fourcc != FOURCC_PRIV) {
1124 VLOG(1) <<
"Skip unrecognized id3 frame during read: "
1125 << FourCCToString(fourcc);
1129 uint32_t frame_size = owner.size() + 1 + value.size();
1133 DCHECK_LT(frame_size, 0x7Fu);
1135 RCHECK(buffer->ReadWriteUInt32(&frame_size) &&
1136 buffer->ReadWriteUInt16(&flags));
1138 if (buffer->Reading()) {
1140 RCHECK(buffer->ReadWriteString(&str, frame_size));
1142 size_t pos = str.find(
'\0');
1143 RCHECK(pos < str.size());
1144 owner = str.substr(0, pos);
1145 value = str.substr(pos + 1);
1148 RCHECK(buffer->ReadWriteString(&owner, owner.size()) &&
1149 buffer->ReadWriteUInt8(&byte) &&
1150 buffer->ReadWriteString(&value, value.size()));
1155 uint32_t PrivFrame::ComputeSize()
const {
1156 if (owner.empty() && value.empty())
1158 const uint32_t kFourCCSize = 4;
1159 return kFourCCSize +
sizeof(uint32_t) +
sizeof(uint16_t) + owner.size() + 1 +
1168 bool ID3v2::ReadWriteInternal(
BoxBuffer* buffer) {
1170 language.ReadWrite(buffer));
1173 std::string id3v2_identifier = kID3v2Identifier;
1174 uint16_t version = kID3v2Version;
1180 DCHECK_LT(data_size, 0x7Fu);
1182 RCHECK(buffer->
ReadWriteString(&id3v2_identifier, id3v2_identifier.size()) &&
1183 buffer->ReadWriteUInt16(&version) &&
1184 buffer->ReadWriteUInt8(&flags) &&
1185 buffer->ReadWriteUInt32(&data_size));
1191 uint32_t ID3v2::ComputeSizeInternal() {
1194 return private_frame_size == 0 ? 0 :
HeaderSize() + language.ComputeSize() +
1199 Metadata::Metadata() {}
1200 Metadata::~Metadata() {}
1206 bool Metadata::ReadWriteInternal(
BoxBuffer* buffer) {
1214 uint32_t Metadata::ComputeSizeInternal() {
1217 return id3v2_size == 0 ? 0
1221 CodecConfigurationRecord::CodecConfigurationRecord() : box_type(FOURCC_NULL) {}
1222 CodecConfigurationRecord::~CodecConfigurationRecord() {}
1229 bool CodecConfigurationRecord::ReadWriteInternal(
BoxBuffer* buffer) {
1232 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1234 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1239 uint32_t CodecConfigurationRecord::ComputeSizeInternal() {
1245 PixelAspectRatio::PixelAspectRatio() : h_spacing(0), v_spacing(0) {}
1246 PixelAspectRatio::~PixelAspectRatio() {}
1249 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1251 buffer->ReadWriteUInt32(&h_spacing) &&
1252 buffer->ReadWriteUInt32(&v_spacing));
1256 uint32_t PixelAspectRatio::ComputeSizeInternal() {
1258 if (h_spacing == 0 && v_spacing == 0)
1261 DCHECK(h_spacing != 0 && v_spacing != 0);
1262 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1265 VideoSampleEntry::VideoSampleEntry()
1266 : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
1268 VideoSampleEntry::~VideoSampleEntry() {}
1270 if (format == FOURCC_NULL) {
1271 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
1272 <<
"handler type recovered in its Media ancestor.";
1277 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1278 std::vector<uint8_t> compressor_name;
1280 DCHECK(buffer->
reader());
1281 format = buffer->
reader()->type();
1285 const FourCC actual_format = GetActualFormat();
1286 switch (actual_format) {
1288 compressor_name.assign(
1290 kAvcCompressorName + arraysize(kAvcCompressorName));
1294 compressor_name.assign(
1295 kHevcCompressorName,
1296 kHevcCompressorName + arraysize(kHevcCompressorName));
1301 compressor_name.assign(
1303 kVpcCompressorName + arraysize(kVpcCompressorName));
1306 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1309 compressor_name.resize(kCompressorNameSize);
1312 uint32_t video_resolution = kVideoResolution;
1313 uint16_t video_frame_count = kVideoFrameCount;
1314 uint16_t video_depth = kVideoDepth;
1315 int16_t predefined = -1;
1317 buffer->ReadWriteUInt16(&data_reference_index) &&
1319 buffer->ReadWriteUInt16(&width) &&
1320 buffer->ReadWriteUInt16(&height) &&
1321 buffer->ReadWriteUInt32(&video_resolution) &&
1322 buffer->ReadWriteUInt32(&video_resolution) &&
1324 buffer->ReadWriteUInt16(&video_frame_count) &&
1325 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1326 buffer->ReadWriteUInt16(&video_depth) &&
1327 buffer->ReadWriteInt16(&predefined));
1331 if (format == FOURCC_encv) {
1335 while (sinf.type.type != FOURCC_cenc && sinf.type.type != FOURCC_cbc1) {
1344 const FourCC actual_format = GetActualFormat();
1345 switch (actual_format) {
1347 codec_config_record.box_type = FOURCC_avcC;
1351 codec_config_record.box_type = FOURCC_hvcC;
1356 codec_config_record.box_type = FOURCC_vpcC;
1359 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1367 uint32_t VideoSampleEntry::ComputeSizeInternal() {
1368 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1369 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1370 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1372 codec_config_record.
ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1376 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1377 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1380 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1383 std::vector<uint8_t> data;
1384 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1385 RCHECK(es_descriptor.Parse(data));
1386 if (es_descriptor.
IsAAC()) {
1387 RCHECK(aac_audio_specific_config.
Parse(
1388 es_descriptor.decoder_specific_info()));
1391 DCHECK(buffer->
writer());
1392 es_descriptor.Write(buffer->
writer());
1397 uint32_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1399 if (es_descriptor.object_type() == kForbidden)
1401 return HeaderSize() + es_descriptor.ComputeSize();
1404 DTSSpecific::DTSSpecific()
1405 : sampling_frequency(0),
1408 pcm_sample_depth(0) {}
1409 DTSSpecific::~DTSSpecific() {}
1412 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1414 buffer->ReadWriteUInt32(&sampling_frequency) &&
1415 buffer->ReadWriteUInt32(&max_bitrate) &&
1416 buffer->ReadWriteUInt32(&avg_bitrate) &&
1417 buffer->ReadWriteUInt8(&pcm_sample_depth));
1420 RCHECK(buffer->ReadWriteVector(&extra_data, buffer->
BytesLeft()));
1422 if (extra_data.empty()) {
1423 extra_data.assign(kDdtsExtraData,
1424 kDdtsExtraData +
sizeof(kDdtsExtraData));
1426 RCHECK(buffer->ReadWriteVector(&extra_data, extra_data.size()));
1431 uint32_t DTSSpecific::ComputeSizeInternal() {
1433 if (sampling_frequency == 0)
1435 return HeaderSize() +
sizeof(sampling_frequency) +
sizeof(max_bitrate) +
1436 sizeof(avg_bitrate) +
sizeof(pcm_sample_depth) +
1437 sizeof(kDdtsExtraData);
1440 AC3Specific::AC3Specific() {}
1441 AC3Specific::~AC3Specific() {}
1445 bool AC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1447 buffer->ReadWriteVector(
1452 uint32_t AC3Specific::ComputeSizeInternal() {
1459 EC3Specific::EC3Specific() {}
1460 EC3Specific::~EC3Specific() {}
1464 bool EC3Specific::ReadWriteInternal(
BoxBuffer* buffer) {
1467 RCHECK(buffer->ReadWriteVector(&data, size));
1471 uint32_t EC3Specific::ComputeSizeInternal() {
1478 AudioSampleEntry::AudioSampleEntry()
1479 : format(FOURCC_NULL),
1480 data_reference_index(1),
1485 AudioSampleEntry::~AudioSampleEntry() {}
1488 if (format == FOURCC_NULL) {
1489 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1490 <<
"handler type recovered in its Media ancestor.";
1495 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1497 DCHECK(buffer->
reader());
1498 format = buffer->
reader()->type();
1506 buffer->ReadWriteUInt16(&data_reference_index) &&
1508 buffer->ReadWriteUInt16(&channelcount) &&
1509 buffer->ReadWriteUInt16(&samplesize) &&
1511 buffer->ReadWriteUInt32(&samplerate));
1516 if (format == FOURCC_enca) {
1520 while (sinf.type.type != FOURCC_cenc && sinf.type.type != FOURCC_cbc1) {
1536 uint32_t AudioSampleEntry::ComputeSizeInternal() {
1537 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1538 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1545 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1546 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1552 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1559 uint32_t WebVTTConfigurationBox::ComputeSizeInternal() {
1563 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1564 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1570 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1574 : source_label.size());
1577 uint32_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1578 if (source_label.empty())
1583 TextSampleEntry::TextSampleEntry() : format(FOURCC_NULL) {}
1584 TextSampleEntry::~TextSampleEntry() {}
1587 if (format == FOURCC_NULL) {
1588 LOG(ERROR) <<
"TextSampleEntry should be parsed according to the "
1589 <<
"handler type recovered in its Media ancestor.";
1594 bool TextSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1596 DCHECK(buffer->
reader());
1597 format = buffer->
reader()->type();
1602 buffer->ReadWriteUInt16(&data_reference_index));
1604 if (format == FOURCC_wvtt) {
1613 uint32_t TextSampleEntry::ComputeSizeInternal() {
1615 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1619 MediaHeader::MediaHeader()
1620 : creation_time(0), modification_time(0), timescale(0), duration(0) {}
1621 MediaHeader::~MediaHeader() {}
1624 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1627 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1630 buffer->ReadWriteUInt32(×cale) &&
1632 language.ReadWrite(buffer) &&
1637 uint32_t MediaHeader::ComputeSizeInternal() {
1638 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1640 sizeof(uint32_t) * (1 + version) * 3 + language.ComputeSize() +
1644 VideoMediaHeader::VideoMediaHeader()
1645 : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1646 const uint32_t kVideoMediaHeaderFlags = 1;
1647 flags = kVideoMediaHeaderFlags;
1649 VideoMediaHeader::~VideoMediaHeader() {}
1651 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1653 buffer->ReadWriteUInt16(&graphicsmode) &&
1654 buffer->ReadWriteUInt16(&opcolor_red) &&
1655 buffer->ReadWriteUInt16(&opcolor_green) &&
1656 buffer->ReadWriteUInt16(&opcolor_blue));
1660 uint32_t VideoMediaHeader::ComputeSizeInternal() {
1661 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1662 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1665 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1666 SoundMediaHeader::~SoundMediaHeader() {}
1668 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1670 buffer->ReadWriteUInt16(&balance) &&
1675 uint32_t SoundMediaHeader::ComputeSizeInternal() {
1676 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1679 SubtitleMediaHeader::SubtitleMediaHeader() {}
1680 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1684 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1688 uint32_t SubtitleMediaHeader::ComputeSizeInternal() {
1692 DataEntryUrl::DataEntryUrl() {
1693 const uint32_t kDataEntryUrlFlags = 1;
1694 flags = kDataEntryUrlFlags;
1696 DataEntryUrl::~DataEntryUrl() {}
1698 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
1701 RCHECK(buffer->ReadWriteVector(&location, buffer->
BytesLeft()));
1703 RCHECK(buffer->ReadWriteVector(&location, location.size()));
1708 uint32_t DataEntryUrl::ComputeSizeInternal() {
1712 DataReference::DataReference() {
1714 data_entry.resize(1);
1716 DataReference::~DataReference() {}
1718 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
1719 uint32_t entry_count = data_entry.size();
1721 buffer->ReadWriteUInt32(&entry_count));
1722 data_entry.resize(entry_count);
1724 for (uint32_t i = 0; i < entry_count; ++i)
1729 uint32_t DataReference::ComputeSizeInternal() {
1730 uint32_t count = data_entry.size();
1731 uint32_t box_size =
HeaderSize() +
sizeof(count);
1732 for (uint32_t i = 0; i < count; ++i)
1737 DataInformation::DataInformation() {}
1738 DataInformation::~DataInformation() {}
1741 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1747 uint32_t DataInformation::ComputeSizeInternal() {
1751 MediaInformation::MediaInformation() {}
1752 MediaInformation::~MediaInformation() {}
1755 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1760 switch (sample_table.description.type) {
1777 uint32_t MediaInformation::ComputeSizeInternal() {
1780 switch (sample_table.description.type) {
1800 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
1812 information.sample_table.description.type =
1813 FourCCToTrackType(handler.handler_type);
1815 handler.handler_type =
1816 TrackTypeToFourCC(information.sample_table.description.type);
1817 RCHECK(handler.handler_type != FOURCC_NULL);
1824 uint32_t Media::ComputeSizeInternal() {
1825 handler.handler_type =
1826 TrackTypeToFourCC(information.sample_table.description.type);
1835 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
1845 uint32_t Track::ComputeSizeInternal() {
1850 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
1851 MovieExtendsHeader::~MovieExtendsHeader() {}
1854 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1856 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1861 uint32_t MovieExtendsHeader::ComputeSizeInternal() {
1863 if (fragment_duration == 0)
1865 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
1866 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1869 TrackExtends::TrackExtends()
1871 default_sample_description_index(0),
1872 default_sample_duration(0),
1873 default_sample_size(0),
1874 default_sample_flags(0) {}
1875 TrackExtends::~TrackExtends() {}
1878 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1880 buffer->ReadWriteUInt32(&track_id) &&
1881 buffer->ReadWriteUInt32(&default_sample_description_index) &&
1882 buffer->ReadWriteUInt32(&default_sample_duration) &&
1883 buffer->ReadWriteUInt32(&default_sample_size) &&
1884 buffer->ReadWriteUInt32(&default_sample_flags));
1888 uint32_t TrackExtends::ComputeSizeInternal() {
1890 sizeof(default_sample_description_index) +
1891 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
1892 sizeof(default_sample_flags);
1895 MovieExtends::MovieExtends() {}
1896 MovieExtends::~MovieExtends() {}
1899 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1904 DCHECK(buffer->
reader());
1907 for (uint32_t i = 0; i < tracks.size(); ++i)
1913 uint32_t MovieExtends::ComputeSizeInternal() {
1915 if (tracks.size() == 0)
1918 for (uint32_t i = 0; i < tracks.size(); ++i)
1927 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
1939 for (uint32_t i = 0; i < tracks.size(); ++i)
1941 for (uint32_t i = 0; i < pssh.size(); ++i)
1947 uint32_t Movie::ComputeSizeInternal() {
1950 for (uint32_t i = 0; i < tracks.size(); ++i)
1952 for (uint32_t i = 0; i < pssh.size(); ++i)
1957 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
1958 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
1961 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
1963 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1968 uint32_t TrackFragmentDecodeTime::ComputeSizeInternal() {
1969 version = IsFitIn32Bits(decode_time) ? 0 : 1;
1970 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1973 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
1974 MovieFragmentHeader::~MovieFragmentHeader() {}
1977 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1979 buffer->ReadWriteUInt32(&sequence_number);
1982 uint32_t MovieFragmentHeader::ComputeSizeInternal() {
1983 return HeaderSize() +
sizeof(sequence_number);
1986 TrackFragmentHeader::TrackFragmentHeader()
1988 sample_description_index(0),
1989 default_sample_duration(0),
1990 default_sample_size(0),
1991 default_sample_flags(0) {}
1993 TrackFragmentHeader::~TrackFragmentHeader() {}
1996 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1998 buffer->ReadWriteUInt32(&track_id));
2000 if (flags & kBaseDataOffsetPresentMask) {
2005 uint64_t base_data_offset;
2006 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
2007 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
2008 "default-base-is-moof.";
2011 if (flags & kSampleDescriptionIndexPresentMask) {
2012 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
2013 }
else if (buffer->
Reading()) {
2014 sample_description_index = 0;
2017 if (flags & kDefaultSampleDurationPresentMask) {
2018 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
2019 }
else if (buffer->
Reading()) {
2020 default_sample_duration = 0;
2023 if (flags & kDefaultSampleSizePresentMask) {
2024 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
2025 }
else if (buffer->
Reading()) {
2026 default_sample_size = 0;
2029 if (flags & kDefaultSampleFlagsPresentMask)
2030 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
2034 uint32_t TrackFragmentHeader::ComputeSizeInternal() {
2035 uint32_t box_size =
HeaderSize() +
sizeof(track_id);
2036 if (flags & kSampleDescriptionIndexPresentMask)
2037 box_size +=
sizeof(sample_description_index);
2038 if (flags & kDefaultSampleDurationPresentMask)
2039 box_size +=
sizeof(default_sample_duration);
2040 if (flags & kDefaultSampleSizePresentMask)
2041 box_size +=
sizeof(default_sample_size);
2042 if (flags & kDefaultSampleFlagsPresentMask)
2043 box_size +=
sizeof(default_sample_flags);
2047 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
2048 TrackFragmentRun::~TrackFragmentRun() {}
2051 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
2057 if (flags & kSampleCompTimeOffsetsPresentMask) {
2058 for (uint32_t i = 0; i < sample_count; ++i) {
2059 if (sample_composition_time_offsets[i] < 0) {
2068 buffer->ReadWriteUInt32(&sample_count));
2070 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
2071 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
2072 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
2073 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
2074 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
2075 bool sample_composition_time_offsets_present =
2076 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
2078 if (data_offset_present) {
2079 RCHECK(buffer->ReadWriteUInt32(&data_offset));
2090 uint32_t first_sample_flags;
2093 if (first_sample_flags_present)
2094 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2096 if (sample_duration_present)
2097 sample_durations.resize(sample_count);
2098 if (sample_size_present)
2099 sample_sizes.resize(sample_count);
2100 if (sample_flags_present)
2101 sample_flags.resize(sample_count);
2102 if (sample_composition_time_offsets_present)
2103 sample_composition_time_offsets.resize(sample_count);
2105 if (first_sample_flags_present) {
2106 first_sample_flags = sample_flags[0];
2107 DCHECK(sample_flags.size() == 1);
2108 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
2111 if (sample_duration_present)
2112 DCHECK(sample_durations.size() == sample_count);
2113 if (sample_size_present)
2114 DCHECK(sample_sizes.size() == sample_count);
2115 if (sample_flags_present)
2116 DCHECK(sample_flags.size() == sample_count);
2117 if (sample_composition_time_offsets_present)
2118 DCHECK(sample_composition_time_offsets.size() == sample_count);
2121 for (uint32_t i = 0; i < sample_count; ++i) {
2122 if (sample_duration_present)
2123 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
2124 if (sample_size_present)
2125 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
2126 if (sample_flags_present)
2127 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
2129 if (sample_composition_time_offsets_present) {
2131 uint32_t sample_offset = sample_composition_time_offsets[i];
2132 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
2133 sample_composition_time_offsets[i] = sample_offset;
2135 int32_t sample_offset = sample_composition_time_offsets[i];
2136 RCHECK(buffer->ReadWriteInt32(&sample_offset));
2137 sample_composition_time_offsets[i] = sample_offset;
2143 if (first_sample_flags_present) {
2144 if (sample_flags.size() == 0) {
2145 sample_flags.push_back(first_sample_flags);
2147 sample_flags[0] = first_sample_flags;
2154 uint32_t TrackFragmentRun::ComputeSizeInternal() {
2155 uint32_t box_size =
HeaderSize() +
sizeof(sample_count);
2156 if (flags & kDataOffsetPresentMask)
2157 box_size +=
sizeof(data_offset);
2158 if (flags & kFirstSampleFlagsPresentMask)
2159 box_size +=
sizeof(uint32_t);
2160 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
2161 (flags & kSampleSizePresentMask ? 1 : 0) +
2162 (flags & kSampleFlagsPresentMask ? 1 : 0) +
2163 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
2164 box_size += fields *
sizeof(uint32_t) * sample_count;
2168 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
2169 SampleToGroup::~SampleToGroup() {}
2172 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
2174 buffer->ReadWriteUInt32(&grouping_type));
2176 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
2178 if (grouping_type != FOURCC_seig) {
2180 DLOG(WARNING) <<
"Sample group "
2181 << FourCCToString(static_cast<FourCC>(grouping_type))
2182 <<
" is not supported.";
2186 uint32_t count = entries.size();
2187 RCHECK(buffer->ReadWriteUInt32(&count));
2188 entries.resize(count);
2189 for (uint32_t i = 0; i < count; ++i) {
2190 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
2191 buffer->ReadWriteUInt32(&entries[i].group_description_index));
2196 uint32_t SampleToGroup::ComputeSizeInternal() {
2198 if (entries.empty())
2200 return HeaderSize() +
sizeof(grouping_type) +
2201 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
2202 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
2205 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
2207 per_sample_iv_size(0),
2208 crypt_byte_block(0),
2209 skip_byte_block(0) {}
2210 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
2212 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
2213 SampleGroupDescription::~SampleGroupDescription() {}
2216 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
2218 buffer->ReadWriteUInt32(&grouping_type));
2220 if (grouping_type != FOURCC_seig) {
2222 DLOG(WARNING) <<
"Sample group '" << grouping_type <<
"' is not supported.";
2226 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
2227 uint32_t default_length = 0;
2230 RCHECK(buffer->ReadWriteUInt32(&default_length));
2231 RCHECK(default_length == 0 || default_length >= kEntrySize);
2233 default_length = kEntrySize;
2234 RCHECK(buffer->ReadWriteUInt32(&default_length));
2238 uint32_t count = entries.size();
2239 RCHECK(buffer->ReadWriteUInt32(&count));
2240 entries.resize(count);
2241 for (uint32_t i = 0; i < count; ++i) {
2243 if (buffer->
Reading() && default_length == 0) {
2244 uint32_t description_length = 0;
2245 RCHECK(buffer->ReadWriteUInt32(&description_length));
2246 RCHECK(description_length >= kEntrySize);
2251 if (entries[i].key_id.size() != kCencKeyIdSize) {
2252 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
2253 <<
" bytes; got " << entries[i].key_id.size()
2254 <<
". Resized accordingly.";
2255 entries[i].key_id.resize(kCencKeyIdSize);
2257 RCHECK(entries[i].crypt_byte_block < 16 &&
2258 entries[i].skip_byte_block < 16);
2264 entries[i].crypt_byte_block << 4 | entries[i].skip_byte_block;
2265 RCHECK(buffer->ReadWriteUInt8(&pattern));
2266 entries[i].crypt_byte_block = pattern >> 4;
2267 entries[i].skip_byte_block = pattern & 0x0F;
2269 RCHECK(buffer->ReadWriteUInt8(&entries[i].is_protected) &&
2270 buffer->ReadWriteUInt8(&entries[i].per_sample_iv_size) &&
2271 buffer->ReadWriteVector(&entries[i].key_id, kCencKeyIdSize));
2273 if (entries[i].is_protected == 1) {
2274 if (entries[i].per_sample_iv_size == 0) {
2275 uint8_t constant_iv_size = entries[i].constant_iv.size();
2276 RCHECK(buffer->ReadWriteUInt8(&constant_iv_size));
2277 RCHECK(constant_iv_size == 8 || constant_iv_size == 16);
2279 buffer->ReadWriteVector(&entries[i].constant_iv, constant_iv_size));
2281 RCHECK(entries[i].per_sample_iv_size == 8 ||
2282 entries[i].per_sample_iv_size == 16);
2283 RCHECK(entries[i].constant_iv.empty());
2288 RCHECK(entries[i].is_protected == 0);
2289 RCHECK(entries[i].per_sample_iv_size == 0);
2296 uint32_t SampleGroupDescription::ComputeSizeInternal() {
2300 if (entries.empty())
2302 size_t entries_size = 0;
2303 for (
const auto& entry : entries) {
2304 entries_size +=
sizeof(uint32_t) + kCencKeyIdSize +
2305 (entry.constant_iv.empty()
2307 : (
sizeof(uint8_t) + entry.constant_iv.size()));
2309 return HeaderSize() +
sizeof(grouping_type) +
2310 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
2314 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2315 TrackFragment::~TrackFragment() {}
2318 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2323 DCHECK(buffer->
reader());
2325 if (!decode_time_absent)
2333 while (sample_to_group.grouping_type != FOURCC_seig &&
2337 while (sample_group_description.grouping_type != FOURCC_seig &&
2342 if (!decode_time_absent)
2344 for (uint32_t i = 0; i < runs.size(); ++i)
2354 uint32_t TrackFragment::ComputeSizeInternal() {
2360 for (uint32_t i = 0; i < runs.size(); ++i)
2365 MovieFragment::MovieFragment() {}
2366 MovieFragment::~MovieFragment() {}
2369 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2379 for (uint32_t i = 0; i < tracks.size(); ++i)
2381 for (uint32_t i = 0; i < pssh.size(); ++i)
2387 uint32_t MovieFragment::ComputeSizeInternal() {
2389 for (uint32_t i = 0; i < tracks.size(); ++i)
2391 for (uint32_t i = 0; i < pssh.size(); ++i)
2396 SegmentIndex::SegmentIndex()
2399 earliest_presentation_time(0),
2401 SegmentIndex::~SegmentIndex() {}
2404 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2406 buffer->ReadWriteUInt32(&reference_id) &&
2407 buffer->ReadWriteUInt32(×cale));
2409 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2414 uint16_t reference_count = references.size();
2416 buffer->ReadWriteUInt16(&reference_count));
2417 references.resize(reference_count);
2419 uint32_t reference_type_size;
2421 for (uint32_t i = 0; i < reference_count; ++i) {
2423 reference_type_size = references[i].referenced_size;
2424 if (references[i].reference_type)
2425 reference_type_size |= (1 << 31);
2426 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2427 if (references[i].starts_with_sap)
2430 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2431 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2432 buffer->ReadWriteUInt32(&sap));
2434 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2435 references[i].referenced_size = reference_type_size & ~(1 << 31);
2436 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2437 references[i].sap_type =
2438 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2439 references[i].sap_delta_time = sap & ~(0xF << 28);
2445 uint32_t SegmentIndex::ComputeSizeInternal() {
2446 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2447 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2448 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2449 3 *
sizeof(uint32_t) * references.size();
2452 MediaData::MediaData() : data_size(0) {}
2453 MediaData::~MediaData() {}
2456 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2457 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2461 uint32_t MediaData::ComputeSizeInternal() {
2465 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2466 CueSourceIDBox::~CueSourceIDBox() {}
2470 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2475 uint32_t CueSourceIDBox::ComputeSizeInternal() {
2476 if (source_id == kCueSourceIdNotSet)
2481 CueTimeBox::CueTimeBox() {}
2482 CueTimeBox::~CueTimeBox() {}
2488 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2495 uint32_t CueTimeBox::ComputeSizeInternal() {
2496 if (cue_current_time.empty())
2498 return HeaderSize() + cue_current_time.size();
2501 CueIDBox::CueIDBox() {}
2502 CueIDBox::~CueIDBox() {}
2508 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2514 uint32_t CueIDBox::ComputeSizeInternal() {
2520 CueSettingsBox::CueSettingsBox() {}
2521 CueSettingsBox::~CueSettingsBox() {}
2527 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2533 uint32_t CueSettingsBox::ComputeSizeInternal() {
2534 if (settings.empty())
2539 CuePayloadBox::CuePayloadBox() {}
2540 CuePayloadBox::~CuePayloadBox() {}
2546 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2552 uint32_t CuePayloadBox::ComputeSizeInternal() {
2556 VTTEmptyCueBox::VTTEmptyCueBox() {}
2557 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2563 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2567 uint32_t VTTEmptyCueBox::ComputeSizeInternal() {
2571 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2572 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2578 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2581 &cue_additional_text,
2585 uint32_t VTTAdditionalTextBox::ComputeSizeInternal() {
2586 return HeaderSize() + cue_additional_text.size();
2589 VTTCueBox::VTTCueBox() {}
2590 VTTCueBox::~VTTCueBox() {}
2596 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2607 uint32_t VTTCueBox::ComputeSizeInternal() {
uint32_t ComputeSize() const
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
FourCC BoxType() const override
bool ParseFromBuffer(uint8_t iv_size, bool has_subsamples, BufferReader *reader)
FourCC BoxType() const override
bool ReadWrite(uint8_t iv_size, bool has_subsamples, BoxBuffer *buffer)
uint32_t GetTotalSizeOfSubsamples() const