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