feat(avplayer): decode H.264 and AAC with FFmpeg #307

Merged
oneandonlydean merged 1 commit from upstream-pr/307 into main 2026-10-02 20:07:49 +00:00
oneandonlydean commented 2026-10-02 01:12:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-02T01:12:28Z.


What

Replaces the AvPlayer from #80 with one that decodes. #80 implements the player's state machine but has no decoder: whatever the source, it plays a fixed clip of 120 black 1920x1080 frames (about 4 s) on a wall clock, never delivers audio, and then reports the end of the stream. A title that shows its intro or tutorial movies through AvPlayer gets about 4 s of black and silence instead. It also logs unconditionally ([AVP], [AVPLIFE]), and none of this is listed in TechnicalDebt.

The player here is a port of shadPS4's AvPlayer (its SPDX header is kept, as in libSceFont) on top of FFmpeg:

  • sceAvPlayerAddSource(Ex) opens the MP4 with libavformat, by path through ResolvePath_nid_no_patch or through the title's file replacement callbacks (a custom AVIOContext). H.264, HEVC and AAC streams are reported; other streams are left out.
  • A demuxer thread feeds one decoder thread per enabled stream. Video is converted to NV12 with a 256-byte pitch and a height aligned to 16, into buffers from the title's texture allocator (num_output_video_framebuffers, clamped to 2..16). Audio is converted to interleaved 16-bit PCM in chunks of 1024 samples, into buffers from the title's plain allocator.
  • Frames are handed out against a media clock. An enabled audio stream drives it at normal speed; otherwise the video timestamps do. sceAvPlayerSetAvSyncMode(1) hands out frames as soon as they are decoded.
  • A handed-out video frame is not reused while the title may still be reading it: the frame handed out last and the num_output_video_framebuffers - 3 before it are held back.
  • sceAvPlayerJumpToTime seeks and reports WAR_JUMP_COMPLETE, looping reports WAR_LOOPING_BACK, and the end of the file fires the STOP event.
  • Events reach the title's callback from a controller thread. With no callback, or with auto_start, the player enables a video stream and an audio stream, preferring default_language, then starts.

Build:

  • Each library keeps its own Export.cpp, as on main since #80, and both export the same set of functions. Both build the shared player in libSceAvPlayer/src through AvPlayerLibrary.cmake (like FontLibrary.cmake), with hidden visibility, and link the shared ffmpeg target from ffmpeg-core, as libSceVideodec2 does.
  • libavformat brings in its network helpers (network.o and url.o reference socket and getaddrinfo), so on MinGW the ffmpeg target also links ws2_32, next to bcrypt, ole32 and user32. No other library on main uses libavformat yet.

Exports, compared with #80:

Export #80 This PR
GetVideoData, GetVideoDataEx a black frame of the 4 s clip the next decoded NV12 frame due on the clock
GetAudioData always false the next PCM chunk
GetStreamInfo one 1920x1080 video stream each stream in the file (video type 1, audio type 0)
GetStreamInfoEx the black clip each stream, with the same wide description its frames carry (pitch, crop offsets, bit depth, range); this_size is left as the caller set it
ChangeStream succeeds, does nothing swaps an enabled stream for another of the same type. While playing, the player seeks to the current time on the new stream without a jump warning. Fails for streams of different types, a stream that is not enabled, or a video stream larger than the allocated frames
SetTrickSpeed accepted, ignored positive speeds scale the clock (audio is withheld off normal speed). Negative speeds run the clock backwards and deliver no frames; when a forward speed is set again, playback resumes from the rewound time
StartEx as Start as Start; the start info is ignored
Init, InitEx without allocators a handle whose first poll ends the stream a handle whose buffers come from the guest heap
SetAvailableBandwidth, SetLogCallback succeed, no effect the same
AddSource with an HLS source (.m3u8 or source type 8) the black clip throws
IsActive, CurrentTime, Pause, Resume, Stop the black clip's wall clock follow the decoder

InitEx reads SceAvPlayerInitDataEx with its own layout (the language pointer, then the per-thread priorities and affinities, then the frame buffer count and auto start), where #80 read it as the InitData layout after the size.

Struct checks: #80's static_asserts move next to the types in SceTypes.hpp: SceAvPlayerFrameInfo (40 bytes, timestamp at 16), SceAvPlayerVideoEx (80, crop offsets at 20, pitch at 36) and SceAvPlayerFrameInfoEx (104, details at 24). New ones cover SceAvPlayerStreamInfo (32) and SceAvPlayerStreamInfoEx (104, details at 16, duration at 96). #80's check for a double frame rate at offset 48 of SceAvPlayerVideoEx is not kept: SceTypes.hpp has a 32-bit framerate at offset 16 instead, and which is right is unknown, so the frame rate is left zero.

