xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/algos/asharp3/rk_aiq_asharp_algo_itf_v3.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 "asharp3/rk_aiq_asharp_algo_itf_v3.h"
21 #include "asharp3/rk_aiq_asharp_algo_v3.h"
22 #include "rk_aiq_algo_types.h"
23 #include "RkAiqCalibDbV2Helper.h"
24 
25 RKAIQ_BEGIN_DECLARE
26 
27 typedef struct _RkAiqAlgoContext {
28     Asharp_Context_V3_t AsharpCtx;
29 } RkAiqAlgoContext;
30 
31 
32 static XCamReturn
create_context(RkAiqAlgoContext ** context,const AlgoCtxInstanceCfg * cfg)33 create_context(RkAiqAlgoContext **context, const AlgoCtxInstanceCfg* cfg)
34 {
35 
36     XCamReturn result = XCAM_RETURN_NO_ERROR;
37     LOGI_ASHARP("%s:oyyf (enter)\n", __FUNCTION__ );
38 
39 #if 1
40     Asharp_Context_V3_t* pAsharpCtx = NULL;
41 #if ASHARP_USE_JSON_FILE_V3
42     Asharp3_result_t ret = Asharp_Init_V3(&pAsharpCtx, cfg->calibv2);
43 #else
44     Asharp3_result_t ret = Asharp_Init_V3(&pAsharpCtx, cfg->calib);
45 #endif
46 
47     if(ret != ASHARP3_RET_SUCCESS) {
48         result = XCAM_RETURN_ERROR_FAILED;
49         LOGE_ASHARP("%s: Initializaion ANR failed (%d)\n", __FUNCTION__, ret);
50     } else {
51         *context = (RkAiqAlgoContext *)(pAsharpCtx);
52     }
53 #endif
54 
55     LOGI_ASHARP("%s:oyyf (exit)\n", __FUNCTION__ );
56     return result;
57 }
58 
59 static XCamReturn
destroy_context(RkAiqAlgoContext * context)60 destroy_context(RkAiqAlgoContext *context)
61 {
62     XCamReturn result = XCAM_RETURN_NO_ERROR;
63 
64     LOGI_ASHARP("%s: oyyf (enter)\n", __FUNCTION__ );
65 
66 #if 1
67     Asharp_Context_V3_t* pAsharpCtx = (Asharp_Context_V3_t*)context;
68     Asharp3_result_t ret = Asharp_Release_V3(pAsharpCtx);
69     if(ret != ASHARP3_RET_SUCCESS) {
70         result = XCAM_RETURN_ERROR_FAILED;
71         LOGE_ASHARP("%s: release ANR failed (%d)\n", __FUNCTION__, ret);
72     }
73 #endif
74 
75     LOGI_ASHARP("%s: (exit)\n", __FUNCTION__ );
76     return result;
77 }
78 
79 static XCamReturn
prepare(RkAiqAlgoCom * params)80 prepare(RkAiqAlgoCom* params)
81 {
82     XCamReturn result = XCAM_RETURN_NO_ERROR;
83 
84     LOGI_ASHARP("%s: oyyf (enter)\n", __FUNCTION__ );
85 
86     Asharp_Context_V3_t* pAsharpCtx = (Asharp_Context_V3_t *)params->ctx;
87     RkAiqAlgoConfigAsharpV3* pCfgParam = (RkAiqAlgoConfigAsharpV3*)params;
88     pAsharpCtx->prepare_type = params->u.prepare.conf_type;
89 
90     if(!!(params->u.prepare.conf_type & RK_AIQ_ALGO_CONFTYPE_UPDATECALIB )) {
91 #if(ASHARP_USE_JSON_FILE_V3)
92         CalibDbV2_SharpV3_t *calibv2_sharp =
93             (CalibDbV2_SharpV3_t *)(CALIBDBV2_GET_MODULE_PTR(pCfgParam->com.u.prepare.calibv2, sharp_v3));
94         pAsharpCtx->sharp_v3 = *calibv2_sharp;
95 #else
96         pAsharpCtx->list_sharp_v3 =
97             (struct list_head*)(CALIBDB_GET_MODULE_PTR(pCfgParam->com.u.prepare.calib, list_sharp_v3));
98 #endif
99         pAsharpCtx->isIQParaUpdate = true;
100         pAsharpCtx->isReCalculate |= 1;
101     }
102 
103     Asharp3_result_t ret = Asharp_Prepare_V3(pAsharpCtx, &pCfgParam->stAsharpConfig);
104     if(ret != ASHARP3_RET_SUCCESS) {
105         result = XCAM_RETURN_ERROR_FAILED;
106         LOGE_ASHARP("%s: config ANR failed (%d)\n", __FUNCTION__, ret);
107     }
108 
109     LOGI_ASHARP("%s: oyyf (exit)\n", __FUNCTION__ );
110     return result;
111 }
112 
113 static XCamReturn
pre_process(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)114 pre_process(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
115 {
116     XCamReturn result = XCAM_RETURN_NO_ERROR;
117     bool oldGrayMode = false;
118 
119     LOGD_ASHARP("%s: oyyf (enter)\n", __FUNCTION__ );
120     Asharp_Context_V3_t* pAsharpCtx = (Asharp_Context_V3_t *)inparams->ctx;
121 
122     RkAiqAlgoPreAsharpV3* pAsharpPreParams = (RkAiqAlgoPreAsharpV3*)inparams;
123 
124     oldGrayMode = pAsharpCtx->isGrayMode;
125     if (pAsharpPreParams->com.u.proc.gray_mode) {
126         pAsharpCtx->isGrayMode = true;
127     } else {
128         pAsharpCtx->isGrayMode = false;
129     }
130 
131     if(oldGrayMode != pAsharpCtx->isGrayMode) {
132         pAsharpCtx->isReCalculate |= 1;
133     }
134 
135     Asharp3_result_t ret = Asharp_PreProcess_V3(pAsharpCtx);
136     if(ret != ASHARP3_RET_SUCCESS) {
137         result = XCAM_RETURN_ERROR_FAILED;
138         LOGE_ASHARP("%s: ANRPreProcess failed (%d)\n", __FUNCTION__, ret);
139     }
140 
141     LOGD_ASHARP("%s: oyyf (exit)\n", __FUNCTION__ );
142     return result;
143 }
144 
145 static XCamReturn
processing(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)146 processing(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
147 {
148     XCamReturn result = XCAM_RETURN_NO_ERROR;
149     int DeltaIso = 0;
150 
151     LOGD_ASHARP("%s:oyyf (enter)\n", __FUNCTION__ );
152 
153 #if 1
154     RkAiqAlgoProcAsharpV3* pAsharpProcParams = (RkAiqAlgoProcAsharpV3*)inparams;
155     RkAiqAlgoProcResAsharpV3* pAsharpProcResParams = (RkAiqAlgoProcResAsharpV3*)outparams;
156     Asharp_Context_V3_t* pAsharpCtx = (Asharp_Context_V3_t *)inparams->ctx;
157     Asharp3_ExpInfo_t stExpInfo;
158     memset(&stExpInfo, 0x00, sizeof(Asharp3_ExpInfo_t));
159 
160     LOGD_ASHARP("%s:%d init:%d hdr mode:%d  \n",
161                 __FUNCTION__, __LINE__,
162                 inparams->u.proc.init,
163                 pAsharpProcParams->hdr_mode);
164 
165     stExpInfo.hdr_mode = 0; //pAsharpProcParams->hdr_mode;
166     for(int i = 0; i < 3; i++) {
167         stExpInfo.arIso[i] = 50;
168         stExpInfo.arAGain[i] = 1.0;
169         stExpInfo.arDGain[i] = 1.0;
170         stExpInfo.arTime[i] = 0.01;
171     }
172 
173     if(pAsharpProcParams->hdr_mode == RK_AIQ_WORKING_MODE_NORMAL) {
174         stExpInfo.hdr_mode = 0;
175     } else if(pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_2_FRAME_HDR
176               || pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_2_LINE_HDR ) {
177         stExpInfo.hdr_mode = 1;
178     } else if(pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_3_FRAME_HDR
179               || pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_3_LINE_HDR ) {
180         stExpInfo.hdr_mode = 2;
181     }
182     stExpInfo.snr_mode = 0;
183 
184 #if 0// TODO Merge:
185     XCamVideoBuffer* xCamAePreRes = pAsharpProcParams->com.u.proc.res_comb->ae_pre_res;
186     RkAiqAlgoPreResAe* pAEPreRes = nullptr;
187     if (xCamAePreRes) {
188         // xCamAePreRes->ref(xCamAePreRes);
189         pAEPreRes = (RkAiqAlgoPreResAe*)xCamAePreRes->map(xCamAePreRes);
190         if (!pAEPreRes) {
191             LOGE_ASHARP("ae pre result is null");
192         } else {
193 
194         }
195         // xCamAePreRes->unref(xCamAePreRes);
196     }
197 #endif
198 
199     RKAiqAecExpInfo_t *curExp = pAsharpProcParams->com.u.proc.curExp;
200     if(curExp != NULL) {
201         stExpInfo.snr_mode = curExp->CISFeature.SNR;
202         if(pAsharpProcParams->hdr_mode == RK_AIQ_WORKING_MODE_NORMAL) {
203             stExpInfo.hdr_mode = 0;
204             stExpInfo.arAGain[0] = curExp->LinearExp.exp_real_params.analog_gain;
205             stExpInfo.arDGain[0] = curExp->LinearExp.exp_real_params.digital_gain;
206             stExpInfo.arTime[0] = curExp->LinearExp.exp_real_params.integration_time;
207             stExpInfo.arIso[0] = stExpInfo.arAGain[0] * stExpInfo.arDGain[0] * 50;
208         } else {
209             for(int i = 0; i < 3; i++) {
210                 stExpInfo.arAGain[i] = curExp->HdrExp[i].exp_real_params.analog_gain;
211                 stExpInfo.arDGain[i] = curExp->HdrExp[i].exp_real_params.digital_gain;
212                 stExpInfo.arTime[i] = curExp->HdrExp[i].exp_real_params.integration_time;
213                 stExpInfo.arIso[i] = stExpInfo.arAGain[i] * stExpInfo.arDGain[i] * 50;
214 
215                 LOGD_ANR("%s:%d index:%d again:%f dgain:%f time:%f iso:%d hdr_mode:%d\n",
216                          __FUNCTION__, __LINE__,
217                          i,
218                          stExpInfo.arAGain[i],
219                          stExpInfo.arDGain[i],
220                          stExpInfo.arTime[i],
221                          stExpInfo.arIso[i],
222                          stExpInfo.hdr_mode);
223             }
224         }
225     } else {
226         LOGE_ANR("%s:%d curExp is NULL, so use default instead \n", __FUNCTION__, __LINE__);
227     }
228 
229 #if 0
230     static int anr_cnt = 0;
231     anr_cnt++;
232 
233     if(anr_cnt % 50 == 0) {
234         for(int i = 0; i < stExpInfo.hdr_mode + 1; i++) {
235             printf("%s:%d index:%d again:%f dgain:%f time:%f iso:%d hdr_mode:%d snr_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                    stExpInfo.snr_mode);
244         }
245     }
246 #endif
247 
248     DeltaIso = abs(stExpInfo.arIso[stExpInfo.hdr_mode] - pAsharpCtx->stExpInfo.arIso[stExpInfo.hdr_mode]);
249     if(DeltaIso > ASHARPV3_RECALCULATE_DELTA_ISO) {
250         pAsharpCtx->isReCalculate |= 1;
251     }
252 
253     if(pAsharpCtx->isReCalculate) {
254         Asharp3_result_t ret = Asharp_Process_V3(pAsharpCtx, &stExpInfo);
255         if(ret != ASHARP3_RET_SUCCESS) {
256             result = XCAM_RETURN_ERROR_FAILED;
257             LOGE_ASHARP("%s: processing ANR failed (%d)\n", __FUNCTION__, ret);
258         }
259 
260         Asharp_GetProcResult_V3(pAsharpCtx, &pAsharpProcResParams->stAsharpProcResult);
261         outparams->cfg_update = true;
262 
263         LOGD_ANR("recalculate: %d delta_iso:%d \n ", pAsharpCtx->isReCalculate, DeltaIso);
264     } else {
265         outparams->cfg_update = false;
266     }
267 
268     pAsharpCtx->isReCalculate = 0;
269 #endif
270 
271     LOGD_ASHARP("%s: oyyf(exit)\n", __FUNCTION__ );
272     return XCAM_RETURN_NO_ERROR;
273 }
274 
275 static XCamReturn
post_process(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)276 post_process(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
277 {
278     LOGI_ASHARP("%s: (enter)\n", __FUNCTION__ );
279 
280     //nothing todo now
281 
282     LOGI_ASHARP("%s: (exit)\n", __FUNCTION__ );
283     return XCAM_RETURN_NO_ERROR;
284 }
285 
286 RkAiqAlgoDescription g_RkIspAlgoDescAsharpV3 = {
287     .common = {
288         .version = RKISP_ALGO_ASHARP_VERSION_V3,
289         .vendor  = RKISP_ALGO_ASHARP_VENDOR_V3,
290         .description = RKISP_ALGO_ASHARP_DESCRIPTION_V3,
291         .type    = RK_AIQ_ALGO_TYPE_ASHARP,
292         .id      = 0,
293         .create_context  = create_context,
294         .destroy_context = destroy_context,
295     },
296     .prepare = prepare,
297     .pre_process = pre_process,
298     .processing = processing,
299     .post_process = post_process,
300 };
301 
302 RKAIQ_END_DECLARE
303