xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/algos/asharp4/rk_aiq_asharp_algo_itf_v4.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * rk_aiq_algo_anr_itf.c
3  *
4  *  Copyright (c) 2019 Rockchip Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 
20 #include "asharp4/rk_aiq_asharp_algo_itf_v4.h"
21 #include "asharp4/rk_aiq_asharp_algo_v4.h"
22 #include "rk_aiq_algo_types.h"
23 
24 RKAIQ_BEGIN_DECLARE
25 
26 typedef struct _RkAiqAlgoContext {
27     Asharp_Context_V4_t AsharpCtx;
28 } RkAiqAlgoContext;
29 
30 
31 static XCamReturn
create_context(RkAiqAlgoContext ** context,const AlgoCtxInstanceCfg * cfg)32 create_context(RkAiqAlgoContext **context, const AlgoCtxInstanceCfg* cfg)
33 {
34 
35     XCamReturn result = XCAM_RETURN_NO_ERROR;
36     LOGI_ASHARP("%s:oyyf (enter)\n", __FUNCTION__ );
37 
38 #if 1
39     Asharp_Context_V4_t* pAsharpCtx = NULL;
40 #if ASHARP_USE_JSON_FILE_V4
41     Asharp4_result_t ret = Asharp_Init_V4(&pAsharpCtx, cfg->calibv2);
42 #endif
43 
44     if(ret != ASHARP4_RET_SUCCESS) {
45         result = XCAM_RETURN_ERROR_FAILED;
46         LOGE_ASHARP("%s: Initializaion ANR failed (%d)\n", __FUNCTION__, ret);
47     } else {
48         *context = (RkAiqAlgoContext *)(pAsharpCtx);
49     }
50 #endif
51 
52     LOGI_ASHARP("%s:oyyf (exit)\n", __FUNCTION__ );
53     return result;
54 }
55 
56 static XCamReturn
destroy_context(RkAiqAlgoContext * context)57 destroy_context(RkAiqAlgoContext *context)
58 {
59     XCamReturn result = XCAM_RETURN_NO_ERROR;
60 
61     LOGI_ASHARP("%s: oyyf (enter)\n", __FUNCTION__ );
62 
63 #if 1
64     Asharp_Context_V4_t* pAsharpCtx = (Asharp_Context_V4_t*)context;
65     Asharp4_result_t ret = Asharp_Release_V4(pAsharpCtx);
66     if(ret != ASHARP4_RET_SUCCESS) {
67         result = XCAM_RETURN_ERROR_FAILED;
68         LOGE_ASHARP("%s: release ANR failed (%d)\n", __FUNCTION__, ret);
69     }
70 #endif
71 
72     LOGI_ASHARP("%s: (exit)\n", __FUNCTION__ );
73     return result;
74 }
75 
76 static XCamReturn
prepare(RkAiqAlgoCom * params)77 prepare(RkAiqAlgoCom* params)
78 {
79     XCamReturn result = XCAM_RETURN_NO_ERROR;
80 
81     LOGI_ASHARP("%s: oyyf (enter)\n", __FUNCTION__ );
82 
83     Asharp_Context_V4_t* pAsharpCtx = (Asharp_Context_V4_t *)params->ctx;
84     RkAiqAlgoConfigAsharpV4* pCfgParam = (RkAiqAlgoConfigAsharpV4*)params;
85     pAsharpCtx->prepare_type = params->u.prepare.conf_type;
86 
87     if(!!(params->u.prepare.conf_type & RK_AIQ_ALGO_CONFTYPE_UPDATECALIB )) {
88 #if(ASHARP_USE_JSON_FILE_V4)
89         CalibDbV2_SharpV4_t *calibv2_sharp =
90             (CalibDbV2_SharpV4_t *)(CALIBDBV2_GET_MODULE_PTR(pCfgParam->com.u.prepare.calibv2, sharp_v4));
91         pAsharpCtx->sharp_v4 = *calibv2_sharp;
92 #endif
93         pAsharpCtx->isIQParaUpdate = true;
94         pAsharpCtx->isReCalculate |= 1;
95     }
96 
97     Asharp4_result_t ret = Asharp_Prepare_V4(pAsharpCtx, &pCfgParam->stAsharpConfig);
98     if(ret != ASHARP4_RET_SUCCESS) {
99         result = XCAM_RETURN_ERROR_FAILED;
100         LOGE_ASHARP("%s: config ANR failed (%d)\n", __FUNCTION__, ret);
101     }
102 
103     LOGI_ASHARP("%s: oyyf (exit)\n", __FUNCTION__ );
104     return result;
105 }
106 
107 static XCamReturn
pre_process(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)108 pre_process(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
109 {
110     XCamReturn result = XCAM_RETURN_NO_ERROR;
111     bool oldGrayMode = false;
112 
113     LOGD_ASHARP("%s: oyyf (enter)\n", __FUNCTION__ );
114     Asharp_Context_V4_t* pAsharpCtx = (Asharp_Context_V4_t *)inparams->ctx;
115 
116     RkAiqAlgoPreAsharpV4* pAsharpPreParams = (RkAiqAlgoPreAsharpV4*)inparams;
117 
118     oldGrayMode = pAsharpCtx->isGrayMode;
119     if (pAsharpPreParams->com.u.proc.gray_mode) {
120         pAsharpCtx->isGrayMode = true;
121     } else {
122         pAsharpCtx->isGrayMode = false;
123     }
124 
125     if(oldGrayMode != pAsharpCtx->isGrayMode) {
126         pAsharpCtx->isReCalculate |= 1;
127     }
128 
129     Asharp4_result_t ret = Asharp_PreProcess_V4(pAsharpCtx);
130     if(ret != ASHARP4_RET_SUCCESS) {
131         result = XCAM_RETURN_ERROR_FAILED;
132         LOGE_ASHARP("%s: ANRPreProcess failed (%d)\n", __FUNCTION__, ret);
133     }
134 
135     LOGD_ASHARP("%s: oyyf (exit)\n", __FUNCTION__ );
136     return result;
137 }
138 
139 static XCamReturn
processing(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)140 processing(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
141 {
142     XCamReturn result = XCAM_RETURN_NO_ERROR;
143     int DeltaIso = 0;
144 
145     LOGD_ASHARP("%s:oyyf (enter)\n", __FUNCTION__ );
146 
147 #if 1
148     RkAiqAlgoProcAsharpV4* pAsharpProcParams = (RkAiqAlgoProcAsharpV4*)inparams;
149     RkAiqAlgoProcResAsharpV4* pAsharpProcResParams = (RkAiqAlgoProcResAsharpV4*)outparams;
150     Asharp_Context_V4_t* pAsharpCtx = (Asharp_Context_V4_t *)inparams->ctx;
151     Asharp4_ExpInfo_t stExpInfo;
152     memset(&stExpInfo, 0x00, sizeof(Asharp4_ExpInfo_t));
153 
154     LOGD_ASHARP("%s:%d init:%d hdr mode:%d  \n",
155                 __FUNCTION__, __LINE__,
156                 inparams->u.proc.init,
157                 pAsharpProcParams->hdr_mode);
158 
159     stExpInfo.hdr_mode = 0; //pAsharpProcParams->hdr_mode;
160     for(int i = 0; i < 3; i++) {
161         stExpInfo.arIso[i] = 50;
162         stExpInfo.arAGain[i] = 1.0;
163         stExpInfo.arDGain[i] = 1.0;
164         stExpInfo.arTime[i] = 0.01;
165     }
166 
167     if(pAsharpProcParams->hdr_mode == RK_AIQ_WORKING_MODE_NORMAL) {
168         stExpInfo.hdr_mode = 0;
169     } else if(pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_2_FRAME_HDR
170               || pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_2_LINE_HDR ) {
171         stExpInfo.hdr_mode = 1;
172     } else if(pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_3_FRAME_HDR
173               || pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_3_LINE_HDR ) {
174         stExpInfo.hdr_mode = 2;
175     }
176     stExpInfo.snr_mode = 0;
177 
178 #if 0// TODO Merge:
179     XCamVideoBuffer* xCamAePreRes = pAsharpProcParams->com.u.proc.res_comb->ae_pre_res;
180     RkAiqAlgoPreResAe* pAEPreRes = nullptr;
181     if (xCamAePreRes) {
182         // xCamAePreRes->ref(xCamAePreRes);
183         pAEPreRes = (RkAiqAlgoPreResAe*)xCamAePreRes->map(xCamAePreRes);
184         if (!pAEPreRes) {
185             LOGE_ASHARP("ae pre result is null");
186         } else {
187 
188         }
189         // xCamAePreRes->unref(xCamAePreRes);
190     }
191 #endif
192 
193     RKAiqAecExpInfo_t *curExp = pAsharpProcParams->com.u.proc.curExp;
194     if(curExp != NULL) {
195         stExpInfo.snr_mode = curExp->CISFeature.SNR;
196         if(pAsharpProcParams->hdr_mode == RK_AIQ_WORKING_MODE_NORMAL) {
197             stExpInfo.hdr_mode = 0;
198             if(curExp->LinearExp.exp_real_params.analog_gain < 1.0) {
199                 stExpInfo.arAGain[0] = 1.0;
200                 LOGW_ANR("leanr mode again is wrong, use 1.0 instead\n");
201             } else {
202                 stExpInfo.arAGain[0] = curExp->LinearExp.exp_real_params.analog_gain;
203             }
204             if(curExp->LinearExp.exp_real_params.digital_gain < 1.0) {
205                 stExpInfo.arDGain[0] = 1.0;
206                 LOGW_ANR("leanr mode dgain is wrong, use 1.0 instead\n");
207             } else {
208                 stExpInfo.arDGain[0] = curExp->LinearExp.exp_real_params.digital_gain;
209             }
210             if(curExp->LinearExp.exp_real_params.isp_dgain < 1.0) {
211                 stExpInfo.arDGain[0] *= 1.0;
212                 LOGW_ANR("leanr mode dgain is wrong, use 1.0 instead\n");
213             } else {
214                 stExpInfo.arDGain[0] *= curExp->LinearExp.exp_real_params.isp_dgain;
215             }
216             stExpInfo.arTime[0] = curExp->LinearExp.exp_real_params.integration_time;
217             stExpInfo.arIso[0] = stExpInfo.arAGain[0] * stExpInfo.arDGain[0] * 50;
218         } else {
219             for(int i = 0; i < 3; i++) {
220                 if(curExp->HdrExp[i].exp_real_params.analog_gain < 1.0) {
221                     stExpInfo.arAGain[i] = 1.0;
222                     LOGW_ANR("hdr mode again is wrong, use 1.0 instead\n");
223                 } else {
224                     stExpInfo.arAGain[i] = curExp->HdrExp[i].exp_real_params.analog_gain;
225                 }
226                 if(curExp->HdrExp[i].exp_real_params.digital_gain < 1.0) {
227                     stExpInfo.arDGain[i] = 1.0;
228                 } else {
229                     LOGW_ANR("hdr mode dgain is wrong, use 1.0 instead\n");
230                     stExpInfo.arDGain[i] = curExp->HdrExp[i].exp_real_params.digital_gain;
231                 }
232                 stExpInfo.arTime[i] = curExp->HdrExp[i].exp_real_params.integration_time;
233                 stExpInfo.arIso[i] = stExpInfo.arAGain[i] * stExpInfo.arDGain[i] * 50;
234 
235                 LOGD_ANR("%s:%d index:%d again:%f dgain:%f time:%f iso:%d hdr_mode:%d\n",
236                          __FUNCTION__, __LINE__,
237                          i,
238                          stExpInfo.arAGain[i],
239                          stExpInfo.arDGain[i],
240                          stExpInfo.arTime[i],
241                          stExpInfo.arIso[i],
242                          stExpInfo.hdr_mode);
243             }
244         }
245     } else {
246         LOGE_ANR("%s:%d curExp is NULL, so use default instead \n", __FUNCTION__, __LINE__);
247     }
248 
249 #if 0
250     static int anr_cnt = 0;
251     anr_cnt++;
252 
253     if(anr_cnt % 50 == 0) {
254         for(int i = 0; i < stExpInfo.hdr_mode + 1; i++) {
255             printf("%s:%d index:%d again:%f dgain:%f time:%f iso:%d hdr_mode:%d snr_mode:%d\n",
256                    __FUNCTION__, __LINE__,
257                    i,
258                    stExpInfo.arAGain[i],
259                    stExpInfo.arDGain[i],
260                    stExpInfo.arTime[i],
261                    stExpInfo.arIso[i],
262                    stExpInfo.hdr_mode,
263                    stExpInfo.snr_mode);
264         }
265     }
266 #endif
267 
268     DeltaIso = abs(stExpInfo.arIso[stExpInfo.hdr_mode] - pAsharpCtx->stExpInfo.arIso[stExpInfo.hdr_mode]);
269     if(DeltaIso > ASHARPV4_RECALCULATE_DELTA_ISO) {
270         pAsharpCtx->isReCalculate |= 1;
271     }
272 
273     if(pAsharpCtx->isReCalculate) {
274         Asharp4_result_t ret = Asharp_Process_V4(pAsharpCtx, &stExpInfo);
275         if(ret != ASHARP4_RET_SUCCESS) {
276             result = XCAM_RETURN_ERROR_FAILED;
277             LOGE_ASHARP("%s: processing ANR failed (%d)\n", __FUNCTION__, ret);
278         }
279 
280         Asharp_GetProcResult_V4(pAsharpCtx, &pAsharpProcResParams->stAsharpProcResult);
281         outparams->cfg_update = true;
282 
283         LOGD_ASHARP("recalculate: %d delta_iso:%d \n ", pAsharpCtx->isReCalculate, DeltaIso);
284     } else {
285         outparams->cfg_update = false;
286     }
287 
288     pAsharpCtx->isReCalculate = 0;
289 #endif
290 
291     LOGD_ASHARP("%s: oyyf(exit)\n", __FUNCTION__ );
292     return XCAM_RETURN_NO_ERROR;
293 }
294 
295 static XCamReturn
post_process(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)296 post_process(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
297 {
298     LOGI_ASHARP("%s: (enter)\n", __FUNCTION__ );
299 
300     //nothing todo now
301 
302     LOGI_ASHARP("%s: (exit)\n", __FUNCTION__ );
303     return XCAM_RETURN_NO_ERROR;
304 }
305 
306 RkAiqAlgoDescription g_RkIspAlgoDescAsharpV4 = {
307     .common = {
308         .version = RKISP_ALGO_ASHARP_VERSION_V4,
309         .vendor  = RKISP_ALGO_ASHARP_VENDOR_V4,
310         .description = RKISP_ALGO_ASHARP_DESCRIPTION_V4,
311         .type    = RK_AIQ_ALGO_TYPE_ASHARP,
312         .id      = 0,
313         .create_context  = create_context,
314         .destroy_context = destroy_context,
315     },
316     .prepare = prepare,
317     .pre_process = pre_process,
318     .processing = processing,
319     .post_process = post_process,
320 };
321 
322 RKAIQ_END_DECLARE
323