xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/algos/atmo/rk_aiq_algo_atmo_itf.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * rk_aiq_algo_ae_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 "atmo/rk_aiq_algo_atmo_itf.h"
21 #include "xcam_log.h"
22 #include "atmo/rk_aiq_atmo_algo.h"
23 #include "atmo/rk_aiq_types_atmo_algo_prvt.h"
24 #include "rk_aiq_algo_types.h"
25 
26 
27 RKAIQ_BEGIN_DECLARE
28 
AtmoCreateCtx(RkAiqAlgoContext ** context,const AlgoCtxInstanceCfg * cfg)29 static XCamReturn AtmoCreateCtx(RkAiqAlgoContext **context, const AlgoCtxInstanceCfg* cfg)
30 {
31 
32     LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
33     RESULT ret = ATMO_RET_SUCCESS;
34     RkAiqAlgoContext *ctx = new RkAiqAlgoContext();
35     if (ctx == NULL) {
36         LOGE_ATMO( "%s: create atmo context fail!\n", __FUNCTION__);
37         return XCAM_RETURN_ERROR_MEM;
38     }
39     memset(&ctx->AtmoInstConfig, 0x00, sizeof(AtmoInstanceConfig_t));
40     ret = AtmoInit(&ctx->AtmoInstConfig, cfg->calibv2);
41 
42     if (ret != ATMO_RET_SUCCESS) {
43         LOGE_ATMO("%s ATMO Init failed: %d", __FUNCTION__, ret);
44         return(XCAM_RETURN_ERROR_FAILED);
45     }
46 
47     *context = ctx;
48 
49     LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
50     return(XCAM_RETURN_NO_ERROR);
51 }
52 
AtmoDestroyCtx(RkAiqAlgoContext * context)53 static XCamReturn AtmoDestroyCtx(RkAiqAlgoContext *context)
54 {
55     LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
56     RESULT ret = ATMO_RET_SUCCESS;
57 
58     if(context != NULL) {
59         AtmoHandle_t pAtmoCtx = (AtmoHandle_t)context->AtmoInstConfig.hAtmo;
60         ret = AtmoRelease(pAtmoCtx);
61         if (ret != ATMO_RET_SUCCESS) {
62             LOGE_ATMO("%s Atmo Release failed: %d", __FUNCTION__, ret);
63             return(XCAM_RETURN_ERROR_FAILED);
64         }
65         delete context;
66         context = NULL;
67     }
68 
69     LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
70     return(XCAM_RETURN_NO_ERROR);
71 }
72 
AtmoPrepare(RkAiqAlgoCom * params)73 static XCamReturn AtmoPrepare(RkAiqAlgoCom* params)
74 {
75     LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
76     RESULT ret = ATMO_RET_SUCCESS;
77 
78     AtmoHandle_t pAtmoCtx = params->ctx->AtmoInstConfig.hAtmo;
79     RkAiqAlgoConfigAtmo* AtmoCfgParam = (RkAiqAlgoConfigAtmo*)params; //come from params in html
80     const CamCalibDbContext_t* pCalibDb = AtmoCfgParam->com.u.prepare.calib;
81     const CamCalibDbV2Context_t* pCalibDbv2 = AtmoCfgParam->com.u.prepare.calibv2;
82     pAtmoCtx->width = AtmoCfgParam->rawWidth;
83     pAtmoCtx->height = AtmoCfgParam->rawHeight;
84 
85     if (AtmoCfgParam->working_mode < RK_AIQ_WORKING_MODE_ISP_HDR2)
86         pAtmoCtx->FrameNumber = LINEAR_NUM;
87     else if (AtmoCfgParam->working_mode < RK_AIQ_WORKING_MODE_ISP_HDR3 &&
88              AtmoCfgParam->working_mode >= RK_AIQ_WORKING_MODE_ISP_HDR2)
89         pAtmoCtx->FrameNumber = HDR_2X_NUM;
90     else
91         pAtmoCtx->FrameNumber = HDR_3X_NUM;
92 
93     CalibDbV2_tmo_t* calibv2_atmo_calib =
94         (CalibDbV2_tmo_t*)(CALIBDBV2_GET_MODULE_PTR((void*)pCalibDbv2, atmo_calib));
95 
96     if(!!(params->u.prepare.conf_type & RK_AIQ_ALGO_CONFTYPE_UPDATECALIB )) {
97         LOGI_ATMO("%s: Ahdr Reload Para!\n", __FUNCTION__);
98         memcpy(&pAtmoCtx->pCalibDB, calibv2_atmo_calib, sizeof(CalibDbV2_tmo_t));//load iq paras
99     }
100 
101     if(/* !params->u.prepare.reconfig*/true) {
102         AtmoStop(pAtmoCtx); // stop firstly for re-preapre
103         ret = AtmoStart(pAtmoCtx);
104         if (ret != ATMO_RET_SUCCESS) {
105             LOGE_ATMO("%s Atmo Start failed: %d", __FUNCTION__, ret);
106             return(XCAM_RETURN_ERROR_FAILED);
107         }
108     }
109 
110     //get aec delay frame
111     CalibDb_Aec_ParaV2_t* aec =
112         (CalibDb_Aec_ParaV2_t*)CALIBDBV2_GET_MODULE_PTR((void*)pCalibDbv2, ae_calib);
113     pAtmoCtx->CurrAeResult.AecDelayframe = MAX(aec->CommCtrl.AecDelayFrmNum.BlackDelay,
114                                            aec->CommCtrl.AecDelayFrmNum.WhiteDelay);
115 
116     LOG1_ATMO("%s:AecDelayframe:%d\n", __FUNCTION__, pAtmoCtx->CurrAeResult.AecDelayframe);
117 
118     LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
119     return(XCAM_RETURN_NO_ERROR);
120 }
121 
AtmoPreProcess(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)122 static XCamReturn AtmoPreProcess(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
123 {
124     LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
125     RESULT ret = ATMO_RET_SUCCESS;
126 
127     AtmoHandle_t pAtmoCtx = inparams->ctx->AtmoInstConfig.hAtmo;
128     RkAiqAlgoConfigAtmo* AtmoCfgParam = (RkAiqAlgoConfigAtmo*)inparams;
129 
130     if(pAtmoCtx->tmoAttr.opMode == TMO_OPMODE_TOOL) {
131         newMalloc(&pAtmoCtx->AtmoConfig, &pAtmoCtx->tmoAttr.stTool);
132         AtmoUpdateConfig(pAtmoCtx, &pAtmoCtx->tmoAttr.stTool);
133     }
134     else {
135         newMalloc(&pAtmoCtx->AtmoConfig, &pAtmoCtx->pCalibDB);
136         AtmoUpdateConfig(pAtmoCtx, &pAtmoCtx->pCalibDB);
137     }
138 
139     LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
140     return(XCAM_RETURN_NO_ERROR);
141 }
142 
AtmoProcess(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)143 static XCamReturn AtmoProcess(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
144 {
145     LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
146     RESULT ret = ATMO_RET_SUCCESS;
147 
148     AtmoHandle_t pAtmoCtx = (AtmoHandle_t)inparams->ctx->AtmoInstConfig.hAtmo;
149     RkAiqAlgoProcAtmo* AtmoParams = (RkAiqAlgoProcAtmo*)inparams;
150     RkAiqAlgoProcResAtmo* AtmoProcResParams = (RkAiqAlgoProcResAtmo*)outparams;
151     AtmoGetStats(pAtmoCtx, &AtmoParams->ispAtmoStats);
152 
153     //get Sensor Info
154     XCamVideoBuffer* xCamAeProcRes = AtmoParams->com.u.proc.res_comb->ae_proc_res;
155     RkAiqAlgoProcResAe* pAEProcRes = NULL;
156     if (xCamAeProcRes) {
157         pAEProcRes = (RkAiqAlgoProcResAe*)xCamAeProcRes->map(xCamAeProcRes);
158         AtmoGetSensorInfo(pAtmoCtx, pAEProcRes->ae_proc_res_rk);
159     } else {
160         AecProcResult_t AeProcResult;
161         memset(&AeProcResult, 0x0, sizeof(AecProcResult_t));
162         LOGW_ATMO("%s: Ae Proc result is null!!!\n", __FUNCTION__);
163         AtmoGetSensorInfo(pAtmoCtx, AeProcResult);
164     }
165 
166     //get ae pre res and proc
167     XCamVideoBuffer* xCamAePreRes = AtmoParams->com.u.proc.res_comb->ae_pre_res;
168     RkAiqAlgoPreResAe* pAEPreRes = NULL;
169     if (xCamAePreRes) {
170         pAEPreRes = (RkAiqAlgoPreResAe*)xCamAePreRes->map(xCamAePreRes);
171     }
172 
173     // TODO: get af pre result
174     af_preprocess_result_t* af_pre_res_int = NULL;
175 
176     if (pAEPreRes && af_pre_res_int)
177         AtmoProcessing(pAtmoCtx,
178                        pAEPreRes->ae_pre_res_rk,
179                        *af_pre_res_int);
180     else if (pAEPreRes) {
181         af_preprocess_result_t AfPreResult;
182         memset(&AfPreResult, 0x0, sizeof(af_preprocess_result_t));
183         LOGW_ATMO("%s: af Pre result is null!!!\n", __FUNCTION__);
184         AtmoProcessing(pAtmoCtx,
185                        pAEPreRes->ae_pre_res_rk,
186                        AfPreResult);
187     }
188     else {
189         AecPreResult_t AecHdrPreResult;
190         af_preprocess_result_t AfPreResult;
191         memset(&AfPreResult, 0x0, sizeof(af_preprocess_result_t));
192         memset(&AecHdrPreResult, 0x0, sizeof(AecPreResult_t));
193         LOGW_ATMO("%s: ae/af Pre result is null!!!\n", __FUNCTION__);
194         AtmoProcessing(pAtmoCtx,
195                        AecHdrPreResult,
196                        AfPreResult);
197     }
198 
199     pAtmoCtx->ProcRes.LongFrameMode = pAtmoCtx->SensorInfo.LongFrmMode;
200     AtmoProcResParams->AtmoProcRes.LongFrameMode = pAtmoCtx->ProcRes.LongFrameMode;
201     AtmoProcResParams->AtmoProcRes.isHdrGlobalTmo = pAtmoCtx->ProcRes.isHdrGlobalTmo;
202     AtmoProcResParams->AtmoProcRes.bTmoEn = pAtmoCtx->ProcRes.bTmoEn;
203     AtmoProcResParams->AtmoProcRes.isLinearTmo = pAtmoCtx->ProcRes.isLinearTmo;
204     memcpy(&AtmoProcResParams->AtmoProcRes.Res, &pAtmoCtx->ProcRes.Res, sizeof(TmoProcRes_t));
205     memcpy(&AtmoProcResParams->AtmoProcRes.TmoFlicker, &pAtmoCtx->ProcRes.TmoFlicker, sizeof(TmoFlickerPara_t));
206 
207     LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
208     return(XCAM_RETURN_NO_ERROR);
209 }
210 
AtmoPostProcess(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)211 static XCamReturn AtmoPostProcess(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
212 {
213     LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
214     RESULT ret = ATMO_RET_SUCCESS;
215 
216     //TODO
217 
218     LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
219     return(XCAM_RETURN_NO_ERROR);
220 }
221 
222 RkAiqAlgoDescription g_RkIspAlgoDescAtmo = {
223     .common = {
224         .version = RKISP_ALGO_ATMO_VERSION,
225         .vendor  = RKISP_ALGO_ATMO_VENDOR,
226         .description = RKISP_ALGO_ATMO_DESCRIPTION,
227         .type    = RK_AIQ_ALGO_TYPE_ATMO,
228         .id      = 0,
229         .create_context  = AtmoCreateCtx,
230         .destroy_context = AtmoDestroyCtx,
231     },
232     .prepare = AtmoPrepare,
233     .pre_process = AtmoPreProcess,
234     .processing = AtmoProcess,
235     .post_process = AtmoPostProcess,
236 };
237 
238 RKAIQ_END_DECLARE
239