amvecm: serial print unexpected information
authorwenfeng.guo <wenfeng.guo@amlogic.com>
Wed, 11 Apr 2018 02:17:07 +0000 (10:17 +0800)
committerYixun Lan <yixun.lan@amlogic.com>
Wed, 11 Apr 2018 13:06:18 +0000 (05:06 -0800)
PD#163944: amvecm: serial print unexpected information

Change-Id: Ia18adf8426e25a1a960609df2d25c76f8adf5690
Signed-off-by: wenfeng.guo <wenfeng.guo@amlogic.com>
drivers/amlogic/media/enhancement/amvecm/dnlp_cal.c

index a014747..55a8393 100644 (file)
 #include "arch/vpp_regs.h"
 #include <linux/amlogic/media/amvecm/ve.h>
 
-unsigned char ve_dnlp_tgt[65];
+#define pr_dnlp_dbg(fmt, args...)\
+       do {\
+               if (dnlp_printk)\
+                       pr_info("DNLP_CAL: " fmt, ## args);\
+       } while (0)
+
+unsigned char ve_dnlp_tgt[65] = {
+       0, 4, 8, 12, 16, 20,
+       24, 28, 32, 36, 40, 44, 48, 52,
+       56, 60, 64, 68, 72, 76, 80, 84,
+       88, 92, 96, 100, 104, 108, 112, 116,
+       120, 124, 128, 132, 136, 140, 144,
+       148, 152, 156, 160, 164, 168, 172,
+       176, 180, 184, 188, 192, 196, 200,
+       204, 208, 212, 216, 220, 224, 228,
+       232, 236, 240, 244, 248, 252, 255
+};
+
 bool ve_en;
 static bool menu_chg_en;
 module_param(menu_chg_en, bool, 0664);
 MODULE_PARM_DESC(menu_chg_en, "menu_chg_en");
 
+static bool print_en;
+module_param(print_en, bool, 0664);
+MODULE_PARM_DESC(print_en, "print_en");
+
+static bool BLE_en = 1;
+module_param(BLE_en, bool, 0664);
+MODULE_PARM_DESC(BLE_en, "BLE_en");
+
+
+/*scene change threshold,>30 to avoid black menu effect*/
+static int scn_chg_th = 48;
+module_param(scn_chg_th, int, 0664);
+MODULE_PARM_DESC(scn_chg_th, "scn_chg_th");
+static int NORM = 10;
+module_param(NORM, int, 0664);
+MODULE_PARM_DESC(NORM, "NORM");
+
+static int ve_hist_cur_gain = 8;
+module_param(ve_hist_cur_gain, int, 0664);
+MODULE_PARM_DESC(ve_hist_cur_gain, "ve_hist_cur_gain");
+
+static int ve_hist_cur_gain_precise = 8;/*>0 */
+module_param(ve_hist_cur_gain_precise, int, 0664);
+MODULE_PARM_DESC(ve_hist_cur_gain_precise, "ve_hist_cur_gain_precise");
+
+
+static int clahe_method;/*0-old,1-new*/
+module_param(clahe_method, int, 0664);
+MODULE_PARM_DESC(clahe_method, "clahe_method");
+
+static unsigned int nTstCnt;
+
 unsigned int ve_dnlp_rt;
 unsigned int ve_dnlp_luma_sum;
 ulong ve_dnlp_lpf[64], ve_dnlp_reg[16];
@@ -27,10 +76,6 @@ ulong ve_dnlp_reg_def[16] = {
        0xdad7d5d2,     0xe6e3e0dd,     0xf2efece9,     0xfdfaf7f4
 };
 
-int glb_scurve[65];
-int glb_clash_curve[65];
-int glb_pst_gamma[65];
-
 /*for read some params in calculating progress*/
 int ro_luma_avg4;/*0~1024*/
 int ro_var_d8;/*1024*/
@@ -38,6 +83,7 @@ int ro_scurv_gain;/*1024*/
 int ro_blk_wht_ext0;/*1024*/
 int ro_blk_wht_ext1;/*1024*/
 int ro_dnlp_brightness;/*1024*/
+int ro_mtdbld_rate;/*1024*/
 
 /* pre-defined 5 s curve, for outside setting */
 int dnlp_scurv_low[65] = {0, 14, 26, 37, 48,
@@ -161,9 +207,6 @@ module_param(ve_dnlp_hghalpha_v3, int, 0664);
 MODULE_PARM_DESC(ve_dnlp_hghalpha_v3,
                "ve_dnlp_hghalpha_v3");
 
-static int ve_dnlp_pst_gmarat = 64;
-module_param(ve_dnlp_pst_gmarat, int, 0664);
-MODULE_PARM_DESC(ve_dnlp_pst_gmarat, "ve_dnlp_pst_gmarat");
 
 /* light -pattern 1 */
 int ve_dnlp_slow_end = 2;
@@ -176,23 +219,6 @@ static int ve_dnlp_almst_wht = 63;
 module_param(ve_dnlp_almst_wht, int, 0664);
 MODULE_PARM_DESC(ve_dnlp_almst_wht, "define the white scence");
 
-/* black gamma end point setting */
-static int ve_dnlp_pstgma_end;
-module_param(ve_dnlp_pstgma_end, int, 0664);
-MODULE_PARM_DESC(ve_dnlp_pstgma_end, "black gamma end point bin num");
-
-/* black gamma end point rate */
-static int ve_dnlp_pstgma_ratio;
-module_param(ve_dnlp_pstgma_ratio, int, 0664);
-MODULE_PARM_DESC(ve_dnlp_pstgma_ratio, "black gamma end point rate");
-
-static int ve_dnlp_pstgma_brghtrate = 8;
-module_param(ve_dnlp_pstgma_brghtrate, int, 0664);
-MODULE_PARM_DESC(ve_dnlp_pstgma_brghtrate, "ve_dnlp_pstgma_brghtrate");
-
-static int ve_dnlp_pstgma_brghtrat1 = 15;
-module_param(ve_dnlp_pstgma_brghtrat1, int, 0664);
-MODULE_PARM_DESC(ve_dnlp_pstgma_brghtrat1, "ve_dnlp_pstgma_brghtrat1");
 
 static int ve_dnlp_adpalpha_lrate = 32;
 module_param(ve_dnlp_adpalpha_lrate, int, 0664);
@@ -238,66 +264,59 @@ static bool hist_sel = 1; /*1->vpp , 0->vdin*/
 module_param(hist_sel, bool, 0664);
 MODULE_PARM_DESC(hist_sel, "hist_sel");
 
-static int PreTstDat[28];
-static int CrtTstDat[28];
 /* last frame status */
-int prev_dnlp_mvreflsh;
-int prev_dnlp_final_gain;
-int prev_dnlp_clahe_gain_neg;
-int prev_dnlp_clahe_gain_pos;
-int prev_dnlp_gmma_rate;
-int prev_dnlp_lowalpha_v3;
-int prev_dnlp_hghalpha_v3;
-int prev_dnlp_sbgnbnd;
-int prev_dnlp_sendbnd;
-int prev_dnlp_cliprate_v3;
-int prev_dnlp_clashBgn;
-int prev_dnlp_clashEnd;
-int prev_dnlp_mtdbld_rate;
-int prev_dnlp_pst_gmarat;
-int prev_dnlp_blk_cctr;
-int prev_dnlp_brgt_ctrl;
-int prev_dnlp_brgt_range;
-int prev_dnlp_brght_add;
-int prev_dnlp_brght_max;
-int prev_dnlp_lgst_ratio;
-int prev_dnlp_lgst_dst;
-int prev_dnlp_almst_wht;
-int prev_dnlp_pstgma_end;
-int prev_dnlp_pstgma_ratio;
-int prev_dnlp_pstgma_brghtrate;
-int prev_dnlp_pstgma_brghtrat1;
-int prev_dnlp_blkext_rate;
-int prev_dnlp_whtext_rate;
-int prev_dnlp_blkext_ofst;
-int prev_dnlp_whtext_ofst;
-int prev_dnlp_bwext_div4x_min;
-int prev_dnlp_lavg_cum;
-int prev_dnlp_schg_sft;
-bool prev_dnlp_smhist_ck;
-int prev_dnlp_cuvbld_min;
-int prev_dnlp_cuvbld_max;
-int prev_dnlp_dbg_map;
-bool prev_dnlp_dbg_adjavg;
-int prev_dnlp_dbg_i2r;
-int prev_dnlp_slow_end;
-int prev_dnlp_pavg_btsft;
-int prev_dnlp_dbg0331;
-int prev_dnlp_cliprate_min;
-int prev_dnlp_adpcrat_lbnd;
-int prev_dnlp_adpcrat_hbnd;
-int prev_dnlp_adpmtd_lbnd;
-int    prev_dnlp_adpmtd_hbnd;
-int prev_dnlp_satur_rat;
-int prev_dnlp_satur_max;
-int prev_dnlp_set_saturtn;
-int prev_dnlp_lowrange;
-int prev_dnlp_hghrange;
-int prev_dnlp_auto_rng;
-int prev_dnlp_bbd_ratio_low;
-int prev_dnlp_bbd_ratio_hig;
-int prev_dnlp_adpalpha_lrate;
-int prev_dnlp_adpalpha_hrate;
+static int prev_dnlp_mvreflsh;
+static int prev_dnlp_final_gain;
+static int prev_dnlp_clahe_gain_neg;
+static int prev_dnlp_clahe_gain_pos;
+static int prev_dnlp_gmma_rate;
+static int prev_dnlp_lowalpha_v3;
+static int prev_dnlp_hghalpha_v3;
+static int prev_dnlp_sbgnbnd;
+static int prev_dnlp_sendbnd;
+static int prev_dnlp_cliprate_v3;
+static int prev_dnlp_clashBgn;
+static int prev_dnlp_clashEnd;
+static int prev_dnlp_mtdbld_rate;
+
+static int prev_dnlp_blk_cctr;
+static int prev_dnlp_brgt_ctrl;
+static int prev_dnlp_brgt_range;
+static int prev_dnlp_brght_add;
+static int prev_dnlp_brght_max;
+static int prev_dnlp_lgst_ratio;
+static int prev_dnlp_lgst_dst;
+static int prev_dnlp_almst_wht;
+
+static int prev_dnlp_blkext_rate;
+static int prev_dnlp_whtext_rate;
+static int prev_dnlp_blkext_ofst;
+static int prev_dnlp_whtext_ofst;
+static int prev_dnlp_bwext_div4x_min;
+static int prev_dnlp_schg_sft;
+static bool prev_dnlp_smhist_ck;
+static int prev_dnlp_cuvbld_min;
+static int prev_dnlp_cuvbld_max;
+static int prev_dnlp_dbg_map;
+static bool prev_dnlp_dbg_adjavg;
+static int prev_dnlp_dbg_i2r;
+static int prev_dnlp_slow_end;
+static int prev_dnlp_pavg_btsft;
+static int prev_dnlp_cliprate_min;
+static int prev_dnlp_adpcrat_lbnd;
+static int prev_dnlp_adpcrat_hbnd;
+static int prev_dnlp_adpmtd_lbnd;
+static int     prev_dnlp_adpmtd_hbnd;
+static int prev_dnlp_satur_rat;
+static int prev_dnlp_satur_max;
+static int prev_dnlp_set_saturtn;
+static int prev_dnlp_lowrange;
+static int prev_dnlp_hghrange;
+static int prev_dnlp_auto_rng;
+static int prev_dnlp_bbd_ratio_low;
+static int prev_dnlp_bbd_ratio_hig;
+static int prev_dnlp_adpalpha_lrate;
+static int prev_dnlp_adpalpha_hrate;
 
 
 static unsigned int pre_1_gamma[65];
@@ -305,19 +324,67 @@ static unsigned int pre_0_gamma[65];
 /* more 2-bit */
 
 /* why here? */
-bool dnlp_scn_chg; /* scene change */
-int dnlp_bld_lvl; /* blend level */
-int RBASE = 1;
-
-int gma_scurve0[65]; /* gamma0 s-curve */
-int gma_scurve1[65]; /* gamma1 s-curve */
-int gma_scurvet[65]; /* gmma0+gamm1 s-curve */
-int GmScurve[65];/*pre-defined s curve,0~1024*/
-int clash_curve[65]; /* clash curve */
-int clsh_scvbld[65]; /* clash + s-curve blend */
-int blk_gma_crv[65]; /* black gamma curve */
-int blk_gma_bld[65]; /* blending with black gamma */
-int blkwht_ebld[65]; /* black white extension */
+static bool dnlp_scn_chg; /* scene change */
+static int dnlp_bld_lvl; /* blend level */
+static int RBASE = 1;
+
+static int gma_scurve0[65]; /* gamma0 s-curve */
+static int gma_scurve1[65]; /* gamma1 s-curve */
+static int gma_scurvet[65]; /* gmma0+gamm1 s-curve */
+int GmScurve[65] = {0,  16,  32,  48,  64,  80,  96, 112,
+                 128, 144, 160, 176, 192, 208, 224, 240,
+                 256, 272, 288, 304, 320, 336, 352, 368,
+                 384, 400, 416, 432, 448, 464, 480, 496,
+                 512, 528, 544, 560, 576, 592, 608, 624,
+                 640, 656, 672, 688, 704, 720, 736, 752,
+                 768, 784, 800, 816, 832, 848, 864, 880,
+                 896, 912, 928, 944, 960, 976, 992, 1008, 1024
+                 };/*pre-defined s curve,0~1024*/
+int clash_curve[65] = {0,   16,   32,  48,  64,  80,  96, 112,
+                 128, 144, 160, 176, 192, 208, 224, 240,
+                 256, 272, 288, 304, 320, 336, 352, 368,
+                 384, 400, 416, 432, 448, 464, 480, 496,
+                 512, 528, 544, 560, 576, 592, 608, 624,
+                 640, 656, 672, 688, 704, 720, 736, 752,
+                 768, 784, 800, 816, 832, 848, 864, 880,
+                 896, 912, 928, 944, 960, 976, 992, 1008, 1024
+                 }; /* clash curve */
+int clsh_scvbld[65] = {0,   16,   32,  48,  64,  80,  96, 112,
+                 128, 144, 160, 176, 192, 208, 224, 240,
+                 256, 272, 288, 304, 320, 336, 352, 368,
+                 384, 400, 416, 432, 448, 464, 480, 496,
+                 512, 528, 544, 560, 576, 592, 608, 624,
+                 640, 656, 672, 688, 704, 720, 736, 752,
+                 768, 784, 800, 816, 832, 848, 864, 880,
+                 896, 912, 928, 944, 960, 976, 992, 1008, 1024
+                 }; /* clash + s-curve blend */
+int blk_gma_crv[65] = {0,   16,   32,  48,  64,  80,  96, 112,
+                 128, 144, 160, 176, 192, 208, 224, 240,
+                 256, 272, 288, 304, 320, 336, 352, 368,
+                 384, 400, 416, 432, 448, 464, 480, 496,
+                 512, 528, 544, 560, 576, 592, 608, 624,
+                 640, 656, 672, 688, 704, 720, 736, 752,
+                 768, 784, 800, 816, 832, 848, 864, 880,
+                 896, 912, 928, 944, 960, 976, 992, 1008, 1024
+                 }; /* black gamma curve */
+int blk_gma_bld[65] = {0,   16,   32,  48,  64,  80,  96, 112,
+                 128, 144, 160, 176, 192, 208, 224, 240,
+                 256, 272, 288, 304, 320, 336, 352, 368,
+                 384, 400, 416, 432, 448, 464, 480, 496,
+                 512, 528, 544, 560, 576, 592, 608, 624,
+                 640, 656, 672, 688, 704, 720, 736, 752,
+                 768, 784, 800, 816, 832, 848, 864, 880,
+                 896, 912, 928, 944, 960, 976, 992, 1008, 1024
+                 }; /* blending with black gamma */
+int blkwht_ebld[65] = {0,   16,   32,  48,  64,  80,  96, 112,
+                 128, 144, 160, 176, 192, 208, 224, 240,
+                 256, 272, 288, 304, 320, 336, 352, 368,
+                 384, 400, 416, 432, 448, 464, 480, 496,
+                 512, 528, 544, 560, 576, 592, 608, 624,
+                 640, 656, 672, 688, 704, 720, 736, 752,
+                 768, 784, 800, 816, 832, 848, 864, 880,
+                 896, 912, 928, 944, 960, 976, 992, 1008, 1024
+                 }; /* black white extension */
 
 /* only for debug */
 static unsigned int premap0[65];
@@ -384,101 +451,7 @@ struct dnlp_parse_cmd_s dnlp_parse_cmd[] = {
        {"", NULL}
 };
 
-#if 0
-static int ve_dnlp_lowrange = 18;
-static int ve_dnlp_hghrange = 18;
-static int ve_dnlp_auto_rng;
-/* v3 dnlp start */
-/* larger -> slower */
-static int ve_dnlp_mvreflsh = 6;
-/* gain to DCE strength, normalized 8 to "1",*
- * old -- ve_dnlp_adj_level
- */
-static int ve_dnlp_final_gain = 8;
-/* threshold to decide var is small,*
- *need tuning to decide its value.1219.2017
- */
-static int ve_dnlp_var_th  = 16;
-/* delta add to ve_dnlp_clahe_gain_neg/ve_dnlp_clahe_gain_pos,*
- * when var is small(C curve weight more)
- */
-/* need test to decide value according to actual pq test */
-static int ve_dnlp_clahe_gain_delta  = 32;
-/* gain to CLAHE, normalized 32 to "1",old */
-static int ve_dnlp_clahe_gain_neg = 36;
-/* gain to CLAHE, normalized 32 to "1",old */
-static int ve_dnlp_clahe_gain_pos = 24;
-int ve_dnlp_sbgnbnd = 4;
-static int ve_dnlp_sendbnd = 4;
-int ve_dnlp_clashBgn;
-static int ve_dnlp_clashEnd = 15;
-/* origin 53  coef of blending clashe and gma_scurvet */
-static int ve_dnlp_mtdbld_rate;
-/*dnlp method = 3, use this flag or no use*/
-bool ve_dnlp_respond_flag;
-/*dnlp method = 3, check the same histogram*/
-bool ve_dnlp_smhist_ck;
-bool ve_dnlp_dbg_adjavg;
-int ve_dnlp_dbg_i2r = 255;
-/*concentration*/
-static int ve_dnlp_blk_cctr = 8;
-/*the center to be brighter*/
-static int ve_dnlp_brgt_ctrl = 48;
-/*brighter range*/
-static int ve_dnlp_brgt_range = 16;
-/*yout=yin+ve_dnlp_brght_add*/
-/* 32 => 0, brght_add range = [0,64] => [-32,+32] */
-static int ve_dnlp_brght_add = 32;
-/*yout=yin+ve_dnlp_brght_add + ve_dnlp_brght_max*rate*/
-static int ve_dnlp_brght_max;
-/* black extension norm to 32 as 1 */
-static int ve_dnlp_blkext_rate = 32;  /* 32 robin */
-/* white extension norm to 32 as 1 */
-static int ve_dnlp_whtext_rate = 32;/* //32 robin */
-/* black extension maximum bins */
-static int ve_dnlp_blkext_ofst = 2;
-/* white extension maximum bins */
-static int ve_dnlp_whtext_ofst = 2;/* robin 2 */
-static int ve_dnlp_bwext_div4x_min = 16;
-/* adpative mtdrate low band */
-/* default=0 */
-static int ve_dnlp_adpmtd_lbnd = 19;
-/* adpative mtdrate high band */
-/* default=0 */
-static int ve_dnlp_adpmtd_hbnd = 20;
-static int ve_dnlp_satur_rat = 30;/* robin 30 */
-static int ve_dnlp_satur_max = 40;
-static unsigned int ve_dnlp_set_saturtn;
-static int ve_dnlp_bbd_ratio_low = 16;
-static int ve_dnlp_bbd_ratio_hig = 128;
-/* hd /fhd maybe set different value */
-static int ve_dnlp_pavg_btsft = 5;
-/* limit range */
-static bool ve_dnlp_limit_rng = 1;
-static bool ve_dnlp_range_det;
-/* scene change: avg - dif shif */
-static int ve_dnlp_schg_sft = 1;
-/* curveblend minimmum level */
-static int ve_dnlp_cuvbld_min = 2;
-/* curveblend minimmum level */
-static int ve_dnlp_cuvbld_max = 17;
-/* output the mapping curve */
-static int ve_dnlp_dbg_map;
-/* global variable */
-unsigned int iRgnBgn; /* i>=iRgnBgn */
-unsigned int iRgnEnd = 64;/* i<iRgnEnd */
-static int ve_dnlp_cliprate_v3 = 36;
-static int ve_dnlp_cliprate_min = 19;
-static int ve_dnlp_adpcrat_lbnd = 10;
-static int ve_dnlp_adpcrat_hbnd = 20;
-/*5 s_curve apl threshold, new alg, 2017.12.29*/
-static int ve_dnlp_scurv_low_th = 32;
-static int ve_dnlp_scurv_mid1_th = 48;
-static int ve_dnlp_scurv_mid2_th = 112;
-static int ve_dnlp_scurv_hgh1_th = 176;
-static int ve_dnlp_scurv_hgh2_th = 240;
-
-#endif
+
 
 void dnlp_alg_param_init(void)
 {
@@ -544,7 +517,6 @@ void dnlp_alg_param_init(void)
 static void ve_dnlp_add_cm(unsigned int value)
 {
        unsigned int reg_value;
-
        VSYNC_WR_MPEG_REG(VPP_CHROMA_ADDR_PORT, 0x207);
        reg_value = VSYNC_RD_MPEG_REG(VPP_CHROMA_DATA_PORT);
        reg_value = (reg_value & 0xf000ffff) | (value << 16);
@@ -558,7 +530,6 @@ void GetWgtLst(ulong *iHst, ulong tAvg, ulong nLen, ulong alpha)
        ulong iMin = 0;
        ulong iPxl = 0;
        ulong iT = 0;
-
        for (iT = 0; iT < nLen; iT++) {
                iPxl = iHst[iT];
                if (iPxl > tAvg) {
@@ -628,76 +599,6 @@ void GetSubCurve(unsigned int *rGmOt,
 
 /*rGmOt[0:64]*/
 /*rGmIn[0:64]*/
-/* 0~1024 */
-void GetGmBlkCvs(unsigned int *rGmOt, unsigned int *rGmIn,
-               unsigned int Bgn, unsigned int End)
-{
-       static unsigned int pgmma0[65];
-       int nT0 = 0;
-       int pwdth = End - Bgn; /* 64 */
-       int pLst[65];
-       int i = 0;
-       int j = 0;
-       int nTmp0 = 0;
-       /* bool prt_flg = ((dnlp_printk >> 1) & 0x1); */
-
-       if (!ve_dnlp_luma_sum) {
-               for (nT0 = 0; nT0 < 65; nT0++)
-                       pgmma0[nT0] = (nT0 << 4); /* 0 ~1024 */
-       }
-
-       GetSubCurve(pLst, rGmIn, pwdth); /*0~1024*/
-
-       for (nT0 = 0; nT0 < 65; nT0++) {
-               if (nT0 < Bgn)
-                       rGmOt[nT0] = (nT0 << 4);
-               else if (nT0 >= End)
-                       rGmOt[nT0] = (nT0 << 4);
-               else {
-                       if (ve_dnlp_pst_gmarat > 64)
-                               rGmOt[nT0] = (Bgn << 4) +
-                               (1024 - pLst[64 + Bgn - nT0]);
-                       else
-                               rGmOt[nT0] = (Bgn << 4) + pLst[nT0 - Bgn];
-               }
-       }
-
-       if (!dnlp_scn_chg)
-               for (i = 0; i < 65; i++) {
-                       nTmp0 = dnlp_bld_lvl * rGmOt[i] + (RBASE >> 1);
-                       nTmp0 = nTmp0 + (RBASE - dnlp_bld_lvl) * pgmma0[i];
-                       nTmp0 = (nTmp0 >> dnlp_alg_param.dnlp_mvreflsh);
-                       /* 0 ~1024 */
-
-                       rGmOt[i] = nTmp0;
-               }
-
-       for (i = 0; i < 65; i++)
-               pgmma0[i] = rGmOt[i];
-
-       if ((dnlp_printk>>13) & 0x1) {
-               /* iHst */
-               pr_info("\n=====blk_gma: [\n");
-               for (j = 0; j < 4; j++) {
-                       i = j*16;
-                       pr_info("%d,%d,%d,%d,%d,%d,%d,%d\n"
-                               "%d,%d,%d,%d,%d,%d,%d,%d\n",
-                               rGmOt[i], rGmOt[i+1],
-                               rGmOt[i+2], rGmOt[i+3],
-                               rGmOt[i+4], rGmOt[i+5],
-                               rGmOt[i+6], rGmOt[i+7],
-                               rGmOt[i+8], rGmOt[i+9],
-                               rGmOt[i+10], rGmOt[i+11],
-                               rGmOt[i+12], rGmOt[i+13],
-                               rGmOt[i+14], rGmOt[i+15]);
-                       }
-               pr_info("%d ]\n", rGmOt[64]);
-       }
-}
-
-
-/*rGmOt[0:64]*/
-/*rGmIn[0:64]*/
 /* 0 ~1024 */
 void GetGmCurves2(unsigned int *rGmOt, unsigned int *rGmIn,
                unsigned int pval, unsigned int BgnBnd, unsigned int EndBnd)
@@ -707,7 +608,6 @@ void GetGmCurves2(unsigned int *rGmOt, unsigned int *rGmIn,
        int pwdth = 0;
        unsigned int pLst[65];
        int nT1 = 0;
-       bool prt_flg = ((dnlp_printk >> 1) & 0x1);
 
        if (pval >= ve_dnlp_almst_wht) {
                /*almost white scene, do not S_curve*/
@@ -737,22 +637,14 @@ void GetGmCurves2(unsigned int *rGmOt, unsigned int *rGmIn,
                pwdth = pval - BgnBnd;
 
                GetSubCurve(pLst, rGmIn, pwdth); /* 0~1024 */
-               if (prt_flg) {
-                       pr_info("\n#---GetSubCurve(down): pwdth= %d\n", pwdth);
-                       pr_info("[index] => Down_curve(45line)\n");
-                       for (nT1 = 0; nT1 < pwdth; nT1++)
-                               pr_info("[%02d] => %4d(%4d)\n",
-                                       nT1, pLst[nT1], nT1<<4);
-                       pr_info("\n");
-               }
 
                for (nT0 = BgnBnd; nT0 <= pval; nT0++) {
                        /* <luma_avg band curve,arch down */
                        nT1 = pLst[nT0 - BgnBnd] + (BgnBnd<<4);
                        rGmOt[nT0] = nT1;
 
-                       if (prt_flg)
-                               pr_info("\n#***down_cur::(index)%02d: (pLst) %4d => (down_cur) %4d(%4d)\n",
+                       if ((dnlp_printk >> 17) & 0x1)
+                               pr_dnlp_dbg("\n#***down_cur::(index)%02d: (pLst) %4d => (down_cur) %4d(%4d)\n",
                                nT0, pLst[nT0 - BgnBnd], nT1, nT0<<4);
                }
        }
@@ -760,37 +652,23 @@ void GetGmCurves2(unsigned int *rGmOt, unsigned int *rGmIn,
        if (pval + EndBnd < 64) {
                pwdth = 64 - pval - EndBnd;
                GetSubCurve(pLst, rGmIn, pwdth);
-               if (prt_flg) {
-                       pr_info("\n#---GetSubCurve(up): pwdth= %d\n", pwdth);
-                       pr_info("[index] => up_curve(45line)\n");
-                       for (nT1 = 0; nT1 < pwdth; nT1++)
-                               pr_info("[%02d] => %4d(%4d)\n",
-                               nT1, pLst[nT1], nT1<<4);
-                       pr_info("\n");
-               }
 
                for (nT0 = pval; nT0 <= 64 - EndBnd; nT0++) {
                        nT1 = (1024 - (EndBnd<<4)
                                - pLst[pwdth - (nT0 - pval)]);
                        rGmOt[nT0] = nT1;  /* 0~1024 */
                        /* >luma_avg band curve,arch up */
-
-                       if (prt_flg)
-                               pr_info("\n#***up_cur::(index)%02d:\n"
-                                       "(pLst) %4d => (up_cur) %4d(%4d)\n",
-                               nT0, pLst[pwdth - (nT0 - pval)], nT1, nT0<<4);
-
                }
        }
 
-       if (prt_flg) {
-               pr_info("\n#---GmCvs2: BgnBnd(%d) pval(%d,luma_avg) EndBnd(%d)\n",
+       if ((dnlp_printk >> 17) & 0x1) {
+               pr_dnlp_dbg("\n#---GmCvs2: BgnBnd(%d) pval(%d,luma_avg) EndBnd(%d)\n",
                        BgnBnd, pval, EndBnd);
-               pr_info("[index] => curve(45line)\n");
+               pr_dnlp_dbg("[index] => curve(45line)\n");
                for (nT1 = 0; nT1 < 65; nT1++)
-                       pr_info("[%02d] => %4d(%4d)\n",
+                       pr_dnlp_dbg("[%02d] => %4d(%4d)\n",
                                nT1, rGmOt[nT1], nT1<<4);
-               pr_info("\n");
+               pr_dnlp_dbg("\n");
        }
 }
 
@@ -809,15 +687,14 @@ void GetGmCurves(unsigned int *rGmOt, unsigned int *rGmIn,
        unsigned int pLst2[65];
        int i = 0;
        int j = 0;
-       bool prt_flg = ((dnlp_printk >> 1) & 0x1);
 
        if (pval2 > ve_dnlp_almst_wht)
                pval2 = ve_dnlp_almst_wht;
 
        GetGmCurves2(pLst1, rGmIn, pval1, BgnBnd, EndBnd);
        GetGmCurves2(pLst2, rGmIn, pval2, BgnBnd, EndBnd);
-       if (prt_flg)
-               pr_info("GetGmCurves 0/1: pval1=%d, pval2=%d\n",
+       if ((dnlp_printk >> 17) & 0x1)
+               pr_dnlp_dbg("GetGmCurves 0/1: pval1=%d, pval2=%d\n",
                        pval1, pval2);
 
        for (i = 0; i < 65; i++) {
@@ -833,12 +710,12 @@ void GetGmCurves(unsigned int *rGmOt, unsigned int *rGmIn,
                rGmOt[i] = pval2;
        }
 
-       if ((dnlp_printk >> 13) & 0x1) {
+       if ((dnlp_printk >> 17) & 0x1) {
                /* iHst */
-               pr_info("\n=====gma_s: [\n");
+               pr_dnlp_dbg("\n=====gma_s: [\n");
                for (j = 0; j < 4; j++) {
                        i = j*16;
-                       pr_info("%d,%d,%d,%d,%d,%d,%d\n"
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d\n"
                                "%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
                                rGmOt[i], rGmOt[i+1],
                                rGmOt[i+2], rGmOt[i+3],
@@ -848,8 +725,8 @@ void GetGmCurves(unsigned int *rGmOt, unsigned int *rGmIn,
                                rGmOt[i+10], rGmOt[i+11],
                                rGmOt[i+12], rGmOt[i+13],
                                rGmOt[i+14], rGmOt[i+15]);
-                       }
-               pr_info("%d ]\n", rGmOt[64]);
+               }
+               pr_dnlp_dbg("%d ]\n", rGmOt[64]);
        }
 
 }
@@ -864,7 +741,16 @@ void GetGmScurve_apl_var(unsigned int *rGmOt, unsigned int luma_avg4,
                int var, unsigned int sBgnBnd, unsigned int sEndBnd)
 {
        int nTmp, nFrac, i, j;
-       static unsigned int pre_scurve[65];
+       static unsigned int pre_scurve[65] = {
+                0, 16, 32, 48, 64, 80, 96, 112,
+                 128, 144, 160, 176, 192, 208, 224, 240,
+                 256, 272, 288, 304, 320, 336, 352, 368,
+                 384, 400, 416, 432, 448, 464, 480, 496,
+                 512, 528, 544, 560, 576, 592, 608, 624,
+                 640, 656, 672, 688, 704, 720, 736, 752,
+                 768, 784, 800, 816, 832, 848, 864, 880,
+                 896, 912, 928, 944, 960, 976, 992, 1008, 1024
+                 };
        int scurv_gain, left, right, var_d8, frac, norm, idx;
 
                /* typical histo <==> var       <==> var/8 */
@@ -914,12 +800,12 @@ void GetGmScurve_apl_var(unsigned int *rGmOt, unsigned int luma_avg4,
        /*for read on tools*/
        ro_scurv_gain = scurv_gain;
 
-       if ((dnlp_printk >> 14) & 0x1)
-               pr_info("@@@ GetGmScurve()--in: hist_var = %d\n"
+       if ((dnlp_printk >> 3) & 0x1)
+               pr_dnlp_dbg("@@@ GetGmScurve()--in: hist_var = %d\n"
                        "luma_avg4(255) = %d, sBgn_Bnd,end = [%d %d]\n\n",
                        var, luma_avg4, sBgnBnd, sEndBnd);
-       if ((dnlp_printk >> 14) & 0x1)
-               pr_info("@@@ GetGmScurve()--scurv_gain: nFrac = %d,frac = %d\n"
+       if ((dnlp_printk >> 3) & 0x1)
+               pr_dnlp_dbg("@@@ GetGmScurve()--scurv_gain: nFrac = %d,frac = %d\n"
                        "idx= %d, left,right= [%d %d] => scurv_gain = %d\n\n",
                        nFrac, frac, idx, left, right, scurv_gain);
 
@@ -933,6 +819,13 @@ void GetGmScurve_apl_var(unsigned int *rGmOt, unsigned int luma_avg4,
        else if (luma_avg4 > 255)
                luma_avg4 = 255;/* luma_avg = 0~255 */
 
+       if ((dnlp_printk >> 3) & 0x1)
+               pr_dnlp_dbg("@@@ luma_avg4:%d, 5_curv_th:[ %d,%d,%d,%d,%d]",
+                       luma_avg4, dnlp_alg_param.dnlp_scurv_low_th,
+                       dnlp_alg_param.dnlp_scurv_mid1_th,
+                       dnlp_alg_param.dnlp_scurv_mid2_th,
+                       dnlp_alg_param.dnlp_scurv_hgh1_th,
+                       dnlp_alg_param.dnlp_scurv_hgh2_th);
 
        /* choose s curve base on luma_avg */
        for (i = 0; i < 65; i++) {
@@ -984,6 +877,10 @@ void GetGmScurve_apl_var(unsigned int *rGmOt, unsigned int luma_avg4,
                        nTmp = (left*(norm - frac) +
                                right*frac + (norm>>1))/norm;
 
+               if ((dnlp_printk >> 3) & 0x1)
+                       pr_dnlp_dbg("@@@ apl_var():[%d]:lavg4=%d,left=%d, right=%d, frac=%d,norm=%d => nTmp=%d\n",
+                               i, luma_avg4, left, right, frac, norm, nTmp);
+
                        }
                }
 
@@ -992,6 +889,10 @@ void GetGmScurve_apl_var(unsigned int *rGmOt, unsigned int luma_avg4,
                nTmp = (i << 4) +
                        ((nTmp - (i << 4)) * scurv_gain + 128)/256;
 
+       if ((dnlp_printk >> 3) & 0x1)
+               pr_dnlp_dbg("@@@ apl_var():[%d]:i4=%d,sgain=%d => out=%d\n",
+                       i, i << 4, scurv_gain, nTmp);
+
                /* clip */
                if (nTmp < 0)
                        nTmp = 0;
@@ -1013,8 +914,8 @@ void GetGmScurve_apl_var(unsigned int *rGmOt, unsigned int luma_avg4,
                        else if (nTmp > 1023)
                                nTmp = 1023;
 
-                       if ((dnlp_printk >> 14) & 0x1)
-                               pr_info("\n@@@ apl_var(iir): %d, rGmOt[i]=%d, pre_scurve=%d => crt=%d\n",
+                       if ((dnlp_printk >> 3) & 0x1)
+                               pr_dnlp_dbg(" @@@ apl_var(iir): %d, rGmOt[i]=%d, pre_scurve=%d => crt=%d\n",
                                        i, rGmOt[i], pre_scurve[i], nTmp);
 
                        rGmOt[i] = nTmp;
@@ -1023,12 +924,12 @@ void GetGmScurve_apl_var(unsigned int *rGmOt, unsigned int luma_avg4,
        for (i = 0; i < 65; i++)
                pre_scurve[i] = rGmOt[i];
 
-       if ((dnlp_printk >> 14) & 0x1) {
+       if ((dnlp_printk >> 13) & 0x1) {
                /* iHst */
-               pr_info("@@@ GetGmScurve()--out,chosen curve: [\n");
+               pr_dnlp_dbg("@@@ GetGmScurve()--out,chosen curve: [\n");
                for (j = 0; j < 4; j++) {
                        i = j*16;
-                       pr_info("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
                                                rGmOt[i], rGmOt[i+1],
                                                rGmOt[i+2], rGmOt[i+3],
                                                rGmOt[i+4], rGmOt[i+5],
@@ -1038,7 +939,7 @@ void GetGmScurve_apl_var(unsigned int *rGmOt, unsigned int luma_avg4,
                                                rGmOt[i+12], rGmOt[i+13],
                                                rGmOt[i+14], rGmOt[i+15]);
                }
-               pr_info("%d ]\n", rGmOt[64]);
+               pr_dnlp_dbg("%d ]\n", rGmOt[64]);
        }
 }
 
@@ -1083,7 +984,6 @@ static int dnlp_adp_mtdrate(int mtdrate, int luma_avg)
 {
        int np = 0;
        int nt = mtdrate;
-       bool prt_flg = ((dnlp_printk >> 5) & 0x1);
 
        if (dnlp_alg_param.dnlp_mtdrate_adp_en) {
                if (dnlp_alg_param.dnlp_adpmtd_lbnd > 0 &&
@@ -1116,9 +1016,8 @@ static int dnlp_adp_mtdrate(int mtdrate, int luma_avg)
                        if (np > nt)
                                nt = np;
                }
-               if (prt_flg)
-                       pr_info("Step C-2.y: adp_mtdrate: imtdrate=%d, luma_avg=%d,\n"
-                       " ve_adpmtd_bnd=[%d %d], nt(mtdrate)=%d (np=%d)\n ",
+               if ((dnlp_printk >> 6) & 0x1)
+                       pr_dnlp_dbg("## adp_mtdrate(): imtdrate=%d, luma_avg=%d,ve_adpmtd_bnd=[%d %d], nt(mtdrate)=%d (np=%d)\n ",
                        mtdrate, luma_avg, dnlp_alg_param.dnlp_adpmtd_lbnd,
                        dnlp_alg_param.dnlp_adpmtd_hbnd, nt, np);
        } else
@@ -1164,7 +1063,7 @@ void cal_bwext_param(int *blk_wht_ext, unsigned int iHstBgn,
                blk_wht_ext[0] = (blk_wht_ext[0] > 16) ? 16 :
                        (blk_wht_ext[0] < 0) ? 0 : blk_wht_ext[0];
        } else
-               blk_wht_ext[0] = 63;
+               blk_wht_ext[0] = 0;
        /* white extension */
        if (dnlp_alg_param.dnlp_whtext_rate > 0) {
                blk_wht_ext[1] = (iHstEnd > lmh_lavg[2]) ? iHstEnd:lmh_lavg[2];
@@ -1173,34 +1072,40 @@ void cal_bwext_param(int *blk_wht_ext, unsigned int iHstBgn,
        } else
                blk_wht_ext[1] = 63;
 
+       if ((dnlp_printk >> 5) & 0x1)
+               pr_dnlp_dbg("&&&&-1: iHstBgn-End = [%d,%d],blk_wht_ext[2] = [%d,%d],lmh_avg[L H]=[%d, %d]\n",
+               iHstBgn, iHstEnd, blk_wht_ext[0], blk_wht_ext[1],
+               lmh_lavg[0], lmh_lavg[2]);
+
+       blk_wht_ext[0] = blk_wht_ext[0] << 4;/*u10*/
+       blk_wht_ext[1] = blk_wht_ext[1] << 4;/*u10*/
+
        /* do IIR if no scene change to keep it stable */
        if (!dnlp_scn_chg) {
-               nT0 = dnlp_bld_lvl * (blk_wht_ext[0]<<4) + (RBASE >> 1);
+               nT0 = dnlp_bld_lvl * blk_wht_ext[0] + (RBASE >> 1);
                nT0 = nT0 + (RBASE - dnlp_bld_lvl) * pblk_wht_extx16[0];
-               pblk_wht_extx16[0] = (nT0 >> dnlp_alg_param.dnlp_mvreflsh);
-
-               nT1 = dnlp_bld_lvl * (blk_wht_ext[1]<<4) + (RBASE >> 1);
+               nT0 = (nT0 >> dnlp_alg_param.dnlp_mvreflsh);
+               nT1 = dnlp_bld_lvl * blk_wht_ext[1] + (RBASE >> 1);
                nT1 = nT1 + (RBASE - dnlp_bld_lvl) * pblk_wht_extx16[1];
-               pblk_wht_extx16[1] = (nT1 >> dnlp_alg_param.dnlp_mvreflsh);
-       }
+               nT1 = (nT1 >> dnlp_alg_param.dnlp_mvreflsh);
+
+       if ((dnlp_printk >> 5) & 0x1)
+               pr_dnlp_dbg("&&&&(iir):bwext[2] = [%d,%d], pbw_extx16[2]=  [%d,  %d] = > cur bw_ext[2]=[%d,%d]\n ",
+               blk_wht_ext[0], blk_wht_ext[1],
+               pblk_wht_extx16[0], pblk_wht_extx16[1],
+               nT0, nT1);
+       blk_wht_ext[0] = nT0;/* u10 */
+       blk_wht_ext[1] = nT1;/* u10 */
 
-       /* output, plus fixed offset */
-       blk_wht_ext[0] = (pblk_wht_extx16[0]);/* u10 */
-       blk_wht_ext[1] = (pblk_wht_extx16[1]);/* u10 */
+       }
+               /* output, plus fixed offset */
+       pblk_wht_extx16[0] = blk_wht_ext[0];/* u10 */
+       pblk_wht_extx16[1] = blk_wht_ext[1];/* u10 */
 
-       /* for debug */
-       CrtTstDat[4] = blk_wht_ext[0];
-       CrtTstDat[5] = blk_wht_ext[1];
+       if ((dnlp_printk >> 5) & 0x1)
+               pr_dnlp_dbg("&&&&-2(iir):blk_wht_ext[2] = [%d,%d](u10)\n",
+               blk_wht_ext[0], blk_wht_ext[1]);
 
-       if ((dnlp_printk >> 3) & 0x1)
-               pr_info("#step B-4.x.1(in cal_bwext_param()): iHstBgn=%d\n"
-                       "iHstEnd=%d, lmh_lavg=[%d %d %d],  bw_ext=[%d %d]\n"
-                       "from [%d %d]\n",
-                       iHstBgn, iHstEnd, lmh_lavg[0],
-                       lmh_lavg[1], lmh_lavg[2],
-                       blk_wht_ext[0], blk_wht_ext[1],
-                       pblk_wht_extx16[0],
-                       pblk_wht_extx16[1]);
 }
 
 
@@ -1226,6 +1131,762 @@ static unsigned int dnlp_adp_cliprate(unsigned int clip_rate,
        return nt;
 }
 
+/*get the hist based curves, 32 nodes*/
+void get_hist_cross_curve(unsigned int *oMap, unsigned int *iHst, int hist_sum,
+               unsigned int hstBgn, unsigned int hstEnd)
+{
+       int i, j, t, tt, k, kk, xidx, xidx_prev, xnum, up_idx, dn_idx;
+       int nTmp, nDif, sum, c, m, p, m3, m4, p2, p3, ofset;
+
+       /*variables*/
+       /*normalized 1:1 curve*/
+       int curv_121n_node[33] = {   16, 48, 80, 112, 144, 176, 208, 240,
+                       272, 304, 336, 368, 400, 432, 464, 496,
+                       528, 560, 592, 624, 656, 688, 720, 752,
+                       784, 816, 848, 880, 912, 944, 976, 1008, 1032};
+       /*32 node version of the oMap curve*/
+       int curv_hist_node[33];
+       /*average bin value.*/
+       int hist_avg = hist_sum >> 6;
+       int adp_thrd[33] = {13, 13, 13, 13, 13, 13, 13, 14,
+               14, 14, 14, 14, 15, 15, 15, 15,
+               15, 14, 14, 14, 14, 14, 13, 12,
+               11, 10,  9,  8,  7,  6,  5,  4,  4};
+
+       int trend[32];
+       int xross_xidx[9], xross_slop[9], xross_ofst[9], xross_num;
+       int xidx_rightmost_valid_hist = 0;
+       int xidx_final_peak, max_shift;
+       int maxbin_2ndabove, lpfbin_1stpeak, bb_protect;
+       int bin_curr, bin_righ;
+       int bin_lpf8, bin_max, bin_max_idx, bin_scl, bin_clip;
+       int lmt_slope, slope_1st, slope_c, slope_p;
+       int slope_step_norm, slope_step_c, slope_step_p;
+       int delta_slop_step_c, delta_slop_step_p;
+       int boost_ofst;
+       int num_2, num_1, num_x, sum_step_2;
+       int sum_step_1, sum_step_x, sum_step_x_min;
+       int rat_1, rat_2, dlt_1, dlt_2, sub_x;
+       int accum, num_xx, up_dn_dist, st_idx;
+       int hist_lpf[33];
+
+
+       /*programmable registers*/
+       /*maximum slop of the first node, normalize to 256 as 1.0*/
+       int reg_max_slop_1st = 614;
+       /*maximum slop of the middle node, normalize to 256 as 1.0*/
+       int reg_max_slop_mid = 400;
+       /*maximum slop of the last node, normalize to 256 as 1.0*/
+       int reg_max_slop_fin = 614;
+       /*minimum slop of the first node, normalize to 256 as 1.0*/
+       int reg_min_slop_1st =  77;
+       /*minimum slop of the middle node, normalize to 256 as 1.0*/
+       int reg_min_slop_mid = 144;
+       /*minimum slop of the final node, normalize to 256 as 1.0*/
+       int reg_min_slop_fin =  77;
+
+       /*(8+6)*/
+       int reg_no_trend_margin = (12 * hist_sum) >> 14;
+       /*oMap offset on the first 12 nodes*/
+       int reg_blk_boost_12[13] = {0, 8, 24, 40, 48,
+               28, 12, 8,  5, 3, 2, 0, 0};
+       /*normalized to 32 as 1.0 */
+       int reg_adp_ofset_20[20] = {0, 0,  1,  2,  3,  3,  4, 5,  6, 7,
+                                       8, 8,  7,  6,  5,  4,  3, 2,  1, 0};
+       /*normalized to 256 as 1.0*/
+       int reg_mono_protect[6] = {307, 332, 358, 371, 460, 560};
+       /*mono color raster protection nodes range, */
+       /*      only nodes within this range can be protected0~31 nodes*/
+       int reg_mono_binrang[2] = {7, 26};
+       /* min_num of transition nodes during local peak;*/
+       int reg_trend_wht_expand_mode = 2;
+       /*min_num of transition nodes during local peak*/
+       int reg_trend_blk_expand_mode = 2;
+       /*from 23 nodes to 30 nodes, the last peak maximum right shift step*/
+       int reg_trend_wht_expand_lut8[9] = {0, 2, 4, 6, 5, 4, 3, 2, 0};
+
+       /*initial */
+       m = 8;
+       for (k = 0; k < 33; k++)
+               curv_hist_node[k] = curv_121n_node[k];
+       for (k = 0; k < 9; k++) {
+               xross_xidx[k] = -1;
+               xross_slop[k] = 256;
+               xross_ofst[k] = 0;
+       }
+
+       /*input params*/
+       if ((dnlp_printk >> 14) & 0x1) {
+               pr_dnlp_dbg("\n=====hist_raw: [\n");
+               for (j = 0; j < 4; j++) {
+                       i = j*16;
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                                       iHst[i], iHst[i+1],
+                                       iHst[i+2], iHst[i+3],
+                                       iHst[i+4], iHst[i+5],
+                                       iHst[i+6], iHst[i+7],
+                                       iHst[i+8], iHst[i+9],
+                                       iHst[i+10], iHst[i+11],
+                                       iHst[i+12], iHst[i+13],
+                                       iHst[i+14], iHst[i+15]);
+               }
+       pr_dnlp_dbg("]\n");
+       pr_dnlp_dbg("hist_sum(iir) = %d\n", hist_sum);
+       }
+       /*step 4.0 gen:  adp_thrd[33], hist_lpf[33], trend[32]*/
+       /* [1 1 1 1 0]/4 lpf filter*/
+       hist_lpf[0] = ((iHst[0] << 1) + iHst[1] + iHst[2]) >> 2;
+       for (xidx = 0; xidx < 32; xidx++) {
+               /*generate the adp_thrd*/
+               /*adaptive threshold to decide if */
+               /* that node is valid hist or not.*/
+               adp_thrd[xidx] = (adp_thrd[xidx] * hist_avg) >> 6;
+
+               /*generate the hist_lpf_righ[]*/
+               sum = 0;
+               for (i = -2; i < 2; i++) {
+                       j = (xidx<<1) + 3 + i;   /*righ 2*(xidx+1)+1*/
+                       kk = (j > 63) ? 63 : j; /*0~63*/
+                       sum += (int) iHst[kk];
+               }
+               hist_lpf[xidx + 1] = (sum >> 2);
+
+               /*get the bin_lpf*/
+               bin_curr = hist_lpf[xidx];
+               bin_righ = hist_lpf[xidx + 1];
+
+               /*get the trend[32]*/
+               if (bin_curr > adp_thrd[xidx]) {
+                       if (bin_curr < (bin_righ - reg_no_trend_margin))
+                               trend[xidx] = 2;  /*upward*/
+                       else if (bin_curr > (bin_righ + reg_no_trend_margin))
+                               trend[xidx] = 1;  /*downward*/
+                       else
+                               trend[xidx] = 0;  /*no trend*/
+               } else
+                       trend[xidx] = -1;/*too small hist not valid trend*/
+               if ((dnlp_printk >> 14) & 0x1)
+                       pr_dnlp_dbg("## step 4.0:(adp_thrd[%2d]) %4d, (bin_curr) %4d,bin_righ %4d => trend[%2d] %d\n",
+                               xidx, adp_thrd[xidx],
+                               bin_curr, bin_righ,
+                               xidx, trend[xidx]);
+       }
+
+       /*step 4.1 gen: xross_point/slope,*/
+       xross_num  = -1;/*make sure it is -1 for C, and 0 for matlab*/
+       lpfbin_1stpeak = maxbin_2ndabove = -1;/*initialization*/
+       bb_protect = -3;
+       slope_1st  = 256;
+
+       for (xidx = 0; xidx < 32; xidx++) {
+               /*get the bin_lpf*/
+               bin_curr = hist_lpf[xidx];
+               bin_righ = hist_lpf[xidx + 1];
+
+               /* 4.1.1: get the cross-121curve point/slope*/
+               if (xidx > 0 && xidx < 31 &&
+                       trend[xidx - 1] == 2 && trend[xidx + 1] == 2)
+                       trend[xidx] = 2;  /*avoid one node valley*/
+
+               c = trend[xidx];
+               m = (xidx < 1) ? trend[0] :
+                       (trend[xidx - 1] == 0) ? m : trend[xidx - 1];
+               p = (xidx > 30) ? trend[31] :
+                       trend[xidx + 1];
+               if (((xidx > 1 && ((c == 1 && m == 2) ||
+                       ((c == 0 || c == -1) &&
+                       (m == -1) && p == 1))) ||
+                       ((xidx == 1 || xidx == 2) &&
+                       (c == 1 || m == 1) && 0)) &&
+                       xross_num < 8) {
+                       /*4.1.1.1:set the index*/
+                       xross_num += 1;
+                       /*node index of cross 121 line*/
+                       xross_xidx[xross_num] = xidx;
+
+               /*get the 2nd and above maximum bin*/
+               if (xross_num == 0)
+                       lpfbin_1stpeak = bin_curr;
+               else if (bin_curr > maxbin_2ndabove)
+                       maxbin_2ndabove = bin_curr;
+
+               /*4.1.1.2:get the node slope*/
+               /*get the bin_lpf8 for divider only for the peak node*/
+               bin_lpf8 = 0;
+               kk = (xidx << 1) + 1;/*bin index in histogram*/
+               m3 = (kk < 3) ? 0 : (kk - 3);
+               m4 = (kk < 4) ? 0 : (kk - 4);
+               p2 = (kk > 61) ? 63 : (kk + 2);
+               p3 = (kk > 60) ? 63 : (kk + 3);
+               bin_lpf8 = ((((int) iHst[m3] + (int) iHst[m4] +
+                                         (int) iHst[p2] + (int) iHst[p3]) >> 2)
+                                               + bin_curr) >> 1;
+
+               /*get the max bin of the [x x C x] four bins*/
+               bin_max = 0; bin_max_idx = -3;
+               for (k = -2; k < 2; k++) {
+                       kk = (xidx << 1) + 1 + k;  /*2x+1 + k*/
+                       kk = (kk < 0) ? 0 : (kk > 63) ? 63 : kk;
+                       if ((int)iHst[kk] > bin_max) {
+                               bin_max = (int)iHst[kk];
+                               bin_max_idx = k;
+                       }
+               }
+
+               /*get the blackbar protection*/
+               bin_scl = (bin_curr << 1) +
+                       bin_curr + (bin_curr >> 2); /*3.25*bin*/
+               if (xross_num == 1 && xidx < 4 && bin_max > bin_scl)
+                       bb_protect = bin_max_idx;  /*[-2,-1,0,1]*/
+
+               /*bin_clip*/
+               bin_scl = (bin_lpf8) >> 1;   /*0.5*/
+               bin_clip = (bin_scl > hist_avg) ? hist_avg :
+               (bin_scl < adp_thrd[xidx]) ? adp_thrd[xidx] : bin_scl;
+
+               /*get the slope by the divider*/
+               bin_scl = (bin_curr < bin_righ) ? bin_righ:bin_curr;
+               /*256 normalized to 1.0 for the slope*/
+               xross_slop[xross_num] = (bin_scl << 8) / (bin_clip + 1);
+
+               /*4.1.1.3:get the cross node ofst*/
+               /*get if local max bin localtes on which bin [-2,-1,0,1]*/
+               bin_scl = (bin_clip << 1); /* 2x */
+               if (xidx > 1 && xidx < 29 && bin_max > bin_scl)
+                       xross_ofst[xross_num] = bin_max_idx;
+               else
+                       xross_ofst[xross_num] = -3;
+
+               /*4.1.1.4 : detect the mono max bins and decrease the slop*/
+               bin_scl = (hist_avg << 2);
+               /*max(bin_curr, 4*hist_avg)*/
+               bin_scl = (bin_curr > bin_scl) ? bin_curr : bin_scl;
+               if (xidx >= reg_mono_binrang[0] &&
+                       xidx <= reg_mono_binrang[1]) {
+                       if (bin_max > ((bin_scl << 1) + (bin_scl)))
+                               lmt_slope = reg_mono_protect[0];/*3.5x*/
+                       else if (bin_max > ((bin_scl << 1) + (bin_scl >> 1)))
+                               lmt_slope = reg_mono_protect[1];/*2.5x*/
+                       else if (bin_max > ((bin_scl << 1)))
+                               lmt_slope = reg_mono_protect[2]; /*2.0x*/
+                       else if (bin_max > ((bin_scl << 1) - (bin_scl >> 2)))
+                               lmt_slope = reg_mono_protect[3]; /*1.75x*/
+                       else if (bin_max > ((bin_scl) + (bin_scl >> 1)))
+                               lmt_slope = reg_mono_protect[4]; /*1.5x*/
+                       else if (bin_max > ((bin_scl) + (bin_scl >> 2)))
+                               lmt_slope = reg_mono_protect[5]; /*1.25x*/
+                       else
+                               lmt_slope = 1024; /*4.0*/
+
+                               /* limit the slope for mono color(face part) */
+                       xross_slop[xross_num] =
+                       (xross_slop[xross_num] > lmt_slope) ?
+                       lmt_slope : xross_slop[xross_num];
+               }
+
+               }
+
+               /*4.1.2 get the 1st node slope*/
+               if (xidx == 0)
+                       /*256 normalized to 1.0*/
+                       slope_1st = (bin_curr << 8) / (adp_thrd[xidx] + 1);
+
+               /*4.1.3 get the right most valid node*/
+               if (bin_curr > adp_thrd[xidx])
+                       xidx_rightmost_valid_hist = xidx;
+       }
+
+
+       /*right most valid peak rightshift to */
+       /* the right most valid peak    (patch)*/
+       xidx_final_peak = xross_xidx[xross_num];
+       if (xidx_final_peak >= 23 && xidx_final_peak < 32) {
+               k = xidx_final_peak - 23;
+               max_shift = reg_trend_wht_expand_lut8[k];
+               t = 0;
+       tt = xidx_final_peak + t;
+               while (trend[tt] >= 0 && t <= max_shift && (tt) < 31) {
+                       trend[tt] = 2;
+                       xross_xidx[xross_num] = tt;
+                       t = t + 1;
+                       tt = xidx_final_peak + t;
+               }
+               if (t > 1)
+                       xross_ofst[xross_num] = -2;
+       }
+
+       /*step 4.2 right most peak ramp to max_range curve*/
+       xross_num += 1;
+       xross_xidx[xross_num] =
+               (xidx_rightmost_valid_hist >= 22) ? 32 :
+               (xidx_rightmost_valid_hist <= 13) ? 23 :
+               (xidx_rightmost_valid_hist+10);
+       xross_slop[xross_num] = 128;
+                 /*0.5*/
+       if ((dnlp_printk >> 4) & 0x1) {
+               pr_dnlp_dbg("];\nxross_xidx =[");
+               for (k = 0; k < 9; k++)
+                       pr_dnlp_dbg("%2d ", xross_xidx[k]);
+               pr_dnlp_dbg("];\nxross_slop =[");
+               for (k = 0; k < 9; k++)
+                       pr_dnlp_dbg("%2d ", xross_slop[k]);
+               pr_dnlp_dbg("];\nxross_ofst(org) =[");
+               for (k = 0; k < 9; k++)
+                       pr_dnlp_dbg("%2d ", xross_ofst[k]);
+               pr_dnlp_dbg("];\n xross_num = %3d\n ", xross_num);
+               pr_dnlp_dbg("lpfbin_1stpeak = %3d\n ", lpfbin_1stpeak);
+               pr_dnlp_dbg("maxbin_2ndabove = %3d\n ", maxbin_2ndabove);
+               pr_dnlp_dbg("bb_protect = %3d\n ", bb_protect);
+               pr_dnlp_dbg("xidx_rightmost_valid_hist = %3d\n ",
+                       xidx_rightmost_valid_hist);
+       }
+
+       /*step 4.3 generate the 32-nodes curve base on the xcross nodes*/
+       slope_step_norm = 32;  /*1024/32nodes*/
+       for (xnum = 0; xnum <= xross_num; xnum++) {
+               xidx = xross_xidx[xnum];
+               if (xnum == 0)
+                       xidx_prev = 0;/*first peak*/
+               else
+                       xidx_prev = xross_xidx[xnum - 1];
+
+               /*4.3.1 black/white expansion of each peak*/
+               if (xnum < xross_num) {
+                       /*black expansion left side (modify the trend[xidx])*/
+                       if (reg_trend_blk_expand_mode >= 1 &&
+                               xidx > 2 && xidx < 29 && trend[xidx - 1] <= 0)
+                               trend[xidx - 1] = 2;
+                       if (reg_trend_blk_expand_mode >= 2 &&
+                               xidx > 2 && xidx < 29 && trend[xidx - 2] <= 0)
+                               trend[xidx - 2] = 2;
+                       /*white expansion right side */
+                       if (reg_trend_wht_expand_mode >= 1 &&
+                               xidx >= 0 && xidx < 30 && trend[xidx + 1] <= 0)
+                               trend[xidx + 1] = 1;
+                       if (reg_trend_wht_expand_mode >= 2 &&
+                               xidx >= 0 && xidx < 29 && trend[xidx + 2] <= 0)
+                               trend[xidx + 2] = 1;
+               }
+               if ((dnlp_printk >> 14) & 0x1)
+                       pr_dnlp_dbg("\n\n## step 4.3.1 BWE : xidx = %2d, trend[%2d-1]= %4d,trend[%2d-2]= %4d\n",
+                               xidx, xidx, trend[xidx - 1],
+                               xidx, trend[xidx - 2]);
+               /*4.3.2 get the slope of each range*/
+               slope_c = xross_slop[xnum];
+               if (xnum == 0)
+                       slope_c =
+                               (slope_c < reg_min_slop_1st) ?
+                               reg_min_slop_1st :
+                               (slope_c > reg_max_slop_1st) ?
+                               reg_max_slop_1st :
+                               slope_c;
+               else if (xnum >= (xross_num - 1))
+                       slope_c =
+                               (slope_c < reg_min_slop_fin) ?
+                               reg_min_slop_fin :
+                               (slope_c > reg_max_slop_fin) ?
+                               reg_max_slop_fin :
+                               slope_c;
+               else
+                       slope_c =
+                               (slope_c < reg_min_slop_mid) ?
+                               reg_min_slop_mid :
+                               (slope_c > reg_max_slop_mid) ?
+                               reg_max_slop_mid :
+                               slope_c;
+
+               if (xnum == 0)
+                       slope_p =
+                               (slope_1st < reg_min_slop_1st) ?
+                               reg_min_slop_1st :
+                               (slope_1st > reg_max_slop_1st) ?
+                               reg_max_slop_1st :
+                               slope_1st;
+               else {
+                       slope_p = xross_slop[xnum - 1];
+                       slope_p =
+                       (slope_p < reg_min_slop_mid) ?
+                       reg_min_slop_mid :
+                       (slope_p > reg_max_slop_mid) ?
+                       reg_max_slop_mid :
+                       slope_p;
+               }
+               if ((dnlp_printk >> 14) & 0x1)
+                       pr_dnlp_dbg("## step 4.3.2,get the slope: xidx = %2d, slope_c= %4d\n",
+                               xidx, slope_c);
+               /*4.3.3 get the slop_steps*/
+               slope_step_c = (slope_c >> 3);   /*(32*slope_c)/256*/
+               slope_step_p = (slope_p >> 3);   /*(32*slope_p)/256*/
+               delta_slop_step_c = slope_step_c - slope_step_norm;
+               delta_slop_step_p = slope_step_p - slope_step_norm;
+               if ((dnlp_printk >> 14) & 0x1)
+                       pr_dnlp_dbg("## step 4.3.2,get the slope: xnum = %2d, slope_p= %4d\n",
+                               xnum, slope_p);
+               /*4.4.4 estimate the curves between nodes*/
+               if (xidx > 0) {
+                       /*4.4.4.1 initialize the output curve */
+       /*      ws consideration of black level boost up*/
+                       if (xnum == 0 && xidx <= 11) {
+                               boost_ofst = 0;
+                               m = (xidx < 1) ? 0 : (xidx - 1);
+                               p = (xidx > 10) ? 11 : (xidx + 1);
+                               c = xidx;
+
+                               if (xross_ofst[xnum] == -2)
+                                       /*local max is on (xidx-2)*/
+                                       boost_ofst = reg_blk_boost_12[m];
+                               else if (xross_ofst[xnum] == -1)
+                                       /*local max is on (xidx-1)*/
+                                       boost_ofst = (reg_blk_boost_12[m] +
+                                               reg_blk_boost_12[c]) >> 1;
+                               else if (xross_ofst[xnum] == 0)
+                                       /*local max is on (xidx-0)*/
+                                       boost_ofst = reg_blk_boost_12[c];
+                               else if (xross_ofst[xnum] ==  1)
+                                       /*local max is on (xidx+1)*/
+                                       boost_ofst = (reg_blk_boost_12[c] +
+                                               reg_blk_boost_12[p]) >> 1;
+                               else
+                                       boost_ofst = reg_blk_boost_12[c];
+
+
+                               if (lpfbin_1stpeak > (maxbin_2ndabove << 1))
+                                       boost_ofst = boost_ofst;/*1.0*/
+                               else if (lpfbin_1stpeak > (maxbin_2ndabove +
+                                               (maxbin_2ndabove >> 1)))
+                                       boost_ofst = boost_ofst -
+                                               (boost_ofst >> 2); /*0.75*/
+                               else if (lpfbin_1stpeak > (maxbin_2ndabove))
+                                       boost_ofst = (boost_ofst >> 1); /*0.5*/
+                               else if (lpfbin_1stpeak > (maxbin_2ndabove -
+                                               (maxbin_2ndabove >> 2)))
+                                       boost_ofst =
+                                               (boost_ofst >> 2); /*0.25*/
+                               else
+                                       boost_ofst =
+                                               (boost_ofst >> 3); /*0.125*/
+
+                               /*black bar protection to */
+                               /* overwrite the boost_ofst*/
+                               if (bb_protect == -2)
+                                       /*local max is on (xidx-2)*/
+                                       boost_ofst =  12;
+                               else if (bb_protect == -1)
+                                       /*local max is on (xidx-1)*/
+                                       boost_ofst = 8;
+                               else if (bb_protect == 0)
+                                       /*local max is on (xidx-0)*/
+                                       boost_ofst = 0;
+                               else if (bb_protect == 1)
+                                       /*local max is on (xidx+1)*/
+                                       boost_ofst =  -8;
+                               else if (bb_protect == 2)
+                                       /*local max is on (xidx+2)*/
+                                       boost_ofst = -12;
+                               else
+                                       boost_ofst = boost_ofst;
+                               /* initialize the curv_hist_node */
+                               curv_hist_node[xidx] =
+                                       curv_121n_node[xidx] + boost_ofst;
+                               if ((dnlp_printk >> 14) & 0x1)
+                                       pr_dnlp_dbg("**4.4.4.1((xnum==0 && xidx<=11)):xidx-%2d,boost_ofst-%2d,=> curv_hist_node[%2d]= %4d\n ",
+                                       xidx, boost_ofst, xidx,
+                                       curv_hist_node[xidx]);
+                       } else {
+                               if (xross_ofst[xnum] == -2)
+                                       /*local max is on (xidx-2)*/
+                                       boost_ofst = 0;
+                               else if (xross_ofst[xnum] == -1)
+                                       /*local max is on (xidx-1)*/
+                                       boost_ofst = 0;
+                               else if (xross_ofst[xnum] == 0)
+                                       /*local max is on (xidx-0)*/
+                                       boost_ofst = 0;
+                               else if (xross_ofst[xnum] ==  1)
+                                       /*local max is on (xidx+1)*/
+                                       boost_ofst = 0;
+                               else if (xross_ofst[xnum] == -2)
+                                       /*local max is on (xidx+2)*/
+                                       boost_ofst = 0;
+                               else
+                                       boost_ofst = 0;
+                               /*initialize the curv_hist_node*/
+                               curv_hist_node[xidx] =  curv_121n_node[xidx] +
+                                       boost_ofst;
+                               if ((dnlp_printk >> 14) & 0x1)
+                                       pr_dnlp_dbg("**4.4.4.1( ):xidx-%2d,boost_ofst-%2d,=> curv_hist_node[%2d]= %4d\n ",
+                                       xidx, boost_ofst,
+                                       xidx, curv_hist_node[xidx]);
+                       }
+
+                       /*4.4.4.2 analyse the trend*/
+                       up_idx = xidx;
+                       dn_idx = xidx_prev;
+                       for (k = (xidx - 1); k >= xidx_prev; k--) {
+                               if (trend[k] == 2 && k > 1)
+                                       up_idx = k; /*up-trend*/
+                               if (trend[k] == 2 ||
+                                       ((trend[k] == -1 || trend[k] == 0)
+                                       && (k > xidx_prev)))
+                                       dn_idx = k;  /*down trend ends*/
+                       }
+                       /*num of bins of up-trend*/
+                       num_2 = xidx - up_idx;
+                       /*num of bins of down-trend*/
+                       num_1 = dn_idx - xidx_prev;
+                       /*num of bins of too small hist*/
+                       num_x = up_idx - dn_idx;
+                       if (num_x > 0)
+                               num_x += 1;
+                       if ((dnlp_printk >> 14) & 0x1)
+                               pr_dnlp_dbg("\n**4.4.4.2:xidx=%2d,up_idx=%2d,xidx_prev=%2d,dn_idx=%2d,num_2_1_x[%d(%d-%d) %d(%d-%d) %d(%d-%d)]\n",
+                                       xidx, up_idx, xidx_prev,
+                                       dn_idx, num_2, xidx, up_idx,
+                                       num_1, dn_idx, xidx_prev,
+                                       num_x, up_idx, dn_idx);
+                       /*4.4.4.3 get the 1st round of */
+                       /* estimation of the curves*/
+                       if (num_2 <= 0)
+                               sum_step_2 = (delta_slop_step_c >> 1);
+                       else
+                               /*(1.5-(2^(-num_2)))*delta_slop_step; */
+                               /* % 0.5 + .5 + .25 +.125.*/
+                       sum_step_2 = (num_2 << 5) +
+                                       delta_slop_step_c +
+                                       (delta_slop_step_c>>1) -
+                                       (delta_slop_step_c>>num_2);
+
+                       if (num_1 == 0)
+                               sum_step_1 = 0;
+                               /*no up-trend most likely for left most*/
+                       else
+                       /*(2- (2^(-num_1)))*delta_slop_step_prev; */
+                       /*      1+.5+.25+.125+...*/
+                       sum_step_1 = (num_1 << 5) +
+                               (delta_slop_step_p << 1) -
+                               (delta_slop_step_p >> num_1);
+
+               sum_step_x = curv_hist_node[xidx] -
+                               curv_hist_node[xidx_prev] -
+                               sum_step_2 - sum_step_1;
+                       if ((dnlp_printk >> 14) & 0x1)
+                               pr_dnlp_dbg(" **4.4.4.3:num_2=%2d,num_1=%2d,sum_step_x=%2d(%4d-%4d-%d-%d)\n",
+                                       num_2, num_1, sum_step_x,
+                                       curv_hist_node[xidx],
+                                       curv_hist_node[xidx_prev],
+                                       sum_step_2, sum_step_1);
+
+               /*4.4.4.4 adaptive offset for nearby peak protection */
+               /*      (2nd peak curve start from 1'st results)*/
+                       if (xnum < 3 && sum_step_x < 0 && xidx < 20) {
+                               ofset = (sum_step_x *
+                                       reg_adp_ofset_20[xidx]) >> 5;
+                               curv_hist_node[xidx] -= ofset;
+                               sum_step_x -= ofset;
+                       }
+
+               /*4.4.4.5 if x range slop too low,*/
+                /* *do the fallback on up/dn range*/
+                       sum_step_x_min = (num_x * reg_min_slop_mid) >> 3;
+                       sub_x = sum_step_x_min - sum_step_x;
+                       if (sub_x > 0) {
+                               /*scale down the up-trend and dn-trend range*/
+                               if (num_2 == 0 || sum_step_2 == 0)
+                                       dlt_2 = 0;
+                               else
+                                       dlt_2 = sub_x * sum_step_2 / (sum_step_2
+                                               + sum_step_1);
+
+                               dlt_1 = sub_x - dlt_2;
+
+                       /*ratio to gain back the slope*/
+                       if (sum_step_2 == 0)
+                               rat_2 = 256;
+                       else
+                               rat_2 = ((sum_step_2 -
+                                       dlt_2) << 8) / sum_step_2;
+                       if (sum_step_1 == 0)
+                               rat_1 = 256;
+                       else
+                               rat_1 = ((sum_step_1 -
+                                       dlt_1) << 8) / sum_step_1;
+                       if ((dnlp_printk >> 14) & 0x1) {
+                               pr_dnlp_dbg("\n$$ 4.4.4.5:sub_x = sum_step_x_min-sum_step_x,sum_step_2(1),dlt_2(1),rat_2(1) ");
+                               pr_dnlp_dbg("\n$$ 4.4.4.5:%d= %d  -  %d,%d(%d),%d(%d), %d(%d)  ",
+                                       sub_x, sum_step_x_min,
+                                       sum_step_x, sum_step_2,
+                                       sum_step_1, dlt_2, dlt_1,
+                                       rat_2, rat_1);
+                       }
+                       } else {
+                                rat_2 = 256;
+                                rat_1 = 256; /*normalize 256 to 1.0*/
+                       }
+
+                       /*4.4.4.6 calculate the up-trend curve*/
+                       t = 0;
+               accum = curv_hist_node[xidx];
+                       for (k = (xidx - 1); k >= up_idx; k--) {
+                               kk = (t < 1) ? 1 : t;
+                               if (rat_2 == 256)
+                                       accum -= (slope_step_norm +
+                                               (delta_slop_step_c >> kk));
+                               else
+                                       accum -= (((slope_step_norm +
+                                       (delta_slop_step_c>>kk)) * rat_2) >> 8);
+
+                               /*get the node curve*/
+                               curv_hist_node[k] = accum;
+                               t = t + 1;
+                               if ((dnlp_printk >> 14) & 0x1)
+                                       pr_dnlp_dbg("==4.4.4.6 up-trend curve:k-%d,kk-%d,rat_2-%d, slope_step_norm-%d,delta_slop_step_c-%d => curv_hist_node[%d]=%4d\n",
+                                               k, kk, rat_2, slope_step_norm,
+                                               delta_slop_step_c,
+                                               k, accum);
+                       }
+
+                       /*4.4.4.7 calculate the dn-trend curve*/
+                       t = 0;
+                       accum = curv_hist_node[xidx_prev];
+                       if (xidx_prev < (dn_idx - 1)) {
+                               for (k = xidx_prev; k <= (dn_idx - 1); k++) {
+                                       curv_hist_node[k] = accum;
+                               if (rat_1 == 256)
+                                       accum = accum + (slope_step_norm +
+                                       (delta_slop_step_p >> t));
+                               else
+                                       accum = accum + (((slope_step_norm +
+                                       (delta_slop_step_p>>t)) * rat_1) >> 8);
+
+                               t = t + 1;
+                               /*for not num_x cases, the dn_idx== up_idx*/
+                               if (num_x == 0 && k == (dn_idx - 1))
+                                       curv_hist_node[dn_idx] =
+                                       (curv_hist_node[dn_idx] +
+                                        accum + 1) >> 1;
+                               if ((dnlp_printk >> 14) & 0x1)
+                                       pr_dnlp_dbg("==4.4.4.7-1:  dn-trend curve:k-%d,t-%d,rat_1-%d, slope_step_norm-%d,delta_slop_step_p-%d => accum[%d]=%4d(%4d)\n",
+                                               k, t, rat_1, slope_step_norm,
+                                               delta_slop_step_p, k,
+                                               accum, curv_hist_node[dn_idx]);
+                       }
+               } else
+               curv_hist_node[xidx_prev] = accum;
+                       if ((dnlp_printk >> 14) & 0x1)
+                               pr_dnlp_dbg("==4.4.4.7-2:dn-trend curve: xidx_prev= %d,curv_hist_node[%d]= %d  ",
+                                       xidx_prev, xidx_prev,
+                                       curv_hist_node[xidx_prev]);
+
+               /*4.4.4.8 get the in-between x range curve*/
+               kk = (dn_idx < 1) ? 0 : (dn_idx - 1);
+               up_dn_dist = curv_hist_node[up_idx] - curv_hist_node[kk];
+               if (num_x > 0) {
+                       accum = curv_hist_node[kk];
+                       if (dn_idx <= 0) {
+                               st_idx = dn_idx + 1;
+                               /*max(num_x -1,1);*/
+                               num_xx = (num_x < 2) ? 1 : (num_x - 1);
+                       } else{
+                               st_idx = dn_idx;
+                               num_xx = num_x;
+                       }
+
+                       for (k = st_idx; k <= (up_idx - 1); k++) {
+                               accum = accum + (up_dn_dist / num_xx);
+                               curv_hist_node[k] = accum;
+                       }
+                       if ((dnlp_printk >> 14) & 0x1)
+                               pr_dnlp_dbg("==4.4.4.8:k-%d,up_dn_dist-%d,num_xx-%d =>curv_hist_node[%d]=%d\n",
+                                       k, up_dn_dist, num_xx, k, accum);
+                       }
+       } /*for xidx*/
+       } /*for xnum*/
+
+       /*1st two nodes blending (optional)*/
+       curv_hist_node[0] = curv_121n_node[0] +
+       ((curv_hist_node[1] - curv_121n_node[1] + 1) >> 1);
+
+       /*do interpolation from 32 nodes to 64 bins.*/
+       for (k = 0; k < 65; k++) {
+               if (k == 0)
+                       oMap[k] = 0;
+               else if (k == 64)
+                       oMap[k] = 1023;
+               else if ((k%2 == 1))
+                       oMap[k] = (unsigned int) curv_hist_node[(k >> 1)];
+               else
+                       oMap[k] = (unsigned int)
+                               ((curv_hist_node[(k >> 1) - 1] +
+                               curv_hist_node[(k >> 1)] + 1) >> 1);
+
+       nDif = oMap[k] - (k << 4);
+       nTmp = (k << 4) + ((nDif * ve_hist_cur_gain +
+               (ve_hist_cur_gain_precise >> 1)) / ve_hist_cur_gain_precise);
+
+       if ((dnlp_printk >> 4) & 0x1)
+               pr_dnlp_dbg("# add_gain:[%d]:oMap[k]_%d,line_%d, Dif_%d => final_%d (gain %d)\n ",
+               k, oMap[k], k << 4, nDif, nTmp, ve_hist_cur_gain);
+
+               oMap[k] = nTmp;
+       }
+
+       if ((dnlp_printk >> 4) & 0x1) {
+               pr_dnlp_dbg("\n=====hist_lpf: [\n");
+               for (j = 0; j < 2; j++) {
+                       i = j * 16;
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                                       hist_lpf[i], hist_lpf[i+1],
+                                       hist_lpf[i+2], hist_lpf[i+3],
+                                       hist_lpf[i+4], hist_lpf[i+5],
+                                       hist_lpf[i+6], hist_lpf[i+7],
+                                       hist_lpf[i+8], hist_lpf[i+9],
+                                       hist_lpf[i+10], hist_lpf[i+11],
+                                       hist_lpf[i+12], hist_lpf[i+13],
+                                       hist_lpf[i+14], hist_lpf[i+15]);
+               }
+               pr_dnlp_dbg("]\n");
+
+               pr_dnlp_dbg("\n=====trend_32: [\n");
+               for (j = 0; j < 2; j++) {
+                       i = j * 16;
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                                       trend[i], trend[i+1],
+                                       trend[i+2], trend[i+3],
+                                       trend[i+4], trend[i+5],
+                                       trend[i+6], trend[i+7],
+                                       trend[i+8], trend[i+9],
+                                       trend[i+10], trend[i+11],
+                                       trend[i+12], trend[i+13],
+                                       trend[i+14], trend[i+15]);
+               }
+               pr_dnlp_dbg("]\n");
+
+               pr_dnlp_dbg("\n=====curv_nod: [\n");
+               for (j = 0; j < 2; j++) {
+                       i = j * 16;
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                                       curv_hist_node[i],
+                                       curv_hist_node[i+1],
+                                       curv_hist_node[i+2],
+                                       curv_hist_node[i+3],
+                                       curv_hist_node[i+4],
+                                       curv_hist_node[i+5],
+                                       curv_hist_node[i+6],
+                                       curv_hist_node[i+7],
+                                       curv_hist_node[i+8],
+                                       curv_hist_node[i+9],
+                                       curv_hist_node[i+10],
+                                       curv_hist_node[i+11],
+                                       curv_hist_node[i+12],
+                                       curv_hist_node[i+13],
+                                       curv_hist_node[i+14],
+                                       curv_hist_node[i+15]);
+               }
+               pr_dnlp_dbg("]\n");
+       }
+
+       k = 0;
+}
 
 /*iHst[0:63]: [0,4)->iHst[0], [252,256)->iHst[63]*/
 /*oMap[0:64]:0:16:1024*/
@@ -1252,8 +1913,7 @@ void get_clahe_curve(unsigned int *oMap, unsigned int *olAvg4,
        unsigned int adp_crate = clip_rate;/* u8 */
        int clahe_gain_neg;
        int clahe_gain_pos;
-
-       bool prt_flg = ((dnlp_printk >> 4) & 0x1);
+       unsigned short *glb_clash_curve;
 
        if (clip_rmin > clip_rate)
                clip_rmin = clip_rate;
@@ -1275,8 +1935,8 @@ void get_clahe_curve(unsigned int *oMap, unsigned int *olAvg4,
        else if (hstEnd < 48)
                hstEnd = 48;
 
-       if (prt_flg)
-               pr_info("step B-2.x( get_clahe_curve): hstBgn = %d,hstEnd = %d\n",
+       if ((dnlp_printk >> 4) & 0x1)
+               pr_dnlp_dbg("step B-2.x( get_clahe_curve): hstBgn = %d,hstEnd = %d\n",
                hstBgn, hstEnd);
 
        oMap[64] = 1024; /* 0~1024 */
@@ -1314,8 +1974,8 @@ void get_clahe_curve(unsigned int *oMap, unsigned int *olAvg4,
        lAvg4 = lAvg4 / tsum;/*luminance avgerage (0~255)*/
        lAvg1 = (lAvg4 + 2) >> 2;/* bin num of average (0~63)*/
 
-       if (prt_flg)/* echo 4 */
-               pr_info("#Step B-2.x:within range luma_avg4 = %d, max_hst(tmax) =%d\n",
+       if ((dnlp_printk >> 4) & 0x1)/* echo 4 */
+               pr_dnlp_dbg("#Step B-2.x:within range luma_avg4 = %d, max_hst(tmax) =%d\n",
                        lAvg4, tmax);
 
        /*4.2 get the adptive clip rate,  << 4 */
@@ -1363,8 +2023,8 @@ void get_clahe_curve(unsigned int *oMap, unsigned int *olAvg4,
                return;
        }
 
-       if (prt_flg)
-               pr_info("# stepB-(get_clahe)CL:\n Range[hstBgn %02d ~ hstEnd %02d] lAvg4=%d(%d),\n"
+       if ((dnlp_printk >> 4) & 0x1)
+               pr_dnlp_dbg("# stepB-(get_clahe)CL:\n Range[hstBgn %02d ~ hstEnd %02d] lAvg4=%d(%d),\n"
                        "(crate /4096)=%d, tsum=%5d, cLmt=%3d, tAvg=%4d, sumshft=%d, norm14=%d\n",
                        hstBgn, hstEnd, lAvg4, lAvg1, adp_crate,
                        tsum, cLmt, tAvg,  sumshft, norm14);
@@ -1429,8 +2089,8 @@ void get_clahe_curve(unsigned int *oMap, unsigned int *olAvg4,
                tmp = (tmp + 32) >> 6;
                oMap[i] = (tmp > 1023) ? 1023 : tmp;
 
-               if (prt_flg)/* echo 4 */
-                       pr_info("StepB-2.x:CLASHE:\n[%02d:iHst=%5d,Hclip=%5d,bin_clip=%5d]:[sum_clp=%5d,j=%2d,tmp1=%6d,tmp2=%6d]%4d(45lin)+%5d(dlt_acc)=>%4d(C)],\n",
+               if ((dnlp_printk >> 4) & 0x1)/* echo 4 */
+                       pr_dnlp_dbg("StepB-2.x:CLASHE:\n[%02d:iHst=%5d,Hclip=%5d,bin_clip=%5d]:[sum_clp=%5d,j=%2d,tmp1=%6d,tmp2=%6d]%4d(45lin)+%5d(dlt_acc)=>%4d(C)],\n",
                                i, iHst[i], Hst_clip[i],
                                tbin_clip, sum_clip, j,
                                tmp1, tmp2, i<<4,
@@ -1441,6 +2101,8 @@ void get_clahe_curve(unsigned int *oMap, unsigned int *olAvg4,
        *olAvg4 = lAvg4;
 
        if (debug_add_curve_en) {
+               glb_clash_curve = kzalloc(65*sizeof(unsigned short),
+                       GFP_KERNEL);
                for (i = 0; i < 65; i++) {
                        oMap[i] = ((128 - glb_clash_curve_bld_rate) * oMap[i] +
                                glb_clash_curve_bld_rate *
@@ -1448,25 +2110,18 @@ void get_clahe_curve(unsigned int *oMap, unsigned int *olAvg4,
                                64) >> 7;
                }
        }
-    /* debug */
-       CrtTstDat[2] = hstBgn;
-       CrtTstDat[3] = hstEnd;
 
-       if ((dnlp_printk >> 14) & 0x1)
-               pr_info("\n@@@get_clahe_curve(): clahe_gain_neg = %d,clahe_gain_pos = %d\n ",
+       if ((dnlp_printk >> 4) & 0x1)
+               pr_dnlp_dbg("\n@@@get_clahe_curve(): clahe_gain_neg = %d,clahe_gain_pos = %d\n ",
                clahe_gain_neg, clahe_gain_pos);
 
-       if (prt_flg)
-               for (i = hstBgn; i < hstEnd; i++)
-                       pr_info("Step B-2.x #clsh: [(index)%02d: (iHst)%5d]:(45line) %4d => (clsh) %4d],\n",
-                               i, iHst[i], i << 4, oMap[i]);
-
        if ((dnlp_printk >> 14) & 0x1) {
                /* iHst */
-               pr_info("\n=====pre_0_gma: [\n");
+
+               pr_dnlp_dbg("\n=====pre_0_gma: [\n");
                for (j = 0; j < 4; j++) {
                        i = j*16;
-                       pr_info("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
                                        iHst[i], iHst[i+1],
                                        iHst[i+2], iHst[i+3],
                                        iHst[i+4], iHst[i+5],
@@ -1476,13 +2131,13 @@ void get_clahe_curve(unsigned int *oMap, unsigned int *olAvg4,
                                        iHst[i+12], iHst[i+13],
                                        iHst[i+14], iHst[i+15]);
                }
-               pr_info("]\n");
+               pr_dnlp_dbg("]\n");
 
                /* clip_Hst */
-               pr_info("\n=====clipped pre_0_gma: [\n");
+               pr_dnlp_dbg("\n=====clipped pre_0_gma: [\n");
                for (j = 0; j < 4; j++) {
                        i = j*16;
-                       pr_info("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
                                Hst_clip[i] + tAvg, Hst_clip[i+1] + tAvg,
                                Hst_clip[i+2] + tAvg, Hst_clip[i+3] + tAvg,
                                Hst_clip[i+4] + tAvg, Hst_clip[i+5] + tAvg,
@@ -1492,19 +2147,19 @@ void get_clahe_curve(unsigned int *oMap, unsigned int *olAvg4,
                                Hst_clip[i+12] + tAvg, Hst_clip[i+13] + tAvg,
                                Hst_clip[i+14] + tAvg, Hst_clip[i+15] + tAvg);
                }
-               pr_info("]\n");
+               pr_dnlp_dbg("]\n");
 
                /* clash_curve */
-               pr_info("\n=====clash_curve: [\n");
+               pr_dnlp_dbg("\n=====clash_curve: [\n");
                for (j = 0; j < 4; j++) {
                        i = j*16;
-                       pr_info("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
                                oMap[i], oMap[i+1], oMap[i+2], oMap[i+3],
                                oMap[i+4], oMap[i+5], oMap[i+6], oMap[i+7],
                                oMap[i+8], oMap[i+9], oMap[i+10], oMap[i+11],
                                oMap[i+12], oMap[i+13], oMap[i+14], oMap[i+15]);
                }
-               pr_info("]\n");
+               pr_dnlp_dbg("]\n");
        }
 }
 
@@ -1554,9 +2209,6 @@ static int cal_brght_plus(int luma_avg4, int low_lavg4)
        }
        pbrtness = dnlp_brightness;
 
-       /* debug */
-       CrtTstDat[12] = low_lavg4;
-       CrtTstDat[14] = dnlp_brightness;
 
        return dnlp_brightness; /* 0 ~ 1024 */
 }
@@ -1566,7 +2218,16 @@ static void clahe_tiir(void)
 {
        int i = 0;
        int nTmp0 = 0;
-       static unsigned int pgmma[65];
+       static unsigned int pgmma[65] = {
+                 0,   16,   32,  48,  64,  80,  96, 112,
+                 128, 144, 160, 176, 192, 208, 224, 240,
+                 256, 272, 288, 304, 320, 336, 352, 368,
+                 384, 400, 416, 432, 448, 464, 480, 496,
+                 512, 528, 544, 560, 576, 592, 608, 624,
+                 640, 656, 672, 688, 704, 720, 736, 752,
+                 768, 784, 800, 816, 832, 848, 864, 880,
+                 896, 912, 928, 944, 960, 976, 992, 1008, 1024
+                 };
 
        if (!ve_dnlp_luma_sum) {
                for (i = 0; i < 65; i++)
@@ -1578,11 +2239,18 @@ static void clahe_tiir(void)
                        nTmp0 = dnlp_bld_lvl * clash_curve[i] + (RBASE >> 1);
                        nTmp0 = nTmp0 + (RBASE - dnlp_bld_lvl) * pgmma[i];
                        nTmp0 = (nTmp0 >> dnlp_alg_param.dnlp_mvreflsh);
-                       clash_curve[i] = nTmp0;
+               if ((dnlp_printk >> 4) & 0x1)
+                       pr_dnlp_dbg("\n(C_iir):[%d]: clash_curve[i]:%d,pgmma[i]:%d => nTmp0:%d(%d/%d)\n",
+                               i, clash_curve[i], pgmma[i],
+                               nTmp0, dnlp_bld_lvl, RBASE);
+
+               clash_curve[i] = nTmp0;
                }
 
+
        for (i = 0; i < 65; i++)
                pgmma[i] = clash_curve[i];
+
 }
 
 /*function:iir filter coef cal base on the luma_avg change in time domain */
@@ -1593,7 +2261,6 @@ int curve_rfrsh_chk(int hstSum, int rbase)
 {
        static unsigned int tLumAvg[30];
        static unsigned int tAvgDif[30];
-       bool prt_flg = 0;
        int lSby = 0;
        int bld_lvl = 0;
        int i = 0;
@@ -1609,7 +2276,6 @@ int curve_rfrsh_chk(int hstSum, int rbase)
                (tLumAvg[29] - tLumAvg[28]) : (tLumAvg[28] - tLumAvg[29]);
 
        /* prt_flg = ((dnlp_printk >> 7) & 0x1); */
-       prt_flg = (dnlp_printk & 0x1);
 
        lSby = 0;
        for (i = 0; i < 8; i++)
@@ -1623,8 +2289,8 @@ int curve_rfrsh_chk(int hstSum, int rbase)
 
        bld_lvl = (bld_lvl << dnlp_alg_param.dnlp_schg_sft);
 
-       if (prt_flg)
-               pr_info("step 0.2.0: bld_lvl=%02d\n", bld_lvl);
+       if (dnlp_printk & 0x1)
+               pr_dnlp_dbg("step 0.2.0: bld_lvl=%02d\n", bld_lvl);
 
        /* play station: return with black scene intersection */
        if (tAvgDif[29] > bld_lvl)
@@ -1638,18 +2304,17 @@ int curve_rfrsh_chk(int hstSum, int rbase)
                bld_lvl = dnlp_alg_param.dnlp_cuvbld_max;
 
        /* print the logs */
-       if (prt_flg) {
-               pr_info("step 0.2.1: bld_lvl=%02d, lSby=%02d\n",
+       if (dnlp_printk & 0x1) {
+               pr_dnlp_dbg("step 0.2.1: bld_lvl=%02d, lSby=%02d\n",
                        bld_lvl, lSby);
                for (i = 0; i < 10; i++)
-                       pr_info("tLumAvg[%d]: = %d\n",
+                       pr_dnlp_dbg("tLumAvg[%d]: = %d\n",
                                i, tLumAvg[29 - i]);
                for (i = 0; i < 10; i++)
-                       pr_info("tAvgDif[%d]: = %d\n",
+                       pr_dnlp_dbg("tAvgDif[%d]: = %d\n",
                                i, tAvgDif[29 - i]);
        }
 
-       CrtTstDat[0] = bld_lvl;
        /* post processing */
        if (dnlp_alg_param.dnlp_respond_flag) {
                bld_lvl = RBASE;
@@ -1658,7 +2323,137 @@ int curve_rfrsh_chk(int hstSum, int rbase)
                bld_lvl = RBASE;
                dnlp_scn_chg = 1;
        }
-       CrtTstDat[1] = bld_lvl;
+
+       return bld_lvl;  /* tiir coefs */
+}
+
+
+
+int curve_rfrsh_chk_v2(int hstSum, int rbase)
+{
+       int i = 0;
+       int sumshft = 0;
+       unsigned int norm14 = 0;
+       unsigned int dif = 0;
+       unsigned int ihst_norm[65];
+       static unsigned int HstDif[8];
+       static unsigned int ScnDif[8];
+       static unsigned int pre_ihst_norm[65];
+       static unsigned int tLumAvg[8];
+       static unsigned int tAvgDif[8];
+       int bld_lvl = 0;
+
+       /*store frame dif for 8*/
+       for (i = 0; i < 7; i++) {
+               HstDif[i] = HstDif[i+1];
+               ScnDif[i] = ScnDif[i+1];
+               tLumAvg[i] = tLumAvg[i+1];
+               tAvgDif[i] = tAvgDif[i+1];
+
+       }
+
+       tLumAvg[7] = 0;
+       tAvgDif[7] = 0;
+       /*cal bld_lvl*/
+       tLumAvg[7] = (ve_dnlp_luma_sum + (hstSum >> 1)) / hstSum;
+       tLumAvg[7] = ((tLumAvg[7] + 4) >> 3);
+       tAvgDif[7] = (tLumAvg[7] > tLumAvg[18]) ?
+               (tLumAvg[7] - tLumAvg[7]) : (tLumAvg[7] - tLumAvg[7]);
+
+       bld_lvl = tAvgDif[7];
+       bld_lvl = (bld_lvl << dnlp_alg_param.dnlp_schg_sft);
+       /* play station: return with black scene intersection */
+       if (tAvgDif[7] > bld_lvl)
+               bld_lvl = tAvgDif[7];
+
+       if (bld_lvl > rbase) {
+               if (print_en) {
+                       for (i = 7; i >= 4; i--)
+                               pr_info("tLumAvg[%d]: = %d\n",
+                                                       i, tLumAvg[i]);
+                       for (i = 7; i >= 4; i--)
+                               pr_info("tAvgDif[%d]: = %d\n ",
+                                                       i, tAvgDif[i]);
+               }
+               bld_lvl = dnlp_alg_param.dnlp_cuvbld_min;
+
+       } else if (bld_lvl < dnlp_alg_param.dnlp_cuvbld_min)
+               bld_lvl = dnlp_alg_param.dnlp_cuvbld_min;
+       else if (bld_lvl > dnlp_alg_param.dnlp_cuvbld_max)
+               bld_lvl = dnlp_alg_param.dnlp_cuvbld_max;
+
+       /*new add,180202*/
+       sumshft = (hstSum >= (1 << 24)) ? 8 :
+                         (hstSum >= (1 << 22)) ? 6 :
+                         (hstSum >= (1 << 20)) ? 4 :
+                         (hstSum >= (1 << 18)) ? 2 :
+                         (hstSum >= (1 << 16)) ? 0 :
+                         (hstSum >= (1 << 14)) ? -2 :
+                         (hstSum >= (1 << 12)) ? -4 :
+                         (hstSum >= (1 << 10)) ? -6 :
+                         (hstSum >= (1 << 8)) ? -8 :
+                         (hstSum >= (1 << 6)) ? -10 :
+                         (hstSum >= (1 << 4)) ? -12 : -16;
+       if (sumshft >= 0)
+               norm14 = (1 << 30) / (hstSum >> sumshft);
+       else if (sumshft > -16)
+               norm14 = (1 << (30 + sumshft)) / hstSum;
+       else
+               norm14 = 0;
+
+       ScnDif[7] = 0;
+       HstDif[7] = 0;
+       for (i = 0; i < 64; i++) {
+               ihst_norm[i] = pre_0_gamma[i];
+               /*calc hist diff*/
+               dif = ihst_norm[i] > pre_ihst_norm[i] ?
+                       (ihst_norm[i] - pre_ihst_norm[i]) :
+                       (pre_ihst_norm[i] - ihst_norm[i]);
+               HstDif[7] += dif;
+               /*hist delay*/
+               pre_ihst_norm[i] = ihst_norm[i];
+       }
+       /*hist_diff sum norm to u16*/
+       if (sumshft >= 0)
+               HstDif[7] = ((HstDif[7] >> sumshft) * norm14 +
+                       (1 << 13)) >> 14;
+       else
+               HstDif[7] = ((HstDif[7] << (-sumshft)) * norm14 +
+                       (1 << 13)) >> 14;
+
+       /*if scene change,hist dif is large*/
+       ScnDif[7] = HstDif[7] > HstDif[6] ?
+               (HstDif[7] - HstDif[6]) :
+               (HstDif[6] - HstDif[7]);
+       ScnDif[7] = ScnDif[7] >> NORM;/*u6*/
+       if ((dnlp_printk >> 1) & 0x01) {
+               for (i = 7; i >= 5; i--)
+                       pr_dnlp_dbg("\n$$$curve_rfrsh_chk_v2():ScnDif[%d]: = %d\n",
+                       i, ScnDif[i]);
+       }
+
+       if (ScnDif[7] > scn_chg_th) {/*scn_chg_th need tunning*/
+               dnlp_scn_chg = 1;
+
+               if (print_en) {
+                       pr_info("\n*********************\n");
+                       pr_info("\n === nTstCnt= %d (in chg) ==\n\n", nTstCnt);
+                       pr_info("\n$$$hstSum=%d,sumshft=%d,norm14=%d,ve_dnlp_luma_sum=%d\n",
+                               hstSum, sumshft, norm14, ve_dnlp_luma_sum);
+                       pr_info("### scene changed ! ScnDif[19](%d) > scn_chg_th(%d)!\n ",
+                               ScnDif[7], scn_chg_th);
+                       for (i = 7; i >= 2; i--)
+                               pr_info("$$$curve_rfrsh_chk_v2():HstDif[%d]: = %d\n",
+                                       i, HstDif[i]);
+                       pr_info("================================\n");
+                       for (i = 7; i >= 2; i--)
+                               pr_info("$$$curve_rfrsh_chk_v2():ScnDif[%d]: = %d\n",
+                                       i, ScnDif[i]);
+               }
+       } else if (dnlp_alg_param.dnlp_respond_flag)
+               dnlp_scn_chg = 1;
+       else
+               dnlp_scn_chg = 0;
 
        return bld_lvl;  /* tiir coefs */
 }
@@ -1686,7 +2481,7 @@ static void dnlp3_param_refrsh(void)
                        (prev_dnlp_clashEnd  != dnlp_alg_param.dnlp_clashEnd) ||
                        (prev_dnlp_mtdbld_rate !=
                                dnlp_alg_param.dnlp_mtdbld_rate) ||
-                       (prev_dnlp_pst_gmarat != ve_dnlp_pst_gmarat) ||
+                       /*(prev_dnlp_pst_gmarat != ve_dnlp_pst_gmarat) || */
                        (prev_dnlp_blk_cctr !=
                                dnlp_alg_param.dnlp_blk_cctr) ||
                        (prev_dnlp_brgt_ctrl !=
@@ -1700,12 +2495,13 @@ static void dnlp3_param_refrsh(void)
                        (prev_dnlp_lgst_ratio != ve_dnlp_lgst_ratio) ||
                        (prev_dnlp_lgst_dst != ve_dnlp_lgst_dst) ||
                        (prev_dnlp_almst_wht != ve_dnlp_almst_wht) ||
-                       (prev_dnlp_pstgma_end != ve_dnlp_pstgma_end) ||
-                       (prev_dnlp_pstgma_ratio != ve_dnlp_pstgma_ratio) ||
-                       (prev_dnlp_pstgma_brghtrate !=
-                               ve_dnlp_pstgma_brghtrate) ||
-                       (prev_dnlp_pstgma_brghtrat1 !=
-                               ve_dnlp_pstgma_brghtrat1) ||
+                       /*(prev_dnlp_pstgma_end != ve_dnlp_pstgma_end) || */
+                       /* (prev_dnlp_pstgma_ratio  */
+                       /* != ve_dnlp_pstgma_ratio) || */
+                       /* (prev_dnlp_pstgma_brghtrate != */
+                       /* ve_dnlp_pstgma_brghtrate) || */
+                       /* (prev_dnlp_pstgma_brghtrat1 != */
+                       /* ve_dnlp_pstgma_brghtrat1) || */
                        (prev_dnlp_blkext_rate !=
                                dnlp_alg_param.dnlp_blkext_rate) ||
                        (prev_dnlp_whtext_rate !=
@@ -1781,7 +2577,7 @@ static void dnlp3_param_refrsh(void)
        prev_dnlp_clashBgn        = dnlp_alg_param.dnlp_clashBgn;
        prev_dnlp_clashEnd        = dnlp_alg_param.dnlp_clashEnd;
        prev_dnlp_mtdbld_rate = dnlp_alg_param.dnlp_mtdbld_rate;
-       prev_dnlp_pst_gmarat  = ve_dnlp_pst_gmarat;
+       /* prev_dnlp_pst_gmarat  = ve_dnlp_pst_gmarat; */
        prev_dnlp_blk_cctr        = dnlp_alg_param.dnlp_blk_cctr;
        prev_dnlp_brgt_ctrl   = dnlp_alg_param.dnlp_brgt_ctrl;
        prev_dnlp_brgt_range  = dnlp_alg_param.dnlp_brgt_range;
@@ -1791,10 +2587,10 @@ static void dnlp3_param_refrsh(void)
        prev_dnlp_lgst_dst        = ve_dnlp_lgst_dst;
        prev_dnlp_almst_wht = ve_dnlp_almst_wht;
 
-       prev_dnlp_pstgma_end = ve_dnlp_pstgma_end;
-       prev_dnlp_pstgma_ratio = ve_dnlp_pstgma_ratio;
-       prev_dnlp_pstgma_brghtrate = ve_dnlp_pstgma_brghtrate;
-       prev_dnlp_pstgma_brghtrat1 = ve_dnlp_pstgma_brghtrat1;
+       /* prev_dnlp_pstgma_end = ve_dnlp_pstgma_end; */
+       /* prev_dnlp_pstgma_ratio = ve_dnlp_pstgma_ratio; */
+       /* prev_dnlp_pstgma_brghtrate = ve_dnlp_pstgma_brghtrate; */
+       /* prev_dnlp_pstgma_brghtrat1 = ve_dnlp_pstgma_brghtrat1; */
 
        prev_dnlp_blkext_rate = dnlp_alg_param.dnlp_blkext_rate;
        prev_dnlp_whtext_rate = dnlp_alg_param.dnlp_whtext_rate;
@@ -1884,6 +2680,7 @@ static void dnlp_rfrsh_subgmma(void)
 static void dnlp_inhist_tiir(void)
 {
        int i = 0;
+       int j = 0;
        int nTmp = 0;
        static unsigned int pgmma0[65];
        /* local static variables for IIR filter */
@@ -1896,16 +2693,37 @@ static void dnlp_inhist_tiir(void)
                        nTmp = (nTmp >> dnlp_alg_param.dnlp_mvreflsh);
                        pre_0_gamma[i] = nTmp;
                }
-
+       /* data overrun error !
                nTmp = dnlp_bld_lvl * ve_dnlp_luma_sum + (RBASE >> 1);
                nTmp = nTmp + (RBASE - dnlp_bld_lvl) * luma_sum;
                nTmp = (nTmp >> dnlp_alg_param.dnlp_mvreflsh);
                ve_dnlp_luma_sum = nTmp;
+               */
        }
 
        for (i = 0; i < 65; i++)
                pgmma0[i] = pre_0_gamma[i];
        luma_sum = ve_dnlp_luma_sum;
+
+       if ((dnlp_printk >> 1) & 0x1) {
+               pr_dnlp_dbg("\n#### dnlp_inhist_tiir()-3: [\n");
+               for (j = 0; j < 4; j++) {
+                       i = j*16;
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                               pre_0_gamma[i], pre_0_gamma[i+1],
+                               pre_0_gamma[i+2], pre_0_gamma[i+3],
+                               pre_0_gamma[i+4], pre_0_gamma[i+5],
+                               pre_0_gamma[i+6], pre_0_gamma[i+7],
+                               pre_0_gamma[i+8], pre_0_gamma[i+9],
+                               pre_0_gamma[i+10], pre_0_gamma[i+11],
+                               pre_0_gamma[i+12], pre_0_gamma[i+13],
+                               pre_0_gamma[i+14], pre_0_gamma[i+15]);
+               }
+               pr_dnlp_dbg("  ]\n");
+               pr_dnlp_dbg("### ve_dnlp_luma_sum(iir) =%d\n",
+                       ve_dnlp_luma_sum);
+       }
+
 }
 
 /*0 ~ 65*/
@@ -1922,7 +2740,7 @@ static void dnlp_gmma_cuvs(unsigned int gmma_rate,
        unsigned int luma_avg4 = (lsft_avg >> dnlp_alg_param.dnlp_pavg_btsft);
 
        static unsigned int pgmma[65];
-       bool prt_flg = ((dnlp_printk >> 3) & 0x1);
+       unsigned short *glb_scurve;
 
        if (!ve_dnlp_luma_sum) {
                for (i = 0; i < 65; i++)
@@ -1933,7 +2751,6 @@ static void dnlp_gmma_cuvs(unsigned int gmma_rate,
        if ((dnlp_alg_param.dnlp_dbg_i2r >> 1) & 0x1)
                dnlp_rfrsh_subgmma();
 
-
        for (i = 0; i < 65; i++) {
                nTmp = (((256 - gmma_rate)*gma_scurve0[i] +
                        gma_scurve1[i]*gmma_rate + 128) >> 8); /* 0 ~1023 */
@@ -1946,8 +2763,10 @@ static void dnlp_gmma_cuvs(unsigned int gmma_rate,
                                (hgh_alpha*i<<4) + 8)>>4;
 
        if (debug_add_curve_en) {
+               glb_scurve = kzalloc(65*sizeof(unsigned short),
+                       GFP_KERNEL);
                nTmp = ((128 - glb_scurve_bld_rate)*nTmp +
-               glb_scurve_bld_rate*(glb_scurve[i]<<4) + 64)>>7;
+                       glb_scurve_bld_rate*(glb_scurve[i]<<4) + 64)>>7;
        }
 
                if (nTmp < 0)
@@ -1956,8 +2775,8 @@ static void dnlp_gmma_cuvs(unsigned int gmma_rate,
                        nTmp = 4095;
                gma_scurvet[i] = nTmp;
 
-               if (prt_flg)
-                       pr_info("Step D-1.z: gmma_cuvs_bld: [%02d] (s0)%4d (s1)%4d => (s)%4d\n",
+               if ((dnlp_printk >> 17) & 0x1)
+                       pr_dnlp_dbg("Step D-1.z: gmma_cuvs_bld: [%02d] (s0)%4d (s1)%4d => (s)%4d\n",
                                i, gma_scurve0[i], gma_scurve1[i],
                                gma_scurvet[i]);
        }
@@ -1977,23 +2796,18 @@ static void dnlp_gmma_cuvs(unsigned int gmma_rate,
        for (i = 0; i < 65; i++)
                gma_scurvet[i] = ((gma_scurvet[i] + 2) >> 2); /*1023*/
 
-       /* for debug */
-       CrtTstDat[24] = gmma_rate;
-       CrtTstDat[25] = low_alpha;
-       CrtTstDat[26] = hgh_alpha;
-       CrtTstDat[27] = lsft_avg;
-       if (prt_flg) {
-               pr_info("Step D-1.z: gmma_cuvs_bld: gma_scurvet cal paramets: lsft_avg=%d gmma_rate=%d low_alpha%d hgh_alpha=%d\n",
+       if ((dnlp_printk >> 17) & 0x1) {
+               pr_dnlp_dbg("Step D-1.z: gmma_cuvs_bld: gma_scurvet cal paramets: lsft_avg=%d gmma_rate=%d low_alpha%d hgh_alpha=%d\n",
                        lsft_avg, gmma_rate, low_alpha, hgh_alpha);
        }
 
 
        /* --draw curve */
-       if ((dnlp_printk>>14) & 0x1) {
-               pr_info("\n#### gma_s_bld: [\n");
+       if ((dnlp_printk>>17) & 0x1) {
+               pr_dnlp_dbg("\n#### gma_s_bld: [\n");
                for (j = 0; j < 4; j++) {
                        i = j*16;
-                       pr_info("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
                                gma_scurvet[i],
                                gma_scurvet[i+1],
                                gma_scurvet[i+2],
@@ -2010,8 +2824,8 @@ static void dnlp_gmma_cuvs(unsigned int gmma_rate,
                                gma_scurvet[i+13],
                                gma_scurvet[i+14],
                                gma_scurvet[i+15]);
-                       }
-               pr_info(" %d ]\n", gma_scurvet[64]);
+               }
+               pr_dnlp_dbg(" %d ]\n", gma_scurvet[64]);
 
        }
 
@@ -2027,7 +2841,16 @@ static void dnlp_clsh_sbld(unsigned int mtdbld_rate)
        int j = 0;
        int nTmp0 = 0;
 
-       static unsigned int pgmma[65];
+       static unsigned int pgmma[65] = {
+                 0,   16,   32,  48,  64,  80,  96, 112,
+                 128, 144, 160, 176, 192, 208, 224, 240,
+                 256, 272, 288, 304, 320, 336, 352, 368,
+                 384, 400, 416, 432, 448, 464, 480, 496,
+                 512, 528, 544, 560, 576, 592, 608, 624,
+                 640, 656, 672, 688, 704, 720, 736, 752,
+                 768, 784, 800, 816, 832, 848, 864, 880,
+                 896, 912, 928, 944, 960, 976, 992, 1008, 1024
+                 };
 
        if (!ve_dnlp_luma_sum) {
                for (i = 0; i < 65; i++)
@@ -2041,12 +2864,13 @@ static void dnlp_clsh_sbld(unsigned int mtdbld_rate)
                nTmp0 = (nTmp0 + 32)>>6; /* 0~1024 */
                clsh_scvbld[i] = nTmp0;
 
-               if ((dnlp_printk>>5) & 0x1)
-                       pr_info("Step D-2.z: clsh_sbld_bld(curvs): GmScurve=%d, clash_curve=%d => clsh_scvbld=%d\n",
-                               GmScurve[i], clash_curve[i], clsh_scvbld[i]);
+               if ((dnlp_printk >> 7) & 0x1)
+                       pr_dnlp_dbg("!!c_s_bld(): GmScurve:%d, clash_curve:%d => clsh_scvbld=%d(%d/64)\n",
+                               GmScurve[i], clash_curve[i],
+                               clsh_scvbld[i], mtdbld_rate);
        }
-       if ((dnlp_printk>>14) & 0x1)
-               pr_info(" @@@ in dnlp_clsh_sbld(before use): mtdbld_rate=%d, dnlp_bld_lvl=%d, dbg_i2r=%d, scn_chg=%d\n",
+       if ((dnlp_printk >> 7) & 0x1)
+               pr_dnlp_dbg(" @@@ dnlp_clsh_sbld(before use): mtdbld_rate=%d, dnlp_bld_lvl=%d, dbg_i2r=%d, scn_chg=%d\n",
                        mtdbld_rate, dnlp_bld_lvl,
                        dnlp_alg_param.dnlp_dbg_i2r, dnlp_scn_chg);
 
@@ -2055,8 +2879,8 @@ static void dnlp_clsh_sbld(unsigned int mtdbld_rate)
                        nTmp0 = dnlp_bld_lvl * clsh_scvbld[i] + (RBASE >> 1);
                        nTmp0 = nTmp0 + (RBASE - dnlp_bld_lvl) * pgmma[i];
                        nTmp0 = (nTmp0 >> dnlp_alg_param.dnlp_mvreflsh);
-                       if ((dnlp_printk>>5) & 0x1)
-                               pr_info("Step D-2.z: clsh_sbld_iir: clsh_scvbld=%d, pre=%d => crt=%d\n",
+                       if ((dnlp_printk >> 7) & 0x1)
+                               pr_dnlp_dbg("!! clsh_sbld_iir: clsh_scvbld=%d, pre=%d => crt=%d\n",
                                        clsh_scvbld[i], pgmma[i], nTmp0);
                        clsh_scvbld[i] = nTmp0;
                }
@@ -2065,11 +2889,11 @@ static void dnlp_clsh_sbld(unsigned int mtdbld_rate)
                pgmma[i] = clsh_scvbld[i]; /* 1023 */
 
        /* --draw curve */
-       if ((dnlp_printk>>14) & 0x1) {
-               pr_info("\n#### clsh_scv_bld: [\n");
+       if ((dnlp_printk >> 7) & 0x1) {
+               pr_dnlp_dbg("\n#### clsh_scv_bld: [\n");
                for (j = 0; j < 4; j++) {
                        i = j*16;
-                       pr_info("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
                                clsh_scvbld[i], clsh_scvbld[i+1],
                                clsh_scvbld[i+2], clsh_scvbld[i+3],
                                clsh_scvbld[i+4], clsh_scvbld[i+5],
@@ -2078,89 +2902,17 @@ static void dnlp_clsh_sbld(unsigned int mtdbld_rate)
                                clsh_scvbld[i+10], clsh_scvbld[i+11],
                                clsh_scvbld[i+12], clsh_scvbld[i+13],
                                clsh_scvbld[i+14], clsh_scvbld[i+15]);
-                       }
-               pr_info(" %d ]\n", clsh_scvbld[64]);
-
                }
-
-
+               pr_dnlp_dbg(" %d ]\n", clsh_scvbld[64]);
+       }
 
 }
 
 
 /*  in : blk_gma_rat[64] (from get_blk_gma_rat() ) */
 /* out : blk_gma_bld = blk_gma_crv + clsh_scvbld */
-static void dnlp_blkgma_bld(unsigned int *blk_gma_rat)
-{
-       int nT1 = 0;
-       int nTmp0 = 0;
-       int i = 0;
-       int j = 0;
-       static unsigned int pgmma[65];
-
-       if (!ve_dnlp_luma_sum) {
-               for (i = 0; i < 65; i++)
-                       pgmma[i] = (i << 4); /* 0 ~1024 */
-       }
-       if (dnlp_printk & 0x1) {
-               pr_info("\n****blk_gma_rat[64] in dnlp_blkgma_bld() before use:\n ");
-               for (i = 0; i < 64; i++)
-                       pr_info("%4d, ", blk_gma_rat[i]);
-       }
 
-       for (i = 0; i < 64; i++) {
-               nT1 = blk_gma_rat[i];
-               nTmp0 = clsh_scvbld[i];
 
-               nTmp0 = blk_gma_crv[i]*nT1 + nTmp0*(64 - nT1);
-               nTmp0 = (nTmp0+32)>>6; /* 0~1024 */
-               blk_gma_bld[i] = nTmp0;
-
-               if (debug_add_curve_en) {
-                       blk_gma_bld[i] =
-                       ((128 - glb_pst_gamma_bld_rate)*blk_gma_bld[i]
-                       + glb_pst_gamma_bld_rate*(glb_pst_gamma[i]<<2) + 64)>>7;
-               }
-
-               if ((dnlp_printk >> 7) & 0x1)
-                       pr_info("Step D-3.z: (clsh_scvbld)%04d, (blk_gma_crv)%04d * (blk_gma_rat)%04d => (blk_gma_bld)%04d\n",
-                               clsh_scvbld[i],  blk_gma_crv[i], nT1, nTmp0);
-       }
-       blk_gma_bld[64] = 1023;
-
-       if (!dnlp_scn_chg && ((dnlp_alg_param.dnlp_dbg_i2r >> 5) & 0x1))
-               for (i = 0; i < 65; i++) {
-                       nTmp0 = dnlp_bld_lvl * blk_gma_bld[i] + (RBASE >> 1);
-                       nTmp0 = nTmp0 + (RBASE - dnlp_bld_lvl) * pgmma[i];
-                       nTmp0 = (nTmp0 >> dnlp_alg_param.dnlp_mvreflsh);
-
-                       blk_gma_bld[i] = nTmp0;
-               }
-
-       for (i = 0; i < 65; i++)
-               pgmma[i] = blk_gma_bld[i]; /* 1023 */
-
-/* --draw curve */
-       if ((dnlp_printk>>14) & 0x1) {
-               pr_info("\n#### blk_gma_bld: [\n");
-               for (j = 0; j < 4; j++) {
-                       i = j*16;
-                       pr_info("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
-                               blk_gma_bld[i], blk_gma_bld[i+1],
-                               blk_gma_bld[i+2], blk_gma_bld[i+3],
-                               blk_gma_bld[i+4], blk_gma_bld[i+5],
-                               blk_gma_bld[i+6], blk_gma_bld[i+7],
-                               blk_gma_bld[i+8], blk_gma_bld[i+9],
-                               blk_gma_bld[i+10], blk_gma_bld[i+11],
-                               blk_gma_bld[i+12], blk_gma_bld[i+13],
-                               blk_gma_bld[i+14], blk_gma_bld[i+15]);
-               }
-               pr_info(" %d ]\n", blk_gma_bld[64]);
-
-       }
-
-
-}
 
 
 /* blkwht_ebld = blk_gma_bld + extension */
@@ -2179,16 +2931,24 @@ static void dnlp_blkwht_bld(int *o_curv, int *i_curv,
        int div4x[4], dist4, mid4, lft4, rgh4, divd;
        int margin64x, delta, st, ed, whtext_gain;
        /* local of bw extension */
-       static unsigned int bwext_curv[65];
+       static unsigned int bwext_curv[65] = {
+                 0,   16,   32,  48,  64,  80,  96, 112,
+                 128, 144, 160, 176, 192, 208, 224, 240,
+                 256, 272, 288, 304, 320, 336, 352, 368,
+                 384, 400, 416, 432, 448, 464, 480, 496,
+                 512, 528, 544, 560, 576, 592, 608, 624,
+                 640, 656, 672, 688, 704, 720, 736, 752,
+                 768, 784, 800, 816, 832, 848, 864, 880,
+                 896, 912, 928, 944, 960, 976, 992, 1008, 1024
+                 };
        /* get it parametered */
        int min_div4x = dnlp_alg_param.dnlp_bwext_div4x_min;
-       int prt_flg = ((dnlp_printk >> 9) & 0x1);
 
        /* adaptive calc on bin st and ed. */
        st = iRgnBgn - dnlp_alg_param.dnlp_blkext_ofst;   if (st < 0)   st = 0;
        ed = iRgnEnd + dnlp_alg_param.dnlp_whtext_ofst;   if (ed > 63)  ed = 63;
-       if (prt_flg) {
-               pr_info("stepD-4.z: blk/wht ext: blk_wht_ext[%d %d] + bright%d, Rgn=[%d %d], sted[%d %d]\n",
+       if ((dnlp_printk >> 8) & 0x1) {
+               pr_dnlp_dbg("stepD-4.z: blk/wht ext: blk_wht_ext[%d %d] + bright%d, Rgn=[%d %d], sted[%d %d]\n",
                        blk_wht_ext[0], blk_wht_ext[1],
                        bright, iRgnBgn, iRgnEnd, st, ed);
        }
@@ -2238,8 +2998,8 @@ static void dnlp_blkwht_bld(int *o_curv, int *i_curv,
        if (div4x[3] < min_div4x)
                div4x[3] = min_div4x;
 
-       if (prt_flg)
-               pr_info("bwext: lft4=%04d, mid4=%04d, rgh4=%4d, div4x=[%4d %4d %4d %4d]\n",
+       if ((dnlp_printk >> 8) & 0x1)
+               pr_dnlp_dbg("bwext: lft4=%04d, mid4=%04d, rgh4=%4d, div4x=[%4d %4d %4d %4d]\n",
                        lft4, mid4, rgh4, div4x[0],
                        div4x[1], div4x[2], div4x[3]);
 
@@ -2273,42 +3033,45 @@ static void dnlp_blkwht_bld(int *o_curv, int *i_curv,
                        delta = 0;
 
                /* nTmp += dnlp_brightness; */
+       if (BLE_en)
                tmp0 = bright + delta + (int)i_curv[i];
-               if (tmp0 < 0)
-                       tmp0 = 0;
-               else if (tmp0 > 1023)
-                       tmp0 = 1023;
-               else
-                       tmp0 = tmp0;
+       else
+               tmp0 = (int)i_curv[i];
 
-               /* IIR filter */
-               if (!dnlp_scn_chg && ((dnlp_alg_param.dnlp_dbg_i2r >> 6)
-                       & 0x1)) {
-                       tmp1 = dnlp_bld_lvl * tmp0 + (RBASE >> 1);
-                       tmp1 = tmp1 + (RBASE - dnlp_bld_lvl) * bwext_curv[i];
-                       tmp1 = (tmp1 >> dnlp_alg_param.dnlp_mvreflsh);
+       if (tmp0 < 0)
+               tmp0 = 0;
+       else if (tmp0 > 1023)
+               tmp0 = 1023;
+       else
+               tmp0 = tmp0;
 
-                       o_curv[i] = tmp1;
-               } else
+          /* IIR filter */
+       if (!dnlp_scn_chg && ((dnlp_alg_param.dnlp_dbg_i2r >> 6) & 0x1)) {
+               tmp1 = dnlp_bld_lvl * tmp0 + (RBASE >> 1);
+               tmp1 = tmp1 + (RBASE - dnlp_bld_lvl) * bwext_curv[i];
+               tmp1 = (tmp1 >> dnlp_alg_param.dnlp_mvreflsh);
+
+               o_curv[i] = tmp1;
+       } else
                        o_curv[i] = tmp0;
                 /* for next frame */
                 bwext_curv[i] = o_curv[i];
 
-               if (prt_flg)
-                       pr_info("Step D-4.z: bwext[%3d]: dst4=%04d, divd=%04d, mg64x=%4d, delt(%4d)+ icurv(%4d) + brght(%4d) => blkwht_ebld(%04d ) iir(%4d)\n",
-                               i, dist4, divd, margin64x, delta,
-                               i_curv[i], bright, tmp0, o_curv[i]);
+               if ((dnlp_printk >> 8) & 0x1)
+               pr_dnlp_dbg("Step D-4.z: bwext[%3d]: dst4=%04d, divd=%04d, mg64x=%4d, delt(%4d)+ icurv(%4d) + brght(%4d) => blkwht_ebld(%04d ) iir(%4d)\n",
+                       i, dist4, divd, margin64x, delta,
+                       i_curv[i], bright, tmp0, o_curv[i]);
 
        }
        /* o_curv[64] = 1023; */
        o_curv[0]  = 0;
        o_curv[64] = 1023;       /* output */
 
-       if ((dnlp_printk>>14) & 0x1) {
-               pr_info("\n#### BW_extent: [\n");
+       if ((dnlp_printk>>8) & 0x1) {
+               pr_dnlp_dbg("\n#### BW_extent: [\n");
                for (j = 0; j < 4; j++) {
                        i = j*16;
-                       pr_info("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
                                o_curv[i], o_curv[i+1],
                                o_curv[i+2], o_curv[i+3],
                                o_curv[i+4], o_curv[i+5],
@@ -2317,8 +3080,9 @@ static void dnlp_blkwht_bld(int *o_curv, int *i_curv,
                                o_curv[i+10], o_curv[i+11],
                                o_curv[i+12], o_curv[i+13],
                                o_curv[i+14], o_curv[i+15]);
-                       }
-               pr_info("]\n");
+               }
+               pr_dnlp_dbg("]\n");
+
        }
 
 
@@ -2353,8 +3117,8 @@ void patch_blk_wht(unsigned int *mMaxLst, unsigned int *mMaxIdx,
                *hgh_alpha -= (*hgh_alpha*nTmp>>4);
                *mtdbld_rate += (nTmp*(64 - *mtdbld_rate)>>4);
 
-               if (dnlp_printk)
-                       pr_info("Step C-1.y.2(after patch_blk_wht() ): special case:gmma_rate=%d low_alpha=%d hgh_alpha=%d mtdbld_rate=%d\n",
+               if ((dnlp_printk >> 18) & 0x01)
+                       pr_dnlp_dbg("## patch_blk_wht()-2: special case:gmma_rate=%d low_alpha=%d hgh_alpha=%d mtdbld_rate=%d\n",
                                *gmma_rate, *low_alpha,
                                *hgh_alpha, *mtdbld_rate);
        }
@@ -2383,7 +3147,6 @@ static void dnlp_params_hist(unsigned int *gmma_rate,
        int thgha = *hgh_alpha;
        int tmrat = *mtdbld_rate;
        static unsigned int xL[32];  /* for test */
-       bool prt_flag = ((dnlp_printk>>2)&0x1);
 
        nTmp = (luma_avg > 31) ? luma_avg-31 : 31-luma_avg;
        nTmp = (32 - nTmp + 2) >> 2;
@@ -2440,41 +3203,28 @@ static void dnlp_params_hist(unsigned int *gmma_rate,
                xL[i] = xL[i+1];
        xL[31] = luma_avg4;
 
-       if (prt_flag) {
-               pr_info("params_hist: gmma_rate=%d [%d %d %d %d %d %d %d],Tmp=%d luma_avg=%d (%d), alpha=[%d %d], mthd_rate=%d\n",
+       if ((dnlp_printk>>18)&0x1) {
+               pr_dnlp_dbg("params_hist: gmma_rate=%d [%d %d %d %d %d %d %d],Tmp=%d luma_avg=%d (%d), alpha=[%d %d], mthd_rate=%d\n",
                        (*gmma_rate), pgmma0[0][0], pgmma0[0][1],
                        pgmma0[0][2], pgmma0[0][3], pgmma0[0][4],
                        pgmma0[0][5], pgmma0[0][6], nTmp, luma_avg,
                        luma_avg4, *low_alpha, *hgh_alpha, *mtdbld_rate);
                i = 1;
-               pr_info("params_hist: low_alpha=%d [%d %d %d %d %d %d %d]\n",
+               pr_dnlp_dbg("params_hist: low_alpha=%d [%d %d %d %d %d %d %d]\n",
                        (*low_alpha), pgmma0[i][0], pgmma0[i][1],
                        pgmma0[i][2], pgmma0[i][3], pgmma0[i][4],
                        pgmma0[i][5], pgmma0[i][6]);
                i = 2;
-               pr_info("params_hist: hgh_alpha=%d [%d %d %d %d %d %d %d]\n",
+               pr_dnlp_dbg("params_hist: hgh_alpha=%d [%d %d %d %d %d %d %d]\n",
                        (*hgh_alpha), pgmma0[i][0], pgmma0[i][1],
                        pgmma0[i][2], pgmma0[i][3], pgmma0[i][4],
                        pgmma0[i][5], pgmma0[i][6]);
                i = 3;
-               pr_info("params_hist: mtdbld_rate=%d [%d %d %d %d %d %d %d]\n",
+               pr_dnlp_dbg("params_hist: mtdbld_rate=%d [%d %d %d %d %d %d %d]\n",
                        (*mtdbld_rate), pgmma0[i][0], pgmma0[i][1],
                        pgmma0[i][2], pgmma0[i][3], pgmma0[i][4],
                        pgmma0[i][5], pgmma0[i][6]);
 
-               /* pr_info("xL = [%d %d %d %d %d %d %d %d ;\n */
-               /*      %d %d %d %d %d %d %d %d ;\n */
-               /*      %d %d %d %d %d %d %d %d ;\n */
-               /*      %d %d %d %d %d %d %d %d ]\n", */
-               /*      xL[0],xL[1],xL[2],xL[3], */
-               /*      xL[4],xL[5],xL[6],xL[7], */
-               /*      xL[8],xL[9],xL[10],xL[11], */
-               /*      xL[12],xL[13],xL[14],xL[15], */
-               /*      xL[16],xL[17],xL[18],xL[19], */
-               /*      xL[20],xL[21],xL[22],xL[23], */
-               /*      xL[24],xL[25],xL[26],xL[27], */
-               /*      xL[28],xL[29],xL[30],xL[31]); */
-
        }
 }
 
@@ -2488,6 +3238,7 @@ static void dnlp_refine_bin0(int hstSum)
        unsigned int nTmp = 0;
        unsigned int nTmp0 = 0;
        unsigned int nsum = 0;
+       int i = 0;
        int j = 0;
 
        nTmp = (hstSum * dnlp_alg_param.dnlp_bbd_ratio_low + 128) >> 8;
@@ -2522,12 +3273,29 @@ static void dnlp_refine_bin0(int hstSum)
        }
 
        if (dnlp_printk & 0x1)
-               pr_info("Bin0 Refine: -%4d\n", nsum);
+               pr_dnlp_dbg("Bin0 Refine: -%4d\n", nsum);
 
        if (nsum >= pre_0_gamma[0])
                pre_0_gamma[0] = 0;
        else
                pre_0_gamma[0] = pre_0_gamma[0] - nsum;
+
+       if ((dnlp_printk >> 1) & 0x1) {
+               pr_dnlp_dbg("\n#### refine_bin0()-2: [\n");
+               for (j = 0; j < 4; j++) {
+                       i = j*16;
+                       pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                       pre_0_gamma[i], pre_0_gamma[i+1],
+                       pre_0_gamma[i+2], pre_0_gamma[i+3],
+                       pre_0_gamma[i+4], pre_0_gamma[i+5],
+                       pre_0_gamma[i+6], pre_0_gamma[i+7],
+                       pre_0_gamma[i+8], pre_0_gamma[i+9],
+                       pre_0_gamma[i+10], pre_0_gamma[i+11],
+                       pre_0_gamma[i+12], pre_0_gamma[i+13],
+                       pre_0_gamma[i+14], pre_0_gamma[i+15]);
+               }
+               pr_dnlp_dbg("  ]\n");
+       }
 }
 
 
@@ -2538,18 +3306,15 @@ static void dnlp_refine_bin0(int hstSum)
 /*     dnlp_pst_gmarat;    (for blk_gma_rat calc) */
 static void dnlp_adp_alpharate(unsigned int *lmh_avg,
        unsigned int *low_alpha, unsigned int *hgh_alpha,
-       unsigned int *pst_gmarat,
+       /*unsigned int *pst_gmarat, */
        unsigned int dnlp_lowrange, unsigned int dnlp_hghrange)
 {
        int nTmp = 0;
        int ndif = 0;
        int nlap = 0;
-       int nbrt0 = 0;
-       int nbrt1 = 0;
-       bool prt_flag = ((dnlp_printk>>2)&0x1);
 
-       if (prt_flag)
-               pr_info("Step C-1.y.1: adp_alpharateinput:lmh_avg= %3d %3d %3d %3d %3d, low_alpha=%2d, hgh_alpha=%2d\n",
+       if ((dnlp_printk>>17)&0x1)
+               pr_dnlp_dbg("input:lmh_avg= %3d %3d %3d %3d %3d, low_alpha=%2d, hgh_alpha=%2d\n",
                        lmh_avg[0], lmh_avg[1], lmh_avg[2],
                        lmh_avg[3], lmh_avg[4], *low_alpha, *hgh_alpha);
 
@@ -2561,8 +3326,8 @@ static void dnlp_adp_alpharate(unsigned int *lmh_avg,
                else
                        *low_alpha = *low_alpha - nTmp;
 
-               if (prt_flag)
-                       pr_info("Step C-1.y.1:out : low alpha-- (%3d) -> %2d\n",
+               if ((dnlp_printk>>17)&0x1)
+                       pr_dnlp_dbg("Step C-1.y.1:out : low alpha-- (%3d) -> %2d\n",
                                nTmp, *low_alpha);
        } else if (lmh_avg[3] > 64) { /* increase low alpha */
                ndif = lmh_avg[3] - 64;
@@ -2572,26 +3337,29 @@ static void dnlp_adp_alpharate(unsigned int *lmh_avg,
                else
                        *low_alpha += nlap;
 
-               if (prt_flag)
-                       pr_info("Step C-1.y.1:out :low alpha++ (%3d) -> %2d\n",
+               if ((dnlp_printk>>17)&0x1)
+                       pr_dnlp_dbg("Step C-1.y.1:out :low alpha++ (%3d) -> %2d\n",
                        nlap, *low_alpha);
 
-               if (lmh_avg[4] < 16) {
-                       nbrt0 = ve_dnlp_pstgma_brghtrat1 * (16 - lmh_avg[4]);
-                       nbrt0 = (nbrt0 + 8) >> 4;
-               }
-               nbrt1 = (ve_dnlp_pstgma_brghtrate * ndif + 16) >> 6;
 
-               nTmp = nbrt0 + nbrt1;
+       /*if (lmh_avg[4] < 16) { */
+       /*              nbrt0 = ve_dnlp_pstgma_brghtrat1 * (16 - lmh_avg[4]); */
+       /*              nbrt0 = (nbrt0 + 8) >> 4; */
+       /*      } */
+       /*      nbrt1 = (ve_dnlp_pstgma_brghtrate * ndif + 16) >> 6; */
 
-               if ((*pst_gmarat + nTmp) > 64)
-                       *pst_gmarat = 64;
-               else
-                       *pst_gmarat += nTmp;
+       /*      nTmp = nbrt0 + nbrt1; */
+
+       /*      if ((*pst_gmarat + nTmp) > 64) */
+       /*              *pst_gmarat = 64; */
+       /*      else */
+       /*              *pst_gmarat += nTmp; */
+
+       /*      if (prt_flag) */
+       /*              pr_info("Step C-1.y.1:out :  */
+       /* pstgma(+%2d +%2d)(%2d)\n", */
+       /*              nbrt0, nbrt1, *pst_gmarat); */
 
-               if (prt_flag)
-                       pr_info("Step C-1.y.1:out : pstgma(+%2d +%2d)(%2d)\n",
-                       nbrt0, nbrt1, *pst_gmarat);
        }
 
        if (lmh_avg[2] < 64 - dnlp_hghrange) { /* decrease hgh alpha */
@@ -2601,8 +3369,8 @@ static void dnlp_adp_alpharate(unsigned int *lmh_avg,
                        *hgh_alpha = 0;
                else
                        *hgh_alpha = *hgh_alpha - nTmp;
-               if (prt_flag)
-                       pr_info("Step C-1.y.1:out: hgh alpha-- (%3d) -> %2d\n",
+               if ((dnlp_printk>>17)&0x1)
+                       pr_dnlp_dbg("Step C-1.y.1:out: hgh alpha-- (%3d) -> %2d\n",
                                nTmp, *hgh_alpha);
        } else if (lmh_avg[2] > 63) { /* increase hgh alpha */
                nTmp = lmh_avg[2] - 63;
@@ -2612,8 +3380,8 @@ static void dnlp_adp_alpharate(unsigned int *lmh_avg,
                else
                        *hgh_alpha += nTmp;
 
-               if (prt_flag)
-                       pr_info("Step C-1.y.1: out: hgh alpha++ (%3d) -> %2d\n",
+               if ((dnlp_printk>>17)&0x1)
+                       pr_dnlp_dbg("Step C-1.y.1: out: hgh alpha++ (%3d) -> %2d\n",
                                nTmp, *hgh_alpha);
        }
 }
@@ -2673,27 +3441,31 @@ static int load_histogram(int *osamebin_num, struct vframe_s *vf,
        }
 
        if (dnlp_printk)
-               pr_info("\nRflsh%03d: %02d same bins hstSum(%d)\n",
+               pr_dnlp_dbg("\nRflsh%03d: %02d same bins hstSum(%d)\n",
                        nTstCnt, nT0, hstSum);
 
        /* output, same hist bin nums of this frame */
        *osamebin_num = nT0;
 
-       if ((dnlp_printk >> 14) & 0x1) {
-               pr_info("\n#### load_histogram(): [\n");
-               for (j = 0; j < 4; j++) {
-                       i = j*16;
-                       pr_info("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
-                       pre_0_gamma[i], pre_0_gamma[i+1],
-                       pre_0_gamma[i+2], pre_0_gamma[i+3],
-                       pre_0_gamma[i+4], pre_0_gamma[i+5],
-                       pre_0_gamma[i+6], pre_0_gamma[i+7],
-                       pre_0_gamma[i+8], pre_0_gamma[i+9],
-                       pre_0_gamma[i+10], pre_0_gamma[i+11],
-                       pre_0_gamma[i+12], pre_0_gamma[i+13],
-                       pre_0_gamma[i+14], pre_0_gamma[i+15]);
-               }
-               pr_info("  ]\n");
+       if (dnlp_printk) {
+               pr_dnlp_dbg("\n ==nTstCnt= %d====\n\n", nTstCnt);
+                       pr_dnlp_dbg("\n#### load_histogram()-1: [\n");
+                       for (j = 0; j < 4; j++) {
+                               i = j*16;
+                               pr_dnlp_dbg("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
+                                       pre_0_gamma[i], pre_0_gamma[i+1],
+                                       pre_0_gamma[i+2], pre_0_gamma[i+3],
+                                       pre_0_gamma[i+4], pre_0_gamma[i+5],
+                                       pre_0_gamma[i+6], pre_0_gamma[i+7],
+                                       pre_0_gamma[i+8], pre_0_gamma[i+9],
+                                       pre_0_gamma[i+10], pre_0_gamma[i+11],
+                                       pre_0_gamma[i+12], pre_0_gamma[i+13],
+                                       pre_0_gamma[i+14], pre_0_gamma[i+15]);
+                       }
+                       pr_dnlp_dbg("  ]\n");
+               pr_dnlp_dbg("### ===== hstSum = %d ======\n", hstSum);
+               pr_dnlp_dbg("### ve_dnlp_luma_sum(raw) = %d\n",
+                       ve_dnlp_luma_sum);
        }
 
        return hstSum;
@@ -2734,9 +3506,6 @@ int get_hist_bgn_end(unsigned int *iRgnBgn, unsigned int *iRgnEnd,
                *iRgnBgn = 0;
                *iRgnEnd = 64;
        }
-       /* before the adaptive range detection */
-       CrtTstDat[6] = *iRgnBgn;
-       CrtTstDat[7] = *iRgnEnd;
 
        if (dnlp_alg_param.dnlp_range_det) {
                if (hstBgn <= 4)
@@ -2744,8 +3513,6 @@ int get_hist_bgn_end(unsigned int *iRgnBgn, unsigned int *iRgnEnd,
                if (hstEnd >= 59)
                        *iRgnEnd = hstEnd;
        }
-       CrtTstDat[8] = *iRgnBgn;
-               CrtTstDat[9] = *iRgnEnd;
 
        /* outputs */
        *ihstBgn = hstBgn;      *ihstEnd = hstEnd;
@@ -2798,21 +3565,15 @@ unsigned int get_hist_max4_avgs(unsigned int *mMaxLst, unsigned int *mMaxIdx,
                                break;
                        }
                }
-               if ((dnlp_printk>>2) & 0x1)
-                       pr_info("#StepA-0.6(get_hist_max4_avgs):i=%2d, luma_sum=%6d, sum=%6d, low_lsum=%6d, low_lsum=%6d\n",
+
+               if ((dnlp_printk>>16) & 0x1)
+                       pr_dnlp_dbg("#(get_hist_max4_avgs)-1:i=%2d, luma_sum=%6d, sum=%6d, low_lsum=%6d, low_lsum=%6d\n",
                                i, luma_sum, sum, *low_lsum, *low_bsum);
-       }
 
-       /*invalid histgram: freeze dnlp curve*/
-       if ((*rgn_hstMax <= 55 || *rgn_hstSum == 0)
-               && (!dnlp_alg_param.dnlp_respond_flag)) {
-               if (dnlp_printk)
-                       pr_info("WARNING: invalid hist @ step 0.6: [%d %d %d]\n",
-                       (*rgn_hstMax), (*rgn_hstSum),
-                       dnlp_alg_param.dnlp_respond_flag);
-               return -1;
        }
 
+
+
        /* Filter to get the max4 bin */
        if (mMaxIdx[0] == 0)
                nTmp = (mMaxIdx[1] * 2) + mMaxIdx[2] + mMaxIdx[3];
@@ -2835,7 +3596,15 @@ unsigned int get_hist_max4_avgs(unsigned int *mMaxLst, unsigned int *mMaxIdx,
        *rgn_lumSum = luma_sum;/* [iRgnBgn, iRgnEnd],not use ihstRgn data */
        *rgn_hstSum = sum;
        *rgn_hstMax = max;
-
+       /*invalid histgram: freeze dnlp curve*/
+       if ((*rgn_hstMax <= 55 || *rgn_hstSum == 0)
+               && (!dnlp_alg_param.dnlp_respond_flag)) {
+               if (dnlp_printk)
+                       pr_dnlp_dbg("WARNING: invalid hist @ step 0.6: [%d %d %d]\n",
+                       (*rgn_hstMax), (*rgn_hstSum),
+                       dnlp_alg_param.dnlp_respond_flag);
+               return -1;
+       }
 
        /* get the averages and output */
        nTmp = (luma_sum << (2 + dnlp_alg_param.dnlp_pavg_btsft));
@@ -2844,42 +3613,12 @@ unsigned int get_hist_max4_avgs(unsigned int *mMaxLst, unsigned int *mMaxIdx,
        *luma_avg4 = ((*lsft_avg) >> dnlp_alg_param.dnlp_pavg_btsft);
        /* low range hist average binx4, equivalent to lmh_avg[0]???? */
        *low_lavg4 = 4*(*low_lsum)/((*low_bsum)+1);
-       if (dnlp_printk & 0x1) {
-               pr_info("step 0.6:nTmp=%d, btsft=%d, lsft_avg=%d, luma_avg4=%d; low_lavg4=4*(%d by %d)= %d",
+       if ((dnlp_printk>>16) & 0x1) {
+               pr_dnlp_dbg("#(get_hist_max4_avgs)-2:luma_sum(shft)=%d, btsft=%d, lsft_avg=%d, luma_avg4=%d; low_lavg4=4*(%d by %d)= %d",
                        nTmp, dnlp_alg_param.dnlp_pavg_btsft,  (*lsft_avg),
                        (*luma_avg4),  (*low_lsum),
                        (*low_bsum), (*low_lavg4));
        }
-       if ((*luma_avg4) < 0 || (*luma_avg4) > 256
-               || luma_sum < 0 || (*lsft_avg) < 0) {
-               pr_info("ERROR 0.6b: :nTmp=%d, btsft=%d, lsft_avg=%d, luma_avg4=%d; low_lavg4=4*(%d by %d)= %d",
-                       nTmp, dnlp_alg_param.dnlp_pavg_btsft, (*lsft_avg),
-                       (*luma_avg4),  (*low_lsum),
-                       (*low_bsum), (*low_lavg4));
-       }
-
-       CrtTstDat[10] = *lsft_avg;
-       CrtTstDat[11] = *luma_avg4;
-       if (dnlp_printk & 0x1) {
-               pr_info("Step 0.60: #Bins:      Pre-hist => Crt-Hist\n");
-               for (i = 0; i < 64; i++)
-                       pr_info("[%03d,%03d): %05d => %05d\n",
-                               4*i, 4*(i+1),
-                               pre_1_gamma[i], pre_0_gamma[i]);
-
-               pr_info("step 0.61:Max: %04d(%d) > %04d(%d) > %04d(%d) > %04d(%d) max=%d, max4_sum=%6d\n",
-               mMaxLst[0], mMaxIdx[0], mMaxLst[1], mMaxIdx[1],
-               mMaxLst[2], mMaxIdx[2], mMaxLst[3], mMaxIdx[3], max, *max4_sum);
-               pr_info("                  ve_dnlp_luma_sum=%d, Rgn_BgnEnd= [%d %d], ihst_BgnEnd =[%d %d]\n",
-                                ve_dnlp_luma_sum, iRgnBgn,
-                                iRgnEnd, ihstBgn, ihstEnd);
-
-               pr_info(" rgn_lumSum by rgn_hstSum =%d by %d => lsft_avg=%d luma_avg4= %d histMax=%d\n",
-                       luma_sum, sum,  *lsft_avg, *luma_avg4, *rgn_hstMax);
-               pr_info(" low_lsum by low_bsum   =%d by %d => low_lavg4=%d\n",
-                       *low_lsum, (*low_bsum), (*low_lavg4));
-       }
-
        return 0;
 }
 
@@ -2902,20 +3641,21 @@ unsigned int cal_hist_var(unsigned int rgn_hstSum, unsigned int luma_avg4,
                dif =  i4 > luma_avg4 ? (i4 - luma_avg4):(luma_avg4 - i4);
                dif = (dif*dif) >> 4;
                if ((dnlp_printk >> 13) & 0x1)
-                       pr_info("@@@cal_hist_var():i4 = %d, luma_avg4 = %d, dif*dif = %4d,      ",
+                       pr_dnlp_dbg("@@@cal_hist_var():i4 = %d, luma_avg4 = %d, dif*dif = %4d,  ",
                                i4, luma_avg4, dif);
 
                dif = dif * pre_0_gamma[i];/* num of i */
                dif_sum += dif;
 
-               if ((dnlp_printk>>13) & 0x1)
-                       pr_info("@@@cal_hist_var():(continue) pre_0_gamma[i] = %4d, dif_sum = %4d\n ",
+               if ((dnlp_printk >> 13) & 0x1)
+                       pr_dnlp_dbg("cal_hist_var():pre_0_gamma[i] = %4d, dif_sum = %4d\n ",
                                pre_0_gamma[i], dif_sum);
        }
        var = dif_sum/((rgn_hstSum>>3)+1);/* mse */
 
-       if ((dnlp_printk>>14) & 0x1)
-               pr_info("@@@cal_hist_var():  var = %d(before clip)\n ", var);
+       if ((dnlp_printk >> 13) & 0x1)
+               pr_dnlp_dbg("@@@cal_hist_var():  var = %d(before clip)\n ",
+                       var);
 
        /* var = (var>2048)? 2048: var; */
        var = (var > 8192) ? 8192 : var;
@@ -2932,8 +3672,8 @@ unsigned int cal_hist_var(unsigned int rgn_hstSum, unsigned int luma_avg4,
        /* ramp 2500 325 */
        /* BW50% xx 1024 */
 
-       if ((dnlp_printk>>14) & 0x1)
-               pr_info("@@@cal_hist_var(): iRgn=[%d %d], luma_avg4= %d, rgn_hstSum = %d,=> var = %d(u8 %d)\n ",
+       if (dnlp_printk)
+               pr_dnlp_dbg("+++ cal_hist_var(): iRgn=[%d %d], luma_avg4= %d, rgn_hstSum = %d,=> var = %d(u8 %d)\n ",
                iRgnBgn, iRgnEnd, luma_avg4, rgn_hstSum, var, var/8);
 
        return var;
@@ -3035,49 +3775,11 @@ void get_lmh_avg(unsigned int *lmh_sum, unsigned int *lmh_avg,
                (lmh_sum[2] + 1);
 }
 
-/*    function:  get the blend coef between blk_gmma and clsh_scvbld */
-/*         in: *dnlp_pstgma_ratio, dnlp_pst_gmarat */
+/*     function:  get the blend coef between blk_gmma and clsh_scvbld */
+/*             in: *dnlp_pstgma_ratio, dnlp_pst_gmarat */
 /*(return)output: blk_gma_rat[0~63],  */
-void get_blk_gma_rat(unsigned int *blk_gma_rat, unsigned int *dnlp_pstgma_ratio,
-       unsigned int dnlp_pst_gmarat)
-{
-       unsigned int i, nT1;
-
-       if (*dnlp_pstgma_ratio > dnlp_pst_gmarat)
-               *dnlp_pstgma_ratio = dnlp_pst_gmarat;/*  */
-       if (dnlp_printk & 0x1)
-               pr_info("\n ****** inside get_blk_gma_rat():\n ");
-       for (i = 0; i < 64; i++) {
-               nT1 = dnlp_pst_gmarat;
-
-               if (i > ve_dnlp_pstgma_end)
-                       nT1 = *dnlp_pstgma_ratio;
-               else if (ve_dnlp_pstgma_end > 0) {
-                       nT1 = dnlp_pst_gmarat - (*dnlp_pstgma_ratio);
-                       nT1 = nT1 * i + (ve_dnlp_pstgma_end >> 1);
-                       nT1 = nT1 / (ve_dnlp_pstgma_end+1);/* can't be 0 */
-                       nT1 = dnlp_pst_gmarat - nT1;
-               }
-
-               if (dnlp_alg_param.dnlp_limit_rng && (i <= 4)) {
-                       nT1 = i * dnlp_pst_gmarat;
-                       nT1 = ((nT1 + 2) >> 2);
-               }
-
-               if (nT1 < 0)
-                       nT1 = 0;
-               else if (nT1 > 64)
-                       nT1 = 64;
-
-               blk_gma_rat[i] = nT1;
-
-               if (dnlp_printk & 0x1)
-                       pr_info("%4d ", blk_gma_rat[i]);
-       }
 
 
-}
-
 
 /* dnlp saturation compensations */
 /* input: ve_dnlp_tgt[]; */
@@ -3102,8 +3804,8 @@ int dnlp_sat_compensation(void)
        if (nTmp0 < nTmp)
                nTmp = nTmp0;
 
-       if (((dnlp_printk>>10)&0x1))
-               pr_info("#sat_comp: pre(%3d) => %5d / %3d => %3d cur(%3d)\n",
+       if (((dnlp_printk>>11) & 0x1))
+               pr_dnlp_dbg("#sat_comp: pre(%3d) => %5d / %3d => %3d cur(%3d)\n",
                        pre_stur, nT0, nT1, nTmp0, nTmp);
 
        if (dnlp_alg_param.dnlp_set_saturtn == 0) {
@@ -3132,9 +3834,18 @@ int dnlp_sat_compensation(void)
 void curv_selection(void)
 {
        int i, nTmp0;
-       static unsigned int pst_0_gamma[65];
-
-       bool prt_flg = ((dnlp_printk >> 10) & 0x1);
+       static unsigned int pst_0_gamma[65] = {
+                                0, 4, 8, 12, 16, 20, 24, 28, 32, 36,
+                               40, 44, 48, 52, 56, 60, 64, 68, 72, 76,
+                               80, 84, 88, 92, 96, 100, 104,
+                               108, 112, 116, 120, 124, 128,
+                               132, 136, 140, 144, 148, 152,
+                               156, 160, 164, 168, 172, 176,
+                               180, 184, 188, 192, 196, 200,
+                               204, 208, 212, 216, 220, 224,
+                               228, 232, 236, 240, 244, 248,
+                               252, 255
+                       };
 
        if (!ve_dnlp_luma_sum) {
                for (i = 0; i < 65; i++)
@@ -3172,19 +3883,6 @@ void curv_selection(void)
                nTmp0 = ((i)<<4) +
                        (((nTmp0-((i)<<4)) *
                                ((int)dnlp_alg_param.dnlp_final_gain) + 4)/8);
-               #if 0
-               /* tiir filter */
-               nTmp0 = dnlp_bld_lvl * nTmp0 + (RBASE >> 1); /* 1024 */
-               nTmp0 = nTmp0 + (RBASE - dnlp_bld_lvl) * pst_0_gamma[i];
-               nTmp0 = (nTmp0 >> dnlp_alg_param.dnlp_mvreflsh); /* 0~4096 */
-
-               if (nTmp0 < 0)
-                       nTmp0 = 0;
-               else if (nTmp0 > 1023)
-                       nTmp0 = 1023;
-
-               pst_0_gamma[i] = nTmp0;
-               #endif
 
                nTmp0 = ((nTmp0 + 2) >> 2);
 
@@ -3193,23 +3891,32 @@ void curv_selection(void)
                else if (nTmp0 < 0)
                        nTmp0 = 0;
 
-               if (prt_flg) {
-                       pr_info("curv_sel[%02d]:  (s0 %4d s1 %4d) (s)%4d  (c)%4d  (cs)%4d  (blk_gma)%4d-(blk_bld)%4d  (blkwht_ebld)%4d  => (final) %3d\n",
-                               i, gma_scurve0[i], gma_scurve1[i],
-                               gma_scurvet[i], clash_curve[i],
+               if ((dnlp_printk >> 10) & 0x1) {
+                       pr_dnlp_dbg("curv_sel[%02d]:(var_s)%4d, (c)%4d, (cs)%4d, (blk_gma)%4d, (blk_bld)%4d, (blkwht_ebld)%4d   => (final) %3d\n",
+                               i, GmScurve[i], clash_curve[i],
                                clsh_scvbld[i], blk_gma_crv[i],
                                blk_gma_bld[i], blkwht_ebld[i], nTmp0);
                }
 
                ve_dnlp_tgt[i] = nTmp0;
        }
+       if ((dnlp_printk >> 10) & 0x1)
+               pr_dnlp_dbg("## final iir param:dnlp_scn_chg=%d, menu_chg_en =%d,dnlp_alg_param.dnlp_dbg_i2r = %d\n",
+                       dnlp_scn_chg, menu_chg_en, dnlp_alg_param.dnlp_dbg_i2r);
 
        if (((!dnlp_scn_chg) && (!menu_chg_en)) &&
                ((dnlp_alg_param.dnlp_dbg_i2r >> 4) & 0x1)) {
                for (i = 0; i < 65; i++) {
-                       nTmp0 = dnlp_bld_lvl * ve_dnlp_tgt[i] + (RBASE >> 1);
+                       nTmp0 = dnlp_bld_lvl * ve_dnlp_tgt[i] +
+                               (RBASE >> 1);/*u8*/
                        nTmp0 = nTmp0 + (RBASE - dnlp_bld_lvl) * pst_0_gamma[i];
                        nTmp0 = (nTmp0 >> dnlp_alg_param.dnlp_mvreflsh);
+
+       if ((dnlp_printk >> 10) & 0x1)
+               pr_dnlp_dbg("### final curv(iir):[%d],ve_dnlp_tgt:%d, pst_0_gamma:%d,=> O_curv:%d (%d/%d,%d)\n ",
+                       i, ve_dnlp_tgt[i], pst_0_gamma[i], nTmp0, dnlp_bld_lvl,
+                       RBASE, dnlp_alg_param.dnlp_mvreflsh);
+
                        /*clip*/
                        if (nTmp0 < 0)
                                nTmp0 = 0;
@@ -3218,9 +3925,12 @@ void curv_selection(void)
 
                        ve_dnlp_tgt[i] = nTmp0;
                }
-               menu_chg_en = 0;
+
        }
 
+       if (menu_chg_en == 1)
+               menu_chg_en = 0;
+
        for (i = 0; i < 65; i++)
                pst_0_gamma[i] = ve_dnlp_tgt[i];
 }
@@ -3233,24 +3943,18 @@ void ve_dnlp_calculate_tgtx_v3(struct vframe_s *vf)
        struct vframe_prop_s *p = &vf->prop;
 
        /* curve blending coef parms */
-       /* 1- scurve0 + scurve1 = gma_scurvet */
-       unsigned int gmma_rate = (unsigned int)ve_dnlp_gmma_rate;
+       /* 1- scurve0 + scurve1 = gma_scurvet : no use anymaore.180227*/
+       unsigned int gmma_rate = (unsigned int)ve_dnlp_gmma_rate;/*no use*/
        /* gma_scurvet with 45 dgree in low luma */
        unsigned int low_alpha = (unsigned int)ve_dnlp_lowalpha_v3;
        /* gma_scurvet with 45 dgree in high luma */
        unsigned int hgh_alpha  = (unsigned int)ve_dnlp_hghalpha_v3;
+
+
        /* 2- gma_scurvet + clahe_curve = clsh_scvbld */
        unsigned int mtdbld_rate  =
                (unsigned int)dnlp_alg_param.dnlp_mtdbld_rate;
 
-       /* for calc blk_gma_rat */
-       unsigned int dnlp_pst_gmarat = (unsigned int)ve_dnlp_pst_gmarat;
-       /* for calc blk_gma_rat, old (ve_dnlp_bkgert) */
-       unsigned int dnlp_pstgma_ratio = (unsigned int)ve_dnlp_pstgma_ratio;
-       /* Black gamma rate: global -> auto local ??*/
-       unsigned int blk_gma_rat[64];
-       /* 3- blk_gma_crv + clsh_scvbld = blk_gma_bld */
-
        /* clash, s curve  begin & end */
        unsigned int clashBgn = (unsigned int)dnlp_alg_param.dnlp_clashBgn;
        unsigned int clashEnd = (unsigned int)dnlp_alg_param.dnlp_clashEnd;
@@ -3264,12 +3968,11 @@ void ve_dnlp_calculate_tgtx_v3(struct vframe_s *vf)
        unsigned int dnlp_hghrange = (unsigned int)dnlp_alg_param.dnlp_hghrange;
        unsigned int dnlp_auto_rng = 0;
 
-       static unsigned int nTstCnt;
-       bool prt_flg = ((dnlp_printk) & 0x1);
+       /*static unsigned int nTstCnt;*/
 
        /*---- some intermediate variables ----*/
        unsigned int raw_hst_sum, iir_hstSum;
-       int i, nT0, smbin_num, nTmp0, dnlp_brightness, var, sat_compens;
+       int i, smbin_num, nTmp0, dnlp_brightness, var, sat_compens;
 
        /* get_hist_max4_avgs () */
        /* only used in get_hist_max4_avgs */
@@ -3295,10 +3998,8 @@ void ve_dnlp_calculate_tgtx_v3(struct vframe_s *vf)
 
        /* black white extension params */
        int blk_wht_ext[2] = {0, 0};   /* u10 precision, */
-       /*--------------------------------------------------*/
-
-       /* pre-define gamma curve,1.2 & 1.8 */
 
+       /* pre-define gamma curve,1.2 & 1.8 no use anymaore.180227 */
        unsigned int rGm1p2[] = {
                0,      2,      4,      7,      9, 12, 15, 18, 21, 24,
                28, 31, 34, 38, 41, 45, 49, 52, 56, 60,
@@ -3308,8 +4009,6 @@ void ve_dnlp_calculate_tgtx_v3(struct vframe_s *vf)
                172, 177, 181, 186, 190, 195, 200, 204,
                209, 213, 218, 223, 227, 232, 237, 242,
                246, 251, 255};
-
-
        /* 2.0 for full range */
        unsigned int rGm1p8[] = {
                0,      0,      0,      1,      1,      2,      2, 3,  4,  5,
@@ -3321,7 +4020,7 @@ void ve_dnlp_calculate_tgtx_v3(struct vframe_s *vf)
                196, 203, 210, 218, 225, 233, 240, 248, 255};
 
 
-       /* -------some pre-processing---------------------------// */
+       /* -------some pre-processing--------------------------- */
 
        /* calc iir-coef params */
        if (dnlp_alg_param.dnlp_mvreflsh < 1)
@@ -3343,22 +4042,20 @@ void ve_dnlp_calculate_tgtx_v3(struct vframe_s *vf)
                clashEnd  = 64;
        if (clashEnd < 49)
                clashEnd  = 49;
-       for (i = 0; i < 28; i++)
-               PreTstDat[i] = CrtTstDat[i];
 
        /* old historic luma sum*/
-       /*sum_b = ve_dnlp_luma_sum;*/
        /* v3 historic luma sum */
        if (hist_sel)
                ve_dnlp_luma_sum = p->hist.vpp_luma_sum;
        else
                ve_dnlp_luma_sum = p->hist.luma_sum;
 
-       /* counter the calling function --for what ?? */
+       /* counter the calling function  */
        nTstCnt++;
-       if (nTstCnt > 240)
+       if (nTstCnt > 16384)
                nTstCnt = 0;
 
+
        /*--------------------------------------------------------*/
 
 
@@ -3367,46 +4064,46 @@ void ve_dnlp_calculate_tgtx_v3(struct vframe_s *vf)
        /*step 0.0 load the histogram*/
        /* nTstCnt not use */
        raw_hst_sum = load_histogram(&smbin_num, vf, hist_sel, nTstCnt);
-       /*step 0.1 all the same histogram as last frame, freeze DNLP */
+
+       /*step 0.01 all the same histogram as last frame, freeze DNLP */
        if (smbin_num == 64 &&
                dnlp_alg_param.dnlp_smhist_ck &&
                (!dnlp_alg_param.dnlp_respond_flag)) {
                if (dnlp_printk & 0x1)
-                       pr_info("WARNING: invalid hist @ step 0.10\n");
+                       pr_dnlp_dbg("WARNING: invalid hist @ step 0.10\n");
                return;
        }
-       /* v3 luma sum is 0,something is wrong,freeze dnlp curve*/
+
+       /* step 0.02 v3 luma sum is 0,something is wrong,freeze dnlp curve*/
        if (!ve_dnlp_luma_sum) {
                dnlp_scn_chg = 1;
                if (dnlp_printk & 0x1)
-                       pr_info("WARNING: invalid hist @ step 0.11\n");
+                       pr_dnlp_dbg("WARNING: invalid hist @ step 0.11\n");
                return;
        }
 
-       /*step 0.2 calc T-IIR blending coef base on historic avg_luma change */
-       dnlp_bld_lvl = curve_rfrsh_chk(raw_hst_sum, RBASE);
-       if (dnlp_printk & 0x1)
-               pr_info("Step A-0.2(after curve_rfrsh_chk()): dnlp_bld_lvl = %d\n",
-                       dnlp_bld_lvl);
+       /*step 0.1 calc T-IIR blending coef base on historic avg_luma change */
+       dnlp_bld_lvl = curve_rfrsh_chk_v2(raw_hst_sum, RBASE);
+       if (dnlp_printk)
+               pr_dnlp_dbg("+++ curve_rfrsh_chk(): dnlp_bld_lvl = %d,scn_chg = %d\n",
+                       dnlp_bld_lvl, dnlp_scn_chg);
 
-       /*step 0.3 black bord detection,*/
-       /*s and histogram clipping,*/
-        /*schange the pre_0_gamma, TBD */
+       /*step 0.2 black bord detection,and histogram clipping*/
        dnlp_refine_bin0(raw_hst_sum);
 
-       /*step 0.4 histogram and luma_sum IIR filters in time domain, */
+       /*step 0.3 histogram and luma_sum IIR filters in time domain, */
        dnlp_inhist_tiir();
 
-       /*step 0.5 get the hist begin and end bins */
+       /*step 0.4 get the hist begin and end bins */
        iir_hstSum = get_hist_bgn_end(&dnlp_alg_param.dnlp_iRgnBgn,
                &dnlp_alg_param.dnlp_iRgnEnd,
                &ihstBgn, &ihstEnd, raw_hst_sum);
-       if (dnlp_printk & 0x1)
-               pr_info("Step A-0.5(after get_hist_bgn_end()): iRgnBgn = %d,iRgnEnd = %d,ihstBgn = %d,ihstEnd = %d\n",
+       if ((dnlp_printk>>2) & 0x1)
+               pr_dnlp_dbg("## hist_bgn_end(): iRgnBgn_end= [%d,%d],ihstBgn_end= [%d,%d]\n",
                        dnlp_alg_param.dnlp_iRgnBgn,
                        dnlp_alg_param.dnlp_iRgnEnd, ihstBgn, ihstEnd);
 
-       /*step 0.6 Get the maximum 4 bins and averages of the histogram*/
+       /*step 0.5 Get the maximum 4 bins and averages of the histogram*/
        if (get_hist_max4_avgs(&mMaxLst[0],
                &mMaxIdx[0], &low_lsum, &low_sum,
                &rgn_lumSum, &rgn_hstSum, &rgn_hstMax,
@@ -3416,10 +4113,13 @@ void ve_dnlp_calculate_tgtx_v3(struct vframe_s *vf)
                ihstBgn, ihstEnd) < 0)
                return; /* rgn_vehstSum ? */
 
-       luma_avg  = (luma_avg4>>2);
+       luma_avg  = (luma_avg4 >> 2);
+       if (dnlp_printk)
+               pr_dnlp_dbg("+++ max4_avgs()-1:rgn_lumSum(ve_dnlp_luma_sum)= %d(%d),luma_avg4(raw) = %d\n",
+               rgn_lumSum, ve_dnlp_luma_sum, luma_avg4);
 
-       if (luma_avg4 < 0 || luma_avg4 > 256)
-               pr_info("ERROR 0.6: lsft_avg=%d luma_avg=%d (%d), low_lavg4=%d, BE[%d %d %d %d], max4[%d %d %d %d][%d %d %d %d],rgn[%d %d %d], low<%d %d>\n",
+       if ((dnlp_printk>>2) & 0x1)
+               pr_dnlp_dbg("## get_hist_max4_avgs():lsft_avg=%d luma_avg=%d (%d), low_lavg4=%d,BE[%d %d %d %d], max4[%d %d %d %d][%d %d %d %d],rgn[%d %d %d], low<%d %d>\n",
                        lsft_avg, luma_avg, luma_avg4, low_lavg4,
                        dnlp_alg_param.dnlp_iRgnBgn,
                        dnlp_alg_param.dnlp_iRgnEnd,
@@ -3429,67 +4129,39 @@ void ve_dnlp_calculate_tgtx_v3(struct vframe_s *vf)
                        mMaxIdx[2], mMaxIdx[3], rgn_lumSum,
                        rgn_hstSum, rgn_hstMax, low_lsum, low_sum);
 
-       if (dnlp_printk)
-               pr_info("Step A- 0.6(after get_hist_max4_avgs()): lsft_avg=%d luma_avg=%d (%d), low_lavg4=%d, BE[%d %d %d %d], max4[%d %d %d %d][%d %d %d %d],rgn[%d %d %d], low<%d %d>\n",
-                       lsft_avg, luma_avg, luma_avg4, low_lavg4,
-                               dnlp_alg_param.dnlp_iRgnBgn,
-                               dnlp_alg_param.dnlp_iRgnEnd,
-                               ihstBgn, ihstEnd,
-                               mMaxLst[0], mMaxLst[1], mMaxLst[2],
-                               mMaxLst[3], mMaxIdx[0], mMaxIdx[1],
-                               mMaxIdx[2], mMaxIdx[3], rgn_lumSum,
-                               rgn_hstSum, rgn_hstMax, low_lsum, low_sum);
-
-       /*step 0.7 IIR filter of lsft_avg in time domain,(for getting s0,s1)*/
+       /*step 0.6 IIR filter of lsft_avg in time domain,(for getting s0,s1,*/
+       /*         no use anymaore. 180227)*/
        lsft_avg  = cal_hst_shft_avg(lsft_avg);
        if (ve_dnlp_scv_dbg != 0) {
                nTmp0 = lsft_avg + 16 * ve_dnlp_scv_dbg;
                lsft_avg = (nTmp0 < 0) ? 0 : nTmp0;
        }
-
-       if (dnlp_printk & 0x1)
-               pr_info("Step A-0.7(after cal_hst_shft_avg()): lsft_avg = %d\n",
+       if ((dnlp_printk>>2) & 0x1)
+               pr_dnlp_dbg("after cal_hst_shft_avg(): lsft_avg = %d\n",
                        lsft_avg);
 
-
-       /* Step 4.x.2 in Step B */
-       /*step 0.8 calclulate adaptive brightness plus
-        *based on luma_avg4 and low_lavg4
-        */
+       /*step 0.7 calclulate brightness  based on luma_avg4 and low_lavg4*/
        dnlp_brightness = cal_brght_plus(luma_avg4, low_lavg4);
-       if (dnlp_printk) {
-               pr_info("step A-0.8(after cal_brght_plus()): [avg1]= luma_avg=(%02d %02d) lsft_avg=(%4d) gmma_rate=%d, low_sum=%6d, low_lavg4=%2d, dnlp_brightness=%d,\n",
-                       luma_avg, luma_avg4, lsft_avg, gmma_rate,
-                       low_sum, low_lavg4, dnlp_brightness);
-
+       if ((dnlp_printk>>2) & 0x1) {
+               pr_dnlp_dbg("cal_brght_plus():luma_avg4=%02d, low_lavg4=%4d,brightness=%d,\n",
+                       luma_avg4, low_lavg4, dnlp_brightness);
        }
 
-       /* ----------------------------------- */
-       /*step 0.9 150918 for 32-step luma pattern*/
+       /*step 0.8 150918 for 32-step luma pattern, not work180227*/
        if (dnlp_alg_param.dnlp_dbg_adjavg)
                luma_avg4 = AdjHistAvg(luma_avg4, ihstEnd);
-       /* if (luma_avg4<0 || luma_avg4>256)
-        *pr_info("ERROR 0.8: lsft_avg=%d luma_avg=%d (%d),
-        * low_lavg4=%d, BE[%d %d %d %d]\n",lsft_avg, luma_avg,
-        * luma_avg4, low_lavg4,iRgnBgn, iRgnEnd, ihstBgn, ihstEnd);
-        */
 
-       /*step 0.a do IIR filter of luma_avg4 on time domain*/
+       /*step 0.9 do IIR filter of luma_avg4 on time domain*/
        luma_avg4 = cal_hist_avg(luma_avg4);  /* IIR filtered in time domain */
+
        /*for read in tool*/
        ro_luma_avg4 = luma_avg4;
        luma_avg = (luma_avg4>>2);
-       if (dnlp_printk & 0x1)
-               pr_info("Step A-0.a(after AdjHistAvg(),cal_hist_avg()): luma_avg4 = %d(iir), luma_avg = %d\n",
-               luma_avg4, luma_avg);
-       /* if (luma_avg4<0 || luma_avg4>256)
-        *pr_info("ERROR 0.9: lsft_avg=%d luma_avg=%d (%d),
-        * low_lavg4=%d, BE[%d %d %d %d]\n",lsft_avg,
-        * luma_avg,luma_avg4, low_lavg4,iRgnBgn,
-        * iRgnEnd, ihstBgn, ihstEnd);
-        */
+       if (dnlp_printk)
+               pr_dnlp_dbg("+++ cal_hist_avg(iir)-2: luma_avg4 = %d(iir)\n",
+               luma_avg4);
 
-       /* cal hist variance */
+       /*step 0.a cal hist variance */
        var = cal_hist_var(rgn_hstSum, luma_avg4,
                dnlp_alg_param.dnlp_iRgnBgn, dnlp_alg_param.dnlp_iRgnEnd);
 
@@ -3499,227 +4171,116 @@ void ve_dnlp_calculate_tgtx_v3(struct vframe_s *vf)
        get_lmh_avg(&lmh_sum[0], &lmh_avg[0], dnlp_lowrange,
                dnlp_hghrange, dnlp_alg_param.dnlp_iRgnBgn,
                dnlp_alg_param.dnlp_iRgnEnd);
-       if (dnlp_printk) {
-               pr_info("step A- 0.b(after get_auto_range() & get_lmh_avg() ):\n auto_rgn=%d, dnlp_lowrange=%d, dnlp_hghrange=%d, lmh_sum=[%d %d %d], lmh_avg=[%d %d %d %d %d]\n",
+       if ((dnlp_printk >> 2) & 0x1) {
+               pr_dnlp_dbg("## get_lmh_avg():auto_rgn=%d, lowrange=%d, hghrange=%d,lmh_sum=[%d %d %d], lmh_avg=[%d %d %d %d %d]\n",
                        dnlp_auto_rng, dnlp_lowrange, dnlp_hghrange,
                        lmh_sum[0], lmh_sum[1], lmh_sum[2], lmh_avg[0],
                        lmh_avg[1], lmh_avg[2], lmh_avg[3], lmh_avg[4]);
        }
-       /* ----------------------------------------- */
-
-       /*-------------------------------------------*/
 
 
        /* -- Step B: prepare different curves---  */
-       /* step 1.x.1 and 1.x.2 */
+
+       /*S curve method 1,old algorithm,no use anymaore. 180227*/
        GetGmCurves(gma_scurve0, rGm1p2, lsft_avg, sBgnBnd, sEndBnd);
        GetGmCurves(gma_scurve1, rGm1p8, lsft_avg, sBgnBnd, sEndBnd);
 
+       /*Step 1.0 S curve method 2*/
        GetGmScurve_apl_var(GmScurve, luma_avg4, var, sBgnBnd, sEndBnd);
 
-       /* Step 2.x: CLAHE curve calculations */
-       get_clahe_curve(clash_curve, &cluma_avg4, pre_0_gamma,
-               var, clashBgn, clashEnd);
-       clahe_tiir();   /* iir filter on time domain */
-
-       /* step 3.x: get the blk_gma_crv down curve */
-       GetGmBlkCvs(blk_gma_crv, rGm1p8, sBgnBnd, dnlp_alg_param.dnlp_iRgnEnd);
-       /* ve_dnlp_pst_gmarat decide up arch or down; */
-        /*adap dnlp_pst_gmarat is later in step-C  */
-
-       /* Step 4.x.1: BWext parameters calc */
-       /* u10 */
-       cal_bwext_param(blk_wht_ext, ihstBgn, ihstEnd,
-               rgn_hstSum, lmh_avg);
-       if (dnlp_printk & 0x1)
-               pr_info("Step B-params: blk_wht_ext =[%d %d], dnlp_brightness =%d, luma_avg4 = %d, iRgnBgnEnd =[%d, %d]\n",
-                       blk_wht_ext[0], blk_wht_ext[1], dnlp_brightness,
-                       luma_avg4, dnlp_alg_param.dnlp_iRgnBgn,
-                       dnlp_alg_param.dnlp_iRgnEnd);
+       /* Step 1.1: CLAHE curve calculations */
+       if (clahe_method == 0)
+               get_clahe_curve(clash_curve, &cluma_avg4, pre_0_gamma, var,
+               clashBgn, clashEnd);
+       else if (clahe_method == 1)
+               get_hist_cross_curve(clash_curve, pre_0_gamma, iir_hstSum,
+                       clashBgn, clashEnd);
 
-       /* for debug */
-       CrtTstDat[20] = sBgnBnd;
-       CrtTstDat[21] = sEndBnd;
-       CrtTstDat[22] = lsft_avg;
-       CrtTstDat[23] = dnlp_alg_param.dnlp_iRgnEnd;
+       clahe_tiir();   /* iir filter on time domain */
 
-       /*----------------------------------------------------*/
+       /* Step 1.2: BWext parameters calc ,u10 */
+       cal_bwext_param(blk_wht_ext, ihstBgn, ihstEnd, rgn_hstSum, lmh_avg);
+       if (dnlp_printk)
+               pr_dnlp_dbg("++:luma_avg4=%d,blkwht_ext[2]=[%d,%d],lmh_avg[L H]=[%d,%d]\n",
+                       luma_avg4, blk_wht_ext[0], blk_wht_ext[1],
+                       lmh_avg[0], lmh_avg[2]);
 
 
        /* --- Step C: do the blending coefs calculations -----  */
-
-       /* step 2.y  blending rate betwen gama_scurve and CLAHE*/
+       /* Step 2.0.0  blending rate betwen gama_scurve and CLAHE*/
        mtdbld_rate = dnlp_adp_mtdrate(mtdbld_rate, luma_avg);
-       if (dnlp_printk & 0x1)
-               pr_info("Step C-2.y(after dnlp_adp_mtdrate()): mtdbld_rate = %d\n",
+       if (dnlp_printk)
+               pr_dnlp_dbg("+++ dnlp_adp_mtdrate()-1: mtdbld_rate = %d\n",
                        mtdbld_rate);
 
-       /*step 1.y. blending rate of s0,s1;
-        *plus a intermediate varibles(dnlp_pst_gmarat) for calc blk_gma_rat
-        */
-               /* post gamma rate: global -> auto local ??? */
-       if (ve_dnlp_pst_gmarat > 64)
-               dnlp_pst_gmarat = ve_dnlp_pst_gmarat - 64;
-       else
-               dnlp_pst_gmarat = 64 - ve_dnlp_pst_gmarat;
+       /*step 2.1. blending rate of s0,s1  no use anymaore.180227*/
        dnlp_adp_alpharate(lmh_avg, &low_alpha, &hgh_alpha,
-               &dnlp_pst_gmarat, dnlp_lowrange, dnlp_hghrange);
+       dnlp_lowrange, dnlp_hghrange);
 
-       if (ve_dnlp_pst_gmarat < 64) /* no adp dnlp_pst_gmarat */
-               dnlp_pst_gmarat = 64 - ve_dnlp_pst_gmarat;
-
-       if (dnlp_printk) {
-               pr_info("Step C-1.y(after dnlp_adp_alpharate() ): mtdbld: %2d (%2d %2d) => %2d (%2d %2d), gmma_rate=%d\n",
-                       dnlp_alg_param.dnlp_mtdbld_rate, ve_dnlp_lowalpha_v3,
-                       ve_dnlp_hghalpha_v3, mtdbld_rate,
-                       low_alpha, hgh_alpha, gmma_rate);
-       }
-
-       /*step y -adap: patch for black+white stripe,
-        *may change gmma_rate,low_alpha, hgh_alpha, mtdbld_rate
-        */
+       /*step 2.0.1.: patch for black+white stripe, mtdbld_rate*/
        patch_blk_wht(&mMaxLst[0], &mMaxIdx[0], &gmma_rate,
                &low_alpha, &hgh_alpha, &mtdbld_rate, iir_hstSum);
-       if (dnlp_printk & 0x1)
-               pr_info("Step C-y(adap:after patch_blk_wht()): mtdbld_rate = %d\n",
+       if (dnlp_printk)
+               pr_dnlp_dbg("+++ patch_blk_wht()-2: mtdbld_rate = %d\n",
                        mtdbld_rate);
 
-       /*step y --adap: do post processing of
-        *the coefs based on the luma_avgs
-        */
+       /*step 2.0.2:post processing ofthe coefs based on the luma_avgs*/
        dnlp_params_hist(&gmma_rate, &low_alpha, &hgh_alpha,
                                  &mtdbld_rate, luma_avg, luma_avg4);
-       if (dnlp_printk & 0x1)
-               pr_info("Step C-y(adap:after dnlp_params_hist()): mtdbld_rate = %d\n",
+       if (dnlp_printk)
+               pr_dnlp_dbg("+++ dnlp_params_hist()-3: mtdbld_rate = %d\n",
                        mtdbld_rate);
 
 
-       /* step 3.y get the coef for blending of
-        *blk_gma_bld = blk_gma_crv + clsh_scvbld
-        */
-       get_blk_gma_rat(&blk_gma_rat[0], &dnlp_pstgma_ratio, dnlp_pst_gmarat);
-       if (dnlp_printk & 0x1) {
-               pr_info("Step C,final-params: gmma_rate =%d, low_alpha =%d, hgh_alpha =%d, lsft_avg =%d; luma_avg=%d(%d), mtdbld_rate=%d\n",
-                       gmma_rate, low_alpha, hgh_alpha, lsft_avg,
-                       luma_avg, luma_avg4, mtdbld_rate);
-               pr_info("Step C-3.y:after get_blk_gma_rat(),blk_gma_rat[64]=\n  ");
-               for (i = 0; i < 64; i++)
-                       pr_info("%4d ", blk_gma_rat[i]);
-       }
-
-       /*------------------------------------------------------*/
-
-
-       /* -------------- Step D: Curves Blendings ------  */
-       /* step 1.z: blend scurve0 and scurve1 to get the gma_scurvet */
-       /* Step 1.x + 1.y */
+       /* -------- Step D: Curves Blendings ------  */
+       /*old S curve, no use anymaore. 180227*/
        dnlp_gmma_cuvs(gmma_rate, low_alpha, hgh_alpha, lsft_avg);
 
-       /*step 2.z: clsh_scvbld = clash_curve + gma_scurvet */
-       /* clash + s-curve              Step 1.z + 2.x,2.y, */
+       /*Step 3.0: clsh_scvbld = clash_curve + GmScurve */
        dnlp_clsh_sbld(mtdbld_rate);
-
-       /*step 3.z: blk_gma_bld = blk_gma_crv + clsh_scvbld */
-       /* Step 2.z + 3.x,3.y */
-       dnlp_blkgma_bld(blk_gma_rat);
-
        /*for read on tools*/
-       ro_blk_wht_ext0    = blk_wht_ext[0];
-       ro_blk_wht_ext1    = blk_wht_ext[1];
+       ro_mtdbld_rate = mtdbld_rate;
+       ro_blk_wht_ext0 = blk_wht_ext[0];
+       ro_blk_wht_ext1 = blk_wht_ext[1];
        ro_dnlp_brightness = dnlp_brightness;
 
-       /*step 4.z: blkwht_ebld = blk_gma_bld + extension */
-       /* Step 3.z + 4.x,4.y */
-       dnlp_blkwht_bld(blkwht_ebld, blk_gma_bld,
+       /*step 3.1: blkwht_ebld = blk_gma_bld + extension */
+       dnlp_blkwht_bld(blkwht_ebld, clsh_scvbld,
                        blk_wht_ext, dnlp_brightness,
                                luma_avg4, luma_avg,
                                dnlp_alg_param.dnlp_iRgnBgn,
                                dnlp_alg_param.dnlp_iRgnEnd);
 
-       /*------------------------------------------------------*/
-
-
 
        /*-- Step E: final curve selection, boosting and sorting Blendings---*/
-       /*Step E.1 selection of curve and apply the dnlp_final_gain*/
+       /*Step 4.0 selection of curve and apply the dnlp_final_gain*/
        curv_selection();
-
-
-       /*Step E.2: sort the curve to avoid luma invert, 0~255 sort */
+       /*Step 4.1: sort the curve to avoid luma invert, 0~255 sort */
        dnlp_tgt_sort();
-
-       /*Step E.3: do the saturation compensation based on the dnlp_curve */
+       /*Step 4.2: do the saturation compensation based on the dnlp_curve */
        sat_compens = dnlp_sat_compensation();
 
-       prt_flg = ((dnlp_printk >> 11) & 0x1);
-
-       if (prt_flg) {
-               for (i = 0; i < 64; i++) {
-                       nT0 = ve_dnlp_tgt[i] - 4*i;
-                       pr_info("%02d: %03d(45_line)=>%03d(final_cur) (%3d(dif))\n",
-                               i, 4*i, ve_dnlp_tgt[i], nT0);           }
-               pr_info("\n");
-       }
-
        if (dnlp_printk) {
-               pr_info("#Dbg:[iRgnBgn < luma_avg(luma_avg4) < iRgnEnd] dnlp_brightness=, sat_compens=\n");
-               pr_info("#Dbg:[%02d < %02d (%03d) < %02d] dnlp_brightness=%03d, sat_compens=%d\n",
+               pr_dnlp_dbg("#Dbg:[iRgnBgn < luma_avg(luma_avg4) < iRgnEnd] sat_compens=\n");
+               pr_dnlp_dbg("#Dbg:[%02d < %02d   (%03d)  < %02d]  sat_compens=%d\n",
                        dnlp_alg_param.dnlp_iRgnBgn, luma_avg,
                        luma_avg4, dnlp_alg_param.dnlp_iRgnEnd,
-                       dnlp_brightness, sat_compens);
+                       sat_compens);
        }
 
-
-       /* debug to print the curves */
-       nT0 = 0;
-       prt_flg = ((dnlp_printk >> 11) & 0x1);
-       if (prt_flg) {
+       if (dnlp_alg_param.dnlp_dbg_map && ((dnlp_printk >> 12) & 0x1))
                for (i = 0; i < 64; i++)
-                       nT0 += ((premap0[i] > ve_dnlp_tgt[i]) ?
-                                       (premap0[i] - ve_dnlp_tgt[i]) :
-                                       (ve_dnlp_tgt[i] - premap0[i]));
-
-               pr_info("Step E.4: map dif= %d\n", nT0);
-
-               if (nT0 > ve_dnlp_dbg_diflvl) {
-                       /* print pre  map & current final map */
-                       pr_info("# mtdbld_rate=%02d brht=%02d\n",
-                               mtdbld_rate, dnlp_brightness);
-                       /* pre ve_dnlp_tgt */
-                       pr_info("[index] pre_1_gamma => pre_0_gamma:(premap0 =>ve_dnlp_tgt[])\n");
-                       for (i = 0; i < 64; i++)
-                               if (premap0[i] != ve_dnlp_tgt[i])
-                                       pr_info("[%02d]%5d=>%5d:(%4d=>%4d)\n",
-                                               i,
-                                               pre_1_gamma[i], pre_0_gamma[i],
-                                               premap0[i], ve_dnlp_tgt[i]);
-                       /* CrtTstDat print out */
-                       pr_info("[index] PreTstDat=>CrtTstDat\n");
-                       for (i = 0; i < 28; i++)
-                               if (PreTstDat[i] != CrtTstDat[i])
-                                       pr_info("[%02d] %5d => %5d\n",
-                                               i, PreTstDat[i], CrtTstDat[i]);
-                       pr_info("\n");
-
-                       if (ve_dnlp_ponce >= 2)
-                               ve_dnlp_ponce--;
-                       else
-                               ve_dnlp_ponce = 1;
-               }
-       }
-
-       prt_flg = ((dnlp_printk >> 11) & 0x1);
-       if (dnlp_alg_param.dnlp_dbg_map && prt_flg)
-               for (i = 0; i < 64; i++)
-                       pr_info("[index %02d] %5d(pre_0_gamma)=>%5d(ve_dnlp_tgt[])\n",
+                       pr_dnlp_dbg("[index %02d] %5d(pre_0_gamma)=>%5d(ve_dnlp_tgt[])\n",
                                i, pre_0_gamma[i], ve_dnlp_tgt[i]);
 
        /* print debug log once */
        if (ve_dnlp_ponce == 1 && dnlp_printk)
                dnlp_printk = 0;
 
+       return;
 }
 
+
  /* lpf[0] is always 0 & no need calculation */
 void ve_dnlp_calculate_lpf(void)
 {