fix(libSceAvPlayer): keep synced video at the presentation clock when the title takes frames slowly #12898

Merged
hugodearaujo merged 9 commits from upstream-pr/12898 into main 2026-11-15 10:48:46 +00:00
hugodearaujo commented 2026-10-29 13:46:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hugodearaujo on 2026-10-29T13:46:55Z.


What

In the default sync mode, a title that asks for video frames slower than the film's frame rate gets a film that plays in slow motion and never reaches its end.

FfmpegSource decodes into the title's output framebuffers. When none is free, acquireBuffer waits until the title takes a frame (takeVideo recycles the queued frames the clock has passed only at that moment). So the decoder can only get a few frames ahead of the last frame the title took, and between two sceAvPlayerGetVideoData(Ex) calls the film position advances by a few frames, however much time passed. takeVideo already skips the queued frames the clock has passed, but the queue never holds frames near the clock, so the picture handed out falls further behind on every call.

PRAGMATA (PPSA02530) shows this in its 245 s opening film (demo01, 30 fps, video only, 9 output framebuffers, SCE_AVPLAYER_AV_SYNC_MODE_DEFAULT). The title renders at 1-3 fps under AnyPS5 while the film plays and takes about one frame per rendered frame. With main's Source.cpp only 33-73 s of the film have been shown when the title ends it by its own clock, about 250 s after it started (the picture handed out is 176-225 s behind the presentation clock): the title pauses, jumps to 0, stops and closes the player. The player never reaches the end of the file.

The change, for the default sync mode at normal speed while the presentation clock runs (not paused, after the first frame was handed out):

  • The decoder drops a decoded frame the clock has already passed before converting it to NV12, as long as a frame of the same epoch is still queued. If no frame is queued, the late frame is kept, so a decoder slower than the film still shows its pictures.
  • When no framebuffer is free, the decoder recycles queued frames whose successor the clock has already reached, instead of waiting for the title to take a frame, and it re-checks every frame duration.

No-sync mode, trick speeds, paused players and the first frame after a jump are unchanged. How far the console decodes ahead is unknown; this is recorded under Functional in docs/dev/debt/libSceAvPlayer.md.

Tested

