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/formats/mp4/rcheck.h"
11 #include "media/formats/mp4/sample_to_group_iterator.h"
21 bool is_random_access_point
;
22 uint32 cenc_group_description_index
;
27 std::vector
<SampleInfo
> samples
;
30 int64 sample_start_offset
;
33 const AudioSampleEntry
* audio_description
;
34 const VideoSampleEntry
* video_description
;
36 int64 aux_info_start_offset
; // Only valid if aux_info_total_size > 0.
37 int aux_info_default_size
;
38 std::vector
<uint8
> aux_info_sizes
; // Populated if default_size == 0.
39 int aux_info_total_size
;
41 std::vector
<CencSampleEncryptionInfoEntry
> sample_encryption_info
;
47 TrackRunInfo::TrackRunInfo()
51 sample_start_offset(-1),
53 aux_info_start_offset(-1),
54 aux_info_default_size(-1),
55 aux_info_total_size(-1) {
57 TrackRunInfo::~TrackRunInfo() {}
59 base::TimeDelta
TimeDeltaFromRational(int64 numer
, int64 denom
) {
60 // To avoid overflow, split the following calculation:
61 // (numer * base::Time::kMicrosecondsPerSecond) / denom
63 // (numer / denom) * base::Time::kMicrosecondsPerSecond +
64 // ((numer % denom) * base::Time::kMicrosecondsPerSecond) / denom
65 int64 a
= numer
/ denom
;
66 DCHECK_LE((a
> 0 ? a
: -a
), kint64max
/ base::Time::kMicrosecondsPerSecond
);
67 int64 timea_in_us
= a
* base::Time::kMicrosecondsPerSecond
;
69 int64 b
= numer
% denom
;
70 DCHECK_LE((b
> 0 ? b
: -b
), kint64max
/ base::Time::kMicrosecondsPerSecond
);
71 int64 timeb_in_us
= (b
* base::Time::kMicrosecondsPerSecond
) / denom
;
73 DCHECK((timeb_in_us
< 0) || (timea_in_us
<= kint64max
- timeb_in_us
));
74 DCHECK((timeb_in_us
> 0) || (timea_in_us
>= kint64min
- timeb_in_us
));
75 return base::TimeDelta::FromMicroseconds(timea_in_us
+ timeb_in_us
);
78 DecodeTimestamp
DecodeTimestampFromRational(int64 numer
, int64 denom
) {
79 return DecodeTimestamp::FromPresentationTime(
80 TimeDeltaFromRational(numer
, denom
));
83 TrackRunIterator::TrackRunIterator(const Movie
* moov
,
85 : moov_(moov
), log_cb_(log_cb
), sample_offset_(0) {
89 TrackRunIterator::~TrackRunIterator() {}
91 static bool PopulateSampleInfo(const TrackExtends
& trex
,
92 const TrackFragmentHeader
& tfhd
,
93 const TrackFragmentRun
& trun
,
94 const int64 edit_list_offset
,
96 SampleInfo
* sample_info
,
97 const SampleDependsOn sdtp_sample_depends_on
,
98 const LogCB
& log_cb
) {
99 if (i
< trun
.sample_sizes
.size()) {
100 sample_info
->size
= trun
.sample_sizes
[i
];
101 } else if (tfhd
.default_sample_size
> 0) {
102 sample_info
->size
= tfhd
.default_sample_size
;
104 sample_info
->size
= trex
.default_sample_size
;
107 if (i
< trun
.sample_durations
.size()) {
108 sample_info
->duration
= trun
.sample_durations
[i
];
109 } else if (tfhd
.default_sample_duration
> 0) {
110 sample_info
->duration
= tfhd
.default_sample_duration
;
112 sample_info
->duration
= trex
.default_sample_duration
;
115 if (i
< trun
.sample_composition_time_offsets
.size()) {
116 sample_info
->cts_offset
= trun
.sample_composition_time_offsets
[i
];
118 sample_info
->cts_offset
= 0;
120 sample_info
->cts_offset
+= edit_list_offset
;
123 if (i
< trun
.sample_flags
.size()) {
124 flags
= trun
.sample_flags
[i
];
125 } else if (tfhd
.has_default_sample_flags
) {
126 flags
= tfhd
.default_sample_flags
;
128 flags
= trex
.default_sample_flags
;
131 SampleDependsOn sample_depends_on
=
132 static_cast<SampleDependsOn
>((flags
>> 24) & 0x3);
134 if (sample_depends_on
== kSampleDependsOnUnknown
)
135 sample_depends_on
= sdtp_sample_depends_on
;
137 // ISO/IEC 14496-12 Section 8.8.3.1 : The negation of |sample_is_sync_sample|
138 // provides the same information as the sync sample table [8.6.2]. When
139 // |sample_is_sync_sample| is true for a sample, it is the same as if the
140 // sample were not in a movie fragment and marked with an entry in the sync
141 // sample table (or, if all samples are sync samples, the sync sample table
143 bool sample_is_sync_sample
= !(flags
& kSampleIsNonSyncSample
);
144 sample_info
->is_random_access_point
= sample_is_sync_sample
;
146 switch (sample_depends_on
) {
147 case kSampleDependsOnUnknown
:
148 sample_info
->is_keyframe
= sample_is_sync_sample
;
151 case kSampleDependsOnOthers
:
152 sample_info
->is_keyframe
= false;
155 case kSampleDependsOnNoOther
:
156 sample_info
->is_keyframe
= true;
159 case kSampleDependsOnReserved
:
160 MEDIA_LOG(ERROR
, log_cb
) << "Reserved value used in sample dependency"
167 // In well-structured encrypted media, each track run will be immediately
168 // preceded by its auxiliary information; this is the only optimal storage
169 // pattern in terms of minimum number of bytes from a serial stream needed to
170 // begin playback. It also allows us to optimize caching on memory-constrained
171 // architectures, because we can cache the relatively small auxiliary
172 // information for an entire run and then discard data from the input stream,
173 // instead of retaining the entire 'mdat' box.
175 // We optimize for this situation (with no loss of generality) by sorting track
176 // runs during iteration in order of their first data offset (either sample data
177 // or auxiliary data).
178 class CompareMinTrackRunDataOffset
{
180 bool operator()(const TrackRunInfo
& a
, const TrackRunInfo
& b
) {
181 int64 a_aux
= a
.aux_info_total_size
? a
.aux_info_start_offset
: kint64max
;
182 int64 b_aux
= b
.aux_info_total_size
? b
.aux_info_start_offset
: kint64max
;
184 int64 a_lesser
= std::min(a_aux
, a
.sample_start_offset
);
185 int64 a_greater
= std::max(a_aux
, a
.sample_start_offset
);
186 int64 b_lesser
= std::min(b_aux
, b
.sample_start_offset
);
187 int64 b_greater
= std::max(b_aux
, b
.sample_start_offset
);
189 if (a_lesser
== b_lesser
) return a_greater
< b_greater
;
190 return a_lesser
< b_lesser
;
194 bool TrackRunIterator::Init(const MovieFragment
& moof
) {
197 for (size_t i
= 0; i
< moof
.tracks
.size(); i
++) {
198 const TrackFragment
& traf
= moof
.tracks
[i
];
200 const Track
* trak
= NULL
;
201 for (size_t t
= 0; t
< moov_
->tracks
.size(); t
++) {
202 if (moov_
->tracks
[t
].header
.track_id
== traf
.header
.track_id
)
203 trak
= &moov_
->tracks
[t
];
207 const TrackExtends
* trex
= NULL
;
208 for (size_t t
= 0; t
< moov_
->extends
.tracks
.size(); t
++) {
209 if (moov_
->extends
.tracks
[t
].track_id
== traf
.header
.track_id
)
210 trex
= &moov_
->extends
.tracks
[t
];
214 const SampleDescription
& stsd
=
215 trak
->media
.information
.sample_table
.description
;
216 if (stsd
.type
!= kAudio
&& stsd
.type
!= kVideo
) {
217 DVLOG(1) << "Skipping unhandled track type";
220 size_t desc_idx
= traf
.header
.sample_description_index
;
221 if (!desc_idx
) desc_idx
= trex
->default_sample_description_index
;
222 RCHECK(desc_idx
> 0); // Descriptions are one-indexed in the file
225 // Process edit list to remove CTS offset introduced in the presence of
226 // B-frames (those that contain a single edit with a nonnegative media
227 // time). Other uses of edit lists are not supported, as they are
228 // both uncommon and better served by higher-level protocols.
229 int64 edit_list_offset
= 0;
230 const std::vector
<EditListEntry
>& edits
= trak
->edit
.list
.edits
;
231 if (!edits
.empty()) {
232 if (edits
.size() > 1)
233 DVLOG(1) << "Multi-entry edit box detected; some components ignored.";
235 if (edits
[0].media_time
< 0) {
236 DVLOG(1) << "Empty edit list entry ignored.";
238 edit_list_offset
= -edits
[0].media_time
;
242 SampleToGroupIterator
sample_to_group_itr(traf
.sample_to_group
);
243 bool is_sample_to_group_valid
= sample_to_group_itr
.IsValid();
245 int64 run_start_dts
= traf
.decode_time
.decode_time
;
246 int sample_count_sum
= 0;
247 for (size_t j
= 0; j
< traf
.runs
.size(); j
++) {
248 const TrackFragmentRun
& trun
= traf
.runs
[j
];
250 tri
.track_id
= traf
.header
.track_id
;
251 tri
.timescale
= trak
->media
.header
.timescale
;
252 tri
.start_dts
= run_start_dts
;
253 tri
.sample_start_offset
= trun
.data_offset
;
254 tri
.sample_encryption_info
= traf
.sample_group_description
.entries
;
256 tri
.is_audio
= (stsd
.type
== kAudio
);
258 RCHECK(!stsd
.audio_entries
.empty());
259 if (desc_idx
> stsd
.audio_entries
.size())
261 tri
.audio_description
= &stsd
.audio_entries
[desc_idx
];
263 RCHECK(!stsd
.video_entries
.empty());
264 if (desc_idx
> stsd
.video_entries
.size())
266 tri
.video_description
= &stsd
.video_entries
[desc_idx
];
269 // Collect information from the auxiliary_offset entry with the same index
270 // in the 'saiz' container as the current run's index in the 'trun'
271 // container, if it is present.
272 if (traf
.auxiliary_offset
.offsets
.size() > j
) {
273 // There should be an auxiliary info entry corresponding to each sample
274 // in the auxiliary offset entry's corresponding track run.
275 RCHECK(traf
.auxiliary_size
.sample_count
>=
276 sample_count_sum
+ trun
.sample_count
);
277 tri
.aux_info_start_offset
= traf
.auxiliary_offset
.offsets
[j
];
278 tri
.aux_info_default_size
=
279 traf
.auxiliary_size
.default_sample_info_size
;
280 if (tri
.aux_info_default_size
== 0) {
281 const std::vector
<uint8
>& sizes
=
282 traf
.auxiliary_size
.sample_info_sizes
;
283 tri
.aux_info_sizes
.insert(tri
.aux_info_sizes
.begin(),
284 sizes
.begin() + sample_count_sum
,
285 sizes
.begin() + sample_count_sum
+ trun
.sample_count
);
288 // If the default info size is positive, find the total size of the aux
289 // info block from it, otherwise sum over the individual sizes of each
290 // aux info entry in the aux_offset entry.
291 if (tri
.aux_info_default_size
) {
292 tri
.aux_info_total_size
=
293 tri
.aux_info_default_size
* trun
.sample_count
;
295 tri
.aux_info_total_size
= 0;
296 for (size_t k
= 0; k
< trun
.sample_count
; k
++) {
297 tri
.aux_info_total_size
+= tri
.aux_info_sizes
[k
];
301 tri
.aux_info_start_offset
= -1;
302 tri
.aux_info_total_size
= 0;
305 tri
.samples
.resize(trun
.sample_count
);
306 for (size_t k
= 0; k
< trun
.sample_count
; k
++) {
307 if (!PopulateSampleInfo(*trex
, traf
.header
, trun
, edit_list_offset
,
309 traf
.sdtp
.sample_depends_on(k
),
314 run_start_dts
+= tri
.samples
[k
].duration
;
316 if (!is_sample_to_group_valid
) {
317 // Set group description index to 0 to read encryption information
318 // from TrackEncryption Box.
319 tri
.samples
[k
].cenc_group_description_index
= 0;
323 // ISO-14496-12 Section 8.9.2.3 and 8.9.4 : group description index
324 // (1) ranges from 1 to the number of sample group entries in the track
325 // level SampleGroupDescription Box, or (2) takes the value 0 to
326 // indicate that this sample is a member of no group, in this case, the
327 // sample is associated with the default values specified in
328 // TrackEncryption Box, or (3) starts at 0x10001, i.e. the index value
329 // 1, with the value 1 in the top 16 bits, to reference fragment-local
330 // SampleGroupDescription Box.
331 // Case (1) is not supported currently. We might not need it either as
332 // the same functionality can be better achieved using (2).
333 uint32 index
= sample_to_group_itr
.group_description_index();
334 if (index
>= SampleToGroupEntry::kFragmentGroupDescriptionIndexBase
) {
335 index
-= SampleToGroupEntry::kFragmentGroupDescriptionIndexBase
;
336 RCHECK(index
!= 0 && index
<= tri
.sample_encryption_info
.size());
337 } else if (index
!= 0) {
338 NOTIMPLEMENTED() << "'sgpd' box in 'moov' is not supported.";
341 tri
.samples
[k
].cenc_group_description_index
= index
;
342 is_sample_to_group_valid
= sample_to_group_itr
.Advance();
344 runs_
.push_back(tri
);
345 sample_count_sum
+= trun
.sample_count
;
348 // We should have iterated through all samples in SampleToGroup Box.
349 RCHECK(!sample_to_group_itr
.IsValid());
352 std::sort(runs_
.begin(), runs_
.end(), CompareMinTrackRunDataOffset());
353 run_itr_
= runs_
.begin();
358 void TrackRunIterator::AdvanceRun() {
363 void TrackRunIterator::ResetRun() {
364 if (!IsRunValid()) return;
365 sample_dts_
= run_itr_
->start_dts
;
366 sample_offset_
= run_itr_
->sample_start_offset
;
367 sample_itr_
= run_itr_
->samples
.begin();
371 void TrackRunIterator::AdvanceSample() {
372 DCHECK(IsSampleValid());
373 sample_dts_
+= sample_itr_
->duration
;
374 sample_offset_
+= sample_itr_
->size
;
378 // This implementation only indicates a need for caching if CENC auxiliary
379 // info is available in the stream.
380 bool TrackRunIterator::AuxInfoNeedsToBeCached() {
381 DCHECK(IsRunValid());
382 return aux_info_size() > 0 && cenc_info_
.size() == 0;
385 // This implementation currently only caches CENC auxiliary info.
386 bool TrackRunIterator::CacheAuxInfo(const uint8
* buf
, int buf_size
) {
387 RCHECK(AuxInfoNeedsToBeCached() && buf_size
>= aux_info_size());
389 cenc_info_
.resize(run_itr_
->samples
.size());
391 for (size_t i
= 0; i
< run_itr_
->samples
.size(); i
++) {
392 int info_size
= run_itr_
->aux_info_default_size
;
394 info_size
= run_itr_
->aux_info_sizes
[i
];
396 if (IsSampleEncrypted(i
)) {
397 BufferReader
reader(buf
+ pos
, info_size
);
398 RCHECK(cenc_info_
[i
].Parse(GetIvSize(i
), &reader
));
406 bool TrackRunIterator::IsRunValid() const {
407 return run_itr_
!= runs_
.end();
410 bool TrackRunIterator::IsSampleValid() const {
411 return IsRunValid() && (sample_itr_
!= run_itr_
->samples
.end());
414 // Because tracks are in sorted order and auxiliary information is cached when
415 // returning samples, it is guaranteed that no data will be required before the
416 // lesser of the minimum data offset of this track and the next in sequence.
417 // (The stronger condition - that no data is required before the minimum data
418 // offset of this track alone - is not guaranteed, because the BMFF spec does
419 // not have any inter-run ordering restrictions.)
420 int64
TrackRunIterator::GetMaxClearOffset() {
421 int64 offset
= kint64max
;
423 if (IsSampleValid()) {
424 offset
= std::min(offset
, sample_offset_
);
425 if (AuxInfoNeedsToBeCached())
426 offset
= std::min(offset
, aux_info_offset());
428 if (run_itr_
!= runs_
.end()) {
429 std::vector
<TrackRunInfo
>::const_iterator next_run
= run_itr_
+ 1;
430 if (next_run
!= runs_
.end()) {
431 offset
= std::min(offset
, next_run
->sample_start_offset
);
432 if (next_run
->aux_info_total_size
)
433 offset
= std::min(offset
, next_run
->aux_info_start_offset
);
436 if (offset
== kint64max
) return 0;
440 uint32
TrackRunIterator::track_id() const {
441 DCHECK(IsRunValid());
442 return run_itr_
->track_id
;
445 bool TrackRunIterator::is_encrypted() const {
446 DCHECK(IsSampleValid());
447 return IsSampleEncrypted(sample_itr_
- run_itr_
->samples
.begin());
450 int64
TrackRunIterator::aux_info_offset() const {
451 return run_itr_
->aux_info_start_offset
;
454 int TrackRunIterator::aux_info_size() const {
455 return run_itr_
->aux_info_total_size
;
458 bool TrackRunIterator::is_audio() const {
459 DCHECK(IsRunValid());
460 return run_itr_
->is_audio
;
463 const AudioSampleEntry
& TrackRunIterator::audio_description() const {
465 DCHECK(run_itr_
->audio_description
);
466 return *run_itr_
->audio_description
;
469 const VideoSampleEntry
& TrackRunIterator::video_description() const {
471 DCHECK(run_itr_
->video_description
);
472 return *run_itr_
->video_description
;
475 int64
TrackRunIterator::sample_offset() const {
476 DCHECK(IsSampleValid());
477 return sample_offset_
;
480 int TrackRunIterator::sample_size() const {
481 DCHECK(IsSampleValid());
482 return sample_itr_
->size
;
485 DecodeTimestamp
TrackRunIterator::dts() const {
486 DCHECK(IsSampleValid());
487 return DecodeTimestampFromRational(sample_dts_
, run_itr_
->timescale
);
490 base::TimeDelta
TrackRunIterator::cts() const {
491 DCHECK(IsSampleValid());
492 return TimeDeltaFromRational(sample_dts_
+ sample_itr_
->cts_offset
,
493 run_itr_
->timescale
);
496 base::TimeDelta
TrackRunIterator::duration() const {
497 DCHECK(IsSampleValid());
498 return TimeDeltaFromRational(sample_itr_
->duration
, run_itr_
->timescale
);
501 bool TrackRunIterator::is_keyframe() const {
502 DCHECK(IsSampleValid());
503 return sample_itr_
->is_keyframe
;
506 bool TrackRunIterator::is_random_access_point() const {
507 DCHECK(IsSampleValid());
508 return sample_itr_
->is_random_access_point
;
511 const TrackEncryption
& TrackRunIterator::track_encryption() const {
513 return audio_description().sinf
.info
.track_encryption
;
514 return video_description().sinf
.info
.track_encryption
;
517 scoped_ptr
<DecryptConfig
> TrackRunIterator::GetDecryptConfig() {
518 DCHECK(is_encrypted());
520 if (cenc_info_
.empty()) {
521 DCHECK_EQ(0, aux_info_size());
522 MEDIA_LOG(ERROR
, log_cb_
) << "Aux Info is not available.";
523 return scoped_ptr
<DecryptConfig
>();
526 size_t sample_idx
= sample_itr_
- run_itr_
->samples
.begin();
527 DCHECK_LT(sample_idx
, cenc_info_
.size());
528 const FrameCENCInfo
& cenc_info
= cenc_info_
[sample_idx
];
530 size_t total_size
= 0;
531 if (!cenc_info
.subsamples
.empty() &&
532 (!cenc_info
.GetTotalSizeOfSubsamples(&total_size
) ||
533 total_size
!= static_cast<size_t>(sample_size()))) {
534 MEDIA_LOG(ERROR
, log_cb_
) << "Incorrect CENC subsample size.";
535 return scoped_ptr
<DecryptConfig
>();
538 const std::vector
<uint8
>& kid
= GetKeyId(sample_idx
);
539 return scoped_ptr
<DecryptConfig
>(new DecryptConfig(
540 std::string(reinterpret_cast<const char*>(&kid
[0]), kid
.size()),
541 std::string(reinterpret_cast<const char*>(cenc_info
.iv
),
542 arraysize(cenc_info
.iv
)),
543 cenc_info
.subsamples
));
546 uint32
TrackRunIterator::GetGroupDescriptionIndex(uint32 sample_index
) const {
547 DCHECK(IsRunValid());
548 DCHECK_LT(sample_index
, run_itr_
->samples
.size());
549 return run_itr_
->samples
[sample_index
].cenc_group_description_index
;
552 const CencSampleEncryptionInfoEntry
&
553 TrackRunIterator::GetSampleEncryptionInfoEntry(
554 uint32 group_description_index
) const {
555 DCHECK(IsRunValid());
556 DCHECK_NE(group_description_index
, 0u);
557 DCHECK_LE(group_description_index
, run_itr_
->sample_encryption_info
.size());
558 // |group_description_index| is 1-based. Subtract by 1 to index the vector.
559 return run_itr_
->sample_encryption_info
[group_description_index
- 1];
562 bool TrackRunIterator::IsSampleEncrypted(size_t sample_index
) const {
563 uint32 index
= GetGroupDescriptionIndex(sample_index
);
564 return (index
== 0) ? track_encryption().is_encrypted
565 : GetSampleEncryptionInfoEntry(index
).is_encrypted
;
568 const std::vector
<uint8
>& TrackRunIterator::GetKeyId(
569 size_t sample_index
) const {
570 uint32 index
= GetGroupDescriptionIndex(sample_index
);
571 return (index
== 0) ? track_encryption().default_kid
572 : GetSampleEncryptionInfoEntry(index
).key_id
;
575 uint8
TrackRunIterator::GetIvSize(size_t sample_index
) const {
576 uint32 index
= GetGroupDescriptionIndex(sample_index
);
577 return (index
== 0) ? track_encryption().default_iv_size
578 : GetSampleEncryptionInfoEntry(index
).iv_size
;