xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/algos/adebayer/rk_aiq_algo_adebayer_itf.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * rk_aiq_algo_debayer_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 "adebayer/rk_aiq_algo_adebayer_itf.h"
21 #if RKAIQ_HAVE_DEBAYER_V1
22 #include "adebayer/rk_aiq_adebayer_algo_v1.h"
23 #endif
24 #if RKAIQ_HAVE_DEBAYER_V2 || RKAIQ_HAVE_DEBAYER_V2_LITE
25 #include "adebayer/rk_aiq_adebayer_algo_v2.h"
26 #endif
27 #include "rk_aiq_algo_types.h"
28 
29 RKAIQ_BEGIN_DECLARE
30 
31 static XCamReturn
create_context(RkAiqAlgoContext ** context,const AlgoCtxInstanceCfg * cfg)32 create_context
33 (
34     RkAiqAlgoContext **context,
35     const AlgoCtxInstanceCfg* cfg
36 )
37 {
38     XCamReturn result = XCAM_RETURN_NO_ERROR;
39 
40     RkAiqAlgoContext *ctx = new RkAiqAlgoContext();
41     if (ctx == NULL) {
42         LOGE_ADEBAYER( "%s: create adebayer context fail!\n", __FUNCTION__);
43         return XCAM_RETURN_ERROR_MEM;
44     }
45 
46     LOGV_ADEBAYER("%s: (enter)\n", __FUNCTION__ );
47 
48     //hw version check
49 
50 #if RKAIQ_HAVE_DEBAYER_V1
51     if(cfg->module_hw_version != ADEBAYER_HARDWARE_V1)
52         LOGE_ADEBAYER("%s: wrong HW version(%d)", __FUNCTION__, cfg->module_hw_version);
53 #endif
54 
55 #if RKAIQ_HAVE_DEBAYER_V2
56     if(cfg->module_hw_version != ADEBAYER_HARDWARE_V2)
57         LOGE_ADEBAYER("%s: wrong HW version(%d)", __FUNCTION__, cfg->module_hw_version);
58 #endif
59 
60 #if RKAIQ_HAVE_DEBAYER_V2_LITE
61     if(cfg->module_hw_version != ADEBAYER_HARDWARE_V2_LITE)
62         LOGE_ADEBAYER("%s: wrong HW version(%d)", __FUNCTION__, cfg->module_hw_version);
63 #endif
64 
65     //copy params from calib
66     AdebayerInit(&ctx->adebayerCtx, cfg->calib, cfg->calibv2);
67 
68     *context = ctx;
69     LOGV_ADEBAYER("%s: (exit)\n", __FUNCTION__ );
70     return result;
71 }
72 
73 static XCamReturn
destroy_context(RkAiqAlgoContext * context)74 destroy_context
75 (
76     RkAiqAlgoContext *context
77 )
78 {
79     XCamReturn result = XCAM_RETURN_NO_ERROR;
80 
81     LOGV_ADEBAYER("%s: (enter)\n", __FUNCTION__ );
82 
83     AdebayerContext_t* pAdebayerCtx = (AdebayerContext_t*)&context->adebayerCtx;
84     AdebayerRelease(pAdebayerCtx);
85 
86     delete context;
87     LOGV_ADEBAYER("%s: (exit)\n", __FUNCTION__ );
88     return result;
89 }
90 
91 static XCamReturn
prepare(RkAiqAlgoCom * params)92 prepare
93 (
94     RkAiqAlgoCom* params
95 )
96 {
97     XCamReturn result = XCAM_RETURN_NO_ERROR;
98 
99     LOGV_ADEBAYER("%s: (enter)\n", __FUNCTION__ );
100     AdebayerContext_t* pAdebayerCtx = (AdebayerContext_t *)&params->ctx->adebayerCtx;
101     RkAiqAlgoConfigAdebayer* pCfgParam = (RkAiqAlgoConfigAdebayer*)params;
102 
103     //reconfig
104     if(!!(params->u.prepare.conf_type & RK_AIQ_ALGO_CONFTYPE_UPDATECALIB )) {
105 
106 #if RKAIQ_HAVE_DEBAYER_V1
107         result = AdebayerFullParamsInit(pAdebayerCtx, pCfgParam->com.u.prepare.calib, pCfgParam->com.u.prepare.calibv2);
108 #endif
109 
110 #if RKAIQ_HAVE_DEBAYER_V2 || RKAIQ_HAVE_DEBAYER_V2_LITE
111         result = AdebayerCalibConfig(pAdebayerCtx, pCfgParam->com.u.prepare.calibv2);
112 #endif
113 
114         pAdebayerCtx->config.updatecfg = true;
115     }
116 
117     AdebayerStart(pAdebayerCtx);
118     LOGV_ADEBAYER("%s: (exit)\n", __FUNCTION__ );
119     return result;
120 }
121 
122 static XCamReturn
pre_process(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)123 pre_process
124 (
125     const RkAiqAlgoCom* inparams,
126     RkAiqAlgoResCom* outparams
127 )
128 {
129     LOGV_ADEBAYER("%s: (enter)\n", __FUNCTION__ );
130 
131     LOGV_ADEBAYER("%s: (exit)\n", __FUNCTION__ );
132     return XCAM_RETURN_NO_ERROR;
133 }
134 
135 static XCamReturn
processing(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)136 processing
137 (
138     const RkAiqAlgoCom* inparams,
139     RkAiqAlgoResCom* outparams
140 )
141 {
142     XCamReturn result = XCAM_RETURN_NO_ERROR;
143     int iso = 50;
144 
145     RkAiqAlgoProcAdebayer* pAdebayerProcParams = (RkAiqAlgoProcAdebayer*)inparams;
146     RkAiqAlgoProcResAdebayer* pAdebayerProcResParams = (RkAiqAlgoProcResAdebayer*)outparams;
147     AdebayerContext_t* pAdebayerCtx = (AdebayerContext_t *)&inparams->ctx->adebayerCtx;
148 
149     LOGV_ADEBAYER("%s: (enter)\n", __FUNCTION__ );
150 
151     if (pAdebayerProcParams->com.u.proc.is_bw_sensor) {
152         pAdebayerCtx->config.enable = 0;
153         pAdebayerCtx->config.updatecfg = true;
154     } else {
155         //get ISO from curExp
156         RKAiqAecExpInfo_t *curExp = pAdebayerProcParams->com.u.proc.curExp;
157         if(curExp != NULL) {
158             if(pAdebayerProcParams->hdr_mode == RK_AIQ_WORKING_MODE_NORMAL) {
159                 iso = curExp->LinearExp.exp_real_params.analog_gain *
160                       curExp->LinearExp.exp_real_params.digital_gain *
161                       curExp->LinearExp.exp_real_params.isp_dgain * 50;
162                 LOGD_ADEBAYER("%s:NORMAL:iso=%d,again=%f\n", __FUNCTION__, iso,
163                               curExp->LinearExp.exp_real_params.analog_gain);
164             } else if(RK_AIQ_HDR_GET_WORKING_MODE(pAdebayerProcParams->hdr_mode) == RK_AIQ_WORKING_MODE_ISP_HDR2) {
165                 iso = curExp->HdrExp[1].exp_real_params.analog_gain *
166                       curExp->HdrExp[1].exp_real_params.digital_gain *
167                       curExp->HdrExp[1].exp_real_params.isp_dgain * 50;
168                 LOGD_ADEBAYER("%s:HDR2:iso=%d,again=%f\n", __FUNCTION__, iso,
169                               curExp->HdrExp[1].exp_real_params.analog_gain);
170             } else if(RK_AIQ_HDR_GET_WORKING_MODE(pAdebayerProcParams->hdr_mode) == RK_AIQ_WORKING_MODE_ISP_HDR3) {
171                 iso = curExp->HdrExp[2].exp_real_params.analog_gain *
172                       curExp->HdrExp[2].exp_real_params.digital_gain *
173                       curExp->HdrExp[2].exp_real_params.isp_dgain * 50;
174                 LOGD_ADEBAYER("%s:HDR3:iso=%d,again=%f\n", __FUNCTION__, iso,
175                               curExp->HdrExp[2].exp_real_params.analog_gain);
176             }
177         } else {
178             LOGE_ADEBAYER("%s: curExp is NULL, so use default instead \n", __FUNCTION__);
179         }
180 
181         if (iso != pAdebayerCtx->iso) {
182             pAdebayerCtx->iso = iso;
183             pAdebayerCtx->config.updatecfg = true;
184         }
185 
186         if (pAdebayerCtx->is_reconfig) {
187             LOGD_ADEBAYER("%s: is_reconfig = true \n", __FUNCTION__);
188             pAdebayerCtx->config.updatecfg = true;
189             pAdebayerCtx->is_reconfig = false;
190         }
191 
192         if (pAdebayerCtx->config.updatecfg) {
193             AdebayerProcess(pAdebayerCtx, iso);
194 #if RKAIQ_HAVE_DEBAYER_V1
195             AdebayerGetProcResult(pAdebayerCtx, &pAdebayerProcResParams->debayerResV1);
196 #endif
197 
198 #if RKAIQ_HAVE_DEBAYER_V2 || RKAIQ_HAVE_DEBAYER_V2_LITE
199             AdebayerGetProcResult(pAdebayerCtx, &pAdebayerProcResParams->debayerResV2);
200 #endif
201             outparams->cfg_update = true;
202         } else {
203             outparams->cfg_update = false;
204         }
205     }
206 
207     LOGV_ADEBAYER("%s: (exit)\n", __FUNCTION__ );
208     return XCAM_RETURN_NO_ERROR;
209 }
210 
211 static XCamReturn
post_process(const RkAiqAlgoCom * inparams,RkAiqAlgoResCom * outparams)212 post_process
213 (
214     const RkAiqAlgoCom* inparams,
215     RkAiqAlgoResCom* outparams
216 )
217 {
218     LOGV_ADEBAYER("%s: (enter)\n", __FUNCTION__ );
219 
220     LOGV_ADEBAYER("%s: (exit)\n", __FUNCTION__ );
221     return XCAM_RETURN_NO_ERROR;
222 }
223 
224 RkAiqAlgoDescription g_RkIspAlgoDescAdebayer = {
225     .common = {
226         .version = RKISP_ALGO_ADEBAYER_VERSION,
227         .vendor  = RKISP_ALGO_ADEBAYER_VENDOR,
228         .description = RKISP_ALGO_ADEBAYER_DESCRIPTION,
229         .type    = RK_AIQ_ALGO_TYPE_ADEBAYER,
230         .id      = 0,
231         .create_context  = create_context,
232         .destroy_context = destroy_context,
233     },
234     .prepare = prepare,
235     .pre_process = NULL,
236     .processing = processing,
237     .post_process = NULL,
238 };
239 
240 RKAIQ_END_DECLARE
241