Skip to content

feat(libSceJxr): create WMP encoders and decoders as jxrlib does - #21991

Merged
oneandonlydean merged 6 commits into
boykopovar:mainfrom
frankea:feat/jxr-wmp-codec-create
Nov 20, 2026
Merged

oneandonlydean merged 6 commits into
boykopovar:mainfrom
frankea:feat/jxr-wmp-codec-create

Conversation

@frankea

@frankea frankea commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

What

PKImageEncode_Create_WMP and PKImageDecode_Create_WMP (libSceJxr) no longer throw. On main both were bare NotImplemented_nid_no_patch stubs, and PKCodecFactory_CreateCodec already sends the two WMP IIDs to them. They follow pinned jxrlib JXRGlueJxr.c, the same commit libSceJxr already follows.

Constructors (src/Wmp.cpp):

  • PKImageEncode_Create_WMP (L1498-L1524) runs the existing guest PKImageEncode_Create, returns its error unchanged if it fails, then installs the eleven _WMP slots (Initialize, Terminate, SetColorContext, SetDescriptiveMetadata, WritePixels, WritePixelsBandedBegin, WritePixelsBanded, WritePixelsBandedEnd, Transcode, CreateNewFrame, Release) and sets bWMP (offset 520).
  • PKImageDecode_Create_WMP (L2261-L2279) runs the existing guest PKImageDecode_Create, then installs Initialize, GetSize, GetRawStream, Copy, GetColorContext, GetDescriptiveMetadata and Release. GetPixelFormat, GetResolution, GetFrameCount and SelectFrame keep the generic slots, as upstream.
  • PKCodecFactory_CreateCodec now calls both with their own prototypes instead of through a cast function pointer.

Slots. Each slot is an APS5_VABI thunk that calls the upstream _WMP function on the guest record through a C bridge (src/NativeWmp.c, declared in src/WmpBridge.h). It does not build JXRGlueJxr.c a second time. The file is already compiled into jxr_metadata_native (Metadata.cmake), where PKFree and FreeDescMetadata are renamed to the guest-heap versions, and PKAlloc comes from the guest-heap JXRGlue.c of jxr_glue_guest (nm shows a single _PKAlloc, and it calls JxrGuestCalloc_nid_no_patch). A second copy would define every _WMP symbol twice. JXRMeta.c, which the decoder uses to read descriptive metadata, is already in jxr. The new Wmp.cmake adds the bridge to jxr_metadata_native and the thunks to libSceJxr.

Guest ABI. On Windows APS5_VABI is sysv_abi, and the upstream code calls stream callbacks and record slots with the host ABI. To handle that:

  • Stream callbacks go through host-ABI adapters: an 8-byte guest pointer after a WMPStream whose six callbacks call the guest callbacks through APS5_VABI.
  • Adapters that the codec keeps (wmiSCP.pWStream from Initialize, pPATempFile from WritePixelsBandedBegin) are owned per record and freed by the record's Release. A new _Create_WMP at the same address drops any left over.
  • Inside a call, pStream is swapped for a stack adapter and then restored, so guest code sees its own stream pointer in pStream and from GetRawStream.
  • The adapter's Close points the record field back at the guest stream before it calls the guest Close, so the guest gets &pStream, as upstream.
  • PKImageEncode_Transcode_WMP calls GetPixelFormat, SetPixelFormat, SetSize, GetResolution, SetResolution and GetRawStream on guest records, so it is ported (L1359-L1435) into NativeWmp.c with APS5_VABI casts. The Microsoft licence notice is kept at the top of the file, as in ListStream.cpp.

Throws, where upstream is undefined or crashes:

  • Unknown pixel formats in WritePixels, WritePixelsBanded and Transcode: NotImplemented_nid_no_patch, as Transcode.cpp and WriteSource.cpp already do. Upstream ignores the PixelFormatLookup error in all three. WritePixels (L1185) and Transcode then read an uninitialized PKPixelInfo. WritePixelsBanded declares it as PKPixelInfo PI = {0} (L1237), so its behaviour is defined there, but encoding with a zeroed pixel info is not verified, so this PR throws in that case too.
  • DPKVT_LPWSTR descriptive text in SetDescriptiveMetadata, and in the header that WritePixels and Transcode write: NotImplemented_nid_no_patch. Upstream measures it with wcslen((wchar_t*)…), and the guest wchar_t width is not settled.
  • Null records, streams, rectangles and outputs where upstream dereferences them, Copy before Initialize and a null GetRawStream slot reached by Transcode: std::invalid_argument.
  • Calls in an encode state that upstream only asserts against: std::logic_error, before any codec state changes. WritePixels needs an encoder that has not started a banded or non-banded encode (L1188), so a second WritePixels or a WritePixels after WritePixelsBandedBegin throws. WritePixelsBandedBegin has the same rule (L1224), so a second Begin throws. WritePixelsBandedEnd throws unless a banded encode has written its first band, so also right after WritePixelsBandedBegin (L1310). In a Release build upstream would instead set up a second codec over the first, or replace the temp file of the first Begin.
  • Transcode with an alpha mode above 3: std::invalid_argument before any codec state changes. Upstream only asserts the range (L1399), and its alpha checks before that assert do not catch mode 4 or higher with an alpha source. The test checks mode 4 with an alpha source, and a control without the check fails it.
  • SetDescriptiveMetadata after the header returns -109 before the native call, so a Debug build does not stop on upstream's assert(FALSE). The result is the same as in Release.
  • SetColorContext reuses the existing Jxr::SetImageEncodeMetadata helper, which already backs the EXIF/GPS/IPTC/Photoshop setters, with the color-context offsets (200/208).

