Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
kali113:fix/ajm-at9-gapless-stays-ended
Nov 14, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
kali113:fix/ajm-at9-gapless-stays-ended

Conversation

@kali113

@kali113 kali113 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

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

… total

On a PS5 (firmware 13.590.001, real libSceAjm.native in a payload process,
a title's stereo 48 kHz ATRAC9 stream of 384-byte superframes) the
gapless total only limits the output. The run that reaches it consumes
all of its input and writes exactly `total` samples, whatever frame or
superframe the total falls in (totals 300, 512, 1100 and 1500). Later
runs consume their input and write nothing, and the total of decoded
samples stays at the total, until SetGaplessDecode with the reset flag
starts a new segment.

RunAt9 stopped at the total and left the rest of its input unconsumed,
then started a new segment on the next run. It now keeps decoding to
consume the input and leaves the segment ended until a reset. The
gapless segments test is rewritten to these results.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 20:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JJenkx

JJenkx commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 7436b8db6d, merged onto main 65351c2931 (7436b8db6d, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_ajm passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Tamalero

Copy link
Copy Markdown
Contributor

@kali113 the Ratchet & Clank: Rift Apart title-screen check you asked for on #11700, on Linux/NVIDIA. With this PR the title screen appears and the game plays on into the opening; on the same main without it, the game freezes before "Press X" appears. Neither build has any local AJM change: the gapless tail rule from my earlier #11700 runs is in neither.

main main + #18522
Logos the audio cuts out normal
"Press X" title screen never appears; presents stop at 413, about 90 s in, and nothing changes for the next 2 minutes appears and keeps running
After Cross — the music starts, the menus work and the opening Rivet sequence plays; closed by hand after 812 s and 1,551 presents

This is one run per build, each from a cold shader cache, and the driving was by hand (Cross on the title and in the menus), so the timings are rough. The freeze on main is complete, though, and it comes before the point the PR run passed at about 4 minutes. A 5 s CPU sample of the frozen main run shows its time in the title's own code and libc, not in libSceAjm.native, so the title is waiting rather than the decoder spinning, as #11700 describes.

Builds and runs

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

@kali113

kali113 commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

@Tamalero thank you, that's the check I was hoping for. On the same main, the title freezes before "Press X" without this PR and reaches the title screen and the opening with it. I'll add your run to the PR description as title evidence, as one run per build on Linux/NVIDIA.

AI-assisted: yes (Claude Code)

@Tamalero

Tamalero commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

@kali113 an AMD data point for the same build, 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 in the parade crowd ("L to Move, R to Look"). I stopped it there, after 22 minutes. In my earlier runs on this GPU, Ratchet & Clank: Rift Apart never reached its title.

Build and run
  • main 4e2acc2120 + a merge of 7436b8db6d (no conflicts); the same commits as the NVIDIA runs, built on the VM.
  • Relinked without --to-intel (AMD CPU).
  • Switches: APS5_MAX_COMPUTE_SPIRV_WORDS=2000000 APS5_SKIP_REFUSED_DISPATCHES=1 APS5_HOST_IMPORT_MIB=12288, and APS5_PAD_INPUT_FILE for the presses.
  • In the log: 2 dropped dispatch programs (the oversized ones), no not implemented, no aborts, no write-back failures, no pointer-chase errors.
  • Ubuntu 24.04.5 VM (kernel 7.0.0), Radeon PRO V620 (Navi 21), Mesa 25.2.8 RADV, AMD EPYC 7452 (24 vCPUs), GCC 13.3, Release. PPSA01474, content version 01.005.003.
image image

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

@kali113

kali113 commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

@Tamalero thank you, the Radeon run helps: the title, menus and opening come up on RADV with this PR, with the #13538 limit set. I'll add it to the PR description as an AMD data point, noting there is no main control on that GPU and that the title also needs the #13538 limit there.

@blawesom

Copy link
Copy Markdown
Contributor

Validation: q42 — PR #18522 (libSceAjm.native ATRAC9 gapless segment)

Automated sequential-queue validation (item 42 of 50). Verdict: PASS (Linux/AMD scoped).

Environment

  • Machine: CachyOS, kernel 7.3.4-1-cachyos, Ryzen 7 9800X3D, AMD Radeon RX 9070 XT (RADV GFX1201) + AI PRO R9700, /dev/kfd present.
  • PR state at check: OPEN, not locked, head 7436b8db6db215935851b495117c53a0908eb5f1 (matches queue entry).
  • BASE: origin/main = a9f291040088cac6babfbe270c50f344a4f6f27d, reused read-only from /home/benj/scratch/wt5/base-main (clean, build/build.ninja present).
  • PR tree: /home/benj/scratch/wt5/pr-18522 at 7436b8db6db215935851b495117c53a0908eb5f1 (parent 65351c2931e, i.e. a stale branch base), submodules initialized.

Build (PR tree)

  • cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DBUILD_TESTING=ON → exit 0, wall 10m42s (log q42-pr18522-configure.log).
  • cmake --build build --parallel 12 (CMAKE_BUILD_PARALLEL_LEVEL=12, MAKEFLAGS=-j12) → exit 0, wall 5m33s (log q42-pr18522-build.log).

Focused test pattern

  • Pattern: ^(atrac9_config_data|atrac9_gradient|guest_ajm|guest_ajm_aac_multichannel|guest_ajm_driver_queries|guest_ajm_teardown|guest_ajm_vibration|guest_ajm_wvorbis|guest_audiodec_ajm_context|ngs2_atrac9|patch_lib_atrac9)$ — 11 tests, identical matched set in both trees (ctest -N -R name lists diffed, identical; saved q42-pattern.txt, q42-pattern-*-N.txt, q42-pattern-*-names.txt).
  • Note: the broader ajm|atrac also matches guest_ajm_initialize_context_pointer and guest_audiodec_ajm_context_pointer on main only; those were added on main after this PR's branch base (e.g. 9de20334dd7 fix(libSceAjm.native): validate the guest context pointer in sceAjmInitialize #18238), not by the PR commit, so the anchored 11-test pattern was used for the identical-set comparison.

Test runs (both trees, identical env, timeout 3600)

  • Env: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_ADD_LAYER_PATH=/home/benj/scratch/vvl-layer VK_LOADER_DEBUG=layer ANYPS5_GPU='AMD Radeon RX 9070 XT'
  • Command: ctest --test-dir build -R '<pattern>' -V
  • Base (q42-tests-base.log): 11/11 Passed, 0 failed/skipped, exit 0, wall 0.27s.
  • PR (q42-tests-pr.log): 11/11 Passed, 0 failed/skipped, exit 0, wall 0.24s.
  • Per-test comparison: all 11 tests (patch_lib_atrac9, atrac9_gradient, atrac9_config_data, guest_ajm, guest_ajm_aac_multichannel, guest_ajm_vibration, guest_ajm_driver_queries, guest_ajm_wvorbis, guest_ajm_teardown, guest_audiodec_ajm_context, ngs2_atrac9) Passed in both trees; no pass/fail/skip differences. guest_ajm (which contains the rewritten gapless-segments test) passes on the PR tree.

Vulkan layer checks (per AGENTS.md)

  • Missing-layer phrase was not found but was requested by env var VK_INSTANCE_LAYERS: absent in both logs.
  • Insert instance layer "VK_LAYER_KHRONOS_validation": absent in both logs because this matched set is CPU-only — the AJM/ATRAC9 guest decoder tests create no Vulkan instance (zero VK_/vulkan lines in either log), so no instance-layer insertion is expected. The env vars were still set identically on both runs as required.
  • VUID-/SYNC-HAZARD-/bracketed diagnostic IDs: none in either log → the PR adds no new validation IDs (empty diff).

Push

  • git push fork +7436b8db6db215935851b495117c53a0908eb5f1:refs/heads/validation/pr-18522 → succeeded (blawesom/AnyPS5 branch validation/pr-18522).

Limitations

  • Scoped to Linux + AMD RADV (RX 9070 XT); no Windows/macOS/NVIDIA coverage.
  • The focused set is CPU-only, so no GPU/Vulkan validation-layer evidence was exercised; AGC/shader-recompiler/VideoOut paths are untouched by this PR (it changes core/libs/prx/libSceAjm.native/src/Ajm.cpp, core/libs/tests/GuestAjm.cpp, docs/dev/debt/libSceAjm.native.md only).
  • The PR branch is based on a stale main (65351c2931e); two AJM tests present on current main do not exist on the branch base and were excluded from the identical-set pattern (attributed to main-side commits, not the PR).
  • Single run per tree; no repetition/statistical claim.

@kali113

kali113 commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @blawesom for the Linux/AMD run (11/11 on both trees). The stale branch base doesn't affect the change: a merge with current main is clean.

AI-assisted: yes (Claude Code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs prx unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants