fix(libSceAjm.native): keep an ATRAC9 gapless segment ended after its total #18522

Merged
kali113 merged 1 commit from upstream-pr/18522 into main 2026-11-14 17:46:53 +00:00
kali113 commented 2026-11-11 20:01:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kali113 on 2026-11-11T20:01:18Z.


What

A run of an ATRAC9 instance with a gapless total stopped decoding when the output reached the total, left the rest of its input unconsumed, and the next run started a new segment. On a PS5 it does neither. This makes the library match the console, as measured below.

Measured on a PS5 (firmware 13.590.001, the real libSceAjm.native called from a payload process) with 16 consecutive superframes of a title's own ATRAC9 file (stereo, 48 kHz, config FE 74 0B F0, 384-byte superframes of four frames; the bytes are not attached). The full table, with the probe source, is in this comment on #11700:

  • The run that reaches the total consumes all of its input and writes exactly total samples: totals 300, 512, 1100 and 1500, the last ending inside a run of two superframes (inputConsumed 384 or 768, frames 4 or 8, outputWritten 1200, 2048, 304 for the 76 samples left of 1100, and 6000 bytes).
  • Later runs consume their input and write nothing; totalDecodedSamples stays at the total. SetGaplessDecode with the same total and reset 0 changes nothing.
  • SetGaplessDecode with the reset flag starts a new segment: the next run wrote 2048 bytes again for total 512.

RunAt9 no longer breaks out of its loop at the total (output was already capped per frame) and no longer zeroes the segment counter after a run that ended it. Resets still do (the reset flag, ClearContext, initialization).

TestAt9GaplessSegments pinned the opposite for a mono synthetic stream (the next run decodes a new segment). It now checks the measured behaviour for totals 512, 300, 1500 and 1100, plus the reset. TestAt9GaplessResetRestartsTotal feeds a whole superframe on its last run, since the previous run now consumes the whole input.

Not measured, and said so in the debt entry: a new total without the reset flag after the end (the segment resumes up to it here), a nonzero skip, mono, other codecs. The tests use the repository's mono synthetic stream, not the measured stereo one.

