hc
2023-11-22 f743a7adbd6e230d66a6206fa115b59fec2d88eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * rk_aiq_algo_ae_itf.c
 *
 *  Copyright (c) 2019 Rockchip Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
 
#include "rk_aiq_algo_types_int.h"
#include "atmo/rk_aiq_algo_atmo_itf.h"
#include "xcam_log.h"
#include "atmo/rk_aiq_atmo_algo.h"
#include "atmo/rk_aiq_types_atmo_algo_prvt.h"
 
 
RKAIQ_BEGIN_DECLARE
 
static XCamReturn AtmoCreateCtx(RkAiqAlgoContext **context, const AlgoCtxInstanceCfg* cfg)
{
 
    LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
    RESULT ret = ATMO_RET_SUCCESS;
    RkAiqAlgoContext *ctx = new RkAiqAlgoContext();
    AlgoCtxInstanceCfgInt* instanc_int = (AlgoCtxInstanceCfgInt*)cfg;
    if (ctx == NULL) {
        LOGE_ATMO( "%s: create atmo context fail!\n", __FUNCTION__);
        return XCAM_RETURN_ERROR_MEM;
    }
    memset(&ctx->AtmoInstConfig, 0x00, sizeof(AtmoInstanceConfig_t));
    ret = AtmoInit(&ctx->AtmoInstConfig, instanc_int->calibv2);
 
    if (ret != ATMO_RET_SUCCESS) {
        LOGE_ATMO("%s ATMO Init failed: %d", __FUNCTION__, ret);
        return(XCAM_RETURN_ERROR_FAILED);
    }
 
    *context = ctx;
 
    LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
    return(XCAM_RETURN_NO_ERROR);
}
 
static XCamReturn AtmoDestroyCtx(RkAiqAlgoContext *context)
{
    LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
    RESULT ret = ATMO_RET_SUCCESS;
 
    if(context != NULL) {
        AtmoHandle_t pAtmoCtx = (AtmoHandle_t)context->AtmoInstConfig.hAtmo;
        ret = AtmoRelease(pAtmoCtx);
        if (ret != ATMO_RET_SUCCESS) {
            LOGE_ATMO("%s Atmo Release failed: %d", __FUNCTION__, ret);
            return(XCAM_RETURN_ERROR_FAILED);
        }
        delete context;
        context = NULL;
    }
 
    LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
    return(XCAM_RETURN_NO_ERROR);
}
 
