From d2e45f5daf752d18a5ab4bb7245b7e7111b9f90d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 15:13:50 +0300 Subject: [PATCH] S1: FLAC STREAMINFO lives inside Kind::Flac; RawDemuxer::new loses its flac argument --- crates/ec-probe/src/raw.rs | 39 ++++++++++++++------------------- crates/ec-probe/src/raw_open.rs | 8 ++----- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/crates/ec-probe/src/raw.rs b/crates/ec-probe/src/raw.rs index bddfc266..558aa48a 100644 --- a/crates/ec-probe/src/raw.rs +++ b/crates/ec-probe/src/raw.rs @@ -27,7 +27,7 @@ const COMPACT_AT: usize = 256 << 10; const WAV_FRAMES: usize = 4096; /// How a format states the length of one frame. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub(crate) enum Kind { /// Fixed-size PCM: a "frame" is one interleaved sample. Wav { @@ -40,7 +40,7 @@ pub(crate) enum Kind { samples: u64, }, Mp3, - Flac, + Flac(ec_flac::StreamInfo), Adts, /// DTS core frames, each with its extension substream when one follows. Dts, @@ -69,8 +69,6 @@ pub(crate) struct RawDemuxer { /// Next packet's first sample, in the stream's own time base. sample: u64, rate: u32, - /// `STREAMINFO`, needed to read a FLAC frame header. - flac: Option, /// `(first sample, offset)` per frame, built on the first seek. index: Vec<(u64, u64)>, indexed: bool, @@ -84,7 +82,6 @@ impl RawDemuxer { start: u64, end: u64, total_samples: Option, - flac: Option, ) -> Result> { let rate = params .audio() @@ -107,7 +104,6 @@ impl RawDemuxer { head: 0, sample: 0, rate, - flac, index: Vec::new(), indexed: false, }) @@ -122,14 +118,11 @@ impl RawDemuxer { /// Samples in a typical frame of this format, for the tail of an estimate. fn frame_samples(&self) -> u64 { - match self.kind { + match &self.kind { Kind::Wav { .. } => WAV_FRAMES as u64, - Kind::WavBlocks { samples, .. } => samples, + Kind::WavBlocks { samples, .. } => *samples, Kind::Mp3 => 1152, - Kind::Flac => self - .flac - .as_ref() - .map_or(4096, |i| u64::from(i.max_block_size)), + Kind::Flac(info) => u64::from(info.max_block_size), Kind::Adts => 1024, Kind::Dts => 512, } @@ -182,16 +175,15 @@ impl RawDemuxer { /// is what carries a reader over an ID3 tag in the middle of a stream or a /// scrap of a torn download. fn framing(&mut self) -> Result { - let want = match self.kind { + let want = match &self.kind { Kind::Wav { block_align } => block_align * WAV_FRAMES, - Kind::WavBlocks { block, .. } => block, + Kind::WavBlocks { block, .. } => *block, // A FLAC frame is bounded by the *next* sync word, so the window // has to hold a whole one plus the head of its successor. - Kind::Flac => 1 << 20, + Kind::Flac(_) => 1 << 20, _ => 8192, }; - let flac = self.flac.clone(); - let kind = self.kind; + let kind = self.kind.clone(); let mut skipped = 0u64; loop { self.fill(want)?; @@ -200,7 +192,7 @@ impl RawDemuxer { return Err(Error::Eof); } let tail = self.read_at >= self.end; - match frame_at(kind, self.window(), flac.as_ref()) { + match frame_at(&kind, self.window()) { Ok(f) => return Ok(f), Err(e) if e.is_need_more() && tail => { // The tail of a file: whatever is left is the last frame. @@ -249,7 +241,7 @@ impl RawDemuxer { } /// One frame's geometry, read from the head of `data`. -fn frame_at(kind: Kind, data: &[u8], flac: Option<&ec_flac::StreamInfo>) -> Result { +fn frame_at(kind: &Kind, data: &[u8]) -> Result { match kind { Kind::Wav { block_align } => { let frames = (data.len() / block_align).min(WAV_FRAMES); @@ -261,10 +253,10 @@ fn frame_at(kind: Kind, data: &[u8], flac: Option<&ec_flac::StreamInfo>) -> Resu }), } } - Kind::WavBlocks { block, samples } => match data.len() >= block && block > 0 { + Kind::WavBlocks { block, samples } => match data.len() >= *block && *block > 0 { true => Ok(Framing { - bytes: block, - samples, + bytes: *block, + samples: *samples, }), false => Err(Error::Eof), }, @@ -307,7 +299,8 @@ fn frame_at(kind: Kind, data: &[u8], flac: Option<&ec_flac::StreamInfo>) -> Resu samples: u64::from(core.samples), }) } - Kind::Flac => { + Kind::Flac(info) => { + let flac = Some(info); if ec_flac::decode::find_frame_sync(data, 0, flac) != Some(0) { return Err(Error::corrupt("flac: not a frame header")); } diff --git a/crates/ec-probe/src/raw_open.rs b/crates/ec-probe/src/raw_open.rs index 9aea13b6..0d429f0f 100644 --- a/crates/ec-probe/src/raw_open.rs +++ b/crates/ec-probe/src/raw_open.rs @@ -52,7 +52,6 @@ fn open_dts_span( start, end, total, - None, )?; Ok(Reader::single(format, demuxer, Tags::default())) } @@ -78,7 +77,7 @@ pub(crate) fn open_wav(mut src: R, end: u64) -> .unwrap_or(end) .min(end); let total = frames.or(Some((audio_end - start) / bytes_per.0 * bytes_per.1)); - let demuxer = raw::RawDemuxer::new(src, kind, params, start, audio_end, total, None)?; + let demuxer = raw::RawDemuxer::new(src, kind, params, start, audio_end, total)?; return Ok(Reader::single(Format::Wav, demuxer, Tags::default())); } let (params, block_align) = raw::wav_parameters(spec)?; @@ -96,7 +95,6 @@ pub(crate) fn open_wav(mut src: R, end: u64) -> start, audio_end, total, - None, )?; Ok(Reader::single(Format::Wav, demuxer, Tags::default())) } @@ -139,7 +137,7 @@ pub(crate) fn open_flac(mut src: R, head: &[u8] 0 => None, n => Some(n), }; - let demuxer = raw::RawDemuxer::new(src, raw::Kind::Flac, params, at, end, total, Some(info))?; + let demuxer = raw::RawDemuxer::new(src, raw::Kind::Flac(info), params, at, end, total)?; Ok(Reader::single(Format::Flac, demuxer, tags)) } @@ -192,7 +190,6 @@ pub(crate) fn open_mp3(mut src: R, head: &[u8], start, audio_end, total, - None, )? .with_initial_padding(padding); Ok(Reader::single(Format::Mp3, demuxer, tags)) @@ -234,7 +231,6 @@ pub(crate) fn open_adts(mut src: R, head: &[u8] start, audio_end, total, - None, )?; Ok(Reader::single(Format::Adts, demuxer, tags)) }