xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/algos/a3dlut/rk_aiq_a3dlut_algo.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2 * alut3d.cpp
3 
4 * for rockchip v2.0.0
5 *
6 *  Copyright (c) 2019 Rockchip Corporation
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21 /* for rockchip v2.0.0*/
22 
23 #include "a3dlut/rk_aiq_a3dlut_algo.h"
24 #include "xcam_log.h"
25 #include "interpolation.h"
26 
27 RKAIQ_BEGIN_DECLARE
28 
29 
lut3d_index_estimation(int lut_num,const CalibDbV2_Lut3D_LutPara_t lutAll[],float awbGain[2],int * index)30 XCamReturn lut3d_index_estimation(int lut_num, const CalibDbV2_Lut3D_LutPara_t lutAll[], float awbGain[2], int* index)
31 {
32 
33     LOG1_ACCM( "%s: (enter)\n", __FUNCTION__);
34     float minDist = 9999999;
35     float *dist = (float*)malloc(lut_num*sizeof(float));
36     float nRG, nBG;
37     nRG = awbGain[0];
38     nBG = awbGain[1];
39     *index = 0;
40     XCamReturn ret = XCAM_RETURN_ERROR_FAILED;
41     for(int i = 0; i < lut_num; i++)
42     {
43         dist[i] = (nRG - lutAll[i].awbGain[0]) * (nRG - lutAll[i].awbGain[0]) +
44                   (nBG - lutAll[i].awbGain[1]) * (nBG - lutAll[i].awbGain[1]);
45         if(dist[i] < minDist)
46         {
47             minDist = dist[i];
48             *index = i;
49             ret = XCAM_RETURN_NO_ERROR;
50         }
51     }
52     if(ret != XCAM_RETURN_NO_ERROR) {
53         LOGE_A3DLUT("fail to estimate idx!!!\n");
54     }
55 
56     LOGD_A3DLUT("wbGain:%f,%f, estimation lut is %s\n", awbGain[0], awbGain[1],
57                 lutAll[*index].name);
58 
59     if (dist)
60         free(dist);
61 
62     LOG1_A3DLUT("%s: (exit)\n", __FUNCTION__);
63     return ret;
64 }
65 
66 #if RKAIQ_A3DLUT_ILLU_VOTE
UpdateDominateIdxList(struct list_head * l,int idx,int listMaxSize)67 static void UpdateDominateIdxList(struct list_head *l, int idx, int listMaxSize)
68 {
69     idx_node_t *pCurNode;
70     if(listMaxSize == 0) {
71         return;
72     }
73     int sizeList = get_list_num(l);
74     if (sizeList < listMaxSize) {
75         pCurNode = (idx_node_t*)malloc(sizeof(idx_node_t));
76         pCurNode->value = idx;
77         list_prepare_item(&pCurNode->p_next);
78         list_add_tail((struct list_head*)(&pCurNode->p_next), l);
79     } else {
80         idx_node_t *pDelNode = (idx_node_t *)(l->next);
81         pDelNode->value = idx;
82         struct list_head* node0 = l->next;
83         list_swap_item(l, node0);
84     }
85 }
86 
StableIdxEstimation(struct list_head * l,int listSize,int Num,int * newIdx)87 static void StableIdxEstimation(struct list_head *l, int listSize, int Num, int *newIdx)
88 {
89     int sizeList = get_list_num(l);
90     if(sizeList < listSize || listSize == 0) {
91         return;
92     }
93 
94     struct list_head *pNextNode = l->next;
95     idx_node_t *pL;
96     int *Set = (int*)malloc(Num*sizeof(int));
97     memset(Set, 0, Num*sizeof(int));
98     while (NULL != pNextNode)
99     {
100         pL = (idx_node_t*)pNextNode;
101         Set[pL->value]++;
102         pNextNode = pNextNode->next;
103     }
104     int max_count = 0;
105     for(int i=0; i<Num; i++){
106         LOGV_A3DLUT("Lut (%d), count(%d)\n", i, Set[i]);
107         if(Set[i] > max_count){
108             max_count = Set[i];
109             *newIdx = i;
110         }
111     }
112     free(Set);
113 }
114 #endif
115 
116 /******************************************************************************
117  * Damping
118  *****************************************************************************/
Damping(const float damp,const rk_aiq_lut3d_hw_tbl_t * pUndamped,rk_aiq_lut3d_hw_tbl_t * pDamped,int * lutSum)119 static XCamReturn Damping(const float damp,                        /**< damping coefficient */
120                           const rk_aiq_lut3d_hw_tbl_t* pUndamped, /**< undamped new computed lut */
121                           rk_aiq_lut3d_hw_tbl_t* pDamped,    /**< old lut and result */
122                           int* lutSum
123 ) {
124     XCamReturn result = XCAM_RETURN_ERROR_PARAM;
125 
126     if ( (pUndamped != NULL) && (pDamped != NULL) )
127     {
128         const float f = 1 - damp;
129         memset(lutSum, 0, 3*sizeof(int));
130 
131         /* calc. damped lut */
132         for (int i = 0; i < LUT3D_LUT_WSIZE; i++) {
133             pDamped->look_up_table_r[i] =
134                 (unsigned short) (damp * (float) pDamped->look_up_table_r[i] + f * (float) pUndamped->look_up_table_r[i]);
135             pDamped->look_up_table_g[i] =
136                 (unsigned short) (damp * (float) pDamped->look_up_table_g[i] + f * (float) pUndamped->look_up_table_g[i]);
137             pDamped->look_up_table_b[i] =
138                 (unsigned short) (damp * (float) pDamped->look_up_table_b[i] + f * (float) pUndamped->look_up_table_b[i]);
139             lutSum[0] += pDamped->look_up_table_r[i];
140             lutSum[1] += pDamped->look_up_table_g[i];
141             lutSum[2] += pDamped->look_up_table_b[i];
142         }
143 
144         result = XCAM_RETURN_NO_ERROR;
145     }
146     LOGD_A3DLUT( "dampfactor:%f \n", damp);
147     return ( result );
148 }
149 
150 /******************************************************************************
151  * interpolation lut table
152  *****************************************************************************/
InterpLutbyAlp(const float alp,const rk_aiq_lut3d_hw_tbl_t * pLut0,const rk_aiq_lut3d_hw_tbl_t * pLutA,rk_aiq_lut3d_hw_tbl_t * pLutB)153 static XCamReturn InterpLutbyAlp(const float alp,
154                         const rk_aiq_lut3d_hw_tbl_t* pLut0,
155                         const rk_aiq_lut3d_hw_tbl_t* pLutA,
156                         rk_aiq_lut3d_hw_tbl_t* pLutB
157 ) {
158     XCamReturn result = XCAM_RETURN_ERROR_PARAM;
159 
160     if ((pLutA != NULL) && (pLutB != NULL) && (pLut0 != NULL))
161     {
162         uint32_t alpha = uint32_t(alp * 128.0f);
163 
164         if (alpha == 0) {
165             *pLutB = *pLut0;
166         } else if (alpha == 128) {
167             *pLutB = (rk_aiq_lut3d_hw_tbl_t) *pLutA;
168         } else {
169             LOGD_A3DLUT("begin lut interp by alp\n");
170             const uint32_t beta = 128 - alpha;
171 
172             for (uint32_t i = 0; i < LUT3D_LUT_WSIZE; i++) {
173                 pLutB->look_up_table_r[i] = (unsigned short)(alpha * (uint32_t)pLutA->look_up_table_r[i] + beta * (uint32_t)pLut0->look_up_table_r[i]);
174                 pLutB->look_up_table_g[i] = (unsigned short)(alpha * (uint32_t)pLutA->look_up_table_g[i] + beta * (uint32_t)pLut0->look_up_table_g[i]);
175                 pLutB->look_up_table_b[i] = (unsigned short)(alpha * (uint32_t)pLutA->look_up_table_b[i] + beta * (uint32_t)pLut0->look_up_table_b[i]);
176             }
177         }
178 
179         result = XCAM_RETURN_NO_ERROR;
180     }
181     return ( result );
182 }
183 
184 /******************************************************************************
185  * interpolation lut table by alp and damp
186  *****************************************************************************/
InterpLutbyAlpandDamp(const float alp,const rk_aiq_lut3d_hw_tbl_t * pLut0,const rk_aiq_lut3d_hw_tbl_t * pLutA,rk_aiq_lut3d_hw_tbl_t * pLutB,const float damp,int * lutSum)187 static XCamReturn InterpLutbyAlpandDamp(const float alp,
188                         const rk_aiq_lut3d_hw_tbl_t* pLut0,
189                         const rk_aiq_lut3d_hw_tbl_t* pLutA,
190                         rk_aiq_lut3d_hw_tbl_t* pLutB,
191                         const float damp,
192                         int* lutSum
193 
194 ) {
195     XCamReturn result = XCAM_RETURN_ERROR_PARAM;
196 
197     if ((pLutA != NULL) && (pLutB != NULL) && (pLut0 != NULL))
198     {
199         if (damp > 0.0) {
200             if (alp < DIVMIN) {
201                 result = Damping(damp, pLut0, pLutB, lutSum);
202             } else if (fabs(alp - 1) < DIVMIN) {
203                 result = Damping(damp, pLutA, pLutB, lutSum);
204             } else {
205                 const float beta = damp * alp;
206                 const float gamma = damp * (1 - alp);
207                 const float f = 1 - damp;
208                 memset(lutSum, 0, 3*sizeof(int));
209 
210                 /* calc. damped lut */
211                 for (uint32_t i = 0; i < LUT3D_LUT_WSIZE; i++) {
212                     pLutB->look_up_table_r[i] = (unsigned short) (beta * (float) pLutA->look_up_table_r[i] +
213                             gamma * (float) pLut0->look_up_table_r[i] +
214                             f * (float) pLutB->look_up_table_r[i]);
215                     pLutB->look_up_table_g[i] = (unsigned short) (beta * (float) pLutA->look_up_table_g[i] +
216                             gamma * (float) pLut0->look_up_table_g[i] +
217                             f * (float) pLutB->look_up_table_g[i]);
218                     pLutB->look_up_table_b[i] = (unsigned short) (beta * (float) pLutA->look_up_table_b[i] +
219                             gamma * (float) pLut0->look_up_table_b[i] +
220                             f * (float) pLutB->look_up_table_b[i]);
221                     lutSum[0] += pLutB->look_up_table_r[i];
222                     lutSum[1] += pLutB->look_up_table_g[i];
223                     lutSum[2] += pLutB->look_up_table_b[i];
224                 }
225                 result = XCAM_RETURN_NO_ERROR;
226             }
227         }
228     }
229     return ( result );
230 }
231 
Alut3dAutoConfig(alut3d_handle_t hAlut3d,bool forceRun)232 XCamReturn Alut3dAutoConfig
233 (
234     alut3d_handle_t hAlut3d,
235     bool forceRun
236 ) {
237 
238     LOGI_A3DLUT("%s: (enter)\n", __FUNCTION__);
239 
240     XCamReturn ret = XCAM_RETURN_NO_ERROR;
241 
242     if (!hAlut3d)
243         return XCAM_RETURN_ERROR_PARAM;
244 
245     const CalibDbV2_Lut3D_LutPara_t* pLutProfile = NULL;
246     float sensorGain =  hAlut3d->swinfo.sensorGain;
247     if (forceRun || hAlut3d->updateAtt) {
248         //(1) estimate idx
249         int dominateProfileIdx;
250         ret = lut3d_index_estimation(hAlut3d->calibV2_lut3d->ALut3D.lutAll_len, hAlut3d->calibV2_lut3d->ALut3D.lutAll, hAlut3d->swinfo.awbGain, &dominateProfileIdx);
251         RETURN_RESULT_IF_DIFFERENT(ret, XCAM_RETURN_NO_ERROR);
252 
253 #if RKAIQ_A3DLUT_ILLU_VOTE
254         int dominateListSize = 0;
255         dominateListSize = hAlut3d->calibV2_lut3d->ALut3D.lutAll_len*2+1;
256         UpdateDominateIdxList(&hAlut3d->restinfo.dominateIdxList, dominateProfileIdx, dominateListSize);
257         StableIdxEstimation(&hAlut3d->restinfo.dominateIdxList, dominateListSize, hAlut3d->calibV2_lut3d->ALut3D.lutAll_len, &dominateProfileIdx);
258 #endif
259         //(2) interpolate alpha
260         pLutProfile = &hAlut3d->calibV2_lut3d->ALut3D.lutAll[dominateProfileIdx];
261         hAlut3d->restinfo.pLutProfile = &hAlut3d->calibV2_lut3d->ALut3D.lutAll[dominateProfileIdx];
262 
263         float alp_tmp = 0;
264         interpolation(pLutProfile->gain_alpha.gain, pLutProfile->gain_alpha.alpha, 9, sensorGain,
265                       &alp_tmp);
266 
267         //(3) lut = alpha*lutfile + (1-alpha)*lut0
268         uint32_t alpha = uint32_t(alp_tmp * 128.0f);
269         hAlut3d->update = hAlut3d->calib_update ||
270                             (dominateProfileIdx != hAlut3d->restinfo.dominateIdx) ||
271                             (alpha != uint32_t(hAlut3d->restinfo.alpha * 128.0f));
272 
273         LOGD_A3DLUT("sensorGain: %f, lutidx_alp_update: %d, Alpha: %f->%f, LutIdx: %d->%d \n",
274                     hAlut3d->update, sensorGain,
275                     hAlut3d->restinfo.alpha, alp_tmp,
276                     hAlut3d->restinfo.dominateIdx, dominateProfileIdx);
277         if (hAlut3d->update) {
278             if (alpha == 0)
279                 hAlut3d->restinfo.alpha = 0;
280             else if (alpha > 128)
281                 hAlut3d->restinfo.alpha = 1;
282             else
283                 hAlut3d->restinfo.alpha = alp_tmp;
284             hAlut3d->restinfo.dominateIdx = dominateProfileIdx;
285         }
286 
287     }
288 
289         //(4) damp
290     if ((!hAlut3d->calibV2_lut3d->ALut3D.damp_en) && (hAlut3d->update)) { // dampen = 0 && (alp changed || lut changed)
291         hAlut3d->swinfo.lut3dConverged = true;
292         ret = InterpLutbyAlp(hAlut3d->restinfo.alpha, &hAlut3d->lut0,
293                         (const rk_aiq_lut3d_hw_tbl_t *)(&hAlut3d->restinfo.pLutProfile->Table),
294                         &hAlut3d->lut3d_hw_conf.tbl);
295     } else if (hAlut3d->calibV2_lut3d->ALut3D.damp_en && ((hAlut3d->swinfo.count <= 1) || (hAlut3d->swinfo.invarMode == 0))) { // first frame or attr mode changed
296         hAlut3d->swinfo.lut3dConverged = true;
297         ret = InterpLutbyAlp(hAlut3d->restinfo.alpha, &hAlut3d->lut0,
298                         (const rk_aiq_lut3d_hw_tbl_t *)(&hAlut3d->restinfo.pLutProfile->Table),
299                         &hAlut3d->lut3d_hw_conf.tbl);
300 
301     } else if (hAlut3d->calibV2_lut3d->ALut3D.damp_en && (hAlut3d->swinfo.awbIIRDampCoef > 0.0) && ((!hAlut3d->swinfo.lut3dConverged) || hAlut3d->update) ) {
302         ret = InterpLutbyAlpandDamp(hAlut3d->restinfo.alpha, &hAlut3d->lut0,
303                         (const rk_aiq_lut3d_hw_tbl_t *)(& hAlut3d->restinfo.pLutProfile->Table),
304                         &hAlut3d->lut3d_hw_conf.tbl,
305                         hAlut3d->swinfo.awbIIRDampCoef,
306                         hAlut3d->restinfo.lutSum);
307         hAlut3d->swinfo.lut3dConverged = !(abs(hAlut3d->restinfo.lutSum[0] - hAlut3d->restinfo.lutSum_last[0]) > 0 ||
308                                            abs(hAlut3d->restinfo.lutSum[1] - hAlut3d->restinfo.lutSum_last[1]) > 0 ||
309                                            abs(hAlut3d->restinfo.lutSum[2] - hAlut3d->restinfo.lutSum_last[2]) > 0);
310 
311         memcpy(hAlut3d->restinfo.lutSum_last, hAlut3d->restinfo.lutSum, sizeof(int)*3);
312 
313         LOGD_A3DLUT("DampCoef = %f, damp lutB[7] = %d, lut converge: %d, count = %d\n", hAlut3d->swinfo.awbIIRDampCoef, hAlut3d->lut3d_hw_conf.tbl.look_up_table_b[7],
314                     hAlut3d->swinfo.lut3dConverged, hAlut3d->swinfo.count);
315 
316     } else {
317         hAlut3d->swinfo.lut3dConverged = true;
318     }
319 
320     LOGI_A3DLUT("%s: (exit)\n", __FUNCTION__);
321 
322     return (ret);
323 }
324 
Alut3dManualConfig(alut3d_handle_t hAlut3d)325 XCamReturn Alut3dManualConfig
326 (
327     alut3d_handle_t hAlut3d
328 ) {
329 
330     LOGI_A3DLUT("%s: (enter)\n", __FUNCTION__);
331 
332     memcpy(hAlut3d->lut3d_hw_conf.tbl.look_up_table_r, hAlut3d->mCurAtt.stManual.look_up_table_r, sizeof(unsigned short)*LUT3D_LUT_WSIZE);
333     memcpy(hAlut3d->lut3d_hw_conf.tbl.look_up_table_r, hAlut3d->mCurAtt.stManual.look_up_table_r, sizeof(unsigned short)*LUT3D_LUT_WSIZE);
334     memcpy(hAlut3d->lut3d_hw_conf.tbl.look_up_table_r, hAlut3d->mCurAtt.stManual.look_up_table_r, sizeof(unsigned short)*LUT3D_LUT_WSIZE);
335 
336     LOGI_A3DLUT("%s: (exit)\n", __FUNCTION__);
337 
338     return XCAM_RETURN_NO_ERROR;
339 }
340 
Alut3dConfig(alut3d_handle_t hAlut3d)341 XCamReturn Alut3dConfig
342 (
343     alut3d_handle_t hAlut3d
344 ) {
345 
346     LOGI_A3DLUT("%s: (enter)\n", __FUNCTION__);
347 
348     XCamReturn ret = XCAM_RETURN_NO_ERROR;
349 
350     if (hAlut3d == NULL) {
351         return XCAM_RETURN_ERROR_PARAM;
352     }
353 
354     LOGD_A3DLUT("%s: updateAtt: %d\n", __FUNCTION__, hAlut3d->updateAtt);
355 #if 0
356     if(hAlut3d->updateAtt) {
357         hAlut3d->swinfo.invarMode = 1;
358         //hAlut3d->mCurAtt = hAlut3d->mNewAtt;
359     }
360 #endif
361 
362     LOGD_A3DLUT("%s: byPass: %d  mode:%d \n", __FUNCTION__, hAlut3d->mCurAtt.byPass, hAlut3d->mCurAtt.mode);
363     if(hAlut3d->mCurAtt.byPass != true) {
364         hAlut3d->lut3d_hw_conf.enable = true;
365         hAlut3d->lut3d_hw_conf.bypass_en = false;
366 
367         if(hAlut3d->mCurAtt.mode == RK_AIQ_LUT3D_MODE_AUTO) {
368             bool update = false;
369             if (fabs(hAlut3d->restinfo.res3a_info.sensorGain - hAlut3d->swinfo.sensorGain) > hAlut3d->calibV2_lut3d->common.gain_tolerance) {
370                 hAlut3d->restinfo.res3a_info.gain_stable = false;
371                 LOGD_A3DLUT( "%s: update sensorGain:%f \n", __FUNCTION__, hAlut3d->swinfo.sensorGain);
372                 hAlut3d->restinfo.res3a_info.sensorGain = hAlut3d->swinfo.sensorGain;
373             } else {
374                 hAlut3d->restinfo.res3a_info.gain_stable = true;
375                 LOGD_A3DLUT( "%s: not update sensorGain:%f \n", __FUNCTION__, hAlut3d->swinfo.sensorGain);
376                 hAlut3d->swinfo.sensorGain = hAlut3d->restinfo.res3a_info.sensorGain;
377             }
378 
379             LOGD_A3DLUT("awbConverged: %d, calib_update: %d\n", hAlut3d->swinfo.awbConverged, hAlut3d->calib_update);
380 
381             if (hAlut3d->swinfo.awbConverged &&
382                 hAlut3d->restinfo.res3a_info.gain_stable &&
383                 (!hAlut3d->calib_update)) {
384                 update = false;
385                 hAlut3d->update = false;
386             } else
387                 update = true;
388             if (update || hAlut3d->updateAtt || (!hAlut3d->swinfo.lut3dConverged))
389                 Alut3dAutoConfig(hAlut3d, update);
390         } else if(hAlut3d->mCurAtt.mode == RK_AIQ_LUT3D_MODE_MANUAL) {
391             if (hAlut3d->updateAtt)
392                 Alut3dManualConfig(hAlut3d);
393         } else {
394             LOGE_A3DLUT("%s: hAlut3d->mCurAtt.mode(%d) is invalid \n", __FUNCTION__, hAlut3d->mCurAtt.mode);
395         }
396 
397         LOGD_A3DLUT("final lutB[7] = %d\n", hAlut3d->lut3d_hw_conf.tbl.look_up_table_b[7]);
398 #if 0
399         memcpy(hAlut3d->mCurAtt.stManual.look_up_table_r, hAlut3d->lut3d_hw_conf.look_up_table_r,
400                sizeof(hAlut3d->mCurAtt.stManual.look_up_table_r));
401         memcpy( hAlut3d->mCurAtt.stManual.look_up_table_g, hAlut3d->lut3d_hw_conf.look_up_table_g,
402                 sizeof(hAlut3d->mCurAtt.stManual.look_up_table_g));
403         memcpy(hAlut3d->mCurAtt.stManual.look_up_table_b, hAlut3d->lut3d_hw_conf.look_up_table_b,
404                sizeof(hAlut3d->mCurAtt.stManual.look_up_table_b));
405 #endif
406     } else {
407         hAlut3d->lut3d_hw_conf.enable = false;
408         hAlut3d->lut3d_hw_conf.bypass_en = true;
409     }
410     //hAlut3d->swinfo.invarMode = 0;
411 
412     LOGD_A3DLUT("%s: enable:(%d),bypass_en(%d) \n", __FUNCTION__,
413                 hAlut3d->lut3d_hw_conf.enable,
414                 hAlut3d->lut3d_hw_conf.bypass_en);
415     hAlut3d->swinfo.count = ((hAlut3d->swinfo.count + 2) > (65536)) ? 2 : (hAlut3d->swinfo.count + 1);
416     LOGI_A3DLUT("%s: (exit)\n", __FUNCTION__);
417 
418     return (ret);
419 }
420 
UpdateLut3dCalibPara(alut3d_handle_t hAlut3d)421 static XCamReturn UpdateLut3dCalibPara(alut3d_handle_t  hAlut3d)
422 {
423     LOGI_A3DLUT("%s: (enter)  \n", __FUNCTION__);
424     XCamReturn ret = XCAM_RETURN_NO_ERROR;
425     bool config_calib = !!(hAlut3d->prepare_type & RK_AIQ_ALGO_CONFTYPE_UPDATECALIB);
426     if(!config_calib)
427     {
428         return(ret);
429     }
430 
431     hAlut3d->lut3d_hw_conf.lut3d_lut_wsize = LUT3D_LUT_WSIZE;
432     memcpy(hAlut3d->lut3d_hw_conf.tbl.look_up_table_r, hAlut3d->calib_lut3d->look_up_table_r,
433            sizeof(hAlut3d->calib_lut3d->look_up_table_r));
434     memcpy(hAlut3d->lut3d_hw_conf.tbl.look_up_table_g, hAlut3d->calib_lut3d->look_up_table_g,
435            sizeof(hAlut3d->calib_lut3d->look_up_table_g));
436     memcpy(hAlut3d->lut3d_hw_conf.tbl.look_up_table_b, hAlut3d->calib_lut3d->look_up_table_b,
437            sizeof(hAlut3d->calib_lut3d->look_up_table_b));
438 
439     hAlut3d->mCurAtt.byPass = !(hAlut3d->calib_lut3d->enable);
440     LOGI_A3DLUT("%s: (exit)  \n", __FUNCTION__);
441     return(ret);
442 }
443 
UpdateLut3dCalibV2Para(alut3d_handle_t hAlut3d)444 static XCamReturn UpdateLut3dCalibV2Para(alut3d_handle_t  hAlut3d)
445 {
446     LOGI_A3DLUT("%s: (enter)  \n", __FUNCTION__);
447     XCamReturn ret = XCAM_RETURN_NO_ERROR;
448     bool config_calib = !!(hAlut3d->prepare_type & RK_AIQ_ALGO_CONFTYPE_UPDATECALIB);
449     if(!config_calib)
450     {
451         return(ret);
452     }
453 
454     // hAlut3d->swinfo.invarMode = 1/*hAlut3d->mCurAtt.mode & hAlut3d->mNewAtt.mode*/;
455     if (hAlut3d->mCurAtt.mode == RK_AIQ_LUT3D_MODE_AUTO) {
456         hAlut3d->mCurAtt.byPass = !(hAlut3d->calibV2_lut3d->common.enable);
457     }
458 
459     hAlut3d->swinfo.lut3dConverged = false;
460     hAlut3d->calib_update = true;
461 
462     hAlut3d->lut3d_hw_conf.lut3d_lut_wsize = LUT3D_LUT_WSIZE;
463 
464 #if RKAIQ_A3DLUT_ILLU_VOTE
465     clear_list(&hAlut3d->restinfo.dominateIdxList);
466 #endif
467 
468     LOGI_A3DLUT("%s: (exit)  \n", __FUNCTION__);
469     return(ret);
470 }
471 
Alut3dInit(alut3d_handle_t * hAlut3d,const CamCalibDbV2Context_t * calibv2)472 XCamReturn Alut3dInit(alut3d_handle_t *hAlut3d, const CamCalibDbV2Context_t* calibv2)
473 {
474     LOGI_A3DLUT("%s: (enter)\n", __FUNCTION__);
475 
476     XCamReturn ret = XCAM_RETURN_NO_ERROR;
477 
478     if(calibv2 == NULL) {
479         return XCAM_RETURN_ERROR_FAILED;
480     }
481     const CalibDbV2_Lut3D_Para_V2_t *calib_lut3d =
482         (CalibDbV2_Lut3D_Para_V2_t*)(CALIBDBV2_GET_MODULE_PTR((void*)calibv2, lut3d_calib));
483     if (calib_lut3d == NULL)
484         return XCAM_RETURN_ERROR_MEM;
485 
486     *hAlut3d = (alut3d_context_t*)malloc(sizeof(alut3d_context_t));
487     if (!*hAlut3d) return XCAM_RETURN_ERROR_MEM;
488 
489     alut3d_context_t* alut3d_contex = *hAlut3d;
490     memset(alut3d_contex, 0, sizeof(alut3d_context_t));
491     // initial lut 0
492     uint16_t lut_idx        = 0;
493     for (uint16_t i = 0; i < LUT3D_LUT_GRID_NUM; i++) {
494         for (uint16_t j = 0; j < LUT3D_LUT_GRID_NUM; j++) {
495             for (uint16_t k = 0; k < LUT3D_LUT_GRID_NUM; k++) {
496                 lut_idx                                      = i * LUT3D_LUT_GRID_NUM * LUT3D_LUT_GRID_NUM + j * LUT3D_LUT_GRID_NUM + k;
497                 alut3d_contex->lut0.look_up_table_r[lut_idx] = (k << 7) - (k >> 3);
498                 alut3d_contex->lut0.look_up_table_g[lut_idx] = (j << 9) - (j >> 3);
499                 alut3d_contex->lut0.look_up_table_b[lut_idx] = (i << 7) - (i >> 3);
500             }
501         }
502     }
503 
504     alut3d_contex->swinfo.sensorGain = 1.0;
505     alut3d_contex->swinfo.awbIIRDampCoef = 0;
506     alut3d_contex->swinfo.awbConverged = false;
507     alut3d_contex->swinfo.awbGain[0] = 1;
508     alut3d_contex->swinfo.awbGain[1] = 1;
509     alut3d_contex->swinfo.count = 0;
510     alut3d_contex->swinfo.invarMode = 1; // 0: mode change, 1: mode unchange
511     alut3d_contex->swinfo.lut3dConverged = false;
512 
513     alut3d_contex->restinfo.res3a_info.sensorGain = 1.0;
514     alut3d_contex->restinfo.res3a_info.gain_stable = false;
515     alut3d_contex->restinfo.alpha = 0.0;
516     alut3d_contex->restinfo.lutSum[0] = 0;
517     alut3d_contex->restinfo.lutSum[1] = 0;
518     alut3d_contex->restinfo.lutSum[2] = 0;
519     alut3d_contex->restinfo.lutSum_last[0] = 0;
520     alut3d_contex->restinfo.lutSum_last[1] = 0;
521     alut3d_contex->restinfo.lutSum_last[2] = 0;
522 #if RKAIQ_A3DLUT_ILLU_VOTE
523     INIT_LIST_HEAD(&alut3d_contex->restinfo.dominateIdxList);
524 #endif
525 
526     alut3d_contex->calibV2_lut3d = calib_lut3d;
527     alut3d_contex->mCurAtt.mode = RK_AIQ_LUT3D_MODE_AUTO;
528     alut3d_contex->prepare_type = RK_AIQ_ALGO_CONFTYPE_UPDATECALIB | RK_AIQ_ALGO_CONFTYPE_NEEDRESET;
529     ret = UpdateLut3dCalibV2Para(alut3d_contex);
530     LOGI_A3DLUT("%s: (exit)\n", __FUNCTION__);
531     return(ret);
532 
533 
534 }
535 
Alut3dRelease(alut3d_handle_t hAlut3d)536 XCamReturn Alut3dRelease(alut3d_handle_t hAlut3d)
537 {
538     LOGI_A3DLUT("%s: (enter)\n", __FUNCTION__);
539 
540     XCamReturn ret = XCAM_RETURN_NO_ERROR;
541 #if RKAIQ_A3DLUT_ILLU_VOTE
542     clear_list(&hAlut3d->restinfo.dominateIdxList);
543 #endif
544 
545     free(hAlut3d);
546 
547     LOGI_A3DLUT("%s: (exit)\n", __FUNCTION__);
548     return(ret);
549 
550 }
551 
Alut3dPrepare(alut3d_handle_t hAlut3d)552 XCamReturn Alut3dPrepare(alut3d_handle_t hAlut3d)
553 {
554     LOGI_A3DLUT("%s: (enter)\n", __FUNCTION__);
555 
556     XCamReturn ret = XCAM_RETURN_NO_ERROR;
557 
558     //ret = UpdateLut3dCalibPara(hAlut3d);
559      ret = UpdateLut3dCalibV2Para(hAlut3d);
560     LOGI_A3DLUT("%s: (exit)\n", __FUNCTION__);
561     return ret;
562 }
Alut3dPreProc(alut3d_handle_t hAlut3d)563 XCamReturn Alut3dPreProc(alut3d_handle_t hAlut3d)
564 {
565 
566     LOGI_A3DLUT("%s: (enter)\n", __FUNCTION__);
567 
568     XCamReturn ret = XCAM_RETURN_NO_ERROR;
569 
570     LOGI_A3DLUT("%s: (exit)\n", __FUNCTION__);
571     return(ret);
572 
573 }
Alut3dProcessing(alut3d_handle_t hAlut3d)574 XCamReturn Alut3dProcessing(alut3d_handle_t hAlut3d)
575 {
576     LOGI_A3DLUT("%s: (enter)\n", __FUNCTION__);
577 
578     XCamReturn ret = XCAM_RETURN_NO_ERROR;
579 
580 
581     LOGI_A3DLUT("%s: (exit)\n", __FUNCTION__);
582     return(ret);
583 }
584 
585 
586 
587 RKAIQ_END_DECLARE
588 
589 
590