Skip to content

media: apple: avd: decode VP9 frames smaller than the capture format - #2136

Open
sithglan wants to merge 4 commits into
aurora-silicon:mainfrom
sithglan:thomas/avd-vp9-smaller-frames
Open

sithglan wants to merge 4 commits into
aurora-silicon:mainfrom
sithglan:thomas/avd-vp9-smaller-frames

Conversation

@sithglan

@sithglan sithglan commented Nov 17, 2026 •

Copy link
Copy Markdown

Stacked on #3017 (commits 1 and 2, the temporal-MV and reference-scale fixes, split out so they can land on their own). Review commits 3 and 4 here. Commit 3 now also fixes the 1280x720+ kept-map case, and has the two-bank and later-enable fixes from Dj's review (head a8e9047aac50).

The AVD now decodes VP9 frames that are smaller than the capture format, so a VP9 stream can change size on an inter frame and keep its references. Today the driver refuses such a frame ("unexpected bitstream resolution") and stops the stream. Accepting it surfaced three bugs in the VP9 path that a size change exposes, and each is fixed in its own commit.

Why

VP9 inter frames can change size and keep predicting from references of the old size: WebRTC and SVC streams do this, as do the libvpx resize_inter conformance vectors. FFmpeg's V4L2 request VP9 hwaccel handles it in FFmpeg PR 20847 (fix 5 of SomeComputerGuy's series): it keeps the capture buffers and decodes a frame that fits in place. On the AVD that path failed, because validate_dec_params() requires the aligned frame size to equal the capture format. mainline rkvdec has the same check.

