xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/algos/adebayer/rk_aiq_adebayer_algo_v1.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 
2 #include "rk_aiq_adebayer_algo_v1.h"
3 
4 #ifndef MIN
5 #define MIN(a,b)             ((a) <= (b) ? (a):(b))
6 #endif
7 #ifndef MAX
8 #define MAX(a,b)             ((a) >= (b) ? (a):(b))
9 #endif
10 
11 XCamReturn
AdebayerFullParamsInit(AdebayerContext_t * pAdebayerCtx,CamCalibDbContext_t * pCalibDb,CamCalibDbV2Context_t * pCalibDbV2)12 AdebayerFullParamsInit
13 (
14     AdebayerContext_t *pAdebayerCtx,
15     CamCalibDbContext_t *pCalibDb,
16     CamCalibDbV2Context_t *pCalibDbV2
17 )
18 {
19     if (pCalibDb) {
20         const CalibDb_RKDM_t *calib_dm =
21             (CalibDb_RKDM_t*)(CALIBDB_GET_MODULE_PTR(pCalibDb, dm));
22 
23         pAdebayerCtx->full_param.enable = calib_dm->debayer_en;
24         for (int i = 0; i < 9; i++) {
25             pAdebayerCtx->full_param.iso[i] = calib_dm->ISO[i];
26             pAdebayerCtx->full_param.hf_offset[i] = calib_dm->debayer_hf_offset[i];
27             pAdebayerCtx->full_param.sharp_strength[i] = calib_dm->sharp_strength[i];
28         }
29         for (int i = 0; i < 5; i++) {
30             pAdebayerCtx->full_param.filter1[i] = calib_dm->debayer_filter1[i];
31             pAdebayerCtx->full_param.filter2[i] = calib_dm->debayer_filter2[i];
32         }
33         pAdebayerCtx->full_param.clip_en = calib_dm->debayer_clip_en;
34         pAdebayerCtx->full_param.filter_g_en = calib_dm->debayer_filter_g_en;
35         pAdebayerCtx->full_param.filter_c_en = calib_dm->debayer_filter_c_en;
36         pAdebayerCtx->full_param.thed0 = calib_dm->debayer_thed0;
37         pAdebayerCtx->full_param.thed1 = calib_dm->debayer_thed1;
38         pAdebayerCtx->full_param.dist_scale = calib_dm->debayer_dist_scale;
39         pAdebayerCtx->full_param.gain_offset = calib_dm->debayer_gain_offset;
40         pAdebayerCtx->full_param.offset = calib_dm->debayer_offset;
41         pAdebayerCtx->full_param.shift_num = calib_dm->debayer_shift_num;
42         pAdebayerCtx->full_param.cnr_strength = calib_dm->debayer_cnr_strength;
43     } else if (pCalibDbV2) {
44         CalibDbV2_Debayer_t* debayer =
45             (CalibDbV2_Debayer_t*)(CALIBDBV2_GET_MODULE_PTR(pCalibDbV2, debayer));
46         pAdebayerCtx->full_param.enable = debayer->param.debayer_en;
47         for (int i = 0; i < 9; i++) {
48             pAdebayerCtx->full_param.iso[i] = debayer->param.array.ISO[i];
49             pAdebayerCtx->full_param.hf_offset[i] = debayer->param.array.debayer_hf_offset[i];
50             pAdebayerCtx->full_param.sharp_strength[i] = debayer->param.array.sharp_strength[i];
51         }
52         for (int i = 0; i < 5; i++) {
53             pAdebayerCtx->full_param.filter1[i] = debayer->param.debayer_filter1[i];
54             pAdebayerCtx->full_param.filter2[i] = debayer->param.debayer_filter2[i];
55         }
56         pAdebayerCtx->full_param.clip_en = debayer->param.debayer_clip_en;
57         pAdebayerCtx->full_param.filter_g_en = debayer->param.debayer_filter_g_en;
58         pAdebayerCtx->full_param.filter_c_en = debayer->param.debayer_filter_c_en;
59         pAdebayerCtx->full_param.thed0 = debayer->param.debayer_thed0;
60         pAdebayerCtx->full_param.thed1 = debayer->param.debayer_thed1;
61         pAdebayerCtx->full_param.dist_scale = debayer->param.debayer_dist_scale;
62         pAdebayerCtx->full_param.gain_offset = debayer->param.debayer_gain_offset;
63         pAdebayerCtx->full_param.offset = debayer->param.debayer_offset;
64         pAdebayerCtx->full_param.shift_num = debayer->param.debayer_shift_num;
65         pAdebayerCtx->full_param.cnr_strength = debayer->param.debayer_cnr_strength;
66     } else {
67         LOGE_ADEBAYER("%s(%d): calibDb are all null!\n", __FUNCTION__, __LINE__);
68     }
69 
70     return XCAM_RETURN_NO_ERROR;
71 }
72 
73 XCamReturn
AdebayerTranslateParamsAtManualMode(AdebayerContext_t * pAdebayerCtx)74 AdebayerTranslateParamsAtManualMode(AdebayerContext_t *pAdebayerCtx)
75 {
76     if(!pAdebayerCtx) {
77         LOGE_ADEBAYER("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
78         return XCAM_RETURN_ERROR_PARAM;
79     }
80 
81     for (int i = 0; i < 5; i++)
82     {
83         pAdebayerCtx->config.filter1_coe[i] = pAdebayerCtx->manualAttrib.filter1[i];
84         pAdebayerCtx->config.filter2_coe[i] = pAdebayerCtx->manualAttrib.filter2[i];
85     }
86     pAdebayerCtx->config.gain_offset    = pAdebayerCtx->manualAttrib.gain_offset;
87     pAdebayerCtx->config.offset         = pAdebayerCtx->manualAttrib.offset;
88     pAdebayerCtx->config.clip_en        = pAdebayerCtx->manualAttrib.clip_en;
89     pAdebayerCtx->config.filter_g_en    = pAdebayerCtx->manualAttrib.filter_g_en;
90     pAdebayerCtx->config.filter_c_en    = pAdebayerCtx->manualAttrib.filter_c_en;
91     pAdebayerCtx->config.thed0          = pAdebayerCtx->manualAttrib.thed0;
92     pAdebayerCtx->config.thed1          = pAdebayerCtx->manualAttrib.thed1;
93     pAdebayerCtx->config.dist_scale     = pAdebayerCtx->manualAttrib.dist_scale;
94     pAdebayerCtx->config.shift_num      = pAdebayerCtx->manualAttrib.shift_num;
95     pAdebayerCtx->config.max_ratio      = pAdebayerCtx->manualAttrib.sharp_strength;
96     pAdebayerCtx->config.hf_offset      = pAdebayerCtx->manualAttrib.hf_offset;
97 
98     return XCAM_RETURN_NO_ERROR;
99 }
100 
101 XCamReturn
AdebayerTranslateParamsAtAutoMode(AdebayerContext_t * pAdebayerCtx,int8_t ISO)102 AdebayerTranslateParamsAtAutoMode(AdebayerContext_t *pAdebayerCtx, int8_t ISO)
103 {
104     if(!pAdebayerCtx) {
105         LOGE_ADEBAYER("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
106         return XCAM_RETURN_ERROR_PARAM;
107     }
108 
109     for (int i = 0; i < 5; i++)
110     {
111         pAdebayerCtx->config.filter1_coe[i] = pAdebayerCtx->full_param.filter1[i];
112         pAdebayerCtx->config.filter2_coe[i] = pAdebayerCtx->full_param.filter2[i];
113     }
114     pAdebayerCtx->config.gain_offset = pAdebayerCtx->full_param.gain_offset;
115 
116     int sharp_strength_tmp[9];
117 
118     for (int i = 0; i < 9; i ++)
119     {
120         float iso_index = pAdebayerCtx->full_param.iso[i];
121         int gain = (int)(log((float)iso_index / 50) / log((float)2));
122         sharp_strength_tmp[gain] = pAdebayerCtx->full_param.sharp_strength[i];
123     }
124     pAdebayerCtx->config.offset = pAdebayerCtx->full_param.offset;
125 
126     int hfOffset_tmp[9];
127     for (int i = 0; i < 9; i ++)
128     {
129         float iso_index = pAdebayerCtx->full_param.iso[i];
130         int gain = (int)(log((float)iso_index / 50) / log((float)2));
131         hfOffset_tmp[gain]  = pAdebayerCtx->full_param.hf_offset[i];
132     }
133     pAdebayerCtx->config.clip_en = pAdebayerCtx->full_param.clip_en;
134     pAdebayerCtx->config.filter_g_en = pAdebayerCtx->full_param.filter_g_en;
135     pAdebayerCtx->config.filter_c_en = pAdebayerCtx->full_param.filter_c_en;
136     pAdebayerCtx->config.thed0 = pAdebayerCtx->full_param.thed0;
137     pAdebayerCtx->config.thed1 = pAdebayerCtx->full_param.thed1;
138     pAdebayerCtx->config.dist_scale = pAdebayerCtx->full_param.dist_scale;
139     pAdebayerCtx->config.shift_num = pAdebayerCtx->full_param.shift_num;
140 
141     //select sharp params
142     int iso_low = ISO, iso_high = ISO;
143     int gain_high, gain_low;
144     float ratio = 0.0f;
145     int iso_div             = 50;
146     int max_iso_step        = 9;
147     for (int i = max_iso_step - 1; i >= 0; i--)
148     {
149         if (ISO < iso_div * (2 << i))
150         {
151             iso_low = iso_div * (2 << (i)) / 2;
152             iso_high = iso_div * (2 << i);
153         }
154     }
155     ratio = (float)(ISO - iso_low) / (iso_high - iso_low);
156     if (iso_low == ISO)
157     {
158         iso_high = ISO;
159         ratio = 0;
160     }
161     if (iso_high == ISO )
162     {
163         iso_low = ISO;
164         ratio = 1;
165     }
166     gain_high = (int)(log((float)iso_high / 50) / log((float)2));
167     gain_low = (int)(log((float)iso_low / 50) / log((float)2));
168 
169     gain_low = MIN(MAX(gain_low, 0), 8);
170     gain_high = MIN(MAX(gain_high, 0), 8);
171 
172     pAdebayerCtx->config.max_ratio = ((ratio) * (sharp_strength_tmp[gain_high] - sharp_strength_tmp[gain_low]) + sharp_strength_tmp[gain_low]);
173     pAdebayerCtx->config.hf_offset = ((ratio) * (hfOffset_tmp[gain_high] - hfOffset_tmp[gain_low]) + hfOffset_tmp[gain_low]);
174 
175     return XCAM_RETURN_NO_ERROR;
176 }
177 
178 XCamReturn
AdebayerTranslateParams(AdebayerContext_t * pAdebayerCtx,int8_t ISO)179 AdebayerTranslateParams(AdebayerContext_t *pAdebayerCtx, int8_t ISO)
180 {
181     if(!pAdebayerCtx) {
182         LOGE_ADEBAYER("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
183         return XCAM_RETURN_ERROR_PARAM;
184     }
185 
186     XCamReturn ret = XCAM_RETURN_NO_ERROR;
187 
188     pAdebayerCtx->config.enable = pAdebayerCtx->full_param.enable;
189 
190     unsigned char false_color_remove_strength_table[10][2] = {
191         {0, 19},
192         {1, 18},
193         {2, 17},
194         {3, 16},
195         {4, 15},
196         {5, 14},
197         {6, 13},
198         {7, 12},
199         {8, 11},
200         {9, 10}
201     };
202 
203     int index = 0;
204     if (pAdebayerCtx->mode == RK_AIQ_DEBAYER_MODE_AUTO) {
205         index = pAdebayerCtx->full_param.cnr_strength;
206         ret = AdebayerTranslateParamsAtAutoMode(pAdebayerCtx, ISO);
207     } else if  (pAdebayerCtx->mode == RK_AIQ_DEBAYER_MODE_MANUAL) {
208         index = pAdebayerCtx->manualAttrib.cnr_strength;
209         ret = AdebayerTranslateParamsAtManualMode(pAdebayerCtx);
210     } else {
211         LOGE_ADEBAYER("Invalid mode: %d\n", pAdebayerCtx->mode);
212         return XCAM_RETURN_ERROR_PARAM;
213     }
214 
215     if (ret != XCAM_RETURN_NO_ERROR)
216         LOGE_ADEBAYER("Failed to translate debayer Params in %s mode\n",
217                       pAdebayerCtx->mode == RK_AIQ_DEBAYER_MODE_AUTO ? "auto" : "manual");
218 
219     pAdebayerCtx->config.order_min = false_color_remove_strength_table[index][0];
220     pAdebayerCtx->config.order_max = false_color_remove_strength_table[index][1];
221 
222     LOGI_ADEBAYER("debayer %s mode: config filter1: %d, %d, %d, %d, %d, filter2: %d, %d, %d, %d, %d\n",
223                   pAdebayerCtx->mode == RK_AIQ_DEBAYER_MODE_AUTO ? "auto" : "manual",
224                   pAdebayerCtx->config.filter1_coe[0], pAdebayerCtx->config.filter1_coe[1],
225                   pAdebayerCtx->config.filter1_coe[2], pAdebayerCtx->config.filter1_coe[3],
226                   pAdebayerCtx->config.filter1_coe[4], pAdebayerCtx->config.filter2_coe[0],
227                   pAdebayerCtx->config.filter2_coe[1], pAdebayerCtx->config.filter2_coe[2],
228                   pAdebayerCtx->config.filter2_coe[3], pAdebayerCtx->config.filter2_coe[4]);
229 
230     LOGI_ADEBAYER("debayer config gain_offset: %d, offset: %d, clip_en: %d, filter_g_en: %d, filter_c_en: %d, " \
231                   "thed0: %d, thed1: %d, dist_scale: %d, shift_num: %d, max_ratio: %d, hf_offset: %d, order_max: %d, order_min: %d\n",
232                   pAdebayerCtx->config.gain_offset, pAdebayerCtx->config.offset, pAdebayerCtx->config.clip_en,
233                   pAdebayerCtx->config.filter_g_en, pAdebayerCtx->config.filter_c_en,
234                   pAdebayerCtx->config.thed0, pAdebayerCtx->config.thed1, pAdebayerCtx->config.dist_scale,
235                   pAdebayerCtx->config.shift_num, pAdebayerCtx->config.max_ratio, pAdebayerCtx->config.hf_offset,
236                   pAdebayerCtx->config.order_max, pAdebayerCtx->config.order_min);
237 
238     return ret;
239 }
240 
241 //debayer inint
242 XCamReturn
AdebayerInit(AdebayerContext_t * pAdebayerCtx,CamCalibDbContext_t * pCalibDb,CamCalibDbV2Context_t * pCalibDbV2)243 AdebayerInit
244 (
245     AdebayerContext_t *pAdebayerCtx,
246     CamCalibDbContext_t *pCalibDb,
247     CamCalibDbV2Context_t *pCalibDbV2
248 )
249 {
250     LOGV_ADEBAYER("%s(%d): enter!\n", __FUNCTION__, __LINE__);
251     if(pAdebayerCtx == NULL) {
252         LOGE_ADEBAYER("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
253         return XCAM_RETURN_ERROR_PARAM;
254     }
255     memset(&pAdebayerCtx->config, 0, sizeof(pAdebayerCtx->config));
256     memset(&pAdebayerCtx->manualAttrib, 0, sizeof(pAdebayerCtx->manualAttrib));
257 
258     AdebayerFullParamsInit(pAdebayerCtx, pCalibDb, pCalibDbV2);
259 
260     pAdebayerCtx->state = ADEBAYER_STATE_INITIALIZED;
261     pAdebayerCtx->mode = RK_AIQ_DEBAYER_MODE_AUTO;
262     pAdebayerCtx->is_reconfig = false;
263 
264     LOGV_ADEBAYER("%s(%d): exit!\n", __FUNCTION__, __LINE__);
265     return XCAM_RETURN_NO_ERROR;
266 }
267 
268 //debayer release
269 XCamReturn
AdebayerRelease(AdebayerContext_t * pAdebayerCtx)270 AdebayerRelease
271 (
272     AdebayerContext_t *pAdebayerCtx
273 )
274 {
275     LOGV_ADEBAYER("%s(%d): enter!\n", __FUNCTION__, __LINE__);
276     if(pAdebayerCtx == NULL) {
277         LOGE_ADEBAYER("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
278         return XCAM_RETURN_ERROR_PARAM;
279     }
280     AdebayerStop(pAdebayerCtx);
281 
282     LOGV_ADEBAYER("%s(%d): exit!\n", __FUNCTION__, __LINE__);
283     return XCAM_RETURN_NO_ERROR;
284 }
285 
286 XCamReturn
AdebayerStart(AdebayerContext_t * pAdebayerCtx)287 AdebayerStart
288 (
289     AdebayerContext_t *pAdebayerCtx
290 )
291 {
292     LOGV_ADEBAYER("%s(%d): enter!\n", __FUNCTION__, __LINE__);
293 
294     if(pAdebayerCtx == NULL) {
295         LOGE_ADEBAYER("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
296         return XCAM_RETURN_ERROR_PARAM;
297     }
298 
299     pAdebayerCtx->state = ADEBAYER_STATE_RUNNING;
300     LOGV_ADEBAYER("%s(%d): exit!\n", __FUNCTION__, __LINE__);
301     return XCAM_RETURN_NO_ERROR;
302 }
303 
304 XCamReturn
AdebayerStop(AdebayerContext_t * pAdebayerCtx)305 AdebayerStop
306 (
307     AdebayerContext_t *pAdebayerCtx
308 )
309 {
310     LOGV_ADEBAYER("%s(%d): enter!\n", __FUNCTION__, __LINE__);
311 
312     if(pAdebayerCtx == NULL) {
313         LOGE_ADEBAYER("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
314         return XCAM_RETURN_ERROR_PARAM;
315     }
316     pAdebayerCtx->state = ADEBAYER_STATE_STOPPED;
317     LOGV_ADEBAYER("%s(%d): exit!\n", __FUNCTION__, __LINE__);
318     return XCAM_RETURN_NO_ERROR;
319 }
320 
321 //debayer preprocess
AdebayerPreProcess(AdebayerContext_t * pAdebayerCtx)322 XCamReturn AdebayerPreProcess(AdebayerContext_t *pAdebayerCtx)
323 {
324     LOGV_ADEBAYER("%s(%d): enter!\n", __FUNCTION__, __LINE__);
325     //need todo what?
326 
327     LOGV_ADEBAYER("%s(%d): exit!\n", __FUNCTION__, __LINE__);
328     return XCAM_RETURN_NO_ERROR;
329 }
330 
331 //debayer process
332 XCamReturn
AdebayerProcess(AdebayerContext_t * pAdebayerCtx,int ISO)333 AdebayerProcess
334 (
335     AdebayerContext_t *pAdebayerCtx,
336     int ISO
337 )
338 {
339     LOGV_ADEBAYER("%s(%d): enter! ISO=%d\n", __FUNCTION__, __LINE__, ISO);
340 
341     if(pAdebayerCtx == NULL) {
342         LOGE_ADEBAYER("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
343         return XCAM_RETURN_ERROR_PARAM;
344     }
345 
346     AdebayerTranslateParams(pAdebayerCtx, ISO);
347 
348     LOGV_ADEBAYER("%s(%d): exit!\n", __FUNCTION__, __LINE__);
349     return XCAM_RETURN_NO_ERROR;
350 
351 }
352 
353 //debayer get result
354 XCamReturn
AdebayerGetProcResult(AdebayerContext_t * pAdebayerCtx,AdebayerProcResultV1_t * pAdebayerResult)355 AdebayerGetProcResult
356 (
357     AdebayerContext_t*    pAdebayerCtx,
358     AdebayerProcResultV1_t* pAdebayerResult
359 )
360 {
361     LOGV_ADEBAYER("%s(%d): enter!\n", __FUNCTION__, __LINE__);
362 
363     if(pAdebayerCtx == NULL) {
364         LOGE_ADEBAYER("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
365         return XCAM_RETURN_ERROR_PARAM;
366     }
367 
368     if(pAdebayerResult == NULL) {
369         LOGE_ADEBAYER("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
370         return XCAM_RETURN_ERROR_PARAM;
371     }
372 
373     *pAdebayerResult->config = pAdebayerCtx->config;
374     pAdebayerCtx->config.updatecfg = false;
375     LOGV_ADEBAYER("%s(%d): exit!\n", __FUNCTION__, __LINE__);
376     return XCAM_RETURN_NO_ERROR;
377 }
378 
379 
380 
381