S3: one wrap_delta for the 33-bit clock difference (unwrap33, Clock::feed, PES dts-pts) with identical arithmetic
Browse files at this commite496c68ba6ee25573b5148d6b6458f644a276e41Parent: c50fa2b2 changed files +12 -22
crates/ec-mpegts/src/demux.rsmodified+9 -8
| @@ -66,18 +66,19 @@ enum Cc { | |||
| 66 | Gap, | 66 | Gap, |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | +const WRAP33: i64 = 1 << 33; | ||
| 70 | + | ||
| 71 | +/// Signed distance from `b` to `a` on the 33-bit clock, in [-2^32, 2^32). | ||
| 72 | +pub(crate) fn wrap_delta(a: i64, b: i64) -> i64 { | ||
| 73 | + let d = (a - b).rem_euclid(WRAP33); | ||
| 74 | + if d >= WRAP33 / 2 { d - WRAP33 } else { d } | ||
| 75 | +} | ||
| 76 | + | ||
| 69 | pub(crate) fn unwrap33(raw: u64, last: Option<i64>) -> i64 { | 77 | pub(crate) fn unwrap33(raw: u64, last: Option<i64>) -> i64 { |
| 70 | - const M: i64 = 1 << 33; | ||
| 71 | let raw = raw as i64; | 78 | let raw = raw as i64; |
| 72 | match last { | 79 | match last { |
| 73 | None => raw, | 80 | None => raw, |
| 74 | - Some(l) => { | 81 | + Some(l) => l + wrap_delta(raw, l), |
| 75 | - let mut d = (raw - l).rem_euclid(M); | ||
| 76 | - if d >= M / 2 { | ||
| 77 | - d -= M; | ||
| 78 | - } | ||
| 79 | - l + d | ||
| 80 | - } | ||
| 81 | } | 82 | } |
| 82 | } | 83 | } |
| 83 | 84 | ||
crates/ec-mpegts/src/ps.rsmodified+3 -14
| @@ -21,7 +21,7 @@ use ec_core::registry::{Demuxer, SeekMode, StreamInfo}; | |||
| 21 | use ec_core::{Error, Packet, Result, Rounding, TimeBase, Timestamp}; | 21 | use ec_core::{Error, Packet, Result, Rounding, TimeBase, Timestamp}; |
| 22 | 22 | ||
| 23 | use crate::codec::StreamKind; | 23 | use crate::codec::StreamKind; |
| 24 | -use crate::demux::{TrackInfo, VideoFraming, parse_pes, read_ts, unwrap33}; | 24 | +use crate::demux::{TrackInfo, VideoFraming, parse_pes, read_ts, unwrap33, wrap_delta}; |
| 25 | use crate::psi::{EsEntry, descriptors}; | 25 | use crate::psi::{EsEntry, descriptors}; |
| 26 | use crate::track::{self, PesIn, Queued, TICKS, Track}; | 26 | use crate::track::{self, PesIn, Queued, TICKS, Track}; |
| 27 | use crate::window::Window; | 27 | use crate::window::Window; |
| @@ -180,15 +180,11 @@ struct Clock { | |||
| 180 | impl Clock { | 180 | impl Clock { |
| 181 | /// Feeds a pack's SCR. `true` when it stepped back far enough to start a new epoch. | 181 | /// Feeds a pack's SCR. `true` when it stepped back far enough to start a new epoch. |
| 182 | fn feed(&mut self, raw: u64) -> bool { | 182 | fn feed(&mut self, raw: u64) -> bool { |
| 183 | - const M: i64 = 1 << 33; | ||
| 184 | let Some(last) = self.last_raw.replace(raw) else { | 183 | let Some(last) = self.last_raw.replace(raw) else { |
| 185 | self.cur_u = unwrap33(raw, Some(self.cur_u)); | 184 | self.cur_u = unwrap33(raw, Some(self.cur_u)); |
| 186 | return false; | 185 | return false; |
| 187 | }; | 186 | }; |
| 188 | - let mut d = (raw as i64 - last as i64).rem_euclid(M); | 187 | + let d = wrap_delta(raw as i64, last as i64); |
| 189 | - if d >= M / 2 { | ||
| 190 | - d -= M; | ||
| 191 | - } | ||
| 192 | if !(-BACK..=FWD).contains(&d) { | 188 | if !(-BACK..=FWD).contains(&d) { |
| 193 | let last_t = self.cur_u + self.shift; | 189 | let last_t = self.cur_u + self.shift; |
| 194 | self.cur_u = raw as i64; | 190 | self.cur_u = raw as i64; |
| @@ -578,17 +574,10 @@ impl<R: Read + Seek> PsDemuxer<R> { | |||
| 578 | 574 | ||
| 579 | /// Timestamps of a PES header on the presented timeline. | 575 | /// Timestamps of a PES header on the presented timeline. |
| 580 | fn stamp(&self, pts: Option<u64>, dts: Option<u64>) -> (Option<i64>, Option<i64>) { | 576 | fn stamp(&self, pts: Option<u64>, dts: Option<u64>) -> (Option<i64>, Option<i64>) { |
| 581 | - const M: i64 = 1 << 33; | ||
| 582 | let t = |raw: u64| unwrap33(raw, Some(self.clock.cur_u)) + self.clock.shift; | 577 | let t = |raw: u64| unwrap33(raw, Some(self.clock.cur_u)) + self.clock.shift; |
| 583 | let p = pts.map(t); | 578 | let p = pts.map(t); |
| 584 | let d = match (dts, pts, p) { | 579 | let d = match (dts, pts, p) { |
| 585 | - (Some(d), Some(rp), Some(p)) => { | 580 | + (Some(d), Some(rp), Some(p)) => Some(p + wrap_delta(d as i64, rp as i64)), |
| 586 | - let mut delta = (d as i64 - rp as i64).rem_euclid(M); | ||
| 587 | - if delta >= M / 2 { | ||
| 588 | - delta -= M; | ||
| 589 | - } | ||
| 590 | - Some(p + delta) | ||
| 591 | - } | ||
| 592 | _ => None, | 581 | _ => None, |
| 593 | }; | 582 | }; |
| 594 | (p, d) | 583 | (p, d) |