Skip to contentSkip to conversation

media: verisilicon: Keep the CAPTURE format in line with the decoder

PATCH New Arun Iyengar 1 participant 5 messages Nov 15

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover media: verisilicon: Keep the CAPTURE format in line with the decoder

Fixes for cases where the decoder writes past the CAPTURE format or an auxiliary buffer, found by Sashiko on hantro-dec-size-check v1 [1].

Tested on an Orange Pi 5 (RK3588S) with AV1: the CAPTURE format is right after film grain is turned off and for 10-bit streams; VP8 and AV1 fluster show no new failures. The overrun in patch 1 was not reproduced. HEVC and VP9 are untested (the RK3588 has no G2 core).

Developed with Claude (Anthropic), which wrote the code and the commit messages. I reviewed every change.

[1] https://lore.kernel.org/r/[email protected]

---
Changes in v2:
- 3/4: also replace a native format when post-processing; second Fixes tag (Sashiko)
- 4/4: check the control returned by v4l2_ctrl_new_custom() (Sashiko)
- Shorter commit messages, fewer code comments
- Link to v1: https://patch.msgid.link/[email protected]

---
Arun Iyengar (4):
media: verisilicon: Size the VP8 segment map for the largest frame
media: verisilicon: Set need_postproc before resetting the CAPTURE format
media: verisilicon: Keep the CAPTURE format at the stream's bit depth
media: verisilicon: Set the bit depth again when its control is rewritten

 drivers/media/platform/verisilicon/hantro_drv.c  | 12 ++++++++++--
 drivers/media/platform/verisilicon/hantro_v4l2.c | 20 +++++++++++++++++---
 drivers/media/platform/verisilicon/hantro_vp8.c  |  4 ++--
 3 files changed, 29 insertions(+), 7 deletions(-)

---
base-commit: ad6549a46748d00818483b535e5cd2a704685c98
change-id: 20261113-hantro-fixes-aace04d5268b

Best regards,

Signature · 1 line
Arun Iyengar <[email protected]>
#1 lore raw

PATCH v2 1/4 media: verisilicon: Size the VP8 segment map for the largest frame

The segment map is sized at STREAMON, but the CAPTURE format can grow while streaming and overrun it. Size it for the largest frame, as VP9 does.

Fixes bbd267daf4fc ("media: verisilicon: vp9: Allow to change resolution while streaming")
---
 drivers/media/platform/verisilicon/hantro_vp8.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/verisilicon/hantro_vp8.c +2 -2
index 381bc1d3bfda..98ad86269bc3 100644
@@ -151,8 +151,8 @@ int hantro_vp8_dec_init(struct hantro_ctx *ctx)
 	int ret;
 
 	/* segment map table size calculation */
