Skip to content

media: apple: ave: H.264 encode on the M1 Pro and M2 6070 firmware - #1440

Merged
RyanTheTide merged 14 commits into
integration/aurorafrom
ave-m1m2/6070-h264
Nov 10, 2026
Merged

RyanTheTide merged 14 commits into
integration/aurorafrom
ave-m1m2/6070-h264

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

Stacked on #1387. H.264 encode on the M1 Pro (T6000) and M2 (T8112) 6070 firmware.

The 6070 Start/Process command images (from #1300), then what the firmware and hardware need to code a picture:

  • a per-session parameter-set buffer, constant QP as rate-control mode 2, the first-Process byte;
  • SPS/PPS fields from the session parameters, compressed reference format, retained slice state from named first-picture defaults;
  • T6000 codec idle request cleared before RUN;
  • 6070 working-buffer, entropy-channel and neighbour sizes, per-picture neighbour banks;
  • the complete AVC first picture (slice map, encoder units, client area, transform mode, QP range);
  • frame results read from the 6070 result layout (per-slice coded bytes, parameter sets in their own buffer);
  • per-picture reference selection with the compressed reference surface laid out on the coded picture (data pair, then metadata pair), two alternating slots;
  • 8-bit depth set explicitly.

The last commit enables streaming for what is qualified: 8-bit H.264 (Baseline/Main/High), constant quality, low delay (I and P), frame widths in whole macroblocks. B pictures are not used yet (a request for them is encoded without), HEVC and other widths are refused.

Tests

  • KUnit apple-ave 28/28.
  • M2 (T8112), fleet51, GStreamer v4l2h264enc, NV12, module from this branch with no other changes: 1920x1080 300/300 frames at 155.7 fps, 3840x2160 120/120 at 41.1 fps; PSNR vs source Y 38.2 dB, U/V 45.2/45.9 dB (1080p) and 38.3 / 45.1 / 47.7 dB (4K); ffmpeg decodes every frame with no errors; 0 DART faults, 0 AVCs.
  • M1 Pro (T6000): the same streams (identical sizes and PSNR) at 67 fps (1080p) and 17.8 fps (4K) at the boot clock; with a PMP performance request for the encoder (separate change) 4K reaches 52.5 fps.

🤖 Generated with Claude Code

On Start the 6070.11.1 firmware maps a per-session buffer for the
parameter sets it generates (SPS/PPS, VPS/SPS/PPS for HEVC), at Start
+0xfb30 with its size at +0xfb38, and copies them there before it
answers. The driver left both zero, so the first Start on the M1 Pro
stopped in the firmware ("MappedMemory.cpp:39: paddr != 0") and never
answered.

Allocate a 4 KiB buffer per 6070 session for it and write the pair;
refuse to build a Start without it.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The 6070.11.1 firmware numbers constant QP as rate-control mode 2; 3 is
the 9003 number. Given 3, the M1 Pro firmware accepted Start but had no
rate control for the session and faulted on the first Process.

Write the firmware's own number for constant QP from the layout, and set
the byte the 6070 Process carries on the first frame after Start.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The first 6070 frame on the M1 Pro entered the pipe and hung: the
firmware saw no usable reference (max_num_ref_frames went one word too
early in the Start SPS, at +0x109d8 instead of +0x109dc), and the
compressed-reference selector at Start +0xfd7d was zero, so no
reconstruction writer was set up.

Write max_num_ref_frames where the 6070 Start keeps it, select the
compressed reference format (each reconstruction entry already carries
its data and metadata address), fill the SPS/PPS fields from the
session's own parameters with their H.264 meanings, write the constant
QP and the GOP interval, give HEVC its reference count, and start the
retained AVC and HEVC slice state from named first-picture defaults
instead of zeros.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The T6000 mailbox block has the same idle-request word at +0x38 as the
T8112 one. macOS clears it before the codec runs; the driver only did
so on T8112, so on the M1 Pro the codec could stay clock-gated: the
first frame started the pipe and never completed.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
On the M1 Pro the first 6070 frame stopped with an SMMU error at the
end of the colocated buffer: the driver sized it from the 9003 rule
(0x20000 at 1080p), but the 6070 engine needs 0xff000 per DPB slot
there. Give the 6070 colocated, low-resolution-result, neighbour,
entropy and HEVC input-control buffers their own size rules, with
KUnit values for 1080p and an odd geometry.

Also add a small driver for the AVE auxiliary DMA (SMMU) error block:
log the error record and acknowledge it, so a fault is reported once
instead of leaving its shared interrupt asserted until the kernel
disables it.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The 6070 auxiliary entropy channels need the same capacity as the main
ones. Sized from the frame height alone (0x480 at 1080p), both filled up
on the M1 Pro and the pipe stopped with its syntax-element-buffer-full
status; the first IDR used 0x880 of each once they were larger.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The 6070 Process repeats the four source-neighbour banks of Start (AVC
at +0x1348/+0x1368/+0x1388/+0x13a8, HEVC at +0x5f30..+0x5f90). The
driver left them zero. Fill them from the same four session buffers as
Start, and give the active neighbour-data channel its own capacity rule
apart from the page-rounded allocation.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The 6070 AVC Start left several work-defining fields zero: the slice
map (no entry, end row 0) and the encoder-unit count, so the bitstream
stage on the M1 Pro was armed, had its entropy input and never coded a
macroblock. Fill them (one slice ending at the visible height, one
unit), and complete the rest of the Start the same way: a per-client
working area, the full coded-header area, four low-resolution results,
the active entropy rows only, High transform mode matching the PPS,
the full QP range and no target bit rate in constant-QP mode, input
format, view counts, inactive multipass entries, first-picture
variance, frame-number/POC widths from the reordering mode, and flat
(H.264 default) scaling lists.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The 6070 firmware reports each frame in the slot's result buffer, not
in the 9003 layout: the coded bytes of each slice at +0x180 (0x220 per
slice record) less a tail byte at +0x38c, the generated parameter-set
length in bits at +0x98 with the Annex B bytes in the session's
parameter-set buffer, and for HEVC the generated slice-header length
at +0x398. The driver read the 9003 words, found zero and dropped the
first frames the M1 Pro produced.

Read the 6070 fields, prepend the parameter sets, and treat reply
+0x40 as a completion index, not a size.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
On the M1 Pro the IDR came out right but every P frame read a wrong
reference: chroma was off from frame 1 and luma drifted. The 6070
reconstruction surface is compressed: one allocation holds luma
metadata, then luma data, then chroma data. Start's two words per DPB
entry are luma data and luma metadata, and each Process carries the
four-address tuples of the current reconstruction and its list-0
reference; the driver left those zero and put the slot's auxiliary
buffer where the current low-resolution reference belongs.

Lay the reconstruction surface out from its own size formulas, fill
the current and list-0 tuples, colocated and low-resolution words per
picture (two DPB slots alternate in low-delay sessions), move the
slot's statistics buffer to its own word, and repeat the session's
low-resolution result buffers in every Process.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Sessions left p->bit_depth at zero for 8-bit input and only set it for
10-bit. The 6070 reference layout and its per-picture reference
selection test for depth 8, so on 8-bit sessions they were skipped and
every P frame on the M1 Pro and M2 used a zero reference. Set 8 for
8-bit sessions; the 9003 writers already treated zero as 8.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The compressed AVC reference is sized on the macroblock-aligned picture
(1080 lines code as 1088, 35 tile rows), and its data pair (luma, then
chroma) comes before the metadata pair (luma, then chroma). Start
carries the base of each pair; Process carries all four addresses in
the order luma data, luma metadata, chroma data, chroma metadata. The
previous layout put metadata first and was sized on the visible height,
so the engine wrote past the surface and over the reference.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Enable streaming on the M1 Pro and M2 6070 firmware for what is
qualified on both Macs: 8-bit H.264 (Baseline, Main or High) in
constant-quality mode with low delay, I and P pictures and two
alternating reconstruction slots, for frame widths in whole
macroblocks. A request for B pictures is encoded without them; HEVC
and other widths are refused until they are qualified.

Measured on the M1 Pro and M2 with GStreamer v4l2h264enc, 1920x1080 and
3840x2160 NV12, 300 and 120 frames: every frame decodes, PSNR 38.2 dB
(Y) / 45 dB (UV) at 1080p and 38.3 / 45-47 dB at 4K, no DART faults.

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