| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "media/mp4/track_run_iterator.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "media/mp4/rcheck.h" |
| 10 |
| 11 namespace media { |
| 12 namespace mp4 { |
| 13 |
| 14 base::TimeDelta TimeDeltaFromFrac(int64 numer, uint64 denom) { |
| 15 DCHECK_LT(std::abs(numer), kint64max / base::Time::kMicrosecondsPerSecond); |
| 16 return base::TimeDelta::FromMicroseconds( |
| 17 base::Time::kMicrosecondsPerSecond * numer / denom); |
| 18 } |
| 19 |
| 20 static const uint32 kSampleIsDifferenceSampleFlagMask = 0x10000; |
| 21 |
| 22 TrackRunInfo::TrackRunInfo() {} |
| 23 TrackRunInfo::~TrackRunInfo() {} |
| 24 |
| 25 TrackRunIterator::TrackRunIterator() {} |
| 26 TrackRunIterator::~TrackRunIterator() {} |
| 27 |
| 28 static void PopulateSampleInfo(const Track& trak, |
| 29 const TrackExtends& trex, |
| 30 const TrackFragmentHeader& tfhd, |
| 31 const TrackFragmentRun& trun, |
| 32 const uint32 i, |
| 33 SampleInfo* sample_info) { |
| 34 if (i < trun.sample_sizes.size()) { |
| 35 sample_info->size = trun.sample_sizes[i]; |
| 36 } else if (tfhd.default_sample_size > 0) { |
| 37 sample_info->size = tfhd.default_sample_size; |
| 38 } else { |
| 39 sample_info->size = trex.default_sample_size; |
| 40 } |
| 41 |
| 42 const uint64 timescale = trak.media.header.timescale; |
| 43 uint64 duration; |
| 44 if (i < trun.sample_durations.size()) { |
| 45 duration = trun.sample_durations[i]; |
| 46 } else if (tfhd.default_sample_duration > 0) { |
| 47 duration = tfhd.default_sample_duration; |
| 48 } else { |
| 49 duration = trex.default_sample_duration; |
| 50 } |
| 51 sample_info->duration = TimeDeltaFromFrac(duration, timescale); |
| 52 |
| 53 if (i < trun.sample_composition_time_offsets.size()) { |
| 54 sample_info->cts_offset = |
| 55 TimeDeltaFromFrac(trun.sample_composition_time_offsets[i], timescale); |
| 56 } else { |
| 57 sample_info->cts_offset = TimeDelta::FromMicroseconds(0); |
| 58 } |
| 59 |
| 60 uint32 flags; |
| 61 if (i < trun.sample_flags.size()) { |
| 62 flags = trun.sample_flags[i]; |
| 63 } else if (tfhd.has_default_sample_flags) { |
| 64 flags = tfhd.default_sample_flags; |
| 65 } else { |
| 66 flags = trex.default_sample_flags; |
| 67 } |
| 68 sample_info->is_keyframe = !(flags & kSampleIsDifferenceSampleFlagMask); |
| 69 } |
| 70 |
| 71 class CompareOffset { |
| 72 public: |
| 73 bool operator()(TrackRunInfo a, TrackRunInfo b) { |
| 74 int64 a_min = a.sample_start_offset; |
| 75 if (a.is_encrypted && a.cenc_start_offset < a_min) |
| 76 a_min = a.cenc_start_offset; |
| 77 int64 b_min = b.sample_start_offset; |
| 78 if (b.is_encrypted && b.cenc_start_offset < b_min) |
| 79 b_min = b.cenc_start_offset; |
| 80 return a_min < b_min; |
| 81 } |
| 82 }; |
| 83 |
| 84 bool TrackRunIterator::Init(const Movie& moov, const MovieFragment& moof) { |
| 85 runs_.clear(); |
| 86 |
| 87 for (size_t i = 0; i < moof.tracks.size(); i++) { |
| 88 const TrackFragment& traf = moof.tracks[i]; |
| 89 |
| 90 const Track* trak = NULL; |
| 91 for (size_t t = 0; t < moov.tracks.size(); t++) { |
| 92 if (moov.tracks[t].header.track_id == traf.header.track_id) |
| 93 trak = &moov.tracks[t]; |
| 94 } |
| 95 |
| 96 const TrackExtends* trex = NULL; |
| 97 for (size_t t = 0; t < moov.extends.tracks.size(); t++) { |
| 98 if (moov.extends.tracks[t].track_id == traf.header.track_id) |
| 99 trex = &moov.extends.tracks[t]; |
| 100 } |
| 101 RCHECK(trak && trex); |
| 102 |
| 103 const ProtectionSchemeInfo* sinf = NULL; |
| 104 const SampleDescription& stsd = |
| 105 trak->media.information.sample_table.description; |
| 106 if (stsd.type == kAudio) { |
| 107 sinf = &stsd.audio_entries[0].sinf; |
| 108 } else if (stsd.type == kVideo) { |
| 109 sinf = &stsd.video_entries[0].sinf; |
| 110 } else { |
| 111 DVLOG(1) << "Skipping unhandled track type"; |
| 112 continue; |
| 113 } |
| 114 |
| 115 for (size_t j = 0; j < traf.runs.size(); j++) { |
| 116 const TrackFragmentRun& trun = traf.runs[j]; |
| 117 TrackRunInfo tri; |
| 118 tri.track_id = traf.header.track_id; |
| 119 tri.start_dts = TimeDeltaFromFrac(traf.decode_time.decode_time, |
| 120 trak->media.header.timescale); |
| 121 tri.sample_start_offset = trun.data_offset; |
| 122 tri.is_encrypted = sinf->info.track_encryption.is_encrypted; |
| 123 // TODO(strobe): CENC recovery and testing (http://crbug.com/132351) |
| 124 |
| 125 tri.samples.resize(trun.sample_count); |
| 126 |
| 127 for (size_t k = 0; k < trun.sample_count; k++) { |
| 128 PopulateSampleInfo(*trak, *trex, traf.header, trun, k, &tri.samples[k]); |
| 129 } |
| 130 runs_.push_back(tri); |
| 131 } |
| 132 } |
| 133 |
| 134 std::sort(runs_.begin(), runs_.end(), CompareOffset()); |
| 135 run_itr_ = runs_.begin(); |
| 136 min_clear_offset_itr_ = min_clear_offsets_.begin(); |
| 137 ResetRun(); |
| 138 return true; |
| 139 } |
| 140 |
| 141 void TrackRunIterator::AdvanceRun() { |
| 142 ++run_itr_; |
| 143 if (min_clear_offset_itr_ != min_clear_offsets_.end()) |
| 144 ++min_clear_offset_itr_; |
| 145 ResetRun(); |
| 146 } |
| 147 |
| 148 void TrackRunIterator::ResetRun() { |
| 149 if (!RunValid()) return; |
| 150 sample_dts_ = run_itr_->start_dts; |
| 151 sample_offset_ = run_itr_->sample_start_offset; |
| 152 sample_itr_ = run_itr_->samples.begin(); |
| 153 } |
| 154 |
| 155 void TrackRunIterator::AdvanceSample() { |
| 156 DCHECK(SampleValid()); |
| 157 sample_dts_ += sample_itr_->duration; |
| 158 sample_offset_ += sample_itr_->size; |
| 159 ++sample_itr_; |
| 160 } |
| 161 |
| 162 bool TrackRunIterator::NeedsCENC() { |
| 163 CHECK(!is_encrypted()) << "TODO(strobe): Implement CENC."; |
| 164 return is_encrypted(); |
| 165 } |
| 166 |
| 167 bool TrackRunIterator::CacheCENC(const uint8* buf, int size) { |
| 168 LOG(FATAL) << "Not implemented"; |
| 169 return false; |
| 170 } |
| 171 |
| 172 bool TrackRunIterator::RunValid() const { |
| 173 return run_itr_ != runs_.end(); |
| 174 } |
| 175 |
| 176 bool TrackRunIterator::SampleValid() const { |
| 177 return RunValid() && (sample_itr_ != run_itr_->samples.end()); |
| 178 } |
| 179 |
| 180 int64 TrackRunIterator::GetMaxClearOffset() { |
| 181 int64 offset = kint64max; |
| 182 |
| 183 if (SampleValid()) { |
| 184 offset = std::min(offset, sample_offset_); |
| 185 if (NeedsCENC()) { |
| 186 offset = std::min(offset, cenc_offset()); |
| 187 } |
| 188 } |
| 189 if (min_clear_offset_itr_ != min_clear_offsets_.end()) { |
| 190 offset = std::min(offset, *min_clear_offset_itr_); |
| 191 } |
| 192 if (offset == kint64max) return 0; |
| 193 return offset; |
| 194 } |
| 195 |
| 196 uint32 TrackRunIterator::track_id() const { |
| 197 DCHECK(RunValid()); |
| 198 return run_itr_->track_id; |
| 199 } |
| 200 |
| 201 bool TrackRunIterator::is_encrypted() const { |
| 202 DCHECK(RunValid()); |
| 203 return run_itr_->is_encrypted; |
| 204 } |
| 205 |
| 206 int64 TrackRunIterator::cenc_offset() const { |
| 207 DCHECK(is_encrypted()); |
| 208 return run_itr_->cenc_start_offset; |
| 209 } |
| 210 |
| 211 int TrackRunIterator::cenc_size() const { |
| 212 DCHECK(is_encrypted()); |
| 213 return run_itr_->cenc_total_size; |
| 214 } |
| 215 |
| 216 int64 TrackRunIterator::offset() const { |
| 217 DCHECK(SampleValid()); |
| 218 return sample_offset_; |
| 219 } |
| 220 |
| 221 int TrackRunIterator::size() const { |
| 222 DCHECK(SampleValid()); |
| 223 return sample_itr_->size; |
| 224 } |
| 225 |
| 226 TimeDelta TrackRunIterator::dts() const { |
| 227 DCHECK(SampleValid()); |
| 228 return sample_dts_; |
| 229 } |
| 230 |
| 231 TimeDelta TrackRunIterator::cts() const { |
| 232 DCHECK(SampleValid()); |
| 233 return sample_dts_ + sample_itr_->cts_offset; |
| 234 } |
| 235 |
| 236 TimeDelta TrackRunIterator::duration() const { |
| 237 DCHECK(SampleValid()); |
| 238 return sample_itr_->duration; |
| 239 } |
| 240 |
| 241 bool TrackRunIterator::is_keyframe() const { |
| 242 DCHECK(SampleValid()); |
| 243 return sample_itr_->is_keyframe; |
| 244 } |
| 245 |
| 246 const FrameCENCInfo& TrackRunIterator::frame_cenc_info() { |
| 247 DCHECK(is_encrypted()); |
| 248 return frame_cenc_info_; |
| 249 } |
| 250 |
| 251 } // namespace mp4 |
| 252 } // namespace media |
| OLD | NEW |