1 /*
2  * rk_aiq_algo_camgroup_awb_itf.c
3  *
4  *  Copyright (c) 2021 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 "rk_aiq_algo_camgroup_abayer2dnr_itf_v2.h"
21 #include "rk_aiq_algo_camgroup_types.h"
22 #include "rk_aiq_types_camgroup_abayer2dnr_prvt_v2.h"
23 #include "abayer2dnr2/rk_aiq_abayer2dnr_algo_itf_v2.h"
24 #include "abayer2dnr2/rk_aiq_abayer2dnr_algo_v2.h"
25 
26 
27 
28 RKAIQ_BEGIN_DECLARE
29 
groupAbayer2dnrV2CreateCtx(RkAiqAlgoContext ** context,const AlgoCtxInstanceCfg * cfg)30 static XCamReturn groupAbayer2dnrV2CreateCtx(RkAiqAlgoContext **context, const AlgoCtxInstanceCfg* cfg)
31 {
32     LOGI_ANR("%s enter \n", __FUNCTION__ );
33 
34     XCamReturn ret = XCAM_RETURN_NO_ERROR;
35     CamGroup_Abayer2dnrV2_Contex_t *abayernr_group_contex = NULL;
36     AlgoCtxInstanceCfgCamGroup *cfgInt = (AlgoCtxInstanceCfgCamGroup*)cfg;
37 
38 
39     if(CHECK_ISP_HW_V30()) {
40         abayernr_group_contex = (CamGroup_Abayer2dnrV2_Contex_t*)malloc(sizeof(CamGroup_Abayer2dnrV2_Contex_t));
41 #if ABAYER2DNR_USE_JSON_FILE_V2
42         Abayer2dnr_result_V2_t ret_v2 = ABAYER2DNR_RET_SUCCESS;
43         ret_v2 = Abayer2dnr_Init_V2(&(abayernr_group_contex->abayer2dnr_contex_v2), (void *)cfgInt->s_calibv2);
44         if(ret_v2 != ABAYER2DNR_RET_SUCCESS) {
45             ret = XCAM_RETURN_ERROR_FAILED;
46             LOGE_ANR("%s: Initializaion ANR failed (%d)\n", __FUNCTION__, ret);
47         }
48 #endif
49     }
50     else {
51         ret = XCAM_RETURN_ERROR_FAILED;
52         LOGE_ANR("module_hw_version of abayernr is invalid!!!!");
53     }
54 
55     if(ret != XCAM_RETURN_NO_ERROR) {
56         LOGE_ANR("%s: Initializaion group bayernr failed (%d)\n", __FUNCTION__, ret);
57     } else {
58         // to do got abayernrSurrViewClib and initinal paras for for surround view
59         abayernr_group_contex->group_CalibV2.groupMethod = CalibDbV2_CAMGROUP_ABAYER2DNRV2_METHOD_MEAN;// to do from json
60         abayernr_group_contex->camera_Num = cfgInt->camIdArrayLen;
61 
62         *context = (RkAiqAlgoContext *)(abayernr_group_contex);
63 
64         LOGI_ANR("%s:%d surrViewMethod(1-mean):%d, cameraNum %d \n",
65                  __FUNCTION__, __LINE__,
66                  abayernr_group_contex->group_CalibV2.groupMethod,
67                  abayernr_group_contex->camera_Num);
68     }
69 
70     LOGI_ANR("%s exit ret:%d\n", __FUNCTION__, ret);
71     return ret;
72 
73 }
74 
groupAbayer2dnrV2DestroyCtx(RkAiqAlgoContext * context)75 static XCamReturn groupAbayer2dnrV2DestroyCtx(RkAiqAlgoContext *context)
76 {
77     LOGI_ANR("%s enter \n", __FUNCTION__ );
78 
79     XCamReturn ret = XCAM_RETURN_NO_ERROR;
80 
81     CamGroup_Abayer2dnrV2_Contex_t *abayernr_group_contex = (CamGroup_Abayer2dnrV2_Contex_t*)context;
82 
83     if(CHECK_ISP_HW_V30()) {
84         Abayer2dnr_result_V2_t ret_v2 = ABAYER2DNR_RET_SUCCESS;
85         ret_v2 = Abayer2dnr_Release_V2(abayernr_group_contex->abayer2dnr_contex_v2);
86         if(ret_v2 != ABAYER2DNR_RET_SUCCESS) {
87             ret = XCAM_RETURN_ERROR_FAILED;
88             LOGE_ANR("%s: Initializaion ANR failed (%d)\n", __FUNCTION__, ret);
89         }
90     }
91     else {
92         ret = XCAM_RETURN_ERROR_FAILED;
93         LOGE_ANR("module_hw_version of abayer2dnr is isvalid!!!!");
94     }
95 
96     if(ret != XCAM_RETURN_NO_ERROR) {
97         LOGE_ANR("%s: release ANR failed (%d)\n", __FUNCTION__, ret);
98     } else {
99         free(abayernr_group_contex);
100     }
101 
102 
103     LOGI_ANR("%s exit ret:%d\n", __FUNCTION__, ret);
104     return ret;
105 }
106 
groupAbayer2dnrV2Prepare(RkAiqAlgoCom * params)107 static XCamReturn groupAbayer2dnrV2Prepare(RkAiqAlgoCom* params)
108 {
109     LOGI_ANR("%s enter \n", __FUNCTION__ );
110 
111     XCamReturn ret = XCAM_RETURN_NO_ERROR;
112 
113     CamGroup_Abayer2dnrV2_Contex_t * abayernr_group_contex = (CamGroup_Abayer2dnrV2_Contex_t *)params->ctx;
114     RkAiqAlgoCamGroupPrepare* para = (RkAiqAlgoCamGroupPrepare*)params;
115 
116     if(CHECK_ISP_HW_V30()) {
117         Abayer2dnr_Context_V2_t * abayer2dnr_contex_v2 = abayernr_group_contex->abayer2dnr_contex_v2;
118         if(!!(params->u.prepare.conf_type & RK_AIQ_ALGO_CONFTYPE_UPDATECALIB )) {
119             // todo  update calib pars for surround view
120 #if ABAYER2DNR_USE_JSON_FILE_V2
121             void *pCalibdbV2 = (void*)(para->s_calibv2);
122             CalibDbV2_Bayer2dnrV2_t *bayer2dnr_v2 = (CalibDbV2_Bayer2dnrV2_t*)(CALIBDBV2_GET_MODULE_PTR((void*)pCalibdbV2, bayer2dnr_v2));
123             abayer2dnr_contex_v2->bayernr_v2 = *bayer2dnr_v2;
124             abayer2dnr_contex_v2->isIQParaUpdate = true;
125             abayer2dnr_contex_v2->isReCalculate |= 1;
126 #endif
127         }
128         Abayer2dnr_Config_V2_t stAbayer2dnrConfigV2;
129         Abayer2dnr_result_V2_t ret_v2 = ABAYER2DNR_RET_SUCCESS;
130         ret_v2 = Abayer2dnr_Prepare_V2(abayer2dnr_contex_v2, &stAbayer2dnrConfigV2);
131         if(ret_v2 != ABAYER2DNR_RET_SUCCESS) {
132             ret = XCAM_RETURN_ERROR_FAILED;
133             LOGE_ANR("%s: config ANR failed (%d)\n", __FUNCTION__, ret);
134         }
135     }
136     else {
137         ret = XCAM_RETURN_ERROR_FAILED;
138         LOGE_ANR("module_hw_version of abayer2dnr is isvalid!!!!");
139     }
140 
141     LOGI_ANR("%s exit ret:%d\n", __FUNCTION__, ret);
142     return ret;
143 }
144 
groupAbayer2dnrV2Processing(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)145 static XCamReturn groupAbayer2dnrV2Processing(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
146 {
147     LOGI_ANR("%s enter \n", __FUNCTION__ );
148     LOGI_ANR("----------------------------------------------frame_id (%d)----------------------------------------------\n", inparams->frame_id);
149 
150     XCamReturn ret = XCAM_RETURN_NO_ERROR;
151     RkAiqAlgoCamGroupProcIn* procParaGroup = (RkAiqAlgoCamGroupProcIn*)inparams;
152     RkAiqAlgoCamGroupProcOut* procResParaGroup = (RkAiqAlgoCamGroupProcOut*)outparams;
153     CamGroup_Abayer2dnrV2_Contex_t * abayernr_group_contex = (CamGroup_Abayer2dnrV2_Contex_t *)inparams->ctx;
154     int deltaIso = 0;
155 
156     //method error
157     if (abayernr_group_contex->group_CalibV2.groupMethod <= CalibDbV2_CAMGROUP_ABAYER2DNRV2_METHOD_MIN
158             ||  abayernr_group_contex->group_CalibV2.groupMethod >=  CalibDbV2_CAMGROUP_ABAYER2DNRV2_METHOD_MAX) {
159         return (ret);
160     }
161 
162     //group empty
163     if(procParaGroup->camgroupParmasArray == nullptr) {
164         LOGE_ANR("camgroupParmasArray is null");
165         return(XCAM_RETURN_ERROR_FAILED);
166     }
167 
168     //get cur ae exposure
169     Abayer2dnr_ExpInfo_V2_t stExpInfoV2;
170     memset(&stExpInfoV2, 0x00, sizeof(stExpInfoV2));
171     stExpInfoV2.hdr_mode = 0; //pAnrProcParams->hdr_mode;
172     stExpInfoV2.snr_mode = 0;
173     for(int i = 0; i < 3; i++) {
174         stExpInfoV2.arIso[i] = 50;
175         stExpInfoV2.arAGain[i] = 1.0;
176         stExpInfoV2.arDGain[i] = 1.0;
177         stExpInfoV2.arTime[i] = 0.01;
178     }
179 
180 
181     //merge ae result, iso mean value
182     rk_aiq_singlecam_3a_result_t* scam_3a_res = procParaGroup->camgroupParmasArray[0];
183     if(scam_3a_res->aec._bEffAecExpValid) {
184         RKAiqAecExpInfo_t* pCurExp = &scam_3a_res->aec._effAecExpInfo;
185         stExpInfoV2.snr_mode = pCurExp->CISFeature.SNR;
186         if((rk_aiq_working_mode_t)procParaGroup->working_mode == RK_AIQ_WORKING_MODE_NORMAL) {
187             stExpInfoV2.hdr_mode = 0;
188             stExpInfoV2.arAGain[0] = pCurExp->LinearExp.exp_real_params.analog_gain;
189             stExpInfoV2.arDGain[0] = pCurExp->LinearExp.exp_real_params.digital_gain;
190             stExpInfoV2.arTime[0] = pCurExp->LinearExp.exp_real_params.integration_time;
191             stExpInfoV2.arIso[0] = stExpInfoV2.arAGain[0] * stExpInfoV2.arDGain[0] * 50;
192 
193         } else {
194             if(procParaGroup->working_mode == RK_AIQ_ISP_HDR_MODE_2_FRAME_HDR
195                     || procParaGroup->working_mode == RK_AIQ_ISP_HDR_MODE_2_LINE_HDR)
196                 stExpInfoV2.hdr_mode = 1;
197             else if (procParaGroup->working_mode == RK_AIQ_ISP_HDR_MODE_3_FRAME_HDR
198                      || procParaGroup->working_mode == RK_AIQ_ISP_HDR_MODE_3_LINE_HDR)
199                 stExpInfoV2.hdr_mode = 2;
200             else {
201                 stExpInfoV2.hdr_mode = 0;
202                 LOGE_ANR("mode error\n");
203             }
204 
205             for(int i = 0; i < 3; i++) {
206                 stExpInfoV2.arAGain[i] = pCurExp->HdrExp[i].exp_real_params.analog_gain;
207                 stExpInfoV2.arDGain[i] = pCurExp->HdrExp[i].exp_real_params.digital_gain;
208                 stExpInfoV2.arTime[i] = pCurExp->HdrExp[i].exp_real_params.integration_time;
209                 stExpInfoV2.arIso[i] = stExpInfoV2.arAGain[i] * stExpInfoV2.arDGain[i] * 50;
210             }
211 
212         }
213     } else {
214         LOGW("fail to get sensor gain form AE module,use default value ");
215     }
216 
217 
218 
219     if(CHECK_ISP_HW_V30()) {
220         Abayer2dnr_Context_V2_t * abayer2dnr_contex_v2 = abayernr_group_contex->abayer2dnr_contex_v2;
221         Abayer2dnr_ProcResult_V2_t stAbayer2dnrResultV2;
222         RK_Bayer2dnr_Fix_V2_t st2DFix;
223         stAbayer2dnrResultV2.st2DFix = &st2DFix;
224 
225         deltaIso = abs(stExpInfoV2.arIso[stExpInfoV2.hdr_mode] - abayer2dnr_contex_v2->stExpInfo.arIso[stExpInfoV2.hdr_mode]);
226         if(deltaIso > ABAYERNRV2_DELTA_ISO) {
227             abayer2dnr_contex_v2->isReCalculate |= 1;
228         }
229         if(abayer2dnr_contex_v2->isReCalculate) {
230             Abayer2dnr_result_V2_t ret_v2 = ABAYER2DNR_RET_SUCCESS;
231             ret_v2 = Abayer2dnr_Process_V2(abayer2dnr_contex_v2, &stExpInfoV2);
232             if(ret_v2 != ABAYER2DNR_RET_SUCCESS) {
233                 ret = XCAM_RETURN_ERROR_FAILED;
234                 LOGE_ANR("%s: processing ANR failed (%d)\n", __FUNCTION__, ret);
235             }
236             outparams->cfg_update = true;
237             LOGD_ANR("recalculate: %d delta_iso:%d \n ", abayer2dnr_contex_v2->isReCalculate, deltaIso);
238         } else {
239             outparams->cfg_update = false;
240         }
241         Abayer2dnr_GetProcResult_V2(abayer2dnr_contex_v2, &stAbayer2dnrResultV2);
242         for (int i = 0; i < procResParaGroup->arraySize; i++) {
243             *(procResParaGroup->camgroupParmasArray[i]->abayernr._abayer2dnr_procRes_v2) = *stAbayer2dnrResultV2.st2DFix;
244             IS_UPDATE_MEM((procResParaGroup->camgroupParmasArray[i]->abayernr._abayer2dnr_procRes_v2), procParaGroup->_offset_is_update) =
245                 outparams->cfg_update;
246         }
247         abayer2dnr_contex_v2->isReCalculate = 0;
248     }
249     else {
250         ret = XCAM_RETURN_ERROR_FAILED;
251         LOGE_ANR("module_hw_version of abayer2dnr is isvalid!!!!");
252     }
253 
254     LOGI_ANR("%s exit\n", __FUNCTION__);
255     return ret;
256 }
257 
258 RkAiqAlgoDescription g_RkIspAlgoDescCamgroupAbayer2dnrV2 = {
259     .common = {
260         .version = RKISP_ALGO_CAMGROUP_ABAYER2DNRV2_VERSION,
261         .vendor  = RKISP_ALGO_CAMGROUP_ABAYER2DNRV2_VENDOR,
262         .description = RKISP_ALGO_CAMGROUP_ABAYER2DNRV2_DESCRIPTION,
263         .type    = RK_AIQ_ALGO_TYPE_ARAWNR,
264         .id      = 0,
265         .create_context  = groupAbayer2dnrV2CreateCtx,
266         .destroy_context = groupAbayer2dnrV2DestroyCtx,
267     },
268     .prepare = groupAbayer2dnrV2Prepare,
269     .pre_process = NULL,
270     .processing = groupAbayer2dnrV2Processing,
271     .post_process = NULL,
272 };
273 
274 RKAIQ_END_DECLARE
275