static XCamReturn AtmoPrepare(RkAiqAlgoCom* params)
{
    LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
    RESULT ret = ATMO_RET_SUCCESS;
 
    AtmoHandle_t pAtmoCtx = params->ctx->AtmoInstConfig.hAtmo;
    RkAiqAlgoConfigAtmoInt* AtmoCfgParam = (RkAiqAlgoConfigAtmoInt*)params; //come from params in html
    const CamCalibDbContext_t* pCalibDb = AtmoCfgParam->rk_com.u.prepare.calib;
    const CamCalibDbV2Context_t* pCalibDbv2 = AtmoCfgParam->rk_com.u.prepare.calibv2;
    pAtmoCtx->width = AtmoCfgParam->rawWidth;
    pAtmoCtx->height = AtmoCfgParam->rawHeight;
 
    if (AtmoCfgParam->working_mode < RK_AIQ_WORKING_MODE_ISP_HDR2)
        pAtmoCtx->FrameNumber = 1;
    else if (AtmoCfgParam->working_mode < RK_AIQ_WORKING_MODE_ISP_HDR3 &&
             AtmoCfgParam->working_mode >= RK_AIQ_WORKING_MODE_ISP_HDR2)
        pAtmoCtx->FrameNumber = 2;
    else
        pAtmoCtx->FrameNumber = 3;
 
    CalibDbV2_tmo_t* calibv2_atmo_calib =
        (CalibDbV2_tmo_t*)(CALIBDBV2_GET_MODULE_PTR((void*)pCalibDbv2, atmo_calib));
 
    if(!!(params->u.prepare.conf_type & RK_AIQ_ALGO_CONFTYPE_UPDATECALIB )) {
        LOGD_ATMO("%s: Ahdr Reload Para!\n", __FUNCTION__);
        memcpy(&pAtmoCtx->pCalibDB, calibv2_atmo_calib, sizeof(CalibDbV2_tmo_t));//load iq paras
    }
 
    if(/* !params->u.prepare.reconfig*/true) {
        AtmoStop(pAtmoCtx); // stop firstly for re-preapre
        ret = AtmoStart(pAtmoCtx);
        if (ret != ATMO_RET_SUCCESS) {
            LOGE_ATMO("%s Atmo Start failed: %d", __FUNCTION__, ret);
            return(XCAM_RETURN_ERROR_FAILED);
        }
    }
 
    //get aec delay frame
    CalibDb_Aec_ParaV2_t* aec =
        (CalibDb_Aec_ParaV2_t*)CALIBDBV2_GET_MODULE_PTR((void*)pCalibDbv2, ae_calib);
    pAtmoCtx->CurrAeResult.AecDelayframe = MAX(aec->CommCtrl.AecDelayFrmNum.BlackDelay,
                                           aec->CommCtrl.AecDelayFrmNum.WhiteDelay);
 
    LOGD_ATMO("%s:AecDelayframe:%d\n", __FUNCTION__, pAtmoCtx->CurrAeResult.AecDelayframe);
 
    LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
    return(XCAM_RETURN_NO_ERROR);
}
 
