Skip to content

GPU: Motion JPEG on the VCN JPEG block (VCNJpeg kext + plug-in, off by default) - #156

Merged
DrogaBox merged 10 commits into
mainfrom
feat/vcn-mjpeg-vt
Nov 26, 2026
Merged

DrogaBox merged 10 commits into
mainfrom
feat/vcn-mjpeg-vt

Conversation

@DrogaBox

Copy link
Copy Markdown
Owner

Motion JPEG (and JPEG) decoding on the JPEG block of the RX 6800 / 6800 XT / 6900 XT, for macOS 26.7.3 only. Off by default.

Apple's driver carries the code for the card's JPEG engine but never starts it. A test kext showed it works: through the VideoToolbox plug-in in this branch it decoded 4:2:0, 4:2:2 and dmb1 clips at 1280x720 in under 5 ms a frame, within 1 of FFmpeg. This PR turns that into a real part of GPU/.

What's in it:

  • GPU/jpeg/: VCNJpeg.kext (Lilu). It creates the JPEG queue and a 128 MiB block of buffers when the driver starts its video queues. That is the only safe moment: taking that memory later hung the GPU in testing. Programs reach it through the accelerator's video context (selector 258), which any process that decodes video can open. Every other request goes to Apple untouched.
  • Power only while decoding: PowerPlay client 21 and the queue start with the first picture, and both are given back 2 s after the last one. A job that times out stops the decoder until the next restart, and new sessions fall back to Apple's software decoder.
  • It checks the UUIDs of AMDRadeonX6000HWLibs and AMDRadeonX6000 and the first bytes of the 20 functions it uses. On any other build it stays off. It makes no ...Force Lilu calls.
  • decode/jpeg/: the plug-in is now always built. StartSession refuses when the kext's decoder is stuck or the picture doesn't fit its buffers, so VideoToolbox picks Apple's decoder.
  • Installer: install/jpeg.sh (the EFI part, like sensors.sh), decoders.sh --jpeg, install.sh --with-jpeg, and a jpeg column in validated-builds.json (25G342 = untested-on-gpu). The loader kext lists the JPEG plug-in. The package carries both only with VCNVT_JPEG_DECODER=1. GPU-Package.zip was rebuilt anyway, since the app tests require its scripts to match the repo (same as GPU decoders: MPEG-2, VC-1 and MPEG-4 part 2 on the VCN (off by default) #151). It uses the previous zip's epoch, so sensors, encoder, AV1 and VP9 are byte-identical. Only the scripts, the loader list and kextentry changed, plus a new pin.
  • Tests: jpeg/tests/run.sh covers the commands, the door under ASAN/UBSAN with 300k random requests, and the power rule (a 30 fps stream powers the block once and never turns it off between frames). It also runs the Apple and Mesa oracles when the KDK and Mesa are present. GPU/tests/run.sh builds the kext twice to the same bytes and compares its table with the running Mac's driver. vtjpegtest -system runs the installed plug-in.

Not done: the product kext itself has not run on a card yet. That boot test is the next step.

Droga added 10 commits November 25, 2026 01:42
A VideoToolbox plug-in for 'jpeg' and 'dmb1'. It is the VP9 plug-in's
VideoToolbox side with the VP9 parts taken out, over a backend that hands
each picture to the apps build of the VCNJpegProbe kext through the AMD
accelerator's video context (selector 258), which any process that decodes
video may open.

Off: ./build.sh builds the bundle only with VCN_JPEG=1, and no installer
puts it anywhere. Nothing of it has run on the GPU.

- jpeg/jpegprep.c rewrites a picture into the shape Mesa gives the block
  (SOI, DQT, DHT, SOF0, SOS, scan, EOI), adds Annex K's Huffman tables for
  Motion JPEG without them, and refuses what the block is not given
  (progressive, 4:2:2, 4:4:4, grey).
- jpeg/vcnjpeg.c: session, picture into an NV12 full-range buffer (BT.601).
- tests: plugintest with -DTEST_JPEG, and jpegtest (the preparer against
  ImageIO, 40000 damaged copies, the session against a made-up kext).
…ntext

A video context answers no selector until it is connected to a shared
user client (type 6) of the same task: IOConnectAddClient ->
IOAccelContext2::connectClient -> contextStart. Type 3 alone returned
kIOReturnNotReady on hardware. ja_iokit_open now opens both and connects
them; ja_iokit_close closes both.
…ox test

The JPEG 2.0 block has no output-format register: the sampling in the frame
header decides. As Mesa does on this block (radeon_dec_jpeg_check_format),
0x221111 is written as NV12 and 0x211111 (webcams), 0x221212 (FFmpeg) and
0x222121 as one packed YUYV plane. jpegprep takes the three 4:2:2 forms,
vcnjpeg hands 4:2:2 pictures to VideoToolbox as '422f' (the YUYV split on
the CPU) and 4:2:0 ones as '420f'; the session attributes list both.

tests/gpu/vtjpegtest registers the bundle in its own process only, asks for
exactly that decoder and compares every frame with FFmpeg (-apple runs the
same check with Apple's software decoder, on the CPU). tests/gpu/mkmjpeg422
writes Motion JPEG with the webcam sampling, which neither FFmpeg nor Apple's
encoder writes.

Run on an RX 6800 XT (macOS 26.7.3, VCNJpegProbe apps build): 1280x720, max
difference 1 against FFmpeg in all four runs; 4:2:0 jpeg 3.79 ms a frame
(PSNR 74.7 dB), dmb1 3.98 ms, 4:2:2 FFmpeg form 4.59 ms, webcam form 4.56 ms.
…ault)

Apple's AMDRadeonX6000HWLibs carries the driver of the Navi 21 JPEG block
but never creates its queue. VCNJpeg (jpeg/) is the product side of the test
kext that decoded through it: the queue and one block of buffers (128 MiB,
vcnjpegpool=8..128) are made inside startMMEngineQueues, the only time the
GART bump pointer allows it, and given back in stopMMEngineQueues.

Programs reach it through selector 258 of the accelerator's video context;
only requests with its own header are taken, the rest go to Apple. The block
is powered only while pictures arrive: PowerPlay client 21 and the queue
start with the first RUN, and a thread_call gives both back two seconds after
the last request (idle_logic.hpp). A timeout or a damaged guard stops the
decoder until the next restart.

It checks the LC_UUID of AMDRadeonX6000HWLibs and AMDRadeonX6000 (26.7.3,
25G342) and the place and first bytes of the 20 functions it uses, and uses
lilu.onKextLoad (no Force call). No sysctl, no root user client.

Tests (jpeg/tests/run.sh): the commands, the door with sanitizers and 300000
random requests, the power rule (a 30 fps stream powers the block once and
never off between frames), and the Apple and Mesa oracles when the KDK and
Mesa are given. tests/run.sh builds the kext twice to the same bytes and
compares its table with the running Mac's driver. Nothing has run on a card.
…oder

JPEGVCNDecoder.bundle is no longer behind VCN_JPEG=1; it goes to the VCNJpeg
kext (jpeg/). At session start it refuses, so that VideoToolbox picks Apple's
software decoder, when the kext's decoder is stuck or has no queue or
buffers (the INFO state bits, now JA_STATE_* in jpeg_app.h), and when the
session's size does not fit the kext's picture buffer (vcnjpeg_fits).

vtjpegtest -system decodes through whatever hardware decoder VideoToolbox
picks, the installed plug-in, without asking for our DecoderID.

Formatting of decode/jpeg and its tests with tools/format.sh.
install/jpeg.sh puts VCNJpeg.kext on the EFI or takes it off (efikext.zsh,
like sensors.sh) and checks the macOS build against the new "jpeg" column
of validated-builds.json (25G342: untested-on-gpu; check_lists.sh allows a
value other than false only for the build of jpeg/kext/hwlibs_expect.hpp).
decoders.sh --jpeg (VCNVT_JPEG_DECODER=1) adds the JPEG plug-in, and an
install without it takes off one an earlier install left; install.sh
--with-jpeg does both. check.sh shows the JPEG kext where it is built or
runs; undo.sh takes it off; kextentry knows VCNJpeg (MinKernel 25.0.0).
The loader kext lists the JPEG plug-in like the other five.

The package carries the plug-in and the kext only with VCNVT_JPEG_DECODER=1
(tools/package.sh, Tools/build-gpu-assets.sh). Docs: jpeg/README.md,
install/README.md, macos-updates.md, testing.md, CHANGELOG.
The zip carried the old install/ scripts and validated-builds.json. Rebuilt
with Tools/build-gpu-assets.sh, without VCNVT_JPEG_DECODER: the DMG carries
no JPEG plug-in or kext, only install/jpeg.sh. SOURCE_DATE_EPOCH of the zip
before (1795550629), so the sensors and encoding kexts and the AV1 and VP9
plug-ins are byte-identical to it; the loader kext changes (it lists the
JPEG plug-in) and kextentry knows VCNJpeg. New pin of its SHA256SUMS in
Tools/make-dmg.sh.
- jpegprep: a sample that ends in fill bytes (0xff 0xff, no EOI) made the
  fill loop stop at the last byte and read one past it. Refused now.
- vcnjpeg: the picture buffer was sized from the frame's SOF before the
  kext saw it, so a frame of a few hundred bytes saying 16384x16384 made
  the decoder process allocate about 400 MB. A picture larger than the
  kext's buffer is refused before anything is allocated or sent.
- the scan must name the frame's three components in the frame's order,
  and the frame's ids must differ (T.81 B.2.3): checked in jpegprep and,
  since any process can reach the door without it, in the kext's
  parseStream too (new Stream::BadScan).

Each has a test (jpegtest, cmds_test); the first two fail on the code
before.
Any process may reach the kext's door (VideoToolbox runs the plug-in in
whichever program decodes Motion JPEG), so instead of picking programs the
door now limits how one client holds the decoder:

