feat(jpegenc): implement the JPEG encoder #146

Merged
Qznad merged 9 commits from upstream-pr/146 into main 2026-09-29 14:52:36 +00:00
Qznad commented 2026-09-29 09:15:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Qznad on 2026-09-29T09:15:46Z.


What

Implements the 4 libSceJpegEnc entry points (previously NotImplemented_nid_no_patch with no parameters), and adds a shared JPEG encoder/decoder in core/Decoder/Jpeg that has no PRX dependency, so the relinker can use it later for the executable icon.

core/Decoder/Jpeg (static library decoder_jpeg):

  • Decoder::Jpeg::Encode(pixels, width, height, channels, quality) and Decoder::Jpeg::Decode(jpeg) on top of stb (stb_image_write.h / stb_image.h), added as the nothings/stb submodule and compiled from source in src/Jpeg.cpp only. stb is compiled with STB_*_STATIC, STBI_ONLY_JPEG and no stdio, and the library uses hidden visibility, so nothing but the 4 Sony functions is exported from libSceJpegEnc.prx.

libSceJpegEnc:

  • Parameter structs JpegEncCreateParam, JpegEncEncodeParam, JpegEncOutputInfo in SceTypes.hpp, sizes checked with static_assert. Layouts, error codes (0x80650101-0x80650104), the 0x800 memory size and the validation rules follow shadPS4's reverse engineering of the same library.
  • QueryMemorySize / Create / Delete: the handle is placed in the caller's memory aligned to 0x20 and points to itself; Delete clears it, so stale, misaligned and foreign handles return INVALID_HANDLE.
  • Encode: validates every argument, reads the image row by row using the pitch, converts R8G8B8A8, B8G8R8A8, Y8U8Y8V8 and Y8 to packed RGB or gray, encodes, and copies the result into the caller's buffer. output_info may be null.

Assumptions (no documentation available):

  • compression_ratio is 0-255 with higher meaning more compression, as documented for the PS Vita sceJpegEncoderSetCompressionRatio. It maps linearly to quality 100..1, capped at 90 for subsampled output so stb emits 4:2:0.
  • An output larger than jpeg_size returns INVALID_SIZE and writes nothing.
  • Y8U8Y8V8 input is full-range BT.601 (JFIF).
  • A zero width or height returns INVALID_PARAM (shadPS4 does not check it).

Not supported: MJPEG encode mode and a positive restart interval throw std::runtime_error. 4:2:2 requests are encoded as 4:2:0, and grayscale requests as a 3-component JPEG with neutral chroma; both are listed in TechnicalDebt.md.

Tested

Linux (GCC 15.2, BUILD_TESTING=ON): full build with no warnings in the changed files, ctest 60/60 pass, including the 2 new tests:

  • decoder_jpeg: RGB and gray round trips through Encode/Decode, invalid arguments, garbage input.
  • guest_jpeg_enc: every create/delete/encode error path, stale and misaligned handles, and real encodes of RGBA with padded rows, BGRA, Y8U8Y8V8 and Y8 that are decoded back and compared with the input.

Also checked:

  • Our sources compile without warnings under -Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wsign-conversion, and both tests plus an edge-case program (1x1, 37x23 with padded pitch, 33x17 Y8U8Y8V8, 1920x1080 B8G8R8A8 with a 2048-pixel pitch, several compression ratios) run clean under ASan and UBSan.
  • The outputs decode with libjpeg (Pillow) at the right size, and quality falls as compression_ratio rises.
  • After NID patching, libSceJpegEnc.prx exports exactly K+rocojkr-I, j1LyMdaM+C0, QbrU0cUghEM and o6ZgXfFdWXQ, the NIDs shadPS4 registers for these functions.

