Skip to content

fix(stb_vorbis): correct type-2 residue size for multichannel audio - #60

Closed
ohxiaoguang wants to merge 1 commit into
seedds:mainfrom
ohxiaoguang:fix/stb-vorbis-multichannel-residue
Closed

ohxiaoguang wants to merge 1 commit into
seedds:mainfrom
ohxiaoguang:fix/stb-vorbis-multichannel-residue

Conversation

@ohxiaoguang

Copy link
Copy Markdown
Contributor

5.1 Vorbis movie audio in Link's Awakening can sound distorted/gurgling, with occasional pops, from the opening through the title screen. decode_residue() clamps the interleaved type-2 residue vector to n * 2, even when its submap contains more than two channels. This truncates the residue decode and leaves subsequent encoded data misinterpreted.

Changes

  • Use n * ch for type-2 residues, where ch is the actual submap channel count; types 0 and 1 retain their existing bounds.
  • Update start_decoder()'s temporary-memory estimate for type-2 residues. The stream channel count is a conservative upper bound for any submap. Fixing only the decode bound can otherwise exceed the advertised caller-owned arena size.
  • Add the included stb_vorbis.c to dmhost's prerequisites so incremental Mac builds pick up decoder changes.
  • Add an asset-free regression test that constructs tiny Vorbis/Ogg packets in memory. It checks mono/stereo types 0/1/2, type-2 streams with 3/6/8 channels, a six-channel stream with two submaps, and identical PCM through heap/arena and pull/push decoding. Fixed arenas use exactly the reported memory requirement.

Validation

Run the synthetic regressions without a game profile, encoder, audio device, or game data:

make -C dm/host/tests build/t_vorbis build/t_vorbis_divides
dm/host/tests/build/t_vorbis
dm/host/tests/build/t_vorbis_divides
  • Both normal and STB_VORBIS_DIVIDES_IN_RESIDUE builds pass all 10 scenarios (four heap/arena and pull/push combinations each).
  • Both configurations also pass AddressSanitizer and UndefinedBehaviorSanitizer. Negative controls fail as expected: unmodified main misses coefficients in the three-channel case; applying only the decode fix exposes insufficient arena scratch space in the six-channel case.
  • The generated six-channel/two-submap Ogg is also accepted by FFmpeg. The regression source compiles for the iOS arm64 target; this is a compile check, not a device test.
  • Previously completed local validation of the same runtime correction: all six channels of the first 15 seconds of Link's Awakening's opening, title, and ending tracks agree with an independent FFmpeg reference to within one PCM16 unit (1/32768), with RMS error below 0.708 units. The MacBook Air M1 user confirmed the opening/title gurgling and pops disappeared on the built-in speakers. These playback/PCM checks preceded rebasing the isolated fix onto current main; no new commercial media is included here.
  • iOS device playback has not been tested.

The vendored file still carries this bug in nothings/stb master. Related upstream reports: #879, #1594. stb PR #1487 addresses the related scratch-space underestimate for the existing stereo bound; the estimate here also accounts for the corrected multichannel bound.

Use the actual submap channel count when bounding interleaved type-2
residues. Reserve enough classification scratch memory for all stream
channels when reporting caller-owned arena requirements.