-	mb_width = DIV_ROUND_UP(ctx->dst_fmt.width, 16);
-	mb_height = DIV_ROUND_UP(ctx->dst_fmt.height, 16);
+	mb_width = DIV_ROUND_UP(ctx->vpu_src_fmt->frmsize.max_width, 16);
+	mb_height = DIV_ROUND_UP(ctx->vpu_src_fmt->frmsize.max_height, 16);
 	segment_map_size = round_up(DIV_ROUND_UP(mb_width * mb_height, 4), 64);
 
 	/*
Signature · 1 line
2.55.0
#2 lore raw

PATCH v2 2/4 media: verisilicon: Set need_postproc before resetting the CAPTURE format

In reply to Arun Iyengar

hantro_reset_raw_fmt() sets bit_depth and need_postproc only after hantro_set_fmt_cap(), so hantro_try_fmt() sizes the format with the old
values: after AV1 film grain is turned off, the CAPTURE buffers lack the motion vector area. Set them first and restore them on error.

Fixes 80c7373a456e ("media: verisilicon: Conditionally ignore native formats")
---
 drivers/media/platform/verisilicon/hantro_v4l2.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/media/platform/verisilicon/hantro_v4l2.c +8 -3
index 83af9fa1ce94..5939fffc350d 100644
@@ -449,6 +449,8 @@ hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth, bool need_postproc)
 {
 	const struct hantro_fmt *raw_vpu_fmt;
 	struct v4l2_pix_format_mplane raw_fmt, *encoded_fmt;
+	bool old_need_postproc = ctx->need_postproc;
+	int old_bit_depth = ctx->bit_depth;
 	int ret;
 
 	raw_vpu_fmt = hantro_get_default_fmt(ctx, false, bit_depth, need_postproc);
@@ -465,14 +467,17 @@ hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth, bool need_postproc)
 	hantro_reset_fmt(&raw_fmt, raw_vpu_fmt);
 	raw_fmt.width = encoded_fmt->width;
 	raw_fmt.height = encoded_fmt->height;
+
+	ctx->bit_depth = bit_depth;
+	ctx->need_postproc = need_postproc;
 	if (ctx->is_encoder)
 		ret = hantro_set_fmt_out(ctx, &raw_fmt, need_postproc);
 	else
 		ret = hantro_set_fmt_cap(ctx, &raw_fmt);
 
-	if (!ret) {
-		ctx->bit_depth = bit_depth;
-		ctx->need_postproc = need_postproc;
+	if (ret) {
+		ctx->bit_depth = old_bit_depth;
+		ctx->need_postproc = old_need_postproc;
 	}
 
 	return ret;
Signature · 1 line
2.55.0
#3 lore raw

PATCH v2 3/4 media: verisilicon: Keep the CAPTURE format at the stream's bit depth

In reply to Arun Iyengar

S_FMT and TRY_FMT on the decoder CAPTURE queue accept formats ENUM_FMT
hides: an 8-bit format for a 10-bit stream, which the decoder overruns, or a native format while the post-processor is needed. Replace such a format with the stream's default one.

Fixes 8d03c2ab881b ("media: hantro: Support format filtering by depth")
Fixes 80c7373a456e ("media: verisilicon: Conditionally ignore native formats")
---
 drivers/media/platform/verisilicon/hantro_v4l2.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/media/platform/verisilicon/hantro_v4l2.c +9 -0
index 5939fffc350d..450775664742 100644
@@ -342,6 +342,15 @@ static int hantro_try_fmt(const struct hantro_ctx *ctx,
 		pix_mp->pixelformat = fmt->fourcc;
 	}
 
+	if (!coded && !ctx->is_encoder &&
+	    (!hantro_check_depth_match(fmt, ctx->bit_depth) ||
+	     (ctx->need_postproc && !fmt->postprocessed))) {
+		fmt = hantro_get_default_fmt(ctx, false, ctx->bit_depth, ctx->need_postproc);
+		if (!fmt)
+			return -EINVAL;
+		pix_mp->pixelformat = fmt->fourcc;
+	}
+
 	if (coded) {
 		pix_mp->num_planes = 1;
 		vpu_fmt = fmt;
Signature · 1 line
2.55.0
#4 lore raw

PATCH v2 4/4 media: verisilicon: Set the bit depth again when its control is rewritten

In reply to Arun Iyengar

S_FMT on OUTPUT resets the CAPTURE format to 8 bits, but rewriting an unchanged 10-bit HEVC SPS, VP9 frame or AV1 sequence control skips s_ctrl, so the format stays 8-bit. Mark these controls EXECUTE_ON_WRITE; every write of them then also sends a V4L2_EVENT_CTRL value change.

Fixes 3c32d94c9c52 ("media: verisilicon: Do not change context bit depth before validating the format")
---
 drivers/media/platform/verisilicon/hantro_drv.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +10 -2
index 26e5d5547466..248fb0e23a29 100644
@@ -534,6 +534,7 @@ static const struct hantro_ctrl controls[] = {
 		.cfg = {
 			.id = V4L2_CID_STATELESS_HEVC_SPS,
 			.ops = &hantro_hevc_ctrl_ops,
+			.flags = V4L2_CTRL_FLAG_EXECUTE_ON_WRITE,
 		},
 	}, {
 		.codec = HANTRO_HEVC_DECODER,
@@ -555,6 +556,7 @@ static const struct hantro_ctrl controls[] = {
 		.cfg = {
 			.id = V4L2_CID_STATELESS_VP9_FRAME,
 			.ops = &hantro_vp9_ctrl_ops,
+			.flags = V4L2_CTRL_FLAG_EXECUTE_ON_WRITE,
 		},
 	}, {
 		.codec = HANTRO_VP9_DECODER,
@@ -577,6 +579,7 @@ static const struct hantro_ctrl controls[] = {
 		.cfg = {
 			.id = V4L2_CID_STATELESS_AV1_SEQUENCE,
 			.ops = &hantro_av1_ctrl_ops,
+			.flags = V4L2_CTRL_FLAG_EXECUTE_ON_WRITE,
 		},
 	}, {
 		.codec = HANTRO_AV1_DECODER,
@@ -591,6 +594,7 @@ static int hantro_ctrls_setup(struct hantro_dev *vpu,
 			      int allowed_codecs)
 {
 	int i, num_ctrls = ARRAY_SIZE(controls);
+	struct v4l2_ctrl *ctrl;
 
 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, num_ctrls);
 
@@ -598,8 +602,8 @@ static int hantro_ctrls_setup(struct hantro_dev *vpu,
 		if (!(allowed_codecs & controls[i].codec))
 			continue;
 
-		v4l2_ctrl_new_custom(&ctx->ctrl_handler,
-				     &controls[i].cfg, NULL);
+		ctrl = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
+					    &controls[i].cfg, NULL);
 		if (ctx->ctrl_handler.error) {
 			vpu_err("Adding control (%d) failed %d\n",
 				controls[i].cfg.id,
@@ -607,6 +611,10 @@ static int hantro_ctrls_setup(struct hantro_dev *vpu,
 			v4l2_ctrl_handler_free(&ctx->ctrl_handler);
 			return ctx->ctrl_handler.error;
 		}
+
+		/* v4l2_ctrl_new_custom() drops cfg.flags of standard controls. */
+		if (ctrl)
+			ctrl->flags |= controls[i].cfg.flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
 	}
 	return v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
 }
Signature · 1 line
2.55.0