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"
9 #include "media/base/buffers.h"
10 #include "media/base/stream_parser_buffer.h"
11 #include "media/formats/mp4/rcheck.h"
12 #include "media/formats/mp4/sample_to_group_iterator.h"
15 static const uint32 kSampleIsDifferenceSampleFlagMask = 0x10000;
26 uint32 cenc_group_description_index;
31 std::vector<SampleInfo> samples;
34 int64 sample_start_offset;
37 const AudioSampleEntry* audio_description;
38 const VideoSampleEntry* video_description;
40 int64 aux_info_start_offset; // Only valid if aux_info_total_size > 0.
41 int aux_info_default_size;
42 std::vector<uint8> aux_info_sizes; // Populated if default_size == 0.
43 int aux_info_total_size;
45 std::vector<CencSampleEncryptionInfoEntry> sample_encryption_info;
51 TrackRunInfo::TrackRunInfo()
55 sample_start_offset(-1),
57 aux_info_start_offset(-1),
58 aux_info_default_size(-1),
59 aux_info_total_size(-1) {
61 TrackRunInfo::~TrackRunInfo() {}
63 TimeDelta TimeDeltaFromRational(int64 numer, int64 denom) {
64 // To avoid overflow, split the following calculation:
65 // (numer * base::Time::kMicrosecondsPerSecond) / denom
67 // (numer / denom) * base::Time::kMicrosecondsPerSecond +
68 // ((numer % denom) * base::Time::kMicrosecondsPerSecond) / denom
69 int64 a = numer / denom;
70 DCHECK_LE((a > 0 ? a : -a), kint64max / base::Time::kMicrosecondsPerSecond);
71 int64 timea_in_us = a * base::Time::kMicrosecondsPerSecond;
73 int64 b = numer % denom;
74 DCHECK_LE((b > 0 ? b : -b), kint64max / base::Time::kMicrosecondsPerSecond);
75 int64 timeb_in_us = (b * base::Time::kMicrosecondsPerSecond) / denom;
77 DCHECK((timeb_in_us < 0) || (timea_in_us <= kint64max - timeb_in_us));
78 DCHECK((timeb_in_us > 0) || (timea_in_us >= kint64min - timeb_in_us));
79 return TimeDelta::FromMicroseconds(timea_in_us + timeb_in_us);
82 TrackRunIterator::TrackRunIterator(const Movie* moov,
84 : moov_(moov), log_cb_(log_cb), sample_offset_(0) {
88 TrackRunIterator::~TrackRunIterator() {}
90 static void PopulateSampleInfo(const TrackExtends& trex,
91 const TrackFragmentHeader& tfhd,
92 const TrackFragmentRun& trun,
93 const int64 edit_list_offset,
95 SampleInfo* sample_info,
96 const SampleDependsOn sample_depends_on) {
97 if (i < trun.sample_sizes.size()) {
98 sample_info->size = trun.sample_sizes[i];
99 } else if (tfhd.default_sample_size > 0) {
100 sample_info->size = tfhd.default_sample_size;
102 sample_info->size = trex.default_sample_size;
105 if (i < trun.sample_durations.size()) {
106 sample_info->duration = trun.sample_durations[i];
107 } else if (tfhd.default_sample_duration > 0) {
108 sample_info->duration = tfhd.default_sample_duration;
110 sample_info->duration = trex.default_sample_duration;
113 if (i < trun.sample_composition_time_offsets.size()) {
114 sample_info->cts_offset = trun.sample_composition_time_offsets[i];
116 sample_info->cts_offset = 0;
118 sample_info->cts_offset += edit_list_offset;
121 if (i < trun.sample_flags.size()) {
122 flags = trun.sample_flags[i];
123 } else if (tfhd.has_default_sample_flags) {
124 flags = tfhd.default_sample_flags;
126 flags = trex.default_sample_flags;
129 switch (sample_depends_on) {
130 case kSampleDependsOnUnknown:
131 sample_info->is_keyframe = !(flags & kSampleIsDifferenceSampleFlagMask);
134 case kSampleDependsOnOthers:
135 sample_info->is_keyframe = false;
138 case kSampleDependsOnNoOther:
139 sample_info->is_keyframe = true;
142 case kSampleDependsOnReserved:
147 // In well-structured encrypted media, each track run will be immediately
148 // preceded by its auxiliary information; this is the only optimal storage
149 // pattern in terms of minimum number of bytes from a serial stream needed to
150 // begin playback. It also allows us to optimize caching on memory-constrained
151 // architectures, because we can cache the relatively small auxiliary
152 // information for an entire run and then discard data from the input stream,
153 // instead of retaining the entire 'mdat' box.
155 // We optimize for this situation (with no loss of generality) by sorting track
156 // runs during iteration in order of their first data offset (either sample data
157 // or auxiliary data).
158 class CompareMinTrackRunDataOffset {
160 bool operator()(const TrackRunInfo& a, const TrackRunInfo& b) {
161 int64 a_aux = a.aux_info_total_size ? a.aux_info_start_offset : kint64max;
162 int64 b_aux = b.aux_info_total_size ? b.aux_info_start_offset : kint64max;
164 int64 a_lesser = std::min(a_aux, a.sample_start_offset);
165 int64 a_greater = std::max(a_aux, a.sample_start_offset);
166 int64 b_lesser = std::min(b_aux, b.sample_start_offset);
167 int64 b_greater = std::max(b_aux, b.sample_start_offset);
169 if (a_lesser == b_lesser) return a_greater < b_greater;
170 return a_lesser < b_lesser;
174 bool TrackRunIterator::Init(const MovieFragment& moof) {
177 for (size_t i = 0; i < moof.tracks.size(); i++) {
178 const TrackFragment& traf = moof.tracks[i];
180 const Track* trak = NULL;
181 for (size_t t = 0; t < moov_->tracks.size(); t++) {
182 if (moov_->tracks[t].header.track_id == traf.header.track_id)
183 trak = &moov_->tracks[t];
187 const TrackExtends* trex = NULL;
188 for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
189 if (moov_->extends.tracks[t].track_id == traf.header.track_id)
190 trex = &moov_->extends.tracks[t];
194 const SampleDescription& stsd =
195 trak->media.information.sample_table.description;
196 if (stsd.type != kAudio && stsd.type != kVideo) {
197 DVLOG(1) << "Skipping unhandled track type";
200 size_t desc_idx = traf.header.sample_description_index;
201 if (!desc_idx) desc_idx = trex->default_sample_description_index;
202 RCHECK(desc_idx > 0); // Descriptions are one-indexed in the file
205 // Process edit list to remove CTS offset introduced in the presence of
206 // B-frames (those that contain a single edit with a nonnegative media
207 // time). Other uses of edit lists are not supported, as they are
208 // both uncommon and better served by higher-level protocols.
209 int64 edit_list_offset = 0;
210 const std::vector<EditListEntry>& edits = trak->edit.list.edits;
211 if (!edits.empty()) {
212 if (edits.size() > 1)
213 DVLOG(1) << "Multi-entry edit box detected; some components ignored.";
215 if (edits[0].media_time < 0) {
216 DVLOG(1) << "Empty edit list entry ignored.";
218 edit_list_offset = -edits[0].media_time;
222 SampleToGroupIterator sample_to_group_itr(traf.sample_to_group);
223 bool is_sample_to_group_valid = sample_to_group_itr.IsValid();
225 int64 run_start_dts = traf.decode_time.decode_time;
226 int sample_count_sum = 0;
227 bool is_sync_sample_box_present =
228 trak->media.information.sample_table.sync_sample.is_present;
229 for (size_t j = 0; j < traf.runs.size(); j++) {
230 const TrackFragmentRun& trun = traf.runs[j];
232 tri.track_id = traf.header.track_id;
233 tri.timescale = trak->media.header.timescale;
234 tri.start_dts = run_start_dts;
235 tri.sample_start_offset = trun.data_offset;
236 tri.sample_encryption_info = traf.sample_group_description.entries;
238 tri.is_audio = (stsd.type == kAudio);
240 RCHECK(!stsd.audio_entries.empty());
241 if (desc_idx > stsd.audio_entries.size())
243 tri.audio_description = &stsd.audio_entries[desc_idx];
245 RCHECK(!stsd.video_entries.empty());
246 if (desc_idx > stsd.video_entries.size())
248 tri.video_description = &stsd.video_entries[desc_idx];
251 // Collect information from the auxiliary_offset entry with the same index
252 // in the 'saiz' container as the current run's index in the 'trun'
253 // container, if it is present.
254 if (traf.auxiliary_offset.offsets.size() > j) {
255 // There should be an auxiliary info entry corresponding to each sample
256 // in the auxiliary offset entry's corresponding track run.
257 RCHECK(traf.auxiliary_size.sample_count >=
258 sample_count_sum + trun.sample_count);
259 tri.aux_info_start_offset = traf.auxiliary_offset.offsets[j];
260 tri.aux_info_default_size =
261 traf.auxiliary_size.default_sample_info_size;
262 if (tri.aux_info_default_size == 0) {
263 const std::vector<uint8>& sizes =
264 traf.auxiliary_size.sample_info_sizes;
265 tri.aux_info_sizes.insert(tri.aux_info_sizes.begin(),
266 sizes.begin() + sample_count_sum,
267 sizes.begin() + sample_count_sum + trun.sample_count);
270 // If the default info size is positive, find the total size of the aux
271 // info block from it, otherwise sum over the individual sizes of each
272 // aux info entry in the aux_offset entry.
273 if (tri.aux_info_default_size) {
274 tri.aux_info_total_size =
275 tri.aux_info_default_size * trun.sample_count;
277 tri.aux_info_total_size = 0;
278 for (size_t k = 0; k < trun.sample_count; k++) {
279 tri.aux_info_total_size += tri.aux_info_sizes[k];
283 tri.aux_info_start_offset = -1;
284 tri.aux_info_total_size = 0;
287 tri.samples.resize(trun.sample_count);
288 for (size_t k = 0; k < trun.sample_count; k++) {
289 PopulateSampleInfo(*trex, traf.header, trun, edit_list_offset,
290 k, &tri.samples[k], traf.sdtp.sample_depends_on(k));
291 run_start_dts += tri.samples[k].duration;
293 // ISO-14496-12 Section 8.20.1 : If the sync sample box is not present,
294 // every sample is a random access point.
296 // NOTE: MPEG's "is random access point" concept is equivalent to this
297 // and downstream code's "is keyframe" concept.
298 if (!is_sync_sample_box_present)
299 tri.samples[k].is_keyframe = true;
301 if (!is_sample_to_group_valid) {
302 // Set group description index to 0 to read encryption information
303 // from TrackEncryption Box.
304 tri.samples[k].cenc_group_description_index = 0;
308 // ISO-14496-12 Section 8.9.2.3 and 8.9.4 : group description index
309 // (1) ranges from 1 to the number of sample group entries in the track
310 // level SampleGroupDescription Box, or (2) takes the value 0 to
311 // indicate that this sample is a member of no group, in this case, the
312 // sample is associated with the default values specified in
313 // TrackEncryption Box, or (3) starts at 0x10001, i.e. the index value
314 // 1, with the value 1 in the top 16 bits, to reference fragment-local
315 // SampleGroupDescription Box.
316 // Case (1) is not supported currently. We might not need it either as
317 // the same functionality can be better achieved using (2).
318 uint32 index = sample_to_group_itr.group_description_index();
319 if (index >= SampleToGroupEntry::kFragmentGroupDescriptionIndexBase) {
320 index -= SampleToGroupEntry::kFragmentGroupDescriptionIndexBase;
321 RCHECK(index != 0 && index <= tri.sample_encryption_info.size());
322 } else if (index != 0) {
323 NOTIMPLEMENTED() << "'sgpd' box in 'moov' is not supported.";
326 tri.samples[k].cenc_group_description_index = index;
327 is_sample_to_group_valid = sample_to_group_itr.Advance();
329 runs_.push_back(tri);
330 sample_count_sum += trun.sample_count;
333 // We should have iterated through all samples in SampleToGroup Box.
334 RCHECK(!sample_to_group_itr.IsValid());
337 std::sort(runs_.begin(), runs_.end(), CompareMinTrackRunDataOffset());
338 run_itr_ = runs_.begin();
343 void TrackRunIterator::AdvanceRun() {
348 void TrackRunIterator::ResetRun() {
349 if (!IsRunValid()) return;
350 sample_dts_ = run_itr_->start_dts;
351 sample_offset_ = run_itr_->sample_start_offset;
352 sample_itr_ = run_itr_->samples.begin();
356 void TrackRunIterator::AdvanceSample() {
357 DCHECK(IsSampleValid());
358 sample_dts_ += sample_itr_->duration;
359 sample_offset_ += sample_itr_->size;
363 // This implementation only indicates a need for caching if CENC auxiliary
364 // info is available in the stream.
365 bool TrackRunIterator::AuxInfoNeedsToBeCached() {
366 DCHECK(IsRunValid());
367 return aux_info_size() > 0 && cenc_info_.size() == 0;
370 // This implementation currently only caches CENC auxiliary info.
371 bool TrackRunIterator::CacheAuxInfo(const uint8* buf, int buf_size) {
372 RCHECK(AuxInfoNeedsToBeCached() && buf_size >= aux_info_size());
374 cenc_info_.resize(run_itr_->samples.size());
376 for (size_t i = 0; i < run_itr_->samples.size(); i++) {
377 int info_size = run_itr_->aux_info_default_size;
379 info_size = run_itr_->aux_info_sizes[i];
381 if (IsSampleEncrypted(i)) {
382 BufferReader reader(buf + pos, info_size);
383 RCHECK(cenc_info_[i].Parse(GetIvSize(i), &reader));
391 bool TrackRunIterator::IsRunValid() const {
392 return run_itr_ != runs_.end();
395 bool TrackRunIterator::IsSampleValid() const {
396 return IsRunValid() && (sample_itr_ != run_itr_->samples.end());
399 // Because tracks are in sorted order and auxiliary information is cached when
400 // returning samples, it is guaranteed that no data will be required before the
401 // lesser of the minimum data offset of this track and the next in sequence.
402 // (The stronger condition - that no data is required before the minimum data
403 // offset of this track alone - is not guaranteed, because the BMFF spec does
404 // not have any inter-run ordering restrictions.)
405 int64 TrackRunIterator::GetMaxClearOffset() {
406 int64 offset = kint64max;
408 if (IsSampleValid()) {
409 offset = std::min(offset, sample_offset_);
410 if (AuxInfoNeedsToBeCached())
411 offset = std::min(offset, aux_info_offset());
413 if (run_itr_ != runs_.end()) {
414 std::vector<TrackRunInfo>::const_iterator next_run = run_itr_ + 1;
415 if (next_run != runs_.end()) {
416 offset = std::min(offset, next_run->sample_start_offset);
417 if (next_run->aux_info_total_size)
418 offset = std::min(offset, next_run->aux_info_start_offset);
421 if (offset == kint64max) return 0;
425 uint32 TrackRunIterator::track_id() const {
426 DCHECK(IsRunValid());
427 return run_itr_->track_id;
430 bool TrackRunIterator::is_encrypted() const {
431 DCHECK(IsSampleValid());
432 return IsSampleEncrypted(sample_itr_ - run_itr_->samples.begin());
435 int64 TrackRunIterator::aux_info_offset() const {
436 return run_itr_->aux_info_start_offset;
439 int TrackRunIterator::aux_info_size() const {
440 return run_itr_->aux_info_total_size;
443 bool TrackRunIterator::is_audio() const {
444 DCHECK(IsRunValid());
445 return run_itr_->is_audio;
448 const AudioSampleEntry& TrackRunIterator::audio_description() const {
450 DCHECK(run_itr_->audio_description);
451 return *run_itr_->audio_description;
454 const VideoSampleEntry& TrackRunIterator::video_description() const {
456 DCHECK(run_itr_->video_description);
457 return *run_itr_->video_description;
460 int64 TrackRunIterator::sample_offset() const {
461 DCHECK(IsSampleValid());
462 return sample_offset_;
465 int TrackRunIterator::sample_size() const {
466 DCHECK(IsSampleValid());
467 return sample_itr_->size;
470 TimeDelta TrackRunIterator::dts() const {
471 DCHECK(IsSampleValid());
472 return TimeDeltaFromRational(sample_dts_, run_itr_->timescale);
475 TimeDelta TrackRunIterator::cts() const {
476 DCHECK(IsSampleValid());
477 return TimeDeltaFromRational(sample_dts_ + sample_itr_->cts_offset,
478 run_itr_->timescale);
481 TimeDelta TrackRunIterator::duration() const {
482 DCHECK(IsSampleValid());
483 return TimeDeltaFromRational(sample_itr_->duration, run_itr_->timescale);
486 bool TrackRunIterator::is_keyframe() const {
487 DCHECK(IsSampleValid());
488 return sample_itr_->is_keyframe;
491 const TrackEncryption& TrackRunIterator::track_encryption() const {
493 return audio_description().sinf.info.track_encryption;
494 return video_description().sinf.info.track_encryption;
497 scoped_ptr<DecryptConfig> TrackRunIterator::GetDecryptConfig() {
498 DCHECK(is_encrypted());
500 if (cenc_info_.empty()) {
501 DCHECK_EQ(0, aux_info_size());
502 MEDIA_LOG(log_cb_) << "Aux Info is not available.";
503 return scoped_ptr<DecryptConfig>();
506 size_t sample_idx = sample_itr_ - run_itr_->samples.begin();
507 DCHECK_LT(sample_idx, cenc_info_.size());
508 const FrameCENCInfo& cenc_info = cenc_info_[sample_idx];
510 size_t total_size = 0;
511 if (!cenc_info.subsamples.empty() &&
512 (!cenc_info.GetTotalSizeOfSubsamples(&total_size) ||
513 total_size != static_cast<size_t>(sample_size()))) {
514 MEDIA_LOG(log_cb_) << "Incorrect CENC subsample size.";
515 return scoped_ptr<DecryptConfig>();
518 const std::vector<uint8>& kid = GetKeyId(sample_idx);
519 return scoped_ptr<DecryptConfig>(new DecryptConfig(
520 std::string(reinterpret_cast<const char*>(&kid[0]), kid.size()),
521 std::string(reinterpret_cast<const char*>(cenc_info.iv),
522 arraysize(cenc_info.iv)),
523 cenc_info.subsamples));
526 uint32 TrackRunIterator::GetGroupDescriptionIndex(uint32 sample_index) const {
527 DCHECK(IsRunValid());
528 DCHECK_LT(sample_index, run_itr_->samples.size());
529 return run_itr_->samples[sample_index].cenc_group_description_index;
532 const CencSampleEncryptionInfoEntry&
533 TrackRunIterator::GetSampleEncryptionInfoEntry(
534 uint32 group_description_index) const {
535 DCHECK(IsRunValid());
536 DCHECK_NE(group_description_index, 0u);
537 DCHECK_LE(group_description_index, run_itr_->sample_encryption_info.size());
538 // |group_description_index| is 1-based. Subtract by 1 to index the vector.
539 return run_itr_->sample_encryption_info[group_description_index - 1];
542 bool TrackRunIterator::IsSampleEncrypted(size_t sample_index) const {
543 uint32 index = GetGroupDescriptionIndex(sample_index);
544 return (index == 0) ? track_encryption().is_encrypted
545 : GetSampleEncryptionInfoEntry(index).is_encrypted;
548 const std::vector<uint8>& TrackRunIterator::GetKeyId(
549 size_t sample_index) const {
550 uint32 index = GetGroupDescriptionIndex(sample_index);
551 return (index == 0) ? track_encryption().default_kid
552 : GetSampleEncryptionInfoEntry(index).key_id;
555 uint8 TrackRunIterator::GetIvSize(size_t sample_index) const {
556 uint32 index = GetGroupDescriptionIndex(sample_index);
557 return (index == 0) ? track_encryption().default_iv_size
558 : GetSampleEncryptionInfoEntry(index).iv_size;