xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/algos/anr/rk_aiq_anr_algo.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 
2 #include "rk_aiq_anr_algo.h"
3 #include "rk_aiq_algo_anr_itf.h"
4 
5 RKAIQ_BEGIN_DECLARE
6 
ANRStart(ANRContext_t * pANRCtx)7 ANRresult_t ANRStart(ANRContext_t *pANRCtx)
8 {
9     LOGI_ANR( "%s:enter!\n", __FUNCTION__);
10 
11     // initial checks
12     if (pANRCtx == NULL) {
13         return (ANR_RET_NULL_POINTER);
14     }
15 
16     if ((ANR_STATE_RUNNING == pANRCtx->eState)
17             || (ANR_STATE_LOCKED == pANRCtx->eState)) {
18         return (ANR_RET_FAILURE);
19     }
20 
21     pANRCtx->eState = ANR_STATE_RUNNING;
22 
23     LOGI_ANR( "%s:exit!\n", __FUNCTION__);
24     return (ANR_RET_SUCCESS);
25 }
26 
27 
ANRStop(ANRContext_t * pANRCtx)28 ANRresult_t ANRStop(ANRContext_t *pANRCtx)
29 {
30     LOGI_ANR( "%s:enter!\n", __FUNCTION__);
31 
32     // initial checks
33     if (pANRCtx == NULL) {
34         return (ANR_RET_NULL_POINTER);
35     }
36 
37     if (ANR_STATE_LOCKED == pANRCtx->eState) {
38         return (ANR_RET_FAILURE);
39     }
40 
41     pANRCtx->eState = ANR_STATE_STOPPED;
42 
43     LOGI_ANR( "%s:exit!\n", __FUNCTION__);
44     return (ANR_RET_SUCCESS);
45 }
46 
47 
48 //anr inint
ANRInit(ANRContext_t ** ppANRCtx,CamCalibDbContext_t * pCalibDb)49 ANRresult_t ANRInit(ANRContext_t **ppANRCtx, CamCalibDbContext_t *pCalibDb)
50 {
51     ANRContext_t * pANRCtx;
52 
53     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
54 
55     pANRCtx = (ANRContext_t *)malloc(sizeof(ANRContext_t));
56     if(pANRCtx == NULL) {
57         LOGE_ANR("%s(%d): malloc fail\n", __FUNCTION__, __LINE__);
58         return ANR_RET_NULL_POINTER;
59     }
60 
61     memset(pANRCtx, 0x00, sizeof(ANRContext_t));
62 
63     //gain state init
64     pANRCtx->stGainState.gain_stat_full_last = -1;
65     pANRCtx->stGainState.gainState = -1;
66     pANRCtx->stGainState.gainState_last = -1;
67     pANRCtx->stGainState.gain_th0[0]    = 2.0;
68     pANRCtx->stGainState.gain_th1[0]    = 4.0;
69     pANRCtx->stGainState.gain_th0[1]    = 32.0;
70     pANRCtx->stGainState.gain_th1[1]    = 64.0;
71 
72     pANRCtx->fLuma_SF_Strength = 1.0;
73     pANRCtx->fLuma_TF_Strength = 1.0;
74     pANRCtx->fChroma_SF_Strength = 1.0;
75     pANRCtx->fChroma_TF_Strength = 1.0;
76     pANRCtx->fRawnr_SF_Strength = 1.0;
77 
78     pANRCtx->eState = ANR_STATE_INITIALIZED;
79     *ppANRCtx = pANRCtx;
80 
81     pANRCtx->refYuvBit = 8;
82     pANRCtx->eMode = ANR_OP_MODE_AUTO;
83     pANRCtx->isIQParaUpdate = false;
84     pANRCtx->isGrayMode = false;
85 
86 #if ANR_USE_XML_FILE
87     //read v1 params from xml
88     pANRCtx->stBayernrCalib =
89         *(CalibDb_BayerNr_2_t*)(CALIBDB_GET_MODULE_PTR((void*)pCalibDb, bayerNr));
90     pANRCtx->stUvnrCalib =
91         *(CalibDb_UVNR_2_t*)(CALIBDB_GET_MODULE_PTR((void*)pCalibDb, uvnr));
92     pANRCtx->stYnrCalib =
93         *(CalibDb_YNR_2_t*)(CALIBDB_GET_MODULE_PTR((void*)pCalibDb, ynr));
94     pANRCtx->stMfnrCalib =
95         *(CalibDb_MFNR_2_t*)(CALIBDB_GET_MODULE_PTR((void*)pCalibDb, mfnr));
96 #endif
97 
98 #if RK_SIMULATOR_HW
99     //just for v2 params from html
100     FILE *fp2 = fopen("rkaiq_anr_html_params.bin", "rb");
101     if(fp2 != NULL) {
102         memset(&pANRCtx->stAuto.stBayernrParams, 0, sizeof(RKAnr_Bayernr_Params_t));
103         fread(&pANRCtx->stAuto.stBayernrParams, 1, sizeof(RKAnr_Bayernr_Params_t), fp2);
104         memset(&pANRCtx->stAuto.stMfnrParams, 0, sizeof(RKAnr_Mfnr_Params_t));
105         fread(&pANRCtx->stAuto.stMfnrParams, 1, sizeof(RKAnr_Mfnr_Params_t), fp2);
106         memset(&pANRCtx->stAuto.stUvnrParams, 0, sizeof(RKAnr_Uvnr_Params_t));
107         fread(&pANRCtx->stAuto.stUvnrParams, 1, sizeof(RKAnr_Uvnr_Params_t), fp2);
108         memset(&pANRCtx->stAuto.stYnrParams, 0, sizeof(RKAnr_Ynr_Params_t));
109         fread(&pANRCtx->stAuto.stYnrParams, 1, sizeof(RKAnr_Ynr_Params_t), fp2);
110         LOGD_ANR("oyyf: %s:(%d) read anr param html file sucess! \n", __FUNCTION__, __LINE__);
111     } else {
112         LOGE_ANR("oyyf: %s:(%d) read anr param html file failed! \n", __FUNCTION__, __LINE__);
113         return ANR_RET_FAILURE;
114     }
115 #endif
116 
117 #if ANR_USE_XML_FILE
118     pANRCtx->stExpInfo.snr_mode = 0;
119     pANRCtx->eParamMode = ANR_PARAM_MODE_NORMAL;
120     ANRConfigSettingParam(pANRCtx, pANRCtx->eParamMode, pANRCtx->stExpInfo.snr_mode);
121 #endif
122 
123     LOGD_ANR("%s(%d): bayernr %f %f %f %d %d %f", __FUNCTION__, __LINE__,
124              pANRCtx->stAuto.stBayernrParams.filtpar[0],
125              pANRCtx->stAuto.stBayernrParams.filtpar[4],
126              pANRCtx->stAuto.stBayernrParams.filtpar[8],
127              pANRCtx->stAuto.stBayernrParams.peaknoisesigma,
128              pANRCtx->stAuto.stBayernrParams.sw_bayernr_edge_filter_en,
129              pANRCtx->stAuto.stBayernrParams.sw_bayernr_filter_strength[0]);
130 
131 
132     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
133     return ANR_RET_SUCCESS;
134 }
135 
136 
137 
138 
139 //anr inint
ANRInit_json(ANRContext_t ** ppANRCtx,CamCalibDbV2Context_t * pCalibDbV2)140 ANRresult_t ANRInit_json(ANRContext_t **ppANRCtx, CamCalibDbV2Context_t *pCalibDbV2)
141 {
142     ANRContext_t * pANRCtx;
143 
144     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
145 
146     pANRCtx = (ANRContext_t *)malloc(sizeof(ANRContext_t));
147     if(pANRCtx == NULL) {
148         LOGE_ANR("%s(%d): malloc fail\n", __FUNCTION__, __LINE__);
149         return ANR_RET_NULL_POINTER;
150     }
151 
152     memset(pANRCtx, 0x00, sizeof(ANRContext_t));
153 
154     //gain state init
155     pANRCtx->stGainState.gainState = -1;
156     pANRCtx->stGainState.gain_th0[0]    = 2.0;
157     pANRCtx->stGainState.gain_th1[0]    = 8.0;
158     pANRCtx->stGainState.gain_th0[1]    = 32.0;
159     pANRCtx->stGainState.gain_th1[1]    = 128.0;
160 
161     pANRCtx->fLuma_SF_Strength = 1.0;
162     pANRCtx->fLuma_TF_Strength = 1.0;
163     pANRCtx->fChroma_SF_Strength = 1.0;
164     pANRCtx->fChroma_TF_Strength = 1.0;
165     pANRCtx->fRawnr_SF_Strength = 1.0;
166 
167     pANRCtx->eState = ANR_STATE_INITIALIZED;
168     *ppANRCtx = pANRCtx;
169 
170     pANRCtx->refYuvBit = 8;
171     pANRCtx->eMode = ANR_OP_MODE_AUTO;
172     pANRCtx->isIQParaUpdate = false;
173     pANRCtx->isGrayMode = false;
174 
175 
176 #if ANR_USE_JSON_PARA
177     //read v1 params from xml
178     CalibDbV2_BayerNrV1_t* calibv2_bayernr_v1 =
179         (CalibDbV2_BayerNrV1_t*)(CALIBDBV2_GET_MODULE_PTR(pCalibDbV2, bayernr_v1));
180     bayernr_calibdbV2_assign(&pANRCtx->bayernr_v1, calibv2_bayernr_v1);
181 
182     CalibDbV2_MFNR_t* calibv2_mfnr_v1 =
183         (CalibDbV2_MFNR_t*)(CALIBDBV2_GET_MODULE_PTR(pCalibDbV2, mfnr_v1));
184     mfnr_calibdbV2_assign(&pANRCtx->mfnr_v1, calibv2_mfnr_v1);
185 
186     CalibDbV2_YnrV1_t* calibv2_ynr_v1 =
187         (CalibDbV2_YnrV1_t*)(CALIBDBV2_GET_MODULE_PTR(pCalibDbV2, ynr_v1));
188     ynr_calibdbV2_assign(&pANRCtx->ynr_v1, calibv2_ynr_v1);
189 
190     CalibDbV2_UVNR_t* calibv2_uvnr_v1 =
191         (CalibDbV2_UVNR_t*)(CALIBDBV2_GET_MODULE_PTR(pCalibDbV2, uvnr_v1));
192     uvnr_calibdbV2_assign(&pANRCtx->uvnr_v1, calibv2_uvnr_v1);
193 #endif
194 
195 #if RK_SIMULATOR_HW
196     //just for v2 params from html
197     FILE *fp2 = fopen("rkaiq_anr_html_params.bin", "rb");
198     if(fp2 != NULL) {
199         memset(&pANRCtx->stAuto.stBayernrParams, 0, sizeof(RKAnr_Bayernr_Params_t));
200         fread(&pANRCtx->stAuto.stBayernrParams, 1, sizeof(RKAnr_Bayernr_Params_t), fp2);
201         memset(&pANRCtx->stAuto.stMfnrParams, 0, sizeof(RKAnr_Mfnr_Params_t));
202         fread(&pANRCtx->stAuto.stMfnrParams, 1, sizeof(RKAnr_Mfnr_Params_t), fp2);
203         memset(&pANRCtx->stAuto.stUvnrParams, 0, sizeof(RKAnr_Uvnr_Params_t));
204         fread(&pANRCtx->stAuto.stUvnrParams, 1, sizeof(RKAnr_Uvnr_Params_t), fp2);
205         memset(&pANRCtx->stAuto.stYnrParams, 0, sizeof(RKAnr_Ynr_Params_t));
206         fread(&pANRCtx->stAuto.stYnrParams, 1, sizeof(RKAnr_Ynr_Params_t), fp2);
207         LOGD_ANR("oyyf: %s:(%d) read anr param html file sucess! \n", __FUNCTION__, __LINE__);
208     } else {
209         LOGE_ANR("oyyf: %s:(%d) read anr param html file failed! \n", __FUNCTION__, __LINE__);
210         return ANR_RET_FAILURE;
211     }
212 #endif
213 
214 #if ANR_USE_JSON_PARA
215     pANRCtx->stExpInfo.snr_mode = 0;
216     pANRCtx->eParamMode = ANR_PARAM_MODE_NORMAL;
217     ANRConfigParamJson(pANRCtx, pANRCtx->eParamMode, pANRCtx->stExpInfo.snr_mode);
218 #endif
219 
220     LOGD_ANR("%s(%d): bayernr %f %f %f %d %d %f", __FUNCTION__, __LINE__,
221              pANRCtx->stAuto.stBayernrParams.filtpar[0],
222              pANRCtx->stAuto.stBayernrParams.filtpar[4],
223              pANRCtx->stAuto.stBayernrParams.filtpar[8],
224              pANRCtx->stAuto.stBayernrParams.peaknoisesigma,
225              pANRCtx->stAuto.stBayernrParams.sw_bayernr_edge_filter_en,
226              pANRCtx->stAuto.stBayernrParams.sw_bayernr_filter_strength[0]);
227 
228 
229     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
230     return ANR_RET_SUCCESS;
231 }
232 
233 
234 
235 //anr release
ANRRelease(ANRContext_t * pANRCtx)236 ANRresult_t ANRRelease(ANRContext_t *pANRCtx)
237 {
238     ANRresult_t result = ANR_RET_SUCCESS;
239     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
240     if(pANRCtx == NULL) {
241         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
242         return ANR_RET_NULL_POINTER;
243     }
244 
245     result = ANRStop(pANRCtx);
246     if (result != ANR_RET_SUCCESS) {
247         LOGE_ANR( "%s: ANRStop() failed!\n", __FUNCTION__);
248         return (result);
249     }
250 
251     // check state
252     if ((ANR_STATE_RUNNING == pANRCtx->eState)
253             || (ANR_STATE_LOCKED == pANRCtx->eState)) {
254         return (ANR_RET_BUSY);
255     }
256 
257 #if ANR_USE_JSON_PARA
258     bayernr_calibdbV2_free(&pANRCtx->bayernr_v1);
259     mfnr_calibdbV2_free(&pANRCtx->mfnr_v1);
260     uvnr_calibdbV2_free(&pANRCtx->uvnr_v1);
261     ynr_calibdbV2_free(&pANRCtx->ynr_v1);
262 #endif
263 
264     memset(pANRCtx, 0x00, sizeof(ANRContext_t));
265     free(pANRCtx);
266 
267     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
268     return ANR_RET_SUCCESS;
269 }
270 
271 //anr config
ANRPrepare(ANRContext_t * pANRCtx,ANRConfig_t * pANRConfig)272 ANRresult_t ANRPrepare(ANRContext_t *pANRCtx, ANRConfig_t* pANRConfig)
273 {
274     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
275 
276     if(pANRCtx == NULL) {
277         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
278         return ANR_RET_INVALID_PARM;
279     }
280 
281     if(pANRConfig == NULL) {
282         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
283         return ANR_RET_INVALID_PARM;
284     }
285 
286     //pANRCtx->eMode = pANRConfig->eMode;
287     //pANRCtx->eState = pANRConfig->eState;
288     //pANRCtx->refYuvBit = pANRConfig->refYuvBit;
289 
290     //update iq calibdb to context
291     if(!!(pANRCtx->prepare_type & RK_AIQ_ALGO_CONFTYPE_UPDATECALIB)) {
292         ANRIQParaUpdate(pANRCtx);
293     }
294 
295     ANRStart(pANRCtx);
296 
297     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
298     return ANR_RET_SUCCESS;
299 }
300 
301 //anr reconfig
ANRReConfig(ANRContext_t * pANRCtx,ANRConfig_t * pANRConfig)302 ANRresult_t ANRReConfig(ANRContext_t *pANRCtx, ANRConfig_t* pANRConfig)
303 {
304     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
305     //need todo what?
306 
307     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
308     return ANR_RET_SUCCESS;
309 }
310 
311 //anr reconfig
ANRIQParaUpdate(ANRContext_t * pANRCtx)312 ANRresult_t ANRIQParaUpdate(ANRContext_t *pANRCtx)
313 {
314     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
315     //need todo what?
316 
317     if(pANRCtx->isIQParaUpdate) {
318         LOGD_ANR("IQ data reconfig\n");
319 #if (ANR_USE_JSON_PARA)
320         ANRConfigParamJson(pANRCtx, pANRCtx->eParamMode, pANRCtx->stExpInfo.snr_mode);
321 #else
322         ANRConfigSettingParam(pANRCtx, pANRCtx->eParamMode, pANRCtx->stExpInfo.snr_mode);
323 #endif
324 
325 
326         pANRCtx->isIQParaUpdate = false;
327     }
328 
329     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
330     return ANR_RET_SUCCESS;
331 }
332 
333 
334 //anr preprocess
ANRPreProcess(ANRContext_t * pANRCtx)335 ANRresult_t ANRPreProcess(ANRContext_t *pANRCtx)
336 {
337     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
338     //need todo what?
339 
340     ANRIQParaUpdate(pANRCtx);
341 
342     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
343     return ANR_RET_SUCCESS;
344 }
345 
346 //anr process
ANRProcess(ANRContext_t * pANRCtx,ANRExpInfo_t * pExpInfo)347 ANRresult_t ANRProcess(ANRContext_t *pANRCtx, ANRExpInfo_t *pExpInfo)
348 {
349     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
350     ANRParamMode_t mode = ANR_PARAM_MODE_INVALID;
351 
352     if(pANRCtx == NULL) {
353         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
354         return ANR_RET_INVALID_PARM;
355     }
356 
357     if(pExpInfo == NULL) {
358         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
359         return ANR_RET_INVALID_PARM;
360     }
361 
362     if(pANRCtx->eState != ANR_STATE_RUNNING) {
363         return ANR_RET_SUCCESS;
364     }
365 
366     ANRGainRatioProcess(&pANRCtx->stGainState, pExpInfo);
367 
368     ANRParamModeProcess(pANRCtx, pExpInfo, &mode);
369 #if ANR_USE_JSON_PARA
370     pExpInfo->mfnr_mode_3to1 = pANRCtx->mfnr_v1.TuningPara.mode_3to1;
371 #else
372     pExpInfo->mfnr_mode_3to1 = pANRCtx->stMfnrCalib.mode_3to1;
373 #endif
374 
375     if(pExpInfo->mfnr_mode_3to1) {
376         pExpInfo->snr_mode = pExpInfo->pre_snr_mode;
377     } else {
378         pExpInfo->snr_mode = pExpInfo->cur_snr_mode;
379     }
380 
381     if(pANRCtx->eMode == ANR_OP_MODE_AUTO) {
382 
383         LOGD_ANR("%s(%d): refYuvBit:%d\n", __FUNCTION__, __LINE__, pANRCtx->refYuvBit);
384 
385 #if ANR_USE_XML_FILE
386         if(pExpInfo->snr_mode != pANRCtx->stExpInfo.snr_mode || pANRCtx->eParamMode != mode) {
387             LOGD_ANR("param mode:%d snr_mode:%d\n", mode, pExpInfo->snr_mode);
388             pANRCtx->eParamMode = mode;
389 #if(ANR_USE_JSON_PARA)
390             ANRConfigParamJson(pANRCtx, pANRCtx->eParamMode, pExpInfo->snr_mode);
391 #else
392             ANRConfigSettingParam(pANRCtx, pANRCtx->eParamMode, pExpInfo->snr_mode);
393 #endif
394         }
395 #endif
396 
397         //select param
398         select_bayernr_params_by_ISO(&pANRCtx->stAuto.stBayernrParams, &pANRCtx->stAuto.stBayernrParamSelect, pExpInfo);
399         select_mfnr_params_by_ISO(&pANRCtx->stAuto.stMfnrParams, &pANRCtx->stAuto.stMfnrParamSelect, pExpInfo, pANRCtx->refYuvBit);
400         select_ynr_params_by_ISO(&pANRCtx->stAuto.stYnrParams, &pANRCtx->stAuto.stYnrParamSelect, pExpInfo, pANRCtx->refYuvBit);
401         select_uvnr_params_by_ISO(&pANRCtx->stAuto.stUvnrParams, &pANRCtx->stAuto.stUvnrParamSelect, pExpInfo);
402         mfnr_dynamic_calc(&pANRCtx->stAuto.stMfnr_dynamic, pExpInfo);
403 
404     } else if(pANRCtx->eMode == ANR_OP_MODE_MANUAL) {
405         //TODO
406     }
407 
408     memcpy(&pANRCtx->stExpInfo, pExpInfo, sizeof(ANRExpInfo_t));
409 
410     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
411     return ANR_RET_SUCCESS;
412 
413 }
414 
ANRSetGainMode(ANRProcResult_t * pANRResult)415 ANRresult_t ANRSetGainMode(ANRProcResult_t* pANRResult)
416 {
417     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
418 
419     if(pANRResult == NULL) {
420         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
421         return ANR_RET_INVALID_PARM;
422     }
423 
424     if(pANRResult->stGainFix.gain_table_en) {
425         pANRResult->stMfnrFix.gain_en = 0;
426         pANRResult->stUvnrFix.nr_gain_en = 0;
427 
428     } else {
429         pANRResult->stMfnrFix.gain_en = 1;
430         pANRResult->stUvnrFix.nr_gain_en = 1;
431     }
432 
433     return ANR_RET_SUCCESS;
434 }
435 
436 
437 //anr get result
ANRGetProcResult(ANRContext_t * pANRCtx,ANRProcResult_t * pANRResult)438 ANRresult_t ANRGetProcResult(ANRContext_t *pANRCtx, ANRProcResult_t* pANRResult)
439 {
440     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
441 
442     if(pANRCtx == NULL) {
443         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
444         return ANR_RET_INVALID_PARM;
445     }
446 
447     if(pANRResult == NULL) {
448         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
449         return ANR_RET_INVALID_PARM;
450     }
451 
452     if(pANRCtx->eMode == ANR_OP_MODE_AUTO) {
453 
454         pANRResult->stBayernrParamSelect = pANRCtx->stAuto.stBayernrParamSelect;
455         pANRResult->stMfnrParamSelect = pANRCtx->stAuto.stMfnrParamSelect;
456         pANRResult->stYnrParamSelect = pANRCtx->stAuto.stYnrParamSelect;
457         pANRResult->stUvnrParamSelect = pANRCtx->stAuto.stUvnrParamSelect;
458 
459         pANRResult->bayernrEn = pANRCtx->stAuto.bayernrEn;
460         pANRResult->mfnrEn = pANRCtx->stAuto.mfnrEn;
461         pANRResult->ynrEN = pANRCtx->stAuto.ynrEn;
462         pANRResult->uvnrEn = pANRCtx->stAuto.uvnrEn;
463 
464         if(pANRCtx->stAuto.mfnrEn && pANRCtx->stAuto.stMfnr_dynamic.enable) {
465             pANRResult->mfnrEn = pANRCtx->stAuto.stMfnr_dynamic.mfnr_enable_state;
466         }
467 
468     } else if(pANRCtx->eMode == ANR_OP_MODE_MANUAL) {
469         //TODO
470         pANRResult->bayernrEn = pANRCtx->stManual.bayernrEn;
471         pANRResult->stBayernrParamSelect = pANRCtx->stManual.stBayernrParamSelect;
472         pANRResult->mfnrEn = pANRCtx->stManual.mfnrEn;
473         pANRResult->stMfnrParamSelect = pANRCtx->stManual.stMfnrParamSelect;
474         pANRResult->ynrEN = pANRCtx->stManual.ynrEn;
475         pANRResult->stYnrParamSelect = pANRCtx->stManual.stYnrParamSelect;
476         pANRResult->uvnrEn = pANRCtx->stManual.uvnrEn;
477         pANRResult->stUvnrParamSelect = pANRCtx->stManual.stUvnrParamSelect;
478         pANRCtx->fLuma_TF_Strength = 1.0;
479         pANRCtx->fLuma_SF_Strength = 1.0;
480         pANRCtx->fChroma_SF_Strength = 1.0;
481         pANRCtx->fChroma_TF_Strength = 1.0;
482         pANRCtx->fRawnr_SF_Strength = 1.0;
483     }
484 
485     //for bw setting
486     if(pANRCtx->isGrayMode) {
487         LOGD_ANR("anr: set gray mode!\n");
488         for(int i = 0; i < MFNR_MAX_LVL_UV; i++) {
489             pANRResult->stMfnrParamSelect.weight_limit_uv[i] = MFNR_MAX_WEIGHT_LIMIT_UV;
490         }
491     }
492 
493     //transfer to reg value
494     bayernr_fix_tranfer(&pANRResult->stBayernrParamSelect, &pANRResult->stBayernrFix, pANRCtx->fRawnr_SF_Strength);
495     mfnr_fix_transfer(&pANRResult->stMfnrParamSelect, &pANRResult->stMfnrFix, &pANRCtx->stExpInfo, pANRCtx->stGainState.ratio, pANRCtx->fLuma_TF_Strength,  pANRCtx->fChroma_TF_Strength);
496     ynr_fix_transfer(&pANRResult->stYnrParamSelect, &pANRResult->stYnrFix, pANRCtx->stGainState.ratio, pANRCtx->fLuma_SF_Strength);
497     uvnr_fix_transfer(&pANRResult->stUvnrParamSelect, &pANRResult->stUvnrFix, &pANRCtx->stExpInfo, pANRCtx->stGainState.ratio, pANRCtx->fChroma_SF_Strength);
498     gain_fix_transfer(&pANRResult->stMfnrParamSelect, &pANRResult->stGainFix, &pANRCtx->stExpInfo, pANRCtx->stGainState.ratio);
499     pANRResult->stBayernrFix.rawnr_en = pANRResult->bayernrEn;
500     pANRResult->stMfnrFix.tnr_en = pANRResult->mfnrEn;
501     pANRResult->stYnrFix.ynr_en = pANRResult->ynrEN;
502     pANRResult->stUvnrFix.uvnr_en = pANRResult->uvnrEn;
503 
504 #if(ANR_USE_JSON_PARA)
505     pANRResult->stMfnrFix.mode = pANRCtx->mfnr_v1.TuningPara.mode_3to1;
506     pANRResult->stGainFix.gain_table_en = pANRCtx->mfnr_v1.TuningPara.local_gain_en;
507 #else
508     pANRResult->stMfnrFix.mode = pANRCtx->stMfnrCalib.mode_3to1;
509     pANRResult->stGainFix.gain_table_en = pANRCtx->stMfnrCalib.local_gain_en;
510 #endif
511 
512 
513     ANRSetGainMode(pANRResult);
514 
515     //for bw setting
516     if(pANRCtx->isGrayMode) {
517         //uvnr disable
518         pANRResult->stUvnrFix.uvnr_step1_en = 0;
519         pANRResult->stUvnrFix.uvnr_step2_en = 0;
520     }
521 
522     LOGD_ANR("%s:%d xml:local:%d mode:%d  reg: local gain:%d  mfnr gain:%d mode:%d\n",
523              __FUNCTION__, __LINE__,
524              pANRResult->stGainFix.gain_table_en,
525              pANRResult->stMfnrFix.mode,
526              pANRResult->stGainFix.gain_table_en,
527              pANRResult->stMfnrFix.gain_en,
528              pANRResult->stMfnrFix.mode);
529 
530     //select motion params
531 #if ANR_USE_JSON_PARA
532     pANRResult->stMotion = pANRCtx->stMotion;
533 #else
534     int mode_idx = 0;
535     if(pANRCtx->eParamMode == ANR_PARAM_MODE_NORMAL) {
536         mfnr_get_mode_cell_idx_by_name(&pANRCtx->stMfnrCalib, "normal", &mode_idx);
537     } else if(pANRCtx->eParamMode == ANR_PARAM_MODE_HDR) {
538         mfnr_get_mode_cell_idx_by_name(&pANRCtx->stMfnrCalib, "hdr", &mode_idx);
539     } else if(pANRCtx->eParamMode == ANR_PARAM_MODE_GRAY) {
540         mfnr_get_mode_cell_idx_by_name(&pANRCtx->stMfnrCalib, "gray", &mode_idx);
541     } else {
542         LOGE_ANR("%s(%d): not support param mode!\n", __FUNCTION__, __LINE__);
543     }
544     pANRResult->stMotion = pANRCtx->stMfnrCalib.mode_cell[mode_idx].motion;
545 #endif
546     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
547     return ANR_RET_SUCCESS;
548 }
549 
ANRGainRatioProcess(ANRGainState_t * pGainState,ANRExpInfo_t * pExpInfo)550 ANRresult_t ANRGainRatioProcess(ANRGainState_t *pGainState, ANRExpInfo_t *pExpInfo)
551 {
552     LOGI_ANR("%s(%d): enter!\n", __FUNCTION__, __LINE__);
553 
554     if(pGainState == NULL) {
555         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
556         return ANR_RET_INVALID_PARM;
557     }
558 
559     if(pExpInfo == NULL) {
560         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
561         return ANR_RET_INVALID_PARM;
562     }
563 
564     float gain_cur = pExpInfo->arAGain[pExpInfo->hdr_mode] * pExpInfo->arDGain[pExpInfo->hdr_mode];
565     float th[2];
566     float gain_th0[2];
567     float gain_th1[2];
568     for(int i = 0; i < 2; i++) {
569         gain_th0[i]     = pGainState->gain_th0[i];
570         gain_th1[i]     = pGainState->gain_th1[i];
571         th[i]           = pow(2.0, (log2(gain_th0[i]) + log2(gain_th1[i])) / 2);
572     }
573 
574     pGainState->gain_cur = gain_cur;
575 
576     int gain_stat_full = -1;
577     int gain_stat_full_last = pGainState->gain_stat_full_last;
578     int gain_stat_last = pGainState->gainState_last;
579     int gain_stat_cur  = -1;
580     int gain_stat = -1;
581 
582     if(gain_cur <= gain_th0[0])
583     {
584         gain_stat_full = 0;
585         gain_stat_cur = 0;
586     }
587     else if(gain_cur <= gain_th1[0] && gain_cur >= gain_th0[0])
588     {
589         gain_stat_full = 1;
590     }
591     else if(gain_cur <= gain_th0[1] && gain_cur >= gain_th1[0])
592     {
593         gain_stat_full = 2;
594         gain_stat_cur = 1;
595     }
596     else if(gain_cur <= gain_th1[1] && gain_cur >= gain_th0[1])
597     {
598         gain_stat_full = 3;
599     }
600     else if(gain_cur >= gain_th1[1])
601     {
602         gain_stat_full = 4;
603         gain_stat_cur = 2;
604     }
605     if(gain_stat_last == -1 || (abs(gain_stat_full - gain_stat_full_last) >= 2 && gain_stat_cur == -1)) {
606         if(gain_cur <= th[0])
607             gain_stat_cur = 0;
608         else if(gain_cur <= th[1])
609             gain_stat_cur = 1;
610         else
611             gain_stat_cur = 2;
612     }
613     if (gain_stat_cur != -1) {
614         gain_stat_last      = gain_stat_cur;
615         gain_stat_full_last = gain_stat_full;
616         gain_stat       = gain_stat_cur;
617     } else {
618         gain_stat       = gain_stat_last;
619     }
620     if (gain_stat == 0) {
621         pGainState->ratio = 16;
622     } else if (gain_stat == 1) {
623         pGainState->ratio = 1;
624     } else {
625         pGainState->ratio = 1.0 / 16.0;
626     }
627 
628     pGainState->gain_stat_full_last     = gain_stat_full_last;
629     pGainState->gainState       = gain_stat;
630     pGainState->gainState_last  = gain_stat_last;
631 
632     LOGD_ANR("%s:%d gain_cur:%f gain th %f %fd %f %f ratio:%f gain_state:%d %d full    %d %d\n",
633              __FUNCTION__, __LINE__,
634              gain_cur,
635              gain_th0[0], gain_th0[1],
636              gain_th1[0], gain_th1[1],
637              pGainState->ratio,
638              pGainState->gainState_last,
639              pGainState->gainState,
640              pGainState->gain_stat_full_last,
641              gain_stat_full);
642 
643 
644     LOGI_ANR("%s(%d): exit!\n", __FUNCTION__, __LINE__);
645 
646     return ANR_RET_SUCCESS;
647 }
648 
ANRConfigSettingParam(ANRContext_t * pANRCtx,ANRParamMode_t eParamMode,int snr_mode)649 ANRresult_t ANRConfigSettingParam(ANRContext_t *pANRCtx, ANRParamMode_t eParamMode, int snr_mode)
650 {
651     char snr_name[CALIBDB_NR_SHARP_NAME_LENGTH];
652     char param_mode_name[CALIBDB_MAX_MODE_NAME_LENGTH];
653     memset(param_mode_name, 0x00, sizeof(param_mode_name));
654     memset(snr_name, 0x00, sizeof(snr_name));
655 
656     if(pANRCtx == NULL) {
657         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
658         return ANR_RET_INVALID_PARM;
659     }
660 
661     //select param mode first
662     if(eParamMode == ANR_PARAM_MODE_NORMAL) {
663         sprintf(param_mode_name, "%s", "normal");
664     } else if(eParamMode == ANR_PARAM_MODE_HDR) {
665         sprintf(param_mode_name, "%s", "hdr");
666     } else if(eParamMode == ANR_PARAM_MODE_GRAY) {
667         sprintf(param_mode_name, "%s", "gray");
668     } else {
669         LOGE_ANR("%s(%d): not support param mode!\n", __FUNCTION__, __LINE__);
670         sprintf(param_mode_name, "%s", "normal");
671     }
672 
673 
674     //then select snr mode next
675     if(snr_mode == 1) {
676         sprintf(snr_name, "%s", "HSNR");
677     } else if(snr_mode == 0) {
678         sprintf(snr_name, "%s", "LSNR");
679     } else {
680         LOGE_ANR("%s(%d): not support snr mode!\n", __FUNCTION__, __LINE__);
681         sprintf(snr_name, "%s", "LSNR");
682     }
683 
684     pANRCtx->stAuto.bayernrEn = pANRCtx->stBayernrCalib.enable;
685     bayernr_config_setting_param(&pANRCtx->stAuto.stBayernrParams, &pANRCtx->stBayernrCalib, param_mode_name, snr_name);
686 
687     pANRCtx->stAuto.uvnrEn = pANRCtx->stUvnrCalib.enable;
688     uvnr_config_setting_param(&pANRCtx->stAuto.stUvnrParams, &pANRCtx->stUvnrCalib, param_mode_name, snr_name);
689 
690     pANRCtx->stAuto.ynrEn = pANRCtx->stYnrCalib.enable;
691     ynr_config_setting_param(&pANRCtx->stAuto.stYnrParams, &pANRCtx->stYnrCalib, param_mode_name, snr_name);
692 
693     pANRCtx->stAuto.mfnrEn = pANRCtx->stMfnrCalib.enable;
694     mfnr_config_setting_param(&pANRCtx->stAuto.stMfnrParams, &pANRCtx->stMfnrCalib, param_mode_name, snr_name);
695     mfnr_config_dynamic_param(&pANRCtx->stAuto.stMfnr_dynamic, &pANRCtx->stMfnrCalib, param_mode_name);
696     return ANR_RET_SUCCESS;
697 }
698 
ANRParamModeProcess(ANRContext_t * pANRCtx,ANRExpInfo_t * pExpInfo,ANRParamMode_t * mode)699 ANRresult_t ANRParamModeProcess(ANRContext_t *pANRCtx, ANRExpInfo_t *pExpInfo, ANRParamMode_t *mode) {
700     ANRresult_t res  = ANR_RET_SUCCESS;
701     *mode = pANRCtx->eParamMode;
702 
703     if(pANRCtx == NULL) {
704         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
705         return ANR_RET_INVALID_PARM;
706     }
707 
708     if(pANRCtx->isGrayMode) {
709         *mode = ANR_PARAM_MODE_GRAY;
710     } else if(pExpInfo->hdr_mode == 0) {
711         *mode = ANR_PARAM_MODE_NORMAL;
712     } else if(pExpInfo->hdr_mode >= 1) {
713         *mode = ANR_PARAM_MODE_HDR;
714     } else {
715         *mode = ANR_PARAM_MODE_NORMAL;
716     }
717 
718     return res;
719 }
720 
721 
722 
ANRConfigParamJson(ANRContext_t * pANRCtx,ANRParamMode_t eParamMode,int snr_mode)723 ANRresult_t ANRConfigParamJson(ANRContext_t *pANRCtx, ANRParamMode_t eParamMode, int snr_mode)
724 {
725     char snr_name[CALIBDB_NR_SHARP_NAME_LENGTH];
726     char param_mode_name[CALIBDB_MAX_MODE_NAME_LENGTH];
727     memset(param_mode_name, 0x00, sizeof(param_mode_name));
728     memset(snr_name, 0x00, sizeof(snr_name));
729 
730     if(pANRCtx == NULL) {
731         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
732         return ANR_RET_INVALID_PARM;
733     }
734 
735     //select param mode first
736     if(eParamMode == ANR_PARAM_MODE_NORMAL) {
737         sprintf(param_mode_name, "%s", "normal");
738     } else if(eParamMode == ANR_PARAM_MODE_HDR) {
739         sprintf(param_mode_name, "%s", "hdr");
740     } else if(eParamMode == ANR_PARAM_MODE_GRAY) {
741         sprintf(param_mode_name, "%s", "gray");
742     } else {
743         LOGE_ANR("%s(%d): not support param mode!\n", __FUNCTION__, __LINE__);
744         sprintf(param_mode_name, "%s", "normal");
745     }
746 
747 
748     //then select snr mode next
749     if(snr_mode == 1) {
750         sprintf(snr_name, "%s", "HSNR");
751     } else if(snr_mode == 0) {
752         sprintf(snr_name, "%s", "LSNR");
753     } else {
754         LOGE_ANR("%s(%d): not support snr mode!\n", __FUNCTION__, __LINE__);
755         sprintf(snr_name, "%s", "LSNR");
756     }
757 
758     pANRCtx->stAuto.bayernrEn = pANRCtx->bayernr_v1.TuningPara.enable;
759     bayernr_config_setting_param_json(&pANRCtx->stAuto.stBayernrParams, &pANRCtx->bayernr_v1, param_mode_name, snr_name);
760 
761     pANRCtx->stAuto.uvnrEn = pANRCtx->uvnr_v1.TuningPara.enable;
762     uvnr_config_setting_param_json(&pANRCtx->stAuto.stUvnrParams, &pANRCtx->uvnr_v1, param_mode_name, snr_name);
763 
764     pANRCtx->stAuto.ynrEn = pANRCtx->ynr_v1.TuningPara.enable;
765     ynr_config_setting_param_json(&pANRCtx->stAuto.stYnrParams, &pANRCtx->ynr_v1, param_mode_name, snr_name);
766 
767     pANRCtx->stAuto.mfnrEn = pANRCtx->mfnr_v1.TuningPara.enable;
768     mfnr_config_setting_param_json(&pANRCtx->stAuto.stMfnrParams, &pANRCtx->mfnr_v1, param_mode_name, snr_name);
769     mfnr_config_dynamic_param_json(&pANRCtx->stAuto.stMfnr_dynamic, &pANRCtx->mfnr_v1, param_mode_name);
770     mfnr_config_motion_param_json(&pANRCtx->stMotion, &pANRCtx->mfnr_v1, param_mode_name);
771 
772     return ANR_RET_SUCCESS;
773 }
774 
775 RKAIQ_END_DECLARE
776 
777 
778