hle/opus: honor Ogg Opus EOS granule trimming and pre-skip #109

Merged
certified-retart merged 1 commit from upstream-pr/110 into main 2026-11-23 14:13:38 +00:00
certified-retart commented 2026-11-23 11:45:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by certified-retart on 2026-11-23T11:45:48Z.


Summary

Honors RFC 7845 Ogg Opus end trimming (EOS granule position) and OpusHead pre-skip for complete, contiguous Ogg Opus submissions in crates/xcl/src/hle/opus.rs.

Previously, extract_ogg_opus flattened Ogg pages into raw packets and dropped page granule positions, causing decoded PCM and estimate_buffer_frames to include trailing padding samples past the final granule (and to ignore the Ogg OpusHead pre-skip when the voice format had no extra_data).

Changes

  • OggTiming + contiguous Ogg page validation (extract_ogg_opus):
    • Tracks page sequence, serial number, BOS/EOS/continuation flags, OpusHead, OpusTags, continued packets across page boundaries, and exact granule deltas across pages.
    • Computes the playable end on the EOS page per RFC 7845 §4.4 (multi-page delta from previous audio granule) and §4.5 (single-page zero origin vs inferred initial granule offset), and clamps pre_skip to end_48k per §4.2 so granule <= pre_skip yields 0 frames without underflow.
    • Uses u128 scaling in frames_at_rate to avoid overflow.
    • Falls back safely to the existing untrimmed behavior for partial/fragmented, chained, multiplexed, or non-contiguous submissions.
  • PCM trimming (Decoder::decode_buffer, OggTiming::trim_pcm):
    • Applies OggTiming only at a decoder stream boundary (timestamp_100ns == 0) with matching channel/mapping/gain configuration, and only when the MFT produced the complete expected decoded timeline (so buffered/primed short outputs keep the existing fallback path).
    • Applies OpusHead pre-skip once (never double-skipping) and updates estimate_buffer_frames to use the same trimmed window.
    • Keeps raw and AWC length-prefixed packet extraction unchanged.
  • Tests:
    • ogg_eos_frame_estimate_handles_preskip_and_initial_granules (including granule < pre_skip, granule == pre_skip, initial granule offset, and 24 kHz scaling)
    • ogg_timing_requires_one_complete_contiguous_link
    • ogg_timing_counts_continued_packet_on_its_completion_page
    • ogg_pcm_trim_requires_full_mft_output
    • decode_ogg_eos_matches_raw_pcm_window_mono_and_stereo (live MSOpusDecoder.dll check with both empty and OpusHead extra_data)

Verification

  • cargo test -p xcl --lib -- --test-threads=1: 500 passed, 0 failed, 16 ignored
  • cargo test -p xcl --test e2e -- --test-threads=1: 2 passed, 0 failed
<!-- greyfinch-sync:ivall/xcl:issue:6558546494 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/110). Originally posted by [certified-retart](https://github.com/certified-retart) on 2026-11-23T11:45:48Z. --- ### Summary Honors RFC 7845 Ogg Opus end trimming (`EOS` granule position) and `OpusHead` pre-skip for complete, contiguous Ogg Opus submissions in `crates/xcl/src/hle/opus.rs`. Previously, `extract_ogg_opus` flattened Ogg pages into raw packets and dropped page granule positions, causing decoded PCM and `estimate_buffer_frames` to include trailing padding samples past the final granule (and to ignore the Ogg `OpusHead` pre-skip when the voice format had no `extra_data`). ### Changes - **`OggTiming` + contiguous Ogg page validation (`extract_ogg_opus`)**: - Tracks page sequence, serial number, BOS/EOS/continuation flags, `OpusHead`, `OpusTags`, continued packets across page boundaries, and exact granule deltas across pages. - Computes the playable end on the EOS page per RFC 7845 §4.4 (multi-page delta from previous audio granule) and §4.5 (single-page zero origin vs inferred initial granule offset), and clamps `pre_skip` to `end_48k` per §4.2 so `granule <= pre_skip` yields 0 frames without underflow. - Uses `u128` scaling in `frames_at_rate` to avoid overflow. - Falls back safely to the existing untrimmed behavior for partial/fragmented, chained, multiplexed, or non-contiguous submissions. - **PCM trimming (`Decoder::decode_buffer`, `OggTiming::trim_pcm`)**: - Applies `OggTiming` only at a decoder stream boundary (`timestamp_100ns == 0`) with matching channel/mapping/gain configuration, and only when the MFT produced the complete expected decoded timeline (so buffered/primed short outputs keep the existing fallback path). - Applies `OpusHead` pre-skip once (never double-skipping) and updates `estimate_buffer_frames` to use the same trimmed window. - Keeps raw and AWC length-prefixed packet extraction unchanged. - **Tests**: - `ogg_eos_frame_estimate_handles_preskip_and_initial_granules` (including `granule < pre_skip`, `granule == pre_skip`, initial granule offset, and 24 kHz scaling) - `ogg_timing_requires_one_complete_contiguous_link` - `ogg_timing_counts_continued_packet_on_its_completion_page` - `ogg_pcm_trim_requires_full_mft_output` - `decode_ogg_eos_matches_raw_pcm_window_mono_and_stereo` (live `MSOpusDecoder.dll` check with both empty and `OpusHead` `extra_data`) ### Verification - `cargo test -p xcl --lib -- --test-threads=1`: **500 passed, 0 failed, 16 ignored** - `cargo test -p xcl --test e2e -- --test-threads=1`: **2 passed, 0 failed**
upstream-sync closed this pull request 2026-11-24 06:57:12 +00:00
ivall commented 2026-11-24 07:00:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T14:14:03Z.


Merged. Checked on Windows 11 with an RX 9070 (RDNA4), merged together with the rest of #110–#129: cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; the retail pack still translates 26 698 / 26 698; the FSR4 GPU probe passes; Unicorn Overlord and XDefiant run. Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7103685112 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/110#issuecomment-7103685112). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T14:14:03Z. --- Merged. Checked on Windows 11 with an RX 9070 (RDNA4), merged together with the rest of #110–#129: `cargo test --workspace` green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with `XCL_SPLIT_WAVE64=1`; the retail pack still translates 26 698 / 26 698; the FSR4 GPU probe passes; Unicorn Overlord and XDefiant run. Thanks!
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!109
No description provided.