Skip to content

media: apple: ave: 6070.11.1 encode quality: H.264 references, CBR/VBR, VUI timing - #2375

Open
ludagoo wants to merge 11 commits into
aurora-silicon:integration/aurorafrom
ludagoo:ave/t6021-rc
Open

ludagoo wants to merge 11 commits into
aurora-silicon:integration/aurorafrom
ludagoo:ave/t6021-rc

Conversation

@ludagoo

@ludagoo ludagoo commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #2374 (ave/t6021). Review the top four commits.

Base: #2374 (ave/t6021, 9aadf56a25ad). Four commits on branch ave/t6021-rc (ludagoo/linux), tip 4f6e71b51956. The settings are keyed on the firmware, not the machine: three are flags of struct ave_fw_layout set in ave_layout_6070_11_1, and the VUI timing is keyed on the stream. So T6000, T8112 and T6021 all get them. They were measured on T6021 (J475c) only.

  1. keep 6070.11.1 H.264 P pictures as references (h264_inter, d0b0e0aa187c). With the Process timescale word nonzero, every H.264 P slice had nal_ref_idc 0; with it zero, P slices have nal_ref_idc 1 and frame_num counts up. The driver writes zero for H.264; HEVC keeps it. The three motion lambdas at Start 0xff98 (full-pel search), 0xffa4 (inter mode decision) and 0xffa8 (intra mode decision): with all three zero, about 94% of P macroblocks were 8x8 partitions and constant quality 0 on a 720p lecture clip came out at 8.4 Mbit/s. With 64, 256 and 64, 8x8 partitions became rare, the same clip came out at 0.30 Mbit/s, and quality 50 took 1.8 times the bits of libx264 at the same PSNR. The values were tuned on that one 720p clip.

  2. offer CBR and VBR on 6070.11.1 (rate_control, a73782b55f09). Rate-control mode 2, the driver's CBR value from 9003, held the Start QP; mode 1 moved slice QP with the bit rate. CBR and VBR both submit mode 1. With a frame interval of 1000/30000 s every frame sat at the QP ceiling (about 0.31 Mbit/s for a 1.0 Mbit/s target), so the firmware gets the frame rate rounded to an integer, over 1; the client's timeperframe and G_PARM are unchanged, and the session keeps timeperframe exactly for the VUI timing (commit 4). A new rate written into the Process frame-rate words while streaming stepped the HEVC picture clock backwards, so the Start rate stays there and the next Process bit rate and VBR limit are scaled by start rate / new rate; an HEVC stream at 30, then 15, then 30 fps kept every picture. The host CBR filler budget follows the live rate. The filler itself is upstream's.

  3. encode 6070.11.1 one frame at a time by default (low_delay, 5ac023072f70). B-frames default to 0, and without B-frames each frame's reply is awaited before the next is submitted. A second session on the same encoder waits behind that frame. On T6000 and T8112 this changes today's default (three B-frames, pipelined); see "Open question" below.

  4. add VUI timing to 6070 parameter sets that lack it (4f6e71b51956). On T6021 the SPS had no VUI timing, and with B-frames off no VUI at all, so a raw Annex B reader guessed 25 fps. The driver adds timing to an SPS that has none, in the output buffer, from the client's timeperframe exactly (not the integer rate the firmware gets): H.264 num_units_in_tick the denominator and time_scale twice the numerator, with fixed_frame_rate_flag; HEVC vui_num_units_in_tick the denominator and vui_time_scale the numerator. The rate is taken in lowest terms, so 30/1 gives 1 and 60 (H.264) or 1 and 30 (HEVC), the same bytes as before, and 30000/1001 gives 1001 and 60000 or 1001 and 30000. If twice the numerator does not fit 32 bits, H.264 halves an even denominator instead; with an odd one the SPS is left as written. ffprobe on a raw H.264 stream prints the field rate, 60/1 for a 30 fps stream and 60000/1001 for 30000/1001; that is the field clock, the stream is 30 fps (an MP4 mux reports 30/1 and 30000/1001). An SPS that already has timing, or that the walk cannot follow (scaling lists, POC type 1, more than one HEVC sub-layer, longer than 256 bytes), is left as written. There is no flag: it runs for every 6070 parameter-set buffer.

    The bit reader is media: apple: ave: 26A428: 60 fps and no-B sessions, B-picture flags #2218's (struct ave_rbsp, ave_rbsp_init, ave_rbsp_bit, ave_rbsp_ue, the same code), with the buffer raised to AVE_RBSP_BYTES (256) for a whole SPS, plus ave_rbsp_get and ave_rbsp_get_ue for the SPS walk. Whichever of the two PRs lands second drops its copy.

What changes on T6000 and T8112

Not run there. For anyone running rows on an M1 Pro or M2:

  • Encodes with default controls change bytes for both codecs (B-frames default to 0), and H.264 also changes with B-frames set (P pictures are references, the lambdas). Production md5s will not match.
  • CBR and VBR are offered; constant quality stays the default.
  • The frame rate the firmware gets is rounded to an integer in every mode, including constant quality. The VUI timing does not use it: it comes from the client's timeperframe, so a 30000/1001 stream is stamped 1001 and 60000 (H.264) or 1001 and 30000 (HEVC).
  • B-frames default to 0, and frames are submitted one at a time.
  • Parameter sets gain VUI timing where the firmware wrote none.

The pinned-bytes KUnit case from #2374 still checks T6000/T8112 Start and Process bytes, built with these layout settings off.

Open question: low_delay

