1 /*
2  * rk_aiq_asharp_algo_itf_v33.cpp
3  *
4  *  Copyright (c) 2022 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 #include "asharpV33/rk_aiq_asharp_algo_itf_v33.h"
20 
21 #include "asharpV33/rk_aiq_asharp_algo_v33.h"
22 #include "rk_aiq_algo_types.h"
23 
24 RKAIQ_BEGIN_DECLARE
25 
26 typedef struct _RkAiqAlgoContext {
27     Asharp_Context_V33_t AsharpCtx;
28 } RkAiqAlgoContext;
29 
create_context(RkAiqAlgoContext ** context,const AlgoCtxInstanceCfg * cfg)30 static XCamReturn create_context(RkAiqAlgoContext** context, const AlgoCtxInstanceCfg* cfg) {
31     XCamReturn result = XCAM_RETURN_NO_ERROR;
32     LOGI_ASHARP("%s:(enter)\n", __FUNCTION__);
33 
34 #if 1
35     Asharp_Context_V33_t* pAsharpCtx = NULL;
36 #if (ASHARP_USE_JSON_FILE_V33)
37     Asharp_result_V33_t ret = Asharp_Init_V33(&pAsharpCtx, cfg->calibv2);
38 #else
39     Asharp_result_V33_t ret = Asharp_Init_V33(&pAsharpCtx, cfg->calib);
40 #endif
41 
42     if (ret != ASHARP_V33_RET_SUCCESS) {
43         result = XCAM_RETURN_ERROR_FAILED;
44         LOGE_ASHARP("%s: Initializaion ASHARP failed (%d)\n", __FUNCTION__, ret);
45     } else {
46         *context = (RkAiqAlgoContext*)(pAsharpCtx);
47     }
48 #endif
49 
50     LOGI_ASHARP("%s:(exit)\n", __FUNCTION__);
51     return result;
52 }
53 
destroy_context(RkAiqAlgoContext * context)54 static XCamReturn destroy_context(RkAiqAlgoContext* context) {
55     XCamReturn result = XCAM_RETURN_NO_ERROR;
56 
57     LOGI_ASHARP("%s: oyyf (enter)\n", __FUNCTION__);
58 
59 #if 1
60     Asharp_Context_V33_t* pAsharpCtx = (Asharp_Context_V33_t*)context;
61     Asharp_result_V33_t ret          = Asharp_Release_V33(pAsharpCtx);
62     if (ret != ASHARP_V33_RET_SUCCESS) {
63         result = XCAM_RETURN_ERROR_FAILED;
64         LOGE_ASHARP("%s: release ANR failed (%d)\n", __FUNCTION__, ret);
65     }
66 #endif
67 
68     LOGI_ASHARP("%s: (exit)\n", __FUNCTION__);
69     return result;
70 }
71 
prepare(RkAiqAlgoCom * params)72 static XCamReturn prepare(RkAiqAlgoCom* params) {
73     XCamReturn result = XCAM_RETURN_NO_ERROR;
74 
75     LOGI_ASHARP("%s: (enter)\n", __FUNCTION__);
76 
77     Asharp_Context_V33_t* pAsharpCtx    = (Asharp_Context_V33_t*)params->ctx;
78     RkAiqAlgoConfigAsharpV33* pCfgParam = (RkAiqAlgoConfigAsharpV33*)params;
79     pAsharpCtx->prepare_type            = params->u.prepare.conf_type;
80 
81     if (!!(params->u.prepare.conf_type & RK_AIQ_ALGO_CONFTYPE_UPDATECALIB)) {
82 #if (ASHARP_USE_JSON_FILE_V33)
83 #if RKAIQ_HAVE_SHARP_V33
84         CalibDbV2_SharpV33_t* calibv2_sharp = (CalibDbV2_SharpV33_t*)(CALIBDBV2_GET_MODULE_PTR(
85                 pCfgParam->com.u.prepare.calibv2, sharp_v33));
86 #else
87         CalibDbV2_SharpV33Lite_t* calibv2_sharp =
88             (CalibDbV2_SharpV33Lite_t*)(CALIBDBV2_GET_MODULE_PTR(pCfgParam->com.u.prepare.calibv2,
89                                                                  sharp_v33));
90 #endif
91         pAsharpCtx->sharp_v33               = *calibv2_sharp;
92 #endif
93         pAsharpCtx->isIQParaUpdate = true;
94         pAsharpCtx->isReCalculate |= 1;
95     }
96 
97     Asharp_result_V33_t ret = Asharp_Prepare_V33(pAsharpCtx, &pCfgParam->stAsharpConfig);
98     if (ret != ASHARP_V33_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: (exit)\n", __FUNCTION__);
104     return result;
105 }
106 #if 0
107 static XCamReturn pre_process(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams) {
108     XCamReturn result = XCAM_RETURN_NO_ERROR;
109     bool oldGrayMode  = false;
110 
111     LOGD_ASHARP("%s: oyyf (enter)\n", __FUNCTION__);
112     Asharp_Context_V33_t* pAsharpCtx = (Asharp_Context_V33_t*)inparams->ctx;
113 
114     RkAiqAlgoPreAsharpV33* pAsharpPreParams = (RkAiqAlgoPreAsharpV33*)inparams;
115 
116     oldGrayMode = pAsharpCtx->isGrayMode;
117     if (pAsharpPreParams->com.u.proc.gray_mode) {
118         pAsharpCtx->isGrayMode = true;
119     } else {
120         pAsharpCtx->isGrayMode = false;
121     }
122 
123     if (oldGrayMode != pAsharpCtx->isGrayMode) {
124         pAsharpCtx->isReCalculate |= 1;
125     }
126 
127     Asharp_result_V33_t ret = Asharp_PreProcess_V33(pAsharpCtx);
128     if (ret != ASHARP_V33_RET_SUCCESS) {
129         result = XCAM_RETURN_ERROR_FAILED;
130         LOGE_ASHARP("%s: ANRPreProcess failed (%d)\n", __FUNCTION__, ret);
131     }
132 
133     LOGI_ASHARP("%s: (exit)\n", __FUNCTION__);
134     return result;
135 }
136 #endif
processing(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)137 static XCamReturn processing(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams) {
138     XCamReturn result = XCAM_RETURN_NO_ERROR;
139     int DeltaIso      = 0;
140 
141     LOGD_ASHARP("%s:oyyf (enter)\n", __FUNCTION__);
142 
143 #if 1
144     RkAiqAlgoProcAsharpV33* pAsharpProcParams       = (RkAiqAlgoProcAsharpV33*)inparams;
145     RkAiqAlgoProcResAsharpV33* pAsharpProcResParams = (RkAiqAlgoProcResAsharpV33*)outparams;
146     Asharp_Context_V33_t* pAsharpCtx                = (Asharp_Context_V33_t*)inparams->ctx;
147     Asharp_ExpInfo_V33_t stExpInfo;
148     memset(&stExpInfo, 0x00, sizeof(Asharp_ExpInfo_V33_t));
149 
150     LOGD_ASHARP("%s:%d init:%d hdr mode:%d  \n", __FUNCTION__, __LINE__, inparams->u.proc.init,
151                 pAsharpProcParams->hdr_mode);
152 
153     bool oldGrayMode  = false;
154     oldGrayMode = pAsharpCtx->isGrayMode;
155     if (inparams->u.proc.gray_mode) {
156         pAsharpCtx->isGrayMode = true;
157     } else {
158         pAsharpCtx->isGrayMode = false;
159     }
160 
161     if (oldGrayMode != pAsharpCtx->isGrayMode) {
162         pAsharpCtx->isReCalculate |= 1;
163     }
164 
165     Asharp_result_V33_t ret = Asharp_PreProcess_V33(pAsharpCtx);
166     if (ret != ASHARP_V33_RET_SUCCESS) {
167         result = XCAM_RETURN_ERROR_FAILED;
168         LOGE_ASHARP("%s: ANRPreProcess failed (%d)\n", __FUNCTION__, ret);
169     }
170 
171     stExpInfo.hdr_mode = 0;  // pAsharpProcParams->hdr_mode;
172     for (int i = 0; i < 3; i++) {
173         stExpInfo.arIso[i]   = 50;
174         stExpInfo.arAGain[i] = 1.0;
175         stExpInfo.arDGain[i] = 1.0;
176         stExpInfo.arTime[i]  = 0.01;
177     }
178 
179     if (pAsharpProcParams->hdr_mode == RK_AIQ_WORKING_MODE_NORMAL) {
180         stExpInfo.hdr_mode = 0;
181     } else if (pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_2_FRAME_HDR ||
182                pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_2_LINE_HDR) {
183         stExpInfo.hdr_mode = 1;
184     } else if (pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_3_FRAME_HDR ||
185                pAsharpProcParams->hdr_mode == RK_AIQ_ISP_HDR_MODE_3_LINE_HDR) {
186         stExpInfo.hdr_mode = 2;
187     }
188     stExpInfo.snr_mode = 0;
189 
190     stExpInfo.blc_ob_predgain = 1.0;
191     if(pAsharpProcParams != NULL) {
192         stExpInfo.blc_ob_predgain = pAsharpProcParams->stAblcV32_proc_res->isp_ob_predgain;
193         if(stExpInfo.blc_ob_predgain != pAsharpCtx->stExpInfo.blc_ob_predgain) {
194             pAsharpCtx->isReCalculate |= 1;
195         }
196     }
197 #if 0  // TODO Merge:
198     XCamVideoBuffer* xCamAePreRes = pAsharpProcParams->com.u.proc.res_comb->ae_pre_res;
199     RkAiqAlgoPreResAe* pAEPreRes  = nullptr;
200     if (xCamAePreRes) {
201         // xCamAePreRes->ref(xCamAePreRes);
202         pAEPreRes = (RkAiqAlgoPreResAe*)xCamAePreRes->map(xCamAePreRes);
203         if (!pAEPreRes) {
204             LOGE_ASHARP("ae pre result is null");
205         } else {
206             LOGD_ASHARP("ae_pre_result: meanluma:%f time:%f gain:%f env_luma:%f \n",
207                         pAEPreRes->ae_pre_res_rk.MeanLuma[0],
208                         pAEPreRes->ae_pre_res_rk.LinearExp.exp_real_params.integration_time,
209                         pAEPreRes->ae_pre_res_rk.LinearExp.exp_real_params.analog_gain,
210                         pAEPreRes->ae_pre_res_rk.GlobalEnvLux);
211         }
212         // xCamAePreRes->unref(xCamAePreRes);
213     }
214 #endif
215 
216     RKAiqAecExpInfo_t* curExp = pAsharpProcParams->com.u.proc.curExp;
217     if (curExp != NULL) {
218         stExpInfo.snr_mode = curExp->CISFeature.SNR;
219         if (pAsharpProcParams->hdr_mode == RK_AIQ_WORKING_MODE_NORMAL) {
220             stExpInfo.hdr_mode = 0;
221             if (curExp->LinearExp.exp_real_params.analog_gain < 1.0) {
222                 stExpInfo.arAGain[0] = 1.0;
223                 LOGW_ASHARP("leanr mode again is wrong, use 1.0 instead\n");
224             } else {
225                 stExpInfo.arAGain[0] = curExp->LinearExp.exp_real_params.analog_gain;
226             }
227             if (curExp->LinearExp.exp_real_params.digital_gain < 1.0) {
228                 stExpInfo.arDGain[0] = 1.0;
229                 LOGW_ASHARP("leanr mode dgain is wrong, use 1.0 instead\n");
230             } else {
231                 stExpInfo.arDGain[0] = curExp->LinearExp.exp_real_params.digital_gain;
232             }
233             if (curExp->LinearExp.exp_real_params.isp_dgain < 1.0) {
234                 stExpInfo.isp_dgain[0] = 1.0;
235                 LOGW_ASHARP("leanr mode isp_dgain is wrong, use 1.0 instead\n");
236             } else {
237                 stExpInfo.isp_dgain[0] = curExp->LinearExp.exp_real_params.isp_dgain;
238             }
239             if(stExpInfo.blc_ob_predgain < 1.0) {
240                 stExpInfo.blc_ob_predgain = 1.0;
241             }
242             stExpInfo.arTime[0] = curExp->LinearExp.exp_real_params.integration_time;
243             stExpInfo.arIso[0]  = stExpInfo.arAGain[0] * stExpInfo.arDGain[0] * stExpInfo.blc_ob_predgain * 50 * stExpInfo.isp_dgain[0];
244             LOGD_ASHARP("new snr mode:%d old:%d  gain:%f isp_dgain:%f iso:%d time:%f\n",
245                         stExpInfo.snr_mode,
246                         pAsharpCtx->stExpInfo.snr_mode,
247                         stExpInfo.arDGain[0],
248                         stExpInfo.isp_dgain[0],
249                         stExpInfo.arIso[0],
250                         stExpInfo.arTime[0]
251                        );
252         } else {
253             for (int i = 0; i < 3; i++) {
254                 if (curExp->HdrExp[i].exp_real_params.analog_gain < 1.0) {
255                     stExpInfo.arAGain[i] = 1.0;
256                     LOGW_ASHARP("hdr mode again is wrong, use 1.0 instead\n");
257                 } else {
258                     stExpInfo.arAGain[i] = curExp->HdrExp[i].exp_real_params.analog_gain;
259                 }
260                 if (curExp->HdrExp[i].exp_real_params.digital_gain < 1.0) {
261                     stExpInfo.arDGain[i] = 1.0;
262                     LOGW_ASHARP("hdr mode dgain is wrong, use 1.0 instead\n");
263                 } else {
264                     stExpInfo.arDGain[i] = curExp->HdrExp[i].exp_real_params.digital_gain;
265                 }
266                 if (curExp->HdrExp[i].exp_real_params.isp_dgain < 1.0) {
267                     stExpInfo.isp_dgain[i] = 1.0;
268                     LOGW_ASHARP("hdr mode isp_dgain is wrong, use 1.0 instead\n");
269                 } else {
270                     stExpInfo.arDGain[i] = curExp->HdrExp[i].exp_real_params.isp_dgain;
271                 }
272                 stExpInfo.blc_ob_predgain = 1.0;
273                 stExpInfo.arTime[i] = curExp->HdrExp[i].exp_real_params.integration_time;
274                 stExpInfo.arIso[i]  = stExpInfo.arAGain[i] * stExpInfo.arDGain[i] * 50 * stExpInfo.arDGain[i];
275 
276                 LOGD_ASHARP("%s:%d index:%d again:%f dgain:%f isp_dgain:%f time:%f iso:%d hdr_mode:%d\n",
277                             __FUNCTION__, __LINE__, i, stExpInfo.arAGain[i], stExpInfo.arDGain[i], stExpInfo.arDGain[i],
278                             stExpInfo.arTime[i], stExpInfo.arIso[i], stExpInfo.hdr_mode);
279             }
280         }
281     } else {
282         LOGE_ASHARP("%s:%d curExp is NULL, so use default instead \n", __FUNCTION__, __LINE__);
283     }
284 
285     DeltaIso =
286         abs(stExpInfo.arIso[stExpInfo.hdr_mode] - pAsharpCtx->stExpInfo.arIso[stExpInfo.hdr_mode]);
287     if (DeltaIso > ASHARPV33_RECALCULATE_DELTA_ISO) {
288         pAsharpCtx->isReCalculate |= 1;
289     }
290 
291     if (pAsharpCtx->isReCalculate) {
292         Asharp_result_V33_t ret = Asharp_Process_V33(pAsharpCtx, &stExpInfo);
293         if (ret != ASHARP_V33_RET_SUCCESS) {
294             result = XCAM_RETURN_ERROR_FAILED;
295             LOGE_ASHARP("%s: processing ANR failed (%d)\n", __FUNCTION__, ret);
296         }
297 
298         Asharp_GetProcResult_V33(pAsharpCtx, &pAsharpProcResParams->stAsharpProcResult);
299         outparams->cfg_update = true;
300 
301         LOGD_ASHARP("recalculate: %d delta_iso:%d \n ", pAsharpCtx->isReCalculate, DeltaIso);
302     } else {
303         outparams->cfg_update = false;
304     }
305 
306     pAsharpCtx->isReCalculate = 0;
307 #endif
308     LOGI_ASHARP("%s: (exit)\n", __FUNCTION__);
309     return XCAM_RETURN_NO_ERROR;
310 }
311 
post_process(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)312 static XCamReturn post_process(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams) {
313     LOGI_ASHARP("%s: (enter)\n", __FUNCTION__);
314 
315     // nothing todo now
316 
317     LOGI_ASHARP("%s: (exit)\n", __FUNCTION__);
318     return XCAM_RETURN_NO_ERROR;
319 }
320 
321 RkAiqAlgoDescription g_RkIspAlgoDescAsharpV33 = {
322     .common =
323     {
324         .version         = RKISP_ALGO_ASHARP_VERSION_V33,
325         .vendor          = RKISP_ALGO_ASHARP_VENDOR_V33,
326         .description     = RKISP_ALGO_ASHARP_DESCRIPTION_V33,
327         .type            = RK_AIQ_ALGO_TYPE_ASHARP,
328         .id              = 0,
329         .create_context  = create_context,
330         .destroy_context = destroy_context,
331     },
332     .prepare      = prepare,
333     .pre_process  = NULL,
334     .processing   = processing,
335     .post_process = NULL,
336 };
337 
338 RKAIQ_END_DECLARE
339