Skip to content

media: apple: jpeg: take NV12/NV16 in one buffer, keep CAPTURE size, fix drain - #1072

Merged
RyanTheTide merged 1 commit into
integration/aurorafrom
jpeg/sizes-v2
Nov 7, 2026
Merged

RyanTheTide merged 1 commit into
integration/aurorafrom
jpeg/sizes-v2

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

What

GStreamer's v4l2jpegenc on the J616s (T6041) failed in three ways with the merged driver. This fixes all three in drivers/media/platform/apple/jpeg/. The same code runs on the Neo (T8140).

  • 720p/480p DART faults and the 1080p read fault. The engine prefetches its input up to AJ_INPUT_END. With NV12M/NV16M the luma and chroma planes were separate buffers. The driver set AJ_INPUT_END to the end of whichever plane was higher, so the engine read past the end of the lower plane and faulted whenever the next page was unmapped. That happened at 640x480 and 1280x720 too, where no MCU padding is involved. It only worked when chroma happened to sit right below luma. At 1080p the planes were also sized in whole MCU rows (1088), and GStreamer places chroma using those sizes.
    The fix takes NV12/NV16 instead: one buffer holding the visible luma rows, the chroma rows right after them at the same stride, and the chroma rows up to the next MCU boundary at the end. AJ_INPUT_END is the end of the negotiated surface, so every read stays inside the buffer.
  • 720p/480p CAPTURE sizeimage mismatch. S_FMT(OUTPUT) recomputed the CAPTURE size. GStreamer sizes CAPTURE at the 1080p default, then sets 720p, so REQBUFS returned buffers smaller than the size it had agreed to. Now the size S_FMT(CAPTURE) returned is kept as a floor, and a larger size a client asks for is honoured, bounded at 64 MiB.
  • 4K EOS hang (gst-launch -e). ENCODER_CMD STOP and the IRQ completion raced with no common lock, so no CAPTURE buffer ever got LAST. Completion, STOP and CAPTURE QBUF now share a per-context spinlock. EOS is also sent when STOP completes at once or a late CAPTURE buffer is returned as LAST, and START clears the last-buffer-dequeued state.

A Daybreak review (t-5381) checked the design against the clean-room contract. AJ_INPUT_END is a single global input bound, and the proven encode layout is one allocation with chroma at an offset, so NV12M/NV16M cannot be made safe.

Hardware evidence (J616s, 7.3.0-aurora-fleet17 with this module, one-shot aura-try Stage 2 with the JPEG nodes enabled)

size encode (8 natural frames, q75) PSNR Y / avg gst-launch -e EOS engine 0 vs 1
3840x2160 8/8 43.8 / 41.8 dB rc 0 (x3 + x1) md5 identical
1920x1080 8/8 46.2 / 43.4 dB rc 0 (x3 + x1) md5 identical
1280x720 8/8 46.0 / 43.3 dB rc 0 (x3 + x1) md5 identical
640x480 8/8 45.8 / 41.6 dB rc 0 (x3 + x1) md5 identical

Before this change: 1080p hit a DART read fault (job failed: status 0x40), 720p and 480p were refused by GStreamer (buffer size 2785280 is smaller then negotiated size 6242304), and 4K with -e timed out at EOS.

After it, with both engines: 0 DART faults and 0 failed jobs. Throughput at q75 is 207 fps at 4K (bound by videotestsrc, which manages 199 fps on its own), 774 fps at 1080p and 1589 fps at 720p. The CPU jpegenc manages 98 fps at 4K. SELinux was enforcing with 0 AVCs.

Checks

  • checkpatch.pl --strict: 0 errors, 0 warnings, 0 checks.
  • W=1, arm64 clang, fleet config: drivers/media/platform/apple/jpeg 0 warnings before and after.
  • apple-jpeg KUnit: 7/7, including the new apple_jpeg_raw_layout_test (1080p, 720p, 480p, 4K, an odd size, 4:2:2) and apple_jpeg_coded_size_test.