Not tested on Windows or with a title.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR (PNG in core/Decoder/Png goes in a separate PR, as discussed)
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt (unsupported modes throw; the two encoding approximations are listed under Functional)
  • 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 (nothings/stb)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4675118947 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/146). Originally posted by [Qznad](https://github.com/Qznad) on 2026-09-29T09:15:46Z. --- ### What Implements the 4 `libSceJpegEnc` entry points (previously `NotImplemented_nid_no_patch` with no parameters), and adds a shared JPEG encoder/decoder in `core/Decoder/Jpeg` that has no PRX dependency, so the relinker can use it later for the executable icon. `core/Decoder/Jpeg` (static library `decoder_jpeg`): - `Decoder::Jpeg::Encode(pixels, width, height, channels, quality)` and `Decoder::Jpeg::Decode(jpeg)` on top of stb (`stb_image_write.h` / `stb_image.h`), added as the `nothings/stb` submodule and compiled from source in `src/Jpeg.cpp` only. stb is compiled with `STB_*_STATIC`, `STBI_ONLY_JPEG` and no stdio, and the library uses hidden visibility, so nothing but the 4 Sony functions is exported from `libSceJpegEnc.prx`. `libSceJpegEnc`: - Parameter structs `JpegEncCreateParam`, `JpegEncEncodeParam`, `JpegEncOutputInfo` in `SceTypes.hpp`, sizes checked with `static_assert`. Layouts, error codes (`0x80650101`-`0x80650104`), the 0x800 memory size and the validation rules follow shadPS4's reverse engineering of the same library. - `QueryMemorySize` / `Create` / `Delete`: the handle is placed in the caller's memory aligned to 0x20 and points to itself; `Delete` clears it, so stale, misaligned and foreign handles return `INVALID_HANDLE`. - `Encode`: validates every argument, reads the image row by row using the pitch, converts R8G8B8A8, B8G8R8A8, Y8U8Y8V8 and Y8 to packed RGB or gray, encodes, and copies the result into the caller's buffer. `output_info` may be null. Assumptions (no documentation available): - `compression_ratio` is 0-255 with higher meaning more compression, as documented for the PS Vita `sceJpegEncoderSetCompressionRatio`. It maps linearly to quality 100..1, capped at 90 for subsampled output so stb emits 4:2:0. - An output larger than `jpeg_size` returns `INVALID_SIZE` and writes nothing. - Y8U8Y8V8 input is full-range BT.601 (JFIF). - A zero width or height returns `INVALID_PARAM` (shadPS4 does not check it). Not supported: MJPEG encode mode and a positive restart interval throw `std::runtime_error`. 4:2:2 requests are encoded as 4:2:0, and grayscale requests as a 3-component JPEG with neutral chroma; both are listed in `TechnicalDebt.md`. ### Tested Linux (GCC 15.2, `BUILD_TESTING=ON`): full build with no warnings in the changed files, `ctest` 60/60 pass, including the 2 new tests: - `decoder_jpeg`: RGB and gray round trips through `Encode`/`Decode`, invalid arguments, garbage input. - `guest_jpeg_enc`: every create/delete/encode error path, stale and misaligned handles, and real encodes of RGBA with padded rows, BGRA, Y8U8Y8V8 and Y8 that are decoded back and compared with the input. Also checked: - Our sources compile without warnings under `-Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wsign-conversion`, and both tests plus an edge-case program (1x1, 37x23 with padded pitch, 33x17 Y8U8Y8V8, 1920x1080 B8G8R8A8 with a 2048-pixel pitch, several compression ratios) run clean under ASan and UBSan. - The outputs decode with libjpeg (Pillow) at the right size, and quality falls as `compression_ratio` rises. - After NID patching, `libSceJpegEnc.prx` exports exactly `K+rocojkr-I`, `j1LyMdaM+C0`, `QbrU0cUghEM` and `o6ZgXfFdWXQ`, the NIDs shadPS4 registers for these functions. Not tested on Windows or with a title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/docs/CONTRIBUTING.md --> - [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 (PNG in `core/Decoder/Png` goes in a separate PR, as discussed) - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/TechnicalDebt.md#silent-stubs) (unsupported modes throw; the two encoding approximations are listed under Functional) - [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 (`nothings/stb`) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
boykopovar commented 2026-09-29 09:54:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by boykopovar on 2026-09-29T09:54:31Z.


Do you know the lightest submodule that will encode and decode PNG and JPG files? Relinker will also need decoder and encoder functionality (to set the icon), and relinker shouldn't depend on PRX. So, you should place the decoder and encoder in the core/Decoder/Png and core/Decoder/Jpeg folders.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5887858351 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/146#issuecomment-5887858351). Originally posted by [boykopovar](https://github.com/boykopovar) on 2026-09-29T09:54:31Z. --- Do you know the lightest submodule that will encode and decode PNG and JPG files? Relinker will also need decoder and encoder functionality (to set the icon), and relinker shouldn't depend on PRX. So, you should place the decoder and encoder in the core/Decoder/Png and core/Decoder/Jpeg folders.
Qznad commented 2026-09-29 10:11:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Qznad on 2026-09-29T10:11:40Z.


Thanks for the direction. stb (nothings/stb) looks like the lightest option: it is header-only with no dependencies, and one submodule covers all four cases (stb_image_write.h encodes PNG and JPEG, stb_image.h decodes both). It is already added as a submodule in this branch.

I will move the JPEG encode/decode code into core/Decoder/Jpeg as a small library with no PRX dependency, and make libSceJpegEnc use it, so the relinker can use it later for icons.

Should core/Decoder/Png also be part of this PR, or would you prefer it in #86 or a separate follow-up, to keep this PR to one topic?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5888118308 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/146#issuecomment-5888118308). Originally posted by [Qznad](https://github.com/Qznad) on 2026-09-29T10:11:40Z. --- Thanks for the direction. stb (nothings/stb) looks like the lightest option: it is header-only with no dependencies, and one submodule covers all four cases (stb_image_write.h encodes PNG and JPEG, stb_image.h decodes both). It is already added as a submodule in this branch. I will move the JPEG encode/decode code into core/Decoder/Jpeg as a small library with no PRX dependency, and make libSceJpegEnc use it, so the relinker can use it later for icons. Should core/Decoder/Png also be part of this PR, or would you prefer it in #86 or a separate follow-up, to keep this PR to one topic?
boykopovar commented 2026-09-29 13:09:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by boykopovar on 2026-09-29T13:09:05Z.


it's better to make a separate pr for png

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5890955368 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/146#issuecomment-5890955368). Originally posted by [boykopovar](https://github.com/boykopovar) on 2026-09-29T13:09:05Z. --- it's better to make a separate pr for png
github-actions[bot] commented 2026-09-29 14:32:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-09-29T14:32:42Z.


Progress report (2e97617 → f9f02e3)

📈 System libraries: 72.65% (+0.14%, +4 implemented functions)

✅ 4 implemented
Library Name
libSceJpegEnc sceJpegEncCreate
libSceJpegEnc sceJpegEncDelete
libSceJpegEnc sceJpegEncEncode
libSceJpegEnc sceJpegEncQueryMemorySize
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5892382541 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/146#issuecomment-5892382541). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-09-29T14:32:42Z. --- <!-- progress-report --> ### Progress report (2e97617 → f9f02e3) 📈 **System libraries**: 72.65% (+0.14%, +4 implemented functions) <details> <summary>✅ 4 implemented</summary> | Library | Name | | - | - | | libSceJpegEnc | `sceJpegEncCreate` | | libSceJpegEnc | `sceJpegEncDelete` | | libSceJpegEnc | `sceJpegEncEncode` | | libSceJpegEnc | `sceJpegEncQueryMemorySize` | </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!146
No description provided.