- a client is its video context and its pid: a context freed and another
  allocated at the same address by another process no longer inherits the
  buffers (and the previous picture); RELEASE checks the pid too
- one turn lasts at most 5 s in all, however often the owner asks; the
  process that held it that long waits 2 s before it may take it again
- a client turned away with BUSY gets the next turn (kept 50 ms; the
  plug-in asks every 2 ms), so the one that just finished cannot take it
  back first
- only the owner's requests keep the block powered
- when a turn ends the JPEG is cleared as well as the picture: if the block
  ever reads past the write pointer (RB_SIZE is 0xfffffff0, as in Mesa;
  not tried on the GPU), it finds zeros, not another client's JPEG

Restart intervals stay as they are: the block reads DRI from the stream,
as Mesa gives it (vlVaGetJpegSliceHeader writes it, no IB register in
Mesa either). Noted in the README with what is still untried on the GPU.

apps_test: 18 new checks (other pid at the same address, the next turn,
the 5 s limit and the wait after it, refused requests do not keep the
block on, JPEG cleared, driver stop forgets every client).
Resolve the decoder test list after the HDR attachments change: the JPEG
plug-in test builds with -Icommon too, and the colour tests run only for
AV1 and VP9.
@DrogaBox DrogaBox mentioned this pull request Nov 26, 2026
@DrogaBox
DrogaBox merged commit 6ce8f27 into main Nov 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants