Skip to contentSkip to conversation

media: mediatek: vcodec: support stateless AV1 decoder

RFC PATCH Superseded Xiaoyong Lu 2 participants 3 messages Sep 1, 2022 – Sep 30, 2022

Subsystems (3)

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v3 1/1 media: mediatek: vcodec: support stateless AV1 decoder

Add mediatek av1 decoder linux driver which use the stateless API in MT8195.

---
Changes from v2:
- Match with av1 uapi v3 modify
- test by av1 fluster, result is 173/239

---
Reference series:
[1]: v3 of this series is presend by Daniel Almeida.

     message-id: [email protected]
 .../media/platform/mediatek/vcodec/Makefile   |    1 +
 .../vcodec/mtk_vcodec_dec_stateless.c         |   47 +-
 .../platform/mediatek/vcodec/mtk_vcodec_drv.h |    1 +
 .../vcodec/vdec/vdec_av1_req_lat_if.c         | 2225 +++++++++++++++++
 .../platform/mediatek/vcodec/vdec_drv_if.c    |    4 +
 .../platform/mediatek/vcodec/vdec_drv_if.h    |    1 +
 .../platform/mediatek/vcodec/vdec_msg_queue.c |   27 +
 .../platform/mediatek/vcodec/vdec_msg_queue.h |    4 +
 8 files changed, 2309 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c
drivers/media/platform/mediatek/vcodec/Makefile +1 -0
index 93e7a343b5b0e..7537259130072 100644
@@ -10,6 +10,7 @@ mtk-vcodec-dec-y := vdec/vdec_h264_if.o \
 		vdec/vdec_vp8_req_if.o \
 		vdec/vdec_vp9_if.o \
 		vdec/vdec_vp9_req_lat_if.o \
+		vdec/vdec_av1_req_lat_if.o \
 		vdec/vdec_h264_req_if.o \
 		vdec/vdec_h264_req_common.o \
 		vdec/vdec_h264_req_multi_if.o \
drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_stateless.c +46 -1

69 lines not on this page · raw message

drivers/media/platform/mediatek/vcodec/mtk_vcodec_drv.h +1 -0
index 6a47a11ff654a..a6db972b1ff72 100644
@@ -344,6 +344,7 @@ enum mtk_vdec_format_types {
 	MTK_VDEC_FORMAT_H264_SLICE = 0x100,
 	MTK_VDEC_FORMAT_VP8_FRAME = 0x200,
 	MTK_VDEC_FORMAT_VP9_FRAME = 0x400,
+	MTK_VDEC_FORMAT_AV1_FRAME = 0x800,
 	MTK_VCODEC_INNER_RACING = 0x20000,
 };
 
drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c new file +2225 -0

2,225 lines not on this page · raw message

drivers/media/platform/mediatek/vcodec/vdec_drv_if.c +4 -0
index f3807f03d8806..4dda59a6c8141 100644
@@ -49,6 +49,10 @@ int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 		ctx->dec_if = &vdec_vp9_slice_lat_if;
 		ctx->hw_id = IS_VDEC_LAT_ARCH(hw_arch) ? MTK_VDEC_LAT0 : MTK_VDEC_CORE;
 		break;
+	case V4L2_PIX_FMT_AV1_FRAME:
+		ctx->dec_if = &vdec_av1_slice_lat_if;
+		ctx->hw_id = MTK_VDEC_LAT0;
+		break;
 	default:
 		return -EINVAL;
 	}
drivers/media/platform/mediatek/vcodec/vdec_drv_if.h +1 -0
index 076306ff2dd49..dc6c8ecd9843a 100644
@@ -61,6 +61,7 @@ extern const struct vdec_common_if vdec_vp8_if;
 extern const struct vdec_common_if vdec_vp8_slice_if;
 extern const struct vdec_common_if vdec_vp9_if;
 extern const struct vdec_common_if vdec_vp9_slice_lat_if;
+extern const struct vdec_common_if vdec_av1_slice_lat_if;
 
 /**
  * vdec_if_init() - initialize decode driver
drivers/media/platform/mediatek/vcodec/vdec_msg_queue.c +27 -0
index ae500980ad45c..05b54b0e3f2d2 100644
@@ -20,6 +20,9 @@
 /* the size used to store avc error information */
 #define VDEC_ERR_MAP_SZ_AVC         (17 * SZ_1K)
 