The review asked for all four settings to be keyed on the firmware. I moved low_delay too, so that the three chips run one configuration, but I think it is a default rather than a property of the firmware: it chooses latency over throughput, and on M1 Pro and M2 it turns off the three default B-frames and their pipelining. The alternative is to drop commit 3: every 6070 part keeps B-frames 3 and pipelining, and a T6021 client that asks for 0 B-frames gets submission ahead, which was not measured on T6021.

Result, plainly

CBR undershoots, and CBR and VBR are the same encode. The firmware's coded rate stays below the target; for CBR the host then adds filler NAL units. With filler removed, CBR and VBR produced byte-identical streams in every row below: the VBR peak changes nothing at these rates. H.264 gets closest on hard content and with B pictures; HEVC sits at about 55% of target everywhere and is not tuned.

Overlap: #2218 (T8142 60 fps, on #1900) conflicts with this PR in ave.h and ave-sess.c if it merges first; its reader is the one commit 4 uses, so the rebase removes one copy. #1900 (ave.h) and #1990 (ave.h, ave-abi-test.c) conflict textually. A rebase needs KUnit again, not hardware.

Tested

  • KUnit apple-ave (arm64, LLVM) at every commit: 47, 48, 48 and 49 tests, all passing, no compiler warnings. New cases: ave_6070_h264_inter (timescale 0 only for H.264; lambdas; skip mode and static mask unchanged), ave_6070_rate_control (CQ, CBR, VBR words; live-rate scale; no scale without the flag), SPS timing vectors for H.264 with and without VUI and for HEVC, and three SPS that already carry timing and must come out unchanged. The same table has 30000/1001 for H.264 with and without VUI and for HEVC, rates that reduce to 30/1 (60/2, 30000/1000) with the 30/1 bytes, the 32-bit limits (H.264 3000000001/2 and 3000000001/3, HEVC 4000000000/3) and a zero rate. Each flag's case checks that 6070.11.1 sets it.
  • checkpatch.pl --strict: 0 errors, 0 warnings, 0 checks on every commit.
  • tools/asahi/run-host-tests.py --only ave: 6/6 at this tip.
  • Mac Studio (M2 Max, J475c), kernel 7.3.0 with the previous version of this stack (the same settings as T6021 variant flags) and the aura PR. The session code that ran is the same as here; what changed since is where the flags live, the VUI stamp running without a flag, and the reader's API. Both encoders, same rows, byte-identical output. Sources: a 720p lecture recording (mostly static) and the same recording panned 16 px/frame across a 2x upscale (high motion), 300 frames each at 30 fps, NV12. "Coded" is the elementary stream with filler NAL units removed, Mbit/s over 10 s; "% of target" is coded / target. PSNR-Y is against the source after decoding.
Clip Codec Mode Target Coded % of target PSNR-Y
lecture H.264 CQ (default) - 0.548 - 42.15
lecture HEVC CQ (default) - 0.515 - 41.17
lecture H.264 CBR = VBR 1.0 0.810 81% 45.70
lecture HEVC CBR = VBR 1.0 0.591 59% 42.15
lecture H.264 CBR = VBR 2.5 1.455 58% 48.27
lecture HEVC CBR = VBR 2.5 1.323 53% 46.00
pan H.264 CQ (default) - 0.905 - 44.03
pan HEVC CQ (default) - 1.092 - 42.77
pan H.264 CBR = VBR 1.0 0.960 96% 44.80
pan HEVC CBR = VBR 1.0 0.552 55% 35.93
pan H.264 CBR = VBR 2.5 1.917 77% 49.06
pan HEVC CBR = VBR 2.5 1.324 53% 42.91

The largest coded second (30 consecutive pictures) went over target in three H.264 rows: lecture 1.0 (1.017 Mbit), pan 1.0 (1.076) and pan 2.5 (2.742, 10% over). HEVC stayed under in every second. CBR wire rates with filler on the lecture were 0.95 and 2.39 Mbit/s.

  • 1080p lecture, 150 frames, CBR 4.0 Mbit/s: H.264 1.824 coded (46%), PSNR-Y 47.75; HEVC 2.056 (51%), 46.85.
  • B pictures (video_b_frames=3, the only nonzero step): I10 P150 B140. H.264 CQ 0.670 (PSNR-Y 42.16), HEVC CQ 0.518 (41.15), H.264 CBR 1.0 Mbit/s 1.020 coded (102%, 45.80), HEVC CBR 1.0 0.580 (58%, 42.10).
  • Frame rate set before streaming, on the output queue (ffmpeg h264_v4l2m2m/hevc_v4l2m2m, ffmpeg -f rawvideo -pix_fmt nv12 -s 1280x720 -r <rate> -i src720.nv12 -frames:v 300 -c:v <codec> -b:v 1M, then ffprobe -show_entries stream=r_frame_rate on the raw stream): -r 60 gives 120/1 for H.264 (time scale 120, the field clock) and 60/1 for HEVC. ffmpeg leaves the bitrate mode at CQ, so these runs check timing, not rate. That output was read in the session; no log file was kept. That run was before the timing followed timeperframe: it stamped 30000/1001 as 30 (60/1 H.264, 30/1 HEVC). With this version 30000/1001 is stamped 1001 and 60000 (H.264) or 1001 and 30000 (HEVC), checked by the KUnit vectors only, not on the J475c.
  • An MP4 mux of a 30 fps H.264 stream reports r_frame_rate=30/1.
  • The live frame-rate change (HEVC 30, 15, 30 fps) and the integer-rate finding were run on the SmartRoom development tree with the same mechanism, before this series was split; not with this PR's build.