Debt. The fragment docs/dev/debt/changes/feat-jxr-wmp-codec-create.md adds two libSceJxr entries (919 and 730 characters). They cover: not measured on a PS5, LP64 layout inferred, the host adapters held in internal stream fields, codec state on the host heap (not freed when a decode stops early, as upstream), and the throws above.

Progress (python3 tools/progress.py --module libSceJxr, then --compare, base 3b7ffd7a05c): libSceJxr goes from 136 of 143 (95.10%) to 152 of 161 (94.41%), -0.69%. The percentage drops because the scanner counts every APS5_VABI definition, so the 18 slot thunks are now counted: 14 as implemented and 4 as pending. Both constructors move from pending to implemented (136 + 2 + 14 = 152, 7 - 2 + 4 = 9). EncodeWritePixels, EncodeBanded, EncodeTranscode and EncodeSetDescriptiveMetadata show as pending because they reach NotImplemented_nid_no_patch for the unknown-format and wide-text cases above.

Neighbouring PRs. pr_overlap.py --names reports no open PR for either function. pr_overlap.py --branch merged 4 sharing PRs without a line overlap. It could not compare #15646 (EnumConversions), which no longer merges with main (CMakeLists.txt, Export.cpp, Api.hpp). This PR changes two lines of PKCodecFactory_CreateCodec and deletes the two stubs in Export.cpp, next to the code #20362 (CreateDecoderFromFile, which reaches these constructors through CreateCodec), #19182 and #18247 touch. Rebasing around them is mechanical. #21550 (checked guest copies in ImageEncode.cpp) and #21717 (progress counting, which touches BitIo.cpp, GlueHelpers.cpp and Metadata.cpp) are open libSceJxr PRs that share no file with this one.

Tested

macOS 27.0.1, x86_64 build under Rosetta 2 (Ninja, Release, /usr/bin/cc/c++, BUILD_TESTING=ON). Not built or run on Linux or Windows; the Windows ABI path (adapters, APS5_VABI casts) is only exercised as the identity ABI here. No title. Built on main 3b7ffd7a05c merged in; ninja builds every target this PR touches. Two unrelated targets fail locally: libSceLibcInternal/TimeGetFields.cpp from main (reinterpret_cast from const void* casts away qualifiers on this clang) and the libcurl.prx link (local environment).

  • New guest_jxr_wmp (core/libs/tests/GuestJxrWmp.cpp with a host jxrlib reference in GuestJxrWmpReference.c, registered in libSceJxr/Wmp.cmake like the other libSceJxr tests):
    • Slot layout compared with the generic constructors and with CreateCodec for both IIDs. Out-of-memory returns -101 with a null output.
    • Guest encodes are byte-identical to host jxrlib over the whole 1 MiB stream buffer for 24bppBGR with a color context and descriptive metadata, for banded 24bppBGR (including the -111 for a 15-line non-final band), for 32bppBGRA with planar alpha, and for banded 32bppBGRA with planar alpha through a guest temp-file stream (the temp buffer also matches host jxrlib byte for byte).
    • Guest decodes match host jxrlib and the source pixels (lossless QP index 1) for both formats, including the planar alpha path. Color context, the -103 short-buffer case, descriptive metadata and GetRawStream are checked.
    • Transcode_WMP output is byte-identical to host jxrlib for 24bppBGR and for 32bppBGRA with planar alpha (uAlphaMode 2, which runs the second alpha transcode), and a generic decoder with no GetRawStream slot throws.
    • The expected throws are checked, including a second WritePixels, WritePixelsBandedBegin after WritePixels, a second WritePixelsBandedBegin, WritePixels during a banded encode and WritePixelsBandedEnd before WritePixelsBandedBegin and before the first band. The guest heap's live-block count returns to its starting value after each release.
    • NativeWmp.c pins the LP64 layout it relies on with _Static_asserts, including offsetof(PKImageEncode, bWMP) == 520.
  • Control: the same test against main's stub bodies (only the prototypes changed so the test compiles) aborts with std::runtime_error: PKImageEncode_Create_WMP_nid_postfix not implemented.
  • ctest -R jxr (26 tests, including guest_jxr_factory, guest_jxr_decode_create, guest_jxr_glue, guest_jxr_metadata(_patched) and the guest_jxr_lookup* patched-prx tests): 26 of 26 passed, 3 runs.
  • guest_pointer_audit, export_gap, unbuilt_exports and public_headers*: 68 of 68 passed.
  • tools/check_conventions.py --base origin/main passes. tools/debt_fold.py --check fails on main because two libSceLibreSsl3 fragments change the same entry. With that unrelated fragment removed in a scratch copy, it passes and --show libSceJxr places both new entries under Functional.

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 (Claude Code)

