fix(ajm): restart the stream total at a gapless reset #12322

Merged
Tamalero merged 2 commits from upstream-pr/12322 into main 2026-10-29 10:52:52 +00:00
Tamalero commented 2026-10-28 09:23:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Tamalero on 2026-10-28T09:23:56Z.


What

Ratchet & Clank: Rift Apart (PPSA01474) hangs on its title screen when it seeks a 4-channel ATRAC9 stream. The game sends sceAjmBatchJobSetGaplessDecode with the reset flag and a new total, then counts the new segment from the stream sideband's total decoded samples.

Cause: the SetGaplessDecode reset clears segmentSamples and skippedSamples but keeps totalDecodedSamples, so every later run reports the samples decoded before the seek as well. In the hang, the stream had decoded 10,944 samples before a seek to a 343,028-sample segment. When the voice hung, the segment had decoded 334,272 samples and the sideband reported 345,216. The game's voice reached its end 8,756 samples early (343,028 − 334,272), and the sideband claimed 2,188 samples past it (345,216 − 343,028). The two add up to 10,944. The voice then owes 140 samples it never gets, and the mixer loop holding the main thread's lock never exits. The trace and the core analysis are in #11700 (https://github.com/boykopovar/AnyPS5/issues/11700#issuecomment-6462306535).

Fix: the reset branch of SetGaplessDecode also sets totalDecodedSamples to 0, for every codec. This matches ClearContext in the same file, which already zeroes it on a CONTROL_RESET, and shadPS4's AjmInstance::Reset() (m_total_samples = 0), which runs for every job with the reset flag. Not measured on a console; the debt entry says so.

Related, already fixed on main: the constant tone I reported in #11700 was not AJM. About 20 of the title's object ports get their PCM from the same two scratch buffers, set one port after another, and AudioOut2 read every pointer at Advance, so each port mixed the last port's grain. A local copy of each grain at sceAudioOut2PortSetAttributes removed it (no full-scale grain in 183 one-second summaries after the title, against 119 of 119 before). #8098 fixes the same thing on main.

This changes the expectation that #6040 added to TestOpusGaplessReset: each segment's total now restarts instead of accumulating. #6040 kept the cumulative count as a choice, without console data. @Ondra97, if you know of a title or a measurement that needs the count kept across a SetGaplessDecode reset, I'd like to hear it.

Tested