Neo (T8140), 7.3.0-aurora-fleet18ctxr with this module, no reboot: 3840x2160, 1920x1080, 1280x720 and 640x480 each encoded 8/8. PSNR averages were 41.6, 43.4, 43.3 and 41.6 dB, and gst-launch -e reached EOS 3 times out of 3 at every size. Throughput was 188 fps at 4K and 623 fps at 1080p. There were 0 JPEG or DART faults, no error-level log lines, and SELinux was enforcing with 0 AVCs.

🤖 Generated with Claude Code

…fix drain

Three problems showed up with GStreamer's v4l2jpegenc on the J616s
(T6041); the same code runs on the A18 Pro (T8140).

The engine prefetches its input up to AJ_INPUT_END. With NV12M/NV16M the
luma and chroma planes are separate buffers, and the driver set
AJ_INPUT_END to the end of whichever plane lies higher. The engine then
read past the end of the lower plane into whatever followed it, and
faulted on the DART whenever that was unmapped: at 640x480 and 1280x720
too, where no MCU padding is involved. It only worked when the chroma
buffer happened to sit right below the luma buffer. The planes also
covered whole MCU rows (1088 rows at 1080p) in sizeimage, and GStreamer
places the chroma plane by those sizes, so it copied the frame to the
wrong offset and crashed or hung at 1080p.

Take NV12 and NV16 instead: one buffer with the visible luma rows, the
chroma rows straight after them at the same stride, and the chroma rows
up to the next MCU boundary at the end. AJ_INPUT_END is the end of that
buffer, so every read the engine makes is inside it. The luma rows of
the last MCU row read into the chroma rows, which are cropped from the
JPEG. bytesperline and the chroma offset are those any client computes
for NV12/NV16; only sizeimage carries the extra tail.

S_FMT on OUTPUT recomputed the CAPTURE size from the new raw size. A
client that sized CAPTURE first (GStreamer does, at the 1080p default)
and then set 1280x720 got smaller buffers from REQBUFS than the size it
had agreed, and refused them. Keep the CAPTURE size that S_FMT returned
as a floor when OUTPUT changes later, and honour a larger size a client
asks for on TRY/S_FMT(CAPTURE), bounded at 64 MiB.

ENCODER_CMD STOP chooses the last queued source buffer, and the IRQ
handler removes finished buffers, with no common lock: a job completing
while STOP ran could leave the queue just after STOP chose it, and no
CAPTURE buffer was ever flagged LAST. gst-launch -e at 4K, where a job
takes several milliseconds, waited for EOS forever. Serialise the
completion, STOP and CAPTURE QBUF on a per-context spinlock, send the
EOS event also when STOP completes at once or a late CAPTURE buffer is
returned as LAST, and clear the last-buffer-dequeued state on START so
a second stream drains too.

KUnit covers the raw layout (1080p, 720p, 480p, 4K, an odd size and
4:2:2) and the coded size floor and bound.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide
RyanTheTide merged commit 61def32 into integration/aurora Nov 7, 2026
1 check passed
@RyanTheTide
RyanTheTide deleted the jpeg/sizes-v2 branch November 7, 2026 05:52
RyanTheTide added a commit that referenced this pull request Nov 7, 2026
Brings aurora-wip up to the tested release branch: CTXR execmem
(#985, #1079), KFIX speaker sleep and resume fixes (#1077, #1080),
JPEG v2 (#1072), Jonathan's G17 and USB work (#1073-#1076), brcmfmac
flowring count (#1081), ANE H16 startup restart (#1066) and the
Touch ID per-prompt match (#904).

Signed-off-by: Ryan Murray <[email protected]>
Assisted-by: LLM
RyanTheTide added a commit that referenced this pull request Nov 8, 2026
Take the T8112 wide64 and T6000 legacy32 layout work onto current
integration. Integration already describes the T8112 and T6000 JPEG
engines and their DARTs (disabled) in the per-SoC peripherals files, and
its binding already covers both compatibles with the apple,jpeg fallback,
so keep integrations binding and DT files and drop the branchs
duplicate nodes. Keep the single-buffer NV12/NV16 wording from #1072.

Signed-off-by: Ryan Murray <[email protected]>
Assisted-by: LLM
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.

1 participant