feat(ngs2): parse and decode VAG and HE-VAG waveforms #11388

Merged
nekomancer01 merged 8 commits from upstream-pr/11388 into main 2026-11-04 17:35:36 +00:00
nekomancer01 commented 2026-10-26 10:14:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nekomancer01 on 2026-10-26T10:14:44Z.


What

Parse and decode VAGp waveforms (type 0x1c) in both NGS2 libraries, so the game-start sound that Steins;Gate Re:Boot (PPSA36591) passes at its title screen plays instead of being set up with an all-zero format (#5677).

  • sceNgs2ParseWaveformData and sceNgs2ParseWaveformFile recognise VAGp and follow prosper's reading of the PS5 module: no version check, channels from byte 0x1e (0 is mono), the declared data size, one frame dropped unless bit 1 of the second frame's flags is set, and otherwise loop markers 6 and 3 splitting the waveform into an intro, an endlessly repeated loop and a tail through the existing VagBlock (block split). Where the module would wrap a 32-bit count, it throws.
  • The sampler decodes each submitted block once to PCM16 with vgmstream's HE-VAG decoder (psv_decoder.c), built from the existing 3rdparty/vgmstream submodule into a small static library. Its first five predictors are the original VAG filters, so both formats decode as type 0x1c, as the module uses one type for every VAGp. Decoder history carries into a block whose data follows the previous VAG block's frames, so an intro-to-loop split does not restart the predictor.
  • The decoded block plays through the existing PCM16 path; the waveform pointer and decoded bytes advance by whole frames.
  • Frames with a predictor above 127 or a shift above 12, configuration data and streaming append throw. Both new gaps are recorded in docs/dev/debt/libSceNgs2.native.md.

This picks up the HE-VAG work of #5797, which was closed while waiting on a rebase. Differences: the header and loop rules follow the module reading instead of vgmstream's file heuristics, decoder history carries across consecutive blocks, and no packaging change is needed since main already ships the vgmstream notice.

Tested

Linux 7.3.0 (CachyOS host, Ubuntu 26.04 distrobox), Ryzen 7 3700X, RTX 3070 (NVIDIA 615.78.08), GCC 15.2.0, Release, on main 5b5d24cb22 (rebased; the title runs below were on b27cc8416 with the same change).

  • New ngs2_vag: header fields, the dropped-frame rule, declared sizes, short inputs, loop splits (intro/loop/tail, loop at 0, end before start), file parsing at an offset, stereo playback with skipped samples and a repeat, decoded bytes and waveform pointer, history carried across consecutive blocks and reset by RESET, flag 7 silence, and the throwing cases. Expected samples are worked out by hand from predictors 0 and 1, not taken from vgmstream.
  • All 17 ngs2 tests pass. Full suite on 5b5d24cb22: 2335 of 2336; the one failure, pad_sdl, is from a physical Xbox controller taking slot 0 on this machine, and this change touches no pad code.
  • tools/check_conventions.py --base upstream/main and tools/check_test_registration.py build pass.
  • Title: STEINS;GATE RE:BOOT (PPSA36591) 01.002.000, my own console dump, relinked with --registry; import_audit.py reports no absent imports. Same script on main and on this branch: 40 s to the title screen, then Cross every 10 s, recording only the title's PipeWire stream with pw-record --target app.elf.
    • At the first Cross the title calls sceNgs2ParseWaveformData with a 152,944-byte VAGp (2 channels, 48 kHz, data size 0x25540, the file described in #5677). With a temporary log (not in this PR), it parsed as 133,756 samples with no loop and decoded 4,777 frames with a peak of 27,955.
    • Branch: a sound starts at the press and decays over about 2.5 s (peak 9,318 in the recorded stream). main: the stream is silent at the same moment, with no error in the log.
    • Both runs reach the opening scene at about 60 fps; nothing else in the logs differs.
  • Not checked: the sound against a console recording, looped VAG files in a title, Windows and macOS.

The PR overlap check lists shared lines with #5548, #10599 and #11315 (NGS2 files these also change); none of them implements VAG.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: Fixes #5677
  • Unblocks: none (PPSA36591, #7384, already plays on main; this restores its title-screen start sound)
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5015709509 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11388). Originally posted by [nekomancer01](https://github.com/nekomancer01) on 2026-10-26T10:14:44Z. --- ### What Parse and decode VAGp waveforms (type `0x1c`) in both NGS2 libraries, so the game-start sound that Steins;Gate Re:Boot (PPSA36591) passes at its title screen plays instead of being set up with an all-zero format (#5677). - `sceNgs2ParseWaveformData` and `sceNgs2ParseWaveformFile` recognise `VAGp` and follow [prosper's reading of the PS5 module](https://github.com/mattias800/prosper/blob/40d95db32efe1ad00106a5b9a84e018fffa9ecff/prosper/src/hle/audio/ngs2_waveform.cpp#L136-L178): no version check, channels from byte `0x1e` (0 is mono), the declared data size, one frame dropped unless bit 1 of the second frame's flags is set, and otherwise loop markers 6 and 3 splitting the waveform into an intro, an endlessly repeated loop and a tail through the existing `VagBlock` ([block split](https://github.com/mattias800/prosper/blob/40d95db32efe1ad00106a5b9a84e018fffa9ecff/prosper/src/hle/audio/ngs2_waveform.cpp#L297-L344)). Where the module would wrap a 32-bit count, it throws. - The sampler decodes each submitted block once to PCM16 with vgmstream's HE-VAG decoder (`psv_decoder.c`), built from the existing `3rdparty/vgmstream` submodule into a small static library. Its first five predictors are the original VAG filters, so both formats decode as type `0x1c`, as the module uses one type for every `VAGp`. Decoder history carries into a block whose data follows the previous VAG block's frames, so an intro-to-loop split does not restart the predictor. - The decoded block plays through the existing PCM16 path; the waveform pointer and decoded bytes advance by whole frames. - Frames with a predictor above 127 or a shift above 12, configuration data and streaming append throw. Both new gaps are recorded in `docs/dev/debt/libSceNgs2.native.md`. This picks up the HE-VAG work of #5797, which was closed while waiting on a rebase. Differences: the header and loop rules follow the module reading instead of vgmstream's file heuristics, decoder history carries across consecutive blocks, and no packaging change is needed since `main` already ships the vgmstream notice. ### Tested Linux 7.3.0 (CachyOS host, Ubuntu 26.04 distrobox), Ryzen 7 3700X, RTX 3070 (NVIDIA 615.78.08), GCC 15.2.0, Release, on `main` `5b5d24cb22` (rebased; the title runs below were on `b27cc8416` with the same change). - New `ngs2_vag`: header fields, the dropped-frame rule, declared sizes, short inputs, loop splits (intro/loop/tail, loop at 0, end before start), file parsing at an offset, stereo playback with skipped samples and a repeat, decoded bytes and waveform pointer, history carried across consecutive blocks and reset by `RESET`, flag 7 silence, and the throwing cases. Expected samples are worked out by hand from predictors 0 and 1, not taken from vgmstream. - All 17 `ngs2` tests pass. Full suite on `5b5d24cb22`: 2335 of 2336; the one failure, `pad_sdl`, is from a physical Xbox controller taking slot 0 on this machine, and this change touches no pad code. - `tools/check_conventions.py --base upstream/main` and `tools/check_test_registration.py build` pass. - Title: STEINS;GATE RE:BOOT (PPSA36591) 01.002.000, my own console dump, relinked with `--registry`; `import_audit.py` reports no absent imports. Same script on `main` and on this branch: 40 s to the title screen, then Cross every 10 s, recording only the title's PipeWire stream with `pw-record --target app.elf`. - At the first Cross the title calls `sceNgs2ParseWaveformData` with a 152,944-byte `VAGp` (2 channels, 48 kHz, data size `0x25540`, the file described in #5677). With a temporary log (not in this PR), it parsed as 133,756 samples with no loop and decoded 4,777 frames with a peak of 27,955. - Branch: a sound starts at the press and decays over about 2.5 s (peak 9,318 in the recorded stream). `main`: the stream is silent at the same moment, with no error in the log. - Both runs reach the opening scene at about 60 fps; nothing else in the logs differs. - Not checked: the sound against a console recording, looped VAG files in a title, Windows and macOS. The PR overlap check lists shared lines with #5548, #10599 and #11315 (NGS2 files these also change); none of them implements VAG. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: Fixes #5677 - [x] Unblocks: none (PPSA36591, #7384, already plays on `main`; this restores its title-screen start sound) - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-26 11:44:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T11:44:29Z.

Review: CHANGES_REQUESTED


The VAG parser accepts a declared payload that extends beyond the buffer passed to it, then returns a block that can make Ngs2DecodeVag read past that buffer.

For example, pass a 0x50-byte mono VAGp buffer (0x30-byte header plus two 16-byte frames), set the big-endian data size at 0x0c to 80, and leave bit 1 of the second frame's flag byte clear. ParseVag's minimum-size check passes, the dropped-frame rule computes 112 samples, and VagBlock returns a 64-byte block at offset 0x30 although only 32 payload bytes were supplied. AddWaveformBlocks accepts that block, and Ngs2DecodeVag reads all four frames from the pointer.

Please reject a declared data size larger than size - VAG_HEADER_SIZE before building any blocks (returning the invalid-waveform-data result is consistent with the other truncation checks), for both the in-memory and file entry points. Add a regression for this two-frame/80-byte case that asserts rejection and a cleared output structure. The existing 64-byte declared-size test has only a one-frame discrepancy after the dropped-frame rule, so its calculated three-frame block still happens to fit the three supplied frames and does not cover this overread.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5651422996 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11388#pullrequestreview-5651422996). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T11:44:29Z. **Review:** CHANGES_REQUESTED --- The VAG parser accepts a declared payload that extends beyond the buffer passed to it, then returns a block that can make `Ngs2DecodeVag` read past that buffer. For example, pass a 0x50-byte mono `VAGp` buffer (0x30-byte header plus two 16-byte frames), set the big-endian data size at 0x0c to 80, and leave bit 1 of the second frame's flag byte clear. `ParseVag`'s minimum-size check passes, the dropped-frame rule computes 112 samples, and `VagBlock` returns a 64-byte block at offset 0x30 although only 32 payload bytes were supplied. `AddWaveformBlocks` accepts that block, and `Ngs2DecodeVag` reads all four frames from the pointer. Please reject a declared data size larger than `size - VAG_HEADER_SIZE` before building any blocks (returning the invalid-waveform-data result is consistent with the other truncation checks), for both the in-memory and file entry points. Add a regression for this two-frame/80-byte case that asserts rejection and a cleared output structure. The existing 64-byte declared-size test has only a one-frame discrepancy after the dropped-frame rule, so its calculated three-frame block still happens to fit the three supplied frames and does not cover this overread.
MarcZX10 commented 2026-10-26 12:30:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T12:30:48Z.

Review: APPROVED


Reviewed exact updated head 35ba20bee8f0411bb46efd49d4c3dfa79464875d. The declared VAG payload is now checked against the actual remaining buffer before block construction; both data and file entry points use this parser and clear the output structure first. The new tests exercise the two-frame/80-byte over-declaration through both APIs, assert the output stays cleared, cover the partial-frame and sample-count boundaries, and retain the existing loop-marker cases. My earlier overread finding is resolved. The author reports ngs2_vag and all 17 NGS2 tests passing, plus sample-identical title audio; these are author-run, not independently reproduced here. Hosted conventions, eligibility, labels and report pass; product/build jobs are skipped by path policy. The intervening main commits do not modify libSceNgs2.native; the #11315 merge-order note separately documents how to retain this VAG reader with that sampler change. AI-assisted review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5651947850 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11388#pullrequestreview-5651947850). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T12:30:48Z. **Review:** APPROVED --- Reviewed exact updated head `35ba20bee8f0411bb46efd49d4c3dfa79464875d`. The declared VAG payload is now checked against the actual remaining buffer before block construction; both data and file entry points use this parser and clear the output structure first. The new tests exercise the two-frame/80-byte over-declaration through both APIs, assert the output stays cleared, cover the partial-frame and sample-count boundaries, and retain the existing loop-marker cases. My earlier overread finding is resolved. The author reports `ngs2_vag` and all 17 NGS2 tests passing, plus sample-identical title audio; these are author-run, not independently reproduced here. Hosted conventions, eligibility, labels and report pass; product/build jobs are skipped by path policy. The intervening main commits do not modify `libSceNgs2.native`; the #11315 merge-order note separately documents how to retain this VAG reader with that sampler change. AI-assisted review.
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
boykopovar/AnyPS5!11388
No description provided.