Arch Linux 7.2.11, GCC 16.2.1, Ninja, Release, AMD Ryzen 7 9800X3D, on main efc24518bc.

  • New tests in core/libs/tests/GuestAvPlayer.cpp, run by guest_avplayer, all in default sync mode with video only and a check that the frame handed out is no more than 150 ms behind the presentation clock (the first measures the clock as the wall time since its first frame, the other two read sceAvPlayerCurrentTime):

    • TestSyncedVideoFollowsClockWithSlowConsumer: takes the first frame, then one frame every 400 ms.
    • TestSyncedVideoCatchesUpAfterResume: takes the first frame, pauses for 300 ms so the decoder fills the queue and waits, resumes, and takes the next frame 600 ms later.
    • TestVideoCatchesUpAfterSwitchToSyncedMode: starts in SCE_AVPLAYER_AV_SYNC_MODE_NONE, takes the first frame, waits 300 ms so the decoder fills the queue and waits, switches to the default mode, and takes the next frame 400 ms later.
  • ctest -R '^guest_avplayer' on this branch: 3/3 passed, three times (guest_avplayer, guest_avplayer_callback_close, guest_avplayer_paused_first_video).

  • Negative controls, each three times out of three:

    • Source.cpp restored from main: the picture handed out fell 367 ms behind the presentation clock.
    • Source.cpp restored from b870224b4c (the catch-up without the wake-ups): after resume the picture handed out fell 367 ms behind the presentation clock.
    • This branch with only the SetSyncMode wake-up reverted: after switching to default sync the picture handed out fell 467 ms behind the presentation clock.
  • python3 tools/check_conventions.py --base origin/main and git diff --check: clean.

  • PRAGMATA (PPSA02530), Linux, RX 9070 XT, 1000 s runs from boot through New Game and the difficulty menu into the opening film, with a local diagnostic log of the player state that is not part of this PR (last is the timestamp of the frame handed out, clock now the presentation clock, dur the film length, all in ms). Both builds are the same local integration build PRAGMATA needs to reach the film (main e61a7c7e24 plus open PRs, including #6565) and differ only in libSceAvPlayer/src/Source.cpp (the run with main's code was also relinked with import tracing, which slows the title further).

    With main's Source.cpp, the last state line before the title ends the film, then the title's calls:

    [avdiag] 0x7f5e92455d90 video: video calls 4 taken 2 last 33367 refusal 0 | audio calls 0 taken 0 last 0 | clock now 258291.1 base 0.2 running 1 cpaused 0 cspeed 100 | epochs clock 1 min 1 demux 1 | started 1 paused 0 speed 100 sync 0 audioDriving 0 presented 1 lastPresented 33367 lastAudio 0.0 reconciled 0 | video stream 0 frames 0 front e0 t0 seam 0 free 6 handed 6 cur 1 pkts 48 bytes 2127873 ended 0 e1 | audio stream -1 frames 0 front e0 t0 free 0 pkts 0 ended 1 e1 | demuxEnded 0 repos 0 loop 0 fd 33 dur 244978
    [avdiag] player 0x7f64ae4fdb80 Pause (state 2)
    [avdiag] player 0x7f64ae4fdb80 JumpToTime(0) (state 3)
    [avdiag] player 0x7f64ae4fdb80 Stop -> 0x0 (state 3 -> 4)
    [avdiag] player 0x7f64ae4fdb80 closed in state 4
    

    (A second run of main's code without #6565 ended the film the same way at last 72773 / clock now 248852.5.)

    With this change, the state at half the film and just before its end, then the player reaches the end of the file and the title stops and closes it:

    [avdiag] 0x7f3995ce4cc0 video: video calls 3 taken 3 last 120754 refusal 0 | audio calls 0 taken 0 last 0 | clock now 120758.1 base 0.3 running 1 cpaused 0 cspeed 100 | epochs clock 1 min 1 demux 1 | started 1 paused 0 speed 100 sync 0 audioDriving 0 presented 1 lastPresented 120754 lastAudio 0.0 reconciled 0 | video stream 0 frames 4 front e1 t120787 seam 0 free 2 handed 6 cur 1 pkts 1814 bytes 2111325 ended 0 e1 | audio stream -1 frames 0 front e0 t0 free 0 pkts 0 ended 1 e1 | demuxEnded 0 repos 0 loop 0 fd 33 dur 244978 droppedLate 3331
    [avdiag] 0x7f3995ce4cc0 video: video calls 1 taken 1 last 244311 refusal 0 | audio calls 0 taken 0 last 0 | clock now 244321.2 base 0.3 running 1 cpaused 0 cspeed 100 | epochs clock 1 min 1 demux 1 | started 1 paused 0 speed 100 sync 0 audioDriving 0 presented 1 lastPresented 244311 lastAudio 0.0 reconciled 0 | video stream 0 frames 4 front e1 t244344 seam 0 free 2 handed 6 cur 1 pkts 12 bytes 209 ended 0 e1 | audio stream -1 frames 0 front e0 t0 free 0 pkts 0 ended 1 e1 | demuxEnded 1 repos 0 loop 0 fd 33 dur 244978 droppedLate 6894
    [avdiag] player 0x7f445ce7e530 IsActive -> 0 (state 2)
    [avdiag] player 0x7f445ce7e530 end of file
    [avdiag] player 0x7f445ce7e530 deliver event id 0x1 warning 0x0 autoStart 0 error 0 (state 5)
    [avdiag] player 0x7f445ce7e530 Stop -> 0x0 (state 5 -> 4)
    [avdiag] player 0x7f445ce7e530 closed in state 4
    

    Over the 104 state lines logged before the end of the file, the frame handed out was a median of 11 ms behind the clock (90th percentile 30 ms, worst 34 ms), against a median of 81 s on main's code. droppedLate counts the decoded frames dropped before conversion.

Checklist

  • Based on current main; no other open PR implements the same functions (#1601 changes how long handed-out frames are kept, #11772 the no-sync clock; neither touches decode-ahead in the default sync mode)
  • 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. #11772 adds a debt entry at the same line of docs/dev/debt/libSceAvPlayer.md (a textual conflict only); #1601 also edits Source.cpp and GuestAvPlayer.cpp, on the side that keeps handed-out frames. Whichever lands second rebases; I'll do it here if this one is later.
  • Fixes: none
  • Unblocks: PPSA02530 #13087 (the opening film plays at its own pace and reaches its end)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5069181694 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12898). Originally posted by [hugodearaujo](https://github.com/hugodearaujo) on 2026-10-29T13:46:55Z. --- ### What In the default sync mode, a title that asks for video frames slower than the film's frame rate gets a film that plays in slow motion and never reaches its end. `FfmpegSource` decodes into the title's output framebuffers. When none is free, `acquireBuffer` waits until the title takes a frame (`takeVideo` recycles the queued frames the clock has passed only at that moment). So the decoder can only get a few frames ahead of the last frame the title took, and between two `sceAvPlayerGetVideoData(Ex)` calls the film position advances by a few frames, however much time passed. `takeVideo` already skips the queued frames the clock has passed, but the queue never holds frames near the clock, so the picture handed out falls further behind on every call. PRAGMATA (PPSA02530) shows this in its 245 s opening film (`demo01`, 30 fps, video only, 9 output framebuffers, `SCE_AVPLAYER_AV_SYNC_MODE_DEFAULT`). The title renders at 1-3 fps under AnyPS5 while the film plays and takes about one frame per rendered frame. With `main`'s `Source.cpp` only 33-73 s of the film have been shown when the title ends it by its own clock, about 250 s after it started (the picture handed out is 176-225 s behind the presentation clock): the title pauses, jumps to 0, stops and closes the player. The player never reaches the end of the file. The change, for the default sync mode at normal speed while the presentation clock runs (not paused, after the first frame was handed out): - The decoder drops a decoded frame the clock has already passed before converting it to NV12, as long as a frame of the same epoch is still queued. If no frame is queued, the late frame is kept, so a decoder slower than the film still shows its pictures. - When no framebuffer is free, the decoder recycles queued frames whose successor the clock has already reached, instead of waiting for the title to take a frame, and it re-checks every frame duration. No-sync mode, trick speeds, paused players and the first frame after a jump are unchanged. How far the console decodes ahead is unknown; this is recorded under Functional in `docs/dev/debt/libSceAvPlayer.md`. ### Tested Arch Linux 7.2.11, GCC 16.2.1, Ninja, Release, AMD Ryzen 7 9800X3D, on `main` efc24518bc. - New tests in `core/libs/tests/GuestAvPlayer.cpp`, run by `guest_avplayer`, all in default sync mode with video only and a check that the frame handed out is no more than 150 ms behind the presentation clock (the first measures the clock as the wall time since its first frame, the other two read `sceAvPlayerCurrentTime`): - `TestSyncedVideoFollowsClockWithSlowConsumer`: takes the first frame, then one frame every 400 ms. - `TestSyncedVideoCatchesUpAfterResume`: takes the first frame, pauses for 300 ms so the decoder fills the queue and waits, resumes, and takes the next frame 600 ms later. - `TestVideoCatchesUpAfterSwitchToSyncedMode`: starts in `SCE_AVPLAYER_AV_SYNC_MODE_NONE`, takes the first frame, waits 300 ms so the decoder fills the queue and waits, switches to the default mode, and takes the next frame 400 ms later. - `ctest -R '^guest_avplayer'` on this branch: 3/3 passed, three times (`guest_avplayer`, `guest_avplayer_callback_close`, `guest_avplayer_paused_first_video`). - Negative controls, each three times out of three: - `Source.cpp` restored from `main`: `the picture handed out fell 367 ms behind the presentation clock`. - `Source.cpp` restored from b870224b4c (the catch-up without the wake-ups): `after resume the picture handed out fell 367 ms behind the presentation clock`. - This branch with only the `SetSyncMode` wake-up reverted: `after switching to default sync the picture handed out fell 467 ms behind the presentation clock`. - `python3 tools/check_conventions.py --base origin/main` and `git diff --check`: clean. - PRAGMATA (PPSA02530), Linux, RX 9070 XT, 1000 s runs from boot through New Game and the difficulty menu into the opening film, with a local diagnostic log of the player state that is not part of this PR (`last` is the timestamp of the frame handed out, `clock now` the presentation clock, `dur` the film length, all in ms). Both builds are the same local integration build PRAGMATA needs to reach the film (`main` e61a7c7e24 plus open PRs, including #6565) and differ only in `libSceAvPlayer/src/Source.cpp` (the run with `main`'s code was also relinked with import tracing, which slows the title further). With `main`'s `Source.cpp`, the last state line before the title ends the film, then the title's calls: ``` [avdiag] 0x7f5e92455d90 video: video calls 4 taken 2 last 33367 refusal 0 | audio calls 0 taken 0 last 0 | clock now 258291.1 base 0.2 running 1 cpaused 0 cspeed 100 | epochs clock 1 min 1 demux 1 | started 1 paused 0 speed 100 sync 0 audioDriving 0 presented 1 lastPresented 33367 lastAudio 0.0 reconciled 0 | video stream 0 frames 0 front e0 t0 seam 0 free 6 handed 6 cur 1 pkts 48 bytes 2127873 ended 0 e1 | audio stream -1 frames 0 front e0 t0 free 0 pkts 0 ended 1 e1 | demuxEnded 0 repos 0 loop 0 fd 33 dur 244978 [avdiag] player 0x7f64ae4fdb80 Pause (state 2) [avdiag] player 0x7f64ae4fdb80 JumpToTime(0) (state 3) [avdiag] player 0x7f64ae4fdb80 Stop -> 0x0 (state 3 -> 4) [avdiag] player 0x7f64ae4fdb80 closed in state 4 ``` (A second run of `main`'s code without #6565 ended the film the same way at `last 72773` / `clock now 248852.5`.) With this change, the state at half the film and just before its end, then the player reaches the end of the file and the title stops and closes it: ``` [avdiag] 0x7f3995ce4cc0 video: video calls 3 taken 3 last 120754 refusal 0 | audio calls 0 taken 0 last 0 | clock now 120758.1 base 0.3 running 1 cpaused 0 cspeed 100 | epochs clock 1 min 1 demux 1 | started 1 paused 0 speed 100 sync 0 audioDriving 0 presented 1 lastPresented 120754 lastAudio 0.0 reconciled 0 | video stream 0 frames 4 front e1 t120787 seam 0 free 2 handed 6 cur 1 pkts 1814 bytes 2111325 ended 0 e1 | audio stream -1 frames 0 front e0 t0 free 0 pkts 0 ended 1 e1 | demuxEnded 0 repos 0 loop 0 fd 33 dur 244978 droppedLate 3331 [avdiag] 0x7f3995ce4cc0 video: video calls 1 taken 1 last 244311 refusal 0 | audio calls 0 taken 0 last 0 | clock now 244321.2 base 0.3 running 1 cpaused 0 cspeed 100 | epochs clock 1 min 1 demux 1 | started 1 paused 0 speed 100 sync 0 audioDriving 0 presented 1 lastPresented 244311 lastAudio 0.0 reconciled 0 | video stream 0 frames 4 front e1 t244344 seam 0 free 2 handed 6 cur 1 pkts 12 bytes 209 ended 0 e1 | audio stream -1 frames 0 front e0 t0 free 0 pkts 0 ended 1 e1 | demuxEnded 1 repos 0 loop 0 fd 33 dur 244978 droppedLate 6894 [avdiag] player 0x7f445ce7e530 IsActive -> 0 (state 2) [avdiag] player 0x7f445ce7e530 end of file [avdiag] player 0x7f445ce7e530 deliver event id 0x1 warning 0x0 autoStart 0 error 0 (state 5) [avdiag] player 0x7f445ce7e530 Stop -> 0x0 (state 5 -> 4) [avdiag] player 0x7f445ce7e530 closed in state 4 ``` Over the 104 state lines logged before the end of the file, the frame handed out was a median of 11 ms behind the clock (90th percentile 30 ms, worst 34 ms), against a median of 81 s on `main`'s code. `droppedLate` counts the decoded frames dropped before conversion. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (#1601 changes how long handed-out frames are kept, #11772 the no-sync clock; neither touches decode-ahead in the default sync mode) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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. #11772 adds a debt entry at the same line of `docs/dev/debt/libSceAvPlayer.md` (a textual conflict only); #1601 also edits `Source.cpp` and `GuestAvPlayer.cpp`, on the side that keeps handed-out frames. Whichever lands second rebases; I'll do it here if this one is later. - [x] Fixes: none - [x] Unblocks: PPSA02530 #13087 (the opening film plays at its own pace and reaches its end) - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!12898
No description provided.