Not done yet

  • This version on the J475c (planned: H.264 P slices all references at 1080p, CBR 1 Mbit/s at 720p with coded rate and PSNR-Y, r_frame_rate on the raw streams, the same checks the maintainer offered to run on M1 Pro and M2).
  • T6000 and T8112 were not run with any of these settings.
  • CBR undershoots (table). VBR's peak has no visible effect. HEVC is not tuned: its lambda table is in the firmware TEXT image, which the driver does not change; HEVC at 1.0 Mbit/s on the pan is 35.9 dB.
  • Measured at 720p and 1080p on two clips; no 4K row. The lambdas come from one 720p clip.
  • No CBR or VBR encode was run at 60 fps.
  • A live S_PARM changes bits per frame only; timestamps and the VUI stay on the Start rate.
  • The firmware rate-controls a 30000/1001 stream as 30 fps; the VUI says 30000/1001. The 30000/1001 VUI was not run on the J475c.
  • bitstream_restriction with max_num_reorder_frames = 0 in the minimal VUI would cut decoder latency with B-frames off; not added.
  • Pipelined submission without B-frames was not measured on T6021.

🤖 Generated with Claude Code

T6000 and T8112 run the 6070.11.1 Start and Process commands that were
qualified on hardware byte for byte. Nothing tests that those bytes
stay the same when the shared 6070 writer changes for another chip.

Add a KUnit case that builds Start and the first two Process commands
for H.264 and HEVC, at 1280x720 30/1 and 1920x1080 30000/1001, with
default session parameters, and compares their CRC32 with the values
the builder produces at this commit.

Assisted-by: Grok 4.7
Assisted-by: Claude Opus 5.5
Signed-off-by: Lucas Goossen <lucas.goossen@hey.com>
Every instance creates the debugfs directory apple-ave. On a machine
with two encoders the second one fails to create it and has no
lifecycle_test, inject_wedge or last-command files.

Name the directory apple-ave-<device name>, for example
apple-ave-40d100000.video-codec, and update the two documents that
give the old path. The path changes on every chip.

Assisted-by: Grok 4.7
Assisted-by: Claude Opus 5.5
Signed-off-by: Lucas Goossen <lucas.goossen@hey.com>
@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks for this. The rate-control and VUI work is careful, and the CBR/VBR results table states its limits plainly.

One convention point. We select behaviour by a hardware or firmware property, not by the list of machines it was run on. The four new behaviours are enabled only on T6021:

  • drivers/media/platform/apple/ave/ave.h:261-263: "6070 encode settings measured on T6021 only. T6000 and T8112 leave them clear"
  • drivers/media/platform/apple/ave/ave-drv.c:115-118: .h264_inter, .rate_control, .low_delay, .vui_timing

T6000, T8112 and T6021 run the same 6070.11.1 firmware. The missing VUI timing and the firmware's RC mode numbering look like properties of that firmware, not of the M2 Max. Could these be keyed on the 6070 layout (or on a firmware-build property, if a build really differs), with a comment where a difference is known? We can run the rows on our M1 Pro (T6000) and M2 (T8112): H.264/HEVC CQ, CBR and VBR, B-frames on and off, and an ffprobe timing check. If any one of them misbehaves there, that result is the hardware reason to keep it per variant.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks for the plain results table, including the CBR undershoot. Three requests:

  1. ave.h:261 and ave-drv.c:115-118: h264_inter, rate_control, low_delay and vui_timing are set only on T6021, but T6000 and T8112 run the same 6070.11.1 layout. Could they hang off ave_layout_6070_11_1 instead? We have an M1 Pro and an M2 and will run your rows there: every H.264 P slice with nal_ref_idc != 0 at 1080p, CBR at 1 Mbit/s 720p (coded rate and PSNR-Y), and r_frame_rate on the raw streams. If one of them misbehaves on those parts, a per-variant flag with that reason in the comment is fine.
  2. vui_timing could key on the stream itself (the SPS has no timing_info) rather than on the chip.
  3. ave-sess.c:1192 onwards adds an RBSP reader, and media: apple: ave: 26A428: 60 fps and no-B sessions, B-picture flags #2218 adds another. Whichever lands second should reuse the first, so the driver keeps one.

As on #2374, please describe the T6021 offsets and values in the PR text and abi.rst by the effect they were checked against on the bitstream.

A boot loader that hands over two encoders cannot name both DATA images
with the one /chosen aurora,ave-data-crc32 property, and the encoders
share one TEXT reservation, so its iommu-addresses property carries
one tuple per encoder.

Read aurora,crc32 from each memory-region reservation and fall back to
the /chosen property when it is absent. Accept several iommu-addresses
tuples and use the one whose phandle is this device; a reservation with
none, or with two for this device, is refused as before. Describe the
reservation property in the admin guide, next to the /chosen ones.

Every chip goes through this code. With today's boot loader (no
aurora,crc32, one tuple per reservation) it does what it did before;
only T6021 was run.

Assisted-by: Grok 4.7
Assisted-by: Claude Opus 5.5
Signed-off-by: Lucas Goossen <lucas.goossen@hey.com>
The driver requires the extra0 register window to start on a 16 KiB
boundary. On T6021 neither encoder's window does: ave0's is 0x7c44
bytes at 0x2902803e0, ave1's 0x3dfc bytes at 0x28e680228.

dma_map_resource() maps the pages that contain an unaligned range and
returns the IOVA of its first byte, which is the address Config takes.
Drop the alignment check, and map the larger of the resource size and
the variant size, so that the whole window is covered: with the
variant size alone, the last 0x24 bytes of ave0's window would be
unmapped.

