Merge branch 'simplify/s3' into simplify/w2
Browse files at this commit6 changed files +160 -205
- crates/ec-mpegts/src/demux.rs +17 -68
- crates/ec-mpegts/src/ps.rs +20 -133
- crates/ec-mpegts/src/ps/clock.rs +31 -0
- crates/ec-mpegts/src/ps/epochs.rs +8 -0
- crates/ec-mpegts/src/ps/es_index.rs +17 -0
- crates/ec-mpegts/src/track.rs +67 -4
crates/ec-mpegts/src/demux.rsmodified+17 -68
| @@ -4,11 +4,11 @@ use std::collections::{HashMap, VecDeque}; | |||
| 4 | use std::io::{Read, Seek}; | 4 | use std::io::{Read, Seek}; |
| 5 | 5 | ||
| 6 | use ec_core::registry::{Demuxer, SeekMode, StreamInfo}; | 6 | 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}; |
| 8 | 8 | ||
| 9 | -use crate::codec::{StreamKind, annexb_to_length_prefixed}; | 9 | +use crate::codec::StreamKind; |
| 10 | use crate::psi::{self, EsEntry, PatEntry, Pmt, SectionBuf}; | 10 | 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}; |
| 12 | use crate::ts::{self, Layout, SYNC, TS_LEN}; | 12 | use crate::ts::{self, Layout, SYNC, TS_LEN}; |
| 13 | use crate::window::Window; | 13 | use crate::window::Window; |
| 14 | 14 | ||
| @@ -66,18 +66,18 @@ enum Cc { | |||
| 66 | Gap, | 66 | Gap, |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 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 | + | ||
| 69 | pub(crate) fn unwrap33(raw: u64, last: Option<i64>) -> i64 { | 76 | pub(crate) fn unwrap33(raw: u64, last: Option<i64>) -> i64 { |
| 70 | - const M: i64 = 1 << 33; | ||
| 71 | let raw = raw as i64; | 77 | let raw = raw as i64; |
| 72 | match last { | 78 | match last { |
| 73 | None => raw, | 79 | None => raw, |
| 74 | - Some(l) => { | 80 | + 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 | } | 81 | } |
| 82 | } | 82 | } |
| 83 | 83 | ||
| @@ -698,7 +698,7 @@ impl<R: Read + Seek> TsDemuxer<R> { | |||
| 698 | for t in &mut self.tracks { | 698 | for t in &mut self.tracks { |
| 699 | t.start_time = t.min_pts; | 699 | t.start_time = t.min_pts; |
| 700 | } | 700 | } |
| 701 | - self.assign_streams(); | 701 | + track::assign_streams(&mut self.tracks, &mut self.streams); |
| 702 | self.frozen = true; | 702 | self.frozen = true; |
| 703 | let tail = self.scan_tail(); | 703 | let tail = self.scan_tail(); |
| 704 | self.frozen = false; | 704 | self.frozen = false; |
| @@ -707,31 +707,6 @@ impl<R: Read + Seek> TsDemuxer<R> { | |||
| 707 | Ok(()) | 707 | Ok(()) |
| 708 | } | 708 | } |
| 709 | 709 | ||
| 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 | - | ||
| 735 | fn scan_tail(&mut self) -> Result<()> { | 710 | fn scan_tail(&mut self) -> Result<()> { |
| 736 | let size = self.layout.size as u64; | 711 | let size = self.layout.size as u64; |
| 737 | let mut win: u64 = 4 << 20; | 712 | let mut win: u64 = 4 << 20; |
| @@ -796,36 +771,6 @@ impl<R: Read + Seek> TsDemuxer<R> { | |||
| 796 | } | 771 | } |
| 797 | } | 772 | } |
| 798 | 773 | ||
| 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 | - | ||
| 829 | // ----- seeking ---------------------------------------------------------------- | 774 | // ----- seeking ---------------------------------------------------------------- |
| 830 | 775 | ||
| 831 | fn align(&self, off: u64) -> u64 { | 776 | fn align(&self, off: u64) -> u64 { |
| @@ -906,7 +851,11 @@ impl<R: Read + Seek + Send> Demuxer for TsDemuxer<R> { | |||
| 906 | let Some(q) = self.pump()? else { | 851 | let Some(q) = self.pump()? else { |
| 907 | return Err(Error::Eof); | 852 | return Err(Error::Eof); |
| 908 | }; | 853 | }; |
| 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; | ||
| 910 | return Ok(p); | 859 | return Ok(p); |
| 911 | } | 860 | } |
| 912 | } | 861 | } |
crates/ec-mpegts/src/ps.rsmodified+20 -133
| @@ -18,14 +18,22 @@ use std::collections::{HashMap, VecDeque}; | |||
| 18 | use std::io::{Read, Seek}; | 18 | use std::io::{Read, Seek}; |
| 19 | 19 | ||
| 20 | use ec_core::registry::{Demuxer, SeekMode, StreamInfo}; | 20 | 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}; |
| 22 | 22 | ||
| 23 | -use crate::codec::{StreamKind, annexb_to_length_prefixed}; | 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::{PesIn, Queued, TICKS, Track}; | 26 | +use crate::track::{self, PesIn, Queued, TICKS, Track}; |
| 27 | use crate::window::Window; | 27 | use crate::window::Window; |
| 28 | 28 | ||
| 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 | + | ||
| 29 | const MAX_TRACKS: usize = 128; | 37 | const MAX_TRACKS: usize = 128; |
| 30 | const PROBE_BYTES: u64 = 32 << 20; | 38 | const PROBE_BYTES: u64 = 32 << 20; |
| 31 | /// Files up to this size are walked completely at open to find SCR discontinuities. | 39 | /// 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> { | |||
| 168 | /// A PSM entry: stream type and its descriptors (tag, body). | 176 | /// A PSM entry: stream type and its descriptors (tag, body). |
| 169 | type PsmEntry = (u8, Vec<(u8, Vec<u8>)>); | 177 | type PsmEntry = (u8, Vec<(u8, Vec<u8>)>); |
| 170 | 178 | ||
| 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 | - | ||
| 235 | // --------------------------------------------------------------------------- | 179 | // --------------------------------------------------------------------------- |
| 236 | // The demuxer | 180 | // The demuxer |
| 237 | // --------------------------------------------------------------------------- | 181 | // --------------------------------------------------------------------------- |
| @@ -578,17 +522,10 @@ impl<R: Read + Seek> PsDemuxer<R> { | |||
| 578 | 522 | ||
| 579 | /// Timestamps of a PES header on the presented timeline. | 523 | /// Timestamps of a PES header on the presented timeline. |
| 580 | fn stamp(&self, pts: Option<u64>, dts: Option<u64>) -> (Option<i64>, Option<i64>) { | 524 | 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; | 525 | let t = |raw: u64| unwrap33(raw, Some(self.clock.cur_u)) + self.clock.shift; |
| 583 | let p = pts.map(t); | 526 | let p = pts.map(t); |
| 584 | let d = match (dts, pts, p) { | 527 | let d = match (dts, pts, p) { |
| 585 | - (Some(d), Some(rp), Some(p)) => { | 528 | + (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, | 529 | _ => None, |
| 593 | }; | 530 | }; |
| 594 | (p, d) | 531 | (p, d) |
| @@ -959,7 +896,7 @@ impl<R: Read + Seek> PsDemuxer<R> { | |||
| 959 | for t in &mut self.tracks { | 896 | for t in &mut self.tracks { |
| 960 | t.start_time = t.min_pts; | 897 | t.start_time = t.min_pts; |
| 961 | } | 898 | } |
| 962 | - self.assign_streams(); | 899 | + track::assign_streams(&mut self.tracks, &mut self.streams); |
| 963 | self.frozen = true; | 900 | self.frozen = true; |
| 964 | let tail = self.scan_tail(); | 901 | let tail = self.scan_tail(); |
| 965 | self.frozen = false; | 902 | self.frozen = false; |
| @@ -968,30 +905,6 @@ impl<R: Read + Seek> PsDemuxer<R> { | |||
| 968 | Ok(()) | 905 | Ok(()) |
| 969 | } | 906 | } |
| 970 | 907 | ||
| 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 | - | ||
| 995 | fn scan_tail(&mut self) -> Result<()> { | 908 | fn scan_tail(&mut self) -> Result<()> { |
| 996 | let mut win: u64 = 4 << 20; | 909 | let mut win: u64 = 4 << 20; |
| 997 | loop { | 910 | loop { |
| @@ -1078,36 +991,6 @@ impl<R: Read + Seek> PsDemuxer<R> { | |||
| 1078 | } | 991 | } |
| 1079 | } | 992 | } |
| 1080 | 993 | ||
| 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 | - | ||
| 1111 | // ----- seeking ------------------------------------------------------------------------------ | 994 | // ----- seeking ------------------------------------------------------------------------------ |
| 1112 | 995 | ||
| 1113 | fn time_at(&mut self, off: u64, ti: usize) -> Result<Option<i64>> { | 996 | fn time_at(&mut self, off: u64, ti: usize) -> Result<Option<i64>> { |
| @@ -1291,7 +1174,7 @@ impl<R: Read + Seek> PsDemuxer<R> { | |||
| 1291 | self.tracks[0].start_time = Some(ix.start); | 1174 | self.tracks[0].start_time = Some(ix.start); |
| 1292 | self.duration = Some(ix.end - ix.start); | 1175 | self.duration = Some(ix.end - ix.start); |
| 1293 | self.es = Some(ix); | 1176 | self.es = Some(ix); |
| 1294 | - self.assign_streams(); | 1177 | + track::assign_streams(&mut self.tracks, &mut self.streams); |
| 1295 | if let Some(s) = self.streams.first_mut() { | 1178 | if let Some(s) = self.streams.first_mut() { |
| 1296 | s.duration = self.duration; | 1179 | s.duration = self.duration; |
| 1297 | } | 1180 | } |
| @@ -1533,7 +1416,11 @@ impl<R: Read + Seek + Send> Demuxer for PsDemuxer<R> { | |||
| 1533 | let Some(q) = self.pump()? else { | 1416 | let Some(q) = self.pump()? else { |
| 1534 | return Err(Error::Eof); | 1417 | return Err(Error::Eof); |
| 1535 | }; | 1418 | }; |
| 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; | ||
| 1537 | return Ok(p); | 1424 | return Ok(p); |
| 1538 | } | 1425 | } |
| 1539 | } | 1426 | } |
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 @@ | |||
| 4 | use std::collections::VecDeque; | 4 | use std::collections::VecDeque; |
| 5 | 5 | ||
| 6 | use ec_core::{ | 6 | use ec_core::{ |
| 7 | - AudioParameters, ChannelLayout, CodecId, CodecParameters, MediaParameters, PixelFormat, | 7 | + AudioParameters, ChannelLayout, CodecId, CodecParameters, MediaParameters, Packet, PacketFlags, |
| 8 | - SampleFormat, StreamInfo, TimeBase, registry::VideoParameters, | 8 | + PixelFormat, SampleFormat, StreamInfo, TimeBase, registry::VideoParameters, |
| 9 | }; | 9 | }; |
| 10 | 10 | ||
| 11 | use crate::codec::{ | 11 | use crate::codec::{ |
| 12 | - self, FrameHdr, Framing, LpcmHeader, StreamKind, build_avcc, build_hvcc, frame_header, | 12 | + self, FrameHdr, Framing, LpcmHeader, StreamKind, annexb_to_length_prefixed, build_avcc, |
| 13 | - header_lookahead, scan_au, | 13 | + build_hvcc, frame_header, header_lookahead, scan_au, |
| 14 | }; | 14 | }; |
| 15 | +use crate::demux::VideoFraming; | ||
| 15 | use crate::psi::EsEntry; | 16 | use crate::psi::EsEntry; |
| 16 | use ec_truehd::{AccessUnitHeader, frame_length}; | 17 | use ec_truehd::{AccessUnitHeader, frame_length}; |
| 17 | 18 | ||
| @@ -972,3 +973,65 @@ fn pixel_format(chroma: u8, depth: u8) -> Option<PixelFormat> { | |||
| 972 | _ => None, | 973 | _ => None, |
| 973 | } | 974 | } |
| 974 | } | 975 | } |
| 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 | +} | ||