From ef4aae1a5bf326abd82f05358db458c16ef35f44 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 23 Nov 2026 14:45:21 +0300 Subject: [PATCH] hle/opus: honor Ogg Opus EOS granule trimming and pre-skip --- crates/xcl/src/hle/opus.rs | 452 ++++++++++++++++++++++++++++++++----- 1 file changed, 393 insertions(+), 59 deletions(-) diff --git a/crates/xcl/src/hle/opus.rs b/crates/xcl/src/hle/opus.rs index 523666a..c2d1018 100644 --- a/crates/xcl/src/hle/opus.rs +++ b/crates/xcl/src/hle/opus.rs @@ -532,58 +532,147 @@ fn try_length_prefixed_block<'a>(block: &'a [u8], multistream: bool) -> Option<( Some((packets, zero_padded)) } -/// Extract Opus audio packets from an Ogg Opus stream (skipping `OpusHead` and `OpusTags`). -fn extract_ogg_opus<'a>(data: &'a [u8], multistream: bool) -> Option>> { +struct OggTiming { + head: OpusHead, + decoded_48k: u64, + end_48k: u64, +} + +impl OggTiming { + fn pcm_range(&self, rate: u32) -> std::ops::Range { + let start_48k = u64::from(self.head.pre_skip).min(self.end_48k); + frames_at_rate(start_48k, rate)..frames_at_rate(self.end_48k, rate) + } + + fn trim_pcm(&self, pcm: &mut Vec, rate: u32, channels: usize) -> bool { + // The MFT can defer output. Only slice PCM when the complete decoded timeline + // is present, so a short result is never mistaken for tail padding. + if pcm.len() % channels != 0 + || (pcm.len() / channels) as u64 != frames_at_rate(self.decoded_48k, rate) + { + return false; + } + let range = self.pcm_range(rate); + pcm.truncate(range.end as usize * channels); + pcm.drain(..range.start as usize * channels); + true + } +} + +struct ExtractedPackets<'a> { + packets: Vec>, + timing: Option, +} + +/// Extract packets with the existing lacing rules. Use timing only when the submitted +/// pages describe one complete Opus logical stream with an unbroken packet sequence. +fn extract_ogg_opus(data: &[u8], multistream: bool) -> Option> { if data.len() < 27 || &data[..4] != b"OggS" { return None; } - let mut packets: Vec> = Vec::new(); - let mut cur: Vec = Vec::new(); + let serial = &data[14..18]; + let mut packets = Vec::new(); + let mut cur = Vec::new(); let mut pos = 0usize; + let mut sequence = 0u32; + let mut contiguous = true; + let mut head = None; + let mut have_tags = false; + let mut previous_granule = None::; + let mut decoded_48k = 0u64; + let mut timing = None; while pos < data.len() { - if pos + 27 > data.len() || &data[pos..pos + 4] != b"OggS" { + let page = data.get(pos..pos + 27)?; + if &page[..4] != b"OggS" { return None; } - let nseg = data[pos + 26] as usize; - let seg_start = pos + 27; - let mut body = seg_start + nseg; - if body > data.len() { - return None; - } - for &l in &data[seg_start..seg_start + nseg] { - let l = l as usize; - let next = body.checked_add(l)?; - if next > data.len() { - return None; - } - let slice = &data[body..next]; + let flags = page[5]; + let bos = flags & 0x02 != 0; + let eos = flags & 0x04 != 0; + let granule = u64::from_le_bytes(page[6..14].try_into().unwrap()); + let laces = data.get(pos + 27..pos + 27 + page[26] as usize)?; + let mut body = pos + 27 + laces.len(); + let mut page_samples = 0u64; + contiguous &= page[4] == 0 + && flags & !0x07 == 0 + && bos == (pos == 0) + && (flags & 0x01 != 0) == !cur.is_empty() + && &page[14..18] == serial + && &page[18..22] == sequence.to_le_bytes().as_slice(); + sequence = sequence.wrapping_add(1); + + for (i, &lace) in laces.iter().enumerate() { + let next = body.checked_add(lace as usize)?; + let slice = data.get(body..next)?; body = next; - if cur.is_empty() && l < 255 { - if !slice.is_empty() - && !slice.starts_with(b"OpusHead") - && !slice.starts_with(b"OpusTags") - { - parse_packet(slice, multistream)?; - packets.push(Cow::Borrowed(slice)); - } + if lace == 255 { + cur.extend_from_slice(slice); + continue; + } + let packet = if cur.is_empty() { + Cow::Borrowed(slice) } else { cur.extend_from_slice(slice); - if l < 255 { - if !cur.is_empty() - && !cur.starts_with(b"OpusHead") - && !cur.starts_with(b"OpusTags") - { - parse_packet(&cur, multistream)?; - packets.push(Cow::Owned(std::mem::take(&mut cur))); - } else { - cur.clear(); - } + Cow::Owned(std::mem::take(&mut cur)) + }; + if packet.starts_with(b"OpusHead") { + if pos == 0 && laces.len() == 1 && granule == 0 { + head = parse_opus_head(&packet); + } else { + contiguous = false; } + } else if packet.starts_with(b"OpusTags") { + contiguous &= head.is_some() + && !have_tags + && packet.len() >= 16 + && i + 1 == laces.len() + && granule == 0; + have_tags = true; + } else if packet.is_empty() { + contiguous = false; + } else { + let info = parse_packet(&packet, multistream)?; + contiguous &= have_tags; + page_samples += u64::from(info.total_samples_48k); + packets.push(packet); } } + + if contiguous + && eos + && body == data.len() + && cur.is_empty() + && page_samples > 0 + && granule != u64::MAX + { + // RFC 7845 section 4.4 counts only packets completed on the EOS page. + // If this is also the first audio page, section 4.5 uses a zero origin for + // a short granule, or infers an initial offset when the granule is larger. + let keep = match previous_granule { + Some(previous) => granule.checked_sub(previous).filter(|&n| n <= page_samples), + None => Some(granule.min(page_samples)), + }; + if let (Some(head), Some(keep)) = (head.take(), keep) { + timing = Some(OggTiming { + head, + decoded_48k: decoded_48k + page_samples, + end_48k: decoded_48k + keep, + }); + } + } + if !eos && page_samples > 0 { + contiguous &= granule != u64::MAX + && match previous_granule { + Some(previous) => previous.checked_add(page_samples) == Some(granule), + None => granule >= page_samples, + }; + previous_granule = Some(granule); + decoded_48k += page_samples; + } + contiguous &= !eos; pos = body; } - Some(packets) + Some(ExtractedPackets { packets, timing }) } /// Extract RFC 6716 Opus packets from a submitted audio buffer. @@ -596,14 +685,25 @@ fn extract_ogg_opus<'a>(data: &'a [u8], multistream: bool) -> Option(data: &'a [u8], block_align: u16, multistream: bool) -> Vec> { + extract_buffer(data, block_align, multistream).packets +} + +fn extract_buffer(data: &[u8], block_align: u16, multistream: bool) -> ExtractedPackets<'_> { + if data.starts_with(b"OggS") { + if let Some(ogg) = extract_ogg_opus(data, multistream) { + return ogg; + } + } + ExtractedPackets { + packets: extract_raw_packets(data, block_align, multistream), + timing: None, + } +} + +fn extract_raw_packets(data: &[u8], block_align: u16, multistream: bool) -> Vec> { if data.is_empty() { return Vec::new(); } - if data.starts_with(b"OggS") { - if let Some(pkts) = extract_ogg_opus(data, multistream) { - return pkts; - } - } // Multiple fixed-size blocks (e.g. N * 2048 B AWC blocks); a format without a usable // nBlockAlign gets the AWC block size let ba = if block_align >= 64 { block_align as usize } else { AWC_BLOCK }; @@ -643,19 +743,27 @@ pub fn extract_packets<'a>(data: &'a [u8], block_align: u16, multistream: bool) Vec::new() } -/// Total expected PCM frames (samples per channel at `rate` Hz) across all extracted Opus packets. +/// Expected PCM frames (samples per channel at `rate` Hz), using Ogg timing when available. /// Used as a fallback estimate if Media Foundation is unavailable. pub fn estimate_buffer_frames(data: &[u8], block_align: u16, rate: u32, multistream: bool) -> Option { - let pkts = extract_packets(data, block_align, multistream); - if pkts.is_empty() { + let extracted = extract_buffer(data, block_align, multistream); + if let Some(timing) = extracted.timing { + let range = timing.pcm_range(rate); + return Some(range.end - range.start); + } + if extracted.packets.is_empty() { return None; } let mut samples_48k = 0u64; - for p in &pkts { + for p in &extracted.packets { let info = parse_packet(p, multistream)?; - samples_48k += info.total_samples_48k as u64; + samples_48k += u64::from(info.total_samples_48k); } - Some((samples_48k * rate as u64) / 48_000) + Some(frames_at_rate(samples_48k, rate)) +} + +fn frames_at_rate(samples_48k: u64, rate: u32) -> u64 { + ((u128::from(samples_48k) * u128::from(rate)) / 48_000) as u64 } // --------------------------------------------------------------------------------------- @@ -1057,11 +1165,7 @@ impl Decoder { } let output_rate = streams[0].output_rate; let pre_skip_scaled = ((head.pre_skip as u64 * output_rate as u64) / 48_000) as usize; - let output_gain_scale = if head.output_gain == 0 { - 1.0 - } else { - 10.0f32.powf(head.output_gain as f32 / (20.0 * 256.0)) - }; + let output_gain_scale = gain_scale(head.output_gain); Ok(Self { streams, @@ -1099,6 +1203,13 @@ impl Decoder { /// Decode one RFC 6716 (single-stream or multistream) Opus packet and append interleaved /// PCM16 samples to `out`. Returns the number of PCM frames (samples per channel) appended. pub fn decode_packet(&mut self, packet: &[u8], out: &mut Vec) -> Result { + let start = out.len(); + self.decode_packet_pcm(packet, out)?; + self.trim_pre_skip(out, start); + Ok((out.len() - start) / self.channels as usize) + } + + fn decode_packet_pcm(&mut self, packet: &[u8], out: &mut Vec) -> Result<(), String> { let start = out.len(); let ch = self.channels as usize; @@ -1149,8 +1260,13 @@ impl Decoder { } } - // Trim RFC 7845 pre-skip samples if declared by OpusHead. + Ok(()) + } + + fn trim_pre_skip(&mut self, out: &mut Vec, start: usize) { + // Trim any remaining pre-skip configured on the voice. if self.pre_skip_left > 0 { + let ch = self.channels as usize; let produced_frames = (out.len() - start) / ch; let drop_frames = produced_frames.min(self.pre_skip_left); if drop_frames > 0 { @@ -1158,24 +1274,52 @@ impl Decoder { self.pre_skip_left -= drop_frames; } } - - Ok((out.len() - start) / ch) } /// Extract and decode all Opus packets from `data` into interleaved PCM16. pub fn decode_buffer(&mut self, data: &[u8], block_align: u16) -> Result, String> { - let packets = extract_packets(data, block_align, self.multistream); + let ExtractedPackets { packets, timing } = extract_buffer(data, block_align, self.multistream); if packets.is_empty() { return Err("buffer contains no valid RFC 6716 Opus packets".to_string()); } let mut out = Vec::new(); - for pkt in &packets { - self.decode_packet(pkt, &mut out)?; + let timing = timing.filter(|t| { + // Reuse the voice's configured MFTs only at a decoder stream boundary + // and when the Ogg headers describe the same channel layout and gain. + u16::from(t.head.channels) == self.channels + && usize::from(t.head.stream_count) == self.streams.len() + && t.head.coupled_count == self.coupled_count + && t.head.mapping == self.mapping + && gain_scale(t.head.output_gain) == self.output_gain_scale + && self.streams.iter().all(|s| s.timestamp_100ns == 0) + }); + if let Some(timing) = timing { + for pkt in &packets { + let start = out.len(); + if let Err(err) = self.decode_packet_pcm(pkt, &mut out) { + out.truncate(start); + self.trim_pre_skip(&mut out, 0); + return Err(err); + } + } + if timing.trim_pcm(&mut out, self.output_rate, self.channels as usize) { + self.pre_skip_left = 0; + } else { + self.trim_pre_skip(&mut out, 0); + } + } else { + for pkt in &packets { + self.decode_packet(pkt, &mut out)?; + } } Ok(out) } } +fn gain_scale(gain: i16) -> f32 { + 10.0f32.powf(f32::from(gain) / (20.0 * 256.0)) +} + #[inline] fn f32_to_i16(x: f32) -> i16 { if !x.is_finite() { @@ -1338,6 +1482,158 @@ pub(crate) mod tests { (&b[doff..doff + dsz], u32_at(foff) as usize, b[foff + 29].max(1) as u16) } + fn push_ogg_page( + ogg: &mut Vec, + serial: u32, + sequence: u32, + flags: u8, + granule: u64, + laces: &[u8], + body: &[u8], + ) { + assert_eq!(laces.iter().map(|&l| usize::from(l)).sum::(), body.len()); + let start = ogg.len(); + ogg.extend_from_slice(b"OggS\x00"); + ogg.push(flags); + ogg.extend_from_slice(&granule.to_le_bytes()); + ogg.extend_from_slice(&serial.to_le_bytes()); + ogg.extend_from_slice(&sequence.to_le_bytes()); + ogg.extend_from_slice(&[0; 4]); + ogg.push(u8::try_from(laces.len()).unwrap()); + ogg.extend_from_slice(laces); + ogg.extend_from_slice(body); + + let mut crc = 0u32; + for &byte in &ogg[start..] { + crc ^= u32::from(byte) << 24; + for _ in 0..8 { + let polynomial = if crc & 0x8000_0000 != 0 { 0x04C1_1DB7 } else { 0 }; + crc = (crc << 1) ^ polynomial; + } + } + ogg[start + 22..start + 26].copy_from_slice(&crc.to_le_bytes()); + } + + fn push_ogg_packets( + ogg: &mut Vec, + serial: u32, + sequence: u32, + flags: u8, + granule: u64, + packets: &[&[u8]], + ) { + let mut laces = Vec::new(); + for packet in packets { + laces.extend(std::iter::repeat(255).take(packet.len() / 255)); + laces.push((packet.len() % 255) as u8); + } + push_ogg_page(ogg, serial, sequence, flags, granule, &laces, &packets.concat()); + } + + fn ogg_headers(head: &[u8], serial: u32) -> Vec { + let mut ogg = Vec::new(); + push_ogg_packets(&mut ogg, serial, 0, 0x02, 0, &[head]); + push_ogg_packets(&mut ogg, serial, 1, 0, 0, &[b"OpusTags\0\0\0\0\0\0\0\0"]); + ogg + } + + #[test] + fn ogg_eos_frame_estimate_handles_preskip_and_initial_granules() { + let head = build_opus_head(1, 48_000, 313, 0); + let packet = &[0xF8, 0xFF, 0xFE]; + let mut ogg = ogg_headers(&head, 7); + let headers_len = ogg.len(); + + for (granule, keep) in [(800, 800), (1960, 960)] { + ogg.truncate(headers_len); + push_ogg_packets(&mut ogg, 7, 2, 0x04, granule, &[packet]); + assert_eq!(estimate_buffer_frames(&ogg, 0, 48_000, false), Some(keep - 313)); + } + + ogg.truncate(headers_len); + // A 5000-frame initial offset puts both absolute granules beyond the decoded length. + push_ogg_packets(&mut ogg, 7, 2, 0, 5960, &[packet]); + push_ogg_packets(&mut ogg, 7, 3, 0x04, 6724, &[packet]); + assert_eq!(estimate_buffer_frames(&ogg, 0, 48_000, false), Some(1724 - 313)); + assert_eq!(estimate_buffer_frames(&ogg, 0, 24_000, false), Some(862 - 156)); + + ogg.truncate(headers_len); + push_ogg_packets(&mut ogg, 7, 2, 0, 5960, &[packet]); + push_ogg_packets(&mut ogg, 7, 3, 0x04, 7684, &[packet, packet]); + assert_eq!(estimate_buffer_frames(&ogg, 0, 48_000, false), Some(2684 - 313)); + + for granule in [200, 313] { + ogg.truncate(headers_len); + push_ogg_packets(&mut ogg, 7, 2, 0x04, granule, &[packet]); + assert_eq!(estimate_buffer_frames(&ogg, 0, 48_000, false), Some(0)); + } + } + + #[test] + fn ogg_timing_requires_one_complete_contiguous_link() { + let head = build_opus_head(1, 48_000, 0, 0); + let packet = &[0xF8, 0xFF, 0xFE]; + let mut ogg = ogg_headers(&head, 7); + let audio_start = ogg.len(); + push_ogg_packets(&mut ogg, 7, 2, 0x04, 800, &[packet]); + assert_eq!(estimate_buffer_frames(&ogg[audio_start..], 0, 48_000, false), Some(960)); + + ogg.extend_from_slice(&ogg_headers(&head, 8)); + push_ogg_packets(&mut ogg, 8, 2, 0x04, 800, &[packet]); + assert_eq!(estimate_buffer_frames(&ogg, 0, 48_000, false), Some(1920)); + + ogg.truncate(audio_start); + push_ogg_packets(&mut ogg, 7, 2, 0, 960, &[packet]); + assert_eq!(estimate_buffer_frames(&ogg, 0, 48_000, false), Some(960)); + push_ogg_packets(&mut ogg, 7, 4, 0x04, 1600, &[packet]); + assert_eq!(estimate_buffer_frames(&ogg, 0, 48_000, false), Some(1920)); + } + + #[test] + fn ogg_timing_counts_continued_packet_on_its_completion_page() { + let head = build_opus_head(1, 48_000, 0, 0); + let mut ogg = ogg_headers(&head, 7); + let packet = &[0xF8, 0xFF, 0xFE]; + let mut continued = [0u8; 256]; + // One 20 ms frame in code-3 framing with 251 bytes of padding. + continued[..5].copy_from_slice(&[0xFB, 0x41, 251, 0xFF, 0xFE]); + push_ogg_packets(&mut ogg, 7, 2, 0, 5960, &[packet]); + push_ogg_page(&mut ogg, 7, 3, 0, u64::MAX, &[255], &continued[..255]); + push_ogg_page(&mut ogg, 7, 4, 0x05, 6724, &[1], &continued[255..]); + + let packets = extract_packets(&ogg, 0, false); + assert_eq!(packets.len(), 2); + assert_eq!(packets[0].as_ref(), packet); + assert_eq!(packets[1].as_ref(), continued.as_slice()); + assert_eq!(estimate_buffer_frames(&ogg, 0, 48_000, false), Some(1724)); + } + + #[test] + fn ogg_pcm_trim_requires_full_mft_output() { + let timing = OggTiming { + head: parse_opus_head(&build_opus_head(2, 48_000, 313, 0)).unwrap(), + decoded_48k: 1920, + end_48k: 1724, + }; + let pcm: Vec = (0..1920).collect(); // 960 stereo frames at 24 kHz. + let mut primed = pcm[..1440].to_vec(); + assert!(!timing.trim_pcm(&mut primed, 24_000, 2)); + assert_eq!(primed.as_slice(), &pcm[..1440]); + + let mut full = pcm.clone(); + assert!(timing.trim_pcm(&mut full, 24_000, 2)); + assert_eq!(full.as_slice(), &pcm[156 * 2..862 * 2]); + + let shorter_than_preskip = OggTiming { + head: parse_opus_head(&build_opus_head(2, 48_000, 313, 0)).unwrap(), + decoded_48k: 1920, + end_48k: 200, + }; + let mut discarded = pcm.clone(); + assert!(shorter_than_preskip.trim_pcm(&mut discarded, 24_000, 2)); + assert!(discarded.is_empty()); + } + #[test] fn framing_detection_awc_blocks_and_raw() { let pkt1 = [0x78u8, 0x11, 0x22, 0x33]; @@ -1356,6 +1652,7 @@ pub(crate) mod tests { two.extend_from_slice(&block); let got2 = extract_packets(&two, 2048, false); assert_eq!(got2.len(), 4); + assert_eq!(estimate_buffer_frames(&two, 2048, 48_000, false), Some(3840)); let mut tricky = vec![0u8; 250]; tricky[0] = 0xF8; @@ -1363,6 +1660,7 @@ pub(crate) mod tests { let got_raw = extract_packets(&tricky, 1, false); assert_eq!(got_raw.len(), 1); assert_eq!(got_raw[0].as_ref(), tricky.as_slice()); + assert_eq!(estimate_buffer_frames(&tricky, 1, 48_000, false), Some(960)); } // Compact real libopus RFC 6716 test packets (440 Hz mono, 440/880 Hz stereo, SILK/Hybrid/CELT, 5.1). @@ -1485,6 +1783,42 @@ pub(crate) mod tests { assert!(peak_ch(0) < 50 && peak_ch(5) < 50, "silent channels should remain near zero"); } + #[test] + fn decode_ogg_eos_matches_raw_pcm_window_mono_and_stereo() { + if !is_available() { + eprintln!("skipping: MSOpusDecoder.dll not available"); + return; + } + let cases = [ + (1, &MONO_HYBRID_20MS[..2], 0, 0), + (2, &STEREO_CELT_20MS[..2], 312, 5000), + ]; + for (channels, packets, pre_skip, initial_granule) in cases { + let head = build_opus_head(channels, 48_000, pre_skip, 0); + let mut ogg = ogg_headers(&head, 7); + push_ogg_packets(&mut ogg, 7, 2, 0, initial_granule + 960, &packets[..1]); + push_ogg_packets(&mut ogg, 7, 3, 0x04, initial_granule + 1724, &packets[1..]); + + let mut raw_decoder = Decoder::new(u16::from(channels), 48_000, &[]).unwrap(); + let mut raw = Vec::new(); + for pkt in packets { + let pcm = raw_decoder.decode_buffer(pkt, 0).unwrap(); + assert_eq!(pcm.len(), 960 * usize::from(channels)); + raw.extend(pcm); + } + let ch = usize::from(channels); + for extra in [&[][..], head.as_slice()] { + let mut dec = Decoder::new(u16::from(channels), 48_000, extra).unwrap(); + let pcm = dec.decode_buffer(&ogg, 0).unwrap(); + assert_eq!(pcm.as_slice(), &raw[usize::from(pre_skip) * ch..1724 * ch]); + assert_eq!( + estimate_buffer_frames(&ogg, 0, 48_000, false), + Some((pcm.len() / ch) as u64), + ); + } + } + } + #[test] fn decode_awc_block_preskip_rate_and_flush() { if !is_available() {