xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/algos/agamma/rk_aiq_agamma_algo_v11.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * rk_aiq_agamma_algo_v11.cp
3  *
4  *  Copyright (c) 2019-2022 Rockchip Eletronics Co., Ltd.
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 #include "rk_aiq_agamma_algo_v11.h"
20 #include <string.h>
21 #include "rk_aiq_types_agamma_algo_int.h"
22 #include "rk_aiq_types_agamma_algo_prvt.h"
23 #include "xcam_common.h"
24 
25 RKAIQ_BEGIN_DECLARE
26 
AgammaInit(AgammaHandle_t ** pGammaCtx,CamCalibDbV2Context_t * pCalib)27 XCamReturn AgammaInit(AgammaHandle_t** pGammaCtx, CamCalibDbV2Context_t* pCalib) {
28     LOG1_AGAMMA("ENTER: %s \n", __func__);
29     XCamReturn ret         = XCAM_RETURN_NO_ERROR;
30     AgammaHandle_t* handle = (AgammaHandle_t*)calloc(sizeof(AgammaHandle_t), 1);
31     if (NULL == handle) return XCAM_RETURN_ERROR_MEM;
32 
33     CalibDbV2_gamma_v11_t* calibv2_agamma_calib =
34         (CalibDbV2_gamma_v11_t*)(CALIBDBV2_GET_MODULE_PTR(pCalib, agamma_calib));
35 
36     if (!calibv2_agamma_calib) {
37         free(handle);
38         return XCAM_RETURN_ERROR_MEM;
39     }
40     memcpy(&handle->agammaAttrV11.stAuto, calibv2_agamma_calib, sizeof(CalibDbV2_gamma_v11_t));
41     handle->agammaAttrV11.mode              = RK_AIQ_GAMMA_MODE_AUTO;
42     handle->ifReCalcStAuto                  = true;
43 
44     *pGammaCtx = handle;
45     LOG1_AGAMMA("EXIT: %s \n", __func__);
46     return (ret);
47 }
48 
AgammaRelease(AgammaHandle_t * pGammaCtx)49 XCamReturn AgammaRelease(AgammaHandle_t* pGammaCtx) {
50     LOG1_AGAMMA("ENTER: %s \n", __func__);
51     XCamReturn ret = XCAM_RETURN_NO_ERROR;
52     if (pGammaCtx) free(pGammaCtx);
53 
54     LOG1_AGAMMA("EXIT: %s \n", __func__);
55     return (ret);
56 }
57 
AgammaSetProcResV11(AgammaHandle_t * pGammaCtx,AgammaProcRes_t * pAgammaProcRes)58 void AgammaSetProcResV11(AgammaHandle_t* pGammaCtx, AgammaProcRes_t* pAgammaProcRes) {
59     LOG1_AGAMMA("ENTER: %s \n", __func__);
60 
61     pAgammaProcRes->Gamma_v11.gamma_en = pGammaCtx->agamma_config.gamma_en;
62     pAgammaProcRes->Gamma_v11.EnableDot49 =
63         pGammaCtx->agamma_config.gamma_out_segnum == ISP3X_SEGNUM_LOG_49 ? true : false;
64     pAgammaProcRes->Gamma_v11.equ_segm = pGammaCtx->agamma_config.gamma_out_segnum > 1
65                                              ? ISP3X_SEGNUM_LOG_45
66                                              : pGammaCtx->agamma_config.gamma_out_segnum;
67     pAgammaProcRes->Gamma_v11.offset = pGammaCtx->agamma_config.gamma_out_offset;
68     for (int i = 0; i < CALIBDB_AGAMMA_KNOTS_NUM_V11; i++)
69         pAgammaProcRes->Gamma_v11.gamma_y[i] = pGammaCtx->agamma_config.gamma_table[i];
70 
71     LOG1_AGAMMA("EXIT: %s \n", __func__);
72 }
73 
AgammaProcessing(AgammaHandle_t * pGammaCtx,AgammaProcRes_t * pProcRes)74 void AgammaProcessing(AgammaHandle_t* pGammaCtx, AgammaProcRes_t* pProcRes) {
75     LOG1_AGAMMA("ENTER: %s \n", __func__);
76 
77     if (pGammaCtx->agammaAttrV11.mode == RK_AIQ_GAMMA_MODE_AUTO) {
78             pGammaCtx->agamma_config.gamma_en =
79                 pGammaCtx->agammaAttrV11.stAuto.GammaTuningPara.Gamma_en;
80             if (pGammaCtx->agamma_config.gamma_en) {
81                 pGammaCtx->agamma_config.gamma_out_segnum = ISP3X_SEGNUM_LOG_49;
82                 pGammaCtx->agamma_config.gamma_out_offset =
83                     pGammaCtx->agammaAttrV11.stAuto.GammaTuningPara.Gamma_out_offset;
84 
85                 for (int i = 0; i < CALIBDB_AGAMMA_KNOTS_NUM_V11; i++) {
86                     int tmp = pGammaCtx->agammaAttrV11.stAuto.GammaTuningPara.Gamma_curve[i];
87                     pGammaCtx->agamma_config.gamma_table[i] =
88                         LIMIT_VALUE(tmp, GAMMA_CRUVE_MAX, GAMMA_CRUVE_MIN);
89                 }
90             }
91     } else if (pGammaCtx->agammaAttrV11.mode == RK_AIQ_GAMMA_MODE_MANUAL) {
92             pGammaCtx->agamma_config.gamma_en = pGammaCtx->agammaAttrV11.stManual.Gamma_en;
93             if (pGammaCtx->agamma_config.gamma_en) {
94                 pGammaCtx->agamma_config.gamma_out_segnum = ISP3X_SEGNUM_LOG_49;
95                 pGammaCtx->agamma_config.gamma_out_offset =
96                     pGammaCtx->agammaAttrV11.stManual.Gamma_out_offset;
97 
98                 for (int i = 0; i < CALIBDB_AGAMMA_KNOTS_NUM_V11; i++) {
99                     int tmp = pGammaCtx->agammaAttrV11.stManual.Gamma_curve[i];
100                     pGammaCtx->agamma_config.gamma_table[i] =
101                         LIMIT_VALUE(tmp, GAMMA_CRUVE_MAX, GAMMA_CRUVE_MIN);
102                 }
103             }
104     } else
105         LOGE_AGAMMA(" %s: FrameID:%d Wrong gamma mode !!!\n", __func__, pGammaCtx->FrameID);
106 
107     LOGD_AGAMMA(
108         " %s: FrameID:%d gamma_en:%d gamma_out_segnum:%d gamma_out_offset:%d apiMode:%d "
109         "ifReCalcStManual:%d ifReCalcStAuto:%d\n",
110         __func__, pGammaCtx->FrameID, pGammaCtx->agamma_config.gamma_en,
111         pGammaCtx->agamma_config.gamma_out_segnum, pGammaCtx->agamma_config.gamma_out_offset,
112         pGammaCtx->agammaAttrV11.mode, pGammaCtx->ifReCalcStManual, pGammaCtx->ifReCalcStAuto);
113     LOGD_AGAMMA(" %s: Gamma_curve[0-15]:%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
114                 __func__, pGammaCtx->agamma_config.gamma_table[0],
115                 pGammaCtx->agamma_config.gamma_table[1], pGammaCtx->agamma_config.gamma_table[2],
116                 pGammaCtx->agamma_config.gamma_table[3], pGammaCtx->agamma_config.gamma_table[4],
117                 pGammaCtx->agamma_config.gamma_table[5], pGammaCtx->agamma_config.gamma_table[6],
118                 pGammaCtx->agamma_config.gamma_table[7], pGammaCtx->agamma_config.gamma_table[8],
119                 pGammaCtx->agamma_config.gamma_table[9], pGammaCtx->agamma_config.gamma_table[10],
120                 pGammaCtx->agamma_config.gamma_table[11], pGammaCtx->agamma_config.gamma_table[12],
121                 pGammaCtx->agamma_config.gamma_table[13], pGammaCtx->agamma_config.gamma_table[14],
122                 pGammaCtx->agamma_config.gamma_table[15]);
123     LOGD_AGAMMA(" %s: Gamma_curve[16-31]:%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
124                 __func__, pGammaCtx->agamma_config.gamma_table[16],
125                 pGammaCtx->agamma_config.gamma_table[17], pGammaCtx->agamma_config.gamma_table[18],
126                 pGammaCtx->agamma_config.gamma_table[19], pGammaCtx->agamma_config.gamma_table[20],
127                 pGammaCtx->agamma_config.gamma_table[21], pGammaCtx->agamma_config.gamma_table[22],
128                 pGammaCtx->agamma_config.gamma_table[23], pGammaCtx->agamma_config.gamma_table[24],
129                 pGammaCtx->agamma_config.gamma_table[25], pGammaCtx->agamma_config.gamma_table[26],
130                 pGammaCtx->agamma_config.gamma_table[27], pGammaCtx->agamma_config.gamma_table[28],
131                 pGammaCtx->agamma_config.gamma_table[29], pGammaCtx->agamma_config.gamma_table[30],
132                 pGammaCtx->agamma_config.gamma_table[31]);
133     LOGD_AGAMMA(" %s: Gamma_curve[32-48]:%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
134                 __func__, pGammaCtx->agamma_config.gamma_table[32],
135                 pGammaCtx->agamma_config.gamma_table[33], pGammaCtx->agamma_config.gamma_table[34],
136                 pGammaCtx->agamma_config.gamma_table[35], pGammaCtx->agamma_config.gamma_table[36],
137                 pGammaCtx->agamma_config.gamma_table[37], pGammaCtx->agamma_config.gamma_table[38],
138                 pGammaCtx->agamma_config.gamma_table[39], pGammaCtx->agamma_config.gamma_table[40],
139                 pGammaCtx->agamma_config.gamma_table[41], pGammaCtx->agamma_config.gamma_table[42],
140                 pGammaCtx->agamma_config.gamma_table[43], pGammaCtx->agamma_config.gamma_table[44],
141                 pGammaCtx->agamma_config.gamma_table[45], pGammaCtx->agamma_config.gamma_table[46],
142                 pGammaCtx->agamma_config.gamma_table[47], pGammaCtx->agamma_config.gamma_table[48]);
143 
144     pGammaCtx->CurrApiMode = pGammaCtx->agammaAttrV11.mode;
145 
146     AgammaSetProcResV11(pGammaCtx, pProcRes);
147 
148     LOG1_AGAMMA("EXIT: %s \n", __func__);
149 }
150 RKAIQ_END_DECLARE
151