xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/algos/aynr3/rk_aiq_aynr_algo_ynr_v3.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 
2 #include "rk_aiq_aynr_algo_ynr_v3.h"
3 
4 RKAIQ_BEGIN_DECLARE
5 
ynr_select_params_by_ISO_V3(RK_YNR_Params_V3_t * pParams,RK_YNR_Params_V3_Select_t * pSelect,Aynr_ExpInfo_V3_t * pExpInfo)6 Aynr_result_V3_t ynr_select_params_by_ISO_V3(RK_YNR_Params_V3_t *pParams, RK_YNR_Params_V3_Select_t *pSelect, Aynr_ExpInfo_V3_t *pExpInfo)
7 {
8     short multBit;
9     float ratio = 0.0f;
10     int iso = 50;
11     RK_YNR_Params_V3_Select_t *pParamHi = NULL;
12     RK_YNR_Params_V3_Select_t *pParamLo = NULL;
13     RK_YNR_Params_V3_Select_t* pParamTmp = NULL;
14 
15 
16     Aynr_result_V3_t res = AYNRV3_RET_SUCCESS;
17 
18     if(pParams == NULL) {
19         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
20         return AYNRV3_RET_NULL_POINTER;
21     }
22 
23     if(pSelect == NULL) {
24         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
25         return AYNRV3_RET_NULL_POINTER;
26     }
27 
28     if(pExpInfo == NULL) {
29         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
30         return AYNRV3_RET_NULL_POINTER;
31     }
32 
33     iso = pExpInfo->arIso[pExpInfo->hdr_mode];
34 
35     // choose integer type data
36     int cur_iso_idx = 0;
37     int idx = 0;
38     for (idx = 0; idx < RK_YNR_V3_MAX_ISO_NUM; idx++) {
39         if (iso < pParams->iso[idx]) {
40             if (idx == 0) {
41                 cur_iso_idx = 0;
42                 break;
43             } else {
44                 int dist1 = iso - pParams->iso[idx - 1];
45                 int dist2 = pParams->iso[idx] - iso;
46                 cur_iso_idx = (dist1 > dist2) ? (idx) : (idx - 1);
47                 break;
48             }
49         }
50     }
51     if (idx == RK_YNR_V3_MAX_ISO_NUM)
52         cur_iso_idx = RK_YNR_V3_MAX_ISO_NUM - 1;
53 
54 
55     pParamTmp = &pParams->arYnrParamsISO[cur_iso_idx];
56 
57     pSelect->enable = pParams->enable;
58     pSelect->ynr_bft3x3_bypass = pParamTmp->ynr_bft3x3_bypass;
59     pSelect->ynr_lbft5x5_bypass = pParamTmp->ynr_lbft5x5_bypass;
60     pSelect->ynr_lgft3x3_bypass = pParamTmp->ynr_lgft3x3_bypass;
61     pSelect->ynr_flt1x1_bypass = pParamTmp->ynr_flt1x1_bypass;
62     pSelect->ynr_sft5x5_bypass = pParamTmp->ynr_sft5x5_bypass;
63 
64 
65     int iso_div = 50;
66     int lowIso = 50;
67     int highIso = 50;
68     int minIso = 50;
69     int maxIso = 50;
70 
71     for(int i = 0; i < RK_YNR_V3_MAX_ISO_NUM - 1; i++) {
72 #ifndef RK_SIMULATOR_HW
73         lowIso = pParams->iso[i];
74         highIso = pParams->iso[i + 1];
75 #else
76         lowIso = iso_div * (1 << i);
77         highIso = iso_div * (1 << (i + 1));
78 #endif
79         if(iso >= lowIso && iso <= highIso) {
80             ratio = (iso - lowIso ) / (float)(highIso - lowIso);
81             pParamLo = &pParams->arYnrParamsISO[i];
82             pParamHi = &pParams->arYnrParamsISO[i + 1];
83 
84             break;
85         }
86     }
87 
88 #ifndef RK_SIMULATOR_HW
89     minIso = pParams->iso[0];
90     maxIso = pParams->iso[RK_YNR_V3_MAX_ISO_NUM - 1];
91 #else
92     minIso = iso_div * (1 << 0);
93     maxIso = iso_div * (1 << (RK_YNR_V3_MAX_ISO_NUM - 1));
94 #endif
95 
96     if(iso < minIso) {
97         ratio = 0;
98         pParamLo = &pParams->arYnrParamsISO[0];
99         pParamHi = &pParams->arYnrParamsISO[1];
100 #ifndef RK_SIMULATOR_HW
101         lowIso = pParams->iso[0];
102         highIso = pParams->iso[1];
103 #else
104         lowIso = iso_div * (1 << 0);
105         highIso = iso_div * (1 << 1);
106 #endif
107     }
108 
109     if(iso > maxIso) {
110         ratio = 1;
111         pParamLo = &pParams->arYnrParamsISO[RK_YNR_V3_MAX_ISO_NUM - 2];
112         pParamHi = &pParams->arYnrParamsISO[RK_YNR_V3_MAX_ISO_NUM - 1];
113 #ifndef RK_SIMULATOR_HW
114         lowIso = pParams->iso[RK_YNR_V3_MAX_ISO_NUM - 2];
115         highIso = pParams->iso[RK_YNR_V3_MAX_ISO_NUM - 1];
116 #else
117         lowIso = iso_div * (1 << (RK_YNR_V3_MAX_ISO_NUM - 2));
118         highIso = iso_div * (1 << (RK_YNR_V3_MAX_ISO_NUM - 1));
119 #endif
120     }
121 
122     pExpInfo->isoHigh = highIso;
123     pExpInfo->isoLow = lowIso;
124 
125     LOGD_ANR("oyyf %s:%d  iso:%d low:%d hight:%d ratio:%f iso_index:%d \n", __FUNCTION__, __LINE__,
126              iso, lowIso, highIso, ratio, cur_iso_idx);
127     //global gain local gain cfg
128     pSelect->ynr_global_gain_alpha = ratio * (pParamHi->ynr_global_gain_alpha - pParamLo->ynr_global_gain_alpha) + pParamLo->ynr_global_gain_alpha;
129     pSelect->ynr_global_gain       = ratio * (pParamHi->ynr_global_gain - pParamLo->ynr_global_gain) + pParamLo->ynr_global_gain;
130     pSelect->ynr_adjust_thresh = ratio * (pParamHi->ynr_adjust_thresh - pParamLo->ynr_adjust_thresh) + pParamLo->ynr_adjust_thresh;
131     pSelect->ynr_adjust_scale = ratio * (pParamHi->ynr_adjust_scale - pParamLo->ynr_adjust_scale) + pParamLo->ynr_adjust_scale;
132 
133     // get rnr parameters
134     for (int i = 0; i < 17; i++)
135     {
136         pSelect->rnr_strength[i] = ratio * (pParamHi->rnr_strength[i] - pParamLo->rnr_strength[i]) + pParamLo->rnr_strength[i];
137     }
138 
139     // get the parameters for current ISO
140     // ci
141     pSelect->lci = ratio * (pParamHi->lci - pParamLo->lci) + pParamLo->lci;
142     pSelect->hci = ratio * (pParamHi->hci - pParamLo->hci) + pParamLo->hci;
143 
144     // noise curve
145     for (int i = 0; i < YNR_V3_ISO_CURVE_POINT_NUM; i++)
146     {
147         pSelect->sigma[i] = ratio * (pParamHi->sigma[i] - pParamLo->sigma[i]) + pParamLo->sigma[i];
148         pSelect->lumaPoint[i] = (short)(ratio * (pParamHi->lumaPoint[i] - pParamLo->lumaPoint[i]) + pParamLo->lumaPoint[i]);
149     }
150 
151     for (int i = 0; i < 6; i++)
152     {
153         pSelect->lo_lumaPoint[i] = ratio * (pParamHi->lo_lumaPoint[i] - pParamLo->lo_lumaPoint[i]) + pParamLo->lo_lumaPoint[i];
154         pSelect->lo_ratio[i] = ratio * (pParamHi->lo_ratio[i] - pParamLo->lo_ratio[i]) + pParamLo->lo_ratio[i];
155 
156         pSelect->lo_lumaPoint[i] *= 4; // curve point 8 bits -> 10 bits
157     }
158 
159     // lo bf
160     pSelect->low_bf1 = ratio * (pParamHi->low_bf1 - pParamLo->low_bf1) + pParamLo->low_bf1;
161     pSelect->low_bf2 = ratio * (pParamHi->low_bf2 - pParamLo->low_bf2) + pParamLo->low_bf2;
162 
163 
164     pSelect->low_thred_adj = ratio * (pParamHi->low_thred_adj - pParamLo->low_thred_adj) + pParamLo->low_thred_adj;
165     pSelect->low_peak_supress = ratio * (pParamHi->low_peak_supress - pParamLo->low_peak_supress) + pParamLo->low_peak_supress;
166     pSelect->low_edge_adj_thresh = ratio * (pParamHi->low_edge_adj_thresh - pParamLo->low_edge_adj_thresh) + pParamLo->low_edge_adj_thresh;
167     pSelect->low_lbf_weight_thresh = ratio * (pParamHi->low_lbf_weight_thresh - pParamLo->low_lbf_weight_thresh) + pParamLo->low_lbf_weight_thresh;
168     pSelect->low_center_weight = ratio * (pParamHi->low_center_weight - pParamLo->low_center_weight) + pParamLo->low_center_weight;
169     pSelect->low_dist_adj = ratio * (pParamHi->low_dist_adj - pParamLo->low_dist_adj) + pParamLo->low_dist_adj;
170     pSelect->low_weight = ratio * (pParamHi->low_weight - pParamLo->low_weight) + pParamLo->low_weight;
171 
172     pSelect->low_filt1_strength = ratio * (pParamHi->low_filt1_strength - pParamLo->low_filt1_strength) + pParamLo->low_filt1_strength;
173     pSelect->low_filt2_strength = ratio * (pParamHi->low_filt2_strength - pParamLo->low_filt2_strength) + pParamLo->low_filt2_strength;
174 
175     pSelect->low_bi_weight = ratio * (pParamHi->low_bi_weight - pParamLo->low_bi_weight) + pParamLo->low_bi_weight;
176 
177     // High Freq
178     pSelect->base_filter_weight1 = ratio * (pParamHi->base_filter_weight1 - pParamLo->base_filter_weight1) + pParamLo->base_filter_weight1;
179     pSelect->base_filter_weight2 = ratio * (pParamHi->base_filter_weight2 - pParamLo->base_filter_weight2) + pParamLo->base_filter_weight2;
180     pSelect->base_filter_weight3 = ratio * (pParamHi->base_filter_weight3 - pParamLo->base_filter_weight3) + pParamLo->base_filter_weight3;
181 
182 
183     pSelect->high_thred_adj = ratio * (pParamHi->high_thred_adj - pParamLo->high_thred_adj) + pParamLo->high_thred_adj;
184     pSelect->high_weight = ratio * (pParamHi->high_weight - pParamLo->high_weight) + pParamLo->high_weight;
185 
186     for (int i = 0; i < 8; i++)
187     {
188         pSelect->high_direction_weight[i] = ratio * (pParamHi->high_direction_weight[i] - pParamLo->high_direction_weight[i]) + pParamLo->high_direction_weight[i];
189     }
190     pSelect->hi_min_adj = ratio * (pParamHi->hi_min_adj - pParamLo->hi_min_adj) + pParamLo->hi_min_adj;
191     pSelect->hi_edge_thed = ratio * (pParamHi->hi_edge_thed - pParamLo->hi_edge_thed) + pParamLo->hi_edge_thed;
192 
193     for (int i = 0; i < 6; i++)
194     {
195         pSelect->hi_lumaPoint[i] = ratio * (pParamHi->hi_lumaPoint[i] - pParamLo->hi_lumaPoint[i]) + pParamLo->hi_lumaPoint[i];
196         pSelect->hi_ratio[i] = ratio * (pParamHi->hi_ratio[i] - pParamLo->hi_ratio[i]) + pParamLo->hi_ratio[i];
197 
198         pSelect->hi_lumaPoint[i] *= 4; // curve point 8 bits -> 10 bits
199     }
200 
201     return res;
202 }
203 
ynr_fix_transfer_V3(RK_YNR_Params_V3_Select_t * pSelect,RK_YNR_Fix_V3_t * pFix,rk_aiq_ynr_strength_v3_t * pStrength,Aynr_ExpInfo_V3_t * pExpInfo)204 Aynr_result_V3_t ynr_fix_transfer_V3(RK_YNR_Params_V3_Select_t* pSelect, RK_YNR_Fix_V3_t *pFix, rk_aiq_ynr_strength_v3_t *pStrength, Aynr_ExpInfo_V3_t *pExpInfo)
205 {
206     LOGI_ANR("%s:(%d) enter \n", __FUNCTION__, __LINE__);
207 
208     Aynr_result_V3_t res = AYNRV3_RET_SUCCESS;
209     int w0, w1, w2;
210     int tmp;
211 
212     if(pSelect == NULL) {
213         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
214         return AYNRV3_RET_NULL_POINTER;
215     }
216 
217     if(pFix == NULL) {
218         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
219         return AYNRV3_RET_NULL_POINTER;
220     }
221 
222     if(pStrength == NULL) {
223         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
224         return AYNRV3_RET_NULL_POINTER;
225     }
226 
227     if(pExpInfo == NULL) {
228         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
229         return AYNRV3_RET_NULL_POINTER;
230     }
231 
232     float fStrength = 1.0;
233 
234     if(pStrength->strength_enable) {
235         fStrength = pStrength->percent;
236     }
237 
238     if(fStrength <= 0.0) {
239         fStrength = 0.000001;
240     }
241 
242     LOGD_ANR("%s:%d strength_enable:%d fStrength:%f raw:width:%d height:%d\n",
243              __FUNCTION__, __LINE__,
244              pStrength->strength_enable,
245              fStrength,
246              pExpInfo->rawHeight,
247              pExpInfo->rawWidth);
248 
249     // YNR_2700_GLOBAL_CTRL (0x0000)
250     pFix->ynr_rnr_en = 1;
251     pFix->ynr_gate_dis = 0;
252     pFix->ynr_thumb_mix_cur_en = 0;
253     tmp = (int)(pSelect->ynr_global_gain_alpha * (1 << 3));
254     pFix->ynr_global_gain_alpha = CLIP(tmp, 0, 8);
255     tmp = (int)(pSelect->ynr_global_gain * (1 << 4));
256     pFix->ynr_global_gain  = CLIP(tmp, 0, 1023);
257     pFix->ynr_flt1x1_bypass_sel = 0;
258     pFix->ynr_sft5x5_bypass = pSelect->ynr_sft5x5_bypass;
259     pFix->ynr_flt1x1_bypass = pSelect->ynr_flt1x1_bypass;
260     pFix->ynr_lgft3x3_bypass = pSelect->ynr_lgft3x3_bypass;
261     pFix->ynr_lbft5x5_bypass = pSelect->ynr_lbft5x5_bypass;
262     pFix->ynr_bft3x3_bypass = pSelect->ynr_bft3x3_bypass;
263     pFix->ynr_en = pSelect->enable;
264 
265     // YNR_2700_RNR_MAX_R  (0x0004)
266     // pFix->ynr_local_gainscale =
267     int rows = pExpInfo->rawHeight; //raw height
268     int cols = pExpInfo->rawWidth; //raw  width
269     float r_sq_inv = 16.0f / (cols * cols + rows * rows); // divide 2
270     int* number_ptr = (int*)(&r_sq_inv);
271     int EE = ((*number_ptr) >> 23) & (0x0ff);
272     EE = -(EE - 127 + 1);
273     int MM = (*number_ptr) & 0x7fffff;
274     float tmp2 = ((MM / float(1 << 23)) + 1) / 2;
275     MM = int(256 * tmp2 + 0.5);
276     tmp = (MM << 5) + EE;
277     pFix->ynr_rnr_max_r = CLIP(tmp, 0, 0x3fff);
278     //local gain scale
279     //tmp = ( sqrt(double(50) / pExpInfo->arIso[pExpInfo->hdr_mode])) * (1 << 7);  //old
280     tmp = (1.0) * (1 << 7);
281     pFix->ynr_local_gainscale = CLIP(tmp, 0, 0x80);
282 
283     //// YNR_2700_CENTRE_COOR (0x0008)
284     pFix->ynr_rnr_center_coorv = rows / 2;
285     pFix->ynr_rnr_center_coorh = cols / 2;
286 
287     //// YNR_2700_CENTRE_COOR (0x000c)
288     tmp = pSelect->ynr_adjust_scale * (1 << 4);
289     pFix->ynr_localgain_adj = CLIP(tmp, 0, 0xff);
290     //tmp = pSelect->ynr_adjust_thresh * 1023;  //old
291     tmp = pSelect->ynr_adjust_thresh * 16;
292     pFix->ynr_localgain_adj_thresh = CLIP(tmp, 0, 0x3ff);;
293 
294     // YNR_2700_LOWNR_CTRL0 (0x0010)
295     tmp = (int)(1.0f / pSelect->low_bf2 / fStrength * (1 << 9));
296     pFix->ynr_low_bf_inv[1] = CLIP(tmp, 0, 0x3fff);
297     tmp = (int)(1.0f / pSelect->low_bf1 / fStrength * (1 << 9));
298     pFix->ynr_low_bf_inv[0] = CLIP(tmp, 0, 0x3fff);
299 
300 
301     // YNR_2700_LOWNR_CTRL1  (0x0014)
302     tmp = (int)(pSelect->low_peak_supress * (1 << 7));
303     pFix->ynr_low_peak_supress = CLIP(tmp, 0, 0x80);
304     tmp = (int)(pSelect->low_thred_adj * fStrength * (1 << 6));
305     pFix->ynr_low_thred_adj = CLIP(tmp, 0, 0x7ff);
306 
307     // YNR_2700_LOWNR_CTRL2 (0x0018)
308     tmp = (int)(pSelect->low_dist_adj * (1 << 2));
309     pFix->ynr_low_dist_adj = CLIP(tmp, 0, 0x1ff);
310     tmp = (int)(pSelect->low_edge_adj_thresh);
311     pFix->ynr_low_edge_adj_thresh = CLIP(tmp, 0, 0x3ff);
312 
313 
314     // YNR_2700_LOWNR_CTRL3 (0x001c)
315     tmp = (int)(pSelect->low_bi_weight * (1 << 7));
316     pFix->ynr_low_bi_weight = CLIP(tmp, 0, 0x80);
317     tmp = (int)(pSelect->low_weight * (1 << 7));
318     pFix->ynr_low_weight = CLIP(tmp, 0, 0x80);
319     tmp = (int)(pSelect->low_center_weight * (1 << 10));
320     pFix->ynr_low_center_weight = CLIP(tmp, 0, 0x400);
321 
322     // YNR_2700_HIGHNR_CTRL0 (0x0020)
323     tmp = (int)(pSelect->hi_min_adj * (1 << 6));
324     pFix->ynr_hi_min_adj = CLIP(tmp, 0, 0x3f);
325     tmp = (int)(pSelect->high_thred_adj * fStrength * (1 << 6));
326     pFix->ynr_high_thred_adj = CLIP(tmp, 0, 0x7ff);
327 
328     // YNR_2700_HIGHNR_CTRL1  (0x0024)
329     tmp = (1 << 7) - (int)(pSelect->high_weight * (1 << 7));
330     pFix->ynr_high_retain_weight = CLIP(tmp, 0, 0x80);
331     tmp = (int)(pSelect->hi_edge_thed);
332     pFix->ynr_hi_edge_thed = CLIP(tmp, 0, 0xff);
333 
334     // YNR_2700_HIGHNR_BASE_FILTER_WEIGHT  (0x0028)
335     w2 = int(pSelect->base_filter_weight3 * 64 / 2 + 0.5);
336     w1 = int(pSelect->base_filter_weight2 * 64 / 2 + 0.5);
337     w0 = 64 - w1 * 2 - w2 * 2;
338     pFix->ynr_base_filter_weight[0] = CLIP(w0, 0, 0x40);
339     pFix->ynr_base_filter_weight[1] = CLIP(w1, 0, 0x1f);
340     pFix->ynr_base_filter_weight[2] = CLIP(w2, 0, 0xf);
341 
342     // YNR_2700_HIGHNR_BASE_FILTER_WEIGHT  (0x002c)
343     pFix->ynr_frame_full_size = 0x0000;
344     tmp = pSelect->low_lbf_weight_thresh * 1023;
345     pFix->ynr_lbf_weight_thres = CLIP(tmp, 0, 0x3ff);
346 
347     // YNR_2700_GAUSS1_COEFF  (0x0030)
348     float filter1_sigma = pSelect->low_filt1_strength;
349     float filt1_coeff1 = exp(-1 / (2 * filter1_sigma * filter1_sigma));
350     float filt1_coeff0 = filt1_coeff1 * filt1_coeff1;
351     float coeff1_sum = 1 + 4 * filt1_coeff1 + 4 * filt1_coeff0;
352     w0 = int(filt1_coeff0 / coeff1_sum * 256 + 0.5);
353     w1 = int(filt1_coeff1 / coeff1_sum * 256 + 0.5);
354     w2 = 256 - w0 * 4 - w1 * 4;
355     pFix->ynr_low_gauss1_coeff[0] = CLIP(w0, 0, 0x3f);
356     pFix->ynr_low_gauss1_coeff[1] = CLIP(w1, 0, 0x3f);
357     pFix->ynr_low_gauss1_coeff[2] = CLIP(w2, 0, 0x100);
358 
359     // YNR_2700_GAUSS2_COEFF  (0x0034)
360     float filter2_sigma = pSelect->low_filt2_strength;
361     float filt2_coeff1 = exp(-1 / (2 * filter2_sigma * filter2_sigma));
362     float filt2_coeff0 = filt2_coeff1 * filt2_coeff1;
363     float coeff2_sum = 1 + 4 * filt2_coeff1 + 4 * filt2_coeff0;
364     w0 = int(filt2_coeff0 / coeff2_sum * 256 + 0.5);
365     w1 = int(filt2_coeff1 / coeff2_sum * 256 + 0.5);
366     w2 = 256 - w0 * 4 - w1 * 4;
367     pFix->ynr_low_gauss2_coeff[0] = CLIP(w0, 0, 0x3f);
368     pFix->ynr_low_gauss2_coeff[1] = CLIP(w1, 0, 0x3f);
369     pFix->ynr_low_gauss2_coeff[2] = CLIP(w2, 0, 0x100);
370 
371 
372     // YNR_2700_DIRECTION_W_0_3  (0x0038 - 0x003c)
373     for (int i = 0; i < 8; i++) {
374         tmp = (int)(pSelect->high_direction_weight[i] * (1 << 4));
375         pFix->ynr_direction_weight[i] = CLIP(tmp, 0, 0x10);
376     }
377 
378     // YNR_2700_SGM_DX_0_1 (0x0040 - 0x0060)
379     // YNR_2700_LSGM_Y_0_1 (0x0070- 0x0090)
380     // YNR_2700_HSGM_Y_0_1 (0x00a0- 0x00c0)
381     for (int i = 0; i < YNR_V3_ISO_CURVE_POINT_NUM; i++) {
382         tmp = pSelect->lumaPoint[i];
383         pFix->ynr_luma_points_x[i] = CLIP(tmp, 0, 0x400);
384         tmp = (int)(pSelect->sigma[i] * pSelect->lci * (1 << YNR_V3_NOISE_SIGMA_FIX_BIT));
385         pFix->ynr_lsgm_y[i] = CLIP(tmp, 0, 0xfff);
386         tmp = (int)(pSelect->sigma[i] * pSelect->hci * (1 << YNR_V3_NOISE_SIGMA_FIX_BIT));
387         pFix->ynr_hsgm_y[i] = CLIP(tmp, 0, 0xfff);
388     }
389 
390     float lo_lumaPoint[6];
391     float lo_ratio[6];
392     float hi_lumaPoint[6];
393     float hi_ratio[6];
394     for (int i = 0; i < 6; i++) {
395         lo_lumaPoint[i] = pSelect->lo_lumaPoint[i];
396         lo_ratio[i] = pSelect->lo_ratio[i];
397         hi_lumaPoint[i] = pSelect->hi_lumaPoint[i];
398         hi_ratio[i] = pSelect->hi_ratio[i];
399     }
400 
401     //update lo noise curve;
402     for (int i = 0; i < YNR_V3_ISO_CURVE_POINT_NUM; i++) {
403         float rate;
404         int j = 0;
405         for (j = 0; j < 6; j++) {
406             if (pFix->ynr_luma_points_x[i] <= lo_lumaPoint[j])
407                 break;
408         }
409 
410         if (j <= 0)
411             rate = lo_ratio[0];
412         else if (j >= 6)
413             rate = lo_ratio[5];
414         else {
415             rate = ((float)pFix->ynr_luma_points_x[i] - lo_lumaPoint[j - 1]) / (lo_lumaPoint[j] - lo_lumaPoint[j - 1]);
416             rate = lo_ratio[j - 1] + rate * (lo_ratio[j] - lo_ratio[j - 1]);
417         }
418         tmp = (int)(rate * pFix->ynr_lsgm_y[i]);
419         pFix->ynr_lsgm_y[i] = CLIP(tmp, 0, 0xfff);
420     }
421 
422     //update hi noise curve;
423     for (int i = 0; i < YNR_V3_ISO_CURVE_POINT_NUM; i++) {
424         float rate;
425         int j;
426         for (j = 0; j < 6; j++) {
427             if (pFix->ynr_luma_points_x[i] <= hi_lumaPoint[j])
428                 break;
429         }
430 
431         if (j <= 0)
432             rate = hi_ratio[0];
433         else if (j >= 6)
434             rate = hi_ratio[5];
435         else {
436             rate = ((float)pFix->ynr_luma_points_x[i] - hi_lumaPoint[j - 1]) / (hi_lumaPoint[j] - hi_lumaPoint[j - 1]);
437             rate = hi_ratio[j - 1] + rate * (hi_ratio[j] - hi_ratio[j - 1]);
438         }
439         tmp = (int)(rate * pFix->ynr_hsgm_y[i]);
440         pFix->ynr_hsgm_y[i] = CLIP(tmp, 0, 0xfff);
441     }
442 
443     // YNR_2700_RNR_STRENGTH03 (0x00d0- 0x00e0)
444     for (int i = 0; i < 17; i++) {
445         tmp = int(pSelect->rnr_strength[i] * 16);
446         pFix->ynr_rnr_strength[i] = CLIP(tmp, 0, 0xff);
447     }
448 
449     ynr_fix_printf_V3(pFix);
450     return res;
451 }
452 
ynr_fix_printf_V3(RK_YNR_Fix_V3_t * pFix)453 Aynr_result_V3_t ynr_fix_printf_V3(RK_YNR_Fix_V3_t * pFix)
454 {
455     LOGD_ANR("%s:(%d) enter \n", __FUNCTION__, __LINE__);
456 
457     Aynr_result_V3_t res = AYNRV3_RET_SUCCESS;
458 
459     if(pFix == NULL) {
460         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
461         return AYNRV3_RET_NULL_POINTER;
462     }
463 
464     // YNR_2700_GLOBAL_CTRL (0x0000)
465     LOGD_ANR("(0x0000) sw_ynr_thumb_mix_cur_en:0x%x sw_ynr_gate_dis:0x%x sw_ynr_rnr_en:0x%x\n",
466              pFix->ynr_thumb_mix_cur_en,
467              pFix->ynr_gate_dis,
468              pFix->ynr_rnr_en);
469 
470     // YNR_2700_GLOBAL_CTRL (0x0000)
471     LOGD_ANR("(0x0000) ynr_global_gain_alpha:0x%x ynr_global_gain:0x%x \n ynr_flt1x1_bypass_sel:0x%x  ynr_sft5x5_bypass:0x%x \n ynr_flt1x1_bypass:0x%x  ynr_lgft3x3_bypass:0x%x \n ynr_lbft5x5_bypass:0x%x  ynr_bft3x3_bypass:0x%x \n ynr_en:0x%x\n",
472              pFix->ynr_global_gain_alpha,
473              pFix->ynr_global_gain,
474              pFix->ynr_flt1x1_bypass_sel,
475              pFix->ynr_sft5x5_bypass,
476              pFix->ynr_flt1x1_bypass,
477              pFix->ynr_lgft3x3_bypass,
478              pFix->ynr_lbft5x5_bypass,
479              pFix->ynr_bft3x3_bypass,
480              pFix->ynr_en);
481 
482 
483     // YNR_2700_RNR_MAX_R  (0x0004)
484     LOGD_ANR("(0x0004) ynr_rnr_max_r:0x%x  ynr_local_gainscale:0x%x\n",
485              pFix->ynr_rnr_max_r, pFix->ynr_local_gainscale);
486 
487     // YNR_2700_RNR_MAX_R  (0x0008)
488     LOGD_ANR("(0x0008) ynr_rnr_center_coorv:0x%x  ynr_rnr_center_coorh:0x%x\n",
489              pFix->ynr_rnr_center_coorv, pFix->ynr_rnr_center_coorh);
490 
491     // YNR_2700_RNR_MAX_R  (0x00010)
492     LOGD_ANR("(0x0010) ynr_localgain_adj:0x%x  ynr_localgain_adj_thresh:0x%x\n",
493              pFix->ynr_localgain_adj, pFix->ynr_localgain_adj_thresh);
494 
495     // YNR_2700_LOWNR_CTRL0 (0x0010)
496     for(int i = 0; i < 2; i++) {
497         LOGD_ANR("(0x0010) ynr_low_bf_inv[%d]:0x%x \n",
498                  i, pFix->ynr_low_bf_inv[i]);
499     }
500 
501     // YNR_2700_LOWNR_CTRL1  (0x0014)
502     LOGD_ANR("(0x0014) ynr_low_peak_supress:0x%x ynr_low_thred_adj:0x%x \n",
503              pFix->ynr_low_peak_supress,
504              pFix->ynr_low_thred_adj);
505 
506     // YNR_2700_LOWNR_CTRL2 (0x0018)
507     LOGD_ANR("(0x0018) ynr_low_dist_adj:0x%x ynr_low_edge_adj_thresh:0x%x \n",
508              pFix->ynr_low_dist_adj,
509              pFix->ynr_low_edge_adj_thresh);
510 
511     // YNR_2700_LOWNR_CTRL3 (0x001c)
512     LOGD_ANR("(0x001c) ynr_low_bi_weight:0x%x ynr_low_weight:0x%x  ynr_low_center_weight:0x%x \n",
513              pFix->ynr_low_bi_weight,
514              pFix->ynr_low_weight,
515              pFix->ynr_low_center_weight);
516 
517     // YNR_2700_HIGHNR_CTRL0 (0x0020)
518     LOGD_ANR("(0x0020) ynr_hi_min_adj:0x%x ynr_high_thred_adj:0x%x \n",
519              pFix->ynr_hi_min_adj,
520              pFix->ynr_high_thred_adj);
521 
522     // YNR_2700_HIGHNR_CTRL1  (0x0024)
523     LOGD_ANR("(0x0024) ynr_high_retain_weight:0x%x ynr_hi_edge_thed:0x%x \n",
524              pFix->ynr_high_retain_weight,
525              pFix->ynr_hi_edge_thed);
526 
527     // YNR_2700_HIGHNR_BASE_FILTER_WEIGHT  (0x0028)
528     for(int i = 0; i < 3; i++) {
529         LOGD_ANR("(0x0028) ynr_base_filter_weight[%d]:0x%x \n",
530                  i, pFix->ynr_base_filter_weight[i]);
531     }
532 
533     // YNR_2700_HIGHNR_CTRL1  (0x002c)
534     LOGD_ANR("(0x002c) ynr_frame_full_size:0x%x ynr_lbf_weight_thres:0x%x \n",
535              pFix->ynr_frame_full_size,
536              pFix->ynr_lbf_weight_thres);
537 
538     // YNR_2700_GAUSS1_COEFF  (0x0030)
539     for(int i = 0; i < 3; i++) {
540         LOGD_ANR("(0x0030) ynr_low_gauss1_coeff[%d]:0x%x \n",
541                  i, pFix->ynr_low_gauss1_coeff[i]);
542     }
543 
544     // YNR_2700_GAUSS2_COEFF  (0x0034)
545     for(int i = 0; i < 3; i++) {
546         LOGD_ANR("(0x0034) ynr_low_gauss2_coeff[%d]:0x%x \n",
547                  i, pFix->ynr_low_gauss2_coeff[i]);
548     }
549 
550     // YNR_2700_DIRECTION_W_0_3  (0x0038 - 0x003c)
551     for(int i = 0; i < 8; i++) {
552         LOGD_ANR("(0x0038- 0x003c) ynr_direction_weight[%d]:0x%x \n",
553                  i, pFix->ynr_direction_weight[i]);
554     }
555 
556     // YNR_2700_SGM_DX_0_1 (0x0040 - 0x0060)
557     for(int i = 0; i < 17; i++) {
558         LOGD_ANR("(0x0040- 0x0060) ynr_luma_points_x[%d]:0x%x \n",
559                  i, pFix->ynr_luma_points_x[i]);
560     }
561 
562     // YNR_2700_LSGM_Y_0_1 (0x0070- 0x0090)
563     for(int i = 0; i < 17; i++) {
564         LOGD_ANR("(0x0070- 0x0090) ynr_lsgm_y[%d]:0x%x \n",
565                  i, pFix->ynr_lsgm_y[i]);
566     }
567 
568     // YNR_2700_HSGM_Y_0_1 (0x00a0- 0x00c0)
569     for(int i = 0; i < 17; i++) {
570         LOGD_ANR("(0x00a0- 0x00c0) ynr_hsgm_y[%d]:0x%x \n",
571                  i, pFix->ynr_hsgm_y[i]);
572     }
573 
574     // YNR_2700_RNR_STRENGTH03 (0x00d0- 0x00e0)
575     for(int i = 0; i < 17; i++) {
576         LOGD_ANR("(0x00d0- 0x00e0) ynr_rnr_strength[%d]:0x%x \n",
577                  i, pFix->ynr_rnr_strength[i]);
578     }
579 
580 
581     LOGD_ANR("%s:(%d) exit \n", __FUNCTION__, __LINE__);
582 
583     return res;
584 }
585 
586 
587 
ynr_get_setting_by_name_json_V3(CalibDbV2_YnrV3_t * pCalibdbV2,char * name,int * calib_idx,int * tuning_idx)588 Aynr_result_V3_t ynr_get_setting_by_name_json_V3(CalibDbV2_YnrV3_t *pCalibdbV2, char *name, int* calib_idx, int* tuning_idx)
589 {
590     int i = 0;
591     Aynr_result_V3_t res = AYNRV3_RET_SUCCESS;
592 
593     if(pCalibdbV2 == NULL || name == NULL
594             || calib_idx == NULL || tuning_idx == NULL) {
595         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
596         return AYNRV3_RET_NULL_POINTER;
597     }
598 
599     for(i = 0; i < pCalibdbV2->TuningPara.Setting_len; i++) {
600         if(strncmp(name, pCalibdbV2->TuningPara.Setting[i].SNR_Mode, strlen(name)*sizeof(char)) == 0) {
601             break;
602         }
603     }
604 
605     if(i < pCalibdbV2->TuningPara.Setting_len) {
606         *tuning_idx = i;
607     } else {
608         *tuning_idx = 0;
609     }
610 
611     for(i = 0; i < pCalibdbV2->CalibPara.Setting_len; i++) {
612         if(strncmp(name, pCalibdbV2->CalibPara.Setting[i].SNR_Mode, strlen(name)*sizeof(char)) == 0) {
613             break;
614         }
615     }
616 
617     if(i < pCalibdbV2->CalibPara.Setting_len) {
618         *calib_idx = i;
619     } else {
620         *calib_idx = 0;
621     }
622 
623     LOGD_ANR("%s:%d snr_name:%s  snr_idx:%d i:%d \n",
624              __FUNCTION__, __LINE__,
625              name, *calib_idx, i);
626 
627     return res;
628 }
629 
ynr_init_params_json_V3(RK_YNR_Params_V3_t * pYnrParams,CalibDbV2_YnrV3_t * pCalibdbV2,int calib_idx,int tuning_idx)630 Aynr_result_V3_t ynr_init_params_json_V3(RK_YNR_Params_V3_t *pYnrParams, CalibDbV2_YnrV3_t *pCalibdbV2, int calib_idx, int tuning_idx)
631 {
632     Aynr_result_V3_t res = AYNRV3_RET_SUCCESS;
633     int i = 0;
634     int j = 0;
635     short isoCurveSectValue;
636     short isoCurveSectValue1;
637     float ave1, ave2, ave3, ave4;
638     int bit_calib = 12;
639     int bit_proc;
640     int bit_shift;
641 
642     CalibDbV2_YnrV3_T_Set_ISO_t *pISO = NULL;
643     CalibDbV2_YnrV3_C_Set_ISO_t *pCalibISO = NULL;
644 
645     LOGD_ANR("%s(%d): enter\n", __FUNCTION__, __LINE__);
646 
647     if(pYnrParams == NULL || pCalibdbV2 == NULL) {
648         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
649         return AYNRV3_RET_NULL_POINTER;
650     }
651 
652     bit_proc = YNR_V3_SIGMA_BITS; // for V3, YNR_SIGMA_BITS = 10
653     bit_shift = bit_calib - bit_proc;
654 
655     isoCurveSectValue = (1 << (bit_calib - YNR_V3_ISO_CURVE_POINT_BIT));
656     isoCurveSectValue1 = (1 << bit_calib);
657 
658 
659 
660     for(j = 0; j < pCalibdbV2->CalibPara.Setting[tuning_idx].Calib_ISO_len && j < RK_YNR_V3_MAX_ISO_NUM ; j++) {
661         pCalibISO = &pCalibdbV2->CalibPara.Setting[tuning_idx].Calib_ISO[j];
662         pYnrParams->iso[j] = pCalibISO->iso;
663         pYnrParams->sigma_use_point = pCalibdbV2->CalibPara.sigma_use_point;
664         if(pCalibdbV2->CalibPara.sigma_use_point) {
665             LOGD_ANR("oyyf ynr use point\n");
666             for (i = 0; i < YNR_V3_ISO_CURVE_POINT_NUM; i++) {
667                 pYnrParams->arYnrParamsISO[j].sigma[i] = pCalibISO->sigma[i];
668                 pYnrParams->arYnrParamsISO[j].lumaPoint[i] = pCalibISO->lumaPoint[i];
669             }
670         } else {
671             LOGD_ANR("oyyf ynr use formula\n");
672             // get noise sigma sample data at [0, 64, 128, ... , 1024]
673             for (i = 0; i < YNR_V3_ISO_CURVE_POINT_NUM; i++) {
674                 if (i == (YNR_V3_ISO_CURVE_POINT_NUM - 1)) {
675                     ave1 = (float)isoCurveSectValue1;
676                 } else {
677                     ave1 = (float)(i * isoCurveSectValue);
678                 }
679                 pYnrParams->arYnrParamsISO[j].lumaPoint[i] = (short)ave1;
680                 ave2 = ave1 * ave1;
681                 ave3 = ave2 * ave1;
682                 ave4 = ave3 * ave1;
683                 pYnrParams->arYnrParamsISO[j].sigma[i] = pCalibISO->sigma_curve[0] * ave4
684                         + pCalibISO->sigma_curve[1] * ave3
685                         + pCalibISO->sigma_curve[2] * ave2
686                         + pCalibISO->sigma_curve[3] * ave1
687                         + pCalibISO->sigma_curve[4];
688 
689                 if (pYnrParams->arYnrParamsISO[j].sigma[i] < 0) {
690                     pYnrParams->arYnrParamsISO[j].sigma[i] = 0;
691                 }
692 
693                 if (bit_shift > 0) {
694                     pYnrParams->arYnrParamsISO[j].lumaPoint[i] >>= bit_shift;
695                 } else {
696                     pYnrParams->arYnrParamsISO[j].lumaPoint[i] <<= ABS(bit_shift);
697                 }
698             }
699         }
700 
701         pYnrParams->arYnrParamsISO[j].lci = pCalibISO->ynr_lci;
702         pYnrParams->arYnrParamsISO[j].hci = pCalibISO->ynr_hci;
703     }
704 
705     LOGD_ANR(" iso len:%d calib_max:%d\n", pCalibdbV2->TuningPara.Setting[tuning_idx].Tuning_ISO_len, RK_YNR_V3_MAX_ISO_NUM);
706 
707     for(j = 0; j < pCalibdbV2->TuningPara.Setting[tuning_idx].Tuning_ISO_len && j < RK_YNR_V3_MAX_ISO_NUM; j++) {
708         pISO = &pCalibdbV2->TuningPara.Setting[tuning_idx].Tuning_ISO[j];
709 
710         for(i = 0; i < 17; i++) {
711             pYnrParams->arYnrParamsISO[j].rnr_strength[i] = pISO->rnr_strength[i];
712         }
713 
714         //luma param
715         for(int k = 0; k < 6; k++) {
716             pYnrParams->arYnrParamsISO[j].lo_lumaPoint[k] = pISO->lumaPara.lo_lumaPoint[k];
717             pYnrParams->arYnrParamsISO[j].lo_ratio[k] = pISO->lumaPara.lo_ratio[k];
718             pYnrParams->arYnrParamsISO[j].hi_lumaPoint[k] = pISO->lumaPara.hi_lumaPoint[k];
719             pYnrParams->arYnrParamsISO[j].hi_ratio[k] = pISO->lumaPara.hi_ratio[k];
720         }
721 
722         pYnrParams->arYnrParamsISO[j].ynr_bft3x3_bypass = pISO->ynr_bft3x3_bypass;
723         pYnrParams->arYnrParamsISO[j].ynr_lbft5x5_bypass = pISO->ynr_lbft5x5_bypass;
724         pYnrParams->arYnrParamsISO[j].ynr_lgft3x3_bypass = pISO->ynr_lgft3x3_bypass;
725         pYnrParams->arYnrParamsISO[j].ynr_flt1x1_bypass = pISO->ynr_flt1x1_bypass;
726         pYnrParams->arYnrParamsISO[j].ynr_sft5x5_bypass = pISO->ynr_sft5x5_bypass;
727 
728         pYnrParams->arYnrParamsISO[j].low_bf1 = pISO->low_bf1;
729         pYnrParams->arYnrParamsISO[j].low_bf2 = pISO->low_bf2;
730         pYnrParams->arYnrParamsISO[j].low_thred_adj = pISO->low_thred_adj;
731         pYnrParams->arYnrParamsISO[j].low_peak_supress = pISO->low_peak_supress;
732         pYnrParams->arYnrParamsISO[j].low_edge_adj_thresh = pISO->low_edge_adj_thresh;
733         pYnrParams->arYnrParamsISO[j].low_lbf_weight_thresh = pISO->low_lbf_weight_thresh;
734         pYnrParams->arYnrParamsISO[j].low_center_weight = pISO->low_center_weight;
735         pYnrParams->arYnrParamsISO[j].low_dist_adj = pISO->low_dist_adj;
736         pYnrParams->arYnrParamsISO[j].low_weight = pISO->low_weight;
737         pYnrParams->arYnrParamsISO[j].low_filt1_strength = pISO->low_filt1_strength;
738         pYnrParams->arYnrParamsISO[j].low_filt2_strength = pISO->low_filt2_strength;
739         pYnrParams->arYnrParamsISO[j].low_bi_weight = pISO->low_bi_weight;
740 
741         pYnrParams->arYnrParamsISO[j].base_filter_weight1 = pISO->base_filter_weight1;
742         pYnrParams->arYnrParamsISO[j].base_filter_weight2 = pISO->base_filter_weight2;
743         pYnrParams->arYnrParamsISO[j].base_filter_weight3 = pISO->base_filter_weight3;
744         pYnrParams->arYnrParamsISO[j].high_thred_adj = pISO->high_thred_adj;
745         pYnrParams->arYnrParamsISO[j].high_weight = pISO->high_weight;
746         for(i = 0; i < 8; i++) {
747             pYnrParams->arYnrParamsISO[j].high_direction_weight[i] = pISO->high_direction_weight[i];
748         }
749         pYnrParams->arYnrParamsISO[j].hi_min_adj = pISO->hi_min_adj;
750         pYnrParams->arYnrParamsISO[j].hi_edge_thed = pISO->hi_edge_thed;
751 
752         //global gain
753         pYnrParams->arYnrParamsISO[j].ynr_global_gain_alpha = pISO->ynr_global_gain_alpha;
754         pYnrParams->arYnrParamsISO[j].ynr_global_gain = pISO->ynr_global_gain;
755         pYnrParams->arYnrParamsISO[j].ynr_adjust_thresh = pISO->ynr_adjust_thresh;
756         pYnrParams->arYnrParamsISO[j].ynr_adjust_scale = pISO->ynr_adjust_scale;
757     }
758 
759     LOGD_ANR("%s(%d): exit\n", __FUNCTION__, __LINE__);
760 
761     return res;
762 }
763 
ynr_config_setting_param_json_V3(RK_YNR_Params_V3_t * pParams,CalibDbV2_YnrV3_t * pCalibdbV2,char * param_mode,char * snr_name)764 Aynr_result_V3_t ynr_config_setting_param_json_V3(RK_YNR_Params_V3_t *pParams, CalibDbV2_YnrV3_t *pCalibdbV2, char* param_mode, char * snr_name)
765 {
766     Aynr_result_V3_t res = AYNRV3_RET_SUCCESS;
767     int calib_idx = 0;
768     int tuning_idx = 0;
769 
770     if(pParams == NULL || pCalibdbV2 == NULL
771             || param_mode == NULL || snr_name == NULL) {
772         LOGE_ANR("%s(%d): null pointer\n", __FUNCTION__, __LINE__);
773         return AYNRV3_RET_NULL_POINTER;
774     }
775 
776     res = ynr_get_setting_by_name_json_V3(pCalibdbV2, snr_name, &calib_idx, &tuning_idx);
777     if(res != AYNRV3_RET_SUCCESS) {
778         LOGW_ANR("%s(%d): error!!!  can't find setting in iq files, use 0 instead\n", __FUNCTION__, __LINE__);
779     }
780 
781     res = ynr_init_params_json_V3(pParams, pCalibdbV2, calib_idx, tuning_idx);
782     pParams->enable = pCalibdbV2->TuningPara.enable;
783     return res;
784 
785 }
786 
787 RKAIQ_END_DECLARE
788 
789