forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'simplify/s3' into simplify/w2

Browse files at this commit

eaa1bec424db078b446f429088260b05186a67e5Ahmet Kaan GümüşParent: b0452d83698754
crates/ec-mpegts/src/demux.rsmodified+17 -68
@@ -4,11 +4,11 @@ use std::collections::{HashMap, VecDeque};
44 use std::io::{Read, Seek};
55
66 use ec_core::registry::{Demuxer, SeekMode, StreamInfo};
7-use ec_core::{Error, Packet, PacketFlags, Result, Rounding, TimeBase, Timestamp};
7+use ec_core::{Error, Packet, Result, Rounding, TimeBase, Timestamp};
88
9-use crate::codec::{StreamKind, annexb_to_length_prefixed};
9+use crate::codec::StreamKind;
1010 use crate::psi::{self, EsEntry, PatEntry, Pmt, SectionBuf};
11-use crate::track::{MAX_PES, PesIn, Queued, TICKS, Track};
11+use crate::track::{self, MAX_PES, PesIn, Queued, TICKS, Track};
1212 use crate::ts::{self, Layout, SYNC, TS_LEN};
1313 use crate::window::Window;
1414
@@ -66,18 +66,18 @@ enum Cc {
6666 Gap,
6767 }
6868
69+const WRAP33: i64 = 1 << 33;
70+
71+pub(crate) fn wrap_delta(a: i64, b: i64) -> i64 {
72+ let d = (a - b).rem_euclid(WRAP33);
73+ if d >= WRAP33 / 2 { d - WRAP33 } else { d }
74+}
75+
6976 pub(crate) fn unwrap33(raw: u64, last: Option<i64>) -> i64 {
70- const M: i64 = 1 << 33;
7177 let raw = raw as i64;
7278 match last {
7379 None => raw,
74- Some(l) => {
75- let mut d = (raw - l).rem_euclid(M);
76- if d >= M / 2 {
77- d -= M;
78- }
79- l + d
80- }
80+ Some(l) => l + wrap_delta(raw, l),
8181 }
8282 }
8383
@@ -698,7 +698,7 @@ impl<R: Read + Seek> TsDemuxer<R> {
698698 for t in &mut self.tracks {
699699 t.start_time = t.min_pts;
700700 }
701- self.assign_streams();
701+ track::assign_streams(&mut self.tracks, &mut self.streams);
702702 self.frozen = true;
703703 let tail = self.scan_tail();
704704 self.frozen = false;
@@ -707,31 +707,6 @@ impl<R: Read + Seek> TsDemuxer<R> {
707707 Ok(())
708708 }
709709
710- fn assign_streams(&mut self) {
711- for ti in 0..self.tracks.len() {
712- self.make_stream(ti);
713- }
714- }
715-
716- /// Gives track `ti` a stream index when it can be described. Returns it.
717- fn make_stream(&mut self, ti: usize) -> Option<u32> {
718- if let Some(s) = self.tracks[ti].stream {
719- return Some(s);
720- }
721- let index = self.streams.len() as u32;
722- let mut info = self.tracks[ti].stream_info(index)?;
723- if !self
724- .streams
725- .iter()
726- .any(|s| s.params.codec.media_type() == info.params.codec.media_type() && s.default)
727- {
728- info.default = true;
729- }
730- self.tracks[ti].stream = Some(index);
731- self.streams.push(info);
732- Some(index)
733- }
734-
735710 fn scan_tail(&mut self) -> Result<()> {
736711 let size = self.layout.size as u64;
737712 let mut win: u64 = 4 << 20;
@@ -796,36 +771,6 @@ impl<R: Read + Seek> TsDemuxer<R> {
796771 }
797772 }
798773
799- fn make_packet(&mut self, q: Queued) -> Option<Packet> {
800- let ti = q.track;
801- let stream = match self.tracks[ti].stream {
802- Some(s) => s,
803- None => {
804- if self.tracks[ti].start_time.is_none() {
805- self.tracks[ti].start_time = self.tracks[ti].min_pts;
806- }
807- self.make_stream(ti)?
808- }
809- };
810- let kind = &self.tracks[ti].kind;
811- let data = if self.framing == VideoFraming::LengthPrefixed && kind.is_annexb_video() {
812- annexb_to_length_prefixed(&q.data, 4)
813- } else {
814- q.data
815- };
816- let mut p = Packet::new(stream, self.time_base(), data);
817- p.pts = q.pts;
818- p.dts = q.dts;
819- p.duration = q.dur;
820- p.flags = PacketFlags {
821- keyframe: q.key,
822- corrupt: q.corrupt,
823- ..PacketFlags::default()
824- };
825- self.last_off = q.off;
826- Some(p)
827- }
828-
829774 // ----- seeking ----------------------------------------------------------------
830775
831776 fn align(&self, off: u64) -> u64 {
@@ -906,7 +851,11 @@ impl<R: Read + Seek + Send> Demuxer for TsDemuxer<R> {
906851 let Some(q) = self.pump()? else {
907852 return Err(Error::Eof);
908853 };
909- if let Some(p) = self.make_packet(q) {
854+ let off = q.off;
855+ if let Some(p) =
856+ track::make_packet(&mut self.tracks, &mut self.streams, self.framing, q)
857+ {
858+ self.last_off = off;
910859 return Ok(p);
911860 }
912861 }
crates/ec-mpegts/src/ps.rsmodified+20 -133
@@ -18,14 +18,22 @@ use std::collections::{HashMap, VecDeque};
1818 use std::io::{Read, Seek};
1919
2020 use ec_core::registry::{Demuxer, SeekMode, StreamInfo};
21-use ec_core::{Error, Packet, PacketFlags, Result, Rounding, TimeBase, Timestamp};
21+use ec_core::{Error, Packet, Result, Rounding, TimeBase, Timestamp};
2222
23-use crate::codec::{StreamKind, annexb_to_length_prefixed};
24-use crate::demux::{TrackInfo, VideoFraming, parse_pes, read_ts, unwrap33};
23+use crate::codec::StreamKind;
24+use crate::demux::{TrackInfo, VideoFraming, parse_pes, read_ts, unwrap33, wrap_delta};
2525 use crate::psi::{EsEntry, descriptors};
26-use crate::track::{PesIn, Queued, TICKS, Track};
26+use crate::track::{self, PesIn, Queued, TICKS, Track};
2727 use crate::window::Window;
2828
29+mod clock;
30+mod epochs;
31+mod es_index;
32+
33+use clock::Clock;
34+use epochs::Epoch;
35+use es_index::{EsIndex, Pic};
36+
2937 const MAX_TRACKS: usize = 128;
3038 const PROBE_BYTES: u64 = 32 << 20;
3139 /// Files up to this size are walked completely at open to find SCR discontinuities.
@@ -168,70 +176,6 @@ fn es_start(head: &[u8]) -> Option<usize> {
168176 /// A PSM entry: stream type and its descriptors (tag, body).
169177 type PsmEntry = (u8, Vec<(u8, Vec<u8>)>);
170178
171-/// The pack clock unwrapped into a continuous `u` (33-bit wraps removed) and the shift
172-/// that keeps the presented timeline monotonic over SCR resets: `T = u + shift`.
173-#[derive(Debug, Clone, Copy)]
174-struct Clock {
175- last_raw: Option<u64>,
176- cur_u: i64,
177- shift: i64,
178-}
179-
180-impl Clock {
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 {
183- const M: i64 = 1 << 33;
184- let Some(last) = self.last_raw.replace(raw) else {
185- self.cur_u = unwrap33(raw, Some(self.cur_u));
186- return false;
187- };
188- let mut d = (raw as i64 - last as i64).rem_euclid(M);
189- if d >= M / 2 {
190- d -= M;
191- }
192- if !(-BACK..=FWD).contains(&d) {
193- let last_t = self.cur_u + self.shift;
194- self.cur_u = raw as i64;
195- self.shift = last_t - self.cur_u;
196- true
197- } else {
198- self.cur_u += d;
199- false
200- }
201- }
202-}
203-
204-/// A stretch of the file with one SCR timeline.
205-#[derive(Debug, Clone, Copy)]
206-struct Epoch {
207- off: u64,
208- shift: i64,
209- /// `u` of the first pack.
210- base: i64,
211-}
212-
213-// ---------------------------------------------------------------------------
214-// Raw video elementary streams: the picture index
215-// ---------------------------------------------------------------------------
216-
217-struct Pic {
218- off: u64,
219- key: bool,
220- pts: i64,
221- dts: i64,
222-}
223-
224-/// Every picture of a raw stream with timestamps made from the frame rate: pictures are
225-/// grouped (a GOP or sequence header, or an anchor whose temporal reference does not
226-/// advance, starts a group), the display index is the group's frame count so far plus
227-/// the temporal reference, `pts` is that index and `dts` the decode index less the
228-/// reordering depth, both in frame durations.
229-struct EsIndex {
230- pics: Vec<Pic>,
231- start: i64,
232- end: i64,
233-}
234-
235179 // ---------------------------------------------------------------------------
236180 // The demuxer
237181 // ---------------------------------------------------------------------------
@@ -578,17 +522,10 @@ impl<R: Read + Seek> PsDemuxer<R> {
578522
579523 /// Timestamps of a PES header on the presented timeline.
580524 fn stamp(&self, pts: Option<u64>, dts: Option<u64>) -> (Option<i64>, Option<i64>) {
581- const M: i64 = 1 << 33;
582525 let t = |raw: u64| unwrap33(raw, Some(self.clock.cur_u)) + self.clock.shift;
583526 let p = pts.map(t);
584527 let d = match (dts, pts, p) {
585- (Some(d), Some(rp), Some(p)) => {
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- }
528+ (Some(d), Some(rp), Some(p)) => Some(p + wrap_delta(d as i64, rp as i64)),
592529 _ => None,
593530 };
594531 (p, d)
@@ -959,7 +896,7 @@ impl<R: Read + Seek> PsDemuxer<R> {
959896 for t in &mut self.tracks {
960897 t.start_time = t.min_pts;
961898 }
962- self.assign_streams();
899+ track::assign_streams(&mut self.tracks, &mut self.streams);
963900 self.frozen = true;
964901 let tail = self.scan_tail();
965902 self.frozen = false;
@@ -968,30 +905,6 @@ impl<R: Read + Seek> PsDemuxer<R> {
968905 Ok(())
969906 }
970907
971- fn assign_streams(&mut self) {
972- for ti in 0..self.tracks.len() {
973- self.make_stream(ti);
974- }
975- }
976-
977- fn make_stream(&mut self, ti: usize) -> Option<u32> {
978- if let Some(s) = self.tracks[ti].stream {
979- return Some(s);
980- }
981- let index = self.streams.len() as u32;
982- let mut info = self.tracks[ti].stream_info(index)?;
983- if !self
984- .streams
985- .iter()
986- .any(|s| s.params.codec.media_type() == info.params.codec.media_type() && s.default)
987- {
988- info.default = true;
989- }
990- self.tracks[ti].stream = Some(index);
991- self.streams.push(info);
992- Some(index)
993- }
994-
995908 fn scan_tail(&mut self) -> Result<()> {
996909 let mut win: u64 = 4 << 20;
997910 loop {
@@ -1078,36 +991,6 @@ impl<R: Read + Seek> PsDemuxer<R> {
1078991 }
1079992 }
1080993
1081- fn make_packet(&mut self, q: Queued) -> Option<Packet> {
1082- let ti = q.track;
1083- let stream = match self.tracks[ti].stream {
1084- Some(s) => s,
1085- None => {
1086- if self.tracks[ti].start_time.is_none() {
1087- self.tracks[ti].start_time = self.tracks[ti].min_pts;
1088- }
1089- self.make_stream(ti)?
1090- }
1091- };
1092- let kind = &self.tracks[ti].kind;
1093- let data = if self.framing == VideoFraming::LengthPrefixed && kind.is_annexb_video() {
1094- annexb_to_length_prefixed(&q.data, 4)
1095- } else {
1096- q.data
1097- };
1098- let mut p = Packet::new(stream, self.time_base(), data);
1099- p.pts = q.pts;
1100- p.dts = q.dts;
1101- p.duration = q.dur;
1102- p.flags = PacketFlags {
1103- keyframe: q.key,
1104- corrupt: q.corrupt,
1105- ..PacketFlags::default()
1106- };
1107- self.last_off = q.off;
1108- Some(p)
1109- }
1110-
1111994 // ----- seeking ------------------------------------------------------------------------------
1112995
1113996 fn time_at(&mut self, off: u64, ti: usize) -> Result<Option<i64>> {
@@ -1291,7 +1174,7 @@ impl<R: Read + Seek> PsDemuxer<R> {
12911174 self.tracks[0].start_time = Some(ix.start);
12921175 self.duration = Some(ix.end - ix.start);
12931176 self.es = Some(ix);
1294- self.assign_streams();
1177+ track::assign_streams(&mut self.tracks, &mut self.streams);
12951178 if let Some(s) = self.streams.first_mut() {
12961179 s.duration = self.duration;
12971180 }
@@ -1533,7 +1416,11 @@ impl<R: Read + Seek + Send> Demuxer for PsDemuxer<R> {
15331416 let Some(q) = self.pump()? else {
15341417 return Err(Error::Eof);
15351418 };
1536- if let Some(p) = self.make_packet(q) {
1419+ let off = q.off;
1420+ if let Some(p) =
1421+ track::make_packet(&mut self.tracks, &mut self.streams, self.framing, q)
1422+ {
1423+ self.last_off = off;
15371424 return Ok(p);
15381425 }
15391426 }
crates/ec-mpegts/src/ps/clock.rsadded+31 -0
@@ -0,0 +1,31 @@
1+use super::{BACK, FWD};
2+use crate::demux::{unwrap33, wrap_delta};
3+
4+/// The pack clock unwrapped into a continuous `u` (33-bit wraps removed) and the shift
5+/// that keeps the presented timeline monotonic over SCR resets: `T = u + shift`.
6+#[derive(Debug, Clone, Copy)]
7+pub(super) struct Clock {
8+ pub(super) last_raw: Option<u64>,
9+ pub(super) cur_u: i64,
10+ pub(super) shift: i64,
11+}
12+
13+impl Clock {
14+ /// Feeds a pack's SCR. `true` when it stepped back far enough to start a new epoch.
15+ pub(super) fn feed(&mut self, raw: u64) -> bool {
16+ let Some(last) = self.last_raw.replace(raw) else {
17+ self.cur_u = unwrap33(raw, Some(self.cur_u));
18+ return false;
19+ };
20+ let d = wrap_delta(raw as i64, last as i64);
21+ if !(-BACK..=FWD).contains(&d) {
22+ let last_t = self.cur_u + self.shift;
23+ self.cur_u = raw as i64;
24+ self.shift = last_t - self.cur_u;
25+ true
26+ } else {
27+ self.cur_u += d;
28+ false
29+ }
30+ }
31+}
crates/ec-mpegts/src/ps/epochs.rsadded+8 -0
@@ -0,0 +1,8 @@
1+/// A stretch of the file with one SCR timeline.
2+#[derive(Debug, Clone, Copy)]
3+pub(super) struct Epoch {
4+ pub(super) off: u64,
5+ pub(super) shift: i64,
6+ /// `u` of the first pack.
7+ pub(super) base: i64,
8+}
crates/ec-mpegts/src/ps/es_index.rsadded+17 -0
@@ -0,0 +1,17 @@
1+pub(super) struct Pic {
2+ pub(super) off: u64,
3+ pub(super) key: bool,
4+ pub(super) pts: i64,
5+ pub(super) dts: i64,
6+}
7+
8+/// Every picture of a raw stream with timestamps made from the frame rate: pictures are
9+/// grouped (a GOP or sequence header, or an anchor whose temporal reference does not
10+/// advance, starts a group), the display index is the group's frame count so far plus
11+/// the temporal reference, `pts` is that index and `dts` the decode index less the
12+/// reordering depth, both in frame durations.
13+pub(super) struct EsIndex {
14+ pub(super) pics: Vec<Pic>,
15+ pub(super) start: i64,
16+ pub(super) end: i64,
17+}
crates/ec-mpegts/src/track.rsmodified+67 -4
@@ -4,14 +4,15 @@
44 use std::collections::VecDeque;
55
66 use ec_core::{
7- AudioParameters, ChannelLayout, CodecId, CodecParameters, MediaParameters, PixelFormat,
8- SampleFormat, StreamInfo, TimeBase, registry::VideoParameters,
7+ AudioParameters, ChannelLayout, CodecId, CodecParameters, MediaParameters, Packet, PacketFlags,
8+ PixelFormat, SampleFormat, StreamInfo, TimeBase, registry::VideoParameters,
99 };
1010
1111 use crate::codec::{
12- self, FrameHdr, Framing, LpcmHeader, StreamKind, build_avcc, build_hvcc, frame_header,
13- header_lookahead, scan_au,
12+ self, FrameHdr, Framing, LpcmHeader, StreamKind, annexb_to_length_prefixed, build_avcc,
13+ build_hvcc, frame_header, header_lookahead, scan_au,
1414 };
15+use crate::demux::VideoFraming;
1516 use crate::psi::EsEntry;
1617 use ec_truehd::{AccessUnitHeader, frame_length};
1718
@@ -972,3 +973,65 @@ fn pixel_format(chroma: u8, depth: u8) -> Option<PixelFormat> {
972973 _ => None,
973974 }
974975 }
976+
977+pub(crate) fn assign_streams(tracks: &mut [Track], streams: &mut Vec<StreamInfo>) {
978+ for ti in 0..tracks.len() {
979+ make_stream(tracks, streams, ti);
980+ }
981+}
982+
983+/// Gives track `ti` a stream index when it can be described. Returns it.
984+pub(crate) fn make_stream(
985+ tracks: &mut [Track],
986+ streams: &mut Vec<StreamInfo>,
987+ ti: usize,
988+) -> Option<u32> {
989+ if let Some(s) = tracks[ti].stream {
990+ return Some(s);
991+ }
992+ let index = streams.len() as u32;
993+ let mut info = tracks[ti].stream_info(index)?;
994+ if !streams
995+ .iter()
996+ .any(|s| s.params.codec.media_type() == info.params.codec.media_type() && s.default)
997+ {
998+ info.default = true;
999+ }
1000+ tracks[ti].stream = Some(index);
1001+ streams.push(info);
1002+ Some(index)
1003+}
1004+
1005+pub(crate) fn make_packet(
1006+ tracks: &mut [Track],
1007+ streams: &mut Vec<StreamInfo>,
1008+ framing: VideoFraming,
1009+ q: Queued,
1010+) -> Option<Packet> {
1011+ let ti = q.track;
1012+ let stream = match tracks[ti].stream {
1013+ Some(s) => s,
1014+ None => {
1015+ if tracks[ti].start_time.is_none() {
1016+ tracks[ti].start_time = tracks[ti].min_pts;
1017+ }
1018+ make_stream(tracks, streams, ti)?
1019+ }
1020+ };
1021+ let kind = &tracks[ti].kind;
1022+ let data = if framing == VideoFraming::LengthPrefixed && kind.is_annexb_video() {
1023+ annexb_to_length_prefixed(&q.data, 4)
1024+ } else {
1025+ q.data
1026+ };
1027+ let mut p = Packet::new(stream, TimeBase::new(1, TICKS), data);
1028+ p.pts = q.pts;
1029+ p.dts = q.dts;
1030+ p.duration = q.dur;
1031+ p.flags = PacketFlags {
1032+ keyframe: q.key,
1033+ corrupt: q.corrupt,
1034+ ..PacketFlags::default()
1035+ };
1036+ Some(p)
1037+}