Risk. The debt entry this replaces says FMOD in PPSA29141 needed the frames after the total decoded as a new segment (#2052). The console does not do that, so I expect the change to be right for that title too, but I did not run PPSA29141 myself. Ratchet & Clank: Rift Apart (PPSA01474, which hangs on the old behaviour per #11700) was run by @Tamalero on Linux/NVIDIA, one run per build (comment): on the same main it freezes before "Press X"; with this PR the title screen appears and the opening plays. A second run by @Tamalero on a Radeon PRO V620 (RADV), main 4e2acc2120 + this PR, reaches the title screen, goes through the menus and the opening and hands the player control, with the #13538 limit set; there is no run of main on that GPU, so it does not show what the PR changes there. PPSA29141 is still unverified.

Tested

Linux x86-64 (Ubuntu container, GCC 15.2): all six guest_ajm* tests pass. With Ajm.cpp reverted to main, guest_ajm aborts, so the rewritten test pins the behaviour. Not run on Windows or macOS. Console measurements as above, one console, one stream.

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
  • Unblocks: PPSA01474 (title screen and opening reached in @Tamalero's runs on Linux/NVIDIA, one run per build, and on Radeon PRO V620 with the #13538 limit set)
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5266000856 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18522). Originally posted by [kali113](https://github.com/kali113) on 2026-11-11T20:01:18Z. --- ### What A run of an ATRAC9 instance with a gapless total stopped decoding when the output reached the total, left the rest of its input unconsumed, and the next run started a new segment. On a PS5 it does neither. This makes the library match the console, as measured below. Measured on a PS5 (firmware 13.590.001, the real `libSceAjm.native` called from a payload process) with 16 consecutive superframes of a title's own ATRAC9 file (stereo, 48 kHz, config `FE 74 0B F0`, 384-byte superframes of four frames; the bytes are not attached). The full table, with the probe source, is in [this comment on #11700](https://github.com/boykopovar/AnyPS5/issues/11700#issuecomment-6805893234): - The run that reaches the total consumes **all of its input** and writes exactly `total` samples: totals 300, 512, 1100 and 1500, the last ending inside a run of two superframes (`inputConsumed` 384 or 768, `frames` 4 or 8, `outputWritten` 1200, 2048, 304 for the 76 samples left of 1100, and 6000 bytes). - **Later runs consume their input and write nothing**; `totalDecodedSamples` stays at the total. `SetGaplessDecode` with the same total and reset 0 changes nothing. - **`SetGaplessDecode` with the reset flag starts a new segment**: the next run wrote 2048 bytes again for total 512. `RunAt9` no longer breaks out of its loop at the total (output was already capped per frame) and no longer zeroes the segment counter after a run that ended it. Resets still do (the reset flag, `ClearContext`, initialization). `TestAt9GaplessSegments` pinned the opposite for a mono synthetic stream (the next run decodes a new segment). It now checks the measured behaviour for totals 512, 300, 1500 and 1100, plus the reset. `TestAt9GaplessResetRestartsTotal` feeds a whole superframe on its last run, since the previous run now consumes the whole input. Not measured, and said so in the debt entry: a new total without the reset flag after the end (the segment resumes up to it here), a nonzero skip, mono, other codecs. The tests use the repository's mono synthetic stream, not the measured stereo one. **Risk.** The debt entry this replaces says FMOD in PPSA29141 needed the frames after the total decoded as a new segment (#2052). The console does not do that, so I expect the change to be right for that title too, but I did not run PPSA29141 myself. Ratchet & Clank: Rift Apart (PPSA01474, which hangs on the old behaviour per #11700) was run by @Tamalero on Linux/NVIDIA, one run per build ([comment](https://github.com/boykopovar/AnyPS5/pull/18522#issuecomment-6846101666)): on the same `main` it freezes before "Press X"; with this PR the title screen appears and the opening plays. A second run by @Tamalero on a Radeon PRO V620 (RADV), `main` `4e2acc2120` + this PR, reaches the title screen, goes through the menus and the opening and hands the player control, with the #13538 limit set; there is no run of `main` on that GPU, so it does not show what the PR changes there. PPSA29141 is still unverified. ### Tested Linux x86-64 (Ubuntu container, GCC 15.2): all six `guest_ajm*` tests pass. With `Ajm.cpp` reverted to `main`, `guest_ajm` aborts, so the rewritten test pins the behaviour. Not run on Windows or macOS. Console measurements as above, one console, one stream. ### 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 - [x] Unblocks: PPSA01474 (title screen and opening reached in @Tamalero's runs on Linux/NVIDIA, one run per build, and on Radeon PRO V620 with the #13538 limit set) - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
copilot-pull-request-reviewer[bot] commented 2026-11-11 20:01:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-11T20:01:27Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5864695736 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18522#pullrequestreview-5864695736). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-11T20:01:27Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
PrivacyOnion commented 2026-11-12 06:08:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T06:08:34Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 7436b8db6d:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_ajm_tests built cleanly.
  • CTest (guest_ajm, guest_ajm_aac_multichannel, guest_ajm_vibration, guest_ajm_driver_queries, guest_ajm_wvorbis, guest_ajm_teardown):
    1/6 Test #2831: guest_ajm_vibration ..............   Passed    0.04 sec
    2/6 Test #2832: guest_ajm_driver_queries .........   Passed    0.02 sec
    3/6 Test #2833: guest_ajm_wvorbis ................   Passed    0.02 sec
    4/6 Test #2834: guest_ajm_teardown ...............   Passed    0.03 sec
    5/6 Test #2830: guest_ajm_aac_multichannel .......   Passed    0.20 sec
    6/6 Test #2829: guest_ajm ........................   Passed    0.18 sec
    100% tests passed, 0 tests failed out of 6
    
  • Negative control: with core/libs/prx/libSceAjm.native/src/Ajm.cpp reverted to main and the new tests kept, the suite fails:
    1/6 Test #2830: guest_ajm_aac_multichannel .......   Passed    0.17 sec
    2/6 Test #2831: guest_ajm_vibration ..............   Passed    0.04 sec
    3/6 Test #2834: guest_ajm_teardown ...............   Passed    0.03 sec
    4/6 Test #2833: guest_ajm_wvorbis ................   Passed    0.03 sec
    5/6 Test #2832: guest_ajm_driver_queries .........   Passed    0.02 sec
    6/6 Test #2829: guest_ajm ........................Subprocess aborted***Exception:   0.67 sec
    83% tests passed, 1 tests failed out of 6
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libSceAjm.native/src/Ajm.cpp:RunAt9: Verified that roomForFrame evaluates to true when segmentEnded() is true, allowing full consumption of input superframes without falsely returning AJM_RESULT_NOT_ENOUGH_ROOM when no output buffer space remains.
    • core/libs/prx/libSceAjm.native/src/Ajm.cpp:EmitFrame: Verified that when segmentEnded() holds (instance.segmentSamples >= instance.gapless.totalSamples), remaining is calculated as 0, preventing any sample emission to PcmOutputs or extraneous buffering in ResamplerPush.
    • core/libs/prx/libSceAjm.native/src/Ajm.cpp:RunAt9: Verified the removal of the post-loop segmentSamples zeroing, preserving the ended segment state across subsequent jobs until explicitly cleared by SetGaplessDecode with the reset flag, ClearContext, or re-initialization.
    • core/libs/tests/GuestAjm.cpp:TestAt9GaplessSegments: Verified unit test coverage across gapless boundaries within, across, and matching superframe boundaries (totals 300, 512, 1100, 1500), verifying zero output produced, full input consumption, and segment restart verification with proper resource teardown via sceAjmInstanceDestroy.
    • core/libs/tests/GuestAjm.cpp:TestAt9GaplessResetRestartsTotal: Verified that updating the job invocation to pass the full stream accurately reflects that the preceding run consumed its entire input buffer, properly validating totalDecodedSamples accumulation.
    • docs/dev/debt/libSceAjm.native.md: Verified AGENTS.md compliance for technical debt updates, noting console test measurements on firmware 13.590.001 and explicitly listing unmeasured cases (mono, nonzero skips, and totals modified without reset).
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5869883852 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18522#pullrequestreview-5869883852). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T06:08:34Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `7436b8db6d`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_ajm_tests` built cleanly. - **CTest** (`guest_ajm`, `guest_ajm_aac_multichannel`, `guest_ajm_vibration`, `guest_ajm_driver_queries`, `guest_ajm_wvorbis`, `guest_ajm_teardown`): ``` 1/6 Test #2831: guest_ajm_vibration .............. Passed 0.04 sec 2/6 Test #2832: guest_ajm_driver_queries ......... Passed 0.02 sec 3/6 Test #2833: guest_ajm_wvorbis ................ Passed 0.02 sec 4/6 Test #2834: guest_ajm_teardown ............... Passed 0.03 sec 5/6 Test #2830: guest_ajm_aac_multichannel ....... Passed 0.20 sec 6/6 Test #2829: guest_ajm ........................ Passed 0.18 sec 100% tests passed, 0 tests failed out of 6 ``` - **Negative control**: with `core/libs/prx/libSceAjm.native/src/Ajm.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/6 Test #2830: guest_ajm_aac_multichannel ....... Passed 0.17 sec 2/6 Test #2831: guest_ajm_vibration .............. Passed 0.04 sec 3/6 Test #2834: guest_ajm_teardown ............... Passed 0.03 sec 4/6 Test #2833: guest_ajm_wvorbis ................ Passed 0.03 sec 5/6 Test #2832: guest_ajm_driver_queries ......... Passed 0.02 sec 6/6 Test #2829: guest_ajm ........................Subprocess aborted***Exception: 0.67 sec 83% tests passed, 1 tests failed out of 6 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libSceAjm.native/src/Ajm.cpp:RunAt9: Verified that roomForFrame evaluates to true when segmentEnded() is true, allowing full consumption of input superframes without falsely returning AJM_RESULT_NOT_ENOUGH_ROOM when no output buffer space remains. - core/libs/prx/libSceAjm.native/src/Ajm.cpp:EmitFrame: Verified that when segmentEnded() holds (instance.segmentSamples >= instance.gapless.totalSamples), remaining is calculated as 0, preventing any sample emission to PcmOutputs or extraneous buffering in ResamplerPush. - core/libs/prx/libSceAjm.native/src/Ajm.cpp:RunAt9: Verified the removal of the post-loop segmentSamples zeroing, preserving the ended segment state across subsequent jobs until explicitly cleared by SetGaplessDecode with the reset flag, ClearContext, or re-initialization. - core/libs/tests/GuestAjm.cpp:TestAt9GaplessSegments: Verified unit test coverage across gapless boundaries within, across, and matching superframe boundaries (totals 300, 512, 1100, 1500), verifying zero output produced, full input consumption, and segment restart verification with proper resource teardown via sceAjmInstanceDestroy. - core/libs/tests/GuestAjm.cpp:TestAt9GaplessResetRestartsTotal: Verified that updating the job invocation to pass the full stream accurately reflects that the preceding run consumed its entire input buffer, properly validating totalDecodedSamples accumulation. - docs/dev/debt/libSceAjm.native.md: Verified AGENTS.md compliance for technical debt updates, noting console test measurements on firmware 13.590.001 and explicitly listing unmeasured cases (mono, nonzero skips, and totals modified without reset). - Note: the AI-assisted checkbox can stay. Looks Good To Me
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!18522
No description provided.