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#ifndef _VDEC_H264_REQ_COMMON_H_
8#define _VDEC_H264_REQ_COMMON_H_
9
10#include <linux/module.h>
11#include <linux/slab.h>
12#include <media/v4l2-h264.h>
13#include <media/v4l2-mem2mem.h>
14#include <media/videobuf2-dma-contig.h>
15
16#include "../mtk_vcodec_dec_drv.h"
17
18#define NAL_NON_IDR_SLICE			0x01
19#define NAL_IDR_SLICE				0x05
20#define NAL_TYPE(value)				((value) & 0x1F)
21
22#define BUF_PREDICTION_SZ			(64 * 4096)
23#define MB_UNIT_LEN				16
24
25/* motion vector size (bytes) for every macro block */
26#define HW_MB_STORE_SZ				64
27
28#define H264_MAX_MV_NUM				32
29
30/**
31 * struct mtk_h264_dpb_info  - h264 dpb information
32 *
33 * @y_dma_addr:	Y bitstream physical address
34 * @c_dma_addr:	CbCr bitstream physical address
35 * @reference_flag:	reference picture flag (short/long term reference picture)
36 * @field:		field picture flag
37 */
38struct mtk_h264_dpb_info {
39	dma_addr_t y_dma_addr;
40	dma_addr_t c_dma_addr;
41	int reference_flag;
42	int field;
43};
44
45/*
46 * struct mtk_h264_sps_param  - parameters for sps
47 */
48struct mtk_h264_sps_param {
49	unsigned char chroma_format_idc;
50	unsigned char bit_depth_luma_minus8;
51	unsigned char bit_depth_chroma_minus8;
52	unsigned char log2_max_frame_num_minus4;
53	unsigned char pic_order_cnt_type;
54	unsigned char log2_max_pic_order_cnt_lsb_minus4;
55	unsigned char max_num_ref_frames;
56	unsigned char separate_colour_plane_flag;
57	unsigned short pic_width_in_mbs_minus1;
58	unsigned short pic_height_in_map_units_minus1;
59	unsigned int max_frame_nums;
60	unsigned char qpprime_y_zero_transform_bypass_flag;
61	unsigned char delta_pic_order_always_zero_flag;
62	unsigned char frame_mbs_only_flag;
63	unsigned char mb_adaptive_frame_field_flag;
64	unsigned char direct_8x8_inference_flag;
65	unsigned char reserved[3];
66};
67
68/*
69 * struct mtk_h264_pps_param  - parameters for pps
70 */
71struct mtk_h264_pps_param {
72	unsigned char num_ref_idx_l0_default_active_minus1;
73	unsigned char num_ref_idx_l1_default_active_minus1;
74	unsigned char weighted_bipred_idc;
75	char pic_init_qp_minus26;
76	char chroma_qp_index_offset;
77	char second_chroma_qp_index_offset;
78	unsigned char entropy_coding_mode_flag;
79	unsigned char pic_order_present_flag;
80	unsigned char deblocking_filter_control_present_flag;
81	unsigned char constrained_intra_pred_flag;
82	unsigned char weighted_pred_flag;
83	unsigned char redundant_pic_cnt_present_flag;
84	unsigned char transform_8x8_mode_flag;
85	unsigned char scaling_matrix_present_flag;
86	unsigned char reserved[2];
87};
88
89/*
90 * struct mtk_h264_slice_hd_param  - parameters for slice header
91 */
92struct mtk_h264_slice_hd_param {
93	unsigned int first_mb_in_slice;
94	unsigned int field_pic_flag;
95	unsigned int slice_type;
96	unsigned int frame_num;
97	int pic_order_cnt_lsb;
98	int delta_pic_order_cnt_bottom;
99	unsigned int bottom_field_flag;
100	unsigned int direct_spatial_mv_pred_flag;
101	int delta_pic_order_cnt0;
102	int delta_pic_order_cnt1;
103	unsigned int cabac_init_idc;
104	int slice_qp_delta;
105	unsigned int disable_deblocking_filter_idc;
106	int slice_alpha_c0_offset_div2;
107	int slice_beta_offset_div2;
108	unsigned int num_ref_idx_l0_active_minus1;
109	unsigned int num_ref_idx_l1_active_minus1;
110	unsigned int reserved;
111};
112
113/*
114 * struct slice_api_h264_scaling_matrix  - parameters for scaling list
115 */
116struct slice_api_h264_scaling_matrix {
117	unsigned char scaling_list_4x4[6][16];
118	unsigned char scaling_list_8x8[6][64];
119};
120
121/*
122 * struct slice_h264_dpb_entry  - each dpb information
123 */
124struct slice_h264_dpb_entry {
125	unsigned long long reference_ts;
126	unsigned short frame_num;
127	unsigned short pic_num;
128	/* Note that field is indicated by v4l2_buffer.field */
129	int top_field_order_cnt;
130	int bottom_field_order_cnt;
131	unsigned int flags;
132};
133
134/*
135 * struct slice_api_h264_decode_param - parameters for decode.
136 */
137struct slice_api_h264_decode_param {
138	struct slice_h264_dpb_entry dpb[V4L2_H264_NUM_DPB_ENTRIES];
139	unsigned short num_slices;
140	unsigned short nal_ref_idc;
141	unsigned char ref_pic_list_p0[32];
142	unsigned char ref_pic_list_b0[32];
143	unsigned char ref_pic_list_b1[32];
144	int top_field_order_cnt;
145	int bottom_field_order_cnt;
146	unsigned int flags;
147};
148
149/**
150 * struct h264_fb - h264 decode frame buffer information
151 *
152 * @vdec_fb_va:	virtual address of struct vdec_fb
153 * @y_fb_dma:		dma address of Y frame buffer (luma)
154 * @c_fb_dma:		dma address of C frame buffer (chroma)
155 * @poc:		picture order count of frame buffer
156 * @reserved:		for 8 bytes alignment
157 */
158struct h264_fb {
159	u64 vdec_fb_va;
160	u64 y_fb_dma;
161	u64 c_fb_dma;
162	s32 poc;
163	u32 reserved;
164};
165
166/**
167 * mtk_vdec_h264_get_ref_list - translate V4L2 reference list
168 *
169 * @ref_list:		Mediatek reference picture list
170 * @v4l2_ref_list:	V4L2 reference picture list
171 * @num_valid:		used reference number
172 */
173void mtk_vdec_h264_get_ref_list(u8 *ref_list,
174				const struct v4l2_h264_reference *v4l2_ref_list,
175				int num_valid);
176
177/**
178 * mtk_vdec_h264_get_ctrl_ptr - get each CID contrl address.
179 *
180 * @ctx:	v4l2 ctx
181 * @id:	CID control ID
182 *
183 * Return: returns CID ctrl address.
184 */
185void *mtk_vdec_h264_get_ctrl_ptr(struct mtk_vcodec_dec_ctx *ctx, int id);
186
187/**
188 * mtk_vdec_h264_fill_dpb_info - get each CID contrl address.
189 *
190 * @ctx:		v4l2 ctx
191 * @decode_params:	slice decode params
192 * @h264_dpb_info:	dpb buffer information
193 */
194void mtk_vdec_h264_fill_dpb_info(struct mtk_vcodec_dec_ctx *ctx,
195				 struct slice_api_h264_decode_param *decode_params,
196				 struct mtk_h264_dpb_info *h264_dpb_info);
197
198/**
199 * mtk_vdec_h264_copy_sps_params - get sps params.
200 *
201 * @dst_param:	sps params for hw decoder
202 * @src_param:	sps params from user driver
203 */
204void mtk_vdec_h264_copy_sps_params(struct mtk_h264_sps_param *dst_param,
205				   const struct v4l2_ctrl_h264_sps *src_param);
206
207/**
208 * mtk_vdec_h264_copy_pps_params - get pps params.
209 *
210 * @dst_param:	pps params for hw decoder
211 * @src_param:	pps params from user driver
212 */
213void mtk_vdec_h264_copy_pps_params(struct mtk_h264_pps_param *dst_param,
214				   const struct v4l2_ctrl_h264_pps *src_param);
215
216/**
217 * mtk_vdec_h264_copy_slice_hd_params - get slice header params.
218 *
219 * @dst_param:	slice params for hw decoder
220 * @src_param:	slice params from user driver
221 * @dec_param:	decode params from user driver
222 */
223void mtk_vdec_h264_copy_slice_hd_params(struct mtk_h264_slice_hd_param *dst_param,
224					const struct v4l2_ctrl_h264_slice_params *src_param,
225					const struct v4l2_ctrl_h264_decode_params *dec_param);
226
227/**
228 * mtk_vdec_h264_copy_scaling_matrix - get each CID contrl address.
229 *
230 * @dst_matrix:	scaling list params for hw decoder
231 * @src_matrix:	scaling list params from user driver
232 */
233void mtk_vdec_h264_copy_scaling_matrix(struct slice_api_h264_scaling_matrix *dst_matrix,
234				       const struct v4l2_ctrl_h264_scaling_matrix *src_matrix);
235
236/**
237 * mtk_vdec_h264_copy_decode_params - get decode params.
238 *
239 * @dst_params:	dst params for hw decoder
240 * @src_params:	decode params from user driver
241 * @dpb:		dpb information
242 */
243void
244mtk_vdec_h264_copy_decode_params(struct slice_api_h264_decode_param *dst_params,
245				 const struct v4l2_ctrl_h264_decode_params *src_params,
246				 const struct v4l2_h264_dpb_entry dpb[V4L2_H264_NUM_DPB_ENTRIES]);
247
248/**
249 * mtk_vdec_h264_update_dpb - updata dpb list.
250 *
251 * @dec_param:	v4l2 control decode params
252 * @dpb:	dpb entry informaton
253 */
254void mtk_vdec_h264_update_dpb(const struct v4l2_ctrl_h264_decode_params *dec_param,
255			      struct v4l2_h264_dpb_entry *dpb);
256
257/**
258 * mtk_vdec_h264_find_start_code - find h264 start code using sofeware.
259 *
260 * @data:	input buffer address
261 * @data_sz:	input buffer size
262 *
263 * Return: returns start code position.
264 */
265int mtk_vdec_h264_find_start_code(unsigned char *data, unsigned int data_sz);
266
267/**
268 * mtk_vdec_h264_get_mv_buf_size - get mv buffer size.
269 *
270 * @width:	picture width
271 * @height:	picture height
272 *
273 * Return: returns mv buffer size.
274 */
275unsigned int mtk_vdec_h264_get_mv_buf_size(unsigned int width, unsigned int height);
276
277#endif
278