WebKit roll 138552:138617
[chromium-blink-merge.git] / media / mp4 / track_run_iterator.cc
blob5538562c071e711e2ed13089a3ef0cd36fc6b699
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/track_run_iterator.h"
7 #include <algorithm>
9 #include "media/base/stream_parser_buffer.h"
10 #include "media/mp4/rcheck.h"
12 namespace {
13 static const uint32 kSampleIsDifferenceSampleFlagMask = 0x10000;
16 namespace media {
17 namespace mp4 {
19 struct SampleInfo {
20 int size;
21 int duration;
22 int cts_offset;
23 bool is_keyframe;
26 struct TrackRunInfo {
27 uint32 track_id;
28 std::vector<SampleInfo> samples;
29 int64 timescale;
30 int64 start_dts;
31 int64 sample_start_offset;
33 bool is_audio;
34 const AudioSampleEntry* audio_description;
35 const VideoSampleEntry* video_description;
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 TrackRunInfo();
43 ~TrackRunInfo();
46 TrackRunInfo::TrackRunInfo()
47 : track_id(0),
48 timescale(-1),
49 start_dts(-1),
50 sample_start_offset(-1),
51 is_audio(false),
52 aux_info_start_offset(-1),
53 aux_info_default_size(-1),
54 aux_info_total_size(-1) {
56 TrackRunInfo::~TrackRunInfo() {}
58 TimeDelta TimeDeltaFromRational(int64 numer, int64 denom) {
59 DCHECK_LT((numer > 0 ? numer : -numer),
60 kint64max / base::Time::kMicrosecondsPerSecond);
61 return TimeDelta::FromMicroseconds(
62 base::Time::kMicrosecondsPerSecond * numer / denom);
65 TrackRunIterator::TrackRunIterator(const Movie* moov,
66 const LogCB& log_cb)
67 : moov_(moov), log_cb_(log_cb), sample_offset_(0) {
68 CHECK(moov);
71 TrackRunIterator::~TrackRunIterator() {}
73 static void PopulateSampleInfo(const TrackExtends& trex,
74 const TrackFragmentHeader& tfhd,
75 const TrackFragmentRun& trun,
76 const int64 edit_list_offset,
77 const uint32 i,
78 SampleInfo* sample_info) {
79 if (i < trun.sample_sizes.size()) {
80 sample_info->size = trun.sample_sizes[i];
81 } else if (tfhd.default_sample_size > 0) {
82 sample_info->size = tfhd.default_sample_size;
83 } else {
84 sample_info->size = trex.default_sample_size;
87 if (i < trun.sample_durations.size()) {
88 sample_info->duration = trun.sample_durations[i];
89 } else if (tfhd.default_sample_duration > 0) {
90 sample_info->duration = tfhd.default_sample_duration;
91 } else {
92 sample_info->duration = trex.default_sample_duration;
95 if (i < trun.sample_composition_time_offsets.size()) {
96 sample_info->cts_offset = trun.sample_composition_time_offsets[i];
97 } else {
98 sample_info->cts_offset = 0;
100 sample_info->cts_offset += edit_list_offset;
102 uint32 flags;
103 if (i < trun.sample_flags.size()) {
104 flags = trun.sample_flags[i];
105 } else if (tfhd.has_default_sample_flags) {
106 flags = tfhd.default_sample_flags;
107 } else {
108 flags = trex.default_sample_flags;
110 sample_info->is_keyframe = !(flags & kSampleIsDifferenceSampleFlagMask);
113 // In well-structured encrypted media, each track run will be immediately
114 // preceded by its auxiliary information; this is the only optimal storage
115 // pattern in terms of minimum number of bytes from a serial stream needed to
116 // begin playback. It also allows us to optimize caching on memory-constrained
117 // architectures, because we can cache the relatively small auxiliary
118 // information for an entire run and then discard data from the input stream,
119 // instead of retaining the entire 'mdat' box.
121 // We optimize for this situation (with no loss of generality) by sorting track
122 // runs during iteration in order of their first data offset (either sample data
123 // or auxiliary data).
124 class CompareMinTrackRunDataOffset {
125 public:
126 bool operator()(const TrackRunInfo& a, const TrackRunInfo& b) {
127 int64 a_aux = a.aux_info_total_size ? a.aux_info_start_offset : kint64max;
128 int64 b_aux = b.aux_info_total_size ? b.aux_info_start_offset : kint64max;
130 int64 a_lesser = std::min(a_aux, a.sample_start_offset);
131 int64 a_greater = std::max(a_aux, a.sample_start_offset);
132 int64 b_lesser = std::min(b_aux, b.sample_start_offset);
133 int64 b_greater = std::max(b_aux, b.sample_start_offset);
135 if (a_lesser == b_lesser) return a_greater < b_greater;
136 return a_lesser < b_lesser;
140 bool TrackRunIterator::Init(const MovieFragment& moof) {
141 runs_.clear();
143 for (size_t i = 0; i < moof.tracks.size(); i++) {
144 const TrackFragment& traf = moof.tracks[i];
146 const Track* trak = NULL;
147 for (size_t t = 0; t < moov_->tracks.size(); t++) {
148 if (moov_->tracks[t].header.track_id == traf.header.track_id)
149 trak = &moov_->tracks[t];
151 RCHECK(trak);
153 const TrackExtends* trex = NULL;
154 for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
155 if (moov_->extends.tracks[t].track_id == traf.header.track_id)
156 trex = &moov_->extends.tracks[t];
158 RCHECK(trex);
160 const SampleDescription& stsd =
161 trak->media.information.sample_table.description;
162 if (stsd.type != kAudio && stsd.type != kVideo) {
163 DVLOG(1) << "Skipping unhandled track type";
164 continue;
166 size_t desc_idx = traf.header.sample_description_index;
167 if (!desc_idx) desc_idx = trex->default_sample_description_index;
168 RCHECK(desc_idx > 0); // Descriptions are one-indexed in the file
169 desc_idx -= 1;
171 // Process edit list to remove CTS offset introduced in the presence of
172 // B-frames (those that contain a single edit with a nonnegative media
173 // time). Other uses of edit lists are not supported, as they are
174 // both uncommon and better served by higher-level protocols.
175 int64 edit_list_offset = 0;
176 const std::vector<EditListEntry>& edits = trak->edit.list.edits;
177 if (!edits.empty()) {
178 if (edits.size() > 1)
179 DVLOG(1) << "Multi-entry edit box detected; some components ignored.";
181 if (edits[0].media_time < 0) {
182 DVLOG(1) << "Empty edit list entry ignored.";
183 } else {
184 edit_list_offset = -edits[0].media_time;
188 int64 run_start_dts = traf.decode_time.decode_time;
189 int sample_count_sum = 0;
191 for (size_t j = 0; j < traf.runs.size(); j++) {
192 const TrackFragmentRun& trun = traf.runs[j];
193 TrackRunInfo tri;
194 tri.track_id = traf.header.track_id;
195 tri.timescale = trak->media.header.timescale;
196 tri.start_dts = run_start_dts;
197 tri.sample_start_offset = trun.data_offset;
199 tri.is_audio = (stsd.type == kAudio);
200 if (tri.is_audio) {
201 RCHECK(!stsd.audio_entries.empty());
202 if (desc_idx > stsd.audio_entries.size())
203 desc_idx = 0;
204 tri.audio_description = &stsd.audio_entries[desc_idx];
205 } else {
206 RCHECK(!stsd.video_entries.empty());
207 if (desc_idx > stsd.video_entries.size())
208 desc_idx = 0;
209 tri.video_description = &stsd.video_entries[desc_idx];
212 // Collect information from the auxiliary_offset entry with the same index
213 // in the 'saiz' container as the current run's index in the 'trun'
214 // container, if it is present.
215 if (traf.auxiliary_offset.offsets.size() > j) {
216 // There should be an auxiliary info entry corresponding to each sample
217 // in the auxiliary offset entry's corresponding track run.
218 RCHECK(traf.auxiliary_size.sample_count >=
219 sample_count_sum + trun.sample_count);
220 tri.aux_info_start_offset = traf.auxiliary_offset.offsets[j];
221 tri.aux_info_default_size =
222 traf.auxiliary_size.default_sample_info_size;
223 if (tri.aux_info_default_size == 0) {
224 const std::vector<uint8>& sizes =
225 traf.auxiliary_size.sample_info_sizes;
226 tri.aux_info_sizes.insert(tri.aux_info_sizes.begin(),
227 sizes.begin() + sample_count_sum,
228 sizes.begin() + sample_count_sum + trun.sample_count);
231 // If the default info size is positive, find the total size of the aux
232 // info block from it, otherwise sum over the individual sizes of each
233 // aux info entry in the aux_offset entry.
234 if (tri.aux_info_default_size) {
235 tri.aux_info_total_size =
236 tri.aux_info_default_size * trun.sample_count;
237 } else {
238 tri.aux_info_total_size = 0;
239 for (size_t k = 0; k < trun.sample_count; k++) {
240 tri.aux_info_total_size += tri.aux_info_sizes[k];
243 } else {
244 tri.aux_info_start_offset = -1;
245 tri.aux_info_total_size = 0;
248 tri.samples.resize(trun.sample_count);
249 for (size_t k = 0; k < trun.sample_count; k++) {
250 PopulateSampleInfo(*trex, traf.header, trun, edit_list_offset,
251 k, &tri.samples[k]);
252 run_start_dts += tri.samples[k].duration;
254 runs_.push_back(tri);
255 sample_count_sum += trun.sample_count;
259 std::sort(runs_.begin(), runs_.end(), CompareMinTrackRunDataOffset());
260 run_itr_ = runs_.begin();
261 ResetRun();
262 return true;
265 void TrackRunIterator::AdvanceRun() {
266 ++run_itr_;
267 ResetRun();
270 void TrackRunIterator::ResetRun() {
271 if (!IsRunValid()) return;
272 sample_dts_ = run_itr_->start_dts;
273 sample_offset_ = run_itr_->sample_start_offset;
274 sample_itr_ = run_itr_->samples.begin();
275 cenc_info_.clear();
278 void TrackRunIterator::AdvanceSample() {
279 DCHECK(IsSampleValid());
280 sample_dts_ += sample_itr_->duration;
281 sample_offset_ += sample_itr_->size;
282 ++sample_itr_;
285 // This implementation only indicates a need for caching if CENC auxiliary
286 // info is available in the stream.
287 bool TrackRunIterator::AuxInfoNeedsToBeCached() {
288 DCHECK(IsRunValid());
289 return is_encrypted() && aux_info_size() > 0 && cenc_info_.size() == 0;
292 // This implementation currently only caches CENC auxiliary info.
293 bool TrackRunIterator::CacheAuxInfo(const uint8* buf, int buf_size) {
294 RCHECK(AuxInfoNeedsToBeCached() && buf_size >= aux_info_size());
296 cenc_info_.resize(run_itr_->samples.size());
297 int64 pos = 0;
298 for (size_t i = 0; i < run_itr_->samples.size(); i++) {
299 int info_size = run_itr_->aux_info_default_size;
300 if (!info_size)
301 info_size = run_itr_->aux_info_sizes[i];
303 BufferReader reader(buf + pos, info_size);
304 RCHECK(cenc_info_[i].Parse(track_encryption().default_iv_size, &reader));
305 pos += info_size;
308 return true;
311 bool TrackRunIterator::IsRunValid() const {
312 return run_itr_ != runs_.end();
315 bool TrackRunIterator::IsSampleValid() const {
316 return IsRunValid() && (sample_itr_ != run_itr_->samples.end());
319 // Because tracks are in sorted order and auxiliary information is cached when
320 // returning samples, it is guaranteed that no data will be required before the
321 // lesser of the minimum data offset of this track and the next in sequence.
322 // (The stronger condition - that no data is required before the minimum data
323 // offset of this track alone - is not guaranteed, because the BMFF spec does
324 // not have any inter-run ordering restrictions.)
325 int64 TrackRunIterator::GetMaxClearOffset() {
326 int64 offset = kint64max;
328 if (IsSampleValid()) {
329 offset = std::min(offset, sample_offset_);
330 if (AuxInfoNeedsToBeCached())
331 offset = std::min(offset, aux_info_offset());
333 if (run_itr_ != runs_.end()) {
334 std::vector<TrackRunInfo>::const_iterator next_run = run_itr_ + 1;
335 if (next_run != runs_.end()) {
336 offset = std::min(offset, next_run->sample_start_offset);
337 if (next_run->aux_info_total_size)
338 offset = std::min(offset, next_run->aux_info_start_offset);
341 if (offset == kint64max) return 0;
342 return offset;
345 TimeDelta TrackRunIterator::GetMinDecodeTimestamp() {
346 TimeDelta dts = kInfiniteDuration();
347 for (size_t i = 0; i < runs_.size(); i++) {
348 dts = std::min(dts, TimeDeltaFromRational(runs_[i].start_dts,
349 runs_[i].timescale));
351 return dts;
354 uint32 TrackRunIterator::track_id() const {
355 DCHECK(IsRunValid());
356 return run_itr_->track_id;
359 bool TrackRunIterator::is_encrypted() const {
360 DCHECK(IsRunValid());
361 return track_encryption().is_encrypted;
364 int64 TrackRunIterator::aux_info_offset() const {
365 return run_itr_->aux_info_start_offset;
368 int TrackRunIterator::aux_info_size() const {
369 return run_itr_->aux_info_total_size;
372 bool TrackRunIterator::is_audio() const {
373 DCHECK(IsRunValid());
374 return run_itr_->is_audio;
377 const AudioSampleEntry& TrackRunIterator::audio_description() const {
378 DCHECK(is_audio());
379 DCHECK(run_itr_->audio_description);
380 return *run_itr_->audio_description;
383 const VideoSampleEntry& TrackRunIterator::video_description() const {
384 DCHECK(!is_audio());
385 DCHECK(run_itr_->video_description);
386 return *run_itr_->video_description;
389 int64 TrackRunIterator::sample_offset() const {
390 DCHECK(IsSampleValid());
391 return sample_offset_;
394 int TrackRunIterator::sample_size() const {
395 DCHECK(IsSampleValid());
396 return sample_itr_->size;
399 TimeDelta TrackRunIterator::dts() const {
400 DCHECK(IsSampleValid());
401 return TimeDeltaFromRational(sample_dts_, run_itr_->timescale);
404 TimeDelta TrackRunIterator::cts() const {
405 DCHECK(IsSampleValid());
406 return TimeDeltaFromRational(sample_dts_ + sample_itr_->cts_offset,
407 run_itr_->timescale);
410 TimeDelta TrackRunIterator::duration() const {
411 DCHECK(IsSampleValid());
412 return TimeDeltaFromRational(sample_itr_->duration, run_itr_->timescale);
415 bool TrackRunIterator::is_keyframe() const {
416 DCHECK(IsSampleValid());
417 return sample_itr_->is_keyframe;
420 const TrackEncryption& TrackRunIterator::track_encryption() const {
421 if (is_audio())
422 return audio_description().sinf.info.track_encryption;
423 return video_description().sinf.info.track_encryption;
426 scoped_ptr<DecryptConfig> TrackRunIterator::GetDecryptConfig() {
427 size_t sample_idx = sample_itr_ - run_itr_->samples.begin();
428 DCHECK(sample_idx < cenc_info_.size());
429 const FrameCENCInfo& cenc_info = cenc_info_[sample_idx];
430 DCHECK(is_encrypted() && !AuxInfoNeedsToBeCached());
432 if (!cenc_info.subsamples.empty() &&
433 (cenc_info.GetTotalSizeOfSubsamples() !=
434 static_cast<size_t>(sample_size()))) {
435 MEDIA_LOG(log_cb_) << "Incorrect CENC subsample size.";
436 return scoped_ptr<DecryptConfig>();
439 const std::vector<uint8>& kid = track_encryption().default_kid;
440 return scoped_ptr<DecryptConfig>(new DecryptConfig(
441 std::string(reinterpret_cast<const char*>(&kid[0]), kid.size()),
442 std::string(reinterpret_cast<const char*>(cenc_info.iv),
443 arraysize(cenc_info.iv)),
444 0, // No offset to start of media data in MP4 using CENC.
445 cenc_info.subsamples));
448 } // namespace mp4
449 } // namespace media