Opus: reuse multistream scratch buffers, validate sub-packets atomically, and bulk-append PCM16 #2696
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!2696
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2696"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Decodere on GitHub · 2026-11-26 06:46 UTC · original
Decodere:fix-opus-multistream-scratch-and-bulk-pcm→main· +421 −57 · 1 files · 1 commits · openWhat is in it
Removes per-packet heap churn on multistream Opus decoding, bulk-copies decoded little-endian PCM16 samples, guards multistream interleaving against short or mismatched stream buffers, and preallocates
decode_bufferoutput incrates/xcl/src/hle/opus.rs:split_self_delimitedintosplit_self_delimited_into(data, pos, out)(keepingsplit_self_delimitedas a thin wrapper sosplit_multistream_packetstays untouched) and addedsplit_multistream_intowhich enforces the minimum2 * stream_count - 1byte length and parses/validates allstream_countsub-packets viaparse_packet(..., false)before returningSome(last)so malformed multistream packets never partially advance anySingleStreamDecoder.const _: () = assert!(cfg!(target_endian = "little"))) and extractedappend_pcm16_le(guardingn == 0, reservingn, and copyingn * 2bytes via byte-alignedstd::ptr::copy_nonoverlapping+set_len) andappend_f32_le(viabytes.as_chunks::<4>().0), called fromMftDecoder::append_sample_pcm16.ChannelSrc, and OOB-safe interleaving: AddedChannelSrc(Silence,Mono,Coupled), defensivebuild_channel_map(mapping255and any out-of-range entries toChannelSrc::Silence), andinterleave_streams(clampingframesagainst every referenced stream buffer length). Appendedchannel_map,sub_pkts, andsub_pcmtoDecoder(empty for mono/stereo; reused in place inDecoder::decode_packet_pcmwith per-stream clampingframes = frames.min(n).min(pcm.len() / expected_ch)).decode_bufferoutput: Addedexpected_buffer_samples(usingtiming.decoded_48kwhen Ogg timing applies or summingparse_packet(p, multistream)otherwise, capped atMAX_PREALLOC_SAMPLES = 16 << 20) and initializeddecode_buffer'soutviaVec::with_capacity(...).append_pcm16_le&append_f32_le,split_self_delimited_into/split_multistream_intoparity and zero-reallocation reuse across codes 0–3 and 5.1 packets,build_channel_map&interleave_streamsshort-stream / OOB clamping,expected_buffer_samplessizing & cap safety (including multistream self-delimited code-1 packets), and liveDecoderatomic validation, zero-reallocation scratch reuse, short-coupled-stream OOB clamping, and exactdecode_bufferpreallocation.Tested
origin/main(b49be183): Reproduced both issues before applying the fix underXCL_ADAPTER=warp XCL_WARP=1:Decoderwhose coupled stream 0 returned mono (960 samples instead of 1920) panicked insideDecoder::decode_packet_pcmatcrates/xcl/src/hle/opus.rs:1438(index out of bounds: the len is 960 but the index is 960).Decoder::decode_bufferon a 5-packet 20 ms AWC block (4800samples,pre_skip = 0) grew from capacity0tocapacity() == 7680instead of a single exact4800-sample allocation.MSOpusDecodertests (hle::opus::tests::):XCL_ADAPTER=warp XCL_WARP=1 cargo test --lib hle::opus::tests:: -- --nocapture):18passed,0failed,0ignored (0.00s;decodes_a_game_soundskips cleanly whenXCL_SAMPLE_AWCis unset).XCL_ADAPTER=warp XCL_WARP=1 cargo test --release --lib hle::opus::tests:: -- --nocapture):18passed,0failed,0ignored (0.00s).cargo test --release --lib hle::opus::tests:: -- --nocapture):18passed,0failed,0ignored (0.01s).cargo clippy --all-targets):0new warnings incrates/xcl/src/hle/opus.rs(only the 9 pre-existing warnings on untouched lines176, 241, 528, 582, 742, 1870, 2112, 2113, 2124).rustc -O, before vs after on identical inputs):pcm16_append_3840B(960 stereo frames, 200k iters):1024.4 ns->31.5 ns(32.47x)split_5_1_packet(SURROUND51_20MS[0], 4 streams, 200k iters):148.7 ns->13.1 ns(11.38x,0steady-state allocs)multistream_5_1_pcm_interleave(960 frames × 6 ch, 50k iters):11798.8 ns->7073.5 ns(1.67x,0steady-state allocs)decode_buffer_5pkt_stereo_alloc(5 × 960 stereo frames, 50k iters):6885.0 ns->380.4 ns(18.10x,1allocation vs repeated doubling)Regression run
cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture):37test binaries,1440passed,0failed,47ignored, with29pre-existing host-gatedSKIPPEDlines (21no wave64,3wave32per-lane form: refused,3no D3D12 debug layer /d3d12SDKLayers.dll,1pso_measurement_compiles_an_exact_synthetic_recorddriver cache,1heap_window_out_of_memory_falls_back_to_the_switch_formno heap windows).scripts/smoke.ps1,origin/mainb49be183vs PR HEAD70ce54a4):Baseline (
origin/main):PR HEAD (
70ce54a4):Not tested
decodes_a_game_sound(XCL_SAMPLE_AWCwas unset).libopus(opus.dll) fallback backend (WindowsMSOpusDecoder.dllis present and selected on this host).append_pcm16_leis compile-time guarded withassert!(cfg!(target_endian = "little"))).29pre-existing host-gated workspace skips listed above (wave64, wave32 per-lane refused, D3D12 debug layer, driver cache disable, heap windows).AI use
Written and verified with assistance from Google Antigravity (Gemini 4 Neon). All changes, unit tests, micro-benchmarks, workspace tests, and paired RTX 4060 smoke runs were executed and reviewed directly against the repository.
@luckyteagirl on GitHub · 2026-11-26 06:50 UTC · original · reviewed
The new atomic split validates each sub-packet's framing but discards
parse_packet(...).total_samples_48k, so it still accepts streams with different packet durations. The added six-stream positive fixture includes a 20 ms code-0 sub-packet (0xFC) and a 40 ms code-1 sub-packet (0xFD), and expects the split to succeed. RFC 7845 §3 requires all sub-packets in one multistream packet to have the same duration and recommends treating a mismatch as malformed. InDecoder::decode_packet, all backends are then advanced before the output is clamped to the shortest stream, leaving their timelines out of step. Could the prevalidation comparetotal_samples_48kacross every sub-packet, including the tail, before decoding any stream? The positive fixture could use equal durations, with a mismatched-tail negative fixture asserting no decoder timestamp advances.The malformed-packet acceptance predates this PR, but its new atomic validation and fixture are a natural place to close it. Exact-head source review of
70ce54a41198899bfebc9feda607ad6e6a1fc346; no audio decode was run. AI-assisted review with Codex.@sage-sage on GitHub · 2026-11-26 07:05 UTC · original
Test report: passes on NVIDIA. Exact head
70ce54a4(its base is currentmainb49be18, so head = merged).Tested
cargo test --workspace -- --test-threads=4: 1440 passed, 0 failed, 47 ignored; the 5 new tests passmainon this machine): 21 wave64 tests (NVIDIA is wave32 only), 3 per-lane wave32 f16/f32 forms (refused), 3 D3D12 debug layer (d3d12SDKLayers.dllnot installed), 1no heap windows, 1pso_measurement_compiles_an_exact_synthetic_record(Developer Mode off)Not tested
Claude Code (Claude Opus 5.5) ran the tests and wrote this report.
github-bot referenced this pull request2026-11-26 12:40:17 +00:00
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.