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/formats/mp4/box_buffer.h"
12 #include "packager/media/formats/mp4/rcheck.h"
15 const uint32_t kFourCCSize = 4;
18 const uint32_t kCencKeyIdSize = 16;
21 const uint8_t kUnityMatrix[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
23 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0};
26 const char kVideoHandlerName[] =
"VideoHandler";
27 const char kAudioHandlerName[] =
"SoundHandler";
28 const char kTextHandlerName[] =
"TextHandler";
31 const uint32_t kVideoResolution = 0x00480000;
32 const uint16_t kVideoFrameCount = 1;
33 const uint16_t kVideoDepth = 0x0018;
35 const uint32_t kCompressorNameSize = 32u;
36 const char kAvcCompressorName[] =
"\012AVC Coding";
37 const char kHevcCompressorName[] =
"\013HEVC Coding";
38 const char kVpcCompressorName[] =
"\012VPC Coding";
42 const int kCueSourceIdNotSet = -1;
46 bool IsIvSizeValid(
size_t iv_size) {
47 return iv_size == 8 || iv_size == 16;
51 bool IsFitIn32Bits(uint64_t a) {
52 return a <= std::numeric_limits<uint32_t>::max();
55 bool IsFitIn32Bits(int64_t a) {
56 return a <= std::numeric_limits<int32_t>::max() &&
57 a >= std::numeric_limits<int32_t>::min();
60 template <
typename T1,
typename T2>
61 bool IsFitIn32Bits(T1 a1, T2 a2) {
62 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
65 template <
typename T1,
typename T2,
typename T3>
66 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
67 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
72 namespace edash_packager {
76 FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
77 FileType::~FileType() {}
80 bool FileType::ReadWriteInternal(
BoxBuffer* buffer) {
82 buffer->ReadWriteFourCC(&major_brand) &&
83 buffer->ReadWriteUInt32(&minor_version));
86 num_brands = (buffer->
Size() - buffer->
Pos()) /
sizeof(FourCC);
87 compatible_brands.resize(num_brands);
89 num_brands = compatible_brands.size();
91 for (
size_t i = 0; i < num_brands; ++i)
92 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
96 uint32_t FileType::ComputeSizeInternal() {
97 return HeaderSize() + kFourCCSize +
sizeof(minor_version) +
98 kFourCCSize * compatible_brands.size();
103 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
104 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
107 bool ProtectionSystemSpecificHeader::ReadWriteInternal(
BoxBuffer* buffer) {
108 if (!buffer->
Reading() && !raw_box.empty()) {
110 buffer->
writer()->AppendVector(raw_box);
114 uint32_t size = data.size();
116 buffer->ReadWriteVector(&system_id, 16) &&
117 buffer->ReadWriteUInt32(&size) &&
118 buffer->ReadWriteVector(&data, size));
123 DCHECK(raw_box.empty());
124 BoxReader* reader = buffer->
reader();
126 raw_box.assign(reader->data(), reader->data() + reader->size());
131 uint32_t ProtectionSystemSpecificHeader::ComputeSizeInternal() {
132 if (!raw_box.empty()) {
133 return raw_box.size();
135 return HeaderSize() + system_id.size() +
sizeof(uint32_t) + data.size();
139 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
140 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
143 bool SampleAuxiliaryInformationOffset::ReadWriteInternal(
BoxBuffer* buffer) {
148 uint32_t count = offsets.size();
149 RCHECK(buffer->ReadWriteUInt32(&count));
150 offsets.resize(count);
152 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
153 for (uint32_t i = 0; i < count; ++i)
158 uint32_t SampleAuxiliaryInformationOffset::ComputeSizeInternal() {
160 if (offsets.size() == 0)
162 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
163 return HeaderSize() +
sizeof(uint32_t) + num_bytes * offsets.size();
166 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
167 : default_sample_info_size(0), sample_count(0) {}
168 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
171 bool SampleAuxiliaryInformationSize::ReadWriteInternal(
BoxBuffer* buffer) {
176 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
177 buffer->ReadWriteUInt32(&sample_count));
178 if (default_sample_info_size == 0)
179 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
183 uint32_t SampleAuxiliaryInformationSize::ComputeSizeInternal() {
185 if (sample_count == 0)
187 return HeaderSize() +
sizeof(default_sample_info_size) +
188 sizeof(sample_count) +
189 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
192 SampleEncryptionEntry::SampleEncryptionEntry() {}
193 SampleEncryptionEntry::~SampleEncryptionEntry() {}
198 DCHECK(IsIvSizeValid(iv_size));
201 RCHECK(buffer->ReadWriteVector(&initialization_vector, iv_size));
203 if (!has_subsamples) {
208 uint16_t subsample_count = subsamples.size();
209 RCHECK(buffer->ReadWriteUInt16(&subsample_count));
210 RCHECK(subsample_count > 0);
211 subsamples.resize(subsample_count);
212 for (
auto& subsample : subsamples) {
213 RCHECK(buffer->ReadWriteUInt16(&subsample.clear_bytes) &&
214 buffer->ReadWriteUInt32(&subsample.cipher_bytes));
222 DCHECK(IsIvSizeValid(iv_size));
225 initialization_vector.resize(iv_size);
226 RCHECK(reader->ReadToVector(&initialization_vector, iv_size));
228 if (!has_subsamples) {
233 uint16_t subsample_count;
234 RCHECK(reader->Read2(&subsample_count));
235 RCHECK(subsample_count > 0);
236 subsamples.resize(subsample_count);
237 for (
auto& subsample : subsamples) {
238 RCHECK(reader->Read2(&subsample.clear_bytes) &&
239 reader->Read4(&subsample.cipher_bytes));
245 const uint32_t subsample_entry_size =
sizeof(uint16_t) +
sizeof(uint32_t);
246 const uint16_t subsample_count = subsamples.size();
247 return initialization_vector.size() +
248 (subsample_count > 0 ? (
sizeof(subsample_count) +
249 subsample_entry_size * subsample_count)
255 for (uint32_t i = 0; i < subsamples.size(); ++i)
256 size += subsamples[i].clear_bytes + subsamples[i].cipher_bytes;
260 SampleEncryption::SampleEncryption() : iv_size(0) {}
261 SampleEncryption::~SampleEncryption() {}
264 bool SampleEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
269 if (buffer->
Reading() && iv_size == 0) {
271 buffer->
Size() - buffer->
Pos()));
275 if (!IsIvSizeValid(iv_size)) {
276 LOG(ERROR) <<
"IV_size can only be 8 or 16, but seeing " << iv_size;
280 uint32_t sample_count = sample_encryption_entries.size();
281 RCHECK(buffer->ReadWriteUInt32(&sample_count));
283 sample_encryption_entries.resize(sample_count);
284 for (
auto& sample_encryption_entry : sample_encryption_entries) {
285 RCHECK(sample_encryption_entry.ReadWrite(
286 iv_size, flags & kUseSubsampleEncryption, buffer));
291 uint32_t SampleEncryption::ComputeSizeInternal() {
292 const uint32_t sample_count = sample_encryption_entries.size();
293 if (sample_count == 0) {
298 DCHECK(IsIvSizeValid(iv_size));
300 if (flags & kUseSubsampleEncryption) {
301 for (
const SampleEncryptionEntry& sample_encryption_entry :
302 sample_encryption_entries) {
303 box_size += sample_encryption_entry.ComputeSize();
306 box_size += sample_count * iv_size;
313 std::vector<SampleEncryptionEntry>* sample_encryption_entries)
const {
314 DCHECK(IsIvSizeValid(iv_size));
318 uint32_t sample_count = 0;
319 RCHECK(reader.Read4(&sample_count));
321 sample_encryption_entries->resize(sample_count);
322 for (
auto& sample_encryption_entry : *sample_encryption_entries) {
323 RCHECK(sample_encryption_entry.ParseFromBuffer(
324 iv_size, flags & kUseSubsampleEncryption, &reader));
329 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
330 OriginalFormat::~OriginalFormat() {}
333 bool OriginalFormat::ReadWriteInternal(
BoxBuffer* buffer) {
337 uint32_t OriginalFormat::ComputeSizeInternal() {
341 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
342 SchemeType::~SchemeType() {}
345 bool SchemeType::ReadWriteInternal(
BoxBuffer* buffer) {
347 buffer->ReadWriteFourCC(&type) &&
348 buffer->ReadWriteUInt32(&version));
352 uint32_t SchemeType::ComputeSizeInternal() {
353 return HeaderSize() + kFourCCSize +
sizeof(version);
356 TrackEncryption::TrackEncryption()
357 : is_encrypted(false), default_iv_size(0), default_kid(16, 0) {}
358 TrackEncryption::~TrackEncryption() {}
361 bool TrackEncryption::ReadWriteInternal(
BoxBuffer* buffer) {
363 if (default_kid.size() != kCencKeyIdSize) {
364 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
365 <<
" bytes; got " << default_kid.size()
366 <<
". Resized accordingly.";
367 default_kid.resize(kCencKeyIdSize);
371 uint8_t flag = is_encrypted ? 1 : 0;
374 buffer->ReadWriteUInt8(&flag) &&
375 buffer->ReadWriteUInt8(&default_iv_size) &&
376 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
378 is_encrypted = (flag != 0);
380 RCHECK(default_iv_size == 8 || default_iv_size == 16);
382 RCHECK(default_iv_size == 0);
388 uint32_t TrackEncryption::ComputeSizeInternal() {
389 return HeaderSize() +
sizeof(uint32_t) + kCencKeyIdSize;
392 SchemeInfo::SchemeInfo() {}
393 SchemeInfo::~SchemeInfo() {}
396 bool SchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
402 uint32_t SchemeInfo::ComputeSizeInternal() {
406 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
407 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
410 bool ProtectionSchemeInfo::ReadWriteInternal(
BoxBuffer* buffer) {
415 if (type.type == FOURCC_CENC)
424 uint32_t ProtectionSchemeInfo::ComputeSizeInternal() {
426 if (format.format == FOURCC_NULL)
432 MovieHeader::MovieHeader()
434 modification_time(0),
440 MovieHeader::~MovieHeader() {}
443 bool MovieHeader::ReadWriteInternal(
BoxBuffer* buffer) {
446 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
449 buffer->ReadWriteUInt32(×cale) &&
452 std::vector<uint8_t> matrix(kUnityMatrix,
453 kUnityMatrix + arraysize(kUnityMatrix));
454 RCHECK(buffer->ReadWriteInt32(&rate) &&
455 buffer->ReadWriteInt16(&volume) &&
457 buffer->ReadWriteVector(&matrix, matrix.size()) &&
459 buffer->ReadWriteUInt32(&next_track_id));
463 uint32_t MovieHeader::ComputeSizeInternal() {
464 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
465 return HeaderSize() +
sizeof(uint32_t) * (1 + version) * 3 +
466 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
467 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
471 TrackHeader::TrackHeader()
473 modification_time(0),
481 flags = kTrackEnabled | kTrackInMovie;
483 TrackHeader::~TrackHeader() {}
486 bool TrackHeader::ReadWriteInternal(
BoxBuffer* buffer) {
489 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
492 buffer->ReadWriteUInt32(&track_id) &&
499 volume = (width != 0 && height != 0) ? 0 : 0x100;
501 std::vector<uint8_t> matrix(kUnityMatrix,
502 kUnityMatrix + arraysize(kUnityMatrix));
504 buffer->ReadWriteInt16(&layer) &&
505 buffer->ReadWriteInt16(&alternate_group) &&
506 buffer->ReadWriteInt16(&volume) &&
508 buffer->ReadWriteVector(&matrix, matrix.size()) &&
509 buffer->ReadWriteUInt32(&width) &&
510 buffer->ReadWriteUInt32(&height));
514 uint32_t TrackHeader::ComputeSizeInternal() {
515 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
517 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
518 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
519 sizeof(height) +
sizeof(kUnityMatrix) + 14;
522 SampleDescription::SampleDescription() : type(kInvalid) {}
523 SampleDescription::~SampleDescription() {}
526 bool SampleDescription::ReadWriteInternal(
BoxBuffer* buffer) {
530 count = video_entries.size();
533 count = audio_entries.size();
536 count = wvtt_entries.size();
539 NOTIMPLEMENTED() <<
"SampleDecryption type " << type
540 <<
" is not handled. Skipping.";
543 buffer->ReadWriteUInt32(&count));
546 BoxReader* reader = buffer->
reader();
548 video_entries.clear();
549 audio_entries.clear();
552 if (type == kVideo) {
553 RCHECK(reader->ReadAllChildren(&video_entries));
554 RCHECK(video_entries.size() == count);
555 }
else if (type == kAudio) {
556 RCHECK(reader->ReadAllChildren(&audio_entries));
557 RCHECK(audio_entries.size() == count);
558 }
else if (type == kText) {
559 RCHECK(reader->ReadAllChildren(&wvtt_entries));
560 RCHECK(wvtt_entries.size() == count);
563 DCHECK_LT(0u, count);
564 if (type == kVideo) {
565 for (uint32_t i = 0; i < count; ++i)
567 }
else if (type == kAudio) {
568 for (uint32_t i = 0; i < count; ++i)
570 }
else if (type == kText) {
571 for (uint32_t i = 0; i < count; ++i)
580 uint32_t SampleDescription::ComputeSizeInternal() {
581 uint32_t box_size =
HeaderSize() +
sizeof(uint32_t);
582 if (type == kVideo) {
583 for (uint32_t i = 0; i < video_entries.size(); ++i)
585 }
else if (type == kAudio) {
586 for (uint32_t i = 0; i < audio_entries.size(); ++i)
592 DecodingTimeToSample::DecodingTimeToSample() {}
593 DecodingTimeToSample::~DecodingTimeToSample() {}
596 bool DecodingTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
597 uint32_t count = decoding_time.size();
599 buffer->ReadWriteUInt32(&count));
601 decoding_time.resize(count);
602 for (uint32_t i = 0; i < count; ++i) {
603 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
604 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
609 uint32_t DecodingTimeToSample::ComputeSizeInternal() {
611 sizeof(DecodingTime) * decoding_time.size();
614 CompositionTimeToSample::CompositionTimeToSample() {}
615 CompositionTimeToSample::~CompositionTimeToSample() {}
618 bool CompositionTimeToSample::ReadWriteInternal(
BoxBuffer* buffer) {
619 uint32_t count = composition_offset.size();
625 for (uint32_t i = 0; i < count; ++i) {
626 if (composition_offset[i].sample_offset < 0) {
634 buffer->ReadWriteUInt32(&count));
636 composition_offset.resize(count);
637 for (uint32_t i = 0; i < count; ++i) {
638 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
641 uint32_t sample_offset = composition_offset[i].sample_offset;
642 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
643 composition_offset[i].sample_offset = sample_offset;
645 int32_t sample_offset = composition_offset[i].sample_offset;
646 RCHECK(buffer->ReadWriteInt32(&sample_offset));
647 composition_offset[i].sample_offset = sample_offset;
653 uint32_t CompositionTimeToSample::ComputeSizeInternal() {
655 if (composition_offset.empty())
660 const uint32_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
662 kCompositionOffsetSize * composition_offset.size();
665 SampleToChunk::SampleToChunk() {}
666 SampleToChunk::~SampleToChunk() {}
669 bool SampleToChunk::ReadWriteInternal(
BoxBuffer* buffer) {
670 uint32_t count = chunk_info.size();
672 buffer->ReadWriteUInt32(&count));
674 chunk_info.resize(count);
675 for (uint32_t i = 0; i < count; ++i) {
676 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
677 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
678 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
680 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
681 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
686 uint32_t SampleToChunk::ComputeSizeInternal() {
688 sizeof(ChunkInfo) * chunk_info.size();
691 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
692 SampleSize::~SampleSize() {}
695 bool SampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
697 buffer->ReadWriteUInt32(&sample_size) &&
698 buffer->ReadWriteUInt32(&sample_count));
700 if (sample_size == 0) {
702 sizes.resize(sample_count);
704 DCHECK(sample_count == sizes.size());
705 for (uint32_t i = 0; i < sample_count; ++i)
706 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
711 uint32_t SampleSize::ComputeSizeInternal() {
712 return HeaderSize() +
sizeof(sample_size) +
sizeof(sample_count) +
713 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
716 CompactSampleSize::CompactSampleSize() : field_size(0) {}
717 CompactSampleSize::~CompactSampleSize() {}
720 bool CompactSampleSize::ReadWriteInternal(
BoxBuffer* buffer) {
721 uint32_t sample_count = sizes.size();
724 buffer->ReadWriteUInt8(&field_size) &&
725 buffer->ReadWriteUInt32(&sample_count));
728 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
729 switch (field_size) {
731 for (uint32_t i = 0; i < sample_count; i += 2) {
734 RCHECK(buffer->ReadWriteUInt8(&size));
735 sizes[i] = size >> 4;
736 sizes[i + 1] = size & 0x0F;
738 DCHECK_LT(sizes[i], 16u);
739 DCHECK_LT(sizes[i + 1], 16u);
740 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
741 RCHECK(buffer->ReadWriteUInt8(&size));
746 for (uint32_t i = 0; i < sample_count; ++i) {
747 uint8_t size = sizes[i];
748 RCHECK(buffer->ReadWriteUInt8(&size));
753 for (uint32_t i = 0; i < sample_count; ++i) {
754 uint16_t size = sizes[i];
755 RCHECK(buffer->ReadWriteUInt16(&size));
762 sizes.resize(sample_count);
766 uint32_t CompactSampleSize::ComputeSizeInternal() {
767 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) +
768 (field_size * sizes.size() + 7) / 8;
771 ChunkOffset::ChunkOffset() {}
772 ChunkOffset::~ChunkOffset() {}
775 bool ChunkOffset::ReadWriteInternal(
BoxBuffer* buffer) {
776 uint32_t count = offsets.size();
778 buffer->ReadWriteUInt32(&count));
780 offsets.resize(count);
781 for (uint32_t i = 0; i < count; ++i)
786 uint32_t ChunkOffset::ComputeSizeInternal() {
787 return HeaderSize() +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
790 ChunkLargeOffset::ChunkLargeOffset() {}
791 ChunkLargeOffset::~ChunkLargeOffset() {}
794 bool ChunkLargeOffset::ReadWriteInternal(
BoxBuffer* buffer) {
795 uint32_t count = offsets.size();
799 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
801 stco.offsets.swap(offsets);
804 stco.offsets.swap(offsets);
810 buffer->ReadWriteUInt32(&count));
812 offsets.resize(count);
813 for (uint32_t i = 0; i < count; ++i)
814 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
818 uint32_t ChunkLargeOffset::ComputeSizeInternal() {
819 uint32_t count = offsets.size();
820 int use_large_offset =
821 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
823 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
826 SyncSample::SyncSample() {}
827 SyncSample::~SyncSample() {}
830 bool SyncSample::ReadWriteInternal(
BoxBuffer* buffer) {
831 uint32_t count = sample_number.size();
833 buffer->ReadWriteUInt32(&count));
835 sample_number.resize(count);
836 for (uint32_t i = 0; i < count; ++i)
837 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
841 uint32_t SyncSample::ComputeSizeInternal() {
843 if (sample_number.empty())
846 sizeof(uint32_t) * sample_number.size();
849 SampleTable::SampleTable() {}
850 SampleTable::~SampleTable() {}
853 bool SampleTable::ReadWriteInternal(
BoxBuffer* buffer) {
869 CompactSampleSize compact_sample_size;
870 RCHECK(reader->
ReadChild(&compact_sample_size));
871 sample_size.sample_size = 0;
872 sample_size.sample_count = compact_sample_size.sizes.size();
873 sample_size.sizes.swap(compact_sample_size.sizes);
877 if (reader->
ChildExist(&chunk_large_offset)) {
878 RCHECK(reader->
ReadChild(&chunk_large_offset));
880 ChunkOffset chunk_offset;
881 RCHECK(reader->
ReadChild(&chunk_offset));
882 chunk_large_offset.offsets.swap(chunk_offset.offsets);
892 uint32_t SampleTable::ComputeSizeInternal() {
900 EditList::EditList() {}
901 EditList::~EditList() {}
904 bool EditList::ReadWriteInternal(
BoxBuffer* buffer) {
905 uint32_t count = edits.size();
909 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
910 for (uint32_t i = 0; i < count; ++i) {
913 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
914 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
915 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
920 uint32_t EditList::ComputeSizeInternal() {
926 for (uint32_t i = 0; i < edits.size(); ++i) {
927 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
933 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
941 bool Edit::ReadWriteInternal(
BoxBuffer* buffer) {
947 uint32_t Edit::ComputeSizeInternal() {
949 if (list.edits.empty())
954 HandlerReference::HandlerReference() : type(kInvalid) {}
955 HandlerReference::~HandlerReference() {}
958 bool HandlerReference::ReadWriteInternal(
BoxBuffer* buffer) {
959 FourCC hdlr_type = FOURCC_NULL;
960 std::vector<uint8_t> handler_name;
962 if (type == kVideo) {
963 hdlr_type = FOURCC_VIDE;
964 handler_name.assign(kVideoHandlerName,
965 kVideoHandlerName + arraysize(kVideoHandlerName));
966 }
else if (type == kAudio) {
967 hdlr_type = FOURCC_SOUN;
968 handler_name.assign(kAudioHandlerName,
969 kAudioHandlerName + arraysize(kAudioHandlerName));
970 }
else if (type == kText) {
971 hdlr_type = FOURCC_TEXT;
972 handler_name.assign(kTextHandlerName,
973 kTextHandlerName + arraysize(kTextHandlerName));
981 buffer->ReadWriteFourCC(&hdlr_type));
984 if (hdlr_type == FOURCC_VIDE) {
986 }
else if (hdlr_type == FOURCC_SOUN) {
993 buffer->ReadWriteVector(&handler_name, handler_name.size()));
998 uint32_t HandlerReference::ComputeSizeInternal() {
999 uint32_t box_size =
HeaderSize() + kFourCCSize + 16;
1000 if (type == kVideo) {
1001 box_size +=
sizeof(kVideoHandlerName);
1002 }
else if (type == kAudio) {
1003 box_size +=
sizeof(kAudioHandlerName);
1005 box_size +=
sizeof(kTextHandlerName);
1010 CodecConfigurationRecord::CodecConfigurationRecord() : box_type(FOURCC_NULL) {}
1011 CodecConfigurationRecord::~CodecConfigurationRecord() {}
1018 bool CodecConfigurationRecord::ReadWriteInternal(
BoxBuffer* buffer) {
1021 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1023 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1028 uint32_t CodecConfigurationRecord::ComputeSizeInternal() {
1034 PixelAspectRatio::PixelAspectRatio() : h_spacing(0), v_spacing(0) {}
1035 PixelAspectRatio::~PixelAspectRatio() {}
1038 bool PixelAspectRatio::ReadWriteInternal(
BoxBuffer* buffer) {
1040 buffer->ReadWriteUInt32(&h_spacing) &&
1041 buffer->ReadWriteUInt32(&v_spacing));
1045 uint32_t PixelAspectRatio::ComputeSizeInternal() {
1047 if (h_spacing == 0 && v_spacing == 0)
1050 DCHECK(h_spacing != 0 && v_spacing != 0);
1051 return HeaderSize() +
sizeof(h_spacing) +
sizeof(v_spacing);
1054 VideoSampleEntry::VideoSampleEntry()
1055 : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
1057 VideoSampleEntry::~VideoSampleEntry() {}
1059 if (format == FOURCC_NULL) {
1060 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
1061 <<
"handler type recovered in its Media ancestor.";
1066 bool VideoSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1067 std::vector<uint8_t> compressor_name;
1069 DCHECK(buffer->
reader());
1070 format = buffer->
reader()->type();
1074 const FourCC actual_format = GetActualFormat();
1075 switch (actual_format) {
1077 compressor_name.assign(
1079 kAvcCompressorName + arraysize(kAvcCompressorName));
1083 compressor_name.assign(
1084 kHevcCompressorName,
1085 kHevcCompressorName + arraysize(kHevcCompressorName));
1090 compressor_name.assign(
1092 kVpcCompressorName + arraysize(kVpcCompressorName));
1095 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1098 compressor_name.resize(kCompressorNameSize);
1101 uint32_t video_resolution = kVideoResolution;
1102 uint16_t video_frame_count = kVideoFrameCount;
1103 uint16_t video_depth = kVideoDepth;
1104 int16_t predefined = -1;
1106 buffer->ReadWriteUInt16(&data_reference_index) &&
1108 buffer->ReadWriteUInt16(&width) &&
1109 buffer->ReadWriteUInt16(&height) &&
1110 buffer->ReadWriteUInt32(&video_resolution) &&
1111 buffer->ReadWriteUInt32(&video_resolution) &&
1113 buffer->ReadWriteUInt16(&video_frame_count) &&
1114 buffer->ReadWriteVector(&compressor_name, kCompressorNameSize) &&
1115 buffer->ReadWriteUInt16(&video_depth) &&
1116 buffer->ReadWriteInt16(&predefined));
1120 if (format == FOURCC_ENCV) {
1124 while (sinf.type.type != FOURCC_CENC) {
1133 const FourCC actual_format = GetActualFormat();
1134 switch (actual_format) {
1136 codec_config_record.box_type = FOURCC_AVCC;
1140 codec_config_record.box_type = FOURCC_HVCC;
1145 codec_config_record.box_type = FOURCC_VPCC;
1148 LOG(ERROR) << FourCCToString(actual_format) <<
" is not supported.";
1156 uint32_t VideoSampleEntry::ComputeSizeInternal() {
1157 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(width) +
1158 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1159 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1161 codec_config_record.
ComputeSize() + kCompressorNameSize + 6 + 4 + 16 +
1165 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1166 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1169 bool ElementaryStreamDescriptor::ReadWriteInternal(
BoxBuffer* buffer) {
1172 std::vector<uint8_t> data;
1173 RCHECK(buffer->ReadWriteVector(&data, buffer->
BytesLeft()));
1174 RCHECK(es_descriptor.Parse(data));
1175 if (es_descriptor.
IsAAC()) {
1176 RCHECK(aac_audio_specific_config.
Parse(
1177 es_descriptor.decoder_specific_info()));
1180 DCHECK(buffer->
writer());
1181 es_descriptor.Write(buffer->
writer());
1186 uint32_t ElementaryStreamDescriptor::ComputeSizeInternal() {
1188 if (es_descriptor.object_type() == kForbidden)
1190 return HeaderSize() + es_descriptor.ComputeSize();
1193 DTSSpecific::DTSSpecific() {}
1194 DTSSpecific::~DTSSpecific() {}
1197 bool DTSSpecific::ReadWriteInternal(
BoxBuffer* buffer) {
1202 buffer->ReadWriteVector(&data, buffer->
Size() - buffer->
Pos()));
1204 RCHECK(buffer->ReadWriteVector(&data, data.size()));
1209 uint32_t DTSSpecific::ComputeSizeInternal() {
1211 if (data.size() == 0)
1216 AudioSampleEntry::AudioSampleEntry()
1217 : format(FOURCC_NULL),
1218 data_reference_index(1),
1223 AudioSampleEntry::~AudioSampleEntry() {}
1226 if (format == FOURCC_NULL) {
1227 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1228 <<
"handler type recovered in its Media ancestor.";
1233 bool AudioSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1235 DCHECK(buffer->
reader());
1236 format = buffer->
reader()->type();
1244 buffer->ReadWriteUInt16(&data_reference_index) &&
1246 buffer->ReadWriteUInt16(&channelcount) &&
1247 buffer->ReadWriteUInt16(&samplesize) &&
1249 buffer->ReadWriteUInt32(&samplerate));
1254 if (format == FOURCC_ENCA) {
1258 while (sinf.type.type != FOURCC_CENC) {
1273 uint32_t AudioSampleEntry::ComputeSizeInternal() {
1274 return HeaderSize() +
sizeof(data_reference_index) +
sizeof(channelcount) +
1275 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1281 WebVTTConfigurationBox::WebVTTConfigurationBox() {}
1282 WebVTTConfigurationBox::~WebVTTConfigurationBox() {}
1288 bool WebVTTConfigurationBox::ReadWriteInternal(
BoxBuffer* buffer) {
1295 uint32_t WebVTTConfigurationBox::ComputeSizeInternal() {
1299 WebVTTSourceLabelBox::WebVTTSourceLabelBox() {}
1300 WebVTTSourceLabelBox::~WebVTTSourceLabelBox() {}
1306 bool WebVTTSourceLabelBox::ReadWriteInternal(
BoxBuffer* buffer) {
1310 : source_label.size());
1313 uint32_t WebVTTSourceLabelBox::ComputeSizeInternal() {
1314 if (source_label.empty())
1319 WVTTSampleEntry::WVTTSampleEntry() {}
1320 WVTTSampleEntry::~WVTTSampleEntry() {}
1326 bool WVTTSampleEntry::ReadWriteInternal(
BoxBuffer* buffer) {
1330 buffer->ReadWriteUInt16(&data_reference_index) &&
1337 uint32_t WVTTSampleEntry::ComputeSizeInternal() {
1339 return HeaderSize() + 6 +
sizeof(data_reference_index) +
1343 MediaHeader::MediaHeader()
1344 : creation_time(0), modification_time(0), timescale(0), duration(0) {
1347 MediaHeader::~MediaHeader() {}
1350 bool MediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1353 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1356 buffer->ReadWriteUInt32(×cale) &&
1362 std::vector<uint8_t> temp;
1363 RCHECK(buffer->ReadWriteVector(&temp, 2));
1366 bit_reader.SkipBits(1);
1367 for (
int i = 0; i < 3; ++i) {
1368 CHECK(bit_reader.ReadBits(5, &language[i]));
1369 language[i] += 0x60;
1374 const char kUndefinedLanguage[] =
"und";
1375 if (language[0] == 0)
1376 strcpy(language, kUndefinedLanguage);
1380 for (
int i = 0; i < 3; ++i)
1381 lang |= (language[i] - 0x60) << ((2 - i) * 5);
1382 RCHECK(buffer->ReadWriteUInt16(&lang));
1389 uint32_t MediaHeader::ComputeSizeInternal() {
1390 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1392 sizeof(uint32_t) * (1 + version) * 3 + 2 +
1396 VideoMediaHeader::VideoMediaHeader()
1397 : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1398 const uint32_t kVideoMediaHeaderFlags = 1;
1399 flags = kVideoMediaHeaderFlags;
1401 VideoMediaHeader::~VideoMediaHeader() {}
1403 bool VideoMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1405 buffer->ReadWriteUInt16(&graphicsmode) &&
1406 buffer->ReadWriteUInt16(&opcolor_red) &&
1407 buffer->ReadWriteUInt16(&opcolor_green) &&
1408 buffer->ReadWriteUInt16(&opcolor_blue));
1412 uint32_t VideoMediaHeader::ComputeSizeInternal() {
1413 return HeaderSize() +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1414 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1417 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1418 SoundMediaHeader::~SoundMediaHeader() {}
1420 bool SoundMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1422 buffer->ReadWriteUInt16(&balance) &&
1427 uint32_t SoundMediaHeader::ComputeSizeInternal() {
1428 return HeaderSize() +
sizeof(balance) +
sizeof(uint16_t);
1431 SubtitleMediaHeader::SubtitleMediaHeader() {}
1432 SubtitleMediaHeader::~SubtitleMediaHeader() {}
1436 bool SubtitleMediaHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1440 uint32_t SubtitleMediaHeader::ComputeSizeInternal() {
1444 DataEntryUrl::DataEntryUrl() {
1445 const uint32_t kDataEntryUrlFlags = 1;
1446 flags = kDataEntryUrlFlags;
1448 DataEntryUrl::~DataEntryUrl() {}
1450 bool DataEntryUrl::ReadWriteInternal(
BoxBuffer* buffer) {
1453 RCHECK(buffer->ReadWriteVector(&location, buffer->
Size() - buffer->
Pos()));
1455 RCHECK(buffer->ReadWriteVector(&location, location.size()));
1460 uint32_t DataEntryUrl::ComputeSizeInternal() {
1464 DataReference::DataReference() {
1466 data_entry.resize(1);
1468 DataReference::~DataReference() {}
1470 bool DataReference::ReadWriteInternal(
BoxBuffer* buffer) {
1471 uint32_t entry_count = data_entry.size();
1473 buffer->ReadWriteUInt32(&entry_count));
1474 data_entry.resize(entry_count);
1476 for (uint32_t i = 0; i < entry_count; ++i)
1481 uint32_t DataReference::ComputeSizeInternal() {
1482 uint32_t count = data_entry.size();
1483 uint32_t box_size =
HeaderSize() +
sizeof(count);
1484 for (uint32_t i = 0; i < count; ++i)
1489 DataInformation::DataInformation() {}
1490 DataInformation::~DataInformation() {}
1493 bool DataInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1499 uint32_t DataInformation::ComputeSizeInternal() {
1503 MediaInformation::MediaInformation() {}
1504 MediaInformation::~MediaInformation() {}
1507 bool MediaInformation::ReadWriteInternal(
BoxBuffer* buffer) {
1512 switch (sample_table.description.type) {
1529 uint32_t MediaInformation::ComputeSizeInternal() {
1532 switch (sample_table.description.type) {
1552 bool Media::ReadWriteInternal(
BoxBuffer* buffer) {
1564 information.sample_table.description.type = handler.type;
1566 DCHECK_EQ(information.sample_table.description.type, handler.type);
1572 uint32_t Media::ComputeSizeInternal() {
1581 bool Track::ReadWriteInternal(
BoxBuffer* buffer) {
1591 uint32_t Track::ComputeSizeInternal() {
1596 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
1597 MovieExtendsHeader::~MovieExtendsHeader() {}
1600 bool MovieExtendsHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1602 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1607 uint32_t MovieExtendsHeader::ComputeSizeInternal() {
1609 if (fragment_duration == 0)
1611 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
1612 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1615 TrackExtends::TrackExtends()
1617 default_sample_description_index(0),
1618 default_sample_duration(0),
1619 default_sample_size(0),
1620 default_sample_flags(0) {}
1621 TrackExtends::~TrackExtends() {}
1624 bool TrackExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1626 buffer->ReadWriteUInt32(&track_id) &&
1627 buffer->ReadWriteUInt32(&default_sample_description_index) &&
1628 buffer->ReadWriteUInt32(&default_sample_duration) &&
1629 buffer->ReadWriteUInt32(&default_sample_size) &&
1630 buffer->ReadWriteUInt32(&default_sample_flags));
1634 uint32_t TrackExtends::ComputeSizeInternal() {
1636 sizeof(default_sample_description_index) +
1637 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
1638 sizeof(default_sample_flags);
1641 MovieExtends::MovieExtends() {}
1642 MovieExtends::~MovieExtends() {}
1645 bool MovieExtends::ReadWriteInternal(
BoxBuffer* buffer) {
1650 DCHECK(buffer->
reader());
1653 for (uint32_t i = 0; i < tracks.size(); ++i)
1659 uint32_t MovieExtends::ComputeSizeInternal() {
1661 if (tracks.size() == 0)
1664 for (uint32_t i = 0; i < tracks.size(); ++i)
1673 bool Movie::ReadWriteInternal(
BoxBuffer* buffer) {
1684 for (uint32_t i = 0; i < tracks.size(); ++i)
1686 for (uint32_t i = 0; i < pssh.size(); ++i)
1692 uint32_t Movie::ComputeSizeInternal() {
1695 for (uint32_t i = 0; i < tracks.size(); ++i)
1697 for (uint32_t i = 0; i < pssh.size(); ++i)
1702 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
1703 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
1706 bool TrackFragmentDecodeTime::ReadWriteInternal(
BoxBuffer* buffer) {
1708 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1713 uint32_t TrackFragmentDecodeTime::ComputeSizeInternal() {
1714 version = IsFitIn32Bits(decode_time) ? 0 : 1;
1715 return HeaderSize() +
sizeof(uint32_t) * (1 + version);
1718 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
1719 MovieFragmentHeader::~MovieFragmentHeader() {}
1722 bool MovieFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1724 buffer->ReadWriteUInt32(&sequence_number);
1727 uint32_t MovieFragmentHeader::ComputeSizeInternal() {
1728 return HeaderSize() +
sizeof(sequence_number);
1731 TrackFragmentHeader::TrackFragmentHeader()
1733 sample_description_index(0),
1734 default_sample_duration(0),
1735 default_sample_size(0),
1736 default_sample_flags(0) {}
1738 TrackFragmentHeader::~TrackFragmentHeader() {}
1741 bool TrackFragmentHeader::ReadWriteInternal(
BoxBuffer* buffer) {
1743 buffer->ReadWriteUInt32(&track_id));
1745 if (flags & kBaseDataOffsetPresentMask) {
1750 uint64_t base_data_offset;
1751 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
1752 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
1753 "default-base-is-moof.";
1756 if (flags & kSampleDescriptionIndexPresentMask) {
1757 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
1758 }
else if (buffer->
Reading()) {
1759 sample_description_index = 0;
1762 if (flags & kDefaultSampleDurationPresentMask) {
1763 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
1764 }
else if (buffer->
Reading()) {
1765 default_sample_duration = 0;
1768 if (flags & kDefaultSampleSizePresentMask) {
1769 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
1770 }
else if (buffer->
Reading()) {
1771 default_sample_size = 0;
1774 if (flags & kDefaultSampleFlagsPresentMask)
1775 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
1779 uint32_t TrackFragmentHeader::ComputeSizeInternal() {
1780 uint32_t box_size =
HeaderSize() +
sizeof(track_id);
1781 if (flags & kSampleDescriptionIndexPresentMask)
1782 box_size +=
sizeof(sample_description_index);
1783 if (flags & kDefaultSampleDurationPresentMask)
1784 box_size +=
sizeof(default_sample_duration);
1785 if (flags & kDefaultSampleSizePresentMask)
1786 box_size +=
sizeof(default_sample_size);
1787 if (flags & kDefaultSampleFlagsPresentMask)
1788 box_size +=
sizeof(default_sample_flags);
1792 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
1793 TrackFragmentRun::~TrackFragmentRun() {}
1796 bool TrackFragmentRun::ReadWriteInternal(
BoxBuffer* buffer) {
1802 if (flags & kSampleCompTimeOffsetsPresentMask) {
1803 for (uint32_t i = 0; i < sample_count; ++i) {
1804 if (sample_composition_time_offsets[i] < 0) {
1813 buffer->ReadWriteUInt32(&sample_count));
1815 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
1816 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
1817 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
1818 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
1819 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
1820 bool sample_composition_time_offsets_present =
1821 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
1823 if (data_offset_present) {
1824 RCHECK(buffer->ReadWriteUInt32(&data_offset));
1835 uint32_t first_sample_flags;
1838 if (first_sample_flags_present)
1839 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
1841 if (sample_duration_present)
1842 sample_durations.resize(sample_count);
1843 if (sample_size_present)
1844 sample_sizes.resize(sample_count);
1845 if (sample_flags_present)
1846 sample_flags.resize(sample_count);
1847 if (sample_composition_time_offsets_present)
1848 sample_composition_time_offsets.resize(sample_count);
1850 if (first_sample_flags_present) {
1851 first_sample_flags = sample_flags[0];
1852 DCHECK(sample_flags.size() == 1);
1853 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
1856 if (sample_duration_present)
1857 DCHECK(sample_durations.size() == sample_count);
1858 if (sample_size_present)
1859 DCHECK(sample_sizes.size() == sample_count);
1860 if (sample_flags_present)
1861 DCHECK(sample_flags.size() == sample_count);
1862 if (sample_composition_time_offsets_present)
1863 DCHECK(sample_composition_time_offsets.size() == sample_count);
1866 for (uint32_t i = 0; i < sample_count; ++i) {
1867 if (sample_duration_present)
1868 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
1869 if (sample_size_present)
1870 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
1871 if (sample_flags_present)
1872 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
1874 if (sample_composition_time_offsets_present) {
1876 uint32_t sample_offset = sample_composition_time_offsets[i];
1877 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
1878 sample_composition_time_offsets[i] = sample_offset;
1880 int32_t sample_offset = sample_composition_time_offsets[i];
1881 RCHECK(buffer->ReadWriteInt32(&sample_offset));
1882 sample_composition_time_offsets[i] = sample_offset;
1888 if (first_sample_flags_present) {
1889 if (sample_flags.size() == 0) {
1890 sample_flags.push_back(first_sample_flags);
1892 sample_flags[0] = first_sample_flags;
1899 uint32_t TrackFragmentRun::ComputeSizeInternal() {
1900 uint32_t box_size =
HeaderSize() +
sizeof(sample_count);
1901 if (flags & kDataOffsetPresentMask)
1902 box_size +=
sizeof(data_offset);
1903 if (flags & kFirstSampleFlagsPresentMask)
1904 box_size +=
sizeof(uint32_t);
1905 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
1906 (flags & kSampleSizePresentMask ? 1 : 0) +
1907 (flags & kSampleFlagsPresentMask ? 1 : 0) +
1908 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
1909 box_size += fields *
sizeof(uint32_t) * sample_count;
1913 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
1914 SampleToGroup::~SampleToGroup() {}
1917 bool SampleToGroup::ReadWriteInternal(
BoxBuffer* buffer) {
1919 buffer->ReadWriteUInt32(&grouping_type));
1921 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
1923 if (grouping_type != FOURCC_SEIG) {
1925 DLOG(WARNING) <<
"Sample group "
1926 << FourCCToString(static_cast<FourCC>(grouping_type))
1927 <<
" is not supported.";
1931 uint32_t count = entries.size();
1932 RCHECK(buffer->ReadWriteUInt32(&count));
1933 entries.resize(count);
1934 for (uint32_t i = 0; i < count; ++i) {
1935 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
1936 buffer->ReadWriteUInt32(&entries[i].group_description_index));
1941 uint32_t SampleToGroup::ComputeSizeInternal() {
1943 if (entries.empty())
1945 return HeaderSize() +
sizeof(grouping_type) +
1946 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
1947 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
1950 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
1951 : is_encrypted(false), iv_size(0) {
1953 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
1955 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
1956 SampleGroupDescription::~SampleGroupDescription() {}
1959 bool SampleGroupDescription::ReadWriteInternal(
BoxBuffer* buffer) {
1961 buffer->ReadWriteUInt32(&grouping_type));
1963 if (grouping_type != FOURCC_SEIG) {
1965 DLOG(WARNING) <<
"Sample group '" << grouping_type <<
"' is not supported.";
1969 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
1970 uint32_t default_length = 0;
1973 RCHECK(buffer->ReadWriteUInt32(&default_length));
1974 RCHECK(default_length == 0 || default_length >= kEntrySize);
1976 default_length = kEntrySize;
1977 RCHECK(buffer->ReadWriteUInt32(&default_length));
1981 uint32_t count = entries.size();
1982 RCHECK(buffer->ReadWriteUInt32(&count));
1983 entries.resize(count);
1984 for (uint32_t i = 0; i < count; ++i) {
1986 if (buffer->
Reading() && default_length == 0) {
1987 uint32_t description_length = 0;
1988 RCHECK(buffer->ReadWriteUInt32(&description_length));
1989 RCHECK(description_length >= kEntrySize);
1994 if (entries[i].key_id.size() != kCencKeyIdSize) {
1995 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
1996 <<
" bytes; got " << entries[i].key_id.size()
1997 <<
". Resized accordingly.";
1998 entries[i].key_id.resize(kCencKeyIdSize);
2002 uint8_t flag = entries[i].is_encrypted ? 1 : 0;
2004 buffer->ReadWriteUInt8(&flag) &&
2005 buffer->ReadWriteUInt8(&entries[i].iv_size) &&
2006 buffer->ReadWriteVector(&entries[i].key_id, kCencKeyIdSize));
2009 entries[i].is_encrypted = (flag != 0);
2010 if (entries[i].is_encrypted) {
2011 RCHECK(entries[i].iv_size == 8 || entries[i].iv_size == 16);
2013 RCHECK(entries[i].iv_size == 0);
2020 uint32_t SampleGroupDescription::ComputeSizeInternal() {
2024 if (entries.empty())
2026 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
2027 return HeaderSize() +
sizeof(grouping_type) +
2028 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
2029 entries.size() * kEntrySize;
2032 TrackFragment::TrackFragment() : decode_time_absent(false) {}
2033 TrackFragment::~TrackFragment() {}
2036 bool TrackFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2041 DCHECK(buffer->
reader());
2043 if (!decode_time_absent)
2051 while (sample_to_group.grouping_type != FOURCC_SEIG &&
2055 while (sample_group_description.grouping_type != FOURCC_SEIG &&
2060 if (!decode_time_absent)
2062 for (uint32_t i = 0; i < runs.size(); ++i)
2072 uint32_t TrackFragment::ComputeSizeInternal() {
2078 for (uint32_t i = 0; i < runs.size(); ++i)
2083 MovieFragment::MovieFragment() {}
2084 MovieFragment::~MovieFragment() {}
2087 bool MovieFragment::ReadWriteInternal(
BoxBuffer* buffer) {
2097 for (uint32_t i = 0; i < tracks.size(); ++i)
2099 for (uint32_t i = 0; i < pssh.size(); ++i)
2105 uint32_t MovieFragment::ComputeSizeInternal() {
2107 for (uint32_t i = 0; i < tracks.size(); ++i)
2109 for (uint32_t i = 0; i < pssh.size(); ++i)
2114 SegmentIndex::SegmentIndex()
2117 earliest_presentation_time(0),
2119 SegmentIndex::~SegmentIndex() {}
2122 bool SegmentIndex::ReadWriteInternal(
BoxBuffer* buffer) {
2124 buffer->ReadWriteUInt32(&reference_id) &&
2125 buffer->ReadWriteUInt32(×cale));
2127 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
2132 uint16_t reference_count = references.size();
2134 buffer->ReadWriteUInt16(&reference_count));
2135 references.resize(reference_count);
2137 uint32_t reference_type_size;
2139 for (uint32_t i = 0; i < reference_count; ++i) {
2141 reference_type_size = references[i].referenced_size;
2142 if (references[i].reference_type)
2143 reference_type_size |= (1 << 31);
2144 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
2145 if (references[i].starts_with_sap)
2148 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
2149 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
2150 buffer->ReadWriteUInt32(&sap));
2152 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
2153 references[i].referenced_size = reference_type_size & ~(1 << 31);
2154 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
2155 references[i].sap_type =
2156 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
2157 references[i].sap_delta_time = sap & ~(0xF << 28);
2163 uint32_t SegmentIndex::ComputeSizeInternal() {
2164 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
2165 return HeaderSize() +
sizeof(reference_id) +
sizeof(timescale) +
2166 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
2167 3 *
sizeof(uint32_t) * references.size();
2170 MediaData::MediaData() : data_size(0) {}
2171 MediaData::~MediaData() {}
2174 bool MediaData::ReadWriteInternal(
BoxBuffer* buffer) {
2175 NOTIMPLEMENTED() <<
"Actual data is parsed and written separately.";
2179 uint32_t MediaData::ComputeSizeInternal() {
2183 CueSourceIDBox::CueSourceIDBox() : source_id(kCueSourceIdNotSet) {}
2184 CueSourceIDBox::~CueSourceIDBox() {}
2188 bool CueSourceIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2193 uint32_t CueSourceIDBox::ComputeSizeInternal() {
2194 if (source_id == kCueSourceIdNotSet)
2199 CueTimeBox::CueTimeBox() {}
2200 CueTimeBox::~CueTimeBox() {}
2206 bool CueTimeBox::ReadWriteInternal(
BoxBuffer* buffer) {
2213 uint32_t CueTimeBox::ComputeSizeInternal() {
2214 if (cue_current_time.empty())
2216 return HeaderSize() + cue_current_time.size();
2219 CueIDBox::CueIDBox() {}
2220 CueIDBox::~CueIDBox() {}
2226 bool CueIDBox::ReadWriteInternal(
BoxBuffer* buffer) {
2232 uint32_t CueIDBox::ComputeSizeInternal() {
2238 CueSettingsBox::CueSettingsBox() {}
2239 CueSettingsBox::~CueSettingsBox() {}
2245 bool CueSettingsBox::ReadWriteInternal(
BoxBuffer* buffer) {
2251 uint32_t CueSettingsBox::ComputeSizeInternal() {
2252 if (settings.empty())
2257 CuePayloadBox::CuePayloadBox() {}
2258 CuePayloadBox::~CuePayloadBox() {}
2264 bool CuePayloadBox::ReadWriteInternal(
BoxBuffer* buffer) {
2270 uint32_t CuePayloadBox::ComputeSizeInternal() {
2274 VTTEmptyCueBox::VTTEmptyCueBox() {}
2275 VTTEmptyCueBox::~VTTEmptyCueBox() {}
2281 bool VTTEmptyCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2285 uint32_t VTTEmptyCueBox::ComputeSizeInternal() {
2289 VTTAdditionalTextBox::VTTAdditionalTextBox() {}
2290 VTTAdditionalTextBox::~VTTAdditionalTextBox() {}
2296 bool VTTAdditionalTextBox::ReadWriteInternal(
BoxBuffer* buffer) {
2299 &cue_additional_text,
2303 uint32_t VTTAdditionalTextBox::ComputeSizeInternal() {
2304 return HeaderSize() + cue_additional_text.size();
2307 VTTCueBox::VTTCueBox() {}
2308 VTTCueBox::~VTTCueBox() {}
2314 bool VTTCueBox::ReadWriteInternal(
BoxBuffer* buffer) {
2325 uint32_t VTTCueBox::ComputeSizeInternal() {
uint32_t ComputeSize() const
FourCC BoxType() const override
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)
bool ReadWrite(uint8_t iv_size, bool has_subsamples, BoxBuffer *buffer)
uint32_t GetTotalSizeOfSubsamples() const