Align AVI seek timestamps
Browse files at this commit555c86acc8409939f89ffecd2653285384d628e7Parent: 24272c3- fix
- align AVI seek timestamps
3 changed files +39 -0
- crates/ec-probe/src/lib.rs +17 -0
- crates/ec-probe/tests/library.rs +17 -0
- crates/ec-riff/src/avi.rs +5 -0
crates/ec-probe/src/lib.rsmodified+17 -0
| @@ -673,6 +673,7 @@ struct AviDemuxer<R> { | |||
| 673 | pending: VecDeque<Packet>, | 673 | pending: VecDeque<Packet>, |
| 674 | carry: Vec<Vec<u8>>, | 674 | carry: Vec<Vec<u8>>, |
| 675 | sync_bias: Vec<u64>, | 675 | sync_bias: Vec<u64>, |
| 676 | + seek_anchor: Vec<Option<(u64, bool)>>, | ||
| 676 | } | 677 | } |
| 677 | 678 | ||
| 678 | impl<R: Read + Seek> AviDemuxer<R> { | 679 | impl<R: Read + Seek> AviDemuxer<R> { |
| @@ -723,6 +724,7 @@ impl<R: Read + Seek> AviDemuxer<R> { | |||
| 723 | } | 724 | } |
| 724 | let samples = vec![0; streams.len()]; | 725 | let samples = vec![0; streams.len()]; |
| 725 | let carry = vec![Vec::new(); streams.len()]; | 726 | let carry = vec![Vec::new(); streams.len()]; |
| 727 | + let seek_anchor = vec![None; streams.len()]; | ||
| 726 | let sync_bias = avi_sync_biases(&mut reader, &streams, &stream_numbers)?; | 728 | let sync_bias = avi_sync_biases(&mut reader, &streams, &stream_numbers)?; |
| 727 | reader.rewind(); | 729 | reader.rewind(); |
| 728 | Ok(AviDemuxer { | 730 | Ok(AviDemuxer { |
| @@ -733,6 +735,7 @@ impl<R: Read + Seek> AviDemuxer<R> { | |||
| 733 | pending: VecDeque::new(), | 735 | pending: VecDeque::new(), |
| 734 | carry, | 736 | carry, |
| 735 | sync_bias, | 737 | sync_bias, |
| 738 | + seek_anchor, | ||
| 736 | }) | 739 | }) |
| 737 | } | 740 | } |
| 738 | 741 | ||
| @@ -802,6 +805,16 @@ impl<R: Read + Seek> AviDemuxer<R> { | |||
| 802 | } | 805 | } |
| 803 | skipped = 0; | 806 | skipped = 0; |
| 804 | } | 807 | } |
| 808 | + if let Some((anchor, after)) = self.seek_anchor[stream].take() { | ||
| 809 | + let phase = self.sync_bias.get(stream).copied().unwrap_or(0); | ||
| 810 | + let relative = anchor.saturating_sub(phase); | ||
| 811 | + let steps = if after { | ||
| 812 | + relative.saturating_add(frames.saturating_sub(1)) / frames | ||
| 813 | + } else { | ||
| 814 | + relative / frames | ||
| 815 | + }; | ||
| 816 | + self.samples[stream] = phase.saturating_add(steps.saturating_mul(frames)); | ||
| 817 | + } | ||
| 805 | self.push_packet(stream, Buf::copy_from_slice(&data[at..at + size]), frames); | 818 | self.push_packet(stream, Buf::copy_from_slice(&data[at..at + size]), frames); |
| 806 | at += size; | 819 | at += size; |
| 807 | } | 820 | } |
| @@ -1011,6 +1024,10 @@ impl<R: Read + Seek + Send> Demuxer for AviDemuxer<R> { | |||
| 1011 | } | 1024 | } |
| 1012 | self.samples[local] = avi_units_to_samples(avi_stream, landed_units) | 1025 | self.samples[local] = avi_units_to_samples(avi_stream, landed_units) |
| 1013 | .saturating_sub(self.sync_bias.get(local).copied().unwrap_or(0)); | 1026 | .saturating_sub(self.sync_bias.get(local).copied().unwrap_or(0)); |
| 1027 | + self.seek_anchor[local] = Some(( | ||
| 1028 | + avi_units_to_samples(avi_stream, seek_units), | ||
| 1029 | + matches!(mode, SeekMode::SyncAfter), | ||
| 1030 | + )); | ||
| 1014 | Ok(()) | 1031 | Ok(()) |
| 1015 | } | 1032 | } |
| 1016 | } | 1033 | } |
crates/ec-probe/tests/library.rsmodified+17 -0
| @@ -168,6 +168,22 @@ fn hive_avi_files() -> Vec<PathBuf> { | |||
| 168 | files | 168 | files |
| 169 | } | 169 | } |
| 170 | 170 | ||
| 171 | +fn assert_avi_index_is_bounded(path: &Path) { | ||
| 172 | + let file_size = std::fs::metadata(path).expect("AVI metadata").len(); | ||
| 173 | + let index_len = ec_riff::AviReader::new(File::open(path).expect("open AVI")) | ||
| 174 | + .expect("AVI index builds") | ||
| 175 | + .index_len(); | ||
| 176 | + assert!( | ||
| 177 | + u64::try_from(index_len).expect("index count fits u64") <= file_size / 8, | ||
| 178 | + "{}: index has {index_len} chunks for {file_size} bytes", | ||
| 179 | + path.display() | ||
| 180 | + ); | ||
| 181 | + eprintln!( | ||
| 182 | + "{}: AVI index {index_len} chunks / {file_size} bytes", | ||
| 183 | + path.display() | ||
| 184 | + ); | ||
| 185 | +} | ||
| 186 | + | ||
| 171 | fn decode_stream_to_eof( | 187 | fn decode_stream_to_eof( |
| 172 | reader: &mut Reader, | 188 | reader: &mut Reader, |
| 173 | stream: u32, | 189 | stream: u32, |
| @@ -239,6 +255,7 @@ fn best_frame_corr( | |||
| 239 | } | 255 | } |
| 240 | 256 | ||
| 241 | fn assert_avi_seeks_match_reference(path: &Path) { | 257 | fn assert_avi_seeks_match_reference(path: &Path) { |
| 258 | + assert_avi_index_is_bounded(path); | ||
| 242 | let bytes = Arc::new(AtomicU64::new(0)); | 259 | let bytes = Arc::new(AtomicU64::new(0)); |
| 243 | let src = CountedFile { | 260 | let src = CountedFile { |
| 244 | inner: File::open(path).expect("open AVI"), | 261 | inner: File::open(path).expect("open AVI"), |
crates/ec-riff/src/avi.rsmodified+5 -0
| @@ -171,6 +171,11 @@ impl<R: Read + Seek> AviReader<R> { | |||
| 171 | !self.index.is_empty() | 171 | !self.index.is_empty() |
| 172 | } | 172 | } |
| 173 | 173 | ||
| 174 | + /// Number of indexed audio chunks available for seeking. | ||
| 175 | + pub fn index_len(&self) -> usize { | ||
| 176 | + self.index.len() | ||
| 177 | + } | ||
| 178 | + | ||
| 174 | /// Return to the first chunk in `movi`. | 179 | /// Return to the first chunk in `movi`. |
| 175 | pub fn rewind(&mut self) { | 180 | pub fn rewind(&mut self) { |
| 176 | self.pos = self.movi_start; | 181 | self.pos = self.movi_start; |