static XCamReturn AtmoPreProcess(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
{
    LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
    RESULT ret = ATMO_RET_SUCCESS;
 
    AtmoHandle_t pAtmoCtx = inparams->ctx->AtmoInstConfig.hAtmo;
    RkAiqAlgoConfigAtmoInt* AtmoCfgParam = (RkAiqAlgoConfigAtmoInt*)inparams;
 
    if(pAtmoCtx->tmoAttr.opMode == TMO_OPMODE_TOOL) {
        newMalloc(&pAtmoCtx->AtmoConfig, &pAtmoCtx->tmoAttr.stTool);
        AtmoUpdateConfig(pAtmoCtx, &pAtmoCtx->tmoAttr.stTool);
    }
    else {
        newMalloc(&pAtmoCtx->AtmoConfig, &pAtmoCtx->pCalibDB);
        AtmoUpdateConfig(pAtmoCtx, &pAtmoCtx->pCalibDB);
    }
 
    LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
    return(XCAM_RETURN_NO_ERROR);
}
 
static XCamReturn AtmoProcess(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
{
    LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
    RESULT ret = ATMO_RET_SUCCESS;
 
    AtmoHandle_t pAtmoCtx = (AtmoHandle_t)inparams->ctx->AtmoInstConfig.hAtmo;
    RkAiqAlgoProcAtmoInt* AtmoParams = (RkAiqAlgoProcAtmoInt*)inparams;
    RkAiqAlgoProcResAtmoInt* AtmoProcResParams = (RkAiqAlgoProcResAtmoInt*)outparams;
    AtmoGetStats(pAtmoCtx, &AtmoParams->ispAtmoStats);
 
    RkAiqAlgoProcResAeInt* ae_proc_res_int =
        (RkAiqAlgoProcResAeInt*)(AtmoParams->rk_com.u.proc.proc_res_comb->ae_proc_res);
 
    if (ae_proc_res_int)
        AtmoGetSensorInfo(pAtmoCtx, ae_proc_res_int->ae_proc_res_rk);
    else {
        AecProcResult_t AeProcResult;
        memset(&AeProcResult, 0x0, sizeof(AecProcResult_t));
        LOGW_ATMO("%s: Ae Proc result is null!!!\n", __FUNCTION__);
        AtmoGetSensorInfo(pAtmoCtx, AeProcResult);
    }
 
    RkAiqAlgoPreResAeInt* ae_pre_res_int =
        (RkAiqAlgoPreResAeInt*)(AtmoParams->rk_com.u.proc.pre_res_comb->ae_pre_res);
    RkAiqAlgoPreResAfInt* af_pre_res_int =
        (RkAiqAlgoPreResAfInt*)(AtmoParams->rk_com.u.proc.pre_res_comb->af_pre_res);
    if (ae_pre_res_int && af_pre_res_int)
        AtmoProcessing(pAtmoCtx,
                       ae_pre_res_int->ae_pre_res_rk,
                       af_pre_res_int->af_pre_result);
    else if (ae_pre_res_int) {
        af_preprocess_result_t AfPreResult;
        memset(&AfPreResult, 0x0, sizeof(af_preprocess_result_t));
        LOGW_ATMO("%s: af Pre result is null!!!\n", __FUNCTION__);
        AtmoProcessing(pAtmoCtx,
                       ae_pre_res_int->ae_pre_res_rk,
                       AfPreResult);
    }
    else {
        AecPreResult_t AecHdrPreResult;
        af_preprocess_result_t AfPreResult;
        memset(&AfPreResult, 0x0, sizeof(af_preprocess_result_t));
        memset(&AecHdrPreResult, 0x0, sizeof(AecPreResult_t));
        LOGW_ATMO("%s: ae/af Pre result is null!!!\n", __FUNCTION__);
        AtmoProcessing(pAtmoCtx,
                       AecHdrPreResult,
                       AfPreResult);
    }
 
    pAtmoCtx->ProcRes.LongFrameMode = pAtmoCtx->SensorInfo.LongFrmMode;
    AtmoProcResParams->AtmoProcRes.LongFrameMode = pAtmoCtx->ProcRes.LongFrameMode;
    AtmoProcResParams->AtmoProcRes.isHdrGlobalTmo = pAtmoCtx->ProcRes.isHdrGlobalTmo;
    AtmoProcResParams->AtmoProcRes.bTmoEn = pAtmoCtx->ProcRes.bTmoEn;
    AtmoProcResParams->AtmoProcRes.isLinearTmo = pAtmoCtx->ProcRes.isLinearTmo;
    memcpy(&AtmoProcResParams->AtmoProcRes.Res, &pAtmoCtx->ProcRes.Res, sizeof(TmoProcRes_t));
    memcpy(&AtmoProcResParams->AtmoProcRes.TmoFlicker, &pAtmoCtx->ProcRes.TmoFlicker, sizeof(TmoFlickerPara_t));
 
    LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
    return(XCAM_RETURN_NO_ERROR);
}
 
static XCamReturn AtmoPostProcess(const RkAiqAlgoCom* inparams, RkAiqAlgoResCom* outparams)
{
    LOG1_ATMO("%s:Enter!\n", __FUNCTION__);
    RESULT ret = ATMO_RET_SUCCESS;
 
    //TODO
 
    LOG1_ATMO("%s:Exit!\n", __FUNCTION__);
    return(XCAM_RETURN_NO_ERROR);
}
 
RkAiqAlgoDescription g_RkIspAlgoDescAtmo = {
    .common = {
        .version = RKISP_ALGO_ATMO_VERSION,
        .vendor  = RKISP_ALGO_ATMO_VENDOR,
        .description = RKISP_ALGO_ATMO_DESCRIPTION,
        .type    = RK_AIQ_ALGO_TYPE_ATMO,
        .id      = 0,
        .create_context  = AtmoCreateCtx,
        .destroy_context = AtmoDestroyCtx,
    },
    .prepare = AtmoPrepare,
    .pre_process = AtmoPreProcess,
    .processing = AtmoProcess,
    .post_process = AtmoPostProcess,
};
 
RKAIQ_END_DECLARE