cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

vdec_h264_req_common.c (10577B)


      1// SPDX-License-Identifier: GPL-2.0
      2/*
      3 * Copyright (c) 2022 MediaTek Inc.
      4 * Author: Yunfei Dong <yunfei.dong@mediatek.com>
      5 */
      6
      7#include "vdec_h264_req_common.h"
      8
      9/* get used parameters for sps/pps */
     10#define GET_MTK_VDEC_FLAG(cond, flag) \
     11	{ dst_param->cond = ((src_param->flags & flag) ? (1) : (0)); }
     12#define GET_MTK_VDEC_PARAM(param) \
     13	{ dst_param->param = src_param->param; }
     14
     15void mtk_vdec_h264_get_ref_list(u8 *ref_list,
     16				const struct v4l2_h264_reference *v4l2_ref_list,
     17				int num_valid)
     18{
     19	u32 i;
     20
     21	/*
     22	 * TODO The firmware does not support field decoding. Future
     23	 * implementation must use v4l2_ref_list[i].fields to obtain
     24	 * the reference field parity.
     25	 */
     26
     27	for (i = 0; i < num_valid; i++)
     28		ref_list[i] = v4l2_ref_list[i].index;
     29
     30	/*
     31	 * The firmware expects unused reflist entries to have the value 0x20.
     32	 */
     33	memset(&ref_list[num_valid], 0x20, 32 - num_valid);
     34}
     35
     36void *mtk_vdec_h264_get_ctrl_ptr(struct mtk_vcodec_ctx *ctx, int id)
     37{
     38	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, id);
     39
     40	if (!ctrl)
     41		return ERR_PTR(-EINVAL);
     42
     43	return ctrl->p_cur.p;
     44}
     45
     46void mtk_vdec_h264_fill_dpb_info(struct mtk_vcodec_ctx *ctx,
     47				 struct slice_api_h264_decode_param *decode_params,
     48				 struct mtk_h264_dpb_info *h264_dpb_info)
     49{
     50	const struct slice_h264_dpb_entry *dpb;
     51	struct vb2_queue *vq;
     52	struct vb2_buffer *vb;
     53	struct vb2_v4l2_buffer *vb2_v4l2;
     54	int index, vb2_index;
     55
     56	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
     57
     58	for (index = 0; index < V4L2_H264_NUM_DPB_ENTRIES; index++) {
     59		dpb = &decode_params->dpb[index];
     60		if (!(dpb->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)) {
     61			h264_dpb_info[index].reference_flag = 0;
     62			continue;
     63		}
     64
     65		vb2_index = vb2_find_timestamp(vq, dpb->reference_ts, 0);
     66		if (vb2_index < 0) {
     67			dev_err(&ctx->dev->plat_dev->dev,
     68				"Reference invalid: dpb_index(%d) reference_ts(%lld)",
     69				index, dpb->reference_ts);
     70			continue;
     71		}
     72
     73		/* 1 for short term reference, 2 for long term reference */
     74		if (!(dpb->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM))
     75			h264_dpb_info[index].reference_flag = 1;
     76		else
     77			h264_dpb_info[index].reference_flag = 2;
     78
     79		vb = vq->bufs[vb2_index];
     80		vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
     81		h264_dpb_info[index].field = vb2_v4l2->field;
     82
     83		h264_dpb_info[index].y_dma_addr =
     84			vb2_dma_contig_plane_dma_addr(vb, 0);
     85		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
     86			h264_dpb_info[index].c_dma_addr =
     87				vb2_dma_contig_plane_dma_addr(vb, 1);
     88		else
     89			h264_dpb_info[index].c_dma_addr =
     90				h264_dpb_info[index].y_dma_addr +
     91				ctx->picinfo.fb_sz[0];
     92	}
     93}
     94
     95void mtk_vdec_h264_copy_sps_params(struct mtk_h264_sps_param *dst_param,
     96				   const struct v4l2_ctrl_h264_sps *src_param)
     97{
     98	GET_MTK_VDEC_PARAM(chroma_format_idc);
     99	GET_MTK_VDEC_PARAM(bit_depth_luma_minus8);
    100	GET_MTK_VDEC_PARAM(bit_depth_chroma_minus8);
    101	GET_MTK_VDEC_PARAM(log2_max_frame_num_minus4);
    102	GET_MTK_VDEC_PARAM(pic_order_cnt_type);
    103	GET_MTK_VDEC_PARAM(log2_max_pic_order_cnt_lsb_minus4);
    104	GET_MTK_VDEC_PARAM(max_num_ref_frames);
    105	GET_MTK_VDEC_PARAM(pic_width_in_mbs_minus1);
    106	GET_MTK_VDEC_PARAM(pic_height_in_map_units_minus1);
    107
    108	GET_MTK_VDEC_FLAG(separate_colour_plane_flag,
    109			  V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE);
    110	GET_MTK_VDEC_FLAG(qpprime_y_zero_transform_bypass_flag,
    111			  V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS);
    112	GET_MTK_VDEC_FLAG(delta_pic_order_always_zero_flag,
    113			  V4L2_H264_SPS_FLAG_DELTA_PIC_ORDER_ALWAYS_ZERO);
    114	GET_MTK_VDEC_FLAG(frame_mbs_only_flag,
    115			  V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY);
    116	GET_MTK_VDEC_FLAG(mb_adaptive_frame_field_flag,
    117			  V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD);
    118	GET_MTK_VDEC_FLAG(direct_8x8_inference_flag,
    119			  V4L2_H264_SPS_FLAG_DIRECT_8X8_INFERENCE);
    120}
    121
    122void mtk_vdec_h264_copy_pps_params(struct mtk_h264_pps_param *dst_param,
    123				   const struct v4l2_ctrl_h264_pps *src_param)
    124{
    125	GET_MTK_VDEC_PARAM(num_ref_idx_l0_default_active_minus1);
    126	GET_MTK_VDEC_PARAM(num_ref_idx_l1_default_active_minus1);
    127	GET_MTK_VDEC_PARAM(weighted_bipred_idc);
    128	GET_MTK_VDEC_PARAM(pic_init_qp_minus26);
    129	GET_MTK_VDEC_PARAM(chroma_qp_index_offset);
    130	GET_MTK_VDEC_PARAM(second_chroma_qp_index_offset);
    131
    132	GET_MTK_VDEC_FLAG(entropy_coding_mode_flag,
    133			  V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE);
    134	GET_MTK_VDEC_FLAG(pic_order_present_flag,
    135			  V4L2_H264_PPS_FLAG_BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT);
    136	GET_MTK_VDEC_FLAG(weighted_pred_flag,
    137			  V4L2_H264_PPS_FLAG_WEIGHTED_PRED);
    138	GET_MTK_VDEC_FLAG(deblocking_filter_control_present_flag,
    139			  V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT);
    140	GET_MTK_VDEC_FLAG(constrained_intra_pred_flag,
    141			  V4L2_H264_PPS_FLAG_CONSTRAINED_INTRA_PRED);
    142	GET_MTK_VDEC_FLAG(redundant_pic_cnt_present_flag,
    143			  V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT);
    144	GET_MTK_VDEC_FLAG(transform_8x8_mode_flag,
    145			  V4L2_H264_PPS_FLAG_TRANSFORM_8X8_MODE);
    146	GET_MTK_VDEC_FLAG(scaling_matrix_present_flag,
    147			  V4L2_H264_PPS_FLAG_SCALING_MATRIX_PRESENT);
    148}
    149
    150void mtk_vdec_h264_copy_slice_hd_params(struct mtk_h264_slice_hd_param *dst_param,
    151					const struct v4l2_ctrl_h264_slice_params *src_param,
    152					const struct v4l2_ctrl_h264_decode_params *dec_param)
    153{
    154	int temp;
    155
    156	GET_MTK_VDEC_PARAM(first_mb_in_slice);
    157	GET_MTK_VDEC_PARAM(slice_type);
    158	GET_MTK_VDEC_PARAM(cabac_init_idc);
    159	GET_MTK_VDEC_PARAM(slice_qp_delta);
    160	GET_MTK_VDEC_PARAM(disable_deblocking_filter_idc);
    161	GET_MTK_VDEC_PARAM(slice_alpha_c0_offset_div2);
    162	GET_MTK_VDEC_PARAM(slice_beta_offset_div2);
    163	GET_MTK_VDEC_PARAM(num_ref_idx_l0_active_minus1);
    164	GET_MTK_VDEC_PARAM(num_ref_idx_l1_active_minus1);
    165
    166	dst_param->frame_num = dec_param->frame_num;
    167	dst_param->pic_order_cnt_lsb = dec_param->pic_order_cnt_lsb;
    168
    169	dst_param->delta_pic_order_cnt_bottom =
    170		dec_param->delta_pic_order_cnt_bottom;
    171	dst_param->delta_pic_order_cnt0 =
    172		dec_param->delta_pic_order_cnt0;
    173	dst_param->delta_pic_order_cnt1 =
    174		dec_param->delta_pic_order_cnt1;
    175
    176	temp = dec_param->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC;
    177	dst_param->field_pic_flag = temp ? 1 : 0;
    178
    179	temp = dec_param->flags & V4L2_H264_DECODE_PARAM_FLAG_BOTTOM_FIELD;
    180	dst_param->bottom_field_flag = temp ? 1 : 0;
    181
    182	GET_MTK_VDEC_FLAG(direct_spatial_mv_pred_flag,
    183			  V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED);
    184}
    185
    186void mtk_vdec_h264_copy_scaling_matrix(struct slice_api_h264_scaling_matrix *dst_matrix,
    187				       const struct v4l2_ctrl_h264_scaling_matrix *src_matrix)
    188{
    189	memcpy(dst_matrix->scaling_list_4x4, src_matrix->scaling_list_4x4,
    190	       sizeof(dst_matrix->scaling_list_4x4));
    191
    192	memcpy(dst_matrix->scaling_list_8x8, src_matrix->scaling_list_8x8,
    193	       sizeof(dst_matrix->scaling_list_8x8));
    194}
    195
    196void
    197mtk_vdec_h264_copy_decode_params(struct slice_api_h264_decode_param *dst_params,
    198				 const struct v4l2_ctrl_h264_decode_params *src_params,
    199				 const struct v4l2_h264_dpb_entry dpb[V4L2_H264_NUM_DPB_ENTRIES])
    200{
    201	struct slice_h264_dpb_entry *dst_entry;
    202	const struct v4l2_h264_dpb_entry *src_entry;
    203	int i;
    204
    205	for (i = 0; i < ARRAY_SIZE(dst_params->dpb); i++) {
    206		dst_entry = &dst_params->dpb[i];
    207		src_entry = &dpb[i];
    208
    209		dst_entry->reference_ts = src_entry->reference_ts;
    210		dst_entry->frame_num = src_entry->frame_num;
    211		dst_entry->pic_num = src_entry->pic_num;
    212		dst_entry->top_field_order_cnt = src_entry->top_field_order_cnt;
    213		dst_entry->bottom_field_order_cnt =
    214			src_entry->bottom_field_order_cnt;
    215		dst_entry->flags = src_entry->flags;
    216	}
    217
    218	/* num_slices is a leftover from the old H.264 support and is ignored
    219	 * by the firmware.
    220	 */
    221	dst_params->num_slices = 0;
    222	dst_params->nal_ref_idc = src_params->nal_ref_idc;
    223	dst_params->top_field_order_cnt = src_params->top_field_order_cnt;
    224	dst_params->bottom_field_order_cnt = src_params->bottom_field_order_cnt;
    225	dst_params->flags = src_params->flags;
    226}
    227
    228static bool mtk_vdec_h264_dpb_entry_match(const struct v4l2_h264_dpb_entry *a,
    229					  const struct v4l2_h264_dpb_entry *b)
    230{
    231	return a->top_field_order_cnt == b->top_field_order_cnt &&
    232	       a->bottom_field_order_cnt == b->bottom_field_order_cnt;
    233}
    234
    235/*
    236 * Move DPB entries of dec_param that refer to a frame already existing in dpb
    237 * into the already existing slot in dpb, and move other entries into new slots.
    238 *
    239 * This function is an adaptation of the similarly-named function in
    240 * hantro_h264.c.
    241 */
    242void mtk_vdec_h264_update_dpb(const struct v4l2_ctrl_h264_decode_params *dec_param,
    243			      struct v4l2_h264_dpb_entry *dpb)
    244{
    245	DECLARE_BITMAP(new, ARRAY_SIZE(dec_param->dpb)) = { 0, };
    246	DECLARE_BITMAP(in_use, ARRAY_SIZE(dec_param->dpb)) = { 0, };
    247	DECLARE_BITMAP(used, ARRAY_SIZE(dec_param->dpb)) = { 0, };
    248	unsigned int i, j;
    249
    250	/* Disable all entries by default, and mark the ones in use. */
    251	for (i = 0; i < ARRAY_SIZE(dec_param->dpb); i++) {
    252		if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
    253			set_bit(i, in_use);
    254		dpb[i].flags &= ~V4L2_H264_DPB_ENTRY_FLAG_ACTIVE;
    255	}
    256
    257	/* Try to match new DPB entries with existing ones by their POCs. */
    258	for (i = 0; i < ARRAY_SIZE(dec_param->dpb); i++) {
    259		const struct v4l2_h264_dpb_entry *ndpb = &dec_param->dpb[i];
    260
    261		if (!(ndpb->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
    262			continue;
    263
    264		/*
    265		 * To cut off some comparisons, iterate only on target DPB
    266		 * entries were already used.
    267		 */
    268		for_each_set_bit(j, in_use, ARRAY_SIZE(dec_param->dpb)) {
    269			struct v4l2_h264_dpb_entry *cdpb;
    270
    271			cdpb = &dpb[j];
    272			if (!mtk_vdec_h264_dpb_entry_match(cdpb, ndpb))
    273				continue;
    274
    275			*cdpb = *ndpb;
    276			set_bit(j, used);
    277			/* Don't reiterate on this one. */
    278			clear_bit(j, in_use);
    279			break;
    280		}
    281
    282		if (j == ARRAY_SIZE(dec_param->dpb))
    283			set_bit(i, new);
    284	}
    285
    286	/* For entries that could not be matched, use remaining free slots. */
    287	for_each_set_bit(i, new, ARRAY_SIZE(dec_param->dpb)) {
    288		const struct v4l2_h264_dpb_entry *ndpb = &dec_param->dpb[i];
    289		struct v4l2_h264_dpb_entry *cdpb;
    290
    291		/*
    292		 * Both arrays are of the same sizes, so there is no way
    293		 * we can end up with no space in target array, unless
    294		 * something is buggy.
    295		 */
    296		j = find_first_zero_bit(used, ARRAY_SIZE(dec_param->dpb));
    297		if (WARN_ON(j >= ARRAY_SIZE(dec_param->dpb)))
    298			return;
    299
    300		cdpb = &dpb[j];
    301		*cdpb = *ndpb;
    302		set_bit(j, used);
    303	}
    304}
    305
    306unsigned int mtk_vdec_h264_get_mv_buf_size(unsigned int width, unsigned int height)
    307{
    308	int unit_size = (width / MB_UNIT_LEN) * (height / MB_UNIT_LEN) + 8;
    309
    310	return HW_MB_STORE_SZ * unit_size;
    311}
    312
    313int mtk_vdec_h264_find_start_code(unsigned char *data, unsigned int data_sz)
    314{
    315	if (data_sz > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
    316		return 3;
    317
    318	if (data_sz > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
    319	    data[3] == 1)
    320		return 4;
    321
    322	return -1;
    323}