Linux 7.3 (CachyOS), GCC 16.2.1, Release, BUILD_TESTING=ON, on main 316615d04.

  • New TestAt9GaplessResetRestartsTotal: a run decodes 1,024 samples, a SetGaplessDecode reset sets a 512-sample segment, and the next run must report a total of 512. A later SetGaplessDecode without the reset must keep counting.
  • Negative checks: with Ajm.cpp from main, guest_ajm fails. The first failure is in TestOpusGaplessReset. With the Opus calls skipped, the new test fails at its second run, which reports a total of 1,536 instead of 512.
  • ctest -R 'ajm|audio': 65/65 passed. tools/check_conventions.py --base origin/main passes.
  • Title: PPSA01474 on an RTX 4090 (NVIDIA 615.78.08), on an older main (abed03546) with #9749, #10906, #10000 and local probes. With this change as a switch, the tail rule from #11700, and local equivalents of #8098 and #11838 (both merged since), the title got through the title screen, save slot, difficulty and control style screens and the opening cutscene, to the first playable stage. Without this change (tail rule alone) it hung on the title screen as described above. One run each.

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: none (#11700 is the separate overrun after a gapless total)
  • Unblocks: none alone; with the #11700 tail rule, PPSA01474 #2172 gets past its title screen
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

Tested and written by Claude (AI assistant) on a contributor's machine.

image
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5048131779 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12322). Originally posted by [Tamalero](https://github.com/Tamalero) on 2026-10-28T09:23:56Z. --- ### What Ratchet & Clank: Rift Apart (PPSA01474) hangs on its title screen when it seeks a 4-channel ATRAC9 stream. The game sends `sceAjmBatchJobSetGaplessDecode` with the reset flag and a new total, then counts the new segment from the stream sideband's total decoded samples. **Cause:** the `SetGaplessDecode` reset clears `segmentSamples` and `skippedSamples` but keeps `totalDecodedSamples`, so every later run reports the samples decoded before the seek as well. In the hang, the stream had decoded 10,944 samples before a seek to a 343,028-sample segment. When the voice hung, the segment had decoded 334,272 samples and the sideband reported 345,216. The game's voice reached its end 8,756 samples early (343,028 − 334,272), and the sideband claimed 2,188 samples past it (345,216 − 343,028). The two add up to 10,944. The voice then owes 140 samples it never gets, and the mixer loop holding the main thread's lock never exits. The trace and the core analysis are in #11700 (https://github.com/boykopovar/AnyPS5/issues/11700#issuecomment-6462306535). **Fix:** the reset branch of `SetGaplessDecode` also sets `totalDecodedSamples` to 0, for every codec. This matches `ClearContext` in the same file, which already zeroes it on a `CONTROL_RESET`, and shadPS4's `AjmInstance::Reset()` (`m_total_samples = 0`), which runs for every job with the reset flag. Not measured on a console; the debt entry says so. Related, already fixed on `main`: the constant tone I reported in #11700 was not AJM. About 20 of the title's object ports get their PCM from the same two scratch buffers, set one port after another, and AudioOut2 read every pointer at `Advance`, so each port mixed the last port's grain. A local copy of each grain at `sceAudioOut2PortSetAttributes` removed it (no full-scale grain in 183 one-second summaries after the title, against 119 of 119 before). #8098 fixes the same thing on `main`. This changes the expectation that #6040 added to `TestOpusGaplessReset`: each segment's total now restarts instead of accumulating. #6040 kept the cumulative count as a choice, without console data. @Ondra97, if you know of a title or a measurement that needs the count kept across a `SetGaplessDecode` reset, I'd like to hear it. ### Tested Linux 7.3 (CachyOS), GCC 16.2.1, Release, `BUILD_TESTING=ON`, on `main` 316615d04. - New `TestAt9GaplessResetRestartsTotal`: a run decodes 1,024 samples, a `SetGaplessDecode` reset sets a 512-sample segment, and the next run must report a total of 512. A later `SetGaplessDecode` without the reset must keep counting. - **Negative checks:** with `Ajm.cpp` from `main`, `guest_ajm` fails. The first failure is in `TestOpusGaplessReset`. With the Opus calls skipped, the new test fails at its second run, which reports a total of 1,536 instead of 512. - `ctest -R 'ajm|audio'`: 65/65 passed. `tools/check_conventions.py --base origin/main` passes. - Title: PPSA01474 on an RTX 4090 (NVIDIA 615.78.08), on an older `main` (abed03546) with #9749, #10906, #10000 and local probes. With this change as a switch, the tail rule from #11700, and local equivalents of #8098 and #11838 (both merged since), the title got through the title screen, save slot, difficulty and control style screens and the opening cutscene, to the first playable stage. Without this change (tail rule alone) it hung on the title screen as described above. One run each. ### Checklist - [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: none (#11700 is the separate overrun after a gapless total) - [x] Unblocks: none alone; with the #11700 tail rule, PPSA01474 #2172 gets past its title screen - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) *Tested and written by Claude (AI assistant) on a contributor's machine.* <img width="1187" height="685" alt="image" src="https://github.com/user-attachments/assets/710ae1a6-fa46-4e6f-9924-612334579293" />
MarcZX10 commented 2026-10-28 09:31:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T09:31:40Z.

Review: APPROVED


The reset branch clears totalDecodedSamples alongside the segment counters, and the updated AT9/Opus tests verify the total restarts after reset but continues across a non-reset gapless update. The author reports 65/65 focused AJM/audio tests, including a negative control and the traced Ratchet & Clank title path; this was not measured on a physical PS5. Hosted Windows, conventions, intake and reporting pass. No blocking issue found on head 08fdabc2d9fa.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5680480786 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12322#pullrequestreview-5680480786). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T09:31:40Z. **Review:** APPROVED --- The reset branch clears `totalDecodedSamples` alongside the segment counters, and the updated AT9/Opus tests verify the total restarts after reset but continues across a non-reset gapless update. The author reports 65/65 focused AJM/audio tests, including a negative control and the traced Ratchet & Clank title path; this was not measured on a physical PS5. Hosted Windows, conventions, intake and reporting pass. No blocking issue found on head `08fdabc2d9fa`.
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!12322
No description provided.