S10: mp3 FrameHeader.layer is a Layer enum (I, II, III) instead of a 1..=3 u8; the layer matches are exhaustive; same bitstream and layer values
Browse files at this commit1ac57b37d79dbc68d425f397a9bc9625b25cd8e3Parent: a06b46b5 changed files +51 -39
- crates/ec-mp3/src/decode.rs +3 -3
- crates/ec-mp3/src/encode.rs +2 -2
- crates/ec-mp3/src/header.rs +33 -22
- crates/ec-mp3/src/layer12.rs +2 -2
- crates/ec-mp3/tests/mp2_matrix.rs +11 -10
crates/ec-mp3/src/decode.rsmodified+3 -3
| @@ -2,7 +2,7 @@ | |||
| 2 | //! filterbank that turns 576 coefficients back into PCM. | 2 | //! filterbank that turns 576 coefficients back into PCM. |
| 3 | 3 | ||
| 4 | use crate::filterbank::{Imdct, Synthesis, alias_reduce}; | 4 | use crate::filterbank::{Imdct, Synthesis, alias_reduce}; |
| 5 | -use crate::header::{ChannelMode, FrameHeader, Version, crc16}; | 5 | +use crate::header::{ChannelMode, FrameHeader, Layer, Version, crc16}; |
| 6 | use crate::huffman; | 6 | use crate::huffman; |
| 7 | use crate::layer12::Layer12; | 7 | use crate::layer12::Layer12; |
| 8 | use crate::tables::{ | 8 | use crate::tables::{ |
| @@ -215,7 +215,7 @@ impl Mp3Decode { | |||
| 215 | if frame.len() < frame_len { | 215 | if frame.len() < frame_len { |
| 216 | return Err(Error::NeedMore); | 216 | return Err(Error::NeedMore); |
| 217 | } | 217 | } |
| 218 | - if header.layer != 3 { | 218 | + if header.layer != Layer::III { |
| 219 | let samples = self.layer12.decode(&header, &frame[..frame_len])?; | 219 | let samples = self.layer12.decode(&header, &frame[..frame_len])?; |
| 220 | return Ok(DecodedFrame { | 220 | return Ok(DecodedFrame { |
| 221 | sample_rate: header.sample_rate, | 221 | sample_rate: header.sample_rate, |
| @@ -759,7 +759,7 @@ impl Mp3Reader { | |||
| 759 | return Err(Error::NeedMore); | 759 | return Err(Error::NeedMore); |
| 760 | } | 760 | } |
| 761 | let frame = &data[..frame_len]; | 761 | let frame = &data[..frame_len]; |
| 762 | - if !self.started && header.layer == 3 && is_info_frame(&header, frame) { | 762 | + if !self.started && header.layer == Layer::III && is_info_frame(&header, frame) { |
| 763 | self.started = true; | 763 | self.started = true; |
| 764 | self.consumed += frame_len; | 764 | self.consumed += frame_len; |
| 765 | continue; | 765 | continue; |
crates/ec-mp3/src/encode.rsmodified+2 -2
| @@ -18,7 +18,7 @@ | |||
| 18 | //! makes available. | 18 | //! makes available. |
| 19 | 19 | ||
| 20 | use crate::filterbank::{Analysis, alias_expand}; | 20 | use crate::filterbank::{Analysis, alias_expand}; |
| 21 | -use crate::header::{ChannelMode, FrameHeader, Version}; | 21 | +use crate::header::{ChannelMode, FrameHeader, Layer, Version}; |
| 22 | use crate::huffman::{self, BitBuf}; | 22 | use crate::huffman::{self, BitBuf}; |
| 23 | use crate::tables::{ | 23 | use crate::tables::{ |
| 24 | MAX_QUANT, SLEN, forward_mdct_long, forward_mdct_short, long_starts, power43, short_starts, | 24 | MAX_QUANT, SLEN, forward_mdct_long, forward_mdct_short, long_starts, power43, short_starts, |
| @@ -251,7 +251,7 @@ impl Mp3Encode { | |||
| 251 | fn header(&self, bitrate_kbps: u32, padding: bool) -> FrameHeader { | 251 | fn header(&self, bitrate_kbps: u32, padding: bool) -> FrameHeader { |
| 252 | FrameHeader { | 252 | FrameHeader { |
| 253 | version: self.version, | 253 | version: self.version, |
| 254 | - layer: 3, | 254 | + layer: Layer::III, |
| 255 | crc: false, | 255 | crc: false, |
| 256 | bitrate_kbps, | 256 | bitrate_kbps, |
| 257 | sample_rate: self.sample_rate, | 257 | sample_rate: self.sample_rate, |
crates/ec-mp3/src/header.rsmodified+33 -22
| @@ -15,6 +15,17 @@ pub enum Version { | |||
| 15 | Mpeg25, | 15 | Mpeg25, |
| 16 | } | 16 | } |
| 17 | 17 | ||
| 18 | +/// The MPEG audio layer a frame is coded with. | ||
| 19 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| 20 | +pub enum Layer { | ||
| 21 | + /// Layer I: 384 samples per frame. | ||
| 22 | + I, | ||
| 23 | + /// Layer II: 1152 samples per frame. | ||
| 24 | + II, | ||
| 25 | + /// Layer III: 576 samples per granule, one or two granules per frame. | ||
| 26 | + III, | ||
| 27 | +} | ||
| 28 | + | ||
| 18 | /// The channel arrangement a frame declares. | 29 | /// The channel arrangement a frame declares. |
| 19 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | 30 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 20 | pub enum ChannelMode { | 31 | pub enum ChannelMode { |
| @@ -55,13 +66,13 @@ const BITRATES_L2_V1: [u32; 15] = [ | |||
| 55 | /// The bitrate table for a layer of a version: Layer III and Layer II of the | 66 | /// The bitrate table for a layer of a version: Layer III and Layer II of the |
| 56 | /// low sampling frequencies share one, the other four combinations each have | 67 | /// low sampling frequencies share one, the other four combinations each have |
| 57 | /// their own. | 68 | /// their own. |
| 58 | -fn bitrate_table(version: Version, layer: u8) -> &'static [u32; 15] { | 69 | +fn bitrate_table(version: Version, layer: Layer) -> &'static [u32; 15] { |
| 59 | match (version == Version::Mpeg1, layer) { | 70 | match (version == Version::Mpeg1, layer) { |
| 60 | - (true, 1) => &BITRATES_L1_V1, | 71 | + (true, Layer::I) => &BITRATES_L1_V1, |
| 61 | - (true, 2) => &BITRATES_L2_V1, | 72 | + (true, Layer::II) => &BITRATES_L2_V1, |
| 62 | - (true, _) => &BITRATES_V1, | 73 | + (true, Layer::III) => &BITRATES_V1, |
| 63 | - (false, 1) => &BITRATES_L1_V2, | 74 | + (false, Layer::I) => &BITRATES_L1_V2, |
| 64 | - (false, _) => &BITRATES_V2, | 75 | + (false, Layer::II | Layer::III) => &BITRATES_V2, |
| 65 | } | 76 | } |
| 66 | } | 77 | } |
| 67 | const RATES_V1: [u32; 3] = [44100, 48000, 32000]; | 78 | const RATES_V1: [u32; 3] = [44100, 48000, 32000]; |
| @@ -71,8 +82,8 @@ const RATES_V1: [u32; 3] = [44100, 48000, 32000]; | |||
| 71 | pub struct FrameHeader { | 82 | pub struct FrameHeader { |
| 72 | /// MPEG generation. | 83 | /// MPEG generation. |
| 73 | pub version: Version, | 84 | pub version: Version, |
| 74 | - /// Layer number, 1..=3. | 85 | + /// The MPEG audio layer. |
| 75 | - pub layer: u8, | 86 | + pub layer: Layer, |
| 76 | /// True when a 16-bit CRC follows the header. | 87 | /// True when a 16-bit CRC follows the header. |
| 77 | pub crc: bool, | 88 | pub crc: bool, |
| 78 | /// Declared bitrate in kbit/s; 0 means free format. | 89 | /// Declared bitrate in kbit/s; 0 means free format. |
| @@ -116,9 +127,9 @@ impl FrameHeader { | |||
| 116 | _ => return Err(Error::corrupt("mp3: reserved MPEG version id")), | 127 | _ => return Err(Error::corrupt("mp3: reserved MPEG version id")), |
| 117 | }; | 128 | }; |
| 118 | let layer = match (b[1] >> 1) & 3 { | 129 | let layer = match (b[1] >> 1) & 3 { |
| 119 | - 1 => 3, | 130 | + 1 => Layer::III, |
| 120 | - 2 => 2, | 131 | + 2 => Layer::II, |
| 121 | - 3 => 1, | 132 | + 3 => Layer::I, |
| 122 | _ => return Err(Error::corrupt("mp3: reserved layer id")), | 133 | _ => return Err(Error::corrupt("mp3: reserved layer id")), |
| 123 | }; | 134 | }; |
| 124 | let crc = (b[1] & 1) == 0; | 135 | let crc = (b[1] & 1) == 0; |
| @@ -166,9 +177,9 @@ impl FrameHeader { | |||
| 166 | Version::Mpeg1 => 3, | 177 | Version::Mpeg1 => 3, |
| 167 | }; | 178 | }; |
| 168 | let layer_id = match self.layer { | 179 | let layer_id = match self.layer { |
| 169 | - 3 => 1, | 180 | + Layer::III => 1, |
| 170 | - 2 => 2, | 181 | + Layer::II => 2, |
| 171 | - _ => 3, | 182 | + Layer::I => 3, |
| 172 | }; | 183 | }; |
| 173 | let bitrate_index = bitrate_table(self.version, self.layer) | 184 | let bitrate_index = bitrate_table(self.version, self.layer) |
| 174 | .iter() | 185 | .iter() |
| @@ -215,9 +226,9 @@ impl FrameHeader { | |||
| 215 | /// PCM samples per channel produced by this frame. | 226 | /// PCM samples per channel produced by this frame. |
| 216 | pub fn samples_per_frame(&self) -> usize { | 227 | pub fn samples_per_frame(&self) -> usize { |
| 217 | match self.layer { | 228 | match self.layer { |
| 218 | - 1 => 384, | 229 | + Layer::I => 384, |
| 219 | - 2 => 1152, | 230 | + Layer::II => 1152, |
| 220 | - _ => self.granules() * 576, | 231 | + Layer::III => self.granules() * 576, |
| 221 | } | 232 | } |
| 222 | } | 233 | } |
| 223 | 234 | ||
| @@ -241,10 +252,10 @@ impl FrameHeader { | |||
| 241 | let rate = self.sample_rate as usize; | 252 | let rate = self.sample_rate as usize; |
| 242 | let pad = usize::from(self.padding); | 253 | let pad = usize::from(self.padding); |
| 243 | Some(match self.layer { | 254 | Some(match self.layer { |
| 244 | - 1 => (12 * bits / rate + pad) * 4, | 255 | + Layer::I => (12 * bits / rate + pad) * 4, |
| 245 | - 2 => 144 * bits / rate + pad, | 256 | + Layer::II => 144 * bits / rate + pad, |
| 246 | - _ if self.version == Version::Mpeg1 => 144 * bits / rate + pad, | 257 | + Layer::III if self.version == Version::Mpeg1 => 144 * bits / rate + pad, |
| 247 | - _ => 72 * bits / rate + pad, | 258 | + Layer::III => 72 * bits / rate + pad, |
| 248 | }) | 259 | }) |
| 249 | } | 260 | } |
| 250 | } | 261 | } |
| @@ -298,7 +309,7 @@ mod tests { | |||
| 298 | let bytes = [0xFF, 0xFB, 0x90, 0x00]; | 309 | let bytes = [0xFF, 0xFB, 0x90, 0x00]; |
| 299 | let h = FrameHeader::parse(&bytes).unwrap(); | 310 | let h = FrameHeader::parse(&bytes).unwrap(); |
| 300 | assert_eq!(h.version, Version::Mpeg1); | 311 | assert_eq!(h.version, Version::Mpeg1); |
| 301 | - assert_eq!(h.layer, 3); | 312 | + assert_eq!(h.layer, Layer::III); |
| 302 | assert_eq!(h.bitrate_kbps, 128); | 313 | assert_eq!(h.bitrate_kbps, 128); |
| 303 | assert_eq!(h.sample_rate, 44100); | 314 | assert_eq!(h.sample_rate, 44100); |
| 304 | assert_eq!(h.mode, ChannelMode::Stereo); | 315 | assert_eq!(h.mode, ChannelMode::Stereo); |
crates/ec-mp3/src/layer12.rsmodified+2 -2
| @@ -7,7 +7,7 @@ | |||
| 7 | //! copied: `scripts/mp2-tables/measure.py` sets one allocation code at a time and | 7 | //! copied: `scripts/mp2-tables/measure.py` sets one allocation code at a time and |
| 8 | //! reads the quantiser size back from the decoded level. | 8 | //! reads the quantiser size back from the decoded level. |
| 9 | 9 | ||
| 10 | -use crate::header::{ChannelMode, FrameHeader, Version, crc16_bits}; | 10 | +use crate::header::{ChannelMode, FrameHeader, Layer, Version, crc16_bits}; |
| 11 | use crate::window::WINDOW; | 11 | use crate::window::WINDOW; |
| 12 | use ec_core::bitio::BitReader; | 12 | use ec_core::bitio::BitReader; |
| 13 | use ec_core::error::{Error, Result}; | 13 | use ec_core::error::{Error, Result}; |
| @@ -188,7 +188,7 @@ impl Layer12 { | |||
| 188 | let body = &frame[4 + usize::from(header.crc) * 2..]; | 188 | let body = &frame[4 + usize::from(header.crc) * 2..]; |
| 189 | let mut r = BitReader::new(body); | 189 | let mut r = BitReader::new(body); |
| 190 | let mut subband = vec![[[0.0f32; 32]; 2]; 36]; | 190 | let mut subband = vec![[[0.0f32; 32]; 2]; 36]; |
| 191 | - let slots = if header.layer == 1 { | 191 | + let slots = if header.layer == Layer::I { |
| 192 | self.read_layer1(header, frame, &mut r, &mut subband)?; | 192 | self.read_layer1(header, frame, &mut r, &mut subband)?; |
| 193 | 12 | 193 | 12 |
| 194 | } else { | 194 | } else { |
crates/ec-mp3/tests/mp2_matrix.rsmodified+11 -10
| @@ -10,6 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | use std::path::PathBuf; | 11 | use std::path::PathBuf; |
| 12 | 12 | ||
| 13 | +use ec_mp3::header::Layer; | ||
| 13 | use ec_mp3::{FrameHeader, Mp3Reader}; | 14 | use ec_mp3::{FrameHeader, Mp3Reader}; |
| 14 | 15 | ||
| 15 | fn work_dir() -> PathBuf { | 16 | fn work_dir() -> PathBuf { |
| @@ -18,8 +19,8 @@ fn work_dir() -> PathBuf { | |||
| 18 | d | 19 | d |
| 19 | } | 20 | } |
| 20 | 21 | ||
| 21 | -fn ffmpeg_decode(path: &std::path::Path, channels: usize, layer: u8) -> Vec<f32> { | 22 | +fn ffmpeg_decode(path: &std::path::Path, channels: usize, layer: Layer) -> Vec<f32> { |
| 22 | - let decoder = if layer == 1 { "mp1float" } else { "mp2float" }; | 23 | + let decoder = if layer == Layer::I { "mp1float" } else { "mp2float" }; |
| 23 | let out = ec_testkit::ffmpeg() | 24 | let out = ec_testkit::ffmpeg() |
| 24 | .args(["-v", "error", "-c:a", decoder, "-i"]) | 25 | .args(["-v", "error", "-c:a", decoder, "-i"]) |
| 25 | .arg(path) | 26 | .arg(path) |
| @@ -40,8 +41,8 @@ fn ffmpeg_decode(path: &std::path::Path, channels: usize, layer: u8) -> Vec<f32> | |||
| 40 | } | 41 | } |
| 41 | 42 | ||
| 42 | /// ffmpeg's fixed-point decoder (the default `mp2`/`mp1`): s16 out. | 43 | /// ffmpeg's fixed-point decoder (the default `mp2`/`mp1`): s16 out. |
| 43 | -fn ffmpeg_decode_s16(path: &std::path::Path, channels: usize, layer: u8) -> Vec<i16> { | 44 | +fn ffmpeg_decode_s16(path: &std::path::Path, channels: usize, layer: Layer) -> Vec<i16> { |
| 44 | - let decoder = if layer == 1 { "mp1" } else { "mp2" }; | 45 | + let decoder = if layer == Layer::I { "mp1" } else { "mp2" }; |
| 45 | let out = ec_testkit::ffmpeg() | 46 | let out = ec_testkit::ffmpeg() |
| 46 | .args(["-v", "error", "-c:a", decoder, "-i"]) | 47 | .args(["-v", "error", "-c:a", decoder, "-i"]) |
| 47 | .arg(path) | 48 | .arg(path) |
| @@ -192,7 +193,7 @@ fn structured_frame(rng: &mut Rng, h: &FrameHeader, density: u32) -> Option<Vec< | |||
| 192 | let rnd = |rng: &mut Rng, n: u32| (rng.next() >> 20) as u32 % n.max(1); | 193 | let rnd = |rng: &mut Rng, n: u32| (rng.next() >> 20) as u32 % n.max(1); |
| 193 | let (nb_sb, bound); | 194 | let (nb_sb, bound); |
| 194 | let rows: Vec<Vec<u32>>; | 195 | let rows: Vec<Vec<u32>>; |
| 195 | - if h.layer == 1 { | 196 | + if h.layer == Layer::I { |
| 196 | nb_sb = 32; | 197 | nb_sb = 32; |
| 197 | rows = Vec::new(); | 198 | rows = Vec::new(); |
| 198 | } else { | 199 | } else { |
| @@ -207,14 +208,14 @@ fn structured_frame(rng: &mut Rng, h: &FrameHeader, density: u32) -> Option<Vec< | |||
| 207 | // allocation codes | 208 | // allocation codes |
| 208 | let mut alloc = vec![vec![0u32; nb_sb]; 2]; | 209 | let mut alloc = vec![vec![0u32; nb_sb]; 2]; |
| 209 | let width = |sb: usize| -> u32 { | 210 | let width = |sb: usize| -> u32 { |
| 210 | - if h.layer == 1 { | 211 | + if h.layer == Layer::I { |
| 211 | 4 | 212 | 4 |
| 212 | } else { | 213 | } else { |
| 213 | rows[sb].len().trailing_zeros() | 214 | rows[sb].len().trailing_zeros() |
| 214 | } | 215 | } |
| 215 | }; | 216 | }; |
| 216 | for sb in 0..nb_sb { | 217 | for sb in 0..nb_sb { |
| 217 | - let maxcode = if h.layer == 1 { | 218 | + let maxcode = if h.layer == Layer::I { |
| 218 | 15 | 219 | 15 |
| 219 | } else { | 220 | } else { |
| 220 | rows[sb].len() as u32 | 221 | rows[sb].len() as u32 |
| @@ -247,7 +248,7 @@ fn structured_frame(rng: &mut Rng, h: &FrameHeader, density: u32) -> Option<Vec< | |||
| 247 | } | 248 | } |
| 248 | } | 249 | } |
| 249 | let mut scfsi = vec![vec![0u32; nb_sb]; 2]; | 250 | let mut scfsi = vec![vec![0u32; nb_sb]; 2]; |
| 250 | - if h.layer == 2 { | 251 | + if h.layer == Layer::II { |
| 251 | for sb in 0..nb_sb { | 252 | for sb in 0..nb_sb { |
| 252 | for ch in 0..channels { | 253 | for ch in 0..channels { |
| 253 | if alloc[ch][sb] != 0 { | 254 | if alloc[ch][sb] != 0 { |
| @@ -264,7 +265,7 @@ fn structured_frame(rng: &mut Rng, h: &FrameHeader, density: u32) -> Option<Vec< | |||
| 264 | if alloc[ch][sb] == 0 { | 265 | if alloc[ch][sb] == 0 { |
| 265 | continue; | 266 | continue; |
| 266 | } | 267 | } |
| 267 | - let n = if h.layer == 1 { | 268 | + let n = if h.layer == Layer::I { |
| 268 | 1 | 269 | 1 |
| 269 | } else { | 270 | } else { |
| 270 | match scfsi[ch][sb] { | 271 | match scfsi[ch][sb] { |
| @@ -287,7 +288,7 @@ fn structured_frame(rng: &mut Rng, h: &FrameHeader, density: u32) -> Option<Vec< | |||
| 287 | if a == 0 { | 288 | if a == 0 { |
| 288 | continue; | 289 | continue; |
| 289 | } | 290 | } |
| 290 | - if h.layer == 1 { | 291 | + if h.layer == Layer::I { |
| 291 | let nb = a + 1; | 292 | let nb = a + 1; |
| 292 | w.put(rnd(rng, (1 << nb) - 1), nb); | 293 | w.put(rnd(rng, (1 << nb) - 1), nb); |
| 293 | } else { | 294 | } else { |