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;
17 const uint32_t kBoxSize = kFourCCSize +
sizeof(uint32_t);
19 const uint32_t kFullBoxSize = kBoxSize + 4;
22 const uint32_t kCencKeyIdSize = 16;
25 const uint8_t kUnityMatrix[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
27 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0};
30 const char kVideoHandlerName[] =
"VideoHandler";
31 const char kAudioHandlerName[] =
"SoundHandler";
34 const uint32_t kVideoResolution = 0x00480000;
35 const uint16_t kVideoFrameCount = 1;
36 const uint16_t kVideoDepth = 0x0018;
39 bool IsFitIn32Bits(uint64_t a) {
40 return a <= std::numeric_limits<uint32_t>::max();
43 bool IsFitIn32Bits(int64_t a) {
44 return a <= std::numeric_limits<int32_t>::max() &&
45 a >= std::numeric_limits<int32_t>::min();
48 template <
typename T1,
typename T2>
49 bool IsFitIn32Bits(T1 a1, T2 a2) {
50 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2);
53 template <
typename T1,
typename T2,
typename T3>
54 bool IsFitIn32Bits(T1 a1, T2 a2, T3 a3) {
55 return IsFitIn32Bits(a1) && IsFitIn32Bits(a2) && IsFitIn32Bits(a3);
60 namespace edash_packager {
64 FileType::FileType() : major_brand(FOURCC_NULL), minor_version(0) {}
65 FileType::~FileType() {}
66 FourCC FileType::BoxType()
const {
return FOURCC_FTYP; }
70 buffer->ReadWriteFourCC(&major_brand) &&
71 buffer->ReadWriteUInt32(&minor_version));
74 num_brands = (buffer->
Size() - buffer->
Pos()) /
sizeof(FourCC);
75 compatible_brands.resize(num_brands);
77 num_brands = compatible_brands.size();
79 for (
size_t i = 0; i < num_brands; ++i)
80 RCHECK(buffer->ReadWriteFourCC(&compatible_brands[i]));
85 atom_size = kBoxSize + kFourCCSize +
sizeof(minor_version) +
86 kFourCCSize * compatible_brands.size();
90 SegmentType::SegmentType() {}
91 SegmentType::~SegmentType() {}
92 FourCC SegmentType::BoxType()
const {
return FOURCC_STYP; }
102 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
103 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
104 FourCC ProtectionSystemSpecificHeader::BoxType()
const {
return FOURCC_PSSH; }
107 if (!buffer->
Reading() && !raw_box.empty()) {
109 buffer->
writer()->AppendVector(raw_box);
113 uint32_t size = data.size();
115 buffer->ReadWriteVector(&system_id, 16) &&
116 buffer->ReadWriteUInt32(&size) &&
117 buffer->ReadWriteVector(&data, size));
122 DCHECK(raw_box.empty());
125 raw_box.assign(reader->data(), reader->data() + reader->size());
131 if (!raw_box.empty()) {
135 kFullBoxSize + system_id.size() +
sizeof(uint32_t) + data.size();
140 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
141 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
142 FourCC SampleAuxiliaryInformationOffset::BoxType()
const {
return FOURCC_SAIO; }
149 uint32_t count = offsets.size();
150 RCHECK(buffer->ReadWriteUInt32(&count));
151 offsets.resize(count);
153 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
154 for (uint32_t i = 0; i < count; ++i)
162 if (offsets.size() != 0) {
163 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
164 atom_size = kFullBoxSize +
sizeof(uint32_t) + num_bytes * offsets.size();
169 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
170 : default_sample_info_size(0), sample_count(0) {}
171 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
172 FourCC SampleAuxiliaryInformationSize::BoxType()
const {
return FOURCC_SAIZ; }
179 RCHECK(buffer->ReadWriteUInt8(&default_sample_info_size) &&
180 buffer->ReadWriteUInt32(&sample_count));
181 if (default_sample_info_size == 0)
182 RCHECK(buffer->ReadWriteVector(&sample_info_sizes, sample_count));
189 if (sample_count != 0) {
190 atom_size = kFullBoxSize +
sizeof(default_sample_info_size) +
191 sizeof(sample_count) +
192 (default_sample_info_size == 0 ? sample_info_sizes.size() : 0);
197 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
198 OriginalFormat::~OriginalFormat() {}
199 FourCC OriginalFormat::BoxType()
const {
return FOURCC_FRMA; }
202 return Box::ReadWrite(buffer) && buffer->ReadWriteFourCC(&format);
210 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
211 SchemeType::~SchemeType() {}
212 FourCC SchemeType::BoxType()
const {
return FOURCC_SCHM; }
216 buffer->ReadWriteFourCC(&type) &&
217 buffer->ReadWriteUInt32(&version));
222 atom_size = kFullBoxSize + kFourCCSize +
sizeof(version);
226 TrackEncryption::TrackEncryption()
227 : is_encrypted(false), default_iv_size(0), default_kid(16, 0) {}
228 TrackEncryption::~TrackEncryption() {}
229 FourCC TrackEncryption::BoxType()
const {
return FOURCC_TENC; }
233 if (default_kid.size() != kCencKeyIdSize) {
234 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
235 <<
" bytes; got " << default_kid.size()
236 <<
". Resized accordingly.";
237 default_kid.resize(kCencKeyIdSize);
241 uint8_t flag = is_encrypted ? 1 : 0;
244 buffer->ReadWriteUInt8(&flag) &&
245 buffer->ReadWriteUInt8(&default_iv_size) &&
246 buffer->ReadWriteVector(&default_kid, kCencKeyIdSize));
248 is_encrypted = (flag != 0);
250 RCHECK(default_iv_size == 8 || default_iv_size == 16);
252 RCHECK(default_iv_size == 0);
259 atom_size = kFullBoxSize +
sizeof(uint32_t) + kCencKeyIdSize;
263 SchemeInfo::SchemeInfo() {}
264 SchemeInfo::~SchemeInfo() {}
265 FourCC SchemeInfo::BoxType()
const {
return FOURCC_SCHI; }
278 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
279 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
280 FourCC ProtectionSchemeInfo::BoxType()
const {
return FOURCC_SINF; }
287 if (type.type == FOURCC_CENC)
299 if (format.format != FOURCC_NULL) {
306 MovieHeader::MovieHeader()
308 modification_time(0),
314 MovieHeader::~MovieHeader() {}
315 FourCC MovieHeader::BoxType()
const {
return FOURCC_MVHD; }
320 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
323 buffer->ReadWriteUInt32(×cale) &&
326 std::vector<uint8_t> matrix(kUnityMatrix,
327 kUnityMatrix + arraysize(kUnityMatrix));
328 RCHECK(buffer->ReadWriteInt32(&rate) &&
329 buffer->ReadWriteInt16(&volume) &&
331 buffer->ReadWriteVector(&matrix, matrix.size()) &&
333 buffer->ReadWriteUInt32(&next_track_id));
338 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
339 atom_size = kFullBoxSize +
sizeof(uint32_t) * (1 + version) * 3 +
340 sizeof(timescale) +
sizeof(rate) +
sizeof(volume) +
341 sizeof(next_track_id) +
sizeof(kUnityMatrix) + 10 +
346 TrackHeader::TrackHeader()
348 modification_time(0),
356 flags = kTrackEnabled | kTrackInMovie;
358 TrackHeader::~TrackHeader() {}
359 FourCC TrackHeader::BoxType()
const {
return FOURCC_TKHD; }
364 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
367 buffer->ReadWriteUInt32(&track_id) &&
374 volume = (width != 0 && height != 0) ? 0 : 0x100;
376 std::vector<uint8_t> matrix(kUnityMatrix,
377 kUnityMatrix + arraysize(kUnityMatrix));
379 buffer->ReadWriteInt16(&layer) &&
380 buffer->ReadWriteInt16(&alternate_group) &&
381 buffer->ReadWriteInt16(&volume) &&
383 buffer->ReadWriteVector(&matrix, matrix.size()) &&
384 buffer->ReadWriteUInt32(&width) &&
385 buffer->ReadWriteUInt32(&height));
390 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
391 atom_size = kFullBoxSize +
sizeof(track_id) +
392 sizeof(uint32_t) * (1 + version) * 3 +
sizeof(layer) +
393 sizeof(alternate_group) +
sizeof(volume) +
sizeof(width) +
394 sizeof(height) +
sizeof(kUnityMatrix) + 14;
398 SampleDescription::SampleDescription() : type(kInvalid) {}
399 SampleDescription::~SampleDescription() {}
400 FourCC SampleDescription::BoxType()
const {
return FOURCC_STSD; }
405 count = video_entries.size();
407 count = audio_entries.size();
409 buffer->ReadWriteUInt32(&count));
414 video_entries.clear();
415 audio_entries.clear();
418 if (type == kVideo) {
420 RCHECK(video_entries.size() == count);
421 }
else if (type == kAudio) {
423 RCHECK(audio_entries.size() == count);
426 DCHECK_LT(0u, count);
427 if (type == kVideo) {
428 for (uint32_t i = 0; i < count; ++i)
429 RCHECK(video_entries[i].
ReadWrite(buffer));
430 }
else if (type == kAudio) {
431 for (uint32_t i = 0; i < count; ++i)
432 RCHECK(audio_entries[i].
ReadWrite(buffer));
441 atom_size = kFullBoxSize +
sizeof(uint32_t);
442 if (type == kVideo) {
443 for (uint32_t i = 0; i < video_entries.size(); ++i)
445 }
else if (type == kAudio) {
446 for (uint32_t i = 0; i < audio_entries.size(); ++i)
452 DecodingTimeToSample::DecodingTimeToSample() {}
453 DecodingTimeToSample::~DecodingTimeToSample() {}
454 FourCC DecodingTimeToSample::BoxType()
const {
return FOURCC_STTS; }
457 uint32_t count = decoding_time.size();
459 buffer->ReadWriteUInt32(&count));
461 decoding_time.resize(count);
462 for (uint32_t i = 0; i < count; ++i) {
463 RCHECK(buffer->ReadWriteUInt32(&decoding_time[i].sample_count) &&
464 buffer->ReadWriteUInt32(&decoding_time[i].sample_delta));
470 atom_size = kFullBoxSize +
sizeof(uint32_t) +
475 CompositionTimeToSample::CompositionTimeToSample() {}
476 CompositionTimeToSample::~CompositionTimeToSample() {}
477 FourCC CompositionTimeToSample::BoxType()
const {
return FOURCC_CTTS; }
480 uint32_t count = composition_offset.size();
486 for (uint32_t i = 0; i < count; ++i) {
487 if (composition_offset[i].sample_offset < 0) {
495 buffer->ReadWriteUInt32(&count));
497 composition_offset.resize(count);
498 for (uint32_t i = 0; i < count; ++i) {
499 RCHECK(buffer->ReadWriteUInt32(&composition_offset[i].sample_count));
502 uint32_t sample_offset = composition_offset[i].sample_offset;
503 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
504 composition_offset[i].sample_offset = sample_offset;
506 int32_t sample_offset = composition_offset[i].sample_offset;
507 RCHECK(buffer->ReadWriteInt32(&sample_offset));
508 composition_offset[i].sample_offset = sample_offset;
517 if (!composition_offset.empty()) {
521 const uint32_t kCompositionOffsetSize =
sizeof(uint32_t) * 2;
522 atom_size = kFullBoxSize +
sizeof(uint32_t) +
523 kCompositionOffsetSize * composition_offset.size();
528 SampleToChunk::SampleToChunk() {}
529 SampleToChunk::~SampleToChunk() {}
530 FourCC SampleToChunk::BoxType()
const {
return FOURCC_STSC; }
533 uint32_t count = chunk_info.size();
535 buffer->ReadWriteUInt32(&count));
537 chunk_info.resize(count);
538 for (uint32_t i = 0; i < count; ++i) {
539 RCHECK(buffer->ReadWriteUInt32(&chunk_info[i].first_chunk) &&
540 buffer->ReadWriteUInt32(&chunk_info[i].samples_per_chunk) &&
541 buffer->ReadWriteUInt32(&chunk_info[i].sample_description_index));
543 RCHECK(i == 0 ? chunk_info[i].first_chunk == 1
544 : chunk_info[i].first_chunk > chunk_info[i - 1].first_chunk);
551 kFullBoxSize +
sizeof(uint32_t) +
sizeof(
ChunkInfo) * chunk_info.size();
555 SampleSize::SampleSize() : sample_size(0), sample_count(0) {}
556 SampleSize::~SampleSize() {}
557 FourCC SampleSize::BoxType()
const {
return FOURCC_STSZ; }
561 buffer->ReadWriteUInt32(&sample_size) &&
562 buffer->ReadWriteUInt32(&sample_count));
564 if (sample_size == 0) {
566 sizes.resize(sample_count);
568 DCHECK(sample_count == sizes.size());
569 for (uint32_t i = 0; i < sample_count; ++i)
570 RCHECK(buffer->ReadWriteUInt32(&sizes[i]));
576 atom_size = kFullBoxSize +
sizeof(sample_size) +
sizeof(sample_count) +
577 (sample_size == 0 ?
sizeof(uint32_t) * sizes.size() : 0);
581 CompactSampleSize::CompactSampleSize() : field_size(0) {}
582 CompactSampleSize::~CompactSampleSize() {}
583 FourCC CompactSampleSize::BoxType()
const {
return FOURCC_STZ2; }
586 uint32_t sample_count = sizes.size();
589 buffer->ReadWriteUInt8(&field_size) &&
590 buffer->ReadWriteUInt32(&sample_count));
593 sizes.resize(sample_count + (field_size == 4 ? 1 : 0), 0);
594 switch (field_size) {
596 for (uint32_t i = 0; i < sample_count; i += 2) {
599 RCHECK(buffer->ReadWriteUInt8(&size));
600 sizes[i] = size >> 4;
601 sizes[i + 1] = size & 0x0F;
603 DCHECK_LT(sizes[i], 16u);
604 DCHECK_LT(sizes[i + 1], 16u);
605 uint8_t size = (sizes[i] << 4) | sizes[i + 1];
606 RCHECK(buffer->ReadWriteUInt8(&size));
611 for (uint32_t i = 0; i < sample_count; ++i) {
612 uint8_t size = sizes[i];
613 RCHECK(buffer->ReadWriteUInt8(&size));
618 for (uint32_t i = 0; i < sample_count; ++i) {
619 uint16_t size = sizes[i];
620 RCHECK(buffer->ReadWriteUInt16(&size));
627 sizes.resize(sample_count);
632 atom_size = kFullBoxSize +
sizeof(uint32_t) +
sizeof(uint32_t) +
633 (field_size * sizes.size() + 7) / 8;
637 ChunkOffset::ChunkOffset() {}
638 ChunkOffset::~ChunkOffset() {}
639 FourCC ChunkOffset::BoxType()
const {
return FOURCC_STCO; }
642 uint32_t count = offsets.size();
644 buffer->ReadWriteUInt32(&count));
646 offsets.resize(count);
647 for (uint32_t i = 0; i < count; ++i)
654 kFullBoxSize +
sizeof(uint32_t) +
sizeof(uint32_t) * offsets.size();
658 ChunkLargeOffset::ChunkLargeOffset() {}
659 ChunkLargeOffset::~ChunkLargeOffset() {}
660 FourCC ChunkLargeOffset::BoxType()
const {
return FOURCC_CO64; }
663 uint32_t count = offsets.size();
667 if (count == 0 || IsFitIn32Bits(offsets[count - 1])) {
669 stco.offsets.swap(offsets);
672 stco.offsets.swap(offsets);
678 buffer->ReadWriteUInt32(&count));
680 offsets.resize(count);
681 for (uint32_t i = 0; i < count; ++i)
682 RCHECK(buffer->ReadWriteUInt64(&offsets[i]));
687 uint32_t count = offsets.size();
688 int use_large_offset =
689 (count > 0 && !IsFitIn32Bits(offsets[count - 1])) ? 1 : 0;
690 atom_size = kFullBoxSize +
sizeof(count) +
691 sizeof(uint32_t) * (1 + use_large_offset) * offsets.size();
695 SyncSample::SyncSample() {}
696 SyncSample::~SyncSample() {}
697 FourCC SyncSample::BoxType()
const {
return FOURCC_STSS; }
700 uint32_t count = sample_number.size();
702 buffer->ReadWriteUInt32(&count));
704 sample_number.resize(count);
705 for (uint32_t i = 0; i < count; ++i)
706 RCHECK(buffer->ReadWriteUInt32(&sample_number[i]));
713 if (!sample_number.empty()) {
714 atom_size = kFullBoxSize +
sizeof(uint32_t) +
715 sizeof(uint32_t) * sample_number.size();
720 SampleTable::SampleTable() {}
721 SampleTable::~SampleTable() {}
722 FourCC SampleTable::BoxType()
const {
return FOURCC_STBL; }
741 RCHECK(reader->
ReadChild(&compact_sample_size));
742 sample_size.sample_size = 0;
743 sample_size.sample_count = compact_sample_size.sizes.size();
744 sample_size.sizes.swap(compact_sample_size.sizes);
748 if (reader->
ChildExist(&chunk_large_offset)) {
749 RCHECK(reader->
ReadChild(&chunk_large_offset));
752 RCHECK(reader->
ReadChild(&chunk_offset));
753 chunk_large_offset.offsets.swap(chunk_offset.offsets);
772 EditList::EditList() {}
773 EditList::~EditList() {}
774 FourCC EditList::BoxType()
const {
return FOURCC_ELST; }
777 uint32_t count = edits.size();
781 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
782 for (uint32_t i = 0; i < count; ++i) {
785 buffer->ReadWriteInt64NBytes(&edits[i].media_time, num_bytes) &&
786 buffer->ReadWriteInt16(&edits[i].media_rate_integer) &&
787 buffer->ReadWriteInt16(&edits[i].media_rate_fraction));
799 for (uint32_t i = 0; i < edits.size(); ++i) {
800 if (!IsFitIn32Bits(edits[i].segment_duration, edits[i].media_time)) {
805 atom_size = kFullBoxSize +
sizeof(uint32_t) +
806 (
sizeof(uint32_t) * (1 + version) * 2 +
sizeof(int16_t) * 2) *
813 FourCC Edit::BoxType()
const {
return FOURCC_EDTS; }
824 if (!list.edits.empty())
829 HandlerReference::HandlerReference() : type(kInvalid) {}
830 HandlerReference::~HandlerReference() {}
831 FourCC HandlerReference::BoxType()
const {
return FOURCC_HDLR; }
834 FourCC hdlr_type = FOURCC_NULL;
835 std::vector<uint8_t> handler_name;
837 if (type == kVideo) {
838 hdlr_type = FOURCC_VIDE;
839 handler_name.assign(kVideoHandlerName,
840 kVideoHandlerName + arraysize(kVideoHandlerName));
841 }
else if (type == kAudio) {
842 hdlr_type = FOURCC_SOUN;
843 handler_name.assign(kAudioHandlerName,
844 kAudioHandlerName + arraysize(kAudioHandlerName));
852 buffer->ReadWriteFourCC(&hdlr_type));
855 if (hdlr_type == FOURCC_VIDE) {
857 }
else if (hdlr_type == FOURCC_SOUN) {
864 buffer->ReadWriteVector(&handler_name, handler_name.size()));
871 kFullBoxSize + kFourCCSize + 16 +
872 (type == kVideo ?
sizeof(kVideoHandlerName) :
sizeof(kAudioHandlerName));
876 AVCDecoderConfigurationRecord::AVCDecoderConfigurationRecord()
878 profile_indication(0),
879 profile_compatibility(0),
883 AVCDecoderConfigurationRecord::~AVCDecoderConfigurationRecord() {}
884 FourCC AVCDecoderConfigurationRecord::BoxType()
const {
return FOURCC_AVCC; }
889 RCHECK(buffer->ReadWriteVector(&data, buffer->
Size() - buffer->
Pos()));
891 return ParseData(&buffer_reader);
893 RCHECK(buffer->ReadWriteVector(&data, data.size()));
898 bool AVCDecoderConfigurationRecord::ParseData(
BufferReader* reader) {
899 RCHECK(reader->
Read1(&version) && version == 1 &&
900 reader->
Read1(&profile_indication) &&
901 reader->
Read1(&profile_compatibility) &&
902 reader->
Read1(&avc_level));
904 uint8_t length_size_minus_one;
905 RCHECK(reader->
Read1(&length_size_minus_one));
906 length_size = (length_size_minus_one & 0x3) + 1;
909 RCHECK(reader->
Read1(&num_sps));
912 sps_list.resize(num_sps);
913 for (
int i = 0; i < num_sps; i++) {
915 RCHECK(reader->Read2(&sps_length) &&
916 reader->ReadToVector(&sps_list[i], sps_length));
920 RCHECK(reader->
Read1(&num_pps));
922 pps_list.resize(num_pps);
923 for (
int i = 0; i < num_pps; i++) {
925 RCHECK(reader->Read2(&pps_length) &&
926 reader->ReadToVector(&pps_list[i], pps_length));
939 PixelAspectRatioBox::PixelAspectRatioBox() : h_spacing(0), v_spacing(0) {}
940 PixelAspectRatioBox::~PixelAspectRatioBox() {}
941 FourCC PixelAspectRatioBox::BoxType()
const {
return FOURCC_PASP; }
945 buffer->ReadWriteUInt32(&h_spacing) &&
946 buffer->ReadWriteUInt32(&v_spacing));
953 if (h_spacing != 0 || v_spacing != 0) {
955 DCHECK(h_spacing != 0 && v_spacing != 0);
956 atom_size = kBoxSize +
sizeof(h_spacing) +
sizeof(v_spacing);
961 VideoSampleEntry::VideoSampleEntry()
962 : format(FOURCC_NULL), data_reference_index(1), width(0), height(0) {}
964 VideoSampleEntry::~VideoSampleEntry() {}
965 FourCC VideoSampleEntry::BoxType()
const {
966 LOG(ERROR) <<
"VideoSampleEntry should be parsed according to the "
967 <<
"handler type recovered in its Media ancestor.";
974 format = buffer->
reader()->type();
976 RCHECK(buffer->ReadWriteUInt32(&
atom_size) &&
977 buffer->ReadWriteFourCC(&format));
980 uint32_t video_resolution = kVideoResolution;
981 uint16_t video_frame_count = kVideoFrameCount;
982 uint16_t video_depth = kVideoDepth;
983 int16_t predefined = -1;
985 buffer->ReadWriteUInt16(&data_reference_index) &&
987 buffer->ReadWriteUInt16(&width) &&
988 buffer->ReadWriteUInt16(&height) &&
989 buffer->ReadWriteUInt32(&video_resolution) &&
990 buffer->ReadWriteUInt32(&video_resolution) &&
992 buffer->ReadWriteUInt16(&video_frame_count) &&
994 buffer->ReadWriteUInt16(&video_depth) &&
995 buffer->ReadWriteInt16(&predefined));
999 if (format == FOURCC_ENCV) {
1003 while (sinf.type.type != FOURCC_CENC) {
1012 if (format == FOURCC_AVC1 ||
1013 (format == FOURCC_ENCV && sinf.format.format == FOURCC_AVC1)) {
1021 atom_size = kBoxSize +
sizeof(data_reference_index) +
sizeof(width) +
1022 sizeof(height) +
sizeof(kVideoResolution) * 2 +
1023 sizeof(kVideoFrameCount) +
sizeof(kVideoDepth) +
1030 ElementaryStreamDescriptor::ElementaryStreamDescriptor() {}
1031 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
1032 FourCC ElementaryStreamDescriptor::BoxType()
const {
return FOURCC_ESDS; }
1037 std::vector<uint8_t> data;
1038 RCHECK(buffer->ReadWriteVector(&data, buffer->
Size() - buffer->
Pos()));
1039 RCHECK(es_descriptor.Parse(data));
1040 if (es_descriptor.
IsAAC()) {
1041 RCHECK(aac_audio_specific_config.
Parse(
1042 es_descriptor.decoder_specific_info()));
1045 DCHECK(buffer->
writer());
1046 es_descriptor.Write(buffer->
writer());
1054 if (es_descriptor.object_type() != kForbidden)
1055 atom_size = kFullBoxSize + es_descriptor.ComputeSize();
1059 AudioSampleEntry::AudioSampleEntry()
1060 : format(FOURCC_NULL),
1061 data_reference_index(1),
1066 AudioSampleEntry::~AudioSampleEntry() {}
1068 FourCC AudioSampleEntry::BoxType()
const {
1069 LOG(ERROR) <<
"AudioSampleEntry should be parsed according to the "
1070 <<
"handler type recovered in its Media ancestor.";
1076 DCHECK(buffer->
reader());
1077 format = buffer->
reader()->type();
1079 RCHECK(buffer->ReadWriteUInt32(&
atom_size) &&
1080 buffer->ReadWriteFourCC(&format));
1086 buffer->ReadWriteUInt16(&data_reference_index) &&
1088 buffer->ReadWriteUInt16(&channelcount) &&
1089 buffer->ReadWriteUInt16(&samplesize) &&
1091 buffer->ReadWriteUInt32(&samplerate));
1096 if (format == FOURCC_ENCA) {
1100 while (sinf.type.type != FOURCC_CENC) {
1115 atom_size = kBoxSize +
sizeof(data_reference_index) +
sizeof(channelcount) +
1116 sizeof(samplesize) +
sizeof(samplerate) + sinf.
ComputeSize() +
1122 MediaHeader::MediaHeader()
1123 : creation_time(0), modification_time(0), timescale(0), duration(0) {
1126 MediaHeader::~MediaHeader() {}
1127 FourCC MediaHeader::BoxType()
const {
return FOURCC_MDHD; }
1132 uint8_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1135 buffer->ReadWriteUInt32(×cale) &&
1141 std::vector<uint8_t> temp;
1142 RCHECK(buffer->ReadWriteVector(&temp, 2));
1145 bit_reader.SkipBits(1);
1146 for (
int i = 0; i < 3; ++i) {
1147 CHECK(bit_reader.ReadBits(5, &language[i]));
1148 language[i] += 0x60;
1153 const char kUndefinedLanguage[] =
"und";
1154 if (language[0] == 0)
1155 strcpy(language, kUndefinedLanguage);
1159 for (
int i = 0; i < 3; ++i)
1160 lang |= (language[i] - 0x60) << ((2 - i) * 5);
1161 RCHECK(buffer->ReadWriteUInt16(&lang));
1169 version = IsFitIn32Bits(creation_time, modification_time, duration) ? 0 : 1;
1170 atom_size = kFullBoxSize +
sizeof(timescale) +
1171 sizeof(uint32_t) * (1 + version) * 3 + 2 +
1176 VideoMediaHeader::VideoMediaHeader()
1177 : graphicsmode(0), opcolor_red(0), opcolor_green(0), opcolor_blue(0) {
1178 const uint32_t kVideoMediaHeaderFlags = 1;
1179 flags = kVideoMediaHeaderFlags;
1181 VideoMediaHeader::~VideoMediaHeader() {}
1182 FourCC VideoMediaHeader::BoxType()
const {
return FOURCC_VMHD; }
1185 buffer->ReadWriteUInt16(&graphicsmode) &&
1186 buffer->ReadWriteUInt16(&opcolor_red) &&
1187 buffer->ReadWriteUInt16(&opcolor_green) &&
1188 buffer->ReadWriteUInt16(&opcolor_blue));
1193 atom_size = kFullBoxSize +
sizeof(graphicsmode) +
sizeof(opcolor_red) +
1194 sizeof(opcolor_green) +
sizeof(opcolor_blue);
1198 SoundMediaHeader::SoundMediaHeader() : balance(0) {}
1199 SoundMediaHeader::~SoundMediaHeader() {}
1200 FourCC SoundMediaHeader::BoxType()
const {
return FOURCC_SMHD; }
1203 buffer->ReadWriteUInt16(&balance) &&
1209 atom_size = kFullBoxSize +
sizeof(balance) +
sizeof(uint16_t);
1213 DataEntryUrl::DataEntryUrl() {
1214 const uint32_t kDataEntryUrlFlags = 1;
1215 flags = kDataEntryUrlFlags;
1217 DataEntryUrl::~DataEntryUrl() {}
1218 FourCC DataEntryUrl::BoxType()
const {
return FOURCC_URL; }
1222 RCHECK(buffer->ReadWriteVector(&location, buffer->
Size() - buffer->
Pos()));
1224 RCHECK(buffer->ReadWriteVector(&location, location.size()));
1230 atom_size = kBoxSize +
sizeof(flags) + location.size();
1234 DataReference::DataReference() {
1236 data_entry.resize(1);
1238 DataReference::~DataReference() {}
1239 FourCC DataReference::BoxType()
const {
return FOURCC_DREF; }
1241 uint32_t entry_count = data_entry.size();
1243 buffer->ReadWriteUInt32(&entry_count));
1244 data_entry.resize(entry_count);
1246 for (uint32_t i = 0; i < entry_count; ++i)
1252 uint32_t count = data_entry.size();
1253 atom_size = kFullBoxSize +
sizeof(count);
1254 for (uint32_t i = 0; i < count; ++i)
1259 DataInformation::DataInformation() {}
1260 DataInformation::~DataInformation() {}
1261 FourCC DataInformation::BoxType()
const {
return FOURCC_DINF; }
1274 MediaInformation::MediaInformation() {}
1275 MediaInformation::~MediaInformation() {}
1276 FourCC MediaInformation::BoxType()
const {
return FOURCC_MINF; }
1283 if (sample_table.description.type == kVideo)
1285 else if (sample_table.description.type == kAudio)
1295 if (sample_table.description.type == kVideo)
1297 else if (sample_table.description.type == kAudio)
1304 FourCC Media::BoxType()
const {
return FOURCC_MDIA; }
1318 information.sample_table.description.type = handler.type;
1320 DCHECK_EQ(information.sample_table.description.type, handler.type);
1334 FourCC Track::BoxType()
const {
return FOURCC_TRAK; }
1351 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
1352 MovieExtendsHeader::~MovieExtendsHeader() {}
1353 FourCC MovieExtendsHeader::BoxType()
const {
return FOURCC_MEHD; }
1357 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1365 if (fragment_duration != 0) {
1366 version = IsFitIn32Bits(fragment_duration) ? 0 : 1;
1367 atom_size = kFullBoxSize +
sizeof(uint32_t) * (1 + version);
1372 TrackExtends::TrackExtends()
1374 default_sample_description_index(0),
1375 default_sample_duration(0),
1376 default_sample_size(0),
1377 default_sample_flags(0) {}
1378 TrackExtends::~TrackExtends() {}
1379 FourCC TrackExtends::BoxType()
const {
return FOURCC_TREX; }
1383 buffer->ReadWriteUInt32(&track_id) &&
1384 buffer->ReadWriteUInt32(&default_sample_description_index) &&
1385 buffer->ReadWriteUInt32(&default_sample_duration) &&
1386 buffer->ReadWriteUInt32(&default_sample_size) &&
1387 buffer->ReadWriteUInt32(&default_sample_flags));
1392 atom_size = kFullBoxSize +
sizeof(track_id) +
1393 sizeof(default_sample_description_index) +
1394 sizeof(default_sample_duration) +
sizeof(default_sample_size) +
1395 sizeof(default_sample_flags);
1399 MovieExtends::MovieExtends() {}
1400 MovieExtends::~MovieExtends() {}
1401 FourCC MovieExtends::BoxType()
const {
return FOURCC_MVEX; }
1408 DCHECK(buffer->
reader());
1411 for (uint32_t i = 0; i < tracks.size(); ++i)
1420 if (tracks.size() != 0) {
1422 for (uint32_t i = 0; i < tracks.size(); ++i)
1430 FourCC Movie::BoxType()
const {
return FOURCC_MOOV; }
1443 for (uint32_t i = 0; i < tracks.size(); ++i)
1445 for (uint32_t i = 0; i < pssh.size(); ++i)
1453 for (uint32_t i = 0; i < tracks.size(); ++i)
1455 for (uint32_t i = 0; i < pssh.size(); ++i)
1460 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
1461 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
1462 FourCC TrackFragmentDecodeTime::BoxType()
const {
return FOURCC_TFDT; }
1466 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1472 version = IsFitIn32Bits(decode_time) ? 0 : 1;
1473 atom_size = kFullBoxSize +
sizeof(uint32_t) * (1 + version);
1477 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
1478 MovieFragmentHeader::~MovieFragmentHeader() {}
1479 FourCC MovieFragmentHeader::BoxType()
const {
return FOURCC_MFHD; }
1483 buffer->ReadWriteUInt32(&sequence_number);
1487 atom_size = kFullBoxSize +
sizeof(sequence_number);
1491 TrackFragmentHeader::TrackFragmentHeader()
1493 sample_description_index(0),
1494 default_sample_duration(0),
1495 default_sample_size(0),
1496 default_sample_flags(0) {}
1498 TrackFragmentHeader::~TrackFragmentHeader() {}
1499 FourCC TrackFragmentHeader::BoxType()
const {
return FOURCC_TFHD; }
1503 buffer->ReadWriteUInt32(&track_id));
1505 if (flags & kBaseDataOffsetPresentMask) {
1510 uint64_t base_data_offset;
1511 RCHECK(buffer->ReadWriteUInt64(&base_data_offset));
1512 DLOG(WARNING) <<
"base-data-offset-present is not expected. Assumes "
1513 "default-base-is-moof.";
1516 if (flags & kSampleDescriptionIndexPresentMask) {
1517 RCHECK(buffer->ReadWriteUInt32(&sample_description_index));
1518 }
else if (buffer->
Reading()) {
1519 sample_description_index = 0;
1522 if (flags & kDefaultSampleDurationPresentMask) {
1523 RCHECK(buffer->ReadWriteUInt32(&default_sample_duration));
1524 }
else if (buffer->
Reading()) {
1525 default_sample_duration = 0;
1528 if (flags & kDefaultSampleSizePresentMask) {
1529 RCHECK(buffer->ReadWriteUInt32(&default_sample_size));
1530 }
else if (buffer->
Reading()) {
1531 default_sample_size = 0;
1534 if (flags & kDefaultSampleFlagsPresentMask)
1535 RCHECK(buffer->ReadWriteUInt32(&default_sample_flags));
1540 atom_size = kFullBoxSize +
sizeof(track_id);
1541 if (flags & kSampleDescriptionIndexPresentMask)
1542 atom_size +=
sizeof(sample_description_index);
1543 if (flags & kDefaultSampleDurationPresentMask)
1544 atom_size +=
sizeof(default_sample_duration);
1545 if (flags & kDefaultSampleSizePresentMask)
1546 atom_size +=
sizeof(default_sample_size);
1547 if (flags & kDefaultSampleFlagsPresentMask)
1548 atom_size +=
sizeof(default_sample_flags);
1552 TrackFragmentRun::TrackFragmentRun() : sample_count(0), data_offset(0) {}
1553 TrackFragmentRun::~TrackFragmentRun() {}
1554 FourCC TrackFragmentRun::BoxType()
const {
return FOURCC_TRUN; }
1562 if (flags & kSampleCompTimeOffsetsPresentMask) {
1563 for (uint32_t i = 0; i < sample_count; ++i) {
1564 if (sample_composition_time_offsets[i] < 0) {
1573 buffer->ReadWriteUInt32(&sample_count));
1575 bool data_offset_present = (flags & kDataOffsetPresentMask) != 0;
1576 bool first_sample_flags_present = (flags & kFirstSampleFlagsPresentMask) != 0;
1577 bool sample_duration_present = (flags & kSampleDurationPresentMask) != 0;
1578 bool sample_size_present = (flags & kSampleSizePresentMask) != 0;
1579 bool sample_flags_present = (flags & kSampleFlagsPresentMask) != 0;
1580 bool sample_composition_time_offsets_present =
1581 (flags & kSampleCompTimeOffsetsPresentMask) != 0;
1583 if (data_offset_present) {
1584 RCHECK(buffer->ReadWriteUInt32(&data_offset));
1595 uint32_t first_sample_flags;
1598 if (first_sample_flags_present)
1599 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
1601 if (sample_duration_present)
1602 sample_durations.resize(sample_count);
1603 if (sample_size_present)
1604 sample_sizes.resize(sample_count);
1605 if (sample_flags_present)
1606 sample_flags.resize(sample_count);
1607 if (sample_composition_time_offsets_present)
1608 sample_composition_time_offsets.resize(sample_count);
1610 if (first_sample_flags_present) {
1611 first_sample_flags = sample_flags[0];
1612 DCHECK(sample_flags.size() == 1);
1613 RCHECK(buffer->ReadWriteUInt32(&first_sample_flags));
1616 if (sample_duration_present)
1617 DCHECK(sample_durations.size() == sample_count);
1618 if (sample_size_present)
1619 DCHECK(sample_sizes.size() == sample_count);
1620 if (sample_flags_present)
1621 DCHECK(sample_flags.size() == sample_count);
1622 if (sample_composition_time_offsets_present)
1623 DCHECK(sample_composition_time_offsets.size() == sample_count);
1626 for (uint32_t i = 0; i < sample_count; ++i) {
1627 if (sample_duration_present)
1628 RCHECK(buffer->ReadWriteUInt32(&sample_durations[i]));
1629 if (sample_size_present)
1630 RCHECK(buffer->ReadWriteUInt32(&sample_sizes[i]));
1631 if (sample_flags_present)
1632 RCHECK(buffer->ReadWriteUInt32(&sample_flags[i]));
1634 if (sample_composition_time_offsets_present) {
1636 uint32_t sample_offset = sample_composition_time_offsets[i];
1637 RCHECK(buffer->ReadWriteUInt32(&sample_offset));
1638 sample_composition_time_offsets[i] = sample_offset;
1640 int32_t sample_offset = sample_composition_time_offsets[i];
1641 RCHECK(buffer->ReadWriteInt32(&sample_offset));
1642 sample_composition_time_offsets[i] = sample_offset;
1648 if (first_sample_flags_present) {
1649 if (sample_flags.size() == 0) {
1650 sample_flags.push_back(first_sample_flags);
1652 sample_flags[0] = first_sample_flags;
1660 atom_size = kFullBoxSize +
sizeof(sample_count);
1661 if (flags & kDataOffsetPresentMask)
1663 if (flags & kFirstSampleFlagsPresentMask)
1665 uint32_t fields = (flags & kSampleDurationPresentMask ? 1 : 0) +
1666 (flags & kSampleSizePresentMask ? 1 : 0) +
1667 (flags & kSampleFlagsPresentMask ? 1 : 0) +
1668 (flags & kSampleCompTimeOffsetsPresentMask ? 1 : 0);
1669 atom_size += fields *
sizeof(uint32_t) * sample_count;
1673 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
1674 SampleToGroup::~SampleToGroup() {}
1675 FourCC SampleToGroup::BoxType()
const {
return FOURCC_SBGP; }
1679 buffer->ReadWriteUInt32(&grouping_type));
1681 RCHECK(buffer->ReadWriteUInt32(&grouping_type_parameter));
1683 if (grouping_type != FOURCC_SEIG) {
1685 DLOG(WARNING) <<
"Sample group '" << grouping_type <<
"' is not supported.";
1689 uint32_t count = entries.size();
1690 RCHECK(buffer->ReadWriteUInt32(&count));
1691 entries.resize(count);
1692 for (uint32_t i = 0; i < count; ++i) {
1693 RCHECK(buffer->ReadWriteUInt32(&entries[i].sample_count) &&
1694 buffer->ReadWriteUInt32(&entries[i].group_description_index));
1702 if (!entries.empty()) {
1703 atom_size = kFullBoxSize +
sizeof(grouping_type) +
1704 (version == 1 ?
sizeof(grouping_type_parameter) : 0) +
1705 sizeof(uint32_t) + entries.size() *
sizeof(entries[0]);
1710 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
1711 : is_encrypted(false), iv_size(0) {
1713 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {};
1715 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
1716 SampleGroupDescription::~SampleGroupDescription() {}
1717 FourCC SampleGroupDescription::BoxType()
const {
return FOURCC_SGPD; }
1721 buffer->ReadWriteUInt32(&grouping_type));
1723 if (grouping_type != FOURCC_SEIG) {
1725 DLOG(WARNING) <<
"Sample group '" << grouping_type <<
"' is not supported.";
1729 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
1730 uint32_t default_length = 0;
1733 RCHECK(buffer->ReadWriteUInt32(&default_length));
1734 RCHECK(default_length == 0 || default_length >= kEntrySize);
1736 default_length = kEntrySize;
1737 RCHECK(buffer->ReadWriteUInt32(&default_length));
1741 uint32_t count = entries.size();
1742 RCHECK(buffer->ReadWriteUInt32(&count));
1743 entries.resize(count);
1744 for (uint32_t i = 0; i < count; ++i) {
1746 if (buffer->
Reading() && default_length == 0) {
1747 uint32_t description_length = 0;
1748 RCHECK(buffer->ReadWriteUInt32(&description_length));
1749 RCHECK(description_length >= kEntrySize);
1754 if (entries[i].key_id.size() != kCencKeyIdSize) {
1755 LOG(WARNING) <<
"CENC defines key id length of " << kCencKeyIdSize
1756 <<
" bytes; got " << entries[i].key_id.size()
1757 <<
". Resized accordingly.";
1758 entries[i].key_id.resize(kCencKeyIdSize);
1762 uint8_t flag = entries[i].is_encrypted ? 1 : 0;
1764 buffer->ReadWriteUInt8(&flag) &&
1765 buffer->ReadWriteUInt8(&entries[i].iv_size) &&
1766 buffer->ReadWriteVector(&entries[i].key_id, kCencKeyIdSize));
1769 entries[i].is_encrypted = (flag != 0);
1770 if (entries[i].is_encrypted) {
1771 RCHECK(entries[i].iv_size == 8 || entries[i].iv_size == 16);
1773 RCHECK(entries[i].iv_size == 0);
1785 if (!entries.empty()) {
1786 const size_t kEntrySize =
sizeof(uint32_t) + kCencKeyIdSize;
1787 atom_size = kFullBoxSize +
sizeof(grouping_type) +
1788 (version == 1 ?
sizeof(uint32_t) : 0) +
sizeof(uint32_t) +
1789 entries.size() * kEntrySize;
1794 TrackFragment::TrackFragment() : decode_time_absent(false) {}
1795 TrackFragment::~TrackFragment() {}
1796 FourCC TrackFragment::BoxType()
const {
return FOURCC_TRAF; }
1803 DCHECK(buffer->
reader());
1805 if (!decode_time_absent)
1813 while (sample_to_group.grouping_type != FOURCC_SEIG &&
1817 while (sample_group_description.grouping_type != FOURCC_SEIG &&
1822 if (!decode_time_absent)
1824 for (uint32_t i = 0; i < runs.size(); ++i)
1838 for (uint32_t i = 0; i < runs.size(); ++i)
1843 MovieFragment::MovieFragment() {}
1844 MovieFragment::~MovieFragment() {}
1845 FourCC MovieFragment::BoxType()
const {
return FOURCC_MOOF; }
1857 for (uint32_t i = 0; i < tracks.size(); ++i)
1859 for (uint32_t i = 0; i < pssh.size(); ++i)
1867 for (uint32_t i = 0; i < tracks.size(); ++i)
1869 for (uint32_t i = 0; i < pssh.size(); ++i)
1874 SegmentIndex::SegmentIndex()
1877 earliest_presentation_time(0),
1879 SegmentIndex::~SegmentIndex() {}
1880 FourCC SegmentIndex::BoxType()
const {
return FOURCC_SIDX; }
1884 buffer->ReadWriteUInt32(&reference_id) &&
1885 buffer->ReadWriteUInt32(×cale));
1887 size_t num_bytes = (version == 1) ?
sizeof(uint64_t) :
sizeof(uint32_t);
1892 uint16_t reference_count = references.size();
1894 buffer->ReadWriteUInt16(&reference_count));
1895 references.resize(reference_count);
1897 uint32_t reference_type_size;
1899 for (uint32_t i = 0; i < reference_count; ++i) {
1901 reference_type_size = references[i].referenced_size;
1902 if (references[i].reference_type)
1903 reference_type_size |= (1 << 31);
1904 sap = (references[i].sap_type << 28) | references[i].sap_delta_time;
1905 if (references[i].starts_with_sap)
1908 RCHECK(buffer->ReadWriteUInt32(&reference_type_size) &&
1909 buffer->ReadWriteUInt32(&references[i].subsegment_duration) &&
1910 buffer->ReadWriteUInt32(&sap));
1912 references[i].reference_type = (reference_type_size >> 31) ?
true :
false;
1913 references[i].referenced_size = reference_type_size & ~(1 << 31);
1914 references[i].starts_with_sap = (sap >> 31) ?
true :
false;
1915 references[i].sap_type =
1916 static_cast<SegmentReference::SAPType
>((sap >> 28) & 0x07);
1917 references[i].sap_delta_time = sap & ~(0xF << 28);
1924 version = IsFitIn32Bits(earliest_presentation_time, first_offset) ? 0 : 1;
1925 atom_size = kFullBoxSize +
sizeof(reference_id) +
sizeof(timescale) +
1926 sizeof(uint32_t) * (1 + version) * 2 + 2 *
sizeof(uint16_t) +
1927 3 *
sizeof(uint32_t) * references.size();
1931 MediaData::MediaData() : data_size(0) {}
1932 MediaData::~MediaData() {}
1933 FourCC MediaData::BoxType()
const {
return FOURCC_MDAT; }
1935 void MediaData::Write(BufferWriter* buffer) {
1936 buffer->AppendInt(ComputeSize());
1937 buffer->AppendInt(static_cast<uint32_t>(BoxType()));
1940 uint32_t MediaData::ComputeSize() {
1941 return kBoxSize + data_size;
bool ReadWrite(BoxBuffer *buffer) override
Read/write the mp4 box from/to BoxBuffer.
bool ReadWrite(BoxBuffer *buffer) override
Read/write the mp4 box from/to BoxBuffer.
uint32_t ComputeSize() override
uint32_t ComputeSize() override
uint32_t ComputeSize() override
bool ReadWrite(BoxBuffer *buffer) override
Read/write the mp4 box from/to BoxBuffer.