forge
Explore
Log in

Don't you dare go Hollow

U18: ec-ogg track state: pos is Option<i64> (None = unknown), partial is Option<Vec<u8>> (Some = mid-packet); drops pos_known and the assembling flag, same packet stream

Browse files at this commit

3a977c6a4b7181cc1fff624c7aee9f7f38a66181Ahmet Kaan GümüşParent: 8057432
crates/ec-ogg/src/demux.rsmodified+23 -37
@@ -49,15 +49,12 @@ struct Track {
49 mapping: Mapping,49 mapping: Mapping,
50 /// Header packets, in order, until [`Mapping::header_packets`] are in.50 /// Header packets, in order, until [`Mapping::header_packets`] are in.
51 headers: Vec<Vec<u8>>,51 headers: Vec<Vec<u8>>,
52- /// Bytes of a packet still being assembled across a page boundary.52+ /// Bytes of a packet still being assembled across a page boundary; `Some` while
53- partial: Vec<u8>,53+ /// one is mid-assembly, even when its bytes so far are empty.
54- /// A packet is mid-assembly (true even when its bytes so far are empty,54+ partial: Option<Vec<u8>>,
55- /// which happens when a page ends exactly on a 255-byte segment).55+ /// Granule position the next packet starts at; `None` after a packet of unknown
56- assembling: bool,56+ /// duration until the next page granule.
57- /// Granule position the next packet starts at.57+ pos: Option<i64>,
58- pos: i64,
59- /// False between a packet of unknown duration and the next page granule.
60- pos_known: bool,
61 /// The stream has seen its end-of-stream page.58 /// The stream has seen its end-of-stream page.
62 eos: bool,59 eos: bool,
63 }60 }
@@ -207,10 +204,10 @@ impl<R: Read + Seek> OggDemuxer<R> {
207 204
208 let mapping = self.tracks[index].mapping;205 let mapping = self.tracks[index].mapping;
209 let time_base = self.streams[index].time_base;206 let time_base = self.streams[index].time_base;
210- let mut packet = std::mem::take(&mut self.tracks[index].partial);207+ let carried = self.tracks[index].partial.take();
211- let mut assembling = self.tracks[index].assembling;208+ let mut assembling = carried.is_some();
209+ let mut packet = carried.unwrap_or_default();
212 let mut pos = self.tracks[index].pos;210 let mut pos = self.tracks[index].pos;
213- let mut pos_known = self.tracks[index].pos_known;
214 let mut headers_done = self.tracks[index].headers_complete();211 let mut headers_done = self.tracks[index].headers_complete();
215 let mut at = 0usize;212 let mut at = 0usize;
216 let mut segments = &page.header.segments[..];213 let mut segments = &page.header.segments[..];
@@ -270,32 +267,26 @@ impl<R: Read + Seek> OggDemuxer<R> {
270 }267 }
271 let duration = mapping.packet_duration(&done);268 let duration = mapping.packet_duration(&done);
272 let mut out = Packet::new(index as u32, time_base, done);269 let mut out = Packet::new(index as u32, time_base, done);
273- out.pts = pos_known.then_some(pos);270+ out.pts = pos;
274 out.dts = out.pts;271 out.dts = out.pts;
275 out.duration = duration;272 out.duration = duration;
276 // Every Ogg audio packet is a decodable entry point as far273 // Every Ogg audio packet is a decodable entry point as far
277 // as the container is concerned; codec pre-roll (a Vorbis274 // as the container is concerned; codec pre-roll (a Vorbis
278 // block needs its predecessor to overlap) is the decoder's.275 // block needs its predecessor to overlap) is the decoder's.
279 out.flags.keyframe = true;276 out.flags.keyframe = true;
280- match duration {277+ // Past i64::MAX the position is no longer known, like an
281- // Past i64::MAX the position is no longer known, like an278+ // unknown duration: the next page granule re-anchors it.
282- // unknown duration: the next page granule re-anchors it.279+ pos = match (pos, duration) {
283- Some(d) if pos_known => match pos.checked_add(d) {280+ (Some(p), Some(d)) => p.checked_add(d),
284- Some(next) => pos = next,281+ _ => None,
285- None => pos_known = false,282+ };
286- },
287- Some(_) => {}
288- None => pos_known = false,
289- }
290 self.queue.push_back(out);283 self.queue.push_back(out);
291 }284 }
292 }285 }
293 }286 }
294 let track = &mut self.tracks[index];287 let track = &mut self.tracks[index];
295- track.partial = packet;288+ track.partial = assembling.then_some(packet);
296- track.assembling = assembling;
297 track.pos = pos;289 track.pos = pos;
298- track.pos_known = pos_known;
299 track.eos |= page.header.eos;290 track.eos |= page.header.eos;
300 291
301 // The granule is where the last packet finishing on this page ends, so292 // The granule is where the last packet finishing on this page ends, so
@@ -309,8 +300,7 @@ impl<R: Read + Seek> OggDemuxer<R> {
309 .push(granule_side_data(page.header.granule));300 .push(granule_side_data(page.header.granule));
310 }301 }
311 let track = &mut self.tracks[index];302 let track = &mut self.tracks[index];
312- track.pos = page.header.granule;303+ track.pos = Some(page.header.granule);
313- track.pos_known = true;
314 }304 }
315 Ok(())305 Ok(())
316 }306 }
@@ -338,10 +328,8 @@ impl<R: Read + Seek> OggDemuxer<R> {
338 serial: page.header.serial,328 serial: page.header.serial,
339 mapping,329 mapping,
340 headers: Vec::new(),330 headers: Vec::new(),
341- partial: Vec::new(),331+ partial: None,
342- assembling: false,332+ pos: Some(0),
343- pos: 0,
344- pos_known: true,
345 eos: false,333 eos: false,
346 });334 });
347 Some(index)335 Some(index)
@@ -383,9 +371,8 @@ impl<R: Read + Seek> OggDemuxer<R> {
383 fn reset_state(&mut self) {371 fn reset_state(&mut self) {
384 self.queue.clear();372 self.queue.clear();
385 for track in &mut self.tracks {373 for track in &mut self.tracks {
386- track.partial.clear();374+ track.partial = None;
387- track.assembling = false;375+ track.pos = None;
388- track.pos_known = false;
389 track.eos = false;376 track.eos = false;
390 }377 }
391 }378 }
@@ -493,8 +480,7 @@ impl<R: Read + Seek + Send> Demuxer for OggDemuxer<R> {
493 self.inner.seek(SeekFrom::Start(self.data_start))?;480 self.inner.seek(SeekFrom::Start(self.data_start))?;
494 }481 }
495 let track = &mut self.tracks[index];482 let track = &mut self.tracks[index];
496- track.pos = granule;483+ track.pos = Some(granule);
497- track.pos_known = true;
498 Ok(())484 Ok(())
499 }485 }
500 }486 }