media: verisilicon: Create AV1 helper library
Subsystems (4)
Benjamin Gaignard
maintainerv8 cover media: verisilicon: Create AV1 helper library
Regroup all none hardware related AV1 functions into a helper library. The goal is to avoid code duplication for future AV1 codecs.
Tested on rock 5b board Fluster score remains the same 204/241.
changes in v8
- check reference frame index when validating the frame before calling the driver.
- add Reviewed-by tag and CC stable kernel.
changes in v7:
- prepend 2 patches with Fixes tag for backport
- rebase the series on v7.4-rc3 tag
changes in v6:
- include b84f6533a8ed ("media: verisilicon: rockchip: guard VPU981 AV1 divisor and tile buffer") and 367db8b23c26 ("media: verisilicon: rockchip: reject AV1 frames exceeding the tile capacity") fixes into the patch to not losing these improvements.
Benjamin Gaignard (3):
media: v4l2-ctrls: validate AV1 reference frame index
media: verisilicon: AV1: Fix luma size
media: verisilicon: Create AV1 helper library
drivers/media/platform/verisilicon/Makefile | 7 +-
.../media/platform/verisilicon/hantro_av1.c | 807 ++++++++++++
.../media/platform/verisilicon/hantro_av1.h | 64 +
...entropymode.c => hantro_av1_entropymode.c} | 18 +-
...entropymode.h => hantro_av1_entropymode.h} | 18 +-
...av1_filmgrain.c => hantro_av1_filmgrain.c} | 125 +-
.../verisilicon/hantro_av1_filmgrain.h | 48 +
.../media/platform/verisilicon/hantro_hw.h | 7 +-
.../verisilicon/rockchip_av1_filmgrain.h | 36 -
.../verisilicon/rockchip_vpu981_hw_av1_dec.c | 1082 ++---------------
.../platform/verisilicon/rockchip_vpu_hw.c | 7 +-
drivers/media/v4l2-core/v4l2-ctrls-core.c | 20 +
include/uapi/linux/v4l2-controls.h | 1 +
13 files changed, 1152 insertions(+), 1088 deletions(-)
create mode 100644 drivers/media/platform/verisilicon/hantro_av1.c
create mode 100644 drivers/media/platform/verisilicon/hantro_av1.h
rename drivers/media/platform/verisilicon/{rockchip_av1_entropymode.c => hantro_av1_entropymode.c} (99%)
rename drivers/media/platform/verisilicon/{rockchip_av1_entropymode.h => hantro_av1_entropymode.h} (95%)
rename drivers/media/platform/verisilicon/{rockchip_av1_filmgrain.c => hantro_av1_filmgrain.c} (85%)
create mode 100644 drivers/media/platform/verisilicon/hantro_av1_filmgrain.h
delete mode 100644 drivers/media/platform/verisilicon/rockchip_av1_filmgrain.hSignature · 1 line
2.53.0
Benjamin Gaignard
maintainerPATCH v8 1/3 media: v4l2-ctrls: validate AV1 reference frame index
The stateless AV1 decoders use primary_ref_frame and ref_frame_idx[] to to find the matching reference frames. V4L2_AV1_PRIMARY_REF_NONE is the value of primary_ref_frame indicating that there is no primary reference frame.
Reject invalid indexes to avoid issues in the drivers while validate other frame parameters.
---
changes in v8
- check reference frame index when validating the frame before calling the driver.
.../verisilicon/rockchip_vpu981_hw_av1_dec.c | 3 +-- drivers/media/v4l2-core/v4l2-ctrls-core.c | 20 +++++++++++++++++++ include/uapi/linux/v4l2-controls.h | 1 + 3 files changed, 22 insertions(+), 2 deletions(-)
drivers/media/platform/verisilicon/rockchip_vpu981_hw_av1_dec.c +1 -2
@@ -20,7 +20,6 @@#define AV1_REF_SCALE_SHIFT 14#define AV1_INVALID_IDX -1#define MAX_FRAME_DISTANCE 31 -#define AV1_PRIMARY_REF_NONE 7#define AV1_TILE_SIZE ALIGN(32 * 128, 4096)/* * These 3 values aren't defined enum v4l2_av1_segment_feature because
@@ -1256,7 +1255,7 @@ static void rockchip_vpu981_av1_dec_set_prob(struct hantro_ctx *ctx)bool frame_is_intra =IS_INTRA (frame->frame_type);if (error_resilient_mode || frame_is_intra || - frame->primary_ref_frame ==AV1_PRIMARY_REF_NONE ) { + frame->primary_ref_frame ==V4L2_AV1_PRIMARY_REF_NONE ) { av1_dec->cdfs = &av1_dec->default_cdfs; av1_dec->cdfs_ndvc = &av1_dec->default_cdfs_ndvc; rockchip_av1_default_coeff_probs(quantization->base_q_idx,
drivers/media/v4l2-core/v4l2-ctrls-core.c +20 -0
@@ -817,10 +817,30 @@ static int validate_av1_tile_info(struct v4l2_av1_tile_info *t)return 0 ; } +static int validate_av1_reference_index (struct v4l2_ctrl_av1_frame *f) +{ +int idx; + +if (f->primary_ref_frame >V4L2_AV1_PRIMARY_REF_NONE ) +return -EINVAL ; + +if (f->primary_ref_frame ==V4L2_AV1_PRIMARY_REF_NONE ) +return 0 ; + + idx = f->ref_frame_idx[f->primary_ref_frame]; +if (idx <0 || idx >=V4L2_AV1_TOTAL_REFS_PER_FRAME ) +return -EINVAL ; + +return 0 ; +} +static int validate_av1_frame (struct v4l2_ctrl_av1_frame *f) {int ret =0 ; + ret =validate_av1_reference_index (f); +if (ret) +return ret; ret =validate_av1_tile_info (&f->tile_info);if (ret)return ret;
include/uapi/linux/v4l2-controls.h +1 -0
@@ -2940,6 +2940,7 @@ struct v4l2_ctrl_vp9_compressed_hdr {#define V4L2_AV1_MAX_TILE_COLS 64#define V4L2_AV1_MAX_TILE_ROWS 64#define V4L2_AV1_MAX_TILE_COUNT 512 +#define V4L2_AV1_PRIMARY_REF_NONE 7#define V4L2_AV1_SEQUENCE_FLAG_STILL_PICTURE 0x00000001#define V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK 0x00000002
Signature · 1 line
2.53.0
Nicolas Dufresne
maintainerRe: [PATCH v8 1/3] media: v4l2-ctrls: validate AV1 reference frame index
Le mardi 24 novembre 2026 à 14:18 +0100, Benjamin Gaignard a écrit :
Quoted 13 lines
The stateless AV1 decoders use
primary_ref_frameand ref_frame_idx[] to to find the matching reference frames.V4L2_AV1_PRIMARY_REF_NONEis the value ofprimary_ref_frameindicating that there is no primary reference frame.
Reject invalid indexes to avoid issues in the drivers while validate other frame parameters.Signed-off-by Benjamin Gaignard <[email protected]>---
changes in v8
- check reference frame index when validating the frame before calling the driver.
Quoted 4 lines
.../verisilicon/rockchip_vpu981_hw_av1_dec.c | 3 +-- drivers/media/v4l2-core/v4l2-ctrls-core.c | 20 +++++++++++++++++++ include/uapi/linux/v4l2-controls.h | 1 + 3 files changed, 22 insertions(+), 2 deletions(-)
Split core from driver changes.
Quoted 69 lines
drivers/media/platform/verisilicon/rockchip_vpu981_hw_av1_dec.c +1
-2@@ -20,7 +20,6 @@#define AV1_REF_SCALE_SHIFT 14#define AV1_INVALID_IDX -1#define MAX_FRAME_DISTANCE 31 -#define AV1_PRIMARY_REF_NONE 7#define AV1_TILE_SIZE ALIGN(32 * 128, 4096)/* * These 3 values aren't defined enum v4l2_av1_segment_feature because @@ -1256,7 +1255,7 @@ static void rockchip_vpu981_av1_dec_set_prob(struct hantro_ctx *ctx)bool frame_is_intra =IS_INTRA (frame->frame_type);if (error_resilient_mode || frame_is_intra || - frame->primary_ref_frame ==AV1_PRIMARY_REF_NONE ) { + frame->primary_ref_frame ==V4L2_AV1_PRIMARY_REF_NONE ) { av1_dec->cdfs = &av1_dec->default_cdfs; av1_dec->cdfs_ndvc = &av1_dec->default_cdfs_ndvc; rockchip_av1_default_coeff_probs(quantization->base_q_idx,drivers/media/v4l2-core/v4l2-ctrls-core.c +20
-0@@ -817,10 +817,30 @@ static int validate_av1_tile_info(struct v4l2_av1_tile_info *t)return 0 ; } +static int validate_av1_reference_index (struct v4l2_ctrl_av1_frame *f) +{ +int idx; + +if (f->primary_ref_frame >V4L2_AV1_PRIMARY_REF_NONE ) +return -EINVAL ; + +if (f->primary_ref_frame ==V4L2_AV1_PRIMARY_REF_NONE ) +return 0 ; + + idx = f->ref_frame_idx[f->primary_ref_frame]; +if (idx <0 || idx >=V4L2_AV1_TOTAL_REFS_PER_FRAME ) +return -EINVAL ; + +return 0 ; +} +static int validate_av1_frame (struct v4l2_ctrl_av1_frame *f) {int ret =0 ; + ret =validate_av1_reference_index (f); +if (ret) +return ret; ret =validate_av1_tile_info (&f->tile_info);if (ret)return ret;include/uapi/linux/v4l2-controls.h +1
-0@@ -2940,6 +2940,7 @@ struct v4l2_ctrl_vp9_compressed_hdr {#define V4L2_AV1_MAX_TILE_COLS 64#define V4L2_AV1_MAX_TILE_ROWS 64#define V4L2_AV1_MAX_TILE_COUNT 512 +#define V4L2_AV1_PRIMARY_REF_NONE 7#define V4L2_AV1_SEQUENCE_FLAG_STILL_PICTURE 0x00000001#define V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK 0x00000002
Benjamin Gaignard
maintainerPATCH v8 2/3 media: verisilicon: AV1: Fix luma size
In reply to Benjamin GaignardUse the values computed by v4l2 helpers to get luma size. Unlike before this will also take care of padding.
---
changes in v8
- add Reviewed-by tag and CC stable kernel.
drivers/media/platform/verisilicon/rockchip_vpu981_hw_av1_dec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/verisilicon/rockchip_vpu981_hw_av1_dec.c +1 -1
@@ -215,7 +215,7 @@ static void rockchip_vpu981_av1_dec_clean_refs(struct hantro_ctx *ctx)static size_t rockchip_vpu981_av1_dec_luma_size (struct hantro_ctx *ctx) { -return ctx->dst_fmt.width * ctx->dst_fmt.height * ctx->bit_depth /8 ; +return ctx->ref_fmt.plane_fmt[0 ].bytesperline * ctx->ref_fmt.height; }static size_t rockchip_vpu981_av1_dec_chroma_size (struct hantro_ctx *ctx)
Signature · 1 line
2.53.0
Benjamin Gaignard
maintainerPATCH v8 3/3 media: verisilicon: Create AV1 helper library
In reply to Benjamin GaignardRegroup all none hardware related AV1 functions into a helper library. The goal is to avoid code duplication for future AV1 codecs.
Tested on rock 5b board Fluster score remains the same 204/241.
---
drivers/media/platform/verisilicon/Makefile | 7 +-
.../media/platform/verisilicon/hantro_av1.c | 807 ++++++++++++
.../media/platform/verisilicon/hantro_av1.h | 64 +
...entropymode.c => hantro_av1_entropymode.c} | 18 +-
...entropymode.h => hantro_av1_entropymode.h} | 18 +-
...av1_filmgrain.c => hantro_av1_filmgrain.c} | 125 +-
.../verisilicon/hantro_av1_filmgrain.h | 48 +
.../media/platform/verisilicon/hantro_hw.h | 7 +-
.../verisilicon/rockchip_av1_filmgrain.h | 36 -
.../verisilicon/rockchip_vpu981_hw_av1_dec.c | 1081 ++---------------
.../platform/verisilicon/rockchip_vpu_hw.c | 7 +-
11 files changed, 1131 insertions(+), 1087 deletions(-)
create mode 100644 drivers/media/platform/verisilicon/hantro_av1.c
create mode 100644 drivers/media/platform/verisilicon/hantro_av1.h
rename drivers/media/platform/verisilicon/{rockchip_av1_entropymode.c => hantro_av1_entropymode.c} (99%)
rename drivers/media/platform/verisilicon/{rockchip_av1_entropymode.h => hantro_av1_entropymode.h} (95%)
rename drivers/media/platform/verisilicon/{rockchip_av1_filmgrain.c => hantro_av1_filmgrain.c} (85%)
create mode 100644 drivers/media/platform/verisilicon/hantro_av1_filmgrain.h
delete mode 100644 drivers/media/platform/verisilicon/rockchip_av1_filmgrain.hdrivers/media/platform/verisilicon/Makefile +4 -3
@@ -19,7 +19,10 @@ hantro-vpu-y += \ hantro_hevc.o \ hantro_mpeg2.o \ hantro_vp8.o \ - hantro_vp9.o + hantro_vp9.o \ + hantro_av1.o \ + hantro_av1_filmgrain.o \ + hantro_av1_entropymode.o hantro-vpu-$(CONFIG_VIDEO_HANTRO_IMX8M) += \ imx8m_vpu_hw.o
@@ -33,8 +36,6 @@ hantro-vpu-$(CONFIG_VIDEO_HANTRO_ROCKCHIP) += \ rockchip_vpu2_hw_mpeg2_dec.o \ rockchip_vpu2_hw_vp8_dec.o \ rockchip_vpu981_hw_av1_dec.o \ - rockchip_av1_filmgrain.o \ - rockchip_av1_entropymode.o \ rockchip_vpu_hw.o hantro-vpu-$(CONFIG_VIDEO_HANTRO_SUNXI) += \
drivers/media/platform/verisilicon/hantro_av1.c new file +807 -0
807 lines not on this page · raw message
drivers/media/platform/verisilicon/hantro_av1.h new file +64 -0
64 lines not on this page · raw message
drivers/media/platform/verisilicon/hantro_av1_entropymode.c renamed from drivers/media/platform/verisilicon/rockchip_av1_entropymode.c +9 -9
@@ -11,7 +11,7 @@*/ #include "hantro.h" -#include "rockchip_av1_entropymode.h" +#include "hantro_av1_entropymode.h" #define AOM_ICDF ICDF#define AOM_CDF2 (a0) AOM_ICDF(a0)
@@ -4195,7 +4195,7 @@ static const u16 default_bits_cdf[][10] = { } }; -static int rockchip_av1_get_q_ctx (int q) +static int hantro_av1_get_q_ctx (int q) {if (q <=20 )return 0 ;
@@ -4206,10 +4206,10 @@ static int rockchip_av1_get_q_ctx(int q)return 3 ; } -void rockchip_av1_default_coeff_probs (u32 base_qindex,void *ptr) +void hantro_av1_default_coeff_probs (u32 base_qindex,void *ptr) {struct av1cdfs *cdfs = (struct av1cdfs *)ptr; -const int index =rockchip_av1_get_q_ctx (base_qindex); +const int index =hantro_av1_get_q_ctx (base_qindex);memcpy (cdfs->txb_skip_cdf, av1_default_txb_skip_cdfs[index],sizeof (av1_default_txb_skip_cdfs[0 ]));
@@ -4240,8 +4240,8 @@ void rockchip_av1_default_coeff_probs(u32 base_qindex, void *ptr) sizeof(av1_default_eob_multi1024_cdfs[0])); } -void rockchip_av1_set_default_cdfs (struct av1cdfs *cdfs, -struct mvcdfs *cdfs_ndvc) +void hantro_av1_set_default_cdfs (struct av1cdfs *cdfs, +struct mvcdfs *cdfs_ndvc) {memcpy (cdfs->partition_cdf, default_partition_cdf,sizeof (cdfs->partition_cdf));
@@ -4398,7 +4398,7 @@ void rockchip_av1_set_default_cdfs(struct av1cdfs *cdfs, sizeof(cdfs->compound_idx_cdf)); } -void rockchip_av1_get_cdfs (struct hantro_ctx *ctx,u32 ref_idx) +void hantro_av1_get_cdfs (struct hantro_ctx *ctx,u32 ref_idx) {struct hantro_av1_dec_hw_ctx *av1_dec = &ctx->av1_dec;
@@ -4406,8 +4406,8 @@ void rockchip_av1_get_cdfs(struct hantro_ctx *ctx, u32 ref_idx) av1_dec->cdfs_ndvc = &av1_dec->cdfs_last_ndvc[ref_idx]; } -void rockchip_av1_store_cdfs (struct hantro_ctx *ctx, -u32 refresh_frame_flags) +void hantro_av1_store_cdfs (struct hantro_ctx *ctx, +u32 refresh_frame_flags) {struct hantro_av1_dec_hw_ctx *av1_dec = &ctx->av1_dec;int i;
drivers/media/platform/verisilicon/hantro_av1_entropymode.h renamed from drivers/media/platform/verisilicon/rockchip_av1_entropymode.h +9 -9
@@ -1,7 +1,7 @@/* SPDX-License-Identifier: GPL-2.0-only */ -#ifndef _ROCKCHIP_AV1_ENTROPYMODE_H_ -#define _ROCKCHIP_AV1_ENTROPYMODE_H_ +#ifndef _HANTRO_AV1_ENTROPYMODE_H_ +#define _HANTRO_AV1_ENTROPYMODE_H_#include <linux/types.h>
@@ -262,11 +262,11 @@ struct av1cdfs { u16 dummy3[16]; }; -void rockchip_av1_store_cdfs (struct hantro_ctx *ctx, -u32 refresh_frame_flags); -void rockchip_av1_get_cdfs (struct hantro_ctx *ctx,u32 ref_idx); -void rockchip_av1_set_default_cdfs (struct av1cdfs *cdfs, -struct mvcdfs *cdfs_ndvc); -void rockchip_av1_default_coeff_probs (u32 base_qindex,void *ptr); +void hantro_av1_store_cdfs (struct hantro_ctx *ctx, +u32 refresh_frame_flags); +void hantro_av1_get_cdfs (struct hantro_ctx *ctx,u32 ref_idx); +void hantro_av1_set_default_cdfs (struct av1cdfs *cdfs, +struct mvcdfs *cdfs_ndvc); +void hantro_av1_default_coeff_probs (u32 base_qindex,void *ptr); -#endif /* _ROCKCHIP_AV1_ENTROPYMODE_H_ */ +#endif /* _HANTRO_AV1_ENTROPYMODE_H_ */
drivers/media/platform/verisilicon/hantro_av1_filmgrain.c renamed from drivers/media/platform/verisilicon/rockchip_av1_filmgrain.c +60 -65
229 lines not on this page · raw message
drivers/media/platform/verisilicon/hantro_av1_filmgrain.h new file +48 -0
48 lines not on this page · raw message
drivers/media/platform/verisilicon/hantro_hw.h +2 -5
@@ -15,8 +15,8 @@#include <media/v4l2-vp9.h> #include <media/videobuf2-core.h> -#include "rockchip_av1_entropymode.h" -#include "rockchip_av1_filmgrain.h" +#include "hantro_av1_entropymode.h" +#include "hantro_av1_filmgrain.h" #define DEC_8190_ALIGN_MASK 0x07U
@@ -458,10 +458,7 @@ 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);int rockchip_vpu981_av1_dec_run (struct hantro_ctx *ctx); -void rockchip_vpu981_av1_dec_done (struct hantro_ctx *ctx);static inline unsigned short hantro_vp9_num_sbs (unsigned short dimension) {
drivers/media/platform/verisilicon/rockchip_av1_filmgrain.h deleted +0 -36
@@ -1,36 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ - -#ifndef _ROCKCHIP_AV1_FILMGRAIN_H_ -#define _ROCKCHIP_AV1_FILMGRAIN_H_ - -#include <linux/types.h> - -void rockchip_av1_generate_luma_grain_block (s32 (*luma_grain_block)[73 ][82 ], -s32 bitdepth, -u8 num_y_points, -s32 grain_scale_shift, -s32 ar_coeff_lag, -s32 (*ar_coeffs_y)[24 ], -s32 ar_coeff_shift, -s32 grain_min, -s32 grain_max, -u16 random_seed); - -void rockchip_av1_generate_chroma_grain_block (s32 (*luma_grain_block)[73 ][82 ], -s32 (*cb_grain_block)[38 ][44 ], -s32 (*cr_grain_block)[38 ][44 ], -s32 bitdepth, -u8 num_y_points, -u8 num_cb_points, -u8 num_cr_points, -s32 grain_scale_shift, -s32 ar_coeff_lag, -s32 (*ar_coeffs_cb)[25 ], -s32 (*ar_coeffs_cr)[25 ], -s32 ar_coeff_shift, -s32 grain_min, -s32 grain_max, -u8 chroma_scaling_from_luma, -u16 random_seed); - -#endif
drivers/media/platform/verisilicon/rockchip_vpu981_hw_av1_dec.c +124 -957
1,387 lines not on this page · raw message
drivers/media/platform/verisilicon/rockchip_vpu_hw.c +4 -3
@@ -9,6 +9,7 @@#include <linux/clk.h> #include "hantro.h" +#include "hantro_av1.h" #include "hantro_jpeg.h" #include "hantro_g1_regs.h" #include "hantro_h1_regs.h"
@@ -608,9 +609,9 @@ static const struct hantro_codec_ops rk3568_vepu_codec_ops[] = {static const struct hantro_codec_ops rk3588_vpu981_codec_ops[] = { [HANTRO_MODE_AV1_DEC ] = { .run = rockchip_vpu981_av1_dec_run, - .init = rockchip_vpu981_av1_dec_init, - .exit = rockchip_vpu981_av1_dec_exit, - .done = rockchip_vpu981_av1_dec_done, + .init = hantro_av1_init, + .exit = hantro_av1_exit, + .done = hantro_av1_update_prob, }, };/*
Signature · 1 line
2.53.0