T6000's window is 0x24 bytes at a 16 KiB aligned address and T8112's is
page aligned and exactly its variant size, so both map 0x4000 bytes as
before. The alignment check goes for every chip: an unaligned window is
now mapped, not refused. The only such window outside T6021 is on the
second M1 Max encoder, which the device trees leave disabled.

Assisted-by: Grok 4.7
Assisted-by: Claude Opus 5.5
Signed-off-by: Lucas Goossen <lucas.goossen@hey.com>
The M2 Max runs a larger build of the 6070.11.1 firmware the T6000 and
T8112 support uses, AppleAVE2FW_H14C: TEXT 0xf0000 and DATA 0x134000,
with the link cell at TEXT 0x423c. Its two encoders share TEXT and
each has its own DATA. The coprocessor registers, boot argument and
timebase are T8112's. With boot device ID 20 and device type 17 both
encoders of a J475c boot, answer the handshake and encode. The
identity CRC, TEXT with the link cell cleared, is 0x0d56db87; select
the 6070.11.1 layout for it.

Config on T6021 needs a shared area. With the pointer and size at
+0x60 and +0x68 left zero, as on T8112, the firmware never answers
Config. With a 64 KiB, 16 KiB-aligned area both encoders answer
Config, Open and Start. Allocate it at the first boot, keep it until
the device is removed, and pass it for a variant with config_shared.
That is a property of the H14C build rather than of 6070.11.1: the
T8112 build answers Config with both words zero.

The 6070 sessions on T6021 run the T6000/T8112 commands unchanged in
this commit: one constant-quality session per encoder.

abi.rst said 6070 streams H.264 without B pictures; the 6070 path
already offers HEVC Main and B pictures, so describe that, and say
what T6021 adds and what each value was checked against. The Kconfig
help now says what each chip offers.

Assisted-by: Grok 4.7
Assisted-by: Claude Opus 5.5
Signed-off-by: Lucas Goossen <lucas.goossen@hey.com>
A session the firmware saw keeps its buffers until the coprocessor is
proved to have lost power, and that happens only after the last
session closes. With two sessions per boot, one long-lived stream and
a second that is opened and closed again and again keep every closed
session's buffers, a full set per session, with no bound.

Count the sessions waiting for the reap. Once four wait, refuse a new
session that would join the running firmware with EBUSY and a
rate-limited warning. The refused session was never handed to the
firmware, so it is freed at once. When the last stream closes, the
power cycle frees all of them and resets the count. This holds for
every layout; 9003 already ran two sessions per boot.

Assisted-by: Claude Opus 5.5
Signed-off-by: Lucas Goossen <lucas.goossen@hey.com>
The 6070.11.1 layout allows one session per firmware boot, while the
driver already keeps the process buffers per session. Allow two, on
every SoC that runs this firmware. On a J475c, four H.264 sessions,
two on each encoder, ran in real time for five minutes at their target
rates, and a fifth STREAMON got EBUSY. T6000 and T8112 were not run
with two sessions.

When the first of two sessions closes, its buffers stay quarantined
until the last session closes and the encoder is power cycled, as on
9003.

Assisted-by: Claude Opus 5.5
Signed-off-by: Lucas Goossen <lucas.goossen@hey.com>
Two Start and Process words make 6070.11.1 H.264 P pictures much
larger than they need to be. Both were checked on a J475c (T6021):

- With the Process timescale word nonzero, every P slice had
  nal_ref_idc 0, so no P picture was a reference. With it zero, P
  slices have nal_ref_idc 1 and frame_num counts up. Submit 0 there
  for H.264; the PTS still counts in the same timescale.
- With the motion lambdas at Start 0xff98, 0xffa4 and 0xffa8 zero,
  about 94% of P macroblocks were 8x8 partitions, and constant
  quality 0 on a 720p lecture clip came out at 8.4 Mbit/s. With 64
  (full-pel search), 256 (inter mode decision) and 64 (intra mode
  decision), 8x8 became rare, the clip came out at 0.30 Mbit/s, and
  quality 50 took 1.8 times the bits of libx264 at the same PSNR.

Name the lambda offsets in the 6070 layout, and key both on a new
layout flag, h264_inter, set for 6070.11.1: they are words of that
firmware's commands, and T6000 and T8112 run the same firmware
version. Those two were not run with them; their encoded H.264
changes. The lambdas were tuned on one 720p clip and checked with
CBR at 720p and 1080p.

The pinned T6000/T8112 command bytes are built with the flag off and
do not change; a new test checks what the flag writes.

Assisted-by: Grok 4.7
Assisted-by: Claude Opus 5.5
Signed-off-by: Lucas Goossen <lucas.goossen@hey.com>
@ludagoo ludagoo changed the title media: apple: ave: T6021 encode quality: H.264 references, CBR/VBR, VUI timing media: apple: ave: 6070.11.1 encode quality: H.264 references, CBR/VBR, VUI timing Nov 19, 2026
@ludagoo

ludagoo commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review. The updates are pushed; replies to each of your comments below.

Re: convention point; rows on M1 Pro and M2

