xref: /OK3568_Linux_fs/external/mpp/mpp/hal/rkenc/h265e/hal_h265e_vepu580_tune.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2021 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "vepu580_tune.h"
18 
19 #define HAL_H265E_DBG_CONTENT           (0x00200000)
20 #define hal_h264e_dbg_content(fmt, ...) hal_h264e_dbg_f(HAL_H264E_DBG_CONTENT, fmt, ## __VA_ARGS__)
21 
22 /*
23  * Please follow the configuration below:
24  *
25  * FRAME_CONTENT_ANALYSIS_NUM >= 5
26  * MD_WIN_LEN >= 3
27  * MD_SHOW_LEN == 4
28  */
29 
30 typedef struct HalH265eVepu580Tune_t {
31     H265eV580HalContext  *ctx;
32 
33     /* motion and texture statistic of previous frames */
34     RK_S32  curr_scene_motion_flag;
35     /* motion and texture statistic of previous frames */
36     RK_S32  ap_motion_flag;
37     // level: 0~2: 0 <--> static, 1 <-->medium motion, 2 <--> large motion
38     RK_S32  md_madp[MD_WIN_LEN];
39     // level: 0~2: 0 <--> simple texture, 1 <--> medium texture, 2 <--> complex texture
40     RK_S32  txtr_madi[FRAME_CONTENT_ANALYSIS_NUM];
41     RK_S32  scene_motion_flag_matrix[FRAME_MOTION_ANALYSIS_NUM];
42     RK_S32  md_flag_matrix[MD_SHOW_LEN];
43 
44     RK_S32  pre_madp[2];
45     RK_S32  pre_madi[2];
46 } HalH265eVepu580Tune;
47 
48 static RK_S32 ctu_avg_madp_thd[6] = {896, 640, 384, 896, 640, 384};
49 
50 static RK_U8 lvl32_preintra_cst_wgt[4][8] = {
51     {21, 20, 19, 18, 20, 22, 24, 16},
52     {19, 18, 17, 16, 18, 20, 21, 16},
53     {20, 19, 18, 17, 19, 21, 22, 16},
54     {16, 16, 16, 16, 16, 16, 16, 16},
55 };
56 
57 static RK_U8 lvl16_preintra_cst_wgt[4][8] = {
58     {21, 20, 19, 18, 20, 22, 24, 16},
59     {19, 18, 17, 16, 18, 20, 21, 16},
60     {20, 19, 18, 17, 19, 21, 22, 16},
61     {16, 16, 16, 16, 16, 16, 16, 16},
62 };
63 
64 static RK_S32 skip_b64_atf_wgt[4][13] = {
65     {16, 13, 14, 15, 14, 14, 15, 15, 15, 16, 16, 16, 16},
66     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
67     {16, 14, 15, 16, 14, 14, 15, 15, 15, 16, 16, 16, 16},
68     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
69 };
70 
71 static RK_S32 intra_b32_atf_wgt[4][12] = {
72     {24, 23, 23, 23, 22, 21, 20, 19, 18, 16, 16, 16},
73     {21, 20, 19, 20, 19, 18, 19, 18, 18, 18, 18, 17},
74     {20, 19, 18, 19, 18, 17, 18, 17, 17, 16, 16, 16},
75     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
76 };
77 
78 static RK_S32 skip_b32_atf_wgt[4][13] = {
79     {16, 13, 14, 15, 14, 14, 15, 15, 15, 16, 16, 16, 16},
80     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
81     {18, 14, 14, 15, 14, 14, 15, 15, 15, 16, 16, 16, 16},
82     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
83 };
84 
85 static RK_S32 intra_b16_atf_wgt[4][12] = {
86     {24, 23, 23, 23, 22, 21, 20, 19, 18, 16, 16, 16},
87     {21, 20, 19, 20, 19, 18, 19, 18, 18, 18, 18, 17},
88     {20, 19, 18, 19, 18, 17, 18, 17, 17, 16, 16, 16},
89     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
90 };
91 
92 static RK_S32 skip_b16_atf_wgt[4][13] = {
93     {16, 13, 14, 15, 14, 14, 15, 15, 15, 16, 16, 16, 16},
94     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
95     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
96     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
97 };
98 
99 static RK_S32 intra_b8_atf_wgt[4][12] = {
100     {24, 23, 23, 23, 22, 21, 20, 19, 18, 16, 16, 16},
101     {21, 20, 19, 20, 19, 18, 19, 18, 18, 18, 18, 17},
102     {20, 19, 18, 19, 18, 17, 18, 17, 17, 16, 16, 16},
103     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
104 };
105 
106 static RK_S32 skip_b8_atf_wgt[4][13] = {
107     {16, 13, 14, 15, 14, 14, 15, 15, 15, 16, 16, 16, 16},
108     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
109     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
110     {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
111 };
112 
113 static RK_U32 intra_lvl16_sobel_a[4][9] = {
114     {32, 32, 32, 32, 32, 32, 32, 32, 32},
115     {16, 16, 16, 16, 16, 16, 16, 16, 16},
116     {16, 16, 16, 16, 16, 16, 16, 16, 16},
117     {16, 16, 16, 16, 16, 16, 16, 16, 16},
118 };
119 
120 static RK_U32 intra_lvl16_sobel_c[4][9] = {
121     {13, 13, 13, 13, 13, 13, 13, 13, 13},
122     {16, 16, 16, 16, 16, 16, 16, 16, 16},
123     {16, 16, 16, 16, 16, 16, 16, 16, 16},
124     {16, 16, 16, 16, 16, 16, 16, 16, 16},
125 };
126 
127 static RK_U32 intra_lvl16_sobel_d[4][9] = {
128     {23750, 23750, 23750, 23750, 23750, 23750, 23750, 23750, 23750},
129     {0, 0, 0, 0, 0, 0, 0, 0, 0},
130     {0, 0, 0, 0, 0, 0, 0, 0, 0},
131     {0, 0, 0, 0, 0, 0, 0, 0, 0},
132 };
133 
134 static RK_U32 intra_lvl32_sobel_a[4][5] = {
135     {18, 18, 18, 18, 18},
136     {16, 16, 16, 16, 16},
137     {16, 16, 16, 16, 16},
138     {16, 16, 16, 16, 16},
139 };
140 
141 static RK_U32 intra_lvl32_sobel_c[4][5] = {
142     {16, 16, 16, 16, 16},
143     {16, 16, 16, 16, 16},
144     {16, 16, 16, 16, 16},
145     {16, 16, 16, 16, 16},
146 };
147 
148 static RK_U32 qnt_bias_i[4] = {
149     128, 128, 128, 128
150 };
151 
152 static RK_U32 qnt_bias_p[4] = {
153     60, 60, 85, 85
154 };
155 
156 static RK_U32 rime_sqi_cime_sad_th[4] = {
157     48, 0, 0, 0
158 };
159 
160 static RK_U32 fme_sqi_cime_sad_pu16_th[4] = {
161     16, 0, 0, 0
162 };
163 
164 static RK_U32 fme_sqi_cime_sad_pu32_th[4] = {
165     16, 0, 0, 0
166 };
167 
168 static RK_U32 fme_sqi_cime_sad_pu64_th[4] = {
169     16, 0, 0, 0
170 };
171 
172 static RK_U32 chrm_klut_ofst[4] = {
173     3, 0, 0, 0
174 };
175 
176 static RK_S32 pre_intra_b32_cost[4][2] = {
177     {31, 30},
178     {23, 20},
179     {31, 30},
180     {23, 20},
181 };
182 
183 static RK_S32 pre_intra_b16_cost[4][2] = {
184     {31, 30},
185     {23, 20},
186     {31, 30},
187     {23, 20},
188 };
189 
190 static RK_S32 cime_multi[4][4] = {
191     {4, 8, 24, 24},
192     {4, 7, 20, 20},
193     {4, 8, 24, 24},
194     {4, 4, 4, 4},
195 };
196 
197 static RK_S32 rime_multi[4][3] = {
198     {4, 32, 128},
199     {4, 16, 64},
200     {4, 32, 128},
201     {4, 4, 4},
202 };
203 
vepu580_h265e_tune_init(H265eV580HalContext * ctx)204 static HalH265eVepu580Tune *vepu580_h265e_tune_init(H265eV580HalContext *ctx)
205 {
206     HalH265eVepu580Tune *tune = mpp_malloc(HalH265eVepu580Tune, 1);
207     RK_S32 scene_mode = ctx->cfg->tune.scene_mode == MPP_ENC_SCENE_MODE_IPC ? 0 : 1;
208 
209     if (NULL == tune)
210         return tune;
211 
212     tune->ctx = ctx;
213     tune->curr_scene_motion_flag = 0;
214     tune->ap_motion_flag = scene_mode;
215     memset(tune->md_madp, 0, sizeof(tune->md_madp));
216     memset(tune->txtr_madi, 0, sizeof(tune->txtr_madi));
217     memset(tune->md_flag_matrix, 0, sizeof(tune->md_flag_matrix));
218     memset(tune->scene_motion_flag_matrix, 0, sizeof(tune->scene_motion_flag_matrix));
219     tune->pre_madi[0] = tune->pre_madi[1] = -1;
220     tune->pre_madp[0] = tune->pre_madp[1] = -1;
221 
222     return tune;
223 }
224 
vepu580_h265e_tune_deinit(void * tune)225 static void vepu580_h265e_tune_deinit(void *tune)
226 {
227     MPP_FREE(tune);
228 }
229 
vepu580_h265e_tune_reg_patch(void * p)230 static void vepu580_h265e_tune_reg_patch(void *p)
231 {
232     HalH265eVepu580Tune *tune = (HalH265eVepu580Tune *)p;
233     H265eV580HalContext *ctx = NULL;
234     RK_S32 scene_mode = 0;
235 
236     if (NULL == tune)
237         return;
238 
239     ctx = tune->ctx;
240     scene_mode = ctx->cfg->tune.scene_mode == MPP_ENC_SCENE_MODE_IPC ? 0 : 1;
241     tune->ap_motion_flag = scene_mode;
242     /* modify register here */
243     H265eV580RegSet *regs = (H265eV580RegSet *)ctx->regs[0];
244     hevc_vepu580_rc_klut *rc_regs =  &regs->reg_rc_klut;
245     hevc_vepu580_wgt *reg_wgt = &regs->reg_wgt;
246     vepu580_rdo_cfg  *reg_rdo = &regs->reg_rdo;
247     RdoAtfSkipCfg *p_rdo_atf_skip;
248     RdoAtfCfg* p_rdo_atf;
249     RK_U32 scene_motion_flag = tune->ap_motion_flag * 2 + tune->curr_scene_motion_flag;
250 
251     if (scene_motion_flag > 3) {
252         mpp_err_f("scene_motion_flag is a wrong value %d\n", scene_motion_flag);
253         return;
254     }
255 
256     memcpy(&reg_wgt->lvl32_intra_CST_WGT0, lvl32_preintra_cst_wgt[scene_motion_flag], sizeof(lvl32_preintra_cst_wgt[scene_motion_flag]));
257     memcpy(&reg_wgt->lvl16_intra_CST_WGT0, lvl16_preintra_cst_wgt[scene_motion_flag], sizeof(lvl16_preintra_cst_wgt[scene_motion_flag]));
258 
259     p_rdo_atf_skip = &reg_rdo->rdo_b64_skip_atf;
260     p_rdo_atf_skip->rdo_b_cime_thd0.cu_rdo_cime_thd0 = 1;
261     p_rdo_atf_skip->rdo_b_cime_thd0.cu_rdo_cime_thd1 = 2;
262     p_rdo_atf_skip->rdo_b_cime_thd1.cu_rdo_cime_thd2 = 4;
263     p_rdo_atf_skip->rdo_b_cime_thd1.cu_rdo_cime_thd3 = 6;
264 
265     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt00  = skip_b64_atf_wgt[scene_motion_flag][0];
266     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt10  = skip_b64_atf_wgt[scene_motion_flag][1];
267     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt11  = skip_b64_atf_wgt[scene_motion_flag][2];
268     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt12  = skip_b64_atf_wgt[scene_motion_flag][3];
269     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt20  = skip_b64_atf_wgt[scene_motion_flag][4];
270     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt21  = skip_b64_atf_wgt[scene_motion_flag][5];
271     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt22  = skip_b64_atf_wgt[scene_motion_flag][6];
272     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt30  = skip_b64_atf_wgt[scene_motion_flag][7];
273     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt31  = skip_b64_atf_wgt[scene_motion_flag][8];
274     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt32  = skip_b64_atf_wgt[scene_motion_flag][9];
275     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt40  = skip_b64_atf_wgt[scene_motion_flag][10];
276     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt41  = skip_b64_atf_wgt[scene_motion_flag][11];
277     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt42  = skip_b64_atf_wgt[scene_motion_flag][12];
278 
279     p_rdo_atf = &reg_rdo->rdo_b32_intra_atf;
280     p_rdo_atf->rdo_b_cime_thd0.cu_rdo_cime_thd0 = 24;
281     p_rdo_atf->rdo_b_cime_thd0.cu_rdo_cime_thd1 = 48;
282     p_rdo_atf->rdo_b_cime_thd1.cu_rdo_cime_thd2 = 64;
283     p_rdo_atf->rdo_b_atf_wgt0.cu_rdo_atf_wgt00  = intra_b32_atf_wgt[scene_motion_flag][0];
284     p_rdo_atf->rdo_b_atf_wgt0.cu_rdo_atf_wgt01  = intra_b32_atf_wgt[scene_motion_flag][1];
285     p_rdo_atf->rdo_b_atf_wgt0.cu_rdo_atf_wgt02  = intra_b32_atf_wgt[scene_motion_flag][2];
286     p_rdo_atf->rdo_b_atf_wgt1.cu_rdo_atf_wgt10  = intra_b32_atf_wgt[scene_motion_flag][3];
287     p_rdo_atf->rdo_b_atf_wgt1.cu_rdo_atf_wgt11  = intra_b32_atf_wgt[scene_motion_flag][4];
288     p_rdo_atf->rdo_b_atf_wgt1.cu_rdo_atf_wgt12  = intra_b32_atf_wgt[scene_motion_flag][5];
289     p_rdo_atf->rdo_b_atf_wgt2.cu_rdo_atf_wgt20  = intra_b32_atf_wgt[scene_motion_flag][6];
290     p_rdo_atf->rdo_b_atf_wgt2.cu_rdo_atf_wgt21  = intra_b32_atf_wgt[scene_motion_flag][7];
291     p_rdo_atf->rdo_b_atf_wgt2.cu_rdo_atf_wgt22  = intra_b32_atf_wgt[scene_motion_flag][8];
292     p_rdo_atf->rdo_b_atf_wgt3.cu_rdo_atf_wgt30  = intra_b32_atf_wgt[scene_motion_flag][9];
293     p_rdo_atf->rdo_b_atf_wgt3.cu_rdo_atf_wgt31  = intra_b32_atf_wgt[scene_motion_flag][10];
294     p_rdo_atf->rdo_b_atf_wgt3.cu_rdo_atf_wgt32  = intra_b32_atf_wgt[scene_motion_flag][11];
295 
296     p_rdo_atf_skip = &reg_rdo->rdo_b32_skip_atf;
297     p_rdo_atf_skip->rdo_b_cime_thd0.cu_rdo_cime_thd0 =  1;
298     p_rdo_atf_skip->rdo_b_cime_thd0.cu_rdo_cime_thd1 =  2;
299     p_rdo_atf_skip->rdo_b_cime_thd1.cu_rdo_cime_thd2 =  4;
300     p_rdo_atf_skip->rdo_b_cime_thd1.cu_rdo_cime_thd3 =  6;
301     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt00  =  skip_b32_atf_wgt[scene_motion_flag][0];
302     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt10  =  skip_b32_atf_wgt[scene_motion_flag][1];
303     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt11  =  skip_b32_atf_wgt[scene_motion_flag][2];
304     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt12  =  skip_b32_atf_wgt[scene_motion_flag][3];
305     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt20  =  skip_b32_atf_wgt[scene_motion_flag][4];
306     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt21  =  skip_b32_atf_wgt[scene_motion_flag][5];
307     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt22  =  skip_b32_atf_wgt[scene_motion_flag][6];
308     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt30  =  skip_b32_atf_wgt[scene_motion_flag][7];
309     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt31  =  skip_b32_atf_wgt[scene_motion_flag][8];
310     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt32  =  skip_b32_atf_wgt[scene_motion_flag][9];
311     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt40  =  skip_b32_atf_wgt[scene_motion_flag][10];
312     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt41  =  skip_b32_atf_wgt[scene_motion_flag][11];
313     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt42  =  skip_b32_atf_wgt[scene_motion_flag][12];
314 
315     p_rdo_atf = &reg_rdo->rdo_b16_intra_atf;
316     p_rdo_atf->rdo_b_cime_thd0.cu_rdo_cime_thd0 = 24;
317     p_rdo_atf->rdo_b_cime_thd0.cu_rdo_cime_thd1 = 48;
318     p_rdo_atf->rdo_b_cime_thd1.cu_rdo_cime_thd2 = 64;
319     p_rdo_atf->rdo_b_atf_wgt0.cu_rdo_atf_wgt00  = intra_b16_atf_wgt[scene_motion_flag][0];
320     p_rdo_atf->rdo_b_atf_wgt0.cu_rdo_atf_wgt01  = intra_b16_atf_wgt[scene_motion_flag][1];
321     p_rdo_atf->rdo_b_atf_wgt0.cu_rdo_atf_wgt02  = intra_b16_atf_wgt[scene_motion_flag][2];
322     p_rdo_atf->rdo_b_atf_wgt1.cu_rdo_atf_wgt10  = intra_b16_atf_wgt[scene_motion_flag][3];
323     p_rdo_atf->rdo_b_atf_wgt1.cu_rdo_atf_wgt11  = intra_b16_atf_wgt[scene_motion_flag][4];
324     p_rdo_atf->rdo_b_atf_wgt1.cu_rdo_atf_wgt12  = intra_b16_atf_wgt[scene_motion_flag][5];
325     p_rdo_atf->rdo_b_atf_wgt2.cu_rdo_atf_wgt20  = intra_b16_atf_wgt[scene_motion_flag][6];
326     p_rdo_atf->rdo_b_atf_wgt2.cu_rdo_atf_wgt21  = intra_b16_atf_wgt[scene_motion_flag][7];
327     p_rdo_atf->rdo_b_atf_wgt2.cu_rdo_atf_wgt22  = intra_b16_atf_wgt[scene_motion_flag][8];
328     p_rdo_atf->rdo_b_atf_wgt3.cu_rdo_atf_wgt30  = intra_b16_atf_wgt[scene_motion_flag][9];
329     p_rdo_atf->rdo_b_atf_wgt3.cu_rdo_atf_wgt31  = intra_b16_atf_wgt[scene_motion_flag][10];
330     p_rdo_atf->rdo_b_atf_wgt3.cu_rdo_atf_wgt32  = intra_b16_atf_wgt[scene_motion_flag][11];
331 
332     p_rdo_atf_skip = &reg_rdo->rdo_b16_skip_atf;
333     p_rdo_atf_skip->rdo_b_cime_thd0.cu_rdo_cime_thd0 = 1;
334     p_rdo_atf_skip->rdo_b_cime_thd0.cu_rdo_cime_thd1 = 2;
335     p_rdo_atf_skip->rdo_b_cime_thd1.cu_rdo_cime_thd2 = 4;
336     p_rdo_atf_skip->rdo_b_cime_thd1.cu_rdo_cime_thd3 = 6;
337     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt00  = skip_b16_atf_wgt[scene_motion_flag][0];
338     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt10  = skip_b16_atf_wgt[scene_motion_flag][1];
339     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt11  = skip_b16_atf_wgt[scene_motion_flag][2];
340     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt12  = skip_b16_atf_wgt[scene_motion_flag][3];
341     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt20  = skip_b16_atf_wgt[scene_motion_flag][4];
342     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt21  = skip_b16_atf_wgt[scene_motion_flag][5];
343     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt22  = skip_b16_atf_wgt[scene_motion_flag][6];
344     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt30  = skip_b16_atf_wgt[scene_motion_flag][7];
345     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt31  = skip_b16_atf_wgt[scene_motion_flag][8];
346     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt32  = skip_b16_atf_wgt[scene_motion_flag][9];
347     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt40  = skip_b16_atf_wgt[scene_motion_flag][10];
348     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt41  = skip_b16_atf_wgt[scene_motion_flag][11];
349     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt42  = skip_b16_atf_wgt[scene_motion_flag][12];
350 
351     p_rdo_atf = &reg_rdo->rdo_b8_intra_atf;
352     p_rdo_atf->rdo_b_cime_thd0.cu_rdo_cime_thd0 = 24;
353     p_rdo_atf->rdo_b_cime_thd0.cu_rdo_cime_thd1 = 48;
354     p_rdo_atf->rdo_b_cime_thd1.cu_rdo_cime_thd2 = 64;
355     p_rdo_atf->rdo_b_atf_wgt0.cu_rdo_atf_wgt00  = intra_b8_atf_wgt[scene_motion_flag][0];
356     p_rdo_atf->rdo_b_atf_wgt0.cu_rdo_atf_wgt01  = intra_b8_atf_wgt[scene_motion_flag][1];
357     p_rdo_atf->rdo_b_atf_wgt0.cu_rdo_atf_wgt02  = intra_b8_atf_wgt[scene_motion_flag][2];
358     p_rdo_atf->rdo_b_atf_wgt1.cu_rdo_atf_wgt10  = intra_b8_atf_wgt[scene_motion_flag][3];
359     p_rdo_atf->rdo_b_atf_wgt1.cu_rdo_atf_wgt11  = intra_b8_atf_wgt[scene_motion_flag][4];
360     p_rdo_atf->rdo_b_atf_wgt1.cu_rdo_atf_wgt12  = intra_b8_atf_wgt[scene_motion_flag][5];
361     p_rdo_atf->rdo_b_atf_wgt2.cu_rdo_atf_wgt20  = intra_b8_atf_wgt[scene_motion_flag][6];
362     p_rdo_atf->rdo_b_atf_wgt2.cu_rdo_atf_wgt21  = intra_b8_atf_wgt[scene_motion_flag][7];
363     p_rdo_atf->rdo_b_atf_wgt2.cu_rdo_atf_wgt22  = intra_b8_atf_wgt[scene_motion_flag][8];
364     p_rdo_atf->rdo_b_atf_wgt3.cu_rdo_atf_wgt30  = intra_b8_atf_wgt[scene_motion_flag][9];
365     p_rdo_atf->rdo_b_atf_wgt3.cu_rdo_atf_wgt31  = intra_b8_atf_wgt[scene_motion_flag][10];
366     p_rdo_atf->rdo_b_atf_wgt3.cu_rdo_atf_wgt32  = intra_b8_atf_wgt[scene_motion_flag][11];
367 
368     p_rdo_atf_skip = &reg_rdo->rdo_b8_skip_atf;
369     p_rdo_atf_skip->rdo_b_cime_thd0.cu_rdo_cime_thd0 = 1;
370     p_rdo_atf_skip->rdo_b_cime_thd0.cu_rdo_cime_thd1 = 2;
371     p_rdo_atf_skip->rdo_b_cime_thd1.cu_rdo_cime_thd2 = 4;
372     p_rdo_atf_skip->rdo_b_cime_thd1.cu_rdo_cime_thd3 = 6;
373     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt00  = skip_b8_atf_wgt[scene_motion_flag][0];
374     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt10  = skip_b8_atf_wgt[scene_motion_flag][1];
375     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt11  = skip_b8_atf_wgt[scene_motion_flag][2];
376     p_rdo_atf_skip->rdo_b_atf_wgt0.cu_rdo_atf_wgt12  = skip_b8_atf_wgt[scene_motion_flag][3];
377     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt20  = skip_b8_atf_wgt[scene_motion_flag][4];
378     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt21  = skip_b8_atf_wgt[scene_motion_flag][5];
379     p_rdo_atf_skip->rdo_b_atf_wgt1.cu_rdo_atf_wgt22  = skip_b8_atf_wgt[scene_motion_flag][6];
380     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt30  = skip_b8_atf_wgt[scene_motion_flag][7];
381     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt31  = skip_b8_atf_wgt[scene_motion_flag][8];
382     p_rdo_atf_skip->rdo_b_atf_wgt2.cu_rdo_atf_wgt32  = skip_b8_atf_wgt[scene_motion_flag][9];
383     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt40  = skip_b8_atf_wgt[scene_motion_flag][10];
384     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt41  = skip_b8_atf_wgt[scene_motion_flag][11];
385     p_rdo_atf_skip->rdo_b_atf_wgt3.cu_rdo_atf_wgt42  = skip_b8_atf_wgt[scene_motion_flag][12];
386 
387     reg_rdo->preintra_b32_cst_wgt.pre_intra32_cst_wgt00 = pre_intra_b32_cost[scene_motion_flag][0];
388     reg_rdo->preintra_b32_cst_wgt.pre_intra32_cst_wgt01 = pre_intra_b32_cost[scene_motion_flag][1];
389     reg_rdo->preintra_b16_cst_wgt.pre_intra16_cst_wgt00 = pre_intra_b16_cost[scene_motion_flag][0];
390     reg_rdo->preintra_b16_cst_wgt.pre_intra16_cst_wgt01 = pre_intra_b16_cost[scene_motion_flag][1];
391 
392     rc_regs->md_sad_thd.md_sad_thd0 = 4;
393     rc_regs->md_sad_thd.md_sad_thd1 = 9;
394     rc_regs->md_sad_thd.md_sad_thd2 = 15;
395     rc_regs->madi_thd.madi_thd0     = 4;
396     rc_regs->madi_thd.madi_thd1     = 9;
397     rc_regs->madi_thd.madi_thd2     = 15;
398 
399     reg_wgt->cime_sqi_cfg.cime_pmv_set_zero = !tune->curr_scene_motion_flag;
400     reg_wgt->cime_sqi_multi0.cime_multi0 = cime_multi[scene_motion_flag][0];
401     reg_wgt->cime_sqi_multi0.cime_multi1 = cime_multi[scene_motion_flag][1];
402     reg_wgt->cime_sqi_multi1.cime_multi2 = cime_multi[scene_motion_flag][2];
403     reg_wgt->cime_sqi_multi1.cime_multi3 = cime_multi[scene_motion_flag][3];
404 
405     reg_wgt->rime_sqi_multi.rime_multi0 = rime_multi[scene_motion_flag][0];
406     reg_wgt->rime_sqi_multi.rime_multi1 = rime_multi[scene_motion_flag][1];
407     reg_wgt->rime_sqi_multi.rime_multi2 = rime_multi[scene_motion_flag][2];
408 
409     if (tune->curr_scene_motion_flag) {
410         reg_wgt->fme_sqi_thd1.move_lambda = 8;
411     }
412 
413     reg_rdo->rdo_sqi_cfg.rdo_segment_en = !tune->curr_scene_motion_flag;
414     reg_rdo->rdo_sqi_cfg.rdo_smear_en = !tune->curr_scene_motion_flag;
415 
416     reg_wgt->i16_sobel_a_00.intra_l16_sobel_a0_qp0 = intra_lvl16_sobel_a[scene_motion_flag][0];
417     reg_wgt->i16_sobel_a_00.intra_l16_sobel_a0_qp1 = intra_lvl16_sobel_a[scene_motion_flag][1];
418     reg_wgt->i16_sobel_a_00.intra_l16_sobel_a0_qp2 = intra_lvl16_sobel_a[scene_motion_flag][2];
419     reg_wgt->i16_sobel_a_00.intra_l16_sobel_a0_qp3 = intra_lvl16_sobel_a[scene_motion_flag][3];
420     reg_wgt->i16_sobel_a_00.intra_l16_sobel_a0_qp4 = intra_lvl16_sobel_a[scene_motion_flag][4];
421     reg_wgt->i16_sobel_a_01.intra_l16_sobel_a0_qp5 = intra_lvl16_sobel_a[scene_motion_flag][5];
422     reg_wgt->i16_sobel_a_01.intra_l16_sobel_a0_qp6 = intra_lvl16_sobel_a[scene_motion_flag][6];
423     reg_wgt->i16_sobel_a_01.intra_l16_sobel_a0_qp7 = intra_lvl16_sobel_a[scene_motion_flag][7];
424     reg_wgt->i16_sobel_a_01.intra_l16_sobel_a0_qp8 = intra_lvl16_sobel_a[scene_motion_flag][8];
425     reg_wgt->i16_sobel_c_00.intra_l16_sobel_c0_qp0 = intra_lvl16_sobel_c[scene_motion_flag][0];
426     reg_wgt->i16_sobel_c_00.intra_l16_sobel_c0_qp1 = intra_lvl16_sobel_c[scene_motion_flag][1];
427     reg_wgt->i16_sobel_c_00.intra_l16_sobel_c0_qp2 = intra_lvl16_sobel_c[scene_motion_flag][2];
428     reg_wgt->i16_sobel_c_00.intra_l16_sobel_c0_qp3 = intra_lvl16_sobel_c[scene_motion_flag][3];
429     reg_wgt->i16_sobel_c_00.intra_l16_sobel_c0_qp4 = intra_lvl16_sobel_c[scene_motion_flag][4];
430     reg_wgt->i16_sobel_c_01.intra_l16_sobel_c0_qp5 = intra_lvl16_sobel_c[scene_motion_flag][5];
431     reg_wgt->i16_sobel_c_01.intra_l16_sobel_c0_qp6 = intra_lvl16_sobel_c[scene_motion_flag][6];
432     reg_wgt->i16_sobel_c_01.intra_l16_sobel_c0_qp7 = intra_lvl16_sobel_c[scene_motion_flag][7];
433     reg_wgt->i16_sobel_c_01.intra_l16_sobel_c0_qp8 = intra_lvl16_sobel_c[scene_motion_flag][8];
434     reg_wgt->i16_sobel_d_00.intra_l16_sobel_d0_qp0 = intra_lvl16_sobel_d[scene_motion_flag][0];
435     reg_wgt->i16_sobel_d_00.intra_l16_sobel_d0_qp1 = intra_lvl16_sobel_d[scene_motion_flag][1];
436     reg_wgt->i16_sobel_d_01.intra_l16_sobel_d0_qp2 = intra_lvl16_sobel_d[scene_motion_flag][2];
437     reg_wgt->i16_sobel_d_01.intra_l16_sobel_d0_qp3 = intra_lvl16_sobel_d[scene_motion_flag][3];
438     reg_wgt->i16_sobel_d_02.intra_l16_sobel_d0_qp4 = intra_lvl16_sobel_d[scene_motion_flag][4];
439     reg_wgt->i16_sobel_d_02.intra_l16_sobel_d0_qp5 = intra_lvl16_sobel_d[scene_motion_flag][5];
440     reg_wgt->i16_sobel_d_03.intra_l16_sobel_d0_qp6 = intra_lvl16_sobel_d[scene_motion_flag][6];
441     reg_wgt->i16_sobel_d_03.intra_l16_sobel_d0_qp7 = intra_lvl16_sobel_d[scene_motion_flag][7];
442     reg_wgt->i16_sobel_d_04.intra_l16_sobel_d0_qp8 = intra_lvl16_sobel_d[scene_motion_flag][8];
443     reg_wgt->i32_sobel_a.intra_l32_sobel_a1_qp0 = intra_lvl32_sobel_a[scene_motion_flag][0];
444     reg_wgt->i32_sobel_a.intra_l32_sobel_a1_qp1 = intra_lvl32_sobel_a[scene_motion_flag][1];
445     reg_wgt->i32_sobel_a.intra_l32_sobel_a1_qp2 = intra_lvl32_sobel_a[scene_motion_flag][2];
446     reg_wgt->i32_sobel_a.intra_l32_sobel_a1_qp3 = intra_lvl32_sobel_a[scene_motion_flag][3];
447     reg_wgt->i32_sobel_a.intra_l32_sobel_a1_qp4 = intra_lvl32_sobel_a[scene_motion_flag][4];
448     reg_wgt->i32_sobel_c.intra_l32_sobel_c1_qp0 = intra_lvl32_sobel_c[scene_motion_flag][0];
449     reg_wgt->i32_sobel_c.intra_l32_sobel_c1_qp1 = intra_lvl32_sobel_c[scene_motion_flag][1];
450     reg_wgt->i32_sobel_c.intra_l32_sobel_c1_qp2 = intra_lvl32_sobel_c[scene_motion_flag][2];
451     reg_wgt->i32_sobel_c.intra_l32_sobel_c1_qp3 = intra_lvl32_sobel_c[scene_motion_flag][3];
452     reg_wgt->i32_sobel_c.intra_l32_sobel_c1_qp4 = intra_lvl32_sobel_c[scene_motion_flag][4];
453 
454     reg_wgt->reg1484_qnt_bias_comb.qnt_bias_i = qnt_bias_i[scene_motion_flag];
455     reg_wgt->reg1484_qnt_bias_comb.qnt_bias_p = qnt_bias_p[scene_motion_flag];
456     reg_wgt->rime_sqi_thd.cime_sad_th0 = rime_sqi_cime_sad_th[scene_motion_flag];
457     reg_wgt->fme_sqi_thd0.cime_sad_pu16_th = fme_sqi_cime_sad_pu16_th[scene_motion_flag];
458     reg_wgt->fme_sqi_thd0.cime_sad_pu32_th = fme_sqi_cime_sad_pu32_th[scene_motion_flag];
459     reg_wgt->fme_sqi_thd1.cime_sad_pu64_th = fme_sqi_cime_sad_pu64_th[scene_motion_flag];
460     rc_regs->klut_ofst.chrm_klut_ofst = chrm_klut_ofst[scene_motion_flag];
461 }
462 
vepu580_h265e_tune_stat_update(void * p)463 static void vepu580_h265e_tune_stat_update(void *p)
464 {
465     HalH265eVepu580Tune *tune = (HalH265eVepu580Tune *)p;
466     H265eV580HalContext *ctx = NULL;
467     RK_S32 scene_mode = 0;
468 
469     if (NULL == tune)
470         return;
471 
472     ctx = tune->ctx;
473     scene_mode = ctx->cfg->tune.scene_mode == MPP_ENC_SCENE_MODE_IPC ? 0 : 1;
474     tune->ap_motion_flag = scene_mode;
475     /* update statistic info here */
476     RK_S32 j;
477     RK_S32 i = 0;
478     RK_S32 mvbit = 10;
479     vepu580_h265_fbk *fb = &ctx->feedback;
480 
481     for (i = 0; i < (RK_S32)ctx->tile_num; i++) {
482         H265eV580StatusElem *elem = (H265eV580StatusElem *)ctx->reg_out[i];
483         fb->st_md_sad_b16num0 += elem->st.md_sad_b16num0;
484         fb->st_md_sad_b16num1 += elem->st.md_sad_b16num1;
485         fb->st_md_sad_b16num2 += elem->st.md_sad_b16num2;
486         fb->st_md_sad_b16num3 += elem->st.md_sad_b16num3;
487         fb->st_madi_b16num0 += elem->st.madi_b16num0;
488         fb->st_madi_b16num1 += elem->st.madi_b16num1;
489         fb->st_madi_b16num2 += elem->st.madi_b16num2;
490         fb->st_madi_b16num3 += elem->st.madi_b16num3;
491     }
492 
493     RK_S32 mb_num = fb->st_mb_num ? fb->st_mb_num : 1;
494     RK_S32 madp = 0;
495     RK_S32 md_flag = 0;
496     RK_S32 nScore = 0;
497     RK_S32 nScoreT = ((MD_WIN_LEN - 2) * 6 + 2 * 8 + 2 * 11 + 2 * 13) / 2;
498     RK_S32 madp_cnt_statistics[5];
499     for (i = 0; i < 5; i++) {
500         madp_cnt_statistics[i] = fb->st_md_sad_b16num0 * madp_num_map[i][0] + fb->st_md_sad_b16num1 * madp_num_map[i][1]
501                                  + fb->st_md_sad_b16num2 * madp_num_map[i][2] + fb->st_md_sad_b16num3 * madp_num_map[i][3];
502     }
503 
504     tune->pre_madi[0] = fb->st_madi;
505     tune->pre_madp[0] = fb->st_madp;
506 
507     if (0 != tune->ap_motion_flag)
508         mvbit = 15;
509 
510     madp = MOTION_LEVEL_STILL;
511     if (0 != madp_cnt_statistics[4]) {
512         RK_S32 base = tune->ap_motion_flag * 3;
513 
514         for (i = 0; i < 3; i++, base++) {
515             if (tune->pre_madp[0] >= ctu_avg_madp_thd[i]) {
516                 if (madp_cnt_statistics[0] > mb_num * ctu_madp_cnt_thd[base][0] >> mvbit ||
517                     madp_cnt_statistics[1] > mb_num * ctu_madp_cnt_thd[base][1] >> mvbit ||
518                     madp_cnt_statistics[2] > mb_num * ctu_madp_cnt_thd[base][2] >> mvbit) {
519                     madp =  MOTION_LEVEL_BIG_MOTION;
520                 } else if ((madp_cnt_statistics[0] > mb_num * ctu_madp_cnt_thd[base][3] >> mvbit ||
521                             madp_cnt_statistics[1] > mb_num * ctu_madp_cnt_thd[base][4] >> mvbit) &&
522                            madp_cnt_statistics[3] < mb_num * ctu_madp_cnt_thd[base][5] >> mvbit) {
523                     madp =  MOTION_LEVEL_BIG_MOTION;
524                 } else if (madp_cnt_statistics[3] < mb_num * ctu_madp_cnt_thd[base][6] >> mvbit) {
525                     madp =  MOTION_LEVEL_BIG_MOTION;
526                 } else if (madp_cnt_statistics[3] < mb_num * ctu_madp_cnt_thd[base][7] >> mvbit) {
527                     madp =  MOTION_LEVEL_MOTION;
528                 }
529                 break;
530             }
531         }
532     } else {
533         madp = MOTION_LEVEL_UNKNOW_SCENE;
534     }
535 
536     if (MOTION_LEVEL_UNKNOW_SCENE != madp) {
537         nScore = madp * 13 + tune->md_madp[0] * 11 + tune->md_madp[1] * 8;
538     } else {
539         nScore = tune->md_madp[0] * 11 + tune->md_madp[1] * 8;
540         nScoreT -= 13;
541     }
542 
543     for (j = 2; j < MD_WIN_LEN; j++) {
544         nScore += tune->md_madp[j] * 6;
545     }
546 
547     if (nScore >= nScoreT) {
548         md_flag = 1;
549     }
550 
551     tune->curr_scene_motion_flag = 0;
552     if (tune->md_flag_matrix[0] && tune->md_flag_matrix[1] && tune->md_flag_matrix[2]) {
553         tune->curr_scene_motion_flag = 1;
554     } else if ((tune->md_flag_matrix[0] && tune->md_flag_matrix[1]) || (tune->md_flag_matrix[1] && tune->md_flag_matrix[2] && tune->md_flag_matrix[3])) {
555         tune->curr_scene_motion_flag = md_flag;
556     }
557 
558     if (MOTION_LEVEL_UNKNOW_SCENE != madp) {
559         for (j = MD_WIN_LEN - 2; j >= 0; j--) {
560             tune->md_madp[j + 1] = tune->md_madp[j];
561         }
562         tune->md_madp[0] = madp;
563     }
564     for (j = MD_SHOW_LEN - 2; j >= 0;  j--) {
565         tune->md_flag_matrix[j + 1] = tune->md_flag_matrix[j];
566     }
567     tune->md_flag_matrix[0] = md_flag;
568 
569     for (j = FRAME_MOTION_ANALYSIS_NUM - 2; j >= 0;  j--) {
570         tune->scene_motion_flag_matrix[j + 1] = tune->scene_motion_flag_matrix[j];
571     }
572     tune->scene_motion_flag_matrix[0] = tune->curr_scene_motion_flag;
573 
574     tune->pre_madi[1] = tune->pre_madi[0];
575     tune->pre_madp[1] = tune->pre_madp[0];
576 }
577