The commits

  1. Temporal MVs only from a frame of the same size. VP9 forbids using the previous frame's MVs after a size change, and avd_vp9_uses_prev_mv() didn't check the size. With only the size check relaxed, every non-error-resilient stream crashed the firmware at the first frame of the new size: open firmware error 0x8, a timeout and a decoder reset.
  2. Reference scale halves. set_refs() put the horizontal scale in the upper 16 bits. The decoder reads the vertical one there. This only showed with references whose aspect ratio differs from the frame's: vp90-2-05-resize (352x288 to 282x173) and vp90-2-18-resize.
  3. Segmentation map reset on a size change. VP9 clears the map when the size changes. After a size change the patch sets VP9_SEG_RESET for every frame that uses segmentation until one updates the map: the decoder doesn't carry a reset map forward, so a frame that keeps the map (update_map 0) after a reset frame would read the old map again. It also clears each bank's seg buffer, which carries part of the map over, before the bank next runs (a busy bank's buffer belongs to the hardware until it retires).
  4. Accept frames that fit, on the legacy transport. A smaller frame is decoded into the current buffers, with their stride; a larger one is still refused. As in media: apple: avd: decode VP9 frames smaller than 64x64 on the legacy transport #2773, the firmware transports (open-queue and the Apple backend) keep requiring the format's size (avd_backend_is_open()): an input their firmware does not expect can reset the engine instead of failing the frame. That scope comes off in a follow-up with the open-queue row (the 26 resize vectors and the aq-mode streams, after media: apple: avd: VP9: one segmentation map per stream, shared by the banks #2195).

Rebased onto #2773 (integration/aurora f5fae3c): this series reuses its avd_backend_is_open(). In tests/vp9-test.py, this series' blocks run first, at the 1920x1088 format; #2773's 64x64 blocks run after them.

Testing

On a J813 (M5 MacBook Air), with this series built as a module against the 7.3 J813 tree. AVD there uses the legacy transport, one VP9 bank. Test frames are compared with libvpx's MD5s, or with FFmpeg's software decode, which is bit-exact with libvpx.

  • libvpx VP9 conformance vectors, with FFmpeg PR 20847 v2 (decoding in place): on integration/aurora 70a00e583b03, 299 of 304 4:2:0 vectors match, up from 273 with media: apple: avd: VP9 temporal MVs and reference scale after a size change #3017 alone (11-24 row). The new matches are the 24 vp90-2-21-resize_inter vectors, 05-resize and 18-resize. No vector stopped matching, and no frame fails in the firmware. With FFmpeg's current code, which renegotiates on every size change, the result is unchanged (215).

  • No regressions: H.264, HEVC Main/Main10, VP9 and AV1 test clips stay bit-exact. The 36 resize-fp-tiles vectors match on both FFmpeg paths. 720p streams with segmentation that keep their size stay bit-exact.

  • Synthetic streams: the conformance vectors that change size on inter frames are all error resilient, so they never reach commits 1 and 3. To cover those paths I encoded streams with libvpx's internal scaling (VP8E_SET_SCALEMODE: full, half, full on inter frames), segmentation AQ and no error resilience. The encoder is below.

    size AQ / segmentation relaxed check only this series
    640x360 none firmware error 40/40
    640x360 cyclic refresh (temporal) firmware error 40/40
    640x360 variance, complexity (kept map) firmware error 40/40
    1280x720 cyclic refresh not run 40/40
    1280x720 variance, complexity not run 40/40 (26/40 with the reset on the first frame only)
    1920x1080 cyclic refresh not run 40/40
    1920x1080 variance, complexity not run 40/40 (28/40, 29/40 with the reset on the first frame only)
vp9-scalemode-enc.c (libvpx test stream encoder)
// SPDX-License-Identifier: MIT
/*
 * vp9-scalemode-enc: encode raw I420 frames to VP9 IVF with inter-frame size
 * changes (libvpx internal scaling, VP8E_SET_SCALEMODE), no error resilience,
 * one key frame, and the given AQ mode (1 variance, 2 complexity, 3 cyclic
 * refresh: all use segmentation). Frames [a, b) are coded at half size.
 *
 *   vp9-scalemode-enc in.yuv W H frames aq a b out.ivf
 *
 * Build: cc -O2 vp9-scalemode-enc.c -lvpx -o vp9-scalemode-enc
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vpx/vp8cx.h>
#include <vpx/vpx_encoder.h>

static void le16(unsigned char *p, unsigned v) { p[0] = v; p[1] = v >> 8; }
static void le32(unsigned char *p, unsigned v) { le16(p, v); le16(p + 2, v >> 16); }

int main(int argc, char **argv)
{
	if (argc != 9) {
		fprintf(stderr, "usage: %s in.yuv W H frames aq a b out.ivf\n", argv[0]);
		return 1;
	}
	unsigned w = atoi(argv[2]), h = atoi(argv[3]), n = atoi(argv[4]);
	int aq = atoi(argv[5]);
	unsigned a = atoi(argv[6]), b = atoi(argv[7]);
	FILE *in = fopen(argv[1], "rb"), *out = fopen(argv[8], "wb");
	vpx_codec_enc_cfg_t cfg;
	vpx_codec_ctx_t c;
	vpx_image_t img;
	unsigned char hdr[32] = "DKIF";
	unsigned count = 0;

	if (!in || !out || vpx_codec_enc_config_default(vpx_codec_vp9_cx(), &cfg, 0))
		return 1;
	cfg.g_w = w;
	cfg.g_h = h;
	cfg.g_timebase.num = 1;
	cfg.g_timebase.den = 30;
	cfg.rc_target_bitrate = 800;
	cfg.g_error_resilient = 0;
	cfg.g_lag_in_frames = 0;
	cfg.kf_mode = VPX_KF_DISABLED;
	cfg.rc_end_usage = VPX_VBR;
	if (vpx_codec_enc_init(&c, vpx_codec_vp9_cx(), &cfg, 0))
		return 1;
	vpx_codec_control(&c, VP9E_SET_AQ_MODE, aq);
	vpx_codec_control(&c, VP8E_SET_CPUUSED, 6);
	vpx_codec_control(&c, VP9E_SET_FRAME_PARALLEL_DECODING, 0);
	if (!vpx_img_alloc(&img, VPX_IMG_FMT_I420, w, h, 1))
		return 1;

	le16(hdr + 6, 32);
	memcpy(hdr + 8, "VP90", 4);
	le16(hdr + 12, w);
	le16(hdr + 14, h);
	le32(hdr + 16, 30);
	le32(hdr + 20, 1);
	fwrite(hdr, 1, 32, out);

	for (unsigned i = 0; i <= n; i++) {
		vpx_image_t *pic = NULL;
		if (i < n) {
			size_t sz = (size_t)w * h * 3 / 2;
			if (fread(img.img_data, 1, sz, in) != sz)
				return 1;
			pic = &img;
			if (i == a || i == b) {
				struct vpx_scaling_mode m = i == a ?
					(struct vpx_scaling_mode){ VP8E_ONETWO, VP8E_ONETWO } :
					(struct vpx_scaling_mode){ VP8E_NORMAL, VP8E_NORMAL };
				vpx_codec_control(&c, VP8E_SET_SCALEMODE, &m);
			}
		}
		if (vpx_codec_encode(&c, pic, i, 1, 0, VPX_DL_GOOD_QUALITY))
			return 1;
		const vpx_codec_cx_pkt_t *p;
		vpx_codec_iter_t it = NULL;
		while ((p = vpx_codec_get_cx_data(&c, &it))) {
			unsigned char fh[12];
			if (p->kind != VPX_CODEC_CX_FRAME_PKT)
				continue;
			le32(fh, p->data.frame.sz);
			le32(fh + 4, p->data.frame.pts);
			le32(fh + 8, 0);
			fwrite(fh, 1, 12, out);
			fwrite(p->data.frame.buf, 1, p->data.frame.sz, out);
			count++;
		}
	}
	le32(hdr + 24, count);
	fseek(out, 24, SEEK_SET);
	fwrite(hdr + 24, 1, 4, out);
	fclose(out);
	fprintf(stderr, "%u frames\n", count);
	return 0;
}

Usage: vp9-scalemode-enc in.yuv 640 360 40 <aq> 10 25 out.ivf codes frames 10 to 24 at half size.

  • Host tests: tests/vp9-test.py has a new case for each commit, and each case fails on the commit before it. tools/asahi/run-host-tests.py: 394/394. checkpatch is clean.

Known gaps

  • Only the legacy transport (one bank) ran on hardware. The two-bank handling (each bank cleared when next taken) is covered by the host test only.
  • The AV1 path packs its reference scale the same way (X in the upper half, AV1_REF_SCALE_X). I haven't changed or tested it.

cc @Acelogic @DjDeveloperr @RyanTheTide @LiamRay10

  • Host test after the rebase: tests/vp9-test.py passes. A 960x540 frame in a 1920x1088 format runs on the legacy transport and is refused on the firmware transport; a 1984x1088 one is refused on both. Both mutants fail it: "fits" on every transport, and exact size on every transport. The AVD host tests in run-host-tests.py pass (12/12), and the W=1 arm64 build of drivers/media/platform/apple/avd is clean.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown

No blockers.

  1. drivers/media/platform/apple/avd/avd-vp9.c:865-866: media: apple: avd: VP9: one segmentation map per stream, shared by the banks #2195 (open) replaces the per-bank seg buffer with one per stream (vp9_ctx->seg), because the queued transport otherwise reads the map of the frame before last. Whichever of the two lands second has to rebase this memset onto the shared buffer. On the queued transport the other bank's frame can still be in flight at prepare time: avd_vp9_run_preamble() takes any bank that is not busy (:846-853). A CPU memset of the shared map would then race that frame. With media: apple: avd: VP9: one segmentation map per stream, shared by the banks #2195 the clear must either wait until no frame is in flight or be left to VP9_SEG_RESET (:349-350). Rebasing on media: apple: avd: VP9: one segmentation map per stream, shared by the banks #2195 also closes your "clears only the bank being prepared" gap.
  2. The known 1280x720+ kept-map gap means that, with FFmpeg's in-place path, a stream the driver used to refuse now decodes into wrong pixels with no error. If the cause isn't found before merge, please say so in the commit message of patch 4 so it is not read as complete support.

I checked the rest. avd_vp9_same_size() compares the previous decoded frame. last is updated for every submitted frame on the queued path (:923) and on completion on the legacy path (:1088), which matches the spec's "previous time compute_image_size was invoked". The callers in make_flags1() and the preamble check last.valid first. The ref-scale swap matches AVD_HDR_HEIGHT/AVD_HDR_WIDTH as used for the reference size. The scratch buffers stay sized from the coded format, so a smaller frame fits. The commits carry a human Signed-off-by and no agent trailers.

HW row: a Mac on the queued two-bank transport (J815, or J813 with the open-queue backend), rebased on #2195: the 26 new resize vectors plus your synthetic aq-mode 1/2/3 streams at 640x360 and 1280x720 against libvpx MD5s, and 0 AVC, 0 failed units, 0 new kernel errors.

Assisted-by: LLM (Claude:Opus 5.5)

@sithglan
sithglan force-pushed the thomas/avd-vp9-smaller-frames branch from 1309f5c to b43dd04 Compare November 21, 2026 13:38
@sithglan

Copy link
Copy Markdown
Author

@RyanTheTide thanks.

  1. Shared seg buffer (media: apple: avd: VP9: one segmentation map per stream, shared by the banks #2195):
  2. 1280x720 gap: patch 4 (b43dd04645d5) now says in its commit message that this isn't complete support yet. At 1280x720 and up, a stream that keeps its segmentation map without coding it (update_map 0) still decodes wrong pixels, with no error, one or two frames after its size grows back, and before the patch the driver refused those frames. The cause isn't found yet.

HW row: after the rebase on #2195, on the J813 with the open-queue backend:

  • the 26 resize vectors and the synthetic aq-mode 1/2/3 streams at 640x360 and 1280x720, against the libvpx MD5s;
  • 0 AVC, 0 failed units and 0 new kernel errors.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown

Thanks for b43dd04. The commit message of patch 4 now states the 1280x720 kept-map gap. Thanks also for the plan for the #2195 memset, which settles point 1 as a merge-order item.

One new thing, from #2773, which touches the same lines of validate_dec_params():

  • media: apple: avd: decode VP9 frames smaller than 64x64 on the legacy transport #2773 keeps the old rule on the firmware transports (open-queue and the Apple backend). On those, an input the firmware does not expect can reset the engine instead of failing the frame, so the 64-pixel floor comes off only where avd_backend_is_open() is true (avd-vp9.c:681 at e08ff74). This PR relaxes the comparison to "fits" on every transport (avd-vp9.c:704). Per the description it was tested only on the legacy transport, and the description itself shows how such inputs can fail there ("open firmware error 0x8", a timeout and a decoder reset before patch 1). Until the open-queue row is in, please scope the relaxation the same way: > with avd_backend_is_open(), != otherwise. The scope can then come off in a small follow-up with that row.
  • Suggested order: media: apple: avd: decode VP9 frames smaller than 64x64 on the legacy transport #2773 first, since it is small and independent, then rebase this onto it and reuse its avd_backend_is_open(). avd-vp9.c merges cleanly into a consistent function: floor only on the legacy transport, aligned size raised to 64, larger frames refused. tests/vp9-test.py conflicts, though, because both add a block at the same place with a different decoded_fmt, so the order of the two blocks needs care when resolving.

HW row unchanged: the 26 resize vectors and the aq-mode streams on the open-queue transport, after the #2195 rebase. That row is also what lets the scope come off.

Assisted-by: LLM (Claude:Opus 5.5)

@sithglan

Copy link
Copy Markdown
Author

@RyanTheTide agreed on both points.

Assisted-by: LLM (Claude:Opus 5.5)

@sithglan
sithglan force-pushed the thomas/avd-vp9-smaller-frames branch from b43dd04 to ec360bc Compare November 22, 2026 19:04
@sithglan

Copy link
Copy Markdown
Author

Rebased onto #2773 as agreed. The head is now ec360bc, on integration/aurora f5fae3c.

The scope comes off in the follow-up with the open-queue row: the 26 resize vectors and the aq-mode streams, after #2195.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formal §1–§6 pre-PR review on ec360bce702f231bbafa504c594f3901547c16b7:

Commits 1 (c2d7aee81e46, temporal MV same-size check) and 2 (d3c0828a7f7b, swapping AVD_HDR_HEIGHT(yscale) | AVD_HDR_WIDTH(xscale) in set_refs()) are solid, well-tested spec fixes, and all 4 commits are §4-clean (human author/committer + Signed-off-by:, zero AI trailers in commits). CI host-tests passed on ec360bce702f.

Concrete blockers before merge:

  1. Code correctness — Two-bank segmentation state & VP9_SEG_RESET edge cases (7a4bc4f37fd5):

    • avd_vp9_run_preamble() unconditionally executes if (vp9_ctx->last.valid && !avd_vp9_same_size(dec_params, &vp9_ctx->last)) memset(vp9_ctx->b->bufs.seg.cpu, 0, vp9_ctx->b->bufs.seg.size); on all transports, including the two-bank queued firmware backend (avd_backend_is_qfw()). Even with Patch 4 gating smaller frames to avd_backend_is_open(), frames whose unaligned dimensions change within the same 64×16 aligned capture format (e.g., 1920×1080 → 1900×1080, noted in commit 1) already pass validate_dec_params() on the queued backend. Clearing only the active bank leaves the other bank's seg buffer stale with the prior size's map (and will race an in-flight bank once #2195 unifies seg per stream).
    • In make_flags1(), VP9_SEG_RESET is gated on frame->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_ENABLED on the size-changing frame itself. If frame N changes size with segmentation disabled and frame N+1 (now same size) enables segmentation with update_map = 0, VP9_SEG_RESET is never asserted.
    • §2 Production-ready: Patch 4 (ec360bce702f) knowingly turns currently rejected (-EINVAL) 1280×720+ update_map = 0 resize streams into silent pixel corruption (26/40 and 28–29/40 matching frames with no error). Either resolve the 720p+ kept-map corruption before enabling smaller frames, or split out the unconditional spec bugfixes (commits 1–2) so they can land cleanly without shipping silent decode corruption.
  2. Stale base — integration/aurora moved in avd-vp9.c and tests/vp9-test.py:

    • Since this PR's base (f5fae3cbb047), integration/aurora landed 966778e43840 (media: apple: avd: vp9: bound the tile counts) and 276447127c76 (media: apple: avd: build HEVC and VP9 jobs from copies of their controls), both touching avd-vp9.c and tests/vp9-test.py. Rebase onto current integration/aurora and re-verify vp9-test.py and the new apple-avd-vp9 KUnit suite.
  3. §5 — Live-hardware verification missing on exact live head & missing regression tuple:

    • Exact head ec360bce702f (with the #2773 rebase and avd_backend_is_open() scoping) has not been booted/tested on hardware; the PR body's results are from an older out-of-tree module build prior to the rebase.
    • Required §5 evidence fields are missing: exact tested head SHA, kernel/build version, SELinux mode, 0 AVC denials, 0 failed units, and 0 new kernel errors. Please post a fresh hardware run against the rebased live head with the full §5 tuple.

Assisted-by: LLM (Gemini 4 Neon)

… size

VP9 predicts motion vectors from the previous frame only if that frame
has the same size (UsePrevFrameMvs in compute_image_size(), libvpx's
use_prev_frame_mvs). avd_vp9_uses_prev_mv() does not compare sizes, so
after a size change the decoder reads the previous frame's MV buffer,
which is laid out for the other size. Frames of different sizes already
pass validate_dec_params() when they share the 64x16 aligned capture
format, e.g. 1920x1080 and 1900x1080.

Accepting frames smaller than the capture format, as
aurora-silicon#2136 does, makes this common: with that change
alone, every VP9 stream that changes size on an inter frame without
error resilience fails at the first frame of the new size with "open
firmware error 0x8", a timeout and a decoder reset. With this patch no
frame fails in the firmware.

Record the size of each frame and require the previous one to match.
The host test decodes a 1900x1080 frame after a 1920x1088 one and checks
that the prior bank's MV buffer is not used.

Signed-off-by: Thomas Glanzmann <thomas@glanzmann.de>
set_refs() packs the horizontal reference scale into the upper 16 bits
and the vertical one into the lower 16 bits. The decoder reads them the
other way round. A reference scaled the same in both directions (2:1,
4:3) decodes either way, so only references whose aspect ratio differs
from the frame's were wrong.

A frame whose size changes within its 64x16 aligned capture format
(1920x1080 to 1900x1080) already predicts from such a reference. Once
frames smaller than the capture format are accepted
(aurora-silicon#2136), libvpx's vp90-2-05-resize.ivf (352x288 to
282x173) and vp90-2-18-resize.ivf (310x230 to 248x138) do too, and
decode bit-exact against libvpx's MD5s: 10 of 10 and 100 of 100 frames,
up from 3 and 94.

Pack the vertical scale with AVD_HDR_HEIGHT() and the horizontal one with
AVD_HDR_WIDTH(), as the reference size above it is. The host test checks
the word for a 1440x1088 reference of a 1920x1088 frame.

Signed-off-by: Thomas Glanzmann <thomas@glanzmann.de>
@sithglan

sithglan commented Nov 23, 2026 •

Copy link
Copy Markdown
Author

@DjDeveloperr thanks. I split it as you suggested.

On the review points:

  1. Both points on commit 3 are right, and I'll fix them there:
    • Two banks. On the queued firmware transport, the size-change clear only reaches the active bank, so the other bank's seg buffer keeps the old map. A size change has to reset the map for every bank (and for the one per-stream buffer once media: apple: avd: VP9: one segmentation map per stream, shared by the banks #2195 lands), not only the bank that runs next.
    • A later enable. VP9_SEG_RESET is only set if the size-changing frame itself has segmentation enabled. The reset has to be latched at the size change and applied to the next frame that uses the map.
    • 1280x720+ kept map. Not solved yet; the commit message states the gap, and patch 4 waits for it.
  2. Rebase: done. The head is b176bbcf82bd on integration/aurora 70a00e583b03, and all four commits applied without conflicts: git range-diff shows them =, apart from the two reworded messages. At this head:
    • tests/vp9-test.py passes;
    • the AVD host tests in run-host-tests.py pass (12/12);
    • KUnit in arm64 QEMU passes (avd-pm.kunitconfig, 81/81, apple-avd-vp9 included).
  3. Hardware rows: media: apple: avd: VP9 temporal MVs and reference scale after a size change #3017 gets its J813 row on its exact head first. This PR gets one after the commit 3 rework, with the full tuple.

Assisted-by: LLM (Claude:Opus 5.5)

@sithglan
sithglan force-pushed the thomas/avd-vp9-smaller-frames branch from b176bbc to b9fee44 Compare November 23, 2026 22:20
@sithglan

Copy link
Copy Markdown
Author

@DjDeveloperr both points on commit 3 are fixed. The head is b9fee448865a; only commit 3 changed, and commit 4's patch is the same.

  1. Two banks. A size change now marks every bank's seg buffer (seg_clear). The active bank's buffer is cleared before its frame runs. A bank that is still busy with an earlier frame isn't touched, because its buffer belongs to the hardware until that frame retires; it is cleared when the bank is next taken. Nothing else clears a seg buffer.
  2. A later enable. The size change sets seg_reset on the stream. make_flags1() gives VP9_SEG_RESET to the first frame with segmentation enabled, and update_ctx_last_info() drops the latch once such a frame is committed. A frame of the new size without segmentation leaves the latch alone.

Host test (vp9-test.py, on the queued transport with two banks):

  • frame A at 1920x1080 keeps bank 0 busy;
  • frame B at 1900x1080 without segmentation gets bank 1: bank 1's buffer is cleared, bank 0's still holds its old contents but is marked, and the latch is set;
  • after both retire, frame C at 1900x1080 enables segmentation without a map update: make_flags1() sets VP9_SEG_RESET, and bank 0's buffer is cleared when C takes it. After C the latch is gone and the bit is no longer set.

Two mutants fail it:

  • clearing every bank at the size change, busy ones included, fails at the busy-bank check;
  • dropping the latch from make_flags1() fails at the VP9_SEG_RESET check.

Checks:

  • vp9-test.py passes at commit 3 and at the head.
  • The AVD host tests pass (12/12).
  • KUnit in arm64 QEMU (avd-pm.kunitconfig) passes 81/81.
  • The W=1 arm64 build of avd-vp9.o is clean, and checkpatch --strict is clean on commits 3 and 4.

Commit 3's message describes the new behaviour. Its hardware table still comes from the legacy transport, with one bank and segmentation on every frame, where the reset goes to the first frame of the new size as before.

The PR stays a draft for the 1280x720+ kept-map case, and the J813 row on this head follows with that fix.

Assisted-by: LLM (Claude:Opus 5.5)

… changes

VP9 clears the segmentation map when the frame size changes
(compute_image_size(); libvpx zeroes last_frame_seg_map). The driver sets
VP9_SEG_RESET only for key frames and after an intra frame without
segmentation, and the decoder keeps part of the map in the seg buffer
across frames. After a size change, a frame that predicts its segment IDs
from the previous frame or keeps them (update_map 0) used the old map,
laid out for the other size.

After a size change, set VP9_SEG_RESET for every frame that uses
segmentation until one updates the map. The decoder does not carry a
reset map forward: a frame that keeps the map (update_map 0) after one
with VP9_SEG_RESET reads the old map again, so the reset is repeated
until a frame has written a new map. Clear each bank's seg buffer before
the bank next runs: a bank still busy with an earlier frame belongs to
the hardware until it retires, so its buffer is cleared when it is next
taken.

Test streams from libvpx with internal scaling (VP8E_SET_SCALEMODE: full,
half, full size on inter frames) and segmentation AQ, no error
resilience, against libvpx-equivalent software decoding, with frames
smaller than the capture format accepted (later in this series):

  640x360    cyclic refresh (temporal map)    25/40 -> 40/40
             variance, complexity (kept map)  10/40, 25/40 -> 40/40
  1280x720   all three                        40/40
  1920x1080  all three                        40/40

With the reset on the first frame of the new size only, the kept-map
streams at 1280x720 and 1920x1080 went wrong from the second frame after
the size grew back (26/40, 26/40, 28/40, 29/40). These streams ran on the
legacy transport, which has one VP9 bank, and use segmentation on every
frame. The host test checks, with two banks, that only a size change
clears the buffers, that a busy bank's buffer waits until the bank is
taken, and that the reset stays until a frame updates the map.

Signed-off-by: Thomas Glanzmann <thomas@glanzmann.de>
VP9 inter frames can change the frame size and keep predicting from
references of the old size. validate_dec_params() refuses every frame
whose aligned size differs from the capture format, so userspace has to
reallocate the capture buffers and loses those references. FFmpeg's
V4L2 request VP9 hwaccel can instead keep the buffers and decode a
frame that fits in place (FFmpeg PR 20847, fix 5), and the decoder
already handles what this needs: each reference carries its own
size and scale (set_refs()), the frame is written with the format's
stride, and the scratch buffers are sized for the format.

On the legacy transport, accept frames whose aligned size fits the
capture format and keep refusing larger ones. The firmware transports
(open-queue and the Apple backend) keep requiring the format's size, as
they keep the 64-pixel floor: an input their firmware does not expect
can reset the engine instead of failing the frame, and this has not
been measured there yet. The host test runs a 960x540 frame in a
1920x1088 format on the legacy transport, refuses it on the firmware
transport, and refuses a 1984x1088 one on both.

On a J813 (legacy transport) with FFmpeg PR 20847 v2 (decoding in place)
and the earlier patches of this series, the libvpx VP9 conformance
vectors match libvpx's MD5s in 299 of 304 4:2:0 vectors, up from 273.
The 26 are the 24 vp90-2-21-resize_inter vectors, vp90-2-05-resize and
vp90-2-18-resize. No vector stopped matching, and no frame fails in the
firmware. Userspace that renegotiates the format on every size change,
such as FFmpeg's current code, decodes as before (273). H.264, HEVC, VP9
and AV1 test clips stay bit-exact.

Signed-off-by: Thomas Glanzmann <thomas@glanzmann.de>
@sithglan
sithglan force-pushed the thomas/avd-vp9-smaller-frames branch from b9fee44 to a8e9047 Compare November 23, 2026 23:55
@sithglan

Copy link
Copy Markdown
Author

@DjDeveloperr the 1280x720+ kept-map case is fixed, and here is the J813 row on the exact head a8e9047aac50.

The cause. After a size change the decoder needs VP9_SEG_RESET on every frame that keeps the map, not only the first:

  • Frame 25 of the 1280x720 test stream (the size grows back there) decoded right with the reset. Frame 26 keeps the map (update_map 0) and read the old map again.
  • Forcing the reset on N frames fixed exactly N frames. Holding it until the stream's next map update (frame 30) fixed the whole stream.
  • So the decoder doesn't carry a reset map forward on its own. Commit 3 now keeps the reset until a frame with segmentation updates the map (update_ctx_last_info()), which matches libvpx: the map stays zero until something writes it.
  • Ruled out on the way, on a debug kernel: clearing any other per-bank buffer at the size change (az_left, above_info, ip_above, lf_above, lf_left_info, lf_left, color, pipe_state), clearing the destination's comp area, and refusing temporal MVs. None changed the result.

Row (MacBook Air M5, J813, legacy transport; one-shot trial boot of 7.3.0-aurora-j813-fit2136+ = integration/aurora 70a00e583b03 + #3017 + this PR, through the enrolled aura, default command line):

#3017 alone (base) this PR
Synthetic scale-mode streams, 640x360 / 1280x720 / 1920x1080, AQ 0-3 (refused) all 40/40
Constant-size streams with kept maps (360p, 720p) — 40/40
libvpx VP9 vectors, FFmpeg PR 20847 v2 (in place) 273 / 31 / 10 299 / 5 / 10
libvpx VP9 vectors, renegotiating FFmpeg 273 / 31 / 10 273 / 31 / 10
Codec clips (H.264, HEVC, Main10, VP9, AV1, 4K60), both builds bit-exact bit-exact
  • The 26 new matches are 05-resize, 18-resize and the 24 resize_inter vectors. No vector stopped matching, in either build.
  • The 5 in-place failures fail the same way on the base: frames larger than the format (13-largescaling, two growing resize-fp-tiles), one SVC vector, and 10-bit (hwdownload).
  • Kernel: the in-place build logs no AVD errors at all. The renegotiating build has the base's lines on 05-resize and 18-resize (open firmware error 0x8, timeouts, the 46 s rest).
  • Counts: 0 DART faults, 0 failed units, 0 AVC denials (SELinux not enabled). The new kernel error lines against the default boot are only those five AVD lines.

Checks: vp9-test.py passes at commit 3 and at the head. Its new case is a frame that keeps the map after a reset frame, then one that updates it. The old rule (latch dropped at the first frame with segmentation) fails it. The AVD host tests pass 12/12, KUnit in arm64 QEMU 81/81, checkpatch --strict is clean on both commits, and W=1 is clean.

With this the PR leaves draft. It still sits on #3017.

Assisted-by: LLM (Claude:Opus 5.5)

@sithglan
sithglan marked this pull request as ready for review November 23, 2026 23:56

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formal §1–§6 pre-PR re-review on a8e9047aac5025ec881cbe436a76663dfbbbd46e (CI host-tests: SUCCESS; merges cleanly onto live integration/aurora a3d477d03f8e).

All prior blockers from ec360bce702f resolved

  1. Two-bank segmentation state & VP9_SEG_RESET latch (652a2495c5d3):
    • avd_vp9_run_preamble() now marks vp9_ctx->bank[i].seg_clear = true across all banks on a size change and zeroes vp9_ctx->b->bufs.seg.cpu only when a bank is next taken, never touching an in-flight busy bank's DMA buffer.
    • vp9_ctx->seg_reset is latched on any size change, applied in make_flags1() whenever V4L2_VP9_SEGMENTATION_FLAG_ENABLED is set, and cleared in update_ctx_last_info() only once a frame with both V4L2_VP9_SEGMENTATION_FLAG_ENABLED and V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP commits.
    • This also cleanly resolves the 1280×720+ kept-map (update_map = 0) pixel corruption root-caused on J813 hardware (the decoder does not persist a reset seg map across a subsequent kept-map frame).
    • drivers/media/platform/apple/avd/tests/vp9-test.py thoroughly exercises the two-bank busy-buffer preservation, delayed enable after a non-segmented resize, kept-map persistence of VP9_SEG_RESET, and latch clearing on UPDATE_MAP.
  2. Base & integration/aurora compatibility:
    • Rebased onto 70a00e583b03 (including 966778e43840 and 276447127c76), and checked against current integration/aurora a3d477d03f8e (#2946 avd_vp9_frame_fits() and 26fcdf5859dd): merges cleanly and stays semantically compatible.
  3. §5 Exact-head hardware evidence:
    • Exact-head J813 (M5 MacBook Air, legacy transport, enrolled Aura, default kernel command line, 7.3.0-aurora-j813-fit2136+) row verified: all 640×360 / 1280×720 / 1920×1080 AQ 0–3 scale-mode streams 40/40 bit-exact, constant-size kept-map streams 40/40, libvpx VP9 conformance 299/304 (+26 resize vectors with FFmpeg PR 20847 v2 in-place, 0 regressions, 0 AVD errors in dmesg), renegotiating FFmpeg unchanged (273/304), codec clips (H.264/HEVC/Main10/VP9/AV1/4K60) bit-exact, and full regression tuple (0 DART faults, 0 failed units, 0 AVC denials with SELinux disabled, 0 new kernel errors on in-place decode).

Checklist Audit (§1–§6)

  • §1 No model gating: Pass (avd_backend_is_open() reuses #2773's backend transport capability check; open-queue enablement is tracked for the follow-up after #2195).
  • §2 Production-ready: Pass (no new parameters or hacks; silent corruption resolved).
  • §3 Safety: Pass (bounds validated against decoded_fmt, non-zero frame dimensions in set_refs(), no CPU writes to in-flight bank DMA buffers).
  • §4 Clean-room & Attribution: Pass. All 4 commits (e04db3b4f021, 6fd057431b55, 652a2495c5d3, a8e9047aac50) carry human author/committer (Thomas Glanzmann <thomas@glanzmann.de>) and human Signed-off-by: only, with zero AI commit trailers; Assisted-by: is disclosed in the PR body.
  • §5 Evidence: Pass (exact-head CI host-tests green + exact-head J813 HW row and regression tuple).
  • §6 Naming: Pass.

Non-blocking notes

  • Commits 1–2 (e04db3b4f021..6fd057431b55) match open, already-approved #3017; MERGETRAIN6 can land #3017 first (or take the stack directly), and whichever of #2136 / #2195 lands second reconciles per-bank seg_clear with #2195's shared vp9_ctx->seg.
  • Minor pre-existing pattern: if avd_init_job() (or set_tiles() after #2946) fails after avd_vp9_run_preamble(), seg_clear/seg_reset have already been mutated for that rejected frame (same as bank->busy / frame_context in the preamble).

Verdict: APPROVE — 0 blockers; ready for MERGETRAIN6.

Assisted-by: LLM (Gemini 4 Neon)

@github-actions github-actions Bot added area:media Area from CODEOWNERS (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026
@github-actions github-actions Bot added the ready-to-merge Meets the merge policy: merge it (bot) label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:media Area from CODEOWNERS (bot) needs-template PR body lacks Tested or Risk (bot) ready-to-merge Meets the merge policy: merge it (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants