xref: /utopia/UTPA2-700.0.x/modules/pq/hal/mooney/pq/mhal_pq_adaptive.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 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 #define MHAL_PQ_ADAPTIVE_C
95 
96 //-------------------------------------------------------------------------------------------------
97 //  Include Files
98 //-------------------------------------------------------------------------------------------------
99 #include <string.h>
100 // Common Definition
101 #include "MsCommon.h"
102 #include "MsOS.h"
103 
104 // Internal Definition
105 #include "hwreg_utility2.h"
106 #include "color_reg.h"
107 
108 #include "drvXC_IOPort.h"
109 #include "apiXC.h"
110 #include "apiXC_Dlc.h"
111 #include "mhal_pq_adaptive.h"
112 
113 //-------------------------------------------------------------------------------------------------
114 //  Driver Compiler Options
115 //-------------------------------------------------------------------------------------------------
116 //-------------------------------------------------------------------------------------------------
117 //  Local Variables for DBK
118 //-------------------------------------------------------------------------------------------------
119 #define MaxWidth 4097
120 #define paraSize 32
121 static MS_U8 WidthIIR = 8;
122 
123 //-------------------------------------------------------------------------------------------------
124 //  Local Defines
125 //-------------------------------------------------------------------------------------------------
126 #define MSIF_ADAPTIVE_LIB_CODE_C            {'A','D','A','P','T','I','V','E','_'}
127 #define MSIF_ADAPTIVE_VERSION_C             {'0','0','0','0','0','1'}
128 #define PQ_ADAPTIVE_VERSION  0x9133
129 
Get_PQ_Adaptive_Version(void * pInstance)130 MS_U32 Get_PQ_Adaptive_Version(void *pInstance)
131 {
132     return PQ_ADAPTIVE_VERSION;
133 }
134 
135 #ifndef UNUSED //to avoid compile warnings...
136 #define UNUSED(var) (void)((var) = (var))
137 #endif
138 
139 //-------------------------------------------------------------------------------------------------
140 //  Local Structures
141 //-------------------------------------------------------------------------------------------------
142 
143 //-------------------------------------------------------------------------------------------------
144 //  Global Variables
145 //-------------------------------------------------------------------------------------------------
146 extern MS_U16 _u16PQSrcType_DBK_Detect[PQ_MAX_WINDOW]; //For Auto_DBK SW driver used
147 //static MS_U16 _u16PQPre_SrcType = 255;          //For Auto_DBK SW driver used
148 //static MS_BOOL bdataread = true;                //For Auto_DBK SW driver used
149 static MS_BOOL bg_bAnyCandenceEnable = TRUE;
150 
151 //static MS_BOOL bisHDSource =true;
152 static XC_MCNRLutQmapSetting m_stMCNRLutQmapSetting = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0}};
153 
154 #define SZ_PATTERN_ENABLE 1
155 #define MCNR_LUT_LOAD_FROM_QMAP 1
156 #define PDNR_LUT_LOAD_FROM_QMAP 1
157 //-------------------------------------------------------------------------------------------------
158 //  Local Variables
159 //-------------------------------------------------------------------------------------------------
160 
161 
162 //-------------------------------------------------------------------------------------------------
163 //  Debug Functions
164 //-------------------------------------------------------------------------------------------------
165 
166 
167 //-------------------------------------------------------------------------------------------------
168 //  Local Functions
169 //-------------------------------------------------------------------------------------------------
170 
171 
172 //-------------------------------------------------------------------------------------------------
173 //  Global Functions
174 //-------------------------------------------------------------------------------------------------
minmax(MS_S32 s32TempA,MS_S32 s32TempB,MS_S32 s32TempC)175 MS_S32 minmax(MS_S32 s32TempA, MS_S32 s32TempB, MS_S32 s32TempC) // make sure s32TempB < s32TempC
176 {
177     if (s32TempA<=s32TempB)
178         return s32TempB;
179     else if (s32TempA >= s32TempC)
180         return s32TempC;
181     else
182         return s32TempA;
183 }
184 
max(MS_S32 s32TempA,MS_S32 s32TempB)185 MS_S32 max(MS_S32 s32TempA, MS_S32 s32TempB)
186 {
187     if (s32TempA>=s32TempB)
188         return s32TempA;
189     else
190         return s32TempB;
191 }
192 
max3(MS_S32 s32TempA,MS_S32 s32TempB,MS_S32 s32TempC)193 MS_S32 max3(MS_S32 s32TempA, MS_S32 s32TempB, MS_S32 s32TempC)
194 {
195     if ( (s32TempA>=s32TempB) && (s32TempA>=s32TempC) )
196         return s32TempA;
197     else if ((s32TempB>=s32TempA) && (s32TempB>=s32TempC) )
198         return s32TempB;
199     else
200         return s32TempC;
201 }
202 
min(MS_S32 s32TempA,MS_S32 s32TempB)203 MS_S32 min(MS_S32 s32TempA, MS_S32 s32TempB)
204 {
205     if (s32TempA>=s32TempB)
206         return s32TempB;
207     else
208         return s32TempA;
209 }
210 
min3(MS_S32 s32TempA,MS_S32 s32TempB,MS_S32 s32TempC)211 MS_S32 min3(MS_S32 s32TempA, MS_S32 s32TempB, MS_S32 s32TempC)
212 {
213     if ( (s32TempA<=s32TempB) && (s32TempA<=s32TempC) )
214         return s32TempA;
215     else if ((s32TempB<=s32TempA) && (s32TempB<=s32TempC) )
216         return s32TempB;
217     else
218         return s32TempC;
219 }
220 /******************************************************************************/
221 ///Get control register for adaptive tuning function
222 ///@return MS_U8: Control status
223 /******************************************************************************/
MDrv_SC_get_adaptive_ctrl(void * pInstance)224 MS_U8 MDrv_SC_get_adaptive_ctrl(void *pInstance)
225 {
226     MS_U8 u8Ctrl;
227 
228     u8Ctrl = (MS_U8)MApi_XC_R2BYTEMSK(REG_SC_BK22_7C_L, 0xFF);
229 
230     return u8Ctrl;
231 }
232 
233 /******************************************************************************/
234 ///Get control register for adaptive tuning function
235 ///@return MS_U8: Control status
236 /******************************************************************************/
MDrv_SC_get_adaptive_ctrl2(void * pInstance)237 MS_U8 MDrv_SC_get_adaptive_ctrl2(void *pInstance)
238 {
239     MS_U8 u8Ctrl;
240 
241     u8Ctrl = (MS_U8)(MApi_XC_R2BYTEMSK(REG_SC_BK22_7C_L, 0xFF00) >> 8);
242 
243     return u8Ctrl;
244 }
245 
246 /******************************************************************************/
247 ///Get control register for adaptive tuning function
248 ///@return MS_U8: Control status
249 /******************************************************************************/
MDrv_SC_get_adaptive_ctrl3(void * pInstance)250 MS_U8 MDrv_SC_get_adaptive_ctrl3(void *pInstance)
251 {
252     MS_U8 u8Ctrl;
253 
254     u8Ctrl = (MS_U8) (MApi_XC_R2BYTEMSK(REG_SC_BK30_01_L, 0x8000) >> 8);
255 
256     return u8Ctrl;
257 }
258 
259 /******************************************************************************/
260 ///Read motion value (F2 motion status)
261 ///@return MS_U8: Motion value
262 /******************************************************************************/
MDrv_SC_read_motion_value1(void * pInstance)263 MS_U32 MDrv_SC_read_motion_value1(void *pInstance)
264 {
265     MS_U32 u32MotionValue;
266     MS_U32 u32RegMadi_1C, u32RegMadi_1B, u32RegMadi_1A;
267 
268     u32RegMadi_1C = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK22_0E_L, 0x3F);
269     u32RegMadi_1B = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK22_0D_L, 0xFF00);
270     u32RegMadi_1A = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK22_0D_L, 0x00FF);
271 
272     u32RegMadi_1C = u32RegMadi_1C * 0x10000UL;
273 
274     u32MotionValue = (u32RegMadi_1C + u32RegMadi_1B + u32RegMadi_1A)  ;
275 
276     DRVSCA_DBG(printf("MotionValue = 0x%lx\n", u32MotionValue));
277 
278     return u32MotionValue;
279 }
280 
281 /******************************************************************************/
282 ///Read motion value (Motion count status)
283 ///@return MS_U8: Motion value
284 /******************************************************************************/
MDrv_SC_read_motion_value2(void * pInstance)285 MS_U32 MDrv_SC_read_motion_value2(void *pInstance)
286 {
287     MS_U32 u32MotionValue;
288 
289     u32MotionValue = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK0A_18_L, 0xFFF) + (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK0A_1A_L, 0x7F);
290 
291     return u32MotionValue;
292 }
293 
294 /******************************************************************************/
295 ///Read motion value (Motion count status)
296 ///@return MS_U8: Motion value
297 /******************************************************************************/
MDrv_SC_read_motion_value3(void * pInstance)298 MS_U8 MDrv_SC_read_motion_value3(void *pInstance)
299 {
300     MS_U8 u8MotionValue;
301 
302     u8MotionValue = MApi_XC_R2BYTEMSK(REG_SC_BK0A_1A_L, 0x7F);
303 
304     return u8MotionValue;
305 }
MDrv_SC_read_motion_value4(void * pInstance)306 MS_U32 MDrv_SC_read_motion_value4(void *pInstance)
307 {
308     MS_U32 u32MotionValue;
309 
310     u32MotionValue = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK0A_2E_L, 0x7FFF);
311 
312     return u32MotionValue;
313 }
314 //-------------------------------------------------------------------------------------------------
315 /// Read motion value (Motion count status)
316 /// @return MS_U8: Motion value
317 //-------------------------------------------------------------------------------------------------
Hal_PQ_GetCurrentMotionValue(void * pInstance)318 MS_U8 Hal_PQ_GetCurrentMotionValue(void *pInstance)
319 {
320     MS_U8 u8MotionValue;
321 
322     u8MotionValue  = (MS_U8)((MApi_XC_R2BYTEMSK(REG_SC_BK22_0E_L, 0x3F) << 2) |
323                              (MApi_XC_R2BYTEMSK(REG_SC_BK22_0D_L, 0xC000) >> 6));
324 
325     return u8MotionValue;
326 }
327 
328 /******************************************************************************/
329 ///Read VCnt value
330 ///@return MS_U8: VCnt value
331 /******************************************************************************/
MDrv_SC_read_v_cnt(void * pInstance)332 MS_U8 MDrv_SC_read_v_cnt(void *pInstance)
333 {
334     MS_U8 u8VCnt;
335 
336     u8VCnt = (MS_U8)(MApi_XC_R2BYTEMSK(REG_SC_BK22_0E_L, 0xC0) >> 6);
337     return u8VCnt;
338 }
339 
340 /******************************************************************************/
341 ///Read SDHDD Detect Threshold value
342 ///@return MS_U16: HDSDD_Det_Threshold value
343 /******************************************************************************/
MDrv_SC_read_HDSDD_Det_Threshold_value(void * pInstance)344 MS_U16 MDrv_SC_read_HDSDD_Det_Threshold_value(void *pInstance)
345 {
346     MS_U16 u16Value;
347 
348     u16Value = 0x30;    //(MS_U16)MApi_XC_R2BYTEMSK(REG_SC_BK22_7F_L, 0xFFFF);
349 
350     return u16Value;
351 }
352 
353 /******************************************************************************/
354 ///MCDi Driver
355 /******************************************************************************/
356 
357 #define MF_Gain            30
MDrv_SC_T12_UC_SmallMoving(void * pInstance)358 MS_U32 MDrv_SC_T12_UC_SmallMoving(void *pInstance)
359 {
360     MS_U32 u32beforeMCfeather;
361     MS_U32 u32nonMV0Comfirm, u32MVContradict;
362     MS_U32 u32MovingFeather, u32MVConfirm;
363     static MS_U32 u32PrevMVConfirm = 0;
364 
365     u32beforeMCfeather = MApi_XC_R2BYTE(REG_SC_BK2A_77_L) ;
366     u32nonMV0Comfirm = MApi_XC_R2BYTE(REG_SC_BK2A_79_L);
367     u32MVContradict = MApi_XC_R2BYTE(REG_SC_BK2A_7A_L);
368 
369     u32MVConfirm = (u32nonMV0Comfirm + u32MVContradict) >> 1;
370 
371     u32MVConfirm = (u32PrevMVConfirm * 3 + u32MVConfirm) >> 2;
372     u32PrevMVConfirm = u32MVConfirm;
373 
374     if(u32MVConfirm > (u32beforeMCfeather * MF_Gain))
375     {
376         u32MovingFeather = u32MVConfirm - ((u32beforeMCfeather * MF_Gain)) ;
377     }
378     else
379     {
380         u32MovingFeather = 0;
381      }
382 
383     return u32MovingFeather;  // "Big": in seach range "Small": out of seach range
384 }
385 
386 #define OOSRF_Gain            4
387 #define OOSRF_Power         2
MDrv_SC_T12_UC_InSearchRange_Feather(void * pInstance)388 MS_U32 MDrv_SC_T12_UC_InSearchRange_Feather(void *pInstance)
389 {
390     MS_U32 u32beforeMCfeather, u32afterMCfeather;
391     MS_U32 u32MCOOSRF;
392     static MS_U32 u32PrevbeforeMCfeather = 0;
393     static MS_U32 u32PrevafterMCfeather = 0;
394 
395     u32beforeMCfeather = MApi_XC_R2BYTE(REG_SC_BK2A_77_L) ;
396     u32afterMCfeather = MApi_XC_R2BYTE(REG_SC_BK2A_7B_L);
397 
398     u32beforeMCfeather = (u32PrevbeforeMCfeather * 3 + u32beforeMCfeather) >> 2;
399     u32PrevbeforeMCfeather = u32beforeMCfeather;
400 
401     u32afterMCfeather = (u32PrevafterMCfeather * 3 + u32afterMCfeather) >> 2;
402     u32PrevafterMCfeather = u32afterMCfeather;
403 
404     if(u32beforeMCfeather > (u32afterMCfeather * OOSRF_Gain) >> OOSRF_Power)
405     {
406         u32MCOOSRF = u32beforeMCfeather - ((u32afterMCfeather * OOSRF_Gain) >> OOSRF_Power) ;
407     }
408     else
409     {
410         u32MCOOSRF = 0;
411     }
412 
413     return u32MCOOSRF;  // "Big": in seach range "Small": out of seach range
414 }
415 
416 #define OOSRMC_Gain         20
MDrv_SC_T12_UC_InSearchRange_MvComfirm(void * pInstance)417 MS_U32 MDrv_SC_T12_UC_InSearchRange_MvComfirm(void *pInstance)
418 {
419     MS_U32 u32MV0Comfirm,  u32nonMV0Comfirm, u32MVContradict;
420     static MS_U32 u32PrevMV0Comfirm = 0;
421     static MS_U32 u32PrevnonMV0Comfirm = 0;
422     static MS_U32 u32PrevMVContradict = 0;
423     MS_U32 u32MVComfirm, u32MCOOSRMC;
424 
425     u32MV0Comfirm = MApi_XC_R2BYTE(REG_SC_BK2A_78_L);
426     u32nonMV0Comfirm = MApi_XC_R2BYTE(REG_SC_BK2A_79_L);
427     u32MVContradict = MApi_XC_R2BYTE(REG_SC_BK2A_7A_L);
428 
429     u32MV0Comfirm = (u32PrevMV0Comfirm * 3 + u32MV0Comfirm) >> 2;
430     u32PrevMV0Comfirm = u32MV0Comfirm;
431 
432     u32nonMV0Comfirm = (u32PrevnonMV0Comfirm * 3 + u32nonMV0Comfirm) >> 2;
433     u32PrevnonMV0Comfirm = u32nonMV0Comfirm;
434 
435     u32MVContradict = (u32PrevMVContradict * 3 + u32MVContradict) >> 2;
436     u32PrevMVContradict = u32MVContradict;
437 
438     u32MVComfirm = u32MV0Comfirm + u32nonMV0Comfirm;
439 
440     if(u32MVComfirm > u32MVContradict * OOSRMC_Gain)
441     {
442         u32MCOOSRMC = u32MVComfirm - u32MVContradict * OOSRMC_Gain;
443     }
444     else
445     {
446         u32MCOOSRMC = 0;
447      }
448 
449     return u32MCOOSRMC;  // "Big": in seach range "Small": out of seach range
450 
451 }
452 
453 #define Confirm_Gain         5
MDrv_SC_UC_InSearchRange_MR_Offset(void * pInstance)454 MS_U8 MDrv_SC_UC_InSearchRange_MR_Offset(void *pInstance)
455 {
456     MS_U32 u32MV0Comfirm,  u32nonMV0Comfirm, u32MVContradict;
457     static MS_U32 u32PrevMV0Comfirm = 0;
458     static MS_U32 u32PrevnonMV0Comfirm = 0;
459     static MS_U32 u32PrevMVContradict = 0;
460     MS_U32 u32MVComfirm;
461     MS_U8 u8MROffset;
462 
463     u32MV0Comfirm = MApi_XC_R2BYTE(REG_SC_BK2A_78_L);
464     u32nonMV0Comfirm = MApi_XC_R2BYTE(REG_SC_BK2A_79_L);
465     u32MVContradict = MApi_XC_R2BYTE(REG_SC_BK2A_7A_L);
466 
467     u32MV0Comfirm = (u32PrevMV0Comfirm * 3 + u32MV0Comfirm) >> 2;
468     u32PrevMV0Comfirm = u32MV0Comfirm;
469 
470     u32nonMV0Comfirm = (u32PrevnonMV0Comfirm * 3 + u32nonMV0Comfirm) >> 2;
471     u32PrevnonMV0Comfirm = u32nonMV0Comfirm;
472 
473     u32MVContradict = (u32PrevMVContradict * 3 + u32MVContradict) >> 2;
474     u32PrevMVContradict = u32MVContradict;
475 
476     u32MVComfirm = u32MV0Comfirm + u32nonMV0Comfirm;
477 
478     if(u32MVComfirm > (u32MVContradict * Confirm_Gain))
479     {
480         u8MROffset = u32nonMV0Comfirm;
481     }
482     else
483     {
484         u8MROffset = 0;
485     }
486 
487     return u8MROffset;
488 }
489 
490 #define ISR_NONMV0_Gain         15
MDrv_SC_T12_UC_InSearchRange_nonMv0Comfirm(void * pInstance)491 MS_U32 MDrv_SC_T12_UC_InSearchRange_nonMv0Comfirm(void *pInstance)
492 {
493     MS_U32 u32MV0Comfirm,  u32nonMV0Comfirm, u32MVContradict;
494     static MS_U32 u32PrevMV0Comfirm = 0;
495     static MS_U32 u32PrevnonMV0Comfirm = 0;
496     static MS_U32 u32PrevMVContradict = 0;
497     MS_U32 u32MVComfirm, u32ComfirmValue;
498 
499     u32MV0Comfirm = MApi_XC_R2BYTE(REG_SC_BK2A_78_L);
500     u32nonMV0Comfirm = MApi_XC_R2BYTE(REG_SC_BK2A_79_L);
501     u32MVContradict = MApi_XC_R2BYTE(REG_SC_BK2A_7A_L);
502 
503     u32MV0Comfirm = (u32PrevMV0Comfirm * 3 + u32MV0Comfirm) >> 2;
504     u32PrevMV0Comfirm = u32MV0Comfirm;
505 
506     u32nonMV0Comfirm = (u32PrevnonMV0Comfirm * 3 + u32nonMV0Comfirm) >> 2;
507     u32PrevnonMV0Comfirm = u32nonMV0Comfirm;
508 
509     u32MVContradict = (u32PrevMVContradict * 3 + u32MVContradict) >> 2;
510     u32PrevMVContradict = u32MVContradict;
511 
512     u32MVComfirm = /*MV0_Comfim +*/u32nonMV0Comfirm;
513 
514     if(u32MVComfirm > (u32MVContradict * ISR_NONMV0_Gain))
515    {
516         u32ComfirmValue = u32MVComfirm - u32MVContradict * ISR_NONMV0_Gain;
517    }
518     else
519    {
520         u32ComfirmValue = 0;
521    }
522 
523     return u32ComfirmValue;  // "Big": in seach range "Small": out of seach range
524 
525 }
526 
MDrv_SC_T12_DIPF_TemporalGainControl(void * pInstance,MS_U32 u32OOSRMC,MS_U32 u32OOSRF,MS_U32 u32ISRNONMV0)527 void MDrv_SC_T12_DIPF_TemporalGainControl(void *pInstance, MS_U32 u32OOSRMC, MS_U32 u32OOSRF, MS_U32 u32ISRNONMV0)
528 {
529     static MS_U16 u16TemporalGainControl = 0x0F;
530 
531     UNUSED(u32OOSRMC);
532     UNUSED(u32OOSRF);
533 
534     if(u32ISRNONMV0 > 1000)  //&& OOSRF > 50)
535     {
536         if(u16TemporalGainControl > 0x00)
537        {
538             u16TemporalGainControl--;
539         }
540         else
541         {
542             u16TemporalGainControl = 0;
543         }
544     }
545     else
546     {
547         if(u16TemporalGainControl < 0x0F)
548         {
549             u16TemporalGainControl++;
550         }
551         else
552         {
553             u16TemporalGainControl = 0x0F;
554         }
555     }
556     MApi_XC_W2BYTEMSK(REG_SC_BK22_14_L, (MS_U16)u16TemporalGainControl, 0x000F);
557 
558 }
559 
560 #define ComplexOffset_MaxValue 0x06
561 #define ComplexOffset_MinValue 0x00
MDrv_SC_T12_UCDi_ComplexOffset(void * pInstance,MS_U32 u32OOSRMC,MS_U32 u32OOSRF,MS_U32 u32ISRNONMV0)562 void MDrv_SC_T12_UCDi_ComplexOffset(void *pInstance, MS_U32 u32OOSRMC, MS_U32 u32OOSRF, MS_U32 u32ISRNONMV0)
563 {
564     static MS_U16 u16ComplexOffset = ComplexOffset_MaxValue;
565 
566     UNUSED(u32OOSRMC);
567     UNUSED(u32OOSRF);
568 
569     if(u32ISRNONMV0 > 1000)  //&& OOSRF > 50)
570     {
571         if(u16ComplexOffset > ComplexOffset_MinValue)
572         {
573             u16ComplexOffset--;
574         }
575         else
576         {
577             u16ComplexOffset = ComplexOffset_MinValue;
578         }
579     }
580     else
581     {
582         if(u16ComplexOffset < ComplexOffset_MaxValue)
583         {
584             u16ComplexOffset++;
585         }
586         else
587         {
588             u16ComplexOffset = ComplexOffset_MaxValue;
589         }
590     }
591     MApi_XC_W2BYTEMSK(REG_SC_BK2A_2E_L, (MS_U16)u16ComplexOffset << 4, 0x00F0);
592 }
593 
594 #define OP1_HisWeight_MaxValue 0x0E
595 #define OP1_HisWeight_MinValue 0x04
MDrv_SC_T12_UCDi_OP1_HisWeight(void * pInstance,MS_U8 u8MotLvl)596 void MDrv_SC_T12_UCDi_OP1_HisWeight(void *pInstance, MS_U8 u8MotLvl)
597 {
598     MS_BOOL bfilm;
599 
600     bfilm = ((MS_U16)MApi_XC_R2BYTEMSK(REG_SC_BK0A_21_L, 0x0008) == 0x0008) ||   // film22
601            ((MS_U16)MApi_XC_R2BYTEMSK(REG_SC_BK0A_21_L, 0x0010) == 0x0010) ||   // film32
602            ((MS_U16)MApi_XC_R2BYTEMSK(REG_SC_BK0A_21_L, 0x0040) == 0x0040) ;    // film any
603 
604     if((u8MotLvl >= 1) || (true == bfilm))
605     {
606         MApi_XC_W2BYTEMSK(REG_SC_BK2A_02_L, OP1_HisWeight_MaxValue, 0x000e);  // history weight = 6
607     }
608     else
609     {
610         MApi_XC_W2BYTEMSK(REG_SC_BK2A_02_L, OP1_HisWeight_MinValue, 0x000e);  // history weight = 2
611     }
612 }
613 
614 #define A5_HIS_MOTION_TH 0x0A
MDrv_SC_A5_UCDi_OP1_HisWeight(void * pInstance,const MS_U32 u32MotionValue,const MS_U32 u32FeatherSum,const MS_U8 u8MotionValue3)615 void MDrv_SC_A5_UCDi_OP1_HisWeight(void *pInstance, const MS_U32 u32MotionValue,const MS_U32 u32FeatherSum,const MS_U8 u8MotionValue3)
616 {
617     static MS_U32 u32reghisweight = OP1_HisWeight_MinValue;
618     static MS_U32 u32MotionValue_prev = 0;
619     MS_BOOL bisSDsource;
620     MS_U32 u32Htotalpix = 0;
621     MS_U32 u32Vtotalpix = 0;
622     u32Htotalpix = MApi_XC_R2BYTE(REG_SC_BK02_3E_L);
623     u32Vtotalpix = MApi_XC_R2BYTE(REG_SC_BK02_3F_L);
624 
625     if( u32MotionValue_prev < u32MotionValue )
626     {
627         u32MotionValue_prev = ((u32MotionValue_prev*3 )+ u32MotionValue ) >> 2;
628     }
629     else
630     {
631         u32MotionValue_prev = u32MotionValue;
632     }
633 
634     if(u8MotionValue3 < A5_HIS_MOTION_TH)
635     {
636         u32reghisweight = OP1_HisWeight_MinValue;
637     }
638     else if( u32MotionValue_prev < A5_HIS_MOTION_TH )
639     {
640         u32reghisweight = OP1_HisWeight_MinValue;
641     }
642     else
643     {
644         if( u32reghisweight < OP1_HisWeight_MaxValue )
645         {
646             u32reghisweight++;
647         }
648         else
649         {
650             u32reghisweight = OP1_HisWeight_MaxValue;
651         }
652     }
653 
654     if((u32Htotalpix < 0x5AA)&&(u32Vtotalpix < 0x120))
655     {
656         bisSDsource = true;
657     }
658     else
659     {
660         bisSDsource = false;
661     }
662 
663     if((u32FeatherSum > 0x3500)&&(bisSDsource == true))
664     {
665         MApi_XC_W2BYTEMSK(REG_SC_BK2A_02_L, OP1_HisWeight_MinValue, 0x000e);  // history weight = 6
666     }
667     else
668     {
669         MApi_XC_W2BYTEMSK(REG_SC_BK2A_02_L, u32reghisweight, 0x000E);
670     }
671 }
672 
673 #define FeatherValueGain_MaxValue 0x02
674 #define FeatherValueGain_MinValue 0x00
MDrv_SC_T12_UCDi_FeatherValueGain(void * pInstance,MS_U32 u32OOSRMC,MS_U32 u32OOSRF,MS_U32 u32ISRNONMV0)675 void MDrv_SC_T12_UCDi_FeatherValueGain(void *pInstance, MS_U32 u32OOSRMC, MS_U32 u32OOSRF, MS_U32 u32ISRNONMV0)
676 {
677     static MS_U16 u16FeatherValue = FeatherValueGain_MaxValue;
678 
679     UNUSED(u32OOSRMC);
680     UNUSED(u32OOSRF);
681 
682     if(u32ISRNONMV0 > 1000)  //&& OOSRF > 50)
683     {
684         if(u16FeatherValue > FeatherValueGain_MinValue)
685         {
686             u16FeatherValue--;
687         }
688         else
689         {
690             u16FeatherValue = FeatherValueGain_MinValue;
691         }
692     }
693     else
694     {
695         if(u16FeatherValue < FeatherValueGain_MaxValue)
696         {
697             u16FeatherValue++;
698         }
699         else
700         {
701             u16FeatherValue = FeatherValueGain_MaxValue;
702         }
703     }
704     MApi_XC_W2BYTEMSK(REG_SC_BK2A_2D_L, (MS_U16)u16FeatherValue << 12, 0xF000);
705 
706 }
707 
708 #define W2SadGain_MaxValue 0x0C
709 #define W2SadGain_MinValue 0x08
MDrv_SC_T12_UCDi_W2SadGain(void * pInstance,MS_U32 u32OOSRMC,MS_U32 u32OOSRF,MS_U32 u32ISRNONMV0)710 void MDrv_SC_T12_UCDi_W2SadGain(void *pInstance, MS_U32 u32OOSRMC, MS_U32 u32OOSRF, MS_U32 u32ISRNONMV0)
711 {
712     static MS_U16 u16W2SadGain = FeatherValueGain_MaxValue;
713 
714     UNUSED(u32OOSRMC);
715     UNUSED(u32OOSRF);
716 
717     if(u32ISRNONMV0 > 1000)  //&& OOSRF > 50)
718     {
719         if(u16W2SadGain < W2SadGain_MaxValue)
720         {
721             u16W2SadGain++;
722         }
723         else
724         {
725             u16W2SadGain = W2SadGain_MaxValue;
726         }
727     }
728     else
729     {
730         if(u16W2SadGain > W2SadGain_MinValue)
731         {
732             u16W2SadGain--;
733         }
734         else
735         {
736             u16W2SadGain = W2SadGain_MinValue;
737         }
738     }
739     MApi_XC_W2BYTEMSK(REG_SC_BK2A_2D_L, (MS_U16)u16W2SadGain << 8, 0x0F00);
740 
741 }
742 
743 #define VerticalMovingUdRatio_MaxValue 0x08
744 #define VerticalMovingUdRatio_MinValue 0x07
MDrv_SC_T12_UCDi_VerticalMovingUdRatio(void * pInstance,MS_U32 u32OOSRMC,MS_U32 u32OOSRF,MS_U32 u32ISRNONMV0)745 void MDrv_SC_T12_UCDi_VerticalMovingUdRatio(void *pInstance, MS_U32 u32OOSRMC, MS_U32 u32OOSRF, MS_U32 u32ISRNONMV0)
746 {
747     static MS_U16 u16VerticalMovingUdRatioValue = VerticalMovingUdRatio_MaxValue;
748 
749     UNUSED(u32OOSRMC);
750     UNUSED(u32ISRNONMV0);
751 
752     if( u32OOSRF > 0x300 )
753     {
754         if(u16VerticalMovingUdRatioValue > VerticalMovingUdRatio_MinValue)
755         {
756             u16VerticalMovingUdRatioValue--;
757         }
758         else
759         {
760             u16VerticalMovingUdRatioValue = VerticalMovingUdRatio_MinValue;
761         }
762     }
763     else
764     {
765         if(u16VerticalMovingUdRatioValue < VerticalMovingUdRatio_MaxValue)
766         {
767             u16VerticalMovingUdRatioValue++;
768         }
769         else
770         {
771             u16VerticalMovingUdRatioValue = VerticalMovingUdRatio_MaxValue;
772         }
773     }
774     MApi_XC_W2BYTEMSK(REG_SC_BK2A_28_L, (MS_U16)u16VerticalMovingUdRatioValue, 0x000F);
775 }
776 
777 #define KFC_8bitMotionGain_MaxValue 0x08
778 #define KFC_8bitMotionGain_MinValue 0x04 //0 have feather issue.
MDrv_SC_T12_KFC_8bitMotionGain(void * pInstance,MS_U32 SmallMoving)779 void MDrv_SC_T12_KFC_8bitMotionGain(void *pInstance, MS_U32 SmallMoving)
780 {
781     static MS_U16 u16KFC8bitMotionGainValue = KFC_8bitMotionGain_MaxValue;
782 
783     if(SmallMoving > 500)
784     {
785         if(u16KFC8bitMotionGainValue > KFC_8bitMotionGain_MinValue)
786         {
787             u16KFC8bitMotionGainValue--;
788         }
789         else
790         {
791             u16KFC8bitMotionGainValue = KFC_8bitMotionGain_MinValue;
792         }
793     }
794     else
795     {
796         if(u16KFC8bitMotionGainValue < KFC_8bitMotionGain_MaxValue)
797         {
798             u16KFC8bitMotionGainValue++;
799         }
800         else
801         {
802             u16KFC8bitMotionGainValue = KFC_8bitMotionGain_MaxValue;
803         }
804     }
805     MApi_XC_W2BYTEMSK(REG_SC_BK2A_3B_L, (MS_U16)u16KFC8bitMotionGainValue << 4, 0x00F0);
806 }
807 
808 #define UCDi_EODiWGain_MaxValue 0x0F
809 #define UCDi_EODiWGain_MinValue 0x00
MDrv_SC_T12_UCDi_EODiWGain(void * pInstance,MS_U32 u32OOSRMC,MS_U32 u32OOSRF,MS_U32 u32ISRNONMV0)810 void MDrv_SC_T12_UCDi_EODiWGain(void *pInstance, MS_U32 u32OOSRMC, MS_U32 u32OOSRF, MS_U32 u32ISRNONMV0)
811 {
812     static MS_U16 u16EODiWGainValue = UCDi_EODiWGain_MaxValue;
813 
814     UNUSED(u32OOSRMC);
815 
816     if((u32ISRNONMV0 > 1000) && (u32OOSRF > 50))
817     {
818         if(u16EODiWGainValue > UCDi_EODiWGain_MinValue)
819         {
820             u16EODiWGainValue--;
821         }
822         else
823         {
824             u16EODiWGainValue = UCDi_EODiWGain_MinValue;
825         }
826     }
827     else
828     {
829         if(u16EODiWGainValue < UCDi_EODiWGain_MaxValue)
830         {
831             u16EODiWGainValue++;
832         }
833         else
834         {
835             u16EODiWGainValue = UCDi_EODiWGain_MaxValue;
836         }
837     }
838     MApi_XC_W2BYTEMSK(REG_SC_BK22_2A_L, (MS_U16)u16EODiWGainValue << 12, 0xF000);
839 }
840 
841 #define UCDi_HistoryRatio_MaxValue 0x07
842 #define UCDi_HistoryRatio_MinValue 0x04
MDrv_SC_T12_UCDi_HistoryRatio(void * pInstance,MS_U32 u32OOSRMC,MS_U32 u32OOSRF,MS_U32 u32ISRNONMV0)843 void MDrv_SC_T12_UCDi_HistoryRatio(void *pInstance, MS_U32 u32OOSRMC, MS_U32 u32OOSRF, MS_U32 u32ISRNONMV0)
844 {
845     static MS_U16 u16HistoryRatioValue = UCDi_HistoryRatio_MaxValue;
846 
847     UNUSED(u32OOSRMC);
848 
849     if((u32ISRNONMV0 > 1000) && (u32OOSRF > 50))
850     {
851         if(u16HistoryRatioValue > UCDi_HistoryRatio_MinValue)
852        {
853             u16HistoryRatioValue--;
854         }
855         else
856         {
857             u16HistoryRatioValue = UCDi_HistoryRatio_MinValue;
858         }
859     }
860     else
861     {
862         if(u16HistoryRatioValue < UCDi_HistoryRatio_MaxValue)
863         {
864             u16HistoryRatioValue++;
865         }
866         else
867         {
868             u16HistoryRatioValue = UCDi_HistoryRatio_MaxValue;
869         }
870     }
871     MApi_XC_W2BYTEMSK(REG_SC_BK2A_2E_L, (MS_U16)u16HistoryRatioValue << 12, 0xF000);
872 }
873 
874 #define KFC_EODIW_GAIN_MAX 4
875 #define KFC_EODIW_GAIN_MIN 0
MDrv_SC_A5_KFC_EODiW(void * pInstance,const MS_U32 u32IsVerticalMoving)876 void MDrv_SC_A5_KFC_EODiW(void *pInstance, const MS_U32 u32IsVerticalMoving)
877 {
878     MS_U32 u32Statisticscplxpixel;
879     static MS_U32 u32Statisticscplxpixeliir = KFC_EODIW_GAIN_MIN;
880     static MS_U32 u32regkfceodiwgain = KFC_EODIW_GAIN_MAX ;
881 
882     u32Statisticscplxpixel = ((u32Statisticscplxpixeliir*3 )+ MApi_XC_R2BYTE(REG_SC_BK2A_7F_L) ) >> 2;
883     u32Statisticscplxpixeliir = u32Statisticscplxpixel;
884 
885     if( (u32IsVerticalMoving < 0x150) && (u32Statisticscplxpixeliir > 0x1000) )
886     {
887         if( u32regkfceodiwgain > KFC_EODIW_GAIN_MIN )
888        {
889             u32regkfceodiwgain--;
890         }
891         else
892         {
893             u32regkfceodiwgain  = KFC_EODIW_GAIN_MIN;
894         }
895     }
896     else
897     {
898         if( u32regkfceodiwgain < KFC_EODIW_GAIN_MAX )
899         {
900             u32regkfceodiwgain++;
901         }
902         else
903         {
904             u32regkfceodiwgain = KFC_EODIW_GAIN_MAX;
905         }
906     }
907      MApi_XC_W2BYTEMSK(REG_SC_BK22_28_L, (MS_U16)u32regkfceodiwgain << 12, 0xF000);
908 }
909 
MDrv_SC_SST_Ctrl(void * pInstance,const MS_U32 u32FeatherSum,const MS_BOOL bisHDSrc)910 void MDrv_SC_SST_Ctrl(void *pInstance, const MS_U32 u32FeatherSum, const MS_BOOL bisHDSrc)
911 {
912     static MS_BOOL bReg_SST_en = true;
913     //MS_U32 u32mv0_feather;
914     MS_U32 u32hor_feather;
915     //u32mv0_feather = MApi_XC_R2BYTE(REG_SC_BK2A_77_L);
916     u32hor_feather = MApi_XC_R2BYTE(REG_SC_BK2A_7B_L);
917     //if( (u32FeatherSum > 0x1200) && (u32mv0_feather>0x1100) && (!bisHDSrc) && (SZ_PATTERN_ENABLE ==1) ) //Add feathersum for TSB#3 capter4 white noise
918     if( (u32FeatherSum > 0x1200) && (u32hor_feather>0x1100) && (!bisHDSrc) && (SZ_PATTERN_ENABLE ==1) )
919     {
920         bReg_SST_en = false;
921     }
922     else
923     {
924         bReg_SST_en = true;
925     }
926     //printf("===u32FeatherSum[%x] u32mv0_feather[%x] u32hor_feather[%x] !bisHDSrc[%d] SZ_PATTERN_ENABLE[%d]====\n",u32FeatherSum,u32mv0_feather,u32hor_feather,(!bisHDSrc),SZ_PATTERN_ENABLE);
927     MApi_XC_W2BYTEMSK(REG_SC_BK2A_04_L, (MS_U16)(bReg_SST_en << 8), 0x0100);
928 }
929 
930 #define SONYC_KFC_EN_MAX 1
931 #define SONYC_KFC_EN_MIN 0
932 #define SONYC_CPLX_GAIN_MAX 4
933 #define SONYC_CPLX_GAIN_MIN 0
MDrv_SC_A5_SonyChurch(void * pInstance,const MS_U32 u32IsVerticalMoving,const MS_U32 u32IsHorizontalMoving,const MS_U32 u32MotionValue,const MS_BOOL bL107_en)934 void MDrv_SC_A5_SonyChurch(void *pInstance, const MS_U32 u32IsVerticalMoving, const MS_U32 u32IsHorizontalMoving, const MS_U32 u32MotionValue, const MS_BOOL bL107_en)
935 {
936     static MS_U32 u32regkfcen = SONYC_KFC_EN_MAX ;
937     static MS_U32 u32regmcdicplxgain = SONYC_CPLX_GAIN_MAX;
938 
939     //IsHorizontalMoving=IsHorizontalMoving;
940     if( (u32IsVerticalMoving > 0xF0) && (u32IsHorizontalMoving < 1) && (u32MotionValue > 0x40000) )
941     {
942         if( u32regmcdicplxgain > SONYC_CPLX_GAIN_MIN )
943         {
944             u32regmcdicplxgain--;
945         }
946         else
947         {
948             u32regmcdicplxgain = SONYC_CPLX_GAIN_MIN;
949         }
950 
951         if( u32regkfcen > SONYC_KFC_EN_MIN )
952         {
953             u32regkfcen--;
954         }
955         else
956         {
957             u32regkfcen = SONYC_KFC_EN_MIN;
958         }
959     }
960     else
961     {
962         if( u32regmcdicplxgain < SONYC_CPLX_GAIN_MAX )
963         {
964             u32regmcdicplxgain++;
965         }
966         else
967         {
968             u32regmcdicplxgain = SONYC_CPLX_GAIN_MAX;
969         }
970 
971         if( u32regkfcen < SONYC_KFC_EN_MAX )
972         {
973             u32regkfcen++;
974         }
975         else
976         {
977             u32regkfcen = SONYC_KFC_EN_MAX;
978         }
979     }
980 
981     if( !bL107_en )
982     {
983         MApi_XC_W2BYTEMSK(REG_SC_BK2A_04_L, (MS_U16)u32regkfcen<<1, 0x0002);
984         MApi_XC_W2BYTEMSK(REG_SC_BK2A_2E_L, (MS_U16)u32regmcdicplxgain<<8, 0x0F00);
985     }
986 }
987 
988 #define L107_FEATHER_GAIN_MAX 2
989 #define L107_FEATHER_GAIN_MIN 0
990 #define L107_CPLX_OFFSET_MAX 6
991 #define L107_CPLX_OFFSET_MIN 0
992 #define L107_CPLX_GAIN_MAX 15
993 #define L107_CPLX_GAIN_MIN 4
MDrv_SC_A5_L107(void * pInstance,const MS_U32 u32IsVerticalMoving,const MS_U32 u32IsHorizontalMoving,const MS_U32 u32TotalFeather,const MS_U32 u32TotalMvComfirm,const MS_U32 u8colorSum,const MS_U32 u32MotionValue,const MS_U32 u32uComplex)994 MS_BOOL MDrv_SC_A5_L107(void *pInstance, const MS_U32 u32IsVerticalMoving, const MS_U32 u32IsHorizontalMoving, const MS_U32 u32TotalFeather, const MS_U32 u32TotalMvComfirm, const MS_U32 u8colorSum,const MS_U32 u32MotionValue, const MS_U32 u32uComplex )
995 {
996     static MS_U32 u32regmcdifeathergain = L107_FEATHER_GAIN_MAX ;
997     static MS_U32 u32regmcdicplxoffset = L107_CPLX_OFFSET_MAX ;
998     static MS_U32 u32regmcdicplxgain = L107_CPLX_GAIN_MIN ;
999     MS_BOOL bL107_en;
1000 
1001     if( (u32IsVerticalMoving < 20) && (u32IsHorizontalMoving < 200) && (u32TotalFeather < 100) && (u32TotalMvComfirm > 200) && (u8colorSum > 200) && (u32MotionValue > 0x3500) && (u32uComplex < 0x700))
1002     {
1003         bL107_en = TRUE;
1004 
1005         if( u32regmcdifeathergain > L107_FEATHER_GAIN_MIN )
1006         {
1007             u32regmcdifeathergain--;
1008         }
1009         else
1010         {
1011             u32regmcdifeathergain = L107_FEATHER_GAIN_MIN;
1012         }
1013 
1014         if( u32regmcdicplxoffset > L107_CPLX_OFFSET_MIN )
1015         {
1016             u32regmcdicplxoffset--;
1017         }
1018         else
1019         {
1020             u32regmcdicplxoffset = L107_CPLX_OFFSET_MIN;
1021         }
1022 
1023         if( u32regmcdicplxgain < L107_CPLX_GAIN_MAX )
1024         {
1025             u32regmcdicplxgain++;
1026         }
1027         else
1028         {
1029             u32regmcdicplxgain = L107_CPLX_GAIN_MAX;
1030         }
1031     }
1032     else
1033     {
1034         bL107_en = FALSE;
1035 
1036         if( u32regmcdifeathergain < L107_FEATHER_GAIN_MAX )
1037         {
1038             u32regmcdifeathergain++;
1039         }
1040         else
1041         {
1042             u32regmcdifeathergain = L107_FEATHER_GAIN_MAX;
1043         }
1044 
1045         if( u32regmcdicplxoffset < L107_CPLX_OFFSET_MAX )
1046         {
1047             u32regmcdicplxoffset++;
1048         }
1049         else
1050         {
1051             u32regmcdicplxoffset = L107_CPLX_OFFSET_MAX;
1052         }
1053 
1054         if( u32regmcdicplxgain > L107_CPLX_GAIN_MIN )
1055         {
1056             u32regmcdicplxgain--;
1057         }
1058         else
1059         {
1060             u32regmcdicplxgain = L107_CPLX_GAIN_MIN;
1061         }
1062     }
1063 
1064     MApi_XC_W2BYTEMSK(REG_SC_BK2A_2D_L, (MS_U16)u32regmcdifeathergain<<12, 0xF000); // 2 to 0
1065     MApi_XC_W2BYTEMSK(REG_SC_BK2A_2E_L, (MS_U16)u32regmcdicplxoffset<<4, 0x00F0);// 6 to 0
1066     MApi_XC_W2BYTEMSK(REG_SC_BK2A_2E_L, (MS_U16)u32regmcdicplxgain<<8, 0x0F00);// 4 to F
1067 
1068     return bL107_en;
1069 }
1070 
MDrv_SC_A5_ComplexSum(void * pInstance)1071 MS_U32 MDrv_SC_A5_ComplexSum(void *pInstance)
1072 {
1073     static MS_U32 u32IsComplex_iir = 0xFFFF;
1074     MS_U32 u32IsComplex;
1075 
1076     u32IsComplex = MApi_XC_R2BYTE(REG_SC_BK2A_7F_L);
1077 
1078     u32IsComplex = (u32IsComplex_iir*3 + u32IsComplex ) >> 2;
1079     u32IsComplex_iir = u32IsComplex;
1080 
1081     return u32IsComplex_iir;
1082 }
1083 
MDrv_SC_A5_KFC_IsVerticalMoving(void * pInstance)1084 MS_U32 MDrv_SC_A5_KFC_IsVerticalMoving(void *pInstance)
1085 {
1086     MS_U32 u32mvc, u32mvu, u32mvd;
1087     MS_U32 u32mevertical;
1088     static MS_U32 u32IsVerticalMovingiir = 0;
1089 
1090     u32mvc     = MApi_XC_R2BYTE(REG_SC_BK2A_7C_L);
1091     u32mvu     = MApi_XC_R2BYTE(REG_SC_BK2A_7D_L);
1092     u32mvd     = MApi_XC_R2BYTE(REG_SC_BK2A_7E_L);
1093 
1094     if ((u32mvu > u32mvc) && (u32mvu > u32mvd) /*&& (u32_mvu > 0x200*/)
1095     {
1096         if( u32mvu > (u32mvc+u32mvd)/2 )
1097         {
1098             u32mevertical = (u32mvu - (u32mvc+u32mvd)/2);
1099         }
1100         else
1101         {
1102             u32mevertical = 0;
1103         }
1104     }
1105     else if((u32mvd > u32mvc) && (u32mvd > u32mvu) /*&& (u32_mvd > 0x200)*/)
1106     {
1107         if( u32mvd > (u32mvc+u32mvu)/2 )
1108         {
1109             u32mevertical = (u32mvd - (u32mvc+u32mvu)/2);
1110         }
1111         else
1112         {
1113             u32mevertical = 0;
1114         }
1115     }
1116     else
1117     {
1118         u32mevertical = 0;
1119     }
1120 
1121     if( u32mevertical > 255 )
1122     {
1123         u32mevertical = 255;
1124     }
1125 
1126     u32mevertical = (u32IsVerticalMovingiir*3 + u32mevertical ) >> 2;
1127     u32IsVerticalMovingiir = u32mevertical;
1128 
1129     return u32mevertical;
1130 }
1131 
MDrv_SC_A5_IsHorizontalMoving(void * pInstance)1132 MS_U32 MDrv_SC_A5_IsHorizontalMoving(void *pInstance)
1133 {
1134     MS_U32 u32mvc, u32mvu, u32mvd;
1135     MS_U32 u32mehorizontal;
1136     static MS_U32 u32IsHorizontalMovingiir = 0;
1137 
1138     u32mvc     = MApi_XC_R2BYTE(REG_SC_BK2A_7C_L);
1139     u32mvu     = MApi_XC_R2BYTE(REG_SC_BK2A_7D_L);
1140     u32mvd     = MApi_XC_R2BYTE(REG_SC_BK2A_7E_L);
1141 
1142     if ((u32mvc > u32mvu) && (u32mvc > u32mvd) /*&& (u32_mvu > 0x200*/)
1143     {
1144         if( u32mvc > (u32mvu+u32mvd)/2 )
1145         {
1146             u32mehorizontal = (u32mvc - (u32mvu+u32mvd)/2);
1147         }
1148         else
1149         {
1150             u32mehorizontal = 0;
1151         }
1152     }
1153     else
1154     {
1155         u32mehorizontal = 0;
1156     }
1157 
1158     if( u32mehorizontal > 255 )
1159     {
1160         u32mehorizontal = 255;
1161     }
1162 
1163     u32mehorizontal = (u32IsHorizontalMovingiir*3 + u32mehorizontal ) >> 2;
1164     u32IsHorizontalMovingiir = u32mehorizontal;
1165 
1166     return u32mehorizontal;
1167 }
1168 
1169 
MDrv_SC_A5_ColorPixelSum(void * pInstance)1170 MS_U32 MDrv_SC_A5_ColorPixelSum(void *pInstance)
1171 {
1172    MS_U32 u32colorSum;
1173    static MS_U32 u32colorSum_iir = 0;
1174 
1175    u32colorSum  = MApi_XC_R2BYTE(REG_SC_BK1A_6E_L);
1176    u32colorSum  = u32colorSum / 8;
1177 
1178    if( u32colorSum > 255 )
1179    {
1180     u32colorSum = 255;
1181    }
1182 
1183    u32colorSum = ((u32colorSum_iir*3 )+ u32colorSum ) >> 2;
1184    u32colorSum_iir = u32colorSum;
1185 
1186    return u32colorSum_iir;
1187 }
1188 
1189 
MDrv_SC_A5_TotalFeather(void * pInstance)1190 MS_U32 MDrv_SC_A5_TotalFeather(void *pInstance)
1191 {
1192     MS_U32 u32mv0feather, u32nonMv0feather;
1193     MS_U32 u32totalfeather;
1194     static MS_U32 u32totalfeatheriir = 255;
1195 
1196     u32mv0feather      = MApi_XC_R2BYTE(REG_SC_BK2A_77_L);
1197     u32nonMv0feather   = MApi_XC_R2BYTE(REG_SC_BK2A_7B_L);
1198 
1199     u32totalfeather = ( u32mv0feather + u32nonMv0feather ) / 4;
1200 
1201     if( u32totalfeather > 255 )
1202     {
1203         u32totalfeather = 255;
1204     }
1205 
1206     u32totalfeather = ((u32totalfeatheriir*3 )+ u32totalfeather ) >> 2;
1207     u32totalfeatheriir = u32totalfeather;
1208 
1209     return u32totalfeatheriir;
1210 }
1211 
MDrv_SC_TotalFeather_Sum(void * pInstance)1212 MS_U32 MDrv_SC_TotalFeather_Sum(void *pInstance)
1213 {
1214     MS_U32 u32mv0feather, u32nonMv0feather;
1215     MS_U32 u32totalfeather;
1216     static MS_U32 u32totalfeatheriir = 0;
1217 
1218     u32mv0feather = MApi_XC_R2BYTE(REG_SC_BK2A_77_L);
1219     u32nonMv0feather = MApi_XC_R2BYTE(REG_SC_BK2A_7B_L);
1220 
1221     u32totalfeather =  u32mv0feather + u32nonMv0feather;
1222 
1223     u32totalfeather = ((u32totalfeatheriir*3 )+ u32totalfeather ) >> 2;
1224     u32totalfeatheriir = u32totalfeather;
1225 
1226     return u32totalfeatheriir;
1227 }
1228 
1229 #define TOTAL_MV_TIME 10
MDrv_SC_A5_TotalMvComfirm(void * pInstance)1230 MS_U32 MDrv_SC_A5_TotalMvComfirm(void *pInstance)
1231 {
1232     MS_U32 u32mv0, u32nonMv0, u32notFound;
1233     MS_U32 u32totalmv, u32mvComfirm;
1234     static MS_U32 u32mvComfirmiir = 0;
1235 
1236     u32mv0      = MApi_XC_R2BYTE(REG_SC_BK2A_78_L);
1237     u32nonMv0   = MApi_XC_R2BYTE(REG_SC_BK2A_79_L);
1238     u32notFound   = MApi_XC_R2BYTE(REG_SC_BK2A_7A_L);
1239 
1240     u32totalmv = u32mv0 + u32nonMv0;
1241 
1242     if( u32totalmv > u32notFound*TOTAL_MV_TIME )
1243     {
1244         u32mvComfirm =   u32totalmv - u32notFound*TOTAL_MV_TIME;
1245     }
1246     else
1247     {
1248         u32mvComfirm = 0;
1249     }
1250 
1251     if( u32mvComfirm > 255 )
1252     {
1253         u32mvComfirm = 255;
1254     }
1255 
1256     u32mvComfirm = ((u32mvComfirmiir*3 )+ u32mvComfirm ) >> 2;
1257     u32mvComfirmiir = u32mvComfirm;
1258 
1259     return u32mvComfirmiir;
1260 }
1261 
1262 
MDrv_SC_A5_UC_PanScan(void * pInstance)1263 MS_U8 MDrv_SC_A5_UC_PanScan(void *pInstance)
1264 {
1265     MS_U32 u32nonMV0Found, u32MVNotFound;
1266     static MS_U32 u32PrevnonMV0Found = 0;
1267     static MS_U32 u32PrevMVNotFound = 0;
1268     static MS_U8  u8panCnt = 0;
1269 
1270     u32nonMV0Found    = MApi_XC_R2BYTE(REG_SC_BK2A_79_L);
1271     u32MVNotFound     = MApi_XC_R2BYTE(REG_SC_BK2A_7A_L);
1272 
1273     u32nonMV0Found = (u32PrevnonMV0Found*3 + u32nonMV0Found ) >> 2;
1274     u32PrevnonMV0Found = u32nonMV0Found;
1275 
1276     u32MVNotFound = (u32PrevMVNotFound*3 + u32MVNotFound ) >> 2;
1277     u32PrevMVNotFound = u32MVNotFound;
1278 
1279     if( u32nonMV0Found > u32MVNotFound*30 )
1280     {
1281         if(u8panCnt != 10)
1282         {
1283             u8panCnt++;
1284         }
1285     }
1286     else if(u8panCnt != 0)
1287     {
1288         u8panCnt--;
1289     }
1290 
1291     return u8panCnt;
1292 }
1293 
1294 #define LG107_FEATHER_GAIN_MAX 2
1295 #define LG107_FEATHER_GAIN_MIN 0
1296 #define LG107_CPLX_OFFSET_MAX 6
1297 #define LG107_CPLX_OFFSET_MIN 0
1298 #define LG107_CPLX_GAIN_MAX 15
1299 #define LG107_CPLX_GAIN_MIN 4
MDrv_SC_A5_UC_PanScanSetting(void * pInstance,const MS_U8 PanScan)1300 void MDrv_SC_A5_UC_PanScanSetting(void *pInstance, const MS_U8 PanScan )
1301 {
1302     static MS_U16 u16regmcdifeathergain = LG107_FEATHER_GAIN_MAX ;
1303     static MS_U16 u16regmcdicplxoffset = LG107_CPLX_OFFSET_MAX ;
1304 
1305     if( PanScan > 8 )
1306     {
1307         u16regmcdifeathergain = LG107_FEATHER_GAIN_MIN;
1308         u16regmcdicplxoffset = LG107_FEATHER_GAIN_MIN;
1309     }
1310     else
1311     {
1312         u16regmcdifeathergain = LG107_FEATHER_GAIN_MAX;
1313         u16regmcdicplxoffset = LG107_FEATHER_GAIN_MAX;
1314     }
1315 
1316     MApi_XC_W2BYTEMSK(REG_SC_BK2A_2D_L, (MS_U16)u16regmcdifeathergain<<12, 0xF000); // 2 to 0
1317     MApi_XC_W2BYTEMSK(REG_SC_BK2A_2E_L, (MS_U16)u16regmcdicplxoffset<<4, 0x00F0);// 6 to 0
1318 }
1319 /******************************************************************************/
1320 ///MCNR Driver
1321 /******************************************************************************/
1322 /******************************************************************************/
1323 ///UCNR Driver from Monaco driver
1324 /******************************************************************************/
Hal_PQ_LoadMCNRLutQmapSetting(void * pInstance)1325 MS_BOOL Hal_PQ_LoadMCNRLutQmapSetting(void *pInstance)
1326 {
1327     //UCNR LUT
1328 #if MCNR_LUT_LOAD_FROM_QMAP
1329     m_stMCNRLutQmapSetting.mcnrLut[0] = MApi_XC_R2BYTE(REG_SC_BK2A_08_L);
1330     m_stMCNRLutQmapSetting.mcnrLut[1] = MApi_XC_R2BYTE(REG_SC_BK2A_09_L);
1331     m_stMCNRLutQmapSetting.mcnrLut[2] = MApi_XC_R2BYTE(REG_SC_BK2A_0A_L);
1332     m_stMCNRLutQmapSetting.mcnrLut[3] = MApi_XC_R2BYTE(REG_SC_BK2A_0B_L);
1333     m_stMCNRLutQmapSetting.mcnrLut[4] = MApi_XC_R2BYTE(REG_SC_BK2A_0C_L);
1334     m_stMCNRLutQmapSetting.mcnrLut[5] = MApi_XC_R2BYTE(REG_SC_BK2A_0D_L);
1335     m_stMCNRLutQmapSetting.mcnrLut[6] = MApi_XC_R2BYTE(REG_SC_BK2A_0E_L);
1336     m_stMCNRLutQmapSetting.mcnrLut[7] = MApi_XC_R2BYTE(REG_SC_BK2A_0F_L);
1337 #else
1338     m_stMCNRLutQmapSetting.mcnrLut[0] = 0xFFEE;
1339     m_stMCNRLutQmapSetting.mcnrLut[1] = 0xDDCC;
1340     m_stMCNRLutQmapSetting.mcnrLut[2] = 0xBBAA;
1341     m_stMCNRLutQmapSetting.mcnrLut[3] = 0x9988;
1342     m_stMCNRLutQmapSetting.mcnrLut[4] = 0x7766;
1343     m_stMCNRLutQmapSetting.mcnrLut[5] = 0x5544;
1344     m_stMCNRLutQmapSetting.mcnrLut[6] = 0x3322;
1345     m_stMCNRLutQmapSetting.mcnrLut[7] = 0x1100;
1346 #endif
1347 
1348     //PDNR LUT
1349 #if PDNR_LUT_LOAD_FROM_QMAP
1350     m_stMCNRLutQmapSetting.pdnrlowLut[0] = MApi_XC_R2BYTE(REG_SC_BK2A_20_L);
1351     m_stMCNRLutQmapSetting.pdnrlowLut[1] = MApi_XC_R2BYTE(REG_SC_BK2A_21_L);
1352     m_stMCNRLutQmapSetting.pdnrlowLut[2] = MApi_XC_R2BYTE(REG_SC_BK2A_22_L);
1353     m_stMCNRLutQmapSetting.pdnrlowLut[3] = MApi_XC_R2BYTE(REG_SC_BK2A_23_L);
1354     m_stMCNRLutQmapSetting.pdnrhighLut[0] = MApi_XC_R2BYTE(REG_SC_BK2A_24_L);
1355     m_stMCNRLutQmapSetting.pdnrhighLut[1] = MApi_XC_R2BYTE(REG_SC_BK2A_25_L);
1356     m_stMCNRLutQmapSetting.pdnrhighLut[2] = MApi_XC_R2BYTE(REG_SC_BK2A_26_L);
1357     m_stMCNRLutQmapSetting.pdnrhighLut[3] = MApi_XC_R2BYTE(REG_SC_BK2A_27_L);
1358 #else
1359     m_stMCNRLutQmapSetting.pdnrlowLut[0] = 0x8888;
1360     m_stMCNRLutQmapSetting.pdnrlowLut[1] = 0x8888;
1361     m_stMCNRLutQmapSetting.pdnrlowLut[2] = 0x7654;
1362     m_stMCNRLutQmapSetting.pdnrlowLut[3] = 0x3210;
1363 
1364     if(_bIsHDSource)
1365     {
1366         m_stMCNRLutQmapSetting.pdnrhighLut[0] = 0x8888;
1367         m_stMCNRLutQmapSetting.pdnrhighLut[1] = 0x8888;
1368         m_stMCNRLutQmapSetting.pdnrhighLut[2] = 0x8887;
1369         m_stMCNRLutQmapSetting.pdnrhighLut[3] = 0x6420;
1370     }
1371     else
1372     {
1373         m_stMCNRLutQmapSetting.pdnrhighLut[0] = 0x8876;
1374         m_stMCNRLutQmapSetting.pdnrhighLut[1] = 0x5432;
1375         m_stMCNRLutQmapSetting.pdnrhighLut[2] = 0x1000;
1376         m_stMCNRLutQmapSetting.pdnrhighLut[3] = 0x0000;
1377     }
1378 #endif
1379 
1380     return true;
1381 }
MDrv_SC_NR_LUT_Aux(void * pInstance,MS_U16 nr_lut,MS_U8 sc)1382 MS_U16 MDrv_SC_NR_LUT_Aux(void *pInstance, MS_U16 nr_lut, MS_U8 sc)
1383 {
1384 	sc = sc < 16 ? sc : 16;
1385 
1386     MS_U8 h0 = (nr_lut >>  0) & 0xF;
1387     MS_U8 h1 = (nr_lut >>  4) & 0xF;
1388     MS_U8 h2 = (nr_lut >>  8) & 0xF;
1389     MS_U8 h3 = (nr_lut >> 12) & 0xF;
1390 
1391     XC_ApiStatus DrvStatus;
1392 	MApi_XC_GetStatus(&DrvStatus, MAIN_WINDOW);
1393 	if ((!DrvStatus.bInterlace) && (DrvStatus.u16InputVFreq < 300)) //reduce gain value if 24p
1394 	{
1395 		h0 = ((h0 * sc)/3) >> 4;
1396 		h1 = ((h1 * sc)/3) >> 4;
1397 		h2 = ((h2 * sc)/3) >> 4;
1398 		h3 = ((h3 * sc)/3) >> 4;
1399 	}
1400 	else
1401 	{
1402 		h0 = (h0 * sc) >> 4;
1403 	    h1 = (h1 * sc) >> 4;
1404 	    h2 = (h2 * sc) >> 4;
1405 	    h3 = (h3 * sc) >> 4;
1406 	}
1407 
1408     return (h3 << 12) | (h2 << 8) | (h1 << 4) | (h0 << 0);
1409 }
MDrv_SC_Set_MCNR_Gain(void * pInstance,MS_U8 gain)1410 void MDrv_SC_Set_MCNR_Gain(void *pInstance, MS_U8 gain)
1411 {
1412 	MApi_XC_W2BYTE(REG_SC_BK2A_08_L, MDrv_SC_NR_LUT_Aux(pInstance, m_stMCNRLutQmapSetting.mcnrLut[0], gain ) );
1413 	MApi_XC_W2BYTE(REG_SC_BK2A_09_L, MDrv_SC_NR_LUT_Aux(pInstance, m_stMCNRLutQmapSetting.mcnrLut[1], gain ) );
1414 	MApi_XC_W2BYTE(REG_SC_BK2A_0A_L, MDrv_SC_NR_LUT_Aux(pInstance, m_stMCNRLutQmapSetting.mcnrLut[2], gain ) );
1415 	MApi_XC_W2BYTE(REG_SC_BK2A_0B_L, MDrv_SC_NR_LUT_Aux(pInstance, m_stMCNRLutQmapSetting.mcnrLut[3], gain ) );
1416 	MApi_XC_W2BYTE(REG_SC_BK2A_0C_L, MDrv_SC_NR_LUT_Aux(pInstance, m_stMCNRLutQmapSetting.mcnrLut[4], gain ) );
1417 	MApi_XC_W2BYTE(REG_SC_BK2A_0D_L, MDrv_SC_NR_LUT_Aux(pInstance, m_stMCNRLutQmapSetting.mcnrLut[5], gain ) );
1418 	MApi_XC_W2BYTE(REG_SC_BK2A_0E_L, MDrv_SC_NR_LUT_Aux(pInstance, m_stMCNRLutQmapSetting.mcnrLut[6], gain ) );
1419 	MApi_XC_W2BYTE(REG_SC_BK2A_0F_L, MDrv_SC_NR_LUT_Aux(pInstance, m_stMCNRLutQmapSetting.mcnrLut[7], gain ) );
1420 }
1421 
MDrv_SC_mcnr_driver(void * pInstance,MS_U32 nonHistoryMotionPixel,MS_BOOL bisHDSource)1422 void MDrv_SC_mcnr_driver(void *pInstance, MS_U32 nonHistoryMotionPixel, MS_BOOL bisHDSource)
1423 {
1424     MS_U32 u32_is_mv0, u32_non_mv0, u32_mvNotFound, u32_mvFeathering, u32_mvc, u32_mvu, u32_mvd, u32_cplxPixel, u32temp1, u32temp2;
1425     MS_BOOL bmenotconfirm , bmevertical, bis24to60Hz/*, scene_change*/;
1426     MS_U8 u8MCNR_Gain=16;
1427     static MS_U8 u8vMOvingReduceNR_HouseHDPatch_counter;
1428     static MS_U8 u8notConfirmCnt;
1429     //static MS_U32 u32lastmvNotFound;
1430     static MS_U8 u8nrWeightPatchForVSweep = 0;
1431 
1432     XC_ApiStatus stXCStatus;
1433 
1434     if(MApi_XC_GetStatus(&stXCStatus, MAIN_WINDOW) == FALSE)
1435     {
1436         //printf("MApi_XC_GetStatus failed because of InitData wrong, please update header file and compile again\n");
1437     }
1438 
1439     // Update MCNR/PDNR LUT from Qmap from Monaco driver
1440     MS_BOOL bMCNR_LUT_Reload = (MApi_XC_R2BYTEMSK(REG_SC_BK22_7E_L, 0x0400)>>8);
1441     if(bMCNR_LUT_Reload)
1442     {
1443         if(Hal_PQ_LoadMCNRLutQmapSetting(pInstance))
1444             MApi_XC_W2BYTEMSK(REG_SC_BK22_7E_L, 0x0000, 0x0400);
1445     }
1446 
1447 
1448     bis24to60Hz = (!stXCStatus.bInterlace) && (stXCStatus.u16InputVFreq < 300);
1449 
1450     u32_is_mv0  = MApi_XC_R2BYTE(REG_SC_BK2A_78_L);
1451     u32_non_mv0 = MApi_XC_R2BYTE(REG_SC_BK2A_79_L);
1452     u32_mvNotFound = MApi_XC_R2BYTE(REG_SC_BK2A_7A_L);
1453     u32_mvFeathering = MApi_XC_R2BYTE(REG_SC_BK2A_7B_L);
1454     u32_mvc     = MApi_XC_R2BYTE(REG_SC_BK2A_7C_L);
1455     u32_mvu     = MApi_XC_R2BYTE(REG_SC_BK2A_7D_L);
1456     u32_mvd     = MApi_XC_R2BYTE(REG_SC_BK2A_7E_L);
1457     u32_cplxPixel = MApi_XC_R2BYTE(REG_SC_BK2A_7F_L);
1458 
1459 
1460     if ( (u32_cplxPixel > 0x1000) && (u32_is_mv0 > 0x1000) && (nonHistoryMotionPixel == 0) ) // patch for VSweep converge too slow
1461     {
1462         if ( u8nrWeightPatchForVSweep > 4 )
1463         {
1464             u8nrWeightPatchForVSweep = u8nrWeightPatchForVSweep - 4;
1465         }
1466         else
1467         {
1468             u8nrWeightPatchForVSweep = 0;
1469         }
1470     }
1471     else
1472     {
1473         if ( u8nrWeightPatchForVSweep < 0xD )
1474         {
1475             u8nrWeightPatchForVSweep += 1;
1476         }
1477     }
1478 
1479     /////////////////////////////////////////////////////////////////
1480     //  NR Patch 1
1481     /////////////////////////////////////////////////////////////////
1482     bmenotconfirm = (u32_is_mv0 < 0x0200) && (u32_non_mv0 < 0x0200);
1483     bmevertical    = ((u32_mvu > u32_mvc) && (u32_mvu > u32_mvd) && (u32_mvu > 0x200)) ||
1484                      ((u32_mvd > u32_mvc) && (u32_mvd > u32_mvu) && (u32_mvd > 0x200));
1485 
1486     if(false == bmenotconfirm)
1487     {
1488         if(u8notConfirmCnt > 0)
1489         {
1490             u8notConfirmCnt--;
1491         }
1492     }
1493     else
1494     {
1495         if(u8notConfirmCnt <= 0x40)
1496         {
1497             u8notConfirmCnt++;
1498         }
1499     }
1500 
1501     if(1)
1502     {
1503         MS_BOOL bBool = FALSE;
1504 
1505         if (MApi_XC_R2BYTEMSK(REG_SC_BK20_10_L , 0x03) & 0x03)
1506         {
1507             bBool = (u8notConfirmCnt > 0x30);//main&sub
1508         }
1509         else if (MApi_XC_R2BYTEMSK(REG_SC_BK20_10_L , BIT(1)) & BIT(1))
1510         {
1511             bBool = (u8notConfirmCnt > 0x30);//sub only
1512         }
1513         else if (MApi_XC_R2BYTEMSK(REG_SC_BK20_10_L , BIT(0)) & BIT(0))
1514         {
1515             bBool = ( (u8notConfirmCnt > 0x30) || bmevertical ); // low nr strength
1516         }
1517 
1518         if (bBool) // low nr strength
1519         {
1520             if ( !bisHDSource )
1521             {
1522                 MApi_XC_W2BYTEMSK(REG_SC_BK2A_1B_L, (MS_U16)0x0064, 0x00FF); // moving picture, increase random motion
1523             }
1524             else
1525             {
1526                 MApi_XC_W2BYTEMSK(REG_SC_BK2A_1B_L, (MS_U16)0x00C2, 0x00FF); // Default 64 -> C2, moving picture, increase random motion
1527             }
1528 
1529             if ( u8nrWeightPatchForVSweep >= 7 )
1530             {
1531                 u8MCNR_Gain=9;
1532             }
1533             else //if ( nrWeightPatchForVSweep >= 4 )
1534             {
1535                 u8MCNR_Gain=7;
1536             }
1537         }
1538         else if(u8notConfirmCnt < 0x10)
1539         {
1540             if ( (u8nrWeightPatchForVSweep >= 0xD) && (!bis24to60Hz) )
1541             {
1542                 u8MCNR_Gain=16;
1543             }
1544             else if ( (u8nrWeightPatchForVSweep >= 0xA) && (!bis24to60Hz) )
1545             {
1546                 u8MCNR_Gain=11;
1547             }
1548             else if ( u8nrWeightPatchForVSweep >= 7 )
1549             {
1550                 u8MCNR_Gain=9;
1551             }
1552             else //if ( nrWeightPatchForVSweep >= 4 )
1553             {
1554                 u8MCNR_Gain=7;
1555             }
1556         }
1557         else
1558         {
1559             u8MCNR_Gain=16;
1560         }
1561 
1562 
1563         // MCNR gain
1564 	 MDrv_SC_Set_MCNR_Gain(pInstance, u8MCNR_Gain);
1565 /*        static MS_U16 au16NR_Lut[8];
1566         au16NR_Lut[0]=0xFFEE;
1567         au16NR_Lut[1]=0xDDCC;
1568         au16NR_Lut[2]=0xBBAA;
1569         au16NR_Lut[3]=0x9988;
1570         au16NR_Lut[4]=0x7766;
1571         au16NR_Lut[5]=0x5544;
1572         au16NR_Lut[6]=0x3322;
1573         au16NR_Lut[7]=0x1100;
1574 
1575         MApi_XC_W2BYTEMSK(REG_SC_BK2A_08_L, MDrv_SC_NR_LUT_Aux(au16NR_Lut[0],u8MCNR_Gain), 0xFFFF);
1576         MApi_XC_W2BYTEMSK(REG_SC_BK2A_09_L, MDrv_SC_NR_LUT_Aux(au16NR_Lut[1],u8MCNR_Gain), 0xFFFF);
1577         MApi_XC_W2BYTEMSK(REG_SC_BK2A_0A_L, MDrv_SC_NR_LUT_Aux(au16NR_Lut[2],u8MCNR_Gain), 0xFFFF);
1578         MApi_XC_W2BYTEMSK(REG_SC_BK2A_0B_L, MDrv_SC_NR_LUT_Aux(au16NR_Lut[3],u8MCNR_Gain), 0xFFFF);
1579         MApi_XC_W2BYTEMSK(REG_SC_BK2A_0C_L, MDrv_SC_NR_LUT_Aux(au16NR_Lut[4],u8MCNR_Gain), 0xFFFF);
1580         MApi_XC_W2BYTEMSK(REG_SC_BK2A_0D_L, MDrv_SC_NR_LUT_Aux(au16NR_Lut[5],u8MCNR_Gain), 0xFFFF);
1581         MApi_XC_W2BYTEMSK(REG_SC_BK2A_0E_L, MDrv_SC_NR_LUT_Aux(au16NR_Lut[6],u8MCNR_Gain), 0xFFFF);
1582         MApi_XC_W2BYTEMSK(REG_SC_BK2A_0F_L, MDrv_SC_NR_LUT_Aux(au16NR_Lut[7],u8MCNR_Gain), 0xFFFF);
1583 */
1584 
1585         if ( bisHDSource && (u32_mvc < 0x10) && (( u32_mvu + u32_mvd ) > 0x40) )
1586         {
1587             u8vMOvingReduceNR_HouseHDPatch_counter = 16;
1588         }
1589         else if ( (u32_is_mv0 <= 0x10) && (u32_non_mv0 > 0x2000) && (u32_mvNotFound > 0x80) && (u32_mvFeathering > 0x8) && (u32_cplxPixel < 0x200) ) // patch shibasoku a little out-of-search range
1590         {
1591             u8vMOvingReduceNR_HouseHDPatch_counter = 16;
1592         }
1593         else if ( u8vMOvingReduceNR_HouseHDPatch_counter > 0 )
1594         {
1595             u8vMOvingReduceNR_HouseHDPatch_counter --;
1596         }
1597 
1598         if ( bis24to60Hz )
1599         {
1600             MApi_XC_W2BYTEMSK(REG_SC_BK2A_1B_L, (MS_U16)0x00F0, 0x00FF);
1601         }
1602         else if ( (u32_is_mv0 > 0x200) && ((u32_is_mv0/2) > u32_non_mv0) && ((u32_is_mv0/2) > u32_mvNotFound) && (u8vMOvingReduceNR_HouseHDPatch_counter == 0) )
1603         {
1604             if ( stXCStatus.bInterlace )
1605             {
1606                 MApi_XC_W2BYTEMSK(REG_SC_BK2A_1B_L, (MS_U16)0x006c, 0x00FF); // still picture, reduce random motion
1607             }
1608             else
1609             {
1610                 MApi_XC_W2BYTEMSK(REG_SC_BK2A_1B_L, (MS_U16)0x0064, 0x00FF); // still picture, reduce random motion
1611             }
1612         }
1613         else if ( (u32_non_mv0 > u32_is_mv0) || (u8vMOvingReduceNR_HouseHDPatch_counter > 0 )) // moving picture, increase random motion
1614         {
1615             if ( !bisHDSource )
1616         {
1617                 MApi_XC_W2BYTEMSK(REG_SC_BK2A_1B_L, (MS_U16)0x0060, 0x00FF);
1618             }
1619             else
1620             {
1621                 if ( stXCStatus.bInterlace )
1622                 {
1623                     u32temp1 = ( 0xE * u8vMOvingReduceNR_HouseHDPatch_counter + 0x6 * ( 16 - u8vMOvingReduceNR_HouseHDPatch_counter ) ) >> 4;
1624                     u32temp2 = ( 0x0 * u8vMOvingReduceNR_HouseHDPatch_counter + 0xC * ( 16 - u8vMOvingReduceNR_HouseHDPatch_counter ) ) >> 4;
1625                 }
1626                 else
1627                 {
1628                     u32temp1 = ( 0xE * u8vMOvingReduceNR_HouseHDPatch_counter + 0x6 * ( 16 - u8vMOvingReduceNR_HouseHDPatch_counter ) ) >> 4;
1629                     u32temp2 = ( 0x0 * u8vMOvingReduceNR_HouseHDPatch_counter + 0x4 * ( 16 - u8vMOvingReduceNR_HouseHDPatch_counter ) ) >> 4;
1630                 }
1631                 MApi_XC_W2BYTEMSK(REG_SC_BK2A_1B_L, (MS_U16)((u32temp1<<4)|u32temp2), 0x00FF); // 60 -> E0. moving picture, increase random motion
1632             }
1633         }
1634         else // default
1635         {
1636             if ( !bisHDSource )
1637             {
1638                 MApi_XC_W2BYTEMSK(REG_SC_BK2A_1B_L, (MS_U16)0x0064, 0x00FF); // default random motion
1639             }
1640             else
1641             {
1642                 if ( stXCStatus.bInterlace )
1643                 {
1644                     MApi_XC_W2BYTEMSK(REG_SC_BK2A_1B_L, (MS_U16)0x00C2, 0x00FF); // Default 64 -> C2, moving picture, increase random motion
1645                 }
1646                 else
1647                 {
1648                     MApi_XC_W2BYTEMSK(REG_SC_BK2A_1B_L, (MS_U16)0x00C2, 0x00FF); // Default 64 -> C2, moving picture, increase random motion
1649                 }
1650         }
1651     }
1652     }
1653     //u32lastmvNotFound = u32_mvNotFound;
1654 }
1655 
1656 /******************************************************************************/
1657 ///DeFeathering
1658 ///@param u32MotionValue \     IN: Motion value
1659 /******************************************************************************/
MDrv_SC_de_feathering(void * pInstance,MS_U32 u32MotionValue)1660 void MDrv_SC_de_feathering(void *pInstance, MS_U32 u32MotionValue)
1661 {
1662     static MS_U32 u32DeFeatherCntLv1 = 0;
1663     static MS_U32 u32DeFeatherCntLv2 = 0;
1664     static MS_U32 u32DeFeatherCntLv3 = 0;
1665     MS_U8 u8SST_Static_Core_TH;
1666     MS_U32 u32regdefetheringlv1cnt;
1667     MS_U32 u32regdefetheringlv2cnt;
1668     MS_U32 u32regmdpcnt;
1669 
1670     u32regdefetheringlv1cnt = 0x0A;
1671     u32regdefetheringlv2cnt = 0x08;
1672     u32regmdpcnt = 0x01;
1673 
1674     // motion level count
1675     if(u32MotionValue >= DEFETHERING_LV1_TH)
1676     {
1677         if(u32DeFeatherCntLv1 < u32regdefetheringlv1cnt)
1678         {
1679             u32DeFeatherCntLv1++;
1680         }
1681     }
1682     else
1683     {
1684         if(u32DeFeatherCntLv1 >= u32regmdpcnt)
1685         {
1686             u32DeFeatherCntLv1 = u32DeFeatherCntLv1 - u32regmdpcnt;
1687         }
1688     }
1689 
1690     if(u32MotionValue >= DEFETHERING_LV2_TH)
1691     {
1692         if(u32DeFeatherCntLv2 < u32regdefetheringlv2cnt)
1693         {
1694             u32DeFeatherCntLv2++;
1695         }
1696     }
1697     else
1698     {
1699         if(u32DeFeatherCntLv2 >= u32regmdpcnt)
1700         {
1701             u32DeFeatherCntLv2 = u32DeFeatherCntLv2 - u32regmdpcnt;
1702         }
1703     }
1704 
1705     if(u32MotionValue >= DEFETHERING_LV3_TH)
1706     {
1707         if(u32DeFeatherCntLv3 < DEFETHERING_LV3_CNT)
1708         {
1709             u32DeFeatherCntLv3++;
1710         }
1711     }
1712     else
1713     {
1714         if(u32DeFeatherCntLv3 >= u32regmdpcnt)
1715         {
1716             u32DeFeatherCntLv3 = u32DeFeatherCntLv3 - u32regmdpcnt;
1717         }
1718     }
1719 
1720     //DeFeathering begin
1721     if(u32DeFeatherCntLv1 >= u32regdefetheringlv1cnt)
1722     {
1723         u8SST_Static_Core_TH    = SST_STATIC_CORE_TH_LV1_VALUE;
1724     }
1725     else if(u32DeFeatherCntLv2 >= u32regdefetheringlv2cnt)
1726     {
1727         u8SST_Static_Core_TH    = SST_STATIC_CORE_TH_LV2_VALUE;
1728     }
1729     else if(u32DeFeatherCntLv3 >= DEFETHERING_LV3_CNT)
1730     {
1731         u8SST_Static_Core_TH    = SST_STATIC_CORE_TH_LV3_VALUE;
1732     }
1733     else
1734     {
1735         u8SST_Static_Core_TH    = SST_STATIC_CORE_TH_LV4_VALUE;
1736     }
1737 
1738     MApi_XC_W2BYTEMSK(REG_SC_BK22_1A_L, (MS_U16)u8SST_Static_Core_TH, 0xFF);
1739 }
1740 
1741 
MDrv_SC_motion_level(void * pInstance,MS_U32 u32MotionValue)1742 int MDrv_SC_motion_level(void *pInstance, MS_U32 u32MotionValue)
1743 {
1744     static MS_U32 u32DeFeatherCntLv1 = 0;
1745     static MS_U32 u32DeFeatherCntLv2 = 0;
1746     static MS_U32 u32DeFeatherCntLv3 = 0;
1747     MS_U8 u8MotLvl;
1748     MS_U32 u32regdefetheringlv1cnt;
1749     MS_U32 u32regdefetheringlv2cnt;
1750     MS_U32 u32regmdpcnt;
1751 
1752     u32regdefetheringlv1cnt = 0x0A;
1753     u32regdefetheringlv2cnt = 0x08;
1754     u32regmdpcnt = 0x01;
1755 
1756     // motion level count
1757     if(u32MotionValue >= DEFETHERING_LV1_TH)
1758     {
1759         if(u32DeFeatherCntLv1 < u32regdefetheringlv1cnt)
1760         {
1761             u32DeFeatherCntLv1++;
1762         }
1763     }
1764     else
1765     {
1766         if(u32DeFeatherCntLv1 >= u32regmdpcnt)
1767         {
1768             u32DeFeatherCntLv1 = u32DeFeatherCntLv1 - u32regmdpcnt;
1769         }
1770     }
1771 
1772     if(u32MotionValue >= DEFETHERING_LV2_TH)
1773     {
1774         if(u32DeFeatherCntLv2 < u32regdefetheringlv2cnt)
1775         {
1776             u32DeFeatherCntLv2++;
1777         }
1778     }
1779     else
1780     {
1781         if(u32DeFeatherCntLv2 >= u32regmdpcnt)
1782         {
1783             u32DeFeatherCntLv2 = u32DeFeatherCntLv2 - u32regmdpcnt;
1784         }
1785     }
1786 
1787     if(u32MotionValue >= DEFETHERING_LV3_TH)
1788     {
1789         if(u32DeFeatherCntLv3 < DEFETHERING_LV3_CNT)
1790         {
1791             u32DeFeatherCntLv3++;
1792         }
1793     }
1794     else
1795     {
1796         if(u32DeFeatherCntLv3 >= u32regmdpcnt)
1797         {
1798             u32DeFeatherCntLv3 = u32DeFeatherCntLv3 - u32regmdpcnt;
1799         }
1800     }
1801 
1802     //level begin
1803     if(u32DeFeatherCntLv1 >= u32regdefetheringlv1cnt)
1804     {
1805         u8MotLvl    = 3;
1806     }
1807     else if(u32DeFeatherCntLv2 >= u32regdefetheringlv2cnt)
1808     {
1809         u8MotLvl    = 2;
1810     }
1811     else if(u32DeFeatherCntLv3 >= DEFETHERING_LV3_CNT)
1812     {
1813         u8MotLvl    = 1;
1814     }
1815     else
1816     {
1817         u8MotLvl    = 0;
1818     }
1819 
1820     return u8MotLvl;
1821 }
1822 
1823 
1824 /******************************************************************************/
1825 ///DeFlickering
1826 ///@param u32MotionValue \     IN: Motion value
1827 /******************************************************************************/
MDrv_SC_de_flickering(void * pInstance,MS_U32 u32MotionValue)1828 void MDrv_SC_de_flickering(void *pInstance, MS_U32 u32MotionValue)
1829 {
1830     static MS_S32 s32DeFlickerCnt = 0;
1831     MS_U32 u32regmfeatsmoothhleth, u32regmfeatsmoothshrink;
1832 
1833     u32regmfeatsmoothhleth = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK22_1E_L, 0xF000); // Feat Smooth HLE TH
1834     u32regmfeatsmoothshrink = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK22_1E_L, 0x8F);
1835 
1836     if(u32MotionValue >= DEFLICKERING_TH)
1837     {
1838         if(s32DeFlickerCnt < 65535)
1839         {
1840             s32DeFlickerCnt++;
1841         }
1842     }
1843     else
1844     {
1845         s32DeFlickerCnt = 0;
1846     }
1847 
1848     if(s32DeFlickerCnt >= DEFLICKERING_CNT)
1849     {
1850         u32regmfeatsmoothhleth += 0x0300;
1851         u32regmfeatsmoothshrink += 0x10;
1852     }
1853     else
1854     {
1855         u32regmfeatsmoothhleth += 0x0700;
1856         u32regmfeatsmoothshrink += 0x30;
1857     }
1858 
1859     MApi_XC_W2BYTEMSK(REG_SC_BK22_1E_L, u32regmfeatsmoothshrink, 0xFF);
1860     MApi_XC_W2BYTEMSK(REG_SC_BK22_1E_L, u32regmfeatsmoothhleth, 0xFF00);
1861 }
1862 
1863 /******************************************************************************/
1864 ///DeBouncing
1865 ///@param u32MotionValue \     IN: Motion value
1866 /******************************************************************************/
1867 #define DEBOUNCING_GAIN 1 //0
MDrv_SC_de_bouncing(void * pInstance,MS_U32 u32MotionValue)1868 void MDrv_SC_de_bouncing(void *pInstance, MS_U32 u32MotionValue)
1869 {
1870     static MS_S32 s32DeBouncingCnt = 0;
1871     MS_U32 u32reghiswtf2;
1872     MS_U32 u32regdebouncingth;
1873     MS_U32 u32regdebouncingcnt;
1874 
1875     u32regdebouncingth = 0x0A;
1876     u32regdebouncingcnt = 0x03;
1877 
1878     u32reghiswtf2 = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK22_0A_L, 0xF8); // history ratio weighting
1879 
1880     MS_BOOL bfilm;
1881     bfilm = ( (MS_U16)MApi_XC_R2BYTEMSK(REG_SC_BK0A_21_L, 0x0008) == 0x0008) ||  // film22
1882            ( (MS_U16)MApi_XC_R2BYTEMSK(REG_SC_BK0A_21_L, 0x0010) == 0x0010) ||  // film32
1883            ( (MS_U16)MApi_XC_R2BYTEMSK(REG_SC_BK0A_21_L, 0x0040) == 0x0040) ;   // film any
1884 
1885     if(u32MotionValue <= u32regdebouncingth * DEBOUNCING_GAIN)
1886     {
1887         s32DeBouncingCnt = 0;
1888     }
1889     else
1890     {
1891         if(s32DeBouncingCnt < 65535)
1892         {
1893             s32DeBouncingCnt++;
1894         }
1895     }
1896 
1897     if(bfilm || (s32DeBouncingCnt >= (MS_S32)u32regdebouncingcnt))
1898     {
1899         u32reghiswtf2 += 0x06; // history = 6 moving
1900     }
1901     else
1902     {
1903         u32reghiswtf2 += 0x03; // history = 3 still
1904     }
1905     MApi_XC_W2BYTEMSK(REG_SC_BK22_0A_L, u32reghiswtf2, 0xFF);
1906 }
1907 
1908 #define HD_ACT_TH 10
1909 #define HDSDD_SPEED 1
1910 #define HDSDD_DIV 0
1911 #define HD_SPEED 2
1912 #define SD_SPEED 8
MDrv_SC_NEW_SDHD_DETECT_driver(void * pInstance)1913 void MDrv_SC_NEW_SDHD_DETECT_driver(void *pInstance)
1914 {
1915 #if 0
1916     MS_U8 u8Value,u8FEATHERCNTTH;
1917     MS_U16 u16MinPixel,u16MaxPixel, u16MaxMinPixel;
1918     MS_U32 u32HDcntAct;
1919     //MS_U32 HDcnt_NonAct;
1920     MS_U32 u32FeatherCnt;
1921     MS_U32 u32SDcntAct;
1922     //MS_U32 SDcnt_NonAct;
1923     static MS_U32 u32PreFeatherCnt = 0;
1924     static MS_U16 u16HDConfirmCnt = 0x0000; // 0x7FFF == 0
1925     static MS_U8 u8PrevLuma = 0;
1926     static MS_U16 u16PrevMinPixel = 0;
1927     static MS_U16 u16PrevMaxPixel = 0;
1928     MS_U32 u32HDConfirmCntReg;
1929     MS_U16 u16HDSDDthreshold,u16HDSDDoffset;
1930 
1931     u16HDSDDoffset = 0x30;  //(MS_U16)MApi_XC_R2BYTEMSK(REG_SC_BK22_7F_L, 0xFFFF);
1932 
1933     u32FeatherCnt = MApi_XC_R2BYTE(REG_SC_BK23_56_L);       //New add for A1 chip
1934     u8FEATHERCNTTH = 0xC8;  //= MApi_XC_R2BYTEMSK(REG_SC_BK22_7D_L, 0xFF00)>>8;
1935 
1936     u32FeatherCnt = ( u32PreFeatherCnt*3 + u32FeatherCnt ) >> 2;
1937     u32PreFeatherCnt = u32FeatherCnt;
1938 
1939     u8Value=(MS_U32)((MS_U32)u8PrevLuma*3 + MApi_XC_DLC_GetAverageValue())>>2;
1940     u8PrevLuma = u8Value;
1941 
1942     u16MaxPixel = MApi_XC_R2BYTEMSK(REG_SC_BK1A_0B_L, 0x00FF);
1943     u16MaxPixel = (u16PrevMaxPixel*3 + u16MaxPixel ) >> 2;
1944     u16PrevMaxPixel = u16MaxPixel;
1945 
1946     u16MinPixel = MApi_XC_R2BYTEMSK(REG_SC_BK1A_0B_L, 0xFF00)>>8;
1947     u16MinPixel = (u16PrevMinPixel*3 + u16MinPixel ) >> 2;
1948     u16PrevMinPixel = u16MinPixel;
1949 
1950     u16MaxMinPixel = u16MaxPixel - u16MinPixel;
1951 
1952     if(u8Value>=0xFF)
1953     {
1954         u8Value = 0xFF;
1955     }
1956 
1957     u16HDSDDthreshold = u16HDSDDoffset +((u16MaxMinPixel*6)>>2) + ((u8Value*5)>>2);
1958 
1959     if(u16HDSDDthreshold >=0xFFFF)
1960     {
1961         u16HDSDDthreshold=0xFFFF;
1962     }
1963     MApi_XC_W2BYTEMSK(REG_SC_BK02_71_L, (MS_U16)u16HDSDDthreshold, 0xFFFF);
1964     MApi_XC_W2BYTEMSK(REG_SC_BK02_72_L, (MS_U16)(u16HDSDDthreshold>>1), 0xFFFF);
1965 
1966     u32HDcntAct = (MApi_XC_R2BYTE(REG_SC_BK02_75_L) << 16) + MApi_XC_R2BYTE(REG_SC_BK02_74_L);
1967     u32SDcntAct = (MApi_XC_R2BYTE(REG_SC_BK02_77_L) << 16) + MApi_XC_R2BYTE(REG_SC_BK02_76_L);
1968 
1969     if( (u32HDcntAct > HD_ACT_TH) && (u32SDcntAct != 0) )
1970     {
1971         if(u16HDConfirmCnt < HD_SPEED)
1972         {
1973             u16HDConfirmCnt = 0;
1974         }
1975         else
1976         {
1977             u16HDConfirmCnt = u16HDConfirmCnt - HD_SPEED;
1978         }
1979     }
1980     else if( (u32SDcntAct != 0) && (u32FeatherCnt > u8FEATHERCNTTH) )//&& HDcnt_NonAct !=0) // && (HDcnt_NonAct < 0x90000))
1981     {
1982         if(u16HDConfirmCnt > (0xFF - SD_SPEED))
1983         {
1984             u16HDConfirmCnt = 0xFF;
1985         }
1986         else
1987         {
1988             u16HDConfirmCnt = u16HDConfirmCnt + SD_SPEED;
1989         }
1990     }
1991     else
1992     {
1993         if(u16HDConfirmCnt < 1)
1994         {
1995             u16HDConfirmCnt = 0;
1996         }
1997         else
1998         {
1999             u16HDConfirmCnt = u16HDConfirmCnt - 1;
2000         }
2001     }
2002 
2003     if( u16HDConfirmCnt > 0x80 )
2004     {
2005         u32HDConfirmCntReg = u16HDConfirmCnt - 0x80;
2006     }
2007     else
2008     {
2009         u32HDConfirmCntReg = 0;
2010     }
2011 
2012     u32HDConfirmCntReg = (u32HDConfirmCntReg*HDSDD_SPEED) >> HDSDD_DIV;
2013 
2014     if( u32HDConfirmCntReg > 0x3F )
2015     {
2016         u32HDConfirmCntReg = 0x3F;
2017     }
2018 
2019     MApi_XC_W2BYTEMSK(REG_SC_BK23_54_L, (MS_U16)u32HDConfirmCntReg, 0x003F);
2020 #endif
2021 }
2022 
MDrv_SC_A5_verticalMovingReduceKFC(void * pInstance,MS_BOOL bVerticalMovingForDHD)2023 void MDrv_SC_A5_verticalMovingReduceKFC(void *pInstance, MS_BOOL bVerticalMovingForDHD )
2024 {
2025     // reduce KFC reduce history motion when vertical moving
2026     if ( bVerticalMovingForDHD || ((MS_U16)MApi_XC_R2BYTEMSK(REG_SC_BK22_7C_L, 0x2000) == 0x2000) )
2027     {
2028         MApi_XC_W2BYTEMSK(REG_SC_BK2A_28_L, 0x0000, 0x0800);
2029     }
2030     else
2031     {
2032         MApi_XC_W2BYTEMSK(REG_SC_BK2A_28_L, 0x0800, 0x0800);
2033     }
2034 }
2035 
MDrv_SC_Amber5_DHD_driver1(void * pInstance,MS_U8 u8SD2HDValue)2036 void MDrv_SC_Amber5_DHD_driver1(void *pInstance, MS_U8 u8SD2HDValue )
2037 {
2038     // dhd news report patch: enhance dhd strength
2039     static MS_U32 u32sd2hdCnt = 0;
2040     MS_U32 u32checkwidth = MApi_XC_R2BYTEMSK(REG_SC_BK12_0F_L, 0xFFFF);
2041     MS_U32 u32isPAL = (MApi_XC_R2BYTE(REG_SC_BK05_12_L)&0x4000)>>14;
2042 
2043     MS_U8 u8MV0Comfirm = MApi_XC_R2BYTE(REG_SC_BK2A_78_L)>>8;
2044 
2045     if((u8SD2HDValue>=60)&&(u32checkwidth>0x700)&&(u8MV0Comfirm>0x20)&&(u32isPAL==0)) //When in HD case, detect SD
2046     {
2047         u32sd2hdCnt++;
2048 
2049         if( u32sd2hdCnt >= 100 )
2050         {
2051             u32sd2hdCnt = 100;
2052         }
2053     }
2054     else
2055     {
2056         if( u32sd2hdCnt == 0 )
2057         {
2058             u32sd2hdCnt = 0;
2059         }
2060         else
2061         {
2062             u32sd2hdCnt--;
2063         }
2064     }
2065 
2066     // divide into 11 level
2067     if( u32sd2hdCnt <= 2 )
2068     {
2069         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x0030, 0x00FF); //c edge gain
2070         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x1000, 0xFF00); //y edge gain
2071         MApi_XC_W2BYTEMSK(REG_SC_BK05_23_L, 0x0010, 0x00FF); //c value gain
2072     }
2073     else if( u32sd2hdCnt == 3 )
2074     {
2075         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x0030, 0x00FF); //c edge gain
2076         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x2000, 0xFF00); //y edge gain
2077         MApi_XC_W2BYTEMSK(REG_SC_BK05_23_L, 0x0020, 0x00FF); //c value gain
2078     }
2079     else if( u32sd2hdCnt == 4 )
2080     {
2081         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x0030, 0x00FF); //c edge gain
2082         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x3000, 0xFF00); //y edge gain
2083         MApi_XC_W2BYTEMSK(REG_SC_BK05_23_L, 0x0020, 0x00FF); //c value gain
2084     }
2085     else if( u32sd2hdCnt == 5 )
2086     {
2087         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x0040, 0x00FF); //c edge gain
2088         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x4000, 0xFF00); //y edge gain
2089         MApi_XC_W2BYTEMSK(REG_SC_BK05_23_L, 0x0030, 0x00FF); //c value gain
2090     }
2091     else if( u32sd2hdCnt == 6 )
2092     {
2093         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x0050, 0x00FF); //c edge gain
2094         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x5000, 0xFF00); //y edge gain
2095         MApi_XC_W2BYTEMSK(REG_SC_BK05_23_L, 0x0040, 0x00FF); //c value gain
2096     }
2097     else if( u32sd2hdCnt == 7 )
2098     {
2099         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x0060, 0x00FF); //c edge gain
2100         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x6000, 0xFF00); //y edge gain
2101         MApi_XC_W2BYTEMSK(REG_SC_BK05_23_L, 0x0050, 0x00FF); //c value gain
2102     }
2103     else if( u32sd2hdCnt == 8 )
2104     {
2105         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x0070, 0x00FF); //c edge gain
2106         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x7000, 0xFF00); //y edge gain
2107         MApi_XC_W2BYTEMSK(REG_SC_BK05_23_L, 0x0060, 0x00FF); //c value gain
2108     }
2109     else if( u32sd2hdCnt == 9 )
2110     {
2111         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x0080, 0x00FF); //c edge gain
2112         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x8000, 0xFF00); //y edge gain
2113         MApi_XC_W2BYTEMSK(REG_SC_BK05_23_L, 0x0070, 0x00FF); //c value gain
2114     }
2115     else if( u32sd2hdCnt == 10 )
2116     {
2117         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x0090, 0x00FF); //c edge gain
2118         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x9000, 0xFF00); //y edge gain
2119         MApi_XC_W2BYTEMSK(REG_SC_BK05_23_L, 0x0080, 0x00FF); //c value gain
2120     }
2121     else if( u32sd2hdCnt == 11 )
2122     {
2123         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x00A0, 0x00FF); //c edge gain
2124         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0xA000, 0xFF00); //y edge gain
2125         MApi_XC_W2BYTEMSK(REG_SC_BK05_23_L, 0x0090, 0x00FF); //c value gain
2126     }
2127     else if( u32sd2hdCnt >= 12 )
2128     {
2129         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0x00B0, 0x00FF); //c edge gain
2130         MApi_XC_W2BYTEMSK(REG_SC_BK05_26_L, 0xB000, 0xFF00); //y edge gain
2131         MApi_XC_W2BYTEMSK(REG_SC_BK05_23_L, 0x00A0, 0x00FF); //c value gain
2132     }
2133 }
2134 
MDrv_SC_Amber5_DHD_driver2(void * pInstance,MS_U32 u32uComplex,MS_U32 u32IsHorizontalMoving,MS_U8 u8SD2HDValue,MS_BOOL bVerticalMovingForDHD)2135 void MDrv_SC_Amber5_DHD_driver2(void *pInstance, MS_U32 u32uComplex, MS_U32 u32IsHorizontalMoving, MS_U8 u8SD2HDValue, MS_BOOL bVerticalMovingForDHD )
2136 {
2137     // dhd stadium patch: lower dhd strength
2138     static MS_U32 u32filmCnt = 0;
2139     MS_U32 u32checkwidth = MApi_XC_R2BYTEMSK(REG_SC_BK12_0F_L, 0xFFFF);
2140     UNUSED(u32IsHorizontalMoving);
2141 
2142     if (((u8SD2HDValue<10) && (u32uComplex>200)
2143         && (u32checkwidth>0x700)) || (bVerticalMovingForDHD && (u32checkwidth>0x700) )
2144         || ( (u8SD2HDValue<5) && (u32checkwidth>0x700)))
2145     {
2146         u32filmCnt++;
2147 
2148         if( u32filmCnt >= 50 )
2149         {
2150             u32filmCnt = 50;
2151         }
2152     }
2153     else
2154     {
2155         if( u32filmCnt == 0 )
2156         {
2157             u32filmCnt = 0;
2158         }
2159         else
2160         {
2161             u32filmCnt--;
2162         }
2163     }
2164 
2165     if( u32filmCnt <= 2 )
2166         MApi_XC_W2BYTEMSK(REG_SC_BK05_2C_L, 0x000F, 0x000F); //dhd user weight
2167     else if( u32filmCnt == 3 )
2168         MApi_XC_W2BYTEMSK(REG_SC_BK05_2C_L, 0x000D, 0x000F); //dhd user weight
2169     else if( u32filmCnt == 4 )
2170         MApi_XC_W2BYTEMSK(REG_SC_BK05_2C_L, 0x000B, 0x000F); //dhd user weight
2171     else if( u32filmCnt == 5 )
2172         MApi_XC_W2BYTEMSK(REG_SC_BK05_2C_L, 0x0009, 0x000F); //dhd user weight
2173     else if( u32filmCnt == 6 )
2174         MApi_XC_W2BYTEMSK(REG_SC_BK05_2C_L, 0x0007, 0x000F); //dhd user weight
2175     else if( u32filmCnt == 7 )
2176         MApi_XC_W2BYTEMSK(REG_SC_BK05_2C_L, 0x0005, 0x000F); //dhd user weight
2177     else if( u32filmCnt == 8 )
2178         MApi_XC_W2BYTEMSK(REG_SC_BK05_2C_L, 0x0003, 0x000F); //dhd user weight
2179     else if( u32filmCnt == 9 )
2180         MApi_XC_W2BYTEMSK(REG_SC_BK05_2C_L, 0x0001, 0x000F); //dhd user weight
2181     else if( u32filmCnt >= 10 )
2182         MApi_XC_W2BYTEMSK(REG_SC_BK05_2C_L, 0x0000, 0x000F); //dhd user weight
2183 }
2184 
2185 /******************************************************************************/
2186 ///CCS Driver
2187 /******************************************************************************/
2188 
MDrv_SC_Amber5_CCS_driver(void * pInstance,MS_U32 u32uComplex)2189 void MDrv_SC_Amber5_CCS_driver(void *pInstance, MS_U32 u32uComplex)
2190 {
2191     // ccs moving color dot pattern patch: lower ccs strength
2192     static MS_U32 u32videoCnt = 0;
2193 
2194     if( u32uComplex < 16 )
2195     {
2196         u32videoCnt++;
2197 
2198         if( u32videoCnt >= 200 )
2199         {
2200             u32videoCnt = 200;
2201         }
2202     }
2203     else
2204     {
2205         if( u32videoCnt == 0 )
2206         {
2207             u32videoCnt = 0;
2208         }
2209         else
2210         {
2211             u32videoCnt--;
2212         }
2213     }
2214 
2215     if( u32videoCnt <= 2 )
2216         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x000F, 0x000F); //ccs user weight
2217     else if( u32videoCnt == 3 )
2218         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x000D, 0x000F); //ccs user weight
2219     else if( u32videoCnt == 4 )
2220         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x000B, 0x000F); //ccs user weight
2221     else if( u32videoCnt == 5 )
2222         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0009, 0x000F); //ccs user weight
2223     else if( u32videoCnt == 6 )
2224         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0007, 0x000F); //ccs user weight
2225     else if( u32videoCnt == 7 )
2226         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0005, 0x000F); //ccs user weight
2227     else if( u32videoCnt == 8 )
2228         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0003, 0x000F); //ccs user weight
2229     else if( u32videoCnt == 9 )
2230         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0001, 0x000F); //ccs user weight
2231     else if( u32videoCnt >= 10 )
2232         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0000, 0x000F); //ccs user weight
2233 }
2234 
MDrv_SC_Amber5_CCS_driver2(void * pInstance,MS_U32 u32OOSRMC,MS_U32 u32IsHorizontalMoving)2235 void MDrv_SC_Amber5_CCS_driver2(void *pInstance, MS_U32 u32OOSRMC, MS_U32 u32IsHorizontalMoving)
2236 {
2237     // horizontal moving => lower ccs weight
2238     static MS_U32 u32videoCnt = 0;
2239 
2240     if( (u32OOSRMC < 20) || (u32IsHorizontalMoving >25) )
2241     {
2242         u32videoCnt++;
2243 
2244         if( u32videoCnt >= 200 )
2245         {
2246             u32videoCnt = 200;
2247         }
2248     }
2249     else
2250     {
2251         if( u32videoCnt == 0 )
2252         {
2253             u32videoCnt = 0;
2254         }
2255         else
2256         {
2257             u32videoCnt--;
2258         }
2259     }
2260 
2261     if( u32videoCnt <= 2 )
2262         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x000F, 0x000F); //ccs user weight
2263     else if( u32videoCnt == 3 )
2264         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x000D, 0x000F); //ccs user weight
2265     else if( u32videoCnt == 4 )
2266         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x000B, 0x000F); //ccs user weight
2267     else if( u32videoCnt == 5 )
2268         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0009, 0x000F); //ccs user weight
2269     else if( u32videoCnt == 6 )
2270         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0007, 0x000F); //ccs user weight
2271     else if( u32videoCnt == 7 )
2272         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0005, 0x000F); //ccs user weight
2273     else if( u32videoCnt == 8 )
2274         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0003, 0x000F); //ccs user weight
2275     else if( u32videoCnt == 9 )
2276         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0001, 0x000F); //ccs user weight
2277     else if( u32videoCnt >= 10 )
2278         MApi_XC_W2BYTEMSK(REG_SC_BK05_5C_L, 0x0000, 0x000F); //ccs user weight
2279 }
2280 #define ScenceChange_TH        0x1000
MDrv_SC_Agate_SceneChangeY(void * pInstance)2281  MS_BOOL MDrv_SC_Agate_SceneChangeY(void *pInstance)
2282  {
2283  #if 0
2284      XC_DLC_ApiStatus DLC_Status;
2285      MApi_XC_DLC_GetStatus_Ex(&DLC_Status,MAIN_WINDOW);
2286      static MS_U16 Pre_LumaHistogram[32], Max_HistogramDiff = 0;
2287      MS_U16 HistogramDiff = 0;
2288      int i;
2289      for(i=0;i<32;i++)
2290      {
2291          if(DLC_Status.DLCluma.g_wLumaHistogram32H[i] >= Pre_LumaHistogram[i])
2292          {
2293              HistogramDiff = DLC_Status.DLCluma.g_wLumaHistogram32H[i] - Pre_LumaHistogram[i];
2294          }
2295          else
2296          {
2297              HistogramDiff = Pre_LumaHistogram[i] - DLC_Status.DLCluma.g_wLumaHistogram32H[i];
2298          }
2299 
2300          if(HistogramDiff > Max_HistogramDiff)
2301          {
2302              Max_HistogramDiff = HistogramDiff;
2303          }
2304 
2305          Pre_LumaHistogram[i] = DLC_Status.DLCluma.g_wLumaHistogram32H[i];
2306      }
2307      if( Max_HistogramDiff > ScenceChange_TH )
2308      {
2309          Max_HistogramDiff = 0;
2310          return true;
2311      }
2312      else
2313      {
2314          Max_HistogramDiff = 0;
2315          return false;
2316      }
2317 #else
2318     return true;
2319 #endif
2320  }
2321 
2322  #define MAX_NM_GAIN 0x8
2323  #define MIN_NM_GAIN 0x0
NoiseMaskingControl(void * pInstance,int DBKStr,MS_BOOL isSceneChange)2324  void NoiseMaskingControl(void *pInstance, int DBKStr,MS_BOOL isSceneChange)
2325  {
2326      //int NM_Gain = minmax((int)(2.6*DBKStr - 96), 0, 16);
2327      int NM_Gain = minmax((int)(2.6*DBKStr - 96), MIN_NM_GAIN, MAX_NM_GAIN);
2328      MApi_XC_W2BYTEMSK(REG_SC_BK0C_52_L, NM_Gain, 0x003F);//Y noise masking gain
2329      if (isSceneChange )
2330      {
2331          MApi_XC_W2BYTEMSK(REG_SC_BK0C_50_L, 0x00, 0x0008); //Y noise masking off
2332      }
2333      else
2334      {
2335          MApi_XC_W2BYTEMSK(REG_SC_BK0C_50_L, 0x08, 0x0008); //Y noise masking on
2336      }
2337  }
normFactorWidth(void * pInstance,MS_U32 u32AvgBLKN)2338  MS_S8 normFactorWidth(void *pInstance, MS_U32 u32AvgBLKN)
2339  {
2340      MS_S8 s8NormFac = 0;
2341      if (u32AvgBLKN > 1048576*1024)
2342      {
2343          s8NormFac = 25;
2344      }
2345      else if (u32AvgBLKN > 1048576*512)
2346      {
2347          s8NormFac = 24;
2348      }
2349      else if (u32AvgBLKN > 1048576*256)
2350      {
2351          s8NormFac = 23;
2352      }
2353      else if (u32AvgBLKN > 1048576*128)
2354      {
2355          s8NormFac = 22;
2356      }
2357      else if (u32AvgBLKN > 1048576*64)
2358      {
2359          s8NormFac = 21;
2360      }
2361      else if (u32AvgBLKN > 1048576*32)
2362      {
2363          s8NormFac = 20;
2364      }
2365      else if (u32AvgBLKN > 1048576*16)
2366      {
2367          s8NormFac = 19;
2368      }
2369      else if (u32AvgBLKN > 1048576*8)
2370      {
2371          s8NormFac = 18;
2372      }
2373      else if (u32AvgBLKN > 1048576*4)
2374      {
2375          s8NormFac = 17;
2376      }
2377      else if (u32AvgBLKN > 1048576*2)
2378      {
2379          s8NormFac = 16;
2380      }
2381      else if (u32AvgBLKN > 1048576)
2382      {
2383          s8NormFac = 15;
2384      }
2385      else if (u32AvgBLKN > 524288)
2386      {
2387          s8NormFac = 14;
2388      }
2389      else if (u32AvgBLKN > 262144)
2390      {
2391          s8NormFac = 13;
2392      }
2393      else if (u32AvgBLKN > 131072)
2394      {
2395          s8NormFac = 12;
2396      }
2397      else if (u32AvgBLKN > 65536)
2398      {
2399          s8NormFac = 11;
2400      }
2401      else if (u32AvgBLKN > 32768)
2402      {
2403          s8NormFac = 10;
2404      }
2405      else if (u32AvgBLKN > 16384)
2406      {
2407          s8NormFac = 9;
2408      }
2409      else if (u32AvgBLKN > 8192)
2410      {
2411          s8NormFac = 8;
2412      }
2413      else if (u32AvgBLKN > 4096)
2414      {
2415          s8NormFac = 7;
2416      }
2417      else if (u32AvgBLKN > 2048)
2418      {
2419          s8NormFac = 6;
2420      }
2421      else if (u32AvgBLKN > 1024)
2422      {
2423          s8NormFac = 5;
2424      }
2425      else if (u32AvgBLKN > 512)
2426      {
2427          s8NormFac = 4;
2428      }
2429      else if (u32AvgBLKN > 256)
2430      {
2431          s8NormFac = 3;
2432      }
2433      else if (u32AvgBLKN > 128)
2434      {
2435          s8NormFac = 2;
2436      }
2437      else if (u32AvgBLKN > 64)
2438      {
2439          s8NormFac = 1;
2440      }
2441      else if (u32AvgBLKN > 32)
2442      {
2443          s8NormFac = 0;
2444      }
2445      else if (u32AvgBLKN > 16)
2446      {
2447          s8NormFac = -1;
2448      }
2449      else if (u32AvgBLKN > 8)
2450      {
2451          s8NormFac = -2;
2452      }
2453      else
2454      {
2455          s8NormFac = 0;
2456      }
2457 
2458      return s8NormFac;
2459  }
readHistogramPart(void * pInstance,MS_U16 * au16Histogram,MS_U8 u8CountForDBK,MS_U16 u16Initial_point,MS_U16 u16End_point)2460  MS_U8 readHistogramPart(void *pInstance, MS_U16* au16Histogram, MS_U8 u8CountForDBK, MS_U16 u16Initial_point, MS_U16 u16End_point)
2461  {
2462      MS_U8 u8Data_ack_en;
2463      MS_U16 u16Bin;
2464 
2465          //--------- Blockiness Histogram Request start ---------//
2466      if (u8CountForDBK == 0)
2467      {
2468 // write 1 to request register
2469          MApi_XC_W2BYTEMSK(REG_SC_BK0C_0D_L, 1, 0x0001);
2470      }
2471 
2472      if (1)
2473      {
2474          //--------- Blockiness Histogram Acknowledge start ---------//
2475          u8Data_ack_en = MApi_XC_R2BYTEMSK(REG_SC_BK0C_0D_L, 0x0002) ;
2476          //--------- Function Process start ---------//
2477          if(u8Data_ack_en)
2478          {
2479              // write 1 to sram_io_en register
2480              MApi_XC_W2BYTEMSK(REG_SC_BK0C_0D_L, 0x0100, 0x0100);
2481              for(u16Bin = u16Initial_point; u16Bin < u16End_point; u16Bin++)
2482              {
2483                  MApi_XC_W2BYTEMSK(REG_SC_BK0C_0E_L, (MS_U16)(0x8000 + u16Bin), 0xFFFF);
2484                  // read data from selected address
2485                  au16Histogram[u16Bin] = MApi_XC_R2BYTEMSK(REG_SC_BK0C_0F_L, 0xFFFF);
2486              }
2487              MApi_XC_W2BYTEMSK(REG_SC_BK0C_0D_L, 0, 0x0001);
2488              return 1;
2489          }
2490          else//(!data_ack_en)
2491          {
2492              return 0;
2493          }
2494      }
2495  }
DBK_WidthPartHW(void * pInstance,MS_U16 * u16WidthDiffHW,MS_U16 * u16Avg,MS_BOOL isSceneChangeY,MS_U8 * u8WidthConfidence)2496  MS_S8 DBK_WidthPartHW(void *pInstance, MS_U16 *u16WidthDiffHW, MS_U16* u16Avg,MS_BOOL isSceneChangeY,MS_U8* u8WidthConfidence)
2497 {
2498      MS_U8 u8Index;
2499      MS_U32 u32MaxWidthDiff  = 0;
2500      MS_U32 u32MinWidthDiff  = 0x7FFFFFFF;
2501      MS_U32 u32Min2WidthDiff = 0x7FFFFFFF;
2502      MS_U32 u32Min3WidthDiff = 0x7FFFFFFF;
2503      MS_U32 u32Min4WidthDiff = 0x7FFFFFFF;
2504      MS_S8 s8MinIdx = -1, s8Min2Idx = -1, s8Min3Idx = -1, s8Min4Idx = -1;// maxIdx = -1;
2505      static MS_U8 u8TempWidth = 4;
2506      MS_U16 u16DiffThrd = 2048;
2507      static MS_S16 s16ConfidenceLevel8 = 0,s16ConfidenceLevel11 = 0,s16ConfidenceLevel16 = 0,s16ConfidenceLevel21 = 0;
2508      int log_en = 0;
2509      //og_en = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK30_01_L, 0x000F); //easter
2510 
2511      u16WidthDiffHW[0 ] = MApi_XC_R2BYTEMSK(REG_SC_BK60_40_L,0xFFFF) ; // width 4
2512      u16WidthDiffHW[1 ] = MApi_XC_R2BYTEMSK(REG_SC_BK60_41_L,0xFFFF) ; // width 5
2513      u16WidthDiffHW[2 ] = MApi_XC_R2BYTEMSK(REG_SC_BK60_42_L,0xFFFF) ; // width 6
2514      u16WidthDiffHW[3 ] = MApi_XC_R2BYTEMSK(REG_SC_BK60_43_L,0xFFFF) ; // width 7
2515      u16WidthDiffHW[4 ] = MApi_XC_R2BYTEMSK(REG_SC_BK60_44_L,0xFFFF) ; // width 8
2516      u16WidthDiffHW[5 ] = MApi_XC_R2BYTEMSK(REG_SC_BK60_45_L,0xFFFF) ; // width 9
2517      u16WidthDiffHW[6 ] = MApi_XC_R2BYTEMSK(REG_SC_BK60_46_L,0xFFFF) ; // width 10
2518      u16WidthDiffHW[7 ] = MApi_XC_R2BYTEMSK(REG_SC_BK60_47_L,0xFFFF) ; // width 11
2519      u16WidthDiffHW[8 ] = MApi_XC_R2BYTEMSK(REG_SC_BK60_48_L,0xFFFF) ; // width 12
2520      u16WidthDiffHW[9 ] = MApi_XC_R2BYTEMSK(REG_SC_BK60_49_L,0xFFFF) ; // width 13
2521      u16WidthDiffHW[10] = MApi_XC_R2BYTEMSK(REG_SC_BK60_4A_L,0xFFFF) ; // width 14
2522      u16WidthDiffHW[11] = MApi_XC_R2BYTEMSK(REG_SC_BK60_4B_L,0xFFFF) ; // width 15
2523      u16WidthDiffHW[12] = MApi_XC_R2BYTEMSK(REG_SC_BK60_4C_L,0xFFFF) ; // width 16
2524      u16WidthDiffHW[13] = MApi_XC_R2BYTEMSK(REG_SC_BK60_4D_L,0xFFFF) ; // width 17
2525      u16WidthDiffHW[14] = MApi_XC_R2BYTEMSK(REG_SC_BK60_4E_L,0xFFFF) ; // width 18
2526      u16WidthDiffHW[15] = MApi_XC_R2BYTEMSK(REG_SC_BK60_4F_L,0xFFFF) ; // width 19
2527      u16WidthDiffHW[16] = MApi_XC_R2BYTEMSK(REG_SC_BK60_50_L,0xFFFF) ; // width 20
2528      u16WidthDiffHW[17] = MApi_XC_R2BYTEMSK(REG_SC_BK60_51_L,0xFFFF) ; // width 21
2529      u16WidthDiffHW[18] = MApi_XC_R2BYTEMSK(REG_SC_BK60_52_L,0xFFFF) ; // width 22
2530      u16WidthDiffHW[19] = MApi_XC_R2BYTEMSK(REG_SC_BK60_53_L,0xFFFF) ; // width 23
2531      u16WidthDiffHW[20] = MApi_XC_R2BYTEMSK(REG_SC_BK60_54_L,0xFFFF) ; // width 24
2532      u16WidthDiffHW[21] = MApi_XC_R2BYTEMSK(REG_SC_BK60_55_L,0xFFFF) ; // width 25
2533      u16WidthDiffHW[22] = MApi_XC_R2BYTEMSK(REG_SC_BK60_56_L,0xFFFF) ; // width 26
2534      u16WidthDiffHW[23] = MApi_XC_R2BYTEMSK(REG_SC_BK60_57_L,0xFFFF) ; // width 27
2535      u16WidthDiffHW[24] = MApi_XC_R2BYTEMSK(REG_SC_BK60_58_L,0xFFFF) ; // width 28
2536      u16WidthDiffHW[25] = MApi_XC_R2BYTEMSK(REG_SC_BK60_59_L,0xFFFF) ; // width 29
2537      u16WidthDiffHW[26] = MApi_XC_R2BYTEMSK(REG_SC_BK60_5A_L,0xFFFF) ; // width 30
2538      u16WidthDiffHW[27] = MApi_XC_R2BYTEMSK(REG_SC_BK60_5B_L,0xFFFF) ; // width 31
2539      u16WidthDiffHW[28] = MApi_XC_R2BYTEMSK(REG_SC_BK60_5C_L,0xFFFF) ; // width 32
2540      u16WidthDiffHW[29] = MApi_XC_R2BYTEMSK(REG_SC_BK60_5D_L,0xFFFF) ; // width 33
2541      u16WidthDiffHW[30] = MApi_XC_R2BYTEMSK(REG_SC_BK60_5E_L,0xFFFF) ; // width 34
2542      u16WidthDiffHW[31] = MApi_XC_R2BYTEMSK(REG_SC_BK60_5F_L,0xFFFF) ; // width 35
2543 
2544      for(u8Index = 1 ; u8Index < paraSize ; u8Index++)
2545      {
2546          if (u16WidthDiffHW[u8Index] > u32MaxWidthDiff)
2547          {
2548              u32MaxWidthDiff = u16WidthDiffHW[u8Index];
2549          }
2550      }
2551 
2552      for(u8Index = 1 ; u8Index < paraSize -1; u8Index+=2)
2553      {
2554          if ( (u8Index==3 || u8Index==11 || u8Index==19 || u8Index==27) && (u16WidthDiffHW[u8Index+1] - u16WidthDiffHW[u8Index ] <= u16DiffThrd) )
2555          {
2556              u16WidthDiffHW[u8Index+1]= min(u16WidthDiffHW[u8Index ], u16WidthDiffHW[u8Index+1]);
2557              u16WidthDiffHW[u8Index  ]= u32MaxWidthDiff;
2558          }
2559          else if ( (u8Index==7) && ( (u16WidthDiffHW[u8Index  ] - u16WidthDiffHW[u8Index-1 ] <= u16DiffThrd) || (u16WidthDiffHW[u8Index  ] - u16WidthDiffHW[u8Index+1 ] <= u16DiffThrd)) )
2560          {
2561              u16WidthDiffHW[u8Index  ]= min3(u16WidthDiffHW[u8Index ], u16WidthDiffHW[u8Index-1], u16WidthDiffHW[u8Index+1]);
2562              u16WidthDiffHW[u8Index-1]= u32MaxWidthDiff;
2563              u16WidthDiffHW[u8Index+1]= u32MaxWidthDiff;
2564          }
2565      }
2566 
2567      for(u8Index = 1 ; u8Index < paraSize ; u8Index++)
2568      {
2569          (*u16Avg) += u16WidthDiffHW[u8Index];
2570          if (u16WidthDiffHW[u8Index] < u32MinWidthDiff)
2571          {
2572              u32Min4WidthDiff = u32Min3WidthDiff;
2573              s8Min4Idx = s8Min3Idx;
2574              u32Min3WidthDiff = u32Min2WidthDiff;
2575              s8Min3Idx = s8Min2Idx;
2576              u32Min2WidthDiff = u32MinWidthDiff;
2577              s8Min2Idx = s8MinIdx;
2578              u32MinWidthDiff = u16WidthDiffHW[u8Index];
2579              s8MinIdx = u8Index;
2580          }
2581          else if (u16WidthDiffHW[u8Index] < u32Min2WidthDiff)
2582          {
2583              u32Min4WidthDiff = u32Min3WidthDiff;
2584              s8Min4Idx = s8Min3Idx;
2585              u32Min3WidthDiff = u32Min2WidthDiff;
2586              s8Min3Idx = s8Min2Idx;
2587              u32Min2WidthDiff = u16WidthDiffHW[u8Index];
2588              s8Min2Idx = u8Index;
2589          }
2590          else if (u16WidthDiffHW[u8Index] < u32Min3WidthDiff)
2591          {
2592              u32Min4WidthDiff = u32Min3WidthDiff;
2593              s8Min4Idx = s8Min3Idx;
2594              u32Min3WidthDiff = u16WidthDiffHW[u8Index];
2595              s8Min3Idx = u8Index;
2596          }
2597          else if (u16WidthDiffHW[u8Index] < u32Min4WidthDiff)
2598          {
2599              u32Min4WidthDiff = u16WidthDiffHW[u8Index];
2600              s8Min4Idx = u8Index;
2601          }
2602      }
2603 
2604      *u16Avg = (*u16Avg) / (paraSize-1);
2605     if(isSceneChangeY)
2606     {
2607         s16ConfidenceLevel8 = 0;
2608         s16ConfidenceLevel11 = 0;
2609         s16ConfidenceLevel16 = 0;
2610         s16ConfidenceLevel21 = 0;
2611     }
2612      s16ConfidenceLevel8 = minmax(s16ConfidenceLevel8,0,16);
2613      s16ConfidenceLevel11 = minmax(s16ConfidenceLevel11,0,16);
2614      s16ConfidenceLevel16 = minmax(s16ConfidenceLevel16,0,16);
2615      s16ConfidenceLevel21 = minmax(s16ConfidenceLevel21,0,16);
2616      if(s16ConfidenceLevel8>15)
2617      {
2618          s16ConfidenceLevel11 = 0;
2619          s16ConfidenceLevel16 = 0;
2620          s16ConfidenceLevel21 = 0;
2621      }
2622      else if(s16ConfidenceLevel11>15)
2623      {
2624          s16ConfidenceLevel8 = 0;
2625          s16ConfidenceLevel16 = 0;
2626          s16ConfidenceLevel21 = 0;
2627      }
2628      else if(s16ConfidenceLevel16>15)
2629      {
2630          s16ConfidenceLevel8 = 0;
2631          s16ConfidenceLevel11 = 0;
2632          s16ConfidenceLevel21 = 0;
2633      }
2634      else if(s16ConfidenceLevel21>15)
2635      {
2636          s16ConfidenceLevel8 = 0;
2637          s16ConfidenceLevel16 = 0;
2638          s16ConfidenceLevel11 = 0;
2639      }
2640 
2641      if(log_en==5)
2642      {
2643          //printf("s8MinIdx:%d s8Min2Idx:%d s8Min3Idx:%d s8Min4Idx:%d\n",s8MinIdx,s8Min2Idx,s8Min3Idx,s8Min4Idx);
2644          //printf("u16ConfidenceLevel8:%d u16ConfidenceLevel11:%d\n",s16ConfidenceLevel8,s16ConfidenceLevel11);
2645          //printf("u16ConfidenceLevel16:%d u16ConfidenceLevel21:%d\n",s16ConfidenceLevel16,s16ConfidenceLevel21);
2646      }
2647      *u8WidthConfidence = max3(s16ConfidenceLevel8,s16ConfidenceLevel11,s16ConfidenceLevel16);
2648      *u8WidthConfidence = max((*u8WidthConfidence),s16ConfidenceLevel21);
2649      /*if(u8TempWidth == 4 )//&& s16ConfidenceLevel8>10)
2650      {
2651          if((abs(s8MinIdx-u8TempWidth) <2)||(abs(s8Min2Idx-u8TempWidth) <2)||(abs(s8Min3Idx-u8TempWidth) <2)||(abs(s8Min4Idx-u8TempWidth) <2))
2652          {
2653              return 4;
2654          }
2655      }
2656      else*/
2657      if(u8TempWidth == 7 && s16ConfidenceLevel11>10)
2658      {
2659          if ((((s8MinIdx == 7) || (s8Min2Idx == 7) || (s8Min3Idx == 7) || (s8Min4Idx == 7))|| ((s8MinIdx == 16) || (s8Min2Idx == 16) || (s8Min3Idx == 16) || (s8Min4Idx == 16))
2660                  || ((s8MinIdx == 17) || (s8Min2Idx == 17) || (s8Min3Idx == 17) || (s8Min4Idx == 17)) ) && ((s8MinIdx == 28) || (s8Min2Idx == 28) || (s8Min3Idx == 28) || (s8Min4Idx == 28)))
2661          {
2662             s16ConfidenceLevel11++;
2663              return 7;
2664          }
2665          else if((abs(s8MinIdx-u8TempWidth) <2)||(abs(s8Min2Idx-u8TempWidth) <2)||(abs(s8Min3Idx-u8TempWidth) <2)||(abs(s8Min4Idx-u8TempWidth) <2))
2666          {
2667             s16ConfidenceLevel11--;
2668              return 7;
2669          }
2670      }
2671      else if(u8TempWidth == 17 && s16ConfidenceLevel21>10)
2672      {
2673           if ((((s8MinIdx == 16 || s8Min2Idx == 16 || s8Min3Idx == 16) || (s8MinIdx == 18 || s8Min2Idx == 18 || s8Min3Idx == 18) ) && (s8MinIdx == 17 || s8Min2Idx == 17 || s8Min3Idx == 17)))
2674          {
2675             s16ConfidenceLevel21++;
2676              u8TempWidth = 17;
2677              return 17;
2678          }
2679          else if((abs(s8MinIdx-u8TempWidth) <4)||(abs(s8Min2Idx-u8TempWidth) <4)||(abs(s8Min3Idx-u8TempWidth) <4)||(abs(s8Min4Idx-u8TempWidth) <4))
2680          {
2681             s16ConfidenceLevel21--;
2682              return 17;
2683          }
2684      }
2685 
2686      if ((((s8MinIdx == 7) || (s8Min2Idx == 7) || (s8Min3Idx == 7) || (s8Min4Idx == 7))|| ((s8MinIdx == 16) || (s8Min2Idx == 16) || (s8Min3Idx == 16) || (s8Min4Idx == 16))
2687              || ((s8MinIdx == 17) || (s8Min2Idx == 17) || (s8Min3Idx == 17) || (s8Min4Idx == 17)) ) && ((s8MinIdx == 28) || (s8Min2Idx == 28) || (s8Min3Idx == 28) || (s8Min4Idx == 28)))
2688      {
2689          if(u8TempWidth == 7)
2690          {
2691              s16ConfidenceLevel11++;
2692          }
2693          else
2694          {
2695              s16ConfidenceLevel8--;
2696              s16ConfidenceLevel16--;
2697              s16ConfidenceLevel21--;
2698          }
2699          u8TempWidth = 7;
2700          return 7;
2701      }
2702      else if ((((s8MinIdx == 16 || s8Min2Idx == 16 || s8Min3Idx == 16) || (s8MinIdx == 18 || s8Min2Idx == 18 || s8Min3Idx == 18) ) && (s8MinIdx == 17 || s8Min2Idx == 17 || s8Min3Idx == 17)))
2703      {
2704          if(u8TempWidth == 17)
2705          {
2706              s16ConfidenceLevel21++;
2707          }
2708          else
2709          {
2710              s16ConfidenceLevel8--;
2711              s16ConfidenceLevel16--;
2712              s16ConfidenceLevel11--;
2713          }
2714          u8TempWidth = 17;
2715          return 17;
2716      }
2717 
2718      if (s8MinIdx == 4 || s8Min2Idx == 4 || s8Min3Idx == 4 || s8Min4Idx == 4)
2719      {
2720          if(u8TempWidth == 4)
2721          {
2722              s16ConfidenceLevel8++;
2723          }
2724          else
2725          {
2726              s16ConfidenceLevel11--;
2727              s16ConfidenceLevel16--;
2728              s16ConfidenceLevel21--;
2729          }
2730          u8TempWidth = 4;
2731          return 4;
2732      }
2733      else if (s8MinIdx == 12 || s8Min2Idx == 12 || s8Min3Idx == 12 || s8Min4Idx == 12)
2734      {
2735         if(u8TempWidth == 12)
2736         {
2737             s16ConfidenceLevel16++;
2738         }
2739         else
2740         {
2741             s16ConfidenceLevel8--;
2742             s16ConfidenceLevel11--;
2743             s16ConfidenceLevel21--;
2744         }
2745          u8TempWidth = 12;
2746          return 12;
2747      }
2748 
2749      return -1;
2750  }
DBK_WidthPartTop(void * pInstance,MS_U16 * u16WidthDiffHW,MS_U16 * u16MaxMinDiff,MS_U16 * u16Avg,MS_BOOL isSceneChangeY,MS_U8 * u8WidthConfidence)2751  MS_S8 DBK_WidthPartTop(void *pInstance, MS_U16 *u16WidthDiffHW, MS_U16* u16MaxMinDiff, MS_U16* u16Avg,MS_BOOL isSceneChangeY,MS_U8* u8WidthConfidence)
2752  {
2753      MS_U8 u8Bin;
2754      MS_U32 u32MinValue = 0x7FFFFFFF;
2755      MS_U16 u16MaxValue = 0;
2756      MS_S8 s8NormFac;
2757      MS_S8 s8MinIndex;
2758      s8MinIndex = DBK_WidthPartHW(pInstance, u16WidthDiffHW, u16Avg,isSceneChangeY,u8WidthConfidence);
2759      if (s8MinIndex == -1)
2760      {
2761          for (u8Bin =0; u8Bin<paraSize;u8Bin++)
2762          {
2763              if (u16WidthDiffHW[u8Bin] > u16MaxValue )
2764             {
2765                  u16MaxValue = u16WidthDiffHW[u8Bin];
2766             }
2767 
2768              if (u16WidthDiffHW[u8Bin] < u32MinValue )
2769             {
2770                  u32MinValue = u16WidthDiffHW[u8Bin];
2771             }
2772          }
2773          *u16MaxMinDiff = u16MaxValue - u32MinValue;
2774          s8NormFac = normFactorWidth(pInstance, *u16MaxMinDiff);
2775          if(s8NormFac >0)
2776          {
2777              for (u8Bin=0;u8Bin<paraSize;u8Bin++)
2778              {
2779                  u16WidthDiffHW[u8Bin] = (u16WidthDiffHW[u8Bin] - u32MinValue) >> s8NormFac;
2780              }
2781              *u16Avg = (*u16Avg-u32MinValue) >> s8NormFac;
2782              *u16MaxMinDiff = (*u16MaxMinDiff) >>s8NormFac;
2783          }
2784          else if(s8NormFac ==0)
2785          {
2786              for (u8Bin=0;u8Bin<paraSize;u8Bin++)
2787              {
2788                  u16WidthDiffHW[u8Bin] = (u16WidthDiffHW[u8Bin] - u32MinValue);
2789              }
2790              *u16Avg = (*u16Avg-u32MinValue);
2791              *u16MaxMinDiff = (*u16MaxMinDiff);
2792          }
2793          else
2794          {
2795              s8NormFac = 0-s8NormFac;
2796              for (u8Bin=0;u8Bin<paraSize;u8Bin++)
2797              {
2798                  u16WidthDiffHW[u8Bin] = (u16WidthDiffHW[u8Bin] - u32MinValue) << s8NormFac;
2799              }
2800              *u16Avg = (*u16Avg-u32MinValue) <<s8NormFac;
2801              *u16MaxMinDiff = (*u16MaxMinDiff) <<s8NormFac;
2802          }
2803          return -1;
2804      }
2805      else
2806      {
2807          return s8MinIndex;
2808      }
2809  }
WidthIIR_H(void * pInstance,MS_U8 u8Width)2810  void WidthIIR_H(void *pInstance, MS_U8 u8Width) // Width IIR Protection
2811  {
2812      static MS_U8 u8HysteresisCount = 0;
2813      static MS_U8 u8LastWidth = 8;
2814      int log_en = 0;
2815      //log_en = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK30_01_L, 0x000F); //easter
2816      if(u8HysteresisCount<6)
2817      {
2818          if(log_en==5)
2819          {
2820              //printf("new width:%d last width:%d final width:%d hysteresisCount:%d\n",u8Width,u8LastWidth,WidthIIR,u8HysteresisCount);
2821          }
2822 
2823          if(u8Width == u8LastWidth)
2824          {
2825              u8HysteresisCount = 0;
2826              WidthIIR = u8Width;
2827          }
2828          else
2829          {
2830              u8HysteresisCount++;
2831              WidthIIR = u8LastWidth;
2832          }
2833      }
2834      else
2835      {
2836          WidthIIR = u8Width;
2837          u8HysteresisCount = 0;
2838      }
2839      u8LastWidth = WidthIIR;
2840  }
DBK_ComputeWidthPart(void * pInstance,MS_U16 * u16WidthDiffHW,MS_U8 u8LastminIdex,MS_U16 u16Avg,MS_U8 u8WidthConfidence)2841  MS_S8 DBK_ComputeWidthPart(void *pInstance, MS_U16 *u16WidthDiffHW,MS_U8 u8LastminIdex, MS_U16 u16Avg,MS_U8 u8WidthConfidence)
2842  {
2843      MS_BOOL bWidthCandidate[paraSize];
2844      MS_U16 u16WTh = u16Avg;
2845      MS_U8 u8MinIndex=0;
2846      MS_U8 u8NumW=0;
2847      MS_U8 u8Bin;
2848      MS_BOOL bIf8 = 0, bIf11 = 0, bIf21 = 0, bIf32 = 0;
2849 
2850      for (u8Bin=0;u8Bin<paraSize;u8Bin++)
2851      {
2852          if (u16WidthDiffHW[u8Bin]<= u16WTh)
2853          {
2854              bWidthCandidate[u8Bin] = 1;
2855          }
2856          else
2857          {
2858              bWidthCandidate[u8Bin] = 0;
2859          }
2860 
2861      }
2862 
2863      if(bWidthCandidate[u8LastminIdex] ==1 && u8WidthConfidence>10)
2864      {
2865          return u8LastminIdex;
2866      }
2867      else
2868      {
2869          if (bWidthCandidate[4] == 1 ) //8
2870          {
2871              if (bWidthCandidate[3] == 1) //7
2872                  bWidthCandidate[3]  = 0;
2873              if (bWidthCandidate[11] == 1) // 15
2874                  bWidthCandidate[11] = 0;
2875              if (bWidthCandidate[12] == 1) // 16
2876                  bWidthCandidate[12] = 0;
2877              if (bWidthCandidate[19] == 1) // 23
2878                  bWidthCandidate[19] = 0;
2879              if (bWidthCandidate[20] == 1) // 24
2880                  bWidthCandidate[20] = 0;
2881              bIf8 = 1;
2882          }
2883          if (bWidthCandidate[7] == 1 ) //11
2884          {
2885              if (bWidthCandidate[6] == 1) //10
2886                  bWidthCandidate[6]  = 0;
2887              if (bWidthCandidate[8] == 1) //12
2888                  bWidthCandidate[8]  = 0;
2889              bIf11 = 1;
2890          }
2891          if ( ( bWidthCandidate[16] == 1 ) || ( bWidthCandidate[17] == 1 ) ||( bWidthCandidate[18] == 1 ) ) //20 21 22
2892          {
2893              bWidthCandidate[16] = 0;
2894              bWidthCandidate[17] = 1;
2895              bWidthCandidate[18] = 0;
2896              bIf21 = 1;
2897          }
2898          if ( bWidthCandidate[27] == 1 && bWidthCandidate[28] == 1)
2899              bIf32 = 1;
2900          if (bIf11 && bIf21 && bIf32) //11/21/32 all true -> source = 11 ->delete 21
2901          {
2902              bWidthCandidate[17] = 0;
2903          }
2904          if (bIf8 || bIf11 || bIf21)
2905          {
2906              if (bWidthCandidate[27] == 1) // 31
2907                  bWidthCandidate[27] = 0;
2908              if (bWidthCandidate[28] == 1) // 32
2909                  bWidthCandidate[28] = 0;
2910          }
2911 
2912      }
2913 
2914      for (u8Bin=0;u8Bin<paraSize;u8Bin++) // count number of frequencies
2915      {
2916          if (bWidthCandidate[u8Bin] == 1)
2917          {
2918              u8MinIndex = u8Bin;
2919              u8NumW++;
2920          }
2921      }
2922      if (u8NumW == 1)
2923      {
2924          return u8MinIndex;
2925      }
2926      else
2927      {
2928          return -1;
2929      }
2930  }
WidthToRegister(void * pInstance,MS_U8 u8Width)2931  void WidthToRegister(void *pInstance, MS_U8 u8Width) // Write width to register
2932  {
2933      MS_U8 u8Left1,u8Right1;
2934      if(u8Width<8)
2935      {
2936          u8Width = 8;
2937          u8Left1 = 8;
2938          u8Right1 = 9;
2939      }
2940      else if (u8Width==8) //test 0808 for performance
2941      {
2942          u8Left1 = 8;
2943          u8Right1 = 8;
2944      }
2945      else
2946      {
2947          u8Left1 = u8Width - 1; //fix prevent defect
2948          u8Right1 = (u8Width >=31) ? 31 : u8Width + 1;
2949      }
2950  //write interval left && interval right
2951      MApi_XC_W2BYTEMSK(REG_SC_BK0C_39_L, (MS_U16)((u8Left1<<8)|u8Right1), 0xFFFF);
2952  }
DBK_Width_Detect(void * pInstance,MS_BOOL isSceneChangeY)2953  MS_U8 DBK_Width_Detect(void *pInstance, MS_BOOL isSceneChangeY)//width detection with hardware accelaration
2954  {
2955      MS_S8 s8MinIndex = -1;     //minIndex+width_offset = candidate width
2956      //For width calculation
2957      //int WidthCandidate[paraSize];
2958      MS_U16 au16WidthDiffHW[paraSize];
2959      MS_U16 u16MaxMinDiff = 0;
2960      MS_U16 u16WidthAvg = 0;
2961      MS_U8 u8Width = 8;
2962      static MS_U8 u8LastminIdex = 4;
2963      MS_U8 u8WidthConfidence = 0;
2964      int log_en = 0;
2965      //log_en = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK30_01_L, 0x000F); //easter
2966      u16MaxMinDiff = 0;
2967      u16WidthAvg = 0;
2968 
2969      s8MinIndex = DBK_WidthPartTop(pInstance, au16WidthDiffHW, &u16MaxMinDiff, &u16WidthAvg,isSceneChangeY,&u8WidthConfidence);
2970 
2971      if (s8MinIndex == -1)
2972      {
2973          s8MinIndex = DBK_ComputeWidthPart(pInstance, au16WidthDiffHW ,u8LastminIdex, u16WidthAvg,u8WidthConfidence);
2974      }
2975 
2976      if (s8MinIndex >= 0 && u8Width != 32)//if (width == 32) //no such pattern yet, only artificials
2977      {
2978          u8Width = s8MinIndex + 4;
2979      }
2980      else
2981      {
2982          u8Width = 8;//can't find any possible width set to default value
2983      }
2984 
2985      WidthIIR_H(pInstance, u8Width);
2986      WidthToRegister(pInstance, WidthIIR);
2987      //FeedBackSD2HD(WidthIIR, 0);
2988 
2989      if(log_en==5)
2990      {
2991          //printf("**bforeIIR[%d]********\n",u8Width);
2992          //printf("*********[%d]*********\n",WidthIIR);
2993          //printf("**Confiden[%d]*********\n",u8WidthConfidence);
2994      }
2995 
2996      u8LastminIdex = WidthIIR -4;
2997 
2998      return WidthIIR;
2999  }
Mdrv_PQ_DBK(void * pInstance,MS_BOOL isSceneChangeY,MS_U32 u32MotionValue,MS_U16 u16Width)3000  void Mdrv_PQ_DBK(void *pInstance, MS_BOOL isSceneChangeY,MS_U32 u32MotionValue,MS_U16 u16Width)
3001  {
3002          //DBK Variables
3003      static MS_U16 au16Histogram[MaxWidth] = {0};
3004      static MS_BOOL bDBK_initial = 0;
3005      MS_U8 u8ReadDBKHistAck = 0;
3006      MS_U16 u16Scan_range = 320;
3007      MS_U16 u16Initial_point = 160;
3008      MS_U16 u16End_point = u16Initial_point+u16Scan_range;
3009      static MS_U8 u8CountForSample = 0;
3010      static MS_U8 u8CountForDBK = 0;
3011      static MS_U8 u8Widdth = 8;
3012      MS_U32 u32Strength[paraSize] = {0};
3013      static MS_U32 u32Strength_IIR[paraSize] ={0};
3014      MS_U8 u8MaxBin = 0;
3015      MS_U32 u32TotalStrength = 0;
3016      MS_U32 u32StrengthLPF =0;
3017      MS_U32 u32AvergeStrength = 1;
3018      MS_U16 u16Bin = 0;
3019      static MS_U8 au8MaxBin_pre[6] = {0};
3020      MS_U8 u8Pre_bin = 0;
3021      MS_S8 s8Boundary_left;
3022      MS_U8 u8Boundary_right;
3023      MS_U16 u16GridReport_l = 0;
3024      MS_U16 u16GridReport_m = 0;
3025      MS_U16 u16Non_gridReport_l = 0;
3026      MS_U16 u16Non_gridReport_m = 0;
3027      MS_U32 u32GridReport_sum=0;
3028      MS_U32 u32Non_gridReport_sum=0;
3029      MS_U32 u32Total_Report_sum=0;
3030      MS_U32 u32AvgReport =25000000,u32DiffReport = 5000000;
3031      static MS_U32 u32GridReport_sumIIR=0;
3032      static MS_U32 u32Non_gridReport_sumIIR=0;
3033      double gridReport_percent = 0.0;
3034      double adaptiveRatio_hwReport = 0.0,adjust_step = 0.02;
3035      static double maxbin_thrd = 1.1,percentage_offset = 0.0;
3036      MS_U8 u8DBKGain_hw=0;
3037      MS_U8 u8DBK_coase_TH = 2;
3038      MS_U8 u8DetailGain_hw = 2;
3039      MS_U8 u8STD_Threshold_hw=0;
3040      MS_U32 u32Max_bin_thrd = 0;
3041      double scaleUp_factor_w11 = 1.0;
3042      static MS_U32 u32StaticValueTemp=0,u32StaticCount=0;
3043      int log_en = 0;
3044      //log_en = (MS_U32)MApi_XC_R2BYTEMSK(REG_SC_BK30_01_L, 0x000F); //easter
3045 
3046     if (isSceneChangeY)
3047     {
3048         u8CountForDBK = 0;
3049         for(u16Bin=0;u16Bin<6;u16Bin++)
3050         {
3051          au8MaxBin_pre[u16Bin] = 0;
3052         }
3053     }
3054     if(u32MotionValue ==0 && u32StaticValueTemp==0)
3055     {
3056         u32StaticCount++;
3057     }
3058     else
3059     {
3060         u32StaticCount = 0;
3061     }
3062     u32StaticValueTemp = u32MotionValue;
3063 
3064     if((u16End_point+u16Scan_range*u8CountForSample) > u16Width)
3065     {
3066         u8CountForSample = 0;
3067     }
3068 
3069 
3070      u16Initial_point =u16Initial_point + u16Scan_range*u8CountForSample;
3071      u16End_point = u16End_point+u16Scan_range*u8CountForSample;
3072 
3073     if ( (u8CountForDBK == 0)  )// Read Histogram
3074     {
3075         u8ReadDBKHistAck = readHistogramPart(pInstance, au16Histogram, u8CountForDBK, u16Initial_point, u16End_point);
3076         bDBK_initial = bDBK_initial | true;
3077         if((u8ReadDBKHistAck==1)&& (u32StaticCount < 15))
3078         {
3079             u8Widdth = DBK_Width_Detect(pInstance, isSceneChangeY);
3080         }
3081 
3082         if (u8ReadDBKHistAck)
3083         {
3084             u8CountForDBK++;
3085         }
3086     }
3087     else if  (u8CountForDBK == 1)
3088     {
3089         if ( bDBK_initial == 1)
3090         {
3091             for (u16Bin = u16Initial_point; u16Bin <u16End_point; u16Bin++)
3092             {
3093                 u32Strength[(u16Bin)%u8Widdth] += (au16Histogram[u16Bin]>>8);
3094             }
3095 
3096             for (u16Bin = 0; u16Bin <u8Widdth; u16Bin++)
3097             {
3098                 u32Strength_IIR[u16Bin] = (6*u32Strength_IIR[u16Bin] + 2*u32Strength[u16Bin])>>3;
3099                 u32TotalStrength += u32Strength_IIR[u16Bin];
3100             }
3101 
3102             if(u8Widdth==8)
3103             {
3104                 maxbin_thrd = 1.1;
3105             }
3106             else
3107             {
3108                 maxbin_thrd = 1.0;
3109             }
3110 
3111             u32AvergeStrength = max(u32TotalStrength/u8Widdth, 1);
3112             u32Max_bin_thrd = u32AvergeStrength*(maxbin_thrd);
3113 
3114             if(log_en == 1)
3115             {
3116                 //printf(" ===== states iir start=======\n");
3117                 for(u16Bin = 0; u16Bin <u8Widdth; u16Bin++)
3118                 {
3119                     //printf(" %d = %05d\n",u16Bin,u32Strength_IIR[u16Bin]);
3120                 }
3121                 //printf(" ===== states iir end=======\n");
3122             }
3123 
3124             for (u16Bin = 0; u16Bin < u8Widdth; u16Bin++)
3125             {
3126                 if (u16Bin == 0)
3127                 {
3128                     u32StrengthLPF = (2*u32Strength_IIR[u16Bin]+ u32Strength_IIR[u16Bin+1] + u32Strength_IIR[u8Widdth-1])>>2;
3129                 }
3130                 else if (u16Bin == u8Widdth-1)
3131                 {
3132                     u32StrengthLPF = (2*u32Strength_IIR[u16Bin]+ u32Strength_IIR[u16Bin-1] + u32Strength_IIR[0])>>2;
3133                 }
3134                 else
3135                 {
3136                     u32StrengthLPF = (2*u32Strength_IIR[u16Bin]+ u32Strength_IIR[u16Bin-1] + u32Strength_IIR[u16Bin+1])>>2;
3137                 }
3138 
3139             //if (str3pt >  maxStr)
3140             //{
3141             //    maxStr = str3pt;
3142             //}
3143 
3144                 if(log_en ==1)
3145                 {
3146                     //printf("u32StrengthLPF:%d u32Max_bin_thrd:%d\n",u32StrengthLPF,u32Max_bin_thrd);
3147                 }
3148 
3149                 if (u32StrengthLPF >  u32Max_bin_thrd)
3150                 {
3151                     u32Max_bin_thrd = u32StrengthLPF;
3152                     au8MaxBin_pre[u8CountForSample] = u16Bin;
3153                 }
3154                 else if(u32Strength_IIR[u16Bin] > u32Max_bin_thrd)
3155                 {
3156                     u32Max_bin_thrd = u32Strength_IIR[u16Bin];
3157                     au8MaxBin_pre[u8CountForSample] = u16Bin;
3158                 }
3159                 else
3160                 {
3161                     u8Pre_bin = (u8CountForSample-1<0)?u8CountForSample:(u8CountForSample-1);
3162                     au8MaxBin_pre[u8CountForSample] = au8MaxBin_pre[u8Pre_bin];
3163                 }
3164             }
3165 
3166             for(u16Bin = 0;u16Bin<(u8CountForSample+1);u16Bin++)
3167             {
3168                 u8MaxBin+= au8MaxBin_pre[u16Bin];
3169                 if(log_en ==1)
3170                 {
3171                     //printf("au8MaxBin_pre[%d]:%d u8CountForSample:%d\n",u16Bin,au8MaxBin_pre[u16Bin],u8CountForSample);
3172                 }
3173             }
3174 
3175             u8MaxBin = u8MaxBin/(u8CountForSample+1);
3176             u32AvergeStrength = max(u32TotalStrength/u8Widdth, 1);
3177 
3178             if(u8Widdth%4 == 0)
3179             {
3180                  s8Boundary_left = u8MaxBin - u8Widdth/4;
3181                  u8Boundary_right = u8MaxBin + u8Widdth/4;
3182             }
3183             else if(u8Widdth%4 == 1)
3184             {
3185                  s8Boundary_left = u8MaxBin - u8Widdth/4;
3186                  u8Boundary_right = u8MaxBin + u8Widdth/4;
3187             }
3188             else if(u8Widdth%4 == 2)
3189             {
3190                  s8Boundary_left = u8MaxBin - (u8Widdth/4);
3191                  u8Boundary_right = u8MaxBin + (u8Widdth/4+1);
3192             }
3193             else
3194             {
3195                  s8Boundary_left = u8MaxBin - (u8Widdth/4+1);
3196                  u8Boundary_right = u8MaxBin + (u8Widdth/4+1);
3197             }
3198 
3199             if(s8Boundary_left < 0)
3200             {
3201                  s8Boundary_left = s8Boundary_left+u8Widdth;
3202             }
3203 
3204             if(u8Boundary_right >=u8Widdth)
3205             {
3206                  u8Boundary_right = u8Boundary_right -u8Widdth;
3207             }
3208 
3209             if(log_en ==5)
3210             {
3211              //printf("=====Final Width:[%d]=====\n",u8Widdth);
3212              //printf("=====Motion :[%u]=====\n",u32MotionValue);
3213              //printf("===u32StaticCount:%d===\n",u32StaticCount);
3214             }
3215 
3216              MApi_XC_W2BYTEMSK(REG_SC_BK0C_29_L, s8Boundary_left+(u8Boundary_right<<8), 0x1F1F);
3217              MApi_XC_W2BYTEMSK(REG_SC_BK0C_28_L, u8Widdth, 0x1F1F);
3218 
3219             if(u8Widdth == 11)
3220             {
3221              scaleUp_factor_w11 = 1.2;
3222             }
3223             else
3224             {
3225              scaleUp_factor_w11 = 1.0;
3226             }
3227 
3228             u16Non_gridReport_l = MApi_XC_R2BYTEMSK(REG_SC_BK0C_60_L, 0xFFFF);
3229             u16Non_gridReport_m = MApi_XC_R2BYTEMSK(REG_SC_BK0C_61_L, 0xFFFF);
3230             u16GridReport_l = MApi_XC_R2BYTEMSK(REG_SC_BK0C_68_L, 0xFFFF);
3231             u16GridReport_m = MApi_XC_R2BYTEMSK(REG_SC_BK0C_69_L, 0xFFFF);
3232             u32GridReport_sum= (u16GridReport_l + (u16GridReport_m<<16));
3233             u32Non_gridReport_sum = (u16Non_gridReport_l + (u16Non_gridReport_m<<16));
3234             u32GridReport_sumIIR = (6*u32GridReport_sumIIR + 2*u32GridReport_sum)>>3;
3235             u32Non_gridReport_sumIIR = (6*u32Non_gridReport_sumIIR + 2*u32Non_gridReport_sum)>>3;
3236             gridReport_percent = (float)u32GridReport_sumIIR/(float)(u32GridReport_sumIIR+u32Non_gridReport_sumIIR);
3237             gridReport_percent = gridReport_percent*scaleUp_factor_w11;
3238             u32Total_Report_sum = u32GridReport_sumIIR+u32Non_gridReport_sumIIR;
3239 
3240             if((gridReport_percent > 0.5) && (u32Total_Report_sum > u32AvgReport) && ((u32GridReport_sumIIR - u32Non_gridReport_sumIIR) > u32DiffReport))
3241             {//for black white water flow ,both of blocky and detail are strong which can't be distinguish by the ratio of blocky/total
3242                 percentage_offset+= adjust_step;
3243                 //printf("percentage_offset:%d\n",(int)(percentage_offset*100));
3244             }
3245             else
3246             {
3247                 percentage_offset-= adjust_step;
3248             }
3249             percentage_offset = minmax((int)(percentage_offset*100.0), 0, 20);
3250             percentage_offset/=100.0;
3251 
3252             if(gridReport_percent > 0.7)
3253             {
3254              adaptiveRatio_hwReport = 3.6*(gridReport_percent+percentage_offset) -0.92;
3255             }
3256             else if(gridReport_percent > 0.5)
3257             {
3258              adaptiveRatio_hwReport = 6.9*(gridReport_percent+percentage_offset) -3.4;
3259             }
3260             else
3261             {
3262              adaptiveRatio_hwReport = 0;
3263             }
3264 
3265 
3266 
3267             u8DetailGain_hw = 76.503*(gridReport_percent+percentage_offset) - 37.154;
3268             u8DBK_coase_TH = 20.0*(gridReport_percent+percentage_offset) - 8.0;
3269 
3270             if(log_en ==1)
3271             {
3272              //printf(" Max bin :%05d width:%5d u32AvergeStrength:%5d\n", u8MaxBin,u8Widdth,u32AvergeStrength);
3273              //printf("left:%5d right:%5d\n",s8Boundary_left,u8Boundary_right);
3274              //printf("total_bin:%d u16Width:%d\n",u32TotalStrength,u16Width);
3275              //printf("grid_report :%x non-grid report:%x bin:%d\n",u32GridReport_sum,u32Non_gridReport_sum,u8MaxBin);
3276              //printf("gridReport_percent:%d u32GridReport_sumIIR:%d u32Non_gridReport_sumIIR:%d\n",(int)(100*gridReport_percent),u32GridReport_sumIIR,u32Non_gridReport_sumIIR);
3277             }
3278              //int strDiff = max( 0, (maxStr-avgStr) );
3279              //adaptiveRatio_cur = (float)(strDiff)/(float)avgStr;
3280              //adaptiveRatio = adaptiveRatio_cur;
3281              //int binCnt = 1;
3282              //old method
3283              //adaptiveRatioIIR = (adaptiveRatioIIR*13 + adaptiveRatio*3 ) / 16;
3284              //dbkGain = (int)(8 + 8*adaptiveRatioIIR);
3285              //detailGain = max(6-(int)6*adaptiveRatioIIR, 0);
3286              //STD_Th = minmax((int)(63*adaptiveRatioIIR), 0x3F, 0xFF);
3287 
3288              //**new method with HW Accelerator**
3289             u8DBKGain_hw = (int)(8 + 8*adaptiveRatio_hwReport);
3290             u8DBK_coase_TH = minmax(u8DBK_coase_TH, 0x02, 0x06);
3291             u8DetailGain_hw = minmax((int)(u8DetailGain_hw), 0x02, 0x0A);
3292             u8STD_Threshold_hw = minmax((int)(63*adaptiveRatio_hwReport), 0x3F, 0xFF);
3293             u8DBKGain_hw = minmax(u8DBKGain_hw, 0x8, 0xF);
3294 
3295             if(u8Widdth == 21 ||u8Widdth == 16)//Scaling up SD2HD
3296             {
3297              u8STD_Threshold_hw = 0xBF ;
3298              u8DBKGain_hw = 0x0F;
3299              u8DBK_coase_TH = 0x0A;
3300              u8DetailGain_hw = 0x0F;
3301              gridReport_percent = 0.7;
3302             }
3303 
3304 
3305             if(log_en ==1)
3306             {
3307              //printf("adaptiveRatio_hwReport:[%3d] \n",(int)(adaptiveRatio_hwReport*64));
3308              //printf("u8DBKGain_hw:[%3d] u8DBK_coase_TH:[%3d]\n",u8DBKGain_hw,u8DBK_coase_TH);
3309              //printf("u8DetailGain_hw:[%3d]\n ",u8DetailGain_hw);
3310              //printf("u8STD_Threshold_hw:[%3d]\n ",u8STD_Threshold_hw);
3311             }
3312 
3313 
3314              NoiseMaskingControl(pInstance, (int)(gridReport_percent*64), isSceneChangeY);
3315              MApi_XC_W2BYTEMSK(REG_SC_BK0C_11_L, u8DBKGain_hw, 0x000F); //DBK Gain
3316              MApi_XC_W2BYTEMSK(REG_SC_BK0C_10_L, u8STD_Threshold_hw<<8, 0xFF00); //DBK STD threshold
3317              MApi_XC_W2BYTEMSK(REG_SC_BK0C_40_L, (u8DBK_coase_TH<<8), 0xFF00); //DBK coase threshold
3318              MApi_XC_W2BYTEMSK(REG_SC_BK0C_42_L, (u8DetailGain_hw<<12), 0xF000); //blockness gain
3319              MApi_XC_W2BYTEMSK(REG_SC_BK60_14_L, 0x000F, 0x000F); //User coring threshold
3320              MApi_XC_W2BYTEMSK(REG_SC_BK60_30_L, 0x0008, 0x0008); //cont3
3321              MApi_XC_W2BYTEMSK(REG_SC_BK0C_41_L, 0x0000, 0x0001); //hd mode disable
3322              MApi_XC_W2BYTEMSK(REG_SC_BK0C_71_L, 0x0000, 0x00C0); //switch to Y 5tap filter
3323              MApi_XC_W2BYTEMSK(REG_SC_BK0C_72_L, 0x0002, 0x0006); //smooth step 1
3324              MApi_XC_W2BYTEMSK(REG_SC_BK0C_02_L, 0x0002, 0x0006); //switch to C 3tap filter
3325              MApi_XC_W2BYTEMSK(REG_SC_BK0C_42_L, 0x0904, 0x0F87); //blockness extend disable/switch pulse filter
3326              if(u8Widdth == 21 ||u8Widdth == 16)//Scaling up SD2HD
3327              {
3328                  MApi_XC_W2BYTEMSK(REG_SC_BK0C_41_L, 0x0001, 0x0001); //hd mode enable
3329                  MApi_XC_W2BYTEMSK(REG_SC_BK0C_71_L, 0x00C0, 0x00C0); //switch to Y 7tap filter
3330                  MApi_XC_W2BYTEMSK(REG_SC_BK0C_02_L, 0x0006, 0x0006); //switch to C 7tap filter
3331                  MApi_XC_W2BYTEMSK(REG_SC_BK0C_72_L, 0x0000, 0x0006); //smooth step 0
3332                  MApi_XC_W2BYTEMSK(REG_SC_BK0C_42_L, 0x0A86, 0x0F87); //blockness extend enable/switch pulse filter
3333              }
3334 
3335 
3336         }
3337         u8CountForDBK++;
3338         u8CountForSample++;
3339 
3340     }
3341     if (u8CountForDBK >= 2)
3342     {
3343         u8CountForDBK = 0;
3344     }
3345 }
3346 
3347 #if (Auto_DeBlock_En)
3348 #define ADBW_HD_Loop_Ini_Start 40
3349 #define ADBW_HD_Max_Range_End 1440
3350 #define ADBW_SD_Loop_Ini_Start 20
3351 #define ADBW_SD_Max_Range_End  720
3352 //static MS_U32 Auto_DBK_Width_val = 8;
3353 //static MS_U32 Auto_DBK_Width_val_tmp = 32; // format is xxxxx.xx
3354 //static MS_U32 Pre_DBK_Width_val = 8;
3355 #endif
3356 
3357 static MS_U16 g_u16BK0A_24 = 0;
3358 static MS_U16 g_u16BK0C_10 = 0;
3359 static MS_U16 g_u16BK0C_39 = 0;
3360 static MS_U16 g_u16BK0C_3A = 0;
3361 static MS_U16 g_u16BK0C_40 = 0;
3362 static MS_U16 g_u16BK0C_41 = 0;
3363 static MS_U16 g_u16BK0C_42 = 0;
3364 static MS_U16 g_u16BK26_10 = 0;
3365 static MS_U16 g_u16BK26_11 = 0;
3366 static MS_U16 g_u16BK26_12 = 0;
3367 static MS_U16 g_u16BK26_13 = 0;
3368 static MS_U16 g_u16BK26_18 = 0;
3369 
3370 /******************************************************************************/
3371 ///Film mode control for Any Candence
3372 ///@param bEnable \     IN: Enable/Disable control
3373 /******************************************************************************/
Hal_PQ_FilmMode_AnyCandence_Enable(void * pInstance,MS_BOOL bEnable)3374 void Hal_PQ_FilmMode_AnyCandence_Enable(void *pInstance, MS_BOOL bEnable)
3375 {
3376     MApi_XC_W2BYTEMSK(REG_SC_BK0A_24_L, (bEnable ? (BIT(15)) : 0), BIT(15));
3377     bg_bAnyCandenceEnable = bEnable;
3378 }
3379 
3380 /******************************************************************************/
3381 ///Store the Current value
3382 /******************************************************************************/
Hal_PQ_StoreCurrentValue(void * pInstance)3383 void Hal_PQ_StoreCurrentValue(void *pInstance)
3384 {
3385     //for AnyCandence Film Mode
3386     g_u16BK0A_24 = MApi_XC_R2BYTEMSK(REG_SC_BK0A_24_L, BIT(15));
3387 
3388     //for DBK
3389     g_u16BK0C_10 = MApi_XC_R2BYTE(REG_SC_BK0C_10_L);
3390     g_u16BK0C_39 = MApi_XC_R2BYTE(REG_SC_BK0C_39_L);
3391     g_u16BK0C_3A = MApi_XC_R2BYTE(REG_SC_BK0C_3A_L);
3392     g_u16BK0C_40 = MApi_XC_R2BYTE(REG_SC_BK0C_40_L);
3393     g_u16BK0C_41 = MApi_XC_R2BYTE(REG_SC_BK0C_41_L);
3394     g_u16BK0C_42 = MApi_XC_R2BYTE(REG_SC_BK0C_42_L);
3395 
3396     //for DMS
3397     g_u16BK26_10 = MApi_XC_R2BYTE(REG_SC_BK26_10_L);
3398     g_u16BK26_11 = MApi_XC_R2BYTE(REG_SC_BK26_11_L);
3399     g_u16BK26_12 = MApi_XC_R2BYTE(REG_SC_BK26_12_L);
3400     g_u16BK26_13 = MApi_XC_R2BYTE(REG_SC_BK26_13_L);
3401     g_u16BK26_18 = MApi_XC_R2BYTE(REG_SC_BK26_18_L);
3402 }
3403 
3404 
3405 /******************************************************************************/
3406 ///Store the Current value
3407 /******************************************************************************/
Hal_PQ_ReStoreToDefault(void * pInstance)3408 void Hal_PQ_ReStoreToDefault(void *pInstance)
3409 {
3410     //for AnyCandence Film Mode
3411     MApi_XC_W2BYTEMSK(REG_SC_BK0A_24_L, g_u16BK0A_24, BIT(15));
3412 
3413     //for DBK
3414     MApi_XC_W2BYTE(REG_SC_BK0C_10_L, g_u16BK0C_10);
3415     MApi_XC_W2BYTE(REG_SC_BK0C_39_L, g_u16BK0C_39);
3416     MApi_XC_W2BYTE(REG_SC_BK0C_3A_L, g_u16BK0C_3A);
3417     MApi_XC_W2BYTE(REG_SC_BK0C_40_L, g_u16BK0C_40);
3418     MApi_XC_W2BYTE(REG_SC_BK0C_41_L, g_u16BK0C_41);
3419     MApi_XC_W2BYTE(REG_SC_BK0C_42_L, g_u16BK0C_42);
3420 
3421     //for DMS
3422     MApi_XC_W2BYTE(REG_SC_BK26_10_L, g_u16BK26_10);
3423     MApi_XC_W2BYTE(REG_SC_BK26_11_L, g_u16BK26_11);
3424     MApi_XC_W2BYTE(REG_SC_BK26_12_L, g_u16BK26_12);
3425     MApi_XC_W2BYTE(REG_SC_BK26_13_L, g_u16BK26_13);
3426     MApi_XC_W2BYTE(REG_SC_BK26_18_L, g_u16BK26_18);
3427 }
3428 /*
3429 #define FPLL_PHASE_VAR_LIMIT    (0x10)          // If phase error variation exceed this, means un-stable
3430 #define FPLL_PRD_THRESHOLD      (0x100)         // The PRD must be smaller than this value to indicate almost finished PRD lock
3431 #define FPLL_PRD_VAR_LIMIT      (0x10)          // If PRD variation exceed this, means un-stable
3432 
3433 static MS_BOOL _Hal_PQ_FrameLockCheck (void *pInstance)
3434 {
3435     MS_U16 u16NewPhase,u16NewPrd;
3436     static MS_U16 _u16OrigPhase,_u16OrigPrd;
3437 
3438     u16NewPrd = MApi_XC_R2BYTEMSK(REG_LPLL_13_L, 0xFFFF);
3439     u16NewPhase = MApi_XC_R2BYTEMSK(REG_LPLL_11_L, 0xFFFF);
3440 
3441     if((u16NewPrd < FPLL_PRD_THRESHOLD) && (abs(_u16OrigPrd - u16NewPrd) < FPLL_PRD_VAR_LIMIT)
3442         && (abs(_u16OrigPhase - u16NewPhase) < FPLL_PHASE_VAR_LIMIT))
3443     {
3444         _u16OrigPhase = u16NewPhase;
3445         _u16OrigPrd = u16NewPrd;
3446         return TRUE;
3447     }
3448     else
3449     {
3450         _u16OrigPhase = u16NewPhase;
3451         _u16OrigPrd = u16NewPrd;
3452         return FALSE;
3453     }
3454 }
3455 */
3456 static MS_BOOL bFakeOutCusEnable = TRUE;
3457 
Hal_PQ_SetFakeOutEnable(void * pInstance,MS_BOOL bEnable)3458 MS_BOOL Hal_PQ_SetFakeOutEnable(void *pInstance, MS_BOOL bEnable)
3459 {
3460     bFakeOutCusEnable = bEnable;
3461     return TRUE;
3462 }
3463 
3464 #define ENABLE_SCALER_DEFEATHERING           0x01   //BK22_7C_L
3465 #define ENABLE_SCALER_DEFLICKERING           0x02
3466 #define ENABLE_SCALER_DEBOUNCING             0x04
3467 #define ENABLE_SCALER_MCNR_DRIVER            0x08
3468 #define ENABLE_SCALER_T12_UCDI_DRIVER        0x10
3469 #define ENABLE_SCALER_NEW_SDHD_DETECT_DRIVER 0x20
3470 #define ENABLE_SCALER_AUTO_DBK_HD_DRIVER     0x40
3471 #define ENABLE_SCALER_AUTO_DBK_DRIVER        0x80
3472 
3473 //-------------------------------------------------------------------------------------------------
3474 /// this function does adaptive tuning periodic
3475 //-------------------------------------------------------------------------------------------------
Hal_PQ_AdaptiveTuning(void * pInstance)3476 void Hal_PQ_AdaptiveTuning(void *pInstance)
3477 {
3478     #if 0   //check driver run time
3479     static MS_U32 u32lastTimeStamp=0;
3480     MS_U32 u32nowTimeStamp;
3481     u32nowTimeStamp = MsOS_GetSystemTime();
3482     if(u32nowTimeStamp-u32lastTimeStamp>16)
3483         printf("====executing time[%d]====\n",(u32nowTimeStamp-u32lastTimeStamp));
3484     u32lastTimeStamp=u32nowTimeStamp;
3485 
3486 
3487     static MS_U8 u8PreviousVcnt = 0;
3488     MS_U8 u8CurrentVcnt;
3489     MS_U8  u8Ctrl,u8MROffset;
3490     MS_U32 u32MotionValue,u32MotionValue2,u32OOSRMC,u32OOSRF,u32ISRNONMV0,u32SmallMoving,u32VerticalMoving,u32MotionValue4;//u32MotionValue3,
3491     MS_U32 u32FeatherSum,u32MotionValue_Sum;
3492     MS_U8 u8MotionValue3;
3493     XC_ApiStatus stXCStatus;
3494     MS_BOOL isSceneChangeY = MDrv_SC_Agate_SceneChangeY(pInstance);
3495     memset(&stXCStatus, 0x00, sizeof(stXCStatus));
3496 
3497     bisHDSource = ( MApi_XC_R2BYTEMSK(REG_SC_BK12_0F_L, 0xFFFF) > 0x700 );
3498 
3499     //
3500     // Check VCnt
3501     //
3502     u8CurrentVcnt = MDrv_SC_read_v_cnt(pInstance);
3503     if (u8PreviousVcnt == u8CurrentVcnt)
3504     {
3505         return;
3506     }
3507     else
3508     {
3509         u8PreviousVcnt = u8CurrentVcnt;
3510     }
3511 
3512     //
3513     // Get adaptive function control
3514     //
3515     u8Ctrl  = MDrv_SC_get_adaptive_ctrl(pInstance);
3516     //u8Ctrl2 = MDrv_SC_get_adaptive_ctrl2();
3517     //u8Ctrl3 = MDrv_SC_get_adaptive_ctrl3();
3518     //
3519     // Get motion value
3520     //
3521     u32MotionValue = MDrv_SC_read_motion_value1(pInstance);
3522     u32MotionValue2 = MDrv_SC_read_motion_value2(pInstance);
3523     u8MotionValue3 = MDrv_SC_read_motion_value3(pInstance);
3524     u32MotionValue4 = MDrv_SC_read_motion_value4(pInstance);
3525     //
3526     // Get UCDi status
3527     //
3528     u32OOSRMC = MDrv_SC_T12_UC_InSearchRange_MvComfirm(pInstance);
3529     u32OOSRF = MDrv_SC_T12_UC_InSearchRange_Feather(pInstance);
3530     u32ISRNONMV0 = MDrv_SC_T12_UC_InSearchRange_nonMv0Comfirm(pInstance);
3531     u32SmallMoving = MDrv_SC_T12_UC_SmallMoving(pInstance);
3532     u32VerticalMoving = MDrv_SC_A5_KFC_IsVerticalMoving(pInstance);
3533 
3534     //u32HorizontalMoving = MDrv_SC_A5_IsHorizontalMoving(pInstance);
3535     //u32TotalFeather = MDrv_SC_A5_TotalFeather(pInstance);
3536     u32FeatherSum = MDrv_SC_TotalFeather_Sum(pInstance);
3537     //u32TotalMvComfirm = MDrv_SC_A5_TotalMvComfirm(pInstance);
3538     //u8ColorSum = MDrv_SC_A5_ColorPixelSum(pInstance);
3539     //u32uComplex = MDrv_SC_A5_ComplexSum(pInstance);
3540     u8MROffset = MDrv_SC_UC_InSearchRange_MR_Offset(pInstance);
3541     u32MotionValue_Sum = u32MotionValue2 + u8MROffset;
3542 
3543     // Adaptive functions
3544     if(_u16PQPre_SrcType != _u16PQSrcType_DBK_Detect[PQ_MAIN_WINDOW])            //For Auto_DBK SW driver used
3545     {
3546         _u16PQPre_SrcType = _u16PQSrcType_DBK_Detect[PQ_MAIN_WINDOW];
3547         bdataread = true;
3548     }
3549 
3550     /******************************************************************************/
3551     ///Auto DBK Driver
3552     /******************************************************************************/
3553     if ((u8Ctrl & ENABLE_SCALER_AUTO_DBK_DRIVER)||(u8Ctrl & ENABLE_SCALER_AUTO_DBK_HD_DRIVER))   //New Add for AUTO DBK Test
3554     {
3555         if(MApi_XC_GetStatus(&stXCStatus, MAIN_WINDOW) == FALSE)
3556         {
3557             printf("MApi_XC_GetStatus failed because of InitData wrong, please update header file and compile again\n");
3558         }
3559          Mdrv_PQ_DBK(pInstance,isSceneChangeY,u32MotionValue4,stXCStatus.stCropWin.width);
3560     }
3561 
3562     /******************************************************************************/
3563     ///UCDi Driver
3564     /******************************************************************************/
3565 
3566     if (u8Ctrl & ENABLE_SCALER_T12_UCDI_DRIVER)
3567     {
3568         MDrv_SC_T12_UCDi_FeatherValueGain(pInstance, u32OOSRMC, u32OOSRF, u32ISRNONMV0);
3569         MDrv_SC_T12_UCDi_VerticalMovingUdRatio(pInstance, u32OOSRMC, u32OOSRF, u32ISRNONMV0);
3570         MDrv_SC_T12_UCDi_ComplexOffset(pInstance, u32OOSRMC, u32OOSRF, u32ISRNONMV0);
3571         MDrv_SC_T12_UCDi_W2SadGain(pInstance, u32OOSRMC, u32OOSRF, u32ISRNONMV0);
3572         MDrv_SC_T12_KFC_8bitMotionGain(pInstance, u32SmallMoving);
3573         MDrv_SC_T12_UCDi_EODiWGain(pInstance, u32OOSRMC, u32OOSRF, u32ISRNONMV0);
3574         MDrv_SC_T12_UCDi_HistoryRatio(pInstance, u32OOSRMC, u32OOSRF, u32ISRNONMV0);
3575         MDrv_SC_A5_UCDi_OP1_HisWeight(pInstance, u32MotionValue_Sum,u32FeatherSum,u8MotionValue3);
3576         MDrv_SC_A5_KFC_EODiW(pInstance, u32VerticalMoving);
3577         MDrv_SC_SST_Ctrl(pInstance, u32FeatherSum, bisHDSource);
3578     }
3579 
3580     /******************************************************************************/
3581     ///UCNR Driver
3582     /******************************************************************************/
3583 
3584     if (u8Ctrl & ENABLE_SCALER_MCNR_DRIVER)
3585     {
3586         MDrv_SC_mcnr_driver(pInstance, u32MotionValue2, bisHDSource);
3587     }
3588 
3589     /******************************************************************************/
3590     ///SDHD Driver
3591     /******************************************************************************/
3592 
3593     if (u8Ctrl & ENABLE_SCALER_NEW_SDHD_DETECT_DRIVER)
3594     {
3595         MDrv_SC_NEW_SDHD_DETECT_driver(pInstance);
3596     }
3597 
3598 
3599     /******************************************************************************/
3600     ///MADi Driver
3601     /******************************************************************************/
3602 
3603     if (u8Ctrl & ENABLE_SCALER_DEFEATHERING)
3604     {
3605         MDrv_SC_de_feathering(pInstance, u32MotionValue);
3606     }
3607 
3608     if (u8Ctrl & ENABLE_SCALER_DEFLICKERING)
3609     {
3610         MDrv_SC_de_flickering(pInstance, u32MotionValue);
3611     }
3612 
3613     if (u8Ctrl & ENABLE_SCALER_DEBOUNCING)
3614     {
3615         MDrv_SC_de_bouncing(pInstance, u32MotionValue2);
3616         MDrv_SC_A5_UCDi_OP1_HisWeight(pInstance, u32MotionValue_Sum,u32FeatherSum,u8MotionValue3);
3617     }
3618 
3619     if( (MApi_XC_R2BYTEMSK(REG_SC_BK0A_10_L, BIT(14)) == BIT(14))
3620             || (MApi_XC_R2BYTEMSK(REG_SC_BK0A_10_L, BIT(15)) == BIT(15)) )
3621     {
3622         if(!_Hal_PQ_FrameLockCheck()) // Make not frame LOCKED then turn off FilmMode
3623         {
3624             MApi_XC_W2BYTEMSK(REG_SC_BK0A_1E_L, BIT(1), BIT(1)); //hardware film mode DISABLE
3625             MApi_XC_W2BYTEMSK(REG_SC_BK0A_21_L, BIT(1)|BIT(2), BIT(1) | BIT(2)); //software film mode DISABLE
3626         }
3627         else    // Make sure frame LOCKED then turn on FilmMode
3628         {
3629             if(bFakeOutCusEnable)
3630             {
3631                 MApi_XC_W2BYTEMSK(REG_SC_BK0A_1E_L, 0, BIT(1)); //hardware film mode ENABLE
3632                 MApi_XC_W2BYTEMSK(REG_SC_BK0A_21_L, 0, BIT(1) | BIT(2)); //software film mode ENABLE
3633             }
3634             else
3635             {
3636                 MApi_XC_W2BYTEMSK(REG_SC_BK0A_1E_L, BIT(1), BIT(1)); //hardware film mode DISABLE
3637                 MApi_XC_W2BYTEMSK(REG_SC_BK0A_21_L, BIT(1)|BIT(2), BIT(1) | BIT(2)); //software film mode DISABLE
3638             }
3639         }
3640     }
3641     #endif
3642 }
3643 
3644