Moved, see the next reply. Thanks for offering the rows; here are the J475c results from this r4 build (4001510ea360 + Stage 2 0f591198bf7b) so they can be compared directly:

  • 720p CQ default (300 frames, byte-identical across /dev/video11 and /dev/video12):
    • H.264: md5 dc475ee7eca930731bcb0cb42d07d714, 300 pics, all 290 P slices references (nal_ref_idc = 1), 0.547 Mbit/s coded (684369 B) / 0.548 Mbit/s whole (685269 B), r_frame_rate=60/1, PSNR-Y 42.150576 (avg 43.073046).
    • HEVC: md5 36ed04cc04166e7a8dad4b8d86324f06, 300 pics, 0.514 Mbit/s coded (642629 B) / 0.515 Mbit/s whole (643829 B), r_frame_rate=30/1, PSNR-Y 41.173396 (avg 42.382809).
  • 1080p H.264 CQ (150 frames): 150 pics, all 145 P slices references (nal_ref_idc = 1), 1.023 Mbit/s coded / 1.024 Mbit/s whole, r_frame_rate=60/1, PSNR-Y 44.012448.
  • 720p CBR 1 Mbit/s (300 frames):
    • H.264: coded 0.810 Mbit/s (1012184 B), whole 0.953 Mbit/s (1191540 B, 83 filler NALs), all 290 P slices references, decoded PSNR-Y 45.695759 (note: feeding the raw .h264 directly to ffmpeg's psnr filter warns about 1/1200000 vs 1/60 timebases and mispairs frames to 20.375; decoding to NV12 first aligns frames 1:1).
    • HEVC: coded 0.590 Mbit/s (738092 B), whole 0.950 Mbit/s (1187300 B, 231 filler NALs), PSNR-Y 42.149604.
  • 720p CQ with video_b_frames=3 (300 frames, v4l2-ctl):
    • H.264: [B140 I10 P150], all 150 P slices references, 0.670 Mbit/s coded and whole, r_frame_rate=60/1, PSNR-Y 42.161067.
    • HEVC: [B140 I10 P150], 0.517 Mbit/s coded / 0.518 Mbit/s whole, r_frame_rate=30/1, PSNR-Y 41.152364.

Re: three requests

  1. h264_inter, rate_control and low_delay are now fields of struct ave_fw_layout, set in ave_layout_6070_11_1, so T6000, T8112 and T6021 all get them. KUnit no longer asserts that T6000 and T8112 leave them clear; the pinned-bytes case from media: apple: ave: add the T6021 (M2 Max) encoder #2374 keeps checking the bytes with the layout's settings off. What this changes on M1 Pro and M2, for your rows:

    • Bytes change: for both codecs with default controls (B-frames default to 0), and for H.264 also with B-frames set (P pictures are references; the three motion lambdas). The production md5s will not match after this PR.
    • CBR and VBR are offered; constant quality stays the default.
    • The frame rate the firmware gets is rounded to an integer in every mode, constant quality included. The VUI timing then says 30 for a 30000/1001 stream.
    • B-frames default to 0, and each frame is answered before the next is submitted.

    I disagree on low_delay and moved it only so all three chips run one configuration in your rows. It is a default (latency over throughput), not a property of the firmware, and on M1 Pro and M2 it turns off the three default B-frames and the pipelining they have today. If you prefer, I will drop that commit: then every 6070 part keeps B-frames 3 and pipelining, and the T6021 results stand only for a client that asks for 0 B-frames, with submission ahead that was never measured on T6021.

    The lambdas (64/256/64) were tuned on one 720p clip on T6021. If M1 Pro or M2 code worse with them, that is a reason for a per-firmware-build value rather than a per-machine one.

  2. vui_timing is gone. The stamp runs on every 6070 parameter-set buffer and does nothing to an SPS that already carries timing; three new KUnit vectors (H.264 with timing, H.264 VUI with timing, HEVC with timing) check that they come out unchanged.

  3. The reader now uses media: apple: ave: 26A428: 60 fps and no-B sessions, B-picture flags #2218's API, struct ave_rbsp with ave_rbsp_init, ave_rbsp_bit and ave_rbsp_ue, the same code, so whichever lands second drops its copy. This PR adds ave_rbsp_get (n bits) and ave_rbsp_get_ue on top. The one difference is the buffer: media: apple: ave: 26A428: 60 fps and no-B sessions, B-picture flags #2218 reads 24 bytes of a slice header, the SPS walk needs the whole SPS, so here it is AVE_RBSP_BYTES, 256. With one reader, media: apple: ave: 26A428: 60 fps and no-B sessions, B-picture flags #2218's slice-type read copies up to 256 bytes per frame instead of 24; if that matters, ave_rbsp_init can take a length cap.

abi.rst now says what each value was checked against on the bitstream (timescale word and nal_ref_idc; the lambdas and the 8x8 share and bit rates; RC modes 1 and 2; the integer frame rate and the QP ceiling; frame-rate changes while streaming).

@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks for moving these onto the layout and for switching to #2218's reader.

On low_delay, we would decide by measurement. On our M1 Pro and M2 we will run 1080p H.264 and HEVC with this head and with #2374 alone (three B-frames, pipelined) and compare frames per second. If one frame at a time stays within 5%, keep commit 3. If it doesn't, please drop it, and the J475c row becomes three B-frames with two pipelined sessions per encoder.

One small change: ave-sess.c:152-155 replaces fps_num/fps_den with the rounded rate, and the VUI stamp reads them, so a 30000/1001 stream is stamped as 30. Could the firmware get the rounded rate while the VUI keeps timeperframe (H.264 num_units_in_tick 1001, time_scale 60000)? A KUnit vector for 30000/1001 would cover it.

6070.11.1 offers constant quality only, because the driver's CBR
value, 2, holds the Start QP on that firmware. On a J475c (T6021),
mode 1 moved slice QP with the bit rate. Without B pictures the coded
rate stayed under the target; for CBR the host's filler data then
brought 1.0 and 2.5 Mbit/s (720p) and 4.0 Mbit/s (1080p) within 15% of
the target over the file and over its last 150 frames. Submit mode 1
for CBR and VBR and offer both.

With a frame interval of 1000/30000 s every frame sat at the QP
ceiling: about 0.31 Mbit/s coded for a 1.0 Mbit/s target. Give the
firmware the frame rate rounded to an integer, over 1. The client's
timeperframe and G_PARM are not changed, and the session keeps
timeperframe exactly (client_fps_num, client_fps_den) for the VUI
timing, which must not follow the firmware's rounded rate.

A new frame rate written into the Process frame-rate words while
streaming stepped the HEVC picture clock backwards. Keep the Start
rate there; store a rate set by S_PARM while streaming in the context,
not the session (the worker frees the session without vdev_lock), and
scale the next Process bit rate and VBR limit by start rate / new rate
so the bits per frame follow. An HEVC stream at 30, then 15, then 30
frames per second kept every picture. The CBR filler budget uses the
live bit rate and frame rate, and drops a surplus above the buffer
size after a rate cut.

Key this on a new layout flag, rate_control, set for 6070.11.1: the
mode numbering is that firmware's, like rc_mode_fixed_qp beside it.
T6000 and T8112 run the same firmware version and were not run with
it. They now also offer CBR and VBR; constant quality stays the
default, and its frame rate is rounded the same way.

Assisted-by: Grok 4.7
Assisted-by: Claude Opus 5.5
Signed-off-by: Lucas Goossen <lucas.goossen@hey.com>
Default the B-frames control to 0 on a low_delay layout, and without
B-frames wait for each frame's reply before submitting the next, as
for the first frame of every session. A second session on the same
encoder waits behind that frame.

Set it for 6070.11.1. Every T6021 measurement in this series was made
this way; submitting frames ahead without B-frames was not measured
on any 6070.11.1 SoC. On T6000 and T8112 this changes the defaults:
B-frames 3 to 0, and one frame in the firmware at a time. A client
that asks for B-frames still gets them and their pipelining.

Assisted-by: Grok 4.7
Assisted-by: Claude Opus 5.5
Signed-off-by: Lucas Goossen <lucas.goossen@hey.com>
6070 firmware writes the parameter sets to a buffer of their own. On
a J475c (T6021) the SPS there had no VUI timing, and with B-frames off
no VUI at all, so a raw Annex B reader guessed 25 fps. When an SPS
from that buffer has no timing_info, rewrite it in the output buffer:
set timing in an existing VUI, or add a minimal one. The timing is the
client's timeperframe exactly, not the integer rate the firmware gets:
H.264 gets num_units_in_tick equal to the denominator and time_scale
twice the numerator, with fixed_frame_rate_flag, which is how H.264
codes a frame rate; HEVC gets vui_num_units_in_tick equal to the
denominator and vui_time_scale equal to the numerator. The rate is
taken in lowest terms, so 60/2 gives the bytes of 30/1, and 30000/1001
gives 1001 and 60000 (H.264) or 1001 and 30000 (HEVC). ffprobe on a
raw H.264 stream prints the field rate (60/1 at 30 fps) and an MP4 mux
reports 30/1.

When twice the numerator does not fit 32 bits, H.264 halves an even
denominator instead; with an odd one there is no exact form and the SPS
is left as written.

The rewrite is keyed on the stream, not on a SoC or firmware flag: an
SPS that already has timing is left alone, and so is one this walk
cannot follow (scaling lists, POC type 1, more than one HEVC
sub-layer, more than 256 bytes). Firmware that puts its parameter
sets in the coded buffer (9003, 26A428) is not touched.

The SPS is read with the RBSP reader of the T8142 slice-type probe
in aurora-silicon#2218 (struct ave_rbsp, ave_rbsp_init(),
ave_rbsp_bit(), ave_rbsp_ue()), with ave_rbsp_get() and
ave_rbsp_get_ue() added and the buffer raised to 256 bytes so a whole
SPS fits. Whichever of the two
lands second drops its copy.

