pub fn frame_block_size(frame: &[u8]) -> Option<u32> {
if frame.len() < 5 || frame[0] != 0xFF || frame[1] & 0xFE != 0xF8 {
return None;
}
let code = frame[2] >> 4;
let lead = frame[4].leading_ones() as usize;
let num_len = match lead {
0 => 1,
2..=7 => lead,
_ => return None,
};
let at = 4 + num_len;
match code {
1 => Some(192),
2..=5 => Some(576 << (code - 2)),
6 => frame.get(at).map(|&b| u32::from(b) + 1),
7 => frame
.get(at..at + 2)
.map(|b| u32::from(u16::from_be_bytes([b[0], b[1]])) + 1),
8..=15 => Some(256 << (code - 8)),
_ => None,
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FlacStats {
pub frames: u64,
pub total_samples: u64,
pub min_frame: u32,
pub max_frame: u32,
pub min_block: u32,
pub max_block: u32,
last_block: u32,
pub unparsed: bool,
}
impl FlacStats {
pub fn push(&mut self, frame: &[u8]) {
let size = u32::try_from(frame.len()).unwrap_or(u32::MAX);
let Some(block) = frame_block_size(frame) else {
self.unparsed = true;
return;
};
if self.frames > 0 {
self.min_block = match self.min_block {
0 => self.last_block,
m => m.min(self.last_block),
};
}
self.frames += 1;
self.total_samples += u64::from(block);
self.max_block = self.max_block.max(block);
self.last_block = block;
self.min_frame = if self.frames == 1 {
size
} else {
self.min_frame.min(size)
};
self.max_frame = self.max_frame.max(size);
}
pub fn rewrite(&self, streaminfo: &[u8; 34]) -> [u8; 34] {
let mut si = *streaminfo;
if self.frames == 0 || self.unparsed {
return si;
}
let min_block = if self.frames == 1 {
self.last_block
} else {
self.min_block
};
let old_total =
u64::from_be_bytes([0, 0, 0, si[13] & 0x0F, si[14], si[15], si[16], si[17]]);
si[0..2].copy_from_slice(&(min_block.min(0xFFFF) as u16).to_be_bytes());
si[2..4].copy_from_slice(&(self.max_block.min(0xFFFF) as u16).to_be_bytes());
si[4..7].copy_from_slice(&self.min_frame.min(0xFF_FFFF).to_be_bytes()[1..]);
si[7..10].copy_from_slice(&self.max_frame.min(0xFF_FFFF).to_be_bytes()[1..]);
let total = self.total_samples.min((1 << 36) - 1);
si[13] = (si[13] & 0xF0) | (total >> 32) as u8;
si[14..18].copy_from_slice(&(total as u32).to_be_bytes());
if old_total != total {
si[18..34].fill(0);
}
si
}
}
#[cfg(test)]
mod tests {
use super::*;
fn frame(code: u8, number: &[u8], extra: &[u8], len: usize) -> Vec<u8> {
let mut f = vec![0xFF, 0xF8, code << 4 | 9, 0x08];
f.extend_from_slice(number);
f.extend_from_slice(extra);
f.resize(len, 0);
f
}
#[test]
fn block_sizes_from_every_code() {
assert_eq!(frame_block_size(&frame(12, &[0], &[], 16)), Some(4096));
assert_eq!(frame_block_size(&frame(1, &[5], &[], 16)), Some(192));
assert_eq!(frame_block_size(&frame(3, &[5], &[], 16)), Some(1152));
assert_eq!(
frame_block_size(&frame(6, &[0xC2, 0x80], &[99], 16)),
Some(100)
);
assert_eq!(
frame_block_size(&frame(7, &[1], &[0x01, 0x00], 16)),
Some(257)
);
assert_eq!(frame_block_size(b"not flac"), None);
}
#[test]
fn streaminfo_is_rewritten_from_the_frames() {
let mut st = FlacStats::default();
for (i, len) in [(0u8, 900usize), (1, 1200), (2, 700)] {
st.push(&frame(12, &[i], &[], len));
}
st.push(&frame(7, &[3], &[0x03, 0xE7], 300)); let mut si = [0u8; 34];
si[10..14].copy_from_slice(&[0x0B, 0xB8, 0x03, 0x70]); si[18..].fill(0xAB);
let out = st.rewrite(&si);
assert_eq!(u16::from_be_bytes([out[0], out[1]]), 4096); assert_eq!(u16::from_be_bytes([out[2], out[3]]), 4096);
assert_eq!(&out[4..7], &[0, 0x01, 0x2C]); assert_eq!(&out[7..10], &[0, 0x04, 0xB0]); assert_eq!(&out[10..13], &si[10..13]);
assert_eq!(out[13] >> 4, si[13] >> 4);
assert_eq!(
u32::from_be_bytes([out[14], out[15], out[16], out[17]]),
3 * 4096 + 1000
);
assert_eq!(&out[18..], &[0u8; 16], "a different total: MD5 unknown");
let mut same = out;
same[18..].fill(0xCD);
assert_eq!(&st.rewrite(&same)[18..], &[0xCD; 16]);
}
}