mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RFC] media: verisilicon: hevc: Find the reference buffers by timestamp
@ 2026-11-29 19:32 Arun Iyengar via B4 Relay
  0 siblings, 0 replies; only message in thread
From: Arun Iyengar via B4 Relay @ 2026-11-29 19:32 UTC (permalink / raw)
  To: Nicolas Dufresne, Benjamin Gaignard, Philipp Zabel,
	Mauro Carvalho Chehab, Ezequiel Garcia, Hans Verkuil
  Cc: linux-media, linux-rockchip, linux-kernel, Arun Iyengar

From: Arun Iyengar <aiyengar1990@gmail.com>

The G2 HEVC decoder keeps a table of CAPTURE buffer addresses keyed
by POC. Entries that left the DPB stay in the table and still match a
POC, so a DPB entry can get the address of a buffer that was requeued
or removed with VIDIOC_REMOVE_BUFS, and the hardware then reads freed
memory.

Look the references up by their DPB timestamp instead, like the other
codecs do, and drop the table.

Fixes: cb5dd5a0fa51 ("media: hantro: Introduce G2/HEVC decoder")
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
Found by code reading. The G2 HEVC decoder (i.MX8MQ, i.MX8MM, i.MX8MP)
can reach this through the DPB controls: an entry whose picture was
not decoded through the table matches a stale slot. RFC because it is compile-tested only (arm64
allmodconfig, W=1); I have no i.MX8M board.

Developed with Claude (Anthropic), which wrote the code and the commit messages. I reviewed every change.
---
 .../platform/verisilicon/hantro_g2_hevc_dec.c      |  8 +---
 drivers/media/platform/verisilicon/hantro_hevc.c   | 44 ----------------------
 drivers/media/platform/verisilicon/hantro_hw.h     | 11 ------
 3 files changed, 1 insertion(+), 62 deletions(-)

diff --git a/drivers/media/platform/verisilicon/hantro_g2_hevc_dec.c b/drivers/media/platform/verisilicon/hantro_g2_hevc_dec.c
index e7a7c7a42467..c988dd579b2f 100644
--- a/drivers/media/platform/verisilicon/hantro_g2_hevc_dec.c
+++ b/drivers/media/platform/verisilicon/hantro_g2_hevc_dec.c
@@ -448,14 +448,11 @@ static int set_ref(struct hantro_ctx *ctx)
 
 	set_ref_pic_list(ctx);
 
