forge
Explore
Log in

Don't you dare go Hollow

S1: FLAC STREAMINFO lives inside Kind::Flac; RawDemuxer::new loses its flac argument

Browse files at this commit

d2e45f5daf752d18a5ab4bb7245b7e7111b9f90dAhmet Kaan GümüşParent: 59ba650
crates/ec-probe/src/raw.rsmodified+16 -23
@@ -27,7 +27,7 @@ const COMPACT_AT: usize = 256 << 10;
2727 const WAV_FRAMES: usize = 4096;
2828
2929 /// How a format states the length of one frame.
30-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
30+#[derive(Clone, Debug, PartialEq, Eq)]
3131 pub(crate) enum Kind {
3232 /// Fixed-size PCM: a "frame" is one interleaved sample.
3333 Wav {
@@ -40,7 +40,7 @@ pub(crate) enum Kind {
4040 samples: u64,
4141 },
4242 Mp3,
43- Flac,
43+ Flac(ec_flac::StreamInfo),
4444 Adts,
4545 /// DTS core frames, each with its extension substream when one follows.
4646 Dts,
@@ -69,8 +69,6 @@ pub(crate) struct RawDemuxer<R> {
6969 /// Next packet's first sample, in the stream's own time base.
7070 sample: u64,
7171 rate: u32,
72- /// `STREAMINFO`, needed to read a FLAC frame header.
73- flac: Option<ec_flac::StreamInfo>,
7472 /// `(first sample, offset)` per frame, built on the first seek.
7573 index: Vec<(u64, u64)>,
7674 indexed: bool,
@@ -84,7 +82,6 @@ impl<R: Read + Seek> RawDemuxer<R> {
8482 start: u64,
8583 end: u64,
8684 total_samples: Option<u64>,
87- flac: Option<ec_flac::StreamInfo>,
8885 ) -> Result<RawDemuxer<R>> {
8986 let rate = params
9087 .audio()
@@ -107,7 +104,6 @@ impl<R: Read + Seek> RawDemuxer<R> {
107104 head: 0,
108105 sample: 0,
109106 rate,
110- flac,
111107 index: Vec::new(),
112108 indexed: false,
113109 })
@@ -122,14 +118,11 @@ impl<R: Read + Seek> RawDemuxer<R> {
122118
123119 /// Samples in a typical frame of this format, for the tail of an estimate.
124120 fn frame_samples(&self) -> u64 {
125- match self.kind {
121+ match &self.kind {
126122 Kind::Wav { .. } => WAV_FRAMES as u64,
127- Kind::WavBlocks { samples, .. } => samples,
123+ Kind::WavBlocks { samples, .. } => *samples,
128124 Kind::Mp3 => 1152,
129- Kind::Flac => self
130- .flac
131- .as_ref()
132- .map_or(4096, |i| u64::from(i.max_block_size)),
125+ Kind::Flac(info) => u64::from(info.max_block_size),
133126 Kind::Adts => 1024,
134127 Kind::Dts => 512,
135128 }
@@ -182,16 +175,15 @@ impl<R: Read + Seek> RawDemuxer<R> {
182175 /// is what carries a reader over an ID3 tag in the middle of a stream or a
183176 /// scrap of a torn download.
184177 fn framing(&mut self) -> Result<Framing> {
185- let want = match self.kind {
178+ let want = match &self.kind {
186179 Kind::Wav { block_align } => block_align * WAV_FRAMES,
187- Kind::WavBlocks { block, .. } => block,
180+ Kind::WavBlocks { block, .. } => *block,
188181 // A FLAC frame is bounded by the *next* sync word, so the window
189182 // has to hold a whole one plus the head of its successor.
190- Kind::Flac => 1 << 20,
183+ Kind::Flac(_) => 1 << 20,
191184 _ => 8192,
192185 };
193- let flac = self.flac.clone();
194- let kind = self.kind;
186+ let kind = self.kind.clone();
195187 let mut skipped = 0u64;
196188 loop {
197189 self.fill(want)?;
@@ -200,7 +192,7 @@ impl<R: Read + Seek> RawDemuxer<R> {
200192 return Err(Error::Eof);
201193 }
202194 let tail = self.read_at >= self.end;
203- match frame_at(kind, self.window(), flac.as_ref()) {
195+ match frame_at(&kind, self.window()) {
204196 Ok(f) => return Ok(f),
205197 Err(e) if e.is_need_more() && tail => {
206198 // The tail of a file: whatever is left is the last frame.
@@ -249,7 +241,7 @@ impl<R: Read + Seek> RawDemuxer<R> {
249241 }
250242
251243 /// One frame's geometry, read from the head of `data`.
252-fn frame_at(kind: Kind, data: &[u8], flac: Option<&ec_flac::StreamInfo>) -> Result<Framing> {
244+fn frame_at(kind: &Kind, data: &[u8]) -> Result<Framing> {
253245 match kind {
254246 Kind::Wav { block_align } => {
255247 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
261253 }),
262254 }
263255 }
264- Kind::WavBlocks { block, samples } => match data.len() >= block && block > 0 {
256+ Kind::WavBlocks { block, samples } => match data.len() >= *block && *block > 0 {
265257 true => Ok(Framing {
266- bytes: block,
267- samples,
258+ bytes: *block,
259+ samples: *samples,
268260 }),
269261 false => Err(Error::Eof),
270262 },
@@ -307,7 +299,8 @@ fn frame_at(kind: Kind, data: &[u8], flac: Option<&ec_flac::StreamInfo>) -> Resu
307299 samples: u64::from(core.samples),
308300 })
309301 }
310- Kind::Flac => {
302+ Kind::Flac(info) => {
303+ let flac = Some(info);
311304 if ec_flac::decode::find_frame_sync(data, 0, flac) != Some(0) {
312305 return Err(Error::corrupt("flac: not a frame header"));
313306 }
crates/ec-probe/src/raw_open.rsmodified+2 -6
@@ -52,7 +52,6 @@ fn open_dts_span<R: Read + Seek + Send + 'static>(
5252 start,
5353 end,
5454 total,
55- None,
5655 )?;
5756 Ok(Reader::single(format, demuxer, Tags::default()))
5857 }
@@ -78,7 +77,7 @@ pub(crate) fn open_wav<R: Read + Seek + Send + 'static>(mut src: R, end: u64) ->
7877 .unwrap_or(end)
7978 .min(end);
8079 let total = frames.or(Some((audio_end - start) / bytes_per.0 * bytes_per.1));
81- let demuxer = raw::RawDemuxer::new(src, kind, params, start, audio_end, total, None)?;
80+ let demuxer = raw::RawDemuxer::new(src, kind, params, start, audio_end, total)?;
8281 return Ok(Reader::single(Format::Wav, demuxer, Tags::default()));
8382 }
8483 let (params, block_align) = raw::wav_parameters(spec)?;
@@ -96,7 +95,6 @@ pub(crate) fn open_wav<R: Read + Seek + Send + 'static>(mut src: R, end: u64) ->
9695 start,
9796 audio_end,
9897 total,
99- None,
10098 )?;
10199 Ok(Reader::single(Format::Wav, demuxer, Tags::default()))
102100 }
@@ -139,7 +137,7 @@ pub(crate) fn open_flac<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8]
139137 0 => None,
140138 n => Some(n),
141139 };
142- let demuxer = raw::RawDemuxer::new(src, raw::Kind::Flac, params, at, end, total, Some(info))?;
140+ let demuxer = raw::RawDemuxer::new(src, raw::Kind::Flac(info), params, at, end, total)?;
143141 Ok(Reader::single(Format::Flac, demuxer, tags))
144142 }
145143
@@ -192,7 +190,6 @@ pub(crate) fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8],
192190 start,
193191 audio_end,
194192 total,
195- None,
196193 )?
197194 .with_initial_padding(padding);
198195 Ok(Reader::single(Format::Mp3, demuxer, tags))
@@ -234,7 +231,6 @@ pub(crate) fn open_adts<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8]
234231 start,
235232 audio_end,
236233 total,
237- None,
238234 )?;
239235 Ok(Reader::single(Format::Adts, demuxer, tags))
240236 }