1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef _ROCKCHIP_AV1_ENTROPYMODE_H_
4 #define _ROCKCHIP_AV1_ENTROPYMODE_H_
5 
6 #include <linux/types.h>
7 
8 struct hantro_ctx;
9 
10 #define AV1_INTER_MODE_CONTEXTS 15
11 #define AV1_INTRA_MODES 13
12 #define AV1_REF_CONTEXTS 3
13 #define AV1_SWITCHABLE_FILTERS 3	/* number of switchable filters */
14 #define AV1_TX_SIZE_CONTEXTS 3
15 #define BLOCK_SIZE_GROUPS 4
16 #define BR_CDF_SIZE 4
17 #define BWD_REFS 3
18 #define CFL_ALLOWED_TYPES 2
19 #define CFL_ALPHA_CONTEXTS 6
20 #define CFL_ALPHABET_SIZE 16
21 #define CFL_JOINT_SIGNS 8
22 #define CDF_SIZE(x) ((x) - 1)
23 #define COMP_GROUP_IDX_CONTEXTS 7
24 #define COMP_INDEX_CONTEXTS 6
25 #define COMP_INTER_CONTEXTS 5
26 #define COMP_REF_TYPE_CONTEXTS 5
27 #define COMPOUND_TYPES 3
28 #define DC_SIGN_CONTEXTS 3
29 #define DELTA_LF_PROBS 3
30 #define DELTA_Q_PROBS 3
31 #define DIRECTIONAL_MODES 8
32 #define DRL_MODE_CONTEXTS 3
33 #define EOB_COEF_CONTEXTS 9
34 #define EXT_TX_SIZES 3
35 #define EXT_TX_TYPES 16
36 #define EXTTX_SIZES 4
37 #define FRAME_LF_COUNT 4
38 #define FWD_REFS 4
39 #define GLOBALMV_MODE_CONTEXTS 2
40 #define ICDF(x) (32768U - (x))
41 #define INTER_COMPOUND_MODES 8
42 #define INTERINTRA_MODES 4
43 #define INTRA_INTER_CONTEXTS 4
44 #define KF_MODE_CONTEXTS 5
45 #define LEVEL_CONTEXTS 21
46 #define MAX_ANGLE_DELTA 3
47 #define MAX_MB_SEGMENTS 8
48 #define MAX_SEGMENTS 8
49 #define MAX_TX_CATS 4
50 #define MAX_TX_DEPTH 2
51 #define MBSKIP_CONTEXTS 3
52 #define MOTION_MODES 3
53 #define MOTION_MODE_CONTEXTS 10
54 #define NEWMV_MODE_CONTEXTS 6
55 #define NUM_BASE_LEVELS 2
56 #define NUM_REF_FRAMES 8
57 #define PALETTE_BLOCK_SIZES 7
58 #define PALETTE_IDX_CONTEXTS 18
59 #define PALETTE_SIZES 7
60 #define PALETTE_UV_MODE_CONTEXTS 2
61 #define PALETTE_Y_MODE_CONTEXTS 3
62 #define PARTITION_PLOFFSET 4
63 #define NUM_PARTITION_CONTEXTS (4 * PARTITION_PLOFFSET)
64 #define PLANE_TYPES 2
65 #define PREDICTION_PROBS 3
66 #define REF_CONTEXTS 5
67 #define REFMV_MODE_CONTEXTS 9
68 #define SEG_TEMPORAL_PRED_CTXS 3
69 #define SIG_COEF_CONTEXTS 42
70 #define SIG_COEF_CONTEXTS_EOB 4
71 #define SINGLE_REFS 7
72 #define SKIP_CONTEXTS 3
73 #define SKIP_MODE_CONTEXTS 3
74 #define SPATIAL_PREDICTION_PROBS 3
75 #define SWITCHABLE_FILTER_CONTEXTS ((AV1_SWITCHABLE_FILTERS + 1) * 4)
76 #define TOKEN_CDF_Q_CTXS 4
77 #define TX_SIZES 5
78 #define TX_SIZE_CONTEXTS 2
79 #define TX_TYPES 4
80 #define TXB_SKIP_CONTEXTS 13
81 #define TXFM_PARTITION_CONTEXTS 22
82 #define UNI_COMP_REF_CONTEXTS 3
83 #define UNIDIR_COMP_REFS 4
84 #define UV_INTRA_MODES 14
85 #define VARTX_PART_CONTEXTS 22
86 #define ZEROMV_MODE_CONTEXTS 2
87 
88 enum blocksizetype {
89 	BLOCK_SIZE_AB4X4,
90 	BLOCK_SIZE_SB4X8,
91 	BLOCK_SIZE_SB8X4,
92 	BLOCK_SIZE_SB8X8,
93 	BLOCK_SIZE_SB8X16,
94 	BLOCK_SIZE_SB16X8,
95 	BLOCK_SIZE_MB16X16,
96 	BLOCK_SIZE_SB16X32,
97 	BLOCK_SIZE_SB32X16,
98 	BLOCK_SIZE_SB32X32,
99 	BLOCK_SIZE_SB32X64,
100 	BLOCK_SIZE_SB64X32,
101 	BLOCK_SIZE_SB64X64,
102 	BLOCK_SIZE_SB64X128,
103 	BLOCK_SIZE_SB128X64,
104 	BLOCK_SIZE_SB128X128,
105 	BLOCK_SIZE_SB4X16,
106 	BLOCK_SIZE_SB16X4,
107 	BLOCK_SIZE_SB8X32,
108 	BLOCK_SIZE_SB32X8,
109 	BLOCK_SIZE_SB16X64,
110 	BLOCK_SIZE_SB64X16,
111 	BLOCK_SIZE_TYPES,
112 	BLOCK_SIZES_ALL = BLOCK_SIZE_TYPES
113 };
114 
115 enum filterintramodetype {
116 	FILTER_DC_PRED,
117 	FILTER_V_PRED,
118 	FILTER_H_PRED,
119 	FILTER_D153_PRED,
120 	FILTER_PAETH_PRED,
121 	FILTER_INTRA_MODES,
122 	FILTER_INTRA_UNUSED = 7
123 };
124 
125 enum frametype {
126 	KEY_FRAME = 0,
127 	INTER_FRAME = 1,
128 	NUM_FRAME_TYPES,
129 };
130 
131 enum txsize {
132 	TX_4X4 = 0,
133 	TX_8X8 = 1,
134 	TX_16X16 = 2,
135 	TX_32X32 = 3,
136 	TX_SIZE_MAX_SB,
137 };
138 
139 enum { SIMPLE_TRANSLATION, OBMC_CAUSAL, MOTION_MODE_COUNT };
140 
141 enum mb_prediction_mode {
142 	DC_PRED,		/* average of above and left pixels */
143 	V_PRED,			/* vertical prediction */
144 	H_PRED,			/* horizontal prediction */
145 	D45_PRED,		/* Directional 45 deg prediction  [anti-clockwise from 0 deg hor] */
146 	D135_PRED,		/* Directional 135 deg prediction [anti-clockwise from 0 deg hor] */
147 	D117_PRED,		/* Directional 112 deg prediction [anti-clockwise from 0 deg hor] */
148 	D153_PRED,		/* Directional 157 deg prediction [anti-clockwise from 0 deg hor] */
149 	D27_PRED,		/* Directional 22 deg prediction  [anti-clockwise from 0 deg hor] */
150 	D63_PRED,		/* Directional 67 deg prediction  [anti-clockwise from 0 deg hor] */
151 	SMOOTH_PRED,
152 	TM_PRED_AV1 = SMOOTH_PRED,
153 	SMOOTH_V_PRED,		// Vertical interpolation
154 	SMOOTH_H_PRED,		// Horizontal interpolation
155 	TM_PRED,		/* Truemotion prediction */
156 	PAETH_PRED = TM_PRED,
157 	NEARESTMV,
158 	NEARMV,
159 	ZEROMV,
160 	NEWMV,
161 	NEAREST_NEARESTMV,
162 	NEAR_NEARMV,
163 	NEAREST_NEWMV,
164 	NEW_NEARESTMV,
165 	NEAR_NEWMV,
166 	NEW_NEARMV,
167 	ZERO_ZEROMV,
168 	NEW_NEWMV,
169 	SPLITMV,
170 	MB_MODE_COUNT
171 };
172 
173 enum partitiontype {
174 	PARTITION_NONE,
175 	PARTITION_HORZ,
176 	PARTITION_VERT,
177 	PARTITION_SPLIT,
178 	PARTITION_TYPES
179 };
180 
181 struct mvcdfs {
182 	u16 joint_cdf[3];
183 	u16 sign_cdf[2];
184 	u16 clsss_cdf[2][10];
185 	u16 clsss0_fp_cdf[2][2][3];
186 	u16 fp_cdf[2][3];
187 	u16 class0_hp_cdf[2];
188 	u16 hp_cdf[2];
189 	u16 class0_cdf[2];
190 	u16 bits_cdf[2][10];
191 };
192 
193 struct av1cdfs {
194 	u16 partition_cdf[13][16];
195 	u16 kf_ymode_cdf[KF_MODE_CONTEXTS][KF_MODE_CONTEXTS][AV1_INTRA_MODES - 1];
196 	u16 segment_pred_cdf[PREDICTION_PROBS];
197 	u16 spatial_pred_seg_tree_cdf[SPATIAL_PREDICTION_PROBS][MAX_MB_SEGMENTS - 1];
198 	u16 mbskip_cdf[MBSKIP_CONTEXTS];
199 	u16 delta_q_cdf[DELTA_Q_PROBS];
200 	u16 delta_lf_multi_cdf[FRAME_LF_COUNT][DELTA_LF_PROBS];
201 	u16 delta_lf_cdf[DELTA_LF_PROBS];
202 	u16 skip_mode_cdf[SKIP_MODE_CONTEXTS];
203 	u16 vartx_part_cdf[VARTX_PART_CONTEXTS][1];
204 	u16 tx_size_cdf[MAX_TX_CATS][AV1_TX_SIZE_CONTEXTS][MAX_TX_DEPTH];
205 	u16 if_ymode_cdf[BLOCK_SIZE_GROUPS][AV1_INTRA_MODES - 1];
206 	u16 uv_mode_cdf[2][AV1_INTRA_MODES][AV1_INTRA_MODES - 1 + 1];
207 	u16 intra_inter_cdf[INTRA_INTER_CONTEXTS];
208 	u16 comp_inter_cdf[COMP_INTER_CONTEXTS];
209 	u16 single_ref_cdf[AV1_REF_CONTEXTS][SINGLE_REFS - 1];
210 	u16 comp_ref_type_cdf[COMP_REF_TYPE_CONTEXTS][1];
211 	u16 uni_comp_ref_cdf[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1][1];
212 	u16 comp_ref_cdf[AV1_REF_CONTEXTS][FWD_REFS - 1];
213 	u16 comp_bwdref_cdf[AV1_REF_CONTEXTS][BWD_REFS - 1];
214 	u16 newmv_cdf[NEWMV_MODE_CONTEXTS];
215 	u16 zeromv_cdf[ZEROMV_MODE_CONTEXTS];
216 	u16 refmv_cdf[REFMV_MODE_CONTEXTS];
217 	u16 drl_cdf[DRL_MODE_CONTEXTS];
218 	u16 interp_filter_cdf[SWITCHABLE_FILTER_CONTEXTS][AV1_SWITCHABLE_FILTERS - 1];
219 	struct mvcdfs mv_cdf;
220 	u16 obmc_cdf[BLOCK_SIZE_TYPES];
221 	u16 motion_mode_cdf[BLOCK_SIZE_TYPES][2];
222 	u16 inter_compound_mode_cdf[AV1_INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES - 1];
223 	u16 compound_type_cdf[BLOCK_SIZE_TYPES][CDF_SIZE(COMPOUND_TYPES - 1)];
224 	u16 interintra_cdf[BLOCK_SIZE_GROUPS];
225 	u16 interintra_mode_cdf[BLOCK_SIZE_GROUPS][INTERINTRA_MODES - 1];
226 	u16 wedge_interintra_cdf[BLOCK_SIZE_TYPES];
227 	u16 wedge_idx_cdf[BLOCK_SIZE_TYPES][CDF_SIZE(16)];
228 	u16 palette_y_mode_cdf[PALETTE_BLOCK_SIZES][PALETTE_Y_MODE_CONTEXTS][1];
229 	u16 palette_uv_mode_cdf[PALETTE_UV_MODE_CONTEXTS][1];
230 	u16 palette_y_size_cdf[PALETTE_BLOCK_SIZES][PALETTE_SIZES - 1];
231 	u16 palette_uv_size_cdf[PALETTE_BLOCK_SIZES][PALETTE_SIZES - 1];
232 	u16 cfl_sign_cdf[CFL_JOINT_SIGNS - 1];
233 	u16 cfl_alpha_cdf[CFL_ALPHA_CONTEXTS][CFL_ALPHABET_SIZE - 1];
234 	u16 intrabc_cdf[1];
235 	u16 angle_delta_cdf[DIRECTIONAL_MODES][6];
236 	u16 filter_intra_mode_cdf[FILTER_INTRA_MODES - 1];
237 	u16 filter_intra_cdf[BLOCK_SIZES_ALL];
238 	u16 comp_group_idx_cdf[COMP_GROUP_IDX_CONTEXTS][CDF_SIZE(2)];
239 	u16 compound_idx_cdf[COMP_INDEX_CONTEXTS][CDF_SIZE(2)];
240 	u16 dummy0[14];
241 	// Palette index contexts; sizes 1/7, 2/6, 3/5 packed together
242 	u16 palette_y_color_index_cdf[PALETTE_IDX_CONTEXTS][8];
243 	u16 palette_uv_color_index_cdf[PALETTE_IDX_CONTEXTS][8];
244 	u16 tx_type_intra0_cdf[EXTTX_SIZES][AV1_INTRA_MODES][8];
245 	u16 tx_type_intra1_cdf[EXTTX_SIZES][AV1_INTRA_MODES][4];
246 	u16 tx_type_inter_cdf[2][EXTTX_SIZES][EXT_TX_TYPES];
247 	u16 txb_skip_cdf[TX_SIZES][TXB_SKIP_CONTEXTS][CDF_SIZE(2)];
248 	u16 eob_extra_cdf[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS][CDF_SIZE(2)];
249 	u16 dummy1[5];
250 	u16 eob_flag_cdf16[PLANE_TYPES][2][4];
251 	u16 eob_flag_cdf32[PLANE_TYPES][2][8];
252 	u16 eob_flag_cdf64[PLANE_TYPES][2][8];
253 	u16 eob_flag_cdf128[PLANE_TYPES][2][8];
254 	u16 eob_flag_cdf256[PLANE_TYPES][2][8];
255 	u16 eob_flag_cdf512[PLANE_TYPES][2][16];
256 	u16 eob_flag_cdf1024[PLANE_TYPES][2][16];
257 	u16 coeff_base_eob_cdf[TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS_EOB][CDF_SIZE(3)];
258 	u16 coeff_base_cdf[TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS][CDF_SIZE(4) + 1];
259 	u16 dc_sign_cdf[PLANE_TYPES][DC_SIGN_CONTEXTS][CDF_SIZE(2)];
260 	u16 dummy2[2];
261 	u16 coeff_br_cdf[TX_SIZES][PLANE_TYPES][LEVEL_CONTEXTS][CDF_SIZE(BR_CDF_SIZE) + 1];
262 	u16 dummy3[16];
263 };
264 
265 void rockchip_av1_store_cdfs(struct hantro_ctx *ctx,
266 			     u32 refresh_frame_flags);
267 void rockchip_av1_get_cdfs(struct hantro_ctx *ctx, u32 ref_idx);
268 void rockchip_av1_set_default_cdfs(struct av1cdfs *cdfs,
269 				   struct mvcdfs *cdfs_ndvc);
270 void rockchip_av1_default_coeff_probs(u32 base_qindex, void *ptr);
271 
272 #endif /* _ROCKCHIP_AV1_ENTROPYMODE_H_ */
273