xref: /utopia/UTPA2-700.0.x/modules/dlc/hal/maserati/dlc/MsDlc.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 /******************************************************************************
79  Copyright (c) 2008 MStar Semiconductor, Inc.
80  All rights reserved.
81 
82  [Module Name]: MsDLC.c
83  [Date]: 2009-03-20
84  [Comment]:
85    MST DLC subroutines.
86  [Reversion History]:
87 *******************************************************************************/
88 
89 #define _MSDLC_C_
90 
91 #include "MsDlc_LIB.h"
92 #include "MsDlc.h"
93 
94 #ifdef MSOS_TYPE_LINUX
95     #include <sys/ioctl.h>
96     #include <unistd.h>
97     #include <fcntl.h> // O_RDWR
98     #include "../../../graphic/drv/gop/mdrv_gflip_io.h"
99 #endif
100 
101 // Compiler option
102 #define DLC_LIB_VERSION             0x3300 //v32.61 // fix bugs of u8_L_H_U & u8_L_H_D
103                                            //v32.59 // 1. Add guard condition to msDlcOnOff & msDlc_CGC_Init. msDlcOnOff & msDlc_CGC_Init will check DLC On/Off status before execution.
104                                            //v32.58 // 1. change register data width from WORD -> DWORD 2. Fixs float point issue.
105                                            //v32.57 // fix bug imgae flick while curve change, fix bug for g_DlcParameters.ucDlcAvgDeltaStill & g_DlcParameters.ucDlcAvgDelta
106                                            //v32.56 // fix bug for g_DlcParameters.ucDlcAvgDeltaStill
107                                            //v32.55 // change the resolution of Yavg for g_DlcParameters.ucDlcAvgDeltaStill
108                                            //v32.54 // add C Gain (CGC) On Off
109                                            //v32.53 // modify msDlc_AP_ReinitWithoutPara() for debug command
110                                            //v32.52 // Include DebugTool Command + move g_u8Yth to CGC parameter groups
111                                            //v32.51 // Modify CGC initial flow
112                                            //v32.50 // Fix CGC Limit definition
113                                            //v32.49 // add Cgain control by Jason.Chen, add protect when AvgMax < Avg Min for pure image
114                                            //v32.48 // reserved for Paulo/Lomeo version number.
115                                            //v32.47 // fix Yavg threshold problem
116                                            //v32.46 // rename parameters, add Yavg threshold, g_DlcParameters.ucDlcYAvgThresholdH & g_DlcParameters.ucDlcYAvgThresholdL
117                                            //v32.45 // fix level[14] over flow issue & unstable when LH2(g_DlcParameters.u8_L_H_D) is high
118                                            //v32.44 // finetune v32.43
119                                            //v32.43 // (test) add new parameter for stop curve cgange when Avg Delta <= g_DlcParameters.ucDlcAvgDeltaStill
120                                            //v32.42 // remove Delay1ms() and replace to a Macro msDlc_Delay1ms
121                                            //v32.41 // change usage of timeout event, use Delay1ms(n);
122                                            //v32.40 // add new parameter for WLE/BLE g_DlcParameters.ucDlcBLEPoint, g_DlcParameters.ucDlcWLEPoint, add new parameter for Avg offset (Compal), add timeout in initial stage
123                                            //v32.39 // modify for get stable pixel count "g_wLumiTotalCount"
124                                            //v32.38 // fix re-init error in Histogram request bit (bit2)
125                                            //v32.37 // seperate code msDlcLumiDiffCtrl()
126                                            //v32.36 // add more overflow/underflow protection
127                                            //v32.35 // to fix the bug => luma curve (0) is almost  "0x00" for all kind of pattern.
128                                            //v32.34 // add compiler option ENABLE_HISTOGRAM_8 and function msDlcSetVarHistogramRange()
129                                            //v32.33 // add histogram range of H
130                                            //v32.32 // add registers initial table
131                                            //v32.31 // add some definition & add function enter and exit
132                                            //v32.30 // modify for ATV/DTV are compatible
133                                            //v32.23 // change definition of g_wAvgDelta =>  Delta = n * 3 , add function msGetAverageLuminous()
134                                            //v32.22 // add g_DlcParameters.ucDlcFastAlphaBlending, update g_wAvgDelta
135                                            //v32.21 // add g_wAvgDelta
136                                            //v32.20 // add level limit
137                                            //v32.19 // modify de-flick function, add parameter for pure image "g_DlcParameters.ucDlcPureImageMode", remove msGetHistogramHandler from DLC handler, finetune the process of pure image
138                                            //v32.18 // modify de-flick function (g_ucUpdateCnt & g_ucDlcFlickAlpha)
139                                            //v32.17 // fix bug of de-flick function
140                                            //v32.16 // modify de-flick function
141                                            //v32.15 // modify alpha belnding & add g_DlcParameters.ucDlcFlickAlphaStart after mode change
142                                            //v32.14 // Remove garbage (L_L_U...)
143                                            //v32.13 // Protect Histogram Overflow
144                                            //v32.12 // modify for tool finetune
145                                            //v32.11 // modify init static curve & for tool finetune
146                                            //v32.10 // Lopez & Raphael series 32-seg DLC & version 10
147 
148 #define DLC_CGC_LIB_VERSION         0x0104 //v1.04 // add C Gain (CGC) On Off
149                                            //v1.03 // move g_u8Yth to CGC parameter groups
150                                            //v1.02 // Modify CGC initial flow
151                                            //v1.01 // Fix CGC Limit definition
152                                            //v1.00 // Initial
153 
154 
155 XDATA StuDlc_FinetuneParamaters g_DlcParameters;
156 
157 #define DLC_DEBUG(x)        //(x)
158 
159 #define DEFAULT_BLE_UPPER_BOND      0x08
160 #define DEFAULT_BLE_LOWER_BOND      0x04
161 
162 #define DLC_DEFLICK_PRECISION_SHIFT   64UL
163 #define DLC_DEFLICK_BLEND_FACTOR      32UL
164 
165 #define DLC_PURE_IMAGE_DLC_CURVE        0L
166 #define DLC_PURE_IMAGE_LINEAR_CURVE     1L
167 #define DLC_PURE_IMAGE_DO_NOTHING       2L
168 
169 #define XC_DLC_ALGORITHM_OLD       0
170 #define XC_DLC_ALGORITHM_NEW       1
171 #define XC_DLC_ALGORITHM_KERNEL    2
172 
173 #define XC_DLC_SET_DLC_CURVE_BOTH_SAME    0   // 0:Main and Sub use the same DLC curve
174 
175 #define XC_DLC_FRAME_WIDTH_4K2K_HALF 1920 //need DLC information
176 #define XC_DLC_FRAME_WIDTH_FHD_HALF 960 //need DLC information
177 
178 #ifdef MSOS_TYPE_LINUX
179 XDATA MS_S32   s32FdDlcBle= -1 ;
180 #endif
181 
182 XDATA WORD g_wLumaHistogram32H[32];
183 XDATA BYTE g_ucHistogramMax, g_ucHistogramMin;
184 XDATA WORD g_wLumiAverageTemp;
185 XDATA BYTE g_ucDlcFlickAlpha;
186 XDATA BYTE g_ucTable[16];     // Final target curve (8 bit)
187 XDATA BYTE g_pre_ucTable[16]; // Final target curve (8 bit)
188 XDATA WORD g_uwTable[16];     // Final target curve (10 bit)
189 XDATA BYTE g_ucCurveYAvg[16];
190 XDATA DWORD g_uwCurveHistogram[16];
191 XDATA WORD g_uwPre_CurveHistogram[16];
192 XDATA WORD g_uwPreTable[16]; // New de-Flick
193 XDATA BYTE g_ucUpdateCnt;
194 XDATA WORD g_wLumiTotalCount;
195 XDATA BYTE g_ucTmpAvgN_1; // Avg value (N-1)
196 XDATA BYTE g_ucTmpAvgN; // Avg value (current N)
197 XDATA WORD g_wTmpAvgN_1_x10; // Avg value (N-1) x 10
198 XDATA WORD g_wTmpAvgN_x10; // Avg value (current N) x 10
199 XDATA BYTE g_ucDlcFastLoop; // for pulse ripple
200 
201 #define FLICK_ALPHA_START_COUNTER   40
202 
203 XDATA DWORD g_dwFactory;
204 XDATA DWORD g_dwFactory_7;
205 XDATA DWORD g_dwFactory_9;
206 XDATA DWORD g_dwLstep;
207 XDATA DWORD g_dwHstep;
208 
209 XDATA BYTE s_Slope[8] = {0x17,0x27,0x47,0x67,0x97,0xB7,0xD7,0xE7};
210 
211 static MS_BOOL  g_bSetDlcBleOn = TRUE;
212 static WORD g_wPIAlpha=0;
213 static WORD g_wPrePIAlpha=0;
214 
215 #define LA1 g_DlcParameters.u8_L_L_U
216 #define LA2 g_DlcParameters.u8_L_L_D
217 #define LB1 g_DlcParameters.u8_L_L_U
218 #define LB2 g_DlcParameters.u8_L_L_D
219 #define LC1 g_DlcParameters.u8_L_L_U
220 #define LC2 g_DlcParameters.u8_L_L_D
221 #define LD1 g_DlcParameters.u8_L_L_U
222 #define LD2 g_DlcParameters.u8_L_L_D
223 #define LE1 g_DlcParameters.u8_L_H_U
224 #define LE2 g_DlcParameters.u8_L_H_D
225 #define LF1 g_DlcParameters.u8_L_H_U
226 #define LF2 g_DlcParameters.u8_L_H_D
227 #define LG1 g_DlcParameters.u8_L_H_U
228 #define LG2 g_DlcParameters.u8_L_H_D
229 #define LH1 g_DlcParameters.u8_L_H_U
230 #define LH2 g_DlcParameters.u8_L_H_D
231 
232 #define SA1 g_DlcParameters.u8_S_L_U
233 #define SA2 g_DlcParameters.u8_S_L_D
234 #define SB1 g_DlcParameters.u8_S_L_U
235 #define SB2 g_DlcParameters.u8_S_L_D
236 #define SC1 g_DlcParameters.u8_S_L_U
237 #define SC2 g_DlcParameters.u8_S_L_D
238 #define SD1 g_DlcParameters.u8_S_L_U
239 #define SD2 g_DlcParameters.u8_S_L_D
240 #define SE1 g_DlcParameters.u8_S_H_U
241 #define SE2 g_DlcParameters.u8_S_H_D
242 #define SF1 g_DlcParameters.u8_S_H_U
243 #define SF2 g_DlcParameters.u8_S_H_D
244 #define SG1 g_DlcParameters.u8_S_H_U
245 #define SG2 g_DlcParameters.u8_S_H_D
246 #define SH1 g_DlcParameters.u8_S_H_U
247 #define SH2 g_DlcParameters.u8_S_H_D
248 
249 typedef enum
250 {
251     Slope_1,
252     Slope_2,
253     Slope_4,
254     Slope_6,
255     Slope_9,
256     Slope_11,
257     Slope_13,
258     Slope_14,
259     Slope_Max
260 } Slop_Type;
261 
262 #define Lstep           g_dwLstep
263 #define Hstep           g_dwHstep
264 
265 //=========================================================
266 
267 #define TOTAL_PIXEL     ((DWORD)(wVEnd-wVStart)*(wHEnd-wHStart)*7*4/(160*3))  // ((DWORD)(wVEnd-wVStart)*0.04375*4/3*(wHEnd-wHStart))
268 
269 void msWriteDLCRegsTbl(DlcRegUnitType code *pTable);
270 void msDlcWriteCurve(BOOL bWindow);
271 void msDlcWriteCurveLSB(BOOL bWindow, BYTE ucIndex, BYTE ucValue);
272 void msDlcLumiDiffCtrl(void);
273 void msDlcSetVarHistogramRange(BYTE *pRangeArray);
274 
275 /******************************************************************************/
276 /*                           CGC                                         */
277 /******************************************************************************/
278 // Constant
279 #define Yvar_Th       4                         // Change threshold for DLC luma curve
280 #define Timer_CountU   3                   // Increasing Chroma Gain change speed
281 #define Timer_CountD   1                   // Decreasing Chroma Gain change speed
282 #define CGain_StepU     1                   // Increasing Chroma Gain step
283 #define CGain_StepD     1                   // Decreasing Chroma Gain step
284 
285 #define Y01_weight    5                      // Luma Curve Y[0], Y[1] weighting
286 #define Y23_weight    3                      // Luma Curve Y[2], Y[3] weighting
287 #define Y45_weight    1                      // Luma Curve Y[4], Y[5] weighting
288 #define Y67_weight    1                      // Luma Curve Y[6], Y[7] weighting
289 
290 // Local
291 XDATA BYTE g_u8Chroma_OriGain = 0x40;
292 XDATA BYTE g_u8Chroma_target;
293 XDATA WORD g_u16Ydark, g_u16Ydark_pre;
294 XDATA BYTE g_u8TCount = 0;
295 XDATA BYTE g_u8CGC_UpperBond, g_u8CGC_LowerBond;
296 XDATA BOOL g_bSetDLCCurveBoth = 0;//We will set main/sub DLC curve at the same time in 3D side-side ap.
297 
298 /******************************************************************************/
299 
300 //////////////////////////////////////////////////////////////
301 //
302 //////////////////////////////////////////////////////////////
msDlcInitWithCurve(WORD wHStart,WORD wHEnd,WORD wVStart,WORD wVEnd)303 void msDlcInitWithCurve( WORD wHStart, WORD wHEnd, WORD wVStart, WORD wVEnd )
304 {
305     BYTE ucTmp, ucTimeout;
306     WORD u16HGuardBangSize=0;
307 
308 
309     msDlc_FunctionEnter();
310 
311     g_ucDlcFastLoop = (g_DlcParameters.ucDlcFastAlphaBlending/2) * 16;
312     if (g_DlcParameters.ucDlcFastAlphaBlending == 32) // 32 / 2 * 16 = 0 overflow
313         g_ucDlcFastLoop = 255;
314 
315     //putstr("\r\nDLC Initilize use HK MCU");
316 
317     g_dwFactory = TOTAL_PIXEL/32/2;
318     g_dwFactory_7 = (g_dwFactory - (g_dwFactory/8));
319     g_dwFactory_9 = (g_dwFactory + (g_dwFactory/8));
320 
321     g_dwLstep = g_dwFactory*1/16*12/10;
322     g_dwHstep = g_dwFactory*3/8;
323 
324     // Registers Init
325     msWriteDLCRegsTbl(tDLC_Initialize);
326 
327     // Set histogram method/dither
328     if (msReadByte(REG_ADDR_DLC_HANDSHAKE) & _BIT2)
329     {
330         ucTimeout = 60;
331         while(!(msReadByte(REG_ADDR_DLC_HANDSHAKE) & _BIT3) && (ucTimeout--))
332         {
333             msDlc_Delay1ms(1);
334         }
335     }
336 
337     msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE)|_BIT5|_BIT4 );
338     // Set histogram range - H
339     if (REG_ADDR_HISTOGRAM_RANGE_M_HST != REG_NULL)
340     {
341         u16HGuardBangSize= MDrv_Read2Byte( REG_ADDR_H_GUARD_BAND_OUTPUT_SIZE_MAIN) & 0xFFF;
342 
343         // Main Window
344         MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HST, wHStart/8);
345 
346         if((MDrv_Read2Byte(REG_ADDR_VOP_OUTPUT_H_TOTAL) & 0x3FFF)>3000)    // 4K output
347         {
348             MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HEN, XC_DLC_FRAME_WIDTH_4K2K_HALF/8);
349         }
350         else
351         {
352             MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HEN, XC_DLC_FRAME_WIDTH_FHD_HALF/8);
353         }
354 
355         MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HST_SEC, u16HGuardBangSize/8);
356 
357         if((MDrv_Read2Byte(REG_ADDR_VOP_OUTPUT_H_TOTAL) & 0x3FFF)>3000)   // 4K output
358         {
359             MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HEN_SEC, (wHEnd-XC_DLC_FRAME_WIDTH_4K2K_HALF+u16HGuardBangSize)/8);
360         }
361         else
362         {
363             MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HEN_SEC, (wHEnd-XC_DLC_FRAME_WIDTH_FHD_HALF+u16HGuardBangSize)/8);
364         }
365 
366     }
367     // Set histogram range - V
368     // Main Window
369     MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_VST, wVStart/8);
370     MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_VEN, wVEnd/8);
371 
372     msWriteByte(REG_ADDR_BLE_UPPER_BOND, DEFAULT_BLE_UPPER_BOND);
373     msWriteByte(REG_ADDR_BLE_LOWER_BOND, DEFAULT_BLE_LOWER_BOND);
374 
375     // Enable range for Histogram
376     msWriteByte(REG_ADDR_HISTOGRAM_RANGE_ENABLE, _BIT7|msReadByte(REG_ADDR_HISTOGRAM_RANGE_ENABLE) ); // Enable MAIN_WINDOW histogram
377 
378     // Init Luma Curve
379     //putstr("[DLC]: Disable the Burst Write 16 Luma Curve\r\n");
380     for(ucTmp=0; ucTmp<=0x0f; ucTmp++)
381     {
382         g_ucTable[ucTmp] = g_DlcParameters.ucLumaCurve[ucTmp];
383         g_uwPreTable[ucTmp] = (WORD)g_DlcParameters.ucLumaCurve[ucTmp] * DLC_DEFLICK_PRECISION_SHIFT; // New de-Flick
384 	    msWriteByte((REG_ADDR_DLC_DATA_START_MAIN+ucTmp), g_ucTable[ucTmp]);
385         msDlcWriteCurveLSB(MAIN_WINDOW, ucTmp, 0);
386     }
387     // init DLC curve index N0 & 16
388     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN, g_ucTable[0]);
389     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN + 1, 0x01);
390     msDlcWriteCurveLSB(MAIN_WINDOW, 0xFF, 0);
391     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN, 255-g_ucTable[15]);
392     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN + 1, 0x01);
393     msDlcWriteCurveLSB(MAIN_WINDOW, 16, 0);
394 
395     // Get Total Pixel Count
396     msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) & 0xF0);
397     msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) | _BIT2 | _BIT1);
398 
399     //Get the histogram report sum of pixel number .
400     g_wLumiTotalCount = (MDrv_Read2Byte(REG_ADDR_HISTOGRAM_TOTAL_COUNT_L)
401                     | (MDrv_Read2Byte(REG_ADDR_HISTOGRAM_TOTAL_COUNT_L+2)<<16))/4;
402 
403     g_DlcParameters.ucDlcTimeOut = 0;
404     g_DlcParameters.ucDlcFlickAlphaStart = FLICK_ALPHA_START_COUNTER;
405     g_DlcParameters.ucDlcSlowEvent = 0;
406     g_ucUpdateCnt = 0;
407 
408     msDlc_FunctionExit();
409 }
410 
msDlc_SetCaptureRange(MS_U16 wHStart,MS_U16 wHEnd,MS_U16 wVStart,MS_U16 wVEnd)411 void msDlc_SetCaptureRange(MS_U16 wHStart,MS_U16 wHEnd, MS_U16 wVStart, MS_U16 wVEnd)
412 {
413     msDlc_FunctionEnter();
414     MS_U16 u16HGuardBangSize=0;
415 
416     if (REG_ADDR_HISTOGRAM_RANGE_M_HST != REG_NULL)
417     {
418         // Set histogram range - H
419         // Main Window
420         u16HGuardBangSize= MDrv_Read2Byte( REG_ADDR_H_GUARD_BAND_OUTPUT_SIZE_MAIN) & 0xFFF;
421         MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HST, wHStart/8);
422 
423         if((MDrv_Read2Byte(REG_ADDR_VOP_OUTPUT_H_TOTAL) & 0x3FFF)>3000)    // 4K output
424         {
425             MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HEN, XC_DLC_FRAME_WIDTH_4K2K_HALF/8);
426         }
427         else
428         {
429             MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HEN, XC_DLC_FRAME_WIDTH_FHD_HALF/8);
430         }
431 
432         MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HST_SEC, u16HGuardBangSize/8);
433 
434         if((MDrv_Read2Byte(REG_ADDR_VOP_OUTPUT_H_TOTAL) & 0x3FFF)>3000)   // 4K output
435         {
436             MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HEN_SEC, (wHEnd-XC_DLC_FRAME_WIDTH_4K2K_HALF+u16HGuardBangSize)/8);
437         }
438         else
439         {
440             MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_HEN_SEC, (wHEnd-XC_DLC_FRAME_WIDTH_FHD_HALF+u16HGuardBangSize)/8);
441         }
442 
443         // Virtical size need to change by display
444         // Set histogram range - V
445         // Main Window
446         MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_VST, wVStart/8);
447         MDrv_Write2Byte(REG_ADDR_HISTOGRAM_RANGE_M_VEN, wVEnd/8);
448 
449         msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) & 0xF2);
450         msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) | _BIT2 );
451     }
452     msDlc_FunctionExit();
453 }
454 
msDlcEnableSetDLCCurveBoth(BOOL bEnable)455 void msDlcEnableSetDLCCurveBoth(BOOL bEnable)
456 {
457     g_bSetDLCCurveBoth = bEnable;
458 }
459 
460 //////////////////////////////////////////////////////////////
461 //
462 //////////////////////////////////////////////////////////////
msDlcOnOff(BOOL bSwitch,BOOL bWindow)463 void msDlcOnOff(BOOL bSwitch, BOOL bWindow)
464 {
465     BYTE ucCtlBit;
466 
467     msDlc_FunctionEnter();
468 
469     if( MAIN_WINDOW == bWindow )
470         ucCtlBit = _BIT7;
471     else // Sub window
472         ucCtlBit = _BIT6;
473 
474     if( bSwitch ) // Enable
475     {
476         if((msReadByte(REG_ADDR_DLC_HANDSHAKE) & ucCtlBit) )// Shall not turn DLC on when it is on already.
477         {
478            msDlc_FunctionExit();
479            return;
480         }
481 
482         msWriteBit(REG_ADDR_DLC_HANDSHAKE, TRUE, ucCtlBit);
483 
484         // Request HW to do histogram
485         msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) & 0xF2);
486 
487         msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) | _BIT2 );
488 
489         g_DlcParameters.ucDlcFlickAlphaStart = FLICK_ALPHA_START_COUNTER;
490         g_DlcParameters.ucDlcSlowEvent = 0;
491     }
492     else // Disable
493     {
494         msWriteBit(REG_ADDR_DLC_HANDSHAKE, FALSE, ucCtlBit);
495     }
496 
497     msDlc_FunctionExit();
498 }
499 
500 //////////////////////////////////////////////////////////////
501 //
502 //////////////////////////////////////////////////////////////
msDlcSetBleOnOff(BOOL bSwitch)503 void msDlcSetBleOnOff(BOOL bSwitch)
504 {
505     g_bSetDlcBleOn= bSwitch ;
506 }
507 
508 //////////////////////////////////////////////////////////////
509 //
510 //////////////////////////////////////////////////////////////
msIsBlackVideoEnable(MS_BOOL bWindow)511 MS_BOOL msIsBlackVideoEnable( MS_BOOL bWindow )
512 {
513     MS_BOOL bReturn;
514 
515     msVop_FunctionEnter();
516 
517     if( MAIN_WINDOW == bWindow)
518     {
519         if(msReadByte(REG_ADDR_VOP_SCREEN_CONTROL) & BIT(1))
520         {
521             bReturn = TRUE;
522         }
523         else
524         {
525             bReturn = FALSE;
526         }
527     }
528     else
529     {
530         if(msReadByte(REG_ADDR_VOP_SCREEN_CONTROL) & BIT(5))
531         {
532             bReturn = TRUE;
533         }
534         else
535         {
536             bReturn = FALSE;
537         }
538     }
539 
540     msVop_FunctionExit();
541     return bReturn;
542 
543 }
544 
545 //////////////////////////////////////////////////////////////
546 //
547 //////////////////////////////////////////////////////////////
msGetAverageLuminous(void)548 BYTE msGetAverageLuminous(void) // = external msGetAverageLuminousValue()
549 {
550     WORD uwTmpAvr;
551 
552     // Normalize
553     if (g_wLumiTotalCount!=0)
554         uwTmpAvr = (256 * (DWORD)g_wLumiAverageTemp + (g_wLumiTotalCount/2)) / g_wLumiTotalCount;
555     else
556         uwTmpAvr = 0;
557 
558     if (uwTmpAvr >= 255)
559         uwTmpAvr = 255;
560 
561     return (BYTE)uwTmpAvr;
562 }
563 
564 //////////////////////////////////////////////////////////////
565 //
566 //////////////////////////////////////////////////////////////
msGetAverageLuminous_x4(void)567 WORD msGetAverageLuminous_x4(void) // = external msGetAverageLuminousValue()
568 {
569     WORD uwTmpAvr;
570 
571     // Normalize
572     if (g_wLumiTotalCount!=0)
573         uwTmpAvr = (1024 * (DWORD)g_wLumiAverageTemp + (g_wLumiTotalCount/2)) / g_wLumiTotalCount;
574     else
575         uwTmpAvr = 0;
576 
577     if (uwTmpAvr >= 1024- 1)
578         uwTmpAvr = 1024- 1;
579 
580     return uwTmpAvr;
581 }
582 
583 //////////////////////////////////////////////////////////////
584 //
585 //////////////////////////////////////////////////////////////
msGetAverageLuminous_x10(void)586 WORD msGetAverageLuminous_x10(void)
587 {
588     WORD uwTmpAvr;
589 
590     // Normalize
591     if (g_wLumiTotalCount!=0)
592         uwTmpAvr = (256 * 10 * (DWORD)g_wLumiAverageTemp + (g_wLumiTotalCount/2)) / g_wLumiTotalCount;
593     else
594         uwTmpAvr = 0;
595 
596     if (uwTmpAvr >= (256 * 10 - 1))
597         uwTmpAvr = (256 * 10 - 1);
598 
599     return uwTmpAvr;
600 }
601 
602 //////////////////////////////////////////////////////////////
603 //
604 //////////////////////////////////////////////////////////////
msGetHistogramHandler(BOOL bWindow)605 BOOL msGetHistogramHandler(BOOL bWindow)
606 {
607     BYTE ucTmp;
608     WORD wTmpHistogramSum; // for protect histogram overflow
609     BYTE ucHistogramOverflowIndex;
610     BYTE ucReturn;
611 
612     msDlc_FunctionEnter();
613 
614 #if 0 //Have double buffer, does not need to wait.
615     // ENABLE_FAST_HISTOGRAM_CATCH
616     if( !(msReadByte(REG_ADDR_DLC_HANDSHAKE) & _BIT3) )  // Wait until ready
617     {
618         msDlc_FunctionExit();
619         return FALSE;
620     }
621 #endif
622 
623     if (g_DlcParameters.ucDlcTimeOut)
624     {
625         // Get histogram 32 data
626         // Total/BW_ref histogram count
627         // putstr("[DLC]: Disable the Burst Read 32-section Histogram\r\n");
628         //dwTotalCount = 0;
629         for (ucTmp = 0, wTmpHistogramSum = 0, ucHistogramOverflowIndex = 0xFF; ucTmp < 32; ucTmp++)
630         {
631             //enhance to support 20 bits
632             g_wLumaHistogram32H[ucTmp]=MDrv_Read2Byte(REG_ADDR_HISTOGRAM_DATA_32+ (4*ucTmp)+2)&0x000F;
633             g_wLumaHistogram32H[ucTmp] <<= 16;
634             g_wLumaHistogram32H[ucTmp] |= MDrv_Read2Byte(REG_ADDR_HISTOGRAM_DATA_32+ (4*ucTmp));
635             g_wLumaHistogram32H[ucTmp] = g_wLumaHistogram32H[ucTmp] /4;
636 
637             //printf(" %d",g_wLumaHistogram32H[ucTmp]);
638             //dwTotalCount += g_wLumaHistogram32H[ucTmp];
639 
640              // protect histogram overflow
641             if (g_wLumaHistogram32H[ucTmp] >= 65535)
642             {
643                 ucHistogramOverflowIndex = ucTmp;
644                 //printf("\r\n overflow %d",ucHistogramOverflowIndex);
645             }
646             g_wLumaHistogram32H[ucTmp] >>= 1; // protect histogram overflow
647             wTmpHistogramSum += g_wLumaHistogram32H[ucTmp];
648         }
649 
650         //Get the histogram report sum of pixel number .
651         g_wLumiTotalCount = (MDrv_Read2Byte(REG_ADDR_HISTOGRAM_TOTAL_COUNT_L)
652                         | (MDrv_Read2Byte(REG_ADDR_HISTOGRAM_TOTAL_COUNT_L+2)<<16))/4;
653 
654         // protect histogram overflow
655         if (ucHistogramOverflowIndex != 0xFF)
656         {
657             if (wTmpHistogramSum > g_wLumiTotalCount)
658                 wTmpHistogramSum = g_wLumiTotalCount;
659             g_wLumaHistogram32H[ucHistogramOverflowIndex] += (g_wLumiTotalCount - wTmpHistogramSum);
660         }
661 
662         // Get MIN. & MAX VALUE
663         if( MAIN_WINDOW == bWindow )   // Main window
664         {
665             g_ucHistogramMin = msReadByte(REG_ADDR_MAIN_MIN_VALUE);
666             g_ucHistogramMax = msReadByte(REG_ADDR_MAIN_MAX_VALUE);
667         }
668 
669         g_ucTmpAvgN_1 = msGetAverageLuminous();
670         g_wTmpAvgN_1_x10 = msGetAverageLuminous_x10();
671 
672         g_wLumiAverageTemp = ((MDrv_Read2Byte( REG_ADDR_HISTOGRAM_TOTAL_SUM_L+2 )<<16) | MDrv_Read2Byte( REG_ADDR_HISTOGRAM_TOTAL_SUM_L ))/4;
673         g_ucTmpAvgN = msGetAverageLuminous();
674         g_wTmpAvgN_x10 = msGetAverageLuminous_x10();
675 
676         ucReturn = TRUE;
677     }
678     else
679     {
680         ucReturn = FALSE;
681         //putstr("\r\n!!!!! DLC Time Out !!!!!");
682     }
683 
684     // After read histogram, request HW to do histogram
685     // Request HW to do histogram
686     msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) & 0xF2);
687 
688     g_DlcParameters.ucDlcTimeOut = 150; // 150ms
689 
690     msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) | _BIT2 );
691 
692     msDlc_FunctionExit();
693 
694     return ucReturn;
695 }
696 
697 //////////////////////////////////////////////////////////////
698 //
699 //////////////////////////////////////////////////////////////
msDlcSetVarHistogramRange(BYTE * pRangeArray)700 void msDlcSetVarHistogramRange(BYTE *pRangeArray)
701 {
702     BYTE ucTmp;
703     DWORD wReg;
704 
705     wReg = REG_ADDR_HISTOGRAM_8_RANGE_START;
706     for (ucTmp=0; ucTmp<7 ; ucTmp++)
707     {
708         msWriteByte(wReg, *(pRangeArray + ucTmp));
709         wReg += 1;
710     }
711 }
712 
713 //=========================================================
714 //
715 //=========================================================
msDlcLumiDiffCtrl(void)716 void msDlcLumiDiffCtrl(void)
717 {
718     if (g_DlcParameters.ucDlcFlickAlphaStart)
719     {
720         g_DlcParameters.ucDlcFlickAlphaStart--;
721     }
722 
723     if (g_DlcParameters.ucDlcSlowEvent)
724     {
725         g_DlcParameters.ucDlcSlowEvent--;
726         g_ucDlcFlickAlpha = 1; // it mean the curve change slowly
727         g_ucDlcFastLoop = 0;
728     }
729     else
730     {
731         if (g_wTmpAvgN_1_x10 >= g_wTmpAvgN_x10)
732         {
733             if ((g_ucDlcFastLoop > 0) || ((g_wTmpAvgN_1_x10 - g_wTmpAvgN_x10 ) >= ((WORD)g_DlcParameters.ucDlcAvgDelta * 10)))
734             {
735                 //printf("\r\n1H:Avg = %d",g_ucTmpAvgN_1 - g_ucTmpAvgN);
736                 //printf(" g_ucDlcFlickAlpha = %d\r\n",g_ucDlcFlickAlpha);
737 
738                 g_ucDlcFlickAlpha = g_DlcParameters.ucDlcFastAlphaBlending; // (32/32) = (32/DLC_DEFLICK_BLEND_FACTOR) // it mean the curve change quickly
739                 if (g_ucDlcFastLoop == 0)
740                 {
741                     g_ucDlcFastLoop = (g_DlcParameters.ucDlcFastAlphaBlending/2) * 16;
742                     if (g_DlcParameters.ucDlcFastAlphaBlending == 32) // 32 / 2 * 16 = 0 overflow
743                     {
744                         g_ucDlcFastLoop = 255;
745                     }
746                 }
747             }
748             else if ((g_DlcParameters.ucDlcAvgDeltaStill <= ((WORD)g_DlcParameters.ucDlcAvgDelta * 10)) && (g_DlcParameters.ucDlcAvgDeltaStill != 0) && ((g_wTmpAvgN_1_x10 - g_wTmpAvgN_x10) <= g_DlcParameters.ucDlcAvgDeltaStill)) // if (Yavg is less than g_DlcParameters.ucDlcAvgDeltaStill) & (Fast change is finish) does NOT do curve change
749             {
750                 g_ucDlcFlickAlpha = 0; // (0/32) = (1/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve will be still
751             }
752             else // if ((g_ucTmpAvgN_1 - g_ucTmpAvgN) < g_DlcParameters.ucDlcAvgDelta) // if (Yavg is less than g_DlcParameters.ucDlcAvgDelta) & (Fast change is finish) may do curve change slowly
753             {
754                 if ( g_DlcParameters.ucDlcFlickAlphaStart == 0) // chagne source and it's stable
755                 {
756                     g_ucDlcFlickAlpha = 1; // (1/32) = (1/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve change slowly
757                 }
758                 else
759                 {
760                     g_ucDlcFlickAlpha = g_DlcParameters.ucDlcFastAlphaBlending; // (32/32) = (32/DLC_DEFLICK_BLEND_FACTOR) // it mean the curve change quickly
761                 }
762             }
763         }
764         else if (g_wTmpAvgN_1_x10 < g_wTmpAvgN_x10)
765         {
766              if ((g_ucDlcFastLoop > 0) || (( g_wTmpAvgN_x10 - g_wTmpAvgN_1_x10 ) >= ((WORD)g_DlcParameters.ucDlcAvgDelta * 10)))
767             {
768                 g_ucDlcFlickAlpha = g_DlcParameters.ucDlcFastAlphaBlending; // (32/32) = (32/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve change quickly
769                 if (g_ucDlcFastLoop == 0)
770                 {
771                     g_ucDlcFastLoop = (g_DlcParameters.ucDlcFastAlphaBlending/2) * 16;
772                     if (g_DlcParameters.ucDlcFastAlphaBlending == 32) // 32 / 2 * 16 = 0 overflow
773                     {
774                         g_ucDlcFastLoop = 255;
775                     }
776                 }
777             }
778             else if ((g_DlcParameters.ucDlcAvgDeltaStill <= ((WORD)g_DlcParameters.ucDlcAvgDelta * 10)) && (g_DlcParameters.ucDlcAvgDeltaStill != 0) && ((g_wTmpAvgN_x10 - g_wTmpAvgN_1_x10) <= g_DlcParameters.ucDlcAvgDeltaStill)) // if (Yavg is less than g_DlcParameters.ucDlcAvgDeltaStill) & (Fast change is finish) does NOT do curve change
779             {
780                 g_ucDlcFlickAlpha = 0; // (0/32) = (1/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve will be still
781             }
782             else // if ((g_ucTmpAvgN - g_ucTmpAvgN_1) < g_DlcParameters.ucDlcAvgDelta) // if (Yavg is less than g_DlcParameters.ucDlcAvgDelta) & (Fast change is finish) may do curve change slowly
783             {
784                 if (g_DlcParameters.ucDlcFlickAlphaStart == 0) // chagne source and it's stable
785                 {
786                     g_ucDlcFlickAlpha = 1; // (1/32) = (1/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve change slowly
787                 }
788                 else
789                 {
790                     g_ucDlcFlickAlpha = g_DlcParameters.ucDlcFastAlphaBlending; // (32/32) = (32/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve change quickly
791                 }
792             }
793         }
794     }
795 }
796 
797 //=========================================================
798 //
799 //=========================================================
msDlcHandler(BOOL bWindow)800 void msDlcHandler(BOOL bWindow)
801 {
802     BYTE ucTmp,Tmp,sepPoint =0,sepPoint0 =0,sepPoint1 =0,sepPoint2 =0,ucYAvgNormalize=0,belnging_factor;
803     WORD ucYAvg=0;
804     WORD uwHistogramMax, uwHistogramMax2nd,uwHistogramSlopRatioL, uwHistogramSlopRatioH; // 20110406 Ranma add
805     DWORD uwHistogramAreaSum[32];//,uwPre_Histogram_ShiftR[32],uwPre_Histogram_ShiftL[32]; // 20110406 Ranma add
806     WORD uwHistogramForBleSum=0;
807     WORD uwHistogram_BLE_AreaSum[32];
808     WORD BLE_StartPoint=0,BLE_EndPoint=0,BLE_StartPoint_Correction=0,BLE_EndPoint_Correction=0;
809     WORD BLE_Slop_400,BLE_Slop_480,BLE_Slop_500,BLE_Slop_600,BLE_Slop_800,Dark_BLE_Slop_Min;
810 
811     static WORD Pre_BLE_Slop=0x4000;
812     static WORD Pre_g_ucDlcFlickAlpha = 0,Pre_TotalYSum = 0,Pre_BLE_StartPoint_Correction=0xFFF;
813     static BYTE ucPre_YAvg =0x00;
814     WORD uwBLE_Blend=0,uwBLE_StartPoint_Blend=0,CurveDiff=0,CurveDiff_Coring=0,CurveDiff_base=0,YAvg_base=0;
815     BYTE g_ucDlcFlickAlpha_Max=0,g_ucDlcFlickAlpha_Min=0,YAvg_TH_H,YAvg_TH_L,g_ucDlcFlickAlpha_temp,ucDlcFlickAlpha_Diff=0;
816     DWORD dwSumBelowAvg=0, dwSumAboveAvg=0,dwHistogramDlcSlop[17],BLE_Slop=0x400,BLE_Slop_tmp=0x400;
817     WORD uwArrayComp, uwArraySum, Delta_YAvg=0,coeff0,coeff1,coeff2;
818     DWORD His_ratio_BelowAvg,His_ratio_AboveAvg;
819     DWORD g_uwCurveHistogram0[16],g_uwCurveHistogram1[16],g_uwCurveHistogram2[16];
820     DWORD BLE_sum=0;
821     WORD Pre_YAvg_base,Diff_L,Diff_M,Diff_H,Pre_CurveDiff_base;
822 
823     //When DLC algorithm is from kernel, force return.
824     if(g_DlcParameters.ucDlcAlgorithmMode == XC_DLC_ALGORITHM_KERNEL)
825     {
826         return ;
827     }
828 
829     msDlc_FunctionEnter();
830 
831     msDlcLumiDiffCtrl();
832 
833     if(g_DlcParameters.ucDlcAlgorithmMode == XC_DLC_ALGORITHM_OLD)
834     {
835         // If difference is too small, don't do dlc
836         if( ((g_ucHistogramMax-g_ucHistogramMin) < 0x0A) || (g_ucHistogramMin >= g_ucHistogramMax) )
837         {
838             if (g_DlcParameters.ucDlcPureImageMode == DLC_PURE_IMAGE_DO_NOTHING)
839             {
840                 msDlc_FunctionExit();
841                 return;
842             }
843             else if (g_DlcParameters.ucDlcPureImageMode == DLC_PURE_IMAGE_LINEAR_CURVE)
844             {
845                 BYTE ucLuma = 0x07;
846                 for( ucTmp = 0; ucTmp < 16; ++ ucTmp )
847                 {
848                     g_ucTable[ucTmp] = ucLuma;
849                     ucLuma += 0x10;
850                 }
851 
852                 g_ucDlcFlickAlpha = g_DlcParameters.ucDlcFastAlphaBlending; // it mean the curve change quickly
853 
854                 // Write data to luma curve ...
855                 msDlcWriteCurve(bWindow);
856                 if(g_bSetDLCCurveBoth)
857                 {
858                     msDlcWriteCurve(SUB_WINDOW);
859                 }
860                 msDlc_FunctionExit();
861                 return;
862             }
863             else // g_DlcParameters.ucDlcPureImageMode == DLC_PURE_IMAGE_DLC_CURVE
864                 ;
865         }
866 
867         //=========================================================
868         // LC[6] - H0 ~ H12
869         //=========================================================
870         uwArraySum=0;
871         for(ucTmp=0;ucTmp<13;ucTmp++)
872             uwArraySum+= g_wLumaHistogram32H[ucTmp];
873         uwArrayComp = uwArraySum / 13;
874 
875         if (uwArrayComp < g_dwFactory_7)
876         {
877             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SD2 / (Lstep*128))+1;
878             if (ucTmp > LD2)
879                 ucTmp = LD2;
880             g_ucTable[6] = 0x67 - ucTmp;
881         }
882         else if (uwArrayComp > g_dwFactory_9)
883         {
884             ucTmp = ((uwArrayComp - g_dwFactory_9) * SD1 / (Hstep*128))+1;
885             if (ucTmp > LD1)
886                 ucTmp = LD1;
887             g_ucTable[6] = 0x67 + ucTmp;
888         }
889         else
890             g_ucTable[6] = 0x67;
891 
892         if ((g_ucTable[6]> s_Slope[Slope_6])&&((g_ucTable[6] - s_Slope[Slope_6])>=2))
893             g_ucTable[6] = g_ucTable[6] - 1;
894         else if ((s_Slope[Slope_6]> g_ucTable[6])&&((s_Slope[Slope_6] - g_ucTable[6])>=2))
895             g_ucTable[6] = g_ucTable[6] + 1;
896         else
897             g_ucTable[6] = s_Slope[Slope_6];
898 
899         //=========================================================
900         // LC[4] - H0 ~ H8
901         //=========================================================
902 
903         g_ucTable[4] = ((g_ucTable[6] - 0x07) * 2 / 3) + 0x07;
904 
905         uwArraySum = uwArraySum - (g_wLumaHistogram32H[12] + g_wLumaHistogram32H[11] + g_wLumaHistogram32H[10] + g_wLumaHistogram32H[9]);
906         uwArrayComp = uwArraySum/9;
907 
908         if (uwArrayComp < g_dwFactory_7)
909         {
910             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SC2 / (Lstep*128))+1;
911             if (ucTmp > LC2)
912                 ucTmp = LC2;
913             g_ucTable[4] = g_ucTable[4] - ucTmp;
914         }
915         else if (uwArrayComp > g_dwFactory_9)
916         {
917             ucTmp = ((uwArrayComp - g_dwFactory_9) * SC1 / (Hstep*128))+1;
918             if (ucTmp > LC1)
919                 ucTmp = LC1;
920             g_ucTable[4] = g_ucTable[4] + ucTmp;
921         }
922 
923         if ((g_ucTable[4]> s_Slope[Slope_4])&&((g_ucTable[4]- s_Slope[Slope_4])>=2))
924             g_ucTable[4] = g_ucTable[4] - 1;
925         else if ((s_Slope[Slope_4]> g_ucTable[4])&&((s_Slope[Slope_4]- g_ucTable[4])>=2))
926             g_ucTable[4] = g_ucTable[4] + 1;
927         else
928             g_ucTable[4] = s_Slope[Slope_4];
929 
930         //=========================================================
931         // LC[2] - H0 ~ H4
932         //=========================================================
933 
934         g_ucTable[2] = ((g_ucTable[4] - 0x07) * 1 / 2) + 0x07;
935         uwArraySum = uwArraySum - (g_wLumaHistogram32H[8] + g_wLumaHistogram32H[7] + g_wLumaHistogram32H[6] + g_wLumaHistogram32H[5]);
936         uwArrayComp = uwArraySum / 5;
937 
938         if (uwArrayComp < g_dwFactory_7)
939         {
940             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SB2 / (Lstep*128))+1;
941             if (ucTmp > LB2)
942                 ucTmp = LB2;
943             g_ucTable[2] = g_ucTable[2] - ucTmp;
944         }
945         else if (uwArrayComp > g_dwFactory_9)
946         {
947             ucTmp = ((uwArrayComp - g_dwFactory_9) * SB1 / (Hstep*128))+1;
948             if (ucTmp > LB1)
949                 ucTmp = LB1;
950             g_ucTable[2] = g_ucTable[2] + ucTmp;
951         }
952 
953         if ((g_ucTable[2]> s_Slope[Slope_2])&&((g_ucTable[2]- s_Slope[Slope_2])>=2))
954             g_ucTable[2] = g_ucTable[2] - 1;
955         else if ((s_Slope[Slope_2]> g_ucTable[2])&&((s_Slope[Slope_2]- g_ucTable[2])>=2))
956             g_ucTable[2] = g_ucTable[2] + 1;
957         else
958             g_ucTable[2] = s_Slope[Slope_2];
959 
960         //=========================================================
961         // LC[1] - H0 ~ H2
962         //=========================================================
963 
964         g_ucTable[1] = ((g_ucTable[2] - 0x07) * 1 / 2) + 0x07;
965         uwArraySum = uwArraySum - (g_wLumaHistogram32H[4] + g_wLumaHistogram32H[3]);
966         uwArrayComp = uwArraySum / 3;
967 
968         if (uwArrayComp < g_dwFactory_7)
969         {
970             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SA2 / (Lstep*128))+1;
971             if (ucTmp > LA2)
972                 ucTmp = LA2;
973 
974             if (g_ucTable[1] <= ucTmp)
975                 g_ucTable[1] = 0x00;
976             else
977                 g_ucTable[1] = g_ucTable[1] - ucTmp;
978         }
979         else if (uwArrayComp > g_dwFactory_9)
980         {
981             ucTmp = ((uwArrayComp - g_dwFactory_9) * SA1 / (Hstep*128))+1;
982             if (ucTmp > LA1)
983                 ucTmp = LA1;
984             g_ucTable[1] = g_ucTable[1] + ucTmp;
985         }
986 
987         if ((g_ucTable[1]> s_Slope[Slope_1])&&((g_ucTable[1]- s_Slope[Slope_1])>=1))
988             g_ucTable[1] = g_ucTable[1] - 1;
989         else if ((s_Slope[Slope_1]> g_ucTable[1])&&((s_Slope[Slope_1]- g_ucTable[1])>=2))
990             g_ucTable[1] = g_ucTable[1] + 1;
991         else
992             g_ucTable[1] = s_Slope[Slope_1];
993 
994         //=========================================================
995         // LC[9] - H19 ~ H31
996         //=========================================================
997 
998         uwArraySum=0;
999         for (ucTmp=19; ucTmp<32; ucTmp++)
1000             uwArraySum+= g_wLumaHistogram32H[ucTmp];
1001 
1002         uwArrayComp = uwArraySum / 13;
1003 
1004         if (uwArrayComp < g_dwFactory_7)
1005         {
1006             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SE1 / (Lstep*128))+1;
1007             if (ucTmp > LE1)
1008                 ucTmp = LE1;
1009             g_ucTable[9] = 0x97 + ucTmp;
1010         }
1011         else if (uwArrayComp > g_dwFactory_9)
1012         {
1013             ucTmp = ((uwArrayComp - g_dwFactory_9) * SE2 / (Hstep*128))+1;
1014             if (ucTmp > LE2)
1015                 ucTmp = LE2;
1016             g_ucTable[9] = 0x97 - ucTmp;
1017         }
1018         else
1019             g_ucTable[9] = 0x97;
1020 
1021         //=========================================================
1022         // LC[11] - H23 ~ H31
1023         //=========================================================
1024 
1025         g_ucTable[11] = 0xF7 - ((0xF7 - g_ucTable[9]) * 2 / 3);
1026         uwArraySum = uwArraySum - (g_wLumaHistogram32H[19] + g_wLumaHistogram32H[20] + g_wLumaHistogram32H[21] + g_wLumaHistogram32H[22]);
1027         uwArrayComp = uwArraySum / 9;
1028 
1029         if (uwArrayComp< g_dwFactory_7)
1030         {
1031             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SF1 / (Lstep*128))+1;
1032             if (ucTmp > LF1)
1033                 ucTmp = LF1;
1034             g_ucTable[11] = g_ucTable[11] + ucTmp;
1035         }
1036         else if (uwArrayComp > g_dwFactory_9)
1037         {
1038             ucTmp = ((uwArrayComp - g_dwFactory_9) * SF2 / (Hstep*128))+1;
1039             if (ucTmp > LF2)
1040                 ucTmp = LF2;
1041             g_ucTable[11] = g_ucTable[11] - ucTmp;
1042         }
1043 
1044         //=========================================================
1045         // LC[13] - H27 ~ H31
1046         //=========================================================
1047 
1048         g_ucTable[13] = 0xF7 - ((0xF7 - g_ucTable[11]) * 1 / 2);
1049         uwArraySum = uwArraySum - (g_wLumaHistogram32H[23] + g_wLumaHistogram32H[24] + g_wLumaHistogram32H[25] + g_wLumaHistogram32H[26]);
1050         uwArrayComp = uwArraySum / 5;
1051 
1052         if (uwArrayComp< g_dwFactory_7)
1053         {
1054             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SG1 / (Lstep*128))+1;
1055             if (ucTmp > LG1)
1056                 ucTmp = LG1;
1057 
1058             if ((0xFF - g_ucTable[13]) <= ucTmp)
1059                 g_ucTable[13] = 0xFF;
1060             else
1061                 g_ucTable[13] = g_ucTable[13] + ucTmp;
1062         }
1063         else if (uwArrayComp > g_dwFactory_9)
1064         {
1065             ucTmp = ((uwArrayComp - g_dwFactory_9) * SG2 / (Hstep*128))+1;
1066             if (ucTmp > LG2)
1067                 ucTmp = LG2;
1068             g_ucTable[13] = g_ucTable[13] - ucTmp;
1069         }
1070 
1071         //=========================================================
1072         // LC[14] - H29 ~ H31
1073         //=========================================================
1074 
1075         g_ucTable[14] = 0xF7 - ((0xF7 - g_ucTable[13]) * 1 / 2);
1076         uwArraySum = uwArraySum - (g_wLumaHistogram32H[27] + g_wLumaHistogram32H[28]);
1077         uwArrayComp = uwArraySum / 3;
1078 
1079         if (uwArrayComp < g_dwFactory_7)
1080         {
1081             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SH1 / (Lstep*128))+1;
1082             if (ucTmp > LH1)
1083             ucTmp = LH1;
1084 
1085             if ((0xFF - g_ucTable[14]) <= ucTmp)
1086                 g_ucTable[14] = 0xFF;
1087             else
1088                 g_ucTable[14] = g_ucTable[14] + ucTmp;
1089         }
1090         else if (uwArrayComp > g_dwFactory_9)
1091         {
1092             ucTmp = ((uwArrayComp - g_dwFactory_9) * SH2 / (Hstep*128))+1;
1093             if (ucTmp > LH2)
1094             ucTmp = LH2;
1095             g_ucTable[14] =g_ucTable[14] - ucTmp;
1096         }
1097 
1098         s_Slope[Slope_1] = g_ucTable[1];
1099         s_Slope[Slope_2] = g_ucTable[2];
1100         s_Slope[Slope_4] = g_ucTable[4];
1101         s_Slope[Slope_6] = g_ucTable[6];
1102 
1103         g_ucTable[3] = g_ucTable[2] + ((g_ucTable[4] - g_ucTable[2]) * 1 / 2);
1104         g_ucTable[5] = g_ucTable[4] + ((g_ucTable[6] - g_ucTable[4]) * 1 / 2);
1105         g_ucTable[7] = g_ucTable[6] + ((g_ucTable[9] - g_ucTable[6]) * 1 / 3);
1106         g_ucTable[8] = g_ucTable[6] + ((g_ucTable[9] - g_ucTable[6]) * 2 / 3);
1107         g_ucTable[10] = g_ucTable[9] + ((g_ucTable[11] - g_ucTable[9]) * 1 / 2);
1108         g_ucTable[12] = g_ucTable[11] + ((g_ucTable[13] - g_ucTable[11]) * 1 / 2);
1109         g_ucTable[0] = g_ucTable[1]/3; // Joseph 20081014
1110         g_ucTable[15] = 0xFF - ((0xFF - g_ucTable[14]) / 3);
1111         g_ucTable[15]= ((g_ucTable[15]) / 2) + (0xf7 / 2);
1112 
1113         // Get Average Value
1114         ucYAvg = msGetAverageLuminous();
1115 
1116         //printf("\r\n ucYAvg = %d",ucYAvg);
1117         if (ucYAvg >= g_DlcParameters.ucDlcYAvgThresholdH)
1118         {
1119             for (ucTmp=0; ucTmp<16; ucTmp++)
1120             {
1121                 g_DlcParameters.ucLumaCurve2[ucTmp] = g_DlcParameters.ucLumaCurve2_a[ucTmp];
1122             }
1123         }
1124         else if (ucYAvg <= g_DlcParameters.ucDlcYAvgThresholdL)
1125         {
1126             for (ucTmp=0; ucTmp<16; ucTmp++)
1127             {
1128                 g_DlcParameters.ucLumaCurve2[ucTmp] = g_DlcParameters.ucLumaCurve2_b[ucTmp];
1129             }
1130         }
1131         else // (ucYAvg > g_DlcParameters.ucDlcYAvgThresholdL) && (ucYAvg < g_DlcParameters.ucDlcYAvgThresholdH)
1132         {
1133             WORD wYavgW;
1134 
1135             wYavgW = ((WORD)(ucYAvg - g_DlcParameters.ucDlcYAvgThresholdL) * 128) / (g_DlcParameters.ucDlcYAvgThresholdH - g_DlcParameters.ucDlcYAvgThresholdL);
1136 
1137             for (ucTmp=0; ucTmp<16; ucTmp++)
1138             {
1139                 g_DlcParameters.ucLumaCurve2[ucTmp] = ((WORD)(wYavgW * g_DlcParameters.ucLumaCurve2_a[ucTmp]) + (WORD)((128 - wYavgW) * g_DlcParameters.ucLumaCurve2_b[ucTmp])) / 128;
1140             }
1141         }
1142 
1143         if (((WORD)g_ucTable[0] + g_DlcParameters.ucLumaCurve[0] + g_DlcParameters.ucLumaCurve2[0]) >= (0x07 + 0x07))
1144             g_ucTable[0] = g_ucTable[0]+(g_DlcParameters.ucLumaCurve[0]-0x07)+(g_DlcParameters.ucLumaCurve2[0]-0x07);
1145         else
1146             g_ucTable[0] = 0;
1147 
1148         g_ucTable[1] = g_ucTable[1]+(g_DlcParameters.ucLumaCurve[1]-0x17)+(g_DlcParameters.ucLumaCurve2[1]-0x17);
1149         g_ucTable[2] = g_ucTable[2]+(g_DlcParameters.ucLumaCurve[2]-0x27)+(g_DlcParameters.ucLumaCurve2[2]-0x27);
1150         g_ucTable[3] = g_ucTable[3]+(g_DlcParameters.ucLumaCurve[3]-0x37)+(g_DlcParameters.ucLumaCurve2[3]-0x37);
1151         g_ucTable[4] = g_ucTable[4]+(g_DlcParameters.ucLumaCurve[4]-0x47)+(g_DlcParameters.ucLumaCurve2[4]-0x47);
1152         g_ucTable[5] = g_ucTable[5]+(g_DlcParameters.ucLumaCurve[5]-0x57)+(g_DlcParameters.ucLumaCurve2[5]-0x57);
1153         g_ucTable[6] = g_ucTable[6]+(g_DlcParameters.ucLumaCurve[6]-0x67)+(g_DlcParameters.ucLumaCurve2[6]-0x67);
1154         g_ucTable[7] = g_ucTable[7]+(g_DlcParameters.ucLumaCurve[7]-0x77)+(g_DlcParameters.ucLumaCurve2[7]-0x77);
1155         g_ucTable[8] = g_ucTable[8]+(g_DlcParameters.ucLumaCurve[8]-0x87)+(g_DlcParameters.ucLumaCurve2[8]-0x87);
1156         g_ucTable[9] = g_ucTable[9]+(g_DlcParameters.ucLumaCurve[9]-0x97)+(g_DlcParameters.ucLumaCurve2[9]-0x97);
1157         g_ucTable[10] = g_ucTable[10]+(g_DlcParameters.ucLumaCurve[10]-0xa7)+(g_DlcParameters.ucLumaCurve2[10]-0xa7);
1158         g_ucTable[11] = g_ucTable[11]+(g_DlcParameters.ucLumaCurve[11]-0xb7)+(g_DlcParameters.ucLumaCurve2[11]-0xb7);
1159         g_ucTable[12] = g_ucTable[12]+(g_DlcParameters.ucLumaCurve[12]-0xc7)+(g_DlcParameters.ucLumaCurve2[12]-0xc7);
1160         g_ucTable[13] = g_ucTable[13]+(g_DlcParameters.ucLumaCurve[13]-0xd7)+(g_DlcParameters.ucLumaCurve2[13]-0xd7);
1161         g_ucTable[14] = g_ucTable[14]+(g_DlcParameters.ucLumaCurve[14]-0xe7)+(g_DlcParameters.ucLumaCurve2[14]-0xe7);
1162         if (((WORD)g_ucTable[15] + g_DlcParameters.ucLumaCurve[15] + g_DlcParameters.ucLumaCurve2[15] - 0xF7 - 0xF7) < 0xFF)
1163             g_ucTable[15] = g_ucTable[15]+(g_DlcParameters.ucLumaCurve[15]-0xf7)+(g_DlcParameters.ucLumaCurve2[15]-0xf7);
1164         else
1165             g_ucTable[15] = 0xFF;
1166 
1167         // Check Over Flow & Under Flow
1168         if (g_ucTable[15] < 0xD0)
1169             g_ucTable[15] = 0xFF;
1170         if (g_ucTable[14] < 0x90)
1171             g_ucTable[14] = 0xFF;
1172         if (g_ucTable[13] < 0x70)
1173             g_ucTable[13] = 0xFF;
1174         if (g_ucTable[12] < 0x60)
1175             g_ucTable[12] = 0xFF;
1176         if (g_ucTable[11] < 0x50)
1177             g_ucTable[11] = 0xFF;
1178         if (g_ucTable[2] > 0x90)
1179             g_ucTable[2] = 0x0;
1180         if (g_ucTable[1] > 0x60)
1181             g_ucTable[1] = 0x0;
1182         if (g_ucTable[0] > 0x30)
1183             g_ucTable[0] = 0x0;
1184     }
1185     else  //New DLC  Algorithm
1186     {
1187 
1188 #if 0 //For customer require. Run DLC algorithm when static field .
1189         if((g_wLumiAverageTemp-Pre_TotalYSum)==0)
1190         {
1191             return;
1192         }
1193         else
1194 #endif
1195         {
1196             ucYAvg = msGetAverageLuminous();
1197 
1198             for (ucTmp=0; ucTmp<32; ucTmp++)
1199             {
1200                 if(ucTmp==0)
1201                 {
1202                     uwHistogramAreaSum[ucTmp]=g_wLumaHistogram32H[ucTmp];
1203                 }
1204                 else
1205                 {
1206                     uwHistogramAreaSum[ucTmp] = uwHistogramAreaSum[ucTmp-1] + g_wLumaHistogram32H[ucTmp];
1207                 }
1208             }
1209 
1210             //Mantis issue of 0232938:Hsense A3 DLC uwHistogramAreaSum[31]=0 will code dump, the divisor can't to be equal to zero .
1211             if(uwHistogramAreaSum[31]==0)
1212             {
1213                 DLC_DEBUG(printf("\n Error in [New DLC Algorithm][ %s  , %d ]  uwHistogramAreaSum[31]=0 !!!\n", __FUNCTION__,__LINE__));
1214                 return;
1215             }
1216 
1217 
1218             // ~~~~~~~~~~~~~~~~~~~~~~~~Pure Image Detect  ~~~~~~~~~~~~~~~~~~~~~~~~
1219             if( ((g_ucHistogramMax-g_ucHistogramMin) <= 10) || (g_ucHistogramMin >= g_ucHistogramMax) )
1220             {
1221                 g_wPIAlpha = 256;
1222             }
1223             else if ((g_ucHistogramMax-g_ucHistogramMin) >= 26)
1224             {
1225                 g_wPIAlpha = 0;
1226             }
1227             else
1228             {
1229                 g_wPIAlpha = ((26-(g_ucHistogramMax-g_ucHistogramMin))*256+8)/16;
1230             }
1231 
1232             if(g_wPIAlpha <= 0)
1233             {
1234                 g_wPIAlpha = 0;
1235             }
1236             else if (g_wPIAlpha >= 256)
1237             {
1238                 g_wPIAlpha = 256;
1239             }
1240             else
1241             {
1242                 g_wPIAlpha = g_wPIAlpha;
1243             }
1244             // ~~~~~~~~~~~~~~~~~~~~~~~~Pure Image Detect End   ~~~~~~~~~~~~~~~~~~~~~~~~
1245 
1246             if(ucYAvg >= g_DlcParameters.ucDlcYAvgThresholdH)
1247             {
1248                 ucYAvgNormalize=g_DlcParameters.ucDlcSepPointL;
1249             }
1250             else if(ucYAvg <= g_DlcParameters.ucDlcYAvgThresholdL)
1251             {
1252                 ucYAvgNormalize=g_DlcParameters.ucDlcSepPointH;
1253             }
1254             else
1255             {
1256                 ucYAvgNormalize=((ucYAvg-g_DlcParameters.ucDlcYAvgThresholdL)*g_DlcParameters.ucDlcSepPointL
1257                                 +(g_DlcParameters.ucDlcYAvgThresholdH-ucYAvg)*g_DlcParameters.ucDlcSepPointH)
1258                                 /(g_DlcParameters.ucDlcYAvgThresholdH-g_DlcParameters.ucDlcYAvgThresholdL);
1259             }
1260 
1261             for (ucTmp=0; ucTmp<31; ucTmp++)
1262             {
1263                 if(((ucTmp+1)*8)<ucYAvgNormalize)
1264                 {
1265                     dwSumBelowAvg = uwHistogramAreaSum[ucTmp]
1266                                    +((ucYAvgNormalize-(ucTmp+1)*8)*g_wLumaHistogram32H[(ucTmp+1)]+4)/8;
1267                 }
1268             }
1269 
1270             dwSumAboveAvg = uwHistogramAreaSum[31] - dwSumBelowAvg ;
1271 
1272             His_ratio_BelowAvg = (4096*dwSumBelowAvg + (uwHistogramAreaSum[31]/2))/uwHistogramAreaSum[31];
1273             His_ratio_AboveAvg = (4096*dwSumAboveAvg + (uwHistogramAreaSum[31]/2))/uwHistogramAreaSum[31];
1274 
1275             if(His_ratio_BelowAvg > 128)
1276             {
1277                 His_ratio_BelowAvg = 128;
1278             }
1279             if(His_ratio_AboveAvg > 128)
1280             {
1281                 His_ratio_AboveAvg = 128;
1282             }
1283 
1284             for (ucTmp=0; ucTmp<32; ucTmp++)
1285             {
1286                 uwHistogram_BLE_AreaSum[ucTmp]=((1024*uwHistogramAreaSum[ucTmp])+uwHistogramAreaSum[31]/2)/uwHistogramAreaSum[31];
1287 
1288                 if(uwHistogram_BLE_AreaSum[ucTmp]>1024)
1289                 {
1290                     uwHistogram_BLE_AreaSum[ucTmp]=1024;
1291                 }
1292             }
1293             //uwHistogram_BLE_AreaSum[ucTmp] = histogram Add to normalize 1024
1294 
1295             if ((dwSumBelowAvg == 0)&&(dwSumAboveAvg != 0))
1296             {
1297                 for (ucTmp = 0; ucTmp < 32; ucTmp++)
1298                 {
1299                     if (ucYAvgNormalize >= (((WORD)ucTmp+1)*8))
1300                     {
1301                          uwHistogramAreaSum[ucTmp] = (ucTmp+1)*8*4;
1302                     }
1303                     else
1304                     {
1305                         uwHistogramAreaSum[ucTmp] = ucYAvgNormalize*4 + ((DWORD)uwHistogramAreaSum[ucTmp]*(1024-ucYAvgNormalize*4)+(dwSumAboveAvg/2))/dwSumAboveAvg;
1306                         if(uwHistogramAreaSum[ucTmp] >= ((ucTmp+1)*32))
1307                         {
1308                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) + (((uwHistogramAreaSum[ucTmp] - ((ucTmp+1)*32))*His_ratio_AboveAvg)/128);
1309                         }
1310                         else
1311                         {
1312                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) - (((((ucTmp+1)*32) - uwHistogramAreaSum[ucTmp])*His_ratio_AboveAvg)/128);
1313                         }
1314                     }
1315                 }
1316             }
1317             else if ((dwSumBelowAvg != 0)&&(dwSumAboveAvg == 0))
1318             {
1319                 for (ucTmp = 0; ucTmp < 32; ucTmp++)
1320                 {
1321                     if (ucYAvgNormalize >= (((WORD)ucTmp+1)*8))
1322                     {
1323                         uwHistogramAreaSum[ucTmp] = ((DWORD)uwHistogramAreaSum[ucTmp]*ucYAvgNormalize*4+(dwSumBelowAvg/2))/dwSumBelowAvg;
1324                         if(uwHistogramAreaSum[ucTmp] >= ((ucTmp+1)*32))
1325                         {
1326                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) + (((uwHistogramAreaSum[ucTmp] - ((ucTmp+1)*32))*His_ratio_BelowAvg)/128);
1327                         }
1328                         else
1329                         {
1330                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) - (((((ucTmp+1)*32) - uwHistogramAreaSum[ucTmp])*His_ratio_BelowAvg)/128);
1331                         }
1332                     }
1333                     else
1334                     {
1335                          uwHistogramAreaSum[ucTmp] = (ucTmp+1)*8*4;
1336                     }
1337                 }
1338             }
1339             else if((dwSumBelowAvg == 0)&&(dwSumAboveAvg == 0))
1340             {
1341                 for (ucTmp = 0; ucTmp < 32; ucTmp++)
1342                 {
1343                     uwHistogramAreaSum[ucTmp] = (ucTmp+1)*8*4;
1344                 }
1345             }
1346             else
1347             {
1348                 for (ucTmp = 0; ucTmp < 32; ucTmp++)
1349                 {
1350                     if (ucYAvgNormalize >= (((WORD)ucTmp+1)*8))
1351                     {
1352                          uwHistogramAreaSum[ucTmp] = ((DWORD)uwHistogramAreaSum[ucTmp]*ucYAvgNormalize*4+(dwSumBelowAvg/2))/dwSumBelowAvg;
1353                          if(uwHistogramAreaSum[ucTmp] >= ((ucTmp+1)*32))
1354                          {
1355                              uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) + (((uwHistogramAreaSum[ucTmp] - ((ucTmp+1)*32))*His_ratio_BelowAvg)/128);
1356                          }
1357                          else
1358                          {
1359                              uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) - (((((ucTmp+1)*32) - uwHistogramAreaSum[ucTmp])*His_ratio_BelowAvg)/128);
1360                          }
1361                     }
1362                     else
1363                     {
1364                         uwHistogramAreaSum[ucTmp] = ucYAvgNormalize*4 + ((uwHistogramAreaSum[ucTmp]-dwSumBelowAvg)*(1024-ucYAvgNormalize*4)+(dwSumAboveAvg/2))/dwSumAboveAvg;
1365 
1366                         if(uwHistogramAreaSum[ucTmp] >= ((ucTmp+1)*32))
1367                         {
1368                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) + (((uwHistogramAreaSum[ucTmp] - ((ucTmp+1)*32))*His_ratio_AboveAvg)/128);
1369                         }
1370                         else
1371                         {
1372                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) - (((((ucTmp+1)*32) - uwHistogramAreaSum[ucTmp])*His_ratio_AboveAvg)/128);
1373                         }
1374                     }
1375                 }
1376             }
1377 
1378             //down sampling to 16 numbers (store to uwHistogramAreaSum[0~15])
1379 
1380             for (ucTmp=0; ucTmp<16; ucTmp++)
1381             {
1382                 uwHistogramAreaSum[ucTmp] = uwHistogramAreaSum[ucTmp*2]; //
1383             }
1384 
1385             //histogram_DLC limit by slope
1386             uwHistogramMax = 0;
1387 
1388             for (ucTmp=0; ucTmp<=16; ucTmp++)
1389             {
1390                 if (ucTmp == 0)
1391                 {
1392                     dwHistogramDlcSlop[ucTmp] = ((uwHistogramAreaSum[ucTmp] - 0) * 256 + 16) / 64;
1393 
1394                     if (dwHistogramDlcSlop[ucTmp] < 257)
1395                     {
1396                         dwHistogramDlcSlop[ucTmp] = 257;
1397                     }
1398                 }
1399                 else if (ucTmp == 16)
1400                 {
1401                     dwHistogramDlcSlop[ucTmp] = ((1024 - uwHistogramAreaSum[ucTmp-1]) * 256 + 16) / 64;
1402 
1403                     if (dwHistogramDlcSlop[ucTmp] < 257)
1404                     {
1405                         dwHistogramDlcSlop[ucTmp] = 257;
1406                     }
1407                 }
1408                 else
1409                 {
1410                     dwHistogramDlcSlop[ucTmp] = ((uwHistogramAreaSum[ucTmp] - uwHistogramAreaSum[ucTmp-1]) * 256 + 32) / 64; //find slop
1411 
1412                     if (dwHistogramDlcSlop[ucTmp] < 257)
1413                     {
1414                         dwHistogramDlcSlop[ucTmp] = 257; //(only take the slop > 1)
1415                     }
1416                 }
1417 
1418                 dwHistogramDlcSlop[ucTmp] = ((g_DlcParameters.ucDlcHistogramLimitCurve[ucTmp]*512)+((dwHistogramDlcSlop[ucTmp]-256)/2)) / (dwHistogramDlcSlop[ucTmp]-256) ; //SlopRatio 8bit 256=1x
1419 
1420                 if(dwHistogramDlcSlop[ucTmp] > 0xFFFF)
1421                 {
1422                     dwHistogramDlcSlop[ucTmp] = 0xFFFF;
1423                 }
1424             }
1425 
1426             sepPoint = (ucYAvgNormalize-8)/16;
1427             sepPoint0 = sepPoint-1;
1428             sepPoint1 = sepPoint;
1429             sepPoint2 = sepPoint+1;
1430 
1431             if(ucYAvgNormalize < (sepPoint2*16))
1432             {
1433                 //sepPoint0 = sepPoint;
1434                 //sepPoint1 = sepPoint+1;
1435                 //sepPoint2 = sepPoint+2;
1436                 coeff0 = (((3*((sepPoint2*16)-ucYAvgNormalize)*((sepPoint2*16)-ucYAvgNormalize))+64*((sepPoint2*16)-ucYAvgNormalize)+320));
1437                 coeff1 = ((1408-6*((sepPoint2*16)-ucYAvgNormalize)*((sepPoint2*16)-ucYAvgNormalize)));
1438                 coeff2 = 2048-coeff0-coeff1;
1439             }
1440             else
1441             {
1442                 //sepPoint0 = sepPoint-1;
1443                 //sepPoint1 = sepPoint;
1444                 //sepPoint2 = sepPoint+1;
1445                 coeff0 = (((3*(ucYAvgNormalize-(sepPoint2*16))*(ucYAvgNormalize-(sepPoint2*16)))-64*(ucYAvgNormalize-(sepPoint2*16))+320));
1446                 coeff1 = ((1408-6*(ucYAvgNormalize-(sepPoint2*16))*(ucYAvgNormalize-(sepPoint2*16))));
1447                 coeff2 = 2048-coeff0-coeff1;
1448             }
1449 
1450             //Calculate for sepPoint0~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1451             uwHistogramSlopRatioL = 256;
1452             uwHistogramSlopRatioH = 256;
1453 
1454             for (ucTmp = 0; ucTmp<= (sepPoint0+1); ucTmp++)
1455             {
1456                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1457 
1458                 if (uwHistogramMax2nd < uwHistogramSlopRatioL)
1459                 {
1460                     uwHistogramSlopRatioL = uwHistogramMax2nd;
1461                 }
1462             }
1463 
1464             for (ucTmp = sepPoint0; ucTmp<=16; ucTmp++)
1465             {
1466                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1467 
1468                 if (uwHistogramMax2nd < uwHistogramSlopRatioH)
1469                 {
1470                     uwHistogramSlopRatioH = uwHistogramMax2nd;
1471                 }
1472             }
1473 
1474             // calcute final target curve for sepPoint0
1475             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1476             {
1477                 if (sepPoint0 >= (WORD)ucTmp)
1478                 {
1479                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1480                     {
1481                         g_uwCurveHistogram0[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioL + 128) / 256 ) ;
1482                     }
1483                     else
1484                     {
1485                         g_uwCurveHistogram0[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioL + 128) / 256);
1486                     }
1487                 }
1488                 else
1489                 {
1490                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1491                     {
1492                         g_uwCurveHistogram0[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioH + 128) / 256 ) ;
1493                     }
1494                     else
1495                     {
1496                         g_uwCurveHistogram0[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioH + 128) / 256);
1497                     }
1498                 }
1499             }
1500 
1501             //Calculate for sepPoint1~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1502             uwHistogramSlopRatioL = 256;
1503             uwHistogramSlopRatioH = 256;
1504 
1505             for (ucTmp = 0; ucTmp<= (sepPoint1+1); ucTmp++)
1506             {
1507                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1508 
1509                 if (uwHistogramMax2nd < uwHistogramSlopRatioL)
1510                 {
1511                     uwHistogramSlopRatioL = uwHistogramMax2nd;
1512                 }
1513             }
1514 
1515             for (ucTmp = sepPoint1; ucTmp<=16; ucTmp++)
1516             {
1517                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1518 
1519                 if (uwHistogramMax2nd < uwHistogramSlopRatioH)
1520                 {
1521                     uwHistogramSlopRatioH = uwHistogramMax2nd;
1522                 }
1523             }
1524 
1525             // calcute final target curve for sepPoint1
1526             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1527             {
1528                 if (sepPoint1 >= (WORD)ucTmp)
1529                 {
1530                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1531                     {
1532                         g_uwCurveHistogram1[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioL + 128) / 256 );
1533                     }
1534                     else
1535                     {
1536                         g_uwCurveHistogram1[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioL + 128) / 256 );
1537                     }
1538                 }
1539                 else
1540                 {
1541                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1542                     {
1543                         g_uwCurveHistogram1[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioH + 128) / 256 );
1544                     }
1545                     else
1546                     {
1547                         g_uwCurveHistogram1[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioH + 128) / 256 );
1548                     }
1549                 }
1550             }
1551 
1552             //Calculate for sepPoint2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1553             uwHistogramSlopRatioL = 256;
1554             uwHistogramSlopRatioH = 256;
1555 
1556             for (ucTmp = 0; ucTmp<= (sepPoint2+1); ucTmp++)
1557             {
1558                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1559 
1560                 if (uwHistogramMax2nd < uwHistogramSlopRatioL)
1561                 {
1562                     uwHistogramSlopRatioL = uwHistogramMax2nd;
1563                 }
1564             }
1565 
1566             for (ucTmp = sepPoint2; ucTmp<=16; ucTmp++)
1567             {
1568                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1569 
1570                 if (uwHistogramMax2nd < uwHistogramSlopRatioH)
1571                 {
1572                     uwHistogramSlopRatioH = uwHistogramMax2nd;
1573                 }
1574             }
1575 
1576             // calcute final target curve for sepPoint2
1577             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1578             {
1579                 if (sepPoint2 >= (WORD)ucTmp)
1580                 {
1581                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1582                     {
1583                         g_uwCurveHistogram2[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioL + 128) / 256 );
1584                     }
1585                     else
1586                     {
1587                         g_uwCurveHistogram2[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioL + 128) / 256 );
1588                     }
1589                 }
1590                 else
1591                 {
1592                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1593                     {
1594                         g_uwCurveHistogram2[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioH + 128) / 256 );
1595                     }
1596                     else
1597                     {
1598                         g_uwCurveHistogram2[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioH + 128) / 256);
1599                     }
1600                 }
1601             }
1602 
1603             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1604             {
1605                 g_uwCurveHistogram[ucTmp] = (coeff0*g_uwCurveHistogram0[ucTmp]+coeff1*g_uwCurveHistogram1[ucTmp]+coeff2*g_uwCurveHistogram2[ucTmp]+1024)/2048;
1606             }
1607             ///////////// Histogram_DLC add Low_Pass_filter /////////////
1608 
1609             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1610             {
1611                 if (ucTmp == 0)
1612                 {
1613                     g_uwCurveHistogram[ucTmp] = (5*g_uwCurveHistogram[ucTmp] + g_uwCurveHistogram[ucTmp+1]+4)/8;
1614                 }
1615                 else if (ucTmp == 15)
1616                 {
1617                     g_uwCurveHistogram[ucTmp] = (g_uwCurveHistogram[ucTmp-1] + 5*g_uwCurveHistogram[ucTmp] + 2048 + 4)/8;
1618                 }
1619                 else
1620                 {
1621                     g_uwCurveHistogram[ucTmp] = (g_uwCurveHistogram[ucTmp-1] + 6*g_uwCurveHistogram[ucTmp] + g_uwCurveHistogram[ucTmp+1]+4)/8;
1622                 }
1623             }
1624 
1625 
1626 #if 0  //Ranma Histogram DLC low pass
1627             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1628             {
1629                 uwHistogramAreaSum[ucTmp+16] = g_uwCurveHistogram[ucTmp];
1630 
1631                 if (ucTmp == sepPoint0)
1632                 {
1633                     uwHistogramAreaSum[ucTmp+16] = (g_uwCurveHistogram[ucTmp-1]+g_uwCurveHistogram[ucTmp]*2+g_uwCurveHistogram[ucTmp+1]+2)/4;
1634                     uwHistogramAreaSum[ucTmp] = coeff0*uwHistogramAreaSum[ucTmp+16]+(2048-coeff0)*uwHistogramAreaSum[ucTmp];
1635                 }
1636                 if (ucTmp == sepPoint1)
1637                 {
1638                     uwHistogramAreaSum[ucTmp+16] = (g_uwCurveHistogram[ucTmp-2]+g_uwCurveHistogram[ucTmp-1]*2+g_uwCurveHistogram[ucTmp]*2+g_uwCurveHistogram[ucTmp+1]*2+g_uwCurveHistogram[ucTmp+2]+4)/8;
1639                     uwHistogramAreaSum[ucTmp] = coeff1*uwHistogramAreaSum[ucTmp+16]+(2048-coeff1)*uwHistogramAreaSum[ucTmp];
1640                 }
1641                 if (ucTmp == sepPoint2)
1642                 {
1643                     uwHistogramAreaSum[ucTmp+16] = (g_uwCurveHistogram[ucTmp-1]+g_uwCurveHistogram[ucTmp]*2+g_uwCurveHistogram[ucTmp+1]+2)/4;
1644                     uwHistogramAreaSum[ucTmp] = coeff2*uwHistogramAreaSum[ucTmp+16]+(2048-coeff2)*uwHistogramAreaSum[ucTmp];
1645                 }
1646             }
1647 
1648             LpfBlendAlpha_temp1 = abs(g_uwCurveHistogram[sepPoint0]-g_uwCurveHistogram[sepPoint1]*2+g_uwCurveHistogram[sepPoint2]);
1649             LpfBlendAlpha_temp2 = abs(g_uwCurveHistogram[sepPoint1]-g_uwCurveHistogram[sepPoint2]*2+g_uwCurveHistogram[sepPoint2+1]);
1650 
1651             if(LpfBlendAlpha_temp1 > LpfBlendAlpha_temp2)
1652             {
1653                 LpfBlendAlpha = LpfBlendAlpha_temp1;
1654             }
1655             else
1656             {
1657                 LpfBlendAlpha = LpfBlendAlpha_temp2;
1658             }
1659 
1660             if (LpfBlendAlpha >64)
1661             {
1662                 LpfBlendAlpha = 64;
1663             }
1664 
1665             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1666             {
1667                 g_uwCurveHistogram[ucTmp] = (uwHistogramAreaSum[ucTmp+16]*LpfBlendAlpha + (64-LpfBlendAlpha)*g_uwCurveHistogram[ucTmp]+32)/64;
1668 
1669                 if(g_uwCurveHistogram[ucTmp]>1023)
1670                 {
1671                     g_uwCurveHistogram[ucTmp]=1023;
1672                 }
1673             }
1674 
1675 #endif
1676             // Calculate curve by Yavg
1677 
1678             //DLC_PRINTF("\r\n ucYAvg = %d",ucYAvg);
1679             if (ucYAvg <= g_DlcParameters.ucDlcYAvgThresholdL)
1680             {
1681                 for (ucTmp=0; ucTmp<16; ucTmp++)
1682                 {
1683                     g_ucCurveYAvg[ucTmp] = g_DlcParameters.ucLumaCurve2_a[ucTmp];
1684                 }
1685             }
1686             else if (ucYAvg <= g_DlcParameters.ucDlcYAvgThresholdM)
1687             {
1688                 for (ucTmp=0; ucTmp<16; ucTmp++)
1689                 {
1690                     g_ucCurveYAvg[ucTmp] = (((WORD)g_DlcParameters.ucLumaCurve[ucTmp] * (ucYAvg - g_DlcParameters.ucDlcYAvgThresholdL)) / (g_DlcParameters.ucDlcYAvgThresholdM - g_DlcParameters.ucDlcYAvgThresholdL)) +
1691                                            (g_DlcParameters.ucLumaCurve2_a[ucTmp] - (((WORD)g_DlcParameters.ucLumaCurve2_a[ucTmp] * (ucYAvg - g_DlcParameters.ucDlcYAvgThresholdL)) / ( g_DlcParameters.ucDlcYAvgThresholdM - g_DlcParameters.ucDlcYAvgThresholdL)));
1692                 }
1693             }
1694             else if (ucYAvg <= g_DlcParameters.ucDlcYAvgThresholdH)
1695             {
1696                 for (ucTmp=0; ucTmp<16; ucTmp++)
1697                 {
1698                     g_ucCurveYAvg[ucTmp] = ((WORD)g_DlcParameters.ucLumaCurve2_b[ucTmp] * (ucYAvg - g_DlcParameters.ucDlcYAvgThresholdM) / (g_DlcParameters.ucDlcYAvgThresholdH - g_DlcParameters.ucDlcYAvgThresholdM)) +
1699                                            (g_DlcParameters.ucLumaCurve[ucTmp] - ((WORD)g_DlcParameters.ucLumaCurve[ucTmp] * (ucYAvg - g_DlcParameters.ucDlcYAvgThresholdM) / (g_DlcParameters.ucDlcYAvgThresholdH - g_DlcParameters.ucDlcYAvgThresholdM)));
1700                 }
1701             }
1702             else // if (ucYAvg > g_DlcParameters.ucDlcYAvgThresholdH)
1703             {
1704                 for (ucTmp=0; ucTmp<16; ucTmp++)
1705                 {
1706                     g_ucCurveYAvg[ucTmp] = g_DlcParameters.ucLumaCurve2_b[ucTmp];
1707                 }
1708             }
1709 
1710             belnging_factor = g_DlcParameters.ucDlcCurveModeMixAlpha;
1711 
1712             // Decide Target Curve
1713             Diff_L =g_DlcParameters.ucDlcDiffBase_L;
1714             Diff_M =g_DlcParameters.ucDlcDiffBase_M;
1715             Diff_H =g_DlcParameters.ucDlcDiffBase_H;
1716 
1717             if (ucPre_YAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdL*4)
1718             {
1719                 Pre_YAvg_base = Diff_L*4;
1720             }
1721             else if (ucPre_YAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdM*4)
1722             {
1723                 if(Diff_M >= Diff_L)
1724                 {
1725                     Pre_YAvg_base = Diff_L*4 + (((Diff_M-Diff_L)*(ucPre_YAvg*4-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1726                 }
1727                 else
1728                 {
1729                     Pre_YAvg_base = Diff_L*4 - (((Diff_L-Diff_M)*(ucPre_YAvg*4-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1730                 }
1731             }
1732             else if (ucPre_YAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdH*4)
1733             {
1734                 if(Diff_H >= Diff_M)
1735                 {
1736                     Pre_YAvg_base = Diff_M*4 + (((Diff_H- Diff_M)*(ucPre_YAvg*4- g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1737                 }
1738                 else
1739                 {
1740                     Pre_YAvg_base = Diff_M*4 - (((Diff_M-Diff_H)*(ucPre_YAvg*4-g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH - g_DlcParameters.ucDlcYAvgThresholdM));
1741                 }
1742             }
1743             else
1744             {
1745                 Pre_YAvg_base = Diff_H*4;
1746             }
1747 
1748             if (ucYAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdL*4)
1749             {
1750                 YAvg_base = Diff_L*4;
1751             }
1752             else if (ucYAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdM*4)
1753             {
1754                 if(Diff_M >= Diff_L)
1755                 {
1756                     YAvg_base = Diff_L*4 + (((Diff_M-Diff_L)*( ucYAvg*4-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1757                 }
1758                 else
1759                 {
1760                     YAvg_base = Diff_L*4 - (((Diff_L-Diff_M)*( ucYAvg*4-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1761                 }
1762             }
1763             else if (ucYAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdH*4)
1764             {
1765                 if(Diff_H >= Diff_M)
1766                 {
1767                     YAvg_base = Diff_M*4 + (((Diff_H- Diff_M)*( ucYAvg*4- g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1768                 }
1769                 else
1770                 {
1771                     YAvg_base = Diff_M*4 - (((Diff_M-Diff_H)*(ucYAvg*4-g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH -g_DlcParameters.ucDlcYAvgThresholdM));
1772                 }
1773             }
1774             else
1775             {
1776                 YAvg_base = Diff_H*4;
1777             }
1778 
1779             YAvg_base = (Pre_YAvg_base + YAvg_base +1)/2;
1780 
1781             //Mantis issue of 0232938:Hsense A3 DLC the divisor can't to be equal to zero .
1782             if(YAvg_base==0)
1783             {
1784                 DLC_DEBUG(printf("\n Error in  [New DLC Algorithm][ %s  , %d ]  YAvg_base=0 !!!\n", __FUNCTION__,__LINE__));
1785                 return;
1786             }
1787 
1788             if (g_DlcParameters.ucDlcCurveMode == 0 ) //DLC_CURVE_MODE_BY_YAVG)
1789             {
1790                 CurveDiff = g_DlcParameters.ucDlcCurveDiff_H_TH;
1791                 for (ucTmp=0; ucTmp<16; ucTmp++)
1792                 {
1793                     g_uwTable[ucTmp] = (g_ucCurveYAvg[ucTmp]<<2);
1794                 }
1795             }
1796             else if (g_DlcParameters.ucDlcCurveMode == 1 ) //DLC_CURVE_MODE_BY_HISTOGRAM)
1797             {
1798                 CurveDiff = g_DlcParameters.ucDlcCurveDiff_H_TH;
1799                 for (ucTmp=0; ucTmp<16; ucTmp++)
1800                 {
1801                     g_uwTable[ucTmp] = g_uwCurveHistogram[ucTmp];
1802                 }
1803             }
1804             else
1805             {
1806                 CurveDiff = 0;
1807                 CurveDiff_Coring = 0;
1808                 for (ucTmp=0; ucTmp<16; ucTmp++)
1809                 {
1810                     g_uwTable[ucTmp] = (((WORD)(g_ucCurveYAvg[ucTmp]<<2) * belnging_factor) + ((g_uwCurveHistogram[ucTmp]) * (128 - belnging_factor))) / 128;
1811 
1812                     if (g_uwPre_CurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdL*4)
1813                     {
1814                         Pre_CurveDiff_base = Diff_L*4;
1815                     }
1816                     else if (g_uwPre_CurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdM*4)
1817                     {
1818                         if(Diff_M >= Diff_L)
1819                         {
1820                             Pre_CurveDiff_base = Diff_L*4 + (((Diff_M-Diff_L)*(g_uwPre_CurveHistogram[ucTmp]-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1821                         }
1822                         else
1823                         {
1824                             Pre_CurveDiff_base = Diff_L*4 - (((Diff_L-Diff_M)*(g_uwPre_CurveHistogram[ucTmp]-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1825                         }
1826                     }
1827                     else if (g_uwPre_CurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdH*4)
1828                     {
1829                         if(Diff_H >= Diff_M)
1830                         {
1831                             Pre_CurveDiff_base = Diff_M*4 + (((Diff_H- Diff_M)*(g_uwPre_CurveHistogram[ucTmp]- g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1832                         }
1833                         else
1834                         {
1835                             Pre_CurveDiff_base = Diff_M*4 -(((Diff_M- Diff_H)*(g_uwPre_CurveHistogram[ucTmp]- g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1836                         }
1837                     }
1838                     else
1839                     {
1840                         Pre_CurveDiff_base = Diff_H*4;
1841                     }
1842 
1843                     if (g_uwCurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdL*4)
1844                     {
1845                         CurveDiff_base = Diff_L*4;
1846                     }
1847                     else if (g_uwCurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdM*4)
1848                     {
1849                         if(Diff_M >= Diff_L)
1850                         {
1851                             CurveDiff_base = Diff_L*4 + (((Diff_M-Diff_L)*(g_uwCurveHistogram[ucTmp]-g_DlcParameters.ucDlcYAvgThresholdL*4))
1852                                         /(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1853                         }
1854                         else
1855                         {
1856                             CurveDiff_base = Diff_L*4 - (((Diff_L-Diff_M)*(g_uwCurveHistogram[ucTmp]-g_DlcParameters.ucDlcYAvgThresholdL*4))
1857                                         /(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1858                         }
1859                     }
1860                     else if (g_uwCurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdH*4)
1861                     {
1862                         if(Diff_H >= Diff_M)
1863                         {
1864                             CurveDiff_base = Diff_M*4 + (((Diff_H- Diff_M)*(g_uwCurveHistogram[ucTmp]- g_DlcParameters.ucDlcYAvgThresholdM*4))
1865                                         /(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1866                         }
1867                         else
1868                         {
1869                             CurveDiff_base = Diff_M*4 -(((Diff_M- Diff_H)*(g_uwCurveHistogram[ucTmp]- g_DlcParameters.ucDlcYAvgThresholdM*4))
1870                                         /(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1871                         }
1872                     }
1873                     else
1874                     {
1875                         CurveDiff_base = Diff_H*4;
1876                     }
1877 
1878                     CurveDiff_base = (Pre_CurveDiff_base + CurveDiff_base+1)/2;
1879 
1880                     //Mantis issue of 0232938:Hsense A3 DLC the divisor can't to be equal to zero .
1881                     if(CurveDiff_base==0)
1882                     {
1883                         DLC_DEBUG(printf("\n Error in  [New DLC Algorithm][ %s  , %d ]  CurveDiff_base=0 !!!\n", __FUNCTION__,__LINE__));
1884                         return;
1885                     }
1886 
1887                     if(ucTmp*16+8 <= ucYAvgNormalize)
1888                     {
1889                         if (ucTmp == 0)
1890                         {
1891                             CurveDiff_Coring = (His_ratio_BelowAvg*(1024-uwHistogram_BLE_AreaSum[1])*abs(g_uwCurveHistogram[ucTmp]-g_uwPre_CurveHistogram[ucTmp]))/(15*CurveDiff_base*128);
1892                         }
1893                         else
1894                         {
1895                             CurveDiff_Coring = (His_ratio_BelowAvg*(1024-(uwHistogram_BLE_AreaSum[ucTmp*2+1]- uwHistogram_BLE_AreaSum[ucTmp*2-1]))*abs(g_uwCurveHistogram[ucTmp]-g_uwPre_CurveHistogram[ucTmp]))/(15*CurveDiff_base*128);
1896                         }
1897                     }
1898                     else
1899                     {
1900                         CurveDiff_Coring = (His_ratio_AboveAvg*(1024-(uwHistogram_BLE_AreaSum[ucTmp*2+1]- uwHistogram_BLE_AreaSum[ucTmp*2-1]))*abs(g_uwCurveHistogram[ucTmp]-g_uwPre_CurveHistogram[ucTmp]))/(15*CurveDiff_base*128);
1901                     }
1902 
1903                     CurveDiff_Coring = CurveDiff_Coring/6;
1904 
1905                     CurveDiff = CurveDiff + CurveDiff_Coring ;
1906                     g_uwPre_CurveHistogram[ucTmp] = g_uwCurveHistogram[ucTmp];
1907                 }
1908             }
1909 
1910             if(CurveDiff <= g_DlcParameters.ucDlcCurveDiff_L_TH)
1911             {
1912                 g_ucDlcFlickAlpha_Max = g_DlcParameters.ucDlcAlphaBlendingMin;
1913             }
1914             else if(CurveDiff >= g_DlcParameters.ucDlcCurveDiff_H_TH)
1915             {
1916                 g_ucDlcFlickAlpha_Max = g_DlcParameters.ucDlcAlphaBlendingMax; ;
1917             }
1918             else
1919             {
1920                 g_ucDlcFlickAlpha_Max = (((g_DlcParameters.ucDlcAlphaBlendingMax-g_DlcParameters.ucDlcAlphaBlendingMin)*(CurveDiff - g_DlcParameters.ucDlcCurveDiff_L_TH))/
1921                                         (g_DlcParameters.ucDlcCurveDiff_H_TH - g_DlcParameters.ucDlcCurveDiff_L_TH))+g_DlcParameters.ucDlcAlphaBlendingMin;
1922             }
1923 
1924             Delta_YAvg = (96*4*abs(ucYAvg - ucPre_YAvg)+ YAvg_base/2)/YAvg_base;
1925 
1926             if(Delta_YAvg > 256)
1927             {
1928                 Delta_YAvg = 256;
1929             }
1930             else
1931             {
1932                 Delta_YAvg = Delta_YAvg;
1933             }
1934 
1935             YAvg_TH_H = g_DlcParameters.ucDlcYAVG_H_TH;
1936             YAvg_TH_L = g_DlcParameters.ucDlcYAVG_L_TH;
1937 
1938             if(Delta_YAvg < YAvg_TH_L)
1939             {
1940                 g_ucDlcFlickAlpha_Min = g_DlcParameters.ucDlcAlphaBlendingMin;
1941             }
1942             else if(Delta_YAvg >= YAvg_TH_H)
1943             {
1944                 g_ucDlcFlickAlpha_Min = g_DlcParameters.ucDlcAlphaBlendingMax;
1945             }
1946             else
1947             {
1948                 g_ucDlcFlickAlpha_Min = ((Delta_YAvg- YAvg_TH_L)*( g_DlcParameters.ucDlcAlphaBlendingMax- g_DlcParameters.ucDlcAlphaBlendingMin)
1949                                         + ( YAvg_TH_H-YAvg_TH_L)/2)/( YAvg_TH_H-YAvg_TH_L)+g_DlcParameters.ucDlcAlphaBlendingMin;
1950             }
1951 
1952             ucPre_YAvg = ucYAvg;
1953 
1954             if (g_ucDlcFlickAlpha_Max > g_ucDlcFlickAlpha_Min)
1955             {
1956                 g_ucDlcFlickAlpha_Max = g_ucDlcFlickAlpha_Max;
1957                 g_ucDlcFlickAlpha_Min = g_ucDlcFlickAlpha_Min;
1958             }
1959             else
1960             {
1961                 g_ucDlcFlickAlpha_temp = g_ucDlcFlickAlpha_Max;
1962                 g_ucDlcFlickAlpha_Max = g_ucDlcFlickAlpha_Min;
1963                 g_ucDlcFlickAlpha_Min = g_ucDlcFlickAlpha_temp;
1964             }
1965 
1966             g_ucDlcFlickAlpha  = ( g_DlcParameters.ucDlcFlicker_alpha*g_ucDlcFlickAlpha_Max + (256- g_DlcParameters.ucDlcFlicker_alpha)*g_ucDlcFlickAlpha_Min+128)/256;
1967 
1968         }
1969     }
1970 
1971     if( g_DlcParameters.bEnableBLE )
1972     {
1973         BYTE uc_cut,uc_lowbound,uc_highbound,i;
1974 
1975         // combine BLE with software
1976         // BLE - get cut point
1977         uc_cut = g_ucHistogramMin;
1978 
1979         uc_highbound = msReadByte( REG_ADDR_BLE_UPPER_BOND );
1980         uc_lowbound = msReadByte( REG_ADDR_BLE_LOWER_BOND);
1981 
1982         if (uc_cut < uc_lowbound )
1983         {
1984             uc_cut = uc_lowbound;
1985         }
1986         else if ( uc_cut > uc_highbound )
1987         {
1988             uc_cut = uc_highbound;
1989         }
1990         else
1991         {
1992             uc_cut = uc_cut;
1993         }
1994 
1995         // BLE - calculation
1996         for(i=0; i<8; ++i)
1997         {
1998             if (g_ucTable[i] < uc_cut)
1999             {
2000                 g_ucTable[i] = 0;
2001             }
2002             else if (g_ucTable[i] > (uc_cut+g_DlcParameters.ucDlcBLEPoint))
2003             {
2004                 ;
2005             }
2006             else
2007             {
2008                 g_ucTable[i] = ((WORD)(g_ucTable[i]-uc_cut))*(uc_cut+g_DlcParameters.ucDlcBLEPoint)/g_DlcParameters.ucDlcBLEPoint;
2009             }
2010         }
2011     }
2012 
2013     if( g_DlcParameters.bEnableWLE )
2014     {
2015         BYTE i,uc_wcut,uc_wlowbound,uc_whighbound;
2016         // combine BLE with software
2017         // WLE - get cut point
2018         uc_wcut = g_ucHistogramMax;
2019 
2020         uc_whighbound = (msReadByte( REG_ADDR_WLE_UPPER_BOND ) & 0x3F) + 0xC0; // bit offset 0x00~0x3F -> 0xC0~0xFF
2021         uc_wlowbound = (msReadByte( REG_ADDR_WLE_LOWER_BOND) & 0x3F) + 0xC0; // bit offset 0x00~0x3F -> 0xC0~0xFF
2022 
2023         if (uc_wcut < uc_wlowbound )
2024         {
2025             uc_wcut = uc_wlowbound;
2026         }
2027         else if ( uc_wcut > uc_whighbound )
2028         {
2029             uc_wcut = uc_whighbound;
2030         }
2031         else
2032         {
2033             uc_wcut = uc_wcut;
2034         }
2035 
2036         // WLE - calculation
2037         for(i=0; i<8; ++i)
2038         {
2039             if (g_ucTable[15-i] > uc_wcut)
2040             {
2041                 g_ucTable[15-i] = 0xFF;
2042             }
2043             else if (g_ucTable[15-i] < (uc_wcut-g_DlcParameters.ucDlcWLEPoint))
2044             {
2045                 ;
2046             }
2047             else
2048             {
2049                 g_ucTable[15-i] = 0xFF - (((WORD)(uc_wcut-g_ucTable[15-i]))*(0xFF-uc_wcut+g_DlcParameters.ucDlcWLEPoint)/g_DlcParameters.ucDlcWLEPoint);
2050             }
2051         }
2052     }
2053 
2054     // Level Limit
2055     if (g_DlcParameters.ucDlcLevelLimit)
2056     {
2057         if ((g_DlcParameters.ucDlcLevelLimit >= 1) && (g_ucTable[15] >= 0xF7))
2058         {
2059             g_ucTable[15] = 0xF7;
2060             if (g_ucTable[14] > 0xF7)
2061             {
2062                 g_ucTable[14] = 0xF7;
2063             }
2064 
2065             if (g_ucTable[13] > 0xF7)
2066             {
2067                 g_ucTable[13] = 0xF7;
2068             }
2069         }
2070 
2071         if ((g_DlcParameters.ucDlcLevelLimit >= 2) && (g_ucTable[14] >= 0xE7))
2072         {
2073             g_ucTable[14] = 0xE7;
2074             if (g_ucTable[13] > 0xE7)
2075             {
2076                 g_ucTable[13] = 0xE7;
2077             }
2078 
2079             if (g_ucTable[12] > 0xE7)
2080             {
2081                 g_ucTable[12] = 0xE7;
2082             }
2083         }
2084 
2085         if ((g_DlcParameters.ucDlcLevelLimit >= 3) && (g_ucTable[13] >= 0xD7))
2086         {
2087             g_ucTable[13] = 0xD7;
2088             if (g_ucTable[12] > 0xD7)
2089             {
2090                 g_ucTable[12] = 0xD7;
2091             }
2092 
2093             if (g_ucTable[11] > 0xD7)
2094             {
2095                 g_ucTable[11] = 0xD7;
2096             }
2097         }
2098 
2099         if ((g_DlcParameters.ucDlcLevelLimit >= 4) && (g_ucTable[12] >= 0xC7))
2100         {
2101             g_ucTable[12] = 0xC7;
2102             if (g_ucTable[11] > 0xC7)
2103             {
2104                 g_ucTable[11] = 0xC7;
2105             }
2106 
2107             if (g_ucTable[10] > 0xC7)
2108             {
2109                 g_ucTable[10] = 0xC7;
2110             }
2111         }
2112     }
2113 #if 0 //For customer require. Run DLC algorithm when static field .
2114     if((g_wLumiAverageTemp-Pre_TotalYSum)!=0)
2115 #endif
2116     {
2117 
2118         if ((g_ucDlcFlickAlpha < (5*Pre_g_ucDlcFlickAlpha /8)) && (ucDlcFlickAlpha_Diff < (g_DlcParameters.ucDlcAlphaBlendingMax- g_DlcParameters.ucDlcAlphaBlendingMin)/2))
2119         {
2120             g_ucDlcFlickAlpha = 2*(ucDlcFlickAlpha_Diff* g_ucDlcFlickAlpha +(( g_DlcParameters.ucDlcAlphaBlendingMax
2121                                   - g_DlcParameters.ucDlcAlphaBlendingMin)/2- ucDlcFlickAlpha_Diff)* (5*Pre_g_ucDlcFlickAlpha /8))
2122                                   /( g_DlcParameters.ucDlcAlphaBlendingMax- g_DlcParameters.ucDlcAlphaBlendingMin);
2123         }
2124 
2125         Pre_g_ucDlcFlickAlpha = g_ucDlcFlickAlpha;
2126         ucDlcFlickAlpha_Diff = g_ucDlcFlickAlpha_Max-g_ucDlcFlickAlpha_Min;
2127 
2128         // ~~~~~~~~~~~~~~~~~~~~~~~~ Sub window open PATCH End  ~~~~~~~~~~~~~~~~~~~~~~~~
2129 
2130         // ~~~~~~~~~~~~~~~~~~~~~~~~ PI_Alpha IIR Start  ~~~~~~~~~~~~~~~~~~~~~~~~
2131         if ( g_wPIAlpha >= g_wPrePIAlpha)
2132         {
2133             g_wPIAlpha = ((g_ucDlcFlickAlpha * g_wPIAlpha) +((128-g_ucDlcFlickAlpha)*g_wPrePIAlpha)+127)/128;
2134         }
2135         else
2136         {
2137             g_wPIAlpha = ((g_ucDlcFlickAlpha * g_wPIAlpha) +((128-g_ucDlcFlickAlpha)*g_wPrePIAlpha))/128;
2138         }
2139 
2140         g_wPrePIAlpha = g_wPIAlpha;
2141         // ~~~~~~~~~~~~~~~~~~~~~~~~ PI_Alpha IIR End  ~~~~~~~~~~~~~~~~~~~~~~~~
2142 
2143         // Write data to luma curve ...
2144         msDlcWriteCurve(bWindow);
2145 
2146         if(g_DlcParameters.ucDlcAlgorithmMode == XC_DLC_ALGORITHM_NEW)
2147         {
2148             //Dyanmic BLE Function
2149             //~~~~~~~~~~~~~~~~~Calculate BLE_StartPoint~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2150             if(((ucYAvg*4) + g_DlcParameters.uwDlcBleStartPointTH)>512)
2151             {
2152                 BLE_StartPoint = ((ucYAvg*4) + g_DlcParameters.uwDlcBleStartPointTH - 512);
2153             }
2154             else
2155             {
2156                 BLE_StartPoint = 0;
2157             }
2158 
2159             BLE_StartPoint_Correction = (BLE_StartPoint+2)/4;
2160 
2161             if(BLE_StartPoint_Correction >(Pre_BLE_StartPoint_Correction/16))
2162             {
2163                 if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2164                 {
2165                     uwBLE_StartPoint_Blend = (WORD)(((16*(DWORD)BLE_StartPoint_Correction)+(63*(DWORD)Pre_BLE_StartPoint_Correction)+63)/64);
2166                     Pre_BLE_StartPoint_Correction = uwBLE_StartPoint_Blend;
2167                     uwBLE_StartPoint_Blend = (uwBLE_StartPoint_Blend+8)/16;
2168                 }
2169                 else
2170                 {
2171                     uwBLE_StartPoint_Blend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)BLE_StartPoint_Correction) +
2172                                              ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(Pre_BLE_StartPoint_Correction/16))+127)/128);
2173                     Pre_BLE_StartPoint_Correction = uwBLE_StartPoint_Blend*16;
2174                 }
2175             }
2176             else
2177             {
2178                 if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2179                 {
2180                     uwBLE_StartPoint_Blend = (WORD)(((16*(DWORD)BLE_StartPoint_Correction)+(63*(DWORD)Pre_BLE_StartPoint_Correction))/64);
2181                     Pre_BLE_StartPoint_Correction = uwBLE_StartPoint_Blend;
2182                     uwBLE_StartPoint_Blend = (uwBLE_StartPoint_Blend+8)/16;
2183                 }
2184                 else
2185                 {
2186                     uwBLE_StartPoint_Blend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)BLE_StartPoint_Correction) +
2187                                              ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(Pre_BLE_StartPoint_Correction/16)))/128);
2188                     Pre_BLE_StartPoint_Correction = uwBLE_StartPoint_Blend*16;
2189                 }
2190             }
2191 
2192             //~~~~~~~~~output = uwBLE_StartPoint_Blend & Pre_BLE_StartPoint_Correction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2193 
2194             //~~~~~~~~~~~~~~~~~Calculate  BLE_EndPoint ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2195 
2196             for (ucTmp=0; ucTmp<32; ucTmp++)
2197             {
2198                 if(ucTmp==0)
2199                 {
2200                     uwHistogramAreaSum[ucTmp]=g_wLumaHistogram32H[ucTmp];
2201                 }
2202                 else
2203                 {
2204                     uwHistogramAreaSum[ucTmp] = uwHistogramAreaSum[ucTmp-1] + g_wLumaHistogram32H[ucTmp];
2205                 }
2206             }
2207 
2208             ucTmp=0;
2209             while( uwHistogramAreaSum[ucTmp] < uwHistogramAreaSum[31]/8)
2210             {
2211                 ucTmp++;
2212             }
2213 
2214             if(ucTmp==0)
2215             {
2216                 //Mantis issue of 0264631 :[Konka J3] . Use autotest tool control dc on/off.  DLC divisor can't to be equal to zero .
2217                 if(g_wLumaHistogram32H[0]==0)
2218                 {
2219                     DLC_DEBUG(printk("\n Error in  [New DLC Algorithm][ %s  , %d ]  g_wLumaHistogram32H[0]=0 !!!\n", __FUNCTION__,__LINE__));
2220                     return;
2221                 }
2222 
2223                 BLE_EndPoint = ((g_uwTable[0]*uwHistogramAreaSum[31])/(16*g_wLumaHistogram32H[0]));
2224             }
2225             else
2226             {
2227                 uwHistogramForBleSum = (uwHistogramAreaSum[31]/8) - uwHistogramAreaSum[ucTmp-1];
2228 
2229                 for (Tmp=0; Tmp<30; Tmp++)
2230                 {
2231                     uwHistogramAreaSum[Tmp] = (g_uwTable[Tmp/2]+g_uwTable[(Tmp+1)/2]+1)/2;
2232                 }
2233 
2234                 for (Tmp=0; Tmp<(ucTmp-1); Tmp++)
2235                 {
2236                     if(Tmp==0)
2237                     {
2238                         BLE_sum = g_wLumaHistogram32H[0]*(g_uwTable[0]+1/2);
2239                     }
2240                     else
2241                     {
2242                         BLE_sum = BLE_sum + g_wLumaHistogram32H[Tmp] * ((uwHistogramAreaSum[Tmp]+uwHistogramAreaSum[Tmp-1]+1)/2);
2243                     }
2244                 }
2245 
2246                 //Mantis issue of 0232938:Hsense A3 DLC the divisor can't to be equal to zero .
2247                 if(g_wLumaHistogram32H[ucTmp]==0)
2248                 {
2249                     DLC_DEBUG(printf("\n Error in  [New DLC Algorithm][ %s  , %d ]  g_wLumaHistogram32H[%d]=0 !!!\n", __FUNCTION__,__LINE__,ucTmp));
2250                     return;
2251                 }
2252 
2253                 BLE_sum = BLE_sum + uwHistogramForBleSum *((uwHistogramAreaSum[ucTmp]+uwHistogramAreaSum[ucTmp-1]+1)/2);
2254 
2255                 BLE_EndPoint = (BLE_sum*8+(uwHistogramAreaSum[31]/2))/uwHistogramAreaSum[31];
2256             }
2257 
2258             BLE_EndPoint = (BLE_EndPoint*g_DlcParameters.uwDlcBleEndPointTH+512)/1024;
2259             BLE_EndPoint_Correction = BLE_EndPoint;
2260 
2261 
2262             //~~~~~~~~~output = BLE_EndPoint Correction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2263 
2264             //~~~~~~~~~Used uwBLE_StartPoint_Blend & BLE_EndPoint_Correction Calculate BLE_Slop ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2265 
2266             if(uwBLE_StartPoint_Blend > 0x7F)
2267             {
2268                 uwBLE_StartPoint_Blend = 0x7F;
2269             }
2270 
2271             if( uwBLE_StartPoint_Blend*4 > BLE_EndPoint_Correction)
2272             {
2273                 BLE_Slop = (1024*uwBLE_StartPoint_Blend*4+((uwBLE_StartPoint_Blend*4 - BLE_EndPoint_Correction)/2))
2274                         /(uwBLE_StartPoint_Blend*4 - BLE_EndPoint_Correction);
2275             }
2276             else
2277             {
2278                 BLE_Slop = 0x7FF;
2279             }
2280 
2281             if(BLE_Slop > 0x7FF)
2282             {
2283                 BLE_Slop = 0x7FF;
2284             }
2285 
2286             if(BLE_Slop < 0x400)
2287             {
2288                 BLE_Slop = 0x400;
2289             }
2290 
2291             BLE_Slop_400 = g_DlcParameters.uwDlcBLESlopPoint_1;//(msReadByte(L_BK_DLC(0x38)))<<3;
2292             BLE_Slop_480 = g_DlcParameters.uwDlcBLESlopPoint_2;//(msReadByte(H_BK_DLC(0x38)))<<3;
2293             BLE_Slop_500 = g_DlcParameters.uwDlcBLESlopPoint_3;//(msReadByte(L_BK_DLC(0x39)))<<3;
2294             BLE_Slop_600 = g_DlcParameters.uwDlcBLESlopPoint_4;//(msReadByte(H_BK_DLC(0x39)))<<3;
2295             BLE_Slop_800 = g_DlcParameters.uwDlcBLESlopPoint_5;//(msReadByte(L_BK_DLC(0x3A)))<<3;
2296             Dark_BLE_Slop_Min = g_DlcParameters.uwDlcDark_BLE_Slop_Min;//(msReadByte(L_BK_DLC(0x3B)))<<3;
2297 
2298             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2299             if(BLE_Slop <= 0x480)
2300             {
2301                 BLE_Slop =((BLE_Slop_480-BLE_Slop_400)*(BLE_Slop-0x400)+64)/128+BLE_Slop_400;
2302             }
2303             else if ((BLE_Slop > 0x480) && (BLE_Slop <= 0x500))
2304             {
2305                 BLE_Slop =((BLE_Slop_500-BLE_Slop_480)*(BLE_Slop-0x480)+64)/128+BLE_Slop_480;
2306             }
2307             else if ((BLE_Slop > 0x500) && (BLE_Slop <= 0x600))
2308             {
2309                 BLE_Slop =((BLE_Slop_600-BLE_Slop_500)*(BLE_Slop-0x500)+128)/256+BLE_Slop_500;
2310             }
2311             else
2312             {
2313                 BLE_Slop =((BLE_Slop_800-BLE_Slop_600)*(BLE_Slop-0x600)+256)/512+BLE_Slop_600;
2314             }
2315 
2316             if (ucYAvg < ((g_DlcParameters.ucDlcYAvgThresholdL+g_DlcParameters.ucDlcYAvgThresholdM)/2))
2317             {
2318                 BLE_Slop_tmp =(((Dark_BLE_Slop_Min - BLE_Slop_400)*(((g_DlcParameters.ucDlcYAvgThresholdL+g_DlcParameters.ucDlcYAvgThresholdM)/2)-ucYAvg)/
2319                               ((g_DlcParameters.ucDlcYAvgThresholdL+g_DlcParameters.ucDlcYAvgThresholdM)/2)) + BLE_Slop_400);
2320 
2321                 if(BLE_Slop > BLE_Slop_tmp)
2322                 {
2323                     BLE_Slop = BLE_Slop;
2324                 }
2325                 else
2326                 {
2327                     BLE_Slop = BLE_Slop_tmp;
2328                 }
2329             }
2330             else
2331             {
2332                 BLE_Slop = BLE_Slop;
2333             }
2334 
2335 
2336             // ~~~~~~~~~~~~~~~~~~~~~~~~ Pure Image Patch  ~~~~~~~~~~~~~~~~~~~~~~~~
2337             BLE_Slop = (BLE_Slop*(256-g_wPIAlpha)+ 1024*g_wPIAlpha + 128)/256;
2338 
2339             //~~~~~~~~~ output = uwBLE_Blend �]with slow approach�^ ~~~~~~~~~~~~~~~~~~~~~
2340 
2341             if(BLE_Slop >(Pre_BLE_Slop/16))
2342             {
2343                 if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2344                 {
2345                     uwBLE_Blend = (WORD)(((16*(DWORD)BLE_Slop)+(63*(DWORD)Pre_BLE_Slop)+63)/64);
2346                     Pre_BLE_Slop = uwBLE_Blend;
2347                     uwBLE_Blend = (uwBLE_Blend+8)/16;
2348                 }
2349                 else
2350                 {
2351                     uwBLE_Blend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)BLE_Slop) +
2352                                   ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(Pre_BLE_Slop/16))+127)/128);
2353                     Pre_BLE_Slop = uwBLE_Blend*16;          //14-bits
2354                 }
2355             }
2356             else
2357             {
2358                 if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2359                 {
2360                     uwBLE_Blend = (WORD)(((16*(DWORD)BLE_Slop)+(63*(DWORD)Pre_BLE_Slop))/64);
2361                     Pre_BLE_Slop = uwBLE_Blend;
2362                     uwBLE_Blend = (uwBLE_Blend+8)/16;
2363                 }
2364                 else
2365                 {
2366                     uwBLE_Blend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)BLE_Slop) +
2367                                   ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(Pre_BLE_Slop/16)))/128);
2368                     Pre_BLE_Slop = uwBLE_Blend*16;
2369                 }
2370             }
2371 
2372             if(g_bSetDlcBleOn)
2373             {
2374                 msWriteByte(L_BK_DLC(0x10),uwBLE_StartPoint_Blend);
2375                 msWriteByte(H_BK_DLC(0x10),uwBLE_Blend>>3);
2376                 msWriteByte(L_BK_DLC(0x0D),(msReadByte(L_BK_DLC(0x0D))&0xF8)|(uwBLE_Blend & 0x07));
2377             }
2378             else
2379             {
2380                 msWriteByte(L_BK_DLC(0x10),0x00);
2381                 msWriteByte(H_BK_DLC(0x10),0x80);
2382                 msWriteByte(L_BK_DLC(0x0D),msReadByte(L_BK_DLC(0x0D))&0xF8);
2383             }
2384         }
2385     }
2386     Pre_TotalYSum = g_wLumiAverageTemp;
2387     msDlc_FunctionExit();
2388 }
2389 
2390 //////////////////////////////////////////////////////////////
2391 //
2392 //////////////////////////////////////////////////////////////
msDlcWriteCurveLSB(BOOL bWindow,BYTE ucIndex,BYTE ucValue)2393 void msDlcWriteCurveLSB(BOOL bWindow, BYTE ucIndex, BYTE ucValue)
2394 {
2395     DWORD wRegStart = 0;
2396     BYTE ucData;
2397 
2398     msDlc_FunctionEnter();
2399 
2400 	if( MAIN_WINDOW == bWindow )
2401 	    wRegStart = REG_ADDR_DLC_DATA_LSB_START_MAIN;
2402 
2403     switch(ucIndex)
2404     {
2405         case 0:
2406             wRegStart += 0;
2407             ucData = (msReadByte(wRegStart) & 0xF3) | (ucValue << 2);
2408             break;
2409         case 1:
2410             wRegStart += 0;
2411             ucData = (msReadByte(wRegStart) & 0xCF) | (ucValue << 4);
2412             break;
2413         case 2:
2414             wRegStart += 0;
2415             ucData = (msReadByte(wRegStart) & 0x3F) | (ucValue << 6);
2416             break;
2417         case 3:
2418             wRegStart += 1;
2419             ucData = (msReadByte(wRegStart) & 0xFC) | (ucValue);
2420             break;
2421         case 4:
2422             wRegStart += 1;
2423             ucData = (msReadByte(wRegStart) & 0xF3) | (ucValue << 2);
2424             break;
2425         case 5:
2426             wRegStart += 1;
2427             ucData = (msReadByte(wRegStart) & 0xCF) | (ucValue << 4);
2428             break;
2429         case 6:
2430             wRegStart += 1;
2431             ucData = (msReadByte(wRegStart) & 0x3F) | (ucValue << 6);
2432             break;
2433         case 7:
2434             wRegStart += 2;
2435             ucData = (msReadByte(wRegStart) & 0xFC) | (ucValue);
2436             break;
2437         case 8:
2438             wRegStart += 2;
2439             ucData = (msReadByte(wRegStart) & 0xF3) | (ucValue << 2);
2440             break;
2441         case 9:
2442             wRegStart += 2;
2443             ucData = (msReadByte(wRegStart) & 0xCF) | (ucValue << 4);
2444             break;
2445         case 10:
2446             wRegStart += 2;
2447             ucData = (msReadByte(wRegStart) & 0x3F) | (ucValue << 6);
2448             break;
2449         case 11:
2450             wRegStart += 3;
2451             ucData = (msReadByte(wRegStart) & 0xFC) | (ucValue);
2452             break;
2453         case 12:
2454             wRegStart += 3;
2455             ucData = (msReadByte(wRegStart) & 0xF3) | (ucValue << 2);
2456             break;
2457         case 13:
2458             wRegStart += 3;
2459             ucData = (msReadByte(wRegStart) & 0xCF) | (ucValue << 4);
2460             break;
2461         case 14:
2462             wRegStart += 3;
2463             ucData = (msReadByte(wRegStart) & 0x3F) | (ucValue << 6);
2464             break;
2465         case 15:
2466             wRegStart += 4;
2467             ucData = (msReadByte(wRegStart) & 0xFC) | (ucValue);
2468             break;
2469         case 16: // index 16
2470             wRegStart += 4;
2471             ucData = (msReadByte(wRegStart) & 0xF3) | (ucValue << 2);
2472             break;
2473         case 0xFF: // index n0
2474         default:
2475             wRegStart += 0;
2476             ucData = (msReadByte(wRegStart) & 0xFC) | (ucValue);
2477             break;
2478     }
2479 
2480     msWriteByte(wRegStart, ucData);
2481 
2482     msDlc_FunctionExit();
2483 }
2484 
msDlcWriteCurve(BOOL bWindow)2485 void msDlcWriteCurve(BOOL bWindow)
2486 {
2487     BYTE ucTmp;
2488     WORD uwBlend;
2489     WORD uwTmp;
2490 
2491     msDlc_FunctionEnter();
2492 
2493     // Write data to luma curve ...
2494 
2495     if(g_DlcParameters.ucDlcAlgorithmMode == XC_DLC_ALGORITHM_OLD)
2496     {
2497 
2498         if( MAIN_WINDOW == bWindow )
2499         {
2500             for(ucTmp=0; ucTmp<=0x0f; ucTmp++)
2501             {
2502                 if(g_ucDlcFastLoop || (g_ucDlcFlickAlpha >= (DLC_DEFLICK_BLEND_FACTOR >> 1)))
2503                 {
2504                     if (g_ucDlcFastLoop)
2505                         g_ucDlcFastLoop--;
2506 
2507                     // target value with alpha blending
2508                     uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_ucTable[ucTmp] * DLC_DEFLICK_PRECISION_SHIFT) +
2509                                  ((DLC_DEFLICK_BLEND_FACTOR - (DWORD)g_ucDlcFlickAlpha) * (DWORD)g_uwPreTable[ucTmp]))/DLC_DEFLICK_BLEND_FACTOR);
2510 
2511                     if(uwBlend > g_uwPreTable[ucTmp])
2512                     {
2513                         uwTmp = (uwBlend - g_uwPreTable[ucTmp]) / (DLC_DEFLICK_BLEND_FACTOR - g_ucDlcFlickAlpha + 1);
2514                         uwBlend = g_uwPreTable[ucTmp] + uwTmp;
2515                     }
2516                     else if(uwBlend < g_uwPreTable[ucTmp])
2517                     {
2518                         uwTmp = (g_uwPreTable[ucTmp] - uwBlend) / (DLC_DEFLICK_BLEND_FACTOR - g_ucDlcFlickAlpha + 1);
2519                         uwBlend = g_uwPreTable[ucTmp] - uwTmp;
2520                     }
2521 
2522                     g_uwPreTable[ucTmp] = uwBlend;
2523 
2524                     //g_ucTable[ucTmp] = g_uwPreTable[ucTmp] / DLC_DEFLICK_PRECISION_SHIFT;
2525                     g_uwTable[ucTmp] = ((DWORD)g_uwPreTable[ucTmp]<<2) / DLC_DEFLICK_PRECISION_SHIFT;
2526                     g_ucTable[ucTmp] = g_uwTable[ucTmp]>>2;
2527 
2528                     msWriteByte((REG_ADDR_DLC_DATA_START_MAIN+ucTmp), g_ucTable[ucTmp]);
2529                     msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2530 
2531                     // set DLC curve index N0 & 16
2532                     if (ucTmp == 0)
2533                     {
2534                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN, g_uwTable[0]>>2);
2535                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN + 1, 0x01);
2536                         msDlcWriteCurveLSB(MAIN_WINDOW, 0xFF, g_uwTable[0]&0x03);
2537                     }
2538 
2539                     if (ucTmp == 15)
2540                     {
2541                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN, 255-(g_uwTable[15]>>2));
2542                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN + 1, 0x01);
2543                         msDlcWriteCurveLSB(MAIN_WINDOW, 16, g_uwTable[15]&0x03);
2544                     }
2545                 }
2546                 else if (g_ucDlcFlickAlpha == 0)
2547                 {
2548                     g_ucTable[ucTmp] = g_uwPreTable[ucTmp] / DLC_DEFLICK_PRECISION_SHIFT;
2549                 }
2550                 else if ((ucTmp%4) == (g_ucUpdateCnt%4))
2551                 {
2552                     // target value with alpha blending
2553                     uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_ucTable[ucTmp] * DLC_DEFLICK_PRECISION_SHIFT) +
2554                                  ((DLC_DEFLICK_BLEND_FACTOR - (DWORD)g_ucDlcFlickAlpha) * (DWORD)g_uwPreTable[ucTmp]))/DLC_DEFLICK_BLEND_FACTOR);
2555 
2556                     if(uwBlend > g_uwPreTable[ucTmp])
2557                     {
2558                         if((uwBlend - g_uwPreTable[ucTmp]) > DLC_DEFLICK_PRECISION_SHIFT)
2559                             uwBlend = g_uwPreTable[ucTmp] + DLC_DEFLICK_PRECISION_SHIFT;
2560                     }
2561                     else if(uwBlend < g_uwPreTable[ucTmp])
2562                     {
2563                         if((g_uwPreTable[ucTmp] - uwBlend) > DLC_DEFLICK_PRECISION_SHIFT)
2564                             uwBlend = g_uwPreTable[ucTmp] - DLC_DEFLICK_PRECISION_SHIFT;
2565                     }
2566 
2567                     g_uwPreTable[ucTmp] = uwBlend;
2568 
2569                     //g_ucTable[ucTmp] = g_uwPreTable[ucTmp] / DLC_DEFLICK_PRECISION_SHIFT;
2570                     g_uwTable[ucTmp] = ((DWORD)g_uwPreTable[ucTmp]<<2) / DLC_DEFLICK_PRECISION_SHIFT;
2571                     g_ucTable[ucTmp] = g_uwTable[ucTmp]>>2;
2572 
2573                     msWriteByte((REG_ADDR_DLC_DATA_START_MAIN+ucTmp), g_ucTable[ucTmp]);
2574                     msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2575 
2576                     // set DLC curve index N0 & 16
2577                     if (ucTmp == 0)
2578                     {
2579                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN, g_uwTable[0]>>2);
2580                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN + 1, 0x01);
2581                         msDlcWriteCurveLSB(MAIN_WINDOW, 0xFF, g_uwTable[0]&0x03);
2582                     }
2583 
2584                     if (ucTmp == 15)
2585                     {
2586                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN, 255-(g_uwTable[15]>>2));
2587                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN + 1, 0x01);
2588                         msDlcWriteCurveLSB(MAIN_WINDOW, 16, g_uwTable[15]&0x03);
2589                     }
2590                 }
2591             }
2592         }
2593     }
2594     else
2595     {
2596 
2597         if( MAIN_WINDOW == bWindow )
2598         {
2599             for(ucTmp=0; ucTmp<=0x0f; ucTmp++)
2600             {
2601                 g_uwTable[ucTmp] = (g_wPIAlpha*(ucTmp*64 + 32)+(256-g_wPIAlpha)*g_uwTable[ucTmp]+128)/256;
2602 
2603                 if(g_uwTable[ucTmp]>(g_uwPreTable[ucTmp]/16))
2604                 {
2605                     if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2606                     {
2607                         uwBlend = (WORD)(((16*(DWORD)g_uwTable[ucTmp])+(63*(DWORD)g_uwPreTable[ucTmp])+63)/64); //14-bits
2608                         g_uwPreTable[ucTmp] = uwBlend;                                                              //14-bits
2609                         uwBlend = (uwBlend+8)/16;                                                                     //10-bits
2610                     }
2611                     else
2612                     {
2613                         uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_uwTable[ucTmp]) +
2614                                   ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(g_uwPreTable[ucTmp]/16))+127)/128); //10-bits
2615                         g_uwPreTable[ucTmp] = uwBlend*16;                                                            //14-bits
2616                         uwBlend = uwBlend;
2617                     }
2618                 }
2619                 else
2620                 {
2621                     if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2622                     {
2623                         uwBlend = (WORD)(((16*(DWORD)g_uwTable[ucTmp])+(63*(DWORD)g_uwPreTable[ucTmp]))/64);  //14-bits
2624                         g_uwPreTable[ucTmp] = uwBlend;                                                            //14-bits
2625                         uwBlend = (uwBlend+8)/16;                                                                   //10-bits
2626                     }
2627                     else
2628                     {
2629                         uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_uwTable[ucTmp]) +
2630                                   ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(g_uwPreTable[ucTmp]/16)))/128); //10-bits
2631                         g_uwPreTable[ucTmp] = uwBlend*16;                                                        //14-bits
2632                         uwBlend = uwBlend;
2633                     }
2634                 }
2635 
2636                 g_uwTable[ucTmp] = uwBlend;
2637                 g_ucTable[ucTmp] = g_uwTable[ucTmp]>>2;
2638 
2639                 msWriteByte((REG_ADDR_DLC_DATA_START_MAIN+ucTmp), g_ucTable[ucTmp]);
2640                 msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2641 
2642                 // set DLC curve index N0 & 16
2643                 if (ucTmp == 0)
2644                 {
2645                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN, g_uwTable[0]>>2);
2646                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN + 1, 0x01);
2647                     msDlcWriteCurveLSB(MAIN_WINDOW, 0xFF, g_uwTable[0]&0x03);
2648                 }
2649 
2650                 if (ucTmp == 15)
2651                 {
2652                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN, 255-(g_uwTable[15]>>2));
2653                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN + 1, 0x01);
2654                     msDlcWriteCurveLSB(MAIN_WINDOW, 16, g_uwTable[15]&0x03);
2655                 }
2656             }
2657         }
2658 
2659     }
2660 
2661     g_ucUpdateCnt++;
2662 
2663     msDlc_FunctionExit();
2664 }
2665 
2666 //////////////////////////////////////////////////////////////
2667 // Program register bytes to MST chip.
2668 //
2669 // Arguments: pTable - pointer of registers table
2670 //////////////////////////////////////////////////////////////
msWriteDLCRegsTbl(DlcRegUnitType code * pTable)2671 void msWriteDLCRegsTbl(DlcRegUnitType code *pTable)
2672 {
2673     WORD wReg; // register index
2674 
2675     while (1)
2676     {
2677          wReg = pTable->wReg; // get register index
2678          if (wReg == _END_OF_TBL_) // check end of table
2679              break;
2680 
2681          msWriteByte(wReg, pTable->ucValue); // write register
2682 
2683          pTable++; // next
2684     } // while
2685 }
2686 
msDlcSpeedupTrigger(BYTE ucLoop)2687 void msDlcSpeedupTrigger(BYTE ucLoop)
2688 {
2689     g_DlcParameters.ucDlcFlickAlphaStart = ucLoop;
2690 }
2691 
2692 /******************************************************************************/
2693 /*                           CGC                                         */
2694 /******************************************************************************/
2695 
msDlc_CGC_Init(void)2696 void msDlc_CGC_Init(void)
2697 {
2698     msDlc_FunctionEnter();
2699 
2700     if((msReadByte(REG_ADDR_DLC_HANDSHAKE) & _BIT7) )// Shall not Init CGC again while DLC is On.
2701     {
2702        msDlc_FunctionExit();
2703        return;
2704     }
2705 
2706     g_u8Chroma_OriGain = msReadByte(REG_ADDR_DLC_C_GAIN);
2707     msDlc_CGC_ReInit();
2708 
2709     msDlc_FunctionExit();
2710 }
2711 
msDlc_CGC_ReInit(void)2712 void msDlc_CGC_ReInit(void) // For CGC Init & Tool change parameter value dynamicly
2713 {
2714     if ((0xFF-g_u8Chroma_OriGain)>g_DlcParameters.ucCGCChroma_GainLimitH)
2715         g_u8CGC_UpperBond = (g_u8Chroma_OriGain+g_DlcParameters.ucCGCChroma_GainLimitH);
2716     else
2717         g_u8CGC_UpperBond = 0xFF;
2718 
2719     if (g_DlcParameters.ucCGCChroma_GainLimitL<g_u8Chroma_OriGain)
2720         g_u8CGC_LowerBond = (g_u8Chroma_OriGain-g_DlcParameters.ucCGCChroma_GainLimitL);
2721     else
2722         g_u8CGC_LowerBond = 0x00;
2723 
2724     g_u16Ydark = (Y01_weight*(g_ucTable[0]+g_ucTable[1])+Y23_weight*(g_ucTable[2]+g_ucTable[3]))/(2*(Y01_weight+Y23_weight));
2725     g_u16Ydark =  g_u16Ydark+((Y45_weight*(g_ucTable[4]+g_ucTable[5])+Y67_weight*(g_ucTable[6]+g_ucTable[7])))/(2*(Y45_weight+Y67_weight));
2726 
2727     if (g_DlcParameters.ucCGCCGain_offset & _BIT7)
2728         g_u16Ydark = (g_u16Ydark-(g_DlcParameters.ucCGCCGain_offset&0x7F));
2729     else
2730         g_u16Ydark = (g_u16Ydark+g_DlcParameters.ucCGCCGain_offset);
2731 
2732     g_u16Ydark_pre = g_u16Ydark;
2733 
2734     //printf("\r\n[1]. g_ucTable[0] = %d \n", g_ucTable[0]);
2735     //printf("\r\n[2]. g_ucTable[1] = %d \n", g_ucTable[1]);
2736     //printf("\r\n[3]. g_ucTable[2] = %d \n", g_ucTable[2]);
2737     //printf("\r\n[4]. g_ucTable[3] = %d \n", g_ucTable[3]);
2738     //printf("\r\n[5]. g_ucTable[4] = %d \n", g_ucTable[4]);
2739 
2740 
2741     //printf("\r\ng_u8CGC_UpperBond = %d \n", g_u8CGC_UpperBond);
2742     //printf("\r\ng_u8CGC_LowerBond = %d \n", g_u8CGC_LowerBond);
2743 
2744     //printf("\r\ng_u16Ydark(std) = %d \n", g_u16Ydark);
2745 }
2746 
msDlc_CGC_SetGain(BYTE u8CGain)2747 void msDlc_CGC_SetGain(BYTE u8CGain)
2748 {
2749     msDlc_FunctionEnter();
2750 
2751     if (g_DlcParameters.bCGCCGainCtrl && (REG_ADDR_DLC_C_GAIN != REG_NULL))
2752         msWriteByte(REG_ADDR_DLC_C_GAIN, u8CGain);
2753 
2754     msDlc_FunctionExit();
2755 }
2756 
msDlc_CGC_Handler(void)2757 void msDlc_CGC_Handler(void)
2758 {
2759     BYTE u8TmpVal;
2760 
2761     WORD u16Ytemp, u16Ydiff;
2762 
2763     msDlc_FunctionEnter();
2764 
2765     //printf("\r\n g_u8CGC_UpperBond = %d \n", g_u8CGC_UpperBond);
2766     //printf("\r\n g_u8CGC_LowerBond = %d \n", g_u8CGC_LowerBond);
2767 
2768     u16Ytemp = (DWORD) (Y01_weight*(g_ucTable[0]+g_ucTable[1])+
2769              Y23_weight*(g_ucTable[2]+g_ucTable[3]))/
2770             (Y01_weight+Y23_weight);
2771 
2772     u16Ytemp =  u16Ytemp/2+(DWORD)(Y45_weight*(g_ucTable[4]+g_ucTable[5])+
2773                        Y67_weight*(g_ucTable[6]+g_ucTable[7]))/
2774                       (2*(Y45_weight+Y67_weight));
2775 
2776     //printf("\r\nu16Ytemp       = %d", u16Ytemp);
2777     //printf("\r\ng_u16Ydark_pre = %d", g_u16Ydark_pre);
2778 
2779     if (abs(u16Ytemp-g_u16Ydark_pre)>=Yvar_Th)
2780     {
2781         g_u8TCount++;
2782 
2783      	 if ((u16Ytemp>g_u16Ydark)&&(g_u8TCount>=Timer_CountU))
2784      {
2785              //printf("\r\ng_u8TCountU       = %d", g_u8TCount);
2786          g_u8TCount = 0;
2787 
2788              u16Ydiff=(u16Ytemp-g_u16Ydark);
2789 
2790              if (u16Ydiff>g_DlcParameters.ucCGCYth)
2791              {
2792      	         g_u8Chroma_target = g_u8Chroma_OriGain+(WORD)((u16Ydiff-g_DlcParameters.ucCGCYth)*g_DlcParameters.ucCGCYCslope)/(0x20);
2793 
2794      	         if (g_u8Chroma_target > g_u8CGC_UpperBond)
2795      	             g_u8Chroma_target = g_u8CGC_UpperBond;
2796      	     }
2797      	     else
2798      	         g_u8Chroma_target = g_u8Chroma_OriGain;
2799          }
2800          else if ((u16Ytemp<=g_u16Ydark)&&(g_u8TCount>=Timer_CountD))
2801          {
2802              //printf("\r\ng_u8TCountD       = %d", g_u8TCount);
2803              g_u8TCount = 0;
2804 
2805              u16Ydiff=(g_u16Ydark-u16Ytemp);
2806 
2807              if (u16Ydiff>g_DlcParameters.ucCGCYth)
2808              {
2809      	         g_u8Chroma_target = g_u8Chroma_OriGain-(WORD)((u16Ydiff-g_DlcParameters.ucCGCYth)*g_DlcParameters.ucCGCYCslope)/(0x20);
2810 
2811      	         if (g_u8Chroma_target < g_u8CGC_LowerBond)
2812      	             g_u8Chroma_target = g_u8CGC_LowerBond;
2813      	     }
2814      	     else
2815      	         g_u8Chroma_target = g_u8Chroma_OriGain;
2816          }
2817 
2818          //printf("\r\ng_u8Chroma_target = %d", g_u8Chroma_target);
2819 
2820      	 u8TmpVal = msReadByte(REG_ADDR_DLC_C_GAIN);
2821 
2822      	 if (g_u8Chroma_target>u8TmpVal)
2823      	     msDlc_CGC_SetGain(u8TmpVal+CGain_StepU);
2824      	 else if (g_u8Chroma_target<u8TmpVal)
2825      	     msDlc_CGC_SetGain(u8TmpVal-CGain_StepD);
2826     }
2827 
2828     g_u16Ydark_pre = (DWORD)(31*g_u16Ydark_pre+u16Ytemp)/32;
2829 
2830     msDlc_FunctionExit();
2831 }
2832 
2833 //////////////////////////////////////////////////////////////
2834 // Debug Protocol
2835 //////////////////////////////////////////////////////////////
2836 
2837 // Debug Command Paser
2838 #define URCMD_MST_DLC_TEST_FORCE_PAUSE      0
2839 #define URCMD_MST_DLC_TEST_FORCE_RUN        1
2840 #define URCMD_MST_DLC_TEST_READ             2 // old mode
2841 #define URCMD_MST_DLC_TEST_WRITE_1          3
2842 #define URCMD_MST_DLC_TEST_WRITE_2          4
2843 #define URCMD_MST_DLC_TEST_REINIT           5
2844 #define URCMD_MST_DLC_TEST_READ_BOUNDARY	6
2845 #define URCMD_MST_DLC_TEST_WRITE_BOUNDARY	7
2846 #define URCMD_MST_DLC_TEST_READ_CV1         8
2847 #define URCMD_MST_DLC_TEST_READ_CV2A        9
2848 #define URCMD_MST_DLC_TEST_READ_CV2B        10
2849 #define URCMD_MST_DLC_TEST_WRITE_CV1        11
2850 #define URCMD_MST_DLC_TEST_WRITE_CV2A       12
2851 #define URCMD_MST_DLC_TEST_WRITE_CV2B       13
2852 #define URCMD_MST_DLC_TEST_READ_INFO        14
2853 #define URCMD_MST_DLC_TEST_WRITE_INFO       15
2854 #define URCMD_MST_DLC_TEST_CGC_REINIT       16
2855 #define URCMD_MST_DLC_TEST_READ_Sloplimit   17
2856 #define URCMD_MST_DLC_TEST_WRITE_Sloplimit  18
2857 
msDLC_ParametersTool(BYTE * pCmdBuff)2858 void msDLC_ParametersTool(BYTE *pCmdBuff)
2859 {
2860     BYTE ucTmpLen;
2861 
2862     ucTmpLen = *(pCmdBuff+1); //ucTmpLen
2863 
2864     switch(*(pCmdBuff+3)) //UART_EXT_CMD
2865     {
2866         case URCMD_MST_DLC_TEST_FORCE_PAUSE:
2867 
2868             break;
2869         case URCMD_MST_DLC_TEST_FORCE_RUN:
2870 
2871             break;
2872         case URCMD_MST_DLC_TEST_READ:
2873             {
2874                 BYTE i;
2875                 for (i=0; i<16; i+=2)
2876                 {
2877                     putchar(0xF3);
2878                     putchar(g_DlcParameters.ucLumaCurve[i]);
2879                     putchar(g_DlcParameters.ucLumaCurve[i+1]);
2880                     Delay1ms(150);
2881                 }
2882             }
2883             break;
2884         case URCMD_MST_DLC_TEST_WRITE_1:
2885             {
2886                 BYTE i;
2887               #if 1//( ENABLE_UART_CHECKSUM )
2888                 ucTmpLen--; // remove checksum byte
2889               #endif
2890                 for (i=0; i<(ucTmpLen-4); i++)
2891                     g_DlcParameters.ucLumaCurve[i] = *(pCmdBuff+4 + i);
2892             }
2893             break;
2894         case URCMD_MST_DLC_TEST_WRITE_2:
2895             {
2896                 BYTE i;
2897               #if 1//( ENABLE_UART_CHECKSUM )
2898                 ucTmpLen--; // remove checksum byte
2899               #endif
2900                 for (i=0; i<(ucTmpLen-4); i++)
2901                     g_DlcParameters.ucLumaCurve[8 + i] = *(pCmdBuff+4 + i);
2902             }
2903             break;
2904         case URCMD_MST_DLC_TEST_READ_CV1:
2905             {
2906                 putchar(0xF2);
2907                 putchar(g_DlcParameters.ucLumaCurve[*(pCmdBuff+4)]);
2908             }
2909             break;
2910         case URCMD_MST_DLC_TEST_READ_CV2A:
2911             {
2912                 putchar(0xF2);
2913                 putchar(g_DlcParameters.ucLumaCurve2_a[*(pCmdBuff+4)]);
2914             }
2915             break;
2916         case URCMD_MST_DLC_TEST_READ_CV2B:
2917             {
2918                 putchar(0xF2);
2919                 putchar(g_DlcParameters.ucLumaCurve2_b[*(pCmdBuff+4)]);
2920             }
2921             break;
2922         case URCMD_MST_DLC_TEST_WRITE_CV1:
2923             {
2924                 BYTE i;
2925               #if 1//( ENABLE_UART_CHECKSUM )
2926                 ucTmpLen--; // remove checksum byte
2927               #endif
2928                 for (i=0; i<(ucTmpLen-4-1); i++)
2929                     g_DlcParameters.ucLumaCurve[*(pCmdBuff+4) + i] = *(pCmdBuff+5 + i);
2930             }
2931             break;
2932         case URCMD_MST_DLC_TEST_WRITE_CV2A:
2933             {
2934                 BYTE i;
2935               #if 1//( ENABLE_UART_CHECKSUM )
2936                 ucTmpLen--; // remove checksum byte
2937               #endif
2938                 for (i=0; i<(ucTmpLen-4-1); i++)
2939                     g_DlcParameters.ucLumaCurve2_a[*(pCmdBuff+4) + i] = *(pCmdBuff+5 + i);
2940             }
2941             break;
2942         case URCMD_MST_DLC_TEST_WRITE_CV2B:
2943             {
2944                 BYTE i;
2945               #if 1//( ENABLE_UART_CHECKSUM )
2946                 ucTmpLen--; // remove checksum byte
2947               #endif
2948                 for (i=0; i<(ucTmpLen-4-1); i++)
2949                     g_DlcParameters.ucLumaCurve2_b[*(pCmdBuff+4) + i] = *(pCmdBuff+5 + i);
2950             }
2951             break;
2952         case URCMD_MST_DLC_TEST_READ_INFO:
2953             {
2954                 if (*(pCmdBuff+4) == 1) // DLC curve
2955                 {
2956                     putchar(0xF2);
2957                     putchar(g_ucTable[*(pCmdBuff+5)]);
2958                 }
2959                 else if (*(pCmdBuff+4) == 2) // DLC other
2960                 {
2961                     if (*(pCmdBuff+5) < 64)
2962                     {
2963                         putchar(0xF2);
2964                         if (*(pCmdBuff+5)%2)
2965                             putchar(g_wLumaHistogram32H[*(pCmdBuff+5)/2] & 0xFF);
2966                         else
2967                             putchar(g_wLumaHistogram32H[*(pCmdBuff+5)/2] >> 8);
2968                     }
2969                     else if (*(pCmdBuff+5) == 200)
2970                     {
2971                         WORD wVer;
2972                         wVer = msDlcLibVersionCheck();
2973                         putchar(0xF3);
2974                         putchar(wVer >> 8);
2975                         putchar(wVer & 0xFF);
2976                     }
2977                     else if (*(pCmdBuff+5) == 201)
2978                     {
2979                         putchar(0xF3);
2980                         putchar(g_DlcParameters.uwDlcBleStartPointTH >> 8);
2981                         putchar(g_DlcParameters.uwDlcBleStartPointTH & 0xFF);
2982                     }
2983                     else if (*(pCmdBuff+5) == 202)
2984                     {
2985                         putchar(0xF3);
2986                         putchar(g_DlcParameters.uwDlcBleEndPointTH >> 8);
2987                         putchar(g_DlcParameters.uwDlcBleEndPointTH & 0xFF);
2988                     }
2989                     else if (*(pCmdBuff+5) == 203)
2990                     {
2991                         putchar(0xF3);
2992                         putchar(g_DlcParameters.uwDlcBLESlopPoint_1 >> 8);
2993                         putchar(g_DlcParameters.uwDlcBLESlopPoint_1 & 0xFF);
2994                     }
2995                     else if (*(pCmdBuff+5) == 204)
2996                     {
2997                         putchar(0xF3);
2998                         putchar(g_DlcParameters.uwDlcBLESlopPoint_2 >> 8);
2999                         putchar(g_DlcParameters.uwDlcBLESlopPoint_2 & 0xFF);
3000                     }
3001                     else if (*(pCmdBuff+5) == 205)
3002                     {
3003                         putchar(0xF3);
3004                         putchar(g_DlcParameters.uwDlcBLESlopPoint_3 >> 8);
3005                         putchar(g_DlcParameters.uwDlcBLESlopPoint_3 & 0xFF);
3006                     }
3007                     else if (*(pCmdBuff+5) == 206)
3008                     {
3009                         putchar(0xF3);
3010                         putchar(g_DlcParameters.uwDlcBLESlopPoint_4 >> 8);
3011                         putchar(g_DlcParameters.uwDlcBLESlopPoint_4 & 0xFF);
3012                     }
3013                     else if (*(pCmdBuff+5) == 207)
3014                     {
3015                         putchar(0xF3);
3016                         putchar(g_DlcParameters.uwDlcBLESlopPoint_5 >> 8);
3017                         putchar(g_DlcParameters.uwDlcBLESlopPoint_5 & 0xFF);
3018                     }
3019                     else if (*(pCmdBuff+5) == 208)
3020                     {
3021                         putchar(0xF3);
3022                         putchar(g_DlcParameters.uwDlcDark_BLE_Slop_Min >> 8);
3023                         putchar(g_DlcParameters.uwDlcDark_BLE_Slop_Min & 0xFF);
3024                     }
3025                     else if (*(pCmdBuff+5) >= 64)
3026                     {
3027                         putchar(0xF2);
3028                         if (*(pCmdBuff+5) == 64)
3029                             putchar(g_wLumiAverageTemp & 0xFF);
3030                         else if (*(pCmdBuff+5) == 65)
3031                             putchar(g_wLumiAverageTemp >> 8);
3032                         else if (*(pCmdBuff+5) == 66)
3033                             putchar(g_wLumiTotalCount & 0xFF);
3034                         else if (*(pCmdBuff+5) == 67)
3035                             putchar(g_wLumiTotalCount >> 8);
3036                         else if (*(pCmdBuff+5) == 68)
3037                             putchar(g_DlcParameters.u8_L_L_U);
3038                         else if (*(pCmdBuff+5) == 69)
3039                             putchar(g_DlcParameters.u8_L_L_D);
3040                         else if (*(pCmdBuff+5) == 70)
3041                             putchar(g_DlcParameters.u8_L_H_U);
3042                         else if (*(pCmdBuff+5) == 71)
3043                             putchar(g_DlcParameters.u8_L_H_D);
3044                         else if (*(pCmdBuff+5) == 72)
3045                             putchar(g_DlcParameters.u8_S_L_U);
3046                         else if (*(pCmdBuff+5) == 73)
3047                             putchar(g_DlcParameters.u8_S_L_D);
3048                         else if (*(pCmdBuff+5) == 74)
3049                             putchar(g_DlcParameters.u8_S_H_U);
3050                         else if (*(pCmdBuff+5) == 75)
3051                             putchar(g_DlcParameters.u8_S_H_D);
3052                         else if (*(pCmdBuff+5) == 76)
3053                             putchar(g_ucHistogramMin);
3054                         else if (*(pCmdBuff+5) == 77)
3055                             putchar(g_ucHistogramMax);
3056                         else if (*(pCmdBuff+5) == 78)
3057                             putchar(g_DlcParameters.ucDlcPureImageMode);
3058                         else if (*(pCmdBuff+5) == 79)
3059                             putchar(g_DlcParameters.ucDlcLevelLimit);
3060                         else if (*(pCmdBuff+5) == 80)
3061                             putchar(g_DlcParameters.ucDlcAvgDelta);
3062                         else if (*(pCmdBuff+5) == 81)
3063                             putchar(g_DlcParameters.ucDlcFastAlphaBlending);
3064                         else if (*(pCmdBuff+5) == 82)
3065                             putchar(g_DlcParameters.bEnableBLE);
3066                         else if (*(pCmdBuff+5) == 83)
3067                             putchar(g_DlcParameters.bEnableWLE);
3068                         else if (*(pCmdBuff+5) == 84)
3069                             putchar(g_DlcParameters.ucDlcBLEPoint);
3070                         else if (*(pCmdBuff+5) == 85)
3071                             putchar(g_DlcParameters.ucDlcWLEPoint);
3072                         else if (*(pCmdBuff+5) == 86)
3073                             putchar(g_DlcParameters.ucDlcAvgDeltaStill);
3074                         else if (*(pCmdBuff+5) == 87)
3075                             putchar(g_DlcParameters.ucDlcYAvgThresholdL);
3076                         else if (*(pCmdBuff+5) == 88)
3077                             putchar(g_DlcParameters.ucDlcYAvgThresholdH);
3078                         else if (*(pCmdBuff+5) == 89)
3079                             putchar(g_DlcParameters.ucCGCCGain_offset);
3080                         else if (*(pCmdBuff+5) == 90)
3081                             putchar(g_DlcParameters.ucCGCChroma_GainLimitH);
3082                         else if (*(pCmdBuff+5) == 91)
3083                             putchar(g_DlcParameters.ucCGCChroma_GainLimitL);
3084                         else if (*(pCmdBuff+5) == 92)
3085                             putchar(g_DlcParameters.ucCGCYCslope);
3086                         else if (*(pCmdBuff+5) == 93)
3087                             putchar(g_DlcParameters.ucCGCYth);
3088                         else if (*(pCmdBuff+5) == 94)
3089                             putchar(g_DlcParameters.bCGCCGainCtrl);
3090                         else if (*(pCmdBuff+5) == 95)
3091                             putchar(g_DlcParameters.ucDlcYAvgThresholdM);
3092                         else if (*(pCmdBuff+5) == 96)
3093                             putchar(g_DlcParameters.ucDlcCurveMode);
3094                         else if (*(pCmdBuff+5) == 97)
3095                             putchar(g_DlcParameters.ucDlcCurveModeMixAlpha);
3096                         else if (*(pCmdBuff+5) == 98)
3097                             putchar(g_DlcParameters.ucDlcAlgorithmMode);
3098                         else if (*(pCmdBuff+5) == 99)
3099                             putchar(g_DlcParameters.ucDlcSepPointH);
3100                         else if (*(pCmdBuff+5) == 100)
3101                             putchar(g_DlcParameters.ucDlcSepPointL);
3102                         else if (*(pCmdBuff+5) == 101)
3103                             putchar(g_DlcParameters.ucDlcCurveDiff_L_TH);
3104                         else if (*(pCmdBuff+5) == 102)
3105                             putchar(g_DlcParameters.ucDlcCurveDiff_H_TH);
3106                         else if (*(pCmdBuff+5) == 103)
3107                             putchar(g_DlcParameters.ucDlcCurveDiffCoringTH);
3108                         else if (*(pCmdBuff+5) == 104)
3109                             putchar(g_DlcParameters.ucDlcAlphaBlendingMin);
3110                         else if (*(pCmdBuff+5) == 105)
3111                             putchar(g_DlcParameters.ucDlcAlphaBlendingMax);
3112                         else if (*(pCmdBuff+5) == 106)
3113                             putchar(g_DlcParameters.ucDlcFlicker_alpha);
3114                         else if (*(pCmdBuff+5) == 107)
3115                             putchar(g_DlcParameters.ucDlcYAVG_L_TH);
3116                         else if (*(pCmdBuff+5) == 108)
3117                             putchar(g_DlcParameters.ucDlcYAVG_H_TH);
3118                         else if (*(pCmdBuff+5) == 109)
3119                             putchar(g_DlcParameters.ucDlcDiffBase_L);
3120                         else if (*(pCmdBuff+5) == 110)
3121                             putchar(g_DlcParameters.ucDlcDiffBase_M);
3122                         else if (*(pCmdBuff+5) == 111)
3123                             putchar(g_DlcParameters.ucDlcDiffBase_H);
3124                         else
3125                             putchar(0);
3126                     }
3127                     else
3128                     {
3129                         putchar(0xF2);
3130                         putchar(0);
3131                     }
3132                 }
3133             }
3134             break;
3135         case URCMD_MST_DLC_TEST_WRITE_INFO:
3136             {
3137                 if (*(pCmdBuff+4) == 0)
3138                 {
3139                     g_DlcParameters.u8_L_L_U = *(pCmdBuff+5);
3140                 }
3141                 else if (*(pCmdBuff+4) == 1)
3142                 {
3143                     g_DlcParameters.u8_L_L_D = *(pCmdBuff+5);
3144                 }
3145                 else if (*(pCmdBuff+4) == 2)
3146                 {
3147                     g_DlcParameters.u8_L_H_U = *(pCmdBuff+5);
3148                 }
3149                 else if (*(pCmdBuff+4) == 3)
3150                 {
3151                     g_DlcParameters.u8_L_H_D = *(pCmdBuff+5);
3152                 }
3153                 else if (*(pCmdBuff+4) == 4)
3154                 {
3155                     g_DlcParameters.u8_S_L_U = *(pCmdBuff+5);
3156                 }
3157                 else if (*(pCmdBuff+4) == 5)
3158                 {
3159                     g_DlcParameters.u8_S_L_D = *(pCmdBuff+5);
3160                 }
3161                 else if (*(pCmdBuff+4) == 6)
3162                 {
3163                     g_DlcParameters.u8_S_H_U = *(pCmdBuff+5);
3164                 }
3165                 else if (*(pCmdBuff+4) == 7)
3166                 {
3167                     g_DlcParameters.u8_S_H_D = *(pCmdBuff+5);
3168                 }
3169                 else if (*(pCmdBuff+4) == 8)
3170                 {
3171                     g_DlcParameters.ucDlcPureImageMode = *(pCmdBuff+5);
3172                 }
3173                 else if (*(pCmdBuff+4) == 9)
3174                 {
3175                     g_DlcParameters.ucDlcLevelLimit = *(pCmdBuff+5);
3176                 }
3177                 else if (*(pCmdBuff+4) == 10)
3178                 {
3179                     g_DlcParameters.ucDlcAvgDelta = *(pCmdBuff+5);
3180                 }
3181                 else if (*(pCmdBuff+4) == 11)
3182                 {
3183                     g_DlcParameters.ucDlcFastAlphaBlending = *(pCmdBuff+5);
3184                 }
3185                 else if (*(pCmdBuff+4) == 12)
3186                 {
3187                     g_DlcParameters.bEnableBLE = *(pCmdBuff+5);
3188                 }
3189                 else if (*(pCmdBuff+4) == 13)
3190                 {
3191                     g_DlcParameters.bEnableWLE = *(pCmdBuff+5);
3192                 }
3193                 else if (*(pCmdBuff+4) == 14)
3194                 {
3195                     g_DlcParameters.ucDlcBLEPoint = *(pCmdBuff+5);
3196                 }
3197                 else if (*(pCmdBuff+4) == 15)
3198                 {
3199                     g_DlcParameters.ucDlcWLEPoint = *(pCmdBuff+5);
3200                 }
3201                 else if (*(pCmdBuff+4) == 16)
3202                 {
3203                     g_DlcParameters.ucDlcAvgDeltaStill= *(pCmdBuff+5);
3204                 }
3205                 else if (*(pCmdBuff+4) == 17)
3206                 {
3207                     g_DlcParameters.ucDlcYAvgThresholdL = *(pCmdBuff+5);
3208                 }
3209                 else if (*(pCmdBuff+4) == 18)
3210                 {
3211                     g_DlcParameters.ucDlcYAvgThresholdH = *(pCmdBuff+5);
3212                 }
3213                 else if (*(pCmdBuff+4) == 19)
3214                 {
3215                     g_DlcParameters.ucCGCCGain_offset = *(pCmdBuff+5);
3216                 }
3217                 else if (*(pCmdBuff+4) == 20)
3218                 {
3219                     g_DlcParameters.ucCGCChroma_GainLimitH = *(pCmdBuff+5);
3220                 }
3221                 else if (*(pCmdBuff+4) == 21)
3222                 {
3223                     g_DlcParameters.ucCGCChroma_GainLimitL = *(pCmdBuff+5);
3224                 }
3225                 else if (*(pCmdBuff+4) == 22)
3226                 {
3227                     g_DlcParameters.ucCGCYCslope = *(pCmdBuff+5);
3228                 }
3229                 else if (*(pCmdBuff+4) == 23)
3230                 {
3231                     g_DlcParameters.ucCGCYth = *(pCmdBuff+5);
3232                 }
3233                 else if (*(pCmdBuff+4) == 24)
3234                 {
3235                     g_DlcParameters.bCGCCGainCtrl = *(pCmdBuff+5);
3236                 }
3237                 else if (*(pCmdBuff+4) == 25)
3238                 {
3239                     g_DlcParameters.ucDlcYAvgThresholdM = *(pCmdBuff+5);
3240                 }
3241                 else if (*(pCmdBuff+4) == 26)
3242                 {
3243                     g_DlcParameters.ucDlcCurveMode = *(pCmdBuff+5);
3244                 }
3245                 else if (*(pCmdBuff+4) == 27)
3246                 {
3247                     g_DlcParameters.ucDlcCurveModeMixAlpha = *(pCmdBuff+5);
3248                 }
3249                 else if (*(pCmdBuff+4) == 28)
3250                 {
3251                     g_DlcParameters.ucDlcAlgorithmMode = *(pCmdBuff+5);
3252                 }
3253                 else if (*(pCmdBuff+4) == 29)
3254                 {
3255                     g_DlcParameters.ucDlcSepPointH = *(pCmdBuff+5);
3256                 }
3257                 else if (*(pCmdBuff+4) == 30)
3258                 {
3259                     g_DlcParameters.ucDlcSepPointL = *(pCmdBuff+5);
3260                 }
3261                 else if (*(pCmdBuff+4) == 31)
3262                 {
3263                     g_DlcParameters.uwDlcBleStartPointTH = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3264                 }
3265                 else if (*(pCmdBuff+4) == 32)
3266                 {
3267                     g_DlcParameters.uwDlcBleEndPointTH = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3268                 }
3269                 else if (*(pCmdBuff+4) == 33)
3270                 {
3271                     g_DlcParameters.ucDlcCurveDiff_L_TH = *(pCmdBuff+5);
3272                 }
3273                 else if (*(pCmdBuff+4) == 34)
3274                 {
3275                     g_DlcParameters.ucDlcCurveDiff_H_TH = *(pCmdBuff+5);
3276                 }
3277                 else if (*(pCmdBuff+4) == 35)
3278                 {
3279                     g_DlcParameters.uwDlcBLESlopPoint_1 = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3280                 }
3281                 else if (*(pCmdBuff+4) == 36)
3282                 {
3283                     g_DlcParameters.uwDlcBLESlopPoint_2 = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3284                 }
3285                 else if (*(pCmdBuff+4) == 37)
3286                 {
3287                     g_DlcParameters.uwDlcBLESlopPoint_3 = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3288                 }
3289                 else if (*(pCmdBuff+4) == 38)
3290                 {
3291                     g_DlcParameters.uwDlcBLESlopPoint_4 = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3292                 }
3293                 else if (*(pCmdBuff+4) == 39)
3294                 {
3295                     g_DlcParameters.uwDlcBLESlopPoint_5 = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3296                 }
3297                 else if (*(pCmdBuff+4) == 40)
3298                 {
3299                     g_DlcParameters.uwDlcDark_BLE_Slop_Min = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3300                 }
3301                 else if (*(pCmdBuff+4) == 41)
3302                 {
3303                     g_DlcParameters.ucDlcCurveDiffCoringTH = *(pCmdBuff+5);
3304                 }
3305                 else if (*(pCmdBuff+4) == 42)
3306                 {
3307                     g_DlcParameters.ucDlcAlphaBlendingMin = *(pCmdBuff+5);
3308                 }
3309                 else if (*(pCmdBuff+4) == 43)
3310                 {
3311                     g_DlcParameters.ucDlcAlphaBlendingMax = *(pCmdBuff+5);
3312                 }
3313                 else if (*(pCmdBuff+4) == 44)
3314                 {
3315                     g_DlcParameters.ucDlcFlicker_alpha= *(pCmdBuff+5);
3316                 }
3317                 else if (*(pCmdBuff+4) == 45)
3318                 {
3319                     g_DlcParameters.ucDlcYAVG_L_TH = *(pCmdBuff+5);
3320                 }
3321                 else if (*(pCmdBuff+4) == 46)
3322                 {
3323                     g_DlcParameters.ucDlcYAVG_H_TH = *(pCmdBuff+5);
3324                 }
3325                 else if (*(pCmdBuff+4) == 47)
3326                 {
3327                     g_DlcParameters.ucDlcDiffBase_L= *(pCmdBuff+5);
3328                 }
3329                 else if (*(pCmdBuff+4) == 48)
3330                 {
3331                     g_DlcParameters.ucDlcDiffBase_M = *(pCmdBuff+5);
3332                 }
3333                 else if (*(pCmdBuff+4) == 49)
3334                 {
3335                     g_DlcParameters.ucDlcDiffBase_H = *(pCmdBuff+5);
3336                 }
3337                 msDlcSpeedupTrigger(16);
3338             }
3339             break;
3340         case URCMD_MST_DLC_TEST_REINIT:
3341             msDlc_AP_ReinitWithoutPara();
3342             msDlcOnOff( _ENABLE, MAIN_WINDOW);
3343             break;
3344         case URCMD_MST_DLC_TEST_CGC_REINIT:
3345             msDlc_CGC_ReInit();
3346             break;
3347         case URCMD_MST_DLC_TEST_READ_Sloplimit:
3348             {
3349                 putchar(0xF2);
3350                 putchar(g_DlcParameters.ucDlcHistogramLimitCurve[*(pCmdBuff+4)]);
3351             }
3352             break;
3353         case URCMD_MST_DLC_TEST_WRITE_Sloplimit:
3354             {
3355                 BYTE i;
3356               #if 1//( ENABLE_UART_CHECKSUM )
3357                 ucTmpLen--; // remove checksum byte
3358               #endif
3359                 for (i=0; i<(ucTmpLen-4-1); i++)
3360                     g_DlcParameters.ucDlcHistogramLimitCurve[*(pCmdBuff+4) + i] = *(pCmdBuff+5 + i);
3361             }
3362             break;
3363     }
3364 }
3365 
3366 //////////////////////////////////////////////////////////////
3367 //
3368 //////////////////////////////////////////////////////////////
msDlcLibVersionCheck(void)3369 WORD msDlcLibVersionCheck(void)
3370 {
3371     return DLC_LIB_VERSION;
3372 }
3373 
msDlcCGCLibVersionCheck(void)3374 WORD msDlcCGCLibVersionCheck(void)
3375 {
3376     return DLC_CGC_LIB_VERSION;
3377 }
3378 
3379 #undef _MSDLC_C_
3380