Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / mp4 / track_run_iterator.cc
blobc3cb0f337e9544f3a0bea3201924f6df577d2962
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/track_run_iterator.h"
7 #include <algorithm>
9 #include "media/base/buffers.h"
10 #include "media/formats/mp4/rcheck.h"
11 #include "media/formats/mp4/sample_to_group_iterator.h"
13 namespace media {
14 namespace mp4 {
16 struct SampleInfo {
17 int size;
18 int duration;
19 int cts_offset;
20 bool is_keyframe;
21 bool is_random_access_point;
22 uint32 cenc_group_description_index;
25 struct TrackRunInfo {
26 uint32 track_id;
27 std::vector<SampleInfo> samples;
28 int64 timescale;
29 int64 start_dts;
30 int64 sample_start_offset;
32 bool is_audio;
33 const AudioSampleEntry* audio_description;
34 const VideoSampleEntry* video_description;
35 const SampleGroupDescription* track_sample_encryption_group;
37 int64 aux_info_start_offset; // Only valid if aux_info_total_size > 0.
38 int aux_info_default_size;
39 std::vector<uint8> aux_info_sizes; // Populated if default_size == 0.
40 int aux_info_total_size;
42 std::vector<CencSampleEncryptionInfoEntry> fragment_sample_encryption_info;
44 TrackRunInfo();
45 ~TrackRunInfo();
48 TrackRunInfo::TrackRunInfo()
49 : track_id(0),
50 timescale(-1),
51 start_dts(-1),
52 sample_start_offset(-1),
53 is_audio(false),
54 aux_info_start_offset(-1),
55 aux_info_default_size(-1),
56 aux_info_total_size(-1) {
58 TrackRunInfo::~TrackRunInfo() {}
60 base::TimeDelta TimeDeltaFromRational(int64 numer, int64 denom) {
61 // To avoid overflow, split the following calculation:
62 // (numer * base::Time::kMicrosecondsPerSecond) / denom
63 // into:
64 // (numer / denom) * base::Time::kMicrosecondsPerSecond +
65 // ((numer % denom) * base::Time::kMicrosecondsPerSecond) / denom
66 int64 a = numer / denom;
67 DCHECK_LE((a > 0 ? a : -a), kint64max / base::Time::kMicrosecondsPerSecond);
68 int64 timea_in_us = a * base::Time::kMicrosecondsPerSecond;
70 int64 b = numer % denom;
71 DCHECK_LE((b > 0 ? b : -b), kint64max / base::Time::kMicrosecondsPerSecond);
72 int64 timeb_in_us = (b * base::Time::kMicrosecondsPerSecond) / denom;
74 DCHECK((timeb_in_us < 0) || (timea_in_us <= kint64max - timeb_in_us));
75 DCHECK((timeb_in_us > 0) || (timea_in_us >= kint64min - timeb_in_us));
76 return base::TimeDelta::FromMicroseconds(timea_in_us + timeb_in_us);
79 DecodeTimestamp DecodeTimestampFromRational(int64 numer, int64 denom) {
80 return DecodeTimestamp::FromPresentationTime(
81 TimeDeltaFromRational(numer, denom));
84 TrackRunIterator::TrackRunIterator(const Movie* moov,
85 const scoped_refptr<MediaLog>& media_log)
86 : moov_(moov), media_log_(media_log), sample_offset_(0) {
87 CHECK(moov);
90 TrackRunIterator::~TrackRunIterator() {}
92 static bool PopulateSampleInfo(const TrackExtends& trex,
93 const TrackFragmentHeader& tfhd,
94 const TrackFragmentRun& trun,
95 const int64 edit_list_offset,
96 const uint32 i,
97 SampleInfo* sample_info,
98 const SampleDependsOn sdtp_sample_depends_on,
99 const scoped_refptr<MediaLog>& media_log) {
100 if (i < trun.sample_sizes.size()) {
101 sample_info->size = trun.sample_sizes[i];
102 } else if (tfhd.default_sample_size > 0) {
103 sample_info->size = tfhd.default_sample_size;
104 } else {
105 sample_info->size = trex.default_sample_size;
108 if (i < trun.sample_durations.size()) {
109 sample_info->duration = trun.sample_durations[i];
110 } else if (tfhd.default_sample_duration > 0) {
111 sample_info->duration = tfhd.default_sample_duration;
112 } else {
113 sample_info->duration = trex.default_sample_duration;
116 if (i < trun.sample_composition_time_offsets.size()) {
117 sample_info->cts_offset = trun.sample_composition_time_offsets[i];
118 } else {
119 sample_info->cts_offset = 0;
121 sample_info->cts_offset += edit_list_offset;
123 uint32 flags;
124 if (i < trun.sample_flags.size()) {
125 flags = trun.sample_flags[i];
126 } else if (tfhd.has_default_sample_flags) {
127 flags = tfhd.default_sample_flags;
128 } else {
129 flags = trex.default_sample_flags;
132 SampleDependsOn sample_depends_on =
133 static_cast<SampleDependsOn>((flags >> 24) & 0x3);
135 if (sample_depends_on == kSampleDependsOnUnknown)
136 sample_depends_on = sdtp_sample_depends_on;
138 // ISO/IEC 14496-12 Section 8.8.3.1 : The negation of |sample_is_sync_sample|
139 // provides the same information as the sync sample table [8.6.2]. When
140 // |sample_is_sync_sample| is true for a sample, it is the same as if the
141 // sample were not in a movie fragment and marked with an entry in the sync
142 // sample table (or, if all samples are sync samples, the sync sample table
143 // were absent).
144 bool sample_is_sync_sample = !(flags & kSampleIsNonSyncSample);
145 sample_info->is_random_access_point = sample_is_sync_sample;
147 switch (sample_depends_on) {
148 case kSampleDependsOnUnknown:
149 sample_info->is_keyframe = sample_is_sync_sample;
150 break;
152 case kSampleDependsOnOthers:
153 sample_info->is_keyframe = false;
154 break;
156 case kSampleDependsOnNoOther:
157 sample_info->is_keyframe = true;
158 break;
160 case kSampleDependsOnReserved:
161 MEDIA_LOG(ERROR, media_log) << "Reserved value used in sample dependency"
162 " info.";
163 return false;
165 return true;
168 static const CencSampleEncryptionInfoEntry* GetSampleEncryptionInfoEntry(
169 const TrackRunInfo& run_info,
170 uint32 group_description_index) {
171 const std::vector<CencSampleEncryptionInfoEntry>* entries = nullptr;
173 // ISO-14496-12 Section 8.9.2.3 and 8.9.4 : group description index
174 // (1) ranges from 1 to the number of sample group entries in the track
175 // level SampleGroupDescription Box, or (2) takes the value 0 to
176 // indicate that this sample is a member of no group, in this case, the
177 // sample is associated with the default values specified in
178 // TrackEncryption Box, or (3) starts at 0x10001, i.e. the index value
179 // 1, with the value 1 in the top 16 bits, to reference fragment-local
180 // SampleGroupDescription Box.
181 // Case (2) is not supported here. The caller must handle it externally
182 // before invoking this function.
183 DCHECK_NE(group_description_index, 0u);
184 if (group_description_index >
185 SampleToGroupEntry::kFragmentGroupDescriptionIndexBase) {
186 group_description_index -=
187 SampleToGroupEntry::kFragmentGroupDescriptionIndexBase;
188 entries = &run_info.fragment_sample_encryption_info;
189 } else {
190 entries = &run_info.track_sample_encryption_group->entries;
193 // |group_description_index| is 1-based.
194 DCHECK_LE(group_description_index, entries->size());
195 return (group_description_index > entries->size())
196 ? nullptr
197 : &(*entries)[group_description_index - 1];
200 // In well-structured encrypted media, each track run will be immediately
201 // preceded by its auxiliary information; this is the only optimal storage
202 // pattern in terms of minimum number of bytes from a serial stream needed to
203 // begin playback. It also allows us to optimize caching on memory-constrained
204 // architectures, because we can cache the relatively small auxiliary
205 // information for an entire run and then discard data from the input stream,
206 // instead of retaining the entire 'mdat' box.
208 // We optimize for this situation (with no loss of generality) by sorting track
209 // runs during iteration in order of their first data offset (either sample data
210 // or auxiliary data).
211 class CompareMinTrackRunDataOffset {
212 public:
213 bool operator()(const TrackRunInfo& a, const TrackRunInfo& b) {
214 int64 a_aux = a.aux_info_total_size ? a.aux_info_start_offset : kint64max;
215 int64 b_aux = b.aux_info_total_size ? b.aux_info_start_offset : kint64max;
217 int64 a_lesser = std::min(a_aux, a.sample_start_offset);
218 int64 a_greater = std::max(a_aux, a.sample_start_offset);
219 int64 b_lesser = std::min(b_aux, b.sample_start_offset);
220 int64 b_greater = std::max(b_aux, b.sample_start_offset);
222 if (a_lesser == b_lesser) return a_greater < b_greater;
223 return a_lesser < b_lesser;
227 bool TrackRunIterator::Init(const MovieFragment& moof) {
228 runs_.clear();
230 for (size_t i = 0; i < moof.tracks.size(); i++) {
231 const TrackFragment& traf = moof.tracks[i];
233 const Track* trak = NULL;
234 for (size_t t = 0; t < moov_->tracks.size(); t++) {
235 if (moov_->tracks[t].header.track_id == traf.header.track_id)
236 trak = &moov_->tracks[t];
238 RCHECK(trak);
240 const TrackExtends* trex = NULL;
241 for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
242 if (moov_->extends.tracks[t].track_id == traf.header.track_id)
243 trex = &moov_->extends.tracks[t];
245 RCHECK(trex);
247 const SampleDescription& stsd =
248 trak->media.information.sample_table.description;
249 if (stsd.type != kAudio && stsd.type != kVideo) {
250 DVLOG(1) << "Skipping unhandled track type";
251 continue;
253 size_t desc_idx = traf.header.sample_description_index;
254 if (!desc_idx) desc_idx = trex->default_sample_description_index;
255 RCHECK(desc_idx > 0); // Descriptions are one-indexed in the file
256 desc_idx -= 1;
258 // Process edit list to remove CTS offset introduced in the presence of
259 // B-frames (those that contain a single edit with a nonnegative media
260 // time). Other uses of edit lists are not supported, as they are
261 // both uncommon and better served by higher-level protocols.
262 int64 edit_list_offset = 0;
263 const std::vector<EditListEntry>& edits = trak->edit.list.edits;
264 if (!edits.empty()) {
265 if (edits.size() > 1)
266 DVLOG(1) << "Multi-entry edit box detected; some components ignored.";
268 if (edits[0].media_time < 0) {
269 DVLOG(1) << "Empty edit list entry ignored.";
270 } else {
271 edit_list_offset = -edits[0].media_time;
275 SampleToGroupIterator sample_to_group_itr(traf.sample_to_group);
276 bool is_sample_to_group_valid = sample_to_group_itr.IsValid();
278 int64 run_start_dts = traf.decode_time.decode_time;
279 int sample_count_sum = 0;
280 for (size_t j = 0; j < traf.runs.size(); j++) {
281 const TrackFragmentRun& trun = traf.runs[j];
282 TrackRunInfo tri;
283 tri.track_id = traf.header.track_id;
284 tri.timescale = trak->media.header.timescale;
285 tri.start_dts = run_start_dts;
286 tri.sample_start_offset = trun.data_offset;
287 tri.track_sample_encryption_group =
288 &trak->media.information.sample_table.sample_group_description;
289 tri.fragment_sample_encryption_info =
290 traf.sample_group_description.entries;
292 tri.is_audio = (stsd.type == kAudio);
293 if (tri.is_audio) {
294 RCHECK(!stsd.audio_entries.empty());
295 if (desc_idx > stsd.audio_entries.size())
296 desc_idx = 0;
297 tri.audio_description = &stsd.audio_entries[desc_idx];
298 } else {
299 RCHECK(!stsd.video_entries.empty());
300 if (desc_idx > stsd.video_entries.size())
301 desc_idx = 0;
302 tri.video_description = &stsd.video_entries[desc_idx];
305 // Collect information from the auxiliary_offset entry with the same index
306 // in the 'saiz' container as the current run's index in the 'trun'
307 // container, if it is present.
308 if (traf.auxiliary_offset.offsets.size() > j) {
309 // There should be an auxiliary info entry corresponding to each sample
310 // in the auxiliary offset entry's corresponding track run.
311 RCHECK(traf.auxiliary_size.sample_count >=
312 sample_count_sum + trun.sample_count);
313 tri.aux_info_start_offset = traf.auxiliary_offset.offsets[j];
314 tri.aux_info_default_size =
315 traf.auxiliary_size.default_sample_info_size;
316 if (tri.aux_info_default_size == 0) {
317 const std::vector<uint8>& sizes =
318 traf.auxiliary_size.sample_info_sizes;
319 tri.aux_info_sizes.insert(tri.aux_info_sizes.begin(),
320 sizes.begin() + sample_count_sum,
321 sizes.begin() + sample_count_sum + trun.sample_count);
324 // If the default info size is positive, find the total size of the aux
325 // info block from it, otherwise sum over the individual sizes of each
326 // aux info entry in the aux_offset entry.
327 if (tri.aux_info_default_size) {
328 tri.aux_info_total_size =
329 tri.aux_info_default_size * trun.sample_count;
330 } else {
331 tri.aux_info_total_size = 0;
332 for (size_t k = 0; k < trun.sample_count; k++) {
333 tri.aux_info_total_size += tri.aux_info_sizes[k];
336 } else {
337 tri.aux_info_start_offset = -1;
338 tri.aux_info_total_size = 0;
341 tri.samples.resize(trun.sample_count);
342 for (size_t k = 0; k < trun.sample_count; k++) {
343 if (!PopulateSampleInfo(*trex, traf.header, trun, edit_list_offset, k,
344 &tri.samples[k], traf.sdtp.sample_depends_on(k),
345 media_log_)) {
346 return false;
349 run_start_dts += tri.samples[k].duration;
351 if (!is_sample_to_group_valid) {
352 // Set group description index to 0 to read encryption information
353 // from TrackEncryption Box.
354 tri.samples[k].cenc_group_description_index = 0;
355 continue;
358 uint32 index = sample_to_group_itr.group_description_index();
359 tri.samples[k].cenc_group_description_index = index;
360 if (index != 0)
361 RCHECK(GetSampleEncryptionInfoEntry(tri, index));
362 is_sample_to_group_valid = sample_to_group_itr.Advance();
364 runs_.push_back(tri);
365 sample_count_sum += trun.sample_count;
368 // We should have iterated through all samples in SampleToGroup Box.
369 RCHECK(!sample_to_group_itr.IsValid());
372 std::sort(runs_.begin(), runs_.end(), CompareMinTrackRunDataOffset());
373 run_itr_ = runs_.begin();
374 ResetRun();
375 return true;
378 void TrackRunIterator::AdvanceRun() {
379 ++run_itr_;
380 ResetRun();
383 void TrackRunIterator::ResetRun() {
384 if (!IsRunValid()) return;
385 sample_dts_ = run_itr_->start_dts;
386 sample_offset_ = run_itr_->sample_start_offset;
387 sample_itr_ = run_itr_->samples.begin();
388 cenc_info_.clear();
391 void TrackRunIterator::AdvanceSample() {
392 DCHECK(IsSampleValid());
393 sample_dts_ += sample_itr_->duration;
394 sample_offset_ += sample_itr_->size;
395 ++sample_itr_;
398 // This implementation only indicates a need for caching if CENC auxiliary
399 // info is available in the stream.
400 bool TrackRunIterator::AuxInfoNeedsToBeCached() {
401 DCHECK(IsRunValid());
402 return aux_info_size() > 0 && cenc_info_.size() == 0;
405 // This implementation currently only caches CENC auxiliary info.
406 bool TrackRunIterator::CacheAuxInfo(const uint8* buf, int buf_size) {
407 RCHECK(AuxInfoNeedsToBeCached() && buf_size >= aux_info_size());
409 cenc_info_.resize(run_itr_->samples.size());
410 int64 pos = 0;
411 for (size_t i = 0; i < run_itr_->samples.size(); i++) {
412 int info_size = run_itr_->aux_info_default_size;
413 if (!info_size)
414 info_size = run_itr_->aux_info_sizes[i];
416 if (IsSampleEncrypted(i)) {
417 BufferReader reader(buf + pos, info_size);
418 RCHECK(cenc_info_[i].Parse(GetIvSize(i), &reader));
420 pos += info_size;
423 return true;
426 bool TrackRunIterator::IsRunValid() const {
427 return run_itr_ != runs_.end();
430 bool TrackRunIterator::IsSampleValid() const {
431 return IsRunValid() && (sample_itr_ != run_itr_->samples.end());
434 // Because tracks are in sorted order and auxiliary information is cached when
435 // returning samples, it is guaranteed that no data will be required before the
436 // lesser of the minimum data offset of this track and the next in sequence.
437 // (The stronger condition - that no data is required before the minimum data
438 // offset of this track alone - is not guaranteed, because the BMFF spec does
439 // not have any inter-run ordering restrictions.)
440 int64 TrackRunIterator::GetMaxClearOffset() {
441 int64 offset = kint64max;
443 if (IsSampleValid()) {
444 offset = std::min(offset, sample_offset_);
445 if (AuxInfoNeedsToBeCached())
446 offset = std::min(offset, aux_info_offset());
448 if (run_itr_ != runs_.end()) {
449 std::vector<TrackRunInfo>::const_iterator next_run = run_itr_ + 1;
450 if (next_run != runs_.end()) {
451 offset = std::min(offset, next_run->sample_start_offset);
452 if (next_run->aux_info_total_size)
453 offset = std::min(offset, next_run->aux_info_start_offset);
456 if (offset == kint64max) return 0;
457 return offset;
460 uint32 TrackRunIterator::track_id() const {
461 DCHECK(IsRunValid());
462 return run_itr_->track_id;
465 bool TrackRunIterator::is_encrypted() const {
466 DCHECK(IsSampleValid());
467 return IsSampleEncrypted(sample_itr_ - run_itr_->samples.begin());
470 int64 TrackRunIterator::aux_info_offset() const {
471 return run_itr_->aux_info_start_offset;
474 int TrackRunIterator::aux_info_size() const {
475 return run_itr_->aux_info_total_size;
478 bool TrackRunIterator::is_audio() const {
479 DCHECK(IsRunValid());
480 return run_itr_->is_audio;
483 const AudioSampleEntry& TrackRunIterator::audio_description() const {
484 DCHECK(is_audio());
485 DCHECK(run_itr_->audio_description);
486 return *run_itr_->audio_description;
489 const VideoSampleEntry& TrackRunIterator::video_description() const {
490 DCHECK(!is_audio());
491 DCHECK(run_itr_->video_description);
492 return *run_itr_->video_description;
495 int64 TrackRunIterator::sample_offset() const {
496 DCHECK(IsSampleValid());
497 return sample_offset_;
500 int TrackRunIterator::sample_size() const {
501 DCHECK(IsSampleValid());
502 return sample_itr_->size;
505 DecodeTimestamp TrackRunIterator::dts() const {
506 DCHECK(IsSampleValid());
507 return DecodeTimestampFromRational(sample_dts_, run_itr_->timescale);
510 base::TimeDelta TrackRunIterator::cts() const {
511 DCHECK(IsSampleValid());
512 return TimeDeltaFromRational(sample_dts_ + sample_itr_->cts_offset,
513 run_itr_->timescale);
516 base::TimeDelta TrackRunIterator::duration() const {
517 DCHECK(IsSampleValid());
518 return TimeDeltaFromRational(sample_itr_->duration, run_itr_->timescale);
521 bool TrackRunIterator::is_keyframe() const {
522 DCHECK(IsSampleValid());
523 return sample_itr_->is_keyframe;
526 bool TrackRunIterator::is_random_access_point() const {
527 DCHECK(IsSampleValid());
528 return sample_itr_->is_random_access_point;
531 const TrackEncryption& TrackRunIterator::track_encryption() const {
532 if (is_audio())
533 return audio_description().sinf.info.track_encryption;
534 return video_description().sinf.info.track_encryption;
537 scoped_ptr<DecryptConfig> TrackRunIterator::GetDecryptConfig() {
538 DCHECK(is_encrypted());
540 if (cenc_info_.empty()) {
541 DCHECK_EQ(0, aux_info_size());
542 MEDIA_LOG(ERROR, media_log_) << "Aux Info is not available.";
543 return scoped_ptr<DecryptConfig>();
546 size_t sample_idx = sample_itr_ - run_itr_->samples.begin();
547 DCHECK_LT(sample_idx, cenc_info_.size());
548 const FrameCENCInfo& cenc_info = cenc_info_[sample_idx];
550 size_t total_size = 0;
551 if (!cenc_info.subsamples.empty() &&
552 (!cenc_info.GetTotalSizeOfSubsamples(&total_size) ||
553 total_size != static_cast<size_t>(sample_size()))) {
554 MEDIA_LOG(ERROR, media_log_) << "Incorrect CENC subsample size.";
555 return scoped_ptr<DecryptConfig>();
558 const std::vector<uint8>& kid = GetKeyId(sample_idx);
559 return scoped_ptr<DecryptConfig>(new DecryptConfig(
560 std::string(reinterpret_cast<const char*>(&kid[0]), kid.size()),
561 std::string(reinterpret_cast<const char*>(cenc_info.iv),
562 arraysize(cenc_info.iv)),
563 cenc_info.subsamples));
566 uint32 TrackRunIterator::GetGroupDescriptionIndex(uint32 sample_index) const {
567 DCHECK(IsRunValid());
568 DCHECK_LT(sample_index, run_itr_->samples.size());
569 return run_itr_->samples[sample_index].cenc_group_description_index;
572 bool TrackRunIterator::IsSampleEncrypted(size_t sample_index) const {
573 uint32 index = GetGroupDescriptionIndex(sample_index);
574 return (index == 0)
575 ? track_encryption().is_encrypted
576 : GetSampleEncryptionInfoEntry(*run_itr_, index)->is_encrypted;
579 const std::vector<uint8>& TrackRunIterator::GetKeyId(
580 size_t sample_index) const {
581 uint32 index = GetGroupDescriptionIndex(sample_index);
582 return (index == 0) ? track_encryption().default_kid
583 : GetSampleEncryptionInfoEntry(*run_itr_, index)->key_id;
586 uint8 TrackRunIterator::GetIvSize(size_t sample_index) const {
587 uint32 index = GetGroupDescriptionIndex(sample_index);
588 return (index == 0) ? track_encryption().default_iv_size
589 : GetSampleEncryptionInfoEntry(*run_itr_, index)->iv_size;
592 } // namespace mp4
593 } // namespace media