S1: read_window helper for the seek-and-read window in the DTS, MP3 and ADTS openers
Browse files at this commit0bb052da344778e30e806a296a8e4195cec20ab9Parent: a3c9e161 changed file +12 -16
- crates/ec-probe/src/raw_open.rs +12 -16
crates/ec-probe/src/raw_open.rsmodified+12 -16
| @@ -21,10 +21,7 @@ fn wav_dts_span<R: Read + Seek>(src: &mut R, end: u64) -> Result<Option<(u64, u6 | |||
| 21 | } else { | 21 | } else { |
| 22 | body + size | 22 | body + size |
| 23 | }; | 23 | }; |
| 24 | - let mut window = vec![0u8; HEAD.min((stop - body) as usize)]; | 24 | + let window = read_window(src, body, stop)?; |
| 25 | - src.seek(SeekFrom::Start(body))?; | ||
| 26 | - let got = read_upto(src, &mut window)?; | ||
| 27 | - window.truncate(got); | ||
| 28 | return Ok(dts_chains(&window).then_some((body, stop))); | 25 | return Ok(dts_chains(&window).then_some((body, stop))); |
| 29 | } | 26 | } |
| 30 | at = body + size + (size & 1); | 27 | at = body + size + (size & 1); |
| @@ -32,16 +29,21 @@ fn wav_dts_span<R: Read + Seek>(src: &mut R, end: u64) -> Result<Option<(u64, u6 | |||
| 32 | Ok(None) | 29 | Ok(None) |
| 33 | } | 30 | } |
| 34 | 31 | ||
| 32 | +fn read_window<R: Read + Seek>(src: &mut R, start: u64, end: u64) -> Result<Vec<u8>> { | ||
| 33 | + let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)]; | ||
| 34 | + src.seek(SeekFrom::Start(start))?; | ||
| 35 | + let got = read_upto(src, &mut window)?; | ||
| 36 | + window.truncate(got); | ||
| 37 | + Ok(window) | ||
| 38 | +} | ||
| 39 | + | ||
| 35 | fn open_dts_span<R: Read + Seek + Send + 'static>( | 40 | fn open_dts_span<R: Read + Seek + Send + 'static>( |
| 36 | mut src: R, | 41 | mut src: R, |
| 37 | format: Format, | 42 | format: Format, |
| 38 | start: u64, | 43 | start: u64, |
| 39 | end: u64, | 44 | end: u64, |
| 40 | ) -> Result<Reader> { | 45 | ) -> Result<Reader> { |
| 41 | - let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)]; | 46 | + let window = read_window(&mut src, start, end)?; |
| 42 | - src.seek(SeekFrom::Start(start))?; | ||
| 43 | - let got = read_upto(&mut src, &mut window)?; | ||
| 44 | - window.truncate(got); | ||
| 45 | let (core, _) = dts::access_unit(&window) | 47 | let (core, _) = dts::access_unit(&window) |
| 46 | .ok_or_else(|| Error::corrupt("dts: no frame at the start of the stream"))?; | 48 | .ok_or_else(|| Error::corrupt("dts: no frame at the start of the stream"))?; |
| 47 | let total = dts_total(&window, end, start); | 49 | let total = dts_total(&window, end, start); |
| @@ -143,10 +145,7 @@ pub(crate) fn open_flac<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8] | |||
| 143 | 145 | ||
| 144 | pub(crate) fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) -> Result<Reader> { | 146 | pub(crate) fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) -> Result<Reader> { |
| 145 | let (start, tags) = id3(&mut src, head, end)?; | 147 | let (start, tags) = id3(&mut src, head, end)?; |
| 146 | - let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)]; | 148 | + let window = read_window(&mut src, start, end)?; |
| 147 | - src.seek(SeekFrom::Start(start))?; | ||
| 148 | - let got = read_upto(&mut src, &mut window)?; | ||
| 149 | - window.truncate(got); | ||
| 150 | let at = frame_sync(&window, Format::Mp3) | 149 | let at = frame_sync(&window, Format::Mp3) |
| 151 | .ok_or_else(|| Error::corrupt("mp3: no frame in the first 64 KiB"))?; | 150 | .ok_or_else(|| Error::corrupt("mp3: no frame in the first 64 KiB"))?; |
| 152 | let header = ec_mp3::FrameHeader::parse(&window[at..])?; | 151 | let header = ec_mp3::FrameHeader::parse(&window[at..])?; |
| @@ -197,10 +196,7 @@ pub(crate) fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], | |||
| 197 | 196 | ||
| 198 | pub(crate) fn open_adts<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) -> Result<Reader> { | 197 | pub(crate) fn open_adts<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) -> Result<Reader> { |
| 199 | let (start, tags) = id3(&mut src, head, end)?; | 198 | let (start, tags) = id3(&mut src, head, end)?; |
| 200 | - let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)]; | 199 | + let window = read_window(&mut src, start, end)?; |
| 201 | - src.seek(SeekFrom::Start(start))?; | ||
| 202 | - let got = read_upto(&mut src, &mut window)?; | ||
| 203 | - window.truncate(got); | ||
| 204 | let at = frame_sync(&window, Format::Adts) | 200 | let at = frame_sync(&window, Format::Adts) |
| 205 | .ok_or_else(|| Error::corrupt("aac: no ADTS frame in the first 64 KiB"))?; | 201 | .ok_or_else(|| Error::corrupt("aac: no ADTS frame in the first 64 KiB"))?; |
| 206 | let header = ec_aac::parse_adts(&window[at..])?; | 202 | let header = ec_aac::parse_adts(&window[at..])?; |