Track the included decoder in the Mac host's dependencies and add
synthetic mono/stereo/multichannel regression coverage for heap and
fixed-buffer decoding through both pull and push APIs.
@ohxiaoguang
ohxiaoguang force-pushed the fix/stb-vorbis-multichannel-residue branch from 7018994 to 9be3041 Compare October 22, 2026 16:04
seedds added a commit that referenced this pull request Oct 22, 2026
…es span every channel of their submap (GitHub PR #60)

stb_vorbis cut a type-2 residue's interleaved vector at n * 2 values even when its submap holds more channels, so the rest of the packet was read as the wrong data: the 5.1 tracks libvorbis encodes came out distorted and gurgling, with pops. The bound is now n * ch, as in the Vorbis spec and libvorbis's res2_inverse, and the temporary-memory estimate for a caller-owned buffer counts every channel of the stream for type-2 residues (with the decode fix alone, a 6-channel stream overran the advertised buffer). Upstream stb still has the bug (nothings/stb #879, #1594).
Link's Awakening's opening and title movies were the report; DRAGON QUEST XI S's movies open only 6-channel Vorbis tracks too. Its 23 6-channel cutscene and title tracks, decoded as the host does, now match FFmpeg to within 0.02 PCM16 units, where main's decoder was off by an RMS of 1,700 to 70,000 units.
dm/host/tests/t_vorbis builds tiny Vorbis streams in memory (types 0/1/2, mono to 8 channels, two submaps) and checks heap and fixed-buffer decoding through the pull and push APIs, with and without STB_VORBIS_DIVIDES_IN_RESIDUE; it needs no game profile. dmhost rebuilds when stb_vorbis.c changes.
On top of the PR: the test rules sit with the others, and the stb_vorbis README and the per-game notes say what the fix repairs.

Co-authored-by: ohxiaoguang <[email protected]>
@seedds

seedds commented Oct 22, 2026

Copy link
Copy Markdown
Owner

Thanks @ohxiaoguang, this is in main as e10277b (e10277b), with you as co-author. It takes the type-2 bound (n*ch), the temporary-memory estimate, the dmhost dependency and t_vorbis as you wrote them; on top, the test rules sit with the other test rules, and the stb_vorbis README and the per-game notes say what the fix repairs. Your fix also covers DRAGON QUEST XI S: its movies open only 6-channel Vorbis tracks, and all 23 of its 6-channel cutscene and title tracks, decoded as the host does, now match FFmpeg to within 0.02 PCM16 units (main was off by an RMS of 1,700 to 70,000 units).

@seedds seedds closed this Oct 22, 2026
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
…es span every channel of their submap (GitHub PR seedds#60)

stb_vorbis cut a type-2 residue's interleaved vector at n * 2 values even when its submap holds more channels, so the rest of the packet was read as the wrong data: the 5.1 tracks libvorbis encodes came out distorted and gurgling, with pops. The bound is now n * ch, as in the Vorbis spec and libvorbis's res2_inverse, and the temporary-memory estimate for a caller-owned buffer counts every channel of the stream for type-2 residues (with the decode fix alone, a 6-channel stream overran the advertised buffer). Upstream stb still has the bug (nothings/stb seedds#879, seedds#1594).
Link's Awakening's opening and title movies were the report; DRAGON QUEST XI S's movies open only 6-channel Vorbis tracks too. Its 23 6-channel cutscene and title tracks, decoded as the host does, now match FFmpeg to within 0.02 PCM16 units, where main's decoder was off by an RMS of 1,700 to 70,000 units.
dm/host/tests/t_vorbis builds tiny Vorbis streams in memory (types 0/1/2, mono to 8 channels, two submaps) and checks heap and fixed-buffer decoding through the pull and push APIs, with and without STB_VORBIS_DIVIDES_IN_RESIDUE; it needs no game profile. dmhost rebuilds when stb_vorbis.c changes.
On top of the PR: the test rules sit with the others, and the stb_vorbis README and the per-game notes say what the fix repairs.

Co-authored-by: ohxiaoguang <[email protected]>
theorigden pushed a commit to theorigden/nxnative that referenced this pull request Nov 17, 2026
The frame step counted in vsyncs of 1/60 s, so a hold could only be 16.7 or 33.3 ms: a device whose GPU
takes 17-24 ms a frame (TotK on an M1 iPad Pro) dropped from 60 to 30 with time to spare. A 120 Hz screen
can show a frame for three refreshes, 25 ms.

- State page +0x2c: the step's ticks a second (float), 60 from the loader. TotK's stub reads it (same
  instruction count: ldr s3 for fmov seedds#30, fcvtzu for the fadd, a nop for mov seedds#60) and declares
  FRAME_STEP_120 (fixups.tsv, the loader's dm_fps_step_120).
- hle_nvn.c fs_ticks: for such stubs on a screen that shows 120 Hz, the frame step counts in ticks of
  1/120 s (decided at the first present), the display link runs at 120 Hz, and a 40 target is a hold of
  3; at 60, late frames go to 40 before 30. The link callback holds 25 ms frames for their period (was
  > 25 ms only). A screen that drops to 60 Hz (Low Power Mode) runs 40 at 30 and keeps the cap's hold.
  DM_FSTEP_120=0 keeps 1/60 s; =1 takes 1/120 s on any screen (tests).
- nvn_fstep.h: holds and steps up to FSTEP_KMAX (6 ticks, the stub's 3 vsyncs); FSTEP_MAX stays the
  default largest step.
- DM_FPS_TARGET=40 in the loader, the live setting and settings.tsv (offered with fixups:FRAME_STEP_120).
- Docs: frame-rate.md entry, settings.md, env_switches.tsv, the TotK note; symptom and patch indexes.

Checked: host build; host, graphics (t_fstep: 40 fps holds 3 ticks at game time = real time; at 60,
2.6-tick frames step to 3, 3.5 to 4) and docs / settings tests. TotK headless with DM_FPS_TARGET=40
DM_FSTEP_120=1 on the Mac (M1 Max): two windows of 1,800 frames, every frame held 3 ticks (25.0 ms),
game time 45.00 and 45.02 s in 45.00 s (1.000 of real time), GPU 17-23 ms, no red lines. The rewritten
main has the new stub (TotK's work folder, patched_v720896). Not yet run on a 120 Hz screen (the Mac's display is a 60 Hz external one, where 40
falls back to 30 as designed).

Front-ends: DM_FPS_TARGET has a new value, 40, offered where the fixups name FRAME_STEP_120 (dm/games/settings.tsv).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants