1 /*
2  * rk_aiq_algo_camgroup_adhaz_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 /* for rockchip v2.0.0*/
20 
21 #include "RkAiqCalibDbTypes.h"
22 #include "algos/adehaze/rk_aiq_algo_adehaze_itf.h"
23 #include "rk_aiq_algo_camgroup_types.h"
24 #if RKAIQ_HAVE_DEHAZE_V10
25 #include "adehaze/rk_aiq_adehaze_algo_v10.h"
26 #endif
27 #if RKAIQ_HAVE_DEHAZE_V11
28 #include "adehaze/rk_aiq_adehaze_algo_v11.h"
29 #endif
30 #if RKAIQ_HAVE_DEHAZE_V11_DUO
31 #include "adehaze/rk_aiq_adehaze_algo_v11_duo.h"
32 #endif
33 #if RKAIQ_HAVE_DEHAZE_V12
34 #include "adehaze/rk_aiq_adehaze_algo_v12.h"
35 #endif
36 #include "RkAiqCalibDbTypes.h"
37 #include "xcam_log.h"
38 
39 RKAIQ_BEGIN_DECLARE
40 
create_context(RkAiqAlgoContext ** context,const AlgoCtxInstanceCfg * cfg)41 static XCamReturn create_context(RkAiqAlgoContext** context, const AlgoCtxInstanceCfg* cfg) {
42     LOG1_ADEHAZE("ENTER: %s \n", __func__);
43     XCamReturn ret                          = XCAM_RETURN_NO_ERROR;
44     AdehazeHandle_t* pAdehazeGrpHandle      = NULL;
45     AlgoCtxInstanceCfgCamGroup* instanc_int = (AlgoCtxInstanceCfgCamGroup*)cfg;
46 
47     ret = AdehazeInit(&pAdehazeGrpHandle, (CamCalibDbV2Context_t*)(instanc_int->s_calibv2));
48 
49     if (ret != XCAM_RETURN_NO_ERROR) {
50         LOGE_ADEHAZE("%s Adehaze Init failed: %d", __FUNCTION__, ret);
51         return (XCAM_RETURN_ERROR_FAILED);
52     }
53 
54     *context = (RkAiqAlgoContext*)(pAdehazeGrpHandle);
55 
56     LOG1_ADEHAZE("EIXT: %s \n", __func__);
57     return ret;
58 }
59 
destroy_context(RkAiqAlgoContext * context)60 static XCamReturn destroy_context(RkAiqAlgoContext* context) {
61     LOG1_ADEHAZE("ENTER: %s \n", __func__);
62     XCamReturn ret                     = XCAM_RETURN_NO_ERROR;
63     AdehazeHandle_t* pAdehazeGrpHandle = (AdehazeHandle_t*)context;
64 
65     ret = AdehazeRelease(pAdehazeGrpHandle);
66 
67     LOG1_ADEHAZE("EIXT: %s \n", __func__);
68     return ret;
69 }
70 
prepare(RkAiqAlgoCom * params)71 static XCamReturn prepare(RkAiqAlgoCom* params) {
72     LOG1_ADEHAZE("ENTER: %s \n", __func__);
73 
74     XCamReturn ret = XCAM_RETURN_NO_ERROR;
75 
76     RkAiqAlgoCamGroupPrepare* config      = (RkAiqAlgoCamGroupPrepare*)params;
77     AdehazeHandle_t* pAdehazeGrpHandle    = (AdehazeHandle_t*)params->ctx;
78     const CamCalibDbV2Context_t* pCalibDb = config->s_calibv2;
79 
80     pAdehazeGrpHandle->working_mode = config->gcom.com.u.prepare.working_mode;
81     pAdehazeGrpHandle->width        = config->gcom.com.u.prepare.sns_op_width;
82     pAdehazeGrpHandle->height       = config->gcom.com.u.prepare.sns_op_height;
83 
84     if (pAdehazeGrpHandle->working_mode < RK_AIQ_WORKING_MODE_ISP_HDR2)
85         pAdehazeGrpHandle->FrameNumber = LINEAR_NUM;
86     else if (pAdehazeGrpHandle->working_mode < RK_AIQ_WORKING_MODE_ISP_HDR3 &&
87              pAdehazeGrpHandle->working_mode >= RK_AIQ_WORKING_MODE_ISP_HDR2)
88         pAdehazeGrpHandle->FrameNumber = HDR_2X_NUM;
89     else
90         pAdehazeGrpHandle->FrameNumber = HDR_3X_NUM;
91 
92     if (!!(params->u.prepare.conf_type & RK_AIQ_ALGO_CONFTYPE_UPDATECALIB)) {
93         LOGI_ADEHAZE("%s: Adehaze Reload Para!\n", __FUNCTION__);
94 #if RKAIQ_HAVE_DEHAZE_V10
95         CalibDbV2_dehaze_v10_t* calibv2_adehaze_calib_V10 =
96             (CalibDbV2_dehaze_v10_t*)(CALIBDBV2_GET_MODULE_PTR((void*)pCalibDb, adehaze_calib));
97         if (calibv2_adehaze_calib_V10)
98             memcpy(&pAdehazeGrpHandle->CalibV10, calibv2_adehaze_calib_V10,
99                    sizeof(CalibDbV2_dehaze_v10_t));
100 #endif
101 #if RKAIQ_HAVE_DEHAZE_V11
102         CalibDbV2_dehaze_v11_t* calibv2_adehaze_calib_V11 =
103             (CalibDbV2_dehaze_v11_t*)(CALIBDBV2_GET_MODULE_PTR((void*)pCalibDb, adehaze_calib));
104         if (calibv2_adehaze_calib_V11)
105             memcpy(&pAdehazeGrpHandle->AdehazeAtrrV11.stAuto, calibv2_adehaze_calib_V11,
106                    sizeof(CalibDbV2_dehaze_v11_t));
107 #endif
108 #if RKAIQ_HAVE_DEHAZE_V11_DUO
109         CalibDbV2_dehaze_v11_t* calibv2_adehaze_calib_V11_duo =
110             (CalibDbV2_dehaze_v11_t*)(CALIBDBV2_GET_MODULE_PTR((void*)pCalibDb, adehaze_calib));
111         if (calibv2_adehaze_calib_V11_duo)
112             memcpy(&pAdehazeGrpHandle->AdehazeAtrrV11duo.stAuto, calibv2_adehaze_calib_V11_duo,
113                    sizeof(CalibDbV2_dehaze_v11_t));
114 #endif
115 #if RKAIQ_HAVE_DEHAZE_V12
116         CalibDbV2_dehaze_v12_t* calibv2_adehaze_calib_V12 =
117             (CalibDbV2_dehaze_v12_t*)(CALIBDBV2_GET_MODULE_PTR((void*)pCalibDb, adehaze_calib));
118         if (calibv2_adehaze_calib_V12)
119             memcpy(&pAdehazeGrpHandle->AdehazeAtrrV12.stAuto, calibv2_adehaze_calib_V12,
120                    sizeof(CalibDbV2_dehaze_v12_t));
121 #endif
122         pAdehazeGrpHandle->ifReCalcStAuto = true;
123     } else if (params->u.prepare.conf_type & RK_AIQ_ALGO_CONFTYPE_CHANGERES) {
124         pAdehazeGrpHandle->isCapture = true;
125     }
126 
127     LOG1_ADEHAZE("EIXT: %s \n", __func__);
128     return ret;
129 }
130 
processing(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)131 static XCamReturn processing(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams) {
132     LOG1_ADEHAZE("ENTER: %s \n", __func__);
133     XCamReturn ret                            = XCAM_RETURN_NO_ERROR;
134     bool dehaze_bypass_processing             = true;
135     AdehazeHandle_t* pAdehazeGrpHandle        = (AdehazeHandle_t*)inparams->ctx;
136     RkAiqAlgoCamGroupProcIn* pGrpProcPara     = (RkAiqAlgoCamGroupProcIn*)inparams;
137     RkAiqAlgoCamGroupProcOut* pGrpProcResPara = (RkAiqAlgoCamGroupProcOut*)outparams;
138     pAdehazeGrpHandle->FrameID                = inparams->frame_id;
139 
140     LOGD_ADEHAZE("/*************************Adehaze Group Start******************/ \n");
141 
142     AdehazeGetCurrDataGroup(pAdehazeGrpHandle, pGrpProcPara->camgroupParmasArray[0]);
143 
144     if (DehazeEnableSetting(pAdehazeGrpHandle,
145                             pGrpProcResPara->camgroupParmasArray[0]->_adehazeConfig)) {
146         dehaze_bypass_processing = AdehazeByPassProcessing(pAdehazeGrpHandle);
147 #if RKAIQ_HAVE_DEHAZE_V10
148         // dehaze group dehaze not ready for now
149         dehaze_stats_v10_t dehazeStats;
150 #endif
151 #if RKAIQ_HAVE_DEHAZE_V11
152         // dehaze group dehaze not ready for now
153         dehaze_stats_v11_t dehazeStats;
154 #endif
155 #if RKAIQ_HAVE_DEHAZE_V11_DUO
156         // dehaze group dehaze not ready for now
157         dehaze_stats_v11_duo_t dehazeStats;
158 #endif
159 #if RKAIQ_HAVE_DEHAZE_V12
160         // dehaze group dehaze not ready for now
161         dehaze_stats_v12_t dehazeStats;
162 #endif
163         memset(&dehazeStats, 0x0, sizeof(dehazeStats));
164         // process
165         if (!dehaze_bypass_processing) {
166 #if RKAIQ_HAVE_DEHAZE_V12
167             ret = AdehazeProcess(pAdehazeGrpHandle, &dehazeStats, false,
168                                  pGrpProcResPara->camgroupParmasArray[0]->_adehazeConfig);
169 #else
170             ret = AdehazeProcess(pAdehazeGrpHandle, &dehazeStats,
171                                  pGrpProcResPara->camgroupParmasArray[0]->_adehazeConfig);
172 #endif
173         }
174     } else {
175         LOGD_ADEHAZE("Group Dehaze Enable is OFF, Bypass Dehaze !!! \n");
176     }
177 
178     LOGD_ADEHAZE("/*************************Adehaze Group Over******************/ \n");
179 
180     outparams->cfg_update = !dehaze_bypass_processing || inparams->u.proc.init;
181     // proc res
182     for (int i = 1; i < pGrpProcResPara->arraySize; i++) {
183         if (outparams->cfg_update) {
184             memcpy(pGrpProcResPara->camgroupParmasArray[i]->_adehazeConfig,
185                    pGrpProcResPara->camgroupParmasArray[0]->_adehazeConfig,
186                    sizeof(RkAiqAdehazeProcResult_t));
187         }
188         IS_UPDATE_MEM((pGrpProcResPara->camgroupParmasArray[i]->_adehazeConfig), pGrpProcPara->_offset_is_update) =
189             outparams->cfg_update;
190     }
191     if (pAdehazeGrpHandle->ifReCalcStManual) pAdehazeGrpHandle->ifReCalcStManual = false;
192     if (pAdehazeGrpHandle->ifReCalcStAuto) pAdehazeGrpHandle->ifReCalcStAuto = false;
193 
194     LOG1_ADEHAZE("EIXT: %s \n", __func__);
195     return ret;
196 }
197 
198 RkAiqAlgoDescription g_RkIspAlgoDescCamgroupAdhaz = {
199     .common =
200         {
201             .version         = RKISP_ALGO_ADHAZ_VERSION,
202             .vendor          = RKISP_ALGO_ADHAZ_VENDOR,
203             .description     = RKISP_ALGO_ADHAZ_DESCRIPTION,
204             .type            = RK_AIQ_ALGO_TYPE_ADHAZ,
205             .id              = 0,
206             .create_context  = create_context,
207             .destroy_context = destroy_context,
208         },
209     .prepare      = prepare,
210     .pre_process  = NULL,
211     .processing   = processing,
212     .post_process = NULL,
213 };
214 
215 RKAIQ_END_DECLARE
216