1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/formats/mp4/box_definitions.h"
7 #include "base/logging.h"
8 #include "media/formats/mp4/es_descriptor.h"
9 #include "media/formats/mp4/rcheck.h"
14 FileType::FileType() {}
15 FileType::~FileType() {}
16 FourCC
FileType::BoxType() const { return FOURCC_FTYP
; }
18 bool FileType::Parse(BoxReader
* reader
) {
19 RCHECK(reader
->ReadFourCC(&major_brand
) && reader
->Read4(&minor_version
));
20 size_t num_brands
= (reader
->size() - reader
->pos()) / sizeof(FourCC
);
21 return reader
->SkipBytes(sizeof(FourCC
) * num_brands
); // compatible_brands
24 ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() {}
25 ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() {}
26 FourCC
ProtectionSystemSpecificHeader::BoxType() const { return FOURCC_PSSH
; }
28 bool ProtectionSystemSpecificHeader::Parse(BoxReader
* reader
) {
29 // Don't bother validating the box's contents.
30 // Copy the entire box, including the header, for passing to EME as initData.
31 DCHECK(raw_box
.empty());
32 raw_box
.assign(reader
->data(), reader
->data() + reader
->size());
36 FullProtectionSystemSpecificHeader::FullProtectionSystemSpecificHeader() {}
37 FullProtectionSystemSpecificHeader::~FullProtectionSystemSpecificHeader() {}
38 FourCC
FullProtectionSystemSpecificHeader::BoxType() const {
42 // The format of a 'pssh' box is as follows:
43 // unsigned int(32) size;
44 // unsigned int(32) type = "pssh";
46 // unsigned int(64) largesize;
47 // } else if (size==0) {
48 // -- box extends to end of file
50 // unsigned int(8) version;
52 // unsigned int(8)[16] SystemID;
55 // unsigned int(32) KID_count;
57 // unsigned int(8)[16] KID;
60 // unsigned int(32) DataSize;
61 // unsigned int(8)[DataSize] Data;
63 bool FullProtectionSystemSpecificHeader::Parse(mp4::BoxReader
* reader
) {
64 RCHECK(reader
->type() == BoxType() && reader
->ReadFullBoxHeader());
66 // Only versions 0 and 1 of the 'pssh' boxes are supported. Any other
67 // versions are ignored.
68 RCHECK(reader
->version() == 0 || reader
->version() == 1);
69 RCHECK(reader
->flags() == 0);
70 RCHECK(reader
->ReadVec(&system_id
, 16));
72 if (reader
->version() > 0) {
74 RCHECK(reader
->Read4(&kid_count
));
75 for (uint32_t i
= 0; i
< kid_count
; ++i
) {
76 std::vector
<uint8_t> kid
;
77 RCHECK(reader
->ReadVec(&kid
, 16));
78 key_ids
.push_back(kid
);
83 RCHECK(reader
->Read4(&data_size
));
84 RCHECK(reader
->ReadVec(&data
, data_size
));
88 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
89 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
90 FourCC
SampleAuxiliaryInformationOffset::BoxType() const { return FOURCC_SAIO
; }
92 bool SampleAuxiliaryInformationOffset::Parse(BoxReader
* reader
) {
93 RCHECK(reader
->ReadFullBoxHeader());
94 if (reader
->flags() & 1)
95 RCHECK(reader
->SkipBytes(8));
98 RCHECK(reader
->Read4(&count
) &&
99 reader
->HasBytes(count
* (reader
->version() == 1 ? 8 : 4)));
100 offsets
.resize(count
);
102 for (uint32 i
= 0; i
< count
; i
++) {
103 if (reader
->version() == 1) {
104 RCHECK(reader
->Read8(&offsets
[i
]));
106 RCHECK(reader
->Read4Into8(&offsets
[i
]));
112 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
113 : default_sample_info_size(0), sample_count(0) {
115 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
116 FourCC
SampleAuxiliaryInformationSize::BoxType() const { return FOURCC_SAIZ
; }
118 bool SampleAuxiliaryInformationSize::Parse(BoxReader
* reader
) {
119 RCHECK(reader
->ReadFullBoxHeader());
120 if (reader
->flags() & 1)
121 RCHECK(reader
->SkipBytes(8));
123 RCHECK(reader
->Read1(&default_sample_info_size
) &&
124 reader
->Read4(&sample_count
));
125 if (default_sample_info_size
== 0)
126 return reader
->ReadVec(&sample_info_sizes
, sample_count
);
130 OriginalFormat::OriginalFormat() : format(FOURCC_NULL
) {}
131 OriginalFormat::~OriginalFormat() {}
132 FourCC
OriginalFormat::BoxType() const { return FOURCC_FRMA
; }
134 bool OriginalFormat::Parse(BoxReader
* reader
) {
135 return reader
->ReadFourCC(&format
);
138 SchemeType::SchemeType() : type(FOURCC_NULL
), version(0) {}
139 SchemeType::~SchemeType() {}
140 FourCC
SchemeType::BoxType() const { return FOURCC_SCHM
; }
142 bool SchemeType::Parse(BoxReader
* reader
) {
143 RCHECK(reader
->ReadFullBoxHeader() &&
144 reader
->ReadFourCC(&type
) &&
145 reader
->Read4(&version
));
149 TrackEncryption::TrackEncryption()
150 : is_encrypted(false), default_iv_size(0) {
152 TrackEncryption::~TrackEncryption() {}
153 FourCC
TrackEncryption::BoxType() const { return FOURCC_TENC
; }
155 bool TrackEncryption::Parse(BoxReader
* reader
) {
157 RCHECK(reader
->ReadFullBoxHeader() &&
158 reader
->SkipBytes(2) &&
159 reader
->Read1(&flag
) &&
160 reader
->Read1(&default_iv_size
) &&
161 reader
->ReadVec(&default_kid
, 16));
162 is_encrypted
= (flag
!= 0);
164 RCHECK(default_iv_size
== 8 || default_iv_size
== 16);
166 RCHECK(default_iv_size
== 0);
171 SchemeInfo::SchemeInfo() {}
172 SchemeInfo::~SchemeInfo() {}
173 FourCC
SchemeInfo::BoxType() const { return FOURCC_SCHI
; }
175 bool SchemeInfo::Parse(BoxReader
* reader
) {
176 return reader
->ScanChildren() && reader
->ReadChild(&track_encryption
);
179 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
180 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
181 FourCC
ProtectionSchemeInfo::BoxType() const { return FOURCC_SINF
; }
183 bool ProtectionSchemeInfo::Parse(BoxReader
* reader
) {
184 RCHECK(reader
->ScanChildren() &&
185 reader
->ReadChild(&format
) &&
186 reader
->ReadChild(&type
));
187 if (type
.type
== FOURCC_CENC
)
188 RCHECK(reader
->ReadChild(&info
));
189 // Other protection schemes are silently ignored. Since the protection scheme
190 // type can't be determined until this box is opened, we return 'true' for
191 // non-CENC protection scheme types. It is the parent box's responsibility to
192 // ensure that this scheme type is a supported one.
196 MovieHeader::MovieHeader()
198 modification_time(0),
204 MovieHeader::~MovieHeader() {}
205 FourCC
MovieHeader::BoxType() const { return FOURCC_MVHD
; }
207 bool MovieHeader::Parse(BoxReader
* reader
) {
208 RCHECK(reader
->ReadFullBoxHeader());
210 if (reader
->version() == 1) {
211 RCHECK(reader
->Read8(&creation_time
) &&
212 reader
->Read8(&modification_time
) &&
213 reader
->Read4(×cale
) &&
214 reader
->Read8(&duration
));
216 RCHECK(reader
->Read4Into8(&creation_time
) &&
217 reader
->Read4Into8(&modification_time
) &&
218 reader
->Read4(×cale
) &&
219 reader
->Read4Into8(&duration
));
222 RCHECK(reader
->Read4s(&rate
) &&
223 reader
->Read2s(&volume
) &&
224 reader
->SkipBytes(10) && // reserved
225 reader
->SkipBytes(36) && // matrix
226 reader
->SkipBytes(24) && // predefined zero
227 reader
->Read4(&next_track_id
));
231 TrackHeader::TrackHeader()
233 modification_time(0),
241 TrackHeader::~TrackHeader() {}
242 FourCC
TrackHeader::BoxType() const { return FOURCC_TKHD
; }
244 bool TrackHeader::Parse(BoxReader
* reader
) {
245 RCHECK(reader
->ReadFullBoxHeader());
246 if (reader
->version() == 1) {
247 RCHECK(reader
->Read8(&creation_time
) &&
248 reader
->Read8(&modification_time
) &&
249 reader
->Read4(&track_id
) &&
250 reader
->SkipBytes(4) && // reserved
251 reader
->Read8(&duration
));
253 RCHECK(reader
->Read4Into8(&creation_time
) &&
254 reader
->Read4Into8(&modification_time
) &&
255 reader
->Read4(&track_id
) &&
256 reader
->SkipBytes(4) && // reserved
257 reader
->Read4Into8(&duration
));
260 RCHECK(reader
->SkipBytes(8) && // reserved
261 reader
->Read2s(&layer
) &&
262 reader
->Read2s(&alternate_group
) &&
263 reader
->Read2s(&volume
) &&
264 reader
->SkipBytes(2) && // reserved
265 reader
->SkipBytes(36) && // matrix
266 reader
->Read4(&width
) &&
267 reader
->Read4(&height
));
273 SampleDescription::SampleDescription() : type(kInvalid
) {}
274 SampleDescription::~SampleDescription() {}
275 FourCC
SampleDescription::BoxType() const { return FOURCC_STSD
; }
277 bool SampleDescription::Parse(BoxReader
* reader
) {
279 RCHECK(reader
->SkipBytes(4) &&
280 reader
->Read4(&count
));
281 video_entries
.clear();
282 audio_entries
.clear();
284 // Note: this value is preset before scanning begins. See comments in the
285 // Parse(Media*) function.
286 if (type
== kVideo
) {
287 RCHECK(reader
->ReadAllChildren(&video_entries
));
288 } else if (type
== kAudio
) {
289 RCHECK(reader
->ReadAllChildren(&audio_entries
));
294 SyncSample::SyncSample() : is_present(false) {}
295 SyncSample::~SyncSample() {}
296 FourCC
SyncSample::BoxType() const { return FOURCC_STSS
; }
298 bool SyncSample::Parse(BoxReader
* reader
) {
300 RCHECK(reader
->ReadFullBoxHeader() &&
301 reader
->Read4(&entry_count
));
305 entries
.resize(entry_count
);
307 if (entry_count
== 0)
310 for (size_t i
= 0; i
< entry_count
; ++i
)
311 RCHECK(reader
->Read4(&entries
[i
]));
316 bool SyncSample::IsSyncSample(size_t k
) const {
317 // ISO/IEC 14496-12 Section 8.6.2.1 : If the sync sample box is not present,
318 // every sample is a sync sample.
322 // ISO/IEC 14496-12 Section 8.6.2.3 : If entry_count is zero, there are no
323 // sync samples within the stream.
324 if (entries
.size() == 0u)
327 for (size_t i
= 0; i
< entries
.size(); ++i
) {
335 SampleTable::SampleTable() {}
336 SampleTable::~SampleTable() {}
337 FourCC
SampleTable::BoxType() const { return FOURCC_STBL
; }
339 bool SampleTable::Parse(BoxReader
* reader
) {
340 return reader
->ScanChildren() &&
341 reader
->ReadChild(&description
) &&
342 reader
->MaybeReadChild(&sync_sample
);
345 EditList::EditList() {}
346 EditList::~EditList() {}
347 FourCC
EditList::BoxType() const { return FOURCC_ELST
; }
349 bool EditList::Parse(BoxReader
* reader
) {
351 RCHECK(reader
->ReadFullBoxHeader() && reader
->Read4(&count
));
353 if (reader
->version() == 1) {
354 RCHECK(reader
->HasBytes(count
* 20));
356 RCHECK(reader
->HasBytes(count
* 12));
360 for (std::vector
<EditListEntry
>::iterator edit
= edits
.begin();
361 edit
!= edits
.end(); ++edit
) {
362 if (reader
->version() == 1) {
363 RCHECK(reader
->Read8(&edit
->segment_duration
) &&
364 reader
->Read8s(&edit
->media_time
));
366 RCHECK(reader
->Read4Into8(&edit
->segment_duration
) &&
367 reader
->Read4sInto8s(&edit
->media_time
));
369 RCHECK(reader
->Read2s(&edit
->media_rate_integer
) &&
370 reader
->Read2s(&edit
->media_rate_fraction
));
377 FourCC
Edit::BoxType() const { return FOURCC_EDTS
; }
379 bool Edit::Parse(BoxReader
* reader
) {
380 return reader
->ScanChildren() && reader
->ReadChild(&list
);
383 HandlerReference::HandlerReference() : type(kInvalid
) {}
384 HandlerReference::~HandlerReference() {}
385 FourCC
HandlerReference::BoxType() const { return FOURCC_HDLR
; }
387 bool HandlerReference::Parse(BoxReader
* reader
) {
389 RCHECK(reader
->SkipBytes(8) && reader
->ReadFourCC(&hdlr_type
));
390 // Note: remaining fields in box ignored
391 if (hdlr_type
== FOURCC_VIDE
) {
393 } else if (hdlr_type
== FOURCC_SOUN
) {
401 AVCDecoderConfigurationRecord::AVCDecoderConfigurationRecord()
403 profile_indication(0),
404 profile_compatibility(0),
408 AVCDecoderConfigurationRecord::~AVCDecoderConfigurationRecord() {}
409 FourCC
AVCDecoderConfigurationRecord::BoxType() const { return FOURCC_AVCC
; }
411 bool AVCDecoderConfigurationRecord::Parse(BoxReader
* reader
) {
412 return ParseInternal(reader
, reader
->log_cb());
415 bool AVCDecoderConfigurationRecord::Parse(const uint8
* data
, int data_size
) {
416 BufferReader
reader(data
, data_size
);
417 return ParseInternal(&reader
, LogCB());
420 bool AVCDecoderConfigurationRecord::ParseInternal(BufferReader
* reader
,
421 const LogCB
& log_cb
) {
422 RCHECK(reader
->Read1(&version
) && version
== 1 &&
423 reader
->Read1(&profile_indication
) &&
424 reader
->Read1(&profile_compatibility
) &&
425 reader
->Read1(&avc_level
));
427 uint8 length_size_minus_one
;
428 RCHECK(reader
->Read1(&length_size_minus_one
));
429 length_size
= (length_size_minus_one
& 0x3) + 1;
431 RCHECK(length_size
!= 3); // Only values of 1, 2, and 4 are valid.
434 RCHECK(reader
->Read1(&num_sps
));
437 sps_list
.resize(num_sps
);
438 for (int i
= 0; i
< num_sps
; i
++) {
440 RCHECK(reader
->Read2(&sps_length
) &&
441 reader
->ReadVec(&sps_list
[i
], sps_length
));
442 RCHECK(sps_list
[i
].size() > 4);
444 if (!log_cb
.is_null()) {
445 MEDIA_LOG(INFO
, log_cb
) << "Video codec: avc1." << std::hex
446 << static_cast<int>(sps_list
[i
][1])
447 << static_cast<int>(sps_list
[i
][2])
448 << static_cast<int>(sps_list
[i
][3]);
453 RCHECK(reader
->Read1(&num_pps
));
455 pps_list
.resize(num_pps
);
456 for (int i
= 0; i
< num_pps
; i
++) {
458 RCHECK(reader
->Read2(&pps_length
) &&
459 reader
->ReadVec(&pps_list
[i
], pps_length
));
465 PixelAspectRatioBox::PixelAspectRatioBox() : h_spacing(1), v_spacing(1) {}
466 PixelAspectRatioBox::~PixelAspectRatioBox() {}
467 FourCC
PixelAspectRatioBox::BoxType() const { return FOURCC_PASP
; }
469 bool PixelAspectRatioBox::Parse(BoxReader
* reader
) {
470 RCHECK(reader
->Read4(&h_spacing
) &&
471 reader
->Read4(&v_spacing
));
475 VideoSampleEntry::VideoSampleEntry()
476 : format(FOURCC_NULL
),
477 data_reference_index(0),
481 VideoSampleEntry::~VideoSampleEntry() {}
482 FourCC
VideoSampleEntry::BoxType() const {
483 DCHECK(false) << "VideoSampleEntry should be parsed according to the "
484 << "handler type recovered in its Media ancestor.";
488 bool VideoSampleEntry::Parse(BoxReader
* reader
) {
489 format
= reader
->type();
490 RCHECK(reader
->SkipBytes(6) &&
491 reader
->Read2(&data_reference_index
) &&
492 reader
->SkipBytes(16) &&
493 reader
->Read2(&width
) &&
494 reader
->Read2(&height
) &&
495 reader
->SkipBytes(50));
497 RCHECK(reader
->ScanChildren() &&
498 reader
->MaybeReadChild(&pixel_aspect
));
500 if (format
== FOURCC_ENCV
) {
501 // Continue scanning until a recognized protection scheme is found, or until
502 // we run out of protection schemes.
503 while (sinf
.type
.type
!= FOURCC_CENC
) {
504 if (!reader
->ReadChild(&sinf
))
510 RCHECK(reader
->ReadChild(&avcc
));
515 bool VideoSampleEntry::IsFormatValid() const {
516 return format
== FOURCC_AVC1
|| format
== FOURCC_AVC3
||
517 (format
== FOURCC_ENCV
&& (sinf
.format
.format
== FOURCC_AVC1
||
518 sinf
.format
.format
== FOURCC_AVC3
));
521 ElementaryStreamDescriptor::ElementaryStreamDescriptor()
522 : object_type(kForbidden
) {}
524 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
526 FourCC
ElementaryStreamDescriptor::BoxType() const {
530 bool ElementaryStreamDescriptor::Parse(BoxReader
* reader
) {
531 std::vector
<uint8
> data
;
532 ESDescriptor es_desc
;
534 RCHECK(reader
->ReadFullBoxHeader());
535 RCHECK(reader
->ReadVec(&data
, reader
->size() - reader
->pos()));
536 RCHECK(es_desc
.Parse(data
));
538 object_type
= es_desc
.object_type();
540 if (object_type
!= 0x40) {
541 MEDIA_LOG(INFO
, reader
->log_cb()) << "Audio codec: mp4a." << std::hex
542 << static_cast<int>(object_type
);
545 if (es_desc
.IsAAC(object_type
))
546 RCHECK(aac
.Parse(es_desc
.decoder_specific_info(), reader
->log_cb()));
551 AudioSampleEntry::AudioSampleEntry()
552 : format(FOURCC_NULL
),
553 data_reference_index(0),
558 AudioSampleEntry::~AudioSampleEntry() {}
560 FourCC
AudioSampleEntry::BoxType() const {
561 DCHECK(false) << "AudioSampleEntry should be parsed according to the "
562 << "handler type recovered in its Media ancestor.";
566 bool AudioSampleEntry::Parse(BoxReader
* reader
) {
567 format
= reader
->type();
568 RCHECK(reader
->SkipBytes(6) &&
569 reader
->Read2(&data_reference_index
) &&
570 reader
->SkipBytes(8) &&
571 reader
->Read2(&channelcount
) &&
572 reader
->Read2(&samplesize
) &&
573 reader
->SkipBytes(4) &&
574 reader
->Read4(&samplerate
));
575 // Convert from 16.16 fixed point to integer
578 RCHECK(reader
->ScanChildren());
579 if (format
== FOURCC_ENCA
) {
580 // Continue scanning until a recognized protection scheme is found, or until
581 // we run out of protection schemes.
582 while (sinf
.type
.type
!= FOURCC_CENC
) {
583 if (!reader
->ReadChild(&sinf
))
588 // ESDS is not valid in case of EAC3.
589 RCHECK(reader
->MaybeReadChild(&esds
));
593 MediaHeader::MediaHeader()
595 modification_time(0),
598 MediaHeader::~MediaHeader() {}
599 FourCC
MediaHeader::BoxType() const { return FOURCC_MDHD
; }
601 bool MediaHeader::Parse(BoxReader
* reader
) {
602 RCHECK(reader
->ReadFullBoxHeader());
604 if (reader
->version() == 1) {
605 RCHECK(reader
->Read8(&creation_time
) &&
606 reader
->Read8(&modification_time
) &&
607 reader
->Read4(×cale
) &&
608 reader
->Read8(&duration
));
610 RCHECK(reader
->Read4Into8(&creation_time
) &&
611 reader
->Read4Into8(&modification_time
) &&
612 reader
->Read4(×cale
) &&
613 reader
->Read4Into8(&duration
));
615 // Skip language information
616 return reader
->SkipBytes(4);
619 MediaInformation::MediaInformation() {}
620 MediaInformation::~MediaInformation() {}
621 FourCC
MediaInformation::BoxType() const { return FOURCC_MINF
; }
623 bool MediaInformation::Parse(BoxReader
* reader
) {
624 return reader
->ScanChildren() &&
625 reader
->ReadChild(&sample_table
);
630 FourCC
Media::BoxType() const { return FOURCC_MDIA
; }
632 bool Media::Parse(BoxReader
* reader
) {
633 RCHECK(reader
->ScanChildren() &&
634 reader
->ReadChild(&header
) &&
635 reader
->ReadChild(&handler
));
637 // Maddeningly, the HandlerReference box specifies how to parse the
638 // SampleDescription box, making the latter the only box (of those that we
639 // support) which cannot be parsed correctly on its own (or even with
640 // information from its strict ancestor tree). We thus copy the handler type
641 // to the sample description box *before* parsing it to provide this
642 // information while parsing.
643 information
.sample_table
.description
.type
= handler
.type
;
644 RCHECK(reader
->ReadChild(&information
));
650 FourCC
Track::BoxType() const { return FOURCC_TRAK
; }
652 bool Track::Parse(BoxReader
* reader
) {
653 RCHECK(reader
->ScanChildren() &&
654 reader
->ReadChild(&header
) &&
655 reader
->ReadChild(&media
) &&
656 reader
->MaybeReadChild(&edit
));
660 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
661 MovieExtendsHeader::~MovieExtendsHeader() {}
662 FourCC
MovieExtendsHeader::BoxType() const { return FOURCC_MEHD
; }
664 bool MovieExtendsHeader::Parse(BoxReader
* reader
) {
665 RCHECK(reader
->ReadFullBoxHeader());
666 if (reader
->version() == 1) {
667 RCHECK(reader
->Read8(&fragment_duration
));
669 RCHECK(reader
->Read4Into8(&fragment_duration
));
674 TrackExtends::TrackExtends()
676 default_sample_description_index(0),
677 default_sample_duration(0),
678 default_sample_size(0),
679 default_sample_flags(0) {}
680 TrackExtends::~TrackExtends() {}
681 FourCC
TrackExtends::BoxType() const { return FOURCC_TREX
; }
683 bool TrackExtends::Parse(BoxReader
* reader
) {
684 RCHECK(reader
->ReadFullBoxHeader() &&
685 reader
->Read4(&track_id
) &&
686 reader
->Read4(&default_sample_description_index
) &&
687 reader
->Read4(&default_sample_duration
) &&
688 reader
->Read4(&default_sample_size
) &&
689 reader
->Read4(&default_sample_flags
));
693 MovieExtends::MovieExtends() {}
694 MovieExtends::~MovieExtends() {}
695 FourCC
MovieExtends::BoxType() const { return FOURCC_MVEX
; }
697 bool MovieExtends::Parse(BoxReader
* reader
) {
698 header
.fragment_duration
= 0;
699 return reader
->ScanChildren() &&
700 reader
->MaybeReadChild(&header
) &&
701 reader
->ReadChildren(&tracks
);
704 Movie::Movie() : fragmented(false) {}
706 FourCC
Movie::BoxType() const { return FOURCC_MOOV
; }
708 bool Movie::Parse(BoxReader
* reader
) {
709 RCHECK(reader
->ScanChildren() && reader
->ReadChild(&header
) &&
710 reader
->ReadChildren(&tracks
));
712 RCHECK_MEDIA_LOGGED(reader
->ReadChild(&extends
), reader
->log_cb(),
713 "Detected unfragmented MP4. Media Source Extensions "
714 "require ISO BMFF moov to contain mvex to indicate that "
715 "Movie Fragments are to be expected.");
717 return reader
->MaybeReadChildren(&pssh
);
720 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
721 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
722 FourCC
TrackFragmentDecodeTime::BoxType() const { return FOURCC_TFDT
; }
724 bool TrackFragmentDecodeTime::Parse(BoxReader
* reader
) {
725 RCHECK(reader
->ReadFullBoxHeader());
726 if (reader
->version() == 1)
727 return reader
->Read8(&decode_time
);
729 return reader
->Read4Into8(&decode_time
);
732 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
733 MovieFragmentHeader::~MovieFragmentHeader() {}
734 FourCC
MovieFragmentHeader::BoxType() const { return FOURCC_MFHD
; }
736 bool MovieFragmentHeader::Parse(BoxReader
* reader
) {
737 return reader
->SkipBytes(4) && reader
->Read4(&sequence_number
);
740 TrackFragmentHeader::TrackFragmentHeader()
742 sample_description_index(0),
743 default_sample_duration(0),
744 default_sample_size(0),
745 default_sample_flags(0),
746 has_default_sample_flags(false) {}
748 TrackFragmentHeader::~TrackFragmentHeader() {}
749 FourCC
TrackFragmentHeader::BoxType() const { return FOURCC_TFHD
; }
751 bool TrackFragmentHeader::Parse(BoxReader
* reader
) {
752 RCHECK(reader
->ReadFullBoxHeader() && reader
->Read4(&track_id
));
754 // Media Source specific: reject tracks that set 'base-data-offset-present'.
755 // Although the Media Source requires that 'default-base-is-moof' (14496-12
756 // Amendment 2) be set, we omit this check as many otherwise-valid files in
757 // the wild don't set it.
759 // RCHECK((flags & 0x020000) && !(flags & 0x1));
760 RCHECK(!(reader
->flags() & 0x1));
762 if (reader
->flags() & 0x2) {
763 RCHECK(reader
->Read4(&sample_description_index
));
765 sample_description_index
= 0;
768 if (reader
->flags() & 0x8) {
769 RCHECK(reader
->Read4(&default_sample_duration
));
771 default_sample_duration
= 0;
774 if (reader
->flags() & 0x10) {
775 RCHECK(reader
->Read4(&default_sample_size
));
777 default_sample_size
= 0;
780 if (reader
->flags() & 0x20) {
781 RCHECK(reader
->Read4(&default_sample_flags
));
782 has_default_sample_flags
= true;
784 has_default_sample_flags
= false;
790 TrackFragmentRun::TrackFragmentRun()
791 : sample_count(0), data_offset(0) {}
792 TrackFragmentRun::~TrackFragmentRun() {}
793 FourCC
TrackFragmentRun::BoxType() const { return FOURCC_TRUN
; }
795 bool TrackFragmentRun::Parse(BoxReader
* reader
) {
796 RCHECK(reader
->ReadFullBoxHeader() &&
797 reader
->Read4(&sample_count
));
798 const uint32 flags
= reader
->flags();
800 bool data_offset_present
= (flags
& 0x1) != 0;
801 bool first_sample_flags_present
= (flags
& 0x4) != 0;
802 bool sample_duration_present
= (flags
& 0x100) != 0;
803 bool sample_size_present
= (flags
& 0x200) != 0;
804 bool sample_flags_present
= (flags
& 0x400) != 0;
805 bool sample_composition_time_offsets_present
= (flags
& 0x800) != 0;
807 if (data_offset_present
) {
808 RCHECK(reader
->Read4(&data_offset
));
813 uint32 first_sample_flags
= 0;
814 if (first_sample_flags_present
)
815 RCHECK(reader
->Read4(&first_sample_flags
));
817 int fields
= sample_duration_present
+ sample_size_present
+
818 sample_flags_present
+ sample_composition_time_offsets_present
;
819 RCHECK(reader
->HasBytes(fields
* sample_count
));
821 if (sample_duration_present
)
822 sample_durations
.resize(sample_count
);
823 if (sample_size_present
)
824 sample_sizes
.resize(sample_count
);
825 if (sample_flags_present
)
826 sample_flags
.resize(sample_count
);
827 if (sample_composition_time_offsets_present
)
828 sample_composition_time_offsets
.resize(sample_count
);
830 for (uint32 i
= 0; i
< sample_count
; ++i
) {
831 if (sample_duration_present
)
832 RCHECK(reader
->Read4(&sample_durations
[i
]));
833 if (sample_size_present
)
834 RCHECK(reader
->Read4(&sample_sizes
[i
]));
835 if (sample_flags_present
)
836 RCHECK(reader
->Read4(&sample_flags
[i
]));
837 if (sample_composition_time_offsets_present
)
838 RCHECK(reader
->Read4s(&sample_composition_time_offsets
[i
]));
841 if (first_sample_flags_present
) {
842 if (sample_flags
.size() == 0) {
843 sample_flags
.push_back(first_sample_flags
);
845 sample_flags
[0] = first_sample_flags
;
851 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
852 SampleToGroup::~SampleToGroup() {}
853 FourCC
SampleToGroup::BoxType() const { return FOURCC_SBGP
; }
855 bool SampleToGroup::Parse(BoxReader
* reader
) {
856 RCHECK(reader
->ReadFullBoxHeader() &&
857 reader
->Read4(&grouping_type
));
859 if (reader
->version() == 1)
860 RCHECK(reader
->Read4(&grouping_type_parameter
));
862 if (grouping_type
!= FOURCC_SEIG
) {
863 DLOG(WARNING
) << "SampleToGroup box with grouping_type '" << grouping_type
864 << "' is not supported.";
869 RCHECK(reader
->Read4(&count
));
870 entries
.resize(count
);
871 for (uint32 i
= 0; i
< count
; ++i
) {
872 RCHECK(reader
->Read4(&entries
[i
].sample_count
) &&
873 reader
->Read4(&entries
[i
].group_description_index
));
878 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
879 : is_encrypted(false), iv_size(0) {}
880 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {}
882 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
883 SampleGroupDescription::~SampleGroupDescription() {}
884 FourCC
SampleGroupDescription::BoxType() const { return FOURCC_SGPD
; }
886 bool SampleGroupDescription::Parse(BoxReader
* reader
) {
887 RCHECK(reader
->ReadFullBoxHeader() &&
888 reader
->Read4(&grouping_type
));
890 if (grouping_type
!= FOURCC_SEIG
) {
891 DLOG(WARNING
) << "SampleGroupDescription box with grouping_type '"
892 << grouping_type
<< "' is not supported.";
896 const uint8 version
= reader
->version();
898 const size_t kKeyIdSize
= 16;
899 const size_t kEntrySize
= sizeof(uint32
) + kKeyIdSize
;
900 uint32 default_length
= 0;
902 RCHECK(reader
->Read4(&default_length
));
903 RCHECK(default_length
== 0 || default_length
>= kEntrySize
);
907 RCHECK(reader
->Read4(&count
));
908 entries
.resize(count
);
909 for (uint32 i
= 0; i
< count
; ++i
) {
911 if (default_length
== 0) {
912 uint32 description_length
= 0;
913 RCHECK(reader
->Read4(&description_length
));
914 RCHECK(description_length
>= kEntrySize
);
919 RCHECK(reader
->SkipBytes(2) && // reserved.
920 reader
->Read1(&flag
) &&
921 reader
->Read1(&entries
[i
].iv_size
) &&
922 reader
->ReadVec(&entries
[i
].key_id
, kKeyIdSize
));
924 entries
[i
].is_encrypted
= (flag
!= 0);
925 if (entries
[i
].is_encrypted
) {
926 RCHECK(entries
[i
].iv_size
== 8 || entries
[i
].iv_size
== 16);
928 RCHECK(entries
[i
].iv_size
== 0);
934 TrackFragment::TrackFragment() {}
935 TrackFragment::~TrackFragment() {}
936 FourCC
TrackFragment::BoxType() const { return FOURCC_TRAF
; }
938 bool TrackFragment::Parse(BoxReader
* reader
) {
939 RCHECK(reader
->ScanChildren() &&
940 reader
->ReadChild(&header
) &&
941 // Media Source specific: 'tfdt' required
942 reader
->ReadChild(&decode_time
) &&
943 reader
->MaybeReadChildren(&runs
) &&
944 reader
->MaybeReadChild(&auxiliary_offset
) &&
945 reader
->MaybeReadChild(&auxiliary_size
) &&
946 reader
->MaybeReadChild(&sdtp
));
948 // There could be multiple SampleGroupDescription and SampleToGroup boxes with
949 // different grouping types. For common encryption, the relevant grouping type
950 // is 'seig'. Continue reading until 'seig' is found, or until running out of
952 while (sample_group_description
.grouping_type
!= FOURCC_SEIG
&&
953 reader
->HasChild(&sample_group_description
)) {
954 RCHECK(reader
->ReadChild(&sample_group_description
));
956 while (sample_to_group
.grouping_type
!= FOURCC_SEIG
&&
957 reader
->HasChild(&sample_to_group
)) {
958 RCHECK(reader
->ReadChild(&sample_to_group
));
963 MovieFragment::MovieFragment() {}
964 MovieFragment::~MovieFragment() {}
965 FourCC
MovieFragment::BoxType() const { return FOURCC_MOOF
; }
967 bool MovieFragment::Parse(BoxReader
* reader
) {
968 RCHECK(reader
->ScanChildren() &&
969 reader
->ReadChild(&header
) &&
970 reader
->ReadChildren(&tracks
) &&
971 reader
->MaybeReadChildren(&pssh
));
975 IndependentAndDisposableSamples::IndependentAndDisposableSamples() {}
976 IndependentAndDisposableSamples::~IndependentAndDisposableSamples() {}
977 FourCC
IndependentAndDisposableSamples::BoxType() const { return FOURCC_SDTP
; }
979 bool IndependentAndDisposableSamples::Parse(BoxReader
* reader
) {
980 RCHECK(reader
->ReadFullBoxHeader());
981 RCHECK(reader
->version() == 0);
982 RCHECK(reader
->flags() == 0);
984 int sample_count
= reader
->size() - reader
->pos();
985 sample_depends_on_
.resize(sample_count
);
986 for (int i
= 0; i
< sample_count
; ++i
) {
988 RCHECK(reader
->Read1(&sample_info
));
990 sample_depends_on_
[i
] =
991 static_cast<SampleDependsOn
>((sample_info
>> 4) & 0x3);
993 RCHECK(sample_depends_on_
[i
] != kSampleDependsOnReserved
);
999 SampleDependsOn
IndependentAndDisposableSamples::sample_depends_on(
1001 if (i
>= sample_depends_on_
.size())
1002 return kSampleDependsOnUnknown
;
1004 return sample_depends_on_
[i
];
1008 } // namespace media