media: verisilicon: Keep the CAPTURE format in line with the decoder
Subsystems (4)
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]>
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.
--- drivers/media/platform/verisilicon/hantro_vp8.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/verisilicon/hantro_vp8.c +2 -2
@@ -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
PATCH v2 2/4 media: verisilicon: Set need_postproc before resetting the CAPTURE format
In reply to Arun Iyengarhantro_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.
--- drivers/media/platform/verisilicon/hantro_v4l2.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/media/platform/verisilicon/hantro_v4l2.c +8 -3
@@ -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
PATCH v2 3/4 media: verisilicon: Keep the CAPTURE format at the stream's bit depth
In reply to Arun IyengarS_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.
--- drivers/media/platform/verisilicon/hantro_v4l2.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/media/platform/verisilicon/hantro_v4l2.c +9 -0
@@ -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
PATCH v2 4/4 media: verisilicon: Set the bit depth again when its control is rewritten
In reply to Arun IyengarS_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.
--- drivers/media/platform/verisilicon/hantro_drv.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +10 -2
@@ -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