TechnicalDebt:

  • Silent stubs: SetLogCallback never calls its callback (the player produces no log messages); SetAvailableBandwidth has no effect (it governs HLS, which AddSource does not implement); negative trick speeds deliver no frames.
  • Unknown function info: the StartEx start info layout; what the library does without a memory replacement; the frame rate field of SceAvPlayerVideoEx.

Tested

  • Linux (CachyOS, GCC 16.2). guest_avplayer (ctest) plays a 5986-byte MP4 embedded in the test as bytes, like GuestVideodec2's stream, so it runs on CI without an encoder. The movie is 1 s long: 100x60 H.264 at 30 fps with B-frames, whose two halves change brightness every frame; a silent English stereo AAC track; and a French mono AAC track with a 440 Hz tone. The test writes it to app0 and covers:
    • stream info and extended stream info;
    • playback, checking every frame's luma on both halves, chroma, crop offsets, pitch and which allocator it came from;
    • pause and resume, a jump, and a reverse trick speed followed by a forward one;
    • sync mode none to the end, with the STOP event, then looping with WAR_LOOPING_BACK;
    • ChangeStream before start and during playback (audio switches to the French track's audible, mono, fra-tagged chunks, with no jump warning);
    • StartEx; Init and InitEx without allocators (frames from the guest heap); file replacement with auto start;
    • handed-out frames staying intact, and no leaked allocations.
  • 30 runs in a row pass, plus 12 pinned to one or two cores.
  • ctest: 97/97 pass. The libs target builds and the NID patcher accepts both libraries.
  • Windows: not built (no MinGW toolchain here). The code uses only standard threads and filesystem; the test allocates with aligned operator new, not aligned_alloc; paths go to FFmpeg as UTF-8 file: URLs.
  • PPSA21564 on my integration branch, with only these two libraries swapped in: the PlayStation Studios intro video plays through, my test run's frame by frame video check finds no torn or stale frames, and my visual regression check passes. Frames from that run, timed from the start of the video:

PPSA21564 intro video decoded by this AvPlayer

  • The earlier version of this decoder on my integration branch also plays the title's tutorial videos.

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
  • New third-party code is a submodule built from source (none added; uses the existing ffmpeg target)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4711484045 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/307). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-02T01:12:28Z. --- ### What Replaces the AvPlayer from #80 with one that decodes. #80 implements the player's state machine but has no decoder: whatever the source, it plays a fixed clip of 120 black 1920x1080 frames (about 4 s) on a wall clock, never delivers audio, and then reports the end of the stream. A title that shows its intro or tutorial movies through AvPlayer gets about 4 s of black and silence instead. It also logs unconditionally (`[AVP]`, `[AVPLIFE]`), and none of this is listed in TechnicalDebt. The player here is a port of shadPS4's AvPlayer (its SPDX header is kept, as in libSceFont) on top of FFmpeg: - `sceAvPlayerAddSource(Ex)` opens the MP4 with libavformat, by path through `ResolvePath_nid_no_patch` or through the title's file replacement callbacks (a custom AVIOContext). H.264, HEVC and AAC streams are reported; other streams are left out. - A demuxer thread feeds one decoder thread per enabled stream. Video is converted to NV12 with a 256-byte pitch and a height aligned to 16, into buffers from the title's texture allocator (`num_output_video_framebuffers`, clamped to 2..16). Audio is converted to interleaved 16-bit PCM in chunks of 1024 samples, into buffers from the title's plain allocator. - Frames are handed out against a media clock. An enabled audio stream drives it at normal speed; otherwise the video timestamps do. `sceAvPlayerSetAvSyncMode(1)` hands out frames as soon as they are decoded. - A handed-out video frame is not reused while the title may still be reading it: the frame handed out last and the `num_output_video_framebuffers - 3` before it are held back. - `sceAvPlayerJumpToTime` seeks and reports `WAR_JUMP_COMPLETE`, looping reports `WAR_LOOPING_BACK`, and the end of the file fires the STOP event. - Events reach the title's callback from a controller thread. With no callback, or with `auto_start`, the player enables a video stream and an audio stream, preferring `default_language`, then starts. Build: - Each library keeps its own `Export.cpp`, as on main since #80, and both export the same set of functions. Both build the shared player in `libSceAvPlayer/src` through `AvPlayerLibrary.cmake` (like `FontLibrary.cmake`), with hidden visibility, and link the shared `ffmpeg` target from ffmpeg-core, as libSceVideodec2 does. - libavformat brings in its network helpers (`network.o` and `url.o` reference `socket` and `getaddrinfo`), so on MinGW the `ffmpeg` target also links `ws2_32`, next to bcrypt, ole32 and user32. No other library on main uses libavformat yet. Exports, compared with #80: | Export | #80 | This PR | |---|---|---| | `GetVideoData`, `GetVideoDataEx` | a black frame of the 4 s clip | the next decoded NV12 frame due on the clock | | `GetAudioData` | always false | the next PCM chunk | | `GetStreamInfo` | one 1920x1080 video stream | each stream in the file (video type 1, audio type 0) | | `GetStreamInfoEx` | the black clip | each stream, with the same wide description its frames carry (pitch, crop offsets, bit depth, range); `this_size` is left as the caller set it | | `ChangeStream` | succeeds, does nothing | swaps an enabled stream for another of the same type. While playing, the player seeks to the current time on the new stream without a jump warning. Fails for streams of different types, a stream that is not enabled, or a video stream larger than the allocated frames | | `SetTrickSpeed` | accepted, ignored | positive speeds scale the clock (audio is withheld off normal speed). Negative speeds run the clock backwards and deliver no frames; when a forward speed is set again, playback resumes from the rewound time | | `StartEx` | as `Start` | as `Start`; the start info is ignored | | `Init`, `InitEx` without allocators | a handle whose first poll ends the stream | a handle whose buffers come from the guest heap | | `SetAvailableBandwidth`, `SetLogCallback` | succeed, no effect | the same | | `AddSource` with an HLS source (`.m3u8` or source type 8) | the black clip | throws | | `IsActive`, `CurrentTime`, `Pause`, `Resume`, `Stop` | the black clip's wall clock | follow the decoder | `InitEx` reads `SceAvPlayerInitDataEx` with its own layout (the language pointer, then the per-thread priorities and affinities, then the frame buffer count and auto start), where #80 read it as the `InitData` layout after the size. Struct checks: #80's `static_assert`s move next to the types in SceTypes.hpp: `SceAvPlayerFrameInfo` (40 bytes, timestamp at 16), `SceAvPlayerVideoEx` (80, crop offsets at 20, pitch at 36) and `SceAvPlayerFrameInfoEx` (104, details at 24). New ones cover `SceAvPlayerStreamInfo` (32) and `SceAvPlayerStreamInfoEx` (104, details at 16, duration at 96). #80's check for a double frame rate at offset 48 of `SceAvPlayerVideoEx` is not kept: SceTypes.hpp has a 32-bit `framerate` at offset 16 instead, and which is right is unknown, so the frame rate is left zero. TechnicalDebt: - Silent stubs: `SetLogCallback` never calls its callback (the player produces no log messages); `SetAvailableBandwidth` has no effect (it governs HLS, which `AddSource` does not implement); negative trick speeds deliver no frames. - Unknown function info: the `StartEx` start info layout; what the library does without a memory replacement; the frame rate field of `SceAvPlayerVideoEx`. ### Tested - Linux (CachyOS, GCC 16.2). `guest_avplayer` (ctest) plays a 5986-byte MP4 embedded in the test as bytes, like GuestVideodec2's stream, so it runs on CI without an encoder. The movie is 1 s long: 100x60 H.264 at 30 fps with B-frames, whose two halves change brightness every frame; a silent English stereo AAC track; and a French mono AAC track with a 440 Hz tone. The test writes it to `app0` and covers: - stream info and extended stream info; - playback, checking every frame's luma on both halves, chroma, crop offsets, pitch and which allocator it came from; - pause and resume, a jump, and a reverse trick speed followed by a forward one; - sync mode none to the end, with the STOP event, then looping with `WAR_LOOPING_BACK`; - `ChangeStream` before start and during playback (audio switches to the French track's audible, mono, `fra`-tagged chunks, with no jump warning); - `StartEx`; `Init` and `InitEx` without allocators (frames from the guest heap); file replacement with auto start; - handed-out frames staying intact, and no leaked allocations. - 30 runs in a row pass, plus 12 pinned to one or two cores. - `ctest`: 97/97 pass. The `libs` target builds and the NID patcher accepts both libraries. - Windows: not built (no MinGW toolchain here). The code uses only standard threads and filesystem; the test allocates with aligned `operator new`, not `aligned_alloc`; paths go to FFmpeg as UTF-8 `file:` URLs. - PPSA21564 on my integration branch, with only these two libraries swapped in: the PlayStation Studios intro video plays through, my test run's frame by frame video check finds no torn or stale frames, and my visual regression check passes. Frames from that run, timed from the start of the video: ![PPSA21564 intro video decoded by this AvPlayer](https://raw.githubusercontent.com/oneandonlydean/AnyPS5/f04309b90047d5662324d7b3a330c21976641d24/avplayer-intro-video-ppsa21564.png) - The earlier version of this decoder on my integration branch also plays the title's tutorial videos. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added; uses the existing `ffmpeg` target) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-02 01:12:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-02T01:12:50Z.


