fix(avplayer): keep handed-out video frames until later frames are handed out #1601

Open
awake-devel wants to merge 1 commit from upstream-pr/1601 into main
awake-devel commented 2026-10-08 14:06:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by awake-devel on 2026-10-08T14:06:59Z.


Investigated and written by Claude Code (AI), and posted by me after review.

Work in progress. The number of frames kept (5) comes from an experiment with one game, not from console documentation. The right lifetime rule for a handed-out video frame needs review; see "Open question" below.

What

A title that asks AvPlayer for 2 output video framebuffers (num_output_video_framebuffers = 2) got no protection for the frame it was shown last. In Source.cpp, present keeps videoBuffers - 3 earlier handed-out frames, which is 0 for 2 or 3 buffers, so the previous frame's buffer goes back to the free list as soon as the next frame is handed out. acquireBuffer then takes the newest free buffer first, so the decoder writes the next decoded picture into exactly that buffer right away. AnyPS5's GPU work runs behind the game thread, and can still be reading that buffer for the frame the game already submitted.

In Hollow Knight: Silksong (PPSA12544) this shows in the intro cinematic: whole frames briefly jump back about 0.3 s (sometimes forward), and one frame was torn. Discussion: https://github.com/boykopovar/AnyPS5/discussions/1519

The change:

  • After the 4 optional decode-ahead framebuffers, the player asks the memory replacement for 4 more optional framebuffers. When it gets them, it keeps at least the 5 newest handed-out frames intact (the current one and 4 earlier ones). When memory is short, the behaviour is the same as before.
  • Free framebuffers are reused oldest first instead of newest first, so a buffer that was just released is the last one to be written again.

The unknown console rule is recorded in docs/dev/TechnicalDebt.md.

Open question

The fix keeps a fixed number of frames; it does not know when the GPU has actually read a frame. AvPlayer has no link to the AGC driver today. A rule tied to GPU completion (for example waiting until the GPU work submitted before the next GetVideoData has finished before reusing a buffer) would be exact, but it needs a hook between the two libraries, and how the console itself handles this (how long a frame from sceAvPlayerGetVideoData stays valid) is not documented. Reviewers who know the console behaviour, or prefer the GPU-completion approach, are welcome to say so.

Tested

