xref: /utopia/UTPA2-700.0.x/modules/xc/drv/ace/MsAce.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 #define _MSACE_C_
79 
80 #ifdef MSOS_TYPE_LINUX_KERNEL
81 #include <linux/string.h>
82 #else
83 #include <string.h>
84 #endif
85 #include "apiXC_Ace.h"
86 #include "mhal_ace.h"
87 #include "ace_hwreg_utility2.h"
88 #include "MsAce_LIB.h"
89 #include "drvACE.h"
90 #ifdef MSOS_TYPE_LINUX
91 #include <sys/ioctl.h>
92 #include <unistd.h>
93 #include <fcntl.h> // O_RDWR
94 #endif
95 #include "../xc/mdrv_xc_io.h"
96 #include "UFO.h"
97 
98 //--------------------------------------------------------------------------------------------------
99 // msACE version history:
100 // v100(2008-04-16): Add version infromation
101 //       Change internal contrast/RGB gain variable to WORD(12bits)
102 //       Add new function for adjust contrast/RGB gain
103 //          void msAdjustVideoContrast_10Bits( BOOL bScalerWin, WORD wContrast );
104 //          void msAdjustVideoRGB_10Bits( BOOL bScalerWin, WORD ucRCon, WORD ucGCon, WORD ucBCon);
105 //          void msAdjustPCContrast_10Bits( BOOL bScalerWin, WORD wContrast );
106 //          void msAdjustPCRGB_10Bits( BOOL bScalerWin, WORD wRed, WORD wGreen, WORD wBlue );
107 // v101(2008-04-16): Modify msACE_GetVersion()
108 // v102(2008-09-04):
109 //  1.Fine tune color correction control.
110 //  2.Fine tune sRGB control
111 // v103(2008-09-08):
112 //  1.Add new function for get more infromation from ACE library
113 //  2.Seperate chip relative code to another file.
114 // v104(2008-09-17):
115 //  1.Use WriteByteMask to access REG_ADDR_CM_CTL_MAIN
116 //  2.Fix msACE_GetInfo() uncall warning.
117 // v105(2009-07-14):
118 //  1.Fine tune the define in LIB_Group.
119 //  2.Add new group for external chip.
120 // v106(2009-10-28):
121 //  1.Add C51 version info.
122 //  2.Add debug-info-YUVtoRGB Matrix selection
123 //  3.Add debug function to get Matrix
124 //  4.Add option for using internal sin/cos function.
125 //  5.Change some function interface to static.
126 // v107(2010-01-26):
127 //  1.Refine Sine&Cosine function: Using lookup table.
128 // v108(2010-04-13):
129 //  1.Enable Sine&Cosine function using lookup table.
130 //  2.Add 2 function: msACE_SetColorMatrixControl(), msACE_SetRBChannelRange()
131 #define MSACE_VERSION   (108)
132 //----------------------------------------------------------------------------------------------------
133 
134 #if defined(__C51__)
135 #define STR_C51_VERSION(x) "C51:"##  #x
136 
137 #define C51_VERSION(x) STR_C51_VERSION(x)
138 
139 char* ace_c51version = C51_VERSION(__C51__);
140 #endif
141 
142 #define MAIN_WINDOW     0
143 #define SUB_WINDOW      1
144 #define CONTRAST_MIN_VALUE 0x400
145 #define CONTRAST_MAX_VALUE 0x7FF
146 
147 typedef short Typ3x3Array[3][3];
148 
149 #if 0
150 typedef struct
151 {
152     WORD wContrast;
153     WORD wRCon;
154     WORD wGCon;
155     WORD wBCon;
156     BYTE ucSaturation;
157     BYTE ucHue;
158     short sContrastRGBMatrix[3][3];
159     short sVideoSatHueMatrix[3][3];
160     short* psColorCorrectionMatrix; // this variable must pointer to a array[3][3]
161     short* psPCsRGBMatrix; // sRGB matrix for PC, this variable must pointer to a array[3][3]
162     short* psYVUtoRGBMatrix; // this variable must pointer to a array[3][3]
163     BYTE bForceYUVtoRGB : 1;
164     BYTE ucYUVtoRGBMatrixSel : 2;
165 }StruAceInfo;
166 #endif
167 
168 /// CFD OSD structure version
169 /// Version 0: Init structure
170 /// Version 1: Add backlight and hue/contrast/saturation valid flag
171 /// Version 2: Add color range and main/sub window.
172 /// Version 3: Add ultra black / white
173 /// Version 4: Add skip picture setting (hue/ contrast/ saturation)
174 /// Version 5: Add color correction matrix
175 #define ACE_OSD_CONTROL_VERSION 5
176 
177 /// Update type
178 typedef enum
179 {
180     /// Update all
181     E_XC_CFD_UPDATE_TYPE_ALL,
182     /// Update OSD only
183     E_XC_CFD_UPDATE_TYPE_OSD_ONLY,
184     /// Max
185     E_XC_CFD_UPDATE_TYPE_MAX
186 } MS_XC_CFD_UPDATE_TYPE;
187 
188 //controls from Panel OSD
189 typedef struct
190 {
191     MS_U32 u32Version;   ///<Version of current structure. Please always set to "ACE_OSD_CONTROL_VERSION" as input
192     MS_U16 u16Length;    ///<Length of this structure, u16Length=sizeof(ST_HAL_PQ_CFD_OSD_CONTROL)
193 
194     MS_U16 u16Hue;
195     MS_U16 u16Saturation;
196     MS_U16 u16Contrast;
197 
198     //default on , not in the document
199     MS_U8  u8OSDUIEn;
200 
201     //Mode 0: update matrix by OSD and color format driver
202     //Mode 1: only update matrix by OSD controls
203     //for mode1 : the configures of matrix keep the same as the values by calling CFD last time
204     MS_U8  u8OSDUIMode;
205 
206     ///
207     MS_BOOL bHueValid;
208     MS_BOOL bSaturationValid;
209     MS_BOOL bContrastValid;
210 
211     MS_BOOL bBacklightValid;
212     MS_U32 u32MinBacklight;
213     MS_U32 u32MaxBacklight;
214     MS_U32 u32Backlight;
215 
216     MS_U8 u8Win;
217     MS_BOOL bColorRangeValid;
218     MS_U8 u8ColorRange; // 0: Auto, 1: Full Range (0~255), 2: Limit Range (16~235)
219 
220     MS_BOOL bUltraBlackLevelValid;
221     MS_U8 u8UltraBlackLevel;
222     MS_BOOL bUltraWhiteLevelValid;
223     MS_U8 u8UltraWhiteLevel;
224 
225     MS_BOOL bSkipPictureSettingValid;
226     MS_BOOL bSkipPictureSetting;
227 
228     MS_BOOL bColorCorrectionValid;
229     MS_S16 s16ColorCorrectionMatrix[32];
230 
231     MS_BOOL bYVUtoRGBMatrixValid;
232     MS_S16 s16YVUtoRGBMatrix[32];
233 
234     MS_U16 u16R;
235     MS_U16 u16G;
236     MS_U16 u16B;
237 
238     MS_BOOL bR;
239     MS_BOOL bG;
240     MS_BOOL bB;
241 } ST_ACE_CFD_OSD_CONTROL;
242 
243 typedef struct
244 {
245     /// Structure version
246     MS_U32 u32Version;
247     /// Structure length
248     MS_U16 u16Length;
249 
250     /// Window (main or sub window)
251     MS_U8 u8Win;
252     /// Input source, reference PQ_INPUT_SOURCE_TYPE in apxXC.h
253     MS_U8 u8InputSource;
254     /// Update type, reference MS_XC_CFD_UPDATE_TYPE
255     MS_U8 u8UpdateType;
256     /// RGB bypass
257     MS_BOOL bIsRgbBypass;
258     /// HD mode
259     MS_BOOL bIsHdMode;
260 } ST_ACE_CFD_FIRE_CONTROL;
261 
262 #if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
263 extern MS_BOOL MDrv_XC_CFDControl(ST_KDRV_XC_CFD_CONTROL_INFO *pstKdrvCFDCtrlInfo);
264 #endif
265 
266 #include "ACE_private.h"
267 #include "utopia_dapi.h"
268 #include "utopia.h"
269 
270 extern void* pu32ACEInst;
271 extern void* pu32ACERes[E_ACE_POOL_ID_MAX];
272 
273 #if 0
274 static StruAceInfo s_AceInfo[XC_ACE_EX_MAX_DEVICE_NUM][2];
275 static BOOL bBypassColorMatrix[XC_ACE_EX_MAX_DEVICE_NUM];
276 #else
277 #define s_AceInfo psACEResPri->stdrvACE.s_AceInfo
278 #define bBypassColorMatrix psACEResPri->stdrvACE.bBypassColorMatrix
279 
280 #endif
281 
282 extern ACE_DRV_HDRinit g_ACEinitParameters;
283 static MS_BOOL _bSetHDRInit=FALSE;
284 static MS_BOOL _bSetGenRead=FALSE;
285 
286 static MS_U16 s16PreRegs[9]={0};
287 static MS_U16 _u16HdrY2RRatio = 2;
288 static MS_U8 _u8PreMatrixCoefficients=0;
289 static MS_U8 _u8PreColorimetry = 0;
290 static MS_U8 _u8PreExtendedColorimetry = 0;
291 #ifdef UFO_XC_HDR
292 #if (UFO_XC_HDR_VERSION == 2)
293 #ifdef MSOS_TYPE_LINUX
294 static MS_S32   _s32FdScaler = -1;
295 #endif
296 #endif
297 #endif
298 ///////////////////////////////////////////////////////////////////////////////////////////////
299 
300 void msAdjustPCContrast_10Bits(void* pInstance, BOOL bScalerWin, WORD wContrast );
301 void msAdjustVideoContrast_10Bits(void* pInstance, BOOL bScalerWin, WORD wContrast );
302 
303 ///////////////////////////////////////////////////////////////////////////////////////////////
msACE_GetVersion(void)304 WORD msACE_GetVersion(void)
305 {
306     return MSACE_VERSION;
307 }
308 
_TransferColorCorrectionOffsetValue(short s)309 static WORD _TransferColorCorrectionOffsetValue(short s)
310 {
311     WORD w;
312 
313     w = s;
314     if( w > 0x7F )
315     {
316         w = s*-1;
317         w |= 0x80;
318     }
319     return w;
320 }
msACE_GetInfo(void * pInstance,BOOL bWindow,EnuACEInfoType ucInfoType)321 WORD msACE_GetInfo(void* pInstance, BOOL bWindow, EnuACEInfoType ucInfoType )
322 {
323     WORD wReturnValue = 0xFFFF;
324     _ACE_GET_VARIABLE();
325 
326     switch( ucInfoType )
327     {
328         case ACE_INFO_TYPE_VERSION:
329             wReturnValue = MSACE_VERSION;
330             break;
331         case ACE_INFO_TYPE_CONTRAST:
332             wReturnValue = s_AceInfo[bWindow].wContrast;
333             break;
334         case ACE_INFO_TYPE_CONTRAST_MIN_VALUE:
335             wReturnValue = CONTRAST_MIN_VALUE;
336             break;
337         case ACE_INFO_TYPE_CONTRAST_MAX_VALUE:
338             wReturnValue = CONTRAST_MAX_VALUE;
339             break;
340         case ACE_INFO_TYPE_R_GAIN:
341             wReturnValue = s_AceInfo[bWindow].wRCon;
342             break;
343         case ACE_INFO_TYPE_G_GAIN:
344             wReturnValue = s_AceInfo[bWindow].wGCon;
345             break;
346         case ACE_INFO_TYPE_B_GAIN:
347             wReturnValue = s_AceInfo[bWindow].wBCon;
348             break;
349         case ACE_INFO_TYPE_SATURATION:
350             wReturnValue = s_AceInfo[bWindow].ucSaturation;
351             break;
352         case ACE_INFO_TYPE_HUE:
353             wReturnValue = s_AceInfo[bWindow].ucHue;
354             break;
355         case ACE_INFO_TYPE_COLOR_CORRECTION_XY_R:
356             if( s_AceInfo[bWindow].psColorCorrectionMatrix != NULL )
357             {
358                 wReturnValue = s_AceInfo[bWindow].psColorCorrectionMatrix[26]*-1;
359                 wReturnValue = ((wReturnValue&0xFF00)>>4)|(wReturnValue&0xFF);
360             }
361             else
362                 wReturnValue = 0;
363             break;
364         case ACE_INFO_TYPE_COLOR_CORRECTION_XY_G:
365             if( s_AceInfo[bWindow].psColorCorrectionMatrix != NULL )
366             {
367                 wReturnValue = s_AceInfo[bWindow].psColorCorrectionMatrix[27];
368                 wReturnValue = ((wReturnValue&0xFF00)>>4)|(wReturnValue&0xFF);
369             }
370             else
371                 wReturnValue = 0;
372             break;
373         case ACE_INFO_TYPE_COLOR_CORRECTION_XY_B:
374             if( s_AceInfo[bWindow].psColorCorrectionMatrix != NULL )
375             {
376                 wReturnValue = s_AceInfo[bWindow].psColorCorrectionMatrix[28]*-1;
377                 wReturnValue = ((wReturnValue&0xFF00)>>4)|(wReturnValue&0xFF);
378             }
379             else
380                 wReturnValue = 0;
381             break;
382         case ACE_INFO_TYPE_COLOR_CORRECTION_OFFSET_R:
383             if( s_AceInfo[bWindow].psColorCorrectionMatrix != NULL )
384             {
385                 wReturnValue = _TransferColorCorrectionOffsetValue(s_AceInfo[bWindow].psColorCorrectionMatrix[29]);
386             }
387             else
388                 wReturnValue = 0;
389             break;
390         case ACE_INFO_TYPE_COLOR_CORRECTION_OFFSET_G:
391             if( s_AceInfo[bWindow].psColorCorrectionMatrix != NULL )
392             {
393                 wReturnValue = _TransferColorCorrectionOffsetValue(s_AceInfo[bWindow].psColorCorrectionMatrix[30]);
394             }
395             else
396                 wReturnValue = 0;
397             break;
398         case ACE_INFO_TYPE_COLOR_CORRECTION_OFFSET_B:
399             if( s_AceInfo[bWindow].psColorCorrectionMatrix != NULL )
400             {
401                 wReturnValue = _TransferColorCorrectionOffsetValue(s_AceInfo[bWindow].psColorCorrectionMatrix[31]);
402             }
403             else
404                 wReturnValue = 0;
405             break;
406 
407         case ACE_INFO_TYPE_COMPILER_VERSION:
408     #if defined(__C51__)
409             wReturnValue = __C51__;
410     #else
411             wReturnValue = 0;
412     #endif
413             break;
414 
415         case ACE_INFO_TYPE_YUV_TO_RGB_MATRIX_SEL:
416             wReturnValue = s_AceInfo[bWindow].ucYUVtoRGBMatrixSel;
417             break;
418 
419         case ACE_INFO_TYPE_IHC_USER_COLOR1:
420         case ACE_INFO_TYPE_IHC_USER_COLOR2:
421         case ACE_INFO_TYPE_IHC_USER_COLOR3:
422         case ACE_INFO_TYPE_IHC_USER_COLOR4:
423         case ACE_INFO_TYPE_IHC_USER_COLOR5:
424         case ACE_INFO_TYPE_IHC_USER_COLOR6:
425         case ACE_INFO_TYPE_IHC_USER_COLOR7:
426         case ACE_INFO_TYPE_IHC_USER_COLOR8:
427         case ACE_INFO_TYPE_IHC_USER_COLOR9:
428         case ACE_INFO_TYPE_IHC_USER_COLOR10:
429         case ACE_INFO_TYPE_IHC_USER_COLOR11:
430         case ACE_INFO_TYPE_IHC_USER_COLOR12:
431         case ACE_INFO_TYPE_IHC_USER_COLOR13:
432         case ACE_INFO_TYPE_IHC_USER_COLOR14:
433         case ACE_INFO_TYPE_IHC_USER_COLOR15:
434         case ACE_INFO_TYPE_IHC_USER_COLOR0:
435             {
436                 MS_U8 u8Val = 0;
437                 XC_ACE_IHC_COLOR_TYPE eIHC;
438                 switch(ucInfoType)
439                 {
440                     case ACE_INFO_TYPE_IHC_USER_COLOR1:
441                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR1;
442                         break;
443                     case ACE_INFO_TYPE_IHC_USER_COLOR2:
444                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR2;
445                         break;
446                     case ACE_INFO_TYPE_IHC_USER_COLOR3:
447                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR3;
448                         break;
449                     case ACE_INFO_TYPE_IHC_USER_COLOR4:
450                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR4;
451                         break;
452                     case ACE_INFO_TYPE_IHC_USER_COLOR5:
453                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR5;
454                         break;
455                     case ACE_INFO_TYPE_IHC_USER_COLOR6:
456                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR6;
457                         break;
458                     case ACE_INFO_TYPE_IHC_USER_COLOR7:
459                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR7;
460                         break;
461                     case ACE_INFO_TYPE_IHC_USER_COLOR8:
462                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR8;
463                         break;
464                     case ACE_INFO_TYPE_IHC_USER_COLOR9:
465                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR9;
466                         break;
467                     case ACE_INFO_TYPE_IHC_USER_COLOR10:
468                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR10;
469                         break;
470                     case ACE_INFO_TYPE_IHC_USER_COLOR11:
471                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR11;
472                         break;
473                     case ACE_INFO_TYPE_IHC_USER_COLOR12:
474                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR12;
475                         break;
476                     case ACE_INFO_TYPE_IHC_USER_COLOR13:
477                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR13;
478                         break;
479                     case ACE_INFO_TYPE_IHC_USER_COLOR14:
480                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR14;
481                         break;
482                     case ACE_INFO_TYPE_IHC_USER_COLOR15:
483                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR15;
484                         break;
485                     case ACE_INFO_TYPE_IHC_USER_COLOR0:
486                         eIHC = E_XC_ACE_INFO_TYPE_IHC_USER_COLOR0;
487                         break;
488                     default:
489                         break;
490                 }
491                 MDrv_ACE_GetIHC(pInstance, bWindow, eIHC, &u8Val);
492                 wReturnValue = u8Val;
493             }
494             break;
495 
496 
497         case ACE_INFO_TYPE_ICC_USER_COLOR1:
498         case ACE_INFO_TYPE_ICC_USER_COLOR2:
499         case ACE_INFO_TYPE_ICC_USER_COLOR3:
500         case ACE_INFO_TYPE_ICC_USER_COLOR4:
501         case ACE_INFO_TYPE_ICC_USER_COLOR5:
502         case ACE_INFO_TYPE_ICC_USER_COLOR6:
503         case ACE_INFO_TYPE_ICC_USER_COLOR7:
504         case ACE_INFO_TYPE_ICC_USER_COLOR8:
505         case ACE_INFO_TYPE_ICC_USER_COLOR9:
506         case ACE_INFO_TYPE_ICC_USER_COLOR10:
507         case ACE_INFO_TYPE_ICC_USER_COLOR11:
508         case ACE_INFO_TYPE_ICC_USER_COLOR12:
509         case ACE_INFO_TYPE_ICC_USER_COLOR13:
510         case ACE_INFO_TYPE_ICC_USER_COLOR14:
511         case ACE_INFO_TYPE_ICC_USER_COLOR15:
512         case ACE_INFO_TYPE_ICC_USER_COLOR0:
513             {
514                 MS_U8 u8Val = 0;
515                 XC_ACE_ICC_COLOR_TYPE eICC;
516                 switch(ucInfoType)
517                 {
518                     case ACE_INFO_TYPE_ICC_USER_COLOR1:
519                         eICC = E_ACE_ICC_USER_COLOR1;
520                         break;
521                     case ACE_INFO_TYPE_ICC_USER_COLOR2:
522                         eICC = E_ACE_ICC_USER_COLOR2;
523                         break;
524                     case ACE_INFO_TYPE_ICC_USER_COLOR3:
525                         eICC = E_ACE_ICC_USER_COLOR3;
526                         break;
527                     case ACE_INFO_TYPE_ICC_USER_COLOR4:
528                         eICC = E_ACE_ICC_USER_COLOR4;
529                         break;
530                     case ACE_INFO_TYPE_ICC_USER_COLOR5:
531                         eICC = E_ACE_ICC_USER_COLOR5;
532                         break;
533                     case ACE_INFO_TYPE_ICC_USER_COLOR6:
534                         eICC = E_ACE_ICC_USER_COLOR6;
535                         break;
536                     case ACE_INFO_TYPE_ICC_USER_COLOR7:
537                         eICC = E_ACE_ICC_USER_COLOR7;
538                         break;
539                     case ACE_INFO_TYPE_ICC_USER_COLOR8:
540                         eICC = E_ACE_ICC_USER_COLOR8;
541                         break;
542                     case ACE_INFO_TYPE_ICC_USER_COLOR9:
543                         eICC = E_ACE_ICC_USER_COLOR9;
544                         break;
545                     case ACE_INFO_TYPE_ICC_USER_COLOR10:
546                         eICC = E_ACE_ICC_USER_COLOR10;
547                         break;
548                     case ACE_INFO_TYPE_ICC_USER_COLOR11:
549                         eICC = E_ACE_ICC_USER_COLOR11;
550                         break;
551                     case ACE_INFO_TYPE_ICC_USER_COLOR12:
552                         eICC = E_ACE_ICC_USER_COLOR12;
553                         break;
554                     case ACE_INFO_TYPE_ICC_USER_COLOR13:
555                         eICC = E_ACE_ICC_USER_COLOR13;
556                         break;
557                     case ACE_INFO_TYPE_ICC_USER_COLOR14:
558                         eICC = E_ACE_ICC_USER_COLOR14;
559                         break;
560                     case ACE_INFO_TYPE_ICC_USER_COLOR15:
561                         eICC = E_ACE_ICC_USER_COLOR15;
562                         break;
563                     case ACE_INFO_TYPE_ICC_USER_COLOR0:
564                         eICC = E_ACE_ICC_USER_COLOR0;
565                         break;
566                     default:
567                         break;
568                 }
569                 MDrv_ACE_GetICC(pInstance, bWindow, eICC, &u8Val);
570                 wReturnValue = u8Val;
571             }
572             break;
573 
574         case ACE_INFO_TYPE_IBC_USER_COLOR1:
575         case ACE_INFO_TYPE_IBC_USER_COLOR2:
576         case ACE_INFO_TYPE_IBC_USER_COLOR3:
577         case ACE_INFO_TYPE_IBC_USER_COLOR4:
578         case ACE_INFO_TYPE_IBC_USER_COLOR5:
579         case ACE_INFO_TYPE_IBC_USER_COLOR6:
580         case ACE_INFO_TYPE_IBC_USER_COLOR7:
581         case ACE_INFO_TYPE_IBC_USER_COLOR8:
582         case ACE_INFO_TYPE_IBC_USER_COLOR9:
583         case ACE_INFO_TYPE_IBC_USER_COLOR10:
584         case ACE_INFO_TYPE_IBC_USER_COLOR11:
585         case ACE_INFO_TYPE_IBC_USER_COLOR12:
586         case ACE_INFO_TYPE_IBC_USER_COLOR13:
587         case ACE_INFO_TYPE_IBC_USER_COLOR14:
588         case ACE_INFO_TYPE_IBC_USER_COLOR15:
589         case ACE_INFO_TYPE_IBC_USER_COLOR0:
590             {
591                 MS_U8 u8Val = 0;
592                 XC_ACE_IBC_COLOR_TYPE eIBC;
593                 switch(ucInfoType)
594                 {
595                     case ACE_INFO_TYPE_IBC_USER_COLOR1:
596                         eIBC = E_ACE_IBC_USER_COLOR1;
597                         break;
598                     case ACE_INFO_TYPE_IBC_USER_COLOR2:
599                         eIBC = E_ACE_IBC_USER_COLOR2;
600                         break;
601                     case ACE_INFO_TYPE_IBC_USER_COLOR3:
602                         eIBC = E_ACE_IBC_USER_COLOR3;
603                         break;
604                     case ACE_INFO_TYPE_IBC_USER_COLOR4:
605                         eIBC = E_ACE_IBC_USER_COLOR4;
606                         break;
607                     case ACE_INFO_TYPE_IBC_USER_COLOR5:
608                         eIBC = E_ACE_IBC_USER_COLOR5;
609                         break;
610                     case ACE_INFO_TYPE_IBC_USER_COLOR6:
611                         eIBC = E_ACE_IBC_USER_COLOR6;
612                         break;
613                     case ACE_INFO_TYPE_IBC_USER_COLOR7:
614                         eIBC = E_ACE_IBC_USER_COLOR7;
615                         break;
616                     case ACE_INFO_TYPE_IBC_USER_COLOR8:
617                         eIBC = E_ACE_IBC_USER_COLOR8;
618                         break;
619                     case ACE_INFO_TYPE_IBC_USER_COLOR9:
620                         eIBC = E_ACE_IBC_USER_COLOR9;
621                         break;
622                     case ACE_INFO_TYPE_IBC_USER_COLOR10:
623                         eIBC = E_ACE_IBC_USER_COLOR10;
624                         break;
625                     case ACE_INFO_TYPE_IBC_USER_COLOR11:
626                         eIBC = E_ACE_IBC_USER_COLOR11;
627                         break;
628                     case ACE_INFO_TYPE_IBC_USER_COLOR12:
629                         eIBC = E_ACE_IBC_USER_COLOR12;
630                         break;
631                     case ACE_INFO_TYPE_IBC_USER_COLOR13:
632                         eIBC = E_ACE_IBC_USER_COLOR13;
633                         break;
634                     case ACE_INFO_TYPE_IBC_USER_COLOR14:
635                         eIBC = E_ACE_IBC_USER_COLOR14;
636                         break;
637                     case ACE_INFO_TYPE_IBC_USER_COLOR15:
638                         eIBC = E_ACE_IBC_USER_COLOR15;
639                         break;
640                     case ACE_INFO_TYPE_IBC_USER_COLOR0:
641                         eIBC = E_ACE_IBC_USER_COLOR0;
642                         break;
643                     default:
644                         break;
645                 }
646                 MDrv_ACE_GetIBC(pInstance, bWindow, eIBC, &u8Val);
647                 wReturnValue = u8Val;
648             }
649             break;
650 
651         case ACE_INFO_TYPE_DUMMY:
652         default:
653             wReturnValue = 0;
654             break;
655     }
656 
657     return wReturnValue;
658 }
659 
660 // matrix used to convert RGB color space to YUV color space for SDTV
661 //code short tSDTVRgb2yuv[3][3] =
662 //{
663 //    {  0x0107,  0x0204,  0x0064 }, // 0.257,  0.504,  0.098
664 //    { -0x0098, -0x012A,  0x01C2 }, // -0.148, -0.291, 0.439
665 //    {  0x01C2, -0x0179, -0x0049 }  // 0.439,  -0.368, -0.071
666 //};
667 
668 // matrix used to convert YUV color space to RGB color space, used for video input for SDTV
669 static code short tSDTVYuv2rgb[3][3] =
670 #if 0
671 {
672     {   0x0662, 0x04A8,  0x0000 }, // 1.596,  1.164, 0
673     {  -0x0341, 0x04A8, -0x0190 }, // -0.813, 1.164, -0.391
674     {   0x0000, 0x04A8,  0x0812 }  // 0,      1.164, 2.018
675 };
676 #else
677 {
678     {   0x0667, 0x04AC,  0x0000 }, // 1.6005,  1.1679, 0
679     {  -0x0343, 0x04AC, -0x0192 }, // -0.8154, 1.1679, -0.3925
680     {   0x0000, 0x04AC,  0x0818 }  // 0,      1.1679, 2.0234
681 };
682 #endif
683 
684 // matrix used to convert RGB color space to YUV color space for HDTV
685 //code short tHDTVRgb2yuv[3][3] =
686 //{
687 //    {  0x00DA,  0x02DC,  0x004A }, // 0.213,  0.715,  0.072
688 //    { -0x0078, -0x0193,  0x020B }, // -0.117, -0.394, 0.511
689 //    {  0x020B, -0x01DB, -0x0030 }  // 0.511,  -0.464, -0.047
690 //};
691 
692 // matrix used to convert YUV color space to RGB color space, used for video input for HDTV
693 static code short tHDTVYuv2rgb[3][3] =
694 #if 0
695 {
696     {   0x072C, 0x04A8,  0x0000 }, // 1.793,  1.164, 0
697     {  -0x0223, 0x04A8, -0x00DA }, // -0.534, 1.164, -0.213
698     {   0x0000, 0x04A8,  0x0876 }  // 0,      1.164, 2.115
699 };
700 #else
701 {
702     {   0x0731, 0x04AC,  0x0000 }, // 1.7978,  1.1679, 0
703     {  -0x0223, 0x04AC, -0x00DB }, // -0.5341, 1.1679, -0.2138
704     {   0x0000, 0x04AC,  0x0879 }  // 0,      1.1679, 2.1181
705 };
706 #endif
CopyMatrix(WORD * pwBufSrc,WORD * pwBufTgt,BYTE ucLen)707 static void CopyMatrix(WORD* pwBufSrc, WORD* pwBufTgt, BYTE ucLen )
708 {
709     BYTE i;
710     for( i = 0; i < ucLen; ++ i )
711     {
712         pwBufTgt[i] = pwBufSrc[i];
713     }
714 }
msACE_GetMatrix(void * pInstance,BOOL bWindow,BYTE ucMatrixId,WORD * pwBuf,BYTE ucBufLen)715 BOOL msACE_GetMatrix(void* pInstance, BOOL bWindow, BYTE ucMatrixId, WORD* pwBuf, BYTE ucBufLen)
716 {
717     BYTE ucCopyLen = 9;
718 
719     _ACE_GET_VARIABLE();
720 
721     // Check parameter ...
722 
723     //if( ucBufLen < ucCopyLen )
724         ucCopyLen = ucBufLen;
725 
726     switch(ucMatrixId)
727     {
728         case ACE_MATRIX_ID__YUV_TO_RGB:
729             if( s_AceInfo[bWindow].ucYUVtoRGBMatrixSel == ACE_YUV_TO_RGB_MATRIX_SDTV )
730             {
731                 CopyMatrix( (WORD*)tSDTVYuv2rgb, pwBuf, ucCopyLen );
732             }
733             else if( s_AceInfo[bWindow].ucYUVtoRGBMatrixSel == ACE_YUV_TO_RGB_MATRIX_HDTV )
734             {
735                 CopyMatrix( (WORD*)tHDTVYuv2rgb, pwBuf, ucCopyLen );
736             }
737             else if( s_AceInfo[bWindow].ucYUVtoRGBMatrixSel == ACE_YUV_TO_RGB_MATRIX_USER )
738             {
739                 if( s_AceInfo[bWindow].psYVUtoRGBMatrix == NULL )
740                     return FALSE;
741                 CopyMatrix( (WORD*)s_AceInfo[bWindow].psYVUtoRGBMatrix, pwBuf, ucCopyLen );
742             }
743             break;
744         case ACE_MATRIX_ID__COLOR_CORRECTION:
745             if( s_AceInfo[bWindow].psColorCorrectionMatrix == NULL)
746                 return FALSE;
747             CopyMatrix( (WORD*)s_AceInfo[bWindow].psColorCorrectionMatrix, pwBuf, ucCopyLen );
748             break;
749 
750         case ACE_MATRIX_ID__SRGB:
751             if( s_AceInfo[bWindow].psPCsRGBMatrix == NULL)
752                 return FALSE;
753             CopyMatrix( (WORD*)s_AceInfo[bWindow].psPCsRGBMatrix, pwBuf, ucCopyLen );
754             break;
755     }
756     return TRUE;
757 }
758 
msACEPCForceYUVtoRGB(void * pInstance,BOOL bWin,BOOL bEnable)759 void msACEPCForceYUVtoRGB(void* pInstance, BOOL bWin, BOOL bEnable )
760 {
761     _ACE_GET_VARIABLE();
762     if( bEnable )
763         s_AceInfo[bWin].bForceYUVtoRGB = 1;
764     else
765         s_AceInfo[bWin].bForceYUVtoRGB = 0;
766 }
msACESelectYUVtoRGBMatrix(void * pInstance,BOOL bWindow,BYTE ucMatrix,short * psUserYUVtoRGBMatrix)767 void msACESelectYUVtoRGBMatrix(void* pInstance, BOOL bWindow, BYTE ucMatrix, short* psUserYUVtoRGBMatrix )
768 {
769     DECLARA_BANK_VARIABLE
770 
771     _ACE_GET_VARIABLE();
772 
773     s_AceInfo[bWindow].ucYUVtoRGBMatrixSel = ucMatrix;
774     if( ucMatrix == ACE_YUV_TO_RGB_MATRIX_USER )
775     {
776 		//printf("Color table User mode\n");
777 
778         if(DISABLE==g_ACEinitParameters.bHDREnable)
779             //s_AceInfo[bWindow].psYVUtoRGBMatrix = psUserYUVtoRGBMatrix;
780             memcpy(s_AceInfo[bWindow].psYVUtoRGBMatrix,psUserYUVtoRGBMatrix,sizeof(s_AceInfo[bWindow].psYVUtoRGBMatrix));
781     }
782     else if( ucMatrix == ACE_YUV_TO_RGB_MATRIX_HDTV )
783     {
784 		//printf("Color table HD mode\n");
785 	    BACKUP_SC_BANK
786 		SET_SC_BANK(BANK_ACE);
787 		if(bWindow==0) // 0: main, 1:sub
788 		{
789 			//printf("Prepare Load Main color table\n");
790 	    	if(ACE_READ_CTL(psACEInstPri->u32DeviceID, REG_ADDR_ACE_R2Y_EQ_SEL,0x04)) // (BK18_6F[2] for main)
791     		{
792 				//printf("Load SD table\n");
793 				if(DISABLE==g_ACEinitParameters.bHDREnable)
794     			    //s_AceInfo[bWindow].psYVUtoRGBMatrix = (short*)tSDTVYuv2rgb;
795                     memcpy(s_AceInfo[bWindow].psYVUtoRGBMatrix,(short*)tSDTVYuv2rgb,sizeof(s_AceInfo[bWindow].psYVUtoRGBMatrix));
796     		}
797 			else
798 			{
799 				//printf("Load HD table\n");
800 				if(DISABLE==g_ACEinitParameters.bHDREnable)
801 				    //s_AceInfo[bWindow].psYVUtoRGBMatrix = (short*)tHDTVYuv2rgb;
802                     memcpy(s_AceInfo[bWindow].psYVUtoRGBMatrix,(short*)tHDTVYuv2rgb,sizeof(s_AceInfo[bWindow].psYVUtoRGBMatrix));
803 			}
804 		}
805 		else
806 		{
807 			//printf("Prepare Load Sub color table\n");
808 	    	if(ACE_READ_CTL(psACEInstPri->u32DeviceID, REG_ADDR_ACE_R2Y_EQ_SEL,0x40))//(BK18_6F[6] for sub)
809     		{
810 				//printf("Load SD table\n");
811 				if(DISABLE==g_ACEinitParameters.bHDREnable)
812     			    //s_AceInfo[bWindow].psYVUtoRGBMatrix = (short*)tSDTVYuv2rgb;
813                     memcpy(s_AceInfo[bWindow].psYVUtoRGBMatrix,(short*)tSDTVYuv2rgb,sizeof(s_AceInfo[bWindow].psYVUtoRGBMatrix));
814     		}
815 			else
816 			{
817 				//printf("Load HD table\n");
818 				if(DISABLE==g_ACEinitParameters.bHDREnable)
819 				    //s_AceInfo[bWindow].psYVUtoRGBMatrix = (short*)tHDTVYuv2rgb;
820                     memcpy(s_AceInfo[bWindow].psYVUtoRGBMatrix,(short*)tHDTVYuv2rgb,sizeof(s_AceInfo[bWindow].psYVUtoRGBMatrix));
821 			}
822 		}
823 	    RECOVER_SC_BANK
824     }
825     else // ACE_YUV_TO_RGB_MATRIX_SDTV
826     {
827 		//printf("Color table SD mode\n");
828 		if(DISABLE==g_ACEinitParameters.bHDREnable)
829             //s_AceInfo[bWindow].psYVUtoRGBMatrix = (short*)tSDTVYuv2rgb;
830             memcpy(s_AceInfo[bWindow].psYVUtoRGBMatrix,(short*)tSDTVYuv2rgb,sizeof(s_AceInfo[bWindow].psYVUtoRGBMatrix));
831     }
832 }
833 //---------------------------------------------------------------------------
834 // Specify the color correction table.
835 //---------------------------------------------------------------------------
msACESetColorCorrectionTable(void * pInstance,BOOL bWindow,short * psColorCorrectionTable)836 void msACESetColorCorrectionTable(void* pInstance, BOOL bWindow, short* psColorCorrectionTable )
837 {
838     _ACE_GET_VARIABLE();
839     if(psColorCorrectionTable!=NULL)
840     {
841         memcpy(s_AceInfo[bWindow].psColorCorrectionMatrix,psColorCorrectionTable,sizeof(MS_S16)*32);
842 
843         ST_ACE_CFD_OSD_CONTROL stAceOSDControl;
844         memset(&stAceOSDControl, 0, sizeof(ST_ACE_CFD_OSD_CONTROL));
845         stAceOSDControl.u32Version = ACE_OSD_CONTROL_VERSION;
846         stAceOSDControl.u16Length = sizeof(ST_ACE_CFD_OSD_CONTROL);
847         stAceOSDControl.bColorCorrectionValid = TRUE;
848         memcpy(stAceOSDControl.s16ColorCorrectionMatrix, psColorCorrectionTable, sizeof(MS_S16)*32);
849         stAceOSDControl.u8OSDUIEn = 1;
850         stAceOSDControl.u8OSDUIMode = 1;
851         stAceOSDControl.u8Win = (bWindow==TRUE)?1:0;
852 
853         ST_KDRV_XC_CFD_CONTROL_INFO stCFDCtrlInfo;
854         memset(&stCFDCtrlInfo, 0, sizeof(ST_KDRV_XC_CFD_CONTROL_INFO));
855         stCFDCtrlInfo.enCtrlType = E_KDRV_XC_CFD_CTRL_SET_OSD;
856         stCFDCtrlInfo.pParam = &stAceOSDControl;
857         stCFDCtrlInfo.u32ParamLen = sizeof(ST_ACE_CFD_OSD_CONTROL);
858 #ifdef UFO_XC_HDR
859 #if (UFO_XC_HDR_VERSION == 2)
860     //_s32FdScaler only defined by UFO_XC_HDR_VERSION == 2
861 #if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
862         if(0 > _s32FdScaler)
863         {
864             _s32FdScaler = open("/dev/scaler", O_RDWR);
865 
866             if(0 > _s32FdScaler)
867             {
868                 printf("Scaler device not opened!!!!\n");
869                 return ;
870             }
871         }
872 
873         if (ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stCFDCtrlInfo))
874         {
875             printf("MDRV_XC_IOC_CFDCONCTRL OSD fail!!!!\n");
876             return ;
877         }
878 
879         if (stCFDCtrlInfo.u16ErrCode != 0)
880         {
881             printf("MDRV_XC_IOC_CFDCONCTRL OSD fail, errCode: %d\n", stCFDCtrlInfo.u16ErrCode);
882         }
883 #endif
884 #if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
885         MDrv_XC_CFDControl(&stCFDCtrlInfo);
886 #endif
887 #endif
888 #endif
889 
890 
891 
892     }
893 }
894 
895 //---------------------------------------------------------------------------
896 // Specify the sRGB table.
897 //---------------------------------------------------------------------------
msACESetPCsRGBTable(void * pInstance,BOOL bWin,short * psPCsRGBTable)898 void msACESetPCsRGBTable(void* pInstance, BOOL bWin, short* psPCsRGBTable )
899 {
900     _ACE_GET_VARIABLE();
901     s_AceInfo[bWin].psPCsRGBMatrix = psPCsRGBTable;
902 }
903 
InitACEVar(void * pInstance)904 void InitACEVar(void* pInstance)
905 {
906     BYTE i;
907     _ACE_GET_VARIABLE();
908 
909     for( i = 0; i < 2; ++ i )
910     {
911         s_AceInfo[i].wRCon = 1024;
912         s_AceInfo[i].wGCon = 1024;
913         s_AceInfo[i].wBCon = 1024;
914         s_AceInfo[i].wContrast = 1024;
915         s_AceInfo[i].ucSaturation = 0x80;
916         s_AceInfo[i].ucHue = 50;
917 
918         s_AceInfo[i].sContrastRGBMatrix[0][0] =
919         s_AceInfo[i].sContrastRGBMatrix[1][1] =
920         s_AceInfo[i].sContrastRGBMatrix[2][2] = 1024;
921         s_AceInfo[i].sContrastRGBMatrix[0][1] =
922         s_AceInfo[i].sContrastRGBMatrix[1][0] =
923         s_AceInfo[i].sContrastRGBMatrix[2][0] =
924         s_AceInfo[i].sContrastRGBMatrix[0][2] =
925         s_AceInfo[i].sContrastRGBMatrix[1][2] =
926         s_AceInfo[i].sContrastRGBMatrix[2][1] = 0;
927 
928         s_AceInfo[i].sVideoSatHueMatrix[0][0] =
929         s_AceInfo[i].sVideoSatHueMatrix[1][1] =
930         s_AceInfo[i].sVideoSatHueMatrix[2][2] = 1024;
931         s_AceInfo[i].sVideoSatHueMatrix[0][1] =
932         s_AceInfo[i].sVideoSatHueMatrix[1][0] =
933         s_AceInfo[i].sVideoSatHueMatrix[2][0] =
934         s_AceInfo[i].sVideoSatHueMatrix[0][2] =
935         s_AceInfo[i].sVideoSatHueMatrix[1][2] =
936         s_AceInfo[i].sVideoSatHueMatrix[2][1] = 0;
937 
938         //psColorCorrectionMatrix change type from a pointer to array,
939         //we init this array by a elementary array instead of zero array
940         s_AceInfo[i].psColorCorrectionMatrix[0] = 0x400;
941         s_AceInfo[i].psColorCorrectionMatrix[4] = 0x400;
942         s_AceInfo[i].psColorCorrectionMatrix[8] = 0x400;
943 
944         //s_AceInfo[i].psColorCorrectionMatrix = NULL;
945         msACESetColorCorrectionTable(pInstance, i, NULL );
946 
947         //s_AceInfo[i].psPCsRGBMatrix = NULL;
948         msACESetPCsRGBTable(pInstance, i, NULL );
949 
950         //s_AceInfo[i].bForceYUVtoRGB = 0;
951         msACEPCForceYUVtoRGB(pInstance, i, FALSE );
952 
953         //s_AceInfo[i].ucYUVtoRGBMatrixSel = ACE_YUV_TO_RGB_MATRIX_SDTV;
954         //s_AceInfo[i].psYVUtoRGBMatrix = (short*)tSDTVYuv2rgb;
955         msACESelectYUVtoRGBMatrix(pInstance, i, ACE_YUV_TO_RGB_MATRIX_SDTV, NULL );
956     }
957 
958 #if defined(__C51__)
959     // For uncall warning...
960     if( i == 0 )
961     {
962         msACE_GetVersion();
963         msACE_GetInfo(pInstance, 0, 0);
964         msAdjustPCContrast_10Bits(pInstance, 0,0);
965         msAdjustPCRGB_10Bits(pInstance, 0, 0, 0, 0 );
966         msAdjustVideoContrast_10Bits(pInstance, 0,0);
967         msAdjustVideoRGB_10Bits(pInstance, 0, 0, 0, 0 );
968         msACE_GetMatrix(pInstance, 0, 0, NULL, 0);
969         msACE_SetColorMatrixControl(pInstance, 0, 0 );
970         msACE_SetRBChannelRange(pInstance, 0, 0 );
971     }
972 #endif
973 }
974 
975 #define ENABLE_SINE_COSINE_USING_LOOKUP_TABLE   1
976 
977 #if( ENABLE_SINE_COSINE_USING_LOOKUP_TABLE ) // sine/cosine use lookup table
978 
sine(BYTE u8Hue)979 static short sine(BYTE u8Hue)
980 {
981     code short tHueToSine[] =
982     {
983         784, // 0
984         772, // 1
985         760, // 2
986         748, // 3
987         736, // 4
988         724, // 5
989         711, // 6
990         698, // 7
991         685, // 8
992         671, // 9
993         658, // 10
994         644, // 11
995         630, // 12
996         616, // 13
997         601, // 14
998         587, // 15
999         572, // 16
1000         557, // 17
1001         542, // 18
1002         527, // 19
1003         511, // 20
1004         496, // 21
1005         480, // 22
1006         464, // 23
1007         448, // 24
1008         432, // 25
1009         416, // 26
1010         400, // 27
1011         383, // 28
1012         366, // 29
1013         350, // 30
1014         333, // 31
1015         316, // 32
1016         299, // 33
1017         282, // 34
1018         265, // 35
1019         247, // 36
1020         230, // 37
1021         212, // 38
1022         195, // 39
1023         177, // 40
1024         160, // 41
1025         142, // 42
1026         124, // 43
1027         107, // 44
1028         89, // 45
1029         71, // 46
1030         53, // 47
1031         35, // 48
1032         17, // 49
1033         0, // 50
1034         -17, // 51
1035         -35, // 52
1036         -53, // 53
1037         -71, // 54
1038         -89, // 55
1039         -107, // 56
1040         -124, // 57
1041         -142, // 58
1042         -160, // 59
1043         -177, // 60
1044         -195, // 61
1045         -212, // 62
1046         -230, // 63
1047         -247, // 64
1048         -265, // 65
1049         -282, // 66
1050         -299, // 67
1051         -316, // 68
1052         -333, // 69
1053         -350, // 70
1054         -366, // 71
1055         -383, // 72
1056         -400, // 73
1057         -416, // 74
1058         -432, // 75
1059         -448, // 76
1060         -464, // 77
1061         -480, // 78
1062         -496, // 79
1063         -512, // 80
1064         -527, // 81
1065         -542, // 82
1066         -557, // 83
1067         -572, // 84
1068         -587, // 85
1069         -601, // 86
1070         -616, // 87
1071         -630, // 88
1072         -644, // 89
1073         -658, // 90
1074         -671, // 91
1075         -685, // 92
1076         -698, // 93
1077         -711, // 94
1078         -724, // 95
1079         -736, // 96
1080         -748, // 97
1081         -760, // 98
1082         -772, // 99
1083         -784, // 100
1084     };
1085     if( u8Hue > 100 )
1086         u8Hue = 100;
1087     return tHueToSine[u8Hue];
1088 }
1089 // cos function
cosine(BYTE u8Hue)1090 static short cosine(BYTE u8Hue)
1091 {
1092     code short tHueToCosine[] =
1093     {
1094         658, // 0
1095         671, // 1
1096         685, // 2
1097         698, // 3
1098         711, // 4
1099         724, // 5
1100         736, // 6
1101         748, // 7
1102         760, // 8
1103         772, // 9
1104         784, // 10
1105         795, // 11
1106         806, // 12
1107         817, // 13
1108         828, // 14
1109         838, // 15
1110         848, // 16
1111         858, // 17
1112         868, // 18
1113         877, // 19
1114         886, // 20
1115         895, // 21
1116         904, // 22
1117         912, // 23
1118         920, // 24
1119         928, // 25
1120         935, // 26
1121         942, // 27
1122         949, // 28
1123         955, // 29
1124         962, // 30
1125         968, // 31
1126         973, // 32
1127         979, // 33
1128         984, // 34
1129         989, // 35
1130         993, // 36
1131         997, // 37
1132         1001, // 38
1133         1005, // 39
1134         1008, // 40
1135         1011, // 41
1136         1014, // 42
1137         1016, // 43
1138         1018, // 44
1139         1020, // 45
1140         1021, // 46
1141         1022, // 47
1142         1023, // 48
1143         1023, // 49
1144         1024, // 50
1145         1023, // 51
1146         1023, // 52
1147         1022, // 53
1148         1021, // 54
1149         1020, // 55
1150         1018, // 56
1151         1016, // 57
1152         1014, // 58
1153         1011, // 59
1154         1008, // 60
1155         1005, // 61
1156         1001, // 62
1157         997, // 63
1158         993, // 64
1159         989, // 65
1160         984, // 66
1161         979, // 67
1162         973, // 68
1163         968, // 69
1164         962, // 70
1165         955, // 71
1166         949, // 72
1167         942, // 73
1168         935, // 74
1169         928, // 75
1170         920, // 76
1171         912, // 77
1172         904, // 78
1173         895, // 79
1174         886, // 80
1175         877, // 81
1176         868, // 82
1177         858, // 83
1178         848, // 84
1179         838, // 85
1180         828, // 86
1181         817, // 87
1182         806, // 88
1183         795, // 89
1184         784, // 90
1185         772, // 91
1186         760, // 92
1187         748, // 93
1188         736, // 94
1189         724, // 95
1190         711, // 96
1191         698, // 97
1192         685, // 98
1193         671, // 99
1194         658, // 100
1195     };
1196     if( u8Hue > 100 )
1197         u8Hue = 100;
1198     return tHueToCosine[u8Hue];
1199 }
1200 
1201 #else
1202 
1203 #define PI  (3.14159265358979323846)
1204 
1205 #if( USE_ACE_INTERNAL_SIN_COS ) // Use user define
_ACE_sin(double f0)1206 static double _ACE_sin( double f0)
1207 {
1208     double  f2, f4;
1209     int     r0;
1210 
1211     f0 *= 6.3661977236758134e-01;
1212     if (f0 < 0)
1213     {
1214         if (f0 < -1073741824.0)
1215         {
1216 
1217             if (f0 < -1.0e+18)
1218                     f0 = 0;
1219 
1220             f0 /= 1073741824.0;
1221             r0 = (int)f0;
1222 
1223             f2 = (double) r0;
1224             f0 -= f2;
1225             f0 *= 1073741824.0;
1226         }
1227 
1228         f2 = f0;
1229         f2 -= 0.5;
1230     }
1231     else
1232     {
1233         if (f0 >= 1073741824.0)
1234         {
1235             if (f0 > 1.0e+18)
1236                 f0 = 0;
1237             f0 /= 1073741824.0;
1238 
1239             r0 = f0;
1240             f2 = (double) r0;
1241             f0 -= f2;
1242             f0 *= 1073741824.0;
1243         }
1244 
1245         f2 = f0;
1246         f2 += 0.5;
1247     }
1248 
1249     r0 = (int) f2;
1250     f2 = (double) r0;
1251     f0 -= f2;
1252     if (r0 & 1)
1253     {
1254         f0 *= f0;
1255         f2 = 6.5659631149794723e-11;
1256         f2 *= f0;
1257         f2 += -6.3866030837918522e-09;
1258         f2 *= f0;
1259         f2 += 4.7108747788181715e-07;
1260         f2 *= f0;
1261         f2 += -2.5202042373060605e-05;
1262         f2 *= f0;
1263         f2 += 9.1926027483942659e-04;
1264         f2 *= f0;
1265         f2 += -2.0863480763352961e-02;
1266         f2 *= f0;
1267         f2 += 2.5366950790104802e-01;
1268         f2 *= f0;
1269         f2 += -1.2337005501361698e+00;
1270         f0 *= f2;
1271         f0 += 1.0;
1272     }
1273     else
1274     {
1275         f4 = f0;
1276         f0 *= f0;
1277         f2 = -6.6880351098114673e-10;
1278         f2 *= f0;
1279         f2 += 5.6921729219679268e-08;
1280         f2 *= f0;
1281         f2 += -3.5988432352120853e-06;
1282         f2 *= f0;
1283         f2 += 1.6044118478735982e-04;
1284         f2 *= f0;
1285         f2 += -4.6817541353186881e-03;
1286         f2 *= f0;
1287         f2 += 7.9692626246167046e-02;
1288         f2 *= f0;
1289         f2 += -6.4596409750624625e-01;
1290         f0 *= f2;
1291         f0 += 1.5707963267948966e+00;
1292         f0 *= f4;
1293     }
1294 
1295     if (r0 & 2)
1296       f0 = -f0;
1297 
1298     return(f0);
1299 }
1300 
_ACE_cos(double f0)1301 static double _ACE_cos(double f0)
1302 {
1303     double f2,f4;
1304     int r0;
1305 
1306     f0 *= 6.3661977236758134e-01;
1307     if (f0 < 0)
1308       f0 = -f0;
1309 
1310     if (f0 > 1073741824.0)
1311     {
1312         if (f0 > 1.0e+18)
1313             f0 = 0;
1314 
1315         f0 /= 1073741824.0;
1316         r0 = f0;
1317         f0 = (double) r0;
1318         f0 -= f2;
1319         f0 *= 1073741824.0;
1320     }
1321 
1322     f2 = f0;
1323     f0 += 0.5;
1324     r0 = (int)f0;
1325     f0 = (double) r0;
1326     f0 -= f2;
1327 
1328     if (r0 & 1)
1329     {
1330         f4 = f0;
1331         f0 *= f0;
1332         f2 = -6.6880351098114673e-10;
1333         f2 *= f0;
1334         f2 += 5.6921729219679268e-08;
1335         f2 *= f0;
1336         f2 += -3.5988432352120853e-06;
1337         f2 *= f0;
1338         f2 += 1.6044118478735982e-04;
1339         f2 *= f0;
1340         f2 += -4.6817541353186881e-03;
1341         f2 *= f0;
1342         f2 += 7.9692626246167046e-02;
1343         f2 *= f0;
1344         f2 += -6.4596409750624625e-01;
1345         f0 *= f2;
1346         f0 += 1.5707963267948966e+00;
1347         f0 *= f4;
1348     }
1349     else
1350     {
1351         f0 *= f0;
1352         f2 = 6.5659631149794723e-11;
1353         f2 *= f0;
1354         f2 += -6.3866030837918522e-09;
1355         f2 *= f0;
1356         f2 += 4.7108747788181715e-07;
1357         f2 *= f0;
1358         f2 += -2.5202042373060605e-05;
1359         f2 *= f0;
1360         f2 += 9.1926027483942659e-04;
1361         f2 *= f0;
1362         f2 += -2.0863480763352961e-02;
1363         f2 *= f0;
1364         f2 += 2.5366950790104802e-01;
1365         f2 *= f0;
1366         f2 += -1.2337005501361698e+00;
1367         f0 *= f2;
1368         f0 += 1.0;
1369     }
1370 
1371     if (r0 & 2)
1372         f0 = -f0;
1373 
1374     return(f0);
1375 }
1376 #define sin     _ACE_sin
1377 #define cos     _ACE_cos
1378 
1379 #endif
1380 
1381 //#define PI  (3.14159265)
1382 // sin function
sine(WORD u16Hue)1383 static short sine(WORD u16Hue)
1384 {
1385     return (short)(sin((u16Hue * PI) / 180.) * 1024.);
1386 }
1387 // cos function
cosine(WORD u16Hue)1388 static short cosine(WORD u16Hue)
1389 {
1390     return (short)(cos((u16Hue * PI) / 180.) * 1024.);
1391 }
1392 #endif
1393 
SetVideoSatHueMatrix(void * pInstance,BOOL bWindow)1394 static void SetVideoSatHueMatrix(void *pInstance, BOOL bWindow )
1395 {
1396     short sTmp;
1397     WORD u16Hue;
1398     _ACE_GET_VARIABLE();
1399 
1400 #if( ENABLE_SINE_COSINE_USING_LOOKUP_TABLE )
1401     u16Hue = s_AceInfo[bWindow].ucHue;
1402 #else
1403     u16Hue = ((s_AceInfo[bWindow].ucHue <= 50) ? (50 - s_AceInfo[bWindow].ucHue) : (360-(s_AceInfo[bWindow].ucHue-50)));
1404 #endif
1405 
1406     sTmp = ((short)s_AceInfo[bWindow].ucSaturation * 8);
1407 
1408     s_AceInfo[bWindow].sVideoSatHueMatrix[2][2] = ((((long)cosine(u16Hue) * sTmp))>>10);
1409     s_AceInfo[bWindow].sVideoSatHueMatrix[0][0] = ((( (long)cosine(u16Hue)* sTmp))>>10);
1410     s_AceInfo[bWindow].sVideoSatHueMatrix[2][0] = ((((long)sine(u16Hue)   * sTmp))>>10);
1411     s_AceInfo[bWindow].sVideoSatHueMatrix[0][2] = (((-(long)sine(u16Hue)  * sTmp))>>10);
1412 
1413     s_AceInfo[bWindow].sVideoSatHueMatrix[1][1] = 1024;
1414     s_AceInfo[bWindow].sVideoSatHueMatrix[0][1] = s_AceInfo[bWindow].sVideoSatHueMatrix[1][0] = s_AceInfo[bWindow].sVideoSatHueMatrix[1][2] = s_AceInfo[bWindow].sVideoSatHueMatrix[2][1] = 0;
1415 }
1416 
SetVideoContrastMatrix(void * pInstance,BOOL bWindow)1417 static void SetVideoContrastMatrix(void *pInstance, BOOL bWindow )
1418 {
1419     _ACE_GET_VARIABLE();
1420     s_AceInfo[bWindow].sContrastRGBMatrix[0][0] = ( (DWORD)s_AceInfo[bWindow].wRCon * (s_AceInfo[bWindow].wContrast)) >> 10;
1421     s_AceInfo[bWindow].sContrastRGBMatrix[1][1] = ( (DWORD)s_AceInfo[bWindow].wGCon * (s_AceInfo[bWindow].wContrast)) >> 10;
1422     s_AceInfo[bWindow].sContrastRGBMatrix[2][2] = ( (DWORD)s_AceInfo[bWindow].wBCon * (s_AceInfo[bWindow].wContrast)) >> 10;
1423 }
1424 
ArrayMultiply(short sFirst[3][3],short sSecond[3][3],short sResult[3][3])1425 static void ArrayMultiply(short sFirst[3][3], short sSecond[3][3], short sResult[3][3])
1426 {
1427     BYTE ucRow, ucCol;
1428 
1429     //go line by line
1430     for(ucRow=0; ucRow!=3; ucRow++)
1431     {
1432         // go column by column
1433         for(ucCol=0; ucCol!=3; ucCol++)
1434         {
1435             sResult[ucRow][ucCol] = (((long)sFirst[ucRow][0] * sSecond[0][ucCol]) +
1436                                      ((long)sFirst[ucRow][1] * sSecond[1][ucCol]) +
1437                                      ((long)sFirst[ucRow][2] * sSecond[2][ucCol])) >> 10;
1438         } // for
1439     } // for
1440 }
1441 
msWriteColorMatrix(void * pInstance,BOOL bWindow,short * psMatrix)1442 static void msWriteColorMatrix(void *pInstance, BOOL bWindow, short* psMatrix )
1443 {
1444 #if defined(UFO_XC_HDR) && defined(UFO_XC_HDR_VERSION) && (UFO_XC_HDR_VERSION == 2) && !defined(ACE_NOT_IN_KERNEL)
1445 #else
1446     BYTE i,j;
1447     BYTE ucAddr;
1448     short sTmp;
1449     _ACE_GET_INST_PRIVATE();
1450     DECLARA_BANK_VARIABLE
1451 
1452     BACKUP_SC_BANK
1453     if( bWindow == MAIN_WINDOW )
1454     {
1455         SET_SC_BANK(BANK_CM_MAIN);
1456         ucAddr = REG_ADDR_CM_MAIN&0xFF;
1457     }
1458     else
1459     {
1460         SET_SC_BANK(BANK_CM_SUB);
1461         ucAddr = REG_ADDR_CM_SUB&0xFF;
1462     }
1463 
1464     for(i=0; i!=3; i++)
1465     {
1466         for(j=0; j!=3; j++)
1467         {
1468             sTmp = psMatrix[i*3+j];
1469             if( sTmp >= 0 )
1470             {
1471                 if( sTmp > 0xfff )
1472                 {
1473                     sTmp = 0xfff;
1474                 }
1475                 //SC_WRITE_2BYTE( ucAddr, sTmp );
1476             }
1477             else
1478             {
1479                 sTmp = sTmp * -1;
1480                 if( sTmp > 0xfff )
1481                 {
1482                     sTmp = 0xfff;
1483                 }
1484                 #ifdef USE_NEW_ACE_MATRIX_RULE
1485                     sTmp = ~sTmp +1;
1486                 #else
1487                 sTmp |= 0x1000;
1488                 #endif
1489 
1490                 //SC_WRITE_2BYTE( ucAddr, sTmp );
1491             }
1492             ACE_WRITE_CM(psACEInstPri->u32DeviceID, ucAddr, sTmp );
1493 
1494             ucAddr += 2;
1495         } // for
1496     } // for
1497 
1498     RECOVER_SC_BANK
1499 #endif
1500 }
1501 
msSetVideoColorMatrix(void * pInstance,BOOL bScalerWin)1502 void msSetVideoColorMatrix(void *pInstance, BOOL bScalerWin )
1503 {
1504     short sResultTmp1[3][3];
1505     short sResultTmp2[3][3];
1506     short* psLastResult;
1507     DECLARA_BANK_VARIABLE
1508     _ACE_GET_VARIABLE();
1509 
1510     U8 i,j;
1511     short ColorMatrix_1[3][3];
1512     short ColorMatrix_2[3][3];
1513 
1514     if( TRUE == bBypassColorMatrix)
1515     {
1516         return ;
1517     }
1518 
1519     BACKUP_SC_BANK
1520 
1521 
1522     // Adjust hue&saturation, and then YUV to RGB
1523     //ArrayMultiply( (Typ3x3Array)(s_AceInfo[bScalerWin].psYVUtoRGBMatrix), s_AceInfo[bScalerWin].sVideoSatHueMatrix, sResultTmp1 );
1524 
1525         for ( i=0;i<3;i++ )
1526         {
1527             for ( j=0;j<3;j++ )
1528             {
1529                ColorMatrix_1[i][j]=*(s_AceInfo[bScalerWin].psYVUtoRGBMatrix +(i*3)+j);;
1530                ColorMatrix_2[i][j]= s_AceInfo[bScalerWin].sVideoSatHueMatrix[i][j];
1531             }
1532         }
1533        ArrayMultiply(ColorMatrix_1,ColorMatrix_2, sResultTmp1 );
1534 
1535 
1536 
1537 
1538     // Do color correction
1539     if(( NULL != s_AceInfo[bScalerWin].psColorCorrectionMatrix )&&(DISABLE==g_ACEinitParameters.bHDREnable))
1540     {
1541       // ArrayMultiply( (Typ3x3Array)(s_AceInfo[bScalerWin].psColorCorrectionMatrix), sResultTmp1, sResultTmp2 );
1542         for ( i=0;i<3;i++ )
1543         {
1544             for ( j=0;j<3;j++ )
1545             {
1546                ColorMatrix_1[i][j]=*(s_AceInfo[bScalerWin].psColorCorrectionMatrix +(i*3)+j);
1547             }
1548          }
1549          ArrayMultiply( ColorMatrix_1, sResultTmp1, sResultTmp2 );
1550 
1551 
1552         // Adjust contrast-RGB
1553         ArrayMultiply( s_AceInfo[bScalerWin].sContrastRGBMatrix, sResultTmp2, sResultTmp1 );
1554 
1555         psLastResult = (short*)sResultTmp1;
1556     }
1557     else // No color correction
1558     {
1559         // Adjust contrast-RGB
1560         ArrayMultiply( s_AceInfo[bScalerWin].sContrastRGBMatrix, sResultTmp1, sResultTmp2 );
1561         psLastResult = (short*)sResultTmp2;
1562     }
1563     if (MDrv_ACE_IsSupportMLoad(pInstance) && (!MDrv_ACE_GetSkipWaitVsync(pInstance, bScalerWin)))
1564     {
1565         MDrv_ACE_WriteColorMatrixBurst(pInstance, bScalerWin, (MS_U16*)psLastResult );
1566     }
1567     else
1568     {
1569         msWriteColorMatrix(pInstance, bScalerWin, psLastResult );
1570     }
1571 
1572 #if defined(UFO_XC_HDR) && defined(UFO_XC_HDR_VERSION) && (UFO_XC_HDR_VERSION == 2) && !defined(ACE_NOT_IN_KERNEL)
1573 #else
1574     if( bScalerWin == MAIN_WINDOW )
1575     {
1576         SET_SC_BANK(BANK_CM_MAIN);
1577         ACE_WRITE_CM_CTL(psACEInstPri->u32DeviceID,  REG_ADDR_CM_CTL_MAIN, ENABLE_CM_CTL1, MASK_CM_CTL ); // Enable color matrix&Change R/B range
1578         //ACE_WRITE_CM_CTL(psACEInstPri->u32DeviceID,  REG_ADDR_CM_CTL_MAIN, 0x35, MASK_CM_CTL ); // Enable color matrix&Change R/B range
1579     }
1580     else // Sub window
1581     {
1582         SET_SC_BANK(BANK_CM_SUB);
1583         ACE_WRITE_CM_CTL(psACEInstPri->u32DeviceID,  REG_ADDR_CM_CTL_SUB, ENABLE_CM_CTL1, MASK_CM_CTL ); // Enable color matrix&Change R/B range
1584         //ACE_WRITE_CM_CTL(psACEInstPri->u32DeviceID,  REG_ADDR_CM_CTL_SUB, 0x35, MASK_CM_CTL ); // Enable color matrix&Change R/B range
1585     }
1586 #endif
1587 
1588     RECOVER_SC_BANK
1589 }
1590 
msAdjustHSC(void * pInstance,BOOL bScalerWin,BYTE ucHue,BYTE ucSaturation,BYTE ucContrast)1591 void msAdjustHSC(void *pInstance, BOOL bScalerWin, BYTE ucHue, BYTE ucSaturation, BYTE ucContrast)
1592 {
1593     _ACE_GET_VARIABLE();
1594 
1595     s_AceInfo[bScalerWin].ucSaturation = ucSaturation;
1596     s_AceInfo[bScalerWin].wContrast = (WORD)ucContrast<<3;
1597     s_AceInfo[bScalerWin].ucHue = ucHue;
1598     SetVideoSatHueMatrix(pInstance, bScalerWin );
1599     SetVideoContrastMatrix(pInstance, bScalerWin );
1600 
1601     msSetVideoColorMatrix(pInstance, bScalerWin );
1602 }
1603 
msAdjustCFDOSDInfo(void * pInstance,BOOL bScalerWin)1604 MS_BOOL msAdjustCFDOSDInfo(void *pInstance, BOOL bScalerWin)
1605 {
1606     _ACE_GET_VARIABLE();
1607 
1608     if(psACEInstPri->u32DeviceID == 1)
1609     {
1610         printf("STB CFD not support SC1 3x3 matrix now!!!!\n");
1611         return FALSE;
1612     }
1613 
1614     ST_ACE_CFD_OSD_CONTROL stAceOSDControl;
1615     memset(&stAceOSDControl, 0, sizeof(ST_ACE_CFD_OSD_CONTROL));
1616     stAceOSDControl.u32Version = ACE_OSD_CONTROL_VERSION;
1617     stAceOSDControl.u16Length = sizeof(ST_ACE_CFD_OSD_CONTROL);
1618     stAceOSDControl.bContrastValid = TRUE;
1619     stAceOSDControl.u16Contrast = s_AceInfo[bScalerWin].wContrast;
1620     stAceOSDControl.bHueValid = TRUE;
1621     stAceOSDControl.u16Hue = s_AceInfo[bScalerWin].ucHue;
1622     stAceOSDControl.bSaturationValid = TRUE;
1623     stAceOSDControl.u16Saturation = s_AceInfo[bScalerWin].ucSaturation;
1624     if(s_AceInfo[bScalerWin].psYVUtoRGBMatrix != NULL)
1625     {
1626         stAceOSDControl.bYVUtoRGBMatrixValid = TRUE;
1627         memcpy(stAceOSDControl.s16YVUtoRGBMatrix, s_AceInfo[bScalerWin].psYVUtoRGBMatrix, sizeof(short)*9);
1628     }
1629     stAceOSDControl.bR = TRUE;
1630     stAceOSDControl.bG = TRUE;
1631     stAceOSDControl.bB = TRUE;
1632     stAceOSDControl.u16R = s_AceInfo[bScalerWin].wRCon;
1633     stAceOSDControl.u16G = s_AceInfo[bScalerWin].wGCon;
1634     stAceOSDControl.u16B = s_AceInfo[bScalerWin].wBCon;
1635     stAceOSDControl.u8OSDUIEn = 1;
1636     stAceOSDControl.u8OSDUIMode = 1;
1637     stAceOSDControl.u8Win = (bScalerWin==TRUE)?1:0;
1638 
1639     ST_KDRV_XC_CFD_CONTROL_INFO stCFDCtrlInfo;
1640     memset(&stCFDCtrlInfo, 0, sizeof(ST_KDRV_XC_CFD_CONTROL_INFO));
1641     stCFDCtrlInfo.enCtrlType = E_KDRV_XC_CFD_CTRL_SET_OSD;
1642     stCFDCtrlInfo.pParam = &stAceOSDControl;
1643     stCFDCtrlInfo.u32ParamLen = sizeof(ST_ACE_CFD_OSD_CONTROL);
1644 #ifdef UFO_XC_HDR
1645 #if (UFO_XC_HDR_VERSION == 2)
1646 //_s32FdScaler only defined by UFO_XC_HDR_VERSION == 2
1647 #if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
1648     if(0 > _s32FdScaler)
1649     {
1650         _s32FdScaler = open("/dev/scaler", O_RDWR);
1651 
1652         if(0 > _s32FdScaler)
1653         {
1654             printf("Scaler device not opened!!!!\n");
1655             return FALSE;
1656         }
1657     }
1658 
1659     if (ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stCFDCtrlInfo))
1660     {
1661         printf("MDRV_XC_IOC_CFDCONCTRL OSD fail!!!!\n");
1662         return FALSE;
1663     }
1664 
1665     if (stCFDCtrlInfo.u16ErrCode != 0)
1666     {
1667         printf("MDRV_XC_IOC_CFDCONCTRL OSD fail, errCode: %d\n", stCFDCtrlInfo.u16ErrCode);
1668     }
1669 
1670     ST_ACE_CFD_FIRE_CONTROL stAceCfdFire;
1671     memset(&stAceCfdFire, 0, sizeof(ST_ACE_CFD_FIRE_CONTROL));
1672     stAceCfdFire.u32Version = 0;
1673     stAceCfdFire.u16Length = sizeof(ST_ACE_CFD_FIRE_CONTROL);
1674     stAceCfdFire.u8UpdateType = E_XC_CFD_UPDATE_TYPE_OSD_ONLY;
1675     stAceCfdFire.u8Win = bScalerWin;
1676 
1677     stCFDCtrlInfo.enCtrlType = E_KDRV_XC_CFD_CTRL_SET_FIRE;
1678     stCFDCtrlInfo.pParam = &stAceCfdFire;
1679     stCFDCtrlInfo.u32ParamLen = sizeof(ST_ACE_CFD_FIRE_CONTROL);
1680     if (ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stCFDCtrlInfo))
1681     {
1682         printf("MDRV_XC_IOC_CFDCONCTRL E_KDRV_XC_CFD_CTRL_FIRE fail!!!!\n");
1683         return FALSE;
1684     }
1685 
1686     if (stCFDCtrlInfo.u16ErrCode != 0)
1687     {
1688         printf("MDRV_XC_IOC_CFDCONCTRL E_KDRV_XC_CFD_CTRL_FIRE fail, errCode: %d\n", stCFDCtrlInfo.u16ErrCode);
1689     }
1690 #endif
1691 #if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
1692     MDrv_XC_CFDControl(&stCFDCtrlInfo);
1693     ST_ACE_CFD_FIRE_CONTROL stAceCfdFire;
1694     memset(&stAceCfdFire, 0, sizeof(ST_ACE_CFD_FIRE_CONTROL));
1695     stAceCfdFire.u32Version = 0;
1696     stAceCfdFire.u16Length = sizeof(ST_ACE_CFD_FIRE_CONTROL);
1697     stAceCfdFire.u8UpdateType = E_XC_CFD_UPDATE_TYPE_OSD_ONLY;
1698     stAceCfdFire.u8Win = bScalerWin;
1699 
1700     stCFDCtrlInfo.enCtrlType = E_KDRV_XC_CFD_CTRL_SET_FIRE;
1701     stCFDCtrlInfo.pParam = &stAceCfdFire;
1702     stCFDCtrlInfo.u32ParamLen = sizeof(ST_ACE_CFD_FIRE_CONTROL);
1703     MDrv_XC_CFDControl(&stCFDCtrlInfo);
1704 #endif
1705 #endif
1706 #endif
1707 
1708     return TRUE;
1709 }
1710 
msAdjustVideoContrast(void * pInstance,BOOL bScalerWin,BYTE ucContrast)1711 void msAdjustVideoContrast(void *pInstance, BOOL bScalerWin, BYTE ucContrast )
1712 {
1713     _ACE_GET_VARIABLE();
1714 
1715     s_AceInfo[bScalerWin].wContrast = (WORD)ucContrast<<3;
1716     SetVideoContrastMatrix(pInstance, bScalerWin );
1717     msSetVideoColorMatrix(pInstance, bScalerWin );
1718 }
msAdjustVideoContrast_10Bits(void * pInstance,BOOL bScalerWin,WORD wContrast)1719 void msAdjustVideoContrast_10Bits(void *pInstance, BOOL bScalerWin, WORD wContrast )
1720 {
1721     _ACE_GET_VARIABLE();
1722 
1723     s_AceInfo[bScalerWin].wContrast = wContrast<<1;
1724     SetVideoContrastMatrix(pInstance, bScalerWin );
1725     msSetVideoColorMatrix(pInstance, bScalerWin );
1726 }
1727 
msAdjustVideoSaturation(void * pInstance,BOOL bScalerWin,BYTE ucSaturation)1728 void msAdjustVideoSaturation(void *pInstance, BOOL bScalerWin, BYTE ucSaturation )
1729 {
1730     _ACE_GET_VARIABLE();
1731 
1732     s_AceInfo[bScalerWin].ucSaturation = ucSaturation;
1733     SetVideoSatHueMatrix(pInstance, bScalerWin);
1734     msSetVideoColorMatrix(pInstance, bScalerWin);
1735 }
1736 
msAdjustVideoHue(void * pInstance,BOOL bScalerWin,BYTE ucHue)1737 void msAdjustVideoHue(void *pInstance, BOOL bScalerWin, BYTE ucHue )
1738 {
1739     _ACE_GET_VARIABLE();
1740     s_AceInfo[bScalerWin].ucHue = ucHue;
1741     SetVideoSatHueMatrix(pInstance, bScalerWin);
1742     msSetVideoColorMatrix(pInstance, bScalerWin);
1743 }
msAdjustVideoRGB(void * pInstance,BOOL bScalerWin,BYTE ucRCon,BYTE ucGCon,BYTE ucBCon)1744 void msAdjustVideoRGB(void *pInstance, BOOL bScalerWin, BYTE ucRCon, BYTE ucGCon, BYTE ucBCon)
1745 {
1746     _ACE_GET_VARIABLE();
1747     s_AceInfo[bScalerWin].wRCon = (WORD)ucRCon<<3;
1748     s_AceInfo[bScalerWin].wGCon = (WORD)ucGCon<<3;
1749     s_AceInfo[bScalerWin].wBCon = (WORD)ucBCon<<3;
1750     SetVideoContrastMatrix(pInstance, bScalerWin);
1751     msSetVideoColorMatrix(pInstance, bScalerWin);
1752 }
msAdjustVideoRGB_10Bits(void * pInstance,BOOL bScalerWin,WORD wRCon,WORD wGCon,WORD wBCon)1753 void msAdjustVideoRGB_10Bits(void *pInstance, BOOL bScalerWin, WORD wRCon, WORD wGCon, WORD wBCon)
1754 {
1755     _ACE_GET_VARIABLE();
1756     s_AceInfo[bScalerWin].wRCon = wRCon<<1;
1757     s_AceInfo[bScalerWin].wGCon = wGCon<<1;
1758     s_AceInfo[bScalerWin].wBCon = wBCon<<1;
1759     SetVideoContrastMatrix(pInstance, bScalerWin );
1760     msSetVideoColorMatrix(pInstance, bScalerWin );
1761 }
1762 
SetPCConRGBMatrix(void * pInstance,BOOL bWindow)1763 static void SetPCConRGBMatrix(void *pInstance, BOOL bWindow )
1764 {
1765     _ACE_GET_VARIABLE();
1766     s_AceInfo[bWindow].sContrastRGBMatrix[0][0] = ( (DWORD)s_AceInfo[bWindow].wRCon * (s_AceInfo[bWindow].wContrast)) >> 10;
1767     s_AceInfo[bWindow].sContrastRGBMatrix[1][1] = ( (DWORD)s_AceInfo[bWindow].wGCon * (s_AceInfo[bWindow].wContrast)) >> 10;
1768     s_AceInfo[bWindow].sContrastRGBMatrix[2][2] = ( (DWORD)s_AceInfo[bWindow].wBCon * (s_AceInfo[bWindow].wContrast)) >> 10;
1769 }
1770 
msSetPCColorMatrix(void * pInstance,BOOL bScalerWin)1771 void msSetPCColorMatrix(void *pInstance, BOOL bScalerWin )
1772 {
1773     _ACE_GET_VARIABLE();
1774     short sResultTmp1[3][3];
1775     short sResultTmp2[3][3];
1776     short* psOutTab;
1777     //BYTE ucCMCtlVal;
1778     DECLARA_CMCTL_VARIABLE
1779     DECLARA_BANK_VARIABLE
1780 
1781     U8 i,j;
1782     short ColorMatrix_1[3][3];
1783     short ColorMatrix_2[3][3];
1784 
1785     if( TRUE == bBypassColorMatrix)
1786     {
1787         return ;
1788     }
1789 
1790     BACKUP_SC_BANK
1791 
1792 
1793     psOutTab = (short*)s_AceInfo[bScalerWin].sContrastRGBMatrix;
1794 
1795     // YUV => RGB
1796     if( (s_AceInfo[bScalerWin].bForceYUVtoRGB) || (bScalerWin == SUB_WINDOW) )
1797     {
1798         //ArrayMultiply( s_AceInfo[bScalerWin].sContrastRGBMatrix, (Typ3x3Array)(s_AceInfo[bScalerWin].psYVUtoRGBMatrix), sResultTmp1 );
1799         for ( i=0;i<3;i++ )
1800         {
1801             for ( j=0;j<3;j++ )
1802             {
1803                ColorMatrix_1[i][j]=*(s_AceInfo[bScalerWin].psYVUtoRGBMatrix +(i*3)+j);;
1804             }
1805         }
1806         ArrayMultiply( s_AceInfo[bScalerWin].sContrastRGBMatrix, ColorMatrix_1, sResultTmp1 );
1807 
1808 
1809         psOutTab = (short*)sResultTmp1;
1810         //ucCMCtlVal = 0x35;
1811         CTLVal = ENABLE_CM_CTL1;
1812     }
1813     else
1814     {
1815         CTLVal = ENABLE_CM_CTL2;
1816         //ucCMCtlVal = 0x30;
1817     }
1818 
1819     // Convert to sRGB
1820     if( s_AceInfo[bScalerWin].psPCsRGBMatrix != NULL )
1821     {
1822         //ArrayMultiply( (Typ3x3Array)s_AceInfo[bScalerWin].psPCsRGBMatrix, (Typ3x3Array)psOutTab, sResultTmp2 );
1823         for ( i=0;i<3;i++ )
1824         {
1825             for ( j=0;j<3;j++ )
1826             {
1827                ColorMatrix_1[i][j]=*(s_AceInfo[bScalerWin].psPCsRGBMatrix +(i*3)+j);;
1828                ColorMatrix_2[i][j]=*(psOutTab +(i*3)+j);;
1829             }
1830         }
1831         ArrayMultiply( ColorMatrix_1, ColorMatrix_2, sResultTmp2 );
1832 
1833 
1834         psOutTab = (short*)sResultTmp2;
1835     }
1836 
1837     if (MDrv_ACE_IsSupportMLoad(pInstance))
1838     {
1839         MDrv_ACE_WriteColorMatrixBurst(pInstance, bScalerWin, (MS_U16*)psOutTab );
1840     }
1841     else
1842     {
1843         msWriteColorMatrix(pInstance, bScalerWin, psOutTab );
1844     }
1845 
1846 #if defined(UFO_XC_HDR) && defined(UFO_XC_HDR_VERSION) && (UFO_XC_HDR_VERSION == 2) && !defined(ACE_NOT_IN_KERNEL)
1847 #else
1848     if( bScalerWin == MAIN_WINDOW )
1849     {
1850         SET_SC_BANK(BANK_CM_MAIN);
1851         ACE_WRITE_CM_CTL(psACEInstPri->u32DeviceID,  REG_ADDR_CM_CTL_MAIN, CTLVal, MASK_CM_CTL );
1852     }
1853     else
1854     {
1855         SET_SC_BANK(BANK_CM_SUB);
1856         ACE_WRITE_CM_CTL(psACEInstPri->u32DeviceID,  REG_ADDR_CM_CTL_SUB, CTLVal, MASK_CM_CTL );
1857     }
1858 #endif
1859 
1860     RECOVER_SC_BANK
1861 }
1862 
msAdjustPCContrast(void * pInstance,BOOL bScalerWin,BYTE ucContrast)1863 void msAdjustPCContrast(void *pInstance, BOOL bScalerWin, BYTE ucContrast )
1864 {
1865     _ACE_GET_VARIABLE();
1866     s_AceInfo[bScalerWin].wContrast = (WORD)ucContrast<<3;
1867     SetPCConRGBMatrix(pInstance, bScalerWin );
1868     msSetPCColorMatrix(pInstance, bScalerWin );
1869 }
msAdjustPCContrast_10Bits(void * pInstance,BOOL bScalerWin,WORD wContrast)1870 void msAdjustPCContrast_10Bits(void *pInstance, BOOL bScalerWin, WORD wContrast )
1871 {
1872     _ACE_GET_VARIABLE();
1873     s_AceInfo[bScalerWin].wContrast = wContrast<<1;
1874     SetPCConRGBMatrix(pInstance, bScalerWin );
1875     msSetPCColorMatrix(pInstance, bScalerWin );
1876 }
1877 
msAdjustPCRGB(void * pInstance,BOOL bScalerWin,BYTE ucRed,BYTE ucGreen,BYTE ucBlue)1878 void msAdjustPCRGB(void *pInstance, BOOL bScalerWin, BYTE ucRed, BYTE ucGreen, BYTE ucBlue )
1879 {
1880     _ACE_GET_VARIABLE();
1881     s_AceInfo[bScalerWin].wRCon = (WORD)ucRed<<3;
1882     s_AceInfo[bScalerWin].wGCon = (WORD)ucGreen<<3;
1883     s_AceInfo[bScalerWin].wBCon = (WORD)ucBlue<<3;
1884     SetPCConRGBMatrix(pInstance,  bScalerWin );
1885     msSetPCColorMatrix(pInstance,  bScalerWin );
1886 }
msAdjustPCRGB_10Bits(void * pInstance,BOOL bScalerWin,WORD wRed,WORD wGreen,WORD wBlue)1887 void msAdjustPCRGB_10Bits(void *pInstance, BOOL bScalerWin, WORD wRed, WORD wGreen, WORD wBlue )
1888 {
1889     _ACE_GET_VARIABLE();
1890     s_AceInfo[bScalerWin].wRCon = wRed<<1;
1891     s_AceInfo[bScalerWin].wGCon = wGreen<<1;
1892     s_AceInfo[bScalerWin].wBCon = wBlue<<1;
1893     SetPCConRGBMatrix(pInstance, bScalerWin );
1894     msSetPCColorMatrix(pInstance, bScalerWin );
1895 }
1896 
1897 //-------------------------------------------------------------------------------
1898 // Directly control color matrix on/off
1899 //-------------------------------------------------------------------------------
msACE_SetColorMatrixControl(void * pInstance,BOOL bScalerWin,BOOL bEnable)1900 void msACE_SetColorMatrixControl(void *pInstance, BOOL bScalerWin, BOOL bEnable )
1901 {
1902     DECLARA_CMCTL_VARIABLE
1903     DECLARA_BANK_VARIABLE
1904 
1905     BACKUP_SC_BANK
1906     _ACE_GET_INST_PRIVATE();
1907 
1908     if( bEnable )
1909         CTLVal = MASK_CM_CTL_EN;
1910     else
1911         CTLVal = 0;
1912 
1913     if( bScalerWin == MAIN_WINDOW )
1914     {
1915         SET_SC_BANK(BANK_CM_MAIN);
1916         ACE_WRITE_CM_CTL(psACEInstPri->u32DeviceID,  REG_ADDR_CM_CTL_MAIN, CTLVal, MASK_CM_CTL_EN );
1917     }
1918     else
1919     {
1920         SET_SC_BANK(BANK_CM_SUB);
1921         ACE_WRITE_CM_CTL(psACEInstPri->u32DeviceID,  REG_ADDR_CM_CTL_SUB, CTLVal, MASK_CM_CTL_EN );
1922     }
1923     RECOVER_SC_BANK
1924 }
1925 
1926 //-------------------------------------------------------------------------------
1927 // Directly control color matrix R/B range
1928 // bRange: 0 -> 0~255, 1 -> -128~127
1929 //-------------------------------------------------------------------------------
msACE_SetRBChannelRange(void * pInstance,BOOL bScalerWin,BOOL bRange)1930 void msACE_SetRBChannelRange(void *pInstance, BOOL bScalerWin, BOOL bRange )
1931 {
1932     DECLARA_CMCTL_VARIABLE
1933 
1934     DECLARA_BANK_VARIABLE
1935 
1936     BACKUP_SC_BANK
1937 
1938     if( bRange )
1939         CTLVal = MASK_CM_CTL_RB_RANGE;
1940     else
1941         CTLVal = 0;
1942     _ACE_GET_INST_PRIVATE();
1943 
1944     if( bScalerWin == MAIN_WINDOW )
1945     {
1946         SET_SC_BANK(BANK_CM_MAIN);
1947         ACE_WRITE_CM_CTL(psACEInstPri->u32DeviceID,  REG_ADDR_CM_CTL_MAIN, CTLVal, MASK_CM_CTL_RB_RANGE );
1948     }
1949     else
1950     {
1951         SET_SC_BANK(BANK_CM_SUB);
1952         ACE_WRITE_CM_CTL(psACEInstPri->u32DeviceID,  REG_ADDR_CM_CTL_SUB, CTLVal, MASK_CM_CTL_RB_RANGE );
1953     }
1954     RECOVER_SC_BANK
1955 }
1956 
msACESetBypassColorMatrix(void * pInstance,MS_BOOL bEnable)1957 MS_BOOL msACESetBypassColorMatrix(void *pInstance, MS_BOOL bEnable)
1958 {
1959     _ACE_GET_VARIABLE();
1960     bBypassColorMatrix = bEnable;
1961     return TRUE ;
1962 }
1963 
msACE_HDR_MatrixInvGen(void * pInstance,MS_S32 Minv[3][3],MS_S32 M[3][3])1964 void msACE_HDR_MatrixInvGen(void *pInstance,MS_S32 Minv[3][3], MS_S32 M[3][3])
1965 {
1966     MS_S32 a = M[0][0];
1967     MS_S32 b = M[0][1];
1968     MS_S32 c = M[0][2];
1969     MS_S32 d = M[1][0];
1970     MS_S32 e = M[1][1];
1971     MS_S32 f = M[1][2];
1972     MS_S32 g = M[2][0];
1973     MS_S32 h = M[2][1];
1974     MS_S32 i = M[2][2];
1975 
1976     MS_S64 ei_fh = (MS_S64)e*i - (MS_S64)f*h; // a b c a
1977     MS_S64 fg_di = (MS_S64)f*g - (MS_S64)d*i; // d e f d
1978     MS_S64 dh_eg = (MS_S64)d*h - (MS_S64)e*g; // g h i g
1979     MS_S64 hc_ib = (MS_S64)h*c - (MS_S64)i*b; // a b c a
1980     MS_S64 ia_gc = (MS_S64)i*a - (MS_S64)g*c;
1981     MS_S64 gb_ha = (MS_S64)g*b - (MS_S64)h*a;
1982     MS_S64 bf_ce = (MS_S64)b*f - (MS_S64)c*e;
1983     MS_S64 cd_af = (MS_S64)c*d - (MS_S64)a*f;
1984     MS_S64 ae_bd = (MS_S64)a*e - (MS_S64)b*d;
1985 
1986     MS_S64 det = a*ei_fh + b*fg_di + c*dh_eg;
1987 
1988     Minv[0][0] = ei_fh * 2500000000LL / det;
1989     Minv[1][0] = fg_di * 2500000000LL / det;
1990     Minv[2][0] = dh_eg * 2500000000LL / det;
1991     Minv[0][1] = hc_ib * 2500000000LL / det;
1992     Minv[1][1] = ia_gc * 2500000000LL / det;
1993     Minv[2][1] = gb_ha * 2500000000LL / det;
1994     Minv[0][2] = bf_ce * 2500000000LL / det;
1995     Minv[1][2] = cd_af * 2500000000LL / det;
1996     Minv[2][2] = ae_bd * 2500000000LL / det;
1997 
1998 }
1999 
msACE_HDR_YUV2RGBGen(void * pInstance,ACE_DRV_HDR_YUV2RGBData * pData)2000 void msACE_HDR_YUV2RGBGen(void *pInstance,ACE_DRV_HDR_YUV2RGBData* pData)
2001 {
2002     MS_S32 Mr2y[3][3], My2r[3][3];
2003     MS_S64 Kr, Kb, Kg;
2004     MS_U16 u16Regs[9];
2005 
2006     memset(Mr2y, 0, sizeof(MS_S32)*3*3);
2007     memset(My2r, 0, sizeof(MS_S32)*3*3);
2008     memset(u16Regs, 0, sizeof(MS_U16)*9);
2009 
2010     if (pData->u8SMode == 0)
2011     {
2012         Kr = pData->u16SKr;
2013         Kb = pData->u16SKb;
2014         Kg = 50000 - Kb - Kr;
2015         Mr2y[0][0] = Kr;
2016         Mr2y[0][1] = Kg;
2017         Mr2y[0][2] = Kb;
2018 
2019         Mr2y[1][0] = -Kr * 25000 / (50000 - Kb);
2020         Mr2y[1][1] = -Kg * 25000 / (50000 - Kb);
2021 
2022         Mr2y[1][2] = (50000 - Kb) * 25000 / (50000 - Kb);
2023         Mr2y[2][0] = (50000 - Kr) * 25000 / (50000 - Kr);
2024 
2025         Mr2y[2][1] = -Kg * 25000 / (50000 - Kr);
2026         Mr2y[2][2] = -Kb * 25000 / (50000 - Kr);
2027 
2028         msACE_HDR_MatrixInvGen(pInstance,My2r, Mr2y);
2029 
2030     }
2031     else if (pData->u8SMode == 1) // YCgCo
2032     {
2033         // TODO
2034     }
2035     else// if (pData->sMode == 2) // 2020CL
2036     {
2037         // TODO
2038     }
2039 
2040     if ((g_ACEinitParameters.ACE_HDRMetadataMpegVUI.u8TransferCharacteristics == 16) ||
2041        (g_ACEinitParameters.ACE_HDRMetadataHdmiTxInfoFrame.u8EOTF == 2))    // HDMI HDR is SMPTE ST2084.
2042     {
2043         // BT.2084.
2044         _u16HdrY2RRatio = 2;
2045     }
2046     else
2047     {
2048         // Others.
2049         _u16HdrY2RRatio = 1;
2050     }
2051 
2052     u16Regs[1] = (((My2r[0][0] << 10)*_u16HdrY2RRatio + 25000) / 50000*1167/1000) & 0x1FFF;
2053     u16Regs[2] = (((My2r[0][1] << 10)*_u16HdrY2RRatio + 25000) / 50000) & 0x1FFF;
2054     u16Regs[0] = (((My2r[0][2] << 10)*_u16HdrY2RRatio + 25000) / 50000) & 0x1FFF;
2055     u16Regs[4] = (((My2r[1][0] << 10)*_u16HdrY2RRatio + 25000) / 50000*1167/1000) & 0x1FFF;
2056     u16Regs[5] = (((My2r[1][1] << 10)*_u16HdrY2RRatio + 25000) / 50000) & 0x1FFF;
2057     u16Regs[3] = (((My2r[1][2] << 10)*_u16HdrY2RRatio + 25000) / 50000) & 0x1FFF;
2058     u16Regs[7] = (((My2r[2][0] << 10)*_u16HdrY2RRatio + 25000) / 50000*1167/1000) & 0x1FFF;
2059     u16Regs[8] = (((My2r[2][1] << 10)*_u16HdrY2RRatio + 25000) / 50000) & 0x1FFF;
2060     u16Regs[6] = (((My2r[2][2] << 10)*_u16HdrY2RRatio + 25000) / 50000) & 0x1FFF;
2061 
2062     BYTE i;
2063     BYTE ucCopyLen = 9;
2064 
2065     for( i = 0; i < ucCopyLen; ++ i )
2066     {
2067         if(u16Regs[i] >0xFFF)
2068         {
2069             u16Regs[i]= u16Regs[i]|0xF000;
2070         }
2071     }
2072 
2073     _ACE_GET_VARIABLE();
2074 
2075     CopyMatrix( u16Regs,(WORD*)s_AceInfo[MAIN_WINDOW].psYVUtoRGBMatrix  ,ucCopyLen );
2076 
2077     msSetVideoColorMatrix(pInstance, MAIN_WINDOW );
2078 }
2079 
msACE_HDR_Set3x3Gen(void * pInstance,MS_BOOL bWrite)2080 MS_BOOL msACE_HDR_Set3x3Gen(void *pInstance,MS_BOOL bWrite)
2081 {
2082     _ACE_GET_VARIABLE();
2083     BYTE ucCopyLen = 9;
2084     MS_BOOL bRet = FALSE;
2085 
2086     if(TRUE==bWrite)
2087     {
2088         if(_bSetGenRead==TRUE)
2089         {
2090             CopyMatrix( s16PreRegs,(WORD*)s_AceInfo[MAIN_WINDOW].psYVUtoRGBMatrix  ,ucCopyLen );
2091             _bSetGenRead=FALSE;
2092             bRet = TRUE;
2093         }
2094     }
2095     else
2096     {
2097 
2098         if(_bSetGenRead==FALSE)
2099         {
2100             if(FALSE==_bSetHDRInit)
2101             {
2102                 _u16HdrY2RRatio = 2;
2103 
2104                 _bSetHDRInit=TRUE;
2105             }
2106             CopyMatrix( (WORD*)s_AceInfo[MAIN_WINDOW].psYVUtoRGBMatrix,s16PreRegs  ,ucCopyLen );
2107 
2108             _bSetGenRead=TRUE;
2109 
2110             bRet = TRUE;
2111         }
2112     }
2113 
2114     return bRet;
2115 }
msACE_HDR_ConfigMetadata_HdmiTxInfoFrame(void * pInstance,ACE_DRV_HDR_YUV2RGBData * pY2RData,ACE_DRV_HDRHdmiTxAviInfoFrame * stMetadata)2116 void msACE_HDR_ConfigMetadata_HdmiTxInfoFrame(void *pInstance,ACE_DRV_HDR_YUV2RGBData* pY2RData, ACE_DRV_HDRHdmiTxAviInfoFrame *stMetadata)
2117 {
2118     // Matrix Coefficients (Note: Don't care about whether the pixel format is RGB or YCC. Just assume input to VOP2 is YCC!! Important!!!!!!!!!)
2119     pY2RData->bSValid = TRUE;
2120 
2121     if (stMetadata->Colorimetry == 0 || stMetadata->Colorimetry == 2) // No data or 709
2122     {
2123         // 709 matrices
2124         pY2RData->u8SMode = 0;
2125         pY2RData->u16SKr = 10630; //0.2126
2126         pY2RData->u16SKb = 3610; //0.0722
2127     }
2128     else if (stMetadata->Colorimetry == 1) // 170M (=601)
2129     {
2130         // 601 matrices
2131         pY2RData->u8SMode = 0;
2132         pY2RData->u16SKr = 14950; //0.299
2133         pY2RData->u16SKb = 5700; //0.114
2134     }
2135     else if (stMetadata->Colorimetry == 3 && (stMetadata->ExtendedColorimetry == 1)) // xvYCC 709
2136     {
2137         // 709 matrices
2138         pY2RData->u8SMode = 0;
2139         pY2RData->u16SKr = 10630; //0.2126
2140         pY2RData->u16SKb = 3610; //0.0722
2141     }
2142     else if (stMetadata->Colorimetry == 3 && (stMetadata->ExtendedColorimetry == 0 || stMetadata->ExtendedColorimetry == 2 || stMetadata->ExtendedColorimetry == 3)) // xvYCC 601 or sYCC 601 or Adobe YCC 601
2143     {
2144         // 601 matrices
2145         pY2RData->u8SMode = 0;
2146         pY2RData->u16SKr = 14950; //0.299
2147         pY2RData->u16SKb = 5700; //0.114
2148     }
2149     else if (stMetadata->Colorimetry == 3 && (stMetadata->ExtendedColorimetry == 4)) // Adobe RGB
2150     {
2151         // 601 matrices (NOTE : Assume the input of VOP2 is YCC)
2152         pY2RData->u8SMode = 0;
2153         pY2RData->u16SKr = 14950; //0.299
2154         pY2RData->u16SKb = 5700; //0.114
2155     }
2156     else if (stMetadata->Colorimetry == 3 && (stMetadata->ExtendedColorimetry == 5 || stMetadata->ExtendedColorimetry == 6)) // BT2020 CL YCC
2157     {
2158         // 2020 matrices
2159         pY2RData->u8SMode = 0;
2160         pY2RData->u16SKr = 13135; //0.2627
2161         pY2RData->u16SKb = 2965; //0.0593
2162     }
2163     else
2164     {
2165         pY2RData->bSValid = FALSE;
2166     }
2167 }
2168 
msACE_HDR_ConfigMetadata_MpegVUI(void * pInstance,ACE_DRV_HDR_YUV2RGBData * pY2RData)2169 void msACE_HDR_ConfigMetadata_MpegVUI(void *pInstance,ACE_DRV_HDR_YUV2RGBData* pY2RData)
2170 {
2171     // Matrix Coefficients
2172     pY2RData->bSValid = TRUE;
2173     switch (g_ACEinitParameters.ACE_HDRMetadataMpegVUI.u8MatrixCoefficients)
2174     {
2175      case 1: // 709
2176         pY2RData->u8SMode = 0;
2177         pY2RData->u16SKr = 10630; //0.2126
2178         pY2RData->u16SKb = 3610; //0.0722
2179         break;
2180      case 4: // 47
2181         pY2RData->u8SMode = 0;
2182         pY2RData->u16SKr = 15000; //0.30
2183         pY2RData->u16SKb = 5500; //0.11
2184         break;
2185      case 5: // 601 (625)
2186      case 6: // 601 (525)
2187         pY2RData->u8SMode = 0;
2188         pY2RData->u16SKr = 14950; //0.299
2189         pY2RData->u16SKb = 5700; //0.114
2190         break;
2191      case 7: // 240M
2192         pY2RData->u8SMode = 0;
2193         pY2RData->u16SKr = 10600; //0.212
2194         pY2RData->u16SKb = 4350; //0.087
2195         break;
2196      case 8: // YCgCo
2197         pY2RData->u8SMode = 1;
2198         pY2RData->u16SKr = 2500;
2199         pY2RData->u16SKb = 1250;
2200         break;
2201      case 9: // 2020NCL
2202         pY2RData->u8SMode = 0;
2203         pY2RData->u16SKr = 13135; //0.2627
2204         pY2RData->u16SKb = 2965; //0.0593
2205         break;
2206      case 10: // 2020CL
2207         pY2RData->u8SMode = 0;  // Spec is 2. Reference mantis 0959025.
2208         pY2RData->u16SKr = 13135; //0.2627
2209         pY2RData->u16SKb = 2965; //0.0593
2210         break;
2211      case 2: // Unspecified
2212      default:
2213         pY2RData->bSValid = FALSE;
2214         break;
2215     }
2216 }
2217 
msACE_HDR_Main(void * pInstance)2218 void msACE_HDR_Main(void *pInstance)
2219 {
2220     ACE_DRV_HDR_YUV2RGBData stY2RData = {0, 0, 0, 3610, 10630, 0, 3610, 10630};
2221 
2222 
2223     if(ENABLE==g_ACEinitParameters.bHDREnable)
2224     {
2225         // Read 3*3
2226         msACE_HDR_Set3x3Gen(pInstance,FALSE);
2227         if(g_ACEinitParameters.u16HDRFunctionSelect & 0x01)
2228         {
2229             msACE_HDR_ConfigMetadata_MpegVUI(pInstance,&stY2RData);
2230 
2231             if(g_ACEinitParameters.ACE_HDRMetadataMpegVUI.u8MatrixCoefficients !=_u8PreMatrixCoefficients)
2232             {
2233                 _u8PreMatrixCoefficients = g_ACEinitParameters.ACE_HDRMetadataMpegVUI.u8MatrixCoefficients;
2234 
2235                 msACE_HDR_YUV2RGBGen(pInstance,&stY2RData);
2236             }
2237         }
2238         // For HDMI HDR
2239         if(g_ACEinitParameters.u16HDRFunctionSelect==0x10)
2240         {
2241 
2242             ACE_DRV_HDRHdmiTxAviInfoFrame stAviInfoFrame;
2243             memcpy(&stAviInfoFrame,&g_ACEinitParameters.ACE_HDRHdmiTxAviInfoFrame,sizeof(ACE_DRV_HDRHdmiTxAviInfoFrame));
2244 
2245             msACE_HDR_ConfigMetadata_HdmiTxInfoFrame(pInstance,&stY2RData,&stAviInfoFrame);
2246 
2247             if((stAviInfoFrame.ExtendedColorimetry !=_u8PreExtendedColorimetry) || (stAviInfoFrame.Colorimetry != _u8PreColorimetry))
2248             {
2249                 printf("PixelFormat: %x\n",stAviInfoFrame.PixelFormat);
2250                 printf("Colorimetry: %x\n",stAviInfoFrame.Colorimetry);
2251                 printf("ExtendedColorimetry: %x\n",stAviInfoFrame.ExtendedColorimetry);
2252                 printf("RgbQuantizationRange: %x\n",stAviInfoFrame.RgbQuantizationRange);
2253                 printf("YccQuantizationRange: %x\n",stAviInfoFrame.YccQuantizationRange);
2254                 printf("StaticMetadataDescriptorID: %x\n",stAviInfoFrame.StaticMetadataDescriptorID);
2255                 printf("_u8PreColorimetry: %x\n",_u8PreColorimetry);
2256                 printf("_u8PreExtendedColorimetry: %x\n",_u8PreExtendedColorimetry);
2257                 _u8PreColorimetry = stAviInfoFrame.Colorimetry;
2258                 _u8PreExtendedColorimetry = stAviInfoFrame.ExtendedColorimetry;
2259                 msACE_HDR_YUV2RGBGen(pInstance,&stY2RData);
2260             }
2261         }
2262     }
2263     else
2264     {
2265         // Write 3*3
2266         if (msACE_HDR_Set3x3Gen(pInstance,TRUE) == TRUE)
2267         {
2268             msSetVideoColorMatrix(pInstance, MAIN_WINDOW );
2269         }
2270         _u8PreMatrixCoefficients=0;
2271          _u8PreExtendedColorimetry = 0;
2272          _u8PreColorimetry = 0;
2273     }
2274 
2275 }
2276 
2277 
2278