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;
27 const WAV_FRAMES: usize = 4096;27 const WAV_FRAMES: usize = 4096;
28 28
29 /// How a format states the length of one frame.29 /// How a format states the length of one frame.
30-#[derive(Clone, Copy, Debug, PartialEq, Eq)]30+#[derive(Clone, Debug, PartialEq, Eq)]
31 pub(crate) enum Kind {31 pub(crate) enum Kind {
32 /// Fixed-size PCM: a "frame" is one interleaved sample.32 /// Fixed-size PCM: a "frame" is one interleaved sample.
33 Wav {33 Wav {
@@ -40,7 +40,7 @@ pub(crate) enum Kind {
40 samples: u64,40 samples: u64,
41 },41 },
42 Mp3,42 Mp3,
43- Flac,43+ Flac(ec_flac::StreamInfo),
44 Adts,44 Adts,
45 /// DTS core frames, each with its extension substream when one follows.45 /// DTS core frames, each with its extension substream when one follows.
46 Dts,46 Dts,
@@ -69,8 +69,6 @@ pub(crate) struct RawDemuxer<R> {
69 /// Next packet's first sample, in the stream's own time base.69 /// Next packet's first sample, in the stream's own time base.
70 sample: u64,70 sample: u64,
71 rate: u32,71 rate: u32,
72- /// `STREAMINFO`, needed to read a FLAC frame header.
73- flac: Option<ec_flac::StreamInfo>,
74 /// `(first sample, offset)` per frame, built on the first seek.72 /// `(first sample, offset)` per frame, built on the first seek.
75 index: Vec<(u64, u64)>,73 index: Vec<(u64, u64)>,
76 indexed: bool,74 indexed: bool,
@@ -84,7 +82,6 @@ impl<R: Read + Seek> RawDemuxer<R> {
84 start: u64,82 start: u64,
85 end: u64,83 end: u64,
86 total_samples: Option<u64>,84 total_samples: Option<u64>,
87- flac: Option<ec_flac::StreamInfo>,
88 ) -> Result<RawDemuxer<R>> {85 ) -> Result<RawDemuxer<R>> {
89 let rate = params86 let rate = params
90 .audio()87 .audio()
@@ -107,7 +104,6 @@ impl<R: Read + Seek> RawDemuxer<R> {
107 head: 0,104 head: 0,
108 sample: 0,105 sample: 0,
109 rate,106 rate,
110- flac,
111 index: Vec::new(),107 index: Vec::new(),
112 indexed: false,108 indexed: false,
113 })109 })
@@ -122,14 +118,11 @@ impl<R: Read + Seek> RawDemuxer<R> {
122 118
123 /// Samples in a typical frame of this format, for the tail of an estimate.119 /// Samples in a typical frame of this format, for the tail of an estimate.
124 fn frame_samples(&self) -> u64 {120 fn frame_samples(&self) -> u64 {
125- match self.kind {121+ match &self.kind {
126 Kind::Wav { .. } => WAV_FRAMES as u64,122 Kind::Wav { .. } => WAV_FRAMES as u64,
127- Kind::WavBlocks { samples, .. } => samples,123+ Kind::WavBlocks { samples, .. } => *samples,
128 Kind::Mp3 => 1152,124 Kind::Mp3 => 1152,
129- Kind::Flac => self125+ Kind::Flac(info) => u64::from(info.max_block_size),
130- .flac
131- .as_ref()
132- .map_or(4096, |i| u64::from(i.max_block_size)),
133 Kind::Adts => 1024,126 Kind::Adts => 1024,
134 Kind::Dts => 512,127 Kind::Dts => 512,
135 }128 }
@@ -182,16 +175,15 @@ impl<R: Read + Seek> RawDemuxer<R> {
182 /// is what carries a reader over an ID3 tag in the middle of a stream or a175 /// is what carries a reader over an ID3 tag in the middle of a stream or a
183 /// scrap of a torn download.176 /// scrap of a torn download.
184 fn framing(&mut self) -> Result<Framing> {177 fn framing(&mut self) -> Result<Framing> {
185- let want = match self.kind {178+ let want = match &self.kind {
186 Kind::Wav { block_align } => block_align * WAV_FRAMES,179 Kind::Wav { block_align } => block_align * WAV_FRAMES,
187- Kind::WavBlocks { block, .. } => block,180+ Kind::WavBlocks { block, .. } => *block,
188 // A FLAC frame is bounded by the *next* sync word, so the window181 // A FLAC frame is bounded by the *next* sync word, so the window
189 // has to hold a whole one plus the head of its successor.182 // has to hold a whole one plus the head of its successor.
190- Kind::Flac => 1 << 20,183+ Kind::Flac(_) => 1 << 20,
191 _ => 8192,184 _ => 8192,
192 };185 };
193- let flac = self.flac.clone();186+ let kind = self.kind.clone();
194- let kind = self.kind;
195 let mut skipped = 0u64;187 let mut skipped = 0u64;
196 loop {188 loop {
197 self.fill(want)?;189 self.fill(want)?;
@@ -200,7 +192,7 @@ impl<R: Read + Seek> RawDemuxer<R> {
200 return Err(Error::Eof);192 return Err(Error::Eof);
201 }193 }
202 let tail = self.read_at >= self.end;194 let tail = self.read_at >= self.end;
203- match frame_at(kind, self.window(), flac.as_ref()) {195+ match frame_at(&kind, self.window()) {
204 Ok(f) => return Ok(f),196 Ok(f) => return Ok(f),
205 Err(e) if e.is_need_more() && tail => {197 Err(e) if e.is_need_more() && tail => {
206 // The tail of a file: whatever is left is the last frame.198 // The tail of a file: whatever is left is the last frame.
@@ -249,7 +241,7 @@ impl<R: Read + Seek> RawDemuxer<R> {
249 }241 }
250 242
251 /// One frame's geometry, read from the head of `data`.243 /// 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> {
253 match kind {245 match kind {
254 Kind::Wav { block_align } => {246 Kind::Wav { block_align } => {
255 let frames = (data.len() / block_align).min(WAV_FRAMES);247 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
261 }),253 }),
262 }254 }
263 }255 }
264- Kind::WavBlocks { block, samples } => match data.len() >= block && block > 0 {256+ Kind::WavBlocks { block, samples } => match data.len() >= *block && *block > 0 {
265 true => Ok(Framing {257 true => Ok(Framing {
266- bytes: block,258+ bytes: *block,
267- samples,259+ samples: *samples,
268 }),260 }),
269 false => Err(Error::Eof),261 false => Err(Error::Eof),
270 },262 },
@@ -307,7 +299,8 @@ fn frame_at(kind: Kind, data: &[u8], flac: Option<&ec_flac::StreamInfo>) -> Resu
307 samples: u64::from(core.samples),299 samples: u64::from(core.samples),
308 })300 })
309 }301 }
310- Kind::Flac => {302+ Kind::Flac(info) => {
303+ let flac = Some(info);
311 if ec_flac::decode::find_frame_sync(data, 0, flac) != Some(0) {304 if ec_flac::decode::find_frame_sync(data, 0, flac) != Some(0) {
312 return Err(Error::corrupt("flac: not a frame header"));305 return Err(Error::corrupt("flac: not a frame header"));
313 }306 }
crates/ec-probe/src/raw_open.rsmodified+2 -6
@@ -52,7 +52,6 @@ fn open_dts_span<R: Read + Seek + Send + 'static>(
52 start,52 start,
53 end,53 end,
54 total,54 total,
55- None,
56 )?;55 )?;
57 Ok(Reader::single(format, demuxer, Tags::default()))56 Ok(Reader::single(format, demuxer, Tags::default()))
58 }57 }
@@ -78,7 +77,7 @@ pub(crate) fn open_wav<R: Read + Seek + Send + 'static>(mut src: R, end: u64) ->
78 .unwrap_or(end)77 .unwrap_or(end)
79 .min(end);78 .min(end);
80 let total = frames.or(Some((audio_end - start) / bytes_per.0 * bytes_per.1));79 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)?;
82 return Ok(Reader::single(Format::Wav, demuxer, Tags::default()));81 return Ok(Reader::single(Format::Wav, demuxer, Tags::default()));
83 }82 }
84 let (params, block_align) = raw::wav_parameters(spec)?;83 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) ->
96 start,95 start,
97 audio_end,96 audio_end,
98 total,97 total,
99- None,
100 )?;98 )?;
101 Ok(Reader::single(Format::Wav, demuxer, Tags::default()))99 Ok(Reader::single(Format::Wav, demuxer, Tags::default()))
102 }100 }
@@ -139,7 +137,7 @@ pub(crate) fn open_flac<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8]
139 0 => None,137 0 => None,
140 n => Some(n),138 n => Some(n),
141 };139 };
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)?;
143 Ok(Reader::single(Format::Flac, demuxer, tags))141 Ok(Reader::single(Format::Flac, demuxer, tags))
144 }142 }
145 143
@@ -192,7 +190,6 @@ pub(crate) fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8],
192 start,190 start,
193 audio_end,191 audio_end,
194 total,192 total,
195- None,
196 )?193 )?
197 .with_initial_padding(padding);194 .with_initial_padding(padding);
198 Ok(Reader::single(Format::Mp3, demuxer, tags))195 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]
234 start,231 start,
235 audio_end,232 audio_end,
236 total,233 total,
237- None,
238 )?;234 )?;
239 Ok(Reader::single(Format::Adts, demuxer, tags))235 Ok(Reader::single(Format::Adts, demuxer, tags))
240 }236 }