Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / media / mp4 / box_definitions.cc
blob6c645be4acd2109b52fe1321df25a498e87e50cf
1 // Copyright (c) 2012 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/mp4/box_definitions.h"
7 #include "base/logging.h"
8 #include "media/mp4/es_descriptor.h"
9 #include "media/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 SampleTable::SampleTable() {}
249 SampleTable::~SampleTable() {}
250 FourCC SampleTable::BoxType() const { return FOURCC_STBL; }
252 bool SampleTable::Parse(BoxReader* reader) {
253 return reader->ScanChildren() &&
254 reader->ReadChild(&description);
257 EditList::EditList() {}
258 EditList::~EditList() {}
259 FourCC EditList::BoxType() const { return FOURCC_ELST; }
261 bool EditList::Parse(BoxReader* reader) {
262 uint32 count;
263 RCHECK(reader->ReadFullBoxHeader() && reader->Read4(&count));
265 if (reader->version() == 1) {
266 RCHECK(reader->HasBytes(count * 20));
267 } else {
268 RCHECK(reader->HasBytes(count * 12));
270 edits.resize(count);
272 for (std::vector<EditListEntry>::iterator edit = edits.begin();
273 edit != edits.end(); ++edit) {
274 if (reader->version() == 1) {
275 RCHECK(reader->Read8(&edit->segment_duration) &&
276 reader->Read8s(&edit->media_time));
277 } else {
278 RCHECK(reader->Read4Into8(&edit->segment_duration) &&
279 reader->Read4sInto8s(&edit->media_time));
281 RCHECK(reader->Read2s(&edit->media_rate_integer) &&
282 reader->Read2s(&edit->media_rate_fraction));
284 return true;
287 Edit::Edit() {}
288 Edit::~Edit() {}
289 FourCC Edit::BoxType() const { return FOURCC_EDTS; }
291 bool Edit::Parse(BoxReader* reader) {
292 return reader->ScanChildren() && reader->ReadChild(&list);
295 HandlerReference::HandlerReference() : type(kInvalid) {}
296 HandlerReference::~HandlerReference() {}
297 FourCC HandlerReference::BoxType() const { return FOURCC_HDLR; }
299 bool HandlerReference::Parse(BoxReader* reader) {
300 FourCC hdlr_type;
301 RCHECK(reader->SkipBytes(8) && reader->ReadFourCC(&hdlr_type));
302 // Note: remaining fields in box ignored
303 if (hdlr_type == FOURCC_VIDE) {
304 type = kVideo;
305 } else if (hdlr_type == FOURCC_SOUN) {
306 type = kAudio;
307 } else {
308 type = kInvalid;
310 return true;
313 AVCDecoderConfigurationRecord::AVCDecoderConfigurationRecord()
314 : version(0),
315 profile_indication(0),
316 profile_compatibility(0),
317 avc_level(0),
318 length_size(0) {}
320 AVCDecoderConfigurationRecord::~AVCDecoderConfigurationRecord() {}
321 FourCC AVCDecoderConfigurationRecord::BoxType() const { return FOURCC_AVCC; }
323 bool AVCDecoderConfigurationRecord::Parse(BoxReader* reader) {
324 RCHECK(reader->Read1(&version) && version == 1 &&
325 reader->Read1(&profile_indication) &&
326 reader->Read1(&profile_compatibility) &&
327 reader->Read1(&avc_level));
329 uint8 length_size_minus_one;
330 RCHECK(reader->Read1(&length_size_minus_one) &&
331 (length_size_minus_one & 0xfc) == 0xfc);
332 length_size = (length_size_minus_one & 0x3) + 1;
334 uint8 num_sps;
335 RCHECK(reader->Read1(&num_sps) && (num_sps & 0xe0) == 0xe0);
336 num_sps &= 0x1f;
338 sps_list.resize(num_sps);
339 for (int i = 0; i < num_sps; i++) {
340 uint16 sps_length;
341 RCHECK(reader->Read2(&sps_length) &&
342 reader->ReadVec(&sps_list[i], sps_length));
345 uint8 num_pps;
346 RCHECK(reader->Read1(&num_pps));
348 pps_list.resize(num_pps);
349 for (int i = 0; i < num_pps; i++) {
350 uint16 pps_length;
351 RCHECK(reader->Read2(&pps_length) &&
352 reader->ReadVec(&pps_list[i], pps_length));
355 return true;
358 PixelAspectRatioBox::PixelAspectRatioBox() : h_spacing(1), v_spacing(1) {}
359 PixelAspectRatioBox::~PixelAspectRatioBox() {}
360 FourCC PixelAspectRatioBox::BoxType() const { return FOURCC_PASP; }
362 bool PixelAspectRatioBox::Parse(BoxReader* reader) {
363 RCHECK(reader->Read4(&h_spacing) &&
364 reader->Read4(&v_spacing));
365 return true;
368 VideoSampleEntry::VideoSampleEntry()
369 : format(FOURCC_NULL),
370 data_reference_index(0),
371 width(0),
372 height(0) {}
374 VideoSampleEntry::~VideoSampleEntry() {}
375 FourCC VideoSampleEntry::BoxType() const {
376 DCHECK(false) << "VideoSampleEntry should be parsed according to the "
377 << "handler type recovered in its Media ancestor.";
378 return FOURCC_NULL;
381 bool VideoSampleEntry::Parse(BoxReader* reader) {
382 format = reader->type();
383 RCHECK(reader->SkipBytes(6) &&
384 reader->Read2(&data_reference_index) &&
385 reader->SkipBytes(16) &&
386 reader->Read2(&width) &&
387 reader->Read2(&height) &&
388 reader->SkipBytes(50));
390 RCHECK(reader->ScanChildren() &&
391 reader->MaybeReadChild(&pixel_aspect));
393 if (format == FOURCC_ENCV) {
394 // Continue scanning until a recognized protection scheme is found, or until
395 // we run out of protection schemes.
396 while (sinf.type.type != FOURCC_CENC) {
397 if (!reader->ReadChild(&sinf))
398 return false;
402 if (format == FOURCC_AVC1 ||
403 (format == FOURCC_ENCV && sinf.format.format == FOURCC_AVC1)) {
404 RCHECK(reader->ReadChild(&avcc));
406 return true;
409 ElementaryStreamDescriptor::ElementaryStreamDescriptor()
410 : object_type(kForbidden) {}
412 ElementaryStreamDescriptor::~ElementaryStreamDescriptor() {}
414 FourCC ElementaryStreamDescriptor::BoxType() const {
415 return FOURCC_ESDS;
418 bool ElementaryStreamDescriptor::Parse(BoxReader* reader) {
419 std::vector<uint8> data;
420 ESDescriptor es_desc;
422 RCHECK(reader->ReadFullBoxHeader());
423 RCHECK(reader->ReadVec(&data, reader->size() - reader->pos()));
424 RCHECK(es_desc.Parse(data));
426 object_type = es_desc.object_type();
428 RCHECK(aac.Parse(es_desc.decoder_specific_info()));
430 return true;
433 AudioSampleEntry::AudioSampleEntry()
434 : format(FOURCC_NULL),
435 data_reference_index(0),
436 channelcount(0),
437 samplesize(0),
438 samplerate(0) {}
440 AudioSampleEntry::~AudioSampleEntry() {}
442 FourCC AudioSampleEntry::BoxType() const {
443 DCHECK(false) << "AudioSampleEntry should be parsed according to the "
444 << "handler type recovered in its Media ancestor.";
445 return FOURCC_NULL;
448 bool AudioSampleEntry::Parse(BoxReader* reader) {
449 format = reader->type();
450 RCHECK(reader->SkipBytes(6) &&
451 reader->Read2(&data_reference_index) &&
452 reader->SkipBytes(8) &&
453 reader->Read2(&channelcount) &&
454 reader->Read2(&samplesize) &&
455 reader->SkipBytes(4) &&
456 reader->Read4(&samplerate));
457 // Convert from 16.16 fixed point to integer
458 samplerate >>= 16;
460 RCHECK(reader->ScanChildren());
461 if (format == FOURCC_ENCA) {
462 // Continue scanning until a recognized protection scheme is found, or until
463 // we run out of protection schemes.
464 while (sinf.type.type != FOURCC_CENC) {
465 if (!reader->ReadChild(&sinf))
466 return false;
470 RCHECK(reader->ReadChild(&esds));
471 return true;
474 MediaHeader::MediaHeader()
475 : creation_time(0),
476 modification_time(0),
477 timescale(0),
478 duration(0) {}
479 MediaHeader::~MediaHeader() {}
480 FourCC MediaHeader::BoxType() const { return FOURCC_MDHD; }
482 bool MediaHeader::Parse(BoxReader* reader) {
483 RCHECK(reader->ReadFullBoxHeader());
485 if (reader->version() == 1) {
486 RCHECK(reader->Read8(&creation_time) &&
487 reader->Read8(&modification_time) &&
488 reader->Read4(&timescale) &&
489 reader->Read8(&duration));
490 } else {
491 RCHECK(reader->Read4Into8(&creation_time) &&
492 reader->Read4Into8(&modification_time) &&
493 reader->Read4(&timescale) &&
494 reader->Read4Into8(&duration));
496 // Skip language information
497 return reader->SkipBytes(4);
500 MediaInformation::MediaInformation() {}
501 MediaInformation::~MediaInformation() {}
502 FourCC MediaInformation::BoxType() const { return FOURCC_MINF; }
504 bool MediaInformation::Parse(BoxReader* reader) {
505 return reader->ScanChildren() &&
506 reader->ReadChild(&sample_table);
509 Media::Media() {}
510 Media::~Media() {}
511 FourCC Media::BoxType() const { return FOURCC_MDIA; }
513 bool Media::Parse(BoxReader* reader) {
514 RCHECK(reader->ScanChildren() &&
515 reader->ReadChild(&header) &&
516 reader->ReadChild(&handler));
518 // Maddeningly, the HandlerReference box specifies how to parse the
519 // SampleDescription box, making the latter the only box (of those that we
520 // support) which cannot be parsed correctly on its own (or even with
521 // information from its strict ancestor tree). We thus copy the handler type
522 // to the sample description box *before* parsing it to provide this
523 // information while parsing.
524 information.sample_table.description.type = handler.type;
525 RCHECK(reader->ReadChild(&information));
526 return true;
529 Track::Track() {}
530 Track::~Track() {}
531 FourCC Track::BoxType() const { return FOURCC_TRAK; }
533 bool Track::Parse(BoxReader* reader) {
534 RCHECK(reader->ScanChildren() &&
535 reader->ReadChild(&header) &&
536 reader->ReadChild(&media) &&
537 reader->MaybeReadChild(&edit));
538 return true;
541 MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
542 MovieExtendsHeader::~MovieExtendsHeader() {}
543 FourCC MovieExtendsHeader::BoxType() const { return FOURCC_MEHD; }
545 bool MovieExtendsHeader::Parse(BoxReader* reader) {
546 RCHECK(reader->ReadFullBoxHeader());
547 if (reader->version() == 1) {
548 RCHECK(reader->Read8(&fragment_duration));
549 } else {
550 RCHECK(reader->Read4Into8(&fragment_duration));
552 return true;
555 TrackExtends::TrackExtends()
556 : track_id(0),
557 default_sample_description_index(0),
558 default_sample_duration(0),
559 default_sample_size(0),
560 default_sample_flags(0) {}
561 TrackExtends::~TrackExtends() {}
562 FourCC TrackExtends::BoxType() const { return FOURCC_TREX; }
564 bool TrackExtends::Parse(BoxReader* reader) {
565 RCHECK(reader->ReadFullBoxHeader() &&
566 reader->Read4(&track_id) &&
567 reader->Read4(&default_sample_description_index) &&
568 reader->Read4(&default_sample_duration) &&
569 reader->Read4(&default_sample_size) &&
570 reader->Read4(&default_sample_flags));
571 return true;
574 MovieExtends::MovieExtends() {}
575 MovieExtends::~MovieExtends() {}
576 FourCC MovieExtends::BoxType() const { return FOURCC_MVEX; }
578 bool MovieExtends::Parse(BoxReader* reader) {
579 header.fragment_duration = 0;
580 return reader->ScanChildren() &&
581 reader->MaybeReadChild(&header) &&
582 reader->ReadChildren(&tracks);
585 Movie::Movie() : fragmented(false) {}
586 Movie::~Movie() {}
587 FourCC Movie::BoxType() const { return FOURCC_MOOV; }
589 bool Movie::Parse(BoxReader* reader) {
590 return reader->ScanChildren() &&
591 reader->ReadChild(&header) &&
592 reader->ReadChildren(&tracks) &&
593 // Media Source specific: 'mvex' required
594 reader->ReadChild(&extends) &&
595 reader->MaybeReadChildren(&pssh);
598 TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
599 TrackFragmentDecodeTime::~TrackFragmentDecodeTime() {}
600 FourCC TrackFragmentDecodeTime::BoxType() const { return FOURCC_TFDT; }
602 bool TrackFragmentDecodeTime::Parse(BoxReader* reader) {
603 RCHECK(reader->ReadFullBoxHeader());
604 if (reader->version() == 1)
605 return reader->Read8(&decode_time);
606 else
607 return reader->Read4Into8(&decode_time);
610 MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
611 MovieFragmentHeader::~MovieFragmentHeader() {}
612 FourCC MovieFragmentHeader::BoxType() const { return FOURCC_MFHD; }
614 bool MovieFragmentHeader::Parse(BoxReader* reader) {
615 return reader->SkipBytes(4) && reader->Read4(&sequence_number);
618 TrackFragmentHeader::TrackFragmentHeader()
619 : track_id(0),
620 sample_description_index(0),
621 default_sample_duration(0),
622 default_sample_size(0),
623 default_sample_flags(0),
624 has_default_sample_flags(false) {}
626 TrackFragmentHeader::~TrackFragmentHeader() {}
627 FourCC TrackFragmentHeader::BoxType() const { return FOURCC_TFHD; }
629 bool TrackFragmentHeader::Parse(BoxReader* reader) {
630 RCHECK(reader->ReadFullBoxHeader() && reader->Read4(&track_id));
632 // Media Source specific: reject tracks that set 'base-data-offset-present'.
633 // Although the Media Source requires that 'default-base-is-moof' (14496-12
634 // Amendment 2) be set, we omit this check as many otherwise-valid files in
635 // the wild don't set it.
637 // RCHECK((flags & 0x020000) && !(flags & 0x1));
638 RCHECK(!(reader->flags() & 0x1));
640 if (reader->flags() & 0x2) {
641 RCHECK(reader->Read4(&sample_description_index));
642 } else {
643 sample_description_index = 0;
646 if (reader->flags() & 0x8) {
647 RCHECK(reader->Read4(&default_sample_duration));
648 } else {
649 default_sample_duration = 0;
652 if (reader->flags() & 0x10) {
653 RCHECK(reader->Read4(&default_sample_size));
654 } else {
655 default_sample_size = 0;
658 if (reader->flags() & 0x20) {
659 RCHECK(reader->Read4(&default_sample_flags));
660 has_default_sample_flags = true;
661 } else {
662 has_default_sample_flags = false;
665 return true;
668 TrackFragmentRun::TrackFragmentRun()
669 : sample_count(0), data_offset(0) {}
670 TrackFragmentRun::~TrackFragmentRun() {}
671 FourCC TrackFragmentRun::BoxType() const { return FOURCC_TRUN; }
673 bool TrackFragmentRun::Parse(BoxReader* reader) {
674 RCHECK(reader->ReadFullBoxHeader() &&
675 reader->Read4(&sample_count));
676 const uint32 flags = reader->flags();
678 bool data_offset_present = (flags & 0x1) != 0;
679 bool first_sample_flags_present = (flags & 0x4) != 0;
680 bool sample_duration_present = (flags & 0x100) != 0;
681 bool sample_size_present = (flags & 0x200) != 0;
682 bool sample_flags_present = (flags & 0x400) != 0;
683 bool sample_composition_time_offsets_present = (flags & 0x800) != 0;
685 if (data_offset_present) {
686 RCHECK(reader->Read4(&data_offset));
687 } else {
688 data_offset = 0;
691 uint32 first_sample_flags;
692 if (first_sample_flags_present)
693 RCHECK(reader->Read4(&first_sample_flags));
695 int fields = sample_duration_present + sample_size_present +
696 sample_flags_present + sample_composition_time_offsets_present;
697 RCHECK(reader->HasBytes(fields * sample_count));
699 if (sample_duration_present)
700 sample_durations.resize(sample_count);
701 if (sample_size_present)
702 sample_sizes.resize(sample_count);
703 if (sample_flags_present)
704 sample_flags.resize(sample_count);
705 if (sample_composition_time_offsets_present)
706 sample_composition_time_offsets.resize(sample_count);
708 for (uint32 i = 0; i < sample_count; ++i) {
709 if (sample_duration_present)
710 RCHECK(reader->Read4(&sample_durations[i]));
711 if (sample_size_present)
712 RCHECK(reader->Read4(&sample_sizes[i]));
713 if (sample_flags_present)
714 RCHECK(reader->Read4(&sample_flags[i]));
715 if (sample_composition_time_offsets_present)
716 RCHECK(reader->Read4s(&sample_composition_time_offsets[i]));
719 if (first_sample_flags_present) {
720 if (sample_flags.size() == 0) {
721 sample_flags.push_back(first_sample_flags);
722 } else {
723 sample_flags[0] = first_sample_flags;
726 return true;
729 TrackFragment::TrackFragment() {}
730 TrackFragment::~TrackFragment() {}
731 FourCC TrackFragment::BoxType() const { return FOURCC_TRAF; }
733 bool TrackFragment::Parse(BoxReader* reader) {
734 return reader->ScanChildren() &&
735 reader->ReadChild(&header) &&
736 // Media Source specific: 'tfdt' required
737 reader->ReadChild(&decode_time) &&
738 reader->MaybeReadChildren(&runs) &&
739 reader->MaybeReadChild(&auxiliary_offset) &&
740 reader->MaybeReadChild(&auxiliary_size);
743 MovieFragment::MovieFragment() {}
744 MovieFragment::~MovieFragment() {}
745 FourCC MovieFragment::BoxType() const { return FOURCC_MOOF; }
747 bool MovieFragment::Parse(BoxReader* reader) {
748 RCHECK(reader->ScanChildren() &&
749 reader->ReadChild(&header) &&
750 reader->ReadChildren(&tracks) &&
751 reader->MaybeReadChildren(&pssh));
752 return true;
755 } // namespace mp4
756 } // namespace media