From c2d7aee81e46b3de573754df409d3bd662309ae9 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Tue, 17 Nov 2026 08:35:10 +0100 Subject: [PATCH 1/4] media: apple: avd: use VP9 temporal MVs only from a frame of the same 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 (later in this series) 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 --- drivers/media/platform/apple/avd/avd-vp9.c | 30 ++++++++++++++----- .../platform/apple/avd/tests/vp9-test.py | 12 ++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index a596b420ac2c7..9ff74ca6c1b20 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -159,6 +159,8 @@ struct avd_vp9_frame_info { u32 tx_mode : 3; u32 interpolation_filter : 3; u32 flags; + u32 width; + u32 height; u64 timestamp; dma_addr_t color; struct v4l2_vp9_segmentation seg; @@ -267,13 +269,25 @@ static void set_refs(struct avd_ctx *ctx, struct avd_vp9_run *run) } } -/* Temporal MV input is the previous shown inter picture, not the new bank. */ -static bool avd_vp9_uses_prev_mv(u32 flags, const struct avd_vp9_frame_info *last) +static bool avd_vp9_same_size(const struct v4l2_ctrl_vp9_frame *frame, + const struct avd_vp9_frame_info *last) { - return !(flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT) && - !(flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME) && last->valid && + return last->width == frame->frame_width_minus_1 + 1 && + last->height == frame->frame_height_minus_1 + 1; +} + +/* + * Temporal MV input is the previous shown inter picture, not the new bank. + * VP9 uses it only if that picture has the same frame size. + */ +static bool avd_vp9_uses_prev_mv(const struct v4l2_ctrl_vp9_frame *frame, + const struct avd_vp9_frame_info *last) +{ + return !(frame->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT) && + !(frame->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME) && last->valid && (last->flags & V4L2_VP9_FRAME_FLAG_SHOW_FRAME) && - !(last->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME); + !(last->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME) && + avd_vp9_same_size(frame, last); } static u32 make_flags1(struct avd_ctx *ctx, struct avd_vp9_run *run) @@ -327,7 +341,7 @@ static u32 make_flags1(struct avd_ctx *ctx, struct avd_vp9_run *run) flags |= VP9_SEG_ENABLED(frame->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_ENABLED); - flags |= VP9_HAS_REF(avd_vp9_uses_prev_mv(frame->flags, &vp9_ctx->last)); + flags |= VP9_HAS_REF(avd_vp9_uses_prev_mv(frame, &vp9_ctx->last)); flags |= VP9_SEG_RESET( frame->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_ENABLED && @@ -425,7 +439,7 @@ static void set_header(struct avd_ctx *ctx, struct avd_vp9_run *run) pusha(vp9_ctx->b->bufs.seg.addr, "seg", 1); pusha(vp9_ctx->b->bufs.seg.addr, "seg", 2); pusha(vp9_ctx->b->bufs.color.addr, "color", 3); - pusha(avd_vp9_uses_prev_mv(frame->flags, &vp9_ctx->last) ? + pusha(avd_vp9_uses_prev_mv(frame, &vp9_ctx->last) ? vp9_ctx->last.color : vp9_ctx->b->bufs.color.addr, "color", 4); @@ -567,6 +581,8 @@ static void update_ctx_cur_info(struct avd_vp9_ctx *vp9_ctx, vp9_ctx->b->cur.reference_mode = dec_params->reference_mode; vp9_ctx->b->cur.interpolation_filter = dec_params->interpolation_filter; vp9_ctx->b->cur.flags = dec_params->flags; + vp9_ctx->b->cur.width = dec_params->frame_width_minus_1 + 1; + vp9_ctx->b->cur.height = dec_params->frame_height_minus_1 + 1; vp9_ctx->b->cur.timestamp = buf->base.vb.vb2_buf.timestamp; vp9_ctx->b->cur.color = vp9_ctx->b->bufs.color.addr; vp9_ctx->b->cur.seg = dec_params->seg; diff --git a/drivers/media/platform/apple/avd/tests/vp9-test.py b/drivers/media/platform/apple/avd/tests/vp9-test.py index 083c06e095a1f..c66edff751f02 100644 --- a/drivers/media/platform/apple/avd/tests/vp9-test.py +++ b/drivers/media/platform/apple/avd/tests/vp9-test.py @@ -209,6 +209,18 @@ def without_includes(s): assert(p->bank[0].busy&&p->bank[1].busy); avd_vp9_done(&ctx,&src,&dst[0].base.vb,VB2_BUF_STATE_DONE);avd_vp9_done(&ctx,&src,&dst[1].base.vb,VB2_BUF_STATE_DONE); } + /* After a frame size change the temporal-MV input is not the prior bank. */ + params=(struct v4l2_ctrl_vp9_frame){.flags=V4L2_VP9_FRAME_FLAG_SHOW_FRAME, + .frame_width_minus_1=1919,.frame_height_minus_1=1087,.bit_depth=8}; + current_dst=&dst[0].base.vb;frame.idx=0;assert(!avd_vp9_run(&ctx)); + struct avd_vp9_bank *shown=p->b; + params.frame_width_minus_1=1899;params.frame_height_minus_1=1079; + current_dst=&dst[1].base.vb;frame.idx=1;assert(!avd_vp9_run(&ctx)); + assert(p->b!=shown&&p->last.width==1900&&p->last.height==1080); + {bool prior_color=false;u32 *words=program.cpu; + for(size_t j=0;jbufs.color.addr>>8))prior_color=true; + assert(!prior_color);} + avd_vp9_done(&ctx,&src,&dst[0].base.vb,VB2_BUF_STATE_DONE);avd_vp9_done(&ctx,&src,&dst[1].base.vb,VB2_BUF_STATE_DONE); /* The firmware transport keeps the 64-pixel floor. */ ctx.decoded_fmt.fmt.pix_mp.width=64;ctx.decoded_fmt.fmt.pix_mp.height=64; ctx.decoded_fmt.fmt.pix_mp.plane_fmt[0].bytesperline=64; From d3c0828a7f7be0a57136185088701203a2126b25 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Tue, 17 Nov 2026 08:35:33 +0100 Subject: [PATCH 2/4] media: apple: avd: put the VP9 reference scale in the right halves 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. libvpx's vp90-2-05-resize.ivf (352x288 to 282x173) and vp90-2-18-resize.ivf (310x230 to 248x138) predict from such references. With frames smaller than the capture format accepted (later in this series), they now 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 --- drivers/media/platform/apple/avd/avd-vp9.c | 2 +- drivers/media/platform/apple/avd/tests/vp9-test.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index 9ff74ca6c1b20..9e7bd6b36282f 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -262,7 +262,7 @@ static void set_refs(struct avd_ctx *ctx, struct avd_vp9_run *run) dst->vp9.width; yscale = (ref_buf[i]->vp9.height << VP9_REF_SCALE_SHIFT) / dst->vp9.height; - push(AVD_HDR_HEIGHT(xscale) | AVD_HDR_WIDTH(yscale), + push(AVD_HDR_HEIGHT(yscale) | AVD_HDR_WIDTH(xscale), "ref_scale"); push_comp(ctx, addr, ref_buf[i]->comp.offsets); diff --git a/drivers/media/platform/apple/avd/tests/vp9-test.py b/drivers/media/platform/apple/avd/tests/vp9-test.py index c66edff751f02..0a1d2c36a9d13 100644 --- a/drivers/media/platform/apple/avd/tests/vp9-test.py +++ b/drivers/media/platform/apple/avd/tests/vp9-test.py @@ -221,6 +221,13 @@ def without_includes(s): for(size_t j=0;jbufs.color.addr>>8))prior_color=true; assert(!prior_color);} avd_vp9_done(&ctx,&src,&dst[0].base.vb,VB2_BUF_STATE_DONE);avd_vp9_done(&ctx,&src,&dst[1].base.vb,VB2_BUF_STATE_DONE); + /* A 1440x1088 reference of a 1920x1088 frame: vertical scale high, horizontal low. */ + params.frame_width_minus_1=1919;params.frame_height_minus_1=1087;reference.vp9.width=1440; + current_dst=&dst[0].base.vb;frame.idx=0;assert(!avd_vp9_run(&ctx)); + {bool scale=false;u32 *words=program.cpu; + for(size_t j=0;j Date: Tue, 17 Nov 2026 08:36:13 +0100 Subject: [PATCH 3/4] media: apple: avd: reset the VP9 segmentation map when the frame size 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. Set VP9_SEG_RESET for the first frame of a new size, and clear the bank's seg buffer before it runs. 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 cyclic refresh 40/40 variance, complexity 26/40 1920x1080 cyclic refresh 40/40 variance, complexity 28/40, 29/40 At 1280x720 and up, a kept map still goes wrong one or two frames after the size grows back; where the decoder keeps the rest of the map is not known. Tested with the legacy transport, which has one VP9 bank. The host test checks that only a frame size change clears the seg buffer. Signed-off-by: Thomas Glanzmann --- drivers/media/platform/apple/avd/avd-vp9.c | 9 +++++++++ drivers/media/platform/apple/avd/tests/vp9-test.py | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index 9e7bd6b36282f..46d35a67eb372 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -346,6 +346,8 @@ static u32 make_flags1(struct avd_ctx *ctx, struct avd_vp9_run *run) flags |= VP9_SEG_RESET( frame->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_ENABLED && (frame->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME || + (vp9_ctx->last.valid && + !avd_vp9_same_size(frame, &vp9_ctx->last)) || (vp9_ctx->last.valid && !(vp9_ctx->last.seg.flags & V4L2_VP9_SEGMENTATION_FLAG_ENABLED) && @@ -867,6 +869,13 @@ static int avd_vp9_run_preamble(struct avd_ctx *ctx, struct avd_vp9_run *run) bank - vp9_ctx->bank + 1; } + /* + * VP9 clears the segmentation map when the frame size changes, and the + * decoder carries part of it over in the seg buffer. + */ + 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); + vp9_ctx->b->cur.tx_mode = run->prob_updates->tx_mode; fctx_idx = v4l2_vp9_reset_frame_ctx(dec_params, vp9_ctx->frame_context); diff --git a/drivers/media/platform/apple/avd/tests/vp9-test.py b/drivers/media/platform/apple/avd/tests/vp9-test.py index 0a1d2c36a9d13..ee8ba8aa41368 100644 --- a/drivers/media/platform/apple/avd/tests/vp9-test.py +++ b/drivers/media/platform/apple/avd/tests/vp9-test.py @@ -228,6 +228,16 @@ def without_includes(s): for(size_t j=0;jnbanks;i++)memset(p->bank[i].bufs.seg.cpu,0xa5,p->bank[i].bufs.seg.size); + params.seg.flags=V4L2_VP9_SEGMENTATION_FLAG_ENABLED; + current_dst=&dst[0].base.vb;frame.idx=0;assert(!avd_vp9_run(&ctx)); + assert(((u8 *)p->b->bufs.seg.cpu)[0]==0xa5); + avd_vp9_done(&ctx,&src,&dst[0].base.vb,VB2_BUF_STATE_DONE); + params.frame_width_minus_1=1899;params.frame_height_minus_1=1079; + current_dst=&dst[1].base.vb;frame.idx=1;assert(!avd_vp9_run(&ctx)); + {u8 *seg=p->b->bufs.seg.cpu;for(size_t j=0;jb->bufs.seg.size;j++)assert(!seg[j]);} + avd_vp9_done(&ctx,&src,&dst[1].base.vb,VB2_BUF_STATE_DONE);params.seg.flags=0; /* The firmware transport keeps the 64-pixel floor. */ ctx.decoded_fmt.fmt.pix_mp.width=64;ctx.decoded_fmt.fmt.pix_mp.height=64; ctx.decoded_fmt.fmt.pix_mp.plane_fmt[0].bytesperline=64; From ec360bce702f231bbafa504c594f3901547c16b7 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Tue, 17 Nov 2026 08:41:32 +0100 Subject: [PATCH 4/4] media: apple: avd: decode VP9 frames smaller than the capture format 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 241 of 304 4:2:0 vectors, up from 215. 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 (215). H.264, HEVC, VP9 and AV1 test clips stay bit-exact. This is not 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. Before this patch the driver refused those frames. Signed-off-by: Thomas Glanzmann --- drivers/media/platform/apple/avd/avd-vp9.c | 17 ++++++++++++----- .../media/platform/apple/avd/tests/vp9-test.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index 46d35a67eb372..6d46ce123b2a0 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -686,7 +686,7 @@ static void init_probs(struct avd_ctx *ctx, const struct avd_vp9_run *run) static int validate_dec_params(struct avd_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params) { - unsigned int aligned_width, aligned_height; + unsigned int aligned_width, aligned_height, width, height; /* * The CAPTURE format is at least 64x64, the minimum of the VP9 frame @@ -709,11 +709,18 @@ static int validate_dec_params(struct avd_ctx *ctx, 64); /* - * Userspace should update the capture/decoded format when the - * resolution changes. + * A larger frame needs a new capture format. On the legacy transport + * a smaller one is decoded into the current buffers with their + * stride: VP9 inter frames can change size and keep predicting from + * references of the old size, which set_refs() scales, so userspace + * may keep the buffers. The firmware transports keep requiring the + * format's size until that is measured there, as for the floor above. */ - if (aligned_width != ctx->decoded_fmt.fmt.pix_mp.width || - aligned_height != ctx->decoded_fmt.fmt.pix_mp.height) { + width = ctx->decoded_fmt.fmt.pix_mp.width; + height = ctx->decoded_fmt.fmt.pix_mp.height; + if (avd_backend_is_open(ctx->dev) ? + (aligned_width > width || aligned_height > height) : + (aligned_width != width || aligned_height != height)) { dev_err(ctx->dev->dev, "unexpected bitstream resolution %dx%d\n", aligned_width, aligned_height); diff --git a/drivers/media/platform/apple/avd/tests/vp9-test.py b/drivers/media/platform/apple/avd/tests/vp9-test.py index ee8ba8aa41368..1ceff2198ba79 100644 --- a/drivers/media/platform/apple/avd/tests/vp9-test.py +++ b/drivers/media/platform/apple/avd/tests/vp9-test.py @@ -238,6 +238,11 @@ def without_includes(s): current_dst=&dst[1].base.vb;frame.idx=1;assert(!avd_vp9_run(&ctx)); {u8 *seg=p->b->bufs.seg.cpu;for(size_t j=0;jb->bufs.seg.size;j++)assert(!seg[j]);} avd_vp9_done(&ctx,&src,&dst[1].base.vb,VB2_BUF_STATE_DONE);params.seg.flags=0; + /* The firmware transport still needs the format's size: smaller and larger frames are refused. */ + params.frame_width_minus_1=959;params.frame_height_minus_1=539; + current_dst=&dst[0].base.vb;frame.idx=0;assert(avd_vp9_run(&ctx)==-EINVAL); + params.frame_width_minus_1=1983;params.frame_height_minus_1=1087; + current_dst=&dst[1].base.vb;frame.idx=1;assert(avd_vp9_run(&ctx)==-EINVAL); /* The firmware transport keeps the 64-pixel floor. */ ctx.decoded_fmt.fmt.pix_mp.width=64;ctx.decoded_fmt.fmt.pix_mp.height=64; ctx.decoded_fmt.fmt.pix_mp.plane_fmt[0].bytesperline=64; @@ -258,6 +263,18 @@ def without_includes(s): params.frame_width_minus_1=1919;params.frame_height_minus_1=1087; assert(avd_vp9_run(&ctx)==-EINVAL); avd_vp9_stop(&ctx); + /* On the legacy transport a frame smaller than the capture format runs in it, a larger one is refused. */ + ctx.decoded_fmt.fmt.pix_mp.width=1920;ctx.decoded_fmt.fmt.pix_mp.height=1088; + ctx.decoded_fmt.fmt.pix_mp.plane_fmt[0].bytesperline=1920; + dev.queued=false;vp_feature=false;assert(!avd_vp9_start(&ctx));p=ctx.priv; + params=(struct v4l2_ctrl_vp9_frame){.flags=V4L2_VP9_FRAME_FLAG_KEY_FRAME|V4L2_VP9_FRAME_FLAG_SHOW_FRAME, + .frame_width_minus_1=959,.frame_height_minus_1=539,.bit_depth=8}; + current_dst=&dst[0].base.vb;frame.idx=0;assert(!avd_vp9_run(&ctx)); + assert(dst[0].vp9.width==960&&dst[0].vp9.height==540); + avd_vp9_done(&ctx,&src,&dst[0].base.vb,VB2_BUF_STATE_DONE); + params.frame_width_minus_1=1983;params.frame_height_minus_1=1087; + current_dst=&dst[0].base.vb;frame.idx=0;assert(avd_vp9_run(&ctx)==-EINVAL); + avd_vp9_stop(&ctx); dev.queued=true;vp_feature=true;fail_alloc_after=13;allocating_ctx=&ctx; assert(avd_vp9_start(&ctx)==-ENOMEM);allocating_ctx=NULL; assert(!ctx.priv&&!avd_vp9_ready(&ctx)&&ctx.max_frames==1);