-	/* We will only keep the reference pictures that are still used */
-	hantro_hevc_ref_init(ctx);
-
 	/* Set up addresses of DPB buffers */
 	dpb_longterm_e = 0;
 	for (i = 0; i < decode_params->num_active_dpb_entries &&
 	     i < (V4L2_HEVC_DPB_ENTRIES_NUM_MAX - 1); i++) {
-		luma_addr = hantro_hevc_get_ref_buf(ctx, dpb[i].pic_order_cnt_val);
+		luma_addr = hantro_get_ref(ctx, dpb[i].timestamp);
 		if (!luma_addr)
 			return -ENOMEM;
 
@@ -480,9 +477,6 @@ static int set_ref(struct hantro_ctx *ctx)
 	if (!luma_addr)
 		return -ENOMEM;
 
-	if (hantro_hevc_add_ref_buf(ctx, decode_params->pic_order_cnt_val, luma_addr))
-		return -EINVAL;
-
 	chroma_addr = luma_addr + cr_offset;
 	mv_addr = luma_addr + mv_offset;
 	compress_luma_addr = luma_addr + compress_luma_offset;
diff --git a/drivers/media/platform/verisilicon/hantro_hevc.c b/drivers/media/platform/verisilicon/hantro_hevc.c
index 83cd12b0ddd6..7a38012cbb32 100644
--- a/drivers/media/platform/verisilicon/hantro_hevc.c
+++ b/drivers/media/platform/verisilicon/hantro_hevc.c
@@ -30,48 +30,6 @@ module_param_named(hevc_use_compression, hevc_use_compression, bool, 0644);
 MODULE_PARM_DESC(hevc_use_compression,
 		 "Use reference frame compression for HEVC");
 
-void hantro_hevc_ref_init(struct hantro_ctx *ctx)
-{
-	struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec;
-
-	hevc_dec->ref_bufs_used = 0;
-}
-
-dma_addr_t hantro_hevc_get_ref_buf(struct hantro_ctx *ctx,
-				   s32 poc)
-{
-	struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec;
-	int i;
-
-	/* Find the reference buffer in already known ones */
-	for (i = 0;  i < NUM_REF_PICTURES; i++) {
-		if (hevc_dec->ref_bufs_poc[i] == poc) {
-			hevc_dec->ref_bufs_used |= 1 << i;
-			return hevc_dec->ref_bufs[i].dma;
-		}
-	}
-
-	return 0;
-}
-
-int hantro_hevc_add_ref_buf(struct hantro_ctx *ctx, int poc, dma_addr_t addr)
-{
-	struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec;
-	int i;
-
-	/* Add a new reference buffer */
-	for (i = 0; i < NUM_REF_PICTURES; i++) {
-		if (!(hevc_dec->ref_bufs_used & 1 << i)) {
-			hevc_dec->ref_bufs_used |= 1 << i;
-			hevc_dec->ref_bufs_poc[i] = poc;
-			hevc_dec->ref_bufs[i].dma = addr;
-			return 0;
-		}
-	}
-
-	return -EINVAL;
-}
-
 static int tile_buffer_reallocate(struct hantro_ctx *ctx)
 {
 	struct hantro_dev *vpu = ctx->dev;
@@ -278,8 +236,6 @@ int hantro_hevc_dec_init(struct hantro_ctx *ctx)
 
 	hevc_dec->scaling_lists.size = SCALING_LIST_SIZE;
 
-	hantro_hevc_ref_init(ctx);
-
 	hevc_dec->use_compression =
 		hevc_use_compression & hantro_needs_postproc(ctx, ctx->vpu_dst_fmt);
 
diff --git a/drivers/media/platform/verisilicon/hantro_hw.h b/drivers/media/platform/verisilicon/hantro_hw.h
index 13e573f1f19d..87f0eaf31869 100644
--- a/drivers/media/platform/verisilicon/hantro_hw.h
+++ b/drivers/media/platform/verisilicon/hantro_hw.h
@@ -36,8 +36,6 @@
 #define FMT_4K_WIDTH		4096
 #define FMT_4K_HEIGHT		2304
 
-#define NUM_REF_PICTURES	(V4L2_HEVC_DPB_ENTRIES_NUM_MAX + 1)
-
 #define AV1_MAX_FRAME_BUF_COUNT	(V4L2_AV1_TOTAL_REFS_PER_FRAME + 1)
 
 #define MAX_POSTPROC_BUFFERS	64
@@ -145,10 +143,7 @@ struct hantro_hevc_dec_ctrls {
  * @tile_filter:	Tile vertical filter buffer
  * @tile_sao:		Tile SAO buffer
  * @tile_bsd:		Tile BSD control buffer
- * @ref_bufs:		Internal reference buffers
  * @scaling_lists:	Scaling lists buffer
- * @ref_bufs_poc:	Internal reference buffers picture order count
- * @ref_bufs_used:	Bitfield of used reference buffers
  * @ctrls:		V4L2 controls attached to a run
  * @num_tile_cols_allocated: number of allocated tiles
  * @use_compression:	use reference buffer compression
@@ -158,10 +153,7 @@ struct hantro_hevc_dec_hw_ctx {
 	struct hantro_aux_buf tile_filter;
 	struct hantro_aux_buf tile_sao;
 	struct hantro_aux_buf tile_bsd;
-	struct hantro_aux_buf ref_bufs[NUM_REF_PICTURES];
 	struct hantro_aux_buf scaling_lists;
-	s32 ref_bufs_poc[NUM_REF_PICTURES];
-	u32 ref_bufs_used;
 	struct hantro_hevc_dec_ctrls ctrls;
 	unsigned int num_tile_cols_allocated;
 	bool use_compression;
@@ -454,9 +446,6 @@ int hantro_hevc_dec_init(struct hantro_ctx *ctx);
 void hantro_hevc_dec_exit(struct hantro_ctx *ctx);
 int hantro_g2_hevc_dec_run(struct hantro_ctx *ctx);
 int hantro_hevc_dec_prepare_run(struct hantro_ctx *ctx);
-void hantro_hevc_ref_init(struct hantro_ctx *ctx);
-dma_addr_t hantro_hevc_get_ref_buf(struct hantro_ctx *ctx, s32 poc);
-int hantro_hevc_add_ref_buf(struct hantro_ctx *ctx, int poc, dma_addr_t addr);
 
 int rockchip_vpu981_av1_dec_init(struct hantro_ctx *ctx);
 void rockchip_vpu981_av1_dec_exit(struct hantro_ctx *ctx);

---
base-commit: fb8b2c10a588f1384b98280b1c32307da9db4747
change-id: 20261129-hantro-hevc-ref-ts-71638230c181

Best regards,
--  
Arun Iyengar <aiyengar1990@gmail.com>



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-29 19:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29 19:32 [PATCH RFC] media: verisilicon: hevc: Find the reference buffers by timestamp Arun Iyengar via B4 Relay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®