Repository navigation
media: apple: jpeg: add a baseline JPEG decoder - #4160
RyanTheTide wants to merge 2 commits into
Conversation
The JPEG engine decodes as well as encodes, but the driver only exposed the encoder, so JPEG decode (MJPEG cameras, image viewers, GStreamer's v4l2jpegdec) fell back to the CPU. Add a second V4L2 stateful decoder device per engine, "apple-jpeg-dec": OUTPUT takes V4L2_PIX_FMT_JPEG, CAPTURE gives NV12, NV16 or NV24 in one buffer, MCU-padded, with the visible size as the compose rectangle. The engine parses the tables and restart markers itself; the driver checks the envelope with v4l2_jpeg_parse_header() (baseline SOF0, 8-bit, 4:2:0/4:2:2/4:4:4/grey, one interleaved scan, Huffman tables 0-1) and programs the geometry. JPEGs it refuses come back at once with an ERROR OUTPUT buffer. Greyscale decodes to NV12 with neutral chroma; the engine also converts between the three chroma layouts (box average down, replication up). The decode programming follows the public m1n1 JPEG experiment (proxyclient/experiments/jpeg.py), moved to the wide-address layout, with MODE 0x757, and the edge sample counts rounded up (the odd-width 4:2:2 chroma column was wrong with them rounded down). The CAPTURE format is set from the first JPEG's headers with a source change event; the OUTPUT format advertises V4L2_FMT_FLAG_DYN_RESOLUTION so clients wait for it. A later JPEG with other headers ends the decoded stream with an empty LAST buffer and another event, and a STOP sent meanwhile still drains once CAPTURE restarts, which GStreamer relies on at EOS. Decode is enabled on the T6041 and T8140 engines, where it was checked on hardware; the T6050 job layout and the untested engines keep only the encoder. Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
There was a problem hiding this comment.
Verdict: FINISH at f10fdf1
Failure: A valid multiplanar OUTPUT buffer with a nonzero data_offset is parsed from the wrong address. VB2 copies the offset from the application buffer and includes it in bytesused. aj_dec_buf_prepare_out() at apple-jpeg-drv.c:1073–1086 gives the JPEG parser the plane base and bytesused instead. A baseline JPEG preceded by an application header therefore starts with the wrong bytes at the parser and is returned in error. The decode job at apple-jpeg-drv.c:406–407 likewise starts DMA at the plane base and uses the allocation size. Use the same validated payload range for parsing and programming the JPEG input, with the required address alignment and documented hardware padding.
Checked: I traced V4L2 OUTPUT preparation, DMA job setup, CAPTURE geometry, the shared completion path and source-change handling. The reported hardware results cover ordinary unprefixed frames. Please consider splitting the header and geometry support from the decoder queue and controls so each can be reviewed independently.
DjDeveloperr
left a comment
There was a problem hiding this comment.
Request changes at f10fdf19: one decoder rule is broken, small fix. Geometry and buffer checks look right.
V4L2_DEC_CMD_START after a source change hangs the stream. aj_decoder_cmd() (apple-jpeg-drv.c:990-995) returns 0 for START but leaves cap_restart set, and aj_job_ready() (line 486) then refuses every job. No second event is sent, so the client waits forever. dev-decoder.rst ("Dynamic Resolution Change", step 1) lets a client resume with START instead of a CAPTURE restart when its buffers are large enough. Fix in the START branch:
if (ret)
return ret;
vb2_clear_last_buffer_dequeued(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx));
spin_lock_irqsave(&ctx->drain_lock, flags);
ctx->cap_restart = false;
ctx->change_last = false;
spin_unlock_irqrestore(&ctx->drain_lock, flags);
v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
return 0;I built this on the merge with main: W=1 and checkpatch clean, KUnit passes. aj_dec_run() already rejects a buffer that is too small for the new size, so this is safe.
Please also run two things on hardware and post the result:
- A stream whose frame header does not match the programmed size. The header is checked at QBUF, but the buffer stays mapped in the client. Also,
v4l2_jpeg_parse_header()keeps the last of two SOF segments, while the engine may use the first. Test: 640x480 with a second 3840x2160 SOF in front, and a 640x480 OUTPUT buffer rewritten to a 4K JPEG between QBUF and CAPTURE STREAMON. Pass means: nothing written past the CAPTURE buffer (check a neighbouring buffer filled with a pattern), no DART fault, next job is good. If the engine follows the stream's size, refuse a second SOF and copy the JPEG into a driver buffer. - T8132: this PR turns decode on there and it has not run on that chip. Either set
decodeonly for T6041 and T8140, or wait for a row. We can give one on our J773g.
data_offset from the earlier review: returning -EINVAL for a nonzero offset in aj_dec_buf_prepare_out() is enough.
Checked on the merge of this head with main dc4d6f06:
- W=1 arm64 objects clean, checkpatch --strict clean, KUnit (UML) 18/18.
- Header parsing: zero sizes, more than 16384, 12-bit, progressive, multi-scan, odd chroma factors and out-of-range table selectors are refused. Sizes fit in u32. A job needs the header, layout and buffer size to match, and
output_endis the end of the CAPTURE plane. - Format changes cannot happen under a running job. Drain, STOP during a pending change and queue stop mid-job follow the m2m helpers. Locks nest in one order.
- Encoder job building is unchanged. Engines without
decoderegister no second node.
Rows: J616s and Neo are in the body, but without the head SHA. After the push, post one line with the new head for J700 or J616s, plus T8132 if decode stays on there.
Clear the pending CAPTURE restart and LAST state after a successful START, then retry scheduling. Reject nonzero OUTPUT data offsets before parsing, since the decoder uses a padded buffer beginning at the plane base. Signed-off-by: DjDeveloperr <hey@djdev.me>
|
Pushed Both JPEG objects built with arm64 Still needed is an independent review and exact-head decode on J700 or J616s and J773g, including a resolution change that resumes with START and confirmation that writes stay within the programmed CAPTURE buffer. |
There was a problem hiding this comment.
Verdict: MERGE at 90babf7.
Checked: The OUTPUT prepare path rejects a nonzero data offset before parsing, and the supported zero-offset path matches the decoder's padded DMA input. The exact callbacks passed valid zero-offset preparation, offset and DMA alignment rejection, successful START after a source change, busy START with pending state preserved, and STOP/EOS. Successful START clears both restart flags and the last-buffer state before scheduling outside the drain lock. Job construction still verifies CAPTURE layout and length before programming the engine.
Follow-ups: Nonzero OUTPUT data offsets are explicitly unsupported on this path. Applications need to place the coded payload at the plane base.
|
J773g (Apple M4 Mac mini, T8132) JPEG + scaler module row receipt:
|
|
MacBook Neo (J700, T8140) row, 12-03 16:40Z, zero reboot, module swap on stock
Not covered: other T8140 Macs, larger frames, long runs, and the display/camera users of these blocks. |
|
Verified baseline JPEG decode on M3 Pro (J516s, T6030,
Once |
The JPEG engine can decode, but the driver only exposed the encoder. This adds a hardware JPEG decoder device and checks it on real hardware.
Problem: no hardware JPEG decode on any Apple SoC. Both apple-jpeg nodes are encoders, so MJPEG and JPEG decode (GStreamer
v4l2jpegdec, viewers) runs on the CPU.Change: a second V4L2 stateful decoder device per engine,
apple-jpeg-dec. OUTPUT takes JPEG. CAPTURE gives NV12, NV16 or NV24 (MCU-padded, with the visible size in the compose rectangle). The engine parses the tables and restart markers itself. The driver checks the envelope withv4l2_jpeg_parse_header()and programs the geometry from the public m1n1 JPEG experiment, moved to the wide-address layout. Source-change event plusV4L2_FMT_FLAG_DYN_RESOLUTIONhandle the first frame and mid-stream size changes. Decode is enabled only on T6041 and T8140. T6050 and untested engines stay encode-only.Tested: 7.3.0-aurora-fleet94 on J616s (M4 Pro, T6041) and Neo (T8140), both engines on each. 20 baseline JPEGs (4:2:0/4:2:2/4:4:4/grey, 33x17 to 3840x2160, odd sizes, q50/q90, restart intervals, optimized Huffman) decode with max abs diff 1 per plane against the ffmpeg mjpeg decoder: Y PSNR 64.8-71.0 dB. Hardware output is bit-identical between the two boards. Conversions to other chroma layouts give max diff 1 against a box-average/replicate model. Hardware encode then decode gives max diff 1 against ffmpeg's decode of the same file. GStreamer 1.28.9
filesrc ! jpegparse ! v4l2jpegdecpicks the node (rank 257) and matches the reference, including an 8-frame stream with 7 size changes. Neo 1080p 4:2:0 789 fps, 4K 211 fps; J616s 401 fps, 4K 106 fps (one job in flight). KUnit apple-jpeg 18/18 (4 new cases) on arm64 qemu. checkpatch --strict and W=1 are clean.Risk: two-way door. The change is new code paths behind a new video device. The encoder's job programming is unchanged. Adding a node shifts /dev/videoN numbering after the JPEG nodes on T6041/T8140 systems.
Still to run: T6050 (K114s) decode, which needs its own job layout. T8112/T8122/T6000 decode once a board is free. T8132 (J713) shares the T6041 variant, so decode is enabled there, but it has not been run on that board yet.
Closes #4101
Decode programming and envelope checks
proxyclient/experiments/jpeg.py,jpeg_doc.md), with the legacy offsets mapped to the wide-address layout through the existingdecode_block_base(0x16c) and layout fields. MODE for decode is 0x757, checked on both engines. The completion arrives as the same interrupt bit as an encode.Per-file results
All rows give max abs diff 1 (or 0) on Y, Cb and Cr. They are identical on J616s and Neo.
Hardware encode then decode, against the encoder's source frame: Y 37.4 dB / CbCr 38.1 dB at q90, 33.1/34.9 dB at q50 (noisy test frame).