diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index a596b420ac2c7..6d46ce123b2a0 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; @@ -260,20 +262,32 @@ 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); } } -/* 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,11 +341,13 @@ 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 && (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) && @@ -425,7 +441,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 +583,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; @@ -668,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 @@ -691,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); @@ -851,6 +876,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 083c06e095a1f..1ceff2198ba79 100644 --- a/drivers/media/platform/apple/avd/tests/vp9-test.py +++ b/drivers/media/platform/apple/avd/tests/vp9-test.py @@ -209,6 +209,40 @@ 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); + /* 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;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 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; @@ -229,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);