xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/algos/again/rk_aiq_again_algo.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 
2 #include "rk_aiq_again_algo.h"
3 #include "rk_aiq_again_algo_itf.h"
4 
5 RKAIQ_BEGIN_DECLARE
6 
Again_Start_V1(Again_Context_V1_t * pAgainCtx)7 Again_result_t Again_Start_V1(Again_Context_V1_t *pAgainCtx)
8 {
9     LOGI_ANR( "%s:enter!\n", __FUNCTION__);
10 
11     // initial checks
12     if (pAgainCtx == NULL) {
13         return (AGAIN_RET_NULL_POINTER);
14     }
15 
16     if ((AGAIN_STATE_RUNNING == pAgainCtx->eState)
17             || (AGAIN_STATE_LOCKED == pAgainCtx->eState)) {
18         return (AGAIN_RET_FAILURE);
19     }
20 
21     pAgainCtx->eState = AGAIN_STATE_RUNNING;
22 
23     LOGI_ANR( "%s:exit!\n", __FUNCTION__);
24     return (AGAIN_RET_SUCCESS);
25 }
26 
27 
Again_Stop_V1(Again_Context_V1_t * pAgainCtx)28 Again_result_t Again_Stop_V1(Again_Context_V1_t *pAgainCtx)
29 {
30     LOGI_ANR( "%s:enter!\n", __FUNCTION__);
31 
32     // initial checks
33     if (pAgainCtx == NULL) {
34         return (AGAIN_RET_NULL_POINTER);
35     }
36 
37     if (AGAIN_STATE_LOCKED == pAgainCtx->eState) {
38         return (AGAIN_RET_FAILURE);
39     }
40 
41     pAgainCtx->eState = AGAIN_STATE_STOPPED;
42 
43     LOGI_ANR( "%s:exit!\n", __FUNCTION__);
44     return (AGAIN_RET_SUCCESS);
45 }
46 
47 
48 //anr inint
Again_Init_V1(Again_Context_V1_t ** ppAgainCtx,CamCalibDbContext_t * pCalibDb)49 Again_result_t Again_Init_V1(Again_Context_V1_t **ppAgainCtx, CamCalibDbContext_t *pCalibDb)
50 {
51     Again_Context_V1_t * pAgainCtx;
52 
53     LOGE_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
54 
55     pAgainCtx = (Again_Context_V1_t *)malloc(sizeof(Again_Context_V1_t));
56     if(pAgainCtx == NULL) {
57         LOGE_ANR("%s(%d): malloc fail\n", __FUNCTION__, __LINE__);
58         return AGAIN_RET_NULL_POINTER;
59     }
60 
61     memset(pAgainCtx, 0x00, sizeof(Again_Context_V1_t));
62 
63     //gain state init
64     pAgainCtx->stGainState.gain_stat_full_last = -1;
65     pAgainCtx->stGainState.gainState = -1;
66     pAgainCtx->stGainState.gainState_last = -1;
67     pAgainCtx->stGainState.gain_th0[0]    = 2.0;
68     pAgainCtx->stGainState.gain_th1[0]    = 4.0;
69     pAgainCtx->stGainState.gain_th0[1]    = 32.0;
70     pAgainCtx->stGainState.gain_th1[1]    = 64.0;
71 
72     pAgainCtx->eState = AGAIN_STATE_INITIALIZED;
73     *ppAgainCtx = pAgainCtx;
74 
75     pAgainCtx->eMode = AGAIN_OP_MODE_AUTO;
76 	pAgainCtx->isIQParaUpdate = false;
77 	pAgainCtx->isGrayMode = false;
78 
79 
80 #if AGAIN_USE_XML_FILE_V1
81 	CalibDb_MFNR_2_t *pMfnrCalib=
82         (CalibDb_MFNR_2_t*)(CALIBDB_GET_MODULE_PTR((void*)pCalibDb, mfnr));
83 	pAgainCtx->mfnr_mode_3to1 = pMfnrCalib->mode_3to1;
84 	pAgainCtx->mfnr_local_gain_en = pMfnrCalib->local_gain_en;
85 #endif
86 
87 
88     LOGE_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
89     return AGAIN_RET_SUCCESS;
90 }
91 
92 
93 
94 //anr inint
Again_Init_Json_V1(Again_Context_V1_t ** ppAgainCtx,CamCalibDbV2Context_t * pCalibDbV2)95 Again_result_t Again_Init_Json_V1(Again_Context_V1_t **ppAgainCtx, CamCalibDbV2Context_t *pCalibDbV2)
96 {
97     Again_Context_V1_t * pAgainCtx;
98 
99     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
100 
101     pAgainCtx = (Again_Context_V1_t *)malloc(sizeof(Again_Context_V1_t));
102     if(pAgainCtx == NULL) {
103         LOGE_ANR("%s(%d): malloc fail\n", __FUNCTION__, __LINE__);
104         return AGAIN_RET_NULL_POINTER;
105     }
106 
107     memset(pAgainCtx, 0x00, sizeof(Again_Context_V1_t));
108 
109     //gain state init
110     pAgainCtx->stGainState.gain_stat_full_last = -1;
111     pAgainCtx->stGainState.gainState = -1;
112     pAgainCtx->stGainState.gainState_last = -1;
113     pAgainCtx->stGainState.gain_th0[0]    = 2.0;
114     pAgainCtx->stGainState.gain_th1[0]    = 4.0;
115     pAgainCtx->stGainState.gain_th0[1]    = 32.0;
116     pAgainCtx->stGainState.gain_th1[1]    = 64.0;
117 
118 
119     pAgainCtx->eState = AGAIN_STATE_INITIALIZED;
120     *ppAgainCtx = pAgainCtx;
121 
122     pAgainCtx->eMode = AGAIN_OP_MODE_AUTO;
123 	pAgainCtx->isIQParaUpdate = false;
124 	pAgainCtx->isGrayMode = false;
125 
126 #if AGAIN_USE_JSON_FILE_V1
127 
128     CalibDbV2_MFNR_t* pCalibv2_mfnr_v1 =
129             (CalibDbV2_MFNR_t*)(CALIBDBV2_GET_MODULE_PTR(pCalibDbV2, mfnr_v1));
130 	pAgainCtx->mfnr_mode_3to1 = pCalibv2_mfnr_v1->TuningPara.mode_3to1;
131 	pAgainCtx->mfnr_local_gain_en = pCalibv2_mfnr_v1->TuningPara.local_gain_en;
132 
133 #endif
134 
135 
136     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
137     return AGAIN_RET_SUCCESS;
138 }
139 
140 
141 //anr release
Again_Release_V1(Again_Context_V1_t * pAgainCtx)142 Again_result_t Again_Release_V1(Again_Context_V1_t *pAgainCtx)
143 {
144 	Again_result_t result = AGAIN_RET_SUCCESS;
145     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
146     if(pAgainCtx == NULL) {
147         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
148         return AGAIN_RET_NULL_POINTER;
149     }
150 
151 	result = Again_Stop_V1(pAgainCtx);
152     if (result != AGAIN_RET_SUCCESS) {
153         LOGE_ANR( "%s: ANRStop() failed!\n", __FUNCTION__);
154         return (result);
155     }
156 
157     // check state
158     if ((AGAIN_STATE_RUNNING == pAgainCtx->eState)
159             || (AGAIN_STATE_LOCKED == pAgainCtx->eState)) {
160         return (AGAIN_RET_BUSY);
161     }
162 
163     memset(pAgainCtx, 0x00, sizeof(Again_Context_V1_t));
164     free(pAgainCtx);
165 
166     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
167     return AGAIN_RET_SUCCESS;
168 }
169 
170 //anr config
Again_Prepare_V1(Again_Context_V1_t * pAgainCtx,Again_Config_V1_t * pAgainConfig)171 Again_result_t Again_Prepare_V1(Again_Context_V1_t *pAgainCtx, Again_Config_V1_t* pAgainConfig)
172 {
173     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
174 
175     if(pAgainCtx == NULL) {
176         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
177         return AGAIN_RET_INVALID_PARM;
178     }
179 
180     if(pAgainConfig == NULL) {
181         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
182         return AGAIN_RET_INVALID_PARM;
183     }
184 
185     Again_Start_V1(pAgainCtx);
186 
187     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
188     return AGAIN_RET_SUCCESS;
189 }
190 
191 //anr reconfig
Again_ReConfig_V1(Again_Context_V1_t * pAgainCtx,Again_Config_V1_t * pAgainConfig)192 Again_result_t Again_ReConfig_V1(Again_Context_V1_t *pAgainCtx, Again_Config_V1_t* pAgainConfig)
193 {
194     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
195     //need todo what?
196 
197     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
198     return AGAIN_RET_SUCCESS;
199 }
200 
201 
202 //anr preprocess
Again_PreProcess_V1(Again_Context_V1_t * pAgainCtx)203 Again_result_t Again_PreProcess_V1(Again_Context_V1_t *pAgainCtx)
204 {
205     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
206     //need todo what?
207 
208     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
209     return AGAIN_RET_SUCCESS;
210 }
211 
Again_GainRatioProcess_V1(Again_GainState_t * pGainState,Again_ExpInfo_t * pExpInfo)212 Again_result_t Again_GainRatioProcess_V1(Again_GainState_t *pGainState, Again_ExpInfo_t *pExpInfo)
213 {
214     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
215 
216     if(pGainState == NULL) {
217         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
218         return AGAIN_RET_INVALID_PARM;
219     }
220 
221     if(pExpInfo == NULL) {
222         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
223         return AGAIN_RET_INVALID_PARM;
224     }
225 
226     float gain_cur = pExpInfo->arAGain[pExpInfo->hdr_mode] * pExpInfo->arDGain[pExpInfo->hdr_mode];
227     float th[2];
228     float gain_th0[2];
229     float gain_th1[2];
230     for(int i = 0; i < 2; i++){
231         gain_th0[i]     = pGainState->gain_th0[i];
232         gain_th1[i]     = pGainState->gain_th1[i];
233         th[i]           = pow(2.0, (log2(gain_th0[i])+log2(gain_th1[i])) / 2);
234     }
235 
236     pGainState->gain_cur = gain_cur;
237 
238 	int gain_stat_full = -1;
239 	int gain_stat_full_last = pGainState->gain_stat_full_last;
240 	int gain_stat_last = pGainState->gainState_last;
241 	int gain_stat_cur  = -1;
242 	int gain_stat = -1;
243 
244     if(gain_cur <= gain_th0[0])
245 	{
246         gain_stat_full = 0;
247         gain_stat_cur = 0;
248 	}
249     else if(gain_cur <= gain_th1[0] && gain_cur >= gain_th0[0])
250     {
251         gain_stat_full = 1;
252     }
253     else if(gain_cur <= gain_th0[1] && gain_cur >= gain_th1[0])
254     {
255         gain_stat_full = 2;
256         gain_stat_cur = 1;
257   	 }
258     else if(gain_cur <= gain_th1[1] && gain_cur >= gain_th0[1])
259     {
260         gain_stat_full = 3;
261     }
262     else if(gain_cur >= gain_th1[1])
263 	{
264         gain_stat_full = 4;
265         gain_stat_cur = 2;
266 	}
267    if(gain_stat_last == -1 || (abs(gain_stat_full - gain_stat_full_last) >= 2 && gain_stat_cur == -1)) {
268         if(gain_cur <= th[0])
269             gain_stat_cur = 0;
270         else if(gain_cur <= th[1])
271             gain_stat_cur = 1;
272         else
273             gain_stat_cur = 2;
274     }
275     if (gain_stat_cur != -1) {
276         gain_stat_last      = gain_stat_cur;
277         gain_stat_full_last = gain_stat_full;
278         gain_stat       = gain_stat_cur;
279     } else {
280         gain_stat       = gain_stat_last;
281     }
282     if (gain_stat ==0)
283         pGainState->ratio = 16;
284     else if (gain_stat == 1)
285         pGainState->ratio = 1;
286     else
287         pGainState->ratio = 1.0/16.0;
288 
289 	pGainState->gain_stat_full_last 	= gain_stat_full_last;
290 	pGainState->gainState 		= gain_stat;
291 	pGainState->gainState_last 	= gain_stat_last;
292 
293     LOGD_ANR("%s:%d gain_cur:%f gain th %f %fd %f %f ratio:%f gain_state:%d %d full    %d %d\n",
294              __FUNCTION__, __LINE__,
295              gain_cur,
296              gain_th0[0],gain_th0[1],
297              gain_th1[0],gain_th1[1],
298              pGainState->ratio,
299              pGainState->gainState_last,
300              pGainState->gainState,
301              pGainState->gain_stat_full_last,
302              gain_stat_full);
303 
304 
305     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
306 
307     return AGAIN_RET_SUCCESS;
308 }
309 
310 
311 //anr process
Again_Process_V1(Again_Context_V1_t * pAgainCtx,Again_ExpInfo_t * pExpInfo)312 Again_result_t Again_Process_V1(Again_Context_V1_t *pAgainCtx, Again_ExpInfo_t *pExpInfo)
313 {
314     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
315 	Again_ParamMode_t mode = AGAIN_PARAM_MODE_INVALID;
316 
317     if(pAgainCtx == NULL) {
318         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
319         return AGAIN_RET_INVALID_PARM;
320     }
321 
322     if(pExpInfo == NULL) {
323         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
324         return AGAIN_RET_INVALID_PARM;
325     }
326 
327 	if(pAgainCtx->eState != AGAIN_STATE_RUNNING){
328 		return AGAIN_RET_SUCCESS;
329 	}
330 
331 	Again_GainRatioProcess_V1(&pAgainCtx->stGainState, pExpInfo);
332 
333     if(pAgainCtx->eMode == AGAIN_OP_MODE_AUTO) {
334 
335         LOGD_ANR("%s(%d): \n", __FUNCTION__, __LINE__);
336         //get param from mfnr
337 
338     } else if(pAgainCtx->eMode == AGAIN_OP_MODE_MANUAL) {
339         //TODO
340     }
341 
342     memcpy(&pAgainCtx->stExpInfo, pExpInfo, sizeof(Again_ExpInfo_t));
343 
344     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
345     return AGAIN_RET_SUCCESS;
346 
347 }
348 
349 
350 //anr get result
Again_GetProcResult_V1(Again_Context_V1_t * pAgainCtx,Again_ProcResult_V1_t * pAgainResult)351 Again_result_t Again_GetProcResult_V1(Again_Context_V1_t *pAgainCtx, Again_ProcResult_V1_t* pAgainResult)
352 {
353     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
354 
355     if(pAgainCtx == NULL) {
356         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
357         return AGAIN_RET_INVALID_PARM;
358     }
359 
360     if(pAgainResult == NULL) {
361         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
362         return AGAIN_RET_INVALID_PARM;
363     }
364 
365     RK_GAIN_Params_V1_Select_t* stSelect = NULL;
366     if(pAgainCtx->eMode == AGAIN_OP_MODE_AUTO) {
367         stSelect = &pAgainCtx->stAuto.stSelect;
368     } else if(pAgainCtx->eMode == AGAIN_OP_MODE_MANUAL) {
369         stSelect= &pAgainCtx->stManual.stSelect;
370     }
371 
372     //transfer to reg value
373 	gain_fix_transfer_v1(stSelect, pAgainResult->stFix, &pAgainCtx->stExpInfo, pAgainCtx->stGainState.ratio);
374 	pAgainResult->stFix->gain_table_en = pAgainCtx->mfnr_local_gain_en;
375 
376     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
377     return AGAIN_RET_SUCCESS;
378 }
379 
380 
381 
382 
383 RKAIQ_END_DECLARE
384 
385 
386