xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/algos/again2/rk_aiq_again_algo_v2.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 
2 #include "rk_aiq_again_algo_v2.h"
3 #include "rk_aiq_again_algo_itf_v2.h"
4 
5 RKAIQ_BEGIN_DECLARE
6 
Again_Start_V2(Again_Context_V2_t * pAgainCtx)7 Again_result_V2_t Again_Start_V2(Again_Context_V2_t *pAgainCtx)
8 {
9     LOGI_ANR( "%s:enter!\n", __FUNCTION__);
10 
11     // initial checks
12     if (pAgainCtx == NULL) {
13         return (AGAINV2_RET_NULL_POINTER);
14     }
15 
16     if ((AGAINV2_STATE_RUNNING == pAgainCtx->eState)
17             || (AGAINV2_STATE_LOCKED == pAgainCtx->eState)) {
18         return (AGAINV2_RET_FAILURE);
19     }
20 
21     pAgainCtx->eState = AGAINV2_STATE_RUNNING;
22 
23     LOGI_ANR( "%s:exit!\n", __FUNCTION__);
24     return (AGAINV2_RET_SUCCESS);
25 }
26 
27 
Again_Stop_V2(Again_Context_V2_t * pAgainCtx)28 Again_result_V2_t Again_Stop_V2(Again_Context_V2_t *pAgainCtx)
29 {
30     LOGI_ANR( "%s:enter!\n", __FUNCTION__);
31 
32     // initial checks
33     if (pAgainCtx == NULL) {
34         return (AGAINV2_RET_NULL_POINTER);
35     }
36 
37     if (AGAINV2_STATE_LOCKED == pAgainCtx->eState) {
38         return (AGAINV2_RET_FAILURE);
39     }
40 
41     pAgainCtx->eState = AGAINV2_STATE_STOPPED;
42 
43     LOGI_ANR( "%s:exit!\n", __FUNCTION__);
44     return (AGAINV2_RET_SUCCESS);
45 }
46 
47 //anr inint
Again_Init_V2(Again_Context_V2_t ** ppAgainCtx,CamCalibDbV2Context_t * pCalibDbV2)48 Again_result_V2_t Again_Init_V2(Again_Context_V2_t **ppAgainCtx, CamCalibDbV2Context_t *pCalibDbV2)
49 {
50     Again_Context_V2_t * pAgainCtx;
51 
52     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
53 
54     pAgainCtx = (Again_Context_V2_t *)malloc(sizeof(Again_Context_V2_t));
55     if(pAgainCtx == NULL) {
56         LOGE_ANR("%s(%d): malloc fail\n", __FUNCTION__, __LINE__);
57         return AGAINV2_RET_NULL_POINTER;
58     }
59 
60     memset(pAgainCtx, 0x00, sizeof(Again_Context_V2_t));
61 
62     //gain state init
63     pAgainCtx->stGainState.gain_stat_full_last = -1;
64     pAgainCtx->stGainState.gainState = -1;
65     pAgainCtx->stGainState.gainState_last = -1;
66     pAgainCtx->stGainState.gain_th0[0]    = 2.0;
67     pAgainCtx->stGainState.gain_th1[0]    = 4.0;
68     pAgainCtx->stGainState.gain_th0[1]    = 32.0;
69     pAgainCtx->stGainState.gain_th1[1]    = 64.0;
70 
71 
72     pAgainCtx->eState = AGAINV2_STATE_INITIALIZED;
73     *ppAgainCtx = pAgainCtx;
74 
75     pAgainCtx->eMode = AGAINV2_OP_MODE_AUTO;
76     pAgainCtx->isIQParaUpdate = false;
77     pAgainCtx->isGrayMode = false;
78     pAgainCtx->isReCalculate = 1;
79 #if AGAIN_USE_JSON_FILE_V2
80 
81     CalibDbV2_GainV2_t * pcalibdbV2_gain_v2 =
82         (CalibDbV2_GainV2_t *)(CALIBDBV2_GET_MODULE_PTR((CamCalibDbV2Context_t*)pCalibDbV2, gain_v2));
83 
84     pAgainCtx->gain_v2 = *pcalibdbV2_gain_v2;
85 
86 
87     pAgainCtx->stExpInfo.snr_mode = 1;
88     pAgainCtx->eParamMode = AGAINV2_PARAM_MODE_NORMAL;
89     Again_ConfigSettingParam_V2(pAgainCtx, pAgainCtx->eParamMode, pAgainCtx->stExpInfo.snr_mode);
90 #endif
91 
92 
93     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
94     return AGAINV2_RET_SUCCESS;
95 }
96 
97 
98 //anr release
Again_Release_V2(Again_Context_V2_t * pAgainCtx)99 Again_result_V2_t Again_Release_V2(Again_Context_V2_t *pAgainCtx)
100 {
101     Again_result_V2_t result = AGAINV2_RET_SUCCESS;
102     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
103     if(pAgainCtx == NULL) {
104         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
105         return AGAINV2_RET_NULL_POINTER;
106     }
107 
108     result = Again_Stop_V2(pAgainCtx);
109     if (result != AGAINV2_RET_SUCCESS) {
110         LOGE_ANR( "%s: ANRStop() failed!\n", __FUNCTION__);
111         return (result);
112     }
113 
114     // check state
115     if ((AGAINV2_STATE_RUNNING == pAgainCtx->eState)
116             || (AGAINV2_STATE_LOCKED == pAgainCtx->eState)) {
117         return (AGAINV2_RET_BUSY);
118     }
119 
120     memset(pAgainCtx, 0x00, sizeof(Again_Context_V2_t));
121     free(pAgainCtx);
122 
123     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
124     return AGAINV2_RET_SUCCESS;
125 }
126 
127 //anr config
Again_Prepare_V2(Again_Context_V2_t * pAgainCtx,Again_Config_V2_t * pAgainConfig)128 Again_result_V2_t Again_Prepare_V2(Again_Context_V2_t *pAgainCtx, Again_Config_V2_t* pAgainConfig)
129 {
130     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
131 
132     if(pAgainCtx == NULL) {
133         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
134         return AGAINV2_RET_INVALID_PARM;
135     }
136 
137     if(pAgainConfig == NULL) {
138         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
139         return AGAINV2_RET_INVALID_PARM;
140     }
141 
142     Again_Start_V2(pAgainCtx);
143 
144     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
145     return AGAINV2_RET_SUCCESS;
146 }
147 
148 //anr reconfig
Again_ReConfig_V2(Again_Context_V2_t * pAgainCtx,Again_Config_V2_t * pAgainConfig)149 Again_result_V2_t Again_ReConfig_V2(Again_Context_V2_t *pAgainCtx, Again_Config_V2_t* pAgainConfig)
150 {
151     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
152     //need todo what?
153 
154     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
155     return AGAINV2_RET_SUCCESS;
156 }
157 
158 
159 //anr preprocess
Again_PreProcess_V2(Again_Context_V2_t * pAgainCtx)160 Again_result_V2_t Again_PreProcess_V2(Again_Context_V2_t *pAgainCtx)
161 {
162     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
163     //need todo what?
164 
165     if(pAgainCtx->isIQParaUpdate) {
166         Again_ConfigSettingParam_V2(pAgainCtx, pAgainCtx->eParamMode, pAgainCtx->stExpInfo.snr_mode);
167         pAgainCtx->isIQParaUpdate = false;
168     }
169 
170     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
171     return AGAINV2_RET_SUCCESS;
172 }
173 
Again_GainRatioProcess_V2(Again_GainState_V2_t * pGainState,Again_ExpInfo_V2_t * pExpInfo)174 Again_result_V2_t Again_GainRatioProcess_V2(Again_GainState_V2_t *pGainState, Again_ExpInfo_V2_t *pExpInfo)
175 {
176     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
177 
178     if(pGainState == NULL) {
179         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
180         return AGAINV2_RET_INVALID_PARM;
181     }
182 
183     if(pExpInfo == NULL) {
184         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
185         return AGAINV2_RET_INVALID_PARM;
186     }
187 
188     float gain_cur = pExpInfo->arAGain[pExpInfo->hdr_mode] * pExpInfo->arDGain[pExpInfo->hdr_mode];
189     float th[2];
190     float gain_th0[2];
191     float gain_th1[2];
192     for(int i = 0; i < 2; i++) {
193         gain_th0[i]     = pGainState->gain_th0[i];
194         gain_th1[i]     = pGainState->gain_th1[i];
195         th[i]           = pow(2.0, (log2(gain_th0[i]) + log2(gain_th1[i])) / 2);
196     }
197 
198     pGainState->gain_cur = gain_cur;
199 
200     int gain_stat_full = -1;
201     int gain_stat_full_last = pGainState->gain_stat_full_last;
202     int gain_stat_last = pGainState->gainState_last;
203     int gain_stat_cur  = -1;
204     int gain_stat = -1;
205 
206     if(gain_cur <= gain_th0[0])
207     {
208         gain_stat_full = 0;
209         gain_stat_cur = 0;
210     }
211     else if(gain_cur <= gain_th1[0] && gain_cur >= gain_th0[0])
212     {
213         gain_stat_full = 1;
214     }
215     else if(gain_cur <= gain_th0[1] && gain_cur >= gain_th1[0])
216     {
217         gain_stat_full = 2;
218         gain_stat_cur = 1;
219     }
220     else if(gain_cur <= gain_th1[1] && gain_cur >= gain_th0[1])
221     {
222         gain_stat_full = 3;
223     }
224     else if(gain_cur >= gain_th1[1])
225     {
226         gain_stat_full = 4;
227         gain_stat_cur = 2;
228     }
229     if(gain_stat_last == -1 || (abs(gain_stat_full - gain_stat_full_last) >= 2 && gain_stat_cur == -1)) {
230         if(gain_cur <= th[0])
231             gain_stat_cur = 0;
232         else if(gain_cur <= th[1])
233             gain_stat_cur = 1;
234         else
235             gain_stat_cur = 2;
236     }
237     if (gain_stat_cur != -1) {
238         gain_stat_last      = gain_stat_cur;
239         gain_stat_full_last = gain_stat_full;
240         gain_stat       = gain_stat_cur;
241     } else {
242         gain_stat       = gain_stat_last;
243     }
244     if (gain_stat == 0)
245         pGainState->ratio = 16;
246     else if (gain_stat == 1)
247         pGainState->ratio = 1;
248     else
249         pGainState->ratio = 1.0 / 16.0;
250 
251     pGainState->gain_stat_full_last     = gain_stat_full_last;
252     pGainState->gainState       = gain_stat;
253     pGainState->gainState_last  = gain_stat_last;
254 
255     LOGD_ANR("%s:%d gain_cur:%f gain th %f %fd %f %f ratio:%f gain_state:%d %d full    %d %d\n",
256              __FUNCTION__, __LINE__,
257              gain_cur,
258              gain_th0[0], gain_th0[1],
259              gain_th1[0], gain_th1[1],
260              pGainState->ratio,
261              pGainState->gainState_last,
262              pGainState->gainState,
263              pGainState->gain_stat_full_last,
264              gain_stat_full);
265 
266 
267     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
268 
269     return AGAINV2_RET_SUCCESS;
270 }
271 
272 
273 //anr process
Again_Process_V2(Again_Context_V2_t * pAgainCtx,Again_ExpInfo_V2_t * pExpInfo)274 Again_result_V2_t Again_Process_V2(Again_Context_V2_t *pAgainCtx, Again_ExpInfo_V2_t *pExpInfo)
275 {
276     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
277     Again_ParamMode_V2_t mode = AGAINV2_PARAM_MODE_INVALID;
278 
279     if(pAgainCtx == NULL) {
280         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
281         return AGAINV2_RET_INVALID_PARM;
282     }
283 
284     if(pExpInfo == NULL) {
285         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
286         return AGAINV2_RET_INVALID_PARM;
287     }
288 
289     if(pAgainCtx->eState != AGAINV2_STATE_RUNNING) {
290         return AGAINV2_RET_SUCCESS;
291     }
292 
293     //Again_GainRatioProcess_V2(&pAgainCtx->stGainState, pExpInfo);
294 
295     if(pAgainCtx->eMode == AGAINV2_OP_MODE_AUTO) {
296 
297 
298 #if AGAIN_USE_JSON_FILE_V2
299         if(pExpInfo->snr_mode != pAgainCtx->stExpInfo.snr_mode || pAgainCtx->eParamMode != mode) {
300             LOGD_ANR("param mode:%d snr_mode:%d\n", mode, pExpInfo->snr_mode);
301             pAgainCtx->eParamMode = mode;
302             Again_ConfigSettingParam_V2(pAgainCtx, pAgainCtx->eParamMode, pExpInfo->snr_mode);
303         }
304 #endif
305 
306         //select param
307         gain_select_params_by_ISO_V2(&pAgainCtx->stAuto.stParams, &pAgainCtx->stAuto.stSelect, pExpInfo);
308 
309     } else if(pAgainCtx->eMode == AGAINV2_OP_MODE_MANUAL) {
310         //TODO
311     }
312 
313     memcpy(&pAgainCtx->stExpInfo, pExpInfo, sizeof(Again_ExpInfo_V2_t));
314 
315     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
316     return AGAINV2_RET_SUCCESS;
317 
318 }
319 
320 
321 //anr get result
Again_GetProcResult_V2(Again_Context_V2_t * pAgainCtx,Again_ProcResult_V2_t * pAgainResult)322 Again_result_V2_t Again_GetProcResult_V2(Again_Context_V2_t *pAgainCtx, Again_ProcResult_V2_t* pAgainResult)
323 {
324     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
325 
326     if(pAgainCtx == NULL) {
327         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
328         return AGAINV2_RET_INVALID_PARM;
329     }
330 
331     if(pAgainResult == NULL) {
332         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
333         return AGAINV2_RET_INVALID_PARM;
334     }
335 
336     RK_GAIN_Select_V2_t* stSelect = NULL;
337     if(pAgainCtx->eMode == AGAINV2_OP_MODE_AUTO) {
338         stSelect = &pAgainCtx->stAuto.stSelect;
339     } else if(pAgainCtx->eMode == AGAINV2_OP_MODE_MANUAL) {
340         stSelect = &pAgainCtx->stManual.stSelect;
341     }
342 
343     //transfer to reg value
344     gain_fix_transfer_v2(stSelect, pAgainResult->stFix, &pAgainCtx->stExpInfo, pAgainCtx->stGainState.ratio);
345 
346     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
347     return AGAINV2_RET_SUCCESS;
348 }
349 
350 
Again_ConfigSettingParam_V2(Again_Context_V2_t * pAgainCtx,Again_ParamMode_V2_t eParamMode,int snr_mode)351 Again_result_V2_t Again_ConfigSettingParam_V2(Again_Context_V2_t *pAgainCtx, Again_ParamMode_V2_t eParamMode, int snr_mode)
352 {
353     char snr_name[CALIBDB_NR_SHARP_NAME_LENGTH];
354     char param_mode_name[CALIBDB_MAX_MODE_NAME_LENGTH];
355     memset(param_mode_name, 0x00, sizeof(param_mode_name));
356     memset(snr_name, 0x00, sizeof(snr_name));
357 
358     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
359     if(pAgainCtx == NULL) {
360         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
361         return AGAINV2_RET_INVALID_PARM;
362     }
363 
364     //select param mode first
365     if(eParamMode == AGAINV2_PARAM_MODE_NORMAL) {
366         sprintf(param_mode_name, "%s", "normal");
367     } else if(eParamMode == AGAINV2_PARAM_MODE_HDR) {
368         sprintf(param_mode_name, "%s", "hdr");
369     } else if(eParamMode == AGAINV2_PARAM_MODE_GRAY) {
370         sprintf(param_mode_name, "%s", "gray");
371     } else {
372         LOGE_ANR("%s(%d): not support param mode!\n", __FUNCTION__, __LINE__);
373         sprintf(param_mode_name, "%s", "normal");
374     }
375 
376     //then select snr mode next
377     if(snr_mode == 1) {
378         sprintf(snr_name, "%s", "HSNR");
379     } else if(snr_mode == 0) {
380         sprintf(snr_name, "%s", "LSNR");
381     } else {
382         LOGE_ANR("%s(%d): not support snr mode:%d!\n", __FUNCTION__, __LINE__, snr_mode);
383         sprintf(snr_name, "%s", "LSNR");
384     }
385 
386 #if (AGAIN_USE_JSON_FILE_V2)
387     gain_config_setting_param_json_V2(&pAgainCtx->stAuto.stParams, &pAgainCtx->gain_v2, param_mode_name, snr_name);
388 #endif
389     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
390     return AGAINV2_RET_SUCCESS;
391 }
392 
393 
Again_ParamModeProcess_V2(Again_Context_V2_t * pAgainCtx,Again_ExpInfo_V2_t * pExpInfo,Again_ParamMode_V2_t * mode)394 Again_result_V2_t Again_ParamModeProcess_V2(Again_Context_V2_t *pAgainCtx, Again_ExpInfo_V2_t *pExpInfo, Again_ParamMode_V2_t *mode)
395 {
396     Again_result_V2_t res  = AGAINV2_RET_SUCCESS;
397     *mode = pAgainCtx->eParamMode;
398 
399     if(pAgainCtx == NULL) {
400         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
401         return AGAINV2_RET_INVALID_PARM;
402     }
403 
404     if(pAgainCtx->isGrayMode) {
405         *mode = AGAINV2_PARAM_MODE_GRAY;
406     } else if(pExpInfo->hdr_mode == 0) {
407         *mode = AGAINV2_PARAM_MODE_NORMAL;
408     } else if(pExpInfo->hdr_mode >= 1) {
409         *mode = AGAINV2_PARAM_MODE_HDR;
410     } else {
411         *mode = AGAINV2_PARAM_MODE_NORMAL;
412     }
413 
414     return res;
415 }
416 
417 RKAIQ_END_DECLARE
418 
419 
420