fix(libSceJpegEnc): preserve packed YCbCr samples #23534

Merged
iris8721 merged 2 commits from upstream-pr/23534 into main 2026-11-19 08:48:21 +00:00
iris8721 commented 2026-11-19 07:19:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by iris8721 on 2026-11-19T07:19:16Z.


What

sceJpegEncEncode converted packed Y8U8Y8V8 to RGB before compression, clipping color samples. Pass Y, Cb, and Cr directly to bundled TurboJPEG. 4:2:2 retains source chroma; 4:2:0 averages adjacent rows with libjpeg-turbo's rounding rule. Console rounding and JPEG bytes remain unverified.

Tested

Windows x86-64, WinLibs GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON. Tested a6fe9a1161 on bf10f07513.

  • With the new regression and the old encoder on e35c90cfe3, a luma sample of 32 decoded as 56. With the fix, all visible samples in the odd-sized, padded fixture decoded within two levels of expected at quality 100 in both sampling modes.
  • guest_jpeg_enc_odd_width_yuv, guest_jpeg_enc, guest_jpeg_encode_capacity, and decoder_jpeg passed.
  • Conventions and the debt-fragment check passed. No overlap across 2,003 open PRs.

No Linux, PS5, or game-title run.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5390608421 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23534). Originally posted by [iris8721](https://github.com/iris8721) on 2026-11-19T07:19:16Z. --- ### What `sceJpegEncEncode` converted packed `Y8U8Y8V8` to RGB before compression, clipping color samples. Pass Y, Cb, and Cr directly to [bundled TurboJPEG](https://github.com/libjpeg-turbo/libjpeg-turbo/blob/c85e6b905bf237038faa936dab160ebfc5da0344/src/turbojpeg.h#L1768-L1794). 4:2:2 retains source chroma; 4:2:0 averages adjacent rows with [libjpeg-turbo's rounding rule](https://github.com/libjpeg-turbo/libjpeg-turbo/blob/c85e6b905bf237038faa936dab160ebfc5da0344/src/jcsample.c#L265-L296). Console rounding and JPEG bytes remain unverified. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows x86-64, WinLibs GCC 15.2.0, Ninja, Release, `BUILD_TESTING=ON`. Tested `a6fe9a1161` on `bf10f07513`. - With the new regression and the old encoder on `e35c90cfe3`, a luma sample of `32` decoded as `56`. With the fix, all visible samples in the odd-sized, padded fixture decoded within two levels of expected at quality 100 in both sampling modes. - `guest_jpeg_enc_odd_width_yuv`, `guest_jpeg_enc`, `guest_jpeg_encode_capacity`, and `decoder_jpeg` passed. - Conventions and the debt-fragment check passed. No overlap across 2,003 open PRs. No Linux, PS5, or game-title run. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/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 - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
PrivacyOnion commented 2026-11-19 07:21:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T07:21:03Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against a6fe9a1161:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_jpeg_enc_odd_width_yuv_tests built cleanly.
  • CTest (guest_jpeg_enc_odd_width_yuv):
    1/1 Test #3994: guest_jpeg_enc_odd_width_yuv .....   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/Decoder/Jpeg/include/Decoder/Jpeg.hpp, core/Decoder/Jpeg/src/Jpeg.cpp, core/libs/prx/libSceJpegEnc/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #3994: guest_jpeg_enc_odd_width_yuv .....Subprocess aborted***Exception:   0.13 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified turbojpeg planar YUV buffer calculation with tj3YUVBufSize(cols, 1, rows, subsampling) in core/Decoder/Jpeg/src/Jpeg.cpp:115.
    • Audited odd width and height handling, chromaStride/lumaStride calculations, and row padding in encodeYuyv() in core/libs/prx/libSceJpegEnc/Export.cpp:211-255.
    • Checked memory copy boundaries and guest memory protections (GuestCopyIn_nid_no_patch / canCopyOut / GuestCopyOut_nid_no_patch) across all entry points in core/libs/prx/libSceJpegEnc/Export.cpp.
    • Verified error code mappings (SCE_JPEG_ENC_ERROR_INVALID_ADDR, SCE_JPEG_ENC_ERROR_INVALID_SIZE, SCE_JPEG_ENC_ERROR_INVALID_PARAM, SCE_JPEG_ENC_ERROR_INVALID_HANDLE) and parameter validation in core/libs/prx/libSceJpegEnc/Export.cpp.
    • Confirmed AGENTS.md compliance regarding comments, conventions, and technical debt documentation in docs/dev/debt/changes/fix-jpegenc-preserve-ycbcr.md.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5970877725 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23534#pullrequestreview-5970877725). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-19T07:21:03Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `a6fe9a1161`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_jpeg_enc_odd_width_yuv_tests` built cleanly. - **CTest** (`guest_jpeg_enc_odd_width_yuv`): ``` 1/1 Test #3994: guest_jpeg_enc_odd_width_yuv ..... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/Decoder/Jpeg/include/Decoder/Jpeg.hpp`, `core/Decoder/Jpeg/src/Jpeg.cpp`, `core/libs/prx/libSceJpegEnc/Export.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #3994: guest_jpeg_enc_odd_width_yuv .....Subprocess aborted***Exception: 0.13 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified turbojpeg planar YUV buffer calculation with tj3YUVBufSize(cols, 1, rows, subsampling) in core/Decoder/Jpeg/src/Jpeg.cpp:115. - Audited odd width and height handling, chromaStride/lumaStride calculations, and row padding in encodeYuyv() in core/libs/prx/libSceJpegEnc/Export.cpp:211-255. - Checked memory copy boundaries and guest memory protections (GuestCopyIn_nid_no_patch / canCopyOut / GuestCopyOut_nid_no_patch) across all entry points in core/libs/prx/libSceJpegEnc/Export.cpp. - Verified error code mappings (SCE_JPEG_ENC_ERROR_INVALID_ADDR, SCE_JPEG_ENC_ERROR_INVALID_SIZE, SCE_JPEG_ENC_ERROR_INVALID_PARAM, SCE_JPEG_ENC_ERROR_INVALID_HANDLE) and parameter validation in core/libs/prx/libSceJpegEnc/Export.cpp. - Confirmed AGENTS.md compliance regarding comments, conventions, and technical debt documentation in docs/dev/debt/changes/fix-jpegenc-preserve-ycbcr.md. Looks Good To Me
PrivacyOnion commented 2026-11-19 07:55:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T07:55:44Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 5d8f64484a:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_jpeg_enc_odd_width_yuv_tests built cleanly.
  • CTest (guest_jpeg_enc_odd_width_yuv):
    1/1 Test #3995: guest_jpeg_enc_odd_width_yuv .....   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/Decoder/Jpeg/include/Decoder/Jpeg.hpp, core/Decoder/Jpeg/src/Jpeg.cpp, core/libs/prx/libSceJpegEnc/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #3995: guest_jpeg_enc_odd_width_yuv .....Subprocess aborted***Exception:   0.12 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/Decoder/Jpeg/src/Jpeg.cpp: verified tj3YUVBufSize and tj3CompressFromYUV8 usage with correct plane alignments and buffer size checks in EncodePlanarYuv
    • core/libs/prx/libSceJpegEnc/Export.cpp: checked Y8U8Y8V8 pitch validation (image_pitch / 2 >= ((image_width + 1) & ~1u)) and row copyIn boundaries against image_pitch
    • core/libs/prx/libSceJpegEnc/Export.cpp: audited luma and chroma plane layout calculations, 4:2:2 and 4:2:0 subsampling, and row averaging rounding rules matching TurboJPEG
    • core/libs/prx/libSceJpegEnc/Export.cpp: checked guest address validation, copyIn/copyOut guards, and error codes (INVALID_ADDR, INVALID_SIZE, INVALID_PARAM, INVALID_HANDLE)
    • docs/dev/debt/changes/fix-jpegenc-preserve-ycbcr.md: verified debt fragment formatting and AGENTS.md compliance regarding comments and stubs

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5971223864 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23534#pullrequestreview-5971223864). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-19T07:55:44Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `5d8f64484a`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_jpeg_enc_odd_width_yuv_tests` built cleanly. - **CTest** (`guest_jpeg_enc_odd_width_yuv`): ``` 1/1 Test #3995: guest_jpeg_enc_odd_width_yuv ..... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/Decoder/Jpeg/include/Decoder/Jpeg.hpp`, `core/Decoder/Jpeg/src/Jpeg.cpp`, `core/libs/prx/libSceJpegEnc/Export.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #3995: guest_jpeg_enc_odd_width_yuv .....Subprocess aborted***Exception: 0.12 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - core/Decoder/Jpeg/src/Jpeg.cpp: verified tj3YUVBufSize and tj3CompressFromYUV8 usage with correct plane alignments and buffer size checks in EncodePlanarYuv - core/libs/prx/libSceJpegEnc/Export.cpp: checked Y8U8Y8V8 pitch validation (image_pitch / 2 >= ((image_width + 1) & ~1u)) and row copyIn boundaries against image_pitch - core/libs/prx/libSceJpegEnc/Export.cpp: audited luma and chroma plane layout calculations, 4:2:2 and 4:2:0 subsampling, and row averaging rounding rules matching TurboJPEG - core/libs/prx/libSceJpegEnc/Export.cpp: checked guest address validation, copyIn/copyOut guards, and error codes (INVALID_ADDR, INVALID_SIZE, INVALID_PARAM, INVALID_HANDLE) - docs/dev/debt/changes/fix-jpegenc-preserve-ycbcr.md: verified debt fragment formatting and AGENTS.md compliance regarding comments and stubs Looks Good To Me
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!23534
No description provided.