From a47cddbc9a100f39b76ece00d51944b6da87500e Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 18 Nov 2026 16:06:36 +0700 Subject: [PATCH] media: apple: avd: VP9: one segmentation map per stream, shared by the banks The segmentation map is state that runs from frame to frame: a frame with temporal map prediction, or one that keeps the map without updating it, reads the map the frame before it left. With the queued transport (two banks, alternating) each bank had its own seg buffer, so a frame read the map of the frame before last. Every stream with segmentation then decoded wrong from its first non-key frame on. Keep one seg buffer per stream and point both banks at it. Tested on J815 (MacBook Air 15" M5), frame-by-frame MD5 against libavcodec, full decode and a seek into the stream: - before: libvpx aq-mode 2 (complexity) and 3 (cyclic refresh) at 720p and 1080p, and a 360p screen-content clip, wrong from frame 1-3; - after: all of them bit-exact, with the rest of a 67-stream H.264, HEVC, VP9 and AV1 set (59 of 59 PASS; the other 8 are 10-bit and interlaced streams the driver does not expose). The single-bank transport also decoded them right, which pointed at the bank swap. Signed-off-by: Nick Co-Authored-By: Claude Opus 5.5 Signed-off-by: Nick --- drivers/media/platform/apple/avd/avd-vp9.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index bf2d8b80854e6..3c27182bbb813 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -189,7 +189,6 @@ struct avd_vp9_bank { struct avd_buf lf_left_info; struct avd_buf lf_left; struct avd_buf color; - struct avd_buf seg; struct avd_buf pipe_state; struct avd_buf counts; struct avd_buf probs; @@ -205,6 +204,13 @@ struct avd_vp9_ctx { unsigned int nbanks; struct avd_vp9_bank bank[AVD_VP9_BANKS]; struct avd_vp9_bank *b; + /* + * The segmentation map, one per stream. A frame reads the map the frame + * before it left (temporal prediction, or a frame without a map update), + * so the two banks of the queued transport must share it: with one + * buffer per bank, a frame read the map of the frame before last. + */ + struct avd_buf seg; }; static bool avd_vp9_needs_counts(u32 flags) @@ -422,8 +428,8 @@ static void set_header(struct avd_ctx *ctx, struct avd_vp9_run *run) pusha(vp9_ctx->b->bufs.counts.addr, "counts", 0); pusha(vp9_ctx->b->bufs.probs.addr, "probs", 0); pusha(vp9_ctx->b->bufs.above_info.addr, "above_info", 0); - pusha(vp9_ctx->b->bufs.seg.addr, "seg", 1); - pusha(vp9_ctx->b->bufs.seg.addr, "seg", 2); + pusha(vp9_ctx->seg.addr, "seg", 1); + pusha(vp9_ctx->seg.addr, "seg", 2); pusha(vp9_ctx->b->bufs.color.addr, "color", 3); pusha(avd_vp9_uses_prev_mv(frame->flags, &vp9_ctx->last) ? vp9_ctx->last.color : vp9_ctx->b->bufs.color.addr, @@ -738,10 +744,6 @@ static int avd_vp9_alloc_bufs(struct avd_ctx *ctx, struct avd_vp9_bank *bank) if (ret) return ret; - ret = avd_buf_alloc(avd, &bank->bufs.seg, DIV_ROUND_UP(w, 8) * 24); - if (ret) - return ret; - ret = avd_buf_alloc(avd, &bank->bufs.lf_left, DIV_ROUND_UP(h, 8) * 16 * bit_depth); if (ret) @@ -1119,6 +1121,7 @@ static void avd_vp9_free(struct avd_ctx *ctx, struct avd_vp9_ctx *vp9_ctx) struct avd_dev *avd = ctx->dev; ctx->max_frames = 1; + avd_buf_free(avd, &vp9_ctx->seg); for (unsigned int i = 0; i < vp9_ctx->nbanks; i++) { struct avd_vp9_bank *bank = &vp9_ctx->bank[i]; @@ -1126,7 +1129,6 @@ static void avd_vp9_free(struct avd_ctx *ctx, struct avd_vp9_ctx *vp9_ctx) avd_buf_free(avd, &bank->bufs.inst); avd_buf_free(avd, &bank->bufs.probs); avd_buf_free(avd, &bank->bufs.counts); - avd_buf_free(avd, &bank->bufs.seg); avd_buf_free(avd, &bank->bufs.color); avd_buf_free(avd, &bank->bufs.ip_above); avd_buf_free(avd, &bank->bufs.lf_above); @@ -1157,6 +1159,9 @@ static int avd_vp9_start(struct avd_ctx *ctx) vp9_ctx->nbanks = avd_qfw_has_vp_done(ctx->dev) ? AVD_VP9_BANKS : 1; vp9_ctx->b = &vp9_ctx->bank[0]; + ret = avd_buf_alloc(ctx->dev, &vp9_ctx->seg, DIV_ROUND_UP(fmt_width(ctx), 8) * 24); + if (ret) + goto err_free_ctx; for (unsigned int i = 0; i < vp9_ctx->nbanks; i++) { ret = avd_vp9_alloc_bufs(ctx, &vp9_ctx->bank[i]); if (ret)