Arch Linux, GCC, Release, -DBUILD_TESTING=ON; Intel i7-8750H, GTX 1050 Ti Max-Q with NVIDIA 580.178.04. Rebased onto main 406b5e1c; the results below are on 406b5e1c unless marked as from the earlier base cba8d86d.

  • guest_avplayer: new cases check that with 2 output buffers the 5 newest handed-out frames are not overwritten (and with 6 buffers), and that the 4 extra framebuffers are requested only when the texture memory allows. Passes; with only the Source.cpp change reverted it fails (checked on cba8d86d).
  • Full ctest -j4: 482/482 passed, 0 skipped, 0 failed, the same set as main 406b5e1c.
  • Full suite under VK_LAYER_KHRONOS_validation (on cba8d86d): 452/452 passed, 0 skipped; the same Validation Error VUIDs and counts as main.
  • tools/check_conventions.py --base 406b5e1c and tools/check_test_registration.py build: pass.
  • Silksong intro cinematic (before the rebase), window recordings counted with the same frame-comparison script (a frame that differs from both neighbours while the neighbours match each other):
    • before (main behaviour): 63 such events in about 130 s of cinematic (50 plain jumps, the rest around the cinematic's own flashes);
    • with an experimental switch that kept 4 more frames and reused buffers oldest first: 3 events (2 small jumps and 1 of the cinematic's own white flashes);
    • with this PR (on a local branch with other open PRs merged, which Silksong needs to run): the cinematic played through and the game reached gameplay and saved. The window-only recordings came out incomplete (other windows covered the game area, and the recorder captured only part of the cinematic), so they give no reliable count: 1 event in 58 s and 0 in 24 s of usable footage.
  • With the change, the version text on the title screen and the "skip" prompt during the cinematic appear again (they were missing before).

Not changed by this PR: in the same cinematic the sound plays well ahead of the picture (the user estimates up to about a minute by the end). It was already there before this change, and the picture does not look slowed down, more like a growing or fixed offset. It is being investigated separately.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01HeC9bA3n74TzJRYv9AgBE8

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4788034023 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1601). Originally posted by [awake-devel](https://github.com/awake-devel) on 2026-10-08T14:06:59Z. --- > Investigated and written by Claude Code (AI), and posted by me after review. > **Work in progress.** The number of frames kept (5) comes from an experiment with one game, not from console documentation. The right lifetime rule for a handed-out video frame needs review; see "Open question" below. ### What A title that asks AvPlayer for 2 output video framebuffers (`num_output_video_framebuffers = 2`) got no protection for the frame it was shown last. In `Source.cpp`, `present` keeps `videoBuffers - 3` earlier handed-out frames, which is 0 for 2 or 3 buffers, so the previous frame's buffer goes back to the free list as soon as the next frame is handed out. `acquireBuffer` then takes the newest free buffer first, so the decoder writes the next decoded picture into exactly that buffer right away. AnyPS5's GPU work runs behind the game thread, and can still be reading that buffer for the frame the game already submitted. In Hollow Knight: Silksong (PPSA12544) this shows in the intro cinematic: whole frames briefly jump back about 0.3 s (sometimes forward), and one frame was torn. Discussion: https://github.com/boykopovar/AnyPS5/discussions/1519 The change: - After the 4 optional decode-ahead framebuffers, the player asks the memory replacement for 4 more optional framebuffers. When it gets them, it keeps at least the 5 newest handed-out frames intact (the current one and 4 earlier ones). When memory is short, the behaviour is the same as before. - Free framebuffers are reused oldest first instead of newest first, so a buffer that was just released is the last one to be written again. The unknown console rule is recorded in `docs/dev/TechnicalDebt.md`. #### Open question The fix keeps a fixed number of frames; it does not know when the GPU has actually read a frame. AvPlayer has no link to the AGC driver today. A rule tied to GPU completion (for example waiting until the GPU work submitted before the next `GetVideoData` has finished before reusing a buffer) would be exact, but it needs a hook between the two libraries, and how the console itself handles this (how long a frame from `sceAvPlayerGetVideoData` stays valid) is not documented. Reviewers who know the console behaviour, or prefer the GPU-completion approach, are welcome to say so. ### Tested Arch Linux, GCC, Release, `-DBUILD_TESTING=ON`; Intel i7-8750H, GTX 1050 Ti Max-Q with NVIDIA 580.178.04. Rebased onto `main` 406b5e1c; the results below are on 406b5e1c unless marked as from the earlier base cba8d86d. - `guest_avplayer`: new cases check that with 2 output buffers the 5 newest handed-out frames are not overwritten (and with 6 buffers), and that the 4 extra framebuffers are requested only when the texture memory allows. Passes; with only the `Source.cpp` change reverted it fails (checked on cba8d86d). - Full `ctest -j4`: 482/482 passed, 0 skipped, 0 failed, the same set as `main` 406b5e1c. - Full suite under `VK_LAYER_KHRONOS_validation` (on cba8d86d): 452/452 passed, 0 skipped; the same `Validation Error` VUIDs and counts as `main`. - `tools/check_conventions.py --base 406b5e1c` and `tools/check_test_registration.py build`: pass. - Silksong intro cinematic (before the rebase), window recordings counted with the same frame-comparison script (a frame that differs from both neighbours while the neighbours match each other): - before (`main` behaviour): 63 such events in about 130 s of cinematic (50 plain jumps, the rest around the cinematic's own flashes); - with an experimental switch that kept 4 more frames and reused buffers oldest first: 3 events (2 small jumps and 1 of the cinematic's own white flashes); - with this PR (on a local branch with other open PRs merged, which Silksong needs to run): the cinematic played through and the game reached gameplay and saved. The window-only recordings came out incomplete (other windows covered the game area, and the recorder captured only part of the cinematic), so they give no reliable count: 1 event in 58 s and 0 in 24 s of usable footage. - With the change, the version text on the title screen and the "skip" prompt during the cinematic appear again (they were missing before). Not changed by this PR: in the same cinematic the sound plays well ahead of the picture (the user estimates up to about a minute by the end). It was already there before this change, and the picture does not look slowed down, more like a growing or fixed offset. It is being investigated separately. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01HeC9bA3n74TzJRYv9AgBE8
blawesom commented 2026-10-11 08:55:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-11T08:55:28Z.


Hi! This PR currently conflicts with main, so I rebased it onto the latest main (d3e84f85, includes #3296). The mechanical conflict in core/libs/tests/GuestAvPlayer.cpp (main added TestPs5ExtendedInitLayout/TestAutoStartKeepsStartedPlayback/TestUnsyncedVideoKeepsUpWithAudio at the same spot as this PR's TestHandedOutFramesStayIntact) resolved cleanly by keeping both sides.

However, there is now a semantic conflict that needs a decision: main's newer TestPs5ExtendedInitLayout waits for TextureCount() == Buffers + DecodeAheadBuffers (3 + 4 = 7), but with this change's 4 additional held framebuffers the count settles at 11, so that check never passes (the PR's own TestHandedOutFramesStayIntact still passes). Either the new test needs updating to expect Buffers + DecodeAheadBuffers + 4 held buffers, or the held-buffer approach needs adjusting.

The rebased branch is available for reference (it builds; the failure above is the only failing check):

git fetch https://github.com/blawesom/AnyPS5 rebase/pr-1601
git reset --hard FETCH_HEAD

Feel free to ignore this if you'd rather rebase yourself.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107335796 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1601#issuecomment-6107335796). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-11T08:55:28Z. --- Hi! This PR currently conflicts with `main`, so I rebased it onto the latest `main` (`d3e84f85`, includes #3296). The mechanical conflict in `core/libs/tests/GuestAvPlayer.cpp` (main added `TestPs5ExtendedInitLayout`/`TestAutoStartKeepsStartedPlayback`/`TestUnsyncedVideoKeepsUpWithAudio` at the same spot as this PR's `TestHandedOutFramesStayIntact`) resolved cleanly by keeping both sides. However, there is now a **semantic conflict** that needs a decision: main's newer `TestPs5ExtendedInitLayout` waits for `TextureCount() == Buffers + DecodeAheadBuffers` (3 + 4 = 7), but with this change's 4 additional held framebuffers the count settles at **11**, so that check never passes (the PR's own `TestHandedOutFramesStayIntact` still passes). Either the new test needs updating to expect `Buffers + DecodeAheadBuffers + 4` held buffers, or the held-buffer approach needs adjusting. The rebased branch is available for reference (it builds; the failure above is the only failing check): ``` git fetch https://github.com/blawesom/AnyPS5 rebase/pr-1601 git reset --hard FETCH_HEAD ``` Feel free to ignore this if you'd rather rebase yourself.
awake-devel commented 2026-10-11 10:47:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by awake-devel on 2026-10-11T10:47:54Z.


Working on this rn dw

If you can open an issue about the semantic conflict so the maintainer can take a decision about this

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6108225066 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1601#issuecomment-6108225066). Originally posted by [awake-devel](https://github.com/awake-devel) on 2026-10-11T10:47:54Z. --- Working on this rn dw If you can open an issue about the semantic conflict so the maintainer can take a decision about this
awake-devel commented 2026-10-11 14:10:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by awake-devel on 2026-10-11T14:10:27Z.


Investigated and written by Claude Code (AI), and posted by me after review.

Thanks @blawesom for the rebase and for spotting this. Since no issue exists yet, here is the decision a maintainer needs to make.

The conflict. #1601 keeps the last handed-out video frames intact. Without that, a title that asks for 2 output framebuffers gets the frame it is drawing overwritten by the decoder (Hollow Knight: Silksong, PPSA12544, intro cinematic: frames jump back about 0.3 s, one torn frame). TestPs5ExtendedInitLayout from #2159 waits for TextureCount() == Buffers + DecodeAheadBuffers (3 + 4 = 7), but #1601 allocates 4 more framebuffers, so the count becomes 11.

Where the 7 comes from. 3 is the count the test writes at 0x228. 4 is AnyPS5's own decode-ahead pool from #591, which TechnicalDebt already lists as an assumption ("the console's internal buffering behaviour is unknown"). As far as we can tell, no console documentation sets the total number of textures AvPlayer allocates; the point of the #2159 check is that the field at 0x228 is read.

Two ways to resolve it:

  1. Hold inside the existing pool (no extra allocation, the count stays 7). Keep the newest handed-out frames and reuse free framebuffers oldest first. Cost: for a title with 2 framebuffers, the pool of 6 has 1 buffer left for decoding ahead instead of 4, which is what #591 added the pool for (Unity titles in no-sync mode falling behind). I'm verifying this version now.
  2. Allocate 4 more framebuffers for the hold (the current #1601). Decode-ahead keeps its 4 buffers, and TestPs5ExtendedInitLayout expects Buffers + DecodeAheadBuffers + 4 textures.

Neither ties a frame's lifetime to the GPU having read it. That would be exact, but it needs a link from AvPlayer to the AGC driver, and how long a frame from sceAvPlayerGetVideoData stays valid on the console is unknown.

Which one would you prefer? I'll update the PR to match and mark it ready.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6109894074 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1601#issuecomment-6109894074). Originally posted by [awake-devel](https://github.com/awake-devel) on 2026-10-11T14:10:27Z. --- > Investigated and written by Claude Code (AI), and posted by me after review. Thanks @blawesom for the rebase and for spotting this. Since no issue exists yet, here is the decision a maintainer needs to make. **The conflict.** #1601 keeps the last handed-out video frames intact. Without that, a title that asks for 2 output framebuffers gets the frame it is drawing overwritten by the decoder (Hollow Knight: Silksong, PPSA12544, intro cinematic: frames jump back about 0.3 s, one torn frame). `TestPs5ExtendedInitLayout` from #2159 waits for `TextureCount() == Buffers + DecodeAheadBuffers` (3 + 4 = 7), but #1601 allocates 4 more framebuffers, so the count becomes 11. **Where the 7 comes from.** 3 is the count the test writes at 0x228. 4 is AnyPS5's own decode-ahead pool from #591, which TechnicalDebt already lists as an assumption ("the console's internal buffering behaviour is unknown"). As far as we can tell, no console documentation sets the total number of textures AvPlayer allocates; the point of the #2159 check is that the field at 0x228 is read. **Two ways to resolve it:** 1. **Hold inside the existing pool** (no extra allocation, the count stays 7). Keep the newest handed-out frames and reuse free framebuffers oldest first. Cost: for a title with 2 framebuffers, the pool of 6 has 1 buffer left for decoding ahead instead of 4, which is what #591 added the pool for (Unity titles in no-sync mode falling behind). I'm verifying this version now. 2. **Allocate 4 more framebuffers for the hold** (the current #1601). Decode-ahead keeps its 4 buffers, and `TestPs5ExtendedInitLayout` expects `Buffers + DecodeAheadBuffers + 4` textures. Neither ties a frame's lifetime to the GPU having read it. That would be exact, but it needs a link from AvPlayer to the AGC driver, and how long a frame from `sceAvPlayerGetVideoData` stays valid on the console is unknown. Which one would you prefer? I'll update the PR to match and mark it ready.
blawesom commented 2026-10-16 17:08:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-16T17:08:30Z.


Opened the semantic-conflict issue as requested: #6898 — it lays out both designs (hold within the existing 7-buffer pool vs. the +4 allocation with the TestPs5ExtendedInitLayout update) for a maintainer decision.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6220694820 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1601#issuecomment-6220694820). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-16T17:08:30Z. --- Opened the semantic-conflict issue as requested: #6898 — it lays out both designs (hold within the existing 7-buffer pool vs. the +4 allocation with the `TestPs5ExtendedInitLayout` update) for a maintainer decision.
MarcZX10 commented 2026-11-20 11:09:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T11:09:31Z.

Review: APPROVED


Reviewed — looks good

Nothing in the reviewed changes shows a blocking defect; the lifetime policy is documented as an approximation and the added tests cover the 2- and 6-buffer cases.

What I ran

Inspected head 91f77a4555c2816218b08fb6f1b622d0d1daa0cc using repo_read.py's review bundle and targeted file reads. No commands or tests were run in this review.

Results

Check Result Note
Diff and changed code passed Three changed files reviewed
Tests not run Author-reported results not independently verified

Findings

Blocking

None.

Non-blocking

None.

Not verified

Did not independently run tests or verify the author's hardware, game, or performance observations. Read all changed files listed in the manifest: core/libs/prx/libSceAvPlayer/src/Source.cpp, core/libs/tests/GuestAvPlayer.cpp, and docs/dev/TechnicalDebt.md. No changed files were excluded by the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5991725232 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1601#pullrequestreview-5991725232). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T11:09:31Z. **Review:** APPROVED --- # Reviewed — looks good Nothing in the reviewed changes shows a blocking defect; the lifetime policy is documented as an approximation and the added tests cover the 2- and 6-buffer cases. ## What I ran Inspected head `91f77a4555c2816218b08fb6f1b622d0d1daa0cc` using `repo_read.py`'s review bundle and targeted file reads. No commands or tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | Three changed files reviewed | | Tests | not run | Author-reported results not independently verified | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Did not independently run tests or verify the author's hardware, game, or performance observations. Read all changed files listed in the manifest: `core/libs/prx/libSceAvPlayer/src/Source.cpp`, `core/libs/tests/GuestAvPlayer.cpp`, and `docs/dev/TechnicalDebt.md`. No changed files were excluded by the manifest. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 17:15:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T17:15:31Z.

Review: COMMENTED


The current lifetime guarantee depends on whether the host allocator can supply both optional video buffer batches. For a title requesting two buffers, addOptionalVideoBuffers retains four previous frames only when all eight optional allocations succeed. The same guest configuration can therefore get different reuse behavior under memory pressure, and the proposed lifetime comes from a single title experiment. Can the PR pin down the console lifetime rule and add a fixture that checks it with and without optional decode-ahead capacity? That would let the extra buffers remain an allocator choice while the guest-visible lifetime follows the same rule.

This finding is from source inspection.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6174737699 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1601#pullrequestreview-6174737699). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T17:15:31Z. **Review:** COMMENTED --- The current lifetime guarantee depends on whether the host allocator can supply both optional video buffer batches. For a title requesting two buffers, `addOptionalVideoBuffers` retains four previous frames only when all eight optional allocations succeed. The same guest configuration can therefore get different reuse behavior under memory pressure, and the proposed lifetime comes from a single title experiment. Can the PR pin down the console lifetime rule and add a fixture that checks it with and without optional decode-ahead capacity? That would let the extra buffers remain an allocator choice while the guest-visible lifetime follows the same rule. This finding is from source inspection. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1601:upstream-pr/1601'
git switch 'upstream-pr/1601'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1601'
git switch 'upstream-pr/1601'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1601'
git switch 'upstream-pr/1601'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1601'
git switch 'main'
git merge --squash 'upstream-pr/1601'
git switch 'main'
git merge --ff-only 'upstream-pr/1601'
git switch 'main'
git merge 'upstream-pr/1601'
git push origin 'main'
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!1601
No description provided.