Assisted-by: Grok 4.7
Assisted-by: Claude Opus 5.5
Signed-off-by: Lucas Goossen <lucas.goossen@hey.com>
@ludagoo

ludagoo commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Good catch, thanks. Fixed: the firmware still gets the rounded rate, and the VUI now comes from the client's timeperframe.

What changed

  • offer CBR and VBR on 6070.11.1 (a73782b55f09): ave_sess_params() still gives the firmware fps_int over 1, and now also keeps timeperframe unrounded in client_fps_num / client_fps_den. G_PARM and the Start/Process bytes are as before.
  • add VUI timing to 6070 parameter sets that lack it (4f6e71b51956): both stamps take the client rate instead of fps_num / fps_den.
    • H.264: num_units_in_tick = den, time_scale = 2 * num, with fixed_frame_rate_flag. 30000/1001 gives 1001 and 60000.
    • HEVC: vui_num_units_in_tick = den, vui_time_scale = num. 30000/1001 gives 1001 and 30000.
    • The rate is taken in lowest terms, so 30/1, 60/2 and 1000/30000 s give the same bytes as before. If 2 * num does not fit 32 bits, H.264 halves an even den instead (same rate). With an odd den there is no exact form, so the SPS is left as the firmware wrote it. HEVC always fits.
  • abi.rst, the two commit messages and the PR text no longer say a 30000/1001 stream is stamped as 30. While there, I fixed the commit message of the VUI commit to name ave_rbsp_get() / ave_rbsp_get_ue(), which are what it adds (it said ave_rbsp_bits()).

