feat(libSceJpegDec): decode pixel format 10 as packed Y8U8Y8V8 #26193

Merged
Ducengarth merged 4 commits from upstream-pr/26193 into main 2026-11-25 10:29:11 +00:00
Ducengarth commented 2026-11-23 12:41:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ducengarth on 2026-11-23T12:41:22Z.


What

sceJpegDecDecode and sceJpegDecDecodeWithInputControl accepted pixel format 10 (Y8U8Y8V8) and then threw. This decodes it.

  • Decoder::Jpeg::DecodePlanarYuv decodes a JPEG, at any of the four down-scales, into its own Y, Cb and Cr planes through TurboJPEG's planar decoder, with no RGB round trip. It throws for sources that are not YCbCr or grayscale, and for subsamplings TurboJPEG cannot split into planes.
  • libSceJpegDec packs those planes into Y0 Cb Y1 Cr rows of (width + 1) / 2 * 4 bytes. That is the layout KytyPS5 uses, and the input libSceJpegEnc reads for the same format number (#23534).
    • 4:2:2 chroma is kept as stored.
    • Coarser chroma (4:2:0 rows, 4:1:1 columns, 4:4:1) is repeated.
    • 4:4:4 and 4:4:0 chroma pairs are averaged with libjpeg-turbo's alternating bias.
    • An odd width repeats the last sample. Grayscale gets 0x80 chroma. The alpha value is ignored.
  • The pitch, image-size and alignment checks are unchanged: a non-zero pitch must hold a row and be a multiple of 4.
  • Pixel format 9 and decode mode 1 still throw.
  • The debt fragment records what is unmeasured:
    • Whether the console interpolates chroma, rounds differently or goes through RGB is unknown.
    • Format 10 is stricter than unscaled formats 0, 1 and 11, which decode through stb. Corrupt data they accept returns 0x80650012 here, as it already does for scaled decodes.

Tested

Linux x86-64 (WSL Ubuntu 24.04, GCC 13.3, Ninja, Release, BUILD_TESTING=ON), at 771f1b1d9e on 7db4926943; rebased to db7f53ffe1 on 8e1af3d9b0, where main changed none of the JPEG files.

  • ctest -R '^(decoder_jpeg|guest_jpeg_.*)$': 14/14 passed.
  • New test guest_jpeg_dec_y8u8y8v8:
    • Source JPEGs are encoded from known planes with TurboJPEG in 4:2:2, 4:4:4, 4:2:0, 4:4:0, 4:1:1 and 4:4:1, at widths 17 and 16, plus a grayscale image. Each is decoded at scales 1, 2, 4 and 8.
    • Both decode calls are checked at pitch 0, at one row and at one row plus 8: the packed bytes, the result, the image info, and that pitch padding and bytes outside the buffer stay untouched.
    • Every sample is within 3 of the source.
    • Refusals write nothing: a short buffer (0x80650002, or 0x80650003 with a pitch), a misaligned address (0x80650001), and a pitch below a row or not a multiple of 4.
    • Format 9 still throws.
    • Round trip through sceJpegEncEncode format 10 at 4:2:2: every byte comes back within 3.
  • decoder_jpeg covers plane sizes and values for 4:4:4, 4:2:2 and 4:2:0, scaled plane ratios, grayscale, invalid scales, and empty, garbage and truncated input.
  • With main's Export.cpp the new test fails (sceJpegDecDecode: pixel format is not implemented).
  • Nine mutations are all killed: no bias, chroma row without the vertical ratio, Y1 read from padding, a wrong row size, a gray chroma of 0, pitch ignored, Cb/Cr swapped, no averaging, and a wrong 4:1:1 index.
  • check_conventions.py and debt_fold.py --check pass. pr_overlap.py --branch reports no overlap across 2,552 open PRs.
  • Not built on Windows or macOS. No PS5 measurement and no title run: no known title uses this format (issue #10114).

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5453309169 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26193). Originally posted by [Ducengarth](https://github.com/Ducengarth) on 2026-11-23T12:41:22Z. --- ### What `sceJpegDecDecode` and `sceJpegDecDecodeWithInputControl` accepted pixel format 10 (`Y8U8Y8V8`) and then threw. This decodes it. - `Decoder::Jpeg::DecodePlanarYuv` decodes a JPEG, at any of the four down-scales, into its own Y, Cb and Cr planes through [TurboJPEG's planar decoder](https://github.com/libjpeg-turbo/libjpeg-turbo/blob/c85e6b905bf237038faa936dab160ebfc5da0344/src/turbojpeg.h#L2150-L2191), with no RGB round trip. It throws for sources that are not YCbCr or grayscale, and for subsamplings TurboJPEG cannot split into planes. - libSceJpegDec packs those planes into `Y0 Cb Y1 Cr` rows of `(width + 1) / 2 * 4` bytes. That is the layout [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/3b549a9d8487a3e488760bffe3287b70d43f46be/src/libs/libJpegDec.cpp) uses, and the input libSceJpegEnc reads for the same format number (#23534). - 4:2:2 chroma is kept as stored. - Coarser chroma (4:2:0 rows, 4:1:1 columns, 4:4:1) is repeated. - 4:4:4 and 4:4:0 chroma pairs are averaged with [libjpeg-turbo's alternating bias](https://github.com/libjpeg-turbo/libjpeg-turbo/blob/c85e6b905bf237038faa936dab160ebfc5da0344/src/jcsample.c#L220-L253). - An odd width repeats the last sample. Grayscale gets 0x80 chroma. The alpha value is ignored. - The pitch, image-size and alignment checks are unchanged: a non-zero pitch must hold a row and be a multiple of 4. - Pixel format 9 and decode mode 1 still throw. - The debt fragment records what is unmeasured: - Whether the console interpolates chroma, rounds differently or goes through RGB is unknown. - Format 10 is stricter than unscaled formats 0, 1 and 11, which decode through stb. Corrupt data they accept returns 0x80650012 here, as it already does for scaled decodes. ### Tested Linux x86-64 (WSL Ubuntu 24.04, GCC 13.3, Ninja, Release, `BUILD_TESTING=ON`), at 771f1b1d9e on 7db4926943; rebased to db7f53ffe1 on 8e1af3d9b0, where main changed none of the JPEG files. - `ctest -R '^(decoder_jpeg|guest_jpeg_.*)$'`: 14/14 passed. - New test `guest_jpeg_dec_y8u8y8v8`: - Source JPEGs are encoded from known planes with TurboJPEG in 4:2:2, 4:4:4, 4:2:0, 4:4:0, 4:1:1 and 4:4:1, at widths 17 and 16, plus a grayscale image. Each is decoded at scales 1, 2, 4 and 8. - Both decode calls are checked at pitch 0, at one row and at one row plus 8: the packed bytes, the result, the image info, and that pitch padding and bytes outside the buffer stay untouched. - Every sample is within 3 of the source. - Refusals write nothing: a short buffer (0x80650002, or 0x80650003 with a pitch), a misaligned address (0x80650001), and a pitch below a row or not a multiple of 4. - Format 9 still throws. - Round trip through `sceJpegEncEncode` format 10 at 4:2:2: every byte comes back within 3. - `decoder_jpeg` covers plane sizes and values for 4:4:4, 4:2:2 and 4:2:0, scaled plane ratios, grayscale, invalid scales, and empty, garbage and truncated input. - With main's `Export.cpp` the new test fails (`sceJpegDecDecode: pixel format is not implemented`). - Nine mutations are all killed: no bias, chroma row without the vertical ratio, Y1 read from padding, a wrong row size, a gray chroma of 0, pitch ignored, Cb/Cr swapped, no averaging, and a wrong 4:1:1 index. - `check_conventions.py` and `debt_fold.py --check` pass. `pr_overlap.py --branch` reports no overlap across 2,552 open PRs. - Not built on Windows or macOS. No PS5 measurement and no title run: no known title uses this format (issue #10114). ### 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 --> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
PrivacyOnion commented 2026-11-23 12:43:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T12:43:48Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: decoder_jpeg_tests, guest_jpeg_dec_y8u8y8v8_tests built cleanly.
  • CTest (decoder_jpeg, guest_jpeg_dec_y8u8y8v8):
    1/2 Test #4245: decoder_jpeg .....................   Passed    0.01 sec
    2/2 Test #4254: guest_jpeg_dec_y8u8y8v8 ..........   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/Decoder/Jpeg/include/Decoder/Jpeg.hpp, core/Decoder/Jpeg/src/Jpeg.cpp, core/libs/prx/libSceJpegDec/Export.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Checked Decoder::Jpeg::DecodePlanarYuv for scaling denominator validation, buffer allocations, error handling, and TurboJPEG API usage (tj3DecompressToYUVPlanes8 and plane dimensions).
    • Verified writeY8U8Y8V8 row packing logic, including chroma subsampling ratios (4:2:2, 4:2:0, 4:4:4, 4:1:1, 4:4:0, 4:4:1), bias rounding, odd width replication, and grayscale 0x80 chroma defaults.
    • Verified checkTarget and imageRowSize calculations for PIXEL_FORMAT_Y8U8Y8V8 pitch and minimum image memory requirements.
    • Audited guest memory safety checks: CopyInPreservingErrno and CopyOutPreservingErrno calls for destination buffers and JpegDecImageInfo.
    • Inspected AGENTS.md compliance, including technical debt documentation fragment in docs/dev/debt/changes/jpegdec-y8u8y8v8.md and absence of prohibited code comments.
    • Verified test coverage in GuestJpegDecY8U8Y8V8.cpp and Jpeg.cpp across all supported subsamplings, odd dimensions, pitch offsets, scaling factors, and encode-decode round trips.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6021895452 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26193#pullrequestreview-6021895452). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-23T12:43:48Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `db7f53ffe1`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `decoder_jpeg_tests`, `guest_jpeg_dec_y8u8y8v8_tests` built cleanly. - **CTest** (`decoder_jpeg`, `guest_jpeg_dec_y8u8y8v8`): ``` 1/2 Test #4245: decoder_jpeg ..................... Passed 0.01 sec 2/2 Test #4254: guest_jpeg_dec_y8u8y8v8 .......... Passed 0.01 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `core/Decoder/Jpeg/include/Decoder/Jpeg.hpp`, `core/Decoder/Jpeg/src/Jpeg.cpp`, `core/libs/prx/libSceJpegDec/Export.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Checked Decoder::Jpeg::DecodePlanarYuv for scaling denominator validation, buffer allocations, error handling, and TurboJPEG API usage (tj3DecompressToYUVPlanes8 and plane dimensions). - Verified writeY8U8Y8V8 row packing logic, including chroma subsampling ratios (4:2:2, 4:2:0, 4:4:4, 4:1:1, 4:4:0, 4:4:1), bias rounding, odd width replication, and grayscale 0x80 chroma defaults. - Verified checkTarget and imageRowSize calculations for PIXEL_FORMAT_Y8U8Y8V8 pitch and minimum image memory requirements. - Audited guest memory safety checks: CopyInPreservingErrno and CopyOutPreservingErrno calls for destination buffers and JpegDecImageInfo. - Inspected AGENTS.md compliance, including technical debt documentation fragment in docs/dev/debt/changes/jpegdec-y8u8y8v8.md and absence of prohibited code comments. - Verified test coverage in GuestJpegDecY8U8Y8V8.cpp and Jpeg.cpp across all supported subsamplings, odd dimensions, pitch offsets, scaling factors, and encode-decode round trips. Looks Good To Me
mihiii1337 commented 2026-11-23 12:56:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mihiii1337 on 2026-11-23T12:56:18Z.

Review: APPROVED


I ran a focused native Windows GCC 15.2 fixture with the pinned TurboJPEG. A complete 5×3 grayscale JPEG produced exact format-10 bytes through both exports, including the repeated last luma sample, neutral chroma, and untouched 20-byte pitch padding. The same executable built against base 8e1af3d9b0 reached the expected unimplemented-format exception at runtime.

Removing only EOI kept the header readable. Format 10 returned 0x80650012 through both exports without touching the output, and the direct export also left imageInfo untouched. Unscaled RGBA accepted the same input on both revisions. A negative input callback after a successful header parse propagated its original status without output. This was a Windows decoder fixture using generated data.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6022060385 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26193#pullrequestreview-6022060385). Originally posted by [mihiii1337](https://github.com/mihiii1337) on 2026-11-23T12:56:18Z. **Review:** APPROVED --- I ran a focused native Windows GCC 15.2 fixture with the pinned TurboJPEG. A complete 5×3 grayscale JPEG produced exact format-10 bytes through both exports, including the repeated last luma sample, neutral chroma, and untouched 20-byte pitch padding. The same executable built against base 8e1af3d9b0 reached the expected unimplemented-format exception at runtime. Removing only EOI kept the header readable. Format 10 returned 0x80650012 through both exports without touching the output, and the direct export also left imageInfo untouched. Unscaled RGBA accepted the same input on both revisions. A negative input callback after a successful header parse propagated its original status without output. This was a Windows decoder fixture using generated data. AI-assisted: yes
Superseabas commented 2026-11-23 13:42:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-23T13:42:53Z.

Review: APPROVED


Windows build and run of head db7f53ffe1 with GCC 16, merged onto main 8e1af3d9b0 (clean merge). The Tested section is Linux only.

Setup: Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (x86_64-ucrt-posix-seh, r3), Release full build, run from PowerShell. Built together with #26165, #26189, #26190 and #26191, which touch other files.

  • ^(decoder_jpeg|guest_jpeg_.*)$: main 13 of 13, and with this PR 14 of 14 in 2 of 2 runs, including the new guest_jpeg_dec_y8u8y8v8 and decoder_jpeg's planar cases.
  • Red check: with the 4:4:4 / 4:4:0 averaging bias fixed at 0 (bias = pair & 1 → 0), guest_jpeg_dec_y8u8y8v8 aborts with failed: packed output. One of your mutations, caught on Windows too.

The code: the packing takes its chroma ratios from TurboJPEG's own plane sizes (chromaRatio throws on anything but 1, 2 or 4). It strides the luma by the padded plane width, so the odd-width 4:2:2 and 4:1:1 sources index the right samples. An odd last pair repeats Y0 and uses its own chroma, and the result packs through the existing reportImage path. The pitch and size checks use the same imageRowSize as checkTarget, so a pitch of 0 and an explicit one agree.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6022706887 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26193#pullrequestreview-6022706887). Originally posted by [Superseabas](https://github.com/Superseabas) on 2026-11-23T13:42:53Z. **Review:** APPROVED --- Windows build and run of head `db7f53ffe1` with GCC 16, merged onto `main` `8e1af3d9b0` (clean merge). The Tested section is Linux only. **Setup:** Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (`x86_64-ucrt-posix-seh`, r3), Release full build, run from PowerShell. Built together with #26165, #26189, #26190 and #26191, which touch other files. - `^(decoder_jpeg|guest_jpeg_.*)$`: `main` 13 of 13, and with this PR 14 of 14 in 2 of 2 runs, including the new `guest_jpeg_dec_y8u8y8v8` and `decoder_jpeg`'s planar cases. - **Red check:** with the 4:4:4 / 4:4:0 averaging bias fixed at 0 (`bias = pair & 1` → `0`), `guest_jpeg_dec_y8u8y8v8` aborts with `failed: packed output`. One of your mutations, caught on Windows too. **The code:** the packing takes its chroma ratios from TurboJPEG's own plane sizes (`chromaRatio` throws on anything but 1, 2 or 4). It strides the luma by the padded plane width, so the odd-width 4:2:2 and 4:1:1 sources index the right samples. An odd last pair repeats Y0 and uses its own chroma, and the result packs through the existing `reportImage` path. The pitch and size checks use the same `imageRowSize` as `checkTarget`, so a pitch of 0 and an explicit one agree. AI-assisted: yes (Claude Code)
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!26193
No description provided.