+#define VDEC_RD_MV_BUFFER_SZ        (((SZ_4K * 2304 >> 4) + SZ_1K) << 1)
+#define VDEC_LAT_TILE_SZ            (64 * SZ_4K)
+
 /* core will read the trans buffer which decoded by lat to decode again.
  * The trans buffer size of FHD and 4K bitstreams are different.
  */
@@ -194,6 +197,14 @@ void vdec_msg_queue_deinit(struct vdec_msg_queue *msg_queue,
 		if (mem->va)
 			mtk_vcodec_mem_free(ctx, mem);
 
+		mem = &lat_buf->rd_mv_addr;
+		if (mem->va)
+			mtk_vcodec_mem_free(ctx, mem);
+
+		mem = &lat_buf->tile_addr;
+		if (mem->va)
+			mtk_vcodec_mem_free(ctx, mem);
+
 		kfree(lat_buf->private_data);
 	}
 }
@@ -270,6 +281,22 @@ int vdec_msg_queue_init(struct vdec_msg_queue *msg_queue,
 			goto mem_alloc_err;
 		}
 
+		if (ctx->current_codec == V4L2_PIX_FMT_AV1_FRAME) {
+			lat_buf->rd_mv_addr.size = VDEC_RD_MV_BUFFER_SZ;
+			err = mtk_vcodec_mem_alloc(ctx, &lat_buf->rd_mv_addr);
+			if (err) {
+				mtk_v4l2_err("failed to allocate rd_mv_addr buf[%d]", i);
+				return -ENOMEM;
+			}
+
+			lat_buf->tile_addr.size = VDEC_LAT_TILE_SZ;
+			err = mtk_vcodec_mem_alloc(ctx, &lat_buf->tile_addr);
+			if (err) {
+				mtk_v4l2_err("failed to allocate tile_addr buf[%d]", i);
+				return -ENOMEM;
+			}
+		}
+
 		lat_buf->private_data = kzalloc(private_size, GFP_KERNEL);
 		if (!lat_buf->private_data) {
 			err = -ENOMEM;
drivers/media/platform/mediatek/vcodec/vdec_msg_queue.h +4 -0
index c43d427f5f544..525170e411ee0 100644
@@ -42,6 +42,8 @@ struct vdec_msg_queue_ctx {
  * struct vdec_lat_buf - lat buffer message used to store lat info for core decode
  * @wdma_err_addr: wdma error address used for lat hardware
  * @slice_bc_addr: slice bc address used for lat hardware
+ * @rd_mv_addr:	mv addr for av1 lat hardware output, core hardware input
+ * @tile_addr:	tile buffer for av1 core input
  * @ts_info: need to set timestamp from output to capture
  * @src_buf_req: output buffer media request object
  *
@@ -54,6 +56,8 @@ struct vdec_msg_queue_ctx {
 struct vdec_lat_buf {
 	struct mtk_vcodec_mem wdma_err_addr;
 	struct mtk_vcodec_mem slice_bc_addr;
+	struct mtk_vcodec_mem rd_mv_addr;
+	struct mtk_vcodec_mem tile_addr;
 	struct vb2_v4l2_buffer ts_info;
 	struct media_request *src_buf_req;
 
Signature · 1 line
2.18.0
#1 lore raw

Hi Xiaoyong.

Comments below (other code removed for brevity)

+/**
+ * struct vdec_av1_slice_slot - slot info need save in global instance
+ * @frame_info: frame info for each slot
+ * @timestamp: time stamp info
+ */
+struct vdec_av1_slice_slot {
+ struct vdec_av1_slice_frame_info frame_info[AV1_MAX_FRAME_BUF_COUNT];
+ u64 timestamp[AV1_MAX_FRAME_BUF_COUNT];
+};

nit: slot info that needs to be saved in the global instance

+static int vdec_av1_slice_get_qindex(struct vdec_av1_slice_uncompressed_header *uh,
+ int segmentation_id)
+{
+ struct vdec_av1_slice_seg *seg = &uh->seg;
+ struct vdec_av1_slice_quantization *quant = &uh->quant;
+ int data = 0, qindex = 0;
+
+ if (seg->segmentation_enabled &&
+ (seg->feature_enabled_mask[segmentation_id] & BIT(0))) {
+ data = seg->feature_data[segmentation_id][0];

Maybe you should replace the 0 above by SEG_LVL_ALT_Q to be more explicit. Same goes for BIT(0).

+static void vdec_av1_slice_setup_lr(struct vdec_av1_slice_lr *lr,
+ struct v4l2_av1_loop_restoration *ctrl_lr)
+{
+ int i;
+
+ for (i = 0; i < V4L2_AV1_NUM_PLANES_MAX; i++) {
+ lr->frame_restoration_type[i] = ctrl_lr->frame_restoration_type[i];
+ lr->loop_restoration_size[i] = ctrl_lr->loop_restoration_size[i];
+ }
+ lr->use_lr = !!lr->frame_restoration_type[0];
+ lr->use_chroma_lr = !!lr->frame_restoration_type[1];
+}

From a first glance, this looks a bit divergent from the spec?

for ( i = 0; i < NumPlanes; i++ ) {
    lr_type
    FrameRestorationType[i] = Remap_Lr_Type[lr_type]
    if ( FrameRestorationType[i] != RESTORE_NONE ) {
        UsesLr = 1
        if ( i > 0 ) {
            usesChromaLr = 1
        }
    }
}

I will include these two variables in the next iteration of the uapi if computing them in the driver is problematic.

+static void vdec_av1_slice_setup_lf(struct vdec_av1_slice_loop_filter *lf,
+ struct v4l2_av1_loop_filter *ctrl_lf)
+{
+ int i;
+
+ for (i = 0; i < 4; i++)
+ lf->loop_filter_level[i] = ctrl_lf->level[i];
+
+ for (i = 0; i < V4L2_AV1_TOTAL_REFS_PER_FRAME; i++)
+ lf->loop_filter_ref_deltas[i] = ctrl_lf->ref_deltas[i];
+
+ for (i = 0; i < 2; i++)
+ lf->loop_filter_mode_deltas[i] = ctrl_lf->mode_deltas[i];
+
+ lf->loop_filter_sharpness = ctrl_lf->sharpness;
+ lf->loop_filter_delta_enabled =
+ BIT_FLAG(ctrl_lf, V4L2_AV1_LOOP_FILTER_FLAG_DELTA_ENABLED);
+}

Maybe ARRAY_SIZE can be of use in the loop indices here?

+static void vdec_av1_slice_setup_cdef(struct vdec_av1_slice_cdef *cdef,
+ struct v4l2_av1_cdef *ctrl_cdef)
+{
+ int i;
+
+ cdef->cdef_damping = ctrl_cdef->damping_minus_3 + 3;
+ cdef->cdef_bits = ctrl_cdef->bits;
+
+ for (i = 0; i < V4L2_AV1_CDEF_MAX; i++) {
+ if (ctrl_cdef->y_sec_strength[i] == 4)
+ ctrl_cdef->y_sec_strength[i] -= 1;
+
+ if (ctrl_cdef->uv_sec_strength[i] == 4)
+ ctrl_cdef->uv_sec_strength[i] -= 1;
+
+ cdef->cdef_y_strength[i] = ctrl_cdef->y_pri_strength[i] << 2 |
+ ctrl_cdef->y_sec_strength[i];
+ cdef->cdef_uv_strength[i] = ctrl_cdef->uv_pri_strength[i] << 2 |
+ ctrl_cdef->uv_sec_strength[i];
+ }
+}

Maybe:

#define SECONDARY_FILTER_STRENGTH_NUM_BITS 2

+ cdef->cdef_y_strength[i] = ctrl_cdef->y_pri_strength[i] << SECONDARY_FILTER_STRENGTH_NUM_BITS |
+ ctrl_cdef->y_sec_strength[i];
+ cdef->cdef_uv_strength[i] = ctrl_cdef->uv_pri_strength[i] << SECONDARY_FILTER_STRENGTH_NUM_BITS |
+ ctrl_cdef->uv_sec_strength[i];

This should make it clearer.

+ sb_boundary_x_m1 =
+ (tile->mi_col_starts[tile_col + 1] - tile->mi_col_starts[tile_col] - 1) &
+ 0x3F;
+ sb_boundary_y_m1 =
+ (tile->mi_row_starts[tile_row + 1] - tile->mi_row_starts[tile_row] - 1) &
+ 0x1FF;
+

IIRC there's a preference for lower case hex values in the media subsystem.

+static void vdec_av1_slice_get_dpb_size(struct vdec_av1_slice_instance *instance, u32 *dpb_sz) +{
+ /* refer av1 specification */
+ *dpb_sz = 9;
+}

That's actually defined as 8 in the spec, i.e.:

NUM_REF_FRAMES 8 Number of frames that can be stored for future reference.

It's helpful to indicate the section if you reference the specification, as it makes it easier for the reviewer to cross check.

+ /* get buffer address from vb2buf */
+ for (i = 0; i < V4L2_AV1_REFS_PER_FRAME; i++) {
+ struct vdec_av1_slice_fb *vref = &vsi->ref[i];
+ int idx = vb2_find_timestamp(vq, pfc->ref_idx[i], 0);

Needs to be converted to vb2_find_buffer in light of https://lore.kernel.org/lkml/[email protected]/T/

-- Daniel

#2 lore raw

Dear Daniel,
Thanks for your good suggestion!

I have updated v4 to fix
your comment.

Changes from v3:

- modify comment for struct vdec_av1_slice_slot
- add define SEG_LVL_ALT_Q
- change use_lr/use_chroma_lr parse from av1 spec
- use ARRAY_SIZE to replace size for loop_filter_level and loop_filter_mode_deltas
- change array size of loop_filter_mode_deltas from 4 to 2
- add define SECONDARY_FILTER_STRENGTH_NUM_BITS
- change some hex values from upper case to lower case
- change *dpb_sz equal to V4L2_AV1_TOTAL_REFS_PER_FRAME + 1
- convert vb2_find_timestamp to vb2_find_buffer
- test by av1 fluster, result is 173/239

detail in link:

https://patchwork.kernel.org/project/linux-mediatek/patch/[email protected]/

thanks !
Xiaoyong Lu

Quoted 173 lines · Daniel Almeida

Hi Xiaoyong.

Comments below (other code removed for brevity)

+/**
+ * struct vdec_av1_slice_slot - slot info need save in global instance
+ * @frame_info: frame info for each slot
+ * @timestamp: time stamp info
+ */

(quoted diff) +2 -0
+struct vdec_av1_slice_slot {
+	struct vdec_av1_slice_frame_info

frame_info[AV1_MAX_FRAME_BUF_COUNT];

(quoted diff) +2 -0
+	u64 timestamp[AV1_MAX_FRAME_BUF_COUNT];
+};

nit: slot info that needs to be saved in the global instance

+static int vdec_av1_slice_get_qindex(struct
vdec_av1_slice_uncompressed_header *uh,

drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c new file +9 -0
+				     int segmentation_id)
+{
+	struct vdec_av1_slice_seg *seg = &uh->seg;
+	struct vdec_av1_slice_quantization *quant = &uh->quant;
+	int data = 0, qindex = 0;
+
+	if (seg->segmentation_enabled &&
+	    (seg->feature_enabled_mask[segmentation_id] & BIT(0))) {
+		data = seg->feature_data[segmentation_id][0];

Maybe you should replace the 0 above by SEG_LVL_ALT_Q to be more explicit. Same goes for BIT(0).

(quoted diff) +2 -0
+static void vdec_av1_slice_setup_lr(struct vdec_av1_slice_lr *lr,
+				    struct

v4l2_av1_loop_restoration *ctrl_lr)

drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c new file +5 -0
+{
+	int i;
+
+	for (i = 0; i < V4L2_AV1_NUM_PLANES_MAX; i++) {
+		lr->frame_restoration_type[i] = ctrl_lr-
Quoted 1 line

frame_restoration_type[i];

(quoted diff) +1 -0
+		lr->loop_restoration_size[i] = ctrl_lr-
Quoted 1 line

loop_restoration_size[i];

drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c new file +4 -0
+	}
+	lr->use_lr = !!lr->frame_restoration_type[0];
+	lr->use_chroma_lr = !!lr->frame_restoration_type[1];
+}
 
 From a first glance, this looks a bit divergent from the spec?
for ( i = 0; i < NumPlanes; i++ ) {
     lr_type
     FrameRestorationType[i] = Remap_Lr_Type[lr_type]
     if ( FrameRestorationType[i] != RESTORE_NONE ) {
         UsesLr = 1
         if ( i > 0 ) {
             usesChromaLr = 1
         }
     }
}

I will include these two variables in the next iteration of the uapi if
computing them in the driver is problematic.

+static void vdec_av1_slice_setup_lf(struct
vdec_av1_slice_loop_filter *lf,

(quoted diff) +1 -0
+				    struct v4l2_av1_loop_filter

*ctrl_lf)

drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c new file +11 -0
+{
+	int i;
+
+	for (i = 0; i < 4; i++)
+		lf->loop_filter_level[i] = ctrl_lf->level[i];
+
+	for (i = 0; i < V4L2_AV1_TOTAL_REFS_PER_FRAME; i++)
+		lf->loop_filter_ref_deltas[i] = ctrl_lf->ref_deltas[i];
+
+	for (i = 0; i < 2; i++)
+		lf->loop_filter_mode_deltas[i] = ctrl_lf-
Quoted 1 line

mode_deltas[i];

drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c new file +4 -0
+
+	lf->loop_filter_sharpness = ctrl_lf->sharpness;
+	lf->loop_filter_delta_enabled =
+		   BIT_FLAG(ctrl_lf,

V4L2_AV1_LOOP_FILTER_FLAG_DELTA_ENABLED);
+}

Maybe ARRAY_SIZE can be of use in the loop indices here?

+static void vdec_av1_slice_setup_cdef(struct vdec_av1_slice_cdef *cdef,

drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c new file +15 -0
+				      struct v4l2_av1_cdef *ctrl_cdef)
+{
+	int i;
+
+	cdef->cdef_damping = ctrl_cdef->damping_minus_3 + 3;
+	cdef->cdef_bits = ctrl_cdef->bits;
+
+	for (i = 0; i < V4L2_AV1_CDEF_MAX; i++) {
+		if (ctrl_cdef->y_sec_strength[i] == 4)
+			ctrl_cdef->y_sec_strength[i] -= 1;
+
+		if (ctrl_cdef->uv_sec_strength[i] == 4)
+			ctrl_cdef->uv_sec_strength[i] -= 1;
+
+		cdef->cdef_y_strength[i] = ctrl_cdef->y_pri_strength[i] 

<< 2 |

(quoted diff) +1 -0
+					   ctrl_cdef-
Quoted 1 line

y_sec_strength[i];

(quoted diff) +1 -0
+		cdef->cdef_uv_strength[i] = ctrl_cdef-
Quoted 1 line

uv_pri_strength[i] << 2 |

(quoted diff) +1 -0
+					    ctrl_cdef-
Quoted 1 line

uv_sec_strength[i];

(quoted diff) +2 -0
+	}
+}

Maybe:

#define SECONDARY_FILTER_STRENGTH_NUM_BITS 2

(quoted diff) +1 -0
+		cdef->cdef_y_strength[i] = ctrl_cdef->y_pri_strength[i] 

<<
SECONDARY_FILTER_STRENGTH_NUM_BITS |

(quoted diff) +1 -0
+					   ctrl_cdef-
Quoted 1 line

y_sec_strength[i];

(quoted diff) +1 -0
+		cdef->cdef_uv_strength[i] = ctrl_cdef-
Quoted 1 line

uv_pri_strength[i] <<

SECONDARY_FILTER_STRENGTH_NUM_BITS |

(quoted diff) +1 -0
+					    ctrl_cdef-
Quoted 1 line

uv_sec_strength[i];

This should make it clearer.

(quoted diff) +2 -0
+		sb_boundary_x_m1 =
+			(tile->mi_col_starts[tile_col + 1] - tile-
Quoted 1 line

mi_col_starts[tile_col] -

1) &

drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c new file +3 -0
+			0x3F;
+		sb_boundary_y_m1 =
+			(tile->mi_row_starts[tile_row + 1] - tile-
Quoted 1 line

mi_row_starts[tile_row] -

1) &

drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c new file +2 -0
+			0x1FF;
+

IIRC there's a preference for lower case hex values in the media subsystem.

+static void vdec_av1_slice_get_dpb_size(struct
vdec_av1_slice_instance
*instance, u32 *dpb_sz)

drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c new file +4 -0
+{
+	/* refer av1 specification */
+	*dpb_sz = 9;
+}

That's actually defined as 8 in the spec, i.e.:

NUM_REF_FRAMES 8 Number of frames that can be stored for future reference.

It's helpful to indicate the section if you reference the specification,
as it makes it easier for the reviewer to cross check.

drivers/media/platform/mediatek/vcodec/vdec/vdec_av1_req_lat_if.c new file +4 -0
+	/* get buffer address from vb2buf */
+	for (i = 0; i < V4L2_AV1_REFS_PER_FRAME; i++) {
+		struct vdec_av1_slice_fb *vref = &vsi->ref[i];
+		int idx = vb2_find_timestamp(vq, pfc->ref_idx[i], 0);
Quoted 2 lines

Needs to be converted to vb2_find_buffer in light of

https://lore.kernel.org/lkml/[email protected]/T/

Quoted 3 lines

-- Daniel