diff --git a/crates/ec-probe/src/decoder.rs b/crates/ec-probe/src/decoder.rs index 62d9bbb1..231d74a3 100644 --- a/crates/ec-probe/src/decoder.rs +++ b/crates/ec-probe/src/decoder.rs @@ -353,7 +353,8 @@ fn flac_stream_info(data: &[u8]) -> Option<&[u8]> { if data.len() == 34 { return Some(data); } - for at in 0..data.len().saturating_sub(38).min(16) { + let last = data.len().checked_sub(38)?; + for at in 0..=last.min(16) { let head = &data[at..at + 4]; let len = u32::from_be_bytes([0, head[1], head[2], head[3]]); if head[0] & 0x7f == 0 && len == 34 { diff --git a/crates/ec-probe/src/lib.rs b/crates/ec-probe/src/lib.rs index ce9d88e3..5399f6c2 100644 --- a/crates/ec-probe/src/lib.rs +++ b/crates/ec-probe/src/lib.rs @@ -315,7 +315,12 @@ impl Reader { .find(|s| s.params.codec == CodecId::Vorbis) .and_then(|s| s.params.extradata.as_deref()) .and_then(|data| ec_ogg::xiph_unlace(data).ok()) - .and_then(|packets| packets.get(1).map(|p| tags::from_vorbis_comment(&p[7..]))) + .and_then(|packets| { + packets + .get(1) + .and_then(|p| p.get(7..)) + .map(tags::from_vorbis_comment) + }) .unwrap_or_default(); let native = (0..demuxer.streams().len() as u64).collect(); Ok(Reader::wrap(format, Box::new(demuxer), tags, native)) @@ -407,7 +412,7 @@ impl Reader { let padding_ticks = s .params .audio() - .filter(|_| s.initial_padding > 0) + .filter(|a| a.sample_rate > 0 && s.initial_padding > 0) .map(|a| { TimeBase::from_rate(a.sample_rate).rescale( i64::from(s.initial_padding), @@ -416,14 +421,10 @@ impl Reader { ) }) .unwrap_or(0); - Timestamp::new(d - padding_ticks, s.time_base) + Timestamp::new(d.saturating_sub(padding_ticks), s.time_base) }) }) - .max_by(|a, b| { - a.as_secs_f64() - .partial_cmp(&b.as_secs_f64()) - .unwrap_or(std::cmp::Ordering::Equal) - }) + .max() } /// Every audio stream with no decoder behind it, and why. @@ -559,7 +560,7 @@ fn open_dts_span( start: u64, end: u64, ) -> Result { - let mut window = vec![0u8; HEAD.min((end - start) as usize)]; + let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)]; src.seek(SeekFrom::Start(start))?; let got = read_upto(&mut src, &mut window)?; window.truncate(got); @@ -655,7 +656,8 @@ fn open_flac(mut src: R, head: &[u8], end: u64) } let last = header[0] & 0x80 != 0; let kind = header[0] & 0x7f; - let len = u64::from(u32::from_be_bytes([0, header[1], header[2], header[3]])).min(1 << 24); + let len = u64::from(u32::from_be_bytes([0, header[1], header[2], header[3]])) + .min(end.saturating_sub(at + 4)); let mut body = vec![0u8; len as usize]; read_upto(&mut src, &mut body)?; at += 4 + len; @@ -680,7 +682,7 @@ fn open_flac(mut src: R, head: &[u8], end: u64) fn open_mp3(mut src: R, head: &[u8], end: u64) -> Result { let (start, tags) = id3(&mut src, head, end)?; - let mut window = vec![0u8; HEAD.min((end - start) as usize)]; + let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)]; src.seek(SeekFrom::Start(start))?; let got = read_upto(&mut src, &mut window)?; window.truncate(got); @@ -688,7 +690,7 @@ fn open_mp3(mut src: R, head: &[u8], end: u64) .ok_or_else(|| Error::corrupt("mp3: no frame in the first 64 KiB"))?; let header = ec_mp3::FrameHeader::parse(&window[at..])?; let mut start = start + at as u64; - let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?); + let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?).max(start); // A Xing/Info header states the frame count exactly; without one the // duration is the bitrate estimate the oracle also falls back to, which is // right for CBR and approximate for VBR. @@ -708,7 +710,10 @@ fn open_mp3(mut src: R, head: &[u8], end: u64) let (padding, total) = match &xing { Some(x) => ( x.delay + MP3_DECODER_DELAY, - Some(x.frames * per_frame - u64::from(x.padding.saturating_sub(MP3_DECODER_DELAY))), + Some( + (x.frames * per_frame) + .saturating_sub(u64::from(x.padding.saturating_sub(MP3_DECODER_DELAY))), + ), ), None => ( 0, @@ -732,7 +737,7 @@ fn open_mp3(mut src: R, head: &[u8], end: u64) fn open_adts(mut src: R, head: &[u8], end: u64) -> Result { let (start, tags) = id3(&mut src, head, end)?; - let mut window = vec![0u8; HEAD.min((end - start) as usize)]; + let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)]; src.seek(SeekFrom::Start(start))?; let got = read_upto(&mut src, &mut window)?; window.truncate(got); @@ -740,7 +745,7 @@ fn open_adts(mut src: R, head: &[u8], end: u64) .ok_or_else(|| Error::corrupt("aac: no ADTS frame in the first 64 KiB"))?; let header = ec_aac::parse_adts(&window[at..])?; let start = start + at as u64; - let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?); + let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?).max(start); // ADTS states no duration anywhere: every frame is 1024 samples, so the // file's size over its *average* frame is the estimate — the same one // the oracle prints "estimating duration from bitrate" about. Averaged rather @@ -1075,10 +1080,7 @@ fn avi_elementary_frame(codec: CodecId, data: &[u8]) -> Result Ok(None), } diff --git a/crates/ec-probe/src/raw.rs b/crates/ec-probe/src/raw.rs index cf0c9aa5..a50511bd 100644 --- a/crates/ec-probe/src/raw.rs +++ b/crates/ec-probe/src/raw.rs @@ -293,7 +293,7 @@ fn frame_at(kind: Kind, data: &[u8], flac: Option<&ec_flac::StreamInfo>) -> Resu } Ok(Framing { bytes: header.frame_length, - samples: 1024 * u64::from(header.raw_blocks), + samples: adts_samples(&header), }) } Kind::Dts => { @@ -362,7 +362,7 @@ impl Demuxer for RawDemuxer { // PCM needs no index and no rounding: every sample is a random access // point, at a byte offset arithmetic gives. if let Kind::Wav { block_align } = self.kind { - let at = self.start + target * block_align as u64; + let at = self.start + target.saturating_mul(block_align as u64); self.jump(at.min(self.end))?; self.sample = target; return Ok(()); @@ -413,7 +413,7 @@ pub(crate) fn wav_compressed_parameters( } let kind = match c.tag { 0x0006 | 0x0007 => Kind::Wav { - block_align: usize::from(spec.channels), + block_align: usize::from(spec.channels.max(1)), }, _ => Kind::WavBlocks { block: usize::from(c.block_align), @@ -451,6 +451,11 @@ pub(crate) fn wav_parameters(spec: ec_riff::WavSpec) -> Result<(CodecParameters, Ok((params, spec.block_align())) } +/// Samples one ADTS frame decodes to: `raw_blocks` already counts the blocks. +pub(crate) fn adts_samples(header: &ec_aac::AdtsHeader) -> u64 { + u64::from(header.raw_blocks) * ec_aac::FRAME_LEN as u64 +} + /// Codec parameters for an ADTS stream, from its first frame header. pub(crate) fn adts_parameters(header: &ec_aac::AdtsHeader) -> CodecParameters { let mut params = CodecParameters::new(CodecId::Aac); diff --git a/crates/ec-probe/src/tags.rs b/crates/ec-probe/src/tags.rs index 6ad20948..29f6dabb 100644 --- a/crates/ec-probe/src/tags.rs +++ b/crates/ec-probe/src/tags.rs @@ -50,10 +50,11 @@ impl Tags { "TRCK" | "TRK" | "TRACKNUMBER" => { // "3/12" is a track number and a total; the number is the part // anything displays. - self.track = value + let track = value .split(['/', ' ']) .next() .and_then(|n| n.trim().parse().ok()); + self.track = self.track.or(track); return; } _ => return,