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