KUnit vectors (ave_sps_timing, now also with a rate per case):

  • 30000/1001 for H.264 without a VUI (274d001fda014016e8400000fa40003a9821), H.264 with a VUI and HEVC without one.
  • 60/2 and 30000/1000 giving the 30/1 bytes.
  • 32-bit limits: H.264 3000000001/2 (stamped, 1 and 3000000001), H.264 3000000001/3 (left as written), HEVC 4000000000/3, and a zero rate.
  • The existing vectors are unchanged and pass.

I built the expected bytes from the 30/1 vectors by replacing the two timing words, not with the driver's code.

Checks (arm64 build host): KUnit apple-ave passes at every commit (47, 48, 48, 49 tests, no compiler warnings); checkpatch --strict has 0 errors, warnings and checks on every commit; run-host-tests.py --only ave is 6/6 at the base and at each tip. Separately, with a throwaway test that I did not commit, ave_sess_params() gave the firmware 30/1 and kept the client rate for 1001/30000, 1/30, 2/60, 1000/30000 and 1/60.

New tips

I have not run the new 30000/1001 stamp on the J475c; the KUnit vectors are the check so far.

low_delay: no change. I will wait for your measurement on M1 Pro and M2 (1080p H.264 and HEVC, this head against #2374 alone, 5%) and drop commit 3 if one frame at a time falls outside it.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks for the fix. The firmware keeps the rounded rate, and both VUI stamps now take the client's timeperframe, so 30000/1001 is stamped as 1001/60000. The 32-bit edge vectors are a good addition.

The low_delay measurement is still on us. We will run it on 4f6e71b against #2374 alone (9aadf56): 1080p H.264 and HEVC on our M1 Pro and M2, with the 5% bar as agreed, and post the numbers here.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Here are the low_delay numbers from our M1 Pro (T6000) and M2 (T8112), plus one more reason to keep commit 1.

Each arm is its own boot of the same kernel, with modprobe.blacklist=apple_ave and that arm's apple-ave.ko loaded by hand. drivers/media/platform/apple/ave comes unchanged from each head. The driver can't be unbound, so arms can't be swapped at runtime. The source is a 120-frame 1080p30 moving SMPTE pattern in NV12, encoded with gst v4l2h264enc / v4l2h265enc at driver defaults. The numbers are fps, median of runs 2 and up (the first run after boot is an outlier on both Macs).

M2 H.264 M2 HEVC M1 Pro H.264 M1 Pro HEVC
#2374 alone (9aadf56) 143.9 116.4 115.1 145.6
this head (4f6e71b) 112.3 (−22%) 101.8 (−13%) 86.7 (−25%) 118.0 (−19%)
this head with 5ac0230 reverted 139.2 (−3%) 116.6 (0%) 116.7 (+1%) 144.8 (−1%)

Commit 3 misses the 5% bar on both Macs, so as agreed, please drop it. With it reverted, the H.264 stream is byte-identical to this head's, so the cost is only the one-frame-at-a-time submission. One more observation: with gst defaults, #2374 alone produced no B-frames on either Mac. Its streams are byte-identical to the current driver's, so the "three B-frames, pipelined" path isn't what a default gst client gets.

Commit 1 fixes a real bug on T6000 and T8112. Today's H.264 stream is an IDR followed by non-reference P pictures that all carry frame_num 1, with pic_order_cnt_type 2. That gives every P picture in a GOP the same POC, and H.264 7.4.2.1.1 doesn't allow POC type 2 with consecutive non-reference pictures. A POC-ordered decoder then shows the frames out of order. GStreamer's v4l2slh264dec on the same Macs puts frames 1-3 after frame 29 in every GOP. Each decoded frame is bit-exact; only the order is wrong. ffmpeg outputs in decode order, which hides it. With this head, the P pictures are references and frame_num counts up, and all 120 frames come back bit-exact and in order through v4l2slh264dec on both Macs. Commit 1 costs nothing measurable (the reverted row above).

Details
  • Kernel: our fleet kernel (7.3.0, integration plus open heads); only apple-ave.ko differs per arm. Each module was built against that tree, with no warnings.
  • H.264 streams: media: apple: ave: add the T6021 (M2 Max) encoder #2374 alone nal_ref_idc 0 / frame_num 1 on every P picture; this head and the reverted build nal_ref_idc 1, frame_num 1, 2, 3, ... (trace_headers).
  • Decode order check: per-frame NV12 md5 of the v4l2slh264dec output against a full ffmpeg software decode of the same clip. media: apple: ave: add the T6021 (M2 Max) encoder #2374-alone clip 4/120 in position (the I pictures), every other frame matches a software frame from a different position (order 0, 4..29, 1, 2, 3 per GOP). This head's clip: 120/120.
  • M1 Pro H.264 is noisy (19-211 fps across runs), so media: apple: ave: add the T6021 (M2 Max) encoder #2374 alone and the reverted build got 26 runs each over two boots. All other cells are 4 runs on one boot.
  • 0 SErrors, 0 DART faults, 0 failed units on every arm.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr 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.

Pre-PR Checklist Review (§1–§6) — 4f6e71b51956f7cb7d8ea26d343799983972fed0

Commits 1, 2, and 4 are technically strong — in particular, commit 1 (d0b0e0aa187c, h264_inter) fixes a confirmed H.264 7.4.2.1.1 POC-type-2 / frame_num compliance bug on T6000 and T8112 (verified by @RyanTheTide in #issuecomment-7080614409: 120/120 frames decoded bit-exact and in order via v4l2slh264dec vs 4/120 before). However, four hard blockers prevent merging this PR as-is.

Blockers

  1. §4 Hard Blocker — AI trailers (Assisted-by:) inside commit messages:
    All 11 commits in the PR (including all 4 top commits d0b0e0aa, a73782b5, 5ac02307, 4f6e71b5 and the 7 stacked #2374 commits) contain Assisted-by: Grok 4.7 and/or Assisted-by: Claude Opus 5.5 trailers in the git commit messages. Per §4 of the pre-PR checklist, commits must carry human author/committer and human Signed-off-by: trailers only (zero AI trailers in commit messages; keep the Assisted-by: disclosure in the PR body only).

  2. §2 / §5 Blocker — Drop commit 3 (5ac023072f70, low_delay) per M1 Pro & M2 measurements:
    Per @RyanTheTide's A/B benchmark in #issuecomment-7080614409, 5ac023072f70 regresses 1080p encode throughput on both T8112 (M2) and T6000 (M1 Pro) by −13% to −25% across every cell (−22% M2 H.264, −13% M2 HEVC, −25% M1 Pro H.264, −19% M1 Pro HEVC), missing the agreed 5% bar. Reverting 5ac023072f70 restores throughput to within −3% to +1% while keeping the H.264 stream byte-identical. Please drop commit 3 as agreed.

  3. Merge conflicts (DIRTY) on integration/aurora and unmerged #2374 stack:

    • The branch is 643 commits behind integration/aurora and has merge conflicts across drivers/media/platform/apple/ave/ (ave-drv.c, ave-sess.c, ave-layout.c, ave-v4l2.c, ave-abi-test.c, etc.) after #2675 (T8122/T8132/T8142 27.2), #2697 (host harnesses), and #2778 (per-device debugfs and workqueue names, which overlaps #2374's dce6e177).
    • All 7 commits from still-open #2374 are currently carried here. Because commit 1 (d0b0e0aa, H.264 P-picture references / POC-type-2 fix) is a standalone correctness fix for already-shipped T6000 and T8112 encoders with zero regression, consider either splitting commit 1 onto integration/aurora independently or rebasing the stack cleanly once #2374 is rebased/merged (and checking deduplication against #2218's RBSP reader if landed).
  4. §5 Evidence — Final rebased head hardware verification:

    • T6000 (M1 Pro) and T8112 (M2) now have decode-order and throughput A/B evidence for commit 1 + 5ac02307 reverted (#issuecomment-7080614409, 0 SErrors / DART faults / failed units).
    • After dropping commit 3 and rebasing, please run the remaining unrun hardware checks noted in the PR body ("Not done yet"): J475c (T6021) with pipelined submission (commit 3 dropped) and the 30000/1001 VUI stamp, plus T6000/T8112 CBR/VBR and VUI r_frame_rate checks.

Code Audit Notes (Commits 1, 2, 4 — Non-blocking)

  • Commit 1 (d0b0e0aa, h264_inter): Clean and well-bounded; KUnit covers both h264_inter on/off and confirms HEVC timescale is untouched.
  • Commit 2 (a73782b5, rate_control): Good separation of firmware rounded rate (fps_num/fps_den) vs exact client_fps_num/client_fps_den, and good CBR surplus clamp on rate drop. Minor: consider pairing WRITE_ONCE in ave_s_ctrl() for ctx->bitrate / ctx->bitrate_peak to match the READ_ONCE() calls in ave_sess_cbr_fill() / ave_build_process(), and note DIV_ROUND_CLOSEST(t->denominator, t->numerator) in ave_s_parm() can overflow 32-bit addition if userspace passes denominator near U32_MAX.
  • Commit 4 (4f6e71b5, VUI timing): ITU-T H.264 Table E-1 and H.265 Table E.1 SPS/VUI bit walks, ave_vui_ticks() 32-bit overflow handling, and KUnit vectors all check out cleanly. Minor: ave_h264_stamp_timing() and ave_hevc_stamp_timing() each place struct ave_rbsp + rebuilt[320] + escaped[384] (~968 B + locals) on the stack, which is close to a 1024-byte Wframe-larger-than limit if built with a tighter frame warning threshold.

Checklist Summary

  • §1 No model gating: PASS (layout flags on ave_layout_6070_11_1, stream-driven VUI rewrite).
  • §2 Production-ready: FAIL (drop low_delay commit 3; resolve DIRTY conflicts on integration/aurora).
  • §3 Safety: PASS.
  • §4 Clean-room & attribution: FAIL (Assisted-by: trailers in all 11 commits).
  • §5 Evidence: PARTIAL (T6000/T8112 decode-order & perf verified via #issuecomment-7080614409; rebased final-head rows pending).
  • §6 Naming: PASS.

Assisted-by: LLM (Gemini 4 Neon)

@RyanTheTide

Copy link
Copy Markdown
Contributor

Commit 1 (d0b0e0aa187c, the POC type 2 / frame_num fix) stands on its own. On our M1 Pro and M2 it is what takes v4l2slh264dec from 4/120 to 120/120 frames in order, bit-exact. Picked onto today's integration/aurora (d84d1c5), its driver changes (ave-layout.c/.h, ave-sess.c, ave-writer.c, ave.h) apply cleanly. Only abi.rst and ave-abi-test.c conflict.

Would you like to send that commit as its own PR so it can land while the rest is reworked? If you'd rather not, we can open it for you with your authorship and sign-off kept as they are, and link it here. Either way, we will run the decode row on that head when it is up.

Assisted-by: LLM (Claude:Opus 5.5)

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.

3 participants