PKImageEncode_Create_WMP and PKImageDecode_Create_WMP follow pinned jxrlib JXRGlueJxr.c: they run the generic constructors and install guest-ABI thunks for the _WMP slots, backed by the JXRGlueJxr.c code already linked into libSceJxr. Guest stream callbacks are reached through host adapters, and Transcode_WMP calls guest record slots with the guest ABI.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_jxr_wmp_tests, jxr_wmp_reference built cleanly.
  • CTest (guest_jxr_wmp):
    1/1 Test #4028: guest_jxr_wmp ....................   Passed    0.05 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceJxr/Export.cpp, core/libs/prx/libSceJxr/include/Api.hpp, core/libs/prx/libSceJxr/src/NativeWmp.c, core/libs/prx/libSceJxr/src/Wmp.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified PKImageEncode_Create_WMP_nid_postfix and PKImageDecode_Create_WMP_nid_postfix creation routines and slot bindings against upstream jxrlib JXRGlueJxr.c
    • Verified WMP stream proxying and VABI callback bridges in NativeWmp.c and Wmp.cpp including scope guard restoration and target unwrapping
    • Audited proxy lifetime management in proxies map guarded by proxyMutex, properly releasing retained proxy buffers on encoder and decoder release
    • Checked AGENTS.md rules, technical debt fragment formatting in docs/dev/debt/changes/feat-jxr-wmp-codec-create.md, and license/comment conventions
    • Verified 64-bit portability (LP64/LLP64) in static asserts and type alignments in NativeWmp.c and WmpBridge.h

Looks Good To Me

@github-actions

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 15646 core/libs/prx/libSceJxr/CMakeLists.txt
core/libs/prx/libSceJxr/Export.cpp
core/libs/prx/libSceJxr/include/Api.hpp

@github-actions

Copy link
Copy Markdown

Progress report (6d46e92 → 0ca88cc)

📈 System libraries: 41.95% (+0.02%, +16 implemented, +4 declared functions)

✅ 16 implemented
Library Name
libSceJxr DecodeCopy
libSceJxr DecodeGetColorContext
libSceJxr DecodeGetDescriptiveMetadata
libSceJxr DecodeGetRawStream
libSceJxr DecodeGetSize
libSceJxr DecodeInitialize
libSceJxr DecodeRelease
libSceJxr EncodeBandedBegin
libSceJxr EncodeBandedEnd
libSceJxr EncodeCreateNewFrame
libSceJxr EncodeInitialize
libSceJxr EncodeRelease
libSceJxr EncodeSetColorContext
libSceJxr EncodeTerminate
libSceJxr PKImageDecode_Create_WMP_nid_postfix
libSceJxr PKImageEncode_Create_WMP_nid_postfix
🆕 4 declared as stubs
Library Name
libSceJxr EncodeBanded
libSceJxr EncodeSetDescriptiveMetadata
libSceJxr EncodeTranscode
libSceJxr EncodeWritePixels

Changed libraries (implemented exports)

Library Before After Change
libSceJxr 136/143 (95.10%) 152/161 (94.41%) -0.69%

@ycbuzi

ycbuzi commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Tested 0ca88cc5fb merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • macOS 13.7.8 (Intel MacBook Pro 8,1, i5-2415M: AVX but no AVX2, BMI, LZCNT, MOVBE or F16C), LLVM 19.1.7, relinker-only build: run 1: 99% tests passed, 12 tests failed out of 848; run 2: 98% tests passed, 13 tests failed out of 848. 12 of the failing tests fail on main too.

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

@oneandonlydean
oneandonlydean merged commit bc21a9f into boykopovar:main Nov 20, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants