Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / media / formats / mp4 / track_run_iterator.cc
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.
4
5 #include "media/formats/mp4/track_run_iterator.h"
6
7 #include <algorithm>
8
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"
13
14 namespace media {
15 namespace mp4 {
16
17 struct SampleInfo {
18   int size;
19   int duration;
20   int cts_offset;
21   bool is_keyframe;
22   bool is_random_access_point;
23   uint32 cenc_group_description_index;
24 };
25
26 struct TrackRunInfo {
27   uint32 track_id;
28   std::vector<SampleInfo> samples;
29   int64 timescale;
30   int64 start_dts;
31   int64 sample_start_offset;
32
33   bool is_audio;
34   const AudioSampleEntry* audio_description;
35   const VideoSampleEntry* video_description;
36
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;
41
42   std::vector<CencSampleEncryptionInfoEntry> sample_encryption_info;
43
44   TrackRunInfo();
45   ~TrackRunInfo();
46 };
47
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) {
57 }
58 TrackRunInfo::~TrackRunInfo() {}
59
60 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;
69
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;
73
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 TimeDelta::FromMicroseconds(timea_in_us + timeb_in_us);
77 }
78
79 TrackRunIterator::TrackRunIterator(const Movie* moov,
80                                    const LogCB& log_cb)
81     : moov_(moov), log_cb_(log_cb), sample_offset_(0) {
82   CHECK(moov);
83 }
84
85 TrackRunIterator::~TrackRunIterator() {}
86
87 static void PopulateSampleInfo(const TrackExtends& trex,
88                                const TrackFragmentHeader& tfhd,
89                                const TrackFragmentRun& trun,
90                                const int64 edit_list_offset,
91                                const uint32 i,
92                                SampleInfo* sample_info,
93                                const SampleDependsOn sdtp_sample_depends_on,
94                                bool is_sync_sample) {
95   if (i < trun.sample_sizes.size()) {
96     sample_info->size = trun.sample_sizes[i];
97   } else if (tfhd.default_sample_size > 0) {
98     sample_info->size = tfhd.default_sample_size;
99   } else {
100     sample_info->size = trex.default_sample_size;
101   }
102
103   if (i < trun.sample_durations.size()) {
104     sample_info->duration = trun.sample_durations[i];
105   } else if (tfhd.default_sample_duration > 0) {
106     sample_info->duration = tfhd.default_sample_duration;
107   } else {
108     sample_info->duration = trex.default_sample_duration;
109   }
110
111   if (i < trun.sample_composition_time_offsets.size()) {
112     sample_info->cts_offset = trun.sample_composition_time_offsets[i];
113   } else {
114     sample_info->cts_offset = 0;
115   }
116   sample_info->cts_offset += edit_list_offset;
117
118   uint32 flags;
119   if (i < trun.sample_flags.size()) {
120     flags = trun.sample_flags[i];
121   } else if (tfhd.has_default_sample_flags) {
122     flags = tfhd.default_sample_flags;
123   } else {
124     flags = trex.default_sample_flags;
125   }
126
127   SampleDependsOn sample_depends_on =
128       static_cast<SampleDependsOn>((flags >> 24) & 0x3);
129
130   if (sample_depends_on == kSampleDependsOnUnknown)
131     sample_depends_on = sdtp_sample_depends_on;
132
133   // ISO/IEC 14496-12  Section 8.8.3.1 : The negation of |sample_is_sync_sample|
134   // provides the same information as the sync sample table [8.6.2]. When
135   // |sample_is_sync_sample| is true for a sample, it is the same as if the
136   // sample were not in a movie fragment and marked with an entry in the sync
137   // sample table (or, if all samples are sync samples, the sync sample table
138   // were absent).
139   bool sample_is_sync_sample = !(flags & kSampleIsNonSyncSample);
140   sample_info->is_random_access_point = sample_is_sync_sample;
141
142   switch (sample_depends_on) {
143     case kSampleDependsOnUnknown:
144       sample_info->is_keyframe = sample_is_sync_sample;
145       break;
146
147     case kSampleDependsOnOthers:
148       sample_info->is_keyframe = false;
149       break;
150
151     case kSampleDependsOnNoOther:
152       sample_info->is_keyframe = true;
153       break;
154
155     case kSampleDependsOnReserved:
156       CHECK(false);
157   }
158 }
159
160 // In well-structured encrypted media, each track run will be immediately
161 // preceded by its auxiliary information; this is the only optimal storage
162 // pattern in terms of minimum number of bytes from a serial stream needed to
163 // begin playback. It also allows us to optimize caching on memory-constrained
164 // architectures, because we can cache the relatively small auxiliary
165 // information for an entire run and then discard data from the input stream,
166 // instead of retaining the entire 'mdat' box.
167 //
168 // We optimize for this situation (with no loss of generality) by sorting track
169 // runs during iteration in order of their first data offset (either sample data
170 // or auxiliary data).
171 class CompareMinTrackRunDataOffset {
172  public:
173   bool operator()(const TrackRunInfo& a, const TrackRunInfo& b) {
174     int64 a_aux = a.aux_info_total_size ? a.aux_info_start_offset : kint64max;
175     int64 b_aux = b.aux_info_total_size ? b.aux_info_start_offset : kint64max;
176
177     int64 a_lesser = std::min(a_aux, a.sample_start_offset);
178     int64 a_greater = std::max(a_aux, a.sample_start_offset);
179     int64 b_lesser = std::min(b_aux, b.sample_start_offset);
180     int64 b_greater = std::max(b_aux, b.sample_start_offset);
181
182     if (a_lesser == b_lesser) return a_greater < b_greater;
183     return a_lesser < b_lesser;
184   }
185 };
186
187 bool TrackRunIterator::Init(const MovieFragment& moof) {
188   runs_.clear();
189
190   for (size_t i = 0; i < moof.tracks.size(); i++) {
191     const TrackFragment& traf = moof.tracks[i];
192
193     const Track* trak = NULL;
194     for (size_t t = 0; t < moov_->tracks.size(); t++) {
195       if (moov_->tracks[t].header.track_id == traf.header.track_id)
196         trak = &moov_->tracks[t];
197     }
198     RCHECK(trak);
199
200     const TrackExtends* trex = NULL;
201     for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
202       if (moov_->extends.tracks[t].track_id == traf.header.track_id)
203         trex = &moov_->extends.tracks[t];
204     }
205     RCHECK(trex);
206
207     const SampleDescription& stsd =
208         trak->media.information.sample_table.description;
209     if (stsd.type != kAudio && stsd.type != kVideo) {
210       DVLOG(1) << "Skipping unhandled track type";
211       continue;
212     }
213     size_t desc_idx = traf.header.sample_description_index;
214     if (!desc_idx) desc_idx = trex->default_sample_description_index;
215     RCHECK(desc_idx > 0);  // Descriptions are one-indexed in the file
216     desc_idx -= 1;
217
218     // Process edit list to remove CTS offset introduced in the presence of
219     // B-frames (those that contain a single edit with a nonnegative media
220     // time). Other uses of edit lists are not supported, as they are
221     // both uncommon and better served by higher-level protocols.
222     int64 edit_list_offset = 0;
223     const std::vector<EditListEntry>& edits = trak->edit.list.edits;
224     if (!edits.empty()) {
225       if (edits.size() > 1)
226         DVLOG(1) << "Multi-entry edit box detected; some components ignored.";
227
228       if (edits[0].media_time < 0) {
229         DVLOG(1) << "Empty edit list entry ignored.";
230       } else {
231         edit_list_offset = -edits[0].media_time;
232       }
233     }
234
235     SampleToGroupIterator sample_to_group_itr(traf.sample_to_group);
236     bool is_sample_to_group_valid = sample_to_group_itr.IsValid();
237
238     int64 run_start_dts = traf.decode_time.decode_time;
239     int sample_count_sum = 0;
240     const SyncSample& sync_sample =
241         trak->media.information.sample_table.sync_sample;
242     for (size_t j = 0; j < traf.runs.size(); j++) {
243       const TrackFragmentRun& trun = traf.runs[j];
244       TrackRunInfo tri;
245       tri.track_id = traf.header.track_id;
246       tri.timescale = trak->media.header.timescale;
247       tri.start_dts = run_start_dts;
248       tri.sample_start_offset = trun.data_offset;
249       tri.sample_encryption_info = traf.sample_group_description.entries;
250
251       tri.is_audio = (stsd.type == kAudio);
252       if (tri.is_audio) {
253         RCHECK(!stsd.audio_entries.empty());
254         if (desc_idx > stsd.audio_entries.size())
255           desc_idx = 0;
256         tri.audio_description = &stsd.audio_entries[desc_idx];
257       } else {
258         RCHECK(!stsd.video_entries.empty());
259         if (desc_idx > stsd.video_entries.size())
260           desc_idx = 0;
261         tri.video_description = &stsd.video_entries[desc_idx];
262       }
263
264       // Collect information from the auxiliary_offset entry with the same index
265       // in the 'saiz' container as the current run's index in the 'trun'
266       // container, if it is present.
267       if (traf.auxiliary_offset.offsets.size() > j) {
268         // There should be an auxiliary info entry corresponding to each sample
269         // in the auxiliary offset entry's corresponding track run.
270         RCHECK(traf.auxiliary_size.sample_count >=
271                sample_count_sum + trun.sample_count);
272         tri.aux_info_start_offset = traf.auxiliary_offset.offsets[j];
273         tri.aux_info_default_size =
274             traf.auxiliary_size.default_sample_info_size;
275         if (tri.aux_info_default_size == 0) {
276           const std::vector<uint8>& sizes =
277               traf.auxiliary_size.sample_info_sizes;
278           tri.aux_info_sizes.insert(tri.aux_info_sizes.begin(),
279               sizes.begin() + sample_count_sum,
280               sizes.begin() + sample_count_sum + trun.sample_count);
281         }
282
283         // If the default info size is positive, find the total size of the aux
284         // info block from it, otherwise sum over the individual sizes of each
285         // aux info entry in the aux_offset entry.
286         if (tri.aux_info_default_size) {
287           tri.aux_info_total_size =
288               tri.aux_info_default_size * trun.sample_count;
289         } else {
290           tri.aux_info_total_size = 0;
291           for (size_t k = 0; k < trun.sample_count; k++) {
292             tri.aux_info_total_size += tri.aux_info_sizes[k];
293           }
294         }
295       } else {
296         tri.aux_info_start_offset = -1;
297         tri.aux_info_total_size = 0;
298       }
299
300       tri.samples.resize(trun.sample_count);
301       for (size_t k = 0; k < trun.sample_count; k++) {
302         PopulateSampleInfo(*trex, traf.header, trun, edit_list_offset,
303                            k, &tri.samples[k], traf.sdtp.sample_depends_on(k),
304                            sync_sample.IsSyncSample(k));
305         run_start_dts += tri.samples[k].duration;
306
307         if (!is_sample_to_group_valid) {
308           // Set group description index to 0 to read encryption information
309           // from TrackEncryption Box.
310           tri.samples[k].cenc_group_description_index = 0;
311           continue;
312         }
313
314         // ISO-14496-12 Section 8.9.2.3 and 8.9.4 : group description index
315         // (1) ranges from 1 to the number of sample group entries in the track
316         // level SampleGroupDescription Box, or (2) takes the value 0 to
317         // indicate that this sample is a member of no group, in this case, the
318         // sample is associated with the default values specified in
319         // TrackEncryption Box, or (3) starts at 0x10001, i.e. the index value
320         // 1, with the value 1 in the top 16 bits, to reference fragment-local
321         // SampleGroupDescription Box.
322         // Case (1) is not supported currently. We might not need it either as
323         // the same functionality can be better achieved using (2).
324         uint32 index = sample_to_group_itr.group_description_index();
325         if (index >= SampleToGroupEntry::kFragmentGroupDescriptionIndexBase) {
326           index -= SampleToGroupEntry::kFragmentGroupDescriptionIndexBase;
327           RCHECK(index != 0 && index <= tri.sample_encryption_info.size());
328         } else if (index != 0) {
329           NOTIMPLEMENTED() << "'sgpd' box in 'moov' is not supported.";
330           return false;
331         }
332         tri.samples[k].cenc_group_description_index = index;
333         is_sample_to_group_valid = sample_to_group_itr.Advance();
334       }
335       runs_.push_back(tri);
336       sample_count_sum += trun.sample_count;
337     }
338
339     // We should have iterated through all samples in SampleToGroup Box.
340     RCHECK(!sample_to_group_itr.IsValid());
341   }
342
343   std::sort(runs_.begin(), runs_.end(), CompareMinTrackRunDataOffset());
344   run_itr_ = runs_.begin();
345   ResetRun();
346   return true;
347 }
348
349 void TrackRunIterator::AdvanceRun() {
350   ++run_itr_;
351   ResetRun();
352 }
353
354 void TrackRunIterator::ResetRun() {
355   if (!IsRunValid()) return;
356   sample_dts_ = run_itr_->start_dts;
357   sample_offset_ = run_itr_->sample_start_offset;
358   sample_itr_ = run_itr_->samples.begin();
359   cenc_info_.clear();
360 }
361
362 void TrackRunIterator::AdvanceSample() {
363   DCHECK(IsSampleValid());
364   sample_dts_ += sample_itr_->duration;
365   sample_offset_ += sample_itr_->size;
366   ++sample_itr_;
367 }
368
369 // This implementation only indicates a need for caching if CENC auxiliary
370 // info is available in the stream.
371 bool TrackRunIterator::AuxInfoNeedsToBeCached() {
372   DCHECK(IsRunValid());
373   return aux_info_size() > 0 && cenc_info_.size() == 0;
374 }
375
376 // This implementation currently only caches CENC auxiliary info.
377 bool TrackRunIterator::CacheAuxInfo(const uint8* buf, int buf_size) {
378   RCHECK(AuxInfoNeedsToBeCached() && buf_size >= aux_info_size());
379
380   cenc_info_.resize(run_itr_->samples.size());
381   int64 pos = 0;
382   for (size_t i = 0; i < run_itr_->samples.size(); i++) {
383     int info_size = run_itr_->aux_info_default_size;
384     if (!info_size)
385       info_size = run_itr_->aux_info_sizes[i];
386
387     if (IsSampleEncrypted(i)) {
388       BufferReader reader(buf + pos, info_size);
389       RCHECK(cenc_info_[i].Parse(GetIvSize(i), &reader));
390     }
391     pos += info_size;
392   }
393
394   return true;
395 }
396
397 bool TrackRunIterator::IsRunValid() const {
398   return run_itr_ != runs_.end();
399 }
400
401 bool TrackRunIterator::IsSampleValid() const {
402   return IsRunValid() && (sample_itr_ != run_itr_->samples.end());
403 }
404
405 // Because tracks are in sorted order and auxiliary information is cached when
406 // returning samples, it is guaranteed that no data will be required before the
407 // lesser of the minimum data offset of this track and the next in sequence.
408 // (The stronger condition - that no data is required before the minimum data
409 // offset of this track alone - is not guaranteed, because the BMFF spec does
410 // not have any inter-run ordering restrictions.)
411 int64 TrackRunIterator::GetMaxClearOffset() {
412   int64 offset = kint64max;
413
414   if (IsSampleValid()) {
415     offset = std::min(offset, sample_offset_);
416     if (AuxInfoNeedsToBeCached())
417       offset = std::min(offset, aux_info_offset());
418   }
419   if (run_itr_ != runs_.end()) {
420     std::vector<TrackRunInfo>::const_iterator next_run = run_itr_ + 1;
421     if (next_run != runs_.end()) {
422       offset = std::min(offset, next_run->sample_start_offset);
423       if (next_run->aux_info_total_size)
424         offset = std::min(offset, next_run->aux_info_start_offset);
425     }
426   }
427   if (offset == kint64max) return 0;
428   return offset;
429 }
430
431 uint32 TrackRunIterator::track_id() const {
432   DCHECK(IsRunValid());
433   return run_itr_->track_id;
434 }
435
436 bool TrackRunIterator::is_encrypted() const {
437   DCHECK(IsSampleValid());
438   return IsSampleEncrypted(sample_itr_ - run_itr_->samples.begin());
439 }
440
441 int64 TrackRunIterator::aux_info_offset() const {
442   return run_itr_->aux_info_start_offset;
443 }
444
445 int TrackRunIterator::aux_info_size() const {
446   return run_itr_->aux_info_total_size;
447 }
448
449 bool TrackRunIterator::is_audio() const {
450   DCHECK(IsRunValid());
451   return run_itr_->is_audio;
452 }
453
454 const AudioSampleEntry& TrackRunIterator::audio_description() const {
455   DCHECK(is_audio());
456   DCHECK(run_itr_->audio_description);
457   return *run_itr_->audio_description;
458 }
459
460 const VideoSampleEntry& TrackRunIterator::video_description() const {
461   DCHECK(!is_audio());
462   DCHECK(run_itr_->video_description);
463   return *run_itr_->video_description;
464 }
465
466 int64 TrackRunIterator::sample_offset() const {
467   DCHECK(IsSampleValid());
468   return sample_offset_;
469 }
470
471 int TrackRunIterator::sample_size() const {
472   DCHECK(IsSampleValid());
473   return sample_itr_->size;
474 }
475
476 TimeDelta TrackRunIterator::dts() const {
477   DCHECK(IsSampleValid());
478   return TimeDeltaFromRational(sample_dts_, run_itr_->timescale);
479 }
480
481 TimeDelta TrackRunIterator::cts() const {
482   DCHECK(IsSampleValid());
483   return TimeDeltaFromRational(sample_dts_ + sample_itr_->cts_offset,
484                                run_itr_->timescale);
485 }
486
487 TimeDelta TrackRunIterator::duration() const {
488   DCHECK(IsSampleValid());
489   return TimeDeltaFromRational(sample_itr_->duration, run_itr_->timescale);
490 }
491
492 bool TrackRunIterator::is_keyframe() const {
493   DCHECK(IsSampleValid());
494   return sample_itr_->is_keyframe;
495 }
496
497 bool TrackRunIterator::is_random_access_point() const {
498   DCHECK(IsSampleValid());
499   return sample_itr_->is_random_access_point;
500 }
501
502 const TrackEncryption& TrackRunIterator::track_encryption() const {
503   if (is_audio())
504     return audio_description().sinf.info.track_encryption;
505   return video_description().sinf.info.track_encryption;
506 }
507
508 scoped_ptr<DecryptConfig> TrackRunIterator::GetDecryptConfig() {
509   DCHECK(is_encrypted());
510
511   if (cenc_info_.empty()) {
512     DCHECK_EQ(0, aux_info_size());
513     MEDIA_LOG(log_cb_) << "Aux Info is not available.";
514     return scoped_ptr<DecryptConfig>();
515   }
516
517   size_t sample_idx = sample_itr_ - run_itr_->samples.begin();
518   DCHECK_LT(sample_idx, cenc_info_.size());
519   const FrameCENCInfo& cenc_info = cenc_info_[sample_idx];
520
521   size_t total_size = 0;
522   if (!cenc_info.subsamples.empty() &&
523       (!cenc_info.GetTotalSizeOfSubsamples(&total_size) ||
524        total_size != static_cast<size_t>(sample_size()))) {
525     MEDIA_LOG(log_cb_) << "Incorrect CENC subsample size.";
526     return scoped_ptr<DecryptConfig>();
527   }
528
529   const std::vector<uint8>& kid = GetKeyId(sample_idx);
530   return scoped_ptr<DecryptConfig>(new DecryptConfig(
531       std::string(reinterpret_cast<const char*>(&kid[0]), kid.size()),
532       std::string(reinterpret_cast<const char*>(cenc_info.iv),
533                   arraysize(cenc_info.iv)),
534       cenc_info.subsamples));
535 }
536
537 uint32 TrackRunIterator::GetGroupDescriptionIndex(uint32 sample_index) const {
538   DCHECK(IsRunValid());
539   DCHECK_LT(sample_index, run_itr_->samples.size());
540   return run_itr_->samples[sample_index].cenc_group_description_index;
541 }
542
543 const CencSampleEncryptionInfoEntry&
544 TrackRunIterator::GetSampleEncryptionInfoEntry(
545     uint32 group_description_index) const {
546   DCHECK(IsRunValid());
547   DCHECK_NE(group_description_index, 0u);
548   DCHECK_LE(group_description_index, run_itr_->sample_encryption_info.size());
549   // |group_description_index| is 1-based. Subtract by 1 to index the vector.
550   return run_itr_->sample_encryption_info[group_description_index - 1];
551 }
552
553 bool TrackRunIterator::IsSampleEncrypted(size_t sample_index) const {
554   uint32 index = GetGroupDescriptionIndex(sample_index);
555   return (index == 0) ? track_encryption().is_encrypted
556                       : GetSampleEncryptionInfoEntry(index).is_encrypted;
557 }
558
559 const std::vector<uint8>& TrackRunIterator::GetKeyId(
560     size_t sample_index) const {
561   uint32 index = GetGroupDescriptionIndex(sample_index);
562   return (index == 0) ? track_encryption().default_kid
563                       : GetSampleEncryptionInfoEntry(index).key_id;
564 }
565
566 uint8 TrackRunIterator::GetIvSize(size_t sample_index) const {
567   uint32 index = GetGroupDescriptionIndex(sample_index);
568   return (index == 0) ? track_encryption().default_iv_size
569                       : GetSampleEncryptionInfoEntry(index).iv_size;
570 }
571
572 }  // namespace mp4
573 }  // namespace media