forge
Explore
Log in

Don't you dare go Hollow

A seek lands where its packets can say what time it is

Browse files at this commit

5a98e1b8d29883e294f2cfa71400236b4e414a07Ahmet Kaan GümüşParent: 254c09a
  • fix
    • a seek lands where its packets can say what time it is
Show full message
fix(ogg): a seek lands where its packets can say what time it is

A granule position is where a page ends, and Vorbis and FLAC packets
state no duration of their own, so every packet of the page a seek
landed on came out with no timestamp at all — a seek that cannot say
where it landed. The landing is now the page after the one the bisection
found, whose first sample is that page's granule exactly, and the reader
never resumes inside the header pages.

Measured on a 3-second Vorbis fixture: seeking to 2.6 s reported 0.000 s
before and 2.033 s (the page boundary before the target) after.
crates/ec-ogg/src/demux.rsmodified+34 -7
@@ -422,18 +422,18 @@ impl<R: Read + Seek + Send> Demuxer for OggDemuxer<R> {
422 let mut hi = end;422 let mut hi = end;
423 // Last page start whose granule is at or before the target, and the423 // Last page start whose granule is at or before the target, and the
424 // first one at or after it — one bisection answers both.424 // first one at or after it — one bisection answers both.
425- let mut before = self.data_start;425+ let mut before: Option<(u64, i64)> = None;
426- let mut after = None;426+ let mut after: Option<(u64, i64)> = None;
427 while lo < hi {427 while lo < hi {
428 let mid = lo + (hi - lo) / 2;428 let mid = lo + (hi - lo) / 2;
429 match self.page_at_or_after(mid, serial)? {429 match self.page_at_or_after(mid, serial)? {
430 Some((offset, granule)) if granule != page::NO_GRANULE && granule <= target => {430 Some((offset, granule)) if granule != page::NO_GRANULE && granule <= target => {
431- before = offset;431+ before = Some((offset, granule));
432 // Always make progress: the found page may start before mid.432 // Always make progress: the found page may start before mid.
433 lo = offset.max(mid) + 1;433 lo = offset.max(mid) + 1;
434 }434 }
435 Some((offset, granule)) if granule != page::NO_GRANULE => {435 Some((offset, granule)) if granule != page::NO_GRANULE => {
436- after = Some(offset);436+ after = Some((offset, granule));
437 hi = match offset > mid {437 hi = match offset > mid {
438 true => mid,438 true => mid,
439 false => offset,439 false => offset,
@@ -445,12 +445,39 @@ impl<R: Read + Seek + Send> Demuxer for OggDemuxer<R> {
445 None => hi = mid,445 None => hi = mid,
446 }446 }
447 }447 }
448- let landing = match mode {448+ let found = match mode {
449- SeekMode::SyncAfter => after.unwrap_or(before),449+ SeekMode::SyncAfter => after.or(before),
450+ // No page ends at or before the target: it is inside the first one,
451+ // and the stream's beginning is what "at or before" means there.
450 _ => before,452 _ => before,
451 };453 };
452 self.reset_state();454 self.reset_state();
453- self.inner.seek(SeekFrom::Start(landing))?;455+ let granule = match found {
456+ // A granule is where a page *ends*, and a Vorbis or FLAC packet
457+ // states no duration of its own — so reading from the page the
458+ // bisection found would hand out its packets with no timestamp at
459+ // all, and a seek could not say where it landed. The landing is the
460+ // page *after* it, whose first sample is that page's granule.
461+ Some((offset, granule)) => {
462+ self.inner.seek(SeekFrom::Start(offset))?;
463+ read_page(&mut self.inner)?;
464+ granule
465+ }
466+ // Nothing indexed the target: the stream's own beginning is the
467+ // only honest answer, and every packet of it is still to come.
468+ None => {
469+ self.inner.seek(SeekFrom::Start(self.data_start))?;
470+ 0
471+ }
472+ };
473+ // Never resume inside the headers: they are not audio and must not be
474+ // handed out as packets.
475+ if self.inner.stream_position()? < self.data_start {
476+ self.inner.seek(SeekFrom::Start(self.data_start))?;
477+ }
478+ let track = &mut self.tracks[index];
479+ track.pos = granule;
480+ track.pos_known = true;
454 Ok(())481 Ok(())
455 }482 }
456 }483 }