forge
Explore
Log in

Don't you dare go Hollow

B6: ec-ogg identify rejects Vorbis/FLAC rate 0, drop silent 48 kHz time base fallback

Browse files at this commit

b4ff43f4ff05e84610065978c1e66af1d07f839bAhmet Kaan GümüşParent: 22f0f3f
Rate 0 in a Vorbis identification or FLAC STREAMINFO header is invalid;
the old code kept the stream and silently timed it at 48 kHz. Now identify
returns None (stream not claimed), and time_base uses the stated rate
directly since Mapping can only be built from a non-zero rate.
Regression: identify on a Vorbis header with rate 0 returns None.
Golden: only affects streams with rate 0 (none in goldens).
crates/ec-ogg/src/mapping.rsmodified+7 -6
@@ -48,8 +48,9 @@ impl Mapping {
48 /// a private mapping — all valid Ogg, none of them ours).48 /// a private mapping — all valid Ogg, none of them ours).
49 pub fn identify(bos: &[u8]) -> Option<Mapping> {49 pub fn identify(bos: &[u8]) -> Option<Mapping> {
50 if bos.len() >= 16 && bos[0] == 1 && &bos[1..7] == b"vorbis" {50 if bos.len() >= 16 && bos[0] == 1 && &bos[1..7] == b"vorbis" {
51- return Some(Mapping::Vorbis {51+ let rate = u32::from_le_bytes(bos[12..16].try_into().ok()?);
52- rate: u32::from_le_bytes(bos[12..16].try_into().ok()?),52+ return (rate != 0).then_some(Mapping::Vorbis {
53+ rate,
53 channels: bos[11],54 channels: bos[11],
54 });55 });
55 }56 }
@@ -67,7 +68,7 @@ impl Mapping {
67 let rate =68 let rate =
68 (u32::from(si[10]) << 12) | (u32::from(si[11]) << 4) | u32::from(si[12] >> 4);69 (u32::from(si[10]) << 12) | (u32::from(si[11]) << 4) | u32::from(si[12] >> 4);
69 let declared = usize::from(u16::from_be_bytes([bos[7], bos[8]]));70 let declared = usize::from(u16::from_be_bytes([bos[7], bos[8]]));
70- return Some(Mapping::Flac {71+ return (rate != 0).then_some(Mapping::Flac {
71 rate,72 rate,
72 channels: ((si[12] >> 1) & 0x07) + 1,73 channels: ((si[12] >> 1) & 0x07) + 1,
73 // A zero here means "not stated"; the comment header still74 // A zero here means "not stated"; the comment header still
@@ -92,9 +93,7 @@ impl Mapping {
92 match self {93 match self {
93 // Opus granules are always 48 kHz, whatever the encoder was fed.94 // Opus granules are always 48 kHz, whatever the encoder was fed.
94 Mapping::Opus { .. } => TimeBase::from_rate(48_000),95 Mapping::Opus { .. } => TimeBase::from_rate(48_000),
95- Mapping::Vorbis { rate, .. } | Mapping::Flac { rate, .. } => {96+ Mapping::Vorbis { rate, .. } | Mapping::Flac { rate, .. } => TimeBase::from_rate(*rate),
96- TimeBase::try_new(1, i64::from(*rate)).unwrap_or(TimeBase::from_rate(48_000))
97- }
98 }97 }
99 }98 }
100 99
@@ -211,6 +210,8 @@ mod tests {
211 assert_eq!(mapping.layout(), ChannelLayout::Surround5_1);210 assert_eq!(mapping.layout(), ChannelLayout::Surround5_1);
212 assert_eq!(mapping.header_packets(), 2);211 assert_eq!(mapping.header_packets(), 2);
213 212
213+ vorbis[12..16].fill(0);
214+ assert_eq!(Mapping::identify(&vorbis), None);
214 assert_eq!(Mapping::identify(b"not a header at all"), None);215 assert_eq!(Mapping::identify(b"not a header at all"), None);
215 assert_eq!(Mapping::identify(&[]), None);216 assert_eq!(Mapping::identify(&[]), None);
216 }217 }