Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / media / formats / mp4 / box_definitions.cc
blob9ee43064b0b82e109ec487e2af994b6c64d007a5
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"
11 namespace media {
12 namespace mp4 {
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 // Validate the box's contents and hang on to the system ID.
30 uint32 size;
31 RCHECK(reader->ReadFullBoxHeader() &&
32 reader->ReadVec(&system_id, 16) &&
33 reader->Read4(&size) &&
34 reader->HasBytes(size));
36 // Copy the entire box, including the header, for passing to EME as initData.
37 DCHECK(raw_box.empty());
38 raw_box.assign(reader->data(), reader->data() + reader->size());
39 return true;
42 SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() {}
43 SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() {}
44 FourCC SampleAuxiliaryInformationOffset::BoxType() const { return FOURCC_SAIO; }
46 bool SampleAuxiliaryInformationOffset::Parse(BoxReader* reader) {
47 RCHECK(reader->ReadFullBoxHeader());
48 if (reader->flags() & 1)
49 RCHECK(reader->SkipBytes(8));
51 uint32 count;
52 RCHECK(reader->Read4(&count) &&
53 reader->HasBytes(count * (reader->version() == 1 ? 8 : 4)));
54 offsets.resize(count);
56 for (uint32 i = 0; i < count; i++) {
57 if (reader->version() == 1) {
58 RCHECK(reader->Read8(&offsets[i]));
59 } else {
60 RCHECK(reader->Read4Into8(&offsets[i]));
63 return true;
66 SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
67 : default_sample_info_size(0), sample_count(0) {
69 SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() {}
70 FourCC SampleAuxiliaryInformationSize::BoxType() const { return FOURCC_SAIZ; }
72 bool SampleAuxiliaryInformationSize::Parse(BoxReader* reader) {
73 RCHECK(reader->ReadFullBoxHeader());
74 if (reader->flags() & 1)
75 RCHECK(reader->SkipBytes(8));
77 RCHECK(reader->Read1(&default_sample_info_size) &&
78 reader->Read4(&sample_count));
79 if (default_sample_info_size == 0)
80 return reader->ReadVec(&sample_info_sizes, sample_count);
81 return true;
84 OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
85 OriginalFormat::~OriginalFormat() {}
86 FourCC OriginalFormat::BoxType() const { return FOURCC_FRMA; }
88 bool OriginalFormat::Parse(BoxReader* reader) {
89 return reader->ReadFourCC(&format);
92 SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
93 SchemeType::~SchemeType() {}
94 FourCC SchemeType::BoxType() const { return FOURCC_SCHM; }
96 bool SchemeType::Parse(BoxReader* reader) {
97 RCHECK(reader->ReadFullBoxHeader() &&
98 reader->ReadFourCC(&type) &&
99 reader->Read4(&version));
100 return true;
103 TrackEncryption::TrackEncryption()
104 : is_encrypted(false), default_iv_size(0) {
106 TrackEncryption::~TrackEncryption() {}
107 FourCC TrackEncryption::BoxType() const { return FOURCC_TENC; }
109 bool TrackEncryption::Parse(BoxReader* reader) {
110 uint8 flag;
111 RCHECK(reader->ReadFullBoxHeader() &&
112 reader->SkipBytes(2) &&
113 reader->Read1(&flag) &&
114 reader->Read1(&default_iv_size) &&
115 reader->ReadVec(&default_kid, 16));
116 is_encrypted = (flag != 0);
117 if (is_encrypted) {
118 RCHECK(default_iv_size == 8 || default_iv_size == 16);
119 } else {
120 RCHECK(default_iv_size == 0);
122 return true;
125 SchemeInfo::SchemeInfo() {}
126 SchemeInfo::~SchemeInfo() {}
127 FourCC SchemeInfo::BoxType() const { return FOURCC_SCHI; }
129 bool SchemeInfo::Parse(BoxReader* reader) {
130 return reader->ScanChildren() && reader->ReadChild(&track_encryption);
133 ProtectionSchemeInfo::ProtectionSchemeInfo() {}
134 ProtectionSchemeInfo::~ProtectionSchemeInfo() {}
135 FourCC ProtectionSchemeInfo::BoxType() const { return FOURCC_SINF; }
137 bool ProtectionSchemeInfo::Parse(BoxReader* reader) {
138 RCHECK(reader->ScanChildren() &&
139 reader->ReadChild(&format) &&
140 reader->ReadChild(&type));
141 if (type.type == FOURCC_CENC)
142 RCHECK(reader->ReadChild(&info));
143 // Other protection schemes are silently ignored. Since the protection scheme
144 // type can't be determined until this box is opened, we return 'true' for
145 // non-CENC protection scheme types. It is the parent box's responsibility to
146 // ensure that this scheme type is a supported one.
147 return true;
150 MovieHeader::MovieHeader()
151 : creation_time(0),
152 modification_time(0),
153 timescale(0),
154 duration(0),
155 rate(-1),
156 volume(-1),
157 next_track_id(0) {}
158 MovieHeader::~MovieHeader() {}
159 FourCC MovieHeader::BoxType() const { return FOURCC_MVHD; }
161 bool MovieHeader::Parse(BoxReader* reader) {
162 RCHECK(reader->ReadFullBoxHeader());
164 if (reader->version() == 1) {
165 RCHECK(reader->Read8(&creation_time) &&
166 reader->Read8(&modification_time) &&
167 reader->Read4(&timescale) &&
168 reader->Read8(&duration));
169 } else {
170 RCHECK(reader->Read4Into8(&creation_time) &&
171 reader->Read4Into8(&modification_time) &&
172 reader->Read4(&timescale) &&
173 reader->Read4Into8(&duration));
176 RCHECK(reader->Read4s(&rate) &&
177 reader->Read2s(&volume) &&
178 reader->SkipBytes(10) && // reserved
179 reader->SkipBytes(36) && // matrix
180 reader->SkipBytes(24) && // predefined zero
181 reader->Read4(&next_track_id));
182 return true;
185 TrackHeader::TrackHeader()
186 : creation_time(0),
187 modification_time(0),
188 track_id(0),
189 duration(0),
190 layer(-1),
191 alternate_group(-1),
192 volume(-1),
193 width(0),
194 height(0) {}
195 TrackHeader::~TrackHeader() {}
196 FourCC TrackHeader::BoxType() const { return FOURCC_TKHD; }
198 bool TrackHeader::Parse(BoxReader* reader) {
199 RCHECK(reader->ReadFullBoxHeader());
200 if (reader->version() == 1) {
201 RCHECK(reader->Read8(&creation_time) &&
202 reader->Read8(&modification_time) &&
203 reader->Read4(&track_id) &&
204 reader->SkipBytes(4) && // reserved
205 reader->Read8(&duration));
206 } else {
207 RCHECK(reader->Read4Into8(&creation_time) &&
208 reader->Read4Into8(&modification_time) &&
209 reader->Read4(&track_id) &&
210 reader->SkipBytes(4) && // reserved
211 reader->Read4Into8(&duration));
214 RCHECK(reader->SkipBytes(8) && // reserved
215 reader->Read2s(&layer) &&
216 reader->Read2s(&alternate_group) &&
217 reader->Read2s(&volume) &&
218 reader->SkipBytes(2) && // reserved
219 reader->SkipBytes(36) && // matrix
220 reader->Read4(&width) &&
221 reader->Read4(&height));
222 width >>= 16;
223 height >>= 16;
224 return true;
227 SampleDescription::SampleDescription() : type(kInvalid) {}
228 SampleDescription::~SampleDescription() {}
229 FourCC SampleDescription::BoxType() const { return FOURCC_STSD; }
231 bool SampleDescription::Parse(BoxReader* reader) {
232 uint32 count;
233 RCHECK(reader->SkipBytes(4) &&
234 reader->Read4(&count));
235 video_entries.clear();
236 audio_entries.clear();
238 // Note: this value is preset before scanning begins. See comments in the
239 // Parse(Media*) function.
240 if (type == kVideo) {
241 RCHECK(reader->ReadAllChildren(&video_entries));
242 } else if (type == kAudio) {
243 RCHECK(reader->ReadAllChildren(&audio_entries));
245 return true;
248 SyncSample::SyncSample() : is_present(false) {}
249 SyncSample::~SyncSample() {}
250 FourCC SyncSample::BoxType() const { return FOURCC_STSS; }
252 bool SyncSample::Parse(BoxReader* reader) {
253 uint32 entry_count;
254 RCHECK(reader->ReadFullBoxHeader() &&
255 reader->Read4(&entry_count));
257 is_present = true;
259 entries.resize(entry_count);
261 if (entry_count == 0)
262 return true;
264 for (size_t i = 0; i < entry_count; ++i)
265 RCHECK(reader->Read4(&entries[i]));
267 return true;
270 bool SyncSample::IsSyncSample(size_t k) const {
271 // ISO/IEC 14496-12 Section 8.6.2.1 : If the sync sample box is not present,
272 // every sample is a sync sample.
273 if (!is_present)
274 return true;
276 // ISO/IEC 14496-12 Section 8.6.2.3 : If entry_count is zero, there are no
277 // sync samples within the stream.
278 if (entries.size() == 0u)
279 return false;
281 for (size_t i = 0; i < entries.size(); ++i) {
282 if (entries[i] == k)
283 return true;
286 return false;
289 SampleTable::SampleTable() {}
290 SampleTable::~SampleTable() {}
291 FourCC SampleTable::BoxType() const { return FOURCC_STBL; }
293 bool SampleTable::Parse(BoxReader* reader) {
294 return reader->ScanChildren() &&
295 reader->ReadChild(&description) &&
296 reader->MaybeReadChild(&sync_sample);
299 EditList::EditList() {}
300 EditList::~EditList() {}
301 FourCC EditList::BoxType() const { return FOURCC_ELST; }
303 bool EditList::Parse(BoxReader* reader) {
304 uint32 count;
305 RCHECK(reader->ReadFullBoxHeader() && reader->Read4(&count));
307 if (reader->version() == 1) {
308 RCHECK(reader->HasBytes(count * 20));
309 } else {
310 RCHECK(reader->HasBytes(count * 12));
312 edits.resize(count);
314 for (std::vector<EditListEntry>::iterator edit = edits.begin();
315 edit != edits.end(); ++edit) {
316 if (reader->version() == 1) {
317 RCHECK(reader->Read8(&edit->segment_duration) &&
318 reader->Read8s(&edit->media_time));
319 } else {
320 RCHECK(reader->Read4Into8(&edit->segment_duration) &&
321 reader->Read4sInto8s(&edit->media_time));
323 RCHECK(reader->Read2s(&edit->media_rate_integer) &&
324 reader->Read2s(&edit->media_rate_fraction));
326 return true;
329 Edit::Edit() {}
330 Edit::~Edit() {}
331 FourCC Edit::BoxType() const { return FOURCC_EDTS; }
333 bool Edit::Parse(BoxReader* reader) {
334 return reader->ScanChildren() && reader->ReadChild(&list);
337 HandlerReference::HandlerReference() : type(kInvalid) {}
338 HandlerReference::~HandlerReference() {}
339 FourCC HandlerReference::BoxType() const { return FOURCC_HDLR; }
341 bool HandlerReference::Parse(BoxReader* reader) {
342 FourCC hdlr_type;
343 RCHECK(reader->SkipBytes(8) && reader->ReadFourCC(&hdlr_type));
344 // Note: remaining fields in box ignored
345 if (hdlr_type == FOURCC_VIDE) {
346 type = kVideo;
347 } else if (hdlr_type == FOURCC_SOUN) {
348 type = kAudio;
349 } else {
350 type = kInvalid;
352 return true;
355 AVCDecoderConfigurationRecord::AVCDecoderConfigurationRecord()
356 : version(0),
357 profile_indication(0),
358 profile_compatibility(0),
359 avc_level(0),
360 length_size(0) {}
362 AVCDecoderConfigurationRecord::~AVCDecoderConfigurationRecord() {}
363 FourCC AVCDecoderConfigurationRecord::BoxType() const { return FOURCC_AVCC; }
365 bool AVCDecoderConfigurationRecord::Parse(BoxReader* reader) {
366 return ParseInternal(reader, reader->log_cb());
369 bool AVCDecoderConfigurationRecord::Parse(const uint8* data, int data_size) {
370 BufferReader reader(data, data_size);
371 return ParseInternal(&reader, LogCB());
374 bool AVCDecoderConfigurationRecord::ParseInternal(BufferReader* reader,
375 const LogCB& log_cb) {
376 RCHECK(reader->Read1(&version) && version == 1 &&
377 reader->Read1(&profile_indication) &&
378 reader->Read1(&profile_compatibility) &&
379 reader->Read1(&avc_level));
381 uint8 length_size_minus_one;
382 RCHECK(reader->Read1(&length_size_minus_one));
383 length_size = (length_size_minus_one & 0x3) + 1;
385 RCHECK(length_size != 3); // Only values of 1, 2, and 4 are valid.
387 uint8 num_sps;
388 RCHECK(reader->Read1(&num_sps));
389 num_sps &= 0x1f;
391 sps_list.resize(num_sps);
392 for (int i = 0; i < num_sps; i++) {
393 uint16 sps_length;
394 RCHECK(reader->Read2(&sps_length) &&
395 reader->ReadVec(&sps_list[i], sps_length));
396 RCHECK(sps_list[i].size() > 4);
398 if (!log_cb.is_null()) {
399 MEDIA_LOG(log_cb) << "Video codec: avc1." << std::hex
400 << static_cast<int>(sps_list[i][1])
401 << static_cast<int>(sps_list[i][2])
402 << static_cast<int>(sps_list[i][3]);
406 uint8 num_pps;
407 RCHECK(reader->Read1(&num_pps));
409 pps_list.resize(num_pps);
410 for (int i = 0; i < num_pps; i++) {
411 uint16 pps_length;
412 RCHECK(reader->Read2(&pps_length) &&
413 reader->ReadVec(&pps_list[i], pps_length));
416 return true;
419 PixelAspectRatioBox::PixelAspectRatioBox() : h_spacing(1), v_spacing(1) {}
420 PixelAspectRatioBox::~PixelAspectRatioBox() {}
421 FourCC PixelAspectRatioBox::BoxType() const { return FOURCC_PASP; }
423 bool PixelAspectRatioBox::Parse(BoxReader* reader) {
424 RCHECK(reader->Read4(&h_spacing) &&
425 reader->Read4(&v_spacing));
426 return true;
429 VideoSampleEntry::VideoSampleEntry()
430 : format(FOURCC_NULL),
431 data_reference_index(0),
432 width(0),
433 height(0) {}
435 VideoSampleEntry::~VideoSampleEntry() {}
436 FourCC VideoSampleEntry::BoxType() const {
437 DCHECK(false) << "VideoSampleEntry should be parsed according to the "
438 << "handler type recovered in its Media ancestor.";
439 return FOURCC_NULL;
442 bool VideoSampleEntry::Parse(BoxReader* reader) {
443 format = reader->type();
444 RCHECK(reader->SkipBytes(6) &&
445 reader->Read2(&data_reference_index) &&
446 reader->SkipBytes(16) &&
447 reader->Read2(&width) &&
448 reader->Read2(&height) &&
449 reader->SkipBytes(50));
451 RCHECK(reader->ScanChildren() &&
452 reader->MaybeReadChild(&pixel_aspect));
454 if (format == FOURCC_ENCV) {
455 // Continue scanning until a recognized protection scheme is found, or until
456 // we run out of protection schemes.
457 while (sinf.type.type != FOURCC_CENC) {
458 if (!reader->ReadChild(&sinf))
459 return false;
463 if (IsFormatValid())
464 RCHECK(reader->ReadChild(&avcc));
466 return true;
469 bool VideoSampleEntry::IsFormatValid() const {
470 return format == FOURCC_AVC1 || format == FOURCC_AVC3 ||
471 (format == FOURCC_ENCV && (sinf.format.format == FOURCC_AVC1 ||
472 sinf.format.format == FOURCC_AVC3));
475 ElementaryStreamDescriptor::ElementaryStreamDescriptor()
476 : object_type(kForbidden) {}
478 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
480 FourCC ElementaryStreamDescriptor::BoxType() const {
481 return FOURCC_ESDS;
484 bool ElementaryStreamDescriptor::Parse(BoxReader* reader) {
485 std::vector<uint8> data;
486 ESDescriptor es_desc;
488 RCHECK(reader->ReadFullBoxHeader());
489 RCHECK(reader->ReadVec(&data, reader->size() - reader->pos()));
490 RCHECK(es_desc.Parse(data));
492 object_type = es_desc.object_type();
494 if (object_type != 0x40) {
495 MEDIA_LOG(reader->log_cb()) << "Audio codec: mp4a."
496 << std::hex << static_cast<int>(object_type);
499 if (es_desc.IsAAC(object_type))
500 RCHECK(aac.Parse(es_desc.decoder_specific_info(), reader->log_cb()));
502 return true;
505 AudioSampleEntry::AudioSampleEntry()
506 : format(FOURCC_NULL),
507 data_reference_index(0),
508 channelcount(0),
509 samplesize(0),
510 samplerate(0) {}
512 AudioSampleEntry::~AudioSampleEntry() {}
514 FourCC AudioSampleEntry::BoxType() const {
515 DCHECK(false) << "AudioSampleEntry should be parsed according to the "
516 << "handler type recovered in its Media ancestor.";
517 return FOURCC_NULL;
520 bool AudioSampleEntry::Parse(BoxReader* reader) {
521 format = reader->type();
522 RCHECK(reader->SkipBytes(6) &&
523 reader->Read2(&data_reference_index) &&
524 reader->SkipBytes(8) &&
525 reader->Read2(&channelcount) &&
526 reader->Read2(&samplesize) &&
527 reader->SkipBytes(4) &&
528 reader->Read4(&samplerate));
529 // Convert from 16.16 fixed point to integer
530 samplerate >>= 16;
532 RCHECK(reader->ScanChildren());
533 if (format == FOURCC_ENCA) {
534 // Continue scanning until a recognized protection scheme is found, or until
535 // we run out of protection schemes.
536 while (sinf.type.type != FOURCC_CENC) {
537 if (!reader->ReadChild(&sinf))
538 return false;
542 // ESDS is not valid in case of EAC3.
543 RCHECK(reader->MaybeReadChild(&esds));
544 return true;
547 MediaHeader::MediaHeader()
548 : creation_time(0),
549 modification_time(0),
550 timescale(0),
551 duration(0) {}
552 MediaHeader::~MediaHeader() {}
553 FourCC MediaHeader::BoxType() const { return FOURCC_MDHD; }
555 bool MediaHeader::Parse(BoxReader* reader) {
556 RCHECK(reader->ReadFullBoxHeader());
558 if (reader->version() == 1) {
559 RCHECK(reader->Read8(&creation_time) &&
560 reader->Read8(&modification_time) &&
561 reader->Read4(&timescale) &&
562 reader->Read8(&duration));
563 } else {
564 RCHECK(reader->Read4Into8(&creation_time) &&
565 reader->Read4Into8(&modification_time) &&
566 reader->Read4(&timescale) &&
567 reader->Read4Into8(&duration));
569 // Skip language information
570 return reader->SkipBytes(4);
573 MediaInformation::MediaInformation() {}
574 MediaInformation::~MediaInformation() {}
575 FourCC MediaInformation::BoxType() const { return FOURCC_MINF; }
577 bool MediaInformation::Parse(BoxReader* reader) {
578 return reader->ScanChildren() &&
579 reader->ReadChild(&sample_table);
582 Media::Media() {}
583 Media::~Media() {}
584 FourCC Media::BoxType() const { return FOURCC_MDIA; }
586 bool Media::Parse(BoxReader* reader) {
587 RCHECK(reader->ScanChildren() &&
588 reader->ReadChild(&header) &&
589 reader->ReadChild(&handler));
591 // Maddeningly, the HandlerReference box specifies how to parse the
592 // SampleDescription box, making the latter the only box (of those that we
593 // support) which cannot be parsed correctly on its own (or even with
594 // information from its strict ancestor tree). We thus copy the handler type
595 // to the sample description box *before* parsing it to provide this
596 // information while parsing.
597 information.sample_table.description.type = handler.type;
598 RCHECK(reader->ReadChild(&information));
599 return true;
602 Track::Track() {}
603 Track::~Track() {}
604 FourCC Track::BoxType() const { return FOURCC_TRAK; }
606 bool Track::Parse(BoxReader* reader) {
607 RCHECK(reader->ScanChildren() &&
608 reader->ReadChild(&header) &&
609 reader->ReadChild(&media) &&
610 reader->MaybeReadChild(&edit));
611 return true;
614 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
615 MovieExtendsHeader::~MovieExtendsHeader() {}
616 FourCC MovieExtendsHeader::BoxType() const { return FOURCC_MEHD; }
618 bool MovieExtendsHeader::Parse(BoxReader* reader) {
619 RCHECK(reader->ReadFullBoxHeader());
620 if (reader->version() == 1) {
621 RCHECK(reader->Read8(&fragment_duration));
622 } else {
623 RCHECK(reader->Read4Into8(&fragment_duration));
625 return true;
628 TrackExtends::TrackExtends()
629 : track_id(0),
630 default_sample_description_index(0),
631 default_sample_duration(0),
632 default_sample_size(0),
633 default_sample_flags(0) {}
634 TrackExtends::~TrackExtends() {}
635 FourCC TrackExtends::BoxType() const { return FOURCC_TREX; }
637 bool TrackExtends::Parse(BoxReader* reader) {
638 RCHECK(reader->ReadFullBoxHeader() &&
639 reader->Read4(&track_id) &&
640 reader->Read4(&default_sample_description_index) &&
641 reader->Read4(&default_sample_duration) &&
642 reader->Read4(&default_sample_size) &&
643 reader->Read4(&default_sample_flags));
644 return true;
647 MovieExtends::MovieExtends() {}
648 MovieExtends::~MovieExtends() {}
649 FourCC MovieExtends::BoxType() const { return FOURCC_MVEX; }
651 bool MovieExtends::Parse(BoxReader* reader) {
652 header.fragment_duration = 0;
653 return reader->ScanChildren() &&
654 reader->MaybeReadChild(&header) &&
655 reader->ReadChildren(&tracks);
658 Movie::Movie() : fragmented(false) {}
659 Movie::~Movie() {}
660 FourCC Movie::BoxType() const { return FOURCC_MOOV; }
662 bool Movie::Parse(BoxReader* reader) {
663 return reader->ScanChildren() &&
664 reader->ReadChild(&header) &&
665 reader->ReadChildren(&tracks) &&
666 // Media Source specific: 'mvex' required
667 reader->ReadChild(&extends) &&
668 reader->MaybeReadChildren(&pssh);
671 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
672 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
673 FourCC TrackFragmentDecodeTime::BoxType() const { return FOURCC_TFDT; }
675 bool TrackFragmentDecodeTime::Parse(BoxReader* reader) {
676 RCHECK(reader->ReadFullBoxHeader());
677 if (reader->version() == 1)
678 return reader->Read8(&decode_time);
679 else
680 return reader->Read4Into8(&decode_time);
683 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
684 MovieFragmentHeader::~MovieFragmentHeader() {}
685 FourCC MovieFragmentHeader::BoxType() const { return FOURCC_MFHD; }
687 bool MovieFragmentHeader::Parse(BoxReader* reader) {
688 return reader->SkipBytes(4) && reader->Read4(&sequence_number);
691 TrackFragmentHeader::TrackFragmentHeader()
692 : track_id(0),
693 sample_description_index(0),
694 default_sample_duration(0),
695 default_sample_size(0),
696 default_sample_flags(0),
697 has_default_sample_flags(false) {}
699 TrackFragmentHeader::~TrackFragmentHeader() {}
700 FourCC TrackFragmentHeader::BoxType() const { return FOURCC_TFHD; }
702 bool TrackFragmentHeader::Parse(BoxReader* reader) {
703 RCHECK(reader->ReadFullBoxHeader() && reader->Read4(&track_id));
705 // Media Source specific: reject tracks that set 'base-data-offset-present'.
706 // Although the Media Source requires that 'default-base-is-moof' (14496-12
707 // Amendment 2) be set, we omit this check as many otherwise-valid files in
708 // the wild don't set it.
710 // RCHECK((flags & 0x020000) && !(flags & 0x1));
711 RCHECK(!(reader->flags() & 0x1));
713 if (reader->flags() & 0x2) {
714 RCHECK(reader->Read4(&sample_description_index));
715 } else {
716 sample_description_index = 0;
719 if (reader->flags() & 0x8) {
720 RCHECK(reader->Read4(&default_sample_duration));
721 } else {
722 default_sample_duration = 0;
725 if (reader->flags() & 0x10) {
726 RCHECK(reader->Read4(&default_sample_size));
727 } else {
728 default_sample_size = 0;
731 if (reader->flags() & 0x20) {
732 RCHECK(reader->Read4(&default_sample_flags));
733 has_default_sample_flags = true;
734 } else {
735 has_default_sample_flags = false;
738 return true;
741 TrackFragmentRun::TrackFragmentRun()
742 : sample_count(0), data_offset(0) {}
743 TrackFragmentRun::~TrackFragmentRun() {}
744 FourCC TrackFragmentRun::BoxType() const { return FOURCC_TRUN; }
746 bool TrackFragmentRun::Parse(BoxReader* reader) {
747 RCHECK(reader->ReadFullBoxHeader() &&
748 reader->Read4(&sample_count));
749 const uint32 flags = reader->flags();
751 bool data_offset_present = (flags & 0x1) != 0;
752 bool first_sample_flags_present = (flags & 0x4) != 0;
753 bool sample_duration_present = (flags & 0x100) != 0;
754 bool sample_size_present = (flags & 0x200) != 0;
755 bool sample_flags_present = (flags & 0x400) != 0;
756 bool sample_composition_time_offsets_present = (flags & 0x800) != 0;
758 if (data_offset_present) {
759 RCHECK(reader->Read4(&data_offset));
760 } else {
761 data_offset = 0;
764 uint32 first_sample_flags = 0;
765 if (first_sample_flags_present)
766 RCHECK(reader->Read4(&first_sample_flags));
768 int fields = sample_duration_present + sample_size_present +
769 sample_flags_present + sample_composition_time_offsets_present;
770 RCHECK(reader->HasBytes(fields * sample_count));
772 if (sample_duration_present)
773 sample_durations.resize(sample_count);
774 if (sample_size_present)
775 sample_sizes.resize(sample_count);
776 if (sample_flags_present)
777 sample_flags.resize(sample_count);
778 if (sample_composition_time_offsets_present)
779 sample_composition_time_offsets.resize(sample_count);
781 for (uint32 i = 0; i < sample_count; ++i) {
782 if (sample_duration_present)
783 RCHECK(reader->Read4(&sample_durations[i]));
784 if (sample_size_present)
785 RCHECK(reader->Read4(&sample_sizes[i]));
786 if (sample_flags_present)
787 RCHECK(reader->Read4(&sample_flags[i]));
788 if (sample_composition_time_offsets_present)
789 RCHECK(reader->Read4s(&sample_composition_time_offsets[i]));
792 if (first_sample_flags_present) {
793 if (sample_flags.size() == 0) {
794 sample_flags.push_back(first_sample_flags);
795 } else {
796 sample_flags[0] = first_sample_flags;
799 return true;
802 SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
803 SampleToGroup::~SampleToGroup() {}
804 FourCC SampleToGroup::BoxType() const { return FOURCC_SBGP; }
806 bool SampleToGroup::Parse(BoxReader* reader) {
807 RCHECK(reader->ReadFullBoxHeader() &&
808 reader->Read4(&grouping_type));
810 if (reader->version() == 1)
811 RCHECK(reader->Read4(&grouping_type_parameter));
813 if (grouping_type != FOURCC_SEIG) {
814 DLOG(WARNING) << "SampleToGroup box with grouping_type '" << grouping_type
815 << "' is not supported.";
816 return true;
819 uint32 count;
820 RCHECK(reader->Read4(&count));
821 entries.resize(count);
822 for (uint32 i = 0; i < count; ++i) {
823 RCHECK(reader->Read4(&entries[i].sample_count) &&
824 reader->Read4(&entries[i].group_description_index));
826 return true;
829 CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
830 : is_encrypted(false), iv_size(0) {}
831 CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() {}
833 SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
834 SampleGroupDescription::~SampleGroupDescription() {}
835 FourCC SampleGroupDescription::BoxType() const { return FOURCC_SGPD; }
837 bool SampleGroupDescription::Parse(BoxReader* reader) {
838 RCHECK(reader->ReadFullBoxHeader() &&
839 reader->Read4(&grouping_type));
841 if (grouping_type != FOURCC_SEIG) {
842 DLOG(WARNING) << "SampleGroupDescription box with grouping_type '"
843 << grouping_type << "' is not supported.";
844 return true;
847 const uint8 version = reader->version();
849 const size_t kKeyIdSize = 16;
850 const size_t kEntrySize = sizeof(uint32) + kKeyIdSize;
851 uint32 default_length = 0;
852 if (version == 1) {
853 RCHECK(reader->Read4(&default_length));
854 RCHECK(default_length == 0 || default_length >= kEntrySize);
857 uint32 count;
858 RCHECK(reader->Read4(&count));
859 entries.resize(count);
860 for (uint32 i = 0; i < count; ++i) {
861 if (version == 1) {
862 if (default_length == 0) {
863 uint32 description_length = 0;
864 RCHECK(reader->Read4(&description_length));
865 RCHECK(description_length >= kEntrySize);
869 uint8 flag;
870 RCHECK(reader->SkipBytes(2) && // reserved.
871 reader->Read1(&flag) &&
872 reader->Read1(&entries[i].iv_size) &&
873 reader->ReadVec(&entries[i].key_id, kKeyIdSize));
875 entries[i].is_encrypted = (flag != 0);
876 if (entries[i].is_encrypted) {
877 RCHECK(entries[i].iv_size == 8 || entries[i].iv_size == 16);
878 } else {
879 RCHECK(entries[i].iv_size == 0);
882 return true;
885 TrackFragment::TrackFragment() {}
886 TrackFragment::~TrackFragment() {}
887 FourCC TrackFragment::BoxType() const { return FOURCC_TRAF; }
889 bool TrackFragment::Parse(BoxReader* reader) {
890 RCHECK(reader->ScanChildren() &&
891 reader->ReadChild(&header) &&
892 // Media Source specific: 'tfdt' required
893 reader->ReadChild(&decode_time) &&
894 reader->MaybeReadChildren(&runs) &&
895 reader->MaybeReadChild(&auxiliary_offset) &&
896 reader->MaybeReadChild(&auxiliary_size) &&
897 reader->MaybeReadChild(&sdtp));
899 // There could be multiple SampleGroupDescription and SampleToGroup boxes with
900 // different grouping types. For common encryption, the relevant grouping type
901 // is 'seig'. Continue reading until 'seig' is found, or until running out of
902 // child boxes.
903 while (sample_group_description.grouping_type != FOURCC_SEIG &&
904 reader->HasChild(&sample_group_description)) {
905 RCHECK(reader->ReadChild(&sample_group_description));
907 while (sample_to_group.grouping_type != FOURCC_SEIG &&
908 reader->HasChild(&sample_to_group)) {
909 RCHECK(reader->ReadChild(&sample_to_group));
911 return true;
914 MovieFragment::MovieFragment() {}
915 MovieFragment::~MovieFragment() {}
916 FourCC MovieFragment::BoxType() const { return FOURCC_MOOF; }
918 bool MovieFragment::Parse(BoxReader* reader) {
919 RCHECK(reader->ScanChildren() &&
920 reader->ReadChild(&header) &&
921 reader->ReadChildren(&tracks) &&
922 reader->MaybeReadChildren(&pssh));
923 return true;
926 IndependentAndDisposableSamples::IndependentAndDisposableSamples() {}
927 IndependentAndDisposableSamples::~IndependentAndDisposableSamples() {}
928 FourCC IndependentAndDisposableSamples::BoxType() const { return FOURCC_SDTP; }
930 bool IndependentAndDisposableSamples::Parse(BoxReader* reader) {
931 RCHECK(reader->ReadFullBoxHeader());
932 RCHECK(reader->version() == 0);
933 RCHECK(reader->flags() == 0);
935 int sample_count = reader->size() - reader->pos();
936 sample_depends_on_.resize(sample_count);
937 for (int i = 0; i < sample_count; ++i) {
938 uint8 sample_info;
939 RCHECK(reader->Read1(&sample_info));
941 sample_depends_on_[i] =
942 static_cast<SampleDependsOn>((sample_info >> 4) & 0x3);
944 RCHECK(sample_depends_on_[i] != kSampleDependsOnReserved);
947 return true;
950 SampleDependsOn IndependentAndDisposableSamples::sample_depends_on(
951 size_t i) const {
952 if (i >= sample_depends_on_.size())
953 return kSampleDependsOnUnknown;
955 return sample_depends_on_[i];
958 } // namespace mp4
959 } // namespace media