diff --git a/crates/ec-mpegts/src/ps.rs b/crates/ec-mpegts/src/ps.rs index 3f24f283..8c71fc6f 100644 --- a/crates/ec-mpegts/src/ps.rs +++ b/crates/ec-mpegts/src/ps.rs @@ -26,6 +26,14 @@ use crate::psi::{EsEntry, descriptors}; use crate::track::{self, PesIn, Queued, TICKS, Track}; use crate::window::Window; +mod clock; +mod epochs; +mod es_index; + +use clock::Clock; +use epochs::Epoch; +use es_index::{EsIndex, Pic}; + const MAX_TRACKS: usize = 128; const PROBE_BYTES: u64 = 32 << 20; /// Files up to this size are walked completely at open to find SCR discontinuities. @@ -168,66 +176,6 @@ fn es_start(head: &[u8]) -> Option { /// A PSM entry: stream type and its descriptors (tag, body). type PsmEntry = (u8, Vec<(u8, Vec)>); -/// The pack clock unwrapped into a continuous `u` (33-bit wraps removed) and the shift -/// that keeps the presented timeline monotonic over SCR resets: `T = u + shift`. -#[derive(Debug, Clone, Copy)] -struct Clock { - last_raw: Option, - cur_u: i64, - shift: i64, -} - -impl Clock { - /// Feeds a pack's SCR. `true` when it stepped back far enough to start a new epoch. - fn feed(&mut self, raw: u64) -> bool { - let Some(last) = self.last_raw.replace(raw) else { - self.cur_u = unwrap33(raw, Some(self.cur_u)); - return false; - }; - let d = wrap_delta(raw as i64, last as i64); - if !(-BACK..=FWD).contains(&d) { - let last_t = self.cur_u + self.shift; - self.cur_u = raw as i64; - self.shift = last_t - self.cur_u; - true - } else { - self.cur_u += d; - false - } - } -} - -/// A stretch of the file with one SCR timeline. -#[derive(Debug, Clone, Copy)] -struct Epoch { - off: u64, - shift: i64, - /// `u` of the first pack. - base: i64, -} - -// --------------------------------------------------------------------------- -// Raw video elementary streams: the picture index -// --------------------------------------------------------------------------- - -struct Pic { - off: u64, - key: bool, - pts: i64, - dts: i64, -} - -/// Every picture of a raw stream with timestamps made from the frame rate: pictures are -/// grouped (a GOP or sequence header, or an anchor whose temporal reference does not -/// advance, starts a group), the display index is the group's frame count so far plus -/// the temporal reference, `pts` is that index and `dts` the decode index less the -/// reordering depth, both in frame durations. -struct EsIndex { - pics: Vec, - start: i64, - end: i64, -} - // --------------------------------------------------------------------------- // The demuxer // --------------------------------------------------------------------------- diff --git a/crates/ec-mpegts/src/ps/clock.rs b/crates/ec-mpegts/src/ps/clock.rs new file mode 100644 index 00000000..16dac7bd --- /dev/null +++ b/crates/ec-mpegts/src/ps/clock.rs @@ -0,0 +1,31 @@ +use super::{BACK, FWD}; +use crate::demux::{unwrap33, wrap_delta}; + +/// The pack clock unwrapped into a continuous `u` (33-bit wraps removed) and the shift +/// that keeps the presented timeline monotonic over SCR resets: `T = u + shift`. +#[derive(Debug, Clone, Copy)] +pub(super) struct Clock { + pub(super) last_raw: Option, + pub(super) cur_u: i64, + pub(super) shift: i64, +} + +impl Clock { + /// Feeds a pack's SCR. `true` when it stepped back far enough to start a new epoch. + pub(super) fn feed(&mut self, raw: u64) -> bool { + let Some(last) = self.last_raw.replace(raw) else { + self.cur_u = unwrap33(raw, Some(self.cur_u)); + return false; + }; + let d = wrap_delta(raw as i64, last as i64); + if !(-BACK..=FWD).contains(&d) { + let last_t = self.cur_u + self.shift; + self.cur_u = raw as i64; + self.shift = last_t - self.cur_u; + true + } else { + self.cur_u += d; + false + } + } +} diff --git a/crates/ec-mpegts/src/ps/epochs.rs b/crates/ec-mpegts/src/ps/epochs.rs new file mode 100644 index 00000000..b2aaad87 --- /dev/null +++ b/crates/ec-mpegts/src/ps/epochs.rs @@ -0,0 +1,8 @@ +/// A stretch of the file with one SCR timeline. +#[derive(Debug, Clone, Copy)] +pub(super) struct Epoch { + pub(super) off: u64, + pub(super) shift: i64, + /// `u` of the first pack. + pub(super) base: i64, +} diff --git a/crates/ec-mpegts/src/ps/es_index.rs b/crates/ec-mpegts/src/ps/es_index.rs new file mode 100644 index 00000000..2aa7fd52 --- /dev/null +++ b/crates/ec-mpegts/src/ps/es_index.rs @@ -0,0 +1,17 @@ +pub(super) struct Pic { + pub(super) off: u64, + pub(super) key: bool, + pub(super) pts: i64, + pub(super) dts: i64, +} + +/// Every picture of a raw stream with timestamps made from the frame rate: pictures are +/// grouped (a GOP or sequence header, or an anchor whose temporal reference does not +/// advance, starts a group), the display index is the group's frame count so far plus +/// the temporal reference, `pts` is that index and `dts` the decode index less the +/// reordering depth, both in frame durations. +pub(super) struct EsIndex { + pub(super) pics: Vec, + pub(super) start: i64, + pub(super) end: i64, +}