Debian Codegraph @

third_party/rust/mp4parse/src/unstable.rs

raw
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
4 use num_traits::{CheckedAdd, CheckedSub, PrimInt, Zero};
5 use std::ops::{Add, Neg, Sub};
6
7 use super::*;
8
9 /// A zero-overhead wrapper around integer types for the sake of always
10 /// requiring checked arithmetic
11 #[repr(transparent)]
12 #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
13 pub struct CheckedInteger<T>(pub T);
14
15 impl<T> From<T> for CheckedInteger<T> {
16 fn from(i: T) -> Self {
17 Self(i)
18 }
19 }
20
21 // Orphan rules prevent a more general implementation, but this suffices
22 impl From<CheckedInteger<i64>> for i64 {
23 fn from(checked: CheckedInteger<i64>) -> i64 {
24 checked.0
25 }
26 }
27
28 impl<T, U: Into<T>> Add<U> for CheckedInteger<T>
29 where
30 T: CheckedAdd,
31 {
32 type Output = Option<Self>;
33
34 fn add(self, other: U) -> Self::Output {
35 self.0.checked_add(&other.into()).map(Into::into)
36 }
37 }
38
39 impl<T, U: Into<T>> Sub<U> for CheckedInteger<T>
40 where
41 T: CheckedSub,
42 {
43 type Output = Option<Self>;
44
45 fn sub(self, other: U) -> Self::Output {
46 self.0.checked_sub(&other.into()).map(Into::into)
47 }
48 }
49
50 /// Implement subtraction of checked `u64`s returning i64
51 // This is necessary for handling Mp4parseTrackInfo::media_time gracefully
52 impl Sub for CheckedInteger<u64> {
53 type Output = Option<CheckedInteger<i64>>;
54
55 fn sub(self, other: Self) -> Self::Output {
56 if self >= other {
57 self.0
58 .checked_sub(other.0)
59 .and_then(|u| i64::try_from(u).ok())
60 .map(CheckedInteger)
61 } else {
62 other
63 .0
64 .checked_sub(self.0)
65 .and_then(|u| i64::try_from(u).ok())
66 .map(i64::neg)
67 .map(CheckedInteger)
68 }
69 }
70 }
71
72 #[test]
73 fn u64_subtraction_returning_i64() {
74 // self > other
75 assert_eq!(
76 CheckedInteger(2u64) - CheckedInteger(1u64),
77 Some(CheckedInteger(1i64))
78 );
79
80 // self == other
81 assert_eq!(
82 CheckedInteger(1u64) - CheckedInteger(1u64),
83 Some(CheckedInteger(0i64))
84 );
85
86 // difference too large to store in i64
87 assert_eq!(CheckedInteger(u64::MAX) - CheckedInteger(1u64), None);
88
89 // self < other
90 assert_eq!(
91 CheckedInteger(1u64) - CheckedInteger(2u64),
92 Some(CheckedInteger(-1i64))
93 );
94
95 // difference not representable due to overflow
96 assert_eq!(CheckedInteger(1u64) - CheckedInteger(u64::MAX), None);
97 }
98
99 impl<T: std::cmp::PartialEq> PartialEq<T> for CheckedInteger<T> {
100 fn eq(&self, other: &T) -> bool {
101 self.0 == *other
102 }
103 }
104
105 /// Provides the following information about a sample in the source file:
106 /// sample data offset (start and end), composition time in microseconds
107 /// (start and end) and whether it is a sync sample
108 #[repr(C)]
109 #[derive(Default, Debug, PartialEq, Eq)]
110 pub struct Indice {
111 /// The byte offset in the file where the indexed sample begins.
112 pub start_offset: CheckedInteger<u64>,
113 /// The byte offset in the file where the indexed sample ends. This is
114 /// equivalent to `start_offset` + the length in bytes of the indexed
115 /// sample. Typically this will be the `start_offset` of the next sample
116 /// in the file.
117 pub end_offset: CheckedInteger<u64>,
118 /// The time in ticks when the indexed sample should be displayed.
119 /// Analogous to the concept of presentation time stamp (pts).
120 pub start_composition: CheckedInteger<i64>,
121 /// The time in ticks when the indexed sample should stop being
122 /// displayed. Typically this would be the `start_composition` time of the
123 /// next sample if samples were ordered by composition time.
124 pub end_composition: CheckedInteger<i64>,
125 /// The time in ticks that the indexed sample should be decoded at.
126 /// Analogous to the concept of decode time stamp (dts).
127 pub start_decode: CheckedInteger<i64>,
128 /// Set if the indexed sample is a sync sample. The meaning of sync is
129 /// somewhat codec specific, but essentially amounts to if the sample is a
130 /// key frame.
131 pub sync: bool,
132 }
133
134 /// Create a vector of `Indice`s with the information about track samples.
135 /// It uses `stsc`, `stco`, `stsz` and `stts` boxes to construct a list of
136 /// every sample in the file and provides offsets which can be used to read
137 /// raw sample data from the file.
138 #[allow(clippy::reversed_empty_ranges)]
139 pub fn create_sample_table(
140 track: &Track,
141 track_offset_time: CheckedInteger<i64>,
142 ) -> Option<TryVec<Indice>> {
143 let (stsc, stco, stsz, stts) = match (&track.stsc, &track.stco, &track.stsz, &track.stts) {
144 (Some(a), Some(b), Some(c), Some(d)) => (a, b, c, d),
145 _ => return None,
146 };
147
148 // According to spec, no sync table means every sample is sync sample.
149 let has_sync_table = track.stss.is_some();
150
151 let mut sample_size_iter = stsz.sample_sizes.iter();
152
153 // Get 'stsc' iterator for (chunk_id, chunk_sample_count) and calculate the sample
154 // offset address.
155
156 // With large numbers of samples, the cost of many allocations dominates,
157 // so it's worth iterating twice to allocate sample_table just once.
158 let total_sample_count = sample_to_chunk_iter(&stsc.samples, &stco.offsets)
159 .map(|(_, sample_counts)| sample_counts.to_usize())
160 .try_fold(0usize, usize::checked_add)?;
161 let mut sample_table = TryVec::with_capacity(total_sample_count).ok()?;
162
163 for i in sample_to_chunk_iter(&stsc.samples, &stco.offsets) {
164 let chunk_id = i.0 as usize;
165 let sample_counts = i.1;
166 let mut cur_position = match stco.offsets.get(chunk_id) {
167 Some(&i) => i.into(),
168 _ => return None,
169 };
170 for _ in 0..sample_counts {
171 let start_offset = cur_position;
172 let end_offset = match (stsz.sample_size, sample_size_iter.next()) {
173 (_, Some(t)) => (start_offset + *t)?,
174 (t, _) if t > 0 => (start_offset + t)?,
175 _ => 0.into(),
176 };
177 if end_offset == 0 {
178 return None;
179 }
180 cur_position = end_offset;
181
182 sample_table
183 .push(Indice {
184 start_offset,
185 end_offset,
186 sync: !has_sync_table,
187 ..Default::default()
188 })
189 .ok()?;
190 }
191 }
192
193 // Mark the sync sample in sample_table according to 'stss'.
194 if let Some(ref v) = track.stss {
195 for iter in &v.samples {
196 match iter
197 .checked_sub(&1)
198 .and_then(|idx| sample_table.get_mut(idx as usize))
199 {
200 Some(elem) => elem.sync = true,
201 _ => return None,
202 }
203 }
204 }
205
206 let ctts_iter = track.ctts.as_ref().map(|v| v.samples.as_slice().iter());
207
208 let mut ctts_offset_iter = TimeOffsetIterator {
209 cur_sample_range: (0..0),
210 cur_offset: 0,
211 ctts_iter,
212 track_id: track.id,
213 };
214
215 let mut stts_iter = TimeToSampleIterator {
216 cur_sample_count: (0..0),
217 cur_sample_delta: 0,
218 stts_iter: stts.samples.as_slice().iter(),
219 track_id: track.id,
220 };
221
222 // sum_delta is the sum of stts_iter delta.
223 // According to spec:
224 // decode time => DT(n) = DT(n-1) + STTS(n)
225 // composition time => CT(n) = DT(n) + CTTS(n)
226 // Note:
227 // composition time needs to add the track offset time from 'elst' table.
228 let mut sum_delta = TrackScaledTime::<i64>(0, track.id);
229 for sample in sample_table.as_mut_slice() {
230 let decode_time = sum_delta;
231 sum_delta = (sum_delta + stts_iter.next_delta())?;
232
233 // ctts_offset is the current sample offset time.
234 let ctts_offset = ctts_offset_iter.next_offset_time();
235
236 let start_composition = decode_time + ctts_offset;
237
238 let end_composition = sum_delta + ctts_offset;
239
240 let start_decode = decode_time;
241
242 sample.start_composition = CheckedInteger(track_offset_time.0 + start_composition?.0);
243 sample.end_composition = CheckedInteger(track_offset_time.0 + end_composition?.0);
244 sample.start_decode = CheckedInteger(start_decode.0);
245 }
246
247 // Correct composition end time due to 'ctts' causes composition time re-ordering.
248 //
249 // Composition end time is not in specification. However, gecko needs it, so we need to
250 // calculate to correct the composition end time.
251 if !sample_table.is_empty() {
252 // Create an index table refers to sample_table and sorted by start_composisiton time.
253 let mut sort_table = TryVec::with_capacity(sample_table.len()).ok()?;
254
255 for i in 0..sample_table.len() {
256 sort_table.push(i).ok()?;
257 }
258
259 sort_table.sort_by_key(|i| match sample_table.get(*i) {
260 Some(v) => v.start_composition,
261 _ => 0.into(),
262 });
263
264 for indices in sort_table.windows(2) {
265 if let [current_index, peek_index] = *indices {
266 let next_start_composition_time = sample_table[peek_index].start_composition;
267 let sample = &mut sample_table[current_index];
268 sample.end_composition = next_start_composition_time;
269 }
270 }
271 }
272
273 Some(sample_table)
274 }
275
276 // Convert a 'ctts' compact table to full table by iterator,
277 // (sample_with_the_same_offset_count, offset) => (offset), (offset), (offset) ...
278 //
279 // For example:
280 // (2, 10), (4, 9) into (10, 10, 9, 9, 9, 9) by calling next_offset_time().
281 struct TimeOffsetIterator<'a> {
282 cur_sample_range: std::ops::Range<u32>,
283 cur_offset: i64,
284 ctts_iter: Option<std::slice::Iter<'a, TimeOffset>>,
285 track_id: usize,
286 }
287
288 impl Iterator for TimeOffsetIterator<'_> {
289 type Item = i64;
290
291 #[allow(clippy::reversed_empty_ranges)]
292 fn next(&mut self) -> Option<i64> {
293 let has_sample = self.cur_sample_range.next().or_else(|| {
294 // At end of current TimeOffset, find the next TimeOffset.
295 let iter = match self.ctts_iter {
296 Some(ref mut v) => v,
297 _ => return None,
298 };
299 let offset_version;
300 self.cur_sample_range = match iter.next() {
301 Some(v) => {
302 offset_version = v.time_offset;
303 0..v.sample_count
304 }
305 _ => {
306 offset_version = TimeOffsetVersion::Version0(0);
307 0..0
308 }
309 };
310
311 self.cur_offset = match offset_version {
312 TimeOffsetVersion::Version0(i) => i64::from(i),
313 TimeOffsetVersion::Version1(i) => i64::from(i),
314 };
315
316 self.cur_sample_range.next()
317 });
318
319 has_sample.and(Some(self.cur_offset))
320 }
321 }
322
323 impl TimeOffsetIterator<'_> {
324 fn next_offset_time(&mut self) -> TrackScaledTime<i64> {
325 match self.next() {
326 Some(v) => TrackScaledTime::<i64>(v, self.track_id),
327 _ => TrackScaledTime::<i64>(0, self.track_id),
328 }
329 }
330 }
331
332 // Convert 'stts' compact table to full table by iterator,
333 // (sample_count_with_the_same_time, time) => (time, time, time) ... repeats
334 // sample_count_with_the_same_time.
335 //
336 // For example:
337 // (2, 3000), (1, 2999) to (3000, 3000, 2999).
338 struct TimeToSampleIterator<'a> {
339 cur_sample_count: std::ops::Range<u32>,
340 cur_sample_delta: u32,
341 stts_iter: std::slice::Iter<'a, Sample>,
342 track_id: usize,
343 }
344
345 impl Iterator for TimeToSampleIterator<'_> {
346 type Item = u32;
347
348 #[allow(clippy::reversed_empty_ranges)]
349 fn next(&mut self) -> Option<u32> {
350 let has_sample = self.cur_sample_count.next().or_else(|| {
351 self.cur_sample_count = match self.stts_iter.next() {
352 Some(v) => {
353 self.cur_sample_delta = v.sample_delta;
354 0..v.sample_count
355 }
356 _ => 0..0,
357 };
358
359 self.cur_sample_count.next()
360 });
361
362 has_sample.and(Some(self.cur_sample_delta))
363 }
364 }
365
366 impl TimeToSampleIterator<'_> {
367 fn next_delta(&mut self) -> TrackScaledTime<i64> {
368 match self.next() {
369 Some(v) => TrackScaledTime::<i64>(i64::from(v), self.track_id),
370 _ => TrackScaledTime::<i64>(0, self.track_id),
371 }
372 }
373 }
374
375 // Convert 'stco' compact table to full table by iterator.
376 // (start_chunk_num, sample_number) => (start_chunk_num, sample_number),
377 // (start_chunk_num + 1, sample_number),
378 // (start_chunk_num + 2, sample_number),
379 // ...
380 // (next start_chunk_num, next sample_number),
381 // ...
382 //
383 // For example:
384 // (1, 5), (5, 10), (9, 2) => (1, 5), (2, 5), (3, 5), (4, 5), (5, 10), (6, 10),
385 // (7, 10), (8, 10), (9, 2)
386 fn sample_to_chunk_iter<'a>(
387 stsc_samples: &'a TryVec<SampleToChunk>,
388 stco_offsets: &'a TryVec<u64>,
389 ) -> SampleToChunkIterator<'a> {
390 SampleToChunkIterator {
391 chunks: (0..0),
392 sample_count: 0,
393 stsc_peek_iter: stsc_samples.as_slice().iter().peekable(),
394 remain_chunk_count: stco_offsets
395 .len()
396 .try_into()
397 .expect("stco.entry_count is u32"),
398 }
399 }
400
401 struct SampleToChunkIterator<'a> {
402 chunks: std::ops::Range<u32>,
403 sample_count: u32,
404 stsc_peek_iter: std::iter::Peekable<std::slice::Iter<'a, SampleToChunk>>,
405 remain_chunk_count: u32, // total chunk number from 'stco'.
406 }
407
408 impl Iterator for SampleToChunkIterator<'_> {
409 type Item = (u32, u32);
410
411 fn next(&mut self) -> Option<(u32, u32)> {
412 let has_chunk = self.chunks.next().or_else(|| {
413 self.chunks = self.locate();
414 self.remain_chunk_count
415 .checked_sub(
416 self.chunks
417 .len()
418 .try_into()
419 .expect("len() of a Range<u32> must fit in u32"),
420 )
421 .and_then(|res| {
422 self.remain_chunk_count = res;
423 self.chunks.next()
424 })
425 });
426
427 has_chunk.map(|id| (id, self.sample_count))
428 }
429 }
430
431 impl SampleToChunkIterator<'_> {
432 #[allow(clippy::reversed_empty_ranges)]
433 fn locate(&mut self) -> std::ops::Range<u32> {
434 loop {
435 return match (self.stsc_peek_iter.next(), self.stsc_peek_iter.peek()) {
436 (Some(next), Some(peek)) if next.first_chunk == peek.first_chunk => {
437 // Invalid entry, skip it and will continue searching at
438 // next loop iteration.
439 continue;
440 }
441 (Some(next), Some(peek)) if next.first_chunk > 0 && peek.first_chunk > 0 => {
442 self.sample_count = next.samples_per_chunk;
443 (next.first_chunk - 1)..(peek.first_chunk - 1)
444 }
445 (Some(next), None) if next.first_chunk > 0 => {
446 self.sample_count = next.samples_per_chunk;
447 // Total chunk number in 'stsc' could be different to 'stco',
448 // there could be more chunks at the last 'stsc' record.
449 match next.first_chunk.checked_add(self.remain_chunk_count) {
450 Some(r) => (next.first_chunk - 1)..r - 1,
451 _ => 0..0,
452 }
453 }
454 _ => 0..0,
455 };
456 }
457 }
458 }
459
460 /// Calculate numerator * scale / denominator, if possible.
461 ///
462 /// Applying the associativity of integer arithmetic, we divide first
463 /// and add the remainder after multiplying each term separately
464 /// to preserve precision while leaving more headroom. That is,
465 /// (n * s) / d is split into floor(n / d) * s + (n % d) * s / d.
466 ///
467 /// Return None on overflow or if the denominator is zero.
468 pub fn rational_scale<T, S>(numerator: T, denominator: T, scale2: S) -> Option<T>
469 where
470 T: PrimInt + Zero,
471 S: PrimInt,
472 {
473 if denominator.is_zero() {
474 return None;
475 }
476
477 let integer = numerator / denominator;
478 let remainder = numerator % denominator;
479 num_traits::cast(scale2).and_then(|s| match integer.checked_mul(&s) {
480 Some(integer) => remainder
481 .checked_mul(&s)
482 .and_then(|remainder| (remainder / denominator).checked_add(&integer)),
483 None => None,
484 })
485 }
486
487 #[derive(Debug, PartialEq, Eq)]
488 pub struct Microseconds<T>(pub T);
489
490 /// Convert `time` in media's global (mvhd) timescale to microseconds,
491 /// using provided `MediaTimeScale`
492 pub fn media_time_to_us(time: MediaScaledTime, scale: MediaTimeScale) -> Option<Microseconds<u64>> {
493 let microseconds_per_second = 1_000_000;
494 rational_scale(time.0, scale.0, microseconds_per_second).map(Microseconds)
495 }
496
497 /// Convert `time` in track's local (mdhd) timescale to microseconds,
498 /// using provided `TrackTimeScale<T>`
499 pub fn track_time_to_us<T>(
500 time: TrackScaledTime<T>,
501 scale: TrackTimeScale<T>,
502 ) -> Option<Microseconds<T>>
503 where
504 T: PrimInt + Zero,
505 {
506 assert_eq!(time.1, scale.1);
507 let microseconds_per_second = 1_000_000;
508 rational_scale(time.0, scale.0, microseconds_per_second).map(Microseconds)
509 }
510
511 #[test]
512 fn rational_scale_overflow() {
513 assert_eq!(rational_scale::<u64, u64>(17, 3, 1000), Some(5666));
514 let large = 0x4000_0000_0000_0000;
515 assert_eq!(rational_scale::<u64, u64>(large, 2, 2), Some(large));
516 assert_eq!(rational_scale::<u64, u64>(large, 4, 4), Some(large));
517 assert_eq!(rational_scale::<u64, u64>(large, 2, 8), None);
518 assert_eq!(rational_scale::<u64, u64>(large, 8, 4), Some(large / 2));
519 assert_eq!(rational_scale::<u64, u64>(large + 1, 4, 4), Some(large + 1));
520 assert_eq!(rational_scale::<u64, u64>(large, 40, 1000), None);
521 }
522
523 #[test]
524 fn media_time_overflow() {
525 let scale = MediaTimeScale(90000);
526 let duration = MediaScaledTime(9_007_199_254_710_000);
527 assert_eq!(
528 media_time_to_us(duration, scale),
529 Some(Microseconds(100_079_991_719_000_000u64))
530 );
531 }
532
533 #[test]
534 fn track_time_overflow() {
535 let scale = TrackTimeScale(44100u64, 0);
536 let duration = TrackScaledTime(4_413_527_634_807_900u64, 0);
537 assert_eq!(
538 track_time_to_us(duration, scale),
539 Some(Microseconds(100_079_991_719_000_000u64))
540 );
541 }