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