Shaka Packager SDK
Loading...
Searching...
No Matches
track_run_iterator.cc
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 <packager/media/formats/mp4/track_run_iterator.h>
6
7#include <algorithm>
8#include <limits>
9
10#include <absl/flags/flag.h>
11#include <absl/log/check.h>
12
13#include <packager/macros/logging.h>
14#include <packager/media/base/buffer_reader.h>
15#include <packager/media/base/fourccs.h>
16#include <packager/media/base/rcheck.h>
17#include <packager/media/formats/mp4/chunk_info_iterator.h>
18#include <packager/media/formats/mp4/composition_offset_iterator.h>
19#include <packager/media/formats/mp4/decoding_time_iterator.h>
20#include <packager/media/formats/mp4/sync_sample_iterator.h>
21
22ABSL_FLAG(bool,
23 mp4_reset_initial_composition_offset_to_zero,
24 true,
25 "MP4 only. If it is true, reset the initial composition offset to "
26 "zero, i.e. by assuming that there is a missing EditList.");
27
28namespace {
29const int64_t kInvalidOffset = std::numeric_limits<int64_t>::max();
30
31int64_t Rescale(int64_t time_in_old_scale,
32 int32_t old_scale,
33 int32_t new_scale) {
34 return (static_cast<double>(time_in_old_scale) / old_scale) * new_scale;
35}
36
37} // namespace
38
39namespace shaka {
40namespace media {
41namespace mp4 {
42
43struct SampleInfo {
44 int64_t size;
45 int64_t duration;
46 int64_t cts_offset;
47 bool is_keyframe;
48};
49
50struct TrackRunInfo {
51 uint32_t track_id;
52 std::vector<SampleInfo> samples;
53 int64_t timescale;
54 int64_t start_dts;
55 int64_t sample_start_offset;
56
57 TrackType track_type;
58 const AudioSampleEntry* audio_description;
59 const VideoSampleEntry* video_description;
60
61 // Stores sample encryption entries, which is populated from 'senc' box if it
62 // is available, otherwise will try to load from cenc auxiliary information.
63 std::vector<SampleEncryptionEntry> sample_encryption_entries;
64
65 // These variables are useful to load |sample_encryption_entries| from cenc
66 // auxiliary information when 'senc' box is not available.
67 int64_t aux_info_start_offset; // Only valid if aux_info_total_size > 0.
68 int aux_info_default_size;
69 std::vector<uint8_t> aux_info_sizes; // Populated if default_size == 0.
70 int aux_info_total_size;
71
72 TrackRunInfo();
73 ~TrackRunInfo();
74};
75
76TrackRunInfo::TrackRunInfo()
77 : track_id(0),
78 timescale(-1),
79 start_dts(-1),
80 sample_start_offset(-1),
81 track_type(kInvalid),
82 audio_description(NULL),
83 video_description(NULL),
84 aux_info_start_offset(-1),
85 aux_info_default_size(0),
86 aux_info_total_size(0) {}
87TrackRunInfo::~TrackRunInfo() {}
88
89TrackRunIterator::TrackRunIterator(const Movie* moov)
90 : moov_(moov), sample_dts_(0), sample_offset_(0) {
91 CHECK(moov);
92}
93
94TrackRunIterator::~TrackRunIterator() {}
95
96static void PopulateSampleInfo(const TrackExtends& trex,
97 const TrackFragmentHeader& tfhd,
98 const TrackFragmentRun& trun,
99 const size_t i,
100 SampleInfo* sample_info) {
101 if (i < trun.sample_sizes.size()) {
102 sample_info->size = trun.sample_sizes[i];
103 } else if (tfhd.default_sample_size > 0) {
104 sample_info->size = tfhd.default_sample_size;
105 } else {
106 sample_info->size = trex.default_sample_size;
107 }
108
109 if (i < trun.sample_durations.size()) {
110 sample_info->duration = trun.sample_durations[i];
111 } else if (tfhd.default_sample_duration > 0) {
112 sample_info->duration = tfhd.default_sample_duration;
113 } else {
114 sample_info->duration = trex.default_sample_duration;
115 }
116
117 if (i < trun.sample_composition_time_offsets.size()) {
118 sample_info->cts_offset = trun.sample_composition_time_offsets[i];
119 } else {
120 sample_info->cts_offset = 0;
121 }
122
123 uint32_t flags;
124 if (i < trun.sample_flags.size()) {
125 flags = trun.sample_flags[i];
126 } else if (tfhd.flags & TrackFragmentHeader::kDefaultSampleFlagsPresentMask) {
127 flags = tfhd.default_sample_flags;
128 } else {
129 flags = trex.default_sample_flags;
130 }
131 sample_info->is_keyframe = !(flags & TrackFragmentHeader::kNonKeySampleMask);
132}
133
134// In well-structured encrypted media, each track run will be immediately
135// preceded by its auxiliary information; this is the only optimal storage
136// pattern in terms of minimum number of bytes from a serial stream needed to
137// begin playback. It also allows us to optimize caching on memory-constrained
138// architectures, because we can cache the relatively small auxiliary
139// information for an entire run and then discard data from the input stream,
140// instead of retaining the entire 'mdat' box.
141//
142// We optimize for this situation (with no loss of generality) by sorting track
143// runs during iteration in order of their first data offset (either sample data
144// or auxiliary data).
145class CompareMinTrackRunDataOffset {
146 public:
147 bool operator()(const TrackRunInfo& a, const TrackRunInfo& b) {
148 int64_t a_aux =
149 a.aux_info_total_size ? a.aux_info_start_offset : kInvalidOffset;
150 int64_t b_aux =
151 b.aux_info_total_size ? b.aux_info_start_offset : kInvalidOffset;
152
153 int64_t a_lesser = std::min(a_aux, a.sample_start_offset);
154 int64_t a_greater = std::max(a_aux, a.sample_start_offset);
155 int64_t b_lesser = std::min(b_aux, b.sample_start_offset);
156 int64_t b_greater = std::max(b_aux, b.sample_start_offset);
157
158 if (a_lesser == b_lesser)
159 return a_greater < b_greater;
160 return a_lesser < b_lesser;
161 }
162};
163
165 runs_.clear();
166
167 for (std::vector<Track>::const_iterator trak = moov_->tracks.begin();
168 trak != moov_->tracks.end(); ++trak) {
169 const SampleDescription& stsd =
170 trak->media.information.sample_table.description;
171 if (stsd.type != kAudio && stsd.type != kVideo) {
172 DVLOG(1) << "Skipping unhandled track type";
173 continue;
174 }
175
176 DecodingTimeIterator decoding_time(
177 trak->media.information.sample_table.decoding_time_to_sample);
178 CompositionOffsetIterator composition_offset(
179 trak->media.information.sample_table.composition_time_to_sample);
180 bool has_composition_offset = composition_offset.IsValid();
181 ChunkInfoIterator chunk_info(
182 trak->media.information.sample_table.sample_to_chunk);
183 SyncSampleIterator sync_sample(
184 trak->media.information.sample_table.sync_sample);
185 // Skip processing saiz and saio boxes for non-fragmented mp4 as we
186 // don't support encrypted non-fragmented mp4.
187
188 const SampleSize& sample_size =
189 trak->media.information.sample_table.sample_size;
190 const std::vector<uint64_t>& chunk_offset_vector =
191 trak->media.information.sample_table.chunk_large_offset.offsets;
192
193 // dts is directly adjusted, which then propagates to pts as pts is encoded
194 // as difference (composition offset) to dts in mp4.
195 int64_t run_start_dts = GetTimestampAdjustment(*moov_, *trak, nullptr);
196
197 uint32_t num_samples = sample_size.sample_count;
198 uint32_t num_chunks = static_cast<uint32_t>(chunk_offset_vector.size());
199
200 // Check that total number of samples match.
201 DCHECK_EQ(num_samples, decoding_time.NumSamples());
202 if (has_composition_offset) {
203 DCHECK_EQ(num_samples, composition_offset.NumSamples());
204 }
205 if (num_chunks > 0) {
206 DCHECK_EQ(num_samples, chunk_info.NumSamples(1, num_chunks));
207 }
208 DCHECK_GE(num_chunks, chunk_info.LastFirstChunk());
209
210 if (num_samples > 0) {
211 // Verify relevant tables are not empty.
212 RCHECK(decoding_time.IsValid());
213 RCHECK(chunk_info.IsValid());
214 }
215
216 uint32_t sample_index = 0;
217 for (uint32_t chunk_index = 0; chunk_index < num_chunks; ++chunk_index) {
218 RCHECK(chunk_info.current_chunk() == chunk_index + 1);
219
220 TrackRunInfo tri;
221 tri.track_id = trak->header.track_id;
222 tri.timescale = trak->media.header.timescale;
223 tri.start_dts = run_start_dts;
224 tri.sample_start_offset = chunk_offset_vector[chunk_index];
225
226 uint32_t desc_idx = chunk_info.sample_description_index();
227 RCHECK(desc_idx > 0); // Descriptions are one-indexed in the file.
228 desc_idx -= 1;
229
230 tri.track_type = stsd.type;
231 if (tri.track_type == kAudio) {
232 RCHECK(!stsd.audio_entries.empty());
233 if (desc_idx > stsd.audio_entries.size())
234 desc_idx = 0;
235 tri.audio_description = &stsd.audio_entries[desc_idx];
236 // We don't support encrypted non-fragmented mp4 for now.
237 RCHECK(tri.audio_description->sinf.info.track_encryption
238 .default_is_protected == 0);
239 } else if (tri.track_type == kVideo) {
240 RCHECK(!stsd.video_entries.empty());
241 if (desc_idx > stsd.video_entries.size())
242 desc_idx = 0;
243 tri.video_description = &stsd.video_entries[desc_idx];
244 // We don't support encrypted non-fragmented mp4 for now.
245 RCHECK(tri.video_description->sinf.info.track_encryption
246 .default_is_protected == 0);
247 }
248
249 uint32_t samples_per_chunk = chunk_info.samples_per_chunk();
250 tri.samples.resize(samples_per_chunk);
251 for (uint32_t k = 0; k < samples_per_chunk; ++k) {
252 SampleInfo& sample = tri.samples[k];
253 sample.size = sample_size.sample_size != 0
254 ? sample_size.sample_size
255 : sample_size.sizes[sample_index];
256 sample.duration = decoding_time.sample_delta();
257 sample.cts_offset =
258 has_composition_offset ? composition_offset.sample_offset() : 0;
259 sample.is_keyframe = sync_sample.IsSyncSample();
260
261 run_start_dts += sample.duration;
262
263 // Advance to next sample. Should success except for last sample.
264 ++sample_index;
265 RCHECK(chunk_info.AdvanceSample() && sync_sample.AdvanceSample());
266 if (sample_index == num_samples) {
267 // We should hit end of tables for decoding time and composition
268 // offset.
269 RCHECK(!decoding_time.AdvanceSample());
270 if (has_composition_offset)
271 RCHECK(!composition_offset.AdvanceSample());
272 } else {
273 RCHECK(decoding_time.AdvanceSample());
274 if (has_composition_offset)
275 RCHECK(composition_offset.AdvanceSample());
276 }
277 }
278
279 runs_.push_back(tri);
280 }
281 }
282
283 std::sort(runs_.begin(), runs_.end(), CompareMinTrackRunDataOffset());
284 run_itr_ = runs_.begin();
285 ResetRun();
286 return true;
287}
288
290 runs_.clear();
291
292 const auto track_count = std::max(moof.tracks.size(), moov_->tracks.size());
293 next_fragment_start_dts_.resize(track_count, 0);
294 for (size_t i = 0; i < moof.tracks.size(); i++) {
295 const TrackFragment& traf = moof.tracks[i];
296 const auto track_index = traf.header.track_id - 1;
297 const Track* trak = NULL;
298 for (size_t t = 0; t < moov_->tracks.size(); t++) {
299 if (moov_->tracks[t].header.track_id == traf.header.track_id)
300 trak = &moov_->tracks[t];
301 }
302 RCHECK(trak);
303
304 const TrackExtends* trex = NULL;
305 for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
306 if (moov_->extends.tracks[t].track_id == traf.header.track_id)
307 trex = &moov_->extends.tracks[t];
308 }
309 RCHECK(trex);
310
311 const SampleDescription& stsd =
312 trak->media.information.sample_table.description;
313 if (stsd.type != kAudio && stsd.type != kVideo) {
314 DVLOG(1) << "Skipping unhandled track type";
315 continue;
316 }
317 size_t desc_idx = traf.header.sample_description_index;
318 if (!desc_idx)
319 desc_idx = trex->default_sample_description_index;
320 RCHECK(desc_idx > 0); // Descriptions are one-indexed in the file
321 desc_idx -= 1;
322
323 const AudioSampleEntry* audio_sample_entry = NULL;
324 const VideoSampleEntry* video_sample_entry = NULL;
325 switch (stsd.type) {
326 case kAudio:
327 RCHECK(!stsd.audio_entries.empty());
328 if (desc_idx > stsd.audio_entries.size())
329 desc_idx = 0;
330 audio_sample_entry = &stsd.audio_entries[desc_idx];
331 break;
332 case kVideo:
333 RCHECK(!stsd.video_entries.empty());
334 if (desc_idx > stsd.video_entries.size())
335 desc_idx = 0;
336 video_sample_entry = &stsd.video_entries[desc_idx];
337 break;
338 default:
339 NOTIMPLEMENTED();
340 break;
341 }
342
343 // SampleEncryptionEntries should not have been parsed, without having
344 // iv_size. Parse the box now.
345 DCHECK(traf.sample_encryption.sample_encryption_entries.empty());
346 std::vector<SampleEncryptionEntry> sample_encryption_entries;
347 if (!traf.sample_encryption.sample_encryption_data.empty()) {
348 RCHECK(audio_sample_entry || video_sample_entry);
349 const uint8_t default_per_sample_iv_size =
350 audio_sample_entry ? audio_sample_entry->sinf.info.track_encryption
351 .default_per_sample_iv_size
352 : video_sample_entry->sinf.info.track_encryption
353 .default_per_sample_iv_size;
354 RCHECK(traf.sample_encryption.ParseFromSampleEncryptionData(
355 default_per_sample_iv_size, &sample_encryption_entries));
356 }
357
358 int64_t run_start_dts = traf.decode_time_absent
359 ? next_fragment_start_dts_[track_index]
360 : traf.decode_time.decode_time;
361
362 // dts is directly adjusted, which then propagates to pts as pts is encoded
363 // as difference (composition offset) to dts in mp4.
364 run_start_dts += GetTimestampAdjustment(*moov_, *trak, &traf);
365
366 int sample_count_sum = 0;
367
368 for (size_t j = 0; j < traf.runs.size(); j++) {
369 const TrackFragmentRun& trun = traf.runs[j];
370 TrackRunInfo tri;
371 tri.track_id = traf.header.track_id;
372 tri.timescale = trak->media.header.timescale;
373 tri.start_dts = run_start_dts;
374 tri.sample_start_offset = trun.data_offset;
375
376 tri.track_type = stsd.type;
377 tri.audio_description = audio_sample_entry;
378 tri.video_description = video_sample_entry;
379
380 tri.aux_info_start_offset = -1;
381 tri.aux_info_total_size = 0;
382 // Populate sample encryption entries from SampleEncryption 'senc' box if
383 // it is available; otherwise initialize aux_info variables, which will
384 // be used to populate sample encryption entries later in CacheAuxInfo.
385 if (!sample_encryption_entries.empty()) {
386 RCHECK(sample_encryption_entries.size() >=
387 sample_count_sum + trun.sample_count);
388 for (size_t k = 0; k < trun.sample_count; ++k) {
389 tri.sample_encryption_entries.push_back(
390 sample_encryption_entries[sample_count_sum + k]);
391 }
392 } else if (traf.auxiliary_offset.offsets.size() > j) {
393 // Collect information from the auxiliary_offset entry with the same
394 // index in the 'saiz' container as the current run's index in the
395 // 'trun' container, if it is present.
396 tri.aux_info_start_offset = traf.auxiliary_offset.offsets[j];
397 // There should be an auxiliary info entry corresponding to each sample
398 // in the auxiliary offset entry's corresponding track run.
399 RCHECK(traf.auxiliary_size.sample_count >=
400 sample_count_sum + trun.sample_count);
401 tri.aux_info_default_size =
402 traf.auxiliary_size.default_sample_info_size;
403 if (tri.aux_info_default_size == 0) {
404 const std::vector<uint8_t>& sizes =
405 traf.auxiliary_size.sample_info_sizes;
406 tri.aux_info_sizes.insert(
407 tri.aux_info_sizes.begin(), sizes.begin() + sample_count_sum,
408 sizes.begin() + sample_count_sum + trun.sample_count);
409 }
410
411 // If the default info size is positive, find the total size of the aux
412 // info block from it, otherwise sum over the individual sizes of each
413 // aux info entry in the aux_offset entry.
414 if (tri.aux_info_default_size) {
415 tri.aux_info_total_size =
416 tri.aux_info_default_size * trun.sample_count;
417 } else {
418 tri.aux_info_total_size = 0;
419 for (size_t k = 0; k < trun.sample_count; k++) {
420 tri.aux_info_total_size += tri.aux_info_sizes[k];
421 }
422 }
423 }
424
425 tri.samples.resize(trun.sample_count);
426 for (size_t k = 0; k < trun.sample_count; k++) {
427 PopulateSampleInfo(*trex, traf.header, trun, k, &tri.samples[k]);
428 run_start_dts += tri.samples[k].duration;
429 }
430 runs_.push_back(tri);
431 sample_count_sum += trun.sample_count;
432 }
433 next_fragment_start_dts_[track_index] = run_start_dts;
434 }
435
436 std::sort(runs_.begin(), runs_.end(), CompareMinTrackRunDataOffset());
437 run_itr_ = runs_.begin();
438 ResetRun();
439 return true;
440}
441
443 ++run_itr_;
444 ResetRun();
445}
446
447void TrackRunIterator::ResetRun() {
448 if (!IsRunValid())
449 return;
450 sample_dts_ = run_itr_->start_dts;
451 sample_offset_ = run_itr_->sample_start_offset;
452 sample_itr_ = run_itr_->samples.begin();
453}
454
456 DCHECK(IsSampleValid());
457 sample_dts_ += sample_itr_->duration;
458 sample_offset_ += sample_itr_->size;
459 ++sample_itr_;
460}
461
462// This implementation only indicates a need for caching if CENC auxiliary
463// info is available in the stream.
465 DCHECK(IsRunValid());
466 return is_encrypted() && aux_info_size() > 0 &&
467 run_itr_->sample_encryption_entries.size() == 0;
468}
469
470// This implementation currently only caches CENC auxiliary info.
471bool TrackRunIterator::CacheAuxInfo(const uint8_t* buf, int buf_size) {
472 RCHECK(AuxInfoNeedsToBeCached() && buf_size >= aux_info_size());
473
474 std::vector<SampleEncryptionEntry>& sample_encryption_entries =
475 runs_[run_itr_ - runs_.begin()].sample_encryption_entries;
476 sample_encryption_entries.resize(run_itr_->samples.size());
477 int64_t pos = 0;
478 for (size_t i = 0; i < run_itr_->samples.size(); i++) {
479 int info_size = run_itr_->aux_info_default_size;
480 if (!info_size)
481 info_size = run_itr_->aux_info_sizes[i];
482
483 BufferReader reader(buf + pos, info_size);
484 const bool has_subsamples =
485 info_size > track_encryption().default_per_sample_iv_size;
486 RCHECK(sample_encryption_entries[i].ParseFromBuffer(
487 track_encryption().default_per_sample_iv_size, has_subsamples,
488 &reader));
489 pos += info_size;
490 }
491
492 return true;
493}
494
496 return run_itr_ != runs_.end();
497}
498
500 return IsRunValid() && (sample_itr_ != run_itr_->samples.end());
501}
502
503// Because tracks are in sorted order and auxiliary information is cached when
504// returning samples, it is guaranteed that no data will be required before the
505// lesser of the minimum data offset of this track and the next in sequence.
506// (The stronger condition - that no data is required before the minimum data
507// offset of this track alone - is not guaranteed, because the BMFF spec does
508// not have any inter-run ordering restrictions.)
510 int64_t offset = kInvalidOffset;
511
512 if (IsSampleValid()) {
513 offset = std::min(offset, sample_offset_);
515 offset = std::min(offset, aux_info_offset());
516 }
517 if (run_itr_ != runs_.end()) {
518 std::vector<TrackRunInfo>::const_iterator next_run = run_itr_ + 1;
519 if (next_run != runs_.end()) {
520 offset = std::min(offset, next_run->sample_start_offset);
521 if (next_run->aux_info_total_size)
522 offset = std::min(offset, next_run->aux_info_start_offset);
523 }
524 }
525 if (offset == kInvalidOffset)
526 return runs_.empty() ? 0 : runs_[0].sample_start_offset;
527 return offset;
528}
529
530uint32_t TrackRunIterator::track_id() const {
531 DCHECK(IsRunValid());
532 return run_itr_->track_id;
533}
534
535bool TrackRunIterator::is_encrypted() const {
536 DCHECK(IsRunValid());
537 return track_encryption().default_is_protected == 1;
538}
539
540int64_t TrackRunIterator::aux_info_offset() const {
541 return run_itr_->aux_info_start_offset;
542}
543
544int TrackRunIterator::aux_info_size() const {
545 return run_itr_->aux_info_total_size;
546}
547
548bool TrackRunIterator::is_audio() const {
549 DCHECK(IsRunValid());
550 return run_itr_->track_type == kAudio;
551}
552
553bool TrackRunIterator::is_video() const {
554 DCHECK(IsRunValid());
555 return run_itr_->track_type == kVideo;
556}
557
559 DCHECK(is_audio());
560 DCHECK(run_itr_->audio_description);
561 return *run_itr_->audio_description;
562}
563
565 DCHECK(is_video());
566 DCHECK(run_itr_->video_description);
567 return *run_itr_->video_description;
568}
569
570int64_t TrackRunIterator::sample_offset() const {
571 DCHECK(IsSampleValid());
572 return sample_offset_;
573}
574
575int TrackRunIterator::sample_size() const {
576 DCHECK(IsSampleValid());
577 return sample_itr_->size;
578}
579
580int64_t TrackRunIterator::dts() const {
581 DCHECK(IsSampleValid());
582 return sample_dts_;
583}
584
585int64_t TrackRunIterator::cts() const {
586 DCHECK(IsSampleValid());
587 return sample_dts_ + sample_itr_->cts_offset;
588}
589
590int64_t TrackRunIterator::duration() const {
591 DCHECK(IsSampleValid());
592 return sample_itr_->duration;
593}
594
595bool TrackRunIterator::is_keyframe() const {
596 DCHECK(IsSampleValid());
597 return sample_itr_->is_keyframe;
598}
599
600const TrackEncryption& TrackRunIterator::track_encryption() const {
601 if (is_audio())
602 return audio_description().sinf.info.track_encryption;
603 DCHECK(is_video());
604 return video_description().sinf.info.track_encryption;
605}
606
607std::unique_ptr<DecryptConfig> TrackRunIterator::GetDecryptConfig() {
608 std::vector<uint8_t> iv;
609 std::vector<SubsampleEntry> subsamples;
610
611 size_t sample_idx = sample_itr_ - run_itr_->samples.begin();
612 if (sample_idx < run_itr_->sample_encryption_entries.size()) {
613 const SampleEncryptionEntry& sample_encryption_entry =
614 run_itr_->sample_encryption_entries[sample_idx];
615 DCHECK(is_encrypted());
616 DCHECK(!AuxInfoNeedsToBeCached());
617
618 const size_t total_size_of_subsamples =
619 sample_encryption_entry.GetTotalSizeOfSubsamples();
620 if (total_size_of_subsamples != 0 &&
621 total_size_of_subsamples != static_cast<size_t>(sample_size())) {
622 LOG(ERROR) << "Incorrect CENC subsample size.";
623 return std::unique_ptr<DecryptConfig>();
624 }
625
626 iv = sample_encryption_entry.initialization_vector;
627 subsamples = sample_encryption_entry.subsamples;
628 }
629
630 FourCC protection_scheme = is_audio() ? audio_description().sinf.type.type
631 : video_description().sinf.type.type;
632 if (iv.empty()) {
633 if (protection_scheme != FOURCC_cbcs) {
634 LOG(WARNING)
635 << "Constant IV should only be used with 'cbcs' protection scheme.";
636 }
637 iv = track_encryption().default_constant_iv;
638 if (iv.empty()) {
639 LOG(ERROR) << "IV cannot be empty.";
640 return std::unique_ptr<DecryptConfig>();
641 }
642 }
643 return std::unique_ptr<DecryptConfig>(new DecryptConfig(
644 track_encryption().default_kid, iv, subsamples, protection_scheme,
645 track_encryption().default_crypt_byte_block,
646 track_encryption().default_skip_byte_block));
647}
648
649int64_t TrackRunIterator::GetTimestampAdjustment(const Movie& movie,
650 const Track& track,
651 const TrackFragment* traf) {
652 const uint32_t track_id = track.header.track_id;
653 const auto iter = timestamp_adjustment_map_.find(track_id);
654 if (iter != timestamp_adjustment_map_.end())
655 return iter->second;
656
657 int64_t timestamp_adjustment = 0;
658 const std::vector<EditListEntry>& edits = track.edit.list.edits;
659 if (!edits.empty()) {
660 // ISO/IEC 14496-12:2015 8.6.6 Edit List Box.
661 for (const EditListEntry& edit : edits) {
662 if (edit.media_rate_integer != 1) {
663 LOG(INFO) << "dwell EditListEntry is ignored.";
664 continue;
665 }
666
667 if (edit.media_time < 0) {
668 // This is an empty edit. |segment_duration| is in movie's timescale
669 // instead of track's timescale.
670 const int64_t scaled_time =
671 Rescale(edit.segment_duration, movie.header.timescale,
672 track.media.header.timescale);
673 timestamp_adjustment += scaled_time;
674 } else {
675 timestamp_adjustment -= edit.media_time;
676 }
677 }
678 }
679
680 if (timestamp_adjustment == 0) {
681 int64_t composition_offset = 0;
682 if (traf && !traf->runs.empty()) {
683 const auto& cts_offsets =
684 traf->runs.front().sample_composition_time_offsets;
685 if (!cts_offsets.empty())
686 composition_offset = cts_offsets.front();
687 } else {
688 CompositionOffsetIterator composition_offset_iter(
689 track.media.information.sample_table.composition_time_to_sample);
690 if (!composition_offset_iter.IsValid()) {
691 // This is the init (sub)segment of a fragmented mp4, which does not
692 // contain any samples. Exit with 0 adjustment and without storing
693 // |timestamp_adjustment|. This function will be called again later
694 // with track fragment |traf|. |timestamp_adjustment| will be computed
695 // and stored then.
696 return 0;
697 }
698 composition_offset = composition_offset_iter.sample_offset();
699 }
700
701 int64_t decode_time = 0;
702 if (traf)
703 decode_time = traf->decode_time.decode_time;
704 if (composition_offset != 0 && decode_time == 0) {
705 LOG(WARNING) << "Seeing non-zero composition offset "
706 << composition_offset
707 << ". An EditList is probably missing.";
708 if (absl::GetFlag(FLAGS_mp4_reset_initial_composition_offset_to_zero)) {
709 LOG(WARNING)
710 << "Adjusting timestamps by " << -composition_offset
711 << ". Please file a bug to "
712 "https://github.com/shaka-project/shaka-packager/issues if you "
713 "do not think it is right or if you are seeing any problems.";
714 timestamp_adjustment = -composition_offset;
715 }
716 }
717 }
718
719 timestamp_adjustment_map_.insert(
720 std::make_pair(track_id, timestamp_adjustment));
721 return timestamp_adjustment;
722}
723
724} // namespace mp4
725} // namespace media
726} // namespace shaka
uint32_t NumSamples(uint32_t start_chunk, uint32_t end_chunk) const
const VideoSampleEntry & video_description() const
Only valid if is_video() is true.
const AudioSampleEntry & audio_description() const
Only valid if is_audio() is true.
bool CacheAuxInfo(const uint8_t *buf, int size)
std::unique_ptr< DecryptConfig > GetDecryptConfig()
All the methods that are virtual are virtual for mocking.
std::vector< uint8_t > sample_encryption_data
bool ParseFromSampleEncryptionData(uint8_t l_iv_size, std::vector< SampleEncryptionEntry > *l_sample_encryption_entries) const