Progress report (5026351 → b62d02c)

📈 System libraries: 80.56% (+0.19%, +28 implemented functions)

✅ 28 implemented
Library Name
libSceAvPlayer sceAvPlayerAddSource
libSceAvPlayer sceAvPlayerAddSourceEx
libSceAvPlayer sceAvPlayerChangeStream
libSceAvPlayer sceAvPlayerClose
libSceAvPlayer sceAvPlayerCurrentTime
libSceAvPlayer sceAvPlayerDisableStream
libSceAvPlayer sceAvPlayerEnableStream
libSceAvPlayer sceAvPlayerGetAudioData
libSceAvPlayer sceAvPlayerGetStreamInfo
libSceAvPlayer sceAvPlayerGetStreamInfoEx
libSceAvPlayer sceAvPlayerGetVideoData
libSceAvPlayer sceAvPlayerGetVideoDataEx
libSceAvPlayer sceAvPlayerInit
libSceAvPlayer sceAvPlayerInitEx
libSceAvPlayer sceAvPlayerIsActive
libSceAvPlayer sceAvPlayerJumpToTime
libSceAvPlayer sceAvPlayerPause
libSceAvPlayer sceAvPlayerPostInit
libSceAvPlayer sceAvPlayerResume
libSceAvPlayer sceAvPlayerSetAvSyncMode
libSceAvPlayer sceAvPlayerSetAvailableBandwidth
libSceAvPlayer sceAvPlayerSetLogCallback
libSceAvPlayer sceAvPlayerSetLooping
libSceAvPlayer sceAvPlayerSetTrickSpeed
libSceAvPlayer sceAvPlayerStart
libSceAvPlayer sceAvPlayerStartEx
libSceAvPlayer sceAvPlayerStop
libSceAvPlayer sceAvPlayerStreamCount
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5943727377 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/307#issuecomment-5943727377). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-02T01:12:50Z. --- <!-- progress-report --> ### Progress report (5026351 → b62d02c) 📈 **System libraries**: 80.56% (+0.19%, +28 implemented functions) <details> <summary>✅ 28 implemented</summary> | Library | Name | | - | - | | libSceAvPlayer | `sceAvPlayerAddSource` | | libSceAvPlayer | `sceAvPlayerAddSourceEx` | | libSceAvPlayer | `sceAvPlayerChangeStream` | | libSceAvPlayer | `sceAvPlayerClose` | | libSceAvPlayer | `sceAvPlayerCurrentTime` | | libSceAvPlayer | `sceAvPlayerDisableStream` | | libSceAvPlayer | `sceAvPlayerEnableStream` | | libSceAvPlayer | `sceAvPlayerGetAudioData` | | libSceAvPlayer | `sceAvPlayerGetStreamInfo` | | libSceAvPlayer | `sceAvPlayerGetStreamInfoEx` | | libSceAvPlayer | `sceAvPlayerGetVideoData` | | libSceAvPlayer | `sceAvPlayerGetVideoDataEx` | | libSceAvPlayer | `sceAvPlayerInit` | | libSceAvPlayer | `sceAvPlayerInitEx` | | libSceAvPlayer | `sceAvPlayerIsActive` | | libSceAvPlayer | `sceAvPlayerJumpToTime` | | libSceAvPlayer | `sceAvPlayerPause` | | libSceAvPlayer | `sceAvPlayerPostInit` | | libSceAvPlayer | `sceAvPlayerResume` | | libSceAvPlayer | `sceAvPlayerSetAvSyncMode` | | libSceAvPlayer | `sceAvPlayerSetAvailableBandwidth` | | libSceAvPlayer | `sceAvPlayerSetLogCallback` | | libSceAvPlayer | `sceAvPlayerSetLooping` | | libSceAvPlayer | `sceAvPlayerSetTrickSpeed` | | libSceAvPlayer | `sceAvPlayerStart` | | libSceAvPlayer | `sceAvPlayerStartEx` | | libSceAvPlayer | `sceAvPlayerStop` | | libSceAvPlayer | `sceAvPlayerStreamCount` | </details>
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!307
No description provided.