//<MStar Software>
//******************************************************************************
// MStar Software
// Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
// All software, firmware and related documentation herein ("MStar Software") are
// intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
// law, including, but not limited to, copyright law and international treaties.
// Any use, modification, reproduction, retransmission, or republication of all
// or part of MStar Software is expressly prohibited, unless prior written
// permission has been granted by MStar.
//
// By accessing, browsing and/or using MStar Software, you acknowledge that you
// have read, understood, and agree, to be bound by below terms ("Terms") and to
// comply with all applicable laws and regulations:
//
// 1. MStar shall retain any and all right, ownership and interest to MStar
//    Software and any modification/derivatives thereof.
//    No right, ownership, or interest to MStar Software and any
//    modification/derivatives thereof is transferred to you under Terms.
//
// 2. You understand that MStar Software might include, incorporate or be
//    supplied together with third party`s software and the use of MStar
//    Software may require additional licenses from third parties.
//    Therefore, you hereby agree it is your sole responsibility to separately
//    obtain any and all third party right and license necessary for your use of
//    such third party`s software.
//
// 3. MStar Software and any modification/derivatives thereof shall be deemed as
//    MStar`s confidential information and you agree to keep MStar`s
//    confidential information in strictest confidence and not disclose to any
//    third party.
//
// 4. MStar Software is provided on an "AS IS" basis without warranties of any
//    kind. Any warranties are hereby expressly disclaimed by MStar, including
//    without limitation, any warranties of merchantability, non-infringement of
//    intellectual property rights, fitness for a particular purpose, error free
//    and in conformity with any international standard.  You agree to waive any
//    claim against MStar for any loss, damage, cost or expense that you may
//    incur related to your use of MStar Software.
//    In no event shall MStar be liable for any direct, indirect, incidental or
//    consequential damages, including without limitation, lost of profit or
//    revenues, lost or damage of data, and unauthorized system use.
//    You agree that this Section 4 shall still apply without being affected
//    even if MStar Software has been modified by MStar in accordance with your
//    request or instruction for your use, except otherwise agreed by both
//    parties in writing.
//
// 5. If requested, MStar may from time to time provide technical supports or
//    services in relation with MStar Software to you for your use of
//    MStar Software in conjunction with your or your customer`s product
//    ("Services").
//    You understand and agree that, except otherwise agreed by both parties in
//    writing, Services are provided on an "AS IS" basis and the warranty
//    disclaimer set forth in Section 4 above shall apply.
//
// 6. Nothing contained herein shall be construed as by implication, estoppels
//    or otherwise:
//    (a) conferring any license or right to use MStar name, trademark, service
//        mark, symbol or any other identification;
//    (b) obligating MStar or any of its affiliates to furnish any person,
//        including without limitation, you and your customers, any assistance
//        of any kind whatsoever, or any information; or
//    (c) conferring any license or right under any intellectual property right.
//
// 7. These terms shall be governed by and construed in accordance with the laws
//    of Taiwan, R.O.C., excluding its conflict of law rules.
//    Any and all dispute arising out hereof or related hereto shall be finally
//    settled by arbitration referred to the Chinese Arbitration Association,
//    Taipei in accordance with the ROC Arbitration Law and the Arbitration
//    Rules of the Association by three (3) arbitrators appointed in accordance
//    with the said Rules.
//    The place of arbitration shall be in Taipei, Taiwan and the language shall
//    be English.
//    The arbitration award shall be final and binding to both parties.
//
//******************************************************************************
//<MStar Software>
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2008-2009 MStar Semiconductor, Inc.
// All rights reserved.
//
// Unless otherwise stipulated in writing, any and all information contained
// herein regardless in any format shall remain the sole proprietary of
// MStar Semiconductor Inc. and be kept in strict confidence
// ("MStar Confidential Information") by the recipient.
// Any unauthorized act including without limitation unauthorized disclosure,
// copying, use, reproduction, sale, distribution, modification, disassembling,
// reverse engineering and compiling of the contents of MStar Confidential
// Information is unlawful and strictly prohibited. MStar hereby reserves the
// rights to any and all damages, losses, costs and expenses resulting therefrom.
//
////////////////////////////////////////////////////////////////////////////////

#define  _MDRV_PQ_C_

#ifdef MSOS_TYPE_LINUX
#include <pthread.h>
#endif

#if !defined(MSOS_TYPE_LINUX_KERNEL)
#include "string.h"
#include "stdio.h"
#include <sys/stat.h>
#include <stdint.h>
#define do_div(x,y) ((x)/=(y))
#else
#include <linux/string.h>
#include <asm/div64.h>
#include <linux/fs.h>
#include <linux/vmalloc.h>
#include <linux/err.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#endif

#include "MsCommon.h"
//#include "MsVersion.h"
#include "MsOS.h"
#include "MsTypes.h"
#include "utopia.h"
#include "utopia_dapi.h"

#include "hwreg_utility2.h"
#include "color_reg.h"

#include "drvXC_IOPort.h"
#include "apiXC.h"
#include "apiXC_v2.h"
#include "apiXC_Ace.h"
#include "apiXC_Cus.h"
#include "drvMVOP.h"
#include "drvSYS.h"

#include "drvPQ_Define.h"
#include "drvPQ_Declare.h"
#include "drvPQ.h"
#include "drvPQ_cus.h"
#include "drvPQ_Datatypes.h"
#include "PQ_context.h"
#include "mhal_pq.h"
#include "mhal_pq_cus.h"
#include "drvPQ_Bin.h"
#include "apiPNL.h"
#include "QualityMode.h"
#include "PQ_private.h"
#include "apiPQ_v2.h"
#include "mhal_pq_adaptive.h"
#include "drvSYS.h"

#include "PQ_utility.h"
#include "drvPQ_VR.h"

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h> // O_RDWR
#include <poll.h>
#endif

#include "mdrv_xc_io.h"
#include "mdrv_xc_st.h"

#ifndef UNUSED //to avoid compile warnings...
#define UNUSED(var) (void)(var)
#endif

#ifndef PQ_HAL_EWINDOW_PARAM
#define PQ_HAL_EWINDOW_PARAM 0
#endif

#ifndef PQ_DISABLE_UC_FOR_PREVSD
#define PQ_DISABLE_UC_FOR_PREVSD  0
#endif

#ifndef PQ_DISABLE_UC_FOR_PIP
#define PQ_DISABLE_UC_FOR_PIP             0
#endif

#ifndef PQ_GRULE_SLZ_ENABLE
#define PQ_GRULE_SLZ_ENABLE   0
#endif

#ifndef PQ_DDR_SELECT_VER
#define PQ_DDR_SELECT_VER 1
#endif

#ifndef PQ_BW_4K2KPIP_SUPPORT
#define PQ_BW_4K2KPIP_SUPPORT 1
#endif

#ifndef PQ_SUPPORT_SUB_POSTCCS
#define PQ_SUPPORT_SUB_POSTCCS 1
#endif

#ifndef PQ_SUPPORT_MAIN_DHD
#define PQ_SUPPORT_MAIN_DHD 1
#endif

#ifndef PQ_SUPPORT_SUB_DHD
#define PQ_SUPPORT_SUB_DHD 1
#endif

#ifndef PQ_GRULE_3D_ENABLE
#define PQ_GRULE_3D_ENABLE 0
#endif

#ifndef PQ_XRULE_MM_DS_ENABLE
#define PQ_XRULE_MM_DS_ENABLE 0
#endif

#ifndef PQ_XRULE_CSC_ENABLE
#define PQ_XRULE_CSC_ENABLE 1
#endif

#ifndef PQ_DUMP_XVYCC_GAMMA_SRAM
#define PQ_DUMP_XVYCC_GAMMA_SRAM 1
#endif

#ifndef PQ_SUPPORT_DS_MODULE_IP_OP
#define PQ_SUPPORT_DS_MODULE_IP_OP 0
#endif

#ifndef PQDS_INFO_VERSION
#define PQDS_INFO_VERSION 0
#endif

#ifndef PQ_GRULE_DYNAMIC_SCBW_ENABLE
#define PQ_GRULE_DYNAMIC_SCBW_ENABLE   0
#endif

#ifndef PQ_GRULE_AI_PQ_ENABLE
#define PQ_GRULE_AI_PQ_ENABLE   0
#endif

#ifndef PQ_XRULE_DLC_DYNAMIC_HISTOGRAM_ENABLE
#define PQ_XRULE_DLC_DYNAMIC_HISTOGRAM_ENABLE (0)
#endif

#ifndef PQ_SUPPORT_DS_MODULE_UCM
#define PQ_SUPPORT_DS_MODULE_UCM 0
#endif

/********************************************************************************
// PQ_IOCTL_SET_UC_FEATURE is flag for set UCNR  During SetWin(if enable this switch please sync CL641359)
// and please check  PQ excel skipRule(UC_CTL/UCDi/MADi_EODiW/MADi_SSTMADi_Motion need skip)item to prevent to set UCNR twice
**********************************************************************************/
#ifndef PQ_IOCTL_SET_UC_FEATURE
#define PQ_IOCTL_SET_UC_FEATURE   0
#endif

#ifndef Y2Y_709_601_VERSION
#define Y2Y_709_601_VERSION     0
#endif

#ifndef REG_CHIP_EXECUTOR_DUMMY
#define REG_CHIP_EXECUTOR_DUMMY 0
#endif
#ifndef REG_CHIP_EXECUTOR_KEY_DUMMY
#define REG_CHIP_EXECUTOR_KEY_DUMMY 0
#endif

#ifndef REG_CHIP_EXECUTOR_KEY
#define REG_CHIP_EXECUTOR_KEY 0
#endif

#ifndef PQ_SUPPORT_ADAPTIVE_VERSION
#define PQ_SUPPORT_ADAPTIVE_VERSION FALSE
#endif

#ifndef PQ_DEMO_MODE_ENABLE
#define PQ_DEMO_MODE_ENABLE 0
#endif

#ifndef PQ_SCALER_GAMMA_ENABLE
#define PQ_SCALER_GAMMA_ENABLE 0
#endif

#ifndef PQ_P_MODE8_3FRAME_MODE_ENABLE
#define PQ_P_MODE8_3FRAME_MODE_ENABLE 0
#endif

#ifndef PQ_STR_DTV_ENABLE_THREAD
#define  PQ_STR_DTV_ENABLE_THREAD 0
#endif

#ifndef PQ_GROUPNUM_BIN_VERSION
#define PQ_GROUPNUM_BIN_VERSION (0)
#endif

#ifndef PQ_SUPPORT_MADI_VR
#define PQ_SUPPORT_MADI_VR     (0)
#endif

#ifndef PQ_SUPPORT_ZNR
#define PQ_SUPPORT_ZNR     (0)
#endif

#ifndef PQ_120HZ_FRC_HW_BEHIND_VOP
#define PQ_120HZ_FRC_HW_BEHIND_VOP    (0)
#endif

#define  COLOUR_PRIMARY_SIZE (18)

#define PQ_ADAPTIVE_INVALID_VERSION (0xFFFFFFFF)

static MSIF_Version _drv_pq_version = {
    .DDI = { PQ_DRV_VERSION },
};

#define IS_PQ_TABLEIDX_NULL(x)    ( ((gbPQBinEnable == (TRUE)) && ((x) == PQ_BIN_IP_NULL)) || ((gbPQBinEnable == (FALSE)) && ((x) == PQ_IP_NULL)) )

MS_U16 _u16PQSrcType[PQ_MAX_WINDOW];
MS_U16 _u16PQSrcType_UFSC[PQ_MAX_WINDOW];
MS_U16 _u16PQSrcType_TMO[PQ_MAX_WINDOW];
#if (ENABLE_PQ_BIN_CF)
MS_U16 _u16PQSrcType_CF[PQ_MAX_WINDOW];
#endif
MS_U16 _u16LastPQSrcType[PQ_MAX_WINDOW] = {0xffff,0xffff};
MS_U16 _u16LastPQSrcTypeTMO[PQ_MAX_WINDOW] = {0xffff,0xffff};
MS_BOOL _bSkipDuplicateSetting = FALSE;
MS_U16 _u16PQSrcType_DBK_Detect[PQ_MAX_WINDOW];     //For Auto_DBK SW driver used

MS_BOOL _bLoadSetting = FALSE;

#if (ENABLE_PQ_BIN_HSY)
MS_U32 _u32PQSrcType_HSY[LEN_CONFIG];
MS_U8  u8PQHSYBinCnt = 0;
#endif

static MS_BOOL _bColorRange0_255[PQ_MAX_WINDOW];
static MS_BOOL _bGameModeOn[PQ_MAX_WINDOW];
static MS_U8 _u8RWmappingNum[PQ_MAX_WINDOW];
static PQ_YUV_STD _enYUVStandard[PQ_MAX_WINDOW];
MS_BOOL _bRWNumIsReset[PQ_MAX_WINDOW];
static MS_PQ_SWDR_GRULE_INDEX _enSWDRModeOn[PQ_MAX_WINDOW];
static MS_U16 _u16SRAM1 = PQ_BIN_IP_NULL;
static MS_U16 _u16SRAM2 = PQ_BIN_IP_NULL;
static MS_BOOL _bNeedRestoreWRBankMappingNum = FALSE;
static MS_BOOL _bPrePTPOn[PQ_MAX_WINDOW];

MS_BOOL _bCustomerMainPQEn = FALSE ;
MS_BOOL _bCustomerSubPQEn = FALSE ;
MS_BOOL _bUFSCPQEn = FALSE;
#if (ENABLE_PQ_BIN_CF)
MS_BOOL _bCFMainPQEn = FALSE;
MS_BOOL _bCFSubPQEn = FALSE;
#endif
MS_BOOL _bTMOPQEn = FALSE;
PqList*  _iPqCurrentTable[PQ_MAX_WINDOW] = {NULL, NULL};

#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
extern MS_BOOL MDrv_XC_CFDControl(ST_KDRV_XC_CFD_CONTROL_INFO *pstKdrvCFDCtrlInfo);
#endif

#if (defined MSOS_TYPE_LINUX_KERNEL)
extern MS_BOOL KDrv_XC_DPU_SetModSettings(ST_KDRV_XC_DPU_SETTING *pstDpuModSetting);
#endif

#ifdef MSOS_TYPE_LINUX
static MS_S32   _s32FdScaler = -1;
#endif

//whether current status is Point-to-point mode
static MS_BOOL _bPointToPointMode = FALSE;
static MS_BOOL _bPqBypassMode = FALSE;
//whether current status is Dualview display mode
MS_BOOL _bDualViewEnable = FALSE;

//whether current status is in force video mode
static MS_BOOL _bForceVideoInputMode[PQ_MAX_WINDOW] = {FALSE, FALSE};

// Enable/Disable P2P force to do CSC
MS_BOOL _bP2pForceToDoCsc = FALSE;

//if CFD do CSC or not
static MS_BOOL _bCFDDoCSC = FALSE;

/// Soc output color format
PQ_COLOR_FORMAT _enOutputColorFmt = E_PQ_COLOR_YUV444;
MS_BOOL _bDS_En = FALSE;
static MS_BOOL _bDMSV12L_PQConfigEn = FALSE; //this flag is coming from PQ file
#if PQ_EN_DMS_SW_CTRL
static MS_BOOL _bDMSV12L_PIPEn = TRUE; //TRUE: it's not PIP/POP; FALSE: PIP/POP case want to disable DMS12L
#endif
MS_U16 _u16RW_Method = 0;

static MS_U16 _u16PQDelyaTime = 0;
static MS_U16 _u16PQSuggestedFrameNum[PQ_MAX_WINDOW];
static MS_BOOL _bSensioMode = FALSE;
static MS_U8 u8PQDelayCount = 0;

static MS_PQ_INFO   _info = {.u16Input_Src_Num = 128, .u8IP_Num = 217,};
static MS_PQ_Status _status = {.bIsInitialized = FALSE, .bIsRunning = FALSE, };
static MS_PQ_RFBL_INFO _stRFBL_Info = {.bEnable = FALSE, .bFilm = FALSE , .u8MADiType = 0, };
static ST_PQ_StatusEx  _stPQ_StatusEx ;
BW_INFO_t bw_info;

MS_U8 *gpu8TmoSettingData = NULL;
MS_U16 gu16RegNum = 0;
MS_BOOL gbTmoSettingMalloc = 0;

MS_BOOL gbPQBinEnable = 0;
MS_PQBin_Header_Info stPQBinHeaderInfo[MAX_PQ_BIN_NUM];
extern MS_PQTextBin_Header_Info stPQTextBinHeaderInfo[MAX_PQ_TEXT_BIN_NUM];
extern MS_BOOL gbEnablePQTextBin;

MS_S32 _PQ_Mutex = -1;
MS_U8 _u8ChipRev = 0;

static EN_POWER_MODE _enPrevPowerState = E_POWER_SUSPEND;

static PQ_BWD_TYPE _eBWDType[PQ_MAX_WINDOW] = {PQ_BWD_NUM,PQ_BWD_NUM};


#ifdef MSOS_TYPE_LINUX
pthread_mutex_t _PQ_MLoad_Mutex;
#endif


#define XC_PQ_IP_INFORMATION_SIZE    (E_KDRV_XC_CFD_QMAPDATA_INFO_MAX)
#define PQ_BIN_CUSTOMER_PANEL_INDEX        0
#define PQ_BIN_UFSC_PANEL_INDEX        0
#if (ENABLE_PQ_BIN_CF)
#define PQ_BIN_CF_PANEL_INDEX          0
#endif
#define PQ_BIN_TMO_PANEL_INDEX        0

MS_PQ_Hdmi_Info _stHDMI_Info[PQ_MAX_WINDOW];
MS_PQ_Vd_Info _stVD_Info[PQ_MAX_WINDOW];
MS_PQ_Mode_Info  _stMode_Info[PQ_MAX_WINDOW];
MS_U8 _u8ModeIndex[PQ_MAX_WINDOW];
MS_PQ_Dtv_Info _stDTV_Info[PQ_MAX_WINDOW];
MS_PQ_Init_Info _stPQ_Info;
MS_BOOL _gbMemfmt422[PQ_MAX_WINDOW]={1, 1};
MS_U16 _gu16SetMemfmt_Madi[PQ_MAX_WINDOW]={0xFFFF, 0xFFFF};
MS_U16 _gu16SetMemfmt_Madi_Motion[PQ_MAX_WINDOW]={0xFFFF, 0xFFFF};

MS_BOOL _gIsSrcHDMode[PQ_MAX_WINDOW];
PQ_FOURCE_COLOR_FMT _gForceColorFmt[PQ_MAX_WINDOW];
MS_BOOL _gIs3D_En = 0;
MS_BOOL _bBypassModeOn[PQ_MAX_WINDOW]={0, 0};
MS_BOOL _gIsEnable8LBMode = FALSE;

MS_S32 _s32LastPQIPMADiMain = -1;
MS_S32 _s32LastPQIPMemFormatMain = -1;
MS_S32 _s32LastPQIPHSDYMain = -1;
MS_S32 _s32LastPQIPHSDCMain = -1;
MS_S32 _s32LastPQIPMADiSub = -1;
MS_S32 _s32LastPQIPMemFormatSub = -1;
MS_S32 _s32LastPQIPMADiMotMain = -1;
MS_S32 _s32LastPQIPMADiMotSub = -1;

static MS_U16 _upLayerConfig = 0;

static MS_U8 _u8CmpBitPerPixel = 0;

static MS_BOOL gbIsSupportSPFLB = FALSE;

#ifdef MSOS_TYPE_LINUX_KERNEL
extern PQ_MPEG_NR_FUNCTION_TYPE _eMPEGNRType[PQ_MAX_WINDOW];
extern PQ_3D_NR_FUNCTION_TYPE _e3DNRType[PQ_MAX_WINDOW];
extern PQ_PTP_FUNCTION_TYPE _ePTPType[PQ_MAX_WINDOW];
extern PQ_HDR_MODE _eHdrMode[PQ_MAX_WINDOW];
extern PQ_FEATURE_SWITCH_TYPE _eCtrType[PQ_MAX_WINDOW];
extern PQ_XVYCC_TYPE _eXvyccType[PQ_MAX_WINDOW];
extern PQ_DYNAMIC_CONTRAST_FUNCTION_TYPE _eDynamicContrastType[PQ_MAX_WINDOW];
extern PQ_FILM_MODE_FUNCTION_TYPE _eFilmModeType[PQ_MAX_WINDOW];
extern MS_BOOL _MApi_XC_GetStrStatus(MS_U8 u8WinID,MS_BOOL *pbSingnalStable,MS_BOOL *pbMute);
#endif

#if PQ_UC_CTL
static MS_U16 _u16MADi_DFK_Current[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
static MS_U16 _u16MADi_SST_Current[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
static MS_U16 _u16MADi_EODiW_Current[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
static MS_U16 _u16UCNR_Current[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
static MS_U16 _u16UCDi_Current[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
static MS_U16 _u16UCCTL_Current[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
static MS_U16 _u16SwDriver_Current[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
static MS_U16 _u16PostCCS_Current[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
static MS_U16 _u16DHD_Current[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};

#endif

extern MS_BOOL _gIsMVC4kx1k;
PQ_DISPLAY_TYPE _enPQDisplayType = PQ_DISPLAY_ONE;

extern MS_U16 QM_InputSourceToIndex(void* pInstance, PQ_WIN eWindow, PQ_INPUT_SOURCE_TYPE enInputSourceType);
extern MS_U16 QM_InputSourceToIndex_UFSC(void* pInstance,PQ_WIN eWindow, MS_U8 enInputSourceType);
#if (ENABLE_PQ_BIN_CF)
extern MS_U16 QM_InputSourceToIndex_CF(void* pInstance,PQ_WIN eWindow, MS_U8 enInputSourceType);
#endif
extern MS_U16 QM_InputSourceToIndex_TMO(void* pInstance, PQ_WIN eWindow, MS_U8 enInputSourceType);
#if (ENABLE_PQ_BIN_HSY)
MS_BOOL QM_InputSourceToIndex_HSY(PQ_WIN eWindow, MS_U8 enInputSourceType, MS_U32 *pu32label);
static EN_HSY_GRULE_IDX _enHsyGruleIdx[E_HSY_GRULE_IP_MAX] = {E_HSY_GRULE_IDX_MAX};
#endif

// Only updated from _MDrv_PQ_LoadHVSPDSXruleTable
static MS_U32 _u32PreHSize = 0;
static MS_U32 _u32PreVSize = 0;
static MS_U32 _gu32PreDispHSize = 0;
static MS_U32 _gu32PreDispVSize = 0;

static MS_BOOL _MDrv_PQ_LoadMMDSXruleTable(void* pInstance,ST_PQ_DS_XRULE_INFO* pstXCMMDSXruleInfo);
static MS_BOOL _MDrv_PQ_LoadSPFDSXruleTable(void* pInstance,ST_PQ_DS_XRULE_INFO* pstXCMMDSXruleInfo);
static MS_BOOL _MDrv_PQ_LoadHVSPDSXruleTable(void * pInstance, ST_PQ_DS_XRULE_INFO * pstXCMMDSXruleInfo);

#if (PQ_SUPPORT_SETGRULEDATA == 1)
static MS_U16 _gu16QmapGRuleLevel[MAX_PQ_BIN_NUM][E_PQ_QMAP_MAX] = {{0}};
#endif

#ifdef UFO_XC_PQ_SUPPORT_SWMOTION
static MS_U8 u8BK22_78_L = 0x00;
static MS_U8 u8BK22_78_H = 0x00;
static MS_U8 u8BK22_79_L = 0x00;
static MS_U8 u8BK22_79_H = 0x00;

//////////////////////////////////////////////////////////////////
// set MADi force Y/C motion enable bit
// function:
//           SAVE/RESTORE/SET
//
#define SAVE_FORCE_YC_MOTION_ENABLE(pInstance,bMainWin) \
do{ \
    if(bMainWin)\
    {\
        u8BK22_78_L = Hal_PQ_get_force_y_motion(pInstance,bMainWin); \
        u8BK22_79_L = Hal_PQ_get_force_c_motion(pInstance,bMainWin);\
    }\
    else\
    {\
        u8BK22_78_H = Hal_PQ_get_force_y_motion(pInstance,bMainWin); \
        u8BK22_79_H = Hal_PQ_get_force_c_motion(pInstance,bMainWin);\
    }\
} while(0)

#define SAVE_FORCE_Y_MOTION_ENABLE(pInstance,bMainWin) \
do{ \
    if(bMainWin)\
    {\
        u8BK22_78_L = Hal_PQ_get_force_y_motion(pInstance,bMainWin); \
    }\
    else\
    {\
        u8BK22_78_H = Hal_PQ_get_force_y_motion(pInstance,bMainWin); \
    }\
} while(0)

#define SAVE_FORCE_C_MOTION_ENABLE(pInstance,bMainWin) \
do{ \
    if(bMainWin)\
    {\
        u8BK22_79_L = Hal_PQ_get_force_c_motion(pInstance,bMainWin);\
    }\
    else\
    {\
        u8BK22_79_H = Hal_PQ_get_force_c_motion(pInstance,bMainWin);\
    }\
} while(0)

#define RESTORE_FORCE_YC_MOTION_ENABLE(pInstance,bMainWin) \
do{ \
    if(bMainWin)\
    {\
        Hal_PQ_set_force_y_motion(pInstance, 1, (MS_U16)u8BK22_78_L);\
        Hal_PQ_set_force_c_motion(pInstance, 1, (MS_U16)u8BK22_79_L);\
    }\
    else\
    {\
        Hal_PQ_set_force_y_motion(pInstance, 0, (((MS_U16)u8BK22_78_H)<<8));\
        Hal_PQ_set_force_c_motion(pInstance, 0, (((MS_U16)u8BK22_79_H))<<8);\
    }\
} while(0)

#define RESTORE_FORCE_Y_MOTION_ENABLE(pInstance,bMainWin) \
do{ \
    if(bMainWin)\
    {\
        Hal_PQ_set_force_y_motion(pInstance, TRUE, (MS_U16)u8BK22_78_L);\
    }\
    else\
    {\
        Hal_PQ_set_force_y_motion(pInstance, FALSE, (((MS_U16)u8BK22_78_H)<<8));\
    }\
} while(0)

#define RESTORE_FORCE_C_MOTION_ENABLE(pInstance,bMainWin) \
do{ \
    if(bMainWin)\
    {\
        Hal_PQ_set_force_c_motion(pInstance, TRUE, (MS_U16)u8BK22_79_L);\
    }\
    else\
    {\
        Hal_PQ_set_force_c_motion(pInstance, FALSE, (((MS_U16)u8BK22_79_H))<<8);\
    }\
} while(0)
#endif
//////////////////////////////////////////////////////////////////
// PQ Patch
MS_BOOL _bOSD_On = FALSE;

MS_BOOL bSetFrameCount = TRUE;

#if 0 //def _PDEBUG
#define PTH_PQ_RET_CHK(_pf_) \
    ({ \
        int r = _pf_; \
        if (r != 0 && r != ETIMEDOUT) \
            fprintf(stderr, "[PTHREAD] %s: %d: %s: %s\n", __FILE__, __LINE__, #_pf_, strerror(r)); \
        r; \
    })
#else
#define PTH_PQ_RET_CHK(_pf_) //_pf_
#endif


#ifdef MSOS_TYPE_LINUX
#define PQ_MLOAD_ENTRY() PTH_PQ_RET_CHK(pthread_mutex_lock(&_PQ_MLoad_Mutex))
#define PQ_MLOAD_RETURN() PTH_PQ_RET_CHK(pthread_mutex_unlock(&_PQ_MLoad_Mutex))
#else
#define PQ_MLOAD_ENTRY()
#define PQ_MLOAD_RETURN()
#endif

#ifndef PQ_VIP_RGBCMY_CTL_VER2
#define PQ_VIP_RGBCMY_CTL_VER2 0
#endif
#if (ENABLE_PQ_BIN_HSY)
MS_FOREST_MAP stForestMap;
MS_BIN_MAP *pstBinMap = NULL;
#endif

//Save PQ bin in PQ_Init
MS_PHYADDR u32PQInitBinAddr[MAX_PQ_BIN_NUM] = {0};
ST_PQ_MITESTBIN_PRIVATE stPQMITestBin = {0};
//////////////////////////////////////////////////////////////////
// OS related
//
//
#define XC_PQ_WAIT_MUTEX              (TICK_PER_ONE_MS * 50)              // 50 ms

//#define TRACE_DBG
/*
// Mutex & Lock
static MS_S32 _s32XC_PQMutex = -1;


#define XC_PQ_ENTRY()                 {   if (_s32XC_PQMutex < 0) { return E_XC_ACE_FAIL; }        \
                                        if(OS_OBTAIN_MUTEX(_s32XC_PQMutex, XC_PQ_WAIT_MUTEX) == FALSE) return E_XC_ACE_OBTAIN_MUTEX_FAIL;  }

#define XC_PQ_RETURN(_ret)            {   OS_RELEASE_MUTEX(_s32XC_PQMutex); return _ret; }
*/

#define PQ_MINIMUM_SIZE_FOR_VMALLOC (4096)

#if (!defined(MSOS_TYPE_LINUX_KERNEL))

#define PQ_MALLOC_MEM(pu8Addr , size)       \
        if(size > 0)       \
        {  \
            pu8Addr = (void *) malloc(size); \
            if (pu8Addr == NULL) \
                { printf("%s: %d, malloc fail.\n", __func__, __LINE__);} \
            else  \
                { memset(pu8Addr, 0, size);} \
        }  \
        else  \
            { printf("%s: %d, malloc size error.\n", __func__, __LINE__);}

#define PQ_FREE_MEM(pu8Addr, size) \
        if (pu8Addr != NULL) \
            { free(pu8Addr); pu8Addr = NULL;}

#else
#define PQ_MALLOC_MEM(pu8Addr, size)       \
        if(size > 0)       \
        {  \
            if(size < PQ_MINIMUM_SIZE_FOR_VMALLOC) \
                {pu8Addr = (void*)kmalloc(size,GFP_KERNEL);} \
            else \
                {pu8Addr = (void*)vmalloc(size);}\
            if (pu8Addr == NULL) \
                { printk("%s: %d, malloc fail.\n", __func__, __LINE__);} \
            else  \
                { memset(pu8Addr, 0, size);} \
        }  \
        else  \
            { printk("%s: %d, malloc size error.\n", __func__, __LINE__);}

#define PQ_FREE_MEM(pu8Addr, size) \
        if ((pu8Addr != NULL) && (size < PQ_MINIMUM_SIZE_FOR_VMALLOC)) \
            { kfree(pu8Addr); pu8Addr = NULL;} \
        else if ((pu8Addr != NULL) && (size >= PQ_MINIMUM_SIZE_FOR_VMALLOC))\
            { vfree(pu8Addr); pu8Addr = NULL;} \
        else \
            {printk("%s: %d, ptr is null.\n", __func__, __LINE__);}
#endif

#if PQ_XRULE_MM_DS_ENABLE
#define MM_DS_DELAY_COUNT   1
#endif

#define FAST_DISP_TIME_LOG  1
#if FAST_DISP_TIME_LOG
static MS_U32 _u32CBtime = 0;
#endif

#ifndef XC_PQ_FRC_FrameDelay_Count
#define XC_PQ_FRC_FrameDelay_Count    (0)  //FRC delay 0 Frame in Buffer
#endif

#ifndef MVOP_FRAMEDELAY_COUNT  //MVOP delay in Buffer
#define MVOP_FRAMEDELAY_COUNT (0)
#endif

char gu8Version[PQ_BIN_VERSION_LEN] = {0};


char pm_PQBinFilePathName[PQ_FILE_PATH_LENGTH] = {0};
const char DefaultPath_Ini[] = "/config/pq/";
const char DefaultPath_Ini_Ext[] = "/tvconfig/config/pq/";
const char CustomerPath_Ini[] = "/Customer/pq/";
const char MainBinName[]     = "Main.bin";
const char MainExBinName[]   = "Main_Ex.bin";
const char MainColorBinName[]= "Main_Color.bin";
const char UFSCBinName[]= "UFSC.bin";
const char SubBinName[]      = "Sub.bin";
const char SubExBinName[]    = "Sub_Ex.bin";
const char SubColorBinName[] = "Sub_Color.bin";
const char TMOBinName[] = "Main_TMO.bin";
const char mainTextBinName[] = "Main_Text.bin";
#if (ENABLE_PQ_BIN_CF)
const char MainCFBinName[]= "Main_CF.bin";
const char SubCFBinName[]= "Sub_CF.bin";
#endif
#if (ENABLE_PQ_BIN_HSY)
const char HSYcolorBinName[]="HSY.bin";
#endif

void* g_pPQResource[E_PQ_DEVICE_ID_MAX];
static MS_U16 MDrv_PQ_GetXRuleIPNum(void* pInstance,PQ_WIN eWindow, MS_U16 u16XRuleType);
static MS_BOOL _MDrv_PQ_LoadScalingTable(void* pInstance,PQ_WIN  eWindow,MS_U8 eXRuleType,MS_U8 u8XRuleIP,MS_BOOL bPreV_ScalingDown,MS_BOOL bInterlace,MS_BOOL bColorSpaceYUV,MS_U16 u16InputSize,MS_U16 u16SizeAfterScaling);
static MS_U16 MDrv_PQ_GetXRuleIPIndex(void* pInstance,PQ_WIN eWindow, MS_U16 u16XRuleType, MS_U16 u16XRuleIP, PQ_ENGINE_TYPE ePQEngineType);
static MS_U16 MDrv_PQ_GetXRuleTableIndex(void* pInstance,PQ_WIN eWindow, MS_U16 u16XRuleType, MS_U16 u16XRuleIdx, MS_U16 u16XRuleIP, PQ_ENGINE_TYPE ePQEngineType);
static MS_BOOL _MDrv_PQ_SetGRuleStatus(void *pInstance,PQ_WIN eWindow, MS_U16 u16GruleType, MS_U16 u16PQ_GroupIdx);
static MS_BOOL _MDrv_PQ_GetTableType(void* pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, MS_U16* pu16TabType);

#if (PQ_GRULE_GAME_MODE_ENABLE == 1)
static MS_BOOL _MDrv_PQ_LoadGameModeTable(void* pInstance, PQ_WIN eWindow, MS_U16 u16PQ_GameMode_Idx);
static MS_BOOL _MDrv_PQ_EnableGameModeUCPatch(void* pInstance,PQ_WIN eWindow);
static MS_BOOL _MDrv_PQ_DisableGameModeUCPatch(void* pInstance,PQ_WIN eWindow);
#endif
#if (PQ_SUPPORT_SETGRULEDATA == 0)
#if defined(PQ_GRULE_HDR_ENABLE) && (PQ_GRULE_HDR_ENABLE == 1)
static MS_BOOL _MDrv_PQ_LoadHDRModeTable(void* pInstance, PQ_WIN eWindow, MS_U16 u16PQ_HDRMode_Idx);
#if ENABLE_PQ_CUSTOMER_BIN_MAIN_GRULE
static MS_BOOL _MDrv_PQ_LoadCustomerHDRTable(void* pInstance, PQ_WIN eWindow, MS_U16 u16PQ_HDRMode_Idx);
#endif
#endif
#endif

#if (defined (UFO_XC_DS_PQ))
extern MS_BOOL _gIsNetworkMM;
extern MS_BOOL MApi_XC_Is_SupportSWDS(void);
extern void MDrv_PQBin_DS_GetTable(void* pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, PQ_ENGINE_TYPE ePQEngineType, EN_DS_MODULE_SEL eDS_SEL,MS_BOOL bCheckGRule);

MS_BOOL imode_PQDS = FALSE;
void MDrv_PQ_set_imode_PQDS(MS_BOOL imode)
{
    imode_PQDS = imode;
}
MS_U16 MDrv_PQ_GetGRule_TableIndex_PQDS(void* pInstance,PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16PQ_NRIdx, MS_U16 u16GRuleIPIndex)
{
    MS_U16 u16Ret = 0;
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            u16Ret = MDrv_PQBin_GetGRule_TableIndex(pInstance,u16GRuleType,
                     _u16PQSrcType[eWindow],
                     u16PQ_NRIdx,
                     u16GRuleIPIndex,
                     &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        }
        else
        {
            u16Ret = MDrv_PQ_GetGRule_TableIndex_(MAIN,pInstance, (MS_U8)u16GRuleType, _u16PQSrcType[eWindow], (MS_U8)u16PQ_NRIdx, (MS_U8)u16GRuleIPIndex);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            u16Ret = MDrv_PQBin_GetGRule_TableIndex(pInstance,u16GRuleType,
                     _u16PQSrcType[eWindow],
                     u16PQ_NRIdx,
                     u16GRuleIPIndex,
                     &stPQBinHeaderInfo[PQ_BIN_STD_SUB]);
        }
        else
        {
            u16Ret = MDrv_PQ_GetGRule_TableIndex_(SUB,pInstance, (MS_U8)u16GRuleType, _u16PQSrcType[eWindow], (MS_U8)u16PQ_NRIdx, (MS_U8)u16GRuleIPIndex);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_GRULE_DBG;
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.pValue= &u16PQ_NRIdx;
    stPQSetVRInfo.u16RuleIndex= u16GRuleType;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }
    return u16Ret;
}

MS_BOOL _MDrv_PQ_LoadDSModeTable(void* pInstance, SCALER_WIN eWindow, MS_U16 u16PQ_GameMode_Idx, PQ_ENGINE_TYPE ePQEngineType)
{
    MS_U16 u16Index=0, u16IPIdx=0, u16TabIdx=0;
    MS_BOOL bCheckGRule = FALSE;
    MS_U16 u16GroupNum = 0;
    MS_U16 u16IPNum = 0;
    MS_U32 u32SupportZNR = 0;
    EN_DS_MODULE_SEL enDSDomain = E_DS_MODULE_OP;
    if(eWindow == SUB_WINDOW)
    {
        return FALSE;
    }

    MApi_XC_GetChipCaps(E_XC_SUPPORT_ZNR, &u32SupportZNR, sizeof(MS_U32));
    if(u32SupportZNR!=0)
    {
        enDSDomain = (MHal_PQ_UCMEnable()) ? E_DS_MODULE_UCMR : E_DS_MODULE_OP;
    }

    switch(ePQEngineType)
    {
        case PQ_XC0_STD:
            u16GroupNum = PQ_GRULE_DS_PARAMETERS_NUM_Main;
            u16IPNum = PQ_GRULE_DS_PARAMETERS_IP_NUM_Main;
            if(MHal_PQ_Support_GroupIPBIN())
            {
                MDrv_PQBin_GetGRule_GroupIPNum(pInstance,(PQ_WIN)eWindow,PQ_GRule_DS_PARAMETERS_Main,PQ_BIN_STD_MAIN ,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
            }
            if (u16PQ_GameMode_Idx > u16GroupNum)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRULE_INDEX_OVERFLOW_ERROR Level Index = %u, Max Level Index = %u",u16PQ_GameMode_Idx,(u16GroupNum-1));
                PQ_ASSERT(0);
                return FALSE;
            }
            for(u16Index = 0; u16Index < u16IPNum; u16Index++)
            {
                u16IPIdx = MDrv_PQ_GetGRule_IPIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, u16Index);
                if (u16PQ_GameMode_Idx == u16GroupNum)
                {
                    u16TabIdx = MDrv_PQ_GetTableIndex_U2(pInstance, eWindow, u16IPIdx);
                }
                else
                {
                    u16TabIdx = MDrv_PQ_GetGRule_TableIndex_PQDS(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, u16PQ_GameMode_Idx, u16Index);
                }
                MDrv_PQBin_DS_GetTable(pInstance, MDrv_PQ_PQtoSC_WinMapping(eWindow), u16TabIdx, u16IPIdx, PQ_XC0_STD, enDSDomain, bCheckGRule);
            }
            break;
        case PQ_XC0_UFSC:
#if defined(PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE) && PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE
            u16GroupNum = PQ_GRULE_DS_PARAMETERS_NUM_UFSC;
            u16IPNum = PQ_GRULE_DS_PARAMETERS_IP_NUM_UFSC;
            if(MHal_PQ_Support_GroupIPBIN())
            {
                MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,PQ_GRule_DS_PARAMETERS_UFSC,PQ_BIN_UFSC ,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_UFSC]);
            }
            _u16PQSrcType_UFSC[eWindow]= QM_InputSourceToIndex_UFSC(pInstance,eWindow, _enInputSourceType[eWindow]);

            if (u16PQ_GameMode_Idx > u16GroupNum)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRULE_INDEX_OVERFLOW_ERROR Level Index = %u, Max Level Index = %u",u16PQ_GameMode_Idx,(u16GroupNum-1));
                PQ_ASSERT(0);
                return FALSE;
            }

            for(u16Index = 0; u16Index < u16IPNum; u16Index++)
            {
                u16IPIdx = MDrv_PQ_GetGRule_IPIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_UFSC, u16Index, PQ_XC0_UFSC);
                if (u16PQ_GameMode_Idx == u16GroupNum)
                {
                    u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, eWindow, u16IPIdx, PQ_XC0_UFSC);
                }
                else
                {
                    u16TabIdx = MDrv_PQ_GetGRule_TableIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_UFSC, u16PQ_GameMode_Idx, u16Index, PQ_XC0_UFSC);
                }
                MDrv_PQBin_DS_GetTable(pInstance, eWindow, u16TabIdx, u16IPIdx, PQ_XC0_UFSC, enDSDomain, bCheckGRule);
            }
#endif
            break;
        case PQ_XC0_CUSTOMER:
#if defined(PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL) && PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL
            u16GroupNum = PQ_GRULE_DS_PARAMETERS_NUM_Main_Color;
            u16IPNum = PQ_GRULE_DS_PARAMETERS_IP_NUM_Main_Color;
            if(MHal_PQ_Support_GroupIPBIN())
            {
                MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,PQ_GRule_DS_PARAMETERS_Main_Color,PQ_BIN_CUSTOMER_MAIN ,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN]);
            }
            if (u16PQ_GameMode_Idx > u16GroupNum)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRULE_INDEX_OVERFLOW_ERROR Level Index = %u, Max Level Index = %u",u16PQ_GameMode_Idx,(u16GroupNum-1));
                PQ_ASSERT(0);
                return FALSE;
            }

            for(u16Index = 0; u16Index < u16IPNum; u16Index++)
            {
                u16IPIdx = MDrv_PQ_GetCustomerGRule_IPIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main_Color, u16Index);
                if (u16PQ_GameMode_Idx == u16GroupNum)
                {
                    u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, eWindow, u16IPIdx, PQ_XC0_CUSTOMER);
                }
                else
                {
                    u16TabIdx = MDrv_PQ_GetGRule_TableIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main_Color, u16PQ_GameMode_Idx, u16Index, PQ_XC0_CUSTOMER);
                }
                MDrv_PQBin_DS_GetTable(pInstance, eWindow, u16TabIdx, u16IPIdx, PQ_XC0_CUSTOMER, enDSDomain, bCheckGRule);
            }
#endif
            break;
        default:
            break;
    }
    return TRUE;
}
void _MDrv_PQ_Load_DS_GRULE_Table(void* pInstance,PQ_WIN eWindow,MS_U32 V_Size, MS_U32 H_Size)
{
    if(MHal_PQ_IsMMDSByPass(eWindow))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"PQ MM_DS By PASS\n");
        return;
    }
    if(MHal_PQ_IsSourceUnLock(_u16PQSrcType[eWindow]) == TRUE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"u16SrcType is Unlock");
        return ;
    }
    MS_U16 u16PQ_GameModeIdx = 0;
#if defined(PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL) && PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL
    MS_U16 u16PQ_GameModeColorIdx = 0;
#endif
#if defined(PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE) && PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE
    MS_U16 u16PQ_GameModeUFSCIdx = 0;
#endif

#if (defined(PQ_GRule_DS_PARAMETERS_H_SD_MAIN) && defined(PQ_GRule_DS_PARAMETERS_H_FHD_MAIN) && defined(PQ_GRule_DS_PARAMETERS_4K1K_MAIN))
    if(H_Size > PQ_4K2K_GRULE_DS_H_MINSIZE) // For H=4096 case
    {
        u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_H_FHD_Main);
    }
    else if(H_Size == PQ_4K2K_GRULE_DS_H_MINSIZE) // 4k
    {
        if(V_Size == PQ_4K2K_GRULE_DS_V_MINSIZE) // 4k2k
        {
            u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_4K_Main);
#if defined(PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL) && PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL
            u16PQ_GameModeColorIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main_Color, PQ_GRule_DS_PARAMETERS_4K_Main_Color,PQ_XC0_CUSTOMER);
#endif
#if defined(PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE) && PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE
            u16PQ_GameModeUFSCIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_UFSC, PQ_GRule_Lvl_DS_PARAMETERS_4K_UFSC,PQ_XC0_UFSC);
#endif
        }
        else // 4k1k
        {
            u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_4K1K_Main);
        }
    }
    else if((H_Size > PQ_FHD_GRULE_DS_H_MAXSIZE) && (H_Size < PQ_4K2K_GRULE_DS_H_MINSIZE)) // H_FHD
    {
        u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_H_FHD_Main);
    }
    else if(H_Size <= PQ_FHD_GRULE_DS_H_MAXSIZE)
    {
        if(V_Size > PQ_FHD_GRULE_DS_V_MAXSIZE) // H_FHD
        {
            u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_H_FHD_Main);
        }
        else if((V_Size >= PQ_FHD_GRULE_DS_V_MINSIZE) && (V_Size <= PQ_FHD_GRULE_DS_V_MAXSIZE)) // FHD
        {
            u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_FHD_Main);
#if defined(PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL) && PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL
            u16PQ_GameModeColorIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main_Color, PQ_GRule_DS_PARAMETERS_FHD_Main_Color,PQ_XC0_CUSTOMER);
#endif
#if defined(PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE) && PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE
            u16PQ_GameModeUFSCIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_UFSC, PQ_GRule_Lvl_DS_PARAMETERS_FHD_UFSC,PQ_XC0_UFSC);
#endif
        }
        else if((V_Size >= PQ_HSD_GRULE_DS_V_MINSIZE) && (V_Size < PQ_FHD_GRULE_DS_V_MINSIZE)) // H_SD
        {
            u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_H_SD_Main);
        }
        else if((V_Size >= PQ_SD_GRULE_DS_V_MINSIZE) && (V_Size < PQ_HSD_GRULE_DS_V_MINSIZE)) // SD
        {
            u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_SD_Main);
#if defined(PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL) && PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL
        u16PQ_GameModeColorIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main_Color, PQ_GRule_DS_PARAMETERS_SD_Main_Color,PQ_XC0_CUSTOMER);
#endif
#if defined(PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE) && PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE
        u16PQ_GameModeUFSCIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_UFSC, PQ_GRule_Lvl_DS_PARAMETERS_SD_UFSC,PQ_XC0_UFSC);
#endif
        }
        else // L_SD
        {
            u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_L_SD_Main);
#if defined(PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL) && PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL
        u16PQ_GameModeColorIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main_Color, PQ_GRule_DS_PARAMETERS_L_SD_Main_Color,PQ_XC0_CUSTOMER);
#endif
#if defined(PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE) && PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE
        u16PQ_GameModeUFSCIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_UFSC, PQ_GRule_Lvl_DS_PARAMETERS_L_SD_UFSC,PQ_XC0_UFSC);
#endif
        }
    }
#else
    if(V_Size >=PQ_4K2K_GRULE_DS_V_MIN_SIZE)
    {
        u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_4K_Main);
#if defined(PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL) && PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL
        u16PQ_GameModeColorIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main_Color, PQ_GRule_DS_PARAMETERS_4K_Main_Color,PQ_XC0_CUSTOMER);
#endif
#if defined(PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE) && PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE
        u16PQ_GameModeUFSCIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_UFSC, PQ_GRule_Lvl_DS_PARAMETERS_4K_UFSC,PQ_XC0_UFSC);
#endif
    }else if( (V_Size < PQ_FHD_GRULE_DS_V_MAXSIZE) && (V_Size >= PQ_FHD_GRULE_DS_V_MINSIZE) )
    {
        u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_FHD_Main);
#if defined(PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL) && PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL
        u16PQ_GameModeColorIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main_Color, PQ_GRule_DS_PARAMETERS_FHD_Main_Color,PQ_XC0_CUSTOMER);
#endif
#if defined(PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE) && PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE
        u16PQ_GameModeUFSCIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_UFSC, PQ_GRule_Lvl_DS_PARAMETERS_FHD_UFSC,PQ_XC0_UFSC);
#endif
    }else if( (V_Size < PQ_SD_GRULE_DS_V_MAXSIZE) && (V_Size >= PQ_SD_GRULE_DS_V_MINSIZE) )
    {
        u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_SD_Main);
#if defined(PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL) && PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL
        u16PQ_GameModeColorIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main_Color, PQ_GRule_DS_PARAMETERS_SD_Main_Color,PQ_XC0_CUSTOMER);
#endif
#if defined(PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE) && PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE
        u16PQ_GameModeUFSCIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_UFSC, PQ_GRule_Lvl_DS_PARAMETERS_SD_UFSC,PQ_XC0_UFSC);
#endif
    }
    else
    {
        u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main, PQ_GRule_Lvl_DS_PARAMETERS_L_SD_Main);
#if defined(PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL) && PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL
        u16PQ_GameModeColorIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_Main_Color, PQ_GRule_DS_PARAMETERS_L_SD_Main_Color,PQ_XC0_CUSTOMER);
#endif
#if defined(PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE) && PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE
        u16PQ_GameModeUFSCIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, PQ_GRule_DS_PARAMETERS_UFSC, PQ_GRule_Lvl_DS_PARAMETERS_L_SD_UFSC,PQ_XC0_UFSC);
#endif
    }
#endif
    PQ_LEVEL_PRINTF(PQ_DBG_INFO,"MDrv_PQ_Load_DS_GRULE_Table GameModeIdx = %u\n",u16PQ_GameModeIdx);
    _MDrv_PQ_LoadDSModeTable(pInstance,eWindow,u16PQ_GameModeIdx,PQ_XC0_STD);
#if defined(PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL) && PQ_GRULE_DS_PARAMETERS_ENABLE_SMALL
    PQ_LEVEL_PRINTF(PQ_DBG_INFO,"MDrv_PQ_Load_DS_GRULE_Table GameModeColorIdx = %u\n",u16PQ_GameModeColorIdx);
    _MDrv_PQ_LoadDSModeTable(pInstance,eWindow,u16PQ_GameModeColorIdx,PQ_XC0_CUSTOMER);
#endif
#if defined(PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE) && PQ_GRULE_UFSC_DS_PARAMETERS_ENABLE
    PQ_LEVEL_PRINTF(PQ_DBG_INFO,"MDrv_PQ_Load_DS_GRULE_Table GameModeUFSCIdx = %u\n",u16PQ_GameModeUFSCIdx);
    _MDrv_PQ_LoadDSModeTable(pInstance,eWindow,u16PQ_GameModeUFSCIdx,PQ_XC0_UFSC);
#endif
}

static MS_BOOL _MDrv_PQ_DS_BypasseFunction (EN_PQ_DS_BYPASS_TYPE enPQDSBypassType)
{
    MS_BOOL bPQDSEnable = FALSE;

    switch (enPQDSBypassType)
    {
        case E_PQ_MMDS_XRULE_BYPASS:
          bPQDSEnable = (((_gu16PQDbgXRuleBypass & BIT(0))== BIT(0))?TRUE:FALSE);
          break;
        case E_PQ_SPF_XRULE_BYPASS:
          bPQDSEnable = (((_gu16PQDbgXRuleBypass & BIT(1))== BIT(1))?TRUE:FALSE);
          break;
        case E_PQ_HVSP_XRULE_BYPASS:
          bPQDSEnable = (((_gu16PQDbgXRuleBypass & BIT(2))== BIT(2))?TRUE:FALSE);
          break;
        default:
          PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"enPQDSBypassType >= E_PQ_BYPASS_NUM\n");
          break;
    }
    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"bPQDSEnable %u enPQDSBypassType %u \n",bPQDSEnable,enPQDSBypassType);
    return bPQDSEnable;
}



static MS_BOOL _MDrv_PQ_MMDSEnableCondition(ST_PQ_DS_XRULE_INFO* pstXCMMDSXruleInfo)
{
    MS_BOOL bEnableMMDS = FALSE;
    PQ_NULL_POINTER(pstXCMMDSXruleInfo);
    if(_MDrv_PQ_DS_BypasseFunction(E_PQ_MMDS_XRULE_BYPASS))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"[MM DS] PQ MM DS is Bypass \n");
        return FALSE;
    }
    if(((pstXCMMDSXruleInfo->u32XCDSUpdateCondition&XC_DS_UPDATE_CONDITION_RESOLUTION_CHANGE)!=0) ||(pstXCMMDSXruleInfo->u32XCDSUpdateCondition == 0))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"[MM DS] PQ MM DS is Enable \n");
        bEnableMMDS = TRUE;
    }
    return bEnableMMDS;
}

static MS_BOOL _MDrv_PQ_SPFDSEnableCondition(ST_PQ_DS_XRULE_INFO* pstXCMMDSXruleInfo)
{
    MS_BOOL bEnableSPFDS = FALSE;
    PQ_NULL_POINTER(pstXCMMDSXruleInfo);
    if(_MDrv_PQ_DS_BypasseFunction(E_PQ_SPF_XRULE_BYPASS))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"[SPF DS] PQ SPF DS is Bypass \n");
        return FALSE;
    }
    if((pstXCMMDSXruleInfo->u32XCDSUpdateCondition&XC_DS_UPDATE_CONDITION_SPF)!=0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"[SPF DS] PQ SPF DS is Enable \n");
        bEnableSPFDS = TRUE;
    }
    return bEnableSPFDS;
}

static MS_BOOL _MDrv_PQ_HVSPDSEnableCondition(ST_PQ_DS_XRULE_INFO* pstXCMMDSXruleInfo)
{
    MS_BOOL bIsSupport = FALSE;
    MS_BOOL bEnableHVSPDS = FALSE;
    MS_U32 u32SupportHVSP_DS = 0;

    PQ_NULL_POINTER(pstXCMMDSXruleInfo);
    MApi_XC_GetChipCaps(E_XC_SUPPORT_HVSP_DS, &u32SupportHVSP_DS, sizeof(MS_U32));
    bIsSupport = (MS_BOOL)u32SupportHVSP_DS;

    if(_MDrv_PQ_DS_BypasseFunction(E_PQ_HVSP_XRULE_BYPASS))
    {
        PQ_LEVEL_PRINTF((PQ_DBG_HVSP|PQ_DBG_SRULE),"[HVSP DS] PQ HVSP DS is Bypass \n");
        return FALSE;
    }
    if(bIsSupport)
    {
        if((pstXCMMDSXruleInfo->u32XCDSUpdateCondition&XC_DS_UPDATE_CONDITION_HVSP_XRULE)!=0)
        {
            if((pstXCMMDSXruleInfo->u32CropWin_Height != _u32PreVSize))
            {
                PQ_LEVEL_PRINTF((PQ_DBG_HVSP|PQ_DBG_SRULE),"[HVSP DS] Resolution is Changed! PreCropV:%u, CropV:%u, \n",_u32PreVSize, pstXCMMDSXruleInfo->u32CropWin_Height);
                bEnableHVSPDS = TRUE;
            }
            else if(pstXCMMDSXruleInfo->u32CropWin_Width != _u32PreHSize)
            {
                PQ_LEVEL_PRINTF((PQ_DBG_HVSP|PQ_DBG_SRULE),"[HVSP DS] Resolution is Changed! PreCropH:%u, CropH:%u, \n",_u32PreHSize, pstXCMMDSXruleInfo->u32CropWin_Width);
                bEnableHVSPDS = TRUE;
            }
            else if(pstXCMMDSXruleInfo->u32DispWin_Height != _gu32PreDispVSize)
            {
                PQ_LEVEL_PRINTF((PQ_DBG_HVSP|PQ_DBG_SRULE),"[HVSP DS] Resolution is Changed! PreDispV:%u, DispV:%u, \n",_gu32PreDispVSize, pstXCMMDSXruleInfo->u32DispWin_Height);
                bEnableHVSPDS = TRUE;
            }
            else if(pstXCMMDSXruleInfo->u32DispWin_Width != _gu32PreDispHSize)
            {
                PQ_LEVEL_PRINTF((PQ_DBG_HVSP|PQ_DBG_SRULE),"[HVSP DS] Resolution is Changed! PreDispH:%u, DispH:%u, \n",_gu32PreDispHSize, pstXCMMDSXruleInfo->u32DispWin_Width);
                bEnableHVSPDS = TRUE;
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"[HVSP DS] Resolution isn't Changed! PreCropH:%u, PreCropV:%u, CropH:%u, CropV:%u, PreDispH:%u, PreDispV:%u, DispH:%u, DispV:%u\n",
                                _u32PreHSize, _u32PreVSize, pstXCMMDSXruleInfo->u32CropWin_Width, pstXCMMDSXruleInfo->u32CropWin_Height,
                                _gu32PreDispHSize, _gu32PreDispVSize, pstXCMMDSXruleInfo->u32DispWin_Width, pstXCMMDSXruleInfo->u32DispWin_Height);
            }
        }
    }
    if(MDrv_SYS_GetChipRev()==0)//U01 == 0
    {
        if(!MHal_PQ_HVSP_RIUWriteSRAMControl(&bEnableHVSPDS))
        {
            PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"[HVSP DS] MHal_PQ_HVSP_RIUWriteSRAMControl Fail! \n");
        }
    }

    return bEnableHVSPDS;
}


MS_BOOL _MDrv_PQ_Load_DS_XRULE_Table(void* pInstance, ST_PQ_DS_XRULE_INFO* pstXCMMDSXruleInfo)
{
    if(pstXCMMDSXruleInfo == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"stXCMMDSXruleInfo == NULL\n");
        return FALSE;
    }
    if(pInstance == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pInstance == NULL\n");
        return FALSE;
    }
    if(MHal_PQ_IsSourceUnLock(_u16PQSrcType[pstXCMMDSXruleInfo->enWindow]) == TRUE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"u16SrcType is Unlock");
        return FALSE;
    }
#if (PQ_XRULE_MM_DS_ENABLE == 1)
    //Resolution change || u32XCDSUpdateCondition not set (Forward compatible)
    if(_MDrv_PQ_MMDSEnableCondition(pstXCMMDSXruleInfo))
    {
        if(!_MDrv_PQ_LoadMMDSXruleTable(pInstance,pstXCMMDSXruleInfo))
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"_MDrv_PQ_LoadMMDSXruleTable Fail\n");
        }
    }
#endif
    //HDR change
#if (PQ_XRULE_SPF_DS_ENABLE == 1)
    if(_MDrv_PQ_SPFDSEnableCondition(pstXCMMDSXruleInfo))
    {
        if(gbIsSupportSPFLB == TRUE)
        {
           pstXCMMDSXruleInfo->enLoadXruleTableMethod = E_PQ_LOADXRULETABLE_DS;
           if(!_MDrv_PQ_LoadSPFDSXruleTable(pInstance,pstXCMMDSXruleInfo))
           {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"LoadSPFDSXruleTable Fail\n");
           }
        }
    }
#endif

    // HVSP
    if(_MDrv_PQ_HVSPDSEnableCondition(pstXCMMDSXruleInfo))
    {
        if(!_MDrv_PQ_LoadHVSPDSXruleTable(pInstance,pstXCMMDSXruleInfo))
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"LoadHVSPDSXruleTable Fail\n");
        }
        else
        {
            // _u32PreHSize and _u32PreVSize Only updated in here.
            _u32PreVSize = pstXCMMDSXruleInfo->u32CropWin_Height;
            _u32PreHSize = pstXCMMDSXruleInfo->u32CropWin_Width;
            _gu32PreDispVSize = pstXCMMDSXruleInfo->u32DispWin_Height;
            _gu32PreDispHSize = pstXCMMDSXruleInfo->u32DispWin_Width;
        }
    }
    return TRUE;
}
#endif

MS_U32 MDrv_PQ_Get_Semaphore(void *pInstance,E_PQ_POOL_ID eID_TYPE)
{
    E_PQ_POOL_ID eID=eID_TYPE;
    PQ_INSTANCE_PRIVATE *psPQInstPri = NULL;
    MS_U32 u32Return = UTOPIA_STATUS_FAIL;

    if(pInstance == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MApi_XC_Init() first\n");
        return u32Return;
    }
    else
    {
        UtopiaInstanceGetPrivate(pInstance, (void**)&psPQInstPri);
    }

    #if 0 // for Debug. It is important to check the tid
    pid_t tid;
    tid = syscall(SYS_gettid);
    printf("tid = (%d)\n",tid);
    #endif

    void* pModule = NULL;
    UtopiaInstanceGetModule(pInstance, &pModule);

    if(UtopiaResourceObtain(pModule, eID, &g_pPQResource[eID]) != UTOPIA_STATUS_SUCCESS)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaResourceObtain fail\n");
        return UTOPIA_STATUS_ERR_RESOURCE;
    }
    u32Return = UTOPIA_STATUS_SUCCESS;
    return u32Return;
}

MS_U32 MDrv_PQ_Release_Semaphore(void *pInstance,E_PQ_POOL_ID eID_TYPE)
{
    E_PQ_POOL_ID eID=eID_TYPE;
    MS_U32 u32Return = UTOPIA_STATUS_FAIL;
    PQ_INSTANCE_PRIVATE *psPQInstPri = NULL;

    if(pInstance == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MApi_XC_Init() first\n");
        return u32Return;
    }
    else
    {
        UtopiaInstanceGetPrivate(pInstance, (void**)&psPQInstPri);
    }
    #if 0 // for Debug. It is important to check the tid
    pid_t tid;
    tid = syscall(SYS_gettid);
    printf("tid = (%d)\n",tid);
    #endif

    u32Return = UtopiaResourceRelease(g_pPQResource[eID]);
    return u32Return;
}

MS_U8 * Open_PQ_Bin(MS_U8 * pName, MS_U32* u32FileSize)
{
    long lFileSize;
    MS_U32 u32Rst;
    const char * Name;
    MS_U8 *pu8Buffer;

    Name = (const char *)pName;
#if !defined(MSOS_TYPE_LINUX_KERNEL)
    FILE * pFile = NULL;
    int fd;
    struct stat fileStat;

    pFile = fopen(Name, "rb");
    if(pFile == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Open %s Failed\n", pName);
        return NULL;
    }

    fd = fileno(pFile);
    if((fstat(fd, &fileStat) != 0))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"get file stat %s Failed\n", pName);
        fclose(pFile);
        return NULL;
    }
    lFileSize = fileStat.st_size;
    *u32FileSize = lFileSize;
    if (lFileSize <= 0)
    {
        fclose(pFile);
        return NULL;
    }

    pu8Buffer = (MS_U8*) malloc(lFileSize);
    if(pu8Buffer == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"out of memory \n");
        fclose(pFile);
        return NULL;
    }

    u32Rst = fread(pu8Buffer, 1, lFileSize, pFile);
    if(u32Rst != (MS_U32)lFileSize)
    {
#if (defined(CONFIG_PURE_SN) || defined(CONFIG_PURE_SN_64))
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Read Main.bin Error %ld %ld\n", u32Rst, lFileSize);
#else
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Read Main.bin Error %d %ld\n", u32Rst, lFileSize);
#endif
        free(pu8Buffer);
        fclose(pFile);
        return NULL;
    }

    fclose(pFile);
#else
    struct file *pFile = NULL;

    mm_segment_t cur_mm_seg;
    pFile = filp_open(Name, O_RDONLY, 0);
    if(pFile == NULL)
    {
        printk("Open %s Failed\n", pName);

        return NULL;
    }
    vfs_llseek(pFile, 0, SEEK_END);
    lFileSize = pFile->f_pos;
    *u32FileSize = lFileSize;
    if (lFileSize <= 0)
    {
        filp_close(pFile, NULL);

        return NULL;
    }
    loff_t pos = vfs_llseek(pFile, 0, SEEK_SET);
    pu8Buffer = (MS_U8*)vmalloc(lFileSize);

    if(pu8Buffer == NULL)
    {
        printk("out of memory \n");
        filp_close(pFile, NULL);

        return NULL;
    }
    cur_mm_seg = get_fs();
    set_fs(KERNEL_DS);
    u32Rst = vfs_read(pFile, (char *)pu8Buffer, lFileSize, &pos);
    set_fs(cur_mm_seg);
    if(u32Rst != (MS_U32)lFileSize)
    {
        printk("Read Main.bin Error %td %td\n", (ptrdiff_t)u32Rst, (ptrdiff_t)lFileSize);
        vfree(pu8Buffer);
        filp_close(pFile, NULL);

        return NULL;
    }

    filp_close(pFile, NULL);

#endif

    return pu8Buffer;

}

void MDrv_PQ_ReplaceBin(void *pInstance, PQ_BIN_TYPE enBinType, MS_BOOL bUnitTest)
{
    MS_PHYADDR u32TmpBinAddr = 0;
    MS_U32 u32FileSize = 0;
    MS_U8 *pu8BinAddr = NULL;
    char u8PQBinFilePathName[PQ_FILE_NAME_LENGTH] = {0};

    if (enBinType < MAX_PQ_BIN_NUM)
    {
        switch (enBinType)
        {
            case PQ_BIN_STD_MAIN:
              strncpy(u8PQBinFilePathName, MainBinName, strlen(MainBinName));
              break;
            case PQ_BIN_STD_SUB:
              strncpy(u8PQBinFilePathName, SubBinName, strlen(SubBinName));
              break;
            case PQ_BIN_EXT_MAIN:
              strncpy(u8PQBinFilePathName, MainExBinName, strlen(MainExBinName));
              break;
            case PQ_BIN_EXT_SUB:
              strncpy(u8PQBinFilePathName, SubExBinName, strlen(SubExBinName));
              break;
            case PQ_BIN_CUSTOMER_MAIN:
              strncpy(u8PQBinFilePathName, MainColorBinName, strlen(MainColorBinName));
              break;
            case PQ_BIN_CUSTOMER_SUB:
              strncpy(u8PQBinFilePathName, SubColorBinName, strlen(SubColorBinName));
              break;
            case PQ_BIN_UFSC:
              strncpy(u8PQBinFilePathName, UFSCBinName, strlen(UFSCBinName));
              break;
#if (ENABLE_PQ_BIN_CF)
            case PQ_BIN_CF_MAIN:
              strncpy(u8PQBinFilePathName, MainCFBinName, strlen(MainCFBinName));
              break;
            case PQ_BIN_CF_SUB:
              strncpy(u8PQBinFilePathName, SubCFBinName, strlen(SubCFBinName));
              break;
#endif
            case PQ_BIN_TMO:
              strncpy(u8PQBinFilePathName, TMOBinName, strlen(TMOBinName));
              break;
#if (ENABLE_PQ_BIN_HSY)
            case PQ_BIN_HSY:
              strncpy(u8PQBinFilePathName, HSYcolorBinName, strlen(HSYcolorBinName));
              break;
#endif
            default:
              PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"enBinType >= MAX_PQ_BIN_NUM\n");
              break;
        }

        if (bUnitTest)
        {
            if (stPQMITestBin.u32PQMITestBinAddr[enBinType] != 0)
            {
                pu8BinAddr = (MS_U8 *)stPQMITestBin.u32PQMITestBinAddr[enBinType];
                PQ_FREE_MEM(pu8BinAddr, stPQMITestBin.u32TestBinSize[enBinType]);
                stPQMITestBin.u32PQMITestBinAddr[enBinType] = 0;
            }
            if (stPQMITestBin.u32PQMITestBinAddr[enBinType] == 0)
            {
                pu8BinAddr = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)u8PQBinFilePathName, &u32FileSize);
                stPQMITestBin.u32PQMITestBinAddr[enBinType] = (MS_PHYADDR)pu8BinAddr;
                stPQMITestBin.u32TestBinSize[enBinType] = u32FileSize;
                u32TmpBinAddr = (MS_PHYADDR)pu8BinAddr;
            }
        }
        else
        {
            u32TmpBinAddr = u32PQInitBinAddr[enBinType];
        }

        if (u32TmpBinAddr != 0)
        {
#if (ENABLE_PQ_BIN_HSY)
            if (enBinType == PQ_BIN_HSY)
            {
                stPQBinHeaderInfo[enBinType].u32BinStartAddress = u32TmpBinAddr;
                memset(&stForestMap, 0, sizeof(stForestMap));
                if (MDrv_ColorBin_Parse((MS_U8 *)_stPQ_Info.stPQBinInfo[PQ_BIN_HSY].PQBin_PhyAddr, &stForestMap) != E_COLOR_SUCCESS)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR, "parse colorbin fail!\n");
                }
                if(stForestMap.stForestHeader.u16MaxBinPerModel == 0)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"HSY BIN is empty.\n");
                    pstBinMap = NULL;
                }
                else
                {
                    if (pstBinMap != NULL)
                    {
                        PQ_FREE_MEM(pstBinMap, sizeof(MS_BIN_MAP)*stForestMap.stForestHeader.u16MaxBinPerModel);
                    }
                    PQ_MALLOC_MEM(pstBinMap, sizeof(MS_BIN_MAP)*stForestMap.stForestHeader.u16MaxBinPerModel);
                }
                if(pstBinMap != NULL)
                {
                    MDrv_HSYBin_Parse((MS_U8 *)_stPQ_Info.stPQBinInfo[PQ_BIN_HSY].PQBin_PhyAddr, &stForestMap, pstBinMap);
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"HSY BIN MAP malloc fail!\n");
                }
            }
            else
#endif
            {
                stPQBinHeaderInfo[enBinType].u32BinStartAddress = u32TmpBinAddr;
                MDrv_PQBin_Parsing(pInstance, &stPQBinHeaderInfo[enBinType]);
            }
        }
    }
}

#if !defined(MSOS_TYPE_LINUX_KERNEL)

MS_U8 *MDrv_PQ_LoadPQBinMethod(void *pInstance, char *BinName, MS_U32 *u32FileSize)//NonLinux
{
    MS_U8 *pu8PQBinBuf = NULL;
    if ((BinName!=NULL)&&(u32FileSize!=NULL))
    {
        PQ_INSTANCE_PRIVATE *psPQInstPri = NULL;
        UtopiaInstanceGetPrivate(pInstance, (void**)&psPQInstPri);
        PQ_RESOURCE_PRIVATE* pPQResourcePrivate = NULL;
        MDrv_PQ_Get_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
        UtopiaResourceGetPrivate(g_pPQResource[E_PQ_DEVICE_ID_0],(void**)(&pPQResourcePrivate));
        FILE *pFile = NULL;
        char* pDefaultPath = NULL;
        char* pCustomerPath = NULL;
        char* pIniPath = NULL;

        PQ_MALLOC_MEM(pDefaultPath , (PQ_FILE_PATH_LENGTH * sizeof(char)));
        PQ_MALLOC_MEM(pCustomerPath, (PQ_FILE_PATH_LENGTH * sizeof(char)));
        PQ_MALLOC_MEM(pIniPath     , (PQ_FILE_PATH_LENGTH * sizeof(char)));

        if((pDefaultPath == NULL) || (pCustomerPath == NULL) || (pIniPath == NULL))
        {
            PQ_FREE_MEM(pDefaultPath , (PQ_FILE_PATH_LENGTH * sizeof(char)));
            PQ_FREE_MEM(pCustomerPath, (PQ_FILE_PATH_LENGTH * sizeof(char)));
            PQ_FREE_MEM(pIniPath     , (PQ_FILE_PATH_LENGTH * sizeof(char)));

            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Malloc fail, can't get PQ BIN Addrss!!\n");
            return NULL;
        }
        else
        {
            memset(pDefaultPath , 0, (PQ_FILE_PATH_LENGTH * sizeof(char)));
            memset(pCustomerPath, 0, (PQ_FILE_PATH_LENGTH * sizeof(char)));
            memset(pIniPath     , 0, (PQ_FILE_PATH_LENGTH * sizeof(char)));
        }

        ///// SET PQ BIN PATH
        if (pPQResourcePrivate->stPQBin_Path.pm_PQ_DEFAULT_PATH[0] != 0) //Set DefaltPath
        {
            strncpy(pDefaultPath, pPQResourcePrivate->stPQBin_Path.pm_PQ_DEFAULT_PATH, PQ_FILE_PATH_LENGTH);
        }
        else
        {
            strncpy(pDefaultPath, DefaultPath_Ini, strlen(DefaultPath_Ini));
        }

        if (pPQResourcePrivate->stPQBin_Path.pm_PQ_CUSTOMER_PATH[0] != 0) //Set CustomerPath
        {
            strncpy(pCustomerPath, pPQResourcePrivate->stPQBin_Path.pm_PQ_CUSTOMER_PATH, PQ_FILE_PATH_LENGTH);
        }
        else
        {
            strncpy(pCustomerPath, CustomerPath_Ini, strlen(CustomerPath_Ini));
        }

        if (pPQResourcePrivate->stPQBin_Path.pm_PQ_INI_PATH[0] != 0) //Set IniPath
        {
            strncpy(pIniPath, pPQResourcePrivate->stPQBin_Path.pm_PQ_INI_PATH, PQ_FILE_PATH_LENGTH);
        }
        else
        {
            strncpy(pIniPath, DefaultPath_Ini, strlen(DefaultPath_Ini));
        }

        if(strlen(BinName)>PQ_FILE_PATH_LENGTH)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"STR Length overflow !\n");
            PQ_FREE_MEM(pDefaultPath , (PQ_FILE_PATH_LENGTH * sizeof(char)));
            PQ_FREE_MEM(pCustomerPath, (PQ_FILE_PATH_LENGTH * sizeof(char)));
            PQ_FREE_MEM(pIniPath     , (PQ_FILE_PATH_LENGTH * sizeof(char)));
            return NULL;
        }

        memset(pm_PQBinFilePathName, 0, PQ_FILE_PATH_LENGTH);
        strncpy(pm_PQBinFilePathName, pCustomerPath, strlen(pCustomerPath));
        strncat(pm_PQBinFilePathName, BinName, strlen(BinName));
        pFile = (FILE *)fopen(pm_PQBinFilePathName, "r");
        if (pFile == NULL)
        {
            memset(pm_PQBinFilePathName, 0, PQ_FILE_PATH_LENGTH);
            strncpy(pm_PQBinFilePathName, pIniPath, strlen(pIniPath));
            strncat(pm_PQBinFilePathName, BinName, strlen(BinName));
            pFile = (FILE *)fopen(pm_PQBinFilePathName, "r");
            if (pFile == NULL)
            {
                memset(pm_PQBinFilePathName, 0, PQ_FILE_PATH_LENGTH);
                strncpy(pm_PQBinFilePathName, pDefaultPath, strlen(pDefaultPath));
                strncat(pm_PQBinFilePathName, BinName, strlen(BinName));
                pFile = (FILE *)fopen(pm_PQBinFilePathName, "r");
            }
        }

        if (pFile != NULL)
        {
            fclose(pFile);
            pFile = NULL;
            pu8PQBinBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, u32FileSize);
        }
        else
        {
            pFile = NULL;
        }
        PQ_FREE_MEM(pDefaultPath , (PQ_FILE_PATH_LENGTH * sizeof(char)));
        PQ_FREE_MEM(pCustomerPath, (PQ_FILE_PATH_LENGTH * sizeof(char)));
        PQ_FREE_MEM(pIniPath     , (PQ_FILE_PATH_LENGTH * sizeof(char)));

        MDrv_PQ_Release_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
    }
    return pu8PQBinBuf;
}

void _MDrv_PQ_LoadPQBin(void* pInstance,MS_PQ_Init_Info *pstPQInitInfo)
{
    PQ_INSTANCE_PRIVATE *psPQInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psPQInstPri);
    PQ_RESOURCE_PRIVATE* pPQResourcePrivate = NULL;
    MDrv_PQ_Get_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
    UtopiaResourceGetPrivate(g_pPQResource[E_PQ_DEVICE_ID_0],(void**)(&pPQResourcePrivate));
    MS_U32 u32FileSize = 0;
    if ((MDrv_SYS_GetChipType() == E_SYS_CHIP_STB) && (USE_PQ_BIN == FALSE))
    {
        pstPQInitInfo->u8PQBinCnt = 0;
        pstPQInitInfo->u8PQTextBinCnt = 0;
        pstPQInitInfo->u8PQBinCustomerCnt = 0;
        pstPQInitInfo->u8PQBinTMOCnt = 0;
    }
    else
    {
        MS_PQ_CAP_INFO *pstPQCaps;
        PQ_MALLOC_MEM(pstPQCaps,sizeof(MS_PQ_CAP_INFO));
        if (pstPQCaps==NULL)
        {
          return;
        }
        MDrv_PQ_GetCaps(pstPQCaps);
        if(pstPQCaps->bPQBin_Enable == TRUE)
        {
            MS_U8 *pu8MainBuf= NULL, *pu8MainExBuf= NULL;

            char DefaultPath[PQ_FILE_NAME_LENGTH]={0};
            char CustomerPath[PQ_FILE_NAME_LENGTH]={0};
            char IniPath[PQ_FILE_NAME_LENGTH]={0};



            ///// SET PQ BIN PATH
            if(pPQResourcePrivate->stPQBin_Path.pm_PQ_DEFAULT_PATH[0] !=0) //Set DefaultPath
            {
                strncpy(DefaultPath,pPQResourcePrivate->stPQBin_Path.pm_PQ_DEFAULT_PATH,PQ_FILE_PATH_LENGTH);
            }
            else
            {
                strncpy(DefaultPath,DefaultPath_Ini,strlen(DefaultPath_Ini));
            }

            if(pPQResourcePrivate->stPQBin_Path.pm_PQ_CUSTOMER_PATH[0] !=0) //Set CustomerPath
            {
                strncpy(CustomerPath,pPQResourcePrivate->stPQBin_Path.pm_PQ_CUSTOMER_PATH,PQ_FILE_PATH_LENGTH);
            }
            else
            {
                strncpy(CustomerPath,CustomerPath_Ini,strlen(CustomerPath_Ini));
            }

            if(pPQResourcePrivate->stPQBin_Path.pm_PQ_INI_PATH[0] !=0) //Set IniPath
            {
                strncpy(IniPath,pPQResourcePrivate->stPQBin_Path.pm_PQ_INI_PATH,PQ_FILE_PATH_LENGTH);
            }

            FILE *pFile = NULL;

            // if Main.bin exists in /Customer/pq, read it
            memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
            strncpy(pm_PQBinFilePathName, CustomerPath, strlen(CustomerPath));
            strncat(pm_PQBinFilePathName, MainBinName, strlen(MainBinName));
            pFile = fopen(pm_PQBinFilePathName, "r");
            if(pFile == NULL)
            {
                memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                strncpy(pm_PQBinFilePathName, IniPath, strlen(IniPath));
                strncat(pm_PQBinFilePathName, MainBinName, strlen(MainBinName));
                pFile = fopen(pm_PQBinFilePathName, "r");
                if(pFile == NULL)
                {
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath, strlen(DefaultPath));
                    strncat(pm_PQBinFilePathName, MainBinName, strlen(MainBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                }

                ///For AN7.1 the Bin Path is in /tvconfig/config/pq,inorder to Handle Usermode PQDS case,We need to check PQ bin Path for /tvconfig/config/pq
                if(pFile == NULL)
                {
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath_Ini_Ext, strlen(DefaultPath_Ini_Ext));
                    strncat(pm_PQBinFilePathName, MainBinName, strlen(MainBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                }
                if (pFile != NULL)
                {
                    fclose(pFile);
                    pFile = NULL;
                    pu8MainBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                }
                else
                {
                   pFile = NULL;
                }
            }
            else
            {
                // read from /Customer/pq
                fclose(pFile);
                pFile = NULL;
                pu8MainBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
            }
            PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Load PQ bin file, PQ_BIN_STD_MAIN path [%s]\n", pm_PQBinFilePathName);
            MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_STD_MAIN);

            // if Main_Ex.bin exists in /Customer/pq, read it
            memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
            strncpy(pm_PQBinFilePathName, CustomerPath, strlen(CustomerPath));
            strncat(pm_PQBinFilePathName, MainExBinName, strlen(MainExBinName));
            pFile = fopen(pm_PQBinFilePathName, "r");
            if(pFile == NULL)
            {
                memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                strncpy(pm_PQBinFilePathName, IniPath, strlen(IniPath));
                strncat(pm_PQBinFilePathName, MainExBinName, strlen(MainExBinName));
                pFile = fopen(pm_PQBinFilePathName, "r");
                if(pFile == NULL)
                {
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath, strlen(DefaultPath));
                    strncat(pm_PQBinFilePathName, MainExBinName, strlen(MainExBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                }
                ///For AN7.1 the Bin Path is in /tvconfig/config/pq,inorder to Handle Usermode PQDS case,We need to check PQ bin Path for /tvconfig/config/pq
                if(pFile == NULL)
                {
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath_Ini_Ext, strlen(DefaultPath_Ini_Ext));
                    strncat(pm_PQBinFilePathName, MainExBinName, strlen(MainExBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                }
                if (pFile != NULL)
                {
                    fclose(pFile);
                    pFile = NULL;
                    pu8MainExBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                }
                else
                {
                    pFile = NULL;
                }
            }
            else
            {
                // read from /Customer/pq
                fclose(pFile);
                pFile = NULL;
                pu8MainExBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
            }
            PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Load PQ bin file, PQ_BIN_EXT_MAIN path [%s]\n", pm_PQBinFilePathName);
            MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_EXT_MAIN);

            if(pu8MainBuf && pu8MainExBuf)
            {
                pstPQInitInfo->u8PQBinCnt = 2;
                pstPQInitInfo->stPQBinInfo[0].u8PQID = (MS_U8) PQ_BIN_STD_MAIN;
                pstPQInitInfo->stPQBinInfo[0].PQBin_PhyAddr = (MS_PHYADDR) pu8MainBuf;
                pstPQInitInfo->stPQBinInfo[1].u8PQID = (MS_U8) PQ_BIN_EXT_MAIN;
                pstPQInitInfo->stPQBinInfo[1].PQBin_PhyAddr = (MS_PHYADDR)pu8MainExBuf;
            }
            else
            {
                pstPQInitInfo->u8PQBinCnt = 0;

                if(pu8MainBuf)
                {
                    free(pu8MainBuf);
                }

                if(pu8MainExBuf)
                {
                    free(pu8MainExBuf);
                }
            }

            MS_U8 *pu8MainCustomerBuf = NULL;
            pstPQInitInfo->u8PQBinCustomerCnt = 0 ;
            // if Main_Color.bin exists in /Customer/pq, read it
            memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
            strncpy(pm_PQBinFilePathName, CustomerPath, strlen(CustomerPath));
            strncat(pm_PQBinFilePathName, MainColorBinName, strlen(MainColorBinName));
            pFile = fopen(pm_PQBinFilePathName, "r");
            if(pFile == NULL)
            {
                // if exists in ini path, read it
                memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                strncpy(pm_PQBinFilePathName, IniPath, strlen(IniPath));
                strncat(pm_PQBinFilePathName, MainColorBinName, strlen(MainColorBinName));
                pFile = fopen(pm_PQBinFilePathName, "r");
                if (pFile == NULL)
                {
                    // if exists in default path, read it
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath, strlen(DefaultPath));
                    strncat(pm_PQBinFilePathName, MainColorBinName, strlen(MainColorBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");

                    ///For AN7.1 the Bin Path is in /tvconfig/config/pq,inorder to Handle Usermode PQDS case,We need to check PQ bin Path for /tvconfig/config/pq
                    if (pFile == NULL)
                    {
                        memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                        strncpy(pm_PQBinFilePathName, DefaultPath_Ini_Ext, strlen(DefaultPath_Ini_Ext));
                        strncat(pm_PQBinFilePathName, MainColorBinName, strlen(MainColorBinName));
                        pFile = fopen(pm_PQBinFilePathName, "r");
                        if (pFile != NULL)
                        {
                            fclose(pFile);
                            pFile = NULL;
                            pu8MainCustomerBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                        }
                        else
                        {
                            PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!!Fail Load PQ bin file, Main_Color path [%s]\n", pm_PQBinFilePathName);
                        }
                    }
                    else
                    {
                        // read from default path
                        fclose(pFile);
                        pFile = NULL;
                        pu8MainCustomerBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                    }
                }
                else
                {
                    // read from ini path
                    fclose(pFile);
                    pFile = NULL;
                    pu8MainCustomerBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                }
            }
            else
            {
                // read from /Customer/pq
                fclose(pFile);
                pFile = NULL;
                pu8MainCustomerBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
            }
            PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Load PQ bin file, PQ_BIN_CUSTOMER_MAIN path [%s]\n", pm_PQBinFilePathName);
            MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_CUSTOMER_MAIN);

            if(pu8MainCustomerBuf)
            {
                pstPQInitInfo->u8PQBinCustomerCnt = 1;
                pstPQInitInfo->stPQBinInfo[4].u8PQID = (MS_U8) PQ_BIN_CUSTOMER_MAIN;
                pstPQInitInfo->stPQBinInfo[4].PQBin_PhyAddr = (MS_PHYADDR) pu8MainCustomerBuf;
            }
            else
            {
                pstPQInitInfo->u8PQBinCustomerCnt = 0;

            }

           //Skip USFC now, which may cause kernel panic, wait PQ Driver complated.

            MS_U8 *pu8UFSCBuf = NULL;
            pstPQInitInfo->u8PQBinUFSCCnt = 0;
            // if UFSC.bin exists in /Customer/pq, read it
            memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
            strncpy(pm_PQBinFilePathName, CustomerPath, strlen(CustomerPath));
            strncat(pm_PQBinFilePathName, UFSCBinName, strlen(UFSCBinName));
            pFile = fopen(pm_PQBinFilePathName, "r");
            if(pFile == NULL)
            {
                memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                strncpy(pm_PQBinFilePathName, IniPath, strlen(IniPath));
                strncat(pm_PQBinFilePathName, UFSCBinName, strlen(UFSCBinName));
                pFile = fopen(pm_PQBinFilePathName, "r");
                if(pFile == NULL)
                {
                    // read from default path
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath, strlen(DefaultPath));
                    strncat(pm_PQBinFilePathName, UFSCBinName, strlen(UFSCBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                }
                ///For AN7.1 the Bin Path is in /tvconfig/config/pq,inorder to Handle Usermode PQDS case,We need to check PQ bin Path for /tvconfig/config/pq
                if(pFile == NULL)
                {
                    // read from default path
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath_Ini_Ext, strlen(DefaultPath_Ini_Ext));
                    strncat(pm_PQBinFilePathName, UFSCBinName, strlen(UFSCBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                }
                if (pFile != NULL)
                {
                    fclose(pFile);
                    pFile = NULL;
                    pu8UFSCBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                }
                else
                {
                    pFile = NULL;
                }
            }
            else
            {
                // read from /Customer/pq
                fclose(pFile);
                pFile = NULL;
                pu8UFSCBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
            }

            if(pu8UFSCBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Load PQ bin file, PQ_BIN_UFSC path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_UFSC);

                pstPQInitInfo->u8PQBinUFSCCnt = 1;
                pstPQInitInfo->stPQBinInfo[6].u8PQID = (MS_U8) PQ_BIN_UFSC;
                pstPQInitInfo->stPQBinInfo[6].PQBin_PhyAddr = (MS_PHYADDR) pu8UFSCBuf;
            }
            else
            {
                pstPQInitInfo->u8PQBinUFSCCnt = 0;
                XC_INITMISC *psXC_Init_Misc;
                PQ_MALLOC_MEM(psXC_Init_Misc,sizeof(XC_INITMISC));
                if (psXC_Init_Misc==NULL)
                {
                    return;
                }
                MApi_XC_GetMISCStatus(psXC_Init_Misc);
                psXC_Init_Misc->u32MISC_B |= E_XC_INIT_MISC_B_SKIP_SR;
                MApi_XC_Init_MISC(psXC_Init_Misc, sizeof(XC_INITMISC));
                PQ_FREE_MEM(psXC_Init_Misc,sizeof(XC_INITMISC));
            }
            #if (ENABLE_PQ_BIN_CF)
            // load Main CF Qmap bin

            MS_U8 *pu8MainCFBuf = NULL;
            pstPQInitInfo->u8PQBinCFCnt = 0;
            // if Main_CF.bin exists in /Customer/pq, read it
            memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
            strncpy(pm_PQBinFilePathName, CustomerPath, strlen(CustomerPath));
            strncat(pm_PQBinFilePathName, MainCFBinName, strlen(MainCFBinName));
            pFile = fopen(pm_PQBinFilePathName, "r");
            if(pFile == NULL)
            {
                memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                strncpy(pm_PQBinFilePathName, IniPath, strlen(IniPath));
                strncat(pm_PQBinFilePathName, MainCFBinName, strlen(MainCFBinName));
                pFile = fopen(pm_PQBinFilePathName, "r");
                if(pFile == NULL)
                {
                    // read from default path
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath, strlen(DefaultPath));
                    strncat(pm_PQBinFilePathName, MainCFBinName, strlen(MainCFBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                }
                ///For AN7.1 the Bin Path is in /tvconfig/config/pq,inorder to Handle Usermode PQDS case,We need to check PQ bin Path for /tvconfig/config/pq
                if(pFile == NULL)
                {
                    // read from default path
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath_Ini_Ext, strlen(DefaultPath_Ini_Ext));
                    strncat(pm_PQBinFilePathName, MainCFBinName, strlen(MainCFBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                }
                if (pFile != NULL)
                {
                    fclose(pFile);
                    pFile = NULL;
                    pu8MainCFBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                }
                else
                {
                    pFile = NULL;
                }
            }
            else
            {
                // read from /Customer/pq
                fclose(pFile);
                pFile = NULL;
                pu8MainCFBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
            }

            if(pu8MainCFBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Load PQ bin file, PQ_BIN_CF_MAIN path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_CF_MAIN);

                pstPQInitInfo->u8PQBinCFCnt = 1;
                pstPQInitInfo->stPQBinInfo[7].u8PQID = (MS_U8) PQ_BIN_CF_MAIN;
                pstPQInitInfo->stPQBinInfo[7].PQBin_PhyAddr = (MS_PHYADDR) pu8MainCFBuf;
            }
#endif

            MS_U8 *pu8TMOBuf = NULL;
            pstPQInitInfo->u8PQBinTMOCnt = 0;
            // if UFSC.bin exists in /Customer/pq, read it
            memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
            strncpy(pm_PQBinFilePathName, CustomerPath, strlen(CustomerPath));
            strncat(pm_PQBinFilePathName, TMOBinName, strlen(TMOBinName));

            pFile = fopen(pm_PQBinFilePathName, "r");

            if(pFile == NULL)
            {
                // read from default path
                memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                strncpy(pm_PQBinFilePathName, DefaultPath, strlen(DefaultPath));
                strncat(pm_PQBinFilePathName, TMOBinName, strlen(TMOBinName));

                pFile = fopen(pm_PQBinFilePathName, "r");

                ///For AN7.1 the Bin Path is in /tvconfig/config/pq,inorder to Handle Usermode PQDS case,We need to check PQ bin Path for /tvconfig/config/pq
                if(pFile == NULL)
                {
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath_Ini_Ext, strlen(DefaultPath_Ini_Ext));
                    strncat(pm_PQBinFilePathName, TMOBinName, strlen(TMOBinName));

                    pFile = fopen(pm_PQBinFilePathName, "r");
                }
                if (pFile != NULL)
                {
                    fclose(pFile);
                    pFile = NULL;
                    pu8TMOBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                }
                else
                {
                    pu8TMOBuf = NULL;
                }
            }
            else
            {
                // read from /Customer/pq
                fclose(pFile);
                pFile = NULL;
                pu8TMOBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
            }

            if(pu8TMOBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Load PQ bin file, PQ_BIN_TMO path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_TMO);

                pstPQInitInfo->u8PQBinTMOCnt = 1;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_TMO].u8PQID = (MS_U8) PQ_BIN_TMO;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_TMO].PQBin_PhyAddr = (MS_PHYADDR) pu8TMOBuf;
            }
            else
            {
                pstPQInitInfo->u8PQBinTMOCnt = 0;
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!!Fail Load PQ bin file, PQ_BIN_CUSTOMER_MAIN path [%s]\n", pm_PQBinFilePathName);

            }

            if(pstPQCaps->b3DVideo_Supported || pstPQCaps->bPIP_Supported)
            {
                MS_U8 *pu8SubBuf= NULL, *pu8SubExBuf= NULL ;

                // check if Sub.bin exists in /Customer/pq/
                memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                strncpy(pm_PQBinFilePathName, CustomerPath, strlen(CustomerPath));
                strncat(pm_PQBinFilePathName, SubBinName, strlen(SubBinName));
                pFile = fopen(pm_PQBinFilePathName, "r");
                if(pFile == NULL)
                {
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, IniPath, strlen(IniPath));
                    strncat(pm_PQBinFilePathName, SubBinName, strlen(SubBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                    if(pFile == NULL)
                    {
                        // read from default path
                        memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                        strncpy(pm_PQBinFilePathName,DefaultPath,strlen(DefaultPath));
                        strncat(pm_PQBinFilePathName,SubBinName,strlen(SubBinName));
                        pFile = fopen(pm_PQBinFilePathName, "r");
                    }
                    ///For AN7.1 the Bin Path is in /tvconfig/config/pq,inorder to Handle Usermode PQDS case,We need to check PQ bin Path for /tvconfig/config/pq
                    if(pFile == NULL)
                    {
                        // read from default path
                        memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                        strncpy(pm_PQBinFilePathName,DefaultPath_Ini_Ext,strlen(DefaultPath_Ini_Ext));
                        strncat(pm_PQBinFilePathName,SubBinName,strlen(SubBinName));
                        pFile = fopen(pm_PQBinFilePathName, "r");
                    }
                    if (pFile != NULL)
                    {
                        fclose(pFile);
                        pFile = NULL;
                        pu8SubBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                    }
                    else
                    {
                        pFile = NULL;
                    }
                }
                else
                {
                    // read from /Customer/pq
                    fclose(pFile);
                    pFile = NULL;
                    pu8SubBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                }
                PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Load PQ bin file, PQ_BIN_STD_SUB path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_STD_SUB);

                // check if Sub_Ex.bin exists in /Customer/pq/
                memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                strncpy(pm_PQBinFilePathName, CustomerPath, strlen(CustomerPath));
                strncat(pm_PQBinFilePathName, SubExBinName, strlen(SubExBinName));
                pFile = fopen(pm_PQBinFilePathName, "r");
                if(pFile == NULL)
                {
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, IniPath, strlen(IniPath));
                    strncat(pm_PQBinFilePathName, SubExBinName, strlen(SubExBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                    if(pFile == NULL)
                    {
                    // read from default path
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName,DefaultPath,strlen(DefaultPath));
                    strncat(pm_PQBinFilePathName,SubExBinName,strlen(SubExBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                    }
                    ///For AN7.1 the Bin Path is in /tvconfig/config/pq,inorder to Handle Usermode PQDS case,We need to check PQ bin Path for /tvconfig/config/pq
                    if(pFile == NULL)
                    {
                    // read from default path
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName,DefaultPath_Ini_Ext,strlen(DefaultPath_Ini_Ext));
                    strncat(pm_PQBinFilePathName,SubExBinName,strlen(SubExBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                    }
                    if (pFile != NULL)
                    {
                        fclose(pFile);
                        pFile = NULL;
                        pu8SubExBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                    }
                    else
                    {
                        pFile = NULL;
                    }
                }
                else
                {
                    // read from /Customer/pq
                    fclose(pFile);
                    pFile = NULL;
                    pu8SubExBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                }
                PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Load PQ bin file, PQ_BIN_EXT_SUB path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_EXT_SUB);

                if(pu8SubBuf && pu8SubExBuf)
                {
                    pstPQInitInfo->u8PQBinCnt = 4;
                    pstPQInitInfo->stPQBinInfo[2].u8PQID = (MS_U8) PQ_BIN_STD_SUB;
                    pstPQInitInfo->stPQBinInfo[2].PQBin_PhyAddr = (MS_PHYADDR) pu8SubBuf;
                    pstPQInitInfo->stPQBinInfo[3].u8PQID = (MS_U8) PQ_BIN_EXT_SUB;
                    pstPQInitInfo->stPQBinInfo[3].PQBin_PhyAddr = (MS_PHYADDR)pu8SubExBuf;
                }

                MS_U8  *pu8SubCustomerBuf = NULL;
                // check if Sub_Color.bin exists in /Customer/pq/
                memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                strncpy(pm_PQBinFilePathName, CustomerPath, strlen(CustomerPath));
                strncat(pm_PQBinFilePathName, SubColorBinName, strlen(SubColorBinName));
                pFile = fopen(pm_PQBinFilePathName, "r");
                if(pFile == NULL)
                {
                    // if exists in ini path
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, IniPath, strlen(IniPath));
                    strncat(pm_PQBinFilePathName, SubColorBinName, strlen(SubColorBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                    if (pFile == NULL)
                    {
                        // if exists in default path
                        memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                        strncpy(pm_PQBinFilePathName, DefaultPath, strlen(DefaultPath));
                        strncat(pm_PQBinFilePathName, SubColorBinName, strlen(SubColorBinName));
                        pFile = fopen(pm_PQBinFilePathName, "r");
                        ///For AN7.1 the Bin Path is in /tvconfig/config/pq,inorder to Handle Usermode PQDS case,We need to check PQ bin Path for /tvconfig/config/pq
                        if (pFile == NULL)
                        {
                            memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                            strncpy(pm_PQBinFilePathName, DefaultPath_Ini_Ext, strlen(DefaultPath_Ini_Ext));
                            strncat(pm_PQBinFilePathName, SubColorBinName, strlen(SubColorBinName));
                            pFile = fopen(pm_PQBinFilePathName, "r");
                            if (pFile != NULL)
                            {
                                fclose(pFile);
                                pFile = NULL;
                                pu8SubCustomerBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                            }
                            else
                            {
                                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Load Sub_Color.bin file failed!");
                            }
                        }
                        else
                        {
                            // read from default path
                            fclose(pFile);
                            pFile = NULL;
                            pu8SubCustomerBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                        }
                    }
                    else
                    {
                        // read from ini path
                        fclose(pFile);
                        pFile = NULL;
                        pu8SubCustomerBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                    }
                }
                else
                {
                    // read from /Customer/pq
                    fclose(pFile);
                    pFile = NULL;
                    pu8SubCustomerBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                }

                if(pu8MainCustomerBuf && pu8SubCustomerBuf)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Load PQ bin file, PQ_BIN_CUSTOMER_SUB path [%s]\n", pm_PQBinFilePathName);
                    MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_CUSTOMER_SUB);

                    pstPQInitInfo->u8PQBinCustomerCnt = 2;
                    pstPQInitInfo->stPQBinInfo[5].u8PQID = (MS_U8) PQ_BIN_CUSTOMER_SUB;
                    pstPQInitInfo->stPQBinInfo[5].PQBin_PhyAddr = (MS_PHYADDR) pu8SubCustomerBuf;
                }
            }
#if (ENABLE_PQ_BIN_CF)
            // load Sub CF Qmap bin

            MS_U8 *pu8SubCFBuf = NULL;
            // if Sub_CF.bin exists in /Customer/pq, read it
            memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
            strncpy(pm_PQBinFilePathName, CustomerPath, strlen(CustomerPath));
            strncat(pm_PQBinFilePathName, SubCFBinName, strlen(SubCFBinName));
            pFile = fopen(pm_PQBinFilePathName, "r");
            if(pFile == NULL)
            {
                memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                strncpy(pm_PQBinFilePathName, IniPath, strlen(IniPath));
                strncat(pm_PQBinFilePathName, SubCFBinName, strlen(SubCFBinName));
                pFile = fopen(pm_PQBinFilePathName, "r");
                if(pFile == NULL)
                {
                    // read from default path
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath, strlen(DefaultPath));
                    strncat(pm_PQBinFilePathName, SubCFBinName, strlen(SubCFBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                }
                ///For AN7.1 the Bin Path is in /tvconfig/config/pq,inorder to Handle Usermode PQDS case,We need to check PQ bin Path for /tvconfig/config/pq
                if(pFile == NULL)
                {
                    // read from default path
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath_Ini_Ext, strlen(DefaultPath_Ini_Ext));
                    strncat(pm_PQBinFilePathName, SubCFBinName, strlen(SubCFBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                }
                if (pFile != NULL)
                {
                    fclose(pFile);
                    pFile = NULL;
                    pu8SubCFBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                }
                else
                {
                    pFile = NULL;
                }
            }
            else
            {
                // read from /Customer/pq
                fclose(pFile);
                pFile = NULL;
                pu8SubCFBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
            }

            if(pu8SubCFBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Load PQ bin file, PQ_BIN_CF_SUB path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_CF_SUB);

                pstPQInitInfo->u8PQBinCFCnt = 2;
                pstPQInitInfo->stPQBinInfo[8].u8PQID = (MS_U8) PQ_BIN_CF_SUB;
                pstPQInitInfo->stPQBinInfo[8].PQBin_PhyAddr = (MS_PHYADDR) pu8SubCFBuf;
            }
#endif
#if (ENABLE_PQ_BIN_HSY)
            // if HYS.bin exists in /Customer/pq, read it
            MS_U8 *pu8HSYBuf = NULL;
            memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
            strncpy(pm_PQBinFilePathName, CustomerPath, strlen(CustomerPath));
            strncat(pm_PQBinFilePathName, HSYcolorBinName, strlen(HSYcolorBinName));
            pFile = fopen(pm_PQBinFilePathName, "r");

            if(pFile == NULL)
            {
                memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                strncpy(pm_PQBinFilePathName, IniPath, strlen(IniPath));
                strncat(pm_PQBinFilePathName, HSYcolorBinName, strlen(HSYcolorBinName));
                pFile = fopen(pm_PQBinFilePathName, "r");
                if(pFile == NULL)
                {
                    // read from default path
                    memset(pm_PQBinFilePathName, 0, PQ_FILE_NAME_LENGTH);
                    strncpy(pm_PQBinFilePathName, DefaultPath, strlen(DefaultPath));
                    strncat(pm_PQBinFilePathName, HSYcolorBinName, strlen(HSYcolorBinName));
                    pFile = fopen(pm_PQBinFilePathName, "r");
                }
                if(pFile == NULL)
                {
                    pFile = NULL;
                    u8PQHSYBinCnt = 0;
                }
                else
                {
                    fclose(pFile);
                    pFile = NULL;
                    pu8HSYBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                    u8PQHSYBinCnt = 1;
                }

            }
            else
            {
                // read from /Customer/pq
                fclose(pFile);
                pFile = NULL;
                pu8HSYBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, &u32FileSize);
                u8PQHSYBinCnt = 1;
            }
            if(u8PQHSYBinCnt)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_HYS path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_HSY);

                pstPQInitInfo->stPQBinInfo[PQ_BIN_HSY].PQBin_PhyAddr = (MS_PHYADDR) pu8HSYBuf;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_HSY].u8PQID = (MS_U8) PQ_BIN_HSY;
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!!Fail Load PQ bin file, PQ_BIN_HYS path [%s]\n", pm_PQBinFilePathName);
            }
#endif
        }
        else
        {
            pstPQInitInfo->u8PQBinCnt = 0;
            pstPQInitInfo->u8PQTextBinCnt = 0;
            pstPQInitInfo->u8PQBinCustomerCnt = 0;
            pstPQInitInfo->u8PQBinUFSCCnt = 0;
            pstPQInitInfo->u8PQBinTMOCnt = 0;
#if (ENABLE_PQ_BIN_CF)
            pstPQInitInfo->u8PQBinCFCnt = 0;
#endif
        }
        PQ_FREE_MEM(pstPQCaps,sizeof(MS_PQ_CAP_INFO));
    }
    MDrv_PQ_Release_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
}
#else
MS_U8 *MDrv_PQ_LoadPQBinMethod(void *pInstance, char *BinName, MS_U32 *u32FileSize)
{
    MS_U8 *pu8PQBinBuf = NULL;
    if ((BinName!=NULL)&&(u32FileSize!=NULL))
    {
        PQ_INSTANCE_PRIVATE *psPQInstPri = NULL;
        UtopiaInstanceGetPrivate(pInstance, (void**)&psPQInstPri);
        PQ_RESOURCE_PRIVATE* pPQResourcePrivate = NULL;
        MDrv_PQ_Get_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
        UtopiaResourceGetPrivate(g_pPQResource[E_PQ_DEVICE_ID_0],(void**)(&pPQResourcePrivate));
        struct file *pFile = NULL;
        char* pDefaultPath = NULL;
        char* pCustomerPath = NULL;
        char* pIniPath = NULL;

        PQ_MALLOC_MEM(pDefaultPath , (PQ_FILE_PATH_LENGTH * sizeof(char)));
        PQ_MALLOC_MEM(pCustomerPath, (PQ_FILE_PATH_LENGTH * sizeof(char)));
        PQ_MALLOC_MEM(pIniPath     , (PQ_FILE_PATH_LENGTH * sizeof(char)));

        if((pDefaultPath == NULL) || (pCustomerPath == NULL) || (pIniPath == NULL))
        {
            PQ_FREE_MEM(pDefaultPath , (PQ_FILE_PATH_LENGTH * sizeof(char)));
            PQ_FREE_MEM(pCustomerPath, (PQ_FILE_PATH_LENGTH * sizeof(char)));
            PQ_FREE_MEM(pIniPath     , (PQ_FILE_PATH_LENGTH * sizeof(char)));

            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Malloc fail, can't get PQ BIN Addrss!!\n");
            return NULL;
        }
        else
        {
            memset(pDefaultPath , 0, (PQ_FILE_PATH_LENGTH * sizeof(char)));
            memset(pCustomerPath, 0, (PQ_FILE_PATH_LENGTH * sizeof(char)));
            memset(pIniPath     , 0, (PQ_FILE_PATH_LENGTH * sizeof(char)));
        }

        ///// SET PQ BIN PATH
        if (pPQResourcePrivate->stPQBin_Path.pm_PQ_DEFAULT_PATH[0] != NULL) //Set DefaltPath
        {
            strncpy(pDefaultPath, pPQResourcePrivate->stPQBin_Path.pm_PQ_DEFAULT_PATH, PQ_FILE_PATH_LENGTH);
        }
        else
        {
            strncpy(pDefaultPath, DefaultPath_Ini, strlen(DefaultPath_Ini));
        }

        if (pPQResourcePrivate->stPQBin_Path.pm_PQ_CUSTOMER_PATH[0] != NULL) //Set CustomerPath
        {
            strncpy(pCustomerPath, pPQResourcePrivate->stPQBin_Path.pm_PQ_CUSTOMER_PATH, PQ_FILE_PATH_LENGTH);
        }
        else
        {
            strncpy(pCustomerPath, CustomerPath_Ini, strlen(CustomerPath_Ini));
        }

        if (pPQResourcePrivate->stPQBin_Path.pm_PQ_INI_PATH[0] != NULL) //Set IniPath
        {
            strncpy(pIniPath, pPQResourcePrivate->stPQBin_Path.pm_PQ_INI_PATH, PQ_FILE_PATH_LENGTH);
        }
        else
        {
            strncpy(pIniPath, DefaultPath_Ini, strlen(DefaultPath_Ini));
        }

        if(strlen(BinName)>PQ_FILE_PATH_LENGTH)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"STR Length overflow !\n");
            PQ_FREE_MEM(pDefaultPath , (PQ_FILE_PATH_LENGTH * sizeof(char)));
            PQ_FREE_MEM(pCustomerPath, (PQ_FILE_PATH_LENGTH * sizeof(char)));
            PQ_FREE_MEM(pIniPath     , (PQ_FILE_PATH_LENGTH * sizeof(char)));
            return NULL;
        }

        memset(pm_PQBinFilePathName, 0, PQ_FILE_PATH_LENGTH);
        strncpy(pm_PQBinFilePathName, pCustomerPath, strlen(pCustomerPath));
        strncat(pm_PQBinFilePathName, BinName, strlen(BinName));
        pFile = (struct file *)filp_open(pm_PQBinFilePathName, O_RDONLY, 0);
        if (IS_ERR(pFile))
        {
            memset(pm_PQBinFilePathName, 0, PQ_FILE_PATH_LENGTH);
            strncpy(pm_PQBinFilePathName, pIniPath, strlen(pIniPath));
            strncat(pm_PQBinFilePathName, BinName, strlen(BinName));
            pFile = (struct file *)filp_open(pm_PQBinFilePathName, O_RDONLY, 0);
            if (IS_ERR(pFile))
            {
                memset(pm_PQBinFilePathName, 0, PQ_FILE_PATH_LENGTH);
                strncpy(pm_PQBinFilePathName, pDefaultPath, strlen(pDefaultPath));
                strncat(pm_PQBinFilePathName, BinName, strlen(BinName));
                pFile = (struct file *)filp_open(pm_PQBinFilePathName, O_RDONLY, 0);
            }
        }

        if (IS_ERR(pFile))
        {
            pFile = NULL;
        }
        else
        {
            filp_close(pFile, NULL);
            pFile = NULL;
            pu8PQBinBuf = Open_PQ_Bin((MS_U8 *)pm_PQBinFilePathName, u32FileSize);
        }
        PQ_FREE_MEM(pDefaultPath , (PQ_FILE_PATH_LENGTH * sizeof(char)));
        PQ_FREE_MEM(pCustomerPath, (PQ_FILE_PATH_LENGTH * sizeof(char)));
        PQ_FREE_MEM(pIniPath     , (PQ_FILE_PATH_LENGTH * sizeof(char)));

        MDrv_PQ_Release_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
    }
    return pu8PQBinBuf;
}

void _MDrv_PQ_LoadPQBin(void* pInstance,MS_PQ_Init_Info *pstPQInitInfo)
{
    MS_U32 u32FileSize = 0;
    if ((MDrv_SYS_GetChipType() == E_SYS_CHIP_STB) && (USE_PQ_BIN == FALSE))
    {
        pstPQInitInfo->u8PQBinCnt = 0;
        pstPQInitInfo->u8PQTextBinCnt = 0;
        pstPQInitInfo->u8PQBinCustomerCnt = 0;
    }
    else   //TV case
    {
        MS_PQ_CAP_INFO *pstPQCaps;
        PQ_MALLOC_MEM(pstPQCaps,sizeof(MS_PQ_CAP_INFO));
        if (pstPQCaps==NULL)
        {
          return;
        }
        MDrv_PQ_GetCaps(pstPQCaps);
        if(pstPQCaps->bPQBin_Enable == TRUE)
        {
        //[Main.bin]=============================================================
            MS_U8 *pu8MainBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)MainBinName, &u32FileSize);
            if (pu8MainBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_STD_MAIN path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_STD_MAIN);
                pstPQInitInfo->u8PQBinCnt = 1;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_MAIN].u8PQID = (MS_U8) PQ_BIN_STD_MAIN;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_MAIN].PQBin_PhyAddr = (MS_PHYADDR) pu8MainBuf;
            }
            else
            {
                pstPQInitInfo->u8PQBinCnt = 0;
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!! Error Load PQ bin file, PQ_BIN_MAIN path [%s]\n", pm_PQBinFilePathName);
            }

        //[Main_Ex.bin]=============================================================
            MS_U8 *pu8MainExBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)MainExBinName, &u32FileSize);
            if (pu8MainBuf && pu8MainExBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_EXT_MAIN path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_EXT_MAIN);
                pstPQInitInfo->u8PQBinCnt = 2;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_MAIN].u8PQID = (MS_U8) PQ_BIN_STD_MAIN;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_MAIN].PQBin_PhyAddr = (MS_PHYADDR) pu8MainBuf;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_EXT_MAIN].u8PQID = (MS_U8) PQ_BIN_EXT_MAIN;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_EXT_MAIN].PQBin_PhyAddr = (MS_PHYADDR)pu8MainExBuf;
            }
            else
            {
                pstPQInitInfo->u8PQBinCnt = 0;

                if(pu8MainBuf)
                {
                    vfree(pu8MainBuf);
                }

                if(pu8MainExBuf)
                {
                    vfree(pu8MainExBuf);
                }
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!! Error Load PQ bin file, PQ_BIN_EXT_MAIN path [%s]\n", pm_PQBinFilePathName);
            }

        //[Main_Color.bin]=============================================================
            MS_U8 *pu8MainCustomerBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)MainColorBinName, &u32FileSize);
            if (pu8MainCustomerBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_CUSTOMER_MAIN path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_CUSTOMER_MAIN);
                pstPQInitInfo->u8PQBinCustomerCnt = 1;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_CUSTOMER_MAIN].u8PQID = (MS_U8) PQ_BIN_CUSTOMER_MAIN;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_CUSTOMER_MAIN].PQBin_PhyAddr = (MS_PHYADDR) pu8MainCustomerBuf;
            }
            else
            {
                pstPQInitInfo->u8PQBinCustomerCnt = 0;
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!! Error Load PQ bin file, PQ_BIN_CUSTOMER_MAIN path [%s]\n", pm_PQBinFilePathName);
            }

        //[Main_Text.bin]=============================================================
            MS_U8 *pu8MainTextBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)mainTextBinName, &u32FileSize);
            if (pu8MainTextBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_TEXT_MAIN path [%s]\n", pm_PQBinFilePathName);
                pstPQInitInfo->u8PQTextBinCnt = 1;
                pstPQInitInfo->stPQTextBinInfo[PQ_BIN_STD_MAIN].u8PQID = (MS_U8) PQ_BIN_STD_MAIN;
                pstPQInitInfo->stPQTextBinInfo[PQ_BIN_STD_MAIN].PQBin_PhyAddr = (MS_PHYADDR) pu8MainTextBuf;
            }
            else
            {
                pstPQInitInfo->u8PQTextBinCnt = 0;
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!! Error Load PQ bin file, PQ_BIN_TEXT_MAIN path [%s]\n", pm_PQBinFilePathName);
            }

        //[UFSC.bin]=============================================================
            MS_U8 *pu8UFSCBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)UFSCBinName, &u32FileSize);
            if (pu8UFSCBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_UFSC path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_UFSC);
                pstPQInitInfo->u8PQBinUFSCCnt = 1;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_UFSC].u8PQID = (MS_U8) PQ_BIN_UFSC;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_UFSC].PQBin_PhyAddr = (MS_PHYADDR) pu8UFSCBuf;
            }
            else
            {
                pstPQInitInfo->u8PQBinUFSCCnt = 0;
                XC_INITMISC *psXC_Init_Misc;
                PQ_MALLOC_MEM(psXC_Init_Misc,sizeof(XC_INITMISC));
                if (psXC_Init_Misc==NULL)
                {
                    return;
                }
                MApi_XC_GetMISCStatus(psXC_Init_Misc);
                psXC_Init_Misc->u32MISC_B |= E_XC_INIT_MISC_B_SKIP_SR;
                MApi_XC_Init_MISC(psXC_Init_Misc, sizeof(XC_INITMISC));
                PQ_FREE_MEM(psXC_Init_Misc,sizeof(XC_INITMISC));
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!! Error Load PQ bin file, PQ_BIN_UFSC_MAIN path [%s]\n", pm_PQBinFilePathName);
            }

        //[TMO.bin]=============================================================
            MS_U8 *pu8TMOBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)TMOBinName, &u32FileSize);
            if (pu8TMOBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_TMO path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_TMO);
                pstPQInitInfo->u8PQBinTMOCnt = 1;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_TMO].u8PQID = (MS_U8) PQ_BIN_TMO;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_TMO].PQBin_PhyAddr = (MS_PHYADDR) pu8TMOBuf;
            }
            else
            {
                pstPQInitInfo->u8PQBinTMOCnt = 0;
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!! Error Load PQ bin file, PQ_BIN_TMO path [%s]\n", pm_PQBinFilePathName);
            }

#if (ENABLE_PQ_BIN_CF)
        //[CF.bin]=============================================================
            MS_U8 *pu8MainCFBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)MainCFBinName, &u32FileSize);
            if(pu8MainCFBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_CF_MAIN path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_CF_MAIN);
                pstPQInitInfo->u8PQBinCFCnt = 1;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_CF_MAIN].u8PQID = (MS_U8) PQ_BIN_CF_MAIN;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_CF_MAIN].PQBin_PhyAddr = (MS_PHYADDR) pu8MainCFBuf;
            }
            else
            {
                pstPQInitInfo->u8PQBinCFCnt = 0;
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!! Error Load PQ bin file, PQ_BIN_CF_MAIN path [%s]\n", pm_PQBinFilePathName);
            }
#endif

            if(pstPQCaps->b3DVideo_Supported || pstPQCaps->bPIP_Supported)
            {
            //[Sub.bin]=============================================================
                MS_U8 *pu8SubBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)SubBinName, &u32FileSize);
                if (pu8SubBuf)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_STD_SUB path [%s]\n", pm_PQBinFilePathName);
                    MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_STD_SUB);
                    pstPQInitInfo->u8PQBinCnt = 3;
                    pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_SUB].u8PQID = (MS_U8) PQ_BIN_STD_SUB;
                    pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_SUB].PQBin_PhyAddr = (MS_PHYADDR) pu8SubBuf;
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!! Error Load PQ bin file, PQ_BIN_STD_SUB path [%s]\n", pm_PQBinFilePathName);
                }

            //[Sub_EX.bin]=============================================================
                MS_U8 *pu8SubExBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)SubExBinName, &u32FileSize);
                if (pu8SubExBuf)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_EXT_SUB path [%s]\n", pm_PQBinFilePathName);
                    MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_EXT_SUB);
                    pstPQInitInfo->u8PQBinCnt = 4;
                    pstPQInitInfo->stPQBinInfo[PQ_BIN_EXT_SUB].u8PQID = (MS_U8) PQ_BIN_EXT_SUB;
                    pstPQInitInfo->stPQBinInfo[PQ_BIN_EXT_SUB].PQBin_PhyAddr = (MS_PHYADDR) pu8SubExBuf;
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!! Error Load PQ bin file, PQ_BIN_EXT_SUB path [%s]\n", pm_PQBinFilePathName);
                }

            //[Sub_Color.bin]=============================================================
                MS_U8 *pu8SubCustomerBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)SubColorBinName, &u32FileSize);
                if(pu8MainCustomerBuf && pu8SubCustomerBuf)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_CUSTOMER_SUB path [%s]\n", pm_PQBinFilePathName);
                    MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_CUSTOMER_SUB);
                    pstPQInitInfo->u8PQBinCustomerCnt = 2;
                    pstPQInitInfo->stPQBinInfo[PQ_BIN_CUSTOMER_SUB].u8PQID = (MS_U8) PQ_BIN_CUSTOMER_SUB;
                    pstPQInitInfo->stPQBinInfo[PQ_BIN_CUSTOMER_SUB].PQBin_PhyAddr = (MS_PHYADDR) pu8SubCustomerBuf;
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!! Error Load PQ bin file, PQ_BIN_CUSTOMER_SUB path [%s]\n", pm_PQBinFilePathName);
                }
            }

#if (ENABLE_PQ_BIN_CF)
        //[CF_Sub.bin]==========================================================
            MS_U8 *pu8SubCFBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)SubCFBinName, &u32FileSize);
            if(pu8SubCFBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_CF_SUB path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_CF_SUB);
                pstPQInitInfo->u8PQBinCFCnt = 2;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_CF_SUB].u8PQID = (MS_U8) PQ_BIN_CF_SUB;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_CF_SUB].PQBin_PhyAddr = (MS_PHYADDR) pu8SubCFBuf;
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!! Error Load PQ bin file, PQ_BIN_CF_SUB path [%s]\n", pm_PQBinFilePathName);
            }
#endif

#if (ENABLE_PQ_BIN_HSY)
        //[HSY.bin]=============================================================
            MS_U8 *pu8HSYBuf = MDrv_PQ_LoadPQBinMethod(pInstance, (char*)HSYcolorBinName, &u32FileSize);
            if (pu8HSYBuf)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Load PQ bin file, PQ_BIN_HYS path [%s]\n", pm_PQBinFilePathName);
                MDrv_PQ_SetBinInfo(u32FileSize,pm_PQBinFilePathName,PQ_BIN_HSY);
                u8PQHSYBinCnt = 1;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_HSY].u8PQID = (MS_U8) PQ_BIN_HSY;
                pstPQInitInfo->stPQBinInfo[PQ_BIN_HSY].PQBin_PhyAddr = (MS_PHYADDR) pu8HSYBuf;
            }
            else
            {
                u8PQHSYBinCnt = 0;
                PQ_LEVEL_PRINTF(PQ_DBG_INFO, "!!Fail Load PQ bin file, PQ_BIN_HYS path [%s]\n", pm_PQBinFilePathName);
            }
#endif
        }
        else
        {
            pstPQInitInfo->u8PQBinCnt = 0;
            pstPQInitInfo->u8PQTextBinCnt = 0;
            pstPQInitInfo->u8PQBinCustomerCnt = 0;
            pstPQInitInfo->u8PQBinUFSCCnt = 0;
            pstPQInitInfo->u8PQBinTMOCnt = 0;
#if (ENABLE_PQ_BIN_CF)
            pstPQInitInfo->u8PQBinCFCnt = 0;
#endif
        }
        PQ_FREE_MEM(pstPQCaps,sizeof(MS_PQ_CAP_INFO));
    }
    MDrv_PQ_Release_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
}
#endif

MS_BOOL MDrv_PQ_SetAIPQGRule(ST_PQ_SET_AIPQ_GRULE *pstSetAIPQGRule)
{
    MS_BOOL bRet = FALSE;
#if(PQ_GRULE_AI_PQ_ENABLE == 1)
    ST_PQ_SET_AIPQ_GRULE stSetAIPQGRule;
    MS_U32 u32ValidLength = 0;

    memset(&stSetAIPQGRule, 0, sizeof(stSetAIPQGRule));
    // version handling
     if ((pstSetAIPQGRule == NULL) || (pstSetAIPQGRule->u32Length == 0))
    {
        //We cosider compatible operation form version2 , so reject the info init when version invalid
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetAIPQGRule: Null paramter or Wrong u32Length!!\n");

        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Exit\n");
        return bRet;
    }
    // the version control is coming in with version 1
    if (pstSetAIPQGRule->u32Version < 1)
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetAIPQGRule: please check your u32Version, it should not set to 0!!\n");

        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Exit\n");
        return bRet;
    }

    //new AP + old lib, only the length corresponding to old lib has meaning.
    if(pstSetAIPQGRule->u32Version > ST_PQ_SET_AIPQ_GRULE_VERSION)
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
#if defined (__aarch64__)
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetAIPQGRule: this old version XC lib has only length:%lu driver status!!\n",sizeof(*pstSetAIPQGRule));
#else
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetAIPQGRule: this old version XC lib has only length:%u driver status!!\n", sizeof(*pstSetAIPQGRule));
#endif
        //we still copy the min size of both structure, but AP should take care of it.
        u32ValidLength = sizeof(*pstSetAIPQGRule);
    }

    //old AP + new lib, driver shouldn't access to the space which doesn't exist in old structure
    if((pstSetAIPQGRule->u32Version < ST_PQ_SET_AIPQ_GRULE_VERSION) || (pstSetAIPQGRule->u32Length < sizeof(*pstSetAIPQGRule)))
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetAIPQGRule: new version XC lib shouldn't access to the space which doesn't exist in old structure!!\n");
        u32ValidLength = pstSetAIPQGRule->u32Length;
    }
    else
    {
        u32ValidLength = sizeof(*pstSetAIPQGRule);
    }
    memcpy(&stSetAIPQGRule, pstSetAIPQGRule, u32ValidLength);
    stSetAIPQGRule.u32Length = u32ValidLength;


    if(stSetAIPQGRule.enPQAIPQType == E_PQ_AIPQ_OFF)
    {
        return MDrv_PQ_SetGRuleStatus(stSetAIPQGRule.enWindow, PQ_GRule_AI_PQ_Main, PQ_GRule_AI_PQ_Off_Main);
    }
    else if(stSetAIPQGRule.enPQAIPQType== E_PQ_AIPQ_ON)
    {
        return MDrv_PQ_SetGRuleStatus(stSetAIPQGRule.enWindow, PQ_GRule_AI_PQ_Main, PQ_GRule_AI_PQ_On_Main);
    }
    else
    {
        return FALSE;
    }
#else
    UNUSED(pstSetAIPQGRule);
    return bRet;
#endif
}

#if (ENABLE_PQ_BIN_TMO == 1)
static MS_BOOL _MDrv_PQ_SetGRuleStatus_Ex(void* pInstance, PQ_WIN eWindow, MS_U16 u16GruleType, MS_U16 u16PQ_GroupIdx, PQ_ENGINE_TYPE ePQEngineType)
{
    PQ_BIN_TYPE enPQBinType = MAX_PQ_BIN_NUM;

    if (eWindow == PQ_MAIN_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                if (gbPQBinEnable)
                {
                    enPQBinType = PQ_BIN_STD_MAIN;
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    enPQBinType = PQ_BIN_EXT_MAIN;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerMainPQEn)
                {
                    enPQBinType = PQ_BIN_CUSTOMER_MAIN;
                }
                break;
            case PQ_XC0_UFSC:
                if (_bUFSCPQEn)
                {
                    enPQBinType = PQ_BIN_UFSC;
                }
                break;
            case PQ_XC0_TMO:
                if (_bTMOPQEn)
                {
                    enPQBinType = PQ_BIN_TMO;
                }
                break;
            default:
                break;
        }
    }
#if (PQ_ENABLE_PIP)
    else if (eWindow == PQ_SUB_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                if (gbPQBinEnable)
                {
                    enPQBinType = PQ_BIN_STD_SUB;
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    enPQBinType = PQ_BIN_EXT_SUB;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerSubPQEn)
                {
                    enPQBinType = PQ_BIN_CUSTOMER_SUB;
                }
                break;
            case PQ_XC0_UFSC:
                break;
            default:
                break;
        }

    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }

    if (enPQBinType == MAX_PQ_BIN_NUM)
    {
        return FALSE;
    }

    if(u16GruleType >= stPQBinHeaderInfo[enPQBinType].u16GRule_RuleNum)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"enPQBinType = %d, u16GruleType = %d out of %d\n", enPQBinType, u16GruleType, stPQBinHeaderInfo[enPQBinType].u16GRule_RuleNum);
        return FALSE;
    }

    MS_U16 i = 0, u16IPIdx = PQ_BIN_IP_NULL, u16TabIdx = PQ_BIN_IP_NULL, u16GroupNum = PQ_BIN_IP_NULL, u16IPNum = PQ_BIN_IP_NULL;
    u16GroupNum = MDrv_PQBin_GetGRule_GroupNum(pInstance, u16GruleType, &stPQBinHeaderInfo[enPQBinType]);
    u16IPNum = MDrv_PQBin_GetGRule_IPNum(pInstance, u16GruleType, &stPQBinHeaderInfo[enPQBinType]);
    if ((PQ_BIN_IP_NULL == u16GroupNum) || (PQ_BIN_IP_NULL == u16IPNum))
    {
        return FALSE;
    }

    if(u16PQ_GroupIdx >= u16GroupNum)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"enPQBinType = %d, u16PQ_GroupIdx = %d out of %d\n", enPQBinType, u16PQ_GroupIdx, u16GroupNum);
        return FALSE;
    }

    for(i = 0; i < u16IPNum; i++)
    {

        u16IPIdx = MDrv_PQ_GetGRule_IPIndex_Ex(eWindow, u16GruleType, i, ePQEngineType);
        u16TabIdx = MDrv_PQ_GetGRule_TableIndex_Ex(eWindow, u16GruleType, u16PQ_GroupIdx, i, ePQEngineType);
        MDrv_PQ_LoadTable_Ex(eWindow, u16TabIdx, u16IPIdx, ePQEngineType);
    }

    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_GRULE_DBG;
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.pValue= &u16PQ_GroupIdx;
    stPQSetVRInfo.u16RuleIndex= u16GruleType;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }
    return TRUE;
}
#endif

MS_U16 MDrv_PQ_GetTMOGRule_LevelIndex(void* pInstance, MS_U16 u16GRuleType, MS_U16 u16TMOGRuleLevelIndex)
{
    MS_U16 u16LevelIndex = 0;
#if (ENABLE_PQ_BIN_TMO == 1)
    MS_U16 u16PanelIdx = 0;

    if(_bTMOPQEn)
    {
        if(u16GRuleType >= stPQBinHeaderInfo[PQ_BIN_TMO].u16GRule_RuleNum)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"GRuleType(%u) is bigger than TMO PQ's(%u), so consider it as TMO PQ's GRule Type\n",u16GRuleType, stPQBinHeaderInfo[PQ_BIN_TMO].u16GRule_RuleNum);
            u16GRuleType = u16GRuleType - stPQBinHeaderInfo[PQ_BIN_TMO].u16GRule_RuleNum;
            u16PanelIdx = MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO);
            if(u16GRuleType >= stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN].u16GRule_RuleNum)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"CustomerPQ: =ERROR!!!=%d, out of %d, =\n", u16GRuleType, stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN].u16GRule_RuleNum);
                return 0;
            }
        }
        else
        {
            u16PanelIdx = MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO);
        }

        u16LevelIndex = (MS_U8) MDrv_PQBin_GetGRule_LevelIndex(pInstance, u16PanelIdx,
                                   u16GRuleType,
                                   u16TMOGRuleLevelIndex,
                                   &stPQBinHeaderInfo[PQ_BIN_TMO]);
    }

    return u16LevelIndex;
#else
    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;33m[TMO] Do Not Have TMO bin");
    return u16LevelIndex;
#endif
}

MS_U16 MDrv_PQ_GetTMOGRule_IPIndex(void* pInstance, PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16GRuleIPIndex)
{
    MS_U16 u16IPIndex = 0;
#if (ENABLE_PQ_BIN_TMO == 1)
    if(eWindow == PQ_SUB_WINDOW)
    {
        return 0;
    }

    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(_bTMOPQEn)
        {
            u16IPIndex = MDrv_PQBin_GetGRule_IPIndex(pInstance, u16GRuleType,
                                                     u16GRuleIPIndex,
                                                     &stPQBinHeaderInfo[PQ_BIN_TMO]);
        }
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }

    return u16IPIndex;
#else
    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;33m[TMO] Do Not Have TMO bin");
    return u16IPIndex;
#endif
}

MS_U16 MDrv_PQ_GetTMOTableIndex(void* pInstance, PQ_WIN eWindow, MS_U16 u16IPIdx)
{
    MS_U16 u16TabIdx = 0;
#if (ENABLE_PQ_BIN_TMO == 1)
    if(eWindow == PQ_SUB_WINDOW)
    {
        return 0;
    }

    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(_bTMOPQEn)
        {
            u16TabIdx = MDrv_PQBin_GetTableIndex(pInstance, _u16PQSrcType_TMO[eWindow],
                                                 u16IPIdx,
                                                 MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO),
                                                 &stPQBinHeaderInfo[PQ_BIN_TMO]);
        }
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
    return u16TabIdx;
#else
    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;33m[TMO] Do Not Have TMO bin");
    return u16TabIdx;
#endif
}

MS_U16 MDrv_PQ_GetTMOGRule_TableIndex(void* pInstance, PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16PQ_NRIdx, MS_U16 u16GRuleIPIndex)
{
    MS_U16 u16TableIndex = 0;

#if (ENABLE_PQ_BIN_TMO == 1)
    if(eWindow == PQ_SUB_WINDOW)
    {
        return 0;
    }

    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(_bTMOPQEn)
        {
            u16TableIndex = MDrv_PQBin_GetGRule_TableIndex(pInstance, u16GRuleType,
                            _u16PQSrcType_TMO[eWindow],
                            u16PQ_NRIdx,
                            u16GRuleIPIndex,
                            &stPQBinHeaderInfo[PQ_BIN_TMO]);
        }
        else
        {
            u16TableIndex = MDrv_PQ_GetGRule_TableIndex_(MAIN,pInstance, (MS_U8)u16GRuleType, _u16PQSrcType_TMO[eWindow], (MS_U8)u16PQ_NRIdx, (MS_U8)u16GRuleIPIndex);
        }
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_TMO_GRULE_DBG;
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.pValue= &u16PQ_NRIdx;
    stPQSetVRInfo.u16RuleIndex= u16GRuleType;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }
    return u16TableIndex;
#else
    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;33m[TMO] Do Not Have TMO bin");
    return u16TableIndex;
#endif
}

#if (PQ_SUPPORT_SETGRULEDATA == 1)

void _MDrv_PQ_SetQmapGRuleLevelIndex(void *pInstance, PQ_WIN enWindow, PQ_BIN_TYPE enPQBinType, EN_PQ_QMAP_TYPE enQmapType, MS_U16 u16GRuleLevel)
{
    _gu16QmapGRuleLevel[enPQBinType][enQmapType] = u16GRuleLevel;
}

MS_U16 _MDrv_PQ_GetQmapGRuleLevelIndex(void *pInstance, PQ_WIN enWindow, PQ_BIN_TYPE enPQBinType, EN_PQ_QMAP_TYPE enQmapType)
{
    MS_U16 u16GRuleLevel = 0;
    u16GRuleLevel = _gu16QmapGRuleLevel[enPQBinType][enQmapType];
    return u16GRuleLevel;
}

static EN_KDRV_XC_CFD_QMAPDATA_TABLE_TYPE _MDrv_PQ_ConvertPqTabtype(MS_U16 u16PqTabType)
{
    EN_KDRV_XC_CFD_QMAPDATA_TABLE_TYPE enRet = E_KDRV_XC_CFD_QMAPDATA_TABTYPE_INVALID;

    if(u16PqTabType == (MS_U16)PQ_TABTYPE_SCALER)
    {
        enRet = E_KDRV_XC_CFD_QMAPDATA_TABTYPE_SCALER;
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "PQ table type: scaler\n");
    }
    else if(u16PqTabType == (MS_U16)PQ_TABTYPE_TWOBYTE_BANK)
    {
        enRet = E_KDRV_XC_CFD_QMAPDATA_TABTYPE_TWOBYTE_BANK;
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "PQ table type: two-byte bank\n");
    }
    else
    {
        // not support
        enRet = E_KDRV_XC_CFD_QMAPDATA_TABTYPE_INVALID;
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR, "PQ table type: %d to be defined!\n", u16PqTabType);
    }

    return enRet;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
/// This function will get Qmap table information
/// ST_XC_PQ_QMAPDATA.u32Quality_InputType_Num: the number of options in Grule type. such as XVYCC has three option(Normal/On_XvYCC/ON_sRGB)
/// ST_XC_PQ_QMAPDATA.u32Quality_IP_Num: the mumber of IPs Controlled in Grule type. such as XVYCC controls five IPs(LinearRGB/RGB_Chip/VIP_IHC/VIP_ICC/VIP_IBC)
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
static MS_BOOL _MDrv_PQ_GetTableInfo(void *pInstance,PQ_WIN eWindow, EN_PQ_QMAP_TYPE eQmapType, MST_GRule_Index_Main *penGruleIndex, ST_XC_PQ_QMAPDATA *pstqmapdata)
{
    MS_BOOL bRet = FALSE;
    MS_U16 u16GroupNum = 0;
    MS_U16 u16IPNum = 0;
    if(MHal_PQ_Support_GroupIPBIN())
    {
        MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,PQ_GRule_HDR_Main,PQ_BIN_STD_MAIN ,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
    }
    else
    {
        u16GroupNum = PQ_GRULE_HDR_NUM_Main;
        u16IPNum = PQ_GRULE_HDR_IP_NUM_Main;
    }
    if(pstqmapdata == NULL)
    {
        return bRet;
    }
    if(penGruleIndex == NULL)
    {
        return bRet;
    }

    if((eQmapType == E_PQ_HDR_GRULE) && (eWindow == PQ_MAIN_WINDOW))
    {
        *penGruleIndex = PQ_GRule_HDR_Main;
        pstqmapdata->u32Quality_InputType_Num = PQ_GRULE_HDR_LVL_NUM_Main;
        pstqmapdata->u32Quality_IP_Num = u16IPNum;
        bRet = TRUE;
    }
    return bRet;
}

#if defined(ENABLE_PQ_TMO_GRULE)&&(ENABLE_PQ_TMO_GRULE==1)
static MS_BOOL _MDrv_PQ_GetTMOGRuleTableInfo(void *pInstance, PQ_WIN enWindow,EN_PQ_QMAP_TYPE enQmapType, MST_GRule_TMO_Index_Main *penGruleIndex, ST_XC_PQ_QMAPDATA *pstQmapData)
{
    MS_BOOL bRet = FALSE;

    if(pstQmapData == NULL)
    {
        return bRet;
    }
    if(penGruleIndex == NULL)
    {
        return bRet;
    }

    MS_U16 u16GroupNum = 0;
    MS_U16 u16IPNum = 0;

    if((enQmapType == E_PQ_TMO_DLC_GRULE) && (enWindow == PQ_MAIN_WINDOW))
    {
        if(MHal_PQ_Support_GroupIPBIN())
        {
            MDrv_PQBin_GetGRule_GroupIPNum(pInstance,enWindow,PQ_GRule_DLC_Setting_MODE_Main,PQ_BIN_TMO,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_TMO]);
        }
        else
        {
            u16GroupNum = PQ_GRULE_DLC_Setting_MODE_NUM_Main;
            u16IPNum = PQ_GRULE_DLC_Setting_MODE_IP_NUM_Main;
        }
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[PQ] GroupNum: %u, IPNum: %u\n", u16GroupNum, u16IPNum);

        *penGruleIndex = PQ_GRule_DLC_Setting_MODE_Main;
        pstQmapData->u32Quality_InputType_Num = QM_TMO_INPUTTYPE_NUM_Main;
        pstQmapData->u32Quality_IP_Num = u16IPNum;
        bRet = TRUE;
    }
    else if((enQmapType == E_PQ_TMO_GRULE) && (enWindow == PQ_MAIN_WINDOW))
    {
        if(MHal_PQ_Support_GroupIPBIN())
        {
            MDrv_PQBin_GetGRule_GroupIPNum(pInstance,enWindow,PQ_GRule_TMO_MODE_Main,PQ_BIN_TMO,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_TMO]);
        }
        else
        {
            u16GroupNum = PQ_GRULE_TMO_MODE_NUM_Main;
            u16IPNum = PQ_GRULE_TMO_MODE_IP_NUM_Main;
        }
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[PQ] GroupNum: %u, IPNum: %u\n", u16GroupNum, u16IPNum);

        *penGruleIndex = PQ_GRule_TMO_MODE_Main;
        pstQmapData->u32Quality_InputType_Num = QM_TMO_INPUTTYPE_NUM_Main;
        pstQmapData->u32Quality_IP_Num = u16IPNum;
        bRet = TRUE;
    }

    return bRet;
}

static MS_BOOL _MDrv_PQ_SetQMAPTMOGruleData(void *pInstance, PQ_WIN enWindow, EN_PQ_QMAP_TYPE enQmapType)
{
    ST_XC_PQ_QMAPDATA stQmapData;
    MS_PQBin_IP_Table_Info stTableInfo;
    MS_U16 u16IPIdx = 0;
    MS_PHYADDR u32Addr = 0;
    MS_U32 u32TabSize  = 0;
    MS_U32 u32TabCnt   = 0;
    MS_U32 u32TabAlign = 0;

    MS_U32 u32IPNum = 0, u32InputNum = 0, u32TableNum = 0;
    MS_U8 u8Row = 0, u8Column = 0;

    MS_U16 u16PQGruleIdx = _MDrv_PQ_GetQmapGRuleLevelIndex(pInstance, enWindow, PQ_BIN_TMO, enQmapType);

    MST_GRule_TMO_Index_Main enGruleIndex;

    memset(&stQmapData,  0, sizeof(ST_XC_PQ_QMAPDATA));
    memset(&stTableInfo, 0, sizeof(MS_PQBin_IP_Table_Info));

    stQmapData.u32Table_IP_Num = XC_PQ_IP_INFORMATION_SIZE;
    if(_MDrv_PQ_GetTMOGRuleTableInfo(pInstance, enWindow, enQmapType, &enGruleIndex, &stQmapData) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"[PQ]Fail to set TMO Grule type[%d] to CFD\n", enQmapType);
        return FALSE;
    }

    u32IPNum    = stQmapData.u32Quality_IP_Num;
    u32InputNum = stQmapData.u32Quality_InputType_Num;
    u32TableNum = stQmapData.u32Table_IP_Num;

    PQ_MALLOC_MEM(stQmapData.pu16Quality_Map_Array, (u32InputNum * u32IPNum * sizeof(MS_U16)));
    PQ_MALLOC_MEM(stQmapData.pu16Table_Map_Array, (u32IPNum * u32TableNum * sizeof(MS_U16)));
    if((stQmapData.pu16Quality_Map_Array == NULL) || (stQmapData.pu16Table_Map_Array == NULL))
    {
        PQ_FREE_MEM(stQmapData.pu16Quality_Map_Array, (u32InputNum * u32IPNum * sizeof(MS_U16)));
        PQ_FREE_MEM(stQmapData.pu16Table_Map_Array, (u32IPNum * u32TableNum * sizeof(MS_U16)));
        return FALSE;
    }

    for (u8Column = 0; u8Column < u32InputNum; u8Column++)
    {
        for (u8Row = 0; u8Row < u32IPNum; u8Row++)
        {
            stQmapData.pu16Quality_Map_Array[(u8Column*u32IPNum+u8Row)] = MDrv_PQBin_GetGRule_TableIndex(pInstance,
                                                                          enGruleIndex,
                                                                          u8Column,
                                                                          u16PQGruleIdx,
                                                                          u8Row,
                                                                          &stPQBinHeaderInfo[PQ_BIN_TMO]);
        }
    }

    for (u8Column = 0; u8Column < u32IPNum; u8Column++)
    {
        u16IPIdx = MDrv_PQ_GetTMOGRule_IPIndex(pInstance, enWindow, enGruleIndex, u8Column);
        MDrv_PQBin_GetTable(pInstance,
                            MDrv_PQBin_GetPanelIdx(pInstance),
                            0,
                            u16IPIdx,
                            &stTableInfo,
                            &stPQBinHeaderInfo[PQ_BIN_TMO]);
        stQmapData.pu16Table_Map_Array[(u8Column * u32TableNum)] = stTableInfo.u16GroupNum;
        stQmapData.pu16Table_Map_Array[(u8Column * u32TableNum + 1)] = stTableInfo.u16RegNum;
        if(u32TableNum > (MS_U32)E_KDRV_XC_CFD_QMAPDATA_INFO_TABLE_TYPE_ALIENMENT)
        {
            MS_U16 u16PqTabType = (MS_U16)_MDrv_PQ_ConvertPqTabtype(stTableInfo.u16TableType);

            stQmapData.pu16Table_Map_Array[(u8Column * u32TableNum + E_KDRV_XC_CFD_QMAPDATA_INFO_TABLE_TYPE_ALIENMENT)] = u16PqTabType;
        }
        stQmapData.u32Table_Data_Size = (stQmapData.u32Table_Data_Size)+
                                        (PQ_BIN_BANK_SIZE + PQ_BIN_ADDR_SIZE + PQ_BIN_MASK_SIZE + (MS_U32)stTableInfo.u16GroupNum)*
                                        (stTableInfo.u16RegNum);
    }

    PQ_MALLOC_MEM(stQmapData.pu8Table_Data_Array, (stQmapData.u32Table_Data_Size * sizeof(MS_U8)));
    if(stQmapData.pu8Table_Data_Array == NULL)
    {
        PQ_FREE_MEM(stQmapData.pu16Quality_Map_Array, (u32InputNum * u32IPNum * sizeof(MS_U16)));
        PQ_FREE_MEM(stQmapData.pu16Table_Map_Array, (u32IPNum * u32TableNum * sizeof(MS_U16)));
        return FALSE;
    }

    for (u8Column = 0; u8Column < u32IPNum; u8Column++)
    {
        u16IPIdx = MDrv_PQ_GetTMOGRule_IPIndex(pInstance, enWindow, enGruleIndex, u8Column);
        MDrv_PQBin_GetTable(pInstance,
                            MDrv_PQBin_GetPanelIdx(pInstance),
                            0,
                            u16IPIdx,
                            &stTableInfo,
                            &stPQBinHeaderInfo[PQ_BIN_TMO]);
        u32Addr = stTableInfo.u32TabOffset;
        u32TabSize = ( PQ_BIN_BANK_SIZE + PQ_BIN_ADDR_SIZE + PQ_BIN_MASK_SIZE + (MS_U32)stTableInfo.u16GroupNum) * stTableInfo.u16RegNum;
        for (u32TabCnt = 0; u32TabCnt < u32TabSize; u32TabCnt++)
        {
            stQmapData.pu8Table_Data_Array[(u32TabCnt + u32TabAlign)] = *((MS_U8 *)(u32Addr + u32TabCnt));
        }
        u32TabAlign = u32TabAlign + u32TabSize;
    }

    stQmapData.u32Version = PQ_QMAPDATA_VERSION;
    stQmapData.u16Length = sizeof(ST_XC_PQ_QMAPDATA);
#if (PQ_QMAPDATA_VERSION >= 2)
    stQmapData.eQmapType = enQmapType;
#endif

#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION >= 2)
#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_QMAPDATA;
    stCfdCtrlInfo.pParam = &stQmapData;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_PQ_QMAPDATA);

    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Scaler device not opened!!!!\n");
        }
    }

    if (ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stCfdCtrlInfo))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDRV_XC_IOC_CFDCONCTRL UPDATE GRULE DATA fail!!!!\n");
    }

    if (stCfdCtrlInfo.u16ErrCode != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDRV_XC_IOC_CFDCONCTRL UPDATE GRULE DATA fail, errCode: %d\n", stCfdCtrlInfo.u16ErrCode);
    }
#endif
#if defined(MSOS_TYPE_LINUX_KERNEL)
    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_QMAPDATA;
    stCfdCtrlInfo.pParam = &stQmapData;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_PQ_QMAPDATA);
    MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrlInfo);
#endif
#endif
#endif

    PQ_FREE_MEM(stQmapData.pu8Table_Data_Array, (stQmapData.u32Table_Data_Size * sizeof(MS_U8)));
    PQ_FREE_MEM(stQmapData.pu16Quality_Map_Array, (u32InputNum * u32IPNum * sizeof(MS_U16)));
    PQ_FREE_MEM(stQmapData.pu16Table_Map_Array, (u32IPNum * u32TableNum * sizeof(MS_U16)));

    return TRUE;
}
#endif

#if (ENABLE_PQ_CUSTOMER_BIN_MAIN_GRULE == 1)
static MS_BOOL _MDrv_PQ_GetColorTableInfo(void *pInstance,PQ_WIN eWindow,EN_PQ_QMAP_TYPE eQmapType, MST_GRule_Index_Main_Color *peGruleIndex,ST_XC_PQ_QMAPDATA *pstqmapdata)
{
    MS_BOOL bRet = FALSE;

    if(pstqmapdata == NULL)
    {
        return bRet;
    }
    if(peGruleIndex == NULL)
    {
        return bRet;
    }

    if((eQmapType == E_PQ_COLOR_HDR_GRULE) && (eWindow == PQ_MAIN_WINDOW))
    {
        *peGruleIndex = PQ_GRule_HDR_Main_Color;
        pstqmapdata->u32Quality_InputType_Num = PQ_GRULE_HDR_LVL_NUM_Main_Color;
        pstqmapdata->u32Quality_IP_Num = PQ_GRULE_HDR_IP_NUM_Main_Color;
        bRet = TRUE;
    }
    return bRet;
}

static MS_BOOL _MDrv_PQ_SetColorQMAPGruleData(void* pInstance, PQ_WIN eWindow, EN_PQ_QMAP_TYPE eQmapType)
{
    ST_XC_PQ_QMAPDATA stqmapdata;
    MS_U8 u8row =0, u8column = 0;
    MS_PQBin_IP_Table_Info stTableInfo;
    MS_U16 u16PQ_HDRMode_Idx;
    MS_U16 u16IPIdx=0;
    MS_PHYADDR u32Addr;
    MS_U32 u32tabsize = 0;
    MS_U32 u32tabcnt = 0;
    MS_U32 u32tabalign = 0;
    MST_GRule_Index_Main_Color eGruleIndex;
    memset(&stqmapdata, 0, sizeof(ST_XC_PQ_QMAPDATA));

    stqmapdata.u32Table_IP_Num = XC_PQ_IP_INFORMATION_SIZE;
    if(_MDrv_PQ_GetColorTableInfo(pInstance, eWindow, eQmapType, &eGruleIndex, &stqmapdata) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"[PQ]Fail to set Color Grule type[%d]  to CFD\n", eQmapType);
        return FALSE;
    }

    PQ_MALLOC_MEM(stqmapdata.pu16Quality_Map_Array, (stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
    PQ_MALLOC_MEM(stqmapdata.pu16Table_Map_Array, (stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));

    if((stqmapdata.pu16Quality_Map_Array == NULL) || (stqmapdata.pu16Table_Map_Array == NULL))
    {
        PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
        PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));

        return FALSE;
    }

    for (u8column = 0; u8column < stqmapdata.u32Quality_InputType_Num; u8column++)
    {
        u16PQ_HDRMode_Idx = MDrv_PQ_GetGRule_LevelIndex_Ex(eWindow, eGruleIndex, u8column, PQ_XC0_CUSTOMER);

        for (u8row = 0; u8row < stqmapdata.u32Quality_IP_Num; u8row++)
        {
            stqmapdata.pu16Quality_Map_Array[(u8column * stqmapdata.u32Quality_IP_Num +u8row)] = MDrv_PQ_GetGRule_CustomerTableIndex(eWindow,
                                                                                                eGruleIndex,
                                                                                                u16PQ_HDRMode_Idx,
                                                                                                u8row);
        }
    }

    for (u8column = 0; u8column < stqmapdata.u32Quality_IP_Num; u8column++)
    {
        u16IPIdx = MDrv_PQ_GetCustomerGRule_IPIndex(eWindow, eGruleIndex, u8column);
        MDrv_PQBin_GetTable(pInstance,
                            PQ_BIN_CUSTOMER_PANEL_INDEX,
                            0,
                            u16IPIdx,
                            &stTableInfo,
                            &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN]);
        stqmapdata.pu16Table_Map_Array[(u8column * stqmapdata.u32Table_IP_Num)] = stTableInfo.u16GroupNum;
        stqmapdata.pu16Table_Map_Array[(u8column * stqmapdata.u32Table_IP_Num + 1)] = stTableInfo.u16RegNum;
        if(stqmapdata.u32Table_IP_Num > (MS_U32)E_KDRV_XC_CFD_QMAPDATA_INFO_TABLE_TYPE_ALIENMENT)
        {
            MS_U16 u16PqTabType = (MS_U16)_MDrv_PQ_ConvertPqTabtype(stTableInfo.u16TableType);

            stqmapdata.pu16Table_Map_Array[(u8column * stqmapdata.u32Table_IP_Num + E_KDRV_XC_CFD_QMAPDATA_INFO_TABLE_TYPE_ALIENMENT)] = u16PqTabType;
        }
        stqmapdata.u32Table_Data_Size = stqmapdata.u32Table_Data_Size + ( PQ_BIN_BANK_SIZE + PQ_BIN_ADDR_SIZE + PQ_BIN_MASK_SIZE + (MS_U32)stTableInfo.u16GroupNum) * stTableInfo.u16RegNum;
    }

    PQ_MALLOC_MEM(stqmapdata.pu8Table_Data_Array, (stqmapdata.u32Table_Data_Size * sizeof(MS_U8)));

    if(stqmapdata.pu8Table_Data_Array == NULL)
    {
        PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
        PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));

        return FALSE;
    }

    for (u8column = 0; u8column < stqmapdata.u32Quality_IP_Num; u8column++)
    {
        u16IPIdx = MDrv_PQ_GetCustomerGRule_IPIndex(eWindow, eGruleIndex, u8column);
        MDrv_PQBin_GetTable(pInstance,
                            PQ_BIN_CUSTOMER_PANEL_INDEX,
                            0,
                            u16IPIdx,
                            &stTableInfo,
                            &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN]);
        u32Addr = stTableInfo.u32TabOffset;
        u32tabsize = ( PQ_BIN_BANK_SIZE + PQ_BIN_ADDR_SIZE + PQ_BIN_MASK_SIZE + (MS_U32)stTableInfo.u16GroupNum) * stTableInfo.u16RegNum;
        for (u32tabcnt = 0; u32tabcnt < u32tabsize; u32tabcnt++)
        {
            stqmapdata.pu8Table_Data_Array[(u32tabcnt + u32tabalign)] = *((MS_U8 *)(u32Addr + u32tabcnt));
        }
        u32tabalign = u32tabalign + u32tabsize;
    }

    stqmapdata.u32Version = PQ_QMAPDATA_VERSION;
    stqmapdata.u16Length = sizeof(ST_XC_PQ_QMAPDATA);
#if (PQ_QMAPDATA_VERSION >= 2)
    stqmapdata.eQmapType = eQmapType;
#endif

#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION >= 2)
#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)

    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_QMAPDATA;
    stCfdCtrlInfo.pParam = &stqmapdata;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_PQ_QMAPDATA);

    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Scaler device not opened!!!!\n");
        }
    }

    if (ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stCfdCtrlInfo))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDRV_XC_IOC_CFDCONCTRL UPDATE GRULE DATA fail!!!!\n");
    }

    if (stCfdCtrlInfo.u16ErrCode != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDRV_XC_IOC_CFDCONCTRL UPDATE GRULE DATA fail, errCode: %d\n", stCfdCtrlInfo.u16ErrCode);
    }
#endif
#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_QMAPDATA;
    stCfdCtrlInfo.pParam = &stqmapdata;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_PQ_QMAPDATA);
    MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrlInfo);
#endif
#endif
#endif

    PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
    PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));
    PQ_FREE_MEM(stqmapdata.pu8Table_Data_Array,(stqmapdata.u32Table_Data_Size * sizeof(MS_U8)));

    return TRUE;
}
#endif

static MS_BOOL _MDrv_PQ_SetQMAPGruleData(void* pInstance, PQ_WIN eWindow, EN_PQ_QMAP_TYPE eQmapType)
{
    ST_XC_PQ_QMAPDATA stqmapdata;
    MS_U8 u8row =0, u8column = 0;
    MS_PQBin_IP_Table_Info stTableInfo;
    MS_U16 u16PQ_HDRMode_Idx;
    MS_U16 u16IPIdx=0;
    MS_PHYADDR u32Addr;
    MS_U32 u32tabsize = 0;
    MS_U32 u32tabcnt = 0;
    MS_U32 u32tabalign = 0;
    MST_GRule_Index_Main eGruleIndex;
    memset(&stqmapdata, 0, sizeof(ST_XC_PQ_QMAPDATA));

    stqmapdata.u32Table_IP_Num = XC_PQ_IP_INFORMATION_SIZE;
    if(_MDrv_PQ_GetTableInfo(pInstance, eWindow, eQmapType, &eGruleIndex, &stqmapdata) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"[PQ]Fail to set Color Grule type[%d]  to CFD\n", eQmapType);
        return FALSE;
    }

    PQ_MALLOC_MEM(stqmapdata.pu16Quality_Map_Array, (stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
    PQ_MALLOC_MEM(stqmapdata.pu16Table_Map_Array, (stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));

    if((stqmapdata.pu16Quality_Map_Array == NULL) || (stqmapdata.pu16Table_Map_Array == NULL))
    {

        PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
        PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));

        return FALSE;
    }

    for (u8column = 0; u8column < stqmapdata.u32Quality_InputType_Num; u8column++)
    {
        u16PQ_HDRMode_Idx = MDrv_PQ_GetGRule_LevelIndex(eWindow, eGruleIndex, u8column);

        for (u8row = 0; u8row < stqmapdata.u32Quality_IP_Num; u8row++)
        {
            stqmapdata.pu16Quality_Map_Array[(u8column * stqmapdata.u32Quality_IP_Num +u8row)] = MDrv_PQ_GetGRule_TableIndex(eWindow,
                                                                                                eGruleIndex,
                                                                                                u16PQ_HDRMode_Idx,
                                                                                                u8row);
        }
    }

    for (u8column = 0; u8column < stqmapdata.u32Quality_IP_Num; u8column++)
    {
        u16IPIdx = MDrv_PQ_GetGRule_IPIndex(eWindow, eGruleIndex, u8column);
        MDrv_PQBin_GetTable(pInstance,
                            MDrv_PQBin_GetPanelIdx(pInstance),
                            0,
                            u16IPIdx,
                            &stTableInfo,
                            &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        stqmapdata.pu16Table_Map_Array[(u8column * stqmapdata.u32Table_IP_Num)] = stTableInfo.u16GroupNum;
        stqmapdata.pu16Table_Map_Array[(u8column * stqmapdata.u32Table_IP_Num + 1)] = stTableInfo.u16RegNum;
        if(stqmapdata.u32Table_IP_Num > (MS_U32)E_KDRV_XC_CFD_QMAPDATA_INFO_TABLE_TYPE_ALIENMENT)
        {
            MS_U16 u16PqTabType = (MS_U16)_MDrv_PQ_ConvertPqTabtype(stTableInfo.u16TableType);

            stqmapdata.pu16Table_Map_Array[(u8column * stqmapdata.u32Table_IP_Num + E_KDRV_XC_CFD_QMAPDATA_INFO_TABLE_TYPE_ALIENMENT)] = u16PqTabType;
        }
        stqmapdata.u32Table_Data_Size = stqmapdata.u32Table_Data_Size + ( PQ_BIN_BANK_SIZE + PQ_BIN_ADDR_SIZE + PQ_BIN_MASK_SIZE + (MS_U32)stTableInfo.u16GroupNum) * stTableInfo.u16RegNum;
    }

    PQ_MALLOC_MEM(stqmapdata.pu8Table_Data_Array, (stqmapdata.u32Table_Data_Size * sizeof(MS_U8)));

    if(stqmapdata.pu8Table_Data_Array == NULL)
    {
        PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
        PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));

        return FALSE;
    }

    for (u8column = 0; u8column < stqmapdata.u32Quality_IP_Num; u8column++)
    {
        u16IPIdx = MDrv_PQ_GetGRule_IPIndex(eWindow, eGruleIndex, u8column);
        MDrv_PQBin_GetTable(pInstance,
                            MDrv_PQBin_GetPanelIdx(pInstance),
                            0,
                            u16IPIdx,
                            &stTableInfo,
                            &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        u32Addr = stTableInfo.u32TabOffset;
        u32tabsize = ( PQ_BIN_BANK_SIZE + PQ_BIN_ADDR_SIZE + PQ_BIN_MASK_SIZE + (MS_U32)stTableInfo.u16GroupNum) * stTableInfo.u16RegNum;
        for (u32tabcnt = 0; u32tabcnt < u32tabsize; u32tabcnt++)
        {
            stqmapdata.pu8Table_Data_Array[(u32tabcnt + u32tabalign)] = *((MS_U8 *)(u32Addr + u32tabcnt));
        }
        u32tabalign = u32tabalign + u32tabsize;
    }

    stqmapdata.u32Version = PQ_QMAPDATA_VERSION;
    stqmapdata.u16Length = sizeof(ST_XC_PQ_QMAPDATA);
#if (PQ_QMAPDATA_VERSION >= 2)
    stqmapdata.eQmapType = eQmapType;
#endif

#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION >= 2)
#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_QMAPDATA;
    stCfdCtrlInfo.pParam = &stqmapdata;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_PQ_QMAPDATA);

    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Scaler device not opened!!!!\n");
        }
    }

    if (ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stCfdCtrlInfo))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDRV_XC_IOC_CFDCONCTRL UPDATE GRULE DATA fail!!!!\n");
    }

    if (stCfdCtrlInfo.u16ErrCode != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDRV_XC_IOC_CFDCONCTRL UPDATE GRULE DATA fail, errCode: %d\n", stCfdCtrlInfo.u16ErrCode);
    }
#endif
#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_QMAPDATA;
    stCfdCtrlInfo.pParam = &stqmapdata;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_PQ_QMAPDATA);
    MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrlInfo);
#endif
#endif
#endif

    PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
    PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));
    PQ_FREE_MEM(stqmapdata.pu8Table_Data_Array,(stqmapdata.u32Table_Data_Size * sizeof(MS_U8)));

    return TRUE;
}
#endif

void SYMBOL_WEAK MDrv_PQ_SetQMAPGruleData(PQ_WIN eWindow, EN_PQ_QMAP_TYPE eQmapType)
{
#if (PQ_SUPPORT_SETGRULEDATA == 1)
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_SetQmapGrule PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.eQmapType = eQmapType;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_QMAPGRULE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
#else
    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;33m[MARCOS] not support\33[m\n");
#endif
}

void MDrv_PQ_SetQMAPGruleData_U2(void* pInstance, PQ_WIN eWindow, EN_PQ_QMAP_TYPE eQmapType)
{
#if (PQ_SUPPORT_SETGRULEDATA == 1)
    switch(eQmapType)
    {
        case E_PQ_HDR_GRULE:
        {
            _MDrv_PQ_SetQMAPGruleData(pInstance, eWindow, eQmapType);
            break;
        }
        case E_PQ_COLOR_HDR_GRULE:
        {
#if (ENABLE_PQ_CUSTOMER_BIN_MAIN_GRULE == 1)
        if((eWindow == PQ_MAIN_WINDOW) && _bCustomerMainPQEn)
        {
            _MDrv_PQ_SetColorQMAPGruleData(pInstance, eWindow, eQmapType);
        }
#endif
            break;
        }
        case E_PQ_TMO_DLC_GRULE:
        {
#if defined(ENABLE_PQ_TMO_GRULE)&&(ENABLE_PQ_TMO_GRULE==1)
        if (_bTMOPQEn)
        {
            _MDrv_PQ_SetQMAPTMOGruleData(pInstance, eWindow, eQmapType);
        }
#endif
            break;
        }
        default :
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Wrong Grule in SetQMAPGruleData\n");
            break;
        }
    }
#else
    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;33m[MARCOS] not support\33[m\n");
#endif
}


void _MDrv_PQ_SetHSYSeamless(void* pInstance)
{
#if (ENABLE_PQ_BIN_HSY)
    //HSY Seamless
    ST_XC_PQ_HSY_TABLE stXCPQHSYTable;
    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    MS_U32 u32ConfigSize = 0;
    MS_U32 u32HSYGRuleIP = 0;
    MS_U8 *pu8TmpAddr = NULL;
    MS_BOOL bGruleTableEntryMallocCheck = TRUE;
    MS_BOOL bExtractCheck = FALSE;
    MS_BOOL bEGrulExtractCheck = TRUE;
    MS_BOOL bEntryNumGruleCheck = TRUE;

    memset(&stXCPQHSYTable, 0, sizeof(ST_XC_PQ_HSY_TABLE));
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));

    //HSY Seamless
    MS_U16 u16HSYBinID = MDrv_HSYBin_GetBinID(E_HSY_BIN_ID_HSY);
    u32ConfigSize = (MS_U32)((uintptr_t)pstBinMap[u16HSYBinID].pu8CacheEnd - (uintptr_t)pstBinMap[u16HSYBinID].pIndexedConfig[0] + 1);

    stXCPQHSYTable.u32Version = CFD_HSY_TABLE_VERSION;
    stXCPQHSYTable.u16Length = sizeof(ST_XC_PQ_HSY_TABLE);
    stXCPQHSYTable.u32ConfigSize = u32ConfigSize;
    stXCPQHSYTable.u32GRuleIPNum = pstBinMap[u16HSYBinID].pBinHeader->u32ExtRuleNum;

    PQ_MALLOC_MEM(stXCPQHSYTable.pu8Config,u32ConfigSize);
    if (stXCPQHSYTable.pu8Config != NULL)
    {
        //Main Rule malloc
        PQ_MALLOC_MEM(stXCPQHSYTable.pstTableEntry,sizeof(ST_XC_PQ_HSY_TABLE_ENTRY)*pstBinMap[u16HSYBinID].pRuleSection->pRuleHeader->u16EntryNum);
        if(stXCPQHSYTable.pstTableEntry != NULL)
        {
            memcpy(stXCPQHSYTable.pu8Config, (MS_U8*)pstBinMap[u16HSYBinID].pIndexedConfig[0],u32ConfigSize);
            stXCPQHSYTable.pstTableEntry->u32Version = CFD_HSY_TABLE_ENTRY_VERSION;
            stXCPQHSYTable.pstTableEntry->u16Length = sizeof(ST_XC_PQ_HSY_TABLE_ENTRY);

            //Main Rule Extract
            stXCPQHSYTable.u16EntryNum = pstBinMap[u16HSYBinID].pRuleSection->pRuleHeader->u16EntryNum;
            if(!MDrv_HSYBin_ExtractRule(pstBinMap[u16HSYBinID].pRuleSection,&pstBinMap[u16HSYBinID],stXCPQHSYTable.pstTableEntry))
            {
                PQ_LEVEL_PRINTF((PQ_DBG_ERROR|PQ_DBG_HSY),"ERROR!! HSY Bin Extract Fail!!!\n");
            }
            else
            {
                bExtractCheck = TRUE;
            }
        }

        //GRule malloc
        if (stXCPQHSYTable.u32GRuleIPNum > 0)
        {
            PQ_MALLOC_MEM(pu8TmpAddr,sizeof(ST_XC_PQ_HSY_TABLE_ENTRY*)*stXCPQHSYTable.u32GRuleIPNum);
            if(pu8TmpAddr == NULL)
            {
                PQ_LEVEL_PRINTF((PQ_DBG_ERROR|PQ_DBG_HSY),"ERROR!! GruleTableEntry* malloc fail!!!\n");
            }
            else
            {
                stXCPQHSYTable.pstGruleTableEntry = (ST_XC_PQ_HSY_TABLE_ENTRY**)pu8TmpAddr;

                for(u32HSYGRuleIP = 0; u32HSYGRuleIP < stXCPQHSYTable.u32GRuleIPNum; u32HSYGRuleIP++)
                {
                    PQ_MALLOC_MEM(stXCPQHSYTable.pstGruleTableEntry[u32HSYGRuleIP], sizeof(ST_XC_PQ_HSY_TABLE_ENTRY) * pstBinMap[u16HSYBinID].pExtRuleSection[u32HSYGRuleIP]->pRuleHeader->u16EntryNum);
                    if(stXCPQHSYTable.pstGruleTableEntry[u32HSYGRuleIP] == NULL)
                    {
                        bGruleTableEntryMallocCheck = FALSE;
                        PQ_LEVEL_PRINTF((PQ_DBG_ERROR|PQ_DBG_HSY),"ERROR!! GruleTableEntry malloc fail!!!\n");
                    }
                }
                PQ_MALLOC_MEM(stXCPQHSYTable.pu16EntryNumGRule,sizeof(MS_U16)*stXCPQHSYTable.u32GRuleIPNum);

                if(stXCPQHSYTable.pu16EntryNumGRule == NULL)
                {
                    bEntryNumGruleCheck = FALSE;
                    PQ_LEVEL_PRINTF((PQ_DBG_ERROR|PQ_DBG_HSY),"ERROR!! pu16EntryNumGRule malloc fail!!!\n");
                }

                //Grule Extract
                if ((bGruleTableEntryMallocCheck == TRUE) && (bEntryNumGruleCheck == TRUE))
                {
                    for(u32HSYGRuleIP = 0; u32HSYGRuleIP < stXCPQHSYTable.u32GRuleIPNum; u32HSYGRuleIP++)
                    {
                        stXCPQHSYTable.pstGruleTableEntry[u32HSYGRuleIP]->u32Version = CFD_HSY_TABLE_ENTRY_VERSION;
                        stXCPQHSYTable.pstGruleTableEntry[u32HSYGRuleIP]->u16Length = sizeof(ST_XC_PQ_HSY_TABLE_ENTRY);
                        stXCPQHSYTable.pu16EntryNumGRule[u32HSYGRuleIP] = pstBinMap[u16HSYBinID].pExtRuleSection[u32HSYGRuleIP]->pRuleHeader->u16EntryNum;
                        if(!MDrv_HSYBin_ExtractRule(pstBinMap[u16HSYBinID].pExtRuleSection[u32HSYGRuleIP], &pstBinMap[u16HSYBinID], stXCPQHSYTable.pstGruleTableEntry[u32HSYGRuleIP]))
                        {
                            bEGrulExtractCheck = FALSE;
                            PQ_LEVEL_PRINTF((PQ_DBG_ERROR|PQ_DBG_HSY),"ERROR!! HSY Bin Extract Fail!!!\n");
                        }
                    }
                }
            }
        }
        if((bExtractCheck == TRUE) && (bEGrulExtractCheck == TRUE) && (bEntryNumGruleCheck == TRUE))
        {
            //CFD_Control
            stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_HSY_TABLE;
            stCfdCtrlInfo.pParam = &stXCPQHSYTable;
            stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_PQ_HSY_TABLE);
            MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrlInfo);
        }
    }

    //Main Rule Free
    PQ_FREE_MEM(stXCPQHSYTable.pu8Config,u32ConfigSize);
    if(!MDrv_HSYBin_FREE_HSYTable(pstBinMap[u16HSYBinID].pRuleSection,&pstBinMap[u16HSYBinID],stXCPQHSYTable.pstTableEntry))
    {
        PQ_LEVEL_PRINTF((PQ_DBG_ERROR|PQ_DBG_HSY),"ERROR!! Fail to Free HSY Table!!!\n");
    }
    PQ_FREE_MEM(stXCPQHSYTable.pstTableEntry,sizeof(ST_XC_PQ_HSY_TABLE_ENTRY)*pstBinMap[u16HSYBinID].pRuleSection->pRuleHeader->u16EntryNum);

    //GRule Free
    if (stXCPQHSYTable.u32GRuleIPNum > 0)
    {
        for(u32HSYGRuleIP = 0; u32HSYGRuleIP < stXCPQHSYTable.u32GRuleIPNum; u32HSYGRuleIP++)
        {
            if(!MDrv_HSYBin_FREE_HSYTable(pstBinMap[u16HSYBinID].pExtRuleSection[u32HSYGRuleIP],&pstBinMap[u16HSYBinID],stXCPQHSYTable.pstGruleTableEntry[u32HSYGRuleIP]))
            {
                PQ_LEVEL_PRINTF((PQ_DBG_ERROR|PQ_DBG_HSY),"ERROR!! Fail to Free HSY Table!!!\n");
            }
            PQ_FREE_MEM(stXCPQHSYTable.pstGruleTableEntry[u32HSYGRuleIP],sizeof(ST_XC_PQ_HSY_TABLE_ENTRY)*pstBinMap[u16HSYBinID].pExtRuleSection[u32HSYGRuleIP]->pRuleHeader->u16EntryNum);
        }
        PQ_FREE_MEM(stXCPQHSYTable.pstGruleTableEntry,sizeof(ST_XC_PQ_HSY_TABLE_ENTRY*)*stXCPQHSYTable.u32GRuleIPNum);
        PQ_FREE_MEM(stXCPQHSYTable.pu16EntryNumGRule,sizeof(MS_U16)*stXCPQHSYTable.u32GRuleIPNum);
    }
#endif
}

void _MDrv_PQ_SetTMOGRuleDara(void* pInstance)
{
#if (ENABLE_PQ_BIN_TMO)
    MS_U8 u8Idx;
    MS_U16 u16PQ_CurLvlIdx = 0;
    ST_TMO_LEVEL stTmoLevel[PQ_GRULE_TMO_MODE_LVL_NUM_Main];
    memset(stTmoLevel, 0, PQ_GRULE_TMO_MODE_LVL_NUM_Main*sizeof(ST_TMO_LEVEL));

    _u16PQSrcType_TMO[0] = QM_SMPTE2084_TMO_Main;

    for(u8Idx=0; u8Idx<PQ_GRULE_TMO_MODE_LVL_NUM_Main; u8Idx++)
    {
        gbTmoSettingMalloc = 0;
        gpu8TmoSettingData = NULL;

        u16PQ_CurLvlIdx = MDrv_PQ_GetGRule_LevelIndex_Ex(PQ_MAIN_WINDOW, PQ_GRule_TMO_MODE_Main, u8Idx, PQ_XC0_TMO);
        _MDrv_PQ_SetGRuleStatus_Ex(pInstance, PQ_MAIN_WINDOW, PQ_GRule_TMO_MODE_Main, u16PQ_CurLvlIdx, PQ_XC0_TMO);

        if(NULL != gpu8TmoSettingData)
        {
            stTmoLevel[u8Idx].u16HdrType = E_XC_CFD_HDR_TYPE_OPEN;
            stTmoLevel[u8Idx].u16Level = u8Idx;
            stTmoLevel[u8Idx].u16ControlSize = PQ_IP_TMO_NUM_Main* gu16RegNum;
            stTmoLevel[u8Idx].pu8data = gpu8TmoSettingData;
        }
        else
        {
            stTmoLevel[u8Idx].u16HdrType = 0xFFFF;
            stTmoLevel[u8Idx].u16Level = 0xFFFF;
            stTmoLevel[u8Idx].u16ControlSize = 0xFFFF;
            stTmoLevel[u8Idx].pu8data = NULL;
        }
    }

    ST_KDRV_XC_CFD_TMO stCfdTmo;
    memset(&stCfdTmo, 0, sizeof(ST_KDRV_XC_CFD_TMO));
    stCfdTmo.u32Version = CFD_TMO_VERSION;
    stCfdTmo.u16Length = sizeof(ST_KDRV_XC_CFD_TMO);
    stCfdTmo.u16LevelCount = PQ_GRULE_TMO_MODE_LVL_NUM_Main;
    stCfdTmo.pstTmoLevel = stTmoLevel;

    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_TMO;
    stCfdCtrlInfo.pParam = &stCfdTmo;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_KDRV_XC_CFD_TMO);

    MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrlInfo);

    for(u8Idx=0; u8Idx<PQ_GRULE_TMO_MODE_LVL_NUM_Main; u8Idx++)
    {
        if(NULL != stTmoLevel[u8Idx].pu8data)
        {
#if !defined(MSOS_TYPE_LINUX_KERNEL)
            free(stTmoLevel[u8Idx].pu8data);            //data is malloc in MDrv_PQBin_DumpDumyRegister function
#else
            vfree(stTmoLevel[u8Idx].pu8data);
#endif
        }
    }
#else
    ST_TMO_LEVEL stTmoLevel[PQ_GRULE_TMO_MODE_LVL_NUM_TMO];
    memset(stTmoLevel, 0, PQ_GRULE_TMO_MODE_LVL_NUM_TMO*sizeof(ST_TMO_LEVEL));

    _u16PQSrcType_TMO[0] = QM_SMPTE2084_TMO;
    for(u8Idx=0; u8Idx<PQ_GRULE_TMO_MODE_LVL_NUM_TMO; u8Idx++)
    {
        gbTmoSettingMalloc = 0;
        gpu8TmoSettingData = NULL;

        u16PQ_CurLvlIdx = MDrv_PQ_GetGRule_LevelIndex_Ex(PQ_MAIN_WINDOW, PQ_GRule_TMO_MODE_TMO, u8Idx, PQ_XC0_TMO);
        _MDrv_PQ_SetGRuleStatus_Ex(pInstance, PQ_MAIN_WINDOW, PQ_GRule_TMO_MODE_TMO, u16PQ_CurLvlIdx, PQ_XC0_TMO);

        if(NULL != gpu8TmoSettingData)
        {
            stTmoLevel[u8Idx].u16HdrType = E_XC_CFD_HDR_TYPE_OPEN;
            stTmoLevel[u8Idx].u16Level = u8Idx;
            stTmoLevel[u8Idx].u16ControlSize = PQ_IP_NUM_TMO * gu16RegNum;
            stTmoLevel[u8Idx].pu8data = gpu8TmoSettingData;
        }
        else
        {
            stTmoLevel[u8Idx].u16HdrType = 0xFFFF;
            stTmoLevel[u8Idx].u16Level = 0xFFFF;
            stTmoLevel[u8Idx].u16ControlSize = 0xFFFF;
            stTmoLevel[u8Idx].pu8data = NULL;
        }
    }

    ST_KDRV_XC_CFD_TMO stCfdTmo;
    memset(&stCfdTmo, 0, sizeof(ST_KDRV_XC_CFD_TMO));
    stCfdTmo.u32Version = CFD_TMO_VERSION;
    stCfdTmo.u16Length = sizeof(ST_KDRV_XC_CFD_TMO);
    stCfdTmo.u16LevelCount = PQ_GRULE_TMO_MODE_LVL_NUM_TMO;
    stCfdTmo.pstTmoLevel = stTmoLevel;

    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_TMO;
    stCfdCtrlInfo.pParam = &stCfdTmo;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_KDRV_XC_CFD_TMO);

    MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrlInfo);

    for(u8Idx=0; u8Idx<PQ_GRULE_TMO_MODE_LVL_NUM_TMO; u8Idx++)
    {
        if(NULL != stTmoLevel[u8Idx].pu8data)
        {
#if !defined(MSOS_TYPE_LINUX_KERNEL)
            free(stTmoLevel[u8Idx].pu8data);            //data is malloc in MDrv_PQBin_DumpDumyRegister function
#else
            vfree(stTmoLevel[u8Idx].pu8data);
#endif
        }
    }
#endif
}

#if (PQ_SUPPORT_SETQMAPDATA == 1)

void SYMBOL_WEAK MDrv_PQ_SetQMAPData(void* pInstance, MS_PQBin_Header_Info *pstPQBinHeader, MS_U8 u8ID)
{
    MS_U8 u8PanelIdx= MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO);
    ST_XC_PQ_QMAPDATA stqmapdata;
    MS_U8 u8row =0, u8column = 0;
    MS_PQBin_IP_Table_Info stTableInfo;
    MS_PHYADDR u32Addr;
    MS_U32 u32tabsize = 0;
    MS_U32 u32tabcnt = 0;
    MS_U32 u32tabalign = 0;
    memset(&stqmapdata, 0, sizeof(ST_XC_PQ_QMAPDATA));
    stqmapdata.u32Qmapid = u8ID;
    stqmapdata.u32Quality_InputType_Num = QM_TMO_INPUTTYPE_NUM_Main;
    stqmapdata.u32Quality_IP_Num = pstPQBinHeader->u16IP_Comm_Num;
    stqmapdata.u32Table_IP_Num = (MS_U32)XC_PQ_IP_INFORMATION_SIZE;

    PQ_MALLOC_MEM(stqmapdata.pu16Quality_Map_Array, (stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
    PQ_MALLOC_MEM(stqmapdata.pu16Table_Map_Array, (stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));

    if((stqmapdata.pu16Quality_Map_Array == NULL) || (stqmapdata.pu16Table_Map_Array == NULL))
    {
        PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
        PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));

        return;
    }

//[Step 1]==============================================//
//   Quality_Map: All source TableIdx                   //
//======================================================//
    for (u8column = 0; u8column < stqmapdata.u32Quality_InputType_Num; u8column++)
    {
        for (u8row = 0; u8row < stqmapdata.u32Quality_IP_Num; u8row++)
        {
            if (MDrv_PQBin_GetSkipRule(pInstance, u8row, pstPQBinHeader))
            {
                stqmapdata.pu16Quality_Map_Array[(u8column * stqmapdata.u32Quality_IP_Num +u8row)] = PQ_BIN_IP_NULL;
            }
            else
            {
                stqmapdata.pu16Quality_Map_Array[(u8column * stqmapdata.u32Quality_IP_Num +u8row)] = MDrv_PQBin_GetTableIndex(pInstance,u8column,
                                                                                                                              u8row,
                                                                                                                              u8PanelIdx,
                                                                                                                              pstPQBinHeader);
            }
        }
    }

//[Step 2]==============================================//
//   Table[0]:How many tableIdx in each IP              //
//   Table[1]:How many register in each IP              //
//   Table[2]:(new)Table type of each IP                //
//======================================================//
    for (u8column = 0; u8column < stqmapdata.u32Quality_IP_Num; u8column++)
    {
        MDrv_PQBin_GetTable(pInstance,u8PanelIdx,
                            0,
                            u8column,
                            &stTableInfo,
                            pstPQBinHeader);
        stqmapdata.pu16Table_Map_Array[(u8column * stqmapdata.u32Table_IP_Num)] = stTableInfo.u16GroupNum;
        stqmapdata.pu16Table_Map_Array[(u8column * stqmapdata.u32Table_IP_Num + 1)] = stTableInfo.u16RegNum;
        if(stqmapdata.u32Table_IP_Num > (MS_U32)E_KDRV_XC_CFD_QMAPDATA_INFO_TABLE_TYPE_ALIENMENT)
        {
            MS_U16 u16PqTabType = (MS_U16)_MDrv_PQ_ConvertPqTabtype(stTableInfo.u16TableType);

            stqmapdata.pu16Table_Map_Array[(u8column * stqmapdata.u32Table_IP_Num + E_KDRV_XC_CFD_QMAPDATA_INFO_TABLE_TYPE_ALIENMENT)] = u16PqTabType;
        }
        stqmapdata.u32Table_Data_Size = stqmapdata.u32Table_Data_Size + ( PQ_BIN_BANK_SIZE + PQ_BIN_ADDR_SIZE + PQ_BIN_MASK_SIZE + (MS_U32)stTableInfo.u16GroupNum) * stTableInfo.u16RegNum;
    }

    PQ_MALLOC_MEM(stqmapdata.pu8Table_Data_Array, (stqmapdata.u32Table_Data_Size * sizeof(MS_U8)));

    if(stqmapdata.pu8Table_Data_Array == NULL)
    {
        PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
        PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));

        return;
    }

//[Step 3]==============================================//
//   TableData[0]:Bank                                  //
//   TableData[1]:Addr                                  //
//   TableData[2]:Mask                                  //
//   TableData[3]:How many register in each IP          //
//             .                                        //
//             .                                        //
//======================================================//
    for (u8column = 0; u8column < stqmapdata.u32Quality_IP_Num; u8column++)
    {
        MDrv_PQBin_GetTable(pInstance,u8PanelIdx,
                            0,
                            u8column,
                            &stTableInfo,
                            pstPQBinHeader);
        u32Addr = stTableInfo.u32TabOffset;
        u32tabsize = ( PQ_BIN_BANK_SIZE + PQ_BIN_ADDR_SIZE + PQ_BIN_MASK_SIZE + (MS_U32)stTableInfo.u16GroupNum) * stTableInfo.u16RegNum;
        for (u32tabcnt = 0; u32tabcnt < u32tabsize; u32tabcnt++)
        {
            stqmapdata.pu8Table_Data_Array[(u32tabcnt + u32tabalign)] = *((MS_U8 *)(u32Addr + u32tabcnt));
        }
        u32tabalign = u32tabalign + u32tabsize;
    }
    stqmapdata.u32Version = PQ_QMAPDATA_VERSION;
    stqmapdata.u16Length = sizeof(ST_XC_PQ_QMAPDATA);
#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION >= 2)
#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)

    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_QMAPDATA;
    stCfdCtrlInfo.pParam = &stqmapdata;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_PQ_QMAPDATA);

    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Scaler device not opened!!!!\n");
        }
    }

    if (ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stCfdCtrlInfo))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDRV_XC_IOC_CFDCONCTRL UPDATE INFO fail!!!!\n");
    }

    if (stCfdCtrlInfo.u16ErrCode != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDRV_XC_IOC_CFDCONCTRL UPDATE INFO fail, errCode: %d\n", stCfdCtrlInfo.u16ErrCode);
    }
#endif
#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_QMAPDATA;
    stCfdCtrlInfo.pParam = &stqmapdata;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_PQ_QMAPDATA);
    MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrlInfo);
#endif
#endif
#endif

    PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
    PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));
    PQ_FREE_MEM(stqmapdata.pu8Table_Data_Array,(stqmapdata.u32Table_Data_Size * sizeof(MS_U8)));
}
#endif

void MDrv_PQ_LoadCommTable(void *pInstance, XC_GET_DS_INFO *pstGetDSInfo, MS_U8 u8PanelIdx, MS_U8 u8PQID)
{
#if (defined(UFO_XC_DS_PQ))

#if (!defined(MSOS_TYPE_LINUX_KERNEL))
        if(!pstGetDSInfo->bUpdate_DS_CMD)
#endif
        {
#endif
            MDrv_PQBin_LoadCommTable(pInstance, u8PanelIdx, &stPQBinHeaderInfo[u8PQID]);
#if (defined(UFO_XC_DS_PQ))
        }
#endif
}

//Equal function
MS_BOOL _Mdrv_PQ_CheckIpIndex(void *pa, void *pb)
{
    if(pa == NULL || pb == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"[%s %d] Null Pointer !!!!\n");
        return 0;
    }
    pqIpTable stIpTableA = *(pqIpTable*) pa;
    pqIpTable stIpTableB = *(pqIpTable*) pb;
    return stIpTableA.u16IpIndex == stIpTableB.u16IpIndex;
}

void MDrv_PQ_Init_U2(void* pInstance,MS_PQ_Init_Info *pstPQInitInfo)
{
#if (defined(MSOS_TYPE_LINUX_KERNEL))
    //Because _status.bIsInitialized is Global variable,
    //In linux kernel driver only one instance.
    //We must return here to avoid re-init write other global variable to init value
    if(_status.bIsInitialized == TRUE)
    {
        printk("[%s,%5d]_status.bIsInitialized %d, return.\033[0m\n",__FUNCTION__,__LINE__,_status.bIsInitialized);
        return;
    }
#endif

    MS_U8 u8Idx = 0;
    MS_U8 u8ID = 0;
    MS_U32 u32TmpRet = 0;
    XC_GET_DS_INFO stGetDSInfo;
    _s32LastPQIPMADiMain = -1;
    _s32LastPQIPMemFormatMain = -1;
    _s32LastPQIPHSDYMain = -1;
    _s32LastPQIPHSDCMain = -1;
    _s32LastPQIPMADiSub = -1;
    _s32LastPQIPMemFormatSub = -1;
    _s32LastPQIPMADiMotMain = -1;
    _s32LastPQIPMADiMotSub = -1;

    _bCustomerMainPQEn = FALSE ;
    _bCustomerSubPQEn = FALSE ;
    _bUFSCPQEn = FALSE;
    _u16LastPQSrcType[PQ_MAIN_WINDOW] = 0xFFFF;
    _u16LastPQSrcType[PQ_SUB_WINDOW] = 0xFFFF;
    _bNeedRestoreWRBankMappingNum = FALSE;
    _gu16PQDbgXRuleBypass = 0;

    _PQ_Mutex = MsOS_CreateMutex(E_MSOS_FIFO, "_PQ_Mutex", MSOS_PROCESS_SHARED);

    if(_PQ_Mutex == -1)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"[MAPI PQ] create mutex fail\n");
        return;
    }

#ifdef UFO_XC_PQ_SUPPORT_INITVAR_INTO_SHAREMEMORY
    PQ_INSTANCE_PRIVATE *psPQInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psPQInstPri);
    PQ_RESOURCE_PRIVATE* pPQResourcePrivate = NULL;
    MDrv_PQ_Get_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
    UtopiaResourceGetPrivate(g_pPQResource[E_PQ_DEVICE_ID_0],(void**)(&pPQResourcePrivate));

    memcpy(&pPQResourcePrivate->PQ_InitInfo, pstPQInitInfo, sizeof(MS_PQ_Init_Info));

    MDrv_PQ_Release_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
#endif

    memset(&stGetDSInfo, 0, sizeof(XC_GET_DS_INFO));
    MApi_XC_GetDSInfo(&stGetDSInfo, sizeof(XC_GET_DS_INFO), PQ_MAIN_WINDOW);

#ifdef MSOS_TYPE_LINUX
#if(ENABLE_PQ_MLOAD)
    PTH_PQ_RET_CHK(pthread_mutexattr_t attr);
    PTH_PQ_RET_CHK(pthread_mutexattr_init(&attr));
    PTH_PQ_RET_CHK(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
    PTH_PQ_RET_CHK(pthread_mutex_init(&_PQ_MLoad_Mutex, &attr));
#endif
#endif
    _u8ChipRev = MDrv_SYS_GetChipRev();
    PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Get Chip Version %u\n",_u8ChipRev);

    _status.bIsInitialized = TRUE;
    _status.bIsRunning     = TRUE;
    _u16PQAssertLevel      = E_PQ_PROCFS_SEL_ASSERT_PANIC;
    _u16PQProcfsFastPlayByMemc = TRUE;

    //[Load PQ Bin]=========================================================
    _MDrv_PQ_LoadPQBin(pInstance,pstPQInitInfo);

    if(pstPQInitInfo->u8PQTextBinCnt)
    {
        gbEnablePQTextBin = 1;
        for(u8Idx = 0; u8Idx < pstPQInitInfo->u8PQTextBinCnt; u8Idx++)
        {
            u8ID = pstPQInitInfo->stPQTextBinInfo[u8Idx].u8PQID;
            stPQTextBinHeaderInfo[u8ID].u8BinID = pstPQInitInfo->stPQTextBinInfo[u8Idx].u8PQID;
            stPQTextBinHeaderInfo[u8ID].u32BinStartAddress = (pstPQInitInfo->stPQTextBinInfo[u8Idx].PQBin_PhyAddr);
            MDrv_PQTextBin_Parsing(pInstance,&stPQTextBinHeaderInfo[u8ID]);
        }
    }
    else
    {
        gbEnablePQTextBin = 0;
    }

    if (pstPQInitInfo->u8PQBinCnt)
    {
        gbPQBinEnable = 1;
        MDrv_PQ_Init_DisplayType_U2(pInstance, pstPQInitInfo->u16PnlWidth, PQ_BIN_DISPLAY_ONE);
        //because main and sub's common table may be different,
        //so we will take main's common table as correct one.
        //so here load sub first and then use main's overwrite sub's
        //Sub first
        if (pstPQInitInfo->u8PQBinCnt > 2)
        {
            //[Sub.bin]========================================================
            u8ID = pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_SUB].u8PQID;
            stPQBinHeaderInfo[u8ID].u8BinID = (MS_U8)pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_SUB].u8PQID;
            stPQBinHeaderInfo[u8ID].u32BinStartAddress = pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_SUB].PQBin_PhyAddr;
            u32PQInitBinAddr[u8ID] = stPQBinHeaderInfo[u8ID].u32BinStartAddress;
            MDrv_PQBin_Parsing(pInstance, &stPQBinHeaderInfo[u8ID]);
            MDrv_PQ_LoadCommTable(pInstance, &stGetDSInfo, MDrv_PQBin_GetPanelIdx(pInstance), u8ID);

            //[Sub_Ext.bin]====================================================
            u8ID = pstPQInitInfo->stPQBinInfo[PQ_BIN_EXT_SUB].u8PQID;
            stPQBinHeaderInfo[u8ID].u8BinID = (MS_U8)pstPQInitInfo->stPQBinInfo[PQ_BIN_EXT_SUB].u8PQID;
            stPQBinHeaderInfo[u8ID].u32BinStartAddress = pstPQInitInfo->stPQBinInfo[PQ_BIN_EXT_SUB].PQBin_PhyAddr;
            u32PQInitBinAddr[u8ID] = stPQBinHeaderInfo[u8ID].u32BinStartAddress;
            MDrv_PQBin_Parsing(pInstance, &stPQBinHeaderInfo[u8ID]);
            MDrv_PQ_LoadCommTable(pInstance, &stGetDSInfo, MDrv_PQBin_GetPanelIdx(pInstance), u8ID);
        }

        if (pstPQInitInfo->u8PQBinCustomerCnt == 2)
        {
            u8ID = pstPQInitInfo->stPQBinInfo[PQ_BIN_CUSTOMER_SUB].u8PQID;
            stPQBinHeaderInfo[u8ID].u8BinID = (MS_U8)pstPQInitInfo->stPQBinInfo[PQ_BIN_CUSTOMER_SUB].u8PQID;
            stPQBinHeaderInfo[u8ID].u32BinStartAddress = pstPQInitInfo->stPQBinInfo[PQ_BIN_CUSTOMER_SUB].PQBin_PhyAddr;
            u32PQInitBinAddr[u8ID] = stPQBinHeaderInfo[u8ID].u32BinStartAddress;
            MDrv_PQBin_Parsing(pInstance, &stPQBinHeaderInfo[u8ID]);
            MDrv_PQ_LoadCommTable(pInstance, &stGetDSInfo, PQ_BIN_CUSTOMER_PANEL_INDEX, u8ID);
            _bCustomerSubPQEn = TRUE;
        }

        //[Main.bin]============================================================
        u8ID = pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_MAIN].u8PQID;
        stPQBinHeaderInfo[u8ID].u8BinID = (MS_U8)pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_MAIN].u8PQID;
        stPQBinHeaderInfo[u8ID].u32BinStartAddress = pstPQInitInfo->stPQBinInfo[PQ_BIN_STD_MAIN].PQBin_PhyAddr;
        u32PQInitBinAddr[u8ID] = stPQBinHeaderInfo[u8ID].u32BinStartAddress;
        MDrv_PQBin_Parsing(pInstance, &stPQBinHeaderInfo[u8ID]);
        MDrv_PQ_LoadCommTable(pInstance, &stGetDSInfo, MDrv_PQBin_GetPanelIdx(pInstance), u8ID);

        //[Main_Ext.bin]========================================================
        u8ID = pstPQInitInfo->stPQBinInfo[PQ_BIN_EXT_MAIN].u8PQID;
        stPQBinHeaderInfo[u8ID].u8BinID = (MS_U8)pstPQInitInfo->stPQBinInfo[PQ_BIN_EXT_MAIN].u8PQID;
        stPQBinHeaderInfo[u8ID].u32BinStartAddress = pstPQInitInfo->stPQBinInfo[PQ_BIN_EXT_MAIN].PQBin_PhyAddr;
        MDrv_PQBin_Parsing(pInstance, &stPQBinHeaderInfo[u8ID]);
        MDrv_PQ_LoadCommTable(pInstance, &stGetDSInfo, MDrv_PQBin_GetPanelIdx(pInstance), u8ID);

        //[Main_Color.bin]======================================================
        if (pstPQInitInfo->u8PQBinCustomerCnt >= 1)
        {
            u8ID = pstPQInitInfo->stPQBinInfo[PQ_BIN_CUSTOMER_MAIN].u8PQID;
            stPQBinHeaderInfo[u8ID].u8BinID = (MS_U8)pstPQInitInfo->stPQBinInfo[PQ_BIN_CUSTOMER_MAIN].u8PQID;
            stPQBinHeaderInfo[u8ID].u32BinStartAddress = pstPQInitInfo->stPQBinInfo[PQ_BIN_CUSTOMER_MAIN].PQBin_PhyAddr;
            u32PQInitBinAddr[u8ID] = stPQBinHeaderInfo[u8ID].u32BinStartAddress;
            MDrv_PQBin_Parsing(pInstance, &stPQBinHeaderInfo[u8ID]);
            MDrv_PQ_LoadCommTable(pInstance, &stGetDSInfo, PQ_BIN_CUSTOMER_PANEL_INDEX, u8ID);
            _bCustomerMainPQEn = TRUE;
        }

        //[UFSC.bin]============================================================
        if (pstPQInitInfo->u8PQBinUFSCCnt == 1)
        {
            u8ID = pstPQInitInfo->stPQBinInfo[PQ_BIN_UFSC].u8PQID;
            stPQBinHeaderInfo[u8ID].u8BinID = (MS_U8)pstPQInitInfo->stPQBinInfo[PQ_BIN_UFSC].u8PQID;
            stPQBinHeaderInfo[u8ID].u32BinStartAddress = pstPQInitInfo->stPQBinInfo[PQ_BIN_UFSC].PQBin_PhyAddr;
            u32PQInitBinAddr[u8ID] = stPQBinHeaderInfo[u8ID].u32BinStartAddress;
            MDrv_PQBin_Parsing(pInstance, &stPQBinHeaderInfo[u8ID]);
            MDrv_PQ_LoadCommTable(pInstance, &stGetDSInfo, PQ_BIN_UFSC_PANEL_INDEX, u8ID);
            _bUFSCPQEn = TRUE;
        }
#if (ENABLE_PQ_BIN_CF)
        //[CF.bin]==============================================================
        if(pstPQInitInfo->u8PQBinCFCnt >= 1)
        {
            u8ID = pstPQInitInfo->stPQBinInfo[PQ_BIN_CF_MAIN].u8PQID;
            stPQBinHeaderInfo[u8ID].u8BinID = (MS_U8)pstPQInitInfo->stPQBinInfo[PQ_BIN_CF_MAIN].u8PQID;
            stPQBinHeaderInfo[u8ID].u32BinStartAddress = pstPQInitInfo->stPQBinInfo[PQ_BIN_CF_MAIN].PQBin_PhyAddr;
            u32PQInitBinAddr[u8ID] = stPQBinHeaderInfo[u8ID].u32BinStartAddress;
            MDrv_PQBin_Parsing(pInstance, &stPQBinHeaderInfo[u8ID]);
            MDrv_PQ_LoadCommTable(pInstance, &stGetDSInfo, PQ_BIN_CF_PANEL_INDEX, u8ID);
            _bCFMainPQEn = TRUE ;
        }
        //[CF_SUB.bin]===========================================================
        if(pstPQInitInfo->u8PQBinCFCnt >= 2)
        {
            u8ID = pstPQInitInfo->stPQBinInfo[PQ_BIN_CF_SUB].u8PQID;
            stPQBinHeaderInfo[u8ID].u8BinID = (MS_U8)pstPQInitInfo->stPQBinInfo[PQ_BIN_CF_SUB].u8PQID;
            stPQBinHeaderInfo[u8ID].u32BinStartAddress = pstPQInitInfo->stPQBinInfo[PQ_BIN_CF_SUB].PQBin_PhyAddr;
            u32PQInitBinAddr[u8ID] = stPQBinHeaderInfo[u8ID].u32BinStartAddress;
            MDrv_PQBin_Parsing(pInstance, &stPQBinHeaderInfo[u8ID]);
            MDrv_PQ_LoadCommTable(pInstance, &stGetDSInfo, PQ_BIN_CF_PANEL_INDEX, u8ID);
            _bCFSubPQEn = TRUE ;
        }
#endif

#if (ENABLE_PQ_BIN_TMO == 1)
        //[Main_TMO.bin]========================================================
        if(pstPQInitInfo->u8PQBinTMOCnt == 1)
        {
            u8ID = pstPQInitInfo->stPQBinInfo[PQ_BIN_TMO].u8PQID;
            stPQBinHeaderInfo[u8ID].u8BinID = (MS_U8)pstPQInitInfo->stPQBinInfo[PQ_BIN_TMO].u8PQID;
            stPQBinHeaderInfo[u8ID].u32BinStartAddress = pstPQInitInfo->stPQBinInfo[PQ_BIN_TMO].PQBin_PhyAddr;
            u32PQInitBinAddr[u8ID] = stPQBinHeaderInfo[u8ID].u32BinStartAddress;
            MDrv_PQBin_Parsing(pInstance, &stPQBinHeaderInfo[u8ID]);
            MDrv_PQ_LoadCommTable(pInstance, &stGetDSInfo, MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO), u8ID);
            _bTMOPQEn = TRUE ;

#if (PQ_SUPPORT_SETQMAPDATA ==1)
            MDrv_PQ_SetQMAPData(pInstance, &stPQBinHeaderInfo[u8ID], u8ID);
#endif
            _MDrv_PQ_SetTMOGRuleDara(pInstance);
        }
#else
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "PQ do not transmit TMO data\n");
#endif

#if (ENABLE_PQ_BIN_HSY)
        if(u8PQHSYBinCnt)
        {
            MS_COLOR_RETURN_VALUE ret = E_COLOR_SUCCESS;
            MS_U16 u16BinIdx = 0;
            MS_U32 u32HSYGRuleIP = 0;
            u8ID = pstPQInitInfo->stPQBinInfo[PQ_BIN_HSY].u8PQID;
            stPQBinHeaderInfo[u8ID].u8BinID = (MS_U8)pstPQInitInfo->stPQBinInfo[PQ_BIN_HSY].u8PQID;
            stPQBinHeaderInfo[u8ID].u32BinStartAddress = (pstPQInitInfo->stPQBinInfo[PQ_BIN_HSY].PQBin_PhyAddr);
            u32PQInitBinAddr[u8ID] = stPQBinHeaderInfo[u8ID].u32BinStartAddress;

            ret = MDrv_ColorBin_Parse((MS_U8 *)pstPQInitInfo->stPQBinInfo[PQ_BIN_HSY].PQBin_PhyAddr, &stForestMap);
            if(stForestMap.stForestHeader.u16MaxBinPerModel == 0)
            {
                PQ_LEVEL_PRINTF((PQ_DBG_ERROR|PQ_DBG_HSY),"HSY BIN is empty.\n");
                pstBinMap = NULL;
            }
            else
            {
                PQ_MALLOC_MEM(pstBinMap, sizeof(MS_BIN_MAP)*stForestMap.stForestHeader.u16MaxBinPerModel);
            }

            if(pstBinMap != NULL)
            {
                MDrv_HSYBin_Parse((MS_U8 *)pstPQInitInfo->stPQBinInfo[PQ_BIN_HSY].PQBin_PhyAddr, &stForestMap, pstBinMap);
                _MDrv_PQ_SetHSYSeamless(pInstance);

                //HSY Debug Message
                PQ_LEVEL_PRINTF(PQ_DBG_HSY,"========================HSY BIN Parser=============================\n");
                PQ_LEVEL_PRINTF(PQ_DBG_HSY,"HSY BIN Parse Result=[%s], there are [%tu] bins in HSY Bin \n", (ret==0)?"SUCCESS":"FAIL", (ptrdiff_t)stForestMap.stForestHeader.u16MaxBinPerModel);
                for(u16BinIdx = 0; (u16BinIdx < stForestMap.stForestHeader.u16MaxBinPerModel); u16BinIdx++)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_HSY,"Bin[%tu] Main rule setting Num = %tu  \n", (ptrdiff_t)u16BinIdx, (ptrdiff_t)pstBinMap[u16BinIdx].pRuleSection->pRuleHeader->u16EntryNum);
                    PQ_LEVEL_PRINTF(PQ_DBG_HSY,"Bin[%tu] GRule Num = %tu  \n", (ptrdiff_t)u16BinIdx, (ptrdiff_t)pstBinMap[u16BinIdx].pBinHeader->u32ExtRuleNum);
                    for(u32HSYGRuleIP = 0; u32HSYGRuleIP < pstBinMap[u16BinIdx].pBinHeader->u32ExtRuleNum; u32HSYGRuleIP++)
                    {
                        PQ_LEVEL_PRINTF(PQ_DBG_HSY,"Bin[%tu] GRule[%tu] setting Num = %tu\n", (ptrdiff_t)u16BinIdx, (ptrdiff_t)u32HSYGRuleIP, (ptrdiff_t)pstBinMap[u16BinIdx].pExtRuleSection[u32HSYGRuleIP]->pRuleHeader->u16EntryNum);
                    }
                }
            }
            else
            {
                PQ_LEVEL_PRINTF((PQ_DBG_ERROR|PQ_DBG_HSY),"ERROR!! HSY BIN MAP malloc fail!\n");
            }
        }
#endif

    }
    else
    {
        gbPQBinEnable = 0;
        MDrv_PQ_Init_DisplayType_U2(pInstance, pstPQInitInfo->u16PnlWidth, PQ_DISPLAY_ONE);

#if(ENABLE_PQ_LOAD_TABLE_INFO)
        MDrv_PQ_PreInitLoadTableInfo(MAIN,pInstance);
        MDrv_PQ_PreInitLoadTableInfo(MAINEX,pInstance);

#if PQ_ENABLE_PIP
        MDrv_PQ_PreInitLoadTableInfo(SUB,pInstance);
        MDrv_PQ_PreInitLoadTableInfo(SUBEX,pInstance);
#endif

#endif
    }
#if PQ_ENABLE_PIP
    //dump sub c_sram3, c_sram4 to avoid garbage when enable pip, add these two into skip rule
#if (defined(UFO_XC_DS_PQ))

#if (!defined(MSOS_TYPE_LINUX_KERNEL))
        if(!stGetDSInfo.bUpdate_DS_CMD)
#endif
        {
#endif

#ifdef PQ_PIP_C_SRAM_HW_WITH_SAME_INTERFACE
//If 4 c_sram with sram read/write interface(HW artitecture)
//We can not write c_sram1 when c_sram3 is reading(using)
//HW suggest load all c_sram when PQ initialize
            MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_C_SRAM1_C121_Main, PQ_IP_C_SRAM1_Main);
            MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_C_SRAM2_C2121_Main, PQ_IP_C_SRAM2_Main);
#endif
            MDrv_PQ_LoadTable_U2(pInstance,PQ_SUB_WINDOW, MHal_PQ_Get_TableIndex(PQ_SUB_WINDOW, E_PQ_TABLE_IP_CSRAM3_C121), MHal_PQ_Get_IPIndex(PQ_SUB_WINDOW, E_PQ_IP_CSRAM3));
            MDrv_PQ_LoadTable_U2(pInstance,PQ_SUB_WINDOW, MHal_PQ_Get_TableIndex(PQ_SUB_WINDOW, E_PQ_TABLE_IP_CSRAM4_C2121), MHal_PQ_Get_IPIndex(PQ_SUB_WINDOW, E_PQ_IP_CSRAM4));

#if (defined(UFO_XC_DS_PQ))
        }
#endif
#endif


#if 1
    // Set BK12 [40] [1:0] = b'11 to enable hw double buffer write
    // at blanking area for changing memory format by L_BK_SCMI(0x02)
    // this register is added after T2 U04
    Hal_PQ_set_memfmt_doublebuffer(pInstance,ENABLE);

    memcpy(&_stPQ_Info, pstPQInitInfo, sizeof(MS_PQ_Init_Info));

    memset(&bw_info, 0x00, sizeof(bw_info));
    bw_info.bDDR2 = pstPQInitInfo->bDDR2;
    bw_info.u32DDRFreq = pstPQInitInfo->u32DDRFreq;
    bw_info.u8BusWidth = pstPQInitInfo->u8BusWidth;
    bw_info.u16output_vtotal = pstPQInitInfo->u16Pnl_vtotal;
    bw_info.u16output_hsize = pstPQInitInfo->u16PnlWidth;
    bw_info.u16OSD_hsize = pstPQInitInfo->u16OSD_hsize;
    bw_info.u32Miu0MemSize = pstPQInitInfo->u32miu0em_size;
    bw_info.u32Miu1MemSize = pstPQInitInfo->u32miu1em_size;

    for(u8Idx = 0; u8Idx < PQ_MAX_WINDOW; u8Idx++)
    {
        _bColorRange0_255[u8Idx] = TRUE;
        _bGameModeOn[u8Idx] = FALSE;
        _u8RWmappingNum[u8Idx] = 0;
        _bRWNumIsReset[u8Idx] = TRUE;
        _u16PQSuggestedFrameNum[u8Idx] = 0;
    }
#endif
#if defined(ANDROID) && defined(UFO_XC_DS_PQ)
    if(stGetDSInfo.bUpdate_DS_CMD)
    {
        //PQ_Function_Info* function_Info = NULL;
        PQ_Function_Info function_Info;
        memset(&function_Info, 0, sizeof(function_Info));
        MS_U32 u32InitDataLen = 0;
        function_Info.pq_deside_srctype = MDrv_PQ_DesideSrcType;
        //function_Info.pq_disable_filmmode = MDrv_PQ_DisableFilmMode;
        function_Info.pq_load_scalingtable = MDrv_PQ_LoadScalingTable;
        //function_Info.pq_set_420upsampling = MDrv_PQ_Set420upsampling;
        //function_Info.pq_set_csc = MDrv_PQ_SetCSC;
        //function_Info.pq_set_memformat = MDrv_PQ_SetMemFormat;
        function_Info.pq_set_modeInfo = MDrv_PQ_Set_ModeInfo;
        function_Info.pq_get_memyuvfmt = MDrv_PQ_Get_MemYUVFmt;
#if(PQ_ENABLE_IOCTL)
        //function_Info.pq_ioctl = MDrv_PQ_IOCTL;
#endif
        MApi_XC_PQ_LoadFunction(&function_Info, u32InitDataLen);
    }
#endif

#if (PQ_XRULE_SPF_DS_ENABLE == 1)
    if(!(MApi_XC_GetChipCaps(E_XC_SUPPORT_SPF_LB_MODE,&u32TmpRet,sizeof(MS_U32))))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GetChipCaps Fail ! \n");
    }
    gbIsSupportSPFLB =  ((u32TmpRet != 0)?TRUE:FALSE);
#endif

    //Create pq List  store current table
    _iPqCurrentTable[PQ_MAIN_WINDOW] = createMySearchList(_Mdrv_PQ_CheckIpIndex);
    _iPqCurrentTable[PQ_SUB_WINDOW] = createMySearchList(_Mdrv_PQ_CheckIpIndex);

    //Not just allocate memory for game ip
    ST_PQ_IPInfo stGRuleIPInfo;
    ST_PQ_IPInfo stUpdatedGRuleIPInfo;
    memset(&stGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));
    stGRuleIPInfo.u16IPIdx= PQ_GRule_GAME_MODE_Main;
    stGRuleIPInfo.u16GruleMaxIPNum = PQ_GRULE_GAME_MODE_IP_NUM_Main;
    stGRuleIPInfo.u16GruleLevelMainNum = PQ_GRULE_GAME_MODE_NUM_Main;
    stGRuleIPInfo.u16GruleLevelNum = PQ_GRULE_GAME_MODE_LVL_NUM_Main;
    MHal_PQ_RemappingGruleIP(pInstance, stGRuleIPInfo, &stUpdatedGRuleIPInfo, TRUE);
    for(u8Idx=0; u8Idx<stUpdatedGRuleIPInfo.u16GruleMaxIPNum; u8Idx++)
    {
        pqIpTable* pstInfo = NULL;
        PQ_MALLOC_MEM(pstInfo, sizeof(pqIpTable))
        if(pstInfo != NULL)
        {
            pstInfo->u16IpIndex = MDrv_PQ_GetGRule_IPIndex_Ex_U2(pInstance, PQ_MAIN_WINDOW, stUpdatedGRuleIPInfo.u16IPIdx, u8Idx, stUpdatedGRuleIPInfo.enPQEngineType);
            pstInfo->u16TableIndex = MDrv_PQBin_GetCurrentTableIndex(pInstance, MDrv_PQBin_GetPanelIdx(pInstance), pstInfo->u16IpIndex);
            pqListInsertDataAtLast(_iPqCurrentTable[PQ_MAIN_WINDOW], pstInfo);
        }
    }
    //init Var
    _gu16HVSPSramWriteCount = HVSP_DISABLE_STAGE;

    //Set Window ID Info to VR
    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version   = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length    = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_TOTAL_WINDOW;
    stPQSetVRInfo.pValue   = &u32TmpRet;
    u32TmpRet = (MS_U32)PQ_MAX_WINDOW;
    stPQSetVRInfo.enWindow = PQ_MAX_WINDOW;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }

    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_WINDOW_ID;
    u32TmpRet = (MS_U32)PQ_MAIN_WINDOW;
    stPQSetVRInfo.enWindow = PQ_MAIN_WINDOW;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }

    u32TmpRet = (MS_U32)PQ_SUB_WINDOW;
    stPQSetVRInfo.enWindow= PQ_SUB_WINDOW;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail ! \n");
    }
}

#if (defined  (ANDROID) && defined (UFO_XC_DS_PQ))
extern void* pu32XCInst;
#endif
void SYMBOL_WEAK MDrv_PQ_Init(MS_PQ_Init_Info *pstPQInitInfo)
{
#if (defined (ANDROID) && defined (UFO_XC_DS_PQ))
    if (pu32XCInst == NULL)
    {
        if(UtopiaOpen(MODULE_XC, &pu32XCInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen XC failed\n");
            return;
        }
    }
    static XC_INITMISC tmp_Init_Misc;
    stXC_GET_MISC_STATUS XCArgs1;
    XCArgs1.pXC_Init_Misc = &tmp_Init_Misc;
    XCArgs1.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_MISC_STATUS, (void*)&XCArgs1) != UTOPIA_STATUS_SUCCESS)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain XC engine fail\n");
        return;
    }

    if(!(tmp_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_IS_ANDROID))
    {
        tmp_Init_Misc.u32MISC_A |= E_XC_INIT_MISC_A_IS_ANDROID;

        stXC_INIT_MISC XCArgs2;
        XCArgs2.pXC_Init_Misc = &tmp_Init_Misc;
        XCArgs2.u32InitMiscDataLen = sizeof(XC_INITMISC);
        XCArgs2.eReturnValue = E_APIXC_RET_FAIL;

        if(UtopiaIoctl(pu32XCInst, E_XC_CMD_INIT_MISC, (void*)&XCArgs2) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain XC engine fail\n");
            return;
        }
    }
#endif
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return ;
        }
        printf("\33[1;33m[MARCOS](%s %d) OPEN FINE\33[m\n", __FUNCTION__, __LINE__);
    }

    stPQ_INIT PQArgs;
    PQArgs.pstPQInitInfo = pstPQInitInfo;

    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_INIT, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return ;
    }
    else
    {
#if (defined  (ANDROID) && defined (UFO_XC_DS_PQ))
        stXC_GET_MISC_STATUS XCArgs3;
        XCArgs3.pXC_Init_Misc = &tmp_Init_Misc;
        XCArgs3.eReturnValue = E_APIXC_RET_FAIL;

        if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_MISC_STATUS, (void*)&XCArgs3) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain XC engine fail\n");
            return;
        }

        if(tmp_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_IS_ANDROID)
        {
            tmp_Init_Misc.u32MISC_A = tmp_Init_Misc.u32MISC_A & (~E_XC_INIT_MISC_A_IS_ANDROID);
            stXC_INIT_MISC XCArgs4;
            XCArgs4.pXC_Init_Misc = &tmp_Init_Misc;
            XCArgs4.u32InitMiscDataLen = sizeof(XC_INITMISC);
            XCArgs4.eReturnValue = E_APIXC_RET_FAIL;

            if(UtopiaIoctl(pu32XCInst, E_XC_CMD_INIT_MISC, (void*)&XCArgs4) != UTOPIA_STATUS_SUCCESS)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain XC engine fail\n");
                return;
            }
        }
#endif
        return ;
    }
}

MS_BOOL MDrv_PQ_GetConfig_U2(void* pInstance, MS_PQ_Init_Info *pstPQInitInfo)
{
#ifdef UFO_XC_PQ_SUPPORT_INITVAR_INTO_SHAREMEMORY
    PQ_INSTANCE_PRIVATE *psPQInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psPQInstPri);
    PQ_RESOURCE_PRIVATE* pPQResourcePrivate = NULL;
    if(_status.bIsInitialized == TRUE)
    {
        MDrv_PQ_Get_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
        UtopiaResourceGetPrivate(g_pPQResource[E_PQ_DEVICE_ID_0],(void**)(&pPQResourcePrivate));

        memcpy(pstPQInitInfo, &pPQResourcePrivate->PQ_InitInfo, sizeof(MS_PQ_Init_Info));

        MDrv_PQ_Release_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);

        return E_PQ_RET_OK;
    }
    else
    {
        return E_PQ_RET_FAIL;
    }
#else
    return E_PQ_RET_FAIL;
#endif
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_GetConfig(MS_PQ_Init_Info *pstPQInitInfo)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
    }

    stPQ_GetConfig PQArgs;
    PQArgs.pstPQInitInfo = pstPQInitInfo;
    PQArgs.eRet = E_PQ_RET_FAIL;

    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GETCONFIG, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"PQ engine GET_CONFIG Ioctl fail\n");
        return E_PQ_RET_FAIL;
    }

    if(E_PQ_RET_OK == PQArgs.eRet)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

MS_BOOL MDrv_PQ_Exit_U2(void* pInstance)
{
    PqListIterator *pIter = NULL;
    pqIpTable *pInfo = NULL;
    pIter = createPqListIterator(_iPqCurrentTable[PQ_MAIN_WINDOW]);
    if(pqListIteratorHasNext(pIter))
    {
        pInfo = (pqIpTable *)pqListIteratorNext(pIter);
#ifdef MSOS_TYPE_LINUX_KERNEL
        vfree(pInfo);
#else
        free(pInfo);
#endif
    }
    freePqListIterator(pIter);
    freePqList(_iPqCurrentTable[PQ_MAIN_WINDOW]);
    freePqList(_iPqCurrentTable[PQ_SUB_WINDOW]);
    return true;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Exit(void)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_EXIT, NULL) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

static const MS_S16 S16DACColorCorrectionMatrix[32] =
{
    0x0400, 0x0000, 0x0000, 0x0000, 0x0400, 0x0000, 0x0000, 0x0000,
    0x0400, -0x02E6, 0x0288, -0x05BB, 0x07A4, -0x062C, 0x06F3, -0x073C,
    -0x0024, 0x01BF, 0x07EF, -0x0116, 0x01EE, 0x052C, -0x03BB, 0x00B1,
    -0x0831, 0x0100, -0x0000, 0x0000, -0x0000, 0x0000, 0x0000, 0x0000,
};

#ifdef MSOS_TYPE_LINUX_KERNEL
static MS_BOOL _PQ_StrResumeSetting(PQ_WIN eWindow)
{
    MS_BOOL bSingnalStable = FALSE;
    MS_BOOL bMute = FALSE;
    MS_U16 u16PQdelay = 0;
    MS_U8 u8Count = 0;

    MDrv_PQ_LoadSettings_U2(pu32PQInst,eWindow);
    MDrv_PQ_LoadUFSCSettings_U2(pu32PQInst,eWindow);


    if((_e3DNRType[eWindow] < PQ_3D_NR_NUM))
    {
        MDrv_PQ_LoadNRTable_U2(pu32PQInst,eWindow,_e3DNRType[eWindow] );
    }

    if((_eMPEGNRType[eWindow] < PQ_MPEG_NR_NUM))
    {
        MDrv_PQ_LoadMPEGNRTable_U2(pu32PQInst,eWindow,_eMPEGNRType[eWindow] );
    }

    if((_ePTPType[eWindow] < PQ_PTP_NUM))
    {
        MDrv_PQ_LoadPTPTable_U2(pu32PQInst,eWindow,_ePTPType[eWindow]);
    }

    if((_eHdrMode[eWindow] < PQ_HDR_NUM))
    {
        MDrv_PQ_Load_HDR_Table_U2(pu32PQInst,eWindow,_eHdrMode[eWindow]);
    }

    if((_eCtrType[eWindow] < PQ_FEATURE_SWITCH_NUM))
    {
        MDrv_PQ_Load_ULTRACLEAR_Table_U2(pu32PQInst,eWindow,_eCtrType[eWindow]);
    }

    if((_eXvyccType[eWindow] < PQ_XVYCC_NUM))
    {
        MDrv_PQ_Load_XVYCC_Table_U2(pu32PQInst,eWindow,_eXvyccType[eWindow]);
    }

    if((_eDynamicContrastType[eWindow] < PQ_DynContr_NUM))
    {
        MDrv_PQ_LoadDynamicContrastTable_U2(pu32PQInst,eWindow,_eDynamicContrastType[eWindow]);
    }

    if((_eFilmModeType[eWindow] < PQ_FilmMode_NUM))
    {
        MDrv_PQ_LoadFilmModeTable_U2(pu32PQInst,eWindow,_eFilmModeType[eWindow]);
    }

    MDrv_PQ_SetGameMode_U2(pu32PQInst,eWindow,_bGameModeOn[eWindow]);

    _MApi_XC_GetStrStatus(eWindow,&bSingnalStable,&bMute);
    u16PQdelay = MDrv_PQ_GetDelayTime_U2(pu32PQInst,(SCALER_WIN)eWindow);
    MsOS_DelayTask(u16PQdelay);

    for(u8Count = 0; u8Count < MAX_WINDOW; u8Count++)
    {
        _MApi_XC_GetStrStatus( u8Count,&bSingnalStable,&bMute);
        if(bSingnalStable == TRUE)
        {
            MApi_XC_GenerateBlackVideo(bMute,(SCALER_WIN)u8Count);
        }
    }
    return TRUE;
}
#endif


#if (PQ_STR_DTV_ENABLE_THREAD ==1)
#ifdef MSOS_TYPE_LINUX_KERNEL
#define PQTHREADWAITTIME  (2000)
static MS_BOOL bPQResumeThreadStart[PQ_MAX_WINDOW] = {FALSE,FALSE};
static MS_BOOL bPQResumeThreadDone[PQ_MAX_WINDOW] = {FALSE,FALSE};

static void _PQ_PowerResume_Task(PQ_WIN eWindow)
{
    if(UTOPIA_STATUS_FAIL == UtopiaStrWaitCondition("pq", E_POWER_RESUME, 0))
    {
        bPQResumeThreadDone[eWindow] = TRUE;
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"PQ PowerState:RESUMEwait Condition failed!!!!\n");
        return;
    }
    _PQ_StrResumeSetting(eWindow);

    bPQResumeThreadDone[eWindow] = TRUE;

}

static MS_BOOL _PQ_PowerResumeThread_Create(PQ_WIN eWindow)
{
    MS_S32 s32TaskId = MsOS_CreateTask(
                (TaskEntry)_PQ_PowerResume_Task,
                (MS_VIRT)eWindow,
                E_TASK_PRI_MEDIUM,
                TRUE,
                NULL,
                32,
                "PQ_Resume");

    if (s32TaskId < 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Pp Task create failed\n");

        return FALSE;
    }

    return TRUE;
}
#endif
#endif//#if (PQ_STR_DTV_ENABLE_THREAD ==1)


MS_U32 MDrv_PQ_SetPowerState_U2(void* pInstance,EN_POWER_MODE enPowerState)
{
    MS_U32 u32Ret = E_PQ_RET_FAIL;
    MS_U8 i = 0;
    MS_U8 u8ID = 0;

    if(_status.bIsInitialized == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"PQ not initial, return.\033[0m\n");
        return E_PQ_RET_FAIL;
    }

    //if customer has own str flow, do nothing
    if(MApi_XC_Get_CusSTRStatus() == TRUE)
    {
        return E_PQ_RET_OK;
    }

    if (enPowerState == E_POWER_SUSPEND)
    {
#ifdef MSOS_TYPE_LINUX_KERNEL
#if (PQ_STR_DTV_ENABLE_THREAD ==1)
        MS_U16 u16ThreadWaitTime = 0;
        while((bPQResumeThreadStart[PQ_MAIN_WINDOW] == TRUE)&&(bPQResumeThreadDone[PQ_MAIN_WINDOW] == FALSE)&&(u16ThreadWaitTime < PQTHREADWAITTIME))
        {
            MsOS_DelayTask(1);
            u16ThreadWaitTime++;
            if(u16ThreadWaitTime == PQTHREADWAITTIME)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"kernel thread wait timeout!\n");
                break;
            }
        }

        bPQResumeThreadStart[PQ_MAIN_WINDOW] = FALSE;
        bPQResumeThreadDone[PQ_MAIN_WINDOW] = FALSE;
#endif//#if (PQ_STR_DTV_ENABLE_THREAD ==1)
#endif
        MDrv_PQ_Str_Reset_Sram_Table(MAIN,pInstance);
        _enPrevPowerState = enPowerState;
        SC_MUTEX_RETURN;        //while STR,PQ get chacne to lock mutex without return it. THerefore, force return before suspend.
        u32Ret = E_PQ_RET_OK;
    }
    else if (enPowerState == E_POWER_RESUME)
    {
        _enPrevPowerState = enPowerState;

        _s32LastPQIPMADiMain = -1;
        _s32LastPQIPMemFormatMain = -1;
        _s32LastPQIPHSDYMain = -1;
        _s32LastPQIPHSDCMain = -1;
        _s32LastPQIPMADiSub = -1;
        _s32LastPQIPMemFormatSub = -1;
        _s32LastPQIPMADiMotMain = -1;
        _s32LastPQIPMADiMotSub = -1;

        _u16LastPQSrcType[PQ_MAIN_WINDOW] = 0xFFFF;
        _u16LastPQSrcType[PQ_SUB_WINDOW] = 0xFFFF;

        _u32PreHSize = 0;
        _u32PreVSize = 0;
        _gu32PreDispHSize = 0;
        _gu32PreDispVSize = 0;

        _gu16HVSPSramWriteCount = HVSP_DISABLE_STAGE;

        if(_stPQ_Info.u8PQBinCnt)
        {
            //[Sub.bin][Sub_Ex.bin]=======================================
            if (_stPQ_Info.u8PQBinCnt > 2)
            {
                u8ID = _stPQ_Info.stPQBinInfo[PQ_BIN_STD_SUB].u8PQID;
                MDrv_PQBin_LoadCommTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance), &stPQBinHeaderInfo[u8ID]);

                u8ID = _stPQ_Info.stPQBinInfo[PQ_BIN_EXT_SUB].u8PQID;
                MDrv_PQBin_LoadCommTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance), &stPQBinHeaderInfo[u8ID]);
            }

            //[Sub_Color.bin]=============================================
            if(_stPQ_Info.u8PQBinCustomerCnt == 2)
            {
                u8ID = _stPQ_Info.stPQBinInfo[PQ_BIN_CUSTOMER_SUB].u8PQID;
                MDrv_PQBin_LoadCommTable(pInstance,PQ_BIN_CUSTOMER_PANEL_INDEX, &stPQBinHeaderInfo[u8ID]);
            }

            //[Main.bin]==================================================
            u8ID = _stPQ_Info.stPQBinInfo[PQ_BIN_STD_MAIN].u8PQID;
            MDrv_PQBin_LoadCommTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance), &stPQBinHeaderInfo[u8ID]);

            //[Main_Ex.bin]===============================================
            u8ID = _stPQ_Info.stPQBinInfo[PQ_BIN_EXT_MAIN].u8PQID;
            MDrv_PQBin_LoadCommTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance), &stPQBinHeaderInfo[u8ID]);

            //[Main_Color.bin]============================================
            if(_stPQ_Info.u8PQBinCustomerCnt >= 1)
            {
                u8ID = _stPQ_Info.stPQBinInfo[PQ_BIN_CUSTOMER_MAIN].u8PQID;
                MDrv_PQBin_LoadCommTable(pInstance,PQ_BIN_CUSTOMER_PANEL_INDEX, &stPQBinHeaderInfo[u8ID]);
            }

            //patch for MI playvideo flow didn't setconnect
            MApi_XC_DisableInputSource(DISABLE, MAIN_WINDOW);

            //[UFSC.bin]==================================================
            if(_stPQ_Info.u8PQBinUFSCCnt == 1)
            {
                u8ID = _stPQ_Info.stPQBinInfo[PQ_BIN_UFSC].u8PQID;
                MDrv_PQBin_LoadCommTable(pInstance, PQ_BIN_UFSC_PANEL_INDEX, &stPQBinHeaderInfo[u8ID]);
            }
#if (ENABLE_PQ_BIN_TMO == 1)
            //[Main_TMO.bin]========================================================
            if(_stPQ_Info.u8PQBinTMOCnt == 1)
            {
                u8ID = _stPQ_Info.stPQBinInfo[PQ_BIN_TMO].u8PQID;
                MDrv_PQBin_LoadCommTable(pInstance, MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO), &stPQBinHeaderInfo[u8ID]);
            }
#endif
        }
        else
        {
#if PQ_ENABLE_PIP
            MDrv_PQ_LoadCommTable_(SUB,pInstance);
#if (ENABLE_PQ_EX)
            MDrv_PQ_LoadCommTable_(SUBEX,pInstance);
#endif
#endif
            MDrv_PQ_LoadCommTable_(MAIN,pInstance);
#if (ENABLE_PQ_EX)
            MDrv_PQ_LoadCommTable_(MAINEX,pInstance);
#endif

            //patch for MI playvideo flow didn't setconnect
            MApi_XC_DisableInputSource(DISABLE, MAIN_WINDOW);
        }

#if PQ_ENABLE_PIP
        //dump sub c_sram3, c_sram4 to avoid garbage when enable pip, add these two into skip rule
        MDrv_PQ_LoadTable_U2(pInstance,PQ_SUB_WINDOW, MHal_PQ_Get_TableIndex(PQ_SUB_WINDOW, E_PQ_TABLE_IP_CSRAM3_C121), MHal_PQ_Get_IPIndex(PQ_SUB_WINDOW, E_PQ_IP_CSRAM3));
        MDrv_PQ_LoadTable_U2(pInstance,PQ_SUB_WINDOW, MHal_PQ_Get_TableIndex(PQ_SUB_WINDOW, E_PQ_TABLE_IP_CSRAM4_C2121), MHal_PQ_Get_IPIndex(PQ_SUB_WINDOW, E_PQ_IP_CSRAM4));
#endif

        // Set BK12 [40] [1:0] = b'11 to enable hw double buffer write
        // at blanking area for changing memory format by L_BK_SCMI(0x02)
        // this register is added after T2 U04
        Hal_PQ_set_memfmt_doublebuffer(pInstance,ENABLE);

        //common bw table
        MDrv_BW_LoadInitTable();
#ifdef MSOS_TYPE_LINUX_KERNEL

#if (PQ_STR_DTV_ENABLE_THREAD ==1)
        bPQResumeThreadStart[PQ_MAIN_WINDOW] = TRUE;
        _PQ_PowerResumeThread_Create(PQ_MAIN_WINDOW);
#else
       if(UTOPIA_STATUS_FAIL == UtopiaStrWaitCondition("pq", E_POWER_RESUME, 0))
       {
           PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"PQ PowerState:RESUME wait Condition failed!!!!\n");
       }
       _PQ_StrResumeSetting(PQ_MAIN_WINDOW);
#endif//#if (PQ_STR_DTV_ENABLE_THREAD ==1)
#endif
        u32Ret = E_PQ_RET_OK;
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Do Nothing: %u\n",enPowerState);
        u32Ret = E_PQ_RET_FAIL;
    }


    return u32Ret;
}

MS_U32 SYMBOL_WEAK MDrv_PQ_SetPowerState(EN_POWER_MODE enPowerState)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SetPowerState PQArgs;
    PQArgs.enPowerState = enPowerState;
    PQArgs.u32Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_POWER_STATE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u32Ret;
    }
}

MS_U16 MDrv_PQ_GetIPNum_U2(void* pInstance,PQ_WIN eWindow)
{
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            return MDrv_PQBin_GetIPNum(pInstance,&stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        }
        else
        {
            return MDrv_PQ_GetIPNum_(MAIN,pInstance);
        }
    }

#if (PQ_ENABLE_PIP)
    if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            return MDrv_PQBin_GetIPNum(pInstance,&stPQBinHeaderInfo[PQ_BIN_STD_SUB]);
        }
        else
        {
            return MDrv_PQ_GetIPNum_(SUB,pInstance);
        }
    }
#endif

    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
        return 0xFFFF;
    }
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetIPNum(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetIPNum PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_IP_NUM, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetTableNum_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16PQIPIdx)
{
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            return MDrv_PQBin_GetTableNum(pInstance,u16PQIPIdx, &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        }
        else
        {
            return MDrv_PQ_GetTableNum_(MAIN,pInstance, u16PQIPIdx);
        }
    }
#if (PQ_ENABLE_PIP)
    if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            return MDrv_PQBin_GetTableNum(pInstance,u16PQIPIdx, &stPQBinHeaderInfo[PQ_BIN_STD_SUB]);
        }
        else
        {
            return MDrv_PQ_GetTableNum_(SUB,pInstance, u16PQIPIdx);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
        return 0xFFFF;
    }
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetTableNum(PQ_WIN eWindow, MS_U16 u16PQIPIdx)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetTableNum PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16PQIPIdx = u16PQIPIdx;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_TABLE_NUM, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetCurrentTableIndex_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16PQIPIdx)
{
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            return MDrv_PQBin_GetCurrentTableIndex(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),  u16PQIPIdx);
        }
        else
        {
            return MDrv_PQ_GetCurrentTableIndex_(MAIN,pInstance, u16PQIPIdx);
        }
    }
#if (PQ_ENABLE_PIP)
    if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            return MDrv_PQBin_GetCurrentTableIndex(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),  u16PQIPIdx);
        }
        else
        {
            return MDrv_PQ_GetCurrentTableIndex_(SUB,pInstance, u16PQIPIdx);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
        return 0xFFFF;
    }
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetCurrentTableIndex(PQ_WIN eWindow, MS_U16 u16PQIPIdx)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetCurrentTableIndex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16PQIPIdx = u16PQIPIdx;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_CURRENT_TABLE_INDEX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}
//Get main page setting from u16IPIdx(column), according to current input source type(row)
MS_U16 MDrv_PQ_GetTableIndex_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16IPIdx)
{
    MS_U16 u16TabIdx = 0;

    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            u16TabIdx = MDrv_PQBin_GetTableIndex(pInstance,_u16PQSrcType[eWindow],
                                                 u16IPIdx,
                                                 MDrv_PQBin_GetPanelIdx(pInstance),
                                                 &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetTableIndex_(MAIN,pInstance, _u16PQSrcType[eWindow], u16IPIdx);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            u16TabIdx = MDrv_PQBin_GetTableIndex(pInstance,_u16PQSrcType[eWindow],
                                                 u16IPIdx,
                                                 MDrv_PQBin_GetPanelIdx(pInstance),
                                                 &stPQBinHeaderInfo[PQ_BIN_STD_SUB]);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetTableIndex_(SUB,pInstance, _u16PQSrcType[eWindow], u16IPIdx);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
    return u16TabIdx;
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetTableIndex(PQ_WIN eWindow, MS_U16 u16IPIdx)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetTableIndex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16PQIPIdx = u16IPIdx;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_TABLE_INDEX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}


//Get main page setting from u16IPIdx(column), according to current input source type(row) by ePQEngineType
MS_U16 MDrv_PQ_GetTableIndex_Ex_U2(void *pInstance,PQ_WIN eWindow, MS_U16 u16IPIdx, PQ_ENGINE_TYPE ePQEngineType)
{
    MS_U16 u16TabIdx = PQ_BIN_IP_NULL, u16SrcIdx = 0;
    MS_U8 u8PanelIdx = 0;
    PQ_BIN_TYPE enPQBinType = MAX_PQ_BIN_NUM;

    if (eWindow == PQ_MAIN_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                u16SrcIdx = _u16PQSrcType[eWindow];
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_STD_MAIN;
                }
                else
                {
                    u16TabIdx = MDrv_PQ_GetTableIndex_(MAIN,pInstance, u16SrcIdx, u16IPIdx);
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    u16SrcIdx = _u16PQSrcType[eWindow];
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_EXT_MAIN;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerMainPQEn)
                {
                    u16SrcIdx = _u16PQSrcType[eWindow];
                    u8PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    enPQBinType = PQ_BIN_CUSTOMER_MAIN;
                }
                break;
            case PQ_XC0_UFSC:
                if (_bUFSCPQEn)
                {
                    u16SrcIdx = _u16PQSrcType_UFSC[eWindow];
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx_UFSC(pInstance);
                    enPQBinType = PQ_BIN_UFSC;
                }
                break;
            default:
                break;
        }
    }
#if (PQ_ENABLE_PIP)
    else if (eWindow == PQ_SUB_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                u16SrcIdx = _u16PQSrcType[eWindow];
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_STD_SUB;
                }
                else
                {
                    u16TabIdx = MDrv_PQ_GetTableIndex_(SUB,pInstance, u16SrcIdx, u16IPIdx);
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    u16SrcIdx = _u16PQSrcType[eWindow];
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_EXT_SUB;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerSubPQEn)
                {
                    u16SrcIdx = _u16PQSrcType[eWindow];
                    u8PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    enPQBinType = PQ_BIN_CUSTOMER_SUB;
                }
                break;
            case PQ_XC0_UFSC:
                break;
            default:
                break;
        }

    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }

    if (enPQBinType != MAX_PQ_BIN_NUM)
    {
        u16TabIdx = MDrv_PQBin_GetTableIndex(pInstance,
                                            u16SrcIdx,
                                            u16IPIdx,
                                            u8PanelIdx,
                                            &stPQBinHeaderInfo[enPQBinType]);
    }

    return u16TabIdx;
}

//Get main page setting from u16IPIdx(column), according to current input source type(row) by ePQEngineType
MS_U16 SYMBOL_WEAK MDrv_PQ_GetTableIndex_Ex(PQ_WIN eWindow, MS_U16 u16IPIdx, PQ_ENGINE_TYPE ePQEngineType)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetTableIndex_Ex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16IPIdx = u16IPIdx;
    PQArgs.ePQEngineType = ePQEngineType;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_TABLE_INDEX_EX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

//Get main page setting from u16IPIdx(column), according to current input source type(row)
MS_U16 MDrv_PQ_GetCustomerTableIndex_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16IPIdx)
{
    MS_U16 u16TabIdx = 0;

    if((eWindow == PQ_MAIN_WINDOW) && _bCustomerMainPQEn)
    {
        if(gbPQBinEnable)
        {
            u16TabIdx = MDrv_PQBin_GetTableIndex(pInstance,_u16PQSrcType[eWindow],
                                                 u16IPIdx,
                                                 PQ_BIN_CUSTOMER_PANEL_INDEX,
                                                 &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN]);
        }
    }
#if (PQ_ENABLE_PIP)
    else if((eWindow == PQ_SUB_WINDOW) && _bCustomerSubPQEn)
    {
        if(gbPQBinEnable)
        {
            u16TabIdx = MDrv_PQBin_GetTableIndex(pInstance,_u16PQSrcType[eWindow],
                                                 u16IPIdx,
                                                 PQ_BIN_CUSTOMER_PANEL_INDEX,
                                                 &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB]);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"CUSTOMER PQ Not support case\n");
    }
    return u16TabIdx;
}

MS_U16 SYMBOL_WEAK MDrv_PQ_GetCustomerTableIndex(PQ_WIN eWindow, MS_U16 u16IPIdx)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetCustomerTableIndex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16PQIPIdx = u16IPIdx;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_CUSTOMER_TABLE_INDEX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

static MS_U16 MDrv_PQ_GetXRuleIPIndex(void* pInstance,PQ_WIN eWindow, MS_U16 u16XRuleType, MS_U16 u16XRuleIP, PQ_ENGINE_TYPE ePQEngineType)
{
    MS_U16 u16IPIdx = 0;
    PQ_BIN_TYPE ePQBinType = PQ_BIN_STD_MAIN;

    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            switch (ePQEngineType)
            {
                case PQ_XC0_STD:
                    ePQBinType = PQ_BIN_STD_MAIN;
                    break;
                case PQ_XC0_EXT:
                    ePQBinType = PQ_BIN_EXT_MAIN;
                    break;
                default:
                    ePQBinType = PQ_BIN_STD_MAIN;
                    break;
            }

            u16IPIdx = MDrv_PQBin_GetXRuleIPIndex(pInstance,u16XRuleType,
                                                  u16XRuleIP,
                                                  &stPQBinHeaderInfo[ePQBinType]);
        }
        else
        {
            u16IPIdx = MDrv_PQ_GetXRuleIPIndex_(MAIN,pInstance, (MS_U8)u16XRuleType, (MS_U8)u16XRuleIP);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            switch (ePQEngineType)
            {
                case PQ_XC0_STD:
                    ePQBinType = PQ_BIN_STD_SUB;
                    break;
                case PQ_XC0_EXT:
                    ePQBinType = PQ_BIN_EXT_SUB;
                    break;
                default:
                    ePQBinType = PQ_BIN_STD_SUB;
                    break;
            }
            u16IPIdx = MDrv_PQBin_GetXRuleIPIndex(pInstance,u16XRuleType,
                                                  u16XRuleIP,
                                                  &stPQBinHeaderInfo[ePQBinType]);
        }
        else
        {
            u16IPIdx = MDrv_PQ_GetXRuleIPIndex_(SUB,pInstance, (MS_U8)u16XRuleType, (MS_U8)u16XRuleIP);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
    return u16IPIdx;
}

static MS_U16 MDrv_PQ_GetXRuleTableIndex(void* pInstance,PQ_WIN eWindow, MS_U16 u16XRuleType, MS_U16 u16XRuleIdx, MS_U16 u16XRuleIP, PQ_ENGINE_TYPE ePQEngineType)
{
    MS_U16 u16TabIdx = 0;
    PQ_BIN_TYPE ePQBinType = PQ_BIN_STD_MAIN;
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            switch (ePQEngineType)
            {
                case PQ_XC0_STD:
                    ePQBinType = PQ_BIN_STD_MAIN;
                    break;
                case PQ_XC0_EXT:
                    ePQBinType = PQ_BIN_EXT_MAIN;
                    break;
                default:
                    ePQBinType = PQ_BIN_STD_MAIN;
                    break;
            }

            u16TabIdx = MDrv_PQBin_GetXRuleTableIndex(pInstance,u16XRuleType,
                        u16XRuleIdx,
                        u16XRuleIP,
                        &stPQBinHeaderInfo[ePQBinType]);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetXRuleTableIndex_(MAIN,pInstance, (MS_U8)u16XRuleType, (MS_U8)u16XRuleIdx, (MS_U8)u16XRuleIP);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            switch (ePQEngineType)
            {
                case PQ_XC0_STD:
                    ePQBinType = PQ_BIN_STD_SUB;
                    break;
                case PQ_XC0_EXT:
                    ePQBinType = PQ_BIN_EXT_SUB;
                    break;
                default:
                    ePQBinType = PQ_BIN_STD_SUB;
                    break;
            }

            u16TabIdx = MDrv_PQBin_GetXRuleTableIndex(pInstance,u16XRuleType,
                        u16XRuleIdx,
                        u16XRuleIP,
                        &stPQBinHeaderInfo[ePQBinType]);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetXRuleTableIndex_(SUB,pInstance, (MS_U8)u16XRuleType, (MS_U8)u16XRuleIdx, (MS_U8)u16XRuleIP);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_XRULE_DBG;
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.pValue= &u16XRuleType;
    stPQSetVRInfo.u16RuleIndex= u16XRuleIdx;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }
    return u16TabIdx;
}

static MS_U16 MDrv_PQ_GetXRuleIPNum(void* pInstance,PQ_WIN eWindow, MS_U16 u16XRuleType)
{
    MS_U16 u16IPNum = 0;
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            u16IPNum = MDrv_PQBin_GetXRuleIPNum(pInstance,u16XRuleType,
                                                &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        }
        else
        {
            u16IPNum = MDrv_PQ_GetXRuleIPNum_(MAIN,pInstance, (MS_U8)u16XRuleType);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            u16IPNum = MDrv_PQBin_GetXRuleIPNum(pInstance,u16XRuleType,
                                                &stPQBinHeaderInfo[PQ_BIN_STD_SUB]);
        }
        else
        {
            u16IPNum = MDrv_PQ_GetXRuleIPNum_(SUB,pInstance, (MS_U8)u16XRuleType);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
    return u16IPNum;
}

void MDrv_PQ_LoadTableData_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, MS_U8 *pTable, MS_U16 u16TableSize)
{
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            MDrv_PQBin_LoadTableData(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),
                                     u16TabIdx,
                                     u16PQIPIdx,
                                     &stPQBinHeaderInfo[PQ_BIN_STD_MAIN],
                                     pTable,
                                     u16TableSize);
        }
        else
        {
            MDrv_PQ_LoadTableData_(MAIN,pInstance, (MS_U8)u16TabIdx, u16PQIPIdx, pTable, u16TableSize);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            MDrv_PQBin_LoadTableData(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),
                                     u16TabIdx,
                                     u16PQIPIdx,
                                     &stPQBinHeaderInfo[PQ_BIN_STD_SUB],
                                     pTable,
                                     u16TableSize);
        }
        else
        {
            MDrv_PQ_LoadTableData_(SUB,pInstance, (MS_U8)u16TabIdx, u16PQIPIdx, pTable, u16TableSize);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
}
void MDrv_PQ_LoadTableData(PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, MS_U8 *pTable, MS_U16 u16TableSize)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_LoadTableData PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16TabIdx = u16TabIdx;
    PQArgs.u16PQIPIdx = u16PQIPIdx;
    PQArgs.pTable = pTable;
    PQArgs.u16TableSize = u16TableSize;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_TABLE_DATA, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_CloneTable_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQMainIPIdx, MS_U16 u16PQSubIPIdx)
{
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            MDrv_PQBin_CloneTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),
                                 u16TabIdx,
                                 u16PQMainIPIdx,
                                 u16PQSubIPIdx,
                                 &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        }
        else
        {
            MDrv_PQ_CloneTable_(MAIN,pInstance, (MS_U8)u16TabIdx, u16PQMainIPIdx, u16PQSubIPIdx);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
}
void SYMBOL_WEAK MDrv_PQ_CloneTable(PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQMainIPIdx, MS_U16 u16PQSubIPIdx)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_CloneTable PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16TabIdx = u16TabIdx;
    PQArgs.u16PQMainIPIdx = u16PQMainIPIdx;
    PQArgs.u16PQSubIPIdx = u16PQSubIPIdx;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_CLONE_TABLE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

MS_BOOL KDrv_PQ_DPU_SetModSettings_TrigerALL(void)
{
    MS_U32 u32SupportABF = 0;

    MApi_XC_GetChipCaps(E_XC_SUPPORT_ABF, &u32SupportABF, sizeof(MS_U32));
    if(u32SupportABF!=0)
    {
#ifdef MSOS_TYPE_LINUX_KERNEL
        ST_KDRV_XC_DPU_SETTING stDpuModSetting;
        memset(&stDpuModSetting, 0, sizeof(ST_KDRV_XC_DPU_SETTING));
        stDpuModSetting.u32Length = sizeof(ST_KDRV_XC_DPU_SETTING);
        stDpuModSetting.u64DpuMod = BIT(E_KDRV_DPU_MOD_ABF_IDX);
        stDpuModSetting.enDpuModSetting = E_KDRV_XC_DPU_MOD_SET_TRIGGER_ALL;
        KDrv_XC_DPU_SetModSettings(&stDpuModSetting);
#else
#if (defined MSOS_TYPE_LINUX)
        if(0 > _s32FdScaler)
        {
            _s32FdScaler = open("/dev/scaler", O_WRONLY | O_NDELAY);
        }

        if (0 > _s32FdScaler)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\nUnable to open /dev/scaler\n");
            return FALSE;
        }
        else
        {
            ST_KDRV_XC_DPU_SETTING stDpuModSetting;
            memset(&stDpuModSetting, 0, sizeof(ST_KDRV_XC_DPU_SETTING));
            stDpuModSetting.u32Length = sizeof(ST_KDRV_XC_DPU_SETTING);
            stDpuModSetting.u64DpuMod = BIT(E_KDRV_DPU_MOD_ABF_IDX);
            stDpuModSetting.enDpuModSetting = E_KDRV_XC_DPU_MOD_SET_TRIGGER_ALL;
            if (ioctl(_s32FdScaler, MDRV_XC_IOC_DPU_SETMODSETTING,&stDpuModSetting))
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDRV_XC_IOC_DPU_SETMODSETTING fail!!!!\n");
                return FALSE;
            }
        }
#endif
#endif
    }

    return TRUE;
}

void MDrv_PQ_LoadTable_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx)
{
    if(eWindow == PQ_MAIN_WINDOW)
    {
        switch (u16PQIPIdx)
        {
            case PQ_IP_MADi_Main:
                if (_s32LastPQIPMADiMain == (MS_S32)u16TabIdx)
                {
                    return;
                }
                _s32LastPQIPMADiMain = u16TabIdx;

                break;
#if (PQ_MADI_DFK == 1)
                case PQ_IP_MADi_Motion_Main:
                if (_s32LastPQIPMADiMotMain == (MS_S32)u16TabIdx)
                {
                    return;
                }
                _s32LastPQIPMADiMotMain = u16TabIdx;

                break;
#endif
            case PQ_IP_MemFormat_Main:
                if (_s32LastPQIPMemFormatMain == (MS_S32)u16TabIdx)
                {
                    return;
                }
                _s32LastPQIPMemFormatMain = u16TabIdx;
                break;
            case PQ_IP_HSD_Y_Main:
                if (_s32LastPQIPHSDYMain == (MS_S32)u16TabIdx)
                {
                    return;
                }
                _s32LastPQIPHSDYMain = u16TabIdx;
                break;
            case PQ_IP_HSD_C_Main:
                if (_s32LastPQIPHSDCMain == (MS_S32)u16TabIdx)
                {
                    return;
                }
                _s32LastPQIPHSDCMain = u16TabIdx;
                break;
            default:
                break;
         }
        if(gbPQBinEnable)
        {
            MDrv_PQBin_LoadTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),
                                 u16TabIdx,
                                 u16PQIPIdx,
                                 &stPQBinHeaderInfo[PQ_BIN_STD_MAIN],eWindow);
        }
        else
        {
            MDrv_PQ_LoadTable_(MAIN,pInstance, (MS_U8)u16TabIdx, u16PQIPIdx);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        MS_BOOL bChanged = FALSE;

        if(u16PQIPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI))
        {
            if(_s32LastPQIPMADiSub != (MS_S32)u16TabIdx)
            {
                bChanged = TRUE;
                _s32LastPQIPMADiSub = (MS_S32)u16TabIdx;
            }
        }
        else if(u16PQIPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI_MOTION))
        {
            if(_s32LastPQIPMADiMotSub != (MS_S32)u16TabIdx)
            {
                bChanged = TRUE;
                _s32LastPQIPMADiMotSub = (MS_S32)u16TabIdx;
            }
        }
        else if(u16PQIPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MEMFORMAT))
        {
            if(_s32LastPQIPMemFormatSub != (MS_S32)u16TabIdx)
            {
                bChanged = TRUE;
                _s32LastPQIPMemFormatSub = (MS_S32)u16TabIdx;
            }
        }
        else
        {
            bChanged = TRUE;
        }

        if(bChanged)
        {
            if(gbPQBinEnable)
            {
                MDrv_PQBin_LoadTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),
                                 u16TabIdx,
                                 u16PQIPIdx,
                                 &stPQBinHeaderInfo[PQ_BIN_STD_SUB],eWindow);
            }
            else
            {
                MDrv_PQ_LoadTable_(SUB,pInstance, (MS_U8)u16TabIdx, u16PQIPIdx);
            }
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
}
void SYMBOL_WEAK MDrv_PQ_LoadTable(PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_LoadTable PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16TabIdx = u16TabIdx;
    PQArgs.u16PQIPIdx = u16PQIPIdx;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_TABLE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_LoadCustomerTable_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx)
{
    if((eWindow == PQ_MAIN_WINDOW) && _bCustomerMainPQEn)
    {
        if(gbPQBinEnable)
        {

            MDrv_PQBin_LoadTable(pInstance,PQ_BIN_CUSTOMER_PANEL_INDEX,
                                 u16TabIdx,
                                 u16PQIPIdx,
                                 &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN],eWindow);
        }
        else
        {
            MDrv_PQ_LoadTable_(MAIN,pInstance, (MS_U8)u16TabIdx, u16PQIPIdx);
        }
    }
#if (PQ_ENABLE_PIP)
    else if((eWindow == PQ_SUB_WINDOW) && _bCustomerSubPQEn)
    {
        if(gbPQBinEnable)
        {

            MDrv_PQBin_LoadTable(pInstance,PQ_BIN_CUSTOMER_PANEL_INDEX,
                                 u16TabIdx,
                                 u16PQIPIdx,
                                 &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB],eWindow);
        }
        else
        {
            MDrv_PQ_LoadTable_(SUB,pInstance, (MS_U8)u16TabIdx, u16PQIPIdx);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"CUSTOMER PQ Not support case\n");
    }
}
void SYMBOL_WEAK MDrv_PQ_LoadCustomerTable(PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_LoadCustomerTable PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16TabIdx = u16TabIdx;
    PQArgs.u16PQIPIdx = u16PQIPIdx;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_CUSTOMER_TABLE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_LoadTable_Ex_U2(void *pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, PQ_ENGINE_TYPE ePQEngineType)
{
    MS_U8 u8PanelIdx = 0;
    MS_U32 u32SupportABF = 0, u32SupportZNR = 0;
    MS_BOOL bChanged = FALSE;
    PQ_BIN_TYPE enPQBinType = MAX_PQ_BIN_NUM;
    ST_PQ_IPInfo stIPInfo;
    ST_PQ_IPInfo stUpdatedIPInfo;
    memset(&stIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedIPInfo, 0, sizeof(ST_PQ_IPInfo));

    MApi_XC_GetChipCaps(E_XC_SUPPORT_ABF, &u32SupportABF, sizeof(MS_U32));
    MApi_XC_GetChipCaps(E_XC_SUPPORT_ZNR, &u32SupportZNR, sizeof(MS_U32));
    if (eWindow == PQ_MAIN_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
            {
                switch (u16PQIPIdx)
                {
                    case PQ_IP_MADi_Main:
                    {
                        if (_s32LastPQIPMADiMain != (MS_S32)u16TabIdx)
                        {
                            _s32LastPQIPMADiMain = u16TabIdx;
                            bChanged = TRUE;
                        }
                        break;
                    }
#if (PQ_MADI_DFK == 1)
                    case PQ_IP_MADi_Motion_Main:
                    {
                        if (_s32LastPQIPMADiMotMain != (MS_S32)u16TabIdx)
                        {
                            _s32LastPQIPMADiMotMain = u16TabIdx;
                            bChanged = TRUE;
                        }
                        break;
                    }
#endif
                    case PQ_IP_MemFormat_Main:
                    {
                        if (_s32LastPQIPMemFormatMain != (MS_S32)u16TabIdx)
                        {
                            _s32LastPQIPMemFormatMain = u16TabIdx;
                            bChanged = TRUE;
                        }
                        break;
                    }
                    case PQ_IP_HSD_Y_Main:
                    {
                        if (_s32LastPQIPHSDYMain != (MS_S32)u16TabIdx)
                        {
                            _s32LastPQIPHSDYMain = u16TabIdx;
                            bChanged = TRUE;
                        }
                        break;
                    }
                    case PQ_IP_HSD_C_Main:
                    {
                        if (_s32LastPQIPHSDCMain != (MS_S32)u16TabIdx)
                        {
                            _s32LastPQIPHSDCMain = u16TabIdx;
                            bChanged = TRUE;
                        }
                        break;
                    }
                    default:
                        bChanged = TRUE;
                        break;
                }

                if(bChanged)
                {
                    if (gbPQBinEnable)
                    {
                        u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                        enPQBinType = PQ_BIN_STD_MAIN;
                    }
                    else
                    {
                        MDrv_PQ_LoadTable_(MAIN, pInstance,(MS_U8)u16TabIdx, u16PQIPIdx);
                    }
                }
                break;
            }
            case PQ_XC0_EXT:
            {
                stIPInfo.u16IPIdx = u16PQIPIdx;
                stIPInfo.enPQEngineType = ePQEngineType;
                MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
                switch (stUpdatedIPInfo.u16IPIdx)
                {
                    case PQ_IP_MADi_Main:
                    {
                        if (_s32LastPQIPMADiMain != (MS_S32)u16TabIdx)
                        {
                            _s32LastPQIPMADiMain = u16TabIdx;
                            bChanged = TRUE;
                        }
                        break;
                    }
#if (PQ_MADI_DFK == 1)
                    case PQ_IP_MADi_Motion_Main:
                    {
                        if (_s32LastPQIPMADiMotMain != (MS_S32)u16TabIdx)
                        {
                            _s32LastPQIPMADiMotMain = u16TabIdx;
                            bChanged = TRUE;
                        }
                        break;
                    }
#endif
                    default:
                    {
                        if(MHal_PQ_IsControlPathSetting(stUpdatedIPInfo.u16IPIdx) && (u32SupportZNR == 0 || u32SupportABF == 0))
                        {
                            bChanged = FALSE;
                        }
                        else
                        {
                            bChanged = TRUE;
                        }
                        break;
                    }
                }

                if(bChanged)
                {
                    if (gbPQBinEnable)
                    {
                        u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                        enPQBinType = PQ_BIN_EXT_MAIN;
                    }
                }
                break;
            }
            case PQ_XC0_CUSTOMER:
            {
                if (_bCustomerMainPQEn)
                {
                    u8PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    enPQBinType = PQ_BIN_CUSTOMER_MAIN;
                }
                bChanged = TRUE;
                break;
            }
            case PQ_XC0_UFSC:
            {
                if (_bUFSCPQEn)
                {
                    u8PanelIdx = PQ_BIN_UFSC_PANEL_INDEX;
                    enPQBinType = PQ_BIN_UFSC;
                }
                bChanged = TRUE;
                break;
            }
            case PQ_XC0_TMO:
            {
                if (_bTMOPQEn)
                {
                    u8PanelIdx = MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO);
                    enPQBinType = PQ_BIN_TMO;
                }
                bChanged = TRUE;
                break;
            }
            default:
            {
                bChanged = TRUE;
                break;
            }
        }
    }
#if (PQ_ENABLE_PIP)
    else if (eWindow == PQ_SUB_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
            {
                if(u16PQIPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI))
                {
                    if(_s32LastPQIPMADiSub != (MS_S32)u16TabIdx)
                    {
                        _s32LastPQIPMADiSub = (MS_S32)u16TabIdx;
                        bChanged = TRUE;
                    }
                }
                else if(u16PQIPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI_MOTION))
                {
                    if(_s32LastPQIPMADiMotSub != (MS_S32)u16TabIdx)
                    {
                        _s32LastPQIPMADiMotSub = (MS_S32)u16TabIdx;
                        bChanged = TRUE;
                    }
                }
                else if(u16PQIPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MEMFORMAT))
                {
                    if(_s32LastPQIPMemFormatSub != (MS_S32)u16TabIdx)
                    {
                        bChanged = TRUE;
                        _s32LastPQIPMemFormatSub = (MS_S32)u16TabIdx;
                    }
                }
                else
                {
                    bChanged = TRUE;
                    PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"IP: %d\n", u16PQIPIdx);
                }

                if(bChanged)
                {
                    if (gbPQBinEnable)
                    {
                        u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                        enPQBinType = PQ_BIN_STD_SUB;
                    }
                    else
                    {
                        MDrv_PQ_LoadTable_(SUB,pInstance, (MS_U8)u16TabIdx, u16PQIPIdx);
                    }
                }
                break;
            }
            case PQ_XC0_EXT:
            {
                stIPInfo.u16IPIdx = u16PQIPIdx;
                stIPInfo.enPQEngineType = ePQEngineType;
                MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);

                if(u16PQIPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI))
                {
                    if(_s32LastPQIPMADiSub != (MS_S32)u16TabIdx)
                    {
                        bChanged = TRUE;
                        _s32LastPQIPMADiSub = (MS_S32)u16TabIdx;
                    }
                }
                else if(u16PQIPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI_MOTION))
                {
                    if(_s32LastPQIPMADiMotSub != (MS_S32)u16TabIdx)
                    {
                        bChanged = TRUE;
                        _s32LastPQIPMADiMotSub = (MS_S32)u16TabIdx;
                    }
                }
                else
                {
                    bChanged = TRUE;
                    PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"IP: %d\n", u16PQIPIdx);
                }

                if(bChanged)
                {
                    if (gbPQBinEnable)
                    {
                        u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                        enPQBinType = PQ_BIN_EXT_SUB;
                    }
                }
                break;
            }
            case PQ_XC0_CUSTOMER:
            {
                if (_bCustomerSubPQEn)
                {
                    u8PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    enPQBinType = PQ_BIN_CUSTOMER_SUB;
                }
                bChanged = TRUE;
                break;
            }
            case PQ_XC0_UFSC:
            {
                bChanged = TRUE;
                break;
            }
            default:
            {
                bChanged = TRUE;
                break;
            }
        }

    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }

    if(bChanged)
    {
        if (enPQBinType != MAX_PQ_BIN_NUM)
        {
            MDrv_PQBin_LoadTable(pInstance,
                                u8PanelIdx,
                                u16TabIdx,
                                u16PQIPIdx,
                                &stPQBinHeaderInfo[enPQBinType],eWindow);
        }
    }

}

void SYMBOL_WEAK MDrv_PQ_LoadTable_Ex(PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, PQ_ENGINE_TYPE ePQEngineType)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_LoadTable_Ex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16TabIdx = u16TabIdx;
    PQArgs.u16PQIPIdx = u16PQIPIdx;
    PQArgs.ePQEngineType = ePQEngineType;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_TABLE_EX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
    }
}

void MDrv_PQ_CheckSettings_U2(void* pInstance,PQ_WIN eWindow)
{
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            //ToDo MAINEX
            MDrv_PQBin_CheckCommTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),
                                      &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);

            MDrv_PQBin_CheckTableBySrcType(pInstance,_u16PQSrcType[eWindow],
                                           PQ_BIN_IP_ALL,
                                           MDrv_PQBin_GetPanelIdx(pInstance),
                                           &stPQBinHeaderInfo[PQ_BIN_STD_MAIN],
                                           eWindow);
        }
        else
        {
#if(ENABLE_PQ_EX)
            MDrv_PQ_CheckCommTable_(MAINEX,pInstance);
            MDrv_PQ_CheckTableBySrcType_(MAINEX,pInstance, _u16PQSrcType[eWindow], PQ_IP_ALL);
#endif
            MDrv_PQ_CheckCommTable_(MAIN,pInstance);
            MDrv_PQ_CheckTableBySrcType_(MAIN,pInstance, _u16PQSrcType[eWindow], PQ_IP_ALL);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            //ToDo SUBEX
            MDrv_PQBin_CheckCommTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),
                                      &stPQBinHeaderInfo[PQ_BIN_STD_SUB]);

            MDrv_PQBin_CheckTableBySrcType(pInstance,_u16PQSrcType[eWindow],
                                           PQ_BIN_IP_ALL,
                                           MDrv_PQBin_GetPanelIdx(pInstance),
                                           &stPQBinHeaderInfo[PQ_BIN_STD_SUB],
                                           eWindow);
        }
        else
        {
        #if(ENABLE_PQ_EX)
            MDrv_PQ_CheckCommTable_(SUBEX,pInstance);
            MDrv_PQ_CheckTableBySrcType_(SUBEX,pInstance, _u16PQSrcType[eWindow], PQ_IP_ALL);
        #endif
            MDrv_PQ_CheckCommTable_(SUB,pInstance);
            MDrv_PQ_CheckTableBySrcType_(SUB,pInstance, _u16PQSrcType[eWindow], PQ_IP_ALL);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
}
void SYMBOL_WEAK MDrv_PQ_CheckSettings(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_CheckSettings PQArgs;
    PQArgs.eWindow = eWindow;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_CHECK_SETTINGS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}


#if(ENABLE_PQ_MLOAD)
void MDrv_PQ_Set_MLoadEn_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bEn)
{
    if(MApi_XC_MLoad_GetStatus() != E_MLOAD_ENABLED)
    {
        bEn = FALSE;
    }

    if(bEn)
    {
        PQ_MLOAD_ENTRY();
    }

    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            MDrv_PQBin_Set_MLoadEn(pInstance,bEn);
        }
        else
        {
            MDrv_PQ_Set_MLoadEn_(MAIN,pInstance, bEn);
        }

    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            MDrv_PQBin_Set_MLoadEn(pInstance,bEn);
        }
        else
        {
            MDrv_PQ_Set_MLoadEn_(SUB,pInstance, bEn);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }


    if(bEn == FALSE)
    {
        PQ_MLOAD_RETURN();
    }
}
void SYMBOL_WEAK MDrv_PQ_Set_MLoadEn(PQ_WIN eWindow, MS_BOOL bEn)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_Set_MLoadEn PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bEn = bEn;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_MLOAD_EN, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif

MS_BOOL MDrv_PQ_SkipDuplicatedSetting_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bSkip)
{
    UNUSED(eWindow);
    _bSkipDuplicateSetting = bSkip;
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_SkipDuplicatedSetting(PQ_WIN eWindow, MS_BOOL bSkip)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SkipDuplicatedSetting PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bSkip = bSkip;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SKIP_DUPLICATED_SETTING, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_GetSkipDuplicatedSettingStatus_U2(void* pInstance,PQ_WIN eWindow)
{
    UNUSED(eWindow);
    return _bSkipDuplicateSetting;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetSkipDuplicatedSettingStatus(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetSkipDuplicatedSettingStatus PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_SKIP_DUPLICATED_SETTING_STATUS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

#if (PQ_FRCM_CBCR_SWAP_BY_SW == 1)
void MDrv_PQ_SetFrcmCbCrSwap_U2(void* pInstance,PQ_WIN eWindow)
{
    // FRCM mirror: BK32_03[12](main), BK32_43[12](sub)
    // FRCM cbcy swap: BK31_19[0](main),BK31_59[0](sub)
    // Bypass CE(compression): BK33_01[0](main),BK33_01[1](sub), 0: ENABLE CE; 1: disable CE
    // --------------------------------------------------------------------------------------------
    // | regs         |  not h_mirror |  h_mirror without compression | h_mirror with compression |
    // | BK32_03[12]  |       0       |              1                |              1            |
    // | BK33_01[0]   |     0 or 1    |              1                |              0            |
    // | BK31_19[0]   |       0       |              1                |              0            |
    // --------------------------------------------------------------------------------------------

    if(eWindow == PQ_MAIN_WINDOW)
    {
        if (MApi_XC_R2BYTEMSK(REG_SC_BK32_03_L, BIT(12)) == 0)
        {
            MApi_XC_W2BYTEMSK(REG_SC_BK31_19_L, 0, BIT(0));
        }
        else
        {
            if (MApi_XC_R2BYTEMSK(REG_SC_BK33_01_L, BIT(0)) != 0)
            {
                MApi_XC_W2BYTEMSK(REG_SC_BK31_19_L, BIT(0), BIT(0));
            }
            else
            {
                MApi_XC_W2BYTEMSK(REG_SC_BK31_19_L, 0, BIT(0));
            }
        }
    }
    else
    {
        if (MApi_XC_R2BYTEMSK(REG_SC_BK32_43_L, BIT(12)) == 0)
        {
            MApi_XC_W2BYTEMSK(REG_SC_BK31_59_L, 0, BIT(0));
        }
        else
        {
            if (MApi_XC_R2BYTEMSK(REG_SC_BK33_01_L, BIT(1)) != 0)
            {
                MApi_XC_W2BYTEMSK(REG_SC_BK31_59_L, BIT(0), BIT(0));
            }
            else
            {
                MApi_XC_W2BYTEMSK(REG_SC_BK31_59_L, 0, BIT(0));
            }
        }
    }
}
void MDrv_PQ_SetFrcmCbCrSwap(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_SetFrcmCbCrSwap PQArgs;
    PQArgs.eWindow = eWindow;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_FRCM_CB_CR_SWAP, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif

MS_BOOL _MDrv_PQ_IsSkipFastDisplay(void* pInstance, PQ_WIN eWindow)
{
    MS_BOOL bRet = FALSE;
    MS_BOOL bIsPointToPoint = MDrv_PQ_Get_PointToPoint(eWindow);

    bRet = bIsPointToPoint;

    return bRet;
}

MS_BOOL _MDrv_PQ_IsSkipLoadSetting(void* pInstance, PQ_WIN eWindow)
{
    MS_BOOL bRet = FALSE;

    MS_BOOL bIsSamePQSrcType = (_u16LastPQSrcType[eWindow] == _u16PQSrcType[eWindow]);
    MS_BOOL bIsSkipDuplicate = _bSkipDuplicateSetting;
    MS_BOOL bIsPointToPoint  = ((_bPrePTPOn[eWindow]==FALSE)||(MDrv_PQ_Get_PointToPoint(eWindow)==TRUE));

    _bPrePTPOn[eWindow] = MDrv_PQ_Get_PointToPoint(eWindow);

    bRet = bIsSamePQSrcType && bIsSkipDuplicate && bIsPointToPoint;

    return bRet;
}

static void _MDrv_PQ_LoadHSYSettings(void* pInstance, PQ_WIN eWindow)
{
#if (ENABLE_PQ_BIN_HSY)
    if(u8PQHSYBinCnt && (pstBinMap != NULL))
    {
        MS_U16 u16BinIdx = 0;
        PQ_LEVEL_PRINTF(PQ_DBG_HSY,"========================HSY LoadSettings=============================\n");
        QM_InputSourceToIndex_HSY(eWindow, _enInputSourceType[eWindow], _u32PQSrcType_HSY);

        for(u16BinIdx = 0; u16BinIdx < stForestMap.stForestHeader.u16MaxBinPerModel; u16BinIdx++)
        {
            if(pstBinMap[u16BinIdx].pu8Cache != NULL)
            {
                if (MDrv_PQBin_SearchRule(pInstance, _u32PQSrcType_HSY, pstBinMap[u16BinIdx].pRuleSection, &pstBinMap[u16BinIdx]))
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_HSY,"\033[1;32m HSY Rule found! BinMap:%x, Label:[%x %x]\033[m\n", u16BinIdx, _u32PQSrcType_HSY[0], _u32PQSrcType_HSY[1]);
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_HSY,"\033[1;32m HSY Rule not found! BinMap:%x, Label:[%x %x]\033[m\n", u16BinIdx, _u32PQSrcType_HSY[0], _u32PQSrcType_HSY[1]);
                }
            }
        }
    }
#endif
}

static void _MDrv_PQ_LoadPQSettings(void* pInstance, PQ_WIN eWindow)
{
    ST_PQ_IPInfo stIPInfo;
    ST_PQ_IPInfo stUpdatedIPInfo;
    memset(&stIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedIPInfo, 0, sizeof(ST_PQ_IPInfo));

    if(_MDrv_PQ_IsSkipLoadSetting(pInstance, eWindow))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"Skip PQ, [PQ timming = %d, eWindow = %d]\n", _u16PQSrcType[eWindow], eWindow);
    }
    else
    {
        _u16LastPQSrcType[eWindow] = _u16PQSrcType[eWindow];

        if(eWindow == PQ_MAIN_WINDOW)
        {
            //load swdriver here before all the others PQ settings.
            //Pls don't add swDriver in to skip rule, otherwise below code cannot set swDriver.
            stIPInfo.u16IPIdx = PQ_IP_SwDriver_Main;
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, MDrv_PQ_GetTableIndex_Ex_U2(pInstance, eWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType), stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);

            if(gbPQBinEnable)
            {
                MDrv_PQBin_LoadTableBySrcType(pInstance,_u16PQSrcType[eWindow],
                                              PQ_BIN_IP_ALL,
                                              MDrv_PQBin_GetPanelIdx(pInstance),
                                              &stPQBinHeaderInfo[PQ_BIN_STD_MAIN],
                                              eWindow);

                MDrv_PQBin_LoadTableBySrcType(pInstance,_u16PQSrcType[eWindow],
                                              PQ_BIN_IP_ALL,
                                              MDrv_PQBin_GetPanelIdx(pInstance),
                                              &stPQBinHeaderInfo[PQ_BIN_EXT_MAIN],
                                              eWindow);
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"MAIN table\n");
                MDrv_PQ_LoadTableBySrcType_(MAIN,pInstance, _u16PQSrcType[eWindow], PQ_IP_ALL);
#if(ENABLE_PQ_EX == 1)
                PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"MAIN EX table\n");
                MDrv_PQ_LoadTableBySrcType_(MAINEX,pInstance, _u16PQSrcType[eWindow], PQ_IP_ALL);
#endif
            }

            if(_gIsMVC4kx1k == TRUE)
            {
#if PQ_LCE_CTL_FOR_MVC4KX1K
                MDrv_PQ_LoadTable_U2(pInstance, PQ_MAIN_WINDOW, PQ_IP_VIP_LCE_OFF_Main, PQ_IP_VIP_LCE_Main);
#endif
#if PQ_DLC_CTL_FOR_MVC4KX1K
                MDrv_PQ_LoadTable_U2(pInstance, PQ_MAIN_WINDOW, PQ_IP_VIP_DLC_OFF_Main, PQ_IP_VIP_DLC_Main);
#endif
            }
        }
#if (PQ_ENABLE_PIP)
        else if(eWindow == PQ_SUB_WINDOW)
        {
            if(gbPQBinEnable)
            {
                MDrv_PQBin_LoadTableBySrcType(pInstance,_u16PQSrcType[eWindow],
                                              PQ_BIN_IP_ALL,
                                              MDrv_PQBin_GetPanelIdx(pInstance),
                                              &stPQBinHeaderInfo[PQ_BIN_STD_SUB],
                                              eWindow);

                MDrv_PQBin_LoadTableBySrcType(pInstance,_u16PQSrcType[eWindow],
                                              PQ_BIN_IP_ALL,
                                              MDrv_PQBin_GetPanelIdx(pInstance),
                                              &stPQBinHeaderInfo[PQ_BIN_EXT_SUB],
                                              eWindow);
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"SUB table\n");
                MDrv_PQ_LoadTableBySrcType_(SUB,pInstance, _u16PQSrcType[eWindow], PQ_IP_ALL);
#if(ENABLE_PQ_EX == 1)
                PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"SUB MAIN EX table\n");
                MDrv_PQ_LoadTableBySrcType_(SUBEX,pInstance, _u16PQSrcType[eWindow], PQ_IP_ALL);
#endif
            }
        }
#endif
        else
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
            PQ_ASSERT(0);
        }
#if (PQ_IOCTL_SET_UC_FEATURE == 0)
        MDrv_PQ_IOCTL((PQ_WIN)eWindow,E_IOCTL_SET_UCFEATURE,NULL,NULL);
#endif

#if(ENABLE_PQ_LOAD_TABLE_INFO)
        if(eWindow == PQ_MAIN_WINDOW)
        {
            MDRV_PQ_PrintLoadTableInfo(PQ_LOAD_TABLE_MAIN);
            MDRV_PQ_PrintLoadTableInfo(PQ_LOAD_TABLE_MAIN_EX);
        }
#if PQ_ENABLE_PIP
        else if(eWindow == PQ_SUB_WINDOW)
        {
            MDRV_PQ_PrintLoadTableInfo(PQ_LOAD_TABLE_SUB);
            MDRV_PQ_PrintLoadTableInfo(PQ_LOAD_TABLE_SUB_EX);
        }
#endif
        else
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
            PQ_ASSERT(0);
        }
#endif
#if (PQ_FRCM_CBCR_SWAP_BY_SW == 1)
        MDrv_PQ_SetFrcmCbCrSwap_U2(pInstance, eWindow);
#endif

#ifdef PQ_DISABLE_BYPASS_MODE2_BY_SW
        if(MApi_XC_R2BYTE(REG_CHIP_REVISION) < 1)//Monaco U02 fix .
        {
            MApi_XC_W2BYTEMSK(REG_SC_BK12_44_L, 0x00, BIT(4));  //Disable bypass mode2, for sub video I mode flicker issue
        }
#endif
    }

    //set SPF Xrule
#if ((PQ_SUPPORT_SETQMAPDATA ==1)&&(PQ_XRULE_SPF_DS_ENABLE == 1))
    if(gbIsSupportSPFLB == TRUE)
    {
        EN_KDRV_XC_SPF_RESOLUTION enXCSPFResolution = E_KDRV_XC_SPF_RESOLUTION_MAX;
        //Get IP OP SPF list
        MS_U16 u16input_hsize =_stMode_Info[eWindow].u16input_hsize;

        //decide resolution
        if(u16input_hsize > PQ_XRULE_SPF_DS_4K2K_H_MIN) // 4k
        {
            enXCSPFResolution = E_KDRV_XC_SPF_RESOLUTION_4K;
        }
        else //below 4k
        {
            enXCSPFResolution = E_KDRV_XC_SPF_RESOLUTION_BELLOW4K;
        }
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"enXCSPFResolution =%d\n",(enXCSPFResolution));

        ST_KDRV_XC_CFD_SPF_XRULE stXCCFDSPFXrule;
        memset(&stXCCFDSPFXrule,0,sizeof(ST_KDRV_XC_CFD_SPF_XRULE));
        stXCCFDSPFXrule.u32Version = ST_KDRV_XC_CFD_XRULE_VERSION;
        stXCCFDSPFXrule.u16Length = sizeof(ST_KDRV_XC_CFD_SPF_XRULE);
        stXCCFDSPFXrule.u16XruleTypeNum = PQ_DS_LineBuffer_NUM_Main;
        stXCCFDSPFXrule.bIsSupportSPF = gbIsSupportSPFLB;
        stXCCFDSPFXrule.enXCSPFResolution = enXCSPFResolution;

        if(!MDrv_PQ_SetQMAPXruleData(pInstance,PQ_MAIN_WINDOW,E_PQ_SPF_XRULE))
        {
            PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"MDrv_PQ_SetQMAPXruleData Fail\n");
        }
        if(!MDrv_PQ_SetSPFXruleSetting(&stXCCFDSPFXrule))
        {
            PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"MDrv_PQ_SetSPFXruleSetting Fail\n");
        }
    }
#endif
}

static void _MDrv_PQ_LoadFastDispSettings(void* pInstance, PQ_WIN eWindow)
{
#if PQ_XRULE_MM_DS_ENABLE
#ifdef PQDS_INFO_VERSION
#if (PQDS_INFO_VERSION >= 1)
    if (_MDrv_PQ_IsSkipFastDisplay(pInstance, eWindow) == FALSE)
    {
        XC_PQ_FAST_DISP_SUPPORTED_CAPS stCaps;
        memset(&stCaps, 0, sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS));
        stCaps.u32Version = ST_XC_PQ_FAST_DISP_VERSION;
        stCaps.u32Length = sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS);
        stCaps.enPQFastDispType = E_PQ_FAST_DISP_ZEROPQ;
        stCaps.bSupported = FALSE;
        MApi_XC_GetChipCaps(E_XC_HW_FAST_DISP, (MS_U32*)&stCaps, stCaps.u32Length);

        if(stCaps.bSupported && (eWindow == PQ_MAIN_WINDOW)) // only main PQ DS XRule
        {
            MS_U8 u8Level = 0xFF;
            MS_U8 u8FBNum = MApi_XC_GetWRBankMappingNum(MDrv_PQ_PQtoSC_WinMapping(eWindow));
            MDrv_PQ_Set_FastDisplay_U2(pInstance, eWindow, TRUE, u8Level, u8FBNum);
        }
    }
#endif
#endif
#endif
}

static MS_BOOL _MDrv_PQ_LoadSPFXruleSetting(void* pInstance,PQ_WIN eWindow)
{
    //Check Input Valid
    if(pInstance == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pInstance == NULL\n");
        return FALSE;
    }
    if(eWindow != PQ_MAIN_WINDOW) //Only main window support SPFXRule
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Invalid input Window=%d \n", eWindow);
        return FALSE;
    }

    ST_PQ_DS_XRULE_INFO stXCMMDSXruleInfo;
    XC_CFD_STATUS stCfdStatus;
    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stXCMMDSXruleInfo, 0, sizeof(ST_PQ_DS_XRULE_INFO));
    memset(&stCfdStatus, 0, sizeof(XC_CFD_STATUS));
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));

    stCfdStatus.u32Version = CFD_STATUS_VERSION;
    stCfdStatus.u16Length = sizeof(XC_CFD_STATUS);
    stCfdStatus.u8Win = eWindow;

    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_TYPE_STATUS;
    stCfdCtrlInfo.pParam = &stCfdStatus;
    stCfdCtrlInfo.u32ParamLen = sizeof(XC_CFD_STATUS);
    MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrlInfo);

    if(stCfdStatus.u8VideoHdrType == E_XC_CFD_HDR_TYPE_DOLBY)
    {
        stXCMMDSXruleInfo.enHDRType = E_XC_HDR_TYPE_DOLBY;
    }
    else if(stCfdStatus.u8VideoHdrType == E_XC_CFD_HDR_TYPE_NONE)
    {
        stXCMMDSXruleInfo.enHDRType = E_XC_HDR_TYPE_NONE;
    }
    else if(stCfdStatus.u8VideoHdrType == E_XC_CFD_HDR_TYPE_OPEN)
    {
        stXCMMDSXruleInfo.enHDRType = E_XC_HDR_TYPE_OPEN;
    }
    else if(stCfdStatus.u8VideoHdrType == E_XC_CFD_HDR_TYPE_TCH)
    {
        stXCMMDSXruleInfo.enHDRType = E_XC_HDR_TYPE_TECHNICOLOR;
    }
    else if(stCfdStatus.u8VideoHdrType == E_XC_CFD_HDR_TYPE_HLG)
    {
        stXCMMDSXruleInfo.enHDRType = E_XC_HDR_TYPE_HLG;
    }
    else if(stCfdStatus.u8VideoHdrType == E_XC_CFD_HDR_TYPE_HDR10_PLUS)
    {
        stXCMMDSXruleInfo.enHDRType = E_XC_HDR_TYPE_HDR10_PLUS;
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Not support HDR type, u8VideoHdrType=%u \n", stCfdStatus.u8VideoHdrType);
    }

    stXCMMDSXruleInfo.enWindow = eWindow;
    stXCMMDSXruleInfo.u32H_Size = QM_GetInputHSize(eWindow);
    stXCMMDSXruleInfo.u32V_Size = QM_GetInputVSize(eWindow);
    stXCMMDSXruleInfo.enLoadXruleTableMethod = E_PQ_LOADXRULETABLE_GENERAL;

    PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"V_Size=%tu, H_Size=%tu, enHDRType = %d\n", (ptrdiff_t)stXCMMDSXruleInfo.u32V_Size,
        (ptrdiff_t)stXCMMDSXruleInfo.u32H_Size, stXCMMDSXruleInfo.enHDRType);

    if(!_MDrv_PQ_LoadSPFDSXruleTable(pInstance, &stXCMMDSXruleInfo))
    {
         PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"LoadSPFDSXruleTable Fail\n");
    }
    return TRUE;
}

void MDrv_PQ_LoadSettings_U2(void* pInstance,PQ_WIN eWindow)
{
    PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"eWindow[%u] _bLoadSetting %u\n",eWindow,_bLoadSetting);
    if(_bLoadSetting)
    {
        _bLoadSetting = FALSE;

        //Load HSY Setting
        _MDrv_PQ_LoadHSYSettings(pInstance, eWindow);

        //Load PQ Setting
        _MDrv_PQ_LoadPQSettings(pInstance, eWindow);

        //Load Fast Display Setting
        _MDrv_PQ_LoadFastDispSettings(pInstance, eWindow);

        //Load SPF XRule Setting
        if(_MDrv_PQ_LoadSPFXruleSetting(pInstance, eWindow) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"_MDrv_PQ_LoadSPFXruleSetting fail.\n");
        }

        //Set PQ Info to VR
        ST_PQ_SET_VR_INFO stPQSetVRInfo;
        memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
        stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
        stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
        stPQSetVRInfo.enWindow= eWindow;
        MS_U32 u32ScalingParam = 0;
        stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_PRE_SCALING_HV;
        stPQSetVRInfo.pValue= &u32ScalingParam;
        if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
        }
        stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_POST_SCALING_HV;
        if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
        }
    }
}

void SYMBOL_WEAK MDrv_PQ_LoadSettings(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_LoadSettings PQArgs;
    PQArgs.eWindow = eWindow;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_SETTINGS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_LoadCustomerSettings_U2(void* pInstance,PQ_WIN eWindow)
{
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {

            MDrv_PQBin_LoadTableBySrcType(pInstance,_u16PQSrcType[eWindow],
                                          PQ_BIN_IP_ALL,
                                          PQ_BIN_CUSTOMER_PANEL_INDEX,
                                          &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN],
                                          eWindow);

        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            MDrv_PQBin_LoadTableBySrcType(pInstance,_u16PQSrcType[eWindow],
                                          PQ_BIN_IP_ALL,
                                          PQ_BIN_CUSTOMER_PANEL_INDEX,
                                          &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB],
                                          eWindow);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
}
void SYMBOL_WEAK MDrv_PQ_LoadCustomerSettings(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_LoadCustomerSettings PQArgs;
    PQArgs.eWindow = eWindow;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_CUSTONER_SETTINGS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_Set_DTVInfo_U2(void* pInstance,PQ_WIN eWindow, MS_PQ_Dtv_Info *pstPQDTVInfo)
{
    memcpy(&_stDTV_Info[eWindow], pstPQDTVInfo, sizeof(MS_PQ_Dtv_Info));

    PQ_LEVEL_PRINTF(PQ_DBG_INFO, "PQ DTV Info:Win=%u, type=%u\n", eWindow, _stDTV_Info[eWindow].eType);
}
void SYMBOL_WEAK MDrv_PQ_Set_DTVInfo(PQ_WIN eWindow, MS_PQ_Dtv_Info *pstPQDTVInfo)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_Set_DTVInfo PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.pstPQDTVInfo = pstPQDTVInfo;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_DTVINFO, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_Set_MultiMediaInfo_U2(void* UNUSED_PARA(pInstance),PQ_WIN eWindow, MS_PQ_MuliMedia_Info *pstPQMMInfo)
{
    if(pstPQMMInfo != NULL)
    {
        if(eWindow < PQ_MAX_WINDOW)
        {
            memcpy(&_stMultiMedia_Info[eWindow], pstPQMMInfo, sizeof(MS_PQ_MuliMedia_Info));
            PQ_LEVEL_PRINTF(PQ_DBG_INFO, "PQ MM Info:Win=%u, type=%u\n", eWindow, _stMultiMedia_Info[eWindow].eType);
        }
        else
        {
            PQ_LEVEL_PRINTF(PQ_DBG_INFO, "eWndow overflow %u\n", eWindow);
        }
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pstPQMMInfo == NULL\n");
    }
}
void SYMBOL_WEAK MDrv_PQ_Set_MultiMediaInfo(PQ_WIN eWindow, MS_PQ_MuliMedia_Info *pstPQMMInfo)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_Set_MultiMediaInfo PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.pstPQMMInfo = pstPQMMInfo;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_MULTIMEDIAINFO, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_Set_VDInfo_U2(void* pInstance,PQ_WIN eWindow, MS_PQ_Vd_Info *pstPQVDInfo)
{
    memcpy(&_stVD_Info[eWindow], pstPQVDInfo, sizeof(MS_PQ_Vd_Info));

    PQ_LEVEL_PRINTF(PQ_DBG_INFO, "PQ VD Info:Win=%u, SigType=%u, bSCARTRGB=%u, VIFIn=%u\n",
                      eWindow,
                      _stVD_Info[eWindow].enVideoStandard,
                      _stVD_Info[eWindow].bIsSCART_RGB,
                      _stVD_Info[eWindow].bIsVIFIN);
}
void SYMBOL_WEAK MDrv_PQ_Set_VDInfo(PQ_WIN eWindow, MS_PQ_Vd_Info *pstPQVDInfo)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_Set_VDInfo PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.pstPQVDInfo = pstPQVDInfo;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_VDINFO, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_Set_ModeInfo_U2(void* pInstance,PQ_WIN eWindow, PQ_INPUT_SOURCE_TYPE enInputSourceType, MS_PQ_Mode_Info *pstPQModeInfo)
{

    memcpy(&_stMode_Info[eWindow], pstPQModeInfo, sizeof(MS_PQ_Mode_Info));

    //printf("PQ Set Mode Info: %s, Src=%x\n", (eWindow)?("SubWin"):("MainWin"), enInputSourceType);

    if(QM_IsSourceYPbPr(enInputSourceType))
    {
        if(((QM_H_Size_Check_x1(pstPQModeInfo->u16input_hsize, 720)) ||
                (QM_H_Size_Check_x2(pstPQModeInfo->u16input_hsize, 720)) ||
                (QM_H_Size_Check_x4(pstPQModeInfo->u16input_hsize, 720)) ||
                (QM_H_Size_Check_x8(pstPQModeInfo->u16input_hsize, 720))) &&
                (pstPQModeInfo->u16input_vsize < 500) &&
                (pstPQModeInfo->u16input_vfreq < 650))
        {
            _u8ModeIndex[eWindow] = (pstPQModeInfo->bInterlace) ?
                                    PQ_MD_720x480_60I :
                                    PQ_MD_720x480_60P;

        }
        else if(((QM_H_Size_Check_x1(pstPQModeInfo->u16input_hsize, 720)) ||
                 (QM_H_Size_Check_x2(pstPQModeInfo->u16input_hsize, 720)) ||
                 (QM_H_Size_Check_x4(pstPQModeInfo->u16input_hsize, 720)) ||
                 (QM_H_Size_Check_x8(pstPQModeInfo->u16input_hsize, 720))) &&
                (pstPQModeInfo->u16input_vsize < 600) &&
                (pstPQModeInfo->u16input_vfreq < 550))
        {
            _u8ModeIndex[eWindow] = (pstPQModeInfo->bInterlace) ?
                                    PQ_MD_720x576_50I :
                                    PQ_MD_720x576_50P;
        }
        else if(((QM_H_Size_Check_x1(pstPQModeInfo->u16input_hsize, 1280)) ||
                 (QM_H_Size_Check_x2(pstPQModeInfo->u16input_hsize, 1280)) ||
                 (QM_H_Size_Check_x4(pstPQModeInfo->u16input_hsize, 1280)) ||
                 (QM_H_Size_Check_x8(pstPQModeInfo->u16input_hsize, 1280))) &&
                (pstPQModeInfo->u16input_vsize < 800) &&
                (pstPQModeInfo->bInterlace == FALSE))
        {
            _u8ModeIndex[eWindow] = (pstPQModeInfo->u16input_vfreq < 550) ?
                                    PQ_MD_1280x720_50P :
                                    PQ_MD_1280x720_60P;

        }
        else if(((QM_H_Size_Check_x1(pstPQModeInfo->u16input_hsize, 1920)) ||
                 (QM_H_Size_Check_x2(pstPQModeInfo->u16input_hsize, 1920)) ||
                 (QM_H_Size_Check_x4(pstPQModeInfo->u16input_hsize, 1920)) ||
                 (QM_H_Size_Check_x8(pstPQModeInfo->u16input_hsize, 1920))) &&
                (pstPQModeInfo->u16input_vsize < 1100))
        {
            if(pstPQModeInfo->bInterlace)
            {
                _u8ModeIndex[eWindow] = (pstPQModeInfo->u16input_vfreq < 550) ?
                                        PQ_MD_1920x1080_50I :
                                        PQ_MD_1920x1080_60I;
            }
            else
            {
                _u8ModeIndex[eWindow] = (pstPQModeInfo->u16input_vfreq < 245) ?  PQ_MD_1920x1080_24P :
                                        (pstPQModeInfo->u16input_vfreq < 270) ?  PQ_MD_1920x1080_25P :
                                        (pstPQModeInfo->u16input_vfreq < 350) ?  PQ_MD_1920x1080_30P :
                                        (pstPQModeInfo->u16input_vfreq < 550) ?  PQ_MD_1920x1080_50P :
                                        PQ_MD_1920x1080_60P;

            }
        }
        else
        {
            _u8ModeIndex[eWindow] = PQ_MD_720x576_50I;
        }
    }
    else if(QM_IsSourceHDMI(enInputSourceType))
    {
        if((pstPQModeInfo->u16input_hsize < 1500) &&
                (pstPQModeInfo->u16input_vsize < 500) &&
                (pstPQModeInfo->u16input_vfreq < 650))
        {
            _u8ModeIndex[eWindow] = (pstPQModeInfo->bInterlace) ?
                                    PQ_MD_720x480_60I :
                                    PQ_MD_720x480_60P;

        }
        else if((pstPQModeInfo->u16input_hsize < 1500) &&
                (pstPQModeInfo->u16input_vsize < 600) &&
                (pstPQModeInfo->u16input_vfreq <  550))
        {
            _u8ModeIndex[eWindow] = (pstPQModeInfo->bInterlace) ?
                                    PQ_MD_720x576_50I :
                                    PQ_MD_720x576_50P;
        }
        else if((pstPQModeInfo->u16input_hsize < 1300) &&
                (pstPQModeInfo->u16input_vsize < 800) &&
                (pstPQModeInfo->bInterlace == FALSE))
        {
            _u8ModeIndex[eWindow] = (pstPQModeInfo->u16input_vfreq < 550) ?
                                    PQ_MD_1280x720_50P :
                                    PQ_MD_1280x720_60P;

        }
        else if((pstPQModeInfo->u16input_hsize < 1930) &&
                (pstPQModeInfo->u16input_vsize < 1100))
        {
            if(pstPQModeInfo->bInterlace)
            {
                _u8ModeIndex[eWindow] = (pstPQModeInfo->u16input_vfreq < 550) ?
                                        PQ_MD_1920x1080_50I :
                                        PQ_MD_1920x1080_60I;
            }
            else
            {
                _u8ModeIndex[eWindow] = (pstPQModeInfo->u16input_vfreq < 245) ?  PQ_MD_1920x1080_24P :
                                        (pstPQModeInfo->u16input_vfreq < 270) ?  PQ_MD_1920x1080_25P :
                                        (pstPQModeInfo->u16input_vfreq < 350) ?  PQ_MD_1920x1080_30P :
                                        (pstPQModeInfo->u16input_vfreq < 550) ?  PQ_MD_1920x1080_50P :
                                        PQ_MD_1920x1080_60P;

            }
        }
        else if(pstPQModeInfo->u16input_hsize < 1350 &&
                pstPQModeInfo->u16input_vsize < 1550)
        {
            //special handle 1280X1470p
            {
                _u8ModeIndex[eWindow] = (pstPQModeInfo->u16input_vfreq < 550) ?  PQ_MD_1280x720_50P :
                               PQ_MD_1280x720_60P;
            }
        }
        else if(pstPQModeInfo->u16input_hsize < 1930 &&
                pstPQModeInfo->u16input_vsize < 2300)
        {
            //special handle 1920X2205p
            if(pstPQModeInfo->bInterlace)
            {
                _u8ModeIndex[eWindow] = (pstPQModeInfo->u16input_vfreq < 550) ?
                               PQ_MD_1920x1080_50I :
                               PQ_MD_1920x1080_60I;
            }
            else
            {
                _u8ModeIndex[eWindow] = (pstPQModeInfo->u16input_vfreq < 250) ?  PQ_MD_1920x1080_24P :
                               (pstPQModeInfo->u16input_vfreq < 350) ?  PQ_MD_1920x1080_30P :
                               (pstPQModeInfo->u16input_vfreq < 550) ?  PQ_MD_1920x1080_50P :
                               PQ_MD_1920x1080_60P;

            }
        }
        else
        {
            _u8ModeIndex[eWindow] = PQ_MD_1920x1080_60P;
        }
    }
    else
    {
        _u8ModeIndex[eWindow] = PQ_MD_Num;
    }

    if(QM_IsSourceYPbPr(enInputSourceType) || QM_IsSourceHDMI(enInputSourceType))
    {
        if(_u8ModeIndex[eWindow] <= (MS_U8)PQ_MD_720x576_50P)
        _gIsSrcHDMode[eWindow] = 0;
        else
            _gIsSrcHDMode[eWindow] = 1;

    }
    else if(QM_IsSourceVD(enInputSourceType))
    {
            _gIsSrcHDMode[eWindow] = 0;
    }
    else if((IsSourceMultiMediaMovieMode(eWindow,enInputSourceType)||
            IsSourceMultiMediaStreamingMode(eWindow,enInputSourceType))&&
            MApi_XC_GetDynamicScalingStatus())
    {
        //MM DS case, capture window is VB size, crop window is real size.
        _gIsSrcHDMode[eWindow] = (pstPQModeInfo->u16cropwin_hsize >= 1200) ? 1 : 0;
    }
    else
    {
        if(pstPQModeInfo->u16input_hsize >= 1200)
            _gIsSrcHDMode[eWindow] = 1;
        else
            _gIsSrcHDMode[eWindow] = 0;
            }

    //Set Info to VR
    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_SCANNING;
    stPQSetVRInfo.pValue= &pstPQModeInfo->bInterlace;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }

    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_FRAME_RATE;
    stPQSetVRInfo.pValue= &pstPQModeInfo->u16input_vfreq;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }

    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_RESOLUTION;
    MS_U32 u32Resolution = (pstPQModeInfo->u16input_hsize<<16 | pstPQModeInfo->u16input_vsize);
    stPQSetVRInfo.pValue= &u32Resolution;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }

    PQ_LEVEL_PRINTF(PQ_DBG_INFO, "PQ ModeInfo:%d input(%d, %d), disp(%d, %d), crop(%d, %d), ModeIdx=%d, FBL=%u, Interlace=%u, InV=%u, OutV=%u, inVtt=%u\n",
                      eWindow,
                      _stMode_Info[eWindow].u16input_hsize,
                      _stMode_Info[eWindow].u16input_vsize,
                      _stMode_Info[eWindow].u16display_hsize,
                      _stMode_Info[eWindow].u16display_vsize,
                      _stMode_Info[eWindow].u16cropwin_hsize,
                      _stMode_Info[eWindow].u16cropwin_vsize,
                      _u8ModeIndex[eWindow],
                      _stMode_Info[eWindow].bFBL,
                      _stMode_Info[eWindow].bInterlace,
                      _stMode_Info[eWindow].u16input_vfreq,
                      _stMode_Info[eWindow].u16ouput_vfreq,
                      _stMode_Info[eWindow].u16input_vtotal);

}
void SYMBOL_WEAK MDrv_PQ_Set_ModeInfo(PQ_WIN eWindow, PQ_INPUT_SOURCE_TYPE enInputSourceType, MS_PQ_Mode_Info *pstPQModeInfo)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return;
        }
    }

    stPQ_Set_ModeInfo PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.enInputSourceType = enInputSourceType;
    PQArgs.pstPQModeInfo = pstPQModeInfo;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_MODEINFO, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_SetHDMIInfo_U2(void* pInstance,PQ_WIN eWindow, const MS_PQ_Hdmi_Info* const pstPQHDMIInfo)
{
    if (eWindow < PQ_MAX_WINDOW)
    {
        _stHDMI_Info[eWindow].bIsHDMI = pstPQHDMIInfo->bIsHDMI;
        _stHDMI_Info[eWindow].enColorFmt = pstPQHDMIInfo->enColorFmt;

        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "PQ HDMI, bHDMI=%u, colorfmt=%u\n",
                                       _stHDMI_Info[eWindow].bIsHDMI,
                                       _stHDMI_Info[eWindow].enColorFmt);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Invalid input: eWindow(%d) >= PQ_MAX_WINDOW(%d)\n", eWindow, PQ_MAX_WINDOW);
    }
}
void SYMBOL_WEAK MDrv_PQ_SetHDMIInfo(PQ_WIN eWindow, const MS_PQ_Hdmi_Info* const pstPQHDMIInfo)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_SetHDMIInfo PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.pstPQHDMIInfo.bIsHDMI = pstPQHDMIInfo->bIsHDMI;
    PQArgs.pstPQHDMIInfo.bIsHDMIPC = pstPQHDMIInfo->bIsHDMIPC;
    PQArgs.pstPQHDMIInfo.enColorFmt = pstPQHDMIInfo->enColorFmt;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_HDMIINFO, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_SetHDMI_PC_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bIsTrue)
{
    _stHDMI_Info[eWindow].bIsHDMIPC = bIsTrue;
}
void SYMBOL_WEAK MDrv_PQ_SetHDMI_PC(PQ_WIN eWindow, MS_BOOL bIsTrue)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_SetHDMI_PC PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bIsTrue = bIsTrue;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_HDMI_PC, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

MS_BOOL MDrv_PQ_GetHDMI_PC_Status_U2(void* pInstance,PQ_WIN eWindow)
{
    return _stHDMI_Info[eWindow].bIsHDMIPC;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetHDMI_PC_Status(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetHDMI_PC_Status PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_HDMI_PC_STATUS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

void MDrv_PQ_DesideSrcType_U2(void* pInstance,PQ_WIN eWindow, PQ_INPUT_SOURCE_TYPE enInputSourceType)
{
    if(_enInputSourceType[eWindow] != enInputSourceType)
    {
        if(eWindow == PQ_SUB_WINDOW)
        {
            _s32LastPQIPMADiSub = -1;
            _s32LastPQIPMemFormatSub = -1;
            _s32LastPQIPMADiMotSub = -1;
        }
        else
        {
            _s32LastPQIPMADiMain = -1;
            _s32LastPQIPMemFormatMain = -1;
            _s32LastPQIPMADiMotMain = -1;
        }
    }
    _enInputSourceType[eWindow] = enInputSourceType;
    _u16PQSrcType[eWindow] = QM_InputSourceToIndex(pInstance, eWindow, enInputSourceType);

    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_SOURCE_TYPE;
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.pValue= &_enInputSourceType[eWindow];
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }

    _u16PQSrcType_DBK_Detect[eWindow] = _u16PQSrcType[eWindow];                     //For Auto_DBK SW driver used
    PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"[PQ_DesideSrcType] window=%u, enInputSrcType=%u, SrcType=%u\n", eWindow, enInputSourceType, _u16PQSrcType[eWindow]);

#if(ENABLE_PQ_LOAD_TABLE_INFO)

    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {

        }
        else
        {
            MDrv_PQ_Set_LoadTableInfo_SrcType(MAIN,pInstance, _u16PQSrcType[eWindow]);
            MDrv_PQ_Set_LoadTableInfo_SrcType(MAINEX,pInstance, _u16PQSrcType[eWindow]);
        }
    }
#if PQ_ENABLE_PIP
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {

        }
        else
        {
            MDrv_PQ_Set_LoadTableInfo_SrcType(SUB,pInstance, _u16PQSrcType[eWindow]);
            MDrv_PQ_Set_LoadTableInfo_SrcType(SUBEX,pInstance, _u16PQSrcType[eWindow]);
        }
    }
#endif

#endif //#if(ENABLE_PQ_LOAD_TABLE_INFO)

    //if loadsetting, must do once DesideSrcType
    _bLoadSetting = TRUE;
}
void SYMBOL_WEAK MDrv_PQ_DesideSrcType(PQ_WIN eWindow, PQ_INPUT_SOURCE_TYPE enInputSourceType)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return;
        }
    }

    stPQ_DesideSrcType PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.enInputSourceType = enInputSourceType;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_DESIDE_SRC_TYPE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

PQ_INPUT_SOURCE_TYPE MDrv_PQ_GetInputSourceType_U2(void* pInstance,PQ_WIN eWindow)
{
    return _enInputSourceType[eWindow];
}
PQ_INPUT_SOURCE_TYPE SYMBOL_WEAK MDrv_PQ_GetInputSourceType(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetInputSourceType PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = PQ_INPUT_SOURCE_NONE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_INPUTSOURCE_TYPE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return PQ_INPUT_SOURCE_NONE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_U16 MDrv_PQ_GetSrcType_U2(void* pInstance,PQ_WIN eWindow)
{
    return _u16PQSrcType[eWindow];
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetSrcType(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetSrcType PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_SRC_TYPE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}


MS_U16 MDrv_PQ_GetDelayTimeByMemc_U2(void* pInstance, SCALER_WIN eWindow, MS_U32 u32MemcFrameDelay)
{
    MS_U8 u8FrameNum=0;
    MS_U8 u8WRBankMappingNum=0;
    MS_U8 u8PQDelayCountNum=0;
    u8FrameNum = MApi_XC_Get_FrameNumFactor(eWindow);
    u8WRBankMappingNum = MApi_XC_GetWRBankMappingNum(eWindow);


    if(u8FrameNum > (u8PQDelayCount +u8WRBankMappingNum ))
    {
        u8PQDelayCountNum = u8PQDelayCount +u8WRBankMappingNum;
    }
    else
    {
        u8PQDelayCountNum = u8FrameNum;
    }

#if PQ_XRULE_MM_DS_ENABLE
#ifdef PQDS_INFO_VERSION
#if (PQDS_INFO_VERSION >= 1)
    if (_MDrv_PQ_IsSkipFastDisplay(pInstance, MDrv_PQ_SCtoPQ_WinMapping(eWindow)) == FALSE)
    {
        XC_PQ_FAST_DISP_SUPPORTED_CAPS stCaps;
        memset(&stCaps, 0, sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS));
        stCaps.u32Version = ST_XC_PQ_FAST_DISP_VERSION;
        stCaps.u32Length = sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS);
        stCaps.enPQFastDispType = E_PQ_FAST_DISP_ZEROPQ;
        stCaps.bSupported = FALSE;
        MApi_XC_GetChipCaps(E_XC_HW_FAST_DISP, (MS_U32*)&stCaps, stCaps.u32Length);

        if(stCaps.bSupported && (eWindow == MAIN_WINDOW)) // only main PQ DS XRule
        {
            u8PQDelayCountNum = MM_DS_DELAY_COUNT;
        }
    }
#endif
#endif
#endif

    XC_INITMISC sXC_Init_Misc;
    memset(&sXC_Init_Misc, 0, sizeof(XC_INITMISC));
    MApi_XC_GetMISCStatus(&sXC_Init_Misc);

    if ((QM_GetInputVFreq(eWindow) != 0) && (QM_GetInputVFreq(eWindow) != 1))
    {

        if ( sXC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE)
        {
            _u16PQDelyaTime = (u8PQDelayCountNum + u32MemcFrameDelay) * ((10000 + (QM_GetInputVFreq(eWindow)/2))/QM_GetInputVFreq(eWindow));  // frame duration is (1 second/input V freq)

        }
        else
        {
            _u16PQDelyaTime = u8PQDelayCountNum * ((10000 + (QM_GetInputVFreq(eWindow)/2))/QM_GetInputVFreq(eWindow));  // frame duration is (1 second/input V freq)
        }
    }
    else
    {
        _u16PQDelyaTime = u8PQDelayCountNum * 20; // default keep original rule, use 20ms as frame duration
    }
    return _u16PQDelyaTime;
}

MS_U16 SYMBOL_WEAK MDrv_PQ_GetDelayTimeByMemc(SCALER_WIN eWindow, MS_U32 u32MemcFrameDelay)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetDelayTimeByMemc PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u32MemcFrameDelay = u32MemcFrameDelay;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_DELAY_TIME_BY_MEMC, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetDelayTime_U2(void* pInstance,SCALER_WIN eWindow)
{
    MS_U8 u8FrameNum=0;
    MS_U8 u8WRBankMappingNum=0;
    MS_U8 u8PQDelayCountNum=0;

    u8FrameNum = MApi_XC_Get_FrameNumFactor(eWindow);
    u8WRBankMappingNum = MApi_XC_GetWRBankMappingNum(eWindow);

    if(u8FrameNum > (u8PQDelayCount +u8WRBankMappingNum ))
    {
        u8PQDelayCountNum = u8PQDelayCount +u8WRBankMappingNum;
    }
    else
    {
        u8PQDelayCountNum = u8FrameNum;
    }

#if PQ_XRULE_MM_DS_ENABLE
#ifdef PQDS_INFO_VERSION
#if (PQDS_INFO_VERSION >= 1)
    if (_MDrv_PQ_IsSkipFastDisplay(pInstance, MDrv_PQ_SCtoPQ_WinMapping(eWindow)) == FALSE)
    {
        XC_PQ_FAST_DISP_SUPPORTED_CAPS stCaps;
        memset(&stCaps, 0, sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS));
        stCaps.u32Version = ST_XC_PQ_FAST_DISP_VERSION;
        stCaps.u32Length = sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS);
        stCaps.enPQFastDispType = E_PQ_FAST_DISP_ZEROPQ;
        stCaps.bSupported = FALSE;
        MApi_XC_GetChipCaps(E_XC_HW_FAST_DISP, (MS_U32*)&stCaps, stCaps.u32Length);

        if(stCaps.bSupported && (eWindow == MAIN_WINDOW)) // only main PQ DS XRule
        {
            u8PQDelayCountNum = MM_DS_DELAY_COUNT;
        }
    }
#endif
#endif
#endif

    //Because write memory not just in vsync blanking, so we add one vsync time for avoid garbage.
    u8PQDelayCountNum += 1;

    XC_INITMISC sXC_Init_Misc;
    memset(&sXC_Init_Misc, 0, sizeof(XC_INITMISC));
    MApi_XC_GetMISCStatus(&sXC_Init_Misc);

    XC_ApiStatusEx sXC_ApiStatusEx;
    memset(&sXC_ApiStatusEx, 0, sizeof(XC_ApiStatusEx));
    sXC_ApiStatusEx.u16ApiStatusEX_Length = sizeof(XC_ApiStatusEx);
    sXC_ApiStatusEx.u32ApiStatusEx_Version = API_STATUS_EX_VERSION;
    MApi_XC_GetStatusEx(&sXC_ApiStatusEx, eWindow);

    MS_BOOL bIsFRCBehindVOPOutput120Hz = FALSE;
    if((sXC_ApiStatusEx.u32ApiStatusEx_Version >= 9) && (PQ_120HZ_FRC_HW_BEHIND_VOP == 1))
    {
        bIsFRCBehindVOPOutput120Hz = IsVBY1_16LANE(sXC_ApiStatusEx.u16PanelInterfaceType);
    }

    //need to add frame delay or not
    if(QM_IsSourceFromMVOP(_enInputSourceType[eWindow]))
    {
        u8PQDelayCountNum = u8PQDelayCountNum + MVOP_FRAMEDELAY_COUNT;
    }

    if ((QM_GetInputVFreq(eWindow) != 0) && (QM_GetInputVFreq(eWindow) != 1))
    {
        if (( sXC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE) &&(sXC_ApiStatusEx.bFRCEnabled == true)&&(bIsFRCBehindVOPOutput120Hz != TRUE))
        {
            _u16PQDelyaTime = (u8PQDelayCountNum + 5) * ((10000 + (QM_GetInputVFreq(eWindow)/2))/QM_GetInputVFreq(eWindow));  // frame duration is (1 second/input V freq)
        }
        else
        {
        _u16PQDelyaTime = u8PQDelayCountNum * ((10000 + (QM_GetInputVFreq(eWindow)/2))/QM_GetInputVFreq(eWindow));  // frame duration is (1 second/input V freq)
        }
    }
    else
        _u16PQDelyaTime = u8PQDelayCountNum * 20; // default keep original rule, use 20ms as frame duration

    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_PQ_DELAY_TIME;
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.pValue= &_u16PQDelyaTime;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }
    return _u16PQDelyaTime;
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetDelayTime(SCALER_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetDelayTime PQArgs;
    PQArgs.eWindow=eWindow;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_DELAY_TIME, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

static void _MDrv_PQ_GetFRCRWDiffCount(void* pInstance,PQ_WIN eWindow,MS_U8* pu8FRCInteger,MS_U8* pu8FRCDecimal)
{
    MS_U16 u16OutputVfreq = 0;
    ST_PQ_GET_FRCRWDIFFCOUNT_INFO stPQGetFRCRWDiffCountInfo;
    XC_ApiStatusEx* psXC_ApiStatusEx = NULL;

    memset(&stPQGetFRCRWDiffCountInfo,0,sizeof(ST_PQ_GET_FRCRWDIFFCOUNT_INFO));

    if(pInstance != NULL)
    {
        if((NULL != pu8FRCInteger) || (NULL != pu8FRCDecimal))
        {
            PQ_MALLOC_MEM(psXC_ApiStatusEx, sizeof(XC_ApiStatusEx));
            if(psXC_ApiStatusEx != NULL)
            {
                memset(psXC_ApiStatusEx, 0, sizeof(XC_ApiStatusEx));
                psXC_ApiStatusEx->u16ApiStatusEX_Length = sizeof(XC_ApiStatusEx);
                psXC_ApiStatusEx->u32ApiStatusEx_Version = API_STATUS_EX_VERSION;
                MApi_XC_GetStatusEx(psXC_ApiStatusEx, eWindow);

                u16OutputVfreq = MApi_XC_GetOutputVFreqX100();

                stPQGetFRCRWDiffCountInfo.enWindow = eWindow;
                stPQGetFRCRWDiffCountInfo.u16Input_vfreq = QM_GetInputVFreq(eWindow);
                stPQGetFRCRWDiffCountInfo.u16Output_vfreq = ((psXC_ApiStatusEx->bIs120HzOutput == TRUE) ? (u16OutputVfreq << 1) : (u16OutputVfreq))/10;
                stPQGetFRCRWDiffCountInfo.bEnableGameMode = MDrv_PQ_GetGameMode_Status_U2(pInstance,eWindow);
                MHal_PQ_GetFRCRWDiffCount(pInstance,&stPQGetFRCRWDiffCountInfo);
                *pu8FRCInteger = stPQGetFRCRWDiffCountInfo.u8FRCInteger;
                *pu8FRCDecimal = stPQGetFRCRWDiffCountInfo.u8FRCDecimal;
                PQ_FREE_MEM(psXC_ApiStatusEx, sizeof(XC_ApiStatusEx));
                psXC_ApiStatusEx = NULL;
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Null paramter psXC_ApiStatusEx!!\n");
            }
        }
        else
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Null paramter pu8FRCInteger\pu8FRCDecimal!!\n");
        }
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pInstance == NULL\n");
    }
}


static MS_U16 _MDrv_PQ_GetVideoDelayTimeFirstFrameOut(void* pInstance,PQ_WIN enWindow,ST_PQ_GET_VIDEOTIME_INFO stPQGetVideoTimeInfo)
{
    MS_U16 u16WRDelayTime = 0;
    MS_U8 u8FRCInteger = 0;
    MS_U8 u8FRCDecimal = 0;
    XC_INITMISC sXC_Init_Misc;
    memset(&sXC_Init_Misc, 0, sizeof(XC_INITMISC));
    MApi_XC_GetMISCStatus(&sXC_Init_Misc);

    if(stPQGetVideoTimeInfo.bIsSupportZNR != 0)
    {
        stPQGetVideoTimeInfo.u8WRBankMappingNum = stPQGetVideoTimeInfo.u8WRBankMappingNum + stPQGetVideoTimeInfo.u16UCMRWDiff;
    }

    if((stPQGetVideoTimeInfo.enSourceType == PQ_INPUT_SOURCE_HDMI) && Hal_PQ_Check_Need_DMA_Delay())
    {
        stPQGetVideoTimeInfo.u8WRBankMappingNum = stPQGetVideoTimeInfo.u8WRBankMappingNum + 1;
    }

    PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"u8WRBankMappingNum:%u\n",stPQGetVideoTimeInfo.u8WRBankMappingNum);

    if ((QM_GetInputVFreq(enWindow) != 0) && (QM_GetInputVFreq(enWindow) != 1))
    {
        if (( sXC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE) &&(stPQGetVideoTimeInfo.bFRCEnabled == true))
        {
            _MDrv_PQ_GetFRCRWDiffCount(pInstance,enWindow,&u8FRCInteger,&u8FRCDecimal);//R2 not get input frame rate when start play source,so HW suggest get r/w diff by list

            if(stPQGetVideoTimeInfo.enMfcLevel == E_MFC_LEVEL_BYPASS)
            {
                u16WRDelayTime = (stPQGetVideoTimeInfo.u8WRBankMappingNum + XC_PQ_FRC_MEMC_BYPASS_RW_DIFF_COUNT) * ((10000 + (QM_GetInputVFreq(enWindow)/2))/QM_GetInputVFreq(enWindow));  // frame duration is (1 second/input V freq)
            }
            else
            {
                u16WRDelayTime = (stPQGetVideoTimeInfo.u8WRBankMappingNum + u8FRCInteger) * ((10000 + (QM_GetInputVFreq(enWindow)/2))/QM_GetInputVFreq(enWindow));  // frame duration is (1 second/input V freq)
            }
            if(u8FRCDecimal != 0)
            {
                u16WRDelayTime += (u8FRCDecimal * ((10000 + (QM_GetInputVFreq(enWindow)/2))/QM_GetInputVFreq(enWindow))) / 10; // FRC av sync delay decimal part
            }
        }
        else
        {
            u16WRDelayTime = stPQGetVideoTimeInfo.u8WRBankMappingNum * ((10000 + (QM_GetInputVFreq(enWindow)/2))/QM_GetInputVFreq(enWindow));  // frame duration is (1 second/input V freq)
        }
    }
    else
    {
        u16WRDelayTime = stPQGetVideoTimeInfo.u8WRBankMappingNum * 20; // default keep original rule, use 20ms as frame duration
    }

    PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"XC_PQ_FRC_MEMC_BYPASS_RW_DIFF_COUNT: %d, XC_PQ_FRC_RW_DIFF_COUNT: %d XC_PQ_FRC_RW_DIFF_COUNT_DECIMAL: %d\n",
        XC_PQ_FRC_MEMC_BYPASS_RW_DIFF_COUNT, XC_PQ_FRC_RW_DIFF_COUNT, XC_PQ_FRC_RW_DIFF_COUNT_DECIMAL);
    PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"InputVFreq:%u, u32MISC_A:%u, bFRCEnabled:%d, enMfcLevel:%d\n", QM_GetInputVFreq(enWindow), sXC_Init_Misc.u32MISC_A, stPQGetVideoTimeInfo.bFRCEnabled, stPQGetVideoTimeInfo.enMfcLevel);
    return u16WRDelayTime;
}

MS_U16 MDrv_PQ_GetVideoDelayTime_U2(void* pInstance,SCALER_WIN eWindow,EN_PQ_DelayTime_Type enDelayType,ST_PQ_Delay_Info stdelayinfo)
{
    MS_U8 u8FrameNum=0;
    MS_U8 u8WRBankMappingNum=0;
    MS_U8 u8PQDelayCountNum=0;
    MS_U8 u8FRCDelayNum = 0;
    MS_U16 u16WRDelayTime=0;
    MS_U16 u16UCMFrameNum = 0;
    MS_U16 u16UCMRWDiff = 0;
    MS_U32 u32SupportZNR = 0;

    MS_BOOL bSupportFastPlayByMemc = FALSE;
    MS_U16  u16input_vfreq = 0;    /// input Vertical frequency
    MS_U8 u8FRCInteger = 0;
    MS_U8 u8FRCDecimal = 0;

    u8FrameNum = MApi_XC_Get_FrameNumFactor(eWindow);
    u8WRBankMappingNum = MApi_XC_GetWRBankMappingNum(eWindow);
    u16UCMFrameNum = MHal_PQ_UCMFrameNum();
    u16UCMRWDiff = MHal_PQ_UCMRWDiff();
    MApi_XC_GetChipCaps(E_XC_SUPPORT_ZNR, &u32SupportZNR, sizeof(MS_U32));

    PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"enDelayType: %d u8FrameNum: %d, u8WRBankMappingNum: %d, u16UCMFrameNum: %d, u16UCMRWDiff:%d u32SupportZNR = %d\n",
        enDelayType,u8FrameNum,u8WRBankMappingNum,u16UCMFrameNum,u16UCMRWDiff,u32SupportZNR);

    switch(enDelayType)
    {
        case E_PQ_Delay_FirstFrameOutUserMode:
        {
            ST_XC_FRCFSCCTRLPOLICY stXCFrcFscCtrlPolicy;

            _stMode_Info[eWindow].u16input_hsize = stdelayinfo.stPQModeInfo.u16input_hsize;
            _stMode_Info[eWindow].u16input_vsize = stdelayinfo.stPQModeInfo.u16input_vsize;
            _stMode_Info[eWindow].u16display_hsize = stdelayinfo.stPQModeInfo.u16display_hsize;
            _stMode_Info[eWindow].u16display_vsize = stdelayinfo.stPQModeInfo.u16display_vsize;
            _stMode_Info[eWindow].bInterlace = stdelayinfo.stPQModeInfo.bInterlace;
            _stMode_Info[eWindow].u16input_vfreq = stdelayinfo.stPQModeInfo.u16input_vfreq;
            _enInputSourceType[eWindow] = stdelayinfo.enSourceType;
            if(QM_IsSourceMultiMedia(stdelayinfo.enSourceType))
            {
                MDrv_PQ_SetNetworkMM_OnOff_U2(pInstance,stdelayinfo.bIsNetworkMM);
                MDrv_PQ_Set_MultiMediaInfo_U2(pInstance,eWindow,&(stdelayinfo.stPQMMInfo));
                MApi_XC_Set_DSForceIndex(TRUE,0,eWindow);
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"Not support this source type %d\n",_enInputSourceType[eWindow]);
                return u16WRDelayTime;
            }
            _u16PQSrcType[eWindow] = QM_InputSourceToIndex(pInstance, eWindow, stdelayinfo.enSourceType);
            PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"u16input_hsize:%u, u16input_vsize:%u, u16display_hsize:%d, u16display_vsize:%d\n",
                _stMode_Info[eWindow].u16input_hsize,_stMode_Info[eWindow].u16input_vsize,_stMode_Info[eWindow].u16display_hsize,_stMode_Info[eWindow].u16display_vsize);
            PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"bInterlace:%u, u16input_vfreq:%u, _enInputSourceType:%d, _u16PQSrcType:%d\n",
                _stMode_Info[eWindow].bInterlace,_stMode_Info[eWindow].u16input_vfreq,_enInputSourceType[eWindow],_u16PQSrcType[eWindow]);

            MS_PQ_INFO stPQInfo;
            memset(&stPQInfo,0,sizeof(MS_PQ_INFO));
            stPQInfo.u32Version  = ST_PQ_INFO_VERSION;
            stPQInfo.u32Length  = sizeof(MS_PQ_INFO);
            if(MDrv_PQ_GetInfo_U2(pInstance,&stPQInfo) == E_PQ_RET_FAIL)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_GetInfo_U2 Fail\n");
                return u16WRDelayTime;
            }
            u8FrameNum = stPQInfo.u8FrameNum;
            u8WRBankMappingNum = stPQInfo.u8WRBankMappingNum;
            u16UCMFrameNum = stPQInfo.u16UCMFrameNum;
            u16UCMRWDiff = stPQInfo.u16UCMRWDiff;
            u8PQDelayCount = stPQInfo.u8PQDelayCount;

            PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"u8FrameNum:%u, u8WRBankMappingNum:%u, u16UCMFrameNum:%d, u16UCMRWDiff:%d u8PQDelayCount = %d\n",
                u8FrameNum,u8WRBankMappingNum,u16UCMFrameNum,u16UCMRWDiff,u8PQDelayCount);

            ST_PQ_GET_VIDEOTIME_INFO stPQGetVideoTimeInfo;
            memset(&stPQGetVideoTimeInfo,0,sizeof(stPQGetVideoTimeInfo));
            stPQGetVideoTimeInfo.u8WRBankMappingNum = u8WRBankMappingNum;
            stPQGetVideoTimeInfo.u16UCMFrameNum = u16UCMFrameNum;
            stPQGetVideoTimeInfo.u16UCMRWDiff = u16UCMRWDiff;
            stPQGetVideoTimeInfo.bIsSupportZNR = (u32SupportZNR==0)?FALSE:TRUE;
            stPQGetVideoTimeInfo.enMfcLevel = stdelayinfo.enMfcLevel;
            stPQGetVideoTimeInfo.enSourceType = stdelayinfo.enSourceType;

            memset(&stXCFrcFscCtrlPolicy, 0, sizeof(ST_XC_FRCFSCCTRLPOLICY));
            stXCFrcFscCtrlPolicy.u32Version = XC_FRCFSCCTRLPOLICY_VERSION;
            stXCFrcFscCtrlPolicy.u32Length = sizeof(ST_XC_FRCFSCCTRLPOLICY);
            stXCFrcFscCtrlPolicy.enInputSourceType = stdelayinfo.enSourceType;
            stXCFrcFscCtrlPolicy.u16InputHSize = stdelayinfo.stPQModeInfo.u16input_hsize;
            stXCFrcFscCtrlPolicy.u16InputVSize = stdelayinfo.stPQModeInfo.u16input_vsize;
            stXCFrcFscCtrlPolicy.u16InputVFreq = stdelayinfo.stPQModeInfo.u16input_vfreq;
            stXCFrcFscCtrlPolicy.enWindow = eWindow;
            if(MApi_XC_GetFrcFscCtrlPolicy(&stXCFrcFscCtrlPolicy) == E_APIXC_RET_OK)
            {
                stPQGetVideoTimeInfo.bFRCEnabled = stXCFrcFscCtrlPolicy.bFRCEnabled;
                PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"bFRCEnabled:%d",stPQGetVideoTimeInfo.bFRCEnabled);
            }

            u16WRDelayTime = _MDrv_PQ_GetVideoDelayTimeFirstFrameOut(pInstance,eWindow,stPQGetVideoTimeInfo);
        }
        break;
        case E_PQ_Delay_DisplayWithPQ:
        {
            if(MHal_PQ_IsHalfPQDelaySupport(_enInputSourceType[MAIN_WINDOW]))
            {
                u8PQDelayCount = 0;
                PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"1/2 PQ Delay support, u8PQDelayCount:%u \n", u8PQDelayCount);
            }

            if(u32SupportZNR!=0)
            {
                if(u8FrameNum + u16UCMFrameNum> (u8PQDelayCount +u8WRBankMappingNum + u16UCMRWDiff))
                {
                    u8PQDelayCountNum = u8PQDelayCount +u8WRBankMappingNum + u16UCMRWDiff;
                }
                else
                {
                    u8PQDelayCountNum = u8FrameNum + u16UCMFrameNum;
                }
                PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"ZNR Support, UCMFrameNum: %u, UCMRWDiff: %u, XC FrameNum: %u, WRDiff:%u, GlobalPQDelay:%u, PQDelayCountNum:%u\n", u16UCMFrameNum, u16UCMRWDiff, u8FrameNum, u8WRBankMappingNum, u8PQDelayCount, u8PQDelayCountNum);
            }
            else
            {
                if(u8FrameNum > (u8PQDelayCount +u8WRBankMappingNum))
                {
                    u8PQDelayCountNum = u8PQDelayCount +u8WRBankMappingNum ;
                }
                else
                {
                    u8PQDelayCountNum = u8FrameNum ;
                }
                PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"ZNR not support, XC FrameNum: %u, WRDiff:%u, GlobalPQDelay:%u,PQDelayCountNum:%u\n", u8FrameNum, u8WRBankMappingNum, u8PQDelayCount, u8PQDelayCountNum);
            }

#if PQ_XRULE_MM_DS_ENABLE
#ifdef PQDS_INFO_VERSION
#if (PQDS_INFO_VERSION >= 1)
            if (_MDrv_PQ_IsSkipFastDisplay(pInstance, MDrv_PQ_SCtoPQ_WinMapping(eWindow)) == FALSE)
            {
                XC_PQ_FAST_DISP_SUPPORTED_CAPS stCaps;
                memset(&stCaps, 0, sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS));
                stCaps.u32Version = ST_XC_PQ_FAST_DISP_VERSION;
                stCaps.u32Length = sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS);
                stCaps.enPQFastDispType = E_PQ_FAST_DISP_ZEROPQ;
                stCaps.bSupported = FALSE;
                MApi_XC_GetChipCaps(E_XC_HW_FAST_DISP, (MS_U32*)&stCaps, stCaps.u32Length);

                if(stCaps.bSupported && (eWindow == MAIN_WINDOW)) // only main PQ DS XRule
                {
                    if(u32SupportZNR != 0)
                    {
                        u8PQDelayCountNum = MM_DS_DELAY_COUNT + u16UCMRWDiff;
                        PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"Fast Display & ZNR support, UCMRDiff:%u, PQDelayCountNum:%u\n", u16UCMRWDiff, u8PQDelayCountNum);
                    }
                    else
                    {
                        u8PQDelayCountNum = MM_DS_DELAY_COUNT;
                        PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"Fast Display support, PQDelayCountNum:%u\n", u8PQDelayCountNum);
                    }
                }
            }
#endif
#endif
#endif

            //Because write memory not just in vsync blanking, so we add one vsync time for avoid garbage.
            u8PQDelayCountNum += 1;
            PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"XC add one frame delay to avoid garbage, PQDelayCountNum:%u\n", u8PQDelayCountNum);

            XC_INITMISC sXC_Init_Misc;
            memset(&sXC_Init_Misc, 0, sizeof(XC_INITMISC));
            MApi_XC_GetMISCStatus(&sXC_Init_Misc);

            XC_ApiStatusEx sXC_ApiStatusEx;
            memset(&sXC_ApiStatusEx, 0, sizeof(XC_ApiStatusEx));
            sXC_ApiStatusEx.u16ApiStatusEX_Length = sizeof(XC_ApiStatusEx);
            sXC_ApiStatusEx.u32ApiStatusEx_Version = API_STATUS_EX_VERSION;
            MApi_XC_GetStatusEx(&sXC_ApiStatusEx, eWindow);

            if(HSD_IP2_RESET_PATCH == TRUE)
            {
                if(sXC_ApiStatusEx.bPreH_ScalingDown == TRUE)
                {
                    //IP2 reset patch
                    u8PQDelayCountNum += 2;
                    PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"XC add two frame delay to avoid garbage for ip2 reset patch, PQDelayCountNum:%u\n", u8PQDelayCountNum);
                }
            }

            MS_BOOL bIsFRCBehindVOPOutput120Hz = FALSE;
            MS_BOOL bIsSupport4K2K120Hz_2pMode = FALSE;
            if((sXC_ApiStatusEx.u32ApiStatusEx_Version >= 9) && (PQ_120HZ_FRC_HW_BEHIND_VOP == 1))
            {
                bIsFRCBehindVOPOutput120Hz = IsVBY1_16LANE(sXC_ApiStatusEx.u16PanelInterfaceType);
            }
            else
            {
                //such as mt5888 120Hz
                bIsSupport4K2K120Hz_2pMode = IsVBY1_16LANE(sXC_ApiStatusEx.u16PanelInterfaceType);
            }

            //need to add frame delay or not
            if(QM_IsSourceFromMVOP(_enInputSourceType[eWindow]))
            {
                u8PQDelayCountNum = u8PQDelayCountNum + MVOP_FRAMEDELAY_COUNT;
                PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"MVOP frame delay: %d, PQDelayCountNum:%u\n", MVOP_FRAMEDELAY_COUNT, u8PQDelayCountNum);
            }

            PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"InputVFreq:%u, u32MISC_A:%u, bFRCEnabled:%d\n", QM_GetInputVFreq(eWindow), sXC_Init_Misc.u32MISC_A, sXC_ApiStatusEx.bFRCEnabled);

            bSupportFastPlayByMemc = MHal_PQ_GetFastPlayByMemcCap();
            if ((bSupportFastPlayByMemc) && (bIsSupport4K2K120Hz_2pMode))
            {
                u8PQDelayCountNum += 1; //RD said,the time that the maibox receive the fastplay CMD is not fixed the timing.
            }
            u16input_vfreq = QM_GetInputVFreq(eWindow);
            if ((u16input_vfreq != 0) && (u16input_vfreq != 1))
            {
                if (( sXC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE) && (sXC_ApiStatusEx.bFRCEnabled == true) && (bIsFRCBehindVOPOutput120Hz != TRUE))
                {
                    //FRC-R2 not support the feature( R2-RD said); procfs disable the feature
                    if((0 == XC_PQ_FRC_RW_DIFF_COUNT) || (!_u16PQProcfsFastPlayByMemc))
                    {
                        u8FRCDelayNum = XC_PQ_FRC_FrameDelay_Count;
                        PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME, "[FastPlay] 1.Disable when totalPQ.\n");
                    }
                    else
                    {
                        //MEMC_LEVEL_BYPASS || MEMC_LEVEL_BYPASS_64
                        if(sXC_ApiStatusEx.u16MemcLevelStatus == MEMC_LEVEL_BYPASS)
                        {
                            u8FRCDelayNum = XC_PQ_FRC_MEMC_BYPASS_RW_DIFF_COUNT;
                            PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME, "[FastPlay]Enable: memc level bypass when totalPQ. \n");
                        }
                        else
                        {
                            _MDrv_PQ_GetFRCRWDiffCount(pInstance,eWindow,&u8FRCInteger,&u8FRCDecimal);//R2 not get input frame rate when start play source,so HW suggest get r/w diff by list
                            u8FRCDelayNum = u8FRCInteger;

                            if (u8FRCDecimal != 0)
                            {
                                u8FRCDelayNum += 1;
                            }
                        }
                        PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME, "[FastPlay]FRC_R2_Outputfreq=0x%x FRC_R2_Inputfreq=0x%x XC_PQ_FRC_RW_DIFF_COUNT=0x%x. \n",
                                            XC_PQ_FRC_R2_Outputfreq,
                                            XC_PQ_FRC_R2_Inputfreq,
                                            XC_PQ_FRC_RW_DIFF_COUNT);
                    }
                    _u16PQDelyaTime = (u8PQDelayCountNum + u8FRCDelayNum) * ((10000 + (u16input_vfreq/2))/u16input_vfreq);  // frame duration is (1 second/input V freq)
                }
                else
                {
                    _u16PQDelyaTime = u8PQDelayCountNum * ((10000 + (u16input_vfreq/2))/u16input_vfreq);  // frame duration is (1 second/input V freq)
                }
            }
            else
                _u16PQDelyaTime = u8PQDelayCountNum * 20; // default keep original rule, use 20ms as frame duration

            u16WRDelayTime = _u16PQDelyaTime;

            PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME, "u8FRCDelayNum=0x%x u8PQDelayCountNum=%d totalPQ:u16WRDelayTime=%d \n",
                                u8FRCDelayNum,
                                u8PQDelayCountNum,
                                u16WRDelayTime);
        }
        break;
        case E_PQ_Delay_FirstFrameOut:
        {
            ST_PQ_GET_VIDEOTIME_INFO stPQGetVideoTimeInfo;
            memset(&stPQGetVideoTimeInfo,0,sizeof(stPQGetVideoTimeInfo));

            XC_ApiStatusEx stXC_ApiStatusEx;
            memset(&stXC_ApiStatusEx, 0, sizeof(XC_ApiStatusEx));
            stXC_ApiStatusEx.u16ApiStatusEX_Length = sizeof(XC_ApiStatusEx);
            stXC_ApiStatusEx.u32ApiStatusEx_Version = API_STATUS_EX_VERSION;
            MApi_XC_GetStatusEx(&stXC_ApiStatusEx, eWindow);

            stPQGetVideoTimeInfo.u8WRBankMappingNum = u8WRBankMappingNum;
            stPQGetVideoTimeInfo.u16UCMFrameNum = u16UCMFrameNum;
            stPQGetVideoTimeInfo.u16UCMRWDiff = u16UCMRWDiff;
            stPQGetVideoTimeInfo.bIsSupportZNR = (u32SupportZNR==0)?FALSE:TRUE;
            stPQGetVideoTimeInfo.enSourceType = stdelayinfo.enSourceType;
            stPQGetVideoTimeInfo.bFRCEnabled = stXC_ApiStatusEx.bFRCEnabled;
            //MEMC_LEVEL_BYPASS || MEMC_LEVEL_BYPASS_64
            if(stXC_ApiStatusEx.u16MemcLevelStatus == MEMC_LEVEL_BYPASS)
            {
                stPQGetVideoTimeInfo.enMfcLevel = E_MFC_LEVEL_BYPASS;
            }
            u16WRDelayTime = _MDrv_PQ_GetVideoDelayTimeFirstFrameOut(pInstance,eWindow,stPQGetVideoTimeInfo);
        }
        break;
        case E_PQ_Delay_FirstFrameOut_Avg:
            if(stdelayinfo.u32Length == 0)
                u16WRDelayTime = 0;
            else
            {
                MS_PQ_INPUT_SOURCE_TYPE enInputSource;
                if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_VGA)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_VGA;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_TV)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_TV;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_CVBS)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_CVBS;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_SVIDEO)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_SVIDEO;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_YPBPR)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_YPBPR;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_SCART)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_SCART;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_HDMI)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_HDMI;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_DTV)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_DTV;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_DVI)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_DVI;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_STORAGE)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_STORAGE;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_KTV)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_KTV;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_JPEG)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_JPEG;
                }
                else if (stdelayinfo.enSourceType == PQ_INPUT_SOURCE_NUM)
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_NUM;
                }
                else
                {
                    enInputSource = MS_PQ_INPUT_SOURCE_NONE;
                }
                u16WRDelayTime = MHal_PQ_GetPQDelayAvg(enInputSource);
            }
        break;
        case E_PQ_Delay_MEMC_PERFRAME:
        {
            MS_U16 u16DelayTimePerFrame = 0;

            XC_INITMISC stXC_Init_Misc;
            memset(&stXC_Init_Misc, 0, sizeof(XC_INITMISC));
            MApi_XC_GetMISCStatus(&stXC_Init_Misc);

            XC_ApiStatusEx stXC_ApiStatusEx;
            memset(&stXC_ApiStatusEx, 0, sizeof(XC_ApiStatusEx));
            stXC_ApiStatusEx.u16ApiStatusEX_Length = sizeof(XC_ApiStatusEx);
            stXC_ApiStatusEx.u32ApiStatusEx_Version = API_STATUS_EX_VERSION;
            MApi_XC_GetStatusEx(&stXC_ApiStatusEx, eWindow);

            u16input_vfreq = QM_GetInputVFreq(eWindow);
            bSupportFastPlayByMemc = MHal_PQ_GetFastPlayByMemcCap();
            //FRC-R2 support the feature;procfs not disable the feature;xc has input signal;
            if ((bSupportFastPlayByMemc)
                && (0 != XC_PQ_FRC_RW_DIFF_COUNT)
                && (_u16PQProcfsFastPlayByMemc)
                && (u16input_vfreq != 0) && (u16input_vfreq != 1))
            {
                if ((stXC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE) &&(stXC_ApiStatusEx.bFRCEnabled == TRUE))
                {
                    u16DelayTimePerFrame = ((10000 + (u16input_vfreq/2))/u16input_vfreq); // frame duration is (1 second/input V freq)
                }
                else
                {
                    u16DelayTimePerFrame = 0;
                }
                PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME, "[FastPlay]FRC_R2_Outputfreq=0x%x FRC_R2_Inputfreq=0x%x XC_PQ_FRC_RW_DIFF_COUNT=0x%x. \n",
                                    XC_PQ_FRC_R2_Outputfreq,
                                    XC_PQ_FRC_R2_Inputfreq,
                                    XC_PQ_FRC_RW_DIFF_COUNT);
            }
            else
            {
                u16DelayTimePerFrame = 0;  // default keep original rule, use 20ms as frame duration
                PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME, "[FastPlay] 2.Disable FastPlay or no signal when perMemc:MemcPerFrame=%d \n", u16DelayTimePerFrame);
            }
            u16WRDelayTime = u16DelayTimePerFrame;

            PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME, "[FastPlay]InputVFreq:%u, u32MISC_A:%u, bFRCEnabled:%d, MemcPerFrame:%d\n",
                                u16input_vfreq,
                                stXC_Init_Misc.u32MISC_A,
                                stXC_ApiStatusEx.bFRCEnabled,
                                u16WRDelayTime);
            }
            break;
            case E_PQ_Delay_MEMC_FIRSTFRAMEOUT:
            {
                XC_INITMISC stXC_Init_Misc;
                memset(&stXC_Init_Misc, 0, sizeof(XC_INITMISC));
                MApi_XC_GetMISCStatus(&stXC_Init_Misc);

                XC_ApiStatusEx stXC_ApiStatusEx;
                memset(&stXC_ApiStatusEx, 0, sizeof(XC_ApiStatusEx));
                stXC_ApiStatusEx.u16ApiStatusEX_Length = sizeof(XC_ApiStatusEx);
                stXC_ApiStatusEx.u32ApiStatusEx_Version = API_STATUS_EX_VERSION;
                MApi_XC_GetStatusEx(&stXC_ApiStatusEx, eWindow);

                u16input_vfreq = QM_GetInputVFreq(eWindow);
                bSupportFastPlayByMemc = MHal_PQ_GetFastPlayByMemcCap();
                //FRC-R2 support the feature;procfs not disable the feature;xc has input signal;
                if ((bSupportFastPlayByMemc)
                    && (0 != XC_PQ_FRC_RW_DIFF_COUNT)
                    && (_u16PQProcfsFastPlayByMemc)
                    && (u16input_vfreq != 0) && (u16input_vfreq != 1))
                {
                    if (( stXC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE) &&(stXC_ApiStatusEx.bFRCEnabled == true))
                    {
                        //MEMC_LEVEL_BYPASS || MEMC_LEVEL_BYPASS_64
                        if(stXC_ApiStatusEx.u16MemcLevelStatus == MEMC_LEVEL_BYPASS)
                        {
                            u8FRCDelayNum = XC_PQ_FRC_MEMC_BYPASS_RW_DIFF_COUNT;
                            PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME, "[FastPlay]Enable memc level bypass when MEMC_FIRSTFRAMEOUT. \n");
                        }
                        else
                        {
                            _MDrv_PQ_GetFRCRWDiffCount(pInstance,eWindow,&u8FRCInteger,&u8FRCDecimal);//R2 not get input frame rate when start play source,so HW suggest get r/w diff by list
                            u8FRCDelayNum = u8FRCInteger;
                            if (u8FRCDecimal != 0)
                            {
                                u8FRCDelayNum += 1;
                            }
                        }
                        PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME, "[FastPlay]FRC_R2_Outputfreq=0x%x FRC_R2_Inputfreq=0x%x XC_PQ_FRC_RW_DIFF_COUNT=0x%x. \n",
                                            XC_PQ_FRC_R2_Outputfreq,
                                            XC_PQ_FRC_R2_Inputfreq,
                                            XC_PQ_FRC_RW_DIFF_COUNT);
                    }
                    else
                    {
                        u8FRCDelayNum = 0;
                    }
                    u16WRDelayTime = u8FRCDelayNum *((10000 + (u16input_vfreq/2))/u16input_vfreq); // frame duration is (1 second/input V freq)
                }
                else
                {
                    u16WRDelayTime = 0;
                    PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME, "[FastPlay] 3.Disable when Memcdelay:MEMC_FIRSTFRAMEOUT=%d \n", u16WRDelayTime);
                }

                PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME, "[FastPlay] InputVFreq:%u, u32MISC_A:%u, bFRCEnabled:%d, enMfcLevel:%d u8FRCDelayNum=%d MEMC_FIRSTFRAMEOUT:%d\n",
                                    u16input_vfreq,
                                    stXC_Init_Misc.u32MISC_A,
                                    stXC_ApiStatusEx.bFRCEnabled,
                                    stdelayinfo.enMfcLevel,
                                    u8FRCDelayNum,
                                    u16WRDelayTime);
        }
        break;
        default:
        break;
    }
    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_PQ_DELAY_TIME;
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.pValue= &u16WRDelayTime;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }
    PQ_LEVEL_PRINTF(PQ_DBG_DELAYTIME,"DelayTime: %d, DelayType: %d\n", u16WRDelayTime, enDelayType);
    return u16WRDelayTime;
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetVideoDelayTime(SCALER_WIN eWindow,EN_PQ_DelayTime_Type enDelayType,ST_PQ_Delay_Info stdelayinfo)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetVideoDelayTime PQArgs;
    PQArgs.eWindow=eWindow;
    PQArgs.enDelayType=enDelayType;
    memcpy(&(PQArgs.stdelayinfo), &stdelayinfo, sizeof(ST_PQ_Delay_Info));
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_VIDEO_DELAY_TIME, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}
MS_PQ_EXECUTOR MDrv_PQ_GetQmapExecutor_U2(void* pInstance,MS_PQ_FUNCTION_TYPE eFunctionType)
{
    MS_PQ_EXECUTOR enChipExecutor = EN_PQ_EXECUTOR_SOC;

    if( REG_CHIP_EXECUTOR_DUMMY != 0)
    {
        MS_U8 u8ExecutorDummy = (MS_U8)MApi_XC_R2BYTEMSK(REG_CHIP_EXECUTOR_DUMMY, 0xFF);
        MS_U8 u8ExecutorKeyDummy = (MS_U8)MApi_XC_R2BYTEMSK(REG_CHIP_EXECUTOR_KEY_DUMMY, 0xFF);

        if(u8ExecutorKeyDummy==REG_CHIP_EXECUTOR_KEY)
        {
            switch (eFunctionType)
            {
                case EN_PQ_ACE:
                    {
                        if(u8ExecutorDummy & BIT(0))
                            enChipExecutor= EN_PQ_EXECUTOR_BACKEND;
                        break;
                    }
                case EN_PQ_COLOR_TEMPERATURE:
                    {
                        if(u8ExecutorDummy & BIT(1))
                            enChipExecutor= EN_PQ_EXECUTOR_BACKEND;
                        break;
                    }
                case EN_PQ_NOISE_REDUCTION:
                    {
                        if(u8ExecutorDummy & BIT(2))
                            enChipExecutor= EN_PQ_EXECUTOR_BACKEND;
                        break;
                    }
                case EN_PQ_MPEG_NOISE_REDUCTION:
                    {
                        if(u8ExecutorDummy & BIT(3))
                            enChipExecutor= EN_PQ_EXECUTOR_BACKEND;
                        break;
                    }
                case EN_PQ_DYNAMIC_CONTRAST:
                    {
                        if(u8ExecutorDummy & BIT(4))
                            enChipExecutor= EN_PQ_EXECUTOR_BACKEND;
                        break;
                    }
                default:
                    enChipExecutor= EN_PQ_NONE;
                    break;
            }
        }
    }
    return enChipExecutor;
}
MS_PQ_EXECUTOR SYMBOL_WEAK MDrv_PQ_GetQmapExecutor(MS_PQ_FUNCTION_TYPE eFunctionType)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetQmapExecutor PQArgs;
    PQArgs.bReturnValue = EN_PQ_NONE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_QMAP_EXECUTOR, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return EN_PQ_NONE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}
// if not HSD case, ignore return value
static MS_BOOL _MDrv_PQ_LoadScalingTable(
        void* pInstance,
        PQ_WIN  eWindow,
        MS_U8 eXRuleType,
        MS_U8 u8XRuleIP,
        MS_BOOL bPreV_ScalingDown,
        MS_BOOL bInterlace,
        MS_BOOL bColorSpaceYUV,
        MS_U16 u16InputSize,
        MS_U16 u16SizeAfterScaling)
{
    MS_U32 u32Ratio;
    MS_U16 u16IPIdx;
#if PQ_ENABLE_PIP
    MS_U16 u16SFilter = 0;
#endif

    MS_U16 u16TabIdx = gbPQBinEnable == 0 ? PQ_IP_NULL : PQ_BIN_IP_NULL;
    MS_U8 u8XRuleIdx = 0xFF;
#if (defined (UFO_XC_DS_PQ))
    MS_BOOL bCheckGRule = FALSE;
#endif
    MS_U16 u16Input_HSize = 0;
    MS_U16 u16Input_VSize = 0;

    XC_GET_DS_INFO stGetDSInfo;
    memset(&stGetDSInfo, 0, sizeof(XC_GET_DS_INFO));
    MApi_XC_GetDSInfo(&stGetDSInfo, sizeof(XC_GET_DS_INFO), eWindow);

    if(u16InputSize == 0)
        u32Ratio = 2000;
    else
        u32Ratio = ((MS_U32) u16SizeAfterScaling * 1000) / u16InputSize;

    u16IPIdx = MDrv_PQ_GetXRuleIPIndex(pInstance, eWindow, eXRuleType, u8XRuleIP, PQ_XC0_STD);
    PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"Win:%u, eXRuleType:%u, XRuleIP:%u, bPreV_ScalingDown:%u, bInterlace:%u, IPIdx:%u, InputSize:%u, SizeAfterScaling:%u, ratio:%td, bColorSpaceYUV:%u",
                                 eWindow, eXRuleType, u8XRuleIP, bPreV_ScalingDown, bInterlace, u16IPIdx,
                                 u16InputSize, u16SizeAfterScaling,(ptrdiff_t) u32Ratio, bColorSpaceYUV);

    if(bPreV_ScalingDown && bInterlace)
    {
        u8XRuleIdx = PQ_HSDRule_PreV_ScalingDown_Interlace_Main;
        u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, eWindow, (MS_U16)eXRuleType, (MS_U16)u8XRuleIdx, (MS_U16)u8XRuleIP, PQ_XC0_STD);
    }
    else if(bPreV_ScalingDown && (!bInterlace))
    {
        u8XRuleIdx = PQ_HSDRule_PreV_ScalingDown_Progressive_Main;
        u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, eWindow, (MS_U16)eXRuleType, (MS_U16)u8XRuleIdx, (MS_U16)u8XRuleIP, PQ_XC0_STD);
    }

    if(!IS_PQ_TABLEIDX_NULL(u16TabIdx)) //PreV saling down case
    {
        PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"u8XRuleIdx: PreV saling down case, interlace:%s, tabidx=%u", (bInterlace?("TRUE"):("FALSE")), (MS_U16)u16TabIdx);
    }
    else
    {
        if(u32Ratio > 1000) //Scaling UP case load Main QMAP
        {
            u16TabIdx = MDrv_PQ_GetTableIndex_U2(pInstance, eWindow, u16IPIdx);
            PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"u8XRuleIdx: >x1 case load back to Main rule, tabidx=%u", (MS_U16)u16TabIdx);
        }
        else
        {
            if(bColorSpaceYUV)
            {
                if(u32Ratio == 1000)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_10x_YUV_Main;
                else if(u32Ratio >= 900)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_09x_YUV_Main;
                else if(u32Ratio >= 800)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_08x_YUV_Main;
                else if(u32Ratio >= 700)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_07x_YUV_Main;
                else if(u32Ratio >= 600)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_06x_YUV_Main;
                else if(u32Ratio >= 500)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_05x_YUV_Main;
                else if(u32Ratio >= 400)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_04x_YUV_Main;
                else if(u32Ratio >= 300)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_03x_YUV_Main;
                else if(u32Ratio >= 200)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_02x_YUV_Main;
                else if(u32Ratio >= 100)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_01x_YUV_Main;
                else
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_00x_YUV_Main;
            }
            else
            {
                if(u32Ratio == 1000)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_10x_RGB_Main;
                else if(u32Ratio >= 900)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_09x_RGB_Main;
                else if(u32Ratio >= 800)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_08x_RGB_Main;
                else if(u32Ratio >= 700)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_07x_RGB_Main;
                else if(u32Ratio >= 600)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_06x_RGB_Main;
                else if(u32Ratio >= 500)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_05x_RGB_Main;
                else if(u32Ratio >= 400)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_04x_RGB_Main;
                else if(u32Ratio >= 300)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_03x_RGB_Main;
                else if(u32Ratio >= 200)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_02x_RGB_Main;
                else if(u32Ratio >= 100)
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_01x_RGB_Main;
                else
                    u8XRuleIdx = PQ_HSDRule_ScalingDown_00x_RGB_Main;
            }

            PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"u8XRuleIdx=%u, ", (MS_U16)u8XRuleIdx);
            if(u8XRuleIdx == 0xFF)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"u8XRuleIdx = %u",u8XRuleIdx);
                PQ_ASSERT(0);
                return 1;
            }

            u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, eWindow, (MS_U16)eXRuleType, (MS_U16)u8XRuleIdx, (MS_U16)u8XRuleIP, PQ_XC0_STD);
            if(IS_PQ_TABLEIDX_NULL(u16TabIdx))
            {
                u16TabIdx = MDrv_PQ_GetTableIndex_U2(pInstance, eWindow, u16IPIdx);
                PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"MDrv_PQ_GetXRuleTableIndex retrun NULL TabIdx load back to Main rule, XRuleIdx=%u, tabidx=%u\n", (MS_U16)u8XRuleIP, u16TabIdx);
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"XRule Table Index got, tabidx=%u\n", u16TabIdx);
            }
        }

        //For non-standard resolution, VSP & HSP need to load different setting from standard case
        if((eXRuleType == HVSP_XRULE_HSP) || (eXRuleType == HVSP_XRULE_VSP))
        {
            u16Input_HSize = QM_GetInputHSize(eWindow);
            u16Input_VSize = QM_GetInputVSize(eWindow);
            if(IS_HVSP_NONSTANDARD_RESOLUTION(u16Input_HSize, u16Input_VSize))
            {
                if(bColorSpaceYUV)
                {
                    u8XRuleIdx = PQ_HSPRule_H1920Up_NonStandard_YUV_Main;
                }
                else
                {
                    u8XRuleIdx = PQ_HSPRule_H1920Up_NonStandard_RGB_Main;
                }

                PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"Non Standard resolution HVSP case, HSize=%u, VSize=%u, XRuleIdx=%u\n", u16Input_HSize, u16Input_VSize, (MS_U16)u8XRuleIdx);
                u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, eWindow, (MS_U16)eXRuleType, (MS_U16)u8XRuleIdx, (MS_U16)u8XRuleIP, PQ_XC0_STD);
                if(IS_PQ_TABLEIDX_NULL(u16TabIdx))
                {
                    u16TabIdx = MDrv_PQ_GetTableIndex_U2(pInstance, eWindow, u16IPIdx);
                    PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"MDrv_PQ_GetXRuleTableIndex retrun NULL TabIdx load back to Main QMAP, XRuleIdx=%u, tabidx=%u\n", (MS_U16)u8XRuleIP, u16TabIdx);
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"XRule Table Index got, tabidx=%u\n", u16TabIdx);
                }
            }
        }
    }

    if(!stGetDSInfo.bUpdate_DS_CMD)
    {
#if PQ_EN_DMS_SW_CTRL
        if((u16IPIdx == PQ_IP_DMS_V_12L_Main) && (PQ_MAIN_WINDOW == eWindow))
        {
            if(u16TabIdx == PQ_IP_DMS_V_12L_OFF_Main)
            {
                _bDMSV12L_PQConfigEn = FALSE;
            }
            else
            {
                _bDMSV12L_PQConfigEn = TRUE;
            }
        }

    // App will call halt_subwin to disconnet sub window.
    // It will casue PQ display type to be PQ_DISPLAY_ONE.
    // But, sub window is still on and DMS_LV_12 will be set
    // to On in HSD rulee for main window.
    // For fix this issue, we need to check sub window wheter
    // on or off to descide DMS_LV_12 off or on.

    //Disable DMS_V12_L
    // 1.VSD : V prescaling, 2. PIP, 3. FBL
        if((((eXRuleType == E_XRULE_VSD)&&(bPreV_ScalingDown))
            ||(_stMode_Info[eWindow].bFBL)
            || (E_XC_3D_INPUT_MODE_NONE != MApi_XC_Get_3D_Input_Mode(MAIN_WINDOW))
#if PQ_ENABLE_PIP
            || (PQ_SUB_WINDOW == eWindow)
            || ((PQ_MAIN_WINDOW == eWindow) && (_bDMSV12L_PIPEn == FALSE))
#endif
            )
          &&
            ((PQ_IP_DMS_V_12L_Main == MDrv_PQ_GetXRuleIPIndex(pInstance, eWindow, eXRuleType, u8XRuleIP, PQ_XC0_STD))
//#if PQ_ENABLE_PIP
//             || (PQ_IP_DMS_V_12L_Sub == MDrv_PQ_GetXRuleIPIndex(pInstance, eWindow, eXRuleType, u8XRuleIP, PQ_XC0_STD))
//#endif
             )
        )
        {
            //Disable De-Mosquito
            u16TabIdx = PQ_IP_DMS_V_12L_OFF_Main;
            u16IPIdx = PQ_IP_DMS_V_12L_Main;
            eWindow = PQ_MAIN_WINDOW;
        }
#endif
#if PQ_ENABLE_PIP

        if(((u16IPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_SRAM3)) || (u16IPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_SRAM4)) ) &&(PQ_SUB_WINDOW == eWindow)
         &&((E_XC_3D_OUTPUT_LINE_ALTERNATIVE == MApi_XC_Get_3D_Output_Mode())
            || (E_XC_3D_OUTPUT_TOP_BOTTOM == MApi_XC_Get_3D_Output_Mode())
            || (E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF == MApi_XC_Get_3D_Output_Mode())
            || (E_XC_3D_OUTPUT_TOP_BOTTOM_HW == MApi_XC_Get_3D_Output_Mode())
            || (E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF_HW == MApi_XC_Get_3D_Output_Mode())))
        {
            u16SFilter = MApi_XC_R2BYTE(REG_SC_BK23_0B_L);
            MApi_XC_W2BYTEMSK(REG_SC_BK23_0B_L, 0x0303 , 0xFFFF );
        }
#endif
    }
#if (defined (UFO_XC_DS_PQ))
    if(stGetDSInfo.bUpdate_DS_CMD)
    {
        if(eWindow == PQ_MAIN_WINDOW)
        {
            switch (u16IPIdx)
            {
                case PQ_IP_MADi_Main:
                    if (_s32LastPQIPMADiMain == (MS_S32)u16TabIdx)
                    {
                        return FALSE;
                    }
                    _s32LastPQIPMADiMain = u16TabIdx;

                    break;
#if (PQ_MADI_DFK == 1)
                    case PQ_IP_MADi_Motion_Main:
                    if (_s32LastPQIPMADiMotMain == (MS_S32)u16TabIdx)
                    {
                        return FALSE;
                    }
                    _s32LastPQIPMADiMotMain = u16TabIdx;

                    break;
#endif
                case PQ_IP_MemFormat_Main:
                    if (_s32LastPQIPMemFormatMain == (MS_S32)u16TabIdx)
                    {
                        return FALSE;
                    }
                    _s32LastPQIPMemFormatMain = u16TabIdx;
                    break;
                case PQ_IP_HSD_Y_Main:
                    if (_s32LastPQIPHSDYMain == (MS_S32)u16TabIdx)
                    {
                        return FALSE;
                    }
                    _s32LastPQIPHSDYMain = u16TabIdx;
                    break;
                case PQ_IP_HSD_C_Main:
                    if (_s32LastPQIPHSDCMain == (MS_S32)u16TabIdx)
                    {
                        return FALSE;
                    }
                    _s32LastPQIPHSDCMain = u16TabIdx;
                    break;
                default:
                    break;
             }
            MDrv_PQBin_DS_GetTable(pInstance,eWindow, u16TabIdx, u16IPIdx, PQ_XC0_STD, E_DS_MODULE_OP, bCheckGRule);  //Only Main has X rule
        }
    }
    else
#endif
    {
        MDrv_PQ_LoadTable_U2(pInstance, eWindow, u16TabIdx, u16IPIdx);
    }

    if(PQ_MAIN_WINDOW == eWindow)
    {
        switch (u16IPIdx)
        {
            case PQ_IP_SRAM1_Main:
                _u16SRAM1 = u16TabIdx;
                break;
            case PQ_IP_SRAM2_Main:
                _u16SRAM2 = u16TabIdx;
                break;
            default:
                break;
        }
    }

#if PQ_ENABLE_PIP
    if(!stGetDSInfo.bUpdate_DS_CMD)
    {
        if(((u16IPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_SRAM3)) || (u16IPIdx == MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_SRAM4))) &&(PQ_SUB_WINDOW == eWindow)
            &&((E_XC_3D_OUTPUT_LINE_ALTERNATIVE == MApi_XC_Get_3D_Output_Mode())
            || (E_XC_3D_OUTPUT_TOP_BOTTOM == MApi_XC_Get_3D_Output_Mode())
            || (E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF == MApi_XC_Get_3D_Output_Mode())
            || (E_XC_3D_OUTPUT_TOP_BOTTOM_HW == MApi_XC_Get_3D_Output_Mode())
            || (E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF_HW == MApi_XC_Get_3D_Output_Mode())))
        {
            MApi_XC_W2BYTEMSK(REG_SC_BK23_0B_L, u16SFilter , 0xFFFF );
        }
    }
#endif

    if(eXRuleType == E_XRULE_VSD)
        return (u16TabIdx  == PQ_IP_VSD_Bilinear_Main) ; // PreVSDMode: 0:Cb, 1:Bilinear
    else
        return (u16TabIdx != PQ_IP_HSD_Y_CB_Main); // PreHSDMode - 0:Cb, 1:Adv

}

MS_BOOL MDrv_PQ_LoadScalingTable_U2(void* pInstance,
                                 PQ_WIN eWindow,
                                 MS_U8 eXRuleType,
                                 MS_BOOL bPreV_ScalingDown,
                                 MS_BOOL bInterlace,
                                 MS_BOOL bColorSpaceYUV,
                                 MS_U16 u16InputSize,
                                 MS_U16 u16SizeAfterScaling)
{
    MS_BOOL bRet = 0; // default is CB mode
    MS_U16 i;

    if(eXRuleType > 3)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"eXRuleType %u",eXRuleType);
        PQ_ASSERT(0);
        return bRet;
    }
    PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"[PQ_LoadScalingTable] HSD/VSD/HSP/VSP:%u\n", (MS_U16)eXRuleType);

    XC_GET_DS_INFO* pstGetDSInfo = NULL;
    PQ_MALLOC_MEM(pstGetDSInfo,sizeof(XC_GET_DS_INFO));
    if(pstGetDSInfo != NULL)
    {
        memset(pstGetDSInfo  , 0, sizeof(XC_GET_DS_INFO));
        MApi_XC_GetDSInfo(pstGetDSInfo, sizeof(XC_GET_DS_INFO), MDrv_PQ_SCtoPQ_WinMapping(eWindow));
#if (defined (UFO_XC_DS_PQ))//PQ DS not support pre scaling
        if(pstGetDSInfo->bUpdate_DS_CMD)
        {
            if((eXRuleType == E_XRULE_HSD)||(eXRuleType== E_XRULE_VSD))
            {
                PQ_FREE_MEM(pstGetDSInfo,sizeof(XC_GET_DS_INFO));
                return bRet;
            }
        }
#endif

        if(!pstGetDSInfo->bUpdate_DS_CMD)
        {
#if (ENABLE_PQ_MLOAD)
            if(eXRuleType == E_XRULE_HSD)
            {
                MDrv_PQ_Set_MLoadEn(eWindow, TRUE);
            }
#endif
        }

        for(i = 0; i < MDrv_PQ_GetXRuleIPNum(pInstance, eWindow, (MS_U16)eXRuleType); i++)
        {
            MS_BOOL bSDMode;
            bSDMode = _MDrv_PQ_LoadScalingTable(pInstance,
                                                eWindow,
                                                eXRuleType,
                                                i,
                                                bPreV_ScalingDown,
                                                bInterlace,
                                                bColorSpaceYUV,
                                                u16InputSize,
                                                u16SizeAfterScaling);
            bRet |= (bSDMode << (1 * i));
        }

        if(!pstGetDSInfo->bUpdate_DS_CMD)
        {
#if (ENABLE_PQ_MLOAD)
            if(eXRuleType == E_XRULE_HSD)
            {
                MDrv_PQ_Set_MLoadEn(eWindow, FALSE);
            }
#endif
        }

        if(eXRuleType == E_XRULE_VSD)
        {
            bRet &= 0x01;
        }
#if 0
        //121 filter
        if(((MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_LINE_ALTERNATIVE) ||
            (MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_TOP_BOTTOM) ||
            (MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_TOP_BOTTOM_HW)) &&
            (MApi_XC_Get_3D_HW_Version() > 0)&&
            (MDrv_PQ_Get_SensioMode() == TRUE))
        {
            if((u16InputSize >= u16SizeAfterScaling) && (eXRuleType == E_XRULE_VSP))
            {
                //if post V down, we will load 121 filter, and when it's post scaling case
                //reload 121 filter's VSP_Y=SRAM_1_4Tap, VSP_C=C_SRAM_1,
                //                    SRAM1=InvSinc4Tc4p4Fc50Apass01Astop55, C_SRAM1=C121 for main-win
                //reload 121 filter's VSP_Y=SRAM_3_4Tap, VSP_C=C_SRAM_3,
                //                    SRAM3=InvSinc4Tc4p4Fc50Apass01Astop55, C_SRAM3=C121 for sub-win
                MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VSP_Y_SRAM_1_4Tap_Main, PQ_IP_VSP_Y_Main);
                MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VSP_C_C_SRAM_1_Main, PQ_IP_VSP_C_Main);
                MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_SRAM1_InvSinc4Tc4p4Fc50Apass01Astop55_Main, PQ_IP_SRAM1_Main);
                MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_C_SRAM1_C121_Main, PQ_IP_C_SRAM1_Main);

                MDrv_PQ_LoadTable_U2(pInstance,PQ_SUB_WINDOW, PQ_IP_VSP_Y_SRAM_3_4Tap_Sub, PQ_IP_VSP_Y_Sub);
                MDrv_PQ_LoadTable_U2(pInstance,PQ_SUB_WINDOW, PQ_IP_VSP_C_C_SRAM_3_Sub, PQ_IP_VSP_C_Sub);
                MDrv_PQ_LoadTable_U2(pInstance,PQ_SUB_WINDOW, PQ_IP_SRAM3_InvSinc4Tc4p4Fc50Apass01Astop55_Sub, PQ_IP_SRAM3_Sub);
                MDrv_PQ_LoadTable_U2(pInstance,PQ_SUB_WINDOW, PQ_IP_C_SRAM3_C121_Sub, PQ_IP_C_SRAM3_Sub);
            }
        }
#endif
    }
    PQ_FREE_MEM(pstGetDSInfo,sizeof(XC_GET_DS_INFO));

    return bRet;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_LoadScalingTable(PQ_WIN eWindow,
        MS_U8 eXRuleType,
        MS_BOOL bPreV_ScalingDown,
        MS_BOOL bInterlace,
        MS_BOOL bColorSpaceYUV,
        MS_U16 u16InputSize,
        MS_U16 u16SizeAfterScaling)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ, &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
    }

    stPQ_LoadScalingTable PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.eXRuleType = eXRuleType;
    PQArgs.bPreV_ScalingDown = bPreV_ScalingDown;
    PQArgs.bInterlace = bInterlace;
    PQArgs.bColorSpaceYUV = bColorSpaceYUV;
    PQArgs.u16InputSize = u16InputSize;
    PQArgs.u16SizeAfterScaling = u16SizeAfterScaling;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_SCALING_TABLE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

//////////////////////////////////////////////////////////////////
//  set photo YUV standard
//////////////////////////////////////////////////////////////////
void MDrv_PQ_SetPhotoYUVStandard_U2(void* pInstance,PQ_WIN eWindow, PQ_YUV_STD enStd)
{
    _enYUVStandard[eWindow] = enStd;
}
void SYMBOL_WEAK MDrv_PQ_SetPhotoYUVStandard(PQ_WIN eWindow, PQ_YUV_STD enStd)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return;
        }
    }

    stPQ_SetPhotoYUVStandard PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.enStd = enStd;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_PHOTO_YUV_STD, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
//////////////////////////////////////////////////////////////////
// set color range of input source and take effect immediately
//
void MDrv_PQ_SetColorRange_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bColorRange0_255)
{
    _bColorRange0_255[PQ_MAIN_WINDOW] = bColorRange0_255;
    _bColorRange0_255[PQ_SUB_WINDOW] = bColorRange0_255;
    MDrv_PQ_SetCSC_U2(pInstance, eWindow, _gForceColorFmt[eWindow]);
}
void SYMBOL_WEAK MDrv_PQ_SetColorRange(PQ_WIN eWindow, MS_BOOL bColorRange0_255)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return;
        }
    }

    stPQ_SetColorRange PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bColorRange0_255 = bColorRange0_255;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_COLOR_RANGE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

MS_BOOL MDrv_PQ_Get_CSC_XRuleIdx_U2(void* pInstance,MS_BOOL *pbInputColorSpaceRGB,
                                            MS_U16 * pu16DoCSC,
                                            PQ_WIN eWindow,
                                            PQ_FOURCE_COLOR_FMT enForceColor,
                                            MS_BOOL bLoadPQTable)
{
    MS_BOOL bInputTypeVideo = FALSE;

    _gForceColorFmt[eWindow] = enForceColor;

#if ENABLE_VGA_EIA_TIMING
    if ((QM_IsSourceVGA(_enInputSourceType[eWindow]) ||
        QM_IsSourceDVI_HDMIPC(_enInputSourceType[eWindow], eWindow)) && !QM_IsInterlaced(eWindow))
#else
    if(QM_IsSourceVGA(_enInputSourceType[eWindow]) ||
       (QM_IsSourceDVI_HDMIPC(_enInputSourceType[eWindow], eWindow) && !QM_IsInterlaced(eWindow)))
#endif
    {
        PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"PC mode\n");
        bInputTypeVideo = FALSE;

#if (PQ_ENABLE_PIP == 1)
        if((eWindow == PQ_SUB_WINDOW) && MDrv_PQ_GetVGASubCaseForceTo422Mode_U2(pInstance))
        {
            PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"VGA in sub video case, force PC mode to video mode \n");
            bInputTypeVideo = TRUE;
        }
#endif
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"VIDEO mode\n");
        bInputTypeVideo = TRUE;
    }

    if(MDrv_PQ_IsForceVideoInputMode_U2(pInstance, eWindow) == TRUE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"Force VIDEO mode\n");
        bInputTypeVideo = TRUE;
    }

    if( QM_IsSourceVGA(_enInputSourceType[eWindow]) ||
       (QM_IsSourceDVI_HDMIPC(_enInputSourceType[eWindow],eWindow)) ||
       (QM_IsSourceHDMI_Video(_enInputSourceType[eWindow],eWindow) && (QM_HDMIPC_COLORRGB(eWindow))) )
    {
        PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"Input RGB\n");
        (*pbInputColorSpaceRGB) = TRUE;
    }
    else if(IsSourceMultiMediaPhotoMode(eWindow,_enInputSourceType[eWindow]))
    {
        MVOP_Handle stMvopHd = { E_MVOP_MODULE_MAIN };
        MVOP_RgbFormat enRGB = E_MVOP_RGB_NONE;
        if(MDrv_MVOP_GetCommand(&stMvopHd, E_MVOP_CMD_GET_RGB_FMT, & enRGB, sizeof(MVOP_RgbFormat)) == E_MVOP_OK)
        {
            if(enRGB == E_MVOP_RGB_888)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"Input RGB\n");
                (*pbInputColorSpaceRGB) = TRUE;
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"Input YUV\n");
                (*pbInputColorSpaceRGB) = FALSE;
            }
        }
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"Input YUV\n");
        (*pbInputColorSpaceRGB) = FALSE;
    }

#if defined(PQ_XRULE_CSC_ENABLE) && (PQ_XRULE_CSC_ENABLE==1)
    MS_U16 u16XRuleIdx = 0, u16XRuleIP = 0;
    MS_U16 u16IPIdx = 0, u16TabIdx = 0;
    MS_U16 eXRuleType = E_XRULE_CSC;
    MS_BOOL bInputResolutionHD = _gIsSrcHDMode[eWindow];

    PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"VIDEO mode=%u, FourColor=%u, HD%u, InputRGB=%u, _bColorRange0_255=%u\n", bInputTypeVideo, enForceColor, bInputResolutionHD, *pbInputColorSpaceRGB, _bColorRange0_255[eWindow]);
    if (bInputTypeVideo)
    {
        if (*pbInputColorSpaceRGB)
        {
            if (eWindow == PQ_MAIN_WINDOW)
            {
                if (bInputResolutionHD == FALSE)
                {
                    if (_bColorRange0_255[eWindow])
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_Video_YUV_SD_Main : // fource to RGB and input is RGB, hence no need change
                                 PQ_CSCRule_Video_RGB_SD_0_255_Main;
                    }
                    else // bInputDataRange16_235
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_Video_YUV_SD_Main :
                                 PQ_CSCRule_Video_RGB_SD_16_235_Main;
                    }
                }
                else // InputResolutionHD
                {
                    if (_bColorRange0_255[eWindow])
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_Video_YUV_HD_Main :
                                 PQ_CSCRule_Video_RGB_HD_0_255_Main;
                    }
                    else // bInputDataRange16_235
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_Video_YUV_HD_Main :
                                 PQ_CSCRule_Video_RGB_HD_16_235_Main;
                    }
                }
            }
#if PQ_ENABLE_PIP
            else
            {
                if (bInputResolutionHD == FALSE)
                {
                    if (_bColorRange0_255[eWindow])
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_Video_YUV_SD_Sub : // fource to RGB and input is RGB, hence no need change
                                 PQ_CSCRule_Video_RGB_SD_0_255_Sub;
                    }
                    else // bInputDataRange16_235
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_Video_YUV_SD_Sub :
                                 PQ_CSCRule_Video_RGB_SD_16_235_Sub;
                    }
                }
                else // InputResolutionHD
                {
                    if (_bColorRange0_255[eWindow])
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_Video_YUV_HD_Sub :
                                 PQ_CSCRule_Video_RGB_HD_0_255_Sub;
                    }
                    else // bInputDataRange16_235
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_Video_YUV_HD_Sub :
                                 PQ_CSCRule_Video_RGB_HD_16_235_Sub;
                    }
                }
            }
#endif
        }
        else // InputColorSpaceYUV
        {
            if(enForceColor == PQ_FOURCE_COLOR_RGB )
            {
                if(_bP2pForceToDoCsc == FALSE)
                {
                    //PQ_ASSERT(0);
                }
                PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"InputColorSpace is YUV, Force Color Space is RGB!!!\n");
            }

            if (eWindow == PQ_MAIN_WINDOW)
            {
                if (bInputResolutionHD == FALSE)
                {
                    u16XRuleIdx = PQ_CSCRule_Video_YUV_SD_Main;
                }
                else // InputResolutionHD
                {
                    u16XRuleIdx = PQ_CSCRule_Video_YUV_HD_Main;
                }
            }
#if PQ_ENABLE_PIP
            else
            {
                if (bInputResolutionHD == FALSE)
                {
                    u16XRuleIdx = PQ_CSCRule_Video_YUV_SD_Sub;
                }
                else // InputResolutionHD
                {
                    u16XRuleIdx = PQ_CSCRule_Video_YUV_HD_Sub;
                }
            }
#endif
        }
    }
    else // InputTypePC
    {
        if (*pbInputColorSpaceRGB)
        {
            if (eWindow == PQ_MAIN_WINDOW)
            {
                if (bInputResolutionHD == FALSE)
                {
                    if (_bColorRange0_255[eWindow])
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_PC_YUV_SD_Main :
                                 PQ_CSCRule_PC_RGB_SD_0_255_Main;
                    }
                    else // bInputDataRange16_235
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_PC_YUV_SD_Main :
                                 PQ_CSCRule_PC_RGB_SD_16_235_Main;
                    }
                }
                else // InputResolutionHD
                {
                    if (_bColorRange0_255[eWindow])
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_PC_YUV_HD_Main :
                                 PQ_CSCRule_PC_RGB_HD_0_255_Main;
                    }
                    else // bInputDataRange16_235
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_PC_YUV_HD_Main :
                                 PQ_CSCRule_PC_RGB_HD_16_235_Main;
                    }
                }
            }
#if PQ_ENABLE_PIP
            else
            {
                if (bInputResolutionHD == FALSE)
                {
                    if (_bColorRange0_255[eWindow])
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_PC_YUV_SD_Sub :
                                 PQ_CSCRule_PC_RGB_SD_0_255_Sub;
                    }
                    else // bInputDataRange16_235
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_PC_YUV_SD_Sub :
                                 PQ_CSCRule_PC_RGB_SD_16_235_Sub;
                    }
                }
                else // InputResolutionHD
                {
                    if (_bColorRange0_255[eWindow])
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_PC_YUV_HD_Sub :
                                 PQ_CSCRule_PC_RGB_HD_0_255_Sub;
                    }
                    else // bInputDataRange16_235
                    {
                        u16XRuleIdx = enForceColor == PQ_FOURCE_COLOR_RGB ?
                                 PQ_CSCRule_PC_YUV_HD_Sub :
                                 PQ_CSCRule_PC_RGB_HD_16_235_Sub;
                    }
                }
            }
#endif
        }
        else // InputColorSpaceYUV
        {
            if(enForceColor == PQ_FOURCE_COLOR_RGB )
            {
                if(_bP2pForceToDoCsc == FALSE)
                {
                    //PQ_ASSERT(0);
                }
                PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"InputColorSpace is YUV, Force Color Space is RGB!!!\n");
            }

            if(eWindow == PQ_MAIN_WINDOW)
            {
                if (bInputResolutionHD == FALSE)
                {
                    u16XRuleIdx = PQ_CSCRule_PC_YUV_SD_Main;
                }
                else // InputResolutionHD
                {
                    u16XRuleIdx = PQ_CSCRule_PC_YUV_HD_Main;
                }
            }
#if PQ_ENABLE_PIP
            else
            {
                if (bInputResolutionHD == FALSE)
                {
                    u16XRuleIdx = PQ_CSCRule_PC_YUV_SD_Sub;
                }
                else // InputResolutionHD
                {
                    u16XRuleIdx = PQ_CSCRule_PC_YUV_HD_Sub;
                }
            }
#endif
        }
    }

    for(u16XRuleIP=0; u16XRuleIP<MDrv_PQ_GetXRuleIPNum(pInstance, eWindow, eXRuleType); u16XRuleIP++)
    {
        u16IPIdx = MDrv_PQ_GetXRuleIPIndex(pInstance, eWindow, eXRuleType, u16XRuleIP, PQ_XC0_STD);
        u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, eWindow, eXRuleType, u16XRuleIdx, u16XRuleIP, PQ_XC0_STD);
        //for ptp, no csc
        if(MDrv_PQ_Get_PointToPoint_U2(pInstance, eWindow))
        {
            if(_bP2pForceToDoCsc == FALSE)
            {
                u16TabIdx = 0x00; //PQ_IP_CSC_OFF_Main and PQ_IP_VIP_CSC_OFF_Main
            }
            PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"PointToPoint \n");
        }
        PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"u16XRuleIdx:%u, u16XRuleIP=%u, IPIdx=%u, TabIdx=%u\n", u16XRuleIdx, u16XRuleIP, u16IPIdx, u16TabIdx);

        if(bLoadPQTable)
        {
#ifdef PQ_CFD_INFO
#else
            MDrv_PQ_LoadTable_U2(pInstance, eWindow, u16TabIdx, u16IPIdx);
#endif
            //if(u16XRuleIP==0)
            //    MDrv_PQ_LoadTable_U2(pInstance,eWindow, 0, 6);
            //else if(u16XRuleIP==1)
            //    MDrv_PQ_LoadTable_U2(pInstance,eWindow, 5, 122);
            //else
            //    MDrv_PQ_LoadTable_U2(pInstance,eWindow, u16TabIdx, u16IPIdx);
        }

        if(u16TabIdx)
        {
#if PQ_HAL_EWINDOW_PARAM
            if(u16IPIdx == Hal_PQ_get_csc_ip_idx(pInstance,MS_CSC_IP_CSC,eWindow))
#else
            if(u16IPIdx == Hal_PQ_get_csc_ip_idx(pInstance,MS_CSC_IP_CSC))
#endif
            {
                (*pu16DoCSC) |= 0x1;
            }
#if PQ_HAL_EWINDOW_PARAM
            else if(u16IPIdx == Hal_PQ_get_csc_ip_idx(pInstance,MS_CSC_IP_VIP_CSC,eWindow))
#else
            else if(u16IPIdx == Hal_PQ_get_csc_ip_idx(pInstance,MS_CSC_IP_VIP_CSC))
#endif
            {
                (*pu16DoCSC) |= 0x02;
            }
            else
            {
                (*pu16DoCSC) |= 0x00;
            }
        }
    }
#else
    UNUSED(bInputTypeVideo);
#endif

    return true;
}
MS_BOOL MDrv_PQ_Get_CSC_XRuleIdx(MS_BOOL *pbInputColorSpaceRGB,
                                            MS_U16 * pu16DoCSC,
                                            PQ_WIN eWindow,
                                            PQ_FOURCE_COLOR_FMT enForceColor,
                                            MS_BOOL bLoadPQTable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Get_CSC_XRuleIdx PQArgs;
    PQArgs.pbInputColorSpaceRGB = pbInputColorSpaceRGB;
    PQArgs.pu16DoCSC = pu16DoCSC;
    PQArgs.eWindow = eWindow;
    PQArgs.enForceColor = enForceColor;
    PQArgs.bLoadPQTable = bLoadPQTable;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_CSC_XRULEIDX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

//////////////////////////////////////////////////////////////////
// get memory color format
// Note: return true if CSC is TRUE
// this case is for video rgb csc case
// return:
//        TRUE: use YUV color space, by front-end csc
//        FALSE: otherwise
MS_BOOL MDrv_PQ_Get_MemYUVFmt_U2(void* pInstance,PQ_WIN eWindow, PQ_FOURCE_COLOR_FMT enForceColor)
{
    MS_U16 u16DoCSC = 0;
    MS_BOOL bInputColorSpaceRGB;
    MS_BOOL bMemYUV;
    MS_BOOL bForceRGB = FALSE;

    MDrv_PQ_Get_CSC_XRuleIdx_U2(pInstance, &bInputColorSpaceRGB, &u16DoCSC, eWindow, enForceColor, FALSE);
    bForceRGB = MDrv_PQ_Get_PointToPoint_U2(pInstance, eWindow);

    if(bInputColorSpaceRGB)
    {
        if (bForceRGB)
        {
            bMemYUV = FALSE;
        }
        else if((u16DoCSC & 0x01) || (_bCFDDoCSC))
        {
            bMemYUV = TRUE;
        }
        else
        {
            bMemYUV = FALSE;
        }
    }
    else
    {
        bMemYUV = TRUE;
    }
    PQ_LEVEL_PRINTF(PQ_DBG_MADI,"MEM is use %s\n",bMemYUV?("YUV"):("RGB"));
    return bMemYUV;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Get_MemYUVFmt(PQ_WIN eWindow, PQ_FOURCE_COLOR_FMT enForceColor)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return PQ_DEINT_OFF;
        }
    }

    stPQ_Get_MemYUVFmt PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.enForceColor = enForceColor;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_MEM_YUVFMT, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}


//////////////////////////////////////////////////////////////////
// do color space conversion from RGB to YUV according to input sourcce and context
// Note: return true if CSC or VIP_CSC is TRUE
// this case is for (video rgb and pc rgb) csc case
// return:
//        TRUE: use YUV color space, for front-end and back-end csc both
//        FALSE: otherwise
//
MS_BOOL MDrv_PQ_SetCSC_U2(void* pInstance,PQ_WIN eWindow, PQ_FOURCE_COLOR_FMT enForceColor)
{
    MS_U16 u16DoCSC = 0;
    MS_BOOL bInputColorSpaceRGB;
    MS_BOOL bForceRGB = FALSE;
    XC_ApiStatus stXCApiStatus;

    memset(&stXCApiStatus, 0, sizeof(XC_ApiStatus));

    if (MApi_XC_GetStatus(&stXCApiStatus, MAIN_WINDOW) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"Get RGB bypass from P2P\n");
        bForceRGB = MDrv_PQ_Get_PointToPoint_U2(pInstance, eWindow);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"Get RGB bypass from XC status\n");
        bForceRGB = stXCApiStatus.bForceRGBin;
    }

    MDrv_PQ_Get_CSC_XRuleIdx_U2(pInstance, &bInputColorSpaceRGB, &u16DoCSC, eWindow, enForceColor, TRUE);

    u16DoCSC = (u16DoCSC != 0);
    if ((bInputColorSpaceRGB && u16DoCSC) || (!bInputColorSpaceRGB) || (!bForceRGB))
    {
        _bCFDDoCSC = TRUE;
        PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"Use YUV Space\n");
        return TRUE;    // use YUV space
    }
    else
    {
        _bCFDDoCSC = FALSE;
        PQ_LEVEL_PRINTF(PQ_DBG_CSCRULE,"Use RGB Space\n");
        return FALSE;   // use RGB space
    }
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_SetCSC(PQ_WIN eWindow, PQ_FOURCE_COLOR_FMT enForceColor)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
    }

    stPQ_SetCSC PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.enForceColor = enForceColor;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_CSC, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

#if (PQ_XRULE_DB_ENABLE == 1)
void MDrv_PQ_Set_DBRule_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16MADi_Idx)
{
    MS_U16 eXRuleType = E_XRULE_CSC;
    MS_U16 u16XRuleIdx, u16XRuleIP;
    MS_U16 u16IPIdx, u16TabIdx;

    eXRuleType = (_stMode_Info[eWindow].u16input_vfreq > 550) ? E_XRULE_DB_NTSC : E_XRULE_DB_PAL;

    if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_24_4R) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_24_4R_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_24_2R) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_24_2R_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_4R_MC) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_25_4R_MC_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_4R) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_25_4R_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_2R) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_25_2R_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_27_4R) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_27_4R_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_27_2R) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_27_2R_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE8) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_P_MODE8_Main;
    }
#if (PQ_P_MODE8_3FRAME_MODE_ENABLE == 1)
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE8_3Frame) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_P_MODE8_Main;
    }
#endif

    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE10) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_P_MODE10_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MOT8) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_P_MODE_MOT8_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MOT10) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_P_MODE_MOT10_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_24_4R_880) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_24_4R_880_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_24_2R_880) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_24_2R_880_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_6R_MC) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_25_6R_MC_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_14F_6R_MC) == u16MADi_Idx)   //Add New MADi mode
    {
        u16XRuleIdx = PQ_DBRule_NTSC_25_14F_6R_MC_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_4R_MC_NW) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_25_4R_MC_NW_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_6R_MC_NW) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_25_6R_MC_NW_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_4R_884) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_25_4R_884_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_2R_884) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_25_2R_884_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_4R_880) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_24_4R_880_Main;
    }
    else if(Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_2R_880) == u16MADi_Idx)
    {
        u16XRuleIdx = PQ_DBRule_NTSC_24_2R_880_Main;
    }
    else
    {
        u16XRuleIdx = 0xFF;
    }

    for(u16XRuleIP = 0; u16XRuleIP < MDrv_PQ_GetXRuleIPNum(pInstance, eWindow, eXRuleType); u16XRuleIP++)
    {
        if(u16XRuleIdx == 0xFF)
            continue;

        u16IPIdx = MDrv_PQ_GetXRuleIPIndex(pInstance, eWindow, eXRuleType, u16XRuleIP, PQ_XC0_STD);
        u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, eWindow, eXRuleType, u16XRuleIdx, u16XRuleIP, PQ_XC0_STD);
        //(printf("u16XRuleIdx:%u, u16XRuleIP=%u, IPIdx=%u, TabIdx=%u\n", u16XRuleIdx, u16XRuleIP, u16IPIdx, u16TabIdx));

        MDrv_PQ_LoadTable_U2(pInstance, eWindow, u16TabIdx, u16IPIdx);
    }

}
void MDrv_PQ_Set_DBRule(PQ_WIN eWindow, MS_U16 u16MADi_Idx)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_Set_DBRule PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16MADi_Idx = u16MADi_Idx;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_DBRULE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif

void _MDrv_PQ_Set_MM_DS_XRule(void* pInstance, PQ_WIN eWindow, MS_U16 u16V_Size, MS_U16 u16H_Size, MS_U8 u8FrameCount, MS_BOOL bInterlace)
{
#if PQ_XRULE_MM_DS_ENABLE

    MS_U16 u16IPIdx = 0;
    MS_U16 u16TabIdx = (gbPQBinEnable == FALSE) ? PQ_IP_NULL : PQ_BIN_IP_NULL;
    MS_U16 u16XRuleIdx = 0x00;
    MS_U16 eXRuleType = E_XRULE_MM_DS;
    MS_U16 u16XRuleIP = 0;
    MS_BOOL bCheckGRule = FALSE;
    PQ_ENGINE_TYPE ePQEngineType = PQ_XC0_STD;
    MS_U8 u8XRuleOPStart = 0;
    MS_U8 u8XRuleUCMStart = 0;

    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"V_Size=%d, H_Size=%d, u8FrameCount=%d, bInterlace=%d\n", u16V_Size, u16H_Size, u8FrameCount, bInterlace);

    //check input valid
    if ((eWindow >= PQ_MAX_WINDOW) || (u16V_Size <= 0) || (u16H_Size <= 0))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"Invalid input:eWindow=%d, V_Size=%d, H_Size=%d, u8FrameCount=%d\n", eWindow, u16V_Size, u16H_Size, u8FrameCount);
        return;
    }

    //check PQbin support Xrule_MM_DS
    if (stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16XRuleNum <= E_XRULE_MM_DS)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"PQbin and header file is mismatch, Please update PQbin!  u16XRuleNum(PQnin)=%d, u16XRuleNum(header)=%d \n", stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16XRuleNum, E_XRULE_MM_DS);
        return;
    }

    //Set u8XRuleIP
    u16XRuleIP = u8FrameCount;
    u8XRuleOPStart = u16XRuleIP + E_DS_XRULE_FRAME_NUM;
    u8XRuleUCMStart = u8XRuleOPStart + E_DS_XRULE_FRAME_NUM;
#if (defined(PQ_MM_DS_RULE_H_SD_I_MAIN_EX) && defined(PQ_MM_DS_RULE_H_FHD_I_MAIN_EX) && defined(PQ_MM_DS_RULE_4K_1K_I_MAIN_EX))
    ePQEngineType = PQ_XC0_EXT;
    eXRuleType = E_XRULE_EX_MM_DS;
    if(u16H_Size > PQ_4K2K_XRULE_DS_H_MINSIZE) // For H=4096 case
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_H_FHD_P_Main_Ex: PQ_MM_DS_rule_H_FHD_I_Main_Ex;
    }
    else if(u16H_Size == PQ_4K2K_XRULE_DS_H_MINSIZE) // 4k
    {
        if(u16V_Size == PQ_4K2K_XRULE_DS_V_MINSIZE) // 4k2k
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_4K_P_Main_Ex: PQ_MM_DS_rule_4K_I_Main_Ex;
        }
        else // 4k1k
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_4K_1K_P_Main_Ex: PQ_MM_DS_rule_4K_1K_I_Main_Ex;
        }
    }
    else if((u16H_Size > PQ_FHD_XRULE_DS_H_MAXSIZE) && (u16H_Size < PQ_4K2K_XRULE_DS_H_MINSIZE)) // H_FHD
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_H_FHD_P_Main_Ex: PQ_MM_DS_rule_H_FHD_I_Main_Ex;
    }
    else if(u16H_Size <= PQ_FHD_XRULE_DS_H_MAXSIZE)
    {
        if(u16V_Size > PQ_FHD_XRULE_DS_V_MAXSIZE) // H_FHD
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_H_FHD_P_Main_Ex: PQ_MM_DS_rule_H_FHD_I_Main_Ex;
        }
        else if((u16V_Size >= PQ_FHD_XRULE_DS_V_MINSIZE) && (u16V_Size <= PQ_FHD_XRULE_DS_V_MAXSIZE)) // FHD
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_FHD_P_Main_Ex: PQ_MM_DS_rule_FHD_I_Main_Ex;
        }
        else if((u16V_Size >= PQ_HSD_XRULE_DS_V_MINSIZE) && (u16V_Size < PQ_FHD_XRULE_DS_V_MINSIZE)) // H_SD
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_H_SD_P_Main_Ex: PQ_MM_DS_rule_H_SD_I_Main_Ex;
        }
        else if((u16V_Size >= PQ_SD_XRULE_DS_V_MINSIZE) && (u16V_Size < PQ_HSD_XRULE_DS_V_MINSIZE)) // SD
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_SD_P_Main_Ex: PQ_MM_DS_rule_SD_I_Main_Ex;
        }
        else // L_SD
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_L_SD_P_Main_Ex: PQ_MM_DS_rule_L_SD_I_Main_Ex;
        }
    }
#elif defined(PQ_MM_DS_RULE_4K_P_MAIN_EX)
    //Get u8XRuleIdx
    ePQEngineType = PQ_XC0_EXT;
    eXRuleType = E_XRULE_EX_MM_DS;
    if(u16V_Size >=2160) //4//4k2k
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_4K_P_Main_Ex: PQ_MM_DS_rule_4K_I_Main_Ex;
    }
    else if( (u16V_Size < 2160) && (u16V_Size >= 720) ) //FHD
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_FHD_P_Main_Ex: PQ_MM_DS_rule_FHD_I_Main_Ex;
    }
    else if( (u16V_Size < 720) && (u16V_Size >= 480) ) //SD
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_SD_P_Main_Ex: PQ_MM_DS_rule_SD_I_Main_Ex;
    }
    else if( (u16V_Size < 480) && (u16V_Size >= 0) ) //LSD
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_L_SD_P_Main_Ex: PQ_MM_DS_rule_L_SD_I_Main_Ex;
    }
#elif (defined(PQ_MM_DS_RULE_H_SD_I_MAIN) && defined(PQ_MM_DS_RULE_H_FHD_I_MAIN) && defined(PQ_MM_DS_RULE_4K_1K_I_MAIN))
    if(u16H_Size > PQ_4K2K_XRULE_DS_H_MINSIZE) // For H=4096 case
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_H_FHD_P_Main : PQ_MM_DS_rule_H_FHD_I_Main;
    }
    else if(u16H_Size == PQ_4K2K_XRULE_DS_H_MINSIZE) // 4k
    {
        if(u16V_Size == PQ_4K2K_XRULE_DS_V_MINSIZE) // 4k2k
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_4K_P_Main : PQ_MM_DS_rule_4K_I_Main;
        }
        else // 4k1k
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_4K_1K_P_Main : PQ_MM_DS_rule_4K_1K_I_Main;
        }
    }
    else if((u16H_Size > PQ_FHD_XRULE_DS_H_MAXSIZE) && (u16H_Size < PQ_4K2K_XRULE_DS_H_MINSIZE)) // H_FHD
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_H_FHD_P_Main : PQ_MM_DS_rule_H_FHD_I_Main;
    }
    else if(u16H_Size <= PQ_FHD_XRULE_DS_H_MAXSIZE)
    {
        if(u16V_Size > PQ_FHD_XRULE_DS_V_MAXSIZE) // H_FHD
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_H_FHD_P_Main : PQ_MM_DS_rule_H_FHD_I_Main;
        }
        else if((u16V_Size >= PQ_FHD_XRULE_DS_V_MINSIZE) && (u16V_Size <= PQ_FHD_XRULE_DS_V_MAXSIZE)) // FHD
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_FHD_P_Main : PQ_MM_DS_rule_FHD_I_Main;
        }
        else if((u16V_Size >= PQ_HSD_XRULE_DS_V_MINSIZE) && (u16V_Size < PQ_FHD_XRULE_DS_V_MINSIZE)) // H_SD
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_H_SD_P_Main : PQ_MM_DS_rule_H_SD_I_Main;
        }
        else if((u16V_Size >= PQ_SD_XRULE_DS_V_MINSIZE) && (u16V_Size < PQ_HSD_XRULE_DS_V_MINSIZE)) // SD
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_SD_P_Main : PQ_MM_DS_rule_SD_I_Main;
        }
        else // L_SD
        {
            u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_L_SD_P_Main : PQ_MM_DS_rule_L_SD_I_Main;
        }
    }
#else
    //Get u8XRuleIdx
    if(u16V_Size >=2160) //4//4k2k
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_4K_P_Main : PQ_MM_DS_rule_4K_I_Main;
    }else if( (u16V_Size < 2160) && (u16V_Size >= 720) ) //FHD
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_FHD_P_Main : PQ_MM_DS_rule_FHD_I_Main;
    }else if( (u16V_Size < 720) && (u16V_Size >= 480) ) //SD
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_SD_P_Main : PQ_MM_DS_rule_SD_I_Main;
    }
    else if( (u16V_Size < 480) && (u16V_Size >= 0) ) //LSD
    {
        u16XRuleIdx = (bInterlace == 0) ? PQ_MM_DS_rule_L_SD_P_Main : PQ_MM_DS_rule_L_SD_I_Main;
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"MM PQ DS u8XRuleIdx is invalid!!\n");
        return;
    }

    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"u8XRuleIdx=%u \n", u16XRuleIdx);

    u16IPIdx  = MDrv_PQ_GetXRuleIPIndex(pInstance, eWindow, eXRuleType, u16XRuleIP, ePQEngineType);
    u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, eWindow, eXRuleType, u16XRuleIdx, u16XRuleIP, ePQEngineType);
#if defined(PQ_SUPPORT_DS_MODULE_IP_OP)&&(PQ_SUPPORT_DS_MODULE_IP_OP == 1)
    bCheckGRule = (u16XRuleIP == E_DS_XRULE_FRAME_CHECK_GRULE) ? TRUE : FALSE;//the second last
#endif

    if (((gbPQBinEnable == TRUE)  && (u16TabIdx == PQ_BIN_IP_NULL)) ||
        ((gbPQBinEnable == FALSE) && (u16TabIdx == PQ_IP_NULL)))
    {
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, eWindow, u16IPIdx, ePQEngineType);
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(d)tabidx=%u\n", u16TabIdx);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(e)tabidx=%u\n", u16TabIdx);
    }

    MDrv_PQBin_Set_MLoadEn(pInstance, TRUE);
    MDrv_PQBin_CheckGRule(bCheckGRule);
    MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx, u16IPIdx, ePQEngineType);
    MDrv_PQBin_CheckGRule(FALSE);

#if defined(PQ_SUPPORT_DS_MODULE_IP_OP)&&(PQ_SUPPORT_DS_MODULE_IP_OP == 1)
    u16IPIdx  = MDrv_PQ_GetXRuleIPIndex(pInstance, eWindow, eXRuleType, u8XRuleOPStart, ePQEngineType);
    u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, eWindow, (MS_U16)eXRuleType, (MS_U16)u16XRuleIdx, (MS_U16)u8XRuleOPStart, ePQEngineType);

    if(((gbPQBinEnable == TRUE)  && (u16TabIdx == PQ_BIN_IP_NULL)) ||
       ((gbPQBinEnable == FALSE) && (u16TabIdx == PQ_IP_NULL)))
    {
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, eWindow, u16IPIdx, ePQEngineType);
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(d)tabidx=%u\n", u16TabIdx);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(e)tabidx=%u\n", u16TabIdx);
    }

    MDrv_PQBin_CheckGRule(bCheckGRule);
    MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx, u16IPIdx, ePQEngineType);
    MDrv_PQBin_CheckGRule(FALSE);

#if defined(PQ_SUPPORT_DS_MODULE_UCM)&&(PQ_SUPPORT_DS_MODULE_UCM == 1)
    u16IPIdx  = MDrv_PQ_GetXRuleIPIndex(pInstance, eWindow, eXRuleType, u8XRuleUCMStart, ePQEngineType);
    u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, eWindow, (MS_U16)eXRuleType, (MS_U16)u16XRuleIdx, (MS_U16)u8XRuleUCMStart, ePQEngineType);

    if(((gbPQBinEnable == TRUE)  && (u16TabIdx == PQ_BIN_IP_NULL)) ||
       ((gbPQBinEnable == FALSE) && (u16TabIdx == PQ_IP_NULL)))
    {
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, eWindow, u16IPIdx, ePQEngineType);
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(d)tabidx=%u\n", u16TabIdx);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(e)tabidx=%u\n", u16TabIdx);
    }

    MDrv_PQBin_CheckGRule(bCheckGRule);
    MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx, u16IPIdx, ePQEngineType);
    MDrv_PQBin_CheckGRule(FALSE);
#endif
#endif
    MDrv_PQBin_Set_MLoadEn(pInstance, FALSE);

#endif
}

void _MDrv_PQ_Reference_Callback(SC_INT_SRC enIntNum, void *pParam)
{
#if PQ_XRULE_MM_DS_ENABLE

#if FAST_DISP_TIME_LOG
    printf("Fast Disp, time = %u\n",(unsigned int)(MsOS_GetSystemTime() - _u32CBtime));
#endif

    PQ_WIN eWindow = PQ_MAIN_WINDOW;
    MS_U16 u16XRuleFrame;

    // 1. de-register callback
    MApi_XC_InterruptDeAttach(SC_INT_F2_FRAME_RDY, _MDrv_PQ_Reference_Callback, pParam);

    // 2. disable fast display hw setting
    if(FALSE == Hal_PQ_SupportFastDisp_Setting(DISABLE, 0, 0))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;32m NOT support E_XC_HW_FAST_DISP!!! bHWSupportFastDisp = FALSE\32[m\n");
        return;
    }

    // 3. Enable PQ reference
    MS_U16 u16Input_HSize = QM_GetInputHSize(eWindow);
    MS_U16 u16Input_VSize = QM_GetInputVSize(eWindow);
    MS_BOOL bIsInterlaced = QM_IsInterlaced(eWindow);

    for(u16XRuleFrame = E_DS_XRULE_FRAME_1; u16XRuleFrame < E_DS_XRULE_FRAME_NUM; u16XRuleFrame++)
    {
        _MDrv_PQ_Set_MM_DS_XRule(pParam, eWindow, u16Input_VSize, u16Input_HSize, u16XRuleFrame, bIsInterlaced);
    }

#endif
}

void MDrv_PQ_Set_FastDisplay_U2(void* pInstance, PQ_WIN eWindow, MS_BOOL bEnable, MS_U8 u8Level, MS_U8 u8ValidFieldNum)
{
#if PQ_XRULE_MM_DS_ENABLE

    if (eWindow != PQ_MAIN_WINDOW)  // only main PQ DS XRule
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;32m Only Support MAIN_WINDOW, eWindow = %d !!!\32[m\n", eWindow);
        return;
    }

    XC_PQ_FAST_DISP_SUPPORTED_CAPS stCaps;
    memset(&stCaps, 0, sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS));
    stCaps.u32Version = ST_XC_PQ_FAST_DISP_VERSION;
    stCaps.u32Length = sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS);
    stCaps.enPQFastDispType = E_PQ_FAST_DISP_ZEROPQ;
    stCaps.bSupported = FALSE;
    MApi_XC_GetChipCaps(E_XC_HW_FAST_DISP, (MS_U32*)&stCaps, stCaps.u32Length);

    if (FALSE == stCaps.bSupported)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;32m NOT support E_XC_HW_FAST_DISP!!! bHWSupportFastDisp = %d\32[m\n", stCaps.bSupported);
        return;
    }

    if(MDrv_PQ_IsFBLMadi(pInstance, eWindow))
    { // fast display need r/w diff >= 1, in FBL case no need fast display and can't use fast display
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "[PQ] FBL no need fast display\n");
        return;
    }

    if (TRUE == bEnable)
    {
        //1.Close PQ reference and setting for HW
        MS_U16 u16Input_HSize = QM_GetInputHSize(eWindow);
        MS_U16 u16Input_VSize = QM_GetInputVSize(eWindow);
        MS_BOOL bIsInterlaced = QM_IsInterlaced(eWindow);

        _MDrv_PQ_Set_MM_DS_XRule(pInstance, eWindow, u16Input_VSize, u16Input_HSize, E_DS_XRULE_FRAME_0, bIsInterlaced);

        // Valid field num can't be larger than rw diff, or ISR can't work
        MS_U8 u8FBNum = MApi_XC_GetWRBankMappingNum(MDrv_PQ_PQtoSC_WinMapping(eWindow));
        if (u8ValidFieldNum > u8FBNum)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;32m u8ValidFieldNum is larger than rw diff num !!! u8ValidFieldNum = %d\32[m\n", u8ValidFieldNum);
            u8ValidFieldNum = u8FBNum;
        }

        //2. enable fast display hw setting
        if (FALSE == Hal_PQ_SupportFastDisp_Setting(ENABLE, u8Level, u8ValidFieldNum))
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;32m NOT support E_XC_HW_FAST_DISP!!! bHWSupportFastDisp = %d\32[m\n", stCaps.bSupported);
            return;
        }

        if (TRUE == MApi_XC_InterruptIsAttached(SC_INT_F2_FRAME_RDY, _MDrv_PQ_Reference_Callback, pInstance))
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;32m Already register CB, De-Attach it first !!!\32[m\n");
            // if already registered, De-Attach first
            MApi_XC_InterruptDeAttach(SC_INT_F2_FRAME_RDY, _MDrv_PQ_Reference_Callback, pInstance);
        }

#if FAST_DISP_TIME_LOG
        _u32CBtime = MsOS_GetSystemTime();
#endif

        //3. register PQ reference callback
        MApi_XC_InterruptAttach(SC_INT_F2_FRAME_RDY, _MDrv_PQ_Reference_Callback, pInstance);
    }

    else
    {
        if (TRUE == MApi_XC_InterruptIsAttached(SC_INT_F2_FRAME_RDY, _MDrv_PQ_Reference_Callback, pInstance))
        {
            // 1. de-register callback
            MApi_XC_InterruptDeAttach(SC_INT_F2_FRAME_RDY, _MDrv_PQ_Reference_Callback, pInstance);
        }

        // 2. disable fast display hw setting
        if(FALSE == Hal_PQ_SupportFastDisp_Setting(DISABLE, 0, 0))
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;32m NOT support E_XC_HW_FAST_DISP!!! bHWSupportFastDisp = %d\32[m\n", stCaps.bSupported);
            return;
        }
    }
#endif
}

MS_BOOL MDrv_PQ_Get_FastDisplay_OnOff_U2(void* pInstance, PQ_WIN eWindow)
{
    MS_BOOL bReturnValue = FALSE;

    if (eWindow != PQ_MAIN_WINDOW)  // only main PQ DS XRule
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;32m Only Support MAIN_WINDOW, eWindow = %d !!!\32[m\n", eWindow);
        return FALSE;
    }

    XC_PQ_FAST_DISP_SUPPORTED_CAPS stCaps;
    memset(&stCaps, 0, sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS));
    stCaps.u32Version = ST_XC_PQ_FAST_DISP_VERSION;
    stCaps.u32Length = sizeof(XC_PQ_FAST_DISP_SUPPORTED_CAPS);
    stCaps.enPQFastDispType = E_PQ_FAST_DISP_ZEROPQ;
    stCaps.bSupported = FALSE;
    MApi_XC_GetChipCaps(E_XC_HW_FAST_DISP, (MS_U32*)&stCaps, stCaps.u32Length);

    if (FALSE == stCaps.bSupported)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;32m NOT support E_XC_HW_FAST_DISP!!! bHWSupportFastDisp = %d\32[m\n", stCaps.bSupported);
        return FALSE;
    }

    Hal_PQ_get_FastDisp_en(&bReturnValue);

    return bReturnValue;
}

void MDrv_PQ_EnableMADIForce_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bFullMotion)
{
#if PQ_ENABLE_FORCE_MADI
    if(bFullMotion)
    {
        if(PQ_MAIN_WINDOW == eWindow)
        {
            MDrv_PQ_LoadTable_U2(pInstance, eWindow, PQ_IP_MADi_Force_YC_FullMotion_Main, PQ_IP_MADi_Force_Main);
        }
//#if (ENABLE_LITE_SN == 0)
#if (PQ_ENABLE_PIP)
        else if( (PQ_SUB_WINDOW == eWindow) && ( _upLayerConfig & E_PQ_MISC_A_LITESN_ENABLE ) )
        {
            MDrv_PQ_LoadTable_U2(pInstance, eWindow, MHal_PQ_Get_TableIndex(eWindow, E_PQ_TABLE_IP_MADI_FORCE_YC_FULLMOTION), MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI_FORCE));
        }
#endif
//#endif
    }
    else
    {
        if(PQ_MAIN_WINDOW == eWindow)
        {
            MDrv_PQ_LoadTable_U2(pInstance, eWindow, PQ_IP_MADi_Force_YC_FullStill_Main, PQ_IP_MADi_Force_Main);
        }
//#if (ENABLE_LITE_SN == 0)
#if (PQ_ENABLE_PIP)
        else if( (PQ_SUB_WINDOW == eWindow) && ( _upLayerConfig & E_PQ_MISC_A_LITESN_ENABLE ) )
        {
            MDrv_PQ_LoadTable_U2(pInstance, eWindow, MHal_PQ_Get_TableIndex(eWindow, E_PQ_TABLE_IP_MADI_FORCE_YC_FULLSTILL), MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI_FORCE));
        }
#endif
//#endif
    }
#else
UNUSED(eWindow);
UNUSED(bFullMotion);
#endif

}
void SYMBOL_WEAK MDrv_PQ_EnableMADIForce(PQ_WIN eWindow, MS_BOOL bFullMotion)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_EnableMADIForce PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bFullMotion = bFullMotion;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_ENABLE_MADI_FORCE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_ReduceBW_ForPVR_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bPVR_On)
{
    UNUSED(eWindow);
    UNUSED(bPVR_On);
}
void SYMBOL_WEAK MDrv_PQ_ReduceBW_ForPVR(PQ_WIN eWindow, MS_BOOL bPVR_On)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_ReduceBW_ForPVR PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bPVR_On = bPVR_On;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_REDUCE_BW_FOR_PVR, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void _MDrv_PQ_Set_MVOP_UVShift(MS_BOOL bEnable)
{
    MS_U8 regval;

    regval = MApi_XC_ReadByte(VOP_MPG_JPG_SWITCH);

    if(((regval & 0x10) == 0x10) && ((regval & 0x3) == 0x2))
    {
        // 422 with MCU control mode
        if(bEnable)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
            PQ_ASSERT(0);
        }
    }

    // output 420 and interlace
    //[IP - Sheet] : Main Page --- 420CUP
    //[Project] :  Titania2
    //[Description]:   Chroma artifacts when 420to422 is applied duplicate method.
    //[Root cause]: Apply 420to422 average algorithm to all DTV input cases.
    //The average algorithm must cooperate with MVOP.
    MApi_XC_WriteByteMask(VOP_UV_SHIFT, (bEnable) ? 1 : 0, 0x3);
}

void MDrv_PQ_Set420upsampling_U2(void* pInstance,
                              PQ_WIN eWindow,
                              MS_BOOL bFBL,
                              MS_BOOL bPreV_ScalingDown,
                              MS_U16 u16V_CropStart)
{
    MS_U16 u16TabIdx;

    u16TabIdx = MDrv_PQ_GetTableIndex_U2(pInstance, eWindow, PQ_IP_420CUP_Main);

    PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"[PQ_Set420upsampling]: SrcType:%u, FBL:%u, PreV down:%u, V_CropStart:%u, u8TabIdx=%u, ",
                     _u16PQSrcType[eWindow], bFBL, bPreV_ScalingDown, u16V_CropStart, u16TabIdx);

#if PQ_HAL_EWINDOW_PARAM
    if((u16TabIdx == Hal_PQ_get_420_cup_idx(pInstance,MS_420_CUP_ON, eWindow)) && (!bPreV_ScalingDown) && (!bFBL))
#else
    if((u16TabIdx == Hal_PQ_get_420_cup_idx(pInstance,MS_420_CUP_ON)) && (!bPreV_ScalingDown) && (!bFBL))
#endif
    {
        PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"UVShift: on\n");
        _MDrv_PQ_Set_MVOP_UVShift(ENABLE);
#if PQ_HAL_EWINDOW_PARAM
        if(eWindow == PQ_MAIN_WINDOW)
        {
            MDrv_PQ_LoadTable_U2(pInstance,
                                eWindow, Hal_PQ_get_420_cup_idx(pInstance,MS_420_CUP_ON, eWindow),PQ_IP_420CUP_Main);
        }
#if (PQ_ENABLE_PIP)
        else
        {
            MDrv_PQ_LoadTable_U2(pInstance,eWindow, Hal_PQ_get_420_cup_idx(pInstance,MS_420_CUP_ON, eWindow),MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_420CUP));
        }
#endif

#else
        MDrv_PQ_LoadTable_U2(pInstance, eWindow, Hal_PQ_get_420_cup_idx(pInstance,MS_420_CUP_ON), PQ_IP_420CUP_Main);
#endif
    }
    else    // P mode should not do UV shift
    {
        PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"UVShift: off\n");
        _MDrv_PQ_Set_MVOP_UVShift(DISABLE);
#if PQ_HAL_EWINDOW_PARAM
if(eWindow == PQ_MAIN_WINDOW)
{
    MDrv_PQ_LoadTable_U2(pInstance,
                        eWindow, Hal_PQ_get_420_cup_idx(pInstance,MS_420_CUP_OFF, eWindow),PQ_IP_420CUP_Main);
}
#if (PQ_ENABLE_PIP)
else
{
    MDrv_PQ_LoadTable_U2(pInstance,eWindow, Hal_PQ_get_420_cup_idx(pInstance,MS_420_CUP_OFF, eWindow),MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_420CUP));
}
#endif
#else
        MDrv_PQ_LoadTable_U2(pInstance, eWindow, Hal_PQ_get_420_cup_idx(pInstance,MS_420_CUP_OFF), PQ_IP_420CUP_Main);
#endif
    }

    if((u16V_CropStart & 0x3) == 0)       // crop lines are multiple of 4
        Hal_PQ_set_420upSample(pInstance,0x6666);
    else if((u16V_CropStart & 0x1) == 0)  // crop lines are multiple of 2
        Hal_PQ_set_420upSample(pInstance,0x9999);
//    else
//        PQ_ASSERT(0);

}
void SYMBOL_WEAK MDrv_PQ_Set420upsampling(PQ_WIN eWindow,
                              MS_BOOL bFBL,
                              MS_BOOL bPreV_ScalingDown,
                              MS_U16 u16V_CropStart)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return;
        }
    }

    stPQ_Set420upsampling PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bFBL = bFBL;
    PQArgs.bPreV_ScalingDown = bPreV_ScalingDown;
    PQArgs.u16V_CropStart = u16V_CropStart;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_420UP_SAMPLEING, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_SetFilmMode_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bEnable)
{
    MS_U16 u16TabIdx;

    PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"[PQ_SetFilmMode]: PQTabType=%u, enable=%u\n", eWindow, bEnable);

    if(bEnable)
        u16TabIdx = MDrv_PQ_GetTableIndex_U2(pInstance, eWindow, PQ_IP_Film32_Main);
    else
        u16TabIdx = PQ_IP_Film32_OFF_Main;

    MDrv_PQ_LoadTable_U2(pInstance, eWindow, u16TabIdx, PQ_IP_Film32_Main);

    if(bEnable)
        u16TabIdx = MDrv_PQ_GetTableIndex_U2(pInstance, eWindow, PQ_IP_Film22_Main);
    else
        u16TabIdx = PQ_IP_Film22_OFF_Main;

    MDrv_PQ_LoadTable_U2(pInstance, eWindow, u16TabIdx, PQ_IP_Film22_Main);

}
void SYMBOL_WEAK MDrv_PQ_SetFilmMode(PQ_WIN eWindow, MS_BOOL bEnable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_SetFilmMode PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bEnable = bEnable;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_FILEMODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}


void MDrv_PQ_SetNonLinearScaling_U2(void* pInstance,PQ_WIN eWindow, MS_U8 u8Level, MS_BOOL bEnable)
{
    MS_U16 u16TabIdx;
    PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"[PQ_SetNonLinearScaling]: Level=%u, enable=%u\n", u8Level, bEnable);

    if(eWindow == PQ_SUB_WINDOW)
    {
        //sub window don't need nonLinear scaling
        return;
    }

    MS_U16 u16SCOutputWidth = _stPQ_Info.u16PnlWidth;
    MS_U16 u16Input_HSize = QM_GetInputHSize(eWindow);
    MS_U16 u16Input_VSize = QM_GetInputVSize(eWindow);

    XC_ApiStatusEx stDrvStatusEx;
    memset(&stDrvStatusEx,0,sizeof(XC_ApiStatusEx));

    stDrvStatusEx.u16ApiStatusEX_Length = sizeof(XC_ApiStatusEx);
    stDrvStatusEx.u32ApiStatusEx_Version = API_STATUS_EX_VERSION;
    MApi_XC_GetStatusEx(&stDrvStatusEx, MAIN_WINDOW);

    if (_bUFSCPQEn && stDrvStatusEx.bFSCEnabled)
    {
        // With FSC case, scaler output width need to reference to UFSC input
        if (!MDrv_PQ_GetPQPathStatus(E_PQ_UFSC_4K_PATH, u16Input_HSize, u16Input_VSize))
        {
            u16SCOutputWidth = u16SCOutputWidth/2; // FHD to UFSC case, scaler output is panel width/2
        }
    }

    if(bEnable)
    {
        switch(u16SCOutputWidth)
        {
            case 3840:
                switch(u8Level)
                {
                    case 3:
                        u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_3840_3);
                        break;
                    case 2:
                        u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_3840_2);
                        break;
                    case 1:
                        u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_3840_1);
                        break;
                    case 0:
                    default:
                        u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_3840_0);
                        break;
                }
                break;
            case 1920:
                switch(u8Level)
                {
                    case 2:
                        u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_1920_2);
                        break;
                    case 1:
                        u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_1920_1);
                        break;
                    case 0:
                    default:
                        u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_1920_0);
                        break;
                }
                break;
            case 1680:
                u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_1680);
                break;
            case 1440:
                u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_1440);
                break;
            case 1366:
                switch(u8Level)
                {
                    case 2:
                        u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_1366_2);
                        break;
                    case 1:
                        u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_1366_1);
                        break;
                    case 0:
                    default:
                        u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_1366_0);
                        break;

                }
                break;
            default:
                u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_OFF);
                break;

                break;
        }
    }
    else
    {
        u16TabIdx = Hal_PQ_get_hnonlinear_idx(pInstance,MS_HNONLINEAR_OFF);
    }
    MDrv_PQ_LoadTable_U2(pInstance, eWindow, u16TabIdx, PQ_IP_HnonLinear_Main);

}
void SYMBOL_WEAK MDrv_PQ_SetNonLinearScaling(PQ_WIN eWindow, MS_U8 u8Level, MS_BOOL bEnable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_SetNonLinearScaling PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u8Level = u8Level;
    PQArgs.bEnable = bEnable;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_NONLINEAR_SCALING, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void _MDrv_PQ_CloneSram(void *pInstance)
{
    MS_U16 u16CSRAM1 = MDrv_PQ_GetTableIndex_U2(pInstance, PQ_MAIN_WINDOW, PQ_IP_C_SRAM1_Main);
    MS_U16 u16CSRAM2 = MDrv_PQ_GetTableIndex_U2(pInstance, PQ_MAIN_WINDOW, PQ_IP_C_SRAM2_Main);

    if(gbPQBinEnable)
    {
        MS_PQBin_IP_Table_Info stTableInfo;
        memset(&stTableInfo, 0, sizeof(MS_PQBin_IP_Table_Info));

        MDrv_PQBin_GetTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),
                        _u16SRAM1,
                        PQ_IP_SRAM1_Main,
                        &stTableInfo,
                        &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        stTableInfo.u16TableType = PQ_TABTYPE_SRAM3;
        MDrv_PQBin_DumpTable(pInstance,&stTableInfo);

        MDrv_PQBin_GetTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),
                        _u16SRAM2,
                        PQ_IP_SRAM2_Main,
                        &stTableInfo,
                        &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        stTableInfo.u16TableType = PQ_TABTYPE_SRAM4;
        MDrv_PQBin_DumpTable(pInstance,&stTableInfo);

        MDrv_PQBin_GetTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),
                        u16CSRAM1,
                        PQ_IP_C_SRAM1_Main,
                        &stTableInfo,
                        &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        stTableInfo.u16TableType = PQ_TABTYPE_C_SRAM3;
        MDrv_PQBin_DumpTable(pInstance,&stTableInfo);

        MDrv_PQBin_GetTable(pInstance,MDrv_PQBin_GetPanelIdx(pInstance),
                        u16CSRAM2,
                        PQ_IP_C_SRAM2_Main,
                        &stTableInfo,
                        &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        stTableInfo.u16TableType = PQ_TABTYPE_C_SRAM4;
        MDrv_PQBin_DumpTable(pInstance,&stTableInfo);
    }
    else
    {
        EN_IP_Info stIpInfo;
        memset(&stIpInfo, 0, sizeof(EN_IP_Info));
        stIpInfo = MDrv_PQ_GetTable_(MAIN,pInstance, _u16SRAM1, PQ_IP_SRAM1_Main);
        Hal_PQ_set_y_sram_table(pInstance,SC_FILTER_Y_SRAM3, (void *)((char *)&stIpInfo.pIPTable[stIpInfo.u8TabIdx * Hal_PQ_get_sram_size(pInstance,SC_FILTER_Y_SRAM1)]));

        stIpInfo = MDrv_PQ_GetTable_(MAIN,pInstance, _u16SRAM2, PQ_IP_SRAM2_Main);
        Hal_PQ_set_y_sram_table(pInstance,SC_FILTER_Y_SRAM4, (void *)((char *)&stIpInfo.pIPTable[stIpInfo.u8TabIdx * Hal_PQ_get_sram_size(pInstance,SC_FILTER_Y_SRAM2)]));

        stIpInfo = MDrv_PQ_GetTable_(MAIN,pInstance, u16CSRAM1, PQ_IP_C_SRAM1_Main);
        Hal_PQ_set_c_sram_table(pInstance,SC_FILTER_C_SRAM3, (void *)((char *)&stIpInfo.pIPTable[stIpInfo.u8TabIdx * Hal_PQ_get_sram_size(pInstance,SC_FILTER_C_SRAM1)]));

        stIpInfo = MDrv_PQ_GetTable_(MAIN,pInstance, u16CSRAM2, PQ_IP_C_SRAM2_Main);
        Hal_PQ_set_c_sram_table(pInstance,SC_FILTER_C_SRAM4, (void *)((char *)&stIpInfo.pIPTable[stIpInfo.u8TabIdx * Hal_PQ_get_sram_size(pInstance,SC_FILTER_C_SRAM2)]));
    }
}

void MDrv_PQ_3DCloneforPIP_U2(void* pInstance,MS_BOOL bIpSync)
{
    MVOP_Handle stHdl = { E_MVOP_MODULE_MAIN };
    MS_BOOL bEnAltOutputState = FALSE;
    ST_PQ_IPInfo stIPInfo;
    ST_PQ_IPInfo stUpdatedIPInfo;
    memset(&stIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedIPInfo, 0, sizeof(ST_PQ_IPInfo));

    if (E_MVOP_OK == MDrv_MVOP_GetCommand(&stHdl, E_MVOP_CMD_GET_3DLR_ALT_OUT, &bEnAltOutputState, sizeof(bEnAltOutputState)))
    {
        //printf("%s Get3DLRAltOutput=0x%x\n", __FUNCTION__, bEnAltOutputState);
    }
    else
    {
        //printf("Fail to query E_MVOP_CMD_GET_3DLR_ALT_OUT!!\n");
    }

    if((E_XC_3D_OUTPUT_LINE_ALTERNATIVE == MApi_XC_Get_3D_Output_Mode())
        || (E_XC_3D_OUTPUT_TOP_BOTTOM == MApi_XC_Get_3D_Output_Mode())
        || (E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF == MApi_XC_Get_3D_Output_Mode())
        || (E_XC_3D_OUTPUT_TOP_BOTTOM_HW == MApi_XC_Get_3D_Output_Mode())
        || (E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF_HW == MApi_XC_Get_3D_Output_Mode())
        || (E_XC_3D_OUTPUT_LEFT_LEFT == MApi_XC_Get_3D_Output_Mode())
        || (E_XC_3D_OUTPUT_RIGHT_RIGHT == MApi_XC_Get_3D_Output_Mode())
        || (E_XC_3D_OUTPUT_TOP_TOP == MApi_XC_Get_3D_Output_Mode())
        || (E_XC_3D_OUTPUT_BOTTOM_BOTTOM == MApi_XC_Get_3D_Output_Mode()))
    {
        _MDrv_PQ_CloneSram(pInstance);
#if PQ_QM_3D_CLONE_ENABLE
        MDrv_PQ_3D_CloneWindow(E_PQ_3DTYPE_XC, PQ_MAIN_WINDOW);
#else
        MApi_XC_ACE_3DClonePQMap(E_ACE_WEAVETYPE_NONE);
#endif

        _s32LastPQIPMADiSub = -1;
        _s32LastPQIPMemFormatSub = -1;
        _u16LastPQSrcType[PQ_SUB_WINDOW] = 0xFFFF;

        //printf("3D:confirm to clone main/sub win\n");

        if((MApi_XC_Get_3D_HW_Version() < 2) && bIpSync)
        {
            MApi_XC_3DMainSub_IPSync();
            //printf("3D:confirm to do main/sub win sync\n");
        }
    }
    else if(((E_XC_3D_INPUT_TOP_BOTTOM == MApi_XC_Get_3D_Input_Mode(MAIN_WINDOW)) && (E_XC_3D_OUTPUT_FRAME_ALTERNATIVE == MApi_XC_Get_3D_Output_Mode()))
    || ((E_XC_3D_INPUT_NORMAL_2D == MApi_XC_Get_3D_Input_Mode(MAIN_WINDOW)) && (E_XC_3D_OUTPUT_FRAME_ALTERNATIVE == MApi_XC_Get_3D_Output_Mode())))
    {
        if(MApi_XC_Get_3D_HW_Version() < 2)
        {
            MDrv_PQ_EnableMADIForce(PQ_MAIN_WINDOW, TRUE);
#if PQ_MADI_DFK
            stIPInfo.u16IPIdx = PQ_IP_MADi_DFK_Main;
            stIPInfo.u16TableIdx = PQ_IP_MADi_DFK_OFF_Main;
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, PQ_MAIN_WINDOW, stUpdatedIPInfo.u16TableIdx, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
#endif
            MDrv_PQ_LoadTable_U2(pInstance, PQ_MAIN_WINDOW, PQ_IP_EODi_OFF_Main, PQ_IP_EODi_Main);
        }
    }
    else if(MApi_XC_IsCurrentFrameBufferLessMode() && bEnAltOutputState)
    {
        MApi_XC_ACE_3DClonePQMap(E_ACE_WEAVETYPE_H);
    }
    MApi_XC_3D_PostPQSetting(MAIN_WINDOW);

#if(PQ_GRULE_3D_ENABLE == 1)
    if(E_XC_3D_OUTPUT_MODE_NONE != MApi_XC_Get_3D_Output_Mode())
    {
        MDrv_PQ_SetGRuleStatus(PQ_MAIN_WINDOW, PQ_GRule_3D_Main, PQ_GRule_3D_On_Main);
    }
    else
    {
        MDrv_PQ_SetGRuleStatus(PQ_MAIN_WINDOW, PQ_GRule_3D_Main, PQ_GRule_3D_Off_Main);
    }
#endif
}
void SYMBOL_WEAK MDrv_PQ_3DCloneforPIP(MS_BOOL bIpSync)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_3DCloneforPIP PQArgs;
    PQArgs.bIpSync = bIpSync;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_3D_CLONE_FOR_PIP, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_DisableFilmMode_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bOn)
{
    if(bOn)
    {
        MDrv_PQ_SetFilmMode_U2(pInstance, eWindow, DISABLE);
    }
    else
    {
        MDrv_PQ_SetFilmMode_U2(pInstance, eWindow, ENABLE);
    }
}
void SYMBOL_WEAK MDrv_PQ_DisableFilmMode(PQ_WIN eWindow, MS_BOOL bOn)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return;
        }
    }

    stPQ_DisableFilmMode PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bOn = bOn;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_DISABLE_FILEMODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#if (PQ_GRULE_GAME_MODE_ENABLE == 1)
static MS_BOOL _MDrv_PQ_LoadGameModeTable(void* pInstance, PQ_WIN eWindow, MS_U16 u16PQ_GameMode_Idx)
{
    MS_U16 u16Index=0, u16IPIdx=0, u16TabIdx=0;
    ST_PQ_IPInfo stGRuleIPInfo;
    ST_PQ_IPInfo stUpdatedGRuleIPInfo;
    memset(&stGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));

    MS_U16 u16TabType = 0;
    if(eWindow == PQ_SUB_WINDOW)
    {
        return FALSE;
    }
    stGRuleIPInfo.u16IPIdx= PQ_GRule_GAME_MODE_Main;
    stGRuleIPInfo.u16GruleMaxIPNum = PQ_GRULE_GAME_MODE_IP_NUM_Main;
    stGRuleIPInfo.u16GruleLevelMainNum = PQ_GRULE_GAME_MODE_NUM_Main;
    stGRuleIPInfo.u16GruleLevelNum = PQ_GRULE_GAME_MODE_LVL_NUM_Main;
    stGRuleIPInfo.enPQEngineType = PQ_XC0_STD;
    MHal_PQ_RemappingGruleIP(pInstance, stGRuleIPInfo, &stUpdatedGRuleIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);
    if(MHal_PQ_Support_GroupIPBIN())
    {
        if(stUpdatedGRuleIPInfo.enPQEngineType == PQ_XC0_STD)
        {
            MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,stUpdatedGRuleIPInfo.u16IPIdx,PQ_BIN_STD_MAIN ,&stUpdatedGRuleIPInfo.u16GruleLevelMainNum, &stUpdatedGRuleIPInfo.u16GruleMaxIPNum ,&stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        }
        else
        {
            MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,stUpdatedGRuleIPInfo.u16IPIdx,PQ_BIN_EXT_MAIN ,&stUpdatedGRuleIPInfo.u16GruleLevelMainNum, &stUpdatedGRuleIPInfo.u16GruleMaxIPNum ,&stPQBinHeaderInfo[PQ_BIN_EXT_MAIN]);
        }
    }
    if (u16PQ_GameMode_Idx > stUpdatedGRuleIPInfo.u16GruleLevelNum)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRULE_INDEX_OVERFLOW_ERROR Level Index = %u, Max Level Index = %u",u16PQ_GameMode_Idx,(stUpdatedGRuleIPInfo.u16GruleLevelMainNum-1));
        PQ_ASSERT(0);
        return FALSE;
    }
#if(ENABLE_PQ_MLOAD == 1)
    MDrv_PQ_Set_MLoadEn(eWindow, TRUE);
#endif

    for(u16Index=0; u16Index<stUpdatedGRuleIPInfo.u16GruleMaxIPNum; u16Index++)
    {
        MS_U16 u16ListIdx = 0;
        pqIpTable stInfo;
        pqIpTable* pstInfo = NULL;
        memset(&stInfo, 0, sizeof(pqIpTable));
        u16IPIdx = MDrv_PQ_GetGRule_IPIndex_Ex_U2(pInstance, eWindow, stUpdatedGRuleIPInfo.u16IPIdx, u16Index, stUpdatedGRuleIPInfo.enPQEngineType);
        u16TabIdx = MDrv_PQBin_GetCurrentTableIndex(pInstance, MDrv_PQBin_GetPanelIdx(pInstance), u16IPIdx);
        stInfo.u16IpIndex = u16IPIdx;
        stInfo.u16TableIndex = u16TabIdx;
        u16ListIdx = pqListFindDataIndex(_iPqCurrentTable[PQ_MAIN_WINDOW], (void *) &stInfo);
        if(u16ListIdx != -1)
        {
            pstInfo = pqListGetDataAt(_iPqCurrentTable[PQ_MAIN_WINDOW], u16ListIdx);
            if(pstInfo != NULL)
            {
                pstInfo->u16TableIndex = u16TabIdx;
                pqListInsertDataAtLast(_iPqCurrentTable[PQ_MAIN_WINDOW], pstInfo);
                PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"Put [key, value] = [%d, %d] \n",pstInfo->u16IpIndex, pstInfo->u16TableIndex);
            }
        }

        if (u16PQ_GameMode_Idx == stUpdatedGRuleIPInfo.u16GruleLevelMainNum) // see it as default
        {
            u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, eWindow, u16IPIdx, stUpdatedGRuleIPInfo.enPQEngineType);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetGRule_TableIndex_Ex(eWindow, stUpdatedGRuleIPInfo.u16IPIdx, u16PQ_GameMode_Idx, u16Index, stUpdatedGRuleIPInfo.enPQEngineType);
        }

        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[GameMode enable and loding]SRC: %u, Game Mode grule_lvl_Idx: %u, Index:%u, IPIdx:%u, u8TabIdx:%u\n",
            _u16PQSrcType[eWindow], u16PQ_GameMode_Idx, u16Index, u16IPIdx, u16TabIdx);

        MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx, u16IPIdx, stUpdatedGRuleIPInfo.enPQEngineType);
#if ((PQ_XRULE_DISABLE_UC == 1)||(PQ_SUPPORT_GAMEMODE_VER == 1))
        if(u16IPIdx == PQ_IP_Game_Mode_Type_Main_Ex)
        {
            // get game mode type from  grule .
            u16TabIdx = MDrv_PQ_GetGRule_TableIndex_Ex(eWindow, stUpdatedGRuleIPInfo.u16IPIdx, u16PQ_GameMode_Idx, stUpdatedGRuleIPInfo.u16GruleMaxIPNum-1, stUpdatedGRuleIPInfo.enPQEngineType);
            switch(u16TabIdx)
            {
                case PQ_IP_Game_Mode_Type_Type0_Main_Ex:
                    _stPQ_StatusEx.bZeroFrameMode[eWindow] = FALSE;
                    break;
                case PQ_IP_Game_Mode_Type_Type1_Main_Ex:
                    _stPQ_StatusEx.bZeroFrameMode[eWindow] = TRUE;
                    break;
                default:
                    _stPQ_StatusEx.bZeroFrameMode[eWindow] = FALSE;
                    break;
            }
            PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"bZeroFrameMode =  %d \n", _stPQ_StatusEx.bZeroFrameMode[eWindow]);
        }
#endif
    }
#if(ENABLE_PQ_MLOAD == 1)
    MDrv_PQ_Set_MLoadEn(eWindow, FALSE);
#endif
    return TRUE;
}
MS_BOOL MDrv_PQ_LoadGameModeGrule(void* pInstance, PQ_WIN eWindow,MS_U16 u16GRuleLevelIndex)
{
#if defined(PQ_GRULE_GAME_MODE_ENABLE) && (PQ_GRULE_GAME_MODE_ENABLE == 1)
    _MDrv_PQ_LoadGameModeTable(pInstance,eWindow,u16GRuleLevelIndex);
#else
    UNUSED(pInstance);
    UNUSED(eWindow);
    UNUSED(u16GRuleLevelIndex);
#endif
    return TRUE;
}

#endif
//void _MDrv_PQ_LoadPTPTable(PQ_WIN eWindow, MS_U16 u16PQ_PTP_Idx);

#if (PQ_SUPPORT_SETGRULEDATA == 0)
#if defined(PQ_GRULE_HDR_ENABLE) && (PQ_GRULE_HDR_ENABLE == 1)
static MS_BOOL _MDrv_PQ_LoadHDRModeTable(void* pInstance, PQ_WIN eWindow, MS_U16 u16PQ_HDRMode_Idx)
{
    MS_U16 u16Index=0, u16IPIdx=0, u16TabIdx=0;
    MS_U16 u16GRuleLevelIndex=0;
    MS_U16 u16GroupNum = 0;
    MS_U16 u16IPNum = 0;
    if(MHal_PQ_Support_GroupIPBIN())
    {
        MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,PQ_GRule_HDR_Main,PQ_BIN_STD_MAIN ,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
    }
    else
    {
        u16GroupNum = PQ_GRULE_HDR_NUM_Main;
        u16IPNum = PQ_GRULE_HDR_IP_NUM_Main;
    }
    if(eWindow == PQ_SUB_WINDOW)
    {
        return FALSE;
    }

    u16GRuleLevelIndex = MDrv_PQ_GetGRule_LevelIndex(eWindow, PQ_GRule_HDR_Main, u16PQ_HDRMode_Idx);

    if (u16GRuleLevelIndex > u16GroupNum)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRULE_INDEX_OVERFLOW_ERROR Level Index = %u, Max Level Index = %u",u16GRuleLevelIndex,(u16GroupNum-1));
        PQ_ASSERT(0);
        return FALSE;
    }
#if(ENABLE_PQ_MLOAD == 1)
    MDrv_PQ_Set_MLoadEn(eWindow, TRUE);
#endif

    for(u16Index = 0; u16Index < u16IPNum; u16Index++)
    {
        u16IPIdx = MDrv_PQ_GetGRule_IPIndex(eWindow, PQ_GRule_HDR_Main, u16Index);
        if (u16GRuleLevelIndex == u16GroupNum) // see it as default
        {
            u16TabIdx = MDrv_PQ_GetTableIndex(eWindow, u16IPIdx);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_HDR_Main, u16GRuleLevelIndex, u16Index);
        }
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[HDRMode]SRC: %u, u16PQ_HDRMode_Idx: %u, HDRModeIPIdx:%u, IPIdx:%u, u8TabIdx:%u\n",
            _u16PQSrcType[eWindow], u16GRuleLevelIndex, u16Index, u16IPIdx, u16TabIdx);
        MDrv_PQ_LoadTable(eWindow, u16TabIdx, u16IPIdx);
    }
#if(ENABLE_PQ_MLOAD == 1)
    MDrv_PQ_Set_MLoadEn(eWindow, FALSE);
#endif

#if defined(PQ_GRULE_UFSC_HDR_ENABLE) && (PQ_GRULE_UFSC_HDR_ENABLE == 1)
    u16GroupNum = PQ_GRULE_HDR_NUM_UFSC;
    u16IPNum = PQ_GRULE_HDR_IP_NUM_UFSC;
    if(MHal_PQ_Support_GroupIPBIN())
    {
        MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,PQ_GRule_HDR_UFSC,PQ_BIN_UFSC ,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_UFSC]);
    }
    u16GRuleLevelIndex = MDrv_PQ_GetGRule_LevelIndex_Ex(eWindow, PQ_GRule_HDR_UFSC,u16PQ_HDRMode_Idx, PQ_XC0_UFSC);

    if(u16GRuleLevelIndex > u16GroupNum)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRULE_INDEX_OVERFLOW_ERROR Level Index = %u, Max Level Index = %u",u16GRuleLevelIndex,(u16GroupNum-1));
        PQ_ASSERT(0);
        return FALSE;
    }
    for(u16Index = 0; u16Index < u16IPNum; u16Index++)
    {
        u16IPIdx = MDrv_PQ_GetGRule_IPIndex_Ex(eWindow, PQ_GRule_HDR_UFSC, u16Index, PQ_XC0_UFSC);
        if (u16GRuleLevelIndex == u16GroupNum) // see it as default
        {
            u16TabIdx = MDrv_PQ_GetTableIndex_Ex(eWindow, u16TabIdx, PQ_XC0_UFSC);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetGRule_TableIndex_Ex(eWindow, PQ_GRule_HDR_UFSC, u16GRuleLevelIndex, u16Index,PQ_XC0_UFSC);
        }
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[DC]SRC: %u, DC: %u, DCIPIdx:%u, IPIdx:%u, u16TabIdx:%u\n",
            _u16PQSrcType[eWindow], u16GRuleLevelIndex, u16Index, u16IPIdx, u16TabIdx);

        MDrv_PQ_LoadTable_Ex(eWindow, u16TabIdx, u16IPIdx, PQ_XC0_UFSC);
    }
#endif
    return TRUE;
}
#endif
#endif

#if (PQ_SUPPORT_SETGRULEDATA == 0)
#if ENABLE_PQ_CUSTOMER_BIN_MAIN_GRULE
#if defined(PQ_GRULE_HDR_ENABLE) && (PQ_GRULE_HDR_ENABLE == 1)
static MS_BOOL _MDrv_PQ_LoadCustomerHDRTable(void* pInstance,PQ_WIN eWindow, MS_U16 u16PQ_HDRMode_Idx)
{
    MS_U16 u16Index=0, u16IPIdx = 0, u16TabIdx = 0;
    MS_U16 u16GRuleLevelIndex=0;
    MS_U16 u16GroupNum = 0;
    MS_U16 u16IPNum = 0;
    if(MHal_PQ_Support_GroupIPBIN())
    {
        MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,PQ_GRule_HDR_Main_Color,PQ_BIN_CUSTOMER_MAIN ,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN]);
    }
    else
    {
        u16GroupNum = PQ_GRULE_HDR_NUM_Main_Color;
        u16IPNum = PQ_GRULE_HDR_IP_NUM_Main_Color;
    }
    u16GRuleLevelIndex = MDrv_PQ_GetGRule_LevelIndex(eWindow, PQ_GRule_HDR_Main_Color + MDrv_PQ_GetGRule_GRuleNum(eWindow), u16PQ_HDRMode_Idx);

    if (eWindow == PQ_MAIN_WINDOW)
    {
        if(u16GRuleLevelIndex > u16GroupNum)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"PQ_GRULE_INDEX_OVERFLOW_ERROR!!,level index = %u ,max level index = %u\n",u16GRuleLevelIndex,u16GroupNum-1);
            return FALSE;
        }
        for(u16Index = 0; u16Index < u16IPNum; u16Index++)
        {
            u16IPIdx = MDrv_PQ_GetCustomerGRule_IPIndex(eWindow, PQ_GRule_HDR_Main_Color, u16Index);
            if(u16GRuleLevelIndex == u16GroupNum) // see it as default
            {
                u16TabIdx = MDrv_PQ_GetCustomerTableIndex(eWindow, u16IPIdx);
            }
            else
            {
                u16TabIdx = MDrv_PQ_GetGRule_CustomerTableIndex(eWindow, PQ_GRule_HDR_Main_Color, u16GRuleLevelIndex, u16Index);
            }
            MDrv_PQ_LoadCustomerTable(eWindow, u16TabIdx, u16IPIdx);
        }
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[HDR] CUSTOMER PQ Not support case \n");
    }

    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_GRULE_DBG;
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.pValue= &u16GRuleLevelIndex;
    stPQSetVRInfo.u16RuleIndex= PQ_GRule_HDR_Main_Color;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }
    return TRUE;
}
#endif
#endif
#endif

MS_BOOL  MDrv_PQ_EnableHDRMode_U2(void* pInstance, PQ_WIN eWindow ,MS_U16 u16GRuleLevelIndex)
{
    UNUSED(eWindow);
    UNUSED(u16GRuleLevelIndex);

#if (PQ_SUPPORT_SETGRULEDATA == 0)
#if defined(PQ_GRULE_HDR_ENABLE) && (PQ_GRULE_HDR_ENABLE == 1)
    _MDrv_PQ_LoadHDRModeTable(pInstance,eWindow, u16GRuleLevelIndex);

#if ENABLE_PQ_CUSTOMER_BIN_MAIN_GRULE
    _MDrv_PQ_LoadCustomerHDRTable(pInstance,eWindow, u16GRuleLevelIndex);
#endif
#endif
#endif
    return TRUE;
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_EnableHDRMode(PQ_WIN eWindow ,MS_U16 u16GRuleLevelIndex)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
    }

    stPQ_EnableHDRMode PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GRuleLevelIndex = u16GRuleLevelIndex;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_ENABLE_HDRMODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}
MS_BOOL MDrv_PQ_DisableUCFeature_U2(void* pInstance,PQ_WIN eWindow)
{
    MS_BOOL bRet = FALSE;
    MS_U32 u32SupportZNR = 0;
    MS_U32 u32SupportABF = 0;
    ST_PQ_IPInfo stGRuleIPInfo;
    ST_PQ_IPInfo stUpdatedGRuleIPInfo;
    memset(&stGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));
    UNUSED(eWindow);
#if (PQ_XRULE_DISABLE_UC == 1)
    MS_U16 u16Index=0, u16IPIdx=0, u16TabIdx=0;
    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"[PQ_Load XRule] \n");

    if(eWindow == PQ_SUB_WINDOW)
    {
        return FALSE;
    }
    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"Disable UC feature by loading DisableUC Xrule\n");

    // from hw rd, DisableUC Xrule only in Ext Qmap.
    for(u16Index=0; u16Index<PQ_DisableUC_rule_IP_NUM_Main_Ex; u16Index++)
    {
        u16IPIdx = MDrv_PQ_GetXRuleIPIndex(pInstance, eWindow, E_XRULE_EX_DISABLE_UC, u16Index,PQ_XC0_EXT);
        u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, eWindow, E_XRULE_EX_DISABLE_UC, PQ_DisableUC_rule_DisableUC_Main_Ex, u16Index,PQ_XC0_EXT);
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"[UC]src type: %u,index:%u, IPIdx:%u, u8TabIdx:%u\n",
        _u16PQSrcType[eWindow], u16Index, u16IPIdx, u16TabIdx);
        MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx, u16IPIdx,PQ_XC0_EXT);
    }

    if(_gIsEnable8LBMode)
    {
        MDrv_PQBin_Enable8LBMode(pInstance,TRUE);

        MApi_XC_Set_OPWriteOffEnableToReg(ENABLE, MAIN_WINDOW); //default
    }
#elif (PQ_GRULE_GAME_MODE_ENABLE == 1)
    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[PQ_LoadGameModeTable]\n");
    MS_U16 u16PQ_GameModeIdx;

    if(eWindow == PQ_SUB_WINDOW)
    {
        return FALSE;
    }
    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"Disable UC feature by loading Game mode Grule\n");

    stGRuleIPInfo.u16IPIdx = PQ_GRule_GAME_MODE_Main;
    stGRuleIPInfo.u16GruleMaxIPNum = PQ_GRULE_GAME_MODE_IP_NUM_Main;
    stGRuleIPInfo.u16GruleLevel = PQ_GRule_Lvl_GAME_MODE_On_Main;
    MHal_PQ_RemappingGruleIP(pInstance, stGRuleIPInfo, &stUpdatedGRuleIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);
    u16PQ_GameModeIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, stUpdatedGRuleIPInfo.u16IPIdx, stUpdatedGRuleIPInfo.u16GruleLevel, stUpdatedGRuleIPInfo.enPQEngineType);

#if defined(PQ_SUPPORT_GAMEMODE_VER ) && (PQ_SUPPORT_GAMEMODE_VER == 1 || PQ_SUPPORT_GAMEMODE_VER == 2)
    MS_U16 u16PQ_GameMode_Lvl_Idx = PQ_GRule_Lvl_GAME_MODE_On_NonMirror_Main_Ex;
    if(MDrv_PQ_GetGameMode_Status_U2(pInstance,eWindow) == TRUE)
    {
        if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
        {
            u16PQ_GameMode_Lvl_Idx = PQ_GRule_Lvl_GAME_MODE_On_Mirror_Main_Ex;
        }
        else
        {
            if(PQ_SUPPORT_GAMEMODE_VER == 2)
            {
                u16PQ_GameMode_Lvl_Idx = PQ_GRule_Lvl_GAME_MODE_On_NonMirror_FBL_Main_Ex;
            }
            else if(PQ_SUPPORT_GAMEMODE_VER == 1)
            {
                u16PQ_GameMode_Lvl_Idx = PQ_GRule_Lvl_GAME_MODE_On_NonMirror_Main_Ex;
            }
        }
    }
    else
    {
        if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
        {
            u16PQ_GameMode_Lvl_Idx = PQ_GRule_Lvl_GAME_MODE_On_Mirror_Main_Ex;
        }
        else
        {
            u16PQ_GameMode_Lvl_Idx = PQ_GRule_Lvl_GAME_MODE_On_NonMirror_Main_Ex;
        }
    }
    u16PQ_GameModeIdx = u16PQ_GameMode_Lvl_Idx;
#endif

    bRet = _MDrv_PQ_LoadGameModeTable(pInstance, eWindow, u16PQ_GameModeIdx);

    if(_gIsEnable8LBMode)
    {
        MDrv_PQBin_Enable8LBMode(pInstance,TRUE);

        MApi_XC_Set_OPWriteOffEnableToReg(ENABLE, MAIN_WINDOW); //default
    }

#else
#if PQ_UC_CTL
    bRet = MDrv_PQ_SetUCFeature(PQ_MAIN_WINDOW,
                                0xFFFF,
                                0xFFFF,
                                PQ_IP_MADi_DFK_DFK1_Main,
                                PQ_IP_MADi_SST_SST3_Main,
                                PQ_IP_MADi_EODiW_OFF_Main,
                                PQ_IP_UCNR_OFF_Main,
                                PQ_IP_UCDi_OFF_Main,
                                PQ_IP_UC_CTL_OFF_Main,
                                0xFFFF,
                                0xFFFF,
                                0xFFFF
                                );
#endif
#endif

    MApi_XC_GetChipCaps(E_XC_SUPPORT_ZNR, &u32SupportZNR, sizeof(MS_U32));
    MApi_XC_GetChipCaps(E_XC_SUPPORT_ABF, &u32SupportABF, sizeof(MS_U32));
    if((u32SupportZNR!=0) && (u32SupportABF!=0))
    {
        MHal_PQ_SetABFPatch(_stMode_Info[eWindow].bInterlace);
    }

    return bRet;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_DisableUCFeature(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_DisableUCFeature PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_DISABLE_UC_FEATURE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}


MS_BOOL MDrv_PQ_GetCurrentUCFeature_U2(void* pInstance,
                                           PQ_WIN eWindow,
                                           MS_U16 *pu16TabIdx_MADi,
                                           MS_U16 *pu16TabIdx_MADi_Mot,
                                           MS_U16 *pu16TabIdx_MADi_DFK,
                                           MS_U16 *pu16TabIdx_MADi_SST,
                                           MS_U16 *pu16TabIdx_MADi_EODiW,
                                           MS_U16 *pu16TabIdx_UCNR,
                                           MS_U16 *pu16TabIdx_UCDi,
                                           MS_U16 *pu16TabIdx_UCCTL,
                                           MS_U16 *pu16TabIdx_SwDriver,
                                           MS_U16 *pu16TabIdx_PostCCS,
                                           MS_U16 *pu16TabIdx_DHD
                                           )
{
    if( (pu16TabIdx_MADi_DFK == NULL)
      ||(pu16TabIdx_MADi_SST == NULL)
      ||(pu16TabIdx_MADi_EODiW == NULL)
      ||(pu16TabIdx_UCDi == NULL)
      ||(pu16TabIdx_UCCTL == NULL)
      ||(pu16TabIdx_UCNR == NULL)
      )
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_GetCurrentUCFeature(), don't pass in NULL pointer!");
        return FALSE;
    }
#if PQ_UC_CTL

    *pu16TabIdx_MADi        = (eWindow==PQ_MAIN_WINDOW)?_s32LastPQIPMADiMain:_s32LastPQIPMADiSub;
    *pu16TabIdx_MADi_Mot    = (eWindow==PQ_MAIN_WINDOW)?_s32LastPQIPMADiMotMain:_s32LastPQIPMADiMotSub;
    *pu16TabIdx_MADi_DFK    = _u16MADi_DFK_Current[eWindow];
    *pu16TabIdx_MADi_SST    = _u16MADi_SST_Current[eWindow];
    *pu16TabIdx_MADi_EODiW  = _u16MADi_EODiW_Current[eWindow];
    *pu16TabIdx_UCDi        = _u16UCDi_Current[eWindow];
    *pu16TabIdx_UCCTL       = _u16UCCTL_Current[eWindow];
    *pu16TabIdx_UCNR        = _u16UCNR_Current[eWindow];
    *pu16TabIdx_SwDriver    = _u16SwDriver_Current[eWindow];
    *pu16TabIdx_PostCCS     = _u16PostCCS_Current[eWindow];
    *pu16TabIdx_DHD         = _u16DHD_Current[eWindow];
    PQMADi_DBG(printf("MDrv_PQ_GetCurrentUCFeature(%u, %u, %u,%u,%u,%u,%u,%u,%u,%u,%u)\n",
                                           *pu16TabIdx_MADi,
                                           *pu16TabIdx_MADi_Mot,
                                           *pu16TabIdx_MADi_DFK,
                                           *pu16TabIdx_MADi_SST,
                                           *pu16TabIdx_MADi_EODiW,
                                           *pu16TabIdx_UCNR,
                                           *pu16TabIdx_UCDi,
                                           *pu16TabIdx_UCCTL,
                                           *pu16TabIdx_SwDriver,
                                           *pu16TabIdx_PostCCS,
                                           *pu16TabIdx_DHD));

    return TRUE;
#else
    return FALSE;
#endif
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetCurrentUCFeature(PQ_WIN eWindow,
                                           MS_U16 *pu16TabIdx_MADi,
                                           MS_U16 *pu16TabIdx_MADi_Mot,
                                           MS_U16 *pu16TabIdx_MADi_DFK,
                                           MS_U16 *pu16TabIdx_MADi_SST,
                                           MS_U16 *pu16TabIdx_MADi_EODiW,
                                           MS_U16 *pu16TabIdx_UCNR,
                                           MS_U16 *pu16TabIdx_UCDi,
                                           MS_U16 *pu16TabIdx_UCCTL,
                                           MS_U16 *pu16TabIdx_SwDriver,
                                           MS_U16 *pu16TabIdx_PostCCS,
                                           MS_U16 *pu16TabIdx_DHD
                                           )
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetCurrentUCFeature PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.pu16TabIdx_MADi = pu16TabIdx_MADi;
    PQArgs.pu16TabIdx_MADi_Mot = pu16TabIdx_MADi_Mot;
    PQArgs.pu16TabIdx_MADi_DFK = pu16TabIdx_MADi_DFK;
    PQArgs.pu16TabIdx_MADi_SST = pu16TabIdx_MADi_SST;
    PQArgs.pu16TabIdx_MADi_EODiW = pu16TabIdx_MADi_EODiW;
    PQArgs.pu16TabIdx_UCNR = pu16TabIdx_UCNR;
    PQArgs.pu16TabIdx_UCDi = pu16TabIdx_UCDi;
    PQArgs.pu16TabIdx_UCCTL = pu16TabIdx_UCCTL;
    PQArgs.pu16TabIdx_SwDriver =pu16TabIdx_SwDriver ;
    PQArgs.pu16TabIdx_PostCCS =pu16TabIdx_PostCCS ;
    PQArgs.pu16TabIdx_DHD =pu16TabIdx_DHD ;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_CURRENT_UC_FEATURE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_GetUCFeatureFromPQ_U2(void* pInstance,
                                           PQ_WIN eWindow,
                                           MS_U16 *pu16TabIdx_MADi_DFK,
                                           MS_U16 *pu16TabIdx_MADi_SST,
                                           MS_U16 *pu16TabIdx_MADi_EODiW,
                                           MS_U16 *pu16TabIdx_UCNR,
                                           MS_U16 *pu16TabIdx_UCDi,
                                           MS_U16 *pu16TabIdx_UCCTL,
                                           MS_U16 *pu16TabIdx_SwDriver,
                                           MS_U16 *pu16TabIdx_PostCCS,
                                           MS_U16 *pu16TabIdx_DHD)
{
    ST_PQ_IPInfo stIPInfo;
    ST_PQ_IPInfo stUpdatedIPInfo;
    memset(&stIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedIPInfo, 0, sizeof(ST_PQ_IPInfo));

    if( (pu16TabIdx_MADi_DFK == NULL)
      ||(pu16TabIdx_MADi_SST == NULL)
      ||(pu16TabIdx_MADi_EODiW == NULL)
      ||(pu16TabIdx_UCDi == NULL)
      ||(pu16TabIdx_UCCTL == NULL)
      ||(pu16TabIdx_UCNR == NULL)
      ||(pu16TabIdx_SwDriver == NULL)
      ||(pu16TabIdx_PostCCS == NULL)
      ||(pu16TabIdx_DHD == NULL)
      )
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR," \r\n MDrv_PQ_GetUCFeatureFromPQ , don't pass in NULL pointer!");
        return FALSE;
    }

    *pu16TabIdx_MADi_DFK =0xFFFF;
    *pu16TabIdx_MADi_SST = 0xFFFF;
    *pu16TabIdx_MADi_EODiW = 0xFFFF;
    *pu16TabIdx_UCNR = 0xFFFF;
    *pu16TabIdx_UCDi = 0xFFFF;
    *pu16TabIdx_UCCTL = 0xFFFF;
    *pu16TabIdx_SwDriver = 0xFFFF;
    *pu16TabIdx_PostCCS = 0xFFFF;
    *pu16TabIdx_DHD = 0xFFFF;
#if PQ_UC_CTL

#if (PQ_GRULE_DDR_SELECT_ENABLE == 1)
#if (PQ_DDR_SELECT_VER == 1)
    //do nothing, because ddr select rule has no uc item in version1
#elif (PQ_DDR_SELECT_VER == 2)
    MS_U16 u16PQ_DDRSELIdx = 0;

    if(bw_info.bDDR2 == FALSE) // DDR 3
    {
         if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
         {
             PQMADi_DBG(printf("MDrv_PQ_GetDDRMirrorSelectInfo: DDR3 mirror\n"));
             u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR3_Mirror_Main);
         }
         else
         {
             PQMADi_DBG(printf("MDrv_PQ_GetDDRMirrorSelectInfo: DDR3 non-mirror\n"));
             u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR3_Main);
         }
    }
    else // DDR 2
    {
         if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
         {
             PQMADi_DBG(printf("MDrv_PQ_GetDDRMirrorSelectInfo: DDR2 mirror\n"));
             u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR2_Mirror_Main);
         }
         else
         {
             PQMADi_DBG(printf("MDrv_PQ_GetDDRMirrorSelectInfo: DDR2 non-mirror\n"));
             u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR2_Main);
         }
    }
    *pu16TabIdx_MADi_DFK = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 2);
    *pu16TabIdx_MADi_SST = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 3);
    *pu16TabIdx_MADi_EODiW = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 4);
    *pu16TabIdx_UCCTL = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 5);
#endif
#else //DDR select disable
    if(eWindow == PQ_MAIN_WINDOW)
    {
        stIPInfo.u16IPIdx = PQ_IP_MADi_DFK_Main;
        stIPInfo.enPQEngineType = PQ_XC0_STD;
        MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
        *pu16TabIdx_MADi_DFK    = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, eWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType); //y MADi_DFK ip
        stIPInfo.u16IPIdx = PQ_IP_MADi_SST_Main;
        stIPInfo.enPQEngineType = PQ_XC0_STD;
        MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
        *pu16TabIdx_MADi_SST    = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, eWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType); //y MADi_SST ip
        stIPInfo.u16IPIdx = PQ_IP_MADi_EODiW_Main;
        stIPInfo.enPQEngineType = PQ_XC0_STD;
        MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
        *pu16TabIdx_MADi_EODiW  = MDrv_PQ_GetTableIndex_Ex_U2(pInstance,eWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType); //y MADi_EODiW ip
        stIPInfo.u16IPIdx = PQ_IP_UCDi_Main;
        stIPInfo.enPQEngineType = PQ_XC0_STD;
        MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
        *pu16TabIdx_UCDi        = MDrv_PQ_GetTableIndex_Ex_U2(pInstance,eWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType); //y UCDi ip
        stIPInfo.u16IPIdx = PQ_IP_UC_CTL_Main;
        stIPInfo.enPQEngineType = PQ_XC0_STD;
        MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
        *pu16TabIdx_UCCTL       = MDrv_PQ_GetTableIndex_Ex_U2(pInstance,eWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType); //y UCCTL ip
    }
#if PQ_ENABLE_PIP
    else
    {
        stIPInfo.u16IPIdx = MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI_DFK);
        stIPInfo.enPQEngineType = PQ_XC0_STD;
        MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);
        *pu16TabIdx_MADi_DFK    = MDrv_PQ_GetTableIndex_Ex_U2(pInstance,eWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType); // MADi_DFK ip

        stIPInfo.u16IPIdx = MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI_SST);
        stIPInfo.enPQEngineType = PQ_XC0_STD;
        MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);
        *pu16TabIdx_MADi_SST    = MDrv_PQ_GetTableIndex_Ex_U2(pInstance,eWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType); // MADi_SST ip
    }
#endif


    //printf("$$[%s]:%d, %s, u16TabIdx_UCCTL=%u\n", __FUNCTION__, __LINE__, eWindow?"SUB":"MAIN", *pu16TabIdx_UCCTL);
#endif

#if (PQ_GRULE_GAME_MODE_ENABLE == 1)
    if(eWindow == PQ_MAIN_WINDOW)
    {
        stIPInfo.u16IPIdx = PQ_IP_SwDriver_Main;
        stIPInfo.enPQEngineType = PQ_XC0_STD;
        MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
        *pu16TabIdx_SwDriver    = MDrv_PQ_GetTableIndex_Ex_U2(pInstance,eWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
#if (PQ_SUPPORT_POSTCCS == 1)
        *pu16TabIdx_PostCCS     = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, PQ_IP_PostCCS_Main);
#endif
        #if PQ_SUPPORT_MAIN_DHD
        *pu16TabIdx_DHD         = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, PQ_IP_DHD_Main);
        #endif
    }
#if PQ_ENABLE_PIP
    else
    {
        stIPInfo.u16IPIdx = MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_SWDRIVER);
        stIPInfo.enPQEngineType = PQ_XC0_STD;
        MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);
        *pu16TabIdx_SwDriver   = MDrv_PQ_GetTableIndex_Ex_U2(pInstance,eWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);

#if PQ_SUPPORT_SUB_POSTCCS
        *pu16TabIdx_PostCCS    = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, PQ_IP_PostCCS_Sub);
#endif

#if PQ_SUPPORT_SUB_DHD
        *pu16TabIdx_DHD        = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, PQ_IP_DHD_Sub);
#endif
    }
#endif
#endif

#if (PQ_GRULE_SLZ_ENABLE == 1)
     MS_U16 u16PQ_SLZIdx = 0;
     MS_BOOL bSeamlessZappingEnable;

     if(MApi_XC_GetSeamlessZappingStatus(eWindow, &bSeamlessZappingEnable) == E_APIXC_RET_OK)
     {
        if(bSeamlessZappingEnable)
        {
            if(eWindow == PQ_MAIN_WINDOW)
            {
                 u16PQ_SLZIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_SLZ_Main, PQ_GRule_Lvl_SLZ_On_Main);
                 PQMADi_DBG(printf("MDrv_PQ_GetUCFeatureFromPQ: Main LSZ =%u\n", u16PQ_SLZIdx));
                 *pu16TabIdx_MADi_DFK    = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_SLZ_Main, u16PQ_SLZIdx, 2); //y MADi_DFK ip
                 *pu16TabIdx_MADi_SST    = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_SLZ_Main, u16PQ_SLZIdx, 3); //y MADi_SST ip
                 *pu16TabIdx_MADi_EODiW  = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_SLZ_Main, u16PQ_SLZIdx, 4); //y MADi_EODiW ip

                 *pu16TabIdx_UCDi        = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_SLZ_Main, u16PQ_SLZIdx, 5); //y UCDi ip
                 *pu16TabIdx_UCCTL       = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_SLZ_Main, u16PQ_SLZIdx, 6); //y UCCTL ip
             }
#if PQ_ENABLE_PIP
            else
            {
                 u16PQ_SLZIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_SLZ_Sub, PQ_GRule_Lvl_SLZ_On_Sub);
                 PQMADi_DBG(printf("MDrv_PQ_GetUCFeatureFromPQ: Sub LSZ =%u\n", u16PQ_SLZIdx));
                 *pu16TabIdx_MADi_DFK    = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_SLZ_Sub, u16PQ_SLZIdx, 2); // MADi_DFK ip
                 *pu16TabIdx_MADi_SST    = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_SLZ_Sub, u16PQ_SLZIdx, 3); // MADi_SST ip
            }
#endif
        }
        else
        {
            //it's not slz case
        }
     }
     else
     {
         PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MApi_XC_GetStatusEx () version is incorrect at %s, the code's purpose is not fulfilled.\n",__FILE__);
     }

     //printf("$$[%s]:%d, %s, u16TabIdx_UCCTL=%u\n", __FUNCTION__, __LINE__, eWindow?"SUB":"MAIN", *pu16TabIdx_UCCTL);

    UNUSED(pu16TabIdx_UCNR);
#else
    UNUSED(pu16TabIdx_UCNR);
    UNUSED(pu16TabIdx_UCDi);
    UNUSED(pu16TabIdx_UCCTL);
    UNUSED(pu16TabIdx_MADi_DFK);
    UNUSED(pu16TabIdx_MADi_SST);
    UNUSED(pu16TabIdx_MADi_EODiW);
    UNUSED(pu16TabIdx_SwDriver);
    UNUSED(pu16TabIdx_PostCCS);
    UNUSED(pu16TabIdx_DHD);
#endif

#endif
    PQMADi_DBG(printf("MDrv_PQ_GetUCFeatureFromPQ(%u,%u,%u,%u,%u,%u,%u,%u,%u)\n",*pu16TabIdx_MADi_DFK,
                                           *pu16TabIdx_MADi_SST,
                                           *pu16TabIdx_MADi_EODiW,
                                           *pu16TabIdx_UCNR,
                                           *pu16TabIdx_UCDi,
                                           *pu16TabIdx_UCCTL,
                                           *pu16TabIdx_SwDriver,
                                           *pu16TabIdx_PostCCS,
                                           *pu16TabIdx_DHD));
    return TRUE;
}

MS_BOOL MDrv_PQ_GetUCFeatureFromPQ(PQ_WIN eWindow,
                                           MS_U16 *pu16TabIdx_MADi_DFK,
                                           MS_U16 *pu16TabIdx_MADi_SST,
                                           MS_U16 *pu16TabIdx_MADi_EODiW,
                                           MS_U16 *pu16TabIdx_UCNR,
                                           MS_U16 *pu16TabIdx_UCDi,
                                           MS_U16 *pu16TabIdx_UCCTL,
                                           MS_U16 *pu16TabIdx_SwDriver,
                                           MS_U16 *pu16TabIdx_PostCCS,
                                           MS_U16 *pu16TabIdx_DHD)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetUCFeatureFromPQ PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.pu16TabIdx_MADi_DFK = pu16TabIdx_MADi_DFK;
    PQArgs.pu16TabIdx_MADi_SST = pu16TabIdx_MADi_SST;
    PQArgs.pu16TabIdx_MADi_EODiW = pu16TabIdx_MADi_EODiW;
    PQArgs.pu16TabIdx_UCNR = pu16TabIdx_UCNR;
    PQArgs.pu16TabIdx_UCDi = pu16TabIdx_UCDi;
    PQArgs.pu16TabIdx_UCCTL = pu16TabIdx_UCCTL;
    PQArgs.pu16TabIdx_SwDriver =pu16TabIdx_SwDriver ;
    PQArgs.pu16TabIdx_PostCCS =pu16TabIdx_PostCCS ;
    PQArgs.pu16TabIdx_DHD =pu16TabIdx_DHD ;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_UC_FEATURE_FROM_PQ, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_EnableUCFeature_U2(void* pInstance,PQ_WIN eWindow)
{
    MS_BOOL bRet = TRUE;
    MS_U32 u32SupportZNR = 0;
    MS_U32 u32SupportABF = 0;
    MS_U16 u16Index=0;
    MS_U16 u16IPIdx = 0x0000;
    MS_U16 u16IPTabIdx_Mode = 0x0000;
    MS_U16 u16TabIdx_UCCTL= 0x0000;
    ST_PQ_IPInfo stIPInfo;
    ST_PQ_IPInfo stUpdatedIPInfo;
    ST_PQ_IPInfo stGRuleIPInfo;
    ST_PQ_IPInfo stUpdatedGRuleIPInfo;
    memset(&stIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));

#if(ENABLE_PQ_MLOAD == 1)
    MDrv_PQ_Set_MLoadEn(eWindow, TRUE);
#endif
    if(eWindow == PQ_MAIN_WINDOW)
    {
#if (PQ_GRULE_DDR_SELECT_ENABLE == 1)
#if (PQ_DDR_SELECT_VER == 1)
        //do nothing, because ddr select rule has no uc item in version1
#elif (PQ_DDR_SELECT_VER == 2)
        MS_U16 u16PQ_DDRSELIdx = 0;
        MS_U16 u16TabIdx_MADi_DFK_DDR = 0xFFFF;
        MS_U16 u16TabIdx_MADi_SST_DDR = 0xFFFF;
        MS_U16 u16TabIdx_MADi_EODiW_DDR = 0xFFFF;
        MS_U16 u16TabIdx_UCCTL_DDR = 0xFFFF;
        if(bw_info.bDDR2 == FALSE) // DDR 3
        {
             if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
             {
                 PQMADi_DBG(printf("MDrv_PQ_GetDDRMirrorSelectInfo: DDR3 mirror\n"));
                 u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR3_Mirror_Main);
             }
             else
             {
                 PQMADi_DBG(printf("MDrv_PQ_GetDDRMirrorSelectInfo: DDR3 non-mirror\n"));
                 u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR3_Main);
             }
        }
        else // DDR 2
        {
             if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
             {
                 PQMADi_DBG(printf("MDrv_PQ_GetDDRMirrorSelectInfo: DDR2 mirror\n"));
                 u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR2_Mirror_Main);
             }
             else
             {
                 PQMADi_DBG(printf("MDrv_PQ_GetDDRMirrorSelectInfo: DDR2 non-mirror\n"));
                 u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR2_Main);
             }
        }

        u16TabIdx_MADi_DFK_DDR = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 2);
        u16TabIdx_MADi_SST_DDR = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 3);
        u16TabIdx_MADi_EODiW_DDR = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 4);
        u16TabIdx_UCCTL_DDR = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 5);
#endif
#endif
#if(PQ_XRULE_DISABLE_UC == 1)
        for(u16Index=0; u16Index<PQ_DisableUC_rule_IP_NUM_Main_Ex; u16Index++)
        {
            u16IPIdx = MDrv_PQ_GetXRuleIPIndex(pInstance, eWindow, E_XRULE_EX_DISABLE_UC, u16Index,PQ_XC0_EXT);
            stIPInfo.u16IPIdx = u16IPIdx;
            stIPInfo.enPQEngineType = PQ_XC0_EXT;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            if(stUpdatedIPInfo.u16IPIdx ==  PQ_IP_MADi_Main)
            {
                u16IPTabIdx_Mode =_gu16SetMemfmt_Madi[eWindow];
            }
            else if(stUpdatedIPInfo.u16IPIdx ==  PQ_IP_MADi_Motion_Main)
            {
               u16IPTabIdx_Mode =  _gu16SetMemfmt_Madi_Motion[eWindow] ;
            }
#if (PQ_GRULE_DDR_SELECT_ENABLE == 1)
#if (PQ_DDR_SELECT_VER == 1)
            //do nothing, because ddr select rule has no uc item in version1
#elif (PQ_DDR_SELECT_VER == 2)
            else if (stUpdatedIPInfo.u16IPIdx ==  PQ_IP_MADi_DFK_Main)
            {
                u16IPTabIdx_Mode = u16TabIdx_MADi_DFK_DDR;
            }
            else if (stUpdatedIPInfo.u16IPIdx ==  PQ_IP_MADi_SST_Main)
            {
                u16IPTabIdx_Mode =  u16TabIdx_MADi_SST_DDR;
            }
            else if (stUpdatedIPInfo.u16IPIdx ==  PQ_IP_MADi_EODiW_Main)
            {
                u16IPTabIdx_Mode = u16TabIdx_MADi_EODiW_DDR;
            }
            else if (stUpdatedIPInfo.u16IPIdx ==  PQ_IP_UC_CTL_Main)
            {
                u16IPTabIdx_Mode = u16TabIdx_UCCTL_DDR;
            }
#endif
#endif
            else
            {
                u16IPTabIdx_Mode = MDrv_PQ_GetTableIndex_Ex_U2(pInstance,eWindow, u16IPIdx,PQ_XC0_EXT);
            }

            PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"[UC FEATURE]SRC: %u,  uc feature IPIdx:%u, IPIdx:%u, u8TabIdx:%u\n",
                _u16PQSrcType[eWindow], u16Index, u16IPIdx, u16IPTabIdx_Mode);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16IPTabIdx_Mode, u16IPIdx,PQ_XC0_EXT);
#if (PQ_UC_CTL == 1)
            if (stUpdatedIPInfo.u16IPIdx == PQ_IP_UC_CTL_Main)
            {
                u16TabIdx_UCCTL = u16IPTabIdx_Mode;
            }
#endif
        }
#elif (PQ_GRULE_GAME_MODE_ENABLE == 1)
        stGRuleIPInfo.u16IPIdx= PQ_GRule_GAME_MODE_Main;
        stGRuleIPInfo.u16GruleMaxIPNum = PQ_GRULE_GAME_MODE_IP_NUM_Main;
        stGRuleIPInfo.u16GruleLevelMainNum = PQ_GRULE_GAME_MODE_NUM_Main;
        stGRuleIPInfo.u16GruleLevelNum = PQ_GRULE_GAME_MODE_LVL_NUM_Main;
        stGRuleIPInfo.enPQEngineType = PQ_XC0_STD;
        MHal_PQ_RemappingGruleIP(pInstance, stGRuleIPInfo, &stUpdatedGRuleIPInfo, TRUE);
        if(MHal_PQ_Support_GroupIPBIN())
        {
            if(stUpdatedGRuleIPInfo.enPQEngineType == PQ_XC0_STD)
            {
                MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,stUpdatedGRuleIPInfo.u16IPIdx,PQ_BIN_STD_MAIN ,&stUpdatedGRuleIPInfo.u16GruleLevelMainNum, &stUpdatedGRuleIPInfo.u16GruleMaxIPNum ,&stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
            }
            else
            {
                MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,stUpdatedGRuleIPInfo.u16IPIdx,PQ_BIN_EXT_MAIN ,&stUpdatedGRuleIPInfo.u16GruleLevelMainNum, &stUpdatedGRuleIPInfo.u16GruleMaxIPNum ,&stPQBinHeaderInfo[PQ_BIN_EXT_MAIN]);
            }
        }
        for(u16Index=0; u16Index<stUpdatedGRuleIPInfo.u16GruleMaxIPNum; u16Index++)
        {
            u16IPIdx = MDrv_PQ_GetGRule_IPIndex_Ex_U2(pInstance, PQ_MAIN_WINDOW, stUpdatedGRuleIPInfo.u16IPIdx, u16Index, stUpdatedGRuleIPInfo.enPQEngineType);
            stIPInfo.u16IPIdx = u16IPIdx;
            stIPInfo.enPQEngineType = stUpdatedGRuleIPInfo.enPQEngineType;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            if(stUpdatedIPInfo.u16IPIdx ==  PQ_IP_MADi_Main)
            {
                u16IPTabIdx_Mode =_gu16SetMemfmt_Madi[eWindow];
            }
            else if(stUpdatedIPInfo.u16IPIdx ==  PQ_IP_MADi_Motion_Main)
            {
               u16IPTabIdx_Mode =  _gu16SetMemfmt_Madi_Motion[eWindow] ;
            }
#if (PQ_GRULE_DDR_SELECT_ENABLE == 1)
#if (PQ_DDR_SELECT_VER == 1)
            //do nothing, because ddr select rule has no uc item in version1
#elif (PQ_DDR_SELECT_VER == 2)
            else if (stUpdatedIPInfo.u16IPIdx ==  PQ_IP_MADi_DFK_Main)
            {
                u16IPTabIdx_Mode = u16TabIdx_MADi_DFK_DDR;
            }
            else if (stUpdatedIPInfo.u16IPIdx ==  PQ_IP_MADi_SST_Main)
            {
                u16IPTabIdx_Mode =  u16TabIdx_MADi_SST_DDR;
            }
            else if (stUpdatedIPInfo.u16IPIdx ==  PQ_IP_MADi_EODiW_Main)
            {
                u16IPTabIdx_Mode = u16TabIdx_MADi_EODiW_DDR;
            }
            else if (stUpdatedIPInfo.u16IPIdx ==  PQ_IP_UC_CTL_Main)
            {
                u16IPTabIdx_Mode = u16TabIdx_UCCTL_DDR;
            }
#endif
#endif
            else
            {
                u16IPTabIdx_Mode = MDrv_PQ_GetTableIndex_Ex_U2(pInstance,eWindow, u16IPIdx, stUpdatedGRuleIPInfo.enPQEngineType);
            }
#if defined(PQ_SUPPORT_GAMEMODE_VER ) && (PQ_SUPPORT_GAMEMODE_VER == 1 || PQ_SUPPORT_GAMEMODE_VER == 2)
            if(MDrv_PQ_GetGameMode_Status_U2(pInstance,eWindow) == TRUE)
            {
                MS_U16 u16PQ_GameMode_Lvl_Idx = PQ_GRule_Lvl_GAME_MODE_On_NonMirror_Main_Ex;
                if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
                {
                    u16PQ_GameMode_Lvl_Idx = PQ_GRule_Lvl_GAME_MODE_On_Mirror_Main_Ex;
                }
                else
                {
                    if(PQ_SUPPORT_GAMEMODE_VER == 2)
                    {
                        u16PQ_GameMode_Lvl_Idx = PQ_GRule_Lvl_GAME_MODE_On_NonMirror_FBL_Main_Ex;
                    }
                }
                u16IPTabIdx_Mode = MDrv_PQ_GetGRule_TableIndex_Ex(eWindow, stUpdatedGRuleIPInfo.u16IPIdx, u16PQ_GameMode_Lvl_Idx, u16Index, stUpdatedGRuleIPInfo.enPQEngineType);
            }
#endif
            PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[UC FEATURE]SRC: %u,  uc feature IPIdx:%u, IPIdx:%u, u8TabIdx:%u\n",
                _u16PQSrcType[eWindow], u16Index, u16IPIdx, u16IPTabIdx_Mode);

            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16IPTabIdx_Mode, u16IPIdx, stUpdatedGRuleIPInfo.enPQEngineType);
#if (PQ_UC_CTL == 1)
            if (stUpdatedIPInfo.u16IPIdx == PQ_IP_UC_CTL_Main)
            {
                u16TabIdx_UCCTL = u16IPTabIdx_Mode;
            }
#endif
        }
#else
        //Not Support control UC feature
#endif
        if(_gIsEnable8LBMode)
        {
#if (PQ_UC_CTL == 1)
            if(u16TabIdx_UCCTL == PQ_IP_UC_CTL_OFF_Main)
            {
                MApi_XC_Set_OPWriteOffEnableToReg(ENABLE, MAIN_WINDOW);
            }
            else
            {
                MApi_XC_Set_OPWriteOffEnableToReg(DISABLE, MAIN_WINDOW);
            }
#endif
        }
    }
#if(PQ_ENABLE_PIP == 1)
    else // Sub Window
    {
       // Sub wibndow not support UC Feature, Do Nothing
    }
#endif
    if(_gIsEnable8LBMode)
    {
        MDrv_PQBin_Enable8LBMode(pInstance,TRUE);
    }

    MApi_XC_GetChipCaps(E_XC_SUPPORT_ZNR, &u32SupportZNR, sizeof(MS_U32));
    MApi_XC_GetChipCaps(E_XC_SUPPORT_ABF, &u32SupportABF, sizeof(MS_U32));
    if((u32SupportZNR!=0) && (u32SupportABF!=0))
    {
        MHal_PQ_SetABFPatch(_stMode_Info[eWindow].bInterlace);
    }

#if(ENABLE_PQ_MLOAD == 1)
    MDrv_PQ_Set_MLoadEn(eWindow, FALSE);
#endif
    return bRet;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_EnableUCFeature(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_EnableUCFeature PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_ENABLE_UC_FEATURE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_SetUCFeature_U2(void* pInstance,
                                           PQ_WIN eWindow,
                                           MS_U16 u16TabIdx_MADi,
                                           MS_U16 u16TabIdx_MADi_Mot,
                                           MS_U16 u16TabIdx_MADi_DFK,
                                           MS_U16 u16TabIdx_MADi_SST,
                                           MS_U16 u16TabIdx_MADi_EODiW,
                                           MS_U16 u16TabIdx_UCNR,
                                           MS_U16 u16TabIdx_UCDi,
                                           MS_U16 u16TabIdx_UCCTL,
                                           MS_U16 u16TabIdx_SwDriver,
                                           MS_U16 u16TabIdx_PostCCS,
                                           MS_U16 u16TabIdx_DHD
                                           )
{
    MS_BOOL bRet = TRUE;
    MS_U32 u32SupportZNR = 0;
    MS_U32 u32SupportABF = 0;
    ST_PQ_IPInfo stIPInfo;
    ST_PQ_IPInfo stUpdatedIPInfo;
    memset(&stIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedIPInfo, 0, sizeof(ST_PQ_IPInfo));
#if PQ_UC_CTL

#if(ENABLE_PQ_MLOAD)
    MDrv_PQ_Set_MLoadEn(eWindow, TRUE);
#endif

    PQMADi_DBG(printf("MDrv_PQ_SetUCFeature(%u, %u, %u,%u,%u,%u,%u,%u,%u,%u,%u)\n",
                                           u16TabIdx_MADi,
                                           u16TabIdx_MADi_Mot,
                                           u16TabIdx_MADi_DFK,
                                           u16TabIdx_MADi_SST,
                                           u16TabIdx_MADi_EODiW,
                                           u16TabIdx_UCNR,
                                           u16TabIdx_UCDi,
                                           u16TabIdx_UCCTL,
                                           u16TabIdx_SwDriver,
                                           u16TabIdx_PostCCS,
                                           u16TabIdx_DHD));

    //no need to store madi, madi_mot here, because MDrv_PQ_LoadTable_U2(pInstance,) has store it.
    _u16MADi_DFK_Current[eWindow] = u16TabIdx_MADi_DFK;
    _u16MADi_SST_Current[eWindow] = u16TabIdx_MADi_SST;
    _u16MADi_EODiW_Current[eWindow] = u16TabIdx_MADi_EODiW;
    _u16UCDi_Current[eWindow] = u16TabIdx_UCDi;
    _u16UCCTL_Current[eWindow] = u16TabIdx_UCCTL;
    _u16UCNR_Current[eWindow] = u16TabIdx_UCNR;
    _u16SwDriver_Current[eWindow] = u16TabIdx_SwDriver;
    _u16PostCCS_Current[eWindow] = u16TabIdx_PostCCS;
    _u16DHD_Current[eWindow] = u16TabIdx_DHD;

    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(0xFFFF != u16TabIdx_MADi)
        {
            stIPInfo.u16IPIdx = (MS_U16)PQ_IP_MADi_Main;
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx_MADi, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

#if (PQ_MADI_DFK == 1)
        if(0xFFFF != u16TabIdx_MADi_Mot)
        {
            stIPInfo.u16IPIdx = (MS_U16)PQ_IP_MADi_Motion_Main;
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx_MADi_Mot, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }
#endif

        if(0xFFFF != u16TabIdx_MADi_DFK)
        {
            stIPInfo.u16IPIdx = (MS_U16)PQ_IP_MADi_DFK_Main;
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx_MADi_DFK, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

        if(0xFFFF != u16TabIdx_MADi_SST)
        {
            stIPInfo.u16IPIdx = (MS_U16)PQ_IP_MADi_SST_Main;
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx_MADi_SST, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

        if(0xFFFF != u16TabIdx_MADi_EODiW)
        {
            stIPInfo.u16IPIdx = (MS_U16)PQ_IP_MADi_EODiW_Main;
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx_MADi_EODiW, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

        if(0xFFFF != u16TabIdx_UCNR)
        {
            stIPInfo.u16IPIdx = (MS_U16)PQ_IP_UCNR_Main;
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx_UCNR, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

        if(0xFFFF != u16TabIdx_UCDi)
        {
            stIPInfo.u16IPIdx = (MS_U16)PQ_IP_UCDi_Main;
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx_UCDi, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

        if(0xFFFF != u16TabIdx_UCCTL)
        {
            stIPInfo.u16IPIdx = (MS_U16)PQ_IP_UC_CTL_Main;
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx_UCCTL, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

        if(0xFFFF != u16TabIdx_SwDriver)
        {
            stIPInfo.u16IPIdx = (MS_U16)PQ_IP_SwDriver_Main;
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx_SwDriver, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }
#if (PQ_SUPPORT_POSTCCS == 1)
        if(0xFFFF != u16TabIdx_PostCCS)
        {
            MDrv_PQ_LoadTable_U2(pInstance, eWindow, u16TabIdx_PostCCS, PQ_IP_PostCCS_Main);
        }
#endif
#if PQ_SUPPORT_MAIN_DHD
        if(0xFFFF != u16TabIdx_DHD)
        {
            MDrv_PQ_LoadTable_U2(pInstance, eWindow, u16TabIdx_DHD, PQ_IP_DHD_Main);
        }
#endif
        if(_gIsEnable8LBMode)
        {
            if(u16TabIdx_UCCTL == PQ_IP_UC_CTL_OFF_Main)
            {
                MApi_XC_Set_OPWriteOffEnableToReg(ENABLE, MAIN_WINDOW);
            }
            else
            {
                MApi_XC_Set_OPWriteOffEnableToReg(DISABLE, MAIN_WINDOW);
            }
        }
    }
#if(PQ_ENABLE_PIP)
    else
    {
        if(0xFFFF != u16TabIdx_MADi)
        {
            stIPInfo.u16IPIdx = MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI);
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx_MADi, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

        if(0xFFFF != u16TabIdx_MADi_Mot)
        {
            stIPInfo.u16IPIdx = MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI_MOTION);
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance,eWindow, u16TabIdx_MADi_Mot, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

        if(0xFFFF != u16TabIdx_MADi_DFK)
        {
            stIPInfo.u16IPIdx = MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI_DFK);
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance,eWindow, u16TabIdx_MADi_DFK, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

        if(0xFFFF != u16TabIdx_MADi_SST)
        {
            stIPInfo.u16IPIdx = MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MADI_SST);
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance,eWindow, u16TabIdx_MADi_SST, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

        if(0xFFFF != u16TabIdx_SwDriver)
        {
            stIPInfo.u16IPIdx = MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_SWDRIVER);
            stIPInfo.enPQEngineType = PQ_XC0_STD;
            MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);
            MDrv_PQ_LoadTable_Ex_U2(pInstance,eWindow, u16TabIdx_SwDriver, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
        }

#if PQ_SUPPORT_SUB_POSTCCS
        if(0xFFFF != u16TabIdx_PostCCS)
        {
            MDrv_PQ_LoadTable_U2(pInstance,eWindow, u16TabIdx_PostCCS, PQ_IP_PostCCS_Sub);
        }
#endif

#if PQ_SUPPORT_SUB_DHD
        if(0xFFFF != u16TabIdx_DHD)
        {
            MDrv_PQ_LoadTable_U2(pInstance,eWindow, u16TabIdx_DHD, PQ_IP_DHD_Sub);
        }
#endif
    }
#endif

    if(_gIsEnable8LBMode)
    {
        MDrv_PQBin_Enable8LBMode(pInstance,TRUE);
    }

#if(ENABLE_PQ_MLOAD)
    MDrv_PQ_Set_MLoadEn(eWindow, FALSE);
#endif

    bRet = TRUE;

#if (PQ_GRULE_GAME_MODE_ENABLE == 0)
    //adjust wrbankmapping for special madi mode such as:25_4R_MC
    if((u16TabIdx_UCNR == PQ_IP_UCNR_OFF_Main)
       && (u16TabIdx_UCDi == PQ_IP_UCDi_OFF_Main)
       && (u16TabIdx_UCCTL == PQ_IP_UC_CTL_OFF_Main))
    {
        if(MApi_XC_GetWRBankMappingNum(eWindow) == 0x0)
        {
            if(_stMode_Info[eWindow].bInterlace)
            {
                MApi_XC_SetWRBankMappingNum(0x2, eWindow);
            }
            else
            {
                MApi_XC_SetWRBankMappingNum(0x1, eWindow);
            }
            _bNeedRestoreWRBankMappingNum = TRUE;
        }
    }
    else if(_bNeedRestoreWRBankMappingNum)
    {
        MApi_XC_SetWRBankMappingNum(0x0, eWindow);
        _bNeedRestoreWRBankMappingNum = FALSE;
    }
#endif

#else
    bRet = FALSE;
#endif

    MApi_XC_GetChipCaps(E_XC_SUPPORT_ZNR, &u32SupportZNR, sizeof(MS_U32));
    MApi_XC_GetChipCaps(E_XC_SUPPORT_ABF, &u32SupportABF, sizeof(MS_U32));
    if((u32SupportZNR!=0) && (u32SupportABF!=0))
    {
        MHal_PQ_SetABFPatch(_stMode_Info[eWindow].bInterlace);
    }

    return bRet;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_SetUCFeature(PQ_WIN eWindow,
                                           MS_U16 u16TabIdx_MADi,
                                           MS_U16 u16TabIdx_MADi_Mot,
                                           MS_U16 u16TabIdx_MADi_DFK,
                                           MS_U16 u16TabIdx_MADi_SST,
                                           MS_U16 u16TabIdx_MADi_EODiW,
                                           MS_U16 u16TabIdx_UCNR,
                                           MS_U16 u16TabIdx_UCDi,
                                           MS_U16 u16TabIdx_UCCTL,
                                           MS_U16 u16TabIdx_SwDriver,
                                           MS_U16 u16TabIdx_PostCCS,
                                           MS_U16 u16TabIdx_DHD
                                           )
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SetUCFeature PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.pu16TabIdx_MADi = u16TabIdx_MADi;
    PQArgs.pu16TabIdx_MADi_Mot = u16TabIdx_MADi_Mot;
    PQArgs.pu16TabIdx_MADi_DFK = u16TabIdx_MADi_DFK;
    PQArgs.pu16TabIdx_MADi_SST = u16TabIdx_MADi_SST;
    PQArgs.pu16TabIdx_MADi_EODiW = u16TabIdx_MADi_EODiW;
    PQArgs.pu16TabIdx_UCNR = u16TabIdx_UCNR;
    PQArgs.pu16TabIdx_UCDi = u16TabIdx_UCDi;
    PQArgs.pu16TabIdx_UCCTL = u16TabIdx_UCCTL;
    PQArgs.pu16TabIdx_SwDriver =u16TabIdx_SwDriver ;
    PQArgs.pu16TabIdx_PostCCS =u16TabIdx_PostCCS ;
    PQArgs.pu16TabIdx_DHD =u16TabIdx_DHD ;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_UC_FEATURE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_SetDMSV12L_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bEnable)
{
#ifdef UFO_XC_PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
    MS_U32 u32IsSupportSC1PIP = 0;
    if (MApi_XC_GetChipCaps(E_XC_SUPPORT_PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB, &u32IsSupportSC1PIP, sizeof(MS_U32)) != E_APIXC_RET_OK)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MApi_XC_GetChipCaps return fail!\n");
        return FALSE;
    }

    if (u32IsSupportSC1PIP == 1)
    {
        // this is not need to control 12 line mode when sc1 as pip
        return TRUE;
    }
#endif

#if PQ_EN_DMS_SW_CTRL
    MS_U16 u16TabIdx, u16IPIdx;

    if(bEnable == TRUE)
    {
        u16TabIdx = PQ_IP_DMS_V_12L_ON_Main;
    }
    else
    {
        u16TabIdx = PQ_IP_DMS_V_12L_OFF_Main;
    }
    u16IPIdx = PQ_IP_DMS_V_12L_Main;
    eWindow = PQ_MAIN_WINDOW;

#if(ENABLE_PQ_MLOAD)
    MDrv_PQ_Set_MLoadEn(eWindow, TRUE);
#endif

    MDrv_PQ_LoadTable_U2(pInstance,eWindow, u16TabIdx, u16IPIdx);

#if(ENABLE_PQ_MLOAD)
    MDrv_PQ_Set_MLoadEn(eWindow, FALSE);
#endif
    _bDMSV12L_PIPEn = bEnable;

#else
    UNUSED(eWindow);
    UNUSED(bEnable);
#endif

    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_SetDMSV12L(PQ_WIN eWindow, MS_BOOL bEnable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SetDMSV12L PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bEnable = bEnable;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_DMSV12L, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_GetDMSV12LFromXRuleTable_U2(void* pInstance,PQ_WIN eWindow)
{
    UNUSED(eWindow);

    return _bDMSV12L_PQConfigEn;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetDMSV12LFromXRuleTable(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetDMSV12LFromXRuleTable PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_DMSV12L_FROM_XRULE_TABLE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

E_DRVPQ_ReturnValue MDrv_PQ_GetLibVer_U2(void* pInstance,const MSIF_Version **ppVersion)
{
    if(ppVersion == NULL)
    {
        return E_DRVPQ_RET_FAIL;
    }

    *ppVersion = &_drv_pq_version;
    return E_DRVPQ_RET_OK;
}
E_DRVPQ_ReturnValue SYMBOL_WEAK MDrv_PQ_GetLibVer(const MSIF_Version **ppVersion)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetLibVer PQArgs;
    PQArgs.ppVersion = ppVersion;
    PQArgs.bReturnValue = E_DRVPQ_RET_FAIL;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_LIB_VER, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return E_DRVPQ_RET_FAIL;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

static void _MDrv_PQ_GetPQInfo(void *pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, MS_RIU_UNIT *pstTableInfo, PQ_ENGINE_TYPE ePQEngineType)
{
    MS_U8 u8PanelIdx = 0;
    PQ_BIN_TYPE enPQBinType = MAX_PQ_BIN_NUM;

    if (eWindow == PQ_MAIN_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_STD_MAIN;
                }
                else
                {
                    MDrv_PQ_LoadTable_(MAIN, pInstance,(MS_U8)u16TabIdx, u16PQIPIdx);
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_EXT_MAIN;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerMainPQEn)
                {
                    u8PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    enPQBinType = PQ_BIN_CUSTOMER_MAIN;
                }
                break;
            case PQ_XC0_UFSC:
                if (_bUFSCPQEn)
                {
                    u8PanelIdx = PQ_BIN_UFSC_PANEL_INDEX;
                    enPQBinType = PQ_BIN_UFSC;
                }
                break;
            case PQ_XC0_TMO:
                if (_bTMOPQEn)
                {
                    u8PanelIdx = MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO);
                    enPQBinType = PQ_BIN_TMO;
                }
                break;
            default:
                break;
        }
    }
#if (PQ_ENABLE_PIP)
    else if (eWindow == PQ_SUB_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_STD_SUB;
                }
                else
                {
                    MDrv_PQ_LoadTable_(SUB,pInstance, (MS_U8)u16TabIdx, u16PQIPIdx);
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_EXT_SUB;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerSubPQEn)
                {
                    u8PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    enPQBinType = PQ_BIN_CUSTOMER_SUB;
                }
                break;
            case PQ_XC0_UFSC:
                break;
            default:
                break;
        }

    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }

    if (enPQBinType != MAX_PQ_BIN_NUM)
    {
        MDrv_PQBin_GetTableValue(pInstance,
                                u8PanelIdx,
                                u16TabIdx,
                                u16PQIPIdx,
                                &stPQBinHeaderInfo[enPQBinType],
                                pstTableInfo,
                                eWindow);
    }
}

static MS_U8 _MDrv_PQ_GetMemConfig(void *pInstance, PQ_WIN enWindow)
{
    MS_U8 u8MemConfig = 0;
    MS_U16 u16TabIdx = 0;
    MS_BOOL bIPMConfigDefineEn = FALSE;
    MS_BOOL bY8Only = FALSE;
    MS_BOOL bY8M4Only = FALSE;
    MS_BOOL bIPM444Read = FALSE;
    MS_BOOL bYUV444_8Bit = FALSE;
    MS_BOOL bYUV444_10Bit = FALSE;
    MS_BOOL bBOBYmr10BitEn = FALSE;
    MS_BOOL bBOBYmr8BitEn = FALSE;
    MS_BOOL bMEMMode5To4 = FALSE;
    MS_BOOL bMEMMode5To6 = FALSE;
    MS_BOOL bMEMMode5To7 = FALSE;
    MS_BOOL bMEMMode6To7 = FALSE;
    MS_U8 u8DeIntlMd = 0;
    MS_RIU_UNIT stTableInfo;
    ST_PQ_IPInfo stIPInfo;
    ST_PQ_IPInfo stUpdatedIPInfo;
    memset(&stTableInfo,0,sizeof(MS_RIU_UNIT));
    memset(&stIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedIPInfo, 0, sizeof(ST_PQ_IPInfo));

#if (PQ_SUPPORT_ZNR)
    stTableInfo.u32Addr = REG_SC_BK12_02_H;
    stTableInfo.u32Mask = BIT(0);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_SCM_Format_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_SCM_Format_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bIPMConfigDefineEn = ((MS_U8)stTableInfo.u32Value & BIT(0)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_04_L;
    stTableInfo.u32Mask = BIT(2);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Default_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Default_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bY8Only = ((MS_U8)stTableInfo.u32Value & BIT(2)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_04_L;
    stTableInfo.u32Mask = BIT(3);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Default_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Default_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bY8M4Only = ((MS_U8)stTableInfo.u32Value & BIT(3)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_04_H;
    stTableInfo.u32Mask = BIT(5);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Default_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Default_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bIPM444Read = ((MS_U8)stTableInfo.u32Value & BIT(5)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_L;
    stTableInfo.u32Mask = BIT(4);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Default_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Default_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bYUV444_8Bit = ((MS_U8)stTableInfo.u32Value & BIT(4)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_L;
    stTableInfo.u32Mask = BIT(5);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_SCM_Format_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_SCM_Format_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bYUV444_10Bit = ((MS_U8)stTableInfo.u32Value & BIT(5)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_04_L;
    stTableInfo.u32Mask = BIT(1);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Default_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Default_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bBOBYmr10BitEn = ((MS_U8)stTableInfo.u32Value & BIT(1)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_04_L;
    stTableInfo.u32Mask = BIT(0);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Default_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Default_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bBOBYmr8BitEn = ((MS_U8)stTableInfo.u32Value & BIT(0)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_L;
    stTableInfo.u32Mask = BIT(0);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Default_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Default_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bMEMMode5To4 = ((MS_U8)stTableInfo.u32Value & BIT(0)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_L;
    stTableInfo.u32Mask = BIT(1);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Default_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Default_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bMEMMode5To6 = ((MS_U8)stTableInfo.u32Value & BIT(1)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_L;
    stTableInfo.u32Mask = BIT(2);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Default_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Default_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bMEMMode5To7 = ((MS_U8)stTableInfo.u32Value & BIT(2)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_L;
    stTableInfo.u32Mask = BIT(3);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Default_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Default_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bMEMMode6To7 = ((MS_U8)stTableInfo.u32Value & BIT(3)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_H;
    stTableInfo.u32Mask = 0x7;
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_SCM_Format_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_SCM_Format_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    u8DeIntlMd = ((MS_U8)stTableInfo.u32Value & 0x7);
#else
    stTableInfo.u32Addr = REG_SC_BK12_02_H;
    stTableInfo.u32Mask = BIT(0);
    stIPInfo.u16IPIdx = PQ_IP_MADi_Main;
    stIPInfo.enPQEngineType = PQ_XC0_STD;
    MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
    bIPMConfigDefineEn = ((MS_U8)stTableInfo.u32Value & BIT(0)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_04_L;
    stTableInfo.u32Mask = BIT(2);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
    bY8Only = ((MS_U8)stTableInfo.u32Value & BIT(2)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_04_L;
    stTableInfo.u32Mask = BIT(3);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
    bY8M4Only = ((MS_U8)stTableInfo.u32Value & BIT(3)) ? TRUE : FALSE;

    bIPM444Read = FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_L;
    stTableInfo.u32Mask = BIT(4);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_MemFormat_Main, PQ_XC0_STD);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_MemFormat_Main, &stTableInfo, PQ_XC0_STD);
    bYUV444_8Bit = ((MS_U8)stTableInfo.u32Value & BIT(4)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_L;
    stTableInfo.u32Mask = BIT(5);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_MemFormat_Main, PQ_XC0_STD);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_MemFormat_Main, &stTableInfo, PQ_XC0_STD);
    bYUV444_10Bit = ((MS_U8)stTableInfo.u32Value & BIT(5)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_04_L;
    stTableInfo.u32Mask = BIT(1);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
    bBOBYmr10BitEn = ((MS_U8)stTableInfo.u32Value & BIT(1)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_04_L;
    stTableInfo.u32Mask = BIT(0);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
    bBOBYmr8BitEn = ((MS_U8)stTableInfo.u32Value & BIT(0)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_L;
    stTableInfo.u32Mask = BIT(0);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
    bMEMMode5To4 = ((MS_U8)stTableInfo.u32Value & BIT(0)) ? TRUE : FALSE;

    bMEMMode5To6 = FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_L;
    stTableInfo.u32Mask = BIT(2);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
    bMEMMode5To7 = ((MS_U8)stTableInfo.u32Value & BIT(2)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_L;
    stTableInfo.u32Mask = BIT(3);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
    bMEMMode6To7 = ((MS_U8)stTableInfo.u32Value & BIT(3)) ? TRUE : FALSE;

    stTableInfo.u32Addr = REG_SC_BK12_01_H;
    stTableInfo.u32Mask = 0x7;
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
    u8DeIntlMd = ((MS_U8)stTableInfo.u32Value & 0x7);
#endif

    if(bIPMConfigDefineEn)
    {
#if (PQ_SUPPORT_ZNR)
        stTableInfo.u32Addr = REG_SC_BK12_02_L;
        stTableInfo.u32Mask = 0x0F;
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_SCM_Format_Main_Ex, PQ_XC0_EXT);
        _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_SCM_Format_Main_Ex, &stTableInfo, PQ_XC0_EXT);
        u8MemConfig = ((MS_U8)stTableInfo.u32Value & 0x0F);

        stTableInfo.u32Addr = REG_SC_BK11_02_L;
        stTableInfo.u32Mask = 0x03;
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Default_Main_Ex, PQ_XC0_EXT);
        _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Default_Main_Ex, &stTableInfo, PQ_XC0_EXT);
        u8MemConfig |= (((MS_U8)stTableInfo.u32Value & 0x03) << PQ_4_BIT);
#else
        stTableInfo.u32Addr = REG_SC_BK12_02_L;
        stTableInfo.u32Mask = 0x0F;
        _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
        u8MemConfig = ((MS_U8)stTableInfo.u32Value & 0x0F);

        stTableInfo.u32Addr = REG_SC_BK11_02_L;
        stTableInfo.u32Mask = 0x03;
        _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
        u8MemConfig |= (((MS_U8)stTableInfo.u32Value & 0x03) << PQ_4_BIT);
#endif
    }
    else if(bY8Only)
    {
        u8MemConfig = MEM_CONFIG_YY;
    }
    else if(bY8M4Only)
    {
        u8MemConfig = MEM_CONFIG_YMY;
    }
    else if(bIPM444Read)
    {
        u8MemConfig = MEM_CONFIG_YCLMYC;
    }
    else if(bYUV444_8Bit)
    {
        u8MemConfig = MEM_CONFIG_RGB;
    }
    else if(bYUV444_10Bit)
    {
        u8MemConfig = MEM_CONFIG_RGBL;
    }
    else if(bBOBYmr10BitEn)
    {
        u8MemConfig = MEM_CONFIG_YCLMYC;
    }
    else if(bBOBYmr8BitEn)
    {
        u8MemConfig = MEM_CONFIG_YCMYC;
    }
    else if(bMEMMode5To4)
    {
        u8MemConfig = MEM_CONFIG_YCXMYC;
    }
    else if(bMEMMode5To6)
    {
        u8MemConfig = MEM_CONFIG_YCLXYC;
    }
    else if(bMEMMode5To7)
    {
        u8MemConfig = MEM_CONFIG_YCXXYC;
    }
    else if(bMEMMode6To7)
    {
        u8MemConfig = MEM_CONFIG_YCXYC;
    }
    else if(u8DeIntlMd == 0)
    {
        u8MemConfig = MEM_CONFIG_YCYC;
    }
    else if(u8DeIntlMd == 1)
    {
        u8MemConfig = MEM_CONFIG_YCLYC;
    }
    else if(u8DeIntlMd == 2)
    {
        u8MemConfig = MEM_CONFIG_YCYC;
    }
    else if(u8DeIntlMd == 3)
    {
        u8MemConfig = MEM_CONFIG_YCYC;
    }
    else if(u8DeIntlMd == 4)
    {
        u8MemConfig = MEM_CONFIG_YCMYC;
    }
    else if(u8DeIntlMd == 5)
    {
        u8MemConfig = MEM_CONFIG_YCLMYC;
    }
    else if(u8DeIntlMd == 6)
    {
        u8MemConfig = MEM_CONFIG_YCLYC;
    }
    else
    {
        u8MemConfig = MEM_CONFIG_YCYC;
    }

    return u8MemConfig;
}

static MS_U8 _MDrv_PQ_GetBitPerPixel_Y(void *pInstance, PQ_WIN enWindow)
{
    MS_BOOL bRFBL = FALSE;
    MS_U8 u8BitPerPixel_Y = 0;
    MS_U8 u8MemConfig = 0;
    MS_U16 u16TabIdx = 0;
    MS_RIU_UNIT stTableInfo;
    ST_PQ_IPInfo stIPInfo;
    ST_PQ_IPInfo stUpdatedIPInfo;
    memset(&stTableInfo,0,sizeof(MS_RIU_UNIT));
    memset(&stIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedIPInfo, 0, sizeof(ST_PQ_IPInfo));

#if (PQ_SUPPORT_ZNR)
    stTableInfo.u32Addr = REG_SC_BK12_04_H;
    stTableInfo.u32Mask = BIT(3);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, PQ_IP_ZNR_Control_Main_Ex, PQ_XC0_EXT);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Control_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bRFBL = ((MS_U8)stTableInfo.u32Value & BIT(3)) ? TRUE : FALSE;

    stTableInfo.u32Addr = IPM_WRITE_REQ_OFF;
    stTableInfo.u32Mask = BIT(0);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, PQ_IP_ZNR_Control_Main_Ex, &stTableInfo, PQ_XC0_EXT);
    bRFBL = (bRFBL && ((MS_U8)stTableInfo.u32Value & BIT(0)) ? TRUE : FALSE) ? TRUE : FALSE;
#else
    stIPInfo.u16IPIdx = PQ_IP_MADi_Main;
    stIPInfo.enPQEngineType = PQ_XC0_STD;
    MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
    stTableInfo.u32Addr = REG_SC_BK12_04_H;
    stTableInfo.u32Mask = BIT(3);
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(enWindow, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
    bRFBL = ((MS_U8)stTableInfo.u32Value & BIT(3)) ? TRUE : FALSE;

    stTableInfo.u32Addr = IPM_WRITE_REQ_OFF;
    stTableInfo.u32Mask = BIT(0);
    _MDrv_PQ_GetPQInfo(pInstance, enWindow, u16TabIdx, stUpdatedIPInfo.u16IPIdx, &stTableInfo, stUpdatedIPInfo.enPQEngineType);
    bRFBL = (bRFBL && ((MS_U8)stTableInfo.u32Value & BIT(0)) ? TRUE : FALSE) ? TRUE : FALSE;
#endif

    if(bRFBL)
    { // RFBL
        u8BitPerPixel_Y = 0;
    }
    else
    {
        u8MemConfig = _MDrv_PQ_GetMemConfig(pInstance, enWindow);
        switch(u8MemConfig)
        {
            case MEM_CONFIG_YCYCYC:
            case MEM_CONFIG_YCXYCXYC:
                u8BitPerPixel_Y = PQ_6_BIT; // YYY
                break;
            case MEM_CONFIG_YCYC:
            case MEM_CONFIG_YCXYC:
            case MEM_CONFIG_YCLYC:
            case MEM_CONFIG_YCXXYC:
            case MEM_CONFIG_YCLXYC:
            case MEM_CONFIG_YY:
                u8BitPerPixel_Y = PQ_8_BIT; // YY
                break;
            case MEM_CONFIG_YCMYCMYC:
                u8BitPerPixel_Y = PQ_10_BIT; // YMYMY
                break;
            case MEM_CONFIG_YCMYC:
            case MEM_CONFIG_YCXMYC:
            case MEM_CONFIG_YCLMYC:
            case MEM_CONFIG_YMY:
                 u8BitPerPixel_Y = PQ_12_BIT; // YMY
                 break;
            default:
                u8BitPerPixel_Y = 0;
                break;
        }
    }

    return u8BitPerPixel_Y;
}

static void _MDrv_PQ_GetGroupNum(void *pInstance, PQ_WIN enWindow, MS_U8 *u8GroupNum, MS_U8 *u8GroupNumCmp)
{
    MS_U8 u8MemConfig = _MDrv_PQ_GetMemConfig(pInstance, enWindow);
    switch(u8MemConfig)
    {
        case MEM_CONFIG_YCYCYC:
            *u8GroupNum = 6;
            *u8GroupNumCmp = 3; // YYY
            break;
        case MEM_CONFIG_YCXYCXYC:
            *u8GroupNum = 8;
            *u8GroupNumCmp = 3; // YYY
            break;
        case MEM_CONFIG_YCYC:
            *u8GroupNum = 8;
            *u8GroupNumCmp = 4; // YYYY
            break;
        case MEM_CONFIG_YCXYC:
            *u8GroupNum = 10;
            *u8GroupNumCmp = 4; // YYYY
            break;
        case MEM_CONFIG_YCXXYC:
            *u8GroupNum = 12;
            *u8GroupNumCmp = 4; // YYYY
            break;
        case MEM_CONFIG_YY:
            *u8GroupNum = 4;
            *u8GroupNumCmp = 0;
            break;
        case MEM_CONFIG_YCMYCMYC:
            *u8GroupNum = 8;
            *u8GroupNumCmp = 5; // YMYYMY
            break;
        case MEM_CONFIG_YCMYC:
            *u8GroupNum = 10;
            *u8GroupNumCmp = 6; // YYYY
            break;
        case MEM_CONFIG_YCXMYC:
        case MEM_CONFIG_YCLMYC:
            *u8GroupNum = 12;
            *u8GroupNumCmp = 6; // YMYYMY
            break;
        case MEM_CONFIG_YMY:
            *u8GroupNum = 6;
            *u8GroupNumCmp = 0;
            break;
        case MEM_CONFIG_RGB:
            *u8GroupNum = 3;
            *u8GroupNumCmp = 3;
            break;
        case MEM_CONFIG_RGBL:
            *u8GroupNum = 4;
            *u8GroupNumCmp = 4;
            break;
        default:
            *u8GroupNum = 0;
            *u8GroupNumCmp = 0;
            break;
    }
}

PQ_ReturnValue MDrv_PQ_GetInfo_U2(void* pInstance,MS_PQ_INFO *pInfo)
{
    PQ_ReturnValue enRet = E_PQ_RET_OK;
    MS_U32 u32CopiedLength = 0;
    MS_U16 u16TabIdx = 0;
    MS_BOOL bIPM420CmpEnable = FALSE, bOPM420CmpEnable = FALSE;
    MS_U8 u8MemConfig = 0;
    MS_RIU_UNIT stTableInfo;
    memset(&stTableInfo,0,sizeof(MS_RIU_UNIT));
    if((NULL == pInfo) || (pInfo->u32Length == 0))
    {
        //We cosider compatible operation form version2 , so reject the info init when version invalid
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pInfo is NULL or struct length is 0\n");
        return E_PQ_RET_FAIL;
    }

    //new AP + old lib, only the length corresponding to old lib has meaning.
    if((pInfo->u32Version > ST_PQ_INFO_VERSION) || (pInfo->u32Length > sizeof(MS_PQ_INFO)))
    {
        pInfo->u32Version = ST_PQ_INFO_VERSION;
        u32CopiedLength =  sizeof(MS_PQ_INFO);
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"this old version PQ lib has only length:%u driver status!!\n",u32CopiedLength);
    }

    //old AP + new lib, driver shouldn't access to the space which doesn't exist in old structure
    if((pInfo->u32Version < ST_PQ_INFO_VERSION) || (pInfo->u32Length < sizeof(MS_PQ_INFO)))
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"new version PQ lib shouldn't access to the space which doesn't exist in old structure!!\n");
    }

    pInfo->u16Input_Src_Num = _info.u16Input_Src_Num;
    pInfo->u8IP_Num = _info.u8IP_Num;

    if(pInfo->u32Version >= 1)
    {
#if (PQ_SUPPORT_MADI_VR)
#if (PQ_SUPPORT_ZNR)
        stTableInfo.u32Addr = IPM_420CMP_EN;
        stTableInfo.u32Mask = BIT(7);
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_ZNR_SCM_Format_Main_Ex, PQ_XC0_EXT);
        _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_SCM_Format_Main_Ex, &stTableInfo, PQ_XC0_EXT);
        bIPM420CmpEnable = ((MS_U8)stTableInfo.u32Value & BIT(7)) ? TRUE : FALSE;

        stTableInfo.u32Addr = OPM_420CMP_EN;
        stTableInfo.u32Mask = BIT(7);
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_ZNR_SCM_Format_Main_Ex, PQ_XC0_EXT);
        _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_SCM_Format_Main_Ex, &stTableInfo, PQ_XC0_EXT);
        bOPM420CmpEnable = ((MS_U8)stTableInfo.u32Value & BIT(7)) ? TRUE : FALSE;
        if(MHal_PQ_IsSupportZNRControlVR())
        {
            stTableInfo.u32Addr = VR_FRAME_COUNT;
            stTableInfo.u32Mask = 0xFF;
            u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_ZNR_Control_VR_Main_Ex, PQ_XC0_EXT);
            _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_Control_VR_Main_Ex, &stTableInfo, PQ_XC0_EXT);
            pInfo->u8FrameNum = (MS_U8)stTableInfo.u32Value;

            if(pInfo->u32Version >= 2)
            {
                stTableInfo.u32Addr = VR_FB_LEVEL;
                stTableInfo.u32Mask = 0x3;
                u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_ZNR_Control_VR_Main_Ex, PQ_XC0_EXT);
                _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_Control_VR_Main_Ex, &stTableInfo, PQ_XC0_EXT);
                pInfo->u8FBLevel= (MS_U8)(stTableInfo.u32Value&0x3);
            }

            stTableInfo.u32Addr = VR_BITS_PER_PIXEL;
            stTableInfo.u32Mask = 0xFF;
            u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_ZNR_Control_VR_Main_Ex, PQ_XC0_EXT);
            _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_Control_VR_Main_Ex, &stTableInfo, PQ_XC0_EXT);
            pInfo->u8BitsPerPixel = (MS_U8)stTableInfo.u32Value;

            if(bIPM420CmpEnable || bOPM420CmpEnable)
            {
                stTableInfo.u32Addr = BITS_PER_PIXEL_CMP;
                stTableInfo.u32Mask = 0xFF;
                u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_ZNR_Control_VR_Main_Ex, PQ_XC0_EXT);
                _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_Control_VR_Main_Ex, &stTableInfo, PQ_XC0_EXT);
                pInfo->u8BitsPerPixel = (MS_U8)stTableInfo.u32Value;
            }
        }
        else
        {
            stTableInfo.u32Addr = FRAME_COUNT;
            stTableInfo.u32Mask = 0x1F;
            u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_ZNR_SCM_Format_Main_Ex, PQ_XC0_EXT);
            _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_SCM_Format_Main_Ex, &stTableInfo, PQ_XC0_EXT);
            pInfo->u8FrameNum = (MS_U8)stTableInfo.u32Value;

            stTableInfo.u32Addr = BITS_PER_PIXEL;
            stTableInfo.u32Mask = 0xFF;
            _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_SCM_Format_Main_Ex, &stTableInfo, PQ_XC0_EXT);
            pInfo->u8BitsPerPixel = (MS_U8)stTableInfo.u32Value;
        }

        stTableInfo.u32Addr = WR_BANKMAPPING_NUM;
        stTableInfo.u32Mask = 0xE0;
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_ZNR_Control_Main_Ex, PQ_XC0_EXT);
        _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_Control_Main_Ex, &stTableInfo, PQ_XC0_EXT);
        pInfo->u8WRBankMappingNum = (MS_U8)(stTableInfo.u32Value >> 5);

        stTableInfo.u32Addr = UCM_INFO;
        stTableInfo.u32Mask = 0x07;
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_ZNR_Control_Ext_Main_Ex, PQ_XC0_EXT);
        _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_Control_Ext_Main_Ex, &stTableInfo, PQ_XC0_EXT);
        pInfo->u16UCMRWDiff = (MS_U8)stTableInfo.u32Value;

        stTableInfo.u32Addr = UCM_INFO;
        stTableInfo.u32Mask = 0xF0;
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_ZNR_UCM_Format_Main_Ex, PQ_XC0_EXT);
        _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_UCM_Format_Main_Ex, &stTableInfo, PQ_XC0_EXT);
        pInfo->u16UCMFrameNum = (MS_U8)(stTableInfo.u32Value >> 4);

        stTableInfo.u32Addr = PQDELAY_COUNT;
        stTableInfo.u32Mask = 0xFF;
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_ZNR_Control_VR_Main_Ex, PQ_XC0_EXT);
        _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_ZNR_Control_VR_Main_Ex, &stTableInfo, PQ_XC0_EXT);
        pInfo->u8PQDelayCount = (MS_U8)stTableInfo.u32Value;

        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Support MADI_VR & ZNR, ZNR VR %s\n",MHal_PQ_IsSupportZNRControlVR()?("Enable"):("Disable"));
#else // Not support ZNR
        if(MHal_PQ_IsSupportMADiVR())
        {
            stTableInfo.u32Addr = VR_FRAME_COUNT;
            stTableInfo.u32Mask = 0xFF;
            u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_MADi_VR_Main_Ex, PQ_XC0_EXT);
            _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_MADi_VR_Main_Ex, &stTableInfo, PQ_XC0_EXT);
            pInfo->u8FrameNum = (MS_U8)stTableInfo.u32Value;

            if(pInfo->u32Version >= 2)
            {
                stTableInfo.u32Addr = VR_FB_LEVEL;
                stTableInfo.u32Mask = 0x3;
                u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_MADi_VR_Main_Ex, PQ_XC0_EXT);
                _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_MADi_VR_Main_Ex, &stTableInfo, PQ_XC0_EXT);
                pInfo->u8FBLevel= (MS_U8)(stTableInfo.u32Value&0x3);
            }

            stTableInfo.u32Addr = VR_BITS_PER_PIXEL;
            stTableInfo.u32Mask = 0xFF;
            u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_MADi_VR_Main_Ex, PQ_XC0_EXT);
            _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_MADi_VR_Main_Ex, &stTableInfo, PQ_XC0_EXT);
            pInfo->u8BitsPerPixel = (MS_U8)stTableInfo.u32Value;
        }

        stTableInfo.u32Addr = WR_BANKMAPPING_NUM;
        stTableInfo.u32Mask = 0xE0;
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_MADi_Main_Ex, PQ_XC0_EXT);
        _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_MADi_Main_Ex, &stTableInfo, PQ_XC0_EXT);
        pInfo->u8WRBankMappingNum = (MS_U8)(stTableInfo.u32Value >> 5);

        stTableInfo.u32Addr = PQDELAY_COUNT;
        stTableInfo.u32Mask = 0xFF;
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pInfo->enWindow, PQ_IP_MADi_VR_Main_Ex, PQ_XC0_EXT);
        _MDrv_PQ_GetPQInfo(pInstance, pInfo->enWindow, u16TabIdx, PQ_IP_MADi_VR_Main_Ex, &stTableInfo, PQ_XC0_EXT);
        pInfo->u8PQDelayCount = (MS_U8)stTableInfo.u32Value;

        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Support MADI_VR, MADI VR %s\n",MHal_PQ_IsSupportMADiVR()?("Enable"):("Disable"));

#endif
#else
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Not Support MADI_VR & ZNR\n");
#endif
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Frame num:%u Bits per pixel:%u u8GroupNum=%u u8GroupNumCmp=%u\n",pInfo->u8FrameNum,pInfo->u8BitsPerPixel,pInfo->u8GroupNum,pInfo->u8GroupNumCmp);
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"u8WRBankMappingNum : %u u8PQDelayCount:%u u16UCMRWDiff : %u u16UCMFrameNum : %u\n",pInfo->u8WRBankMappingNum,pInfo->u8PQDelayCount,pInfo->u16UCMRWDiff,pInfo->u16UCMFrameNum);
    }

    if(pInfo->u32Version >= 3)
    {
        pInfo->u8BitsPerPixel_Y = _MDrv_PQ_GetBitPerPixel_Y(pInstance, pInfo->enWindow);
        if(bIPM420CmpEnable || bOPM420CmpEnable)
        {
            pInfo->b420CmpEnable = TRUE;
        }
        else
        {
            pInfo->b420CmpEnable = FALSE;
        }
    }

    if(pInfo->u32Version >= 4)
    {
        _MDrv_PQ_GetGroupNum(pInstance, pInfo->enWindow, &pInfo->u8GroupNum, &pInfo->u8GroupNumCmp);
    }

    if(pInfo->u32Version >= 5)
    {
        //check memconfig RGB case for crop formula
        u8MemConfig = _MDrv_PQ_GetMemConfig(pInstance, pInfo->enWindow);
        if((u8MemConfig == MEM_CONFIG_RGB) || (u8MemConfig == MEM_CONFIG_RGBL))
        {
            pInfo->bMemConfigRGB = TRUE;
        }
        else
        {
            pInfo->bMemConfigRGB = FALSE;
        }
    }

    return enRet;
}
PQ_ReturnValue SYMBOL_WEAK MDrv_PQ_GetInfo(MS_PQ_INFO *pInfo)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetInfo PQArgs;
    PQArgs.pInfo = pInfo;
    PQArgs.bReturnValue = E_PQ_RET_FAIL;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_INFO, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return E_PQ_RET_FAIL;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL  MDrv_PQ_GetStatus_U2(void* pInstance,MS_PQ_Status *pStatus)
{
    memcpy((void*)pStatus, (void*)&_status, sizeof(MS_PQ_Status));
    return FALSE;
}
MS_BOOL  SYMBOL_WEAK MDrv_PQ_GetStatus(MS_PQ_Status *pStatus)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetStatus PQArgs;
    PQArgs.pStatus = pStatus;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_STATUS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL  MDrv_PQ_SetDbgLevel_U2(void* pInstance,MS_U16 u16DbgSwitch)
{
    _u16PQDbgSwitch = u16DbgSwitch;
    return TRUE;
}
MS_BOOL  SYMBOL_WEAK MDrv_PQ_SetDbgLevel(MS_U16 u16DbgSwitch)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SetDbgLevel PQArgs;
    PQArgs.u16DbgSwitch = u16DbgSwitch;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_DBGLEVEL, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

void MDrv_PQ_MADiForceMotionC_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bEnable)
{
    MS_U16 u16DataC = 0;
    u16DataC = Hal_PQ_get_force_c_motion(pInstance,eWindow == PQ_MAIN_WINDOW);

    if(eWindow == PQ_SUB_WINDOW)
    {
        u16DataC  <<= 8;
    }

    if(bEnable)
    {
        u16DataC |= (eWindow == PQ_MAIN_WINDOW ? 0x0080 : 0x8000);
    }
    else
    {
        u16DataC &= (eWindow == PQ_MAIN_WINDOW ? 0x000F : 0x0F00);
    }
    Hal_PQ_set_force_c_motion(pInstance,eWindow == PQ_MAIN_WINDOW, u16DataC);

}
void SYMBOL_WEAK MDrv_PQ_MADiForceMotionC(PQ_WIN eWindow, MS_BOOL bEnable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_MADiForceMotionC PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bEnable = bEnable;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_MADI_FORCE_MOTIONC, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

#ifdef UFO_XC_PQ_SUPPORT_SWMOTION
void MDrv_PQ_MADiForceMotion_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bEnableY, MS_U16 u16DataY,
                             MS_BOOL bEnableC, MS_U16 u16DataC)
{
    // Y
    if (bEnableY)
    {
        SAVE_FORCE_Y_MOTION_ENABLE(pInstance, eWindow == PQ_MAIN_WINDOW);
        if (eWindow == PQ_SUB_WINDOW)
        {
            u16DataY <<= 8;
        }
        u16DataY |= (eWindow == PQ_MAIN_WINDOW ? 0x0080 : 0x8000);
        Hal_PQ_set_force_y_motion(pInstance, eWindow == PQ_MAIN_WINDOW, u16DataY);
    }
    else
    {
        RESTORE_FORCE_Y_MOTION_ENABLE(pInstance, eWindow == PQ_MAIN_WINDOW);
    }

    // C
    if (bEnableC)
    {
        SAVE_FORCE_C_MOTION_ENABLE(pInstance, eWindow == PQ_MAIN_WINDOW);
        if (eWindow == PQ_SUB_WINDOW)
        {
            u16DataC <<= 8;
        }
        u16DataC |= (eWindow == PQ_MAIN_WINDOW ? 0x0080 : 0x8000);
        Hal_PQ_set_force_c_motion(pInstance, eWindow == PQ_MAIN_WINDOW, u16DataC);
    }
    else
    {
        RESTORE_FORCE_C_MOTION_ENABLE(pInstance, eWindow == PQ_MAIN_WINDOW);
    }
}


void SYMBOL_WEAK MDrv_PQ_MADiForceMotion(PQ_WIN eWindow, MS_BOOL bEnableY, MS_U16 u16DataY,
                             MS_BOOL bEnableC, MS_U16 u16DataC)
{
	if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_MADiForceMotion PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bEnableY = bEnableY;
    PQArgs.u16DataY = u16DataY;
    PQArgs.bEnableC = bEnableC;
    PQArgs.u16DataC = u16DataC;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_MADI_FORCE_MOTION, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif

void MDrv_PQ_MADiForceMotionY_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bEnable)
{
    MS_U16 u16Data = 0;

    u16Data = Hal_PQ_get_force_y_motion(pInstance,eWindow == PQ_MAIN_WINDOW);

    if(eWindow == PQ_SUB_WINDOW)
    {
        u16Data <<= 8;
    }
    if(bEnable)
    {
        u16Data |= (eWindow == PQ_MAIN_WINDOW ? 0x0080 : 0x8000);
    }
    else
    {
        u16Data &= (eWindow == PQ_MAIN_WINDOW ? 0x003F : 0x3F00);
    }

    Hal_PQ_set_force_y_motion(pInstance,eWindow == PQ_MAIN_WINDOW, u16Data);
}
void SYMBOL_WEAK MDrv_PQ_MADiForceMotionY(PQ_WIN eWindow, MS_BOOL bEnable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_MADiForceMotionY PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bEnable = bEnable;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_MADI_FORCE_MOTIONY, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_SetRFblMode_U2(void* pInstance,MS_BOOL bEnable, MS_BOOL bFilm)
{
    _stRFBL_Info.bEnable = bEnable;
    _stRFBL_Info.bFilm = bFilm;

    if(!bEnable)    //default mode
    {
        MDrv_PQ_MADiForceMotionY_U2(pInstance,PQ_MAIN_WINDOW, FALSE);
        MDrv_PQ_MADiForceMotionC_U2(pInstance,PQ_MAIN_WINDOW, FALSE);

        _stRFBL_Info.u8MADiType = MS_MADI_24_2R;
        //printf("[%s]-[%d] : Y/C motion : ( 0 , 0 ) \n", __FUNCTION__, __LINE__);
    }
    else
    {
        _stRFBL_Info.u8MADiType = (bFilm) ? (MS_U8)MS_MADI_24_RFBL_FILM : (MS_U8)MS_MADI_24_RFBL_NFILM;
        MDrv_PQ_MADiForceMotionY_U2(pInstance,PQ_MAIN_WINDOW, FALSE);
        MDrv_PQ_MADiForceMotionC_U2(pInstance,PQ_MAIN_WINDOW, (!bFilm));

        //printf("[%s]-[%d] : Y/C motion : ( 0 , %d) \n", __FUNCTION__, __LINE__,(!bFilm));
    }
}
void SYMBOL_WEAK MDrv_PQ_SetRFblMode(MS_BOOL bEnable, MS_BOOL bFilm)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_SetRFblMode PQArgs;
    PQArgs.bEnable = bEnable;
    PQArgs.bFilm = bFilm;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_RFBL_MODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_GetRFblMode_U2(void* pInstance,MS_PQ_RFBL_INFO * pstInfo)
{
    memcpy(pstInfo, &_stRFBL_Info, sizeof(MS_PQ_RFBL_INFO));
    return;
}
void SYMBOL_WEAK MDrv_PQ_GetRFblMode(MS_PQ_RFBL_INFO * pstInfo)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_GetRFblMode PQArgs;
    PQArgs.pstInfo = pstInfo;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_RFBL_MODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

MS_BOOL MDrv_PQ_Get_VDSampling_Info_U2(void* pInstance,PQ_WIN eWindow, PQ_INPUT_SOURCE_TYPE enInputSourceType, PQ_VIDEOSTANDARD_TYPE eStandard, MS_PQ_VD_Sampling_Info *pInfo)
{
#if (PQ_ENABLE_VD_SAMPLING)
    PQ_VIDEOSTANDARD_TYPE eCurStandard = _stVD_Info[eWindow].enVideoStandard;
    MS_U16 u16CurSrc = _u16PQSrcType[eWindow];
    MS_U16 u16TableIdx;
#if PQ_HAL_EWINDOW_PARAM
    MS_U16 u16IPIdx = Hal_PQ_get_ip_idx(pInstance,MS_PQ_IP_VD_SAMPLING, eWindow);
#else
    MS_U16 u16IPIdx = Hal_PQ_get_ip_idx(pInstance,MS_PQ_IP_VD_SAMPLING);
#endif
    MS_U8 u8SampleTable[10];

    memset(u8SampleTable, 0, sizeof(u8SampleTable));

    PQ_LEVEL_PRINTF(PQ_DBG_INFO, "[PQ] GetVDSampling: win:%d, src:%d, standard:%d, PQIP=%x\n", eWindow, enInputSourceType, eStandard, u16IPIdx);
    if((((enInputSourceType >= PQ_INPUT_SOURCE_TV) && (enInputSourceType <= PQ_INPUT_SOURCE_SVIDEO))||(enInputSourceType == PQ_INPUT_SOURCE_SCART)) &&
            (eStandard < E_PQ_VIDEOSTANDARD_NOTSTANDARD) &&
            (u16IPIdx != 0xFFFF))
    {
        _stVD_Info[eWindow].enVideoStandard = eStandard;
        _u16PQSrcType[eWindow] = QM_InputSourceToIndex(pInstance, eWindow, enInputSourceType);



        u16TableIdx = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, u16IPIdx);
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "[PQ] VDSampling: TableIdx=%d\n", u16TableIdx);
        if(((gbPQBinEnable == 1) && (u16TableIdx == PQ_BIN_IP_NULL))
                || ((gbPQBinEnable == 0) && (u16TableIdx == PQ_IP_NULL)))
        {
            pInfo->eType = PQ_VD_SAMPLING_OFF;
        }
        else
        {
            MDrv_PQ_LoadTableData_U2(pInstance, eWindow, u16TableIdx, u16IPIdx, u8SampleTable, 10);
            pInfo->u16Vstart = ((MS_U16)u8SampleTable[0]) | (((MS_U16)u8SampleTable[1])<<8);
            pInfo->u16Hstart = ((MS_U16)u8SampleTable[2]) | (((MS_U16)u8SampleTable[3])<<8);
            pInfo->u16Hsize  = ((MS_U16)u8SampleTable[4]) | (((MS_U16)u8SampleTable[5]) << 8);
            pInfo->u16Vsize  = ((MS_U16)u8SampleTable[6]) | (((MS_U16)u8SampleTable[7]) << 8);
            pInfo->u16Htt    = ((MS_U16)u8SampleTable[8]) | (((MS_U16)u8SampleTable[9]) << 8);
            pInfo->eType = PQ_VD_SAMPLING_ON;

            PQ_LEVEL_PRINTF(PQ_DBG_INFO, "VD x:%x, y:%x, w:%x, h:%x, Htt=%x \n", pInfo->u16Hstart, pInfo->u16Vstart, pInfo->u16Hsize, pInfo->u16Vsize, pInfo->u16Htt);
        }

        _stVD_Info[eWindow].enVideoStandard  = eCurStandard;
        _u16PQSrcType[eWindow] = u16CurSrc;
    }
    else
    {
        pInfo->eType = PQ_VD_SAMPLING_OFF;
    }
    return TRUE;
#else
    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;33m[PQ_ENABLE_VD_SAMPLING]\33[m\n");
    return FALSE;
#endif
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Get_VDSampling_Info(PQ_WIN eWindow, PQ_INPUT_SOURCE_TYPE enInputSourceType, PQ_VIDEOSTANDARD_TYPE eStandard, MS_PQ_VD_Sampling_Info *pInfo)
{
#if (PQ_ENABLE_VD_SAMPLING)
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
    }

    stPQ_Get_VDSampling_Info PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.enInputSourceType = enInputSourceType;
    PQArgs.eStandard = eStandard;
    PQArgs.pInfo = pInfo;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_VDSAMPLING_INFO, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
#else
    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;33m[PQ_ENABLE_VD_SAMPLING]\33[m\n");
    return FALSE;
#endif
}

MS_BOOL _MDrv_PQ_Get_HSDSampling(void *pInstance,PQ_WIN eWindow, MS_U32 *pu32ratio, MS_BOOL *bADVMode)
{
#if PQ_HAL_EWINDOW_PARAM
    MS_U16 u16IPIdx = Hal_PQ_get_ip_idx(pInstance,MS_PQ_IP_HSD_SAMPLING, eWindow);
#else
    MS_U16 u16IPIdx = Hal_PQ_get_ip_idx(pInstance,MS_PQ_IP_HSD_SAMPLING);
#endif
    MS_U16 u16TableIdx;
    MS_BOOL bret = TRUE;

    if(u16IPIdx != 0xFFFF)
    {
        u16TableIdx = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, u16IPIdx);
    }
    else
    {
        u16TableIdx = (gbPQBinEnable) ? PQ_BIN_IP_NULL : PQ_IP_NULL;
    }

    if(((gbPQBinEnable == 1) && (u16TableIdx == PQ_BIN_IP_NULL)) ||
            ((gbPQBinEnable == 0) && (u16TableIdx == PQ_IP_NULL)))
    {
        *pu32ratio = 0;
        bret = FALSE;
    }
    else if( (( gbPQBinEnable == 1) &&
       (( MDrv_PQBin_GetSkipRule(pInstance,PQ_IP_HSD_Y_Main, &stPQBinHeaderInfo[PQ_BIN_STD_MAIN])) ||
            ( MDrv_PQBin_GetSkipRule(pInstance,PQ_IP_HSD_C_Main, &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]))) )
            ||
       (( gbPQBinEnable == 1) &&
        ((MST_SkipRule_IP_Main[PQ_IP_HSD_Y_Main] == 1) || (MST_SkipRule_IP_Main[PQ_IP_HSD_C_Main] == 1))))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"HSD_Y, HSD_C is True in SKipRule, driver consider HSDSampling is not defined in QMap.\n");
        *pu32ratio = 0;
        bret = FALSE;
    }
    else
    {
#if PQ_NEW_HSD_SAMPLING_TYPE
#if PQ_HAL_EWINDOW_PARAM
        if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_000, eWindow))
#else
        if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_000))
#endif
        {
            *pu32ratio = 1000;
        }
#if PQ_HAL_EWINDOW_PARAM
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_125, eWindow))
#else
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_125))
#endif
        {
            *pu32ratio = 875;
        }
#if PQ_HAL_EWINDOW_PARAM
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_250, eWindow))
#else
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_250))
#endif
        {
            *pu32ratio = 750;
        }
#if PQ_HAL_EWINDOW_PARAM
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_375, eWindow))
#else
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_375))
#endif
        {
            *pu32ratio = 625;
        }
#if PQ_HAL_EWINDOW_PARAM
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_500, eWindow))
#else
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_500))
#endif
        {
            *pu32ratio = 500;
        }
#if PQ_HAL_EWINDOW_PARAM
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_625, eWindow))
#else
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_625))
#endif
        {
            *pu32ratio = 375;
        }
#if PQ_HAL_EWINDOW_PARAM
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_750, eWindow))
#else
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_750))
#endif
        {
            *pu32ratio = 250;
        }
#if PQ_HAL_EWINDOW_PARAM
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_875, eWindow))
#else
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_1_875))
#endif
        {
            *pu32ratio = 125;
        }
#if PQ_HAL_EWINDOW_PARAM
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_2_000, eWindow))
#else
        else if(u16TableIdx == Hal_PQ_get_hsd_sampling_idx(pInstance,MS_HSD_SAMPLING_Div_2_000))
#endif
        {
            *pu32ratio = 1;
        }
        else
#endif
        {
            *pu32ratio = 0;
        }

        bret = TRUE;
    }

    // Load HSD_Y & HSD_C
    if(bret)
    {
        MS_U16 u16HSD_Y_idx, u16HSD_C_idx;

        u16HSD_Y_idx = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, PQ_IP_HSD_Y_Main);
        MDrv_PQ_LoadTable_U2(pInstance,eWindow, u16HSD_Y_idx, PQ_IP_HSD_Y_Main);

        u16HSD_C_idx = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, PQ_IP_HSD_C_Main);
        MDrv_PQ_LoadTable_U2(pInstance,eWindow, u16HSD_C_idx, PQ_IP_HSD_C_Main);

        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"HSD_Y:%d, HSD_C:%d\n", u16HSD_Y_idx, u16HSD_C_idx);

        *bADVMode = ((u16HSD_Y_idx == PQ_IP_HSD_Y_CB_Main) &&
                     (u16HSD_C_idx == PQ_IP_HSD_C_CB_Main)) ? FALSE : TRUE;
    }
    else
    {
        *bADVMode = FALSE;
    }

    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"[PQ] GetHSDSampling, IPIdx=%d, Table=%d, ratio=%td ret=%d\n", u16IPIdx, u16TableIdx, (ptrdiff_t) *pu32ratio, bret);

    return bret;
}

static MS_BOOL _MDrv_PQ_Set_DLC_Histogram_XRule(void* pInstance, PQ_WIN eWindow, MS_U16 u16AspectRatio)
{
#if defined(PQ_XRULE_DLC_DYNAMIC_HISTOGRAM_ENABLE) && (PQ_XRULE_DLC_DYNAMIC_HISTOGRAM_ENABLE == TRUE)
    MS_U16 u16IPIdx = 0;
    MS_U16 u16TabIdx = (gbPQBinEnable == FALSE) ? PQ_IP_NULL : PQ_BIN_IP_NULL;
    MS_U16 u16XRuleIdx = 0;
    EN_XRULE_IP enXRuleType = E_XRULE_DLC_HISTOGRAM;
    MS_U16 u16XRuleIP = 0;
    EN_XC_ASPECT_RATIO enAspectRatio = (EN_XC_ASPECT_RATIO)u16AspectRatio;
    //check PQbin support Xrule
    if (stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16XRuleNum <= E_XRULE_DLC_HISTOGRAM)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"PQbin and header file is mismatch, Please update PQbin!  u16XRuleNum(PQnin)=%d, u16XRuleNum(header)=%d \n",stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16XRuleNum, E_XRULE_DLC_HISTOGRAM);
        return FALSE;
    }

    switch(enAspectRatio)
    {
        case E_XC_DLC_RATIO_16X9:
        {
            u16XRuleIdx = PQ_DLC_Histogram_ActWin_Rule_ASPECT_RATIO_16_9_Main;
            break;
        }
        case E_XC_DLC_RATIO_4X3:
        {
            u16XRuleIdx = PQ_DLC_Histogram_ActWin_Rule_ASPECT_RATIO_4_3_Main;
             break;
        }
        case E_XC_DLC_RATIO_JUSTSCAN:
        {
            u16XRuleIdx = PQ_DLC_Histogram_ActWin_Rule_ASPECT_RATIO_JustScan_Main;
            break;
        }
       default:
       {
            u16XRuleIdx = PQ_DLC_Histogram_ActWin_Rule_ASPECT_RATIO_16_9_Main;
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
            return FALSE;
       }
    }
    PQ_LEVEL_PRINTF(PQ_DBG_INFO,"XRuleIdx =%u \n",u16XRuleIdx);

    for(u16XRuleIP = 0; u16XRuleIP < MDrv_PQ_GetXRuleIPNum(pInstance, eWindow, enXRuleType); u16XRuleIP++)
    {
        u16IPIdx = MDrv_PQ_GetXRuleIPIndex(pInstance, eWindow, enXRuleType, u16XRuleIP,PQ_XC0_STD);
        u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, eWindow, enXRuleType, u16XRuleIdx, u16XRuleIP,PQ_XC0_STD);
        if(((gbPQBinEnable == TRUE)  && (u16TabIdx == PQ_BIN_IP_NULL)) ||
           ((gbPQBinEnable == FALSE) && (u16TabIdx == PQ_IP_NULL)))
        {
            u16TabIdx = MDrv_PQ_GetTableIndex_U2(pInstance, eWindow, u16IPIdx);
            PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(d)tabidx=%u\n",u16TabIdx);
        }
        else
        {
            PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(d)tabidx=%u\n",u16TabIdx);
        }
        MDrv_PQ_LoadTable_U2(pInstance, eWindow, u16TabIdx, u16IPIdx);
    }
    return TRUE;
#else
    return FALSE;
#endif

}

static MS_BOOL _MDrv_PQ_SetDLCRangeOffset(void* pInstance, PQ_WIN eWindow)
{
    MS_U16 u16TableIdx = 0;
    if(MHal_PQ_IP_Support(E_PQ_IP_DLCHIST_RANGEOFFSET) == TRUE)
    {
        u16TableIdx = MDrv_PQ_GetTableIndex_U2(pInstance, eWindow, MHal_PQ_Get_IPNum(E_PQ_IP_DLCHIST_RANGEOFFSET));
        MDrv_PQ_LoadTable_U2(pInstance, eWindow, u16TableIdx, MHal_PQ_Get_IPNum(E_PQ_IP_DLCHIST_RANGEOFFSET));
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"[%s] TableIdx: %u, Window: %u, IPNum: %u\n", __FUNCTION__, u16TableIdx, eWindow, MHal_PQ_Get_IPNum(E_PQ_IP_DLCHIST_RANGEOFFSET));
        return TRUE;
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"Not Support DLC Offset.\n");
        return FALSE;
    }
}

//get_figure
MS_BOOL MDrv_PQ_IOCTL_U2(void* pInstance,PQ_WIN eWindow, MS_U32 u32Flag, void *pBuf, MS_U32 u32BufSize)
{
    MS_BOOL bret = TRUE;

    switch(u32Flag)
    {
        case E_IOCTL_HSD_SAMPLING:
        {
            if(u32BufSize == sizeof(PQ_HSD_SAMPLING_INFO))
            {
                MS_U32 u32ratio;
                MS_BOOL bADVMode;

                bret = _MDrv_PQ_Get_HSDSampling(pInstance,eWindow, &u32ratio, &bADVMode);
                ((PQ_HSD_SAMPLING_INFO *)pBuf)->u32ratio = u32ratio;
                ((PQ_HSD_SAMPLING_INFO *)pBuf)->bADVMode = bADVMode;
                //printf("ratio %lx, bADVmode=%d\n\n",((PQ_HSD_SAMPLING_INFO *)pBuf)->u32ratio, ((PQ_HSD_SAMPLING_INFO *)pBuf)->bADVMode);
            }
            else
            {
                //printf("Size is not correct, in=%ld, %d\n", u32BufSize, sizeof(PQ_HSD_SAMPLING_INFO));
                ((PQ_HSD_SAMPLING_INFO *)pBuf)->u32ratio = 0;
                ((PQ_HSD_SAMPLING_INFO *)pBuf)->bADVMode = 0;
                bret = FALSE;
            }
            break;
        }

        case E_IOCTL_PREVSD_BILINEAR:
            bret = TRUE;
            *((MS_BOOL *)pBuf) = TRUE;
            break;

        case E_IOCTL_ADC_SAMPLING:
            if(u32BufSize == sizeof(PQ_ADC_SAMPLING_INFO))
            {

                PQ_INPUT_SOURCE_TYPE enInputSrc = ((PQ_ADC_SAMPLING_INFO *)pBuf)->enPQSourceType;
                MS_PQ_Mode_Info      *pModeInfo = &(((PQ_ADC_SAMPLING_INFO *)pBuf)->stPQModeInfo);
                MS_PQ_ADC_SAMPLING_TYPE enADCSamplingType = MDrv_PQ_Get_ADCSampling_Info(eWindow, enInputSrc, pModeInfo);

                if((enADCSamplingType == E_PQ_ADC_SAMPLING_NOT_SUPPORT) || (enADCSamplingType == E_PQ_ADC_SAMPLING_NONE))
                {
                    ((PQ_ADC_SAMPLING_INFO *)pBuf)->u16ratio = 1;
                    bret = FALSE;
                }
                else
                {
                    ((PQ_ADC_SAMPLING_INFO *)pBuf)->u16ratio = (MS_U16)enADCSamplingType;
                    bret = TRUE;
                }
            }
            else
            {
                bret = FALSE;
                ((PQ_ADC_SAMPLING_INFO *)pBuf)->u16ratio = 1;
            }
            break;
        case E_IOCTL_RFBL_CTRL:
            //printf("IOCTL %lx\n", u32Flag);
#if PQ_ENABLE_RFBL
            bret = MDrv_PQ_Get_RFBL_Info(eWindow);
#else
            bret = FALSE;
#endif
            UNUSED(pBuf);
            UNUSED(u32BufSize);
            break;

        case E_IOCTL_PQ_SUGGESTED_FRAMENUM:
        {
            if(u32BufSize == sizeof(MS_U16))
            {
                *((MS_U16 *)pBuf) = _u16PQSuggestedFrameNum[eWindow];
                bret = TRUE;
            }
            else
            {
                *((MS_U16 *)pBuf) = 0;
                bret = FALSE;
            }
            break;
        }
#if PQ_UC_CTL
        case E_IOCTL_SET_UCFEATURE:
        {
#if(PQ_GRULE_GAME_MODE_ENABLE == 0)
            MS_U16 u16MADi_DFK_ToSet[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
            MS_U16 u16MADi_SST_ToSet[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
            MS_U16 u16MADi_EODiW_ToSet[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
            MS_U16 u16UCNR_ToSet[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
            MS_U16 u16UCDi_ToSet[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
            MS_U16 u16UCCTL_ToSet[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
            MS_U16 u16PostCCS_ToSet[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
            MS_U16 u16SwDriver_ToSet[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
            MS_U16 u16DHD_ToSet[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
#endif
            MS_BOOL bDisableUCFeature = FALSE;

            if(u32BufSize == sizeof(MS_BOOL))
            {
                _gIsEnable8LBMode = *((MS_BOOL *)pBuf);
            }
            else
            {
                _gIsEnable8LBMode = FALSE;
            }
#if (PQ_DISABLE_UC_FOR_PREVSD)
            XC_ApiStatus stXCApiStatus;
            memset(&stXCApiStatus, 0, sizeof(XC_ApiStatus));
            if (MApi_XC_GetStatus(&stXCApiStatus, MAIN_WINDOW) == FALSE)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MApi_XC_GetStatus failed.\n");
            }
            if(stXCApiStatus.bPreV_ScalingDown)
            {
                bDisableUCFeature = TRUE;
            }
            else
#endif
            if(MApi_SC_IsForceFreerun() || MApi_XC_IsCurrentFrameBufferLessMode())//can not support ucnr when freerun or fbl
            {
                bDisableUCFeature = TRUE;
            }
            else if((((E_XC_3D_OUTPUT_LINE_ALTERNATIVE == MApi_XC_Get_3D_Output_Mode())
                            || (E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF == MApi_XC_Get_3D_Output_Mode())
                            || (E_XC_3D_OUTPUT_TOP_BOTTOM == MApi_XC_Get_3D_Output_Mode())
                            || (E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF_HW == MApi_XC_Get_3D_Output_Mode())
                            || (E_XC_3D_OUTPUT_TOP_BOTTOM_HW == MApi_XC_Get_3D_Output_Mode())
                            || (E_XC_3D_OUTPUT_FRAME_R == MApi_XC_Get_3D_Output_Mode())
                            || (E_XC_3D_OUTPUT_FRAME_L == MApi_XC_Get_3D_Output_Mode())
                          )&& (1 <= MApi_XC_Get_3D_HW_Version()))
                        || ((MApi_XC_Get_3D_Output_Mode() != E_XC_3D_OUTPUT_MODE_NONE)&& (5 == MApi_XC_Get_3D_HW_Version()))//monaco 3d disable uc
                   )  // 3d version >=2 && using sub window
            {
                bDisableUCFeature = TRUE;
            }

#if PQ_DISABLE_UC_FOR_PIP
            else if(MApi_XC_Is_SubWindowEanble()
                   || _enPQDisplayType != PQ_DISPLAY_ONE) //disable ucnr when sub window bit is setting(pip/pop)
            {
                bDisableUCFeature = TRUE;
            }
            else if(eWindow == PQ_SUB_WINDOW)//sub window need disable ucnr for  dual view/travelingmode enable sub window bit after setwindow
            {
                bDisableUCFeature = TRUE;
            }
#endif
            PQMADi_DBG(printf("E_IOCTL_SET_UCFEATURE: Disable UC=%u\n", bDisableUCFeature));
            if(bDisableUCFeature == TRUE)
            {
                MDrv_PQ_DisableUCFeature(PQ_MAIN_WINDOW);
            }
            else
            {
#if(PQ_GRULE_GAME_MODE_ENABLE == 1)
                MDrv_PQ_EnableUCFeature_U2(pInstance, eWindow);
#else
                MDrv_PQ_GetUCFeatureFromPQ(eWindow, &u16MADi_DFK_ToSet[eWindow], &u16MADi_SST_ToSet[eWindow], &u16MADi_EODiW_ToSet[eWindow],
                    &u16UCNR_ToSet[eWindow], &u16UCDi_ToSet[eWindow], &u16UCCTL_ToSet[eWindow],
                    &u16SwDriver_ToSet[eWindow], &u16PostCCS_ToSet[eWindow], &u16DHD_ToSet[eWindow]);
#if PQ_DISABLE_UC_FOR_PIP
                MDrv_PQ_SetUCFeature(eWindow, _gu16SetMemfmt_Madi[eWindow], _gu16SetMemfmt_Madi_Motion[eWindow], u16MADi_DFK_ToSet[eWindow],
                    u16MADi_SST_ToSet[eWindow], u16MADi_EODiW_ToSet[eWindow], u16UCNR_ToSet[eWindow], u16UCDi_ToSet[eWindow],
                    u16UCCTL_ToSet[eWindow], u16SwDriver_ToSet[eWindow], u16PostCCS_ToSet[eWindow], u16DHD_ToSet[eWindow]);
#else
                if(eWindow == PQ_SUB_WINDOW)
                {
                    //no need to set madi/madi_mot again, because setmemformat has done it. so here pass 0xffff in
                    MDrv_PQ_SetUCFeature(eWindow, 0xFFFF, 0xFFFF, u16MADi_DFK_ToSet[eWindow],
                        u16MADi_SST_ToSet[eWindow], u16MADi_EODiW_ToSet[eWindow], u16UCNR_ToSet[eWindow], u16UCDi_ToSet[eWindow],
                        u16UCCTL_ToSet[eWindow], u16SwDriver_ToSet[eWindow], u16PostCCS_ToSet[eWindow], u16DHD_ToSet[eWindow]);
                }
                else
                {
                    MDrv_PQ_SetUCFeature(eWindow, _s32LastPQIPMADiMain, _s32LastPQIPMADiMotMain, u16MADi_DFK_ToSet[eWindow],
                        u16MADi_SST_ToSet[eWindow], u16MADi_EODiW_ToSet[eWindow], u16UCNR_ToSet[eWindow], u16UCDi_ToSet[eWindow],
                        u16UCCTL_ToSet[eWindow], u16SwDriver_ToSet[eWindow], u16PostCCS_ToSet[eWindow], u16DHD_ToSet[eWindow]);
                }
#endif
#endif
            }
            break;
        }
#endif
        case E_IOCTL_SET_DLC_HISTOGRAM_RANGE:
        {
            if(u32BufSize == sizeof(MS_U16))
            {

                MS_U16 u16AspectRatio = 0;
                u16AspectRatio = *(MS_U16 *)pBuf;
                bret = _MDrv_PQ_Set_DLC_Histogram_XRule(pInstance,eWindow,u16AspectRatio);
                bret = _MDrv_PQ_SetDLCRangeOffset(pInstance,eWindow);
            }
            else
            {
                bret = FALSE;
            }
            break;
        }
            default:
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"unknown IOCTL %tx\n", (ptrdiff_t) u32Flag);
            UNUSED(pBuf);
            UNUSED(u32BufSize);
            bret = FALSE;
            break;
    }
    //printf("[PQ] IOCTL : win:%x, Flag=%08lx, size=%ld, ret:%x\n", eWindow, u32Flag, u32BufSize, bret);
    return bret;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_IOCTL(PQ_WIN eWindow, MS_U32 u32Flag, void *pBuf, MS_U32 u32BufSize)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
    }

    stPQ_INTER_IOCTL PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u32Flag = u32Flag;
    PQArgs.pBuf = pBuf;
    PQArgs.u32BufSize = u32BufSize;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_INTER_IOCTL, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}


MS_PQ_ADC_SAMPLING_TYPE MDrv_PQ_Get_ADCSampling_Info_U2(void* pInstance,PQ_WIN eWindow, PQ_INPUT_SOURCE_TYPE enInputSourceType, MS_PQ_Mode_Info *pstPQModeInfo)
{
    MS_PQ_ADC_SAMPLING_TYPE enRetType;
    MS_U16 u16PQSrcType;
    MS_U16 u16TableIdx;
    MS_PQ_Mode_Info stModeInfo;

    u16PQSrcType = _u16PQSrcType[eWindow];
    memcpy(&stModeInfo, &_stMode_Info[eWindow], sizeof(MS_PQ_Mode_Info));

    MDrv_PQ_Set_ModeInfo_U2(pInstance, eWindow, enInputSourceType, pstPQModeInfo);
    _u16PQSrcType[eWindow] = QM_InputSourceToIndex(pInstance, eWindow, enInputSourceType);

#if PQ_HAL_EWINDOW_PARAM
    if(Hal_PQ_get_ip_idx(pInstance,MS_PQ_IP_ADC_SAMPLING, eWindow) == 0xFFFF)
#else
    if(Hal_PQ_get_ip_idx(pInstance,MS_PQ_IP_ADC_SAMPLING) == 0xFFFF)
#endif
    {
        _u16PQSrcType[eWindow] = u16PQSrcType;
        return E_PQ_ADC_SAMPLING_NOT_SUPPORT;
    }

#if PQ_HAL_EWINDOW_PARAM
    u16TableIdx = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, Hal_PQ_get_ip_idx(pInstance,MS_PQ_IP_ADC_SAMPLING, eWindow));
#else
    u16TableIdx = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, Hal_PQ_get_ip_idx(pInstance,MS_PQ_IP_ADC_SAMPLING));
#endif

#if PQ_HAL_EWINDOW_PARAM
    if(u16TableIdx == Hal_PQ_get_adc_sampling_idx(pInstance,MS_ADC_SAMPLING_X_1,eWindow))
#else
    if(u16TableIdx == Hal_PQ_get_adc_sampling_idx(pInstance,MS_ADC_SAMPLING_X_1))
#endif
    {
        enRetType = E_PQ_ADC_SAMPLING_X_1;
    }
#if PQ_HAL_EWINDOW_PARAM
    else if(u16TableIdx == Hal_PQ_get_adc_sampling_idx(pInstance,MS_ADC_SAMPLING_X_2,eWindow))
#else
    else if(u16TableIdx == Hal_PQ_get_adc_sampling_idx(pInstance,MS_ADC_SAMPLING_X_2))
#endif
    {
        enRetType = E_PQ_ADC_SAMPLING_X_2;
    }
#if PQ_HAL_EWINDOW_PARAM
    else if(u16TableIdx == Hal_PQ_get_adc_sampling_idx(pInstance,MS_ADC_SAMPLING_X_4,eWindow))
#else
    else if(u16TableIdx == Hal_PQ_get_adc_sampling_idx(pInstance,MS_ADC_SAMPLING_X_4))
#endif
    {
        enRetType = E_PQ_ADC_SAMPLING_X_4;
    }
    else
    {
        enRetType = E_PQ_ADC_SAMPLING_NONE;
    }

    memcpy(&_stMode_Info[eWindow], &stModeInfo, sizeof(MS_PQ_Mode_Info));
    _u16PQSrcType[eWindow] = u16PQSrcType;

    return enRetType;
}
MS_PQ_ADC_SAMPLING_TYPE SYMBOL_WEAK MDrv_PQ_Get_ADCSampling_Info(PQ_WIN eWindow, PQ_INPUT_SOURCE_TYPE enInputSourceType, MS_PQ_Mode_Info *pstPQModeInfo)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Get_ADCSampling_Info PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.enInputSourceType = enInputSourceType;
    PQArgs.pstPQModeInfo = pstPQModeInfo;
    PQArgs.bReturnValue = E_PQ_ADC_SAMPLING_NONE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_ADC_SAMPLING_INTO, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return E_PQ_ADC_SAMPLING_NONE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_Set3D_OnOff_U2(void* pInstance,MS_BOOL bEn)
{
    _gIs3D_En = bEn;
    //Set Info to VR
    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_DIMENSION;
    stPQSetVRInfo.enWindow= PQ_MAIN_WINDOW;
    stPQSetVRInfo.pValue= &_gIs3D_En;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Set3D_OnOff(MS_BOOL bEn)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Set3D_OnOff PQArgs;
    PQArgs.bEn = bEn;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_3D_ONOFF, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_GetCaps_U2(void* pInstance,MS_PQ_CAP_INFO *pInfo)
{
    MS_BOOL bRet = TRUE;
#if(PQ_ENABLE_PIP)
    const XC_ApiInfo* pXC_ApiInfo = MApi_XC_GetInfo();
    if (pXC_ApiInfo != NULL)
    {
        pInfo->bPIP_Supported = ((pXC_ApiInfo->u8MaxWindowNum) >= 2) ? TRUE : FALSE;
    }
    else
    {
        pInfo->bPIP_Supported = FALSE;
        bRet = FALSE;
    }
#else
    pInfo->bPIP_Supported = FALSE;
#endif

#if(PQ_ENABLE_3D_VIDEO)
    pInfo->b3DVideo_Supported = TRUE;
#else
    pInfo->b3DVideo_Supported = FALSE;
#endif

    if(pInfo->bPIP_Supported)
    {
        pInfo->b4K2KPIP_Supported = PQ_BW_4K2KPIP_SUPPORT;
    }
    else
    {
        pInfo->b4K2KPIP_Supported = FALSE;
    }
#ifdef ENABLE_PQ_BIN
#if(ENABLE_PQ_BIN)
    pInfo ->bPQBin_Enable = TRUE;
#else
    pInfo ->bPQBin_Enable = FALSE;
#endif
#else
    pInfo ->bPQBin_Enable = FALSE;
#endif

#ifdef PQ_GRULE_UFSC_DLC_ENABLE
#if(PQ_GRULE_UFSC_DLC_ENABLE)
    pInfo ->bUFSCDLC_Enable = TRUE;
#else
    pInfo ->bUFSCDLC_Enable = FALSE;
#endif
#else
    pInfo ->bUFSCDLC_Enable = FALSE;
#endif

#ifdef ENABLE_PQ_BIN_HSY
#if(ENABLE_PQ_BIN_HSY)
    pInfo ->bPQBinHSY_Enable = TRUE;
#else
    pInfo ->bPQBinHSY_Enable = FALSE;
#endif
#else
    pInfo ->bPQBinHSY_Enable = FALSE;
#endif

#ifdef ENABLE_PQ_BIN_TMO
#if(ENABLE_PQ_BIN_TMO)
    pInfo ->bPQBinTMO_Enable = TRUE;
#else
    pInfo ->bPQBinTMO_Enable = FALSE;
#endif
#else
    pInfo ->bPQBinTMO_Enable = FALSE;
#endif
// ver1 for 7632, ver2 for 7642
#if defined(PQ_GRULE_GAME_MODE_ENABLE) && (PQ_GRULE_GAME_MODE_ENABLE == 1)
#if (PQ_SUPPORT_GAMEMODE_VER == 1)
    pInfo ->u8GameModeVer = 1;
#elif (PQ_SUPPORT_GAMEMODE_VER == 2)
    pInfo ->u8GameModeVer = 2;
#else
    pInfo ->u8GameModeVer = 0;
#endif
#endif

    return bRet;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetCaps(MS_PQ_CAP_INFO *pInfo)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
    }

    stPQ_GetCaps PQArgs;
    PQArgs.pInfo = pInfo;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_CAPS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

void MDRV_PQ_PrintLoadTableInfo_U2(void* pInstance,MS_PQ_PQLOADTABLE_WIN enPQWin)
{
#if(ENABLE_PQ_LOAD_TABLE_INFO)

    MS_U16 u16IPIdx;
    MS_U8  u8TabIdx;

    if(enPQWin == PQ_LOAD_TABLE_MAIN)
        printf("[TableInfo::Main] \n");
    else if(enPQWin == PQ_LOAD_TABLE_MAIN_EX)
        printf("[TableInfo::Main Ex] \n");
    else if(enPQWin == PQ_LOAD_TABLE_SUB)
        printf("[TableInfo::SUB] \n");
    else if(enPQWin == PQ_LOAD_TABLE_SUB_EX)
        printf("[TableInfo::SUB Ex] \n");
    else
        return;

    for(u16IPIdx = 0; u16IPIdx < PQ_IP_NUM_Main; ++ u16IPIdx)
    {
        printf(" %u(%s)=", u16IPIdx, MDrv_PQ_GetIPName(u16IPIdx));  // IP Name

        if(enPQWin == PQ_LOAD_TABLE_MAIN)
        {
            u8TabIdx = MDrv_PQ_Get_LoadTableInfo_IP_Tab(MAIN,pInstance, u16IPIdx);
            if(u8TabIdx == 0xFF)
                printf("_____\n");
            else
                printf("%u(%s)\n", u8TabIdx, MDrv_PQ_GetTableName(u16IPIdx, u8TabIdx));
        }
        else if(enPQWin == PQ_LOAD_TABLE_MAIN_EX)
        {
            u8TabIdx = MDrv_PQ_Get_LoadTableInfo_IP_Tab(MAINEX,pInstance, u16IPIdx);
            if(u8TabIdx == 0xFF)
                printf("_____\n");
            else
                printf("Tab%u\n", u8TabIdx);
        }

#if PQ_ENABLE_PIP
        else if(enPQWin == PQ_LOAD_TABLE_SUB)
        {
            u8TabIdx = MDrv_PQ_Get_LoadTableInfo_IP_Tab(SUB,pInstance, u16IPIdx);
            if(u8TabIdx == 0xFF)
                printf("_____\n");
            else
                printf("%u(%s)\n", u8TabIdx, MDrv_PQ_GetTableName(u16IPIdx, u8TabIdx));
        }
        else if(enPQWin == PQ_LOAD_TABLE_SUB_EX)
        {
            u8TabIdx = MDrv_PQ_Get_LoadTableInfo_IP_Tab(SUBEX,pInstance, u16IPIdx);
            if(u8TabIdx == 0xFF)
                printf("_____\n");
            else
                printf("Tab%u\n", u8TabIdx);

        }
#endif
        else
        {
        }
    }
#else
    UNUSED(enPQWin);
#endif
}
void SYMBOL_WEAK MDRV_PQ_PrintLoadTableInfo(MS_PQ_PQLOADTABLE_WIN enPQWin)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_PrintLoadTableInfo PQArgs;
    PQArgs.enPQWin = enPQWin;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_PRINT_LOADTABLE_INFO, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

MS_BOOL MDrv_PQ_SetDS_OnOFF_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bEn)
{
    //store the original value
    _u16RW_Method = Hal_PQ_get_rw_method(pInstance,eWindow == PQ_MAIN_WINDOW ? TRUE : FALSE);
    if(bEn)
    {
        _bDS_En = TRUE;
    }
    else
    {
        _bDS_En = FALSE;
        Hal_PQ_set_rw_method(pInstance,eWindow == PQ_MAIN_WINDOW ? TRUE : FALSE, _u16RW_Method);
    }

    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_SetDS_OnOFF(PQ_WIN eWindow, MS_BOOL bEn)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SetDS_OnOFF PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bEn = bEn;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SETDS_ONOFF, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_Get_RFBL_Info_U2(void* pInstance,PQ_WIN eWindow)
{
    MS_BOOL bRet = FALSE;

    if (eWindow == PQ_MAIN_WINDOW)
    {
#ifdef UFO_XC_FB_LEVEL
        E_XC_FB_LEVEL eFBLevel = E_XC_FB_LEVEL_NUM;
        eFBLevel = MApi_XC_Get_FB_Level(MAIN_WINDOW);
        if(eFBLevel == E_XC_FB_LEVEL_RFBL_DI)
        {
            bRet = TRUE;
        }
        else
        {
            bRet = FALSE;
        }
#else
         bRet = MApi_XC_IsCurrentRequest_FrameBufferLessMode();
#endif
     }
     else
     {
         PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"sub have no FBL mode!\n");
     }
     return bRet;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Get_RFBL_Info(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Get_RFBL_Info PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_RFBL_INFO, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}


MS_BOOL MDrv_PQ_CheckHDMode_U2(void* pInstance,PQ_WIN eWindow)
{
    return _gIsSrcHDMode[eWindow];
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_CheckHDMode(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_CheckHDMode PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_CHECK_HD_MODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

//for Super Resolution interlace and SD mode detected use
MS_BOOL MDrv_PQ_Get_Interlace_SD_mode_U2(void* pInstance)
{
    if(QM_IsInterlaced(PQ_MAIN_WINDOW) && (QM_GetInputHSize(PQ_MAIN_WINDOW) < 1500))
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Get_Interlace_SD_mode(void)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Get_Interlace_SD_mode PQArgs;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_INTERLACE_SD_MODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_SetDualViewState_U2(void* pInstance,MS_BOOL bEnable)
{
    _bDualViewEnable = bEnable;
    MDrv_PQ_ForceVideoInputMode(bEnable, PQ_MAIN_WINDOW);
    MDrv_PQ_ForceVideoInputMode(bEnable, PQ_SUB_WINDOW);
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_SetDualViewState(MS_BOOL bEnable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SetDualViewState PQArgs;
    PQArgs.bEnable = bEnable;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_DUAL_VIEW_STATE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_GetVGASubCaseForceTo422Mode_U2(void* pInstance)
{
    return PQ_VGASubCaseForceTo422Mode;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetVGASubCaseForceTo422Mode(void)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetVGASubCaseForceTo422Mode PQArgs;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_VGASUBCASE_FORCETO_422MODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_GetDualViewState_U2(void* pInstance)
{
    return _bDualViewEnable;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetDualViewState(void)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetDualViewState PQArgs;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_DUAL_VIEW_STATE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

//Is Force Input to do IP_CSC
MS_BOOL MDrv_PQ_ForceVideoInputMode_U2(void* pInstance,MS_BOOL bEnable, PQ_WIN eWindow)
{
    _bForceVideoInputMode[eWindow] = bEnable;
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_ForceVideoInputMode(MS_BOOL bEnable, PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_ForceVideoInputMode PQArgs;
    PQArgs.bEnable = bEnable;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_FORCE_VIDEO_INPUT_MODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

//Force Input to do IP_CSC
MS_BOOL MDrv_PQ_IsForceVideoInputMode_U2(void* pInstance,PQ_WIN eWindow)
{
    if(_bForceVideoInputMode[eWindow])
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_IsForceVideoInputMode(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_IsForceVideoInputMode PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_IS_FORCE_VIDEO_INPUTMODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_Set_PointToPoint_U2(void* pInstance,XC_SETWIN_INFO *pstXC_SetWin_Info,
                               MS_BOOL bEnable,
                               PQ_WIN eWindow)
{
    //sub window don't support point to point
    if(pstXC_SetWin_Info == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pstXC_SetWin_Info == NULL\n");
        return FALSE;
    }

    if(eWindow == PQ_SUB_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_P2P,"Sub window don't support point to point\n");
        return FALSE;
    }
    if(bEnable)
    {
        //Center the display window
        if(g_IPanel.Width() >= pstXC_SetWin_Info->stCropWin.width)
        {
            pstXC_SetWin_Info->stDispWin.x = (g_IPanel.Width() - pstXC_SetWin_Info->stCropWin.width)/2;
            pstXC_SetWin_Info->stDispWin.width = pstXC_SetWin_Info->stCropWin.width;
        }
        else
        {
            pstXC_SetWin_Info->stDispWin.x = 0;
            pstXC_SetWin_Info->stDispWin.width = g_IPanel.Width();
        }

        if(g_IPanel.Height() >= pstXC_SetWin_Info->stCropWin.height)
        {
            pstXC_SetWin_Info->stDispWin.y = (g_IPanel.Height() - pstXC_SetWin_Info->stCropWin.height)/2;
            pstXC_SetWin_Info->stDispWin.height = pstXC_SetWin_Info->stCropWin.height;
        }
        else
        {
            pstXC_SetWin_Info->stDispWin.y = 0;
            pstXC_SetWin_Info->stDispWin.height = g_IPanel.Height();
        }

        PQ_LEVEL_PRINTF(PQ_DBG_P2P,"stDispWin (%u,%u,%u,%u)\n",pstXC_SetWin_Info->stDispWin.x,pstXC_SetWin_Info->stDispWin.y,pstXC_SetWin_Info->stDispWin.width,pstXC_SetWin_Info->stDispWin.height);

        //RGB HDMI, DVI, VGA
        if((((IsSrcTypeHDMI(pstXC_SetWin_Info->enInputSourceType)) && (_stHDMI_Info[eWindow].bIsHDMI == TRUE) && (QM_HDMIPC_COLORRGB(PQ_MAIN_WINDOW))) ||
             ((IsSrcTypeHDMI(pstXC_SetWin_Info->enInputSourceType)) && (_stHDMI_Info[eWindow].bIsHDMI == FALSE) && (QM_HDMIPC_COLORRGB(PQ_MAIN_WINDOW))) ||
             (IsSrcTypeVga(pstXC_SetWin_Info->enInputSourceType))) &&
             (!pstXC_SetWin_Info->bInterlace))
        {
            if(_bP2pForceToDoCsc && _enOutputColorFmt == E_PQ_COLOR_YUV444)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_P2P,"Disable Force RGB\n");
                MApi_XC_EnableForceRGBin(FALSE,  MAIN_WINDOW);
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_P2P,"Enable Force RGB\n");
                MApi_XC_EnableForceRGBin(TRUE,  MAIN_WINDOW);
            }
        }
        MApi_XC_Set_MemFmtEx(E_MS_XC_MEM_FMT_444, eWindow);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_P2P,"Disable Force RGB\n");
        MApi_XC_EnableForceRGBin(FALSE,  MAIN_WINDOW);
        MApi_XC_Set_MemFmtEx(E_MS_XC_MEM_FMT_AUTO, eWindow);
    }
    PQ_LEVEL_PRINTF(PQ_DBG_P2P,"Set Point to Point is %s",bEnable?("TRUE"):("FALSE"));
    _bPointToPointMode = bEnable;
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Set_PointToPoint(XC_SETWIN_INFO *pstXC_SetWin_Info,
                               MS_BOOL bEnable,
                               PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Set_PointToPoint PQArgs;
    PQArgs.pstXC_SetWin_Info = pstXC_SetWin_Info;
    PQArgs.bEnable = bEnable;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_POINT2POINT, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_Get_PointToPoint_U2(void* pInstance,PQ_WIN eWindow)
{
    //Only Main window support point to point
    if(eWindow == PQ_MAIN_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_P2P,"%s Point to Point\n",_bPointToPointMode?("Enable"):("Disable"));
        return _bPointToPointMode;
    }
    else
    {
        return FALSE;
    }
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Get_PointToPoint(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Get_PointToPoint PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_POINT2POINT, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_Set_PqBypass_U2(void* pInstance,XC_SETWIN_INFO *pstXC_SetWin_Info,
                               MS_BOOL bEnable,
                               PQ_WIN eWindow)
{
    if(pstXC_SetWin_Info == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pstXC_SetWin_Info == NULL\n");
        return FALSE;
    }

    if(eWindow == PQ_SUB_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_P2P,"Sub window don't support Set PQ Bypass\n");
        return FALSE;
    }
    if(bEnable)
    {
        //RGB HDMI, DVI, VGA
        if((((IsSrcTypeHDMI(pstXC_SetWin_Info->enInputSourceType)) && (_stHDMI_Info[eWindow].bIsHDMI == TRUE) && (QM_HDMIPC_COLORRGB(PQ_MAIN_WINDOW))) ||
             ((IsSrcTypeHDMI(pstXC_SetWin_Info->enInputSourceType)) && (_stHDMI_Info[eWindow].bIsHDMI == FALSE) && (QM_HDMIPC_COLORRGB(PQ_MAIN_WINDOW))) ||
             (IsSrcTypeVga(pstXC_SetWin_Info->enInputSourceType))) &&
             (!pstXC_SetWin_Info->bInterlace))
        {
            PQ_LEVEL_PRINTF(PQ_DBG_P2P,"Enable Force RGB\n");
            MApi_XC_EnableForceRGBin(TRUE,  MAIN_WINDOW);
        }
        //MApi_XC_Set_MemFmtEx(E_MS_XC_MEM_FMT_444, eWindow);
        MApi_XC_Set_MemFmtEx(E_MS_XC_MEM_FMT_444, eWindow);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_P2P,"Disable Force RGB\n");
        MApi_XC_EnableForceRGBin(FALSE,  MAIN_WINDOW);
        //MApi_XC_Set_MemFmtEx(E_MS_XC_MEM_FMT_AUTO, eWindow);
        MApi_XC_Set_MemFmtEx(E_MS_XC_MEM_FMT_AUTO, eWindow);
    }
    PQ_LEVEL_PRINTF(PQ_DBG_P2P,"Set PQ Bypass is %s",bEnable?("TRUE"):("FALSE"));
    _bPqBypassMode = bEnable;

    return TRUE;
}

MS_BOOL MDrv_PQ_Set_PqBypass(XC_SETWIN_INFO *pstXC_SetWin_Info,
                               MS_BOOL bEnable,
                               PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Set_PqBypass PQArgs;
    PQArgs.pstXC_SetWin_Info = pstXC_SetWin_Info;
    PQArgs.bEnable = bEnable;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_PQBYPASS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_Get_PqBypass_U2(void* pInstance,PQ_WIN eWindow)
{
    //Only Main window support PQ bypass
    if(eWindow == PQ_MAIN_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_P2P,"%s PQ Bypass\n",_bPqBypassMode?("Enable"):("Disable"));
        return _bPqBypassMode;
    }
    else
    {
        return FALSE;
    }
}

MS_BOOL MDrv_PQ_Get_PqBypass(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Get_PqBypass PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_POINT2POINT, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_Update_MemFormat_U2(void* pInstance)
{
    MS_U8 u8BitPerPixel = 0;
    MDrv_PQ_SetMemFormat(
        PQ_MAIN_WINDOW,
        _gbMemfmt422[PQ_MAIN_WINDOW],
        _stMode_Info[PQ_MAIN_WINDOW].bFBL,
        &u8BitPerPixel);

    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Update_MemFormat(void)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Update_MemFormat PQArgs;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_UPDATE_MEMFORMAT, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_U16 MDrv_PQ_GetHsize_U2(void* pInstance,PQ_WIN enPQWin)
{
    return _stMode_Info[enPQWin].u16input_hsize;
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetHsize(PQ_WIN enPQWin)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetHsize PQArgs;
    PQArgs.enPQWin = enPQWin;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_HSIZE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetVsize_U2(void* pInstance,PQ_WIN enPQWin)
{
    return _stMode_Info[enPQWin].u16input_vsize;
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetVsize(PQ_WIN enPQWin)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
    }

    stPQ_GetVsize PQArgs;
    PQArgs.enPQWin = enPQWin;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_VSIZE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_BOOL MDrv_PQ_IsInterlace_U2(void* pInstance,PQ_WIN enPQWin)
{
    return _stMode_Info[enPQWin].bInterlace;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_IsInterlace(PQ_WIN enPQWin)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_IsInterlace PQArgs;
    PQArgs.enPQWin = enPQWin;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_IS_INTERLACE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_LOW_3dQuality_U2(void* pInstance,MS_BOOL bEnable)
{
    if(bEnable)
    {
#if PQ_VIP_CTL
        MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_Peaking_band_AV_NTSC_Main, PQ_IP_VIP_Peaking_band_Main);
        MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_VNMR_S2_Main, PQ_IP_VIP_VNMR_Main);
        MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_VLPF_coef1_0x6_Main, PQ_IP_VIP_VLPF_coef1_Main);
#endif
    }
    else
    {
#if PQ_VIP_CTL
        MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, MDrv_PQ_GetTableIndex_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_Peaking_band_Main), PQ_IP_VIP_Peaking_band_Main);
        MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, MDrv_PQ_GetTableIndex_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_VNMR_Main), PQ_IP_VIP_VNMR_Main);
        MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, MDrv_PQ_GetTableIndex_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_VLPF_coef1_Main), PQ_IP_VIP_VLPF_coef1_Main);
#endif
    }

    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_LOW_3dQuality(MS_BOOL bEnable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_LOW_3dQuality PQArgs;
    PQArgs.bEnable = bEnable;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOW_3D_QUALITY, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_MWE_SetEnhanceQuality_U2(void* pInstance)
{
#if PQ_VIP_RGBCMY_CTL
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_R_INC_3_Main, PQ_IP_VIP_ICC_R_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_G_INC_8_Main, PQ_IP_VIP_ICC_G_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_B_INC_6_Main, PQ_IP_VIP_ICC_B_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_C_INC_6_Main, PQ_IP_VIP_ICC_C_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_M_INC_6_Main, PQ_IP_VIP_ICC_M_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_Y_INC_7_Main, PQ_IP_VIP_ICC_Y_Main);
#elif PQ_VIP_RGBCMY_CTL_VER2
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_SETTING_Enhance_6Color_Main, PQ_IP_VIP_ICC_SETTING_Main);
#endif

    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_MWE_SetEnhanceQuality(void)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_MWE_SetEnhanceQuality PQArgs;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_MWE_SET_ENHANCE_QUALITY, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_MWE_RestoreEnhanceQuality_U2(void* pInstance)
{
#if PQ_VIP_RGBCMY_CTL
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, MDrv_PQ_GetTableIndex_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_R_Main), PQ_IP_VIP_ICC_R_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, MDrv_PQ_GetTableIndex_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_G_Main), PQ_IP_VIP_ICC_G_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, MDrv_PQ_GetTableIndex_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_B_Main), PQ_IP_VIP_ICC_B_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, MDrv_PQ_GetTableIndex_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_C_Main), PQ_IP_VIP_ICC_C_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, MDrv_PQ_GetTableIndex_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_M_Main), PQ_IP_VIP_ICC_M_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, MDrv_PQ_GetTableIndex_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_Y_Main), PQ_IP_VIP_ICC_Y_Main);
#elif PQ_VIP_RGBCMY_CTL_VER2
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, MDrv_PQ_GetTableIndex_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_SETTING_Main), PQ_IP_VIP_ICC_SETTING_Main);
#endif

    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_MWE_RestoreEnhanceQuality(void)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_MWE_RestoreEnhanceQuality PQArgs;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_MWE_RESTORE_ENHANCE_QUALITY, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_MWE_RestoreOffQuality_U2(void* pInstance)
{
#if PQ_VIP_CTL
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_ON_Main, PQ_IP_VIP_ICC_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_IBC_ON_Main, PQ_IP_VIP_IBC_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_IHC_ON_Main, PQ_IP_VIP_IHC_Main);
#endif
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_MWE_RestoreOffQuality(void)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_MWE_RestoreOffQuality PQArgs;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_MWE_RESTORE_OFF_QUALITY, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_Demo_CloneWindow_U2(void* pInstance, PQ_WIN eWindow, MS_U8 u8Mode)
{
#if (PQ_DEMO_MODE_ENABLE == 1)
    if(u8Mode >= PQ_IP_MWE_diff_no_comm_NUMS_Main)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Not support the PQ Mode:%d\n",u8Mode);
        return FALSE;
    }

#if (PQ_QM_MWE_CLONE_VER1 == 1)
    MDrv_PQ_CloneTable_U2(pInstance, eWindow, PQ_IP_MWE_Clone_main_no_comm_OFF_Main, PQ_IP_MWE_Clone_main_no_comm_Main, PQ_IP_MWE_Clone_sub_no_comm_Main);
    MDrv_PQ_LoadTable_U2(pInstance, eWindow, u8Mode, PQ_IP_MWE_diff_no_comm_Main);
#elif (PQ_QM_MWE_CLONE_VER2 == 1)
    MDrv_PQ_CloneTable_U2(pInstance, eWindow, PQ_IP_Clone_main_no_comm_OFF_Main, PQ_IP_Clone_main_no_comm_Main, PQ_IP_Clone_sub_no_comm_Main);
    MDrv_PQ_LoadTable_U2(pInstance, eWindow, u8Mode, PQ_IP_MWE_diff_no_comm_Main);
#else
    UNUSED(eWindow);
#endif
#endif
    return TRUE;
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_Demo_CloneWindow(PQ_WIN eWindow, MS_U8 u8Mode)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Demo_CloneWindow PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u8Mode = u8Mode;
    PQArgs.bRet = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_DEMO_CLONE_WINDOW, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bRet;
    }
}

void MDrv_PQ_EnableScalerGamma_U2(void* pInstance, PQ_WIN eWindow, MS_BOOL bEnable)
{
#if (PQ_SCALER_GAMMA_ENABLE == 1)
    PQ_IP_Gamma_Group_Main ePQIPGamma;
    ePQIPGamma = (bEnable == TRUE)?PQ_IP_Gamma_ON_Main:PQ_IP_Gamma_OFF_Main;
    PQ_LEVEL_PRINTF(PQ_DBG_INFO,"enable gamma: %d \n",ePQIPGamma);
    MDrv_PQ_LoadTable_U2(pInstance, PQ_MAIN_WINDOW, ePQIPGamma, PQ_IP_Gamma_Main);
#endif
}
void SYMBOL_WEAK MDrv_PQ_EnableScalerGamma(PQ_WIN eWindow, MS_BOOL bEnable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
    }

    stPQ_EnableScalerGamma PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bEnable = bEnable;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_ENABLE_SCALER_GAMMA, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
    }
}
void MDrv_PQ_SetGammaTbl_U2(void* pInstance, APIPNL_GAMMA_TYPE enGammType, MS_U8 **pu8GammTable, APIPNL_GAMMA_MAPPEING_MODE enGammaMappingMode)
{
     g_IPanel.SetGammaTbl(enGammType, pu8GammTable, enGammaMappingMode);
}
void SYMBOL_WEAK MDrv_PQ_SetGammaTbl(APIPNL_GAMMA_TYPE enGammType, MS_U8 **pu8GammTable, APIPNL_GAMMA_MAPPEING_MODE enGammaMappingMode)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
    }

    stPQ_SetGammaTbl PQArgs;
    PQArgs.pu8GammTable = pu8GammTable;
    PQArgs.enGammaMappingMode = enGammaMappingMode;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_GAMMA_TBl, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
    }
}

MS_BOOL MDrv_PQ_MWE_SetOffQuality_U2(void* pInstance)
{
#if PQ_VIP_CTL
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_ICC_OFF_Main, PQ_IP_VIP_ICC_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_IBC_OFF_Main, PQ_IP_VIP_IBC_Main);
    MDrv_PQ_LoadTable_U2(pInstance,PQ_MAIN_WINDOW, PQ_IP_VIP_IHC_OFF_Main, PQ_IP_VIP_IHC_Main);
#endif

    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_MWE_SetOffQuality(void)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_MWE_SetOffQuality PQArgs;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_MWE_SET_OFF_QUALITY, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

void MDrv_PQ_MWE_CloneWindow_U2(void* pInstance,PQ_WIN eWindow)
{
#if (PQ_QM_MWE_CLONE_VER1 == 1)
    MDrv_PQ_CloneTable(eWindow, PQ_IP_MWE_Clone_main_no_comm_OFF_Main, PQ_IP_MWE_Clone_main_no_comm_Main, PQ_IP_MWE_Clone_sub_no_comm_Main);
    MApi_XC_ACE_SetMWELoadFromPQ(TRUE);
    MDrv_PQ_LoadTable_U2(pInstance,eWindow, PQ_IP_MWE_diff_no_comm_ON_Main, PQ_IP_MWE_diff_no_comm_Main);
#elif (PQ_QM_MWE_CLONE_VER2 == 1)
    MDrv_PQ_CloneTable(eWindow, PQ_IP_Clone_main_no_comm_OFF_Main, PQ_IP_Clone_main_no_comm_Main, PQ_IP_Clone_sub_no_comm_Main);
    MApi_XC_ACE_SetMWELoadFromPQ(TRUE);
    MDrv_PQ_LoadTable_U2(pInstance,eWindow, PQ_IP_MWE_diff_no_comm_ON_Main, PQ_IP_MWE_diff_no_comm_Main);
#else
    UNUSED(eWindow);
#endif
}
void SYMBOL_WEAK MDrv_PQ_MWE_CloneWindow(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_MWE_CloneWindow PQArgs;
    PQArgs.eWindow = eWindow;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_MWE_CLONE_WINDOW, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

MS_BOOL MDrv_PQ_3D_SettingForLBL_U2(void* pInstance,PQ_WIN eWindow)
{
#if PQ_QM_3D_CLONE_ENABLE
    MDrv_PQ_LoadTable_U2(pInstance,eWindow, PQ_IP_3DSettingForLBL_no_comm_ON_Main, PQ_IP_3DSettingForLBL_no_comm_Main);
    return TRUE;
#else
    UNUSED(eWindow);
    return FALSE;
#endif
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_3D_SettingForLBL(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_3D_SettingForLBL PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_3D_SETTING_FOR_LBL, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

void MDrv_PQ_3D_CloneWindow_U2(void* pInstance,PQ_3DTYPE enPQ3DType, PQ_WIN eWindow)
{
#if PQ_QM_3D_CLONE_ENABLE
    if(E_PQ_3DTYPE_XC == enPQ3DType)
    {
        MDrv_PQ_CloneTable(eWindow, PQ_IP_Clone_main_no_comm_OFF_Main, PQ_IP_Clone_main_no_comm_Main, PQ_IP_Clone_sub_no_comm_Main);
        if(((MApi_XC_Get_3D_HW_Version() >= 2) && (E_XC_3D_OUTPUT_LINE_ALTERNATIVE == MApi_XC_Get_3D_Output_Mode()))
             || ((MApi_XC_Get_3D_HW_Version() < 2) && (E_XC_3D_OUTPUT_TOP_BOTTOM == MApi_XC_Get_3D_Output_Mode())
                  && MApi_XC_3D_Is_LR_Sbs2Line()))
        {
            MDrv_PQ_3D_SettingForLBL(eWindow);
        }
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Attention: not support clone this PQ 3D type:%u from qmap!\n", enPQ3DType);
    }
#else
    UNUSED(enPQ3DType);
    UNUSED(eWindow);
#endif
}
void SYMBOL_WEAK MDrv_PQ_3D_CloneWindow(PQ_3DTYPE enPQ3DType, PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_3D_CloneWindow PQArgs;
    PQArgs.enPQ3DType = enPQ3DType;
    PQArgs.eWindow = eWindow;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_3D_CLONE_WINDOW, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

MS_BOOL MDrv_PQ_Get_SensioMode_U2(void* pInstance)
{
    return _bSensioMode;
}
MS_BOOL MDrv_PQ_Get_SensioMode(void)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Get_SensioMode PQArgs;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_SENSIOMODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

void  MDrv_PQ_Set_SensioMode_U2(void* pInstance,MS_BOOL  bEnable)
{
     _bSensioMode= bEnable;
}
void  MDrv_PQ_Set_SensioMode(MS_BOOL  bEnable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_Set_SensioMode PQArgs;
    PQArgs.bEnable = bEnable;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_SENSIOMODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

#ifndef MirrorYpbpr1080iNoiseReduce
#define MirrorYpbpr1080iNoiseReduce 0
#endif

void  MDrv_PQ_Set_DIPF_U2(void* pInstance)
{
#if MirrorYpbpr1080iNoiseReduce
      if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
     {
            //Mirror
            MApi_XC_W2BYTEMSK(REG_SC_BK22_20_L, (MS_U16)0x0800, 0x0800 );
      }
      else
      {
           //Non_Mirror
            MApi_XC_W2BYTEMSK(REG_SC_BK22_20_L, (MS_U16)0x0000, 0x0800 );
      }
#endif
}
void  MDrv_PQ_Set_DIPF(void)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_DIPF, NULL) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

void MDrv_PQ_SetSelectCSC_U2(void* pInstance,MS_U16 u16selection, PQ_WIN eWindow)
{
    HAL_PQ_set_SelectCSC(pInstance,u16selection, (eWindow == PQ_MAIN_WINDOW ? TRUE : FALSE));
}
void SYMBOL_WEAK MDrv_PQ_SetSelectCSC(MS_U16 u16selection, PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_SetSelectCSC PQArgs;
    PQArgs.u16selection = u16selection;
    PQArgs.eWindow = eWindow;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_SELECT_CSC, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

MS_BOOL MDrv_PQ_GetR2YEqSelect_U2(void* pInstance,PQ_WIN eWindow)
{
#if (Y2Y_709_601_VERSION == 0)
    if(PQ_MAIN_WINDOW == eWindow)
    {
        return ((MApi_XC_R2BYTEMSK(REG_SC_BK18_6F_L, BIT(2)) == 0)? FALSE : TRUE);
    }
    else
    {
        return ((MApi_XC_R2BYTEMSK(REG_SC_BK18_6F_L, BIT(6)) == 0)? FALSE : TRUE);
    }
#elif (Y2Y_709_601_VERSION == 1)

    MS_BOOL bIs709to601 = FALSE;
    if(PQ_MAIN_WINDOW == eWindow)
    {
        if ((MApi_XC_R2BYTEMSK(REG_SC_BK18_06_L, 0x0001) ==  0x0001) &&
            (MApi_XC_R2BYTEMSK(REG_SC_BK18_0A_L, 0x00F0) ==  0x00C0))
        {
            bIs709to601 = TRUE;
        }
    }
    else
    {
        if ((MApi_XC_R2BYTEMSK(REG_SC_BK18_76_L, 0x0001) ==  0x0001) &&
            (MApi_XC_R2BYTEMSK(REG_SC_BK18_7A_L, 0x00F0) ==  0x00C0))
        {
            bIs709to601 = TRUE;
        }
    }
    return bIs709to601;

#endif
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetR2YEqSelect(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetR2YEqSelect PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_R2YEQ_SELECT, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_GetMemFmtInGeneral_U2(void* pInstance,
                                            PQ_WIN eWindow,
                                            MS_BOOL bMemFmt422,
                                            MS_U16 *pu16TabIdx_MemFormat,
                                            MS_U16 *pu16TabIdx_444To422,
                                            MS_U16 *pu16TabIdx_422To444
                                            )
{
    PQ_NULL_POINTER(pu16TabIdx_MemFormat);
    PQ_NULL_POINTER(pu16TabIdx_444To422);
    PQ_NULL_POINTER(pu16TabIdx_422To444);

    if(bMemFmt422)
    {
        if(eWindow == PQ_MAIN_WINDOW)
        {
            *pu16TabIdx_MemFormat = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, PQ_IP_MemFormat_Main);

            if ( _upLayerConfig & E_PQ_MISC_A_HDMITX_ENABLE )
                *pu16TabIdx_444To422 = PQ_IP_444To422_OFF_Main;
            else
                *pu16TabIdx_444To422 = PQ_IP_444To422_ON_Main;

#if PQ_HAL_EWINDOW_PARAM
            *pu16TabIdx_422To444 = Hal_PQ_get_422To444_idx(pInstance,(MS_422To444_TYPE)Hal_PQ_get_422To444_mode(pInstance,bMemFmt422), PQ_MAIN_WINDOW);
#else
            *pu16TabIdx_422To444 = Hal_PQ_get_422To444_idx(pInstance,(MS_422To444_TYPE)Hal_PQ_get_422To444_mode(pInstance,bMemFmt422));
#endif
        }
#if PQ_ENABLE_PIP
        else
        {
           *pu16TabIdx_MemFormat = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MEMFORMAT));

            if ( _upLayerConfig & E_PQ_MISC_A_HDMITX_ENABLE )
            {
                *pu16TabIdx_444To422 = MHal_PQ_Get_TableIndex(eWindow, E_PQ_TABLE_IP_444TO422_OFF);
            }
            else
            {
                *pu16TabIdx_444To422 = MHal_PQ_Get_TableIndex(eWindow, E_PQ_TABLE_IP_444TO422_ON);
            }

#if PQ_HAL_EWINDOW_PARAM
            *pu16TabIdx_422To444 = Hal_PQ_get_422To444_idx(pInstance,(MS_422To444_TYPE)Hal_PQ_get_422To444_mode(pInstance,bMemFmt422), PQ_SUB_WINDOW);
#else
            *pu16TabIdx_422To444 = Hal_PQ_get_422To444_idx(pInstance,(MS_422To444_TYPE)Hal_PQ_get_422To444_mode(pInstance,bMemFmt422));
#endif
        }
#endif
    }
    else
    {
        // use 444_10BIT mode only if < 1440x1080
        // frame buffer 1920x1080x3x2 == 1440x1080x4x2
        // also bandwidth not enough for 1920x1080 444_10BIT
        if(eWindow == PQ_MAIN_WINDOW)
        {
            *pu16TabIdx_MemFormat = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, PQ_IP_MemFormat_Main);
            *pu16TabIdx_444To422 = PQ_IP_444To422_OFF_Main;
#if PQ_HAL_EWINDOW_PARAM
            *pu16TabIdx_422To444 = Hal_PQ_get_422To444_idx(pInstance,(MS_422To444_TYPE)Hal_PQ_get_422To444_mode(pInstance,bMemFmt422), PQ_MAIN_WINDOW);
#else
            *pu16TabIdx_422To444 = Hal_PQ_get_422To444_idx(pInstance,(MS_422To444_TYPE)Hal_PQ_get_422To444_mode(pInstance,bMemFmt422));
#endif
        }
#if PQ_ENABLE_PIP
        else
        {
            *pu16TabIdx_MemFormat = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, MHal_PQ_Get_IPIndex(eWindow, E_PQ_IP_MEMFORMAT));
            *pu16TabIdx_444To422 = MHal_PQ_Get_TableIndex(eWindow, E_PQ_TABLE_IP_444TO422_OFF);

#if PQ_HAL_EWINDOW_PARAM
            *pu16TabIdx_422To444 = Hal_PQ_get_422To444_idx(pInstance,(MS_422To444_TYPE)Hal_PQ_get_422To444_mode(pInstance,bMemFmt422), PQ_SUB_WINDOW);
#else
            *pu16TabIdx_422To444 = Hal_PQ_get_422To444_idx(pInstance,(MS_422To444_TYPE)Hal_PQ_get_422To444_mode(pInstance,bMemFmt422));
#endif
        }
#endif
    }

    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetMemFmtInGeneral(PQ_WIN eWindow,
                                            MS_BOOL bMemFmt422,
                                            MS_U16 *pu16TabIdx_MemFormat,
                                            MS_U16 *pu16TabIdx_444To422,
                                            MS_U16 *pu16TabIdx_422To444
                                            )
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetMemFmtInGeneral PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bMemFmt422 = bMemFmt422;
    PQArgs.pu16TabIdx_MemFormat = pu16TabIdx_MemFormat;
    PQArgs.pu16TabIdx_444To422 = pu16TabIdx_444To422;
    PQArgs.pu16TabIdx_422To444 = pu16TabIdx_422To444;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_MEMFMT_IN_GENERAL, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}


MS_BOOL MDrv_PQ_GetMADiForRFBL_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bFBL, MS_U16 *pu16TabIdx_MADi_Motion, MS_U16 *pu16TabIdx_MADi)
{
    UNUSED(pu16TabIdx_MADi_Motion);
    if((_stRFBL_Info.bEnable)&&(!bFBL))
    {
        *pu16TabIdx_MADi = Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, (MS_MADI_TYPE)_stRFBL_Info.u8MADiType);
        if(!_stMode_Info[eWindow].bInterlace)
        {
            *pu16TabIdx_MADi = Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE8_NO_MIU);

            if(eWindow == PQ_MAIN_WINDOW)
            {
                MDrv_PQ_MADiForceMotionY_U2(pInstance,PQ_MAIN_WINDOW, FALSE);
                MDrv_PQ_MADiForceMotionC_U2(pInstance,PQ_MAIN_WINDOW, FALSE);
            }
            else
            {
                MDrv_PQ_MADiForceMotionY_U2(pInstance,PQ_SUB_WINDOW, FALSE);
                MDrv_PQ_MADiForceMotionC_U2(pInstance,PQ_SUB_WINDOW, FALSE);
            }
            //printf("[%s]-[%d] :  disable Y/C motion\n", __FUNCTION__, __LINE__);
        }
        //printf("[%s]-[%d] : u16TabIdx_MADi = %d\n",__FUNCTION__,__LINE__,u16TabIdx_MADi);
    }

    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetMADiForRFBL(PQ_WIN eWindow, MS_BOOL bFBL, MS_U16 *pu16TabIdx_MADi_Motion, MS_U16 *pu16TabIdx_MADi)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetMADiForRFBL PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bFBL = bFBL;
    PQArgs.pu16TabIdx_MADi_Motion = pu16TabIdx_MADi_Motion;
    PQArgs.pu16TabIdx_MADi = pu16TabIdx_MADi;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_MADI_FOR_RFBL, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_Patch2Rto4RForFieldPackingMode_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx_MADi)
{
#if PQ_GRULE_OSD_BW_ENABLE
    // Patch: in OSD turn off case, when 2R-->4R, J2, M10 with new OPM system will flash(4R-->2R is OK)
    // we need to set 4R first, and wait some time, and do other MAdi setting, the flash will be ok.
    // which is only for non-OS platform patch, since OS platform won't turn off OSD for DFB system.
    // Only FieldPackingMode need this solution
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if((MApi_XC_IsFieldPackingModeSupported() &&
            (_stMode_Info[eWindow].u16input_hsize>=1280)&&
            (_stMode_Info[eWindow].u16input_vsize>=720) &&
            (!_bOSD_On)) &&
            (  (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,TRUE, MS_MADI_24_4R))
            || (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,TRUE, MS_MADI_25_4R))
            || (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,TRUE, MS_MADI_26_4R))
            || (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,TRUE, MS_MADI_27_4R))
#if PQ_MADI_88X_MODE
            || (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,TRUE, MS_MADI_24_4R_880))
            || (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,TRUE, MS_MADI_25_4R_880))
            || (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,TRUE, MS_MADI_25_4R_884))
#endif
        ))
        {
            //set to 4R first,
            MApi_XC_W2BYTEMSK(REG_SC_BK12_03_L, 0 , 0x0002 );
            //delay
            MApi_XC_WaitOutputVSync(1, 90, MAIN_WINDOW);
        }
    }
#else
    UNUSED(eWindow);
    UNUSED(u16TabIdx_MADi);
#endif
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Patch2Rto4RForFieldPackingMode(PQ_WIN eWindow, MS_U16 u16TabIdx_MADi)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Patch2Rto4RForFieldPackingMode PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16TabIdx_MADi = u16TabIdx_MADi;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_PATCH2RTO4R_FOR_FIELDPACKINGMODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_GetBPPInfoFromMADi_U2(void* pInstance,
                                              PQ_WIN eWindow,
                                              MS_BOOL bMemFmt422,
                                              MS_U16 u16TabIdx_MADi,
                                              MS_U16 u16TabIdx_MemFormat,
                                              MS_U8 *pu8BitsPerPixel,
                                              PQ_DEINTERLACE_MODE *peDeInterlaceMode,
                                              MS_U8 *pu8FrameCount
                                              )
{
    _u16PQDelyaTime= 0;
#if PQ_ENABLE_RFBL
    E_XC_FB_LEVEL eFBLevel = E_XC_FB_LEVEL_FB;
#endif
    PQMADi_DBG(printf("[PQ] MADI: DISABLE RFBL\n"));

    if(bMemFmt422)
    {
        // decide bits/pixel and deinterlace mode flag
        if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_24_4R))
        {
            *pu8BitsPerPixel = 20;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("24_4R PQ_DEINT_3DDI_HISTORY \n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE10_NR_Yonly_4Frame))
        {
            *pu8BitsPerPixel = 16;
            _u8CmpBitPerPixel = 12;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 1;
            PQMADi_DBG(printf("P 10 16Bits/pixel Y PQ_DEINT_2DDI_BOB MS_MADI_P_MODE10_NR_Yonly_4Frame\n"));

            *pu8FrameCount = 4;
        }
        else if( (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_24_2R)) ||
            (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_24_2R_880)))
        {
            *pu8BitsPerPixel = 20;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("24_2R PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_4R))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("25 4R PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_25_4R_720i_Main))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("25 4R 720i PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_4R_MC))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("25 4R MC_PQ_DEINT_3DDI_HISTORY\n"));

            if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"!!Error: Cannot set MS_MADI_25_4R_MC(4 fields mode) for Mirror(at least 6 fields). Fix your PQ excel!!\n");
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
            }
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_25_4R_M_MC))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("25 4R M MC_PQ_DEINT_3DDI_HISTORY\n"));
            MirrorMode_t enVideoMirror = MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW);
            if((enVideoMirror == MIRROR_HV) || (enVideoMirror == MIRROR_V_ONLY))
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"!!Error: Cannot set MS_MADi_25_4R_M_MC(4 fields mode) for Mirror(at least 6 fields). Fix your PQ excel!!\n");
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
            }
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_25_6F_4R_MC))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            u8PQDelayCount = 3;
            *pu8FrameCount = 6;
            PQMADi_DBG(printf("25 6F 4R MC_PQ_DEINT_3DDI_HISTORY\n"));

        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_6F_4R_MC))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            u8PQDelayCount = 3;
            *pu8FrameCount = 6;
            PQMADi_DBG(printf("25 6F 4R MC_PQ_DEINT_3DDI_HISTORY\n"));

        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_6R_MC))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            *pu8FrameCount = 8;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 6R MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_14F_6R_MC))  //Add New MADi mode
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            *pu8FrameCount = 14;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 14F 6R MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_14F_8R_MC))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *pu8FrameCount = 14;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 14F 8R MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_12F_8R_MC))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *pu8FrameCount = 12;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 12F 8R MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_16F_8R_MC))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *pu8FrameCount = 16;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 16F 8R MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_12F_6R_MC))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *pu8FrameCount = 12;
            u8PQDelayCount = 7; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 12F 6R MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_16F_6R_MC))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *pu8FrameCount = 16;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 16F 6R MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_25_16F_8R_M_MC))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *pu8FrameCount = 16;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 16F 8R M MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_6R_MC_NW))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *pu8FrameCount = 8;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 6R MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_6R_FilmPreDet))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            *pu8FrameCount = 8;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("25 6R FilmPreDet_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_6R_FilmPreDet_PAL))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            *pu8FrameCount = 8;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("25 6R FilmPreDet PAL_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_24_6R_FilmPreDet))
        {
            *pu8BitsPerPixel = 20;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            *pu8FrameCount = 8;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("24 6R FilmPreDet_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_24_6R_FilmPreDet_PAL))
        {
            *pu8BitsPerPixel = 20;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            *pu8FrameCount = 8;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("24 6R FilmPreDet PAL_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_8F_4R_MC))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            *pu8FrameCount = 8;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 8F 4R MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_25_8F_8R_MC))
        {
            *pu8BitsPerPixel = 24;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            *pu8FrameCount = 8;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 8F 8R MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance, (eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MOT10_NR_YMOnly_4Frame))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            *pu8FrameCount = 4;
            u8PQDelayCount = 2; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("P 10 Y PQ_DEINT_2DDI_BOB MS_MADI_P_MODE10_NR_YMonly_4Frame\n"));
        }
#if PQ_ENABLE_RFBL
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_2D)) // Need to ReDefine
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 12;
            u8PQDelayCount = 3;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_2D\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_25D)) // Need to ReDefine
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 8;
            u8PQDelayCount = 3;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_25D\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_3D)) // Need to ReDefine
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 12;
            u8PQDelayCount = 3;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_3D\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_P_MODE8_MOT)) // Need to ReDefine
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 12;
            u8PQDelayCount = 3;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_P_MODE8_MOT\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_P_MODE10_MOT)) // Need to ReDefine
        {
            *pu8BitsPerPixel = 16;
            _u8CmpBitPerPixel = 12;
            u8PQDelayCount = 1;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            *pu8FrameCount = 1;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_P_MODE10_MOT\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_3D_YC))
        {
            *pu8BitsPerPixel = 24;
            u8PQDelayCount = 3;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_3D_YC\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_3D_FILM))
        {
            *pu8BitsPerPixel = 24;
            u8PQDelayCount = 3;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_3D_FILM\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_MC))
        {
            *pu8BitsPerPixel = 24;
            u8PQDelayCount = 6;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_MC\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_P_MODE10_MOT_Y8))
        {
            *pu8BitsPerPixel = 12;
            _u8CmpBitPerPixel = 8;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 1;
            PQMADi_DBG(printf("RFBL P M 10 PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 1;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_P_MODE10_MOT_Y8\n"));
        }
#endif
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_2R))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("25 2 PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_26_4R))
        {
            *pu8BitsPerPixel = 20;
            _u8CmpBitPerPixel = 16;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("26 4 PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_26_2R))
        {
            *pu8BitsPerPixel = 20;
            _u8CmpBitPerPixel = 16;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("26 2 PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_27_4R))
        {
            *pu8BitsPerPixel = 16;
            _u8CmpBitPerPixel = 16;
            *peDeInterlaceMode = PQ_DEINT_3DDI;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("27 4 PQ_DEINT_3DDI\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_27_2R))
        {
            *pu8BitsPerPixel = 16;
            _u8CmpBitPerPixel = 16;
            *peDeInterlaceMode = PQ_DEINT_3DDI;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("27 2 PQ_DEINT_3DDI\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE8))
        {
            *pu8BitsPerPixel = 16;
            _u8CmpBitPerPixel = 16;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P 8 PQ_DEINT_2DDI_BOB\n"));

            if ( _upLayerConfig & E_PQ_MISC_A_HDMITX_ENABLE )
            {
                // HDMITX uses freerun
                // and when in some special case like 29.97fps in, 30fps out
                // sometimes we can see tearing because it is not locked yet.
                // so at least 3 frames is needed.
                *pu8FrameCount = 3;
            }else
            {
                if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
                {
                    *pu8FrameCount = 3;
                }
                else
                {
                    *pu8FrameCount = 2;
                }
            }
        }
#if (PQ_P_MODE8_3FRAME_MODE_ENABLE == 1)
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx((eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE8_3Frame))
        {
            *pu8BitsPerPixel = 16;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("P 8 PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 3;
        }
#endif
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE10))
        {
            *pu8BitsPerPixel = 20;
            _u8CmpBitPerPixel = 16;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P 10 PQ_DEINT_2DDI_BOB\n"));

            if ( _upLayerConfig & E_PQ_MISC_A_HDMITX_ENABLE )
            {
                // HDMITX uses freerun
                // and when in some special case like 29.97fps in, 30fps out
                // sometimes we can see tearing because it is not locked yet.
                // so at least 3 frames is needed.
                *pu8FrameCount = 3;
            }else
            {
                if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
                {
                    *pu8FrameCount = 3;
                }
                else
                {
                    *pu8FrameCount = 2;
                }
            }
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MOT8))
        {
            *pu8BitsPerPixel = 20;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P M 8 PQ_DEINT_2DDI_BOB\n"));

            if ( _upLayerConfig & E_PQ_MISC_A_HDMITX_ENABLE )
            {
                // HDMITX uses freerun
                // and when in some special case like 29.97fps in, 30fps out
                // sometimes we can see tearing because it is not locked yet.
                // so at least 3 frames is needed.
                //for IP DLC contrast fastly and DE-flicker dynamicly NR,need 4 frames.
                *pu8FrameCount = 4;
            }
            else
            {
                if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
                {
                    *pu8FrameCount = 4;
                }
                else
                {
                    *pu8FrameCount = 2;
                }
            }
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MOT10))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P M 10 PQ_DEINT_2DDI_BOB\n"));

            if ( _upLayerConfig & E_PQ_MISC_A_HDMITX_ENABLE )
            {
                // HDMITX uses freerun
                // and when in some special case like 29.97fps in, 30fps out
                // sometimes we can see tearing because it is not locked yet.
                // so at least 3 frames is needed.
                *pu8FrameCount = 3;
            }else
            {
                if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
                {
                    *pu8FrameCount = 3;
                }
                else
                {
                    *pu8FrameCount = 2;
                }
            }
        }
#if PQ_ENABLE_RFBL
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_24_RFBL_FILM))
        {
            *pu8BitsPerPixel = 12;
            u8PQDelayCount = 3;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            PQMADi_DBG(printf("24 RFBL FILM PQ_DEINT_3DDI_HISTORY\n"));
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_24_RFBL_FILM\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_24_RFBL_NFILM))
        {
            *pu8BitsPerPixel = 12;
            u8PQDelayCount = 3;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            PQMADi_DBG(printf("24 RFBL NFILM PQ_DEINT_3DDI_HISTORY\n"));
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_24_RFBL_NFILM\n"));
        }
#endif
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_FBL_DNR))
        {
            *pu8BitsPerPixel = 16;
            u8PQDelayCount = 3;
            *peDeInterlaceMode = PQ_DEINT_2DDI_AVG;
            PQMADi_DBG(printf("FBL DNR PQ_DEINT_2DDI_AVG\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_FBL_MIU))
        {
            *pu8BitsPerPixel = 16;
            u8PQDelayCount = 3;
            *peDeInterlaceMode = PQ_DEINT_2DDI_AVG;
            PQMADi_DBG(printf("FBL MIU PQ_DEINT_2DDI_AVG\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE8_NO_MIU))
        {
            *pu8BitsPerPixel = 16;
            u8PQDelayCount = 2;
            *peDeInterlaceMode = PQ_DEINT_2DDI_AVG;
#if PQ_ENABLE_RFBL
            eFBLevel = E_XC_FB_LEVEL_FBL;
#endif//#if PQ_ENABLE_RFBL
            PQMADi_DBG(printf("P 8 NO MIU PQ_DEINT_2DDI_AVG\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MOT10_8Frame))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("P M 10 8Frame PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 8;
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MOT10_4Frame))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("P M 10 4Frame PQ_DEINT_2DDI_BOB\n"));
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MC_3Frame))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("P MC 3Frame PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 3;
            if((MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL) &&
               (QM_GetInputVFreq(eWindow) > 235) && (QM_GetInputVFreq(eWindow) < 245))
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"!!Error: Cannot set MS_MADI_P_MODE_MC_3Frame for mirror 24p(at least 4 frame number). Fix your PQ excel!!\n");
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
            }
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MC_4Frame))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("P MC 4Frame PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 4;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE_MC_4Frame_Yonly_Main))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("P MC 4Frame Yonly Main PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 4;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MC_5Frame))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("P MC 5Frame PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 5;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MC_6Frame))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("P MC 6Frame PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 6;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MC_6Frame_6R))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            PQMADi_DBG(printf("P MC 6Frame 6R PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 6;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MC_8Frame_6R))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            PQMADi_DBG(printf("P MC 8Frame 6R PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 8;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MC_12Frame_8R))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            PQMADi_DBG(printf("P MC 12Frame 8R PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 12;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE_MC_12Frame_8R_M))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            PQMADi_DBG(printf("P MC 12Frame 8R M PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 12;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE_MC_8Frame_8R_M))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            PQMADi_DBG(printf("P MC 8Frame 8R M PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 8;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MC_16Frame_8R))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            PQMADi_DBG(printf("P MC 16Frame 8R PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 16;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_N_MC_12Frame_8R))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            PQMADi_DBG(printf("P M N MC 12 8Frame PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 12;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_P_MC_12Frame_8R))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            PQMADi_DBG(printf("P M P MC 12 8Frame PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 12;
        }
#if PQ_ENABLE_RFBL
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_P_MODE10_MOT))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 12;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("RFBL P M 10 PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 2;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_P_MODE10_MOT\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_P_MODE8_MOT))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 12;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("RFBL P M 8 PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 2;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_P_MODE8_MOT\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_RFBL_P_MODE_MC))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 6;
            PQMADi_DBG(printf("RFBL P M MC PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 2;
            eFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            PQMADi_DBG(printf("[PQ] MADI: ENABLE RFBL: MS_MADI_RFBL_P_MODE_MC\n"));
        }
#endif
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_6F_4R_MC_I))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 12;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 6;
            PQMADi_DBG(printf("I 25 4R MC_PQ_DEINT_3DDI_HISTORY\n"));
//#if (STB_ENABLE == 0)
//            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
//            *pu8FrameCount = 8;
//#else
            *pu8FrameCount = 6;
//#endif
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_6F_4R_MC_P))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 6;
            PQMADi_DBG(printf("P 25 4R MC_PQ_DEINT_3DDI_HISTORY\n"));
//#if (STB_ENABLE == 0)
//            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
//            *pu8FrameCount = 8;
//#else
            *pu8FrameCount = 6;
//#endif
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_25_4R_I))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("I 25 4R PQ_DEINT_3DDI_HISTORY\n"));
            *pu8FrameCount = 6;
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_25_4R_P))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("P 25 4R PQ_DEINT_3DDI_HISTORY\n"));
            *pu8FrameCount = 6;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_25_16F_6R_M_MC))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *pu8FrameCount = 16;
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("25 16F 6R M MC_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE_MC_M_4F))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("P MC M 4Frame PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 4;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE8_M))
        {
            *pu8BitsPerPixel = 20;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQ_LEVEL_PRINTF(PQ_DBG_MADI,"24_2R MS_MADi_P_MODE8_M\n");
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE10_M))
        {
            *pu8BitsPerPixel = 20;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P 10 M PQ_DEINT_2DDI_BOB\n"));

            if ( _upLayerConfig & E_PQ_MISC_A_HDMITX_ENABLE )
            {
                // HDMITX uses freerun
                // and when in some special case like 29.97fps in, 30fps out
                // sometimes we can see tearing because it is not locked yet.
                // so at least 3 frames is needed.
                *pu8FrameCount = 3;
            }
            else
            {
                if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
                {
                    *pu8FrameCount = 3;
                }
                else
                {
                    *pu8FrameCount = 2;
                }
            }
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_25_4R_M))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("25 4R M PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE_MOT10_M))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P Mot 10 M PQ_DEINT_2DDI_BOB\n"));

            if ( _upLayerConfig & E_PQ_MISC_A_HDMITX_ENABLE )
            {
                // HDMITX uses freerun
                // and when in some special case like 29.97fps in, 30fps out
                // sometimes we can see tearing because it is not locked yet.
                // so at least 3 frames is needed.
                *pu8FrameCount = 3;
            }else
            {
                if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
                {
                    *pu8FrameCount = 3;
                }
                else
                {
                    *pu8FrameCount = 2;
                }
            }
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE_MOT10_M_4K))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P Mot 10 M 4K PQ_DEINT_2DDI_BOB\n"));

            if ( _upLayerConfig & E_PQ_MISC_A_HDMITX_ENABLE )
            {
                // HDMITX uses freerun
                // and when in some special case like 29.97fps in, 30fps out
                // sometimes we can see tearing because it is not locked yet.
                // so at least 3 frames is needed.
                *pu8FrameCount = 3;
            }
            else
            {
                if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
                {
                    *pu8FrameCount = 3;
                }
                else
                {
                    *pu8FrameCount = 2;
                }
            }
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE_MOT8_M))
        {
            *pu8BitsPerPixel = 20;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P M 8 PQ_DEINT_2DDI_BOB\n"));

            if ( _upLayerConfig & E_PQ_MISC_A_HDMITX_ENABLE )
            {
                // HDMITX uses freerun
                // and when in some special case like 29.97fps in, 30fps out
                // sometimes we can see tearing because it is not locked yet.
                // so at least 3 frames is needed.
                *pu8FrameCount = 3;
            }
            else
            {
                if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
                {
                    *pu8FrameCount = 3;
                }
                else
                {
                    *pu8FrameCount = 2;
                }
            }
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_24_8F_4R_M_MC))
        {
            *pu8BitsPerPixel = 20;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            *pu8FrameCount = 4;
            PQ_LEVEL_PRINTF(PQ_DBG_MADI,"24 8F 4R M MC PQ_DEINT_3DDI_HISTORY 4frame\n");
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance, (eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE_MOT10_NR_Yonly_M))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P Mot 10 M PQ_DEINT_2DDI_BOB MS_MADi_P_MODE_MOT10_NR_Yonly_M\n"));

            if(_upLayerConfig & E_PQ_MISC_A_HDMITX_ENABLE )
            {
                // HDMITX uses freerun
                // and when in some special case like 29.97fps in, 30fps out
                // sometimes we can see tearing because it is not locked yet.
                // so at least 3 frames is needed.
                *pu8FrameCount = 3;
            }
            else
            {
                if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
                {
                    *pu8FrameCount = 3;
                }
                else
                {
                    *pu8FrameCount = 2;
                }
            }
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE_MOT10_NO_DNR))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P Mot 10 M PQ_DEINT_2DDI_BOB MS_MADi_P_MODE_MOT10_NO_DNR\n"));

            if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
            {
                *pu8FrameCount = 3;
            }
            else
            {
                *pu8FrameCount = 2;
            }

        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MOT10_NO_DNR_4Frame))
        {
            PQMADi_DBG(printf("PQ_DEINT_2DDI_BOB MS_MADI_P_MODE_MOT10_NO_DNR_4Frame\n"));
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            *pu8FrameCount = 4;
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE_NO_DNR_NO_MOT))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P PQ_DEINT_2DDI_BOB MS_MADi_P_MODE_NO_DNR_NO_MOT\n"));

            if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
            {
                *pu8FrameCount = 3;
            }
            else
            {
                *pu8FrameCount = 2;
            }

        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE_MOT10_NR_YConly))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P PQ_DEINT_2DDI_BOB MS_MADi_P_MODE_MOT10_NR_YConly\n"));

            if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
            {
                *pu8FrameCount = 3;
            }
            else
            {
                *pu8FrameCount = 2;
            }

        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MOT10_NR_YOnly))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P Mot 10 M PQ_DEINT_2DDI_BOB MS_MADI_P_MODE_MOT10_NR_YOnly\n"));

            if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
            {
                *pu8FrameCount = 3;
            }
            else
            {
                *pu8FrameCount = 2;
            }

        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_MODE_MOT10_NR_YMOnly))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 18;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P Mot 10 M PQ_DEINT_2DDI_BOB MS_MADI_P_MOD10_NR_Yonly\n"));


            if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
            {
                *pu8FrameCount = 3;
            }
            else
            {
                *pu8FrameCount = 2;
            }
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_Yonly_4Frame))
        {
            *pu8BitsPerPixel = 16;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P PQ_DEINT_2DDI_BOB MS_MADI_P_MODE_Yonly_4Frame\n"));
            *pu8FrameCount = 4;
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE_MOT8_4Frame))
        {
            *pu8BitsPerPixel = 20;
            _u8CmpBitPerPixel = 16;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P M 8_4Frame PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 4;
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_25_8F_4R_MC_I))
        {
            *pu8BitsPerPixel = 16;
            MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            *pu8FrameCount = 8;
            u8PQDelayCount = 3; ////Because only enable DI and NR,need 3;
            PQMADi_DBG(printf("25 8F 4R MC I_PQ_DEINT_3DDI_HISTORY\n"));
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE8_MC_4Frame_Main))
        {
            *pu8BitsPerPixel = 20;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("P MC 4Frame PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 4;
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE8_MC_4Frame_YMonly_Main))
        {
            *pu8BitsPerPixel = 20;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            MApi_XC_Set_OPWriteOffEnable(DISABLE,MAIN_WINDOW);
            u8PQDelayCount = 6; //When MC mode and read/write bank mapping mode set 2, have to delay 125 ms .
            PQMADi_DBG(printf("P MC 4Frame PQ_DEINT_2DDI_BOB_YMonly\n"));
            *pu8FrameCount = 4;
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_25_8F_4R_MC_YM))
        {
            *pu8BitsPerPixel = 20;
            *peDeInterlaceMode = PQ_DEINT_3DDI_HISTORY;
            u8PQDelayCount = 3;
            PQMADi_DBG(printf("25_8F_4R_MC_YM PQ_DEINT_3DDI_HISTORY\n"));
            *pu8FrameCount = 8;
        }
        else
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!! Invalid MADI, u16TabIdx_MADi = %u",u16TabIdx_MADi);
            PQ_ASSERT(0);
        }
    }
    else
    {
        //in order to disable user mode,
        if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE8_444))
        {
            *pu8BitsPerPixel = 24;
            _u8CmpBitPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P 8 444 PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 2;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE8_M_444))
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P MODE8 M 444 PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 2;
        }
        else if(u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADI_P_MODE10_444))
        {
            *pu8BitsPerPixel = 30;
            _u8CmpBitPerPixel = 30;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P 10 444 PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 2;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE10_M_444))
        {
            *pu8BitsPerPixel = 30;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P 10 M 444 PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 2;
        }
        else if (u16TabIdx_MADi == Hal_PQ_get_madi_idx(pInstance,(eWindow == PQ_MAIN_WINDOW) ? TRUE : FALSE, MS_MADi_P_MODE10_M_444_4K))
        {
            *pu8BitsPerPixel = 30;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            u8PQDelayCount = 2;
            PQMADi_DBG(printf("P 10 M 444 4K PQ_DEINT_2DDI_BOB\n"));
            *pu8FrameCount = 2;
        }
        if(u16TabIdx_MemFormat == PQ_IP_MemFormat_444_10BIT_Main
#if PQ_ENABLE_PIP
            ||u16TabIdx_MemFormat == PQ_IP_MemFormat_444_10BIT_Sub
#endif
            )
        {
            *pu8BitsPerPixel = 32;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            PQMADi_DBG(printf("PQ_IP_MemFormat_444_10BIT\n"));

            if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
            {
                *pu8FrameCount = 3;
                u8PQDelayCount = 3;

            }
            else
            {
                *pu8FrameCount = 2;
                u8PQDelayCount = 2;
            }
        }
        else if(u16TabIdx_MemFormat == PQ_IP_MemFormat_444_8BIT_Main
#if PQ_ENABLE_PIP
            ||u16TabIdx_MemFormat == PQ_IP_MemFormat_444_8BIT_Sub
#endif
            )
        {
            *pu8BitsPerPixel = 24;
            *peDeInterlaceMode = PQ_DEINT_2DDI_BOB;
            PQMADi_DBG(printf("PQ_IP_MemFormat_444_8BIT\n"));

            if(MApi_XC_GetIPAutoNoSignal((SCALER_WIN)eWindow)==ENABLE)
            {
                *pu8FrameCount = 3;
                u8PQDelayCount = 3;
            }
            else
            {
                *pu8FrameCount = 2;
                u8PQDelayCount = 2;
            }
        }
        else
        {
            *pu8BitsPerPixel = 24;
            PQMADi_DBG(printf("PQ_IP_MemFormat_422MF_Main\n"));
            *pu8FrameCount = 2;
        }
#if (PQ_GRULE_GAME_MODE_ENABLE == 0)
        if(MApi_XC_GetUCEnabled((SCALER_WIN)eWindow))   //if on need disable.
        {
            MDrv_PQ_DisableUCFeature(PQ_MAIN_WINDOW);
        }
#endif
    }
// if there is no video mute, don't DO set FBL enable/disable for prevent screen flash
#if PQ_ENABLE_RFBL
    if (TRUE == MApi_XC_IsBlackVideoEnable((SCALER_WIN)eWindow))
    {
        if (eWindow == PQ_MAIN_WINDOW)
        {
#ifdef UFO_XC_FB_LEVEL
           E_XC_FB_LEVEL entmpFBlevel = MApi_XC_Get_FB_Level(MAIN_WINDOW);
           PQ_LEVEL_PRINTF(PQ_DBG_MADI,"enFBLevel = %d entmpFBlevel = %d\n",eFBLevel,entmpFBlevel);
            switch(entmpFBlevel)
            {
                case E_XC_FB_LEVEL_FB_VSD:
                case E_XC_FB_LEVEL_FBL_VSD:
                case E_XC_FB_LEVEL_RFBL_VSD:
                {
                    if(eFBLevel == E_XC_FB_LEVEL_FB)
                    {
                        eFBLevel = E_XC_FB_LEVEL_FB_VSD;
                    }
                    else if(eFBLevel == E_XC_FB_LEVEL_FBL)
                    {
                        eFBLevel = E_XC_FB_LEVEL_FBL_VSD;
                    }
                    else if(eFBLevel == E_XC_FB_LEVEL_RFBL_DI)
                    {
                        eFBLevel = E_XC_FB_LEVEL_RFBL_VSD;
                    }
                    else
                    {
                        PQ_LEVEL_PRINTF(PQ_DBG_MADI,"Exception enFBLevel = %d\n",eFBLevel);
                    }
                    break;
                }
                case E_XC_FB_LEVEL_FB_MVOPDROPLINE:
                case E_XC_FB_LEVEL_FBL_MVOPDROPLINE:
                case E_XC_FB_LEVEL_RFBL_MVOPDROPLINE:
                {
                    if(eFBLevel == E_XC_FB_LEVEL_FB)
                    {
                        eFBLevel = E_XC_FB_LEVEL_FB_MVOPDROPLINE;
                    }
                    else if(eFBLevel == E_XC_FB_LEVEL_FBL)
                    {
                        eFBLevel = E_XC_FB_LEVEL_FBL_MVOPDROPLINE;
                    }
                    else if(eFBLevel == E_XC_FB_LEVEL_RFBL_DI)
                    {
                        eFBLevel = E_XC_FB_LEVEL_RFBL_MVOPDROPLINE;
                    }
                    else
                    {
                        PQ_LEVEL_PRINTF(PQ_DBG_MADI,"Exception enFBLevel = %d\n",eFBLevel);
                    }
                    break;
                }
                default:
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_MADI,"No VSD or MVOPDROPLINE entmpFBlevel = %d\n",entmpFBlevel);
                    break;
                }
           }
           PQ_LEVEL_PRINTF(PQ_DBG_MADI,"enFBLevel = %d\n",eFBLevel);
           MApi_XC_Set_FB_Level(eFBLevel, MAIN_WINDOW);
#else
           MS_BOOL bFBL = FALSE;
           MS_BOOL bRFBL = FALSE;

           if(eFBLevel == E_XC_FB_LEVEL_RFBL_DI)
           {
                bFBL = FALSE;
                bRFBL = TRUE;
           }
           else if(eFBLevel == E_XC_FB_LEVEL_FBL)
           {
                bFBL = TRUE;
                bRFBL = FALSE;
           }
           else
           {
                bFBL = FALSE;
                bRFBL = FALSE;
           }
           MApi_XC_EnableRequest_FrameBufferLess(bRFBL);
           MApi_XC_EnableFrameBufferLess(bFBL);
#endif
        }
        else
        {
           PQMADi_DBG(printf("%s  %d,  sub have no FBL mode!\n",__FUNCTION__,__LINE__));
        }
    }
#endif
    if ((QM_GetInputVFreq(eWindow) != 0) && (QM_GetInputVFreq(eWindow) != 1))
        _u16PQDelyaTime = u8PQDelayCount * ((10000 + (QM_GetInputVFreq(eWindow)/2))/QM_GetInputVFreq(eWindow));  // frame duration is (1 second/input V freq)
    else
        _u16PQDelyaTime = u8PQDelayCount * 20; // default keep original rule, use 20ms as frame duration

    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetBPPInfoFromMADi(PQ_WIN eWindow,
                                              MS_BOOL bMemFmt422,
                                              MS_U16 u16TabIdx_MADi,
                                              MS_U16 u16TabIdx_MemFormat,
                                              MS_U8 *pu8BitsPerPixel,
                                              PQ_DEINTERLACE_MODE *peDeInterlaceMode,
                                              MS_U8 *pu8FrameCount
                                              )
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetBPPInfoFromMADi PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bMemFmt422 = bMemFmt422;
    PQArgs.u16TabIdx_MADi = u16TabIdx_MADi;
    PQArgs.u16TabIdx_MemFormat = u16TabIdx_MemFormat;
    PQArgs.pu8BitsPerPixel = pu8BitsPerPixel;
    PQArgs.peDeInterlaceMode = peDeInterlaceMode;
    PQArgs.pu8FrameCount = pu8FrameCount;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_BPPINFO_FROM_MADI, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_ChangeMemConfigFor3D_U2(void* pInstance,
                                            PQ_WIN eWindow,
                                            MS_BOOL bMemFmt422,
                                            MS_U16 *pu16TabIdx_MemFormat,
                                            MS_U16 *pu16TabIdx_444To422,
                                            MS_U16 *pu16TabIdx_422To444,
                                            MS_U8 *pu8BitsPerPixel,
                                            MS_U16 *pu16TabIdx_MADi_Motion,
                                            MS_U16 *pu16TabIdx_MADi
                                            )
{
    UNUSED(pu16TabIdx_MADi_Motion);
    if(eWindow == PQ_MAIN_WINDOW)
    {
        //reduce mem size, put it into 16M, 2 frame mode.
        if((MApi_XC_Get_3D_Input_Mode((SCALER_WIN)eWindow) == E_XC_3D_INPUT_FRAME_PACKING)
            && (MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_FRAME_ALTERNATIVE)
            && (_stMode_Info[eWindow].u16input_hsize == 1920)
            && (_stMode_Info[eWindow].u16input_vsize == 2205))
        {
#if PQ_EN_UCNR_3D_MADI
            *pu16TabIdx_MADi = Hal_PQ_get_madi_idx(pInstance,TRUE , MS_MADI_P_MODE8_2BYTE_3D);
#else
            *pu16TabIdx_MADi = Hal_PQ_get_madi_idx(pInstance,TRUE, MS_MADI_P_MODE8_444);
#endif
            *pu8BitsPerPixel = 16;

            // DVI is 444 input, we need to conver it to 422, otherwise DVI will use 32 bpp instead, not 16
            // because 444 format combine to the setting of pu16TabIdx_MemFormat
            if(!bMemFmt422)
            {
                //PQ_IP_MemFormat_Main 422
                *pu16TabIdx_MemFormat = PQ_IP_MemFormat_422MF_Main;
                *pu16TabIdx_444To422 = PQ_IP_444To422_ON_Main;
                //PQ_IP_422To444_Main on
                *pu16TabIdx_422To444 = PQ_IP_422To444_ON_Main;
                PQ_LEVEL_PRINTF(PQ_DBG_BW,"3D: force 422, 444to422 and 422to444 ON\n");
            }

            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"3D: modify progressive memory fmt for 1080p framepacking\n");
        }

        if(MApi_XC_Get_3D_HW_Version() < 2)
        {
            //PQ main page maybe MADi mode to 8 field mode, but it cannot do madi for line by line output.
            //so we force it back to normal 25_2R like mode.
            if(((MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_TOP_BOTTOM) && (TRUE == _stMode_Info[eWindow].bInterlace))
               || ((MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_TOP_BOTTOM_HW) && (TRUE == _stMode_Info[eWindow].bInterlace)))
            {
                // we must use MS_MADI_25_4R or 2R to avoid MC madi mode for dnr offset calcuation problem.
                if(TRUE == _stMode_Info[eWindow].bInterlace)
                {
                    if((_stMode_Info[eWindow].u16input_vsize >= 900) &&
                        (_stMode_Info[eWindow].u16input_hsize >= 1440) &&
                        (bw_info.u32Miu1MemSize==0))// 1ddr case, should save bw
                    {
                        *pu16TabIdx_MADi = Hal_PQ_get_madi_idx(pInstance, TRUE, MS_MADI_25_2R);
                        PQ_LEVEL_PRINTF(PQ_DBG_BW,"3D: force to 25_2R\n");
                    }
                    else
                    {
                        *pu16TabIdx_MADi = Hal_PQ_get_madi_idx(pInstance, TRUE, MS_MADI_25_4R);
                        PQ_LEVEL_PRINTF(PQ_DBG_BW,"3D: force to 25_4R\n");
                    }
                    if(eWindow == PQ_MAIN_WINDOW)
                    {
                        MApi_XC_Set_OPWriteOffEnable(ENABLE, MAIN_WINDOW); //default
                    }
                    else
                    {
                        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "No need to enable OP write with sub window, it would change main window setting \n");
                    }
                }

                //Madi do line by line must use 422 format
                if(!bMemFmt422)
                {
                    //PQ_IP_MemFormat_Main 422
                    *pu16TabIdx_MemFormat = PQ_IP_MemFormat_422MF_Main;
                    *pu16TabIdx_444To422 = PQ_IP_444To422_ON_Main;
                    //PQ_IP_422To444_Main on
                    *pu16TabIdx_422To444 = PQ_IP_422To444_ON_Main;
                    PQ_LEVEL_PRINTF(PQ_DBG_BW,"3D: force 422, 444to422 and 422to444 ON\n");
                }

                // we can only process 422 format for E_XC_3D_INPUT_TOP_BOTTOM.
                // so, if it's 444 format, we have to force it to be 422 and the u8BitsPerPixel is also changed to 24.
                *pu8BitsPerPixel = 24;
                PQ_LEVEL_PRINTF(PQ_DBG_BW,"3D:force u8BitsPerPixel to 24\n");
            }

            //frame alterntive must use bob mode
            if((E_XC_3D_OUTPUT_FRAME_ALTERNATIVE_NOFRC == MApi_XC_Get_3D_Output_Mode()) ||
               (E_XC_3D_OUTPUT_FRAME_ALTERNATIVE == MApi_XC_Get_3D_Output_Mode()))
            {
                MDrv_PQ_EnableMADIForce(PQ_MAIN_WINDOW, TRUE);
#if PQ_MADI_DFK
                ST_PQ_IPInfo stIPInfo;
                ST_PQ_IPInfo stUpdatedIPInfo;
                memset(&stIPInfo, 0, sizeof(ST_PQ_IPInfo));
                memset(&stUpdatedIPInfo, 0, sizeof(ST_PQ_IPInfo));
                stIPInfo.u16IPIdx = PQ_IP_MADi_DFK_Main;
                stIPInfo.u16TableIdx = PQ_IP_MADi_DFK_OFF_Main;
                stIPInfo.enPQEngineType = PQ_XC0_STD;
                MHal_PQ_RemappingIP(pInstance, stIPInfo, &stUpdatedIPInfo, TRUE);
                MDrv_PQ_LoadTable_Ex_U2(pInstance,eWindow, stUpdatedIPInfo.u16TableIdx, stUpdatedIPInfo.u16IPIdx, stUpdatedIPInfo.enPQEngineType);
#endif
                MDrv_PQ_LoadTable_U2(pInstance,eWindow, PQ_IP_EODi_OFF_Main, PQ_IP_EODi_Main);
            }
            //line by line must use weave mode
            else if(E_XC_3D_OUTPUT_LINE_ALTERNATIVE == MApi_XC_Get_3D_Output_Mode())
            {
                MDrv_PQ_EnableMADIForce(eWindow, FALSE);
            }
        }

        //Frame packing interlace must use 25_2R for
        // A. Madi for line by line output for old 3D engine. Or,
        // B. better quality. (P mode will shaking a little)
        if(((MApi_XC_Get_3D_Input_Mode((SCALER_WIN)eWindow) == E_XC_3D_INPUT_FRAME_PACKING) && (MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_FRAME_L) && (_stMode_Info[eWindow].u16input_vsize == 2228))
            || ((MApi_XC_Get_3D_Input_Mode((SCALER_WIN)eWindow) == E_XC_3D_INPUT_FRAME_PACKING) && (MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_FRAME_R) && (_stMode_Info[eWindow].u16input_vsize == 2228))
            || ((MApi_XC_Get_3D_Input_Mode((SCALER_WIN)eWindow) == E_XC_3D_INPUT_FRAME_PACKING) && (MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_TOP_BOTTOM) && (_stMode_Info[eWindow].u16input_vsize == 2228))
            || ((MApi_XC_Get_3D_Input_Mode((SCALER_WIN)eWindow) == E_XC_3D_INPUT_FRAME_PACKING) && (MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF) && (_stMode_Info[eWindow].u16input_vsize == 2228))
            || ((MApi_XC_Get_3D_Input_Mode((SCALER_WIN)eWindow) == E_XC_3D_INPUT_FRAME_PACKING) && (MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_LINE_ALTERNATIVE) && (_stMode_Info[eWindow].u16input_vsize == 2228))
            || ((MApi_XC_Get_3D_Input_Mode((SCALER_WIN)eWindow) == E_XC_3D_INPUT_FRAME_PACKING) && (MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_FRAME_ALTERNATIVE) && (_stMode_Info[eWindow].u16input_vsize == 2228))
            )
        {
            //framepacking interlace 1080i
            *pu16TabIdx_MADi = Hal_PQ_get_madi_idx(pInstance,TRUE, MS_MADI_25_2R);
            _stMode_Info[eWindow].bInterlace = TRUE;
            *pu8BitsPerPixel = 24;

            PQ_LEVEL_PRINTF(PQ_DBG_BW,"3D: force to 25_2R\n");
        }
    }
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_ChangeMemConfigFor3D(PQ_WIN eWindow,
                                            MS_BOOL bMemFmt422,
                                            MS_U16 *pu16TabIdx_MemFormat,
                                            MS_U16 *pu16TabIdx_444To422,
                                            MS_U16 *pu16TabIdx_422To444,
                                            MS_U8 *pu8BitsPerPixel,
                                            MS_U16 *pu16TabIdx_MADi_Motion,
                                            MS_U16 *pu16TabIdx_MADi
                                            )
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_ChangeMemConfigFor3D PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bMemFmt422 = bMemFmt422;
    PQArgs.pu16TabIdx_MemFormat = pu16TabIdx_MemFormat;
    PQArgs.pu16TabIdx_444To422 = pu16TabIdx_444To422;
    PQArgs.pu16TabIdx_422To444 = pu16TabIdx_422To444;
    PQArgs.pu8BitsPerPixel = pu8BitsPerPixel;
    PQArgs.pu16TabIdx_MADi_Motion = pu16TabIdx_MADi_Motion;
    PQArgs.pu16TabIdx_MADi = pu16TabIdx_MADi;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_CHANGE_MEMCONFIG_FOR3D, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_ForceBPPForDynamicMemFmt_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bMemFmt422, MS_U8 *pu8BitsPerPixel)
{
    //force Memory FMT and use the same pixel number to avoid the garbage when memory mode is changed.
    if(bMemFmt422)
    {
        MS_U16 u16MemFMT= 0;

        if (eWindow == PQ_MAIN_WINDOW)
        {
            u16MemFMT = MApi_XC_R2BYTEMSK(REG_SC_BK12_02_L, 0xFF);
        }
        else
        {
            u16MemFMT = MApi_XC_R2BYTEMSK(REG_SC_BK12_42_L, 0xFF);
        }

        if(!_stMode_Info[eWindow].bInterlace)
        {
            // for dynamically memory format changing, we need to keep it as 24 bits
            if(SUPPORT_DYNAMIC_MEM_FMT)//STB could be false
            {
                //Please refer to "MDrv_PQ_ChangeMemConfigFor3D"
                //reduce mem size, put it into 16M, 2 frame mode.
                if(!((MApi_XC_Get_3D_Input_Mode((SCALER_WIN)eWindow) == E_XC_3D_INPUT_FRAME_PACKING)
                    && (MApi_XC_Get_3D_Output_Mode() == E_XC_3D_OUTPUT_FRAME_ALTERNATIVE)
                    && (_stMode_Info[eWindow].u16input_hsize == 1920)
                    && (_stMode_Info[eWindow].u16input_vsize == 2205)))
                    {
                        if(u16MemFMT != 0)
                        {
                            *pu8BitsPerPixel = 24;
                        }
                    }
            }
        }


        if((u16MemFMT == 0xBB) || (u16MemFMT == 0xAA) || (u16MemFMT == 0x99) || (u16MemFMT == 0x88))
        {
            *pu8BitsPerPixel = 24;
        }
        else if((u16MemFMT == 0x55) || (u16MemFMT == 0x66) ||(u16MemFMT == 0x44))
        {
            *pu8BitsPerPixel = 20;
        }
        else
        {
            //decide by original setting
        }
    }
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_ForceBPPForDynamicMemFmt(PQ_WIN eWindow, MS_BOOL bMemFmt422, MS_U8 *pu8BitsPerPixel)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_ForceBPPForDynamicMemFmt PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bMemFmt422 = bMemFmt422;
    PQArgs.pu8BitsPerPixel = pu8BitsPerPixel;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_FORCE_BPP_FOR_DYNAMIC_MEMFMT, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_SuggestFrameNum_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx_MADi, MS_U8 u8FrameCount)
{
    //the chips, who are older than SCMI_V2, set frame count reg by Qmap.
    //the chips, who are SCMI_V2, the frame count is suggested by Qmap, but sw code judge and replace the value.
    //      and then, set to hw reg
    //note: SUPPORT_SCMI_V2 is corresponding withe utopia flag: _FIELD_PACKING_MODE_SUPPORTED
    //      we can remove SUPPORT_SCMI_V2 later
    if(MApi_XC_IsFieldPackingModeSupported())
    {
        //Set Frame number
        UNUSED(u16TabIdx_MADi);
        if(bSetFrameCount )
        {
            _u16PQSuggestedFrameNum[eWindow] = u8FrameCount;
            PQ_LEVEL_PRINTF(PQ_DBG_MADI,"eWindow = %u, _u16PQSuggestedFrameNum = %u\n",eWindow,u8FrameCount);
        }
    }
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_SuggestFrameNum(PQ_WIN eWindow, MS_U16 u16TabIdx_MADi, MS_U8 u8FrameCount)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SuggestFrameNum PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16TabIdx_MADi = u16TabIdx_MADi;
    PQArgs.u8FrameCount = u8FrameCount;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SUGGEST_FRAMENUM, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

#if (PQ_GRULE_GAME_MODE_ENABLE == 1)
static MS_BOOL _MDrv_PQ_EnableGameModeUCPatch(void* pInstance,PQ_WIN eWindow)
{
    if(_gIsEnable8LBMode)
    {
        MDrv_PQBin_Enable8LBMode(pInstance,TRUE);
        eWindow = MAIN_WINDOW; // default only set main window
        {
            MApi_XC_Set_OPWriteOffEnableToReg(ENABLE,eWindow); //default
        }
    }
    return TRUE;
}

static MS_BOOL _MDrv_PQ_DisableGameModeUCPatch(void* pInstance,PQ_WIN eWindow)
{
    MS_BOOL bRet = TRUE;
    MS_U8 u8Index = 0 ;
    MS_U16 u16IPIdx = 0xFFFF;
    MS_U16 u16IPTabIdx_Mode = 0xFFFF;
    MS_U16 u16TabIdx_UCCTL= 0xFFFF;

    if(eWindow == PQ_MAIN_WINDOW)
    {
#if (PQ_GRULE_DDR_SELECT_ENABLE == 1)
#if (PQ_DDR_SELECT_VER == 1)
        //do nothing, because ddr select rule has no uc item in version1
#elif (PQ_DDR_SELECT_VER == 2)
        MS_U16 u16PQ_DDRSELIdx = 0;
        MS_U16 u16TabIdx_MADi_DFK_DDR = 0xFFFF;
        MS_U16 u16TabIdx_MADi_SST_DDR = 0xFFFF;
        MS_U16 u16TabIdx_MADi_EODiW_DDR = 0xFFFF;
        MS_U16 u16TabIdx_UCCTL_DDR = 0xFFFF;
        if(bw_info.bDDR2 == FALSE) // DDR 3
        {
             if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
             {
                 PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"MDrv_PQ_GetDDRMirrorSelectInfo: DDR3 mirror\n");
                 u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR3_Mirror_Main);
             }
             else
             {
                 PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"MDrv_PQ_GetDDRMirrorSelectInfo: DDR3 non-mirror\n");
                 u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR3_Main);
             }
        }
        else // DDR 2
        {
             if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
             {
                 PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"MDrv_PQ_GetDDRMirrorSelectInfo: DDR2 mirror\n");
                 u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR2_Mirror_Main);
             }
             else
             {
                 PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"MDrv_PQ_GetDDRMirrorSelectInfo: DDR2 non-mirror\n");
                 u16PQ_DDRSELIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, PQ_GRule_DDR_SELECT_Main, PQ_GRule_Lvl_DDR_SELECT_DDR2_Main);
             }
        }

        u16TabIdx_MADi_DFK_DDR = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 2);
        u16TabIdx_MADi_SST_DDR = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 3);
        u16TabIdx_MADi_EODiW_DDR = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 4);
        u16TabIdx_UCCTL_DDR = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_DDR_SELECT_Main, u16PQ_DDRSELIdx, 5);
#endif
#endif
        ST_PQ_IPInfo stGRuleIPInfo;
        ST_PQ_IPInfo stUpdatedGRuleIPInfo;
        memset(&stGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));
        memset(&stUpdatedGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));

        stGRuleIPInfo.u16IPIdx= PQ_GRule_GAME_MODE_Main;
        stGRuleIPInfo.u16GruleMaxIPNum = PQ_GRULE_GAME_MODE_IP_NUM_Main;
        stGRuleIPInfo.u16GruleLevelMainNum = PQ_GRULE_GAME_MODE_NUM_Main;
        stGRuleIPInfo.u16GruleLevelNum = PQ_GRULE_GAME_MODE_LVL_NUM_Main;
        MHal_PQ_RemappingGruleIP(pInstance, stGRuleIPInfo, &stUpdatedGRuleIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);
        for(u8Index=0; u8Index<stUpdatedGRuleIPInfo.u16GruleMaxIPNum; u8Index++)
        {
            u16IPIdx = MDrv_PQ_GetGRule_IPIndex_Ex_U2(pInstance, eWindow, stUpdatedGRuleIPInfo.u16IPIdx, u8Index, stUpdatedGRuleIPInfo.enPQEngineType);
#if (PQ_GRULE_DDR_SELECT_ENABLE == 1)
#if (PQ_DDR_SELECT_VER == 1)
            //do nothing, because ddr select rule has no uc item in version1
#elif (PQ_DDR_SELECT_VER == 2)
            if (u16IPIdx ==  PQ_IP_MADi_DFK_Main)
            {
                u16IPTabIdx_Mode = u16TabIdx_MADi_DFK_DDR;
            }
            else if (u16IPIdx ==  PQ_IP_MADi_SST_Main)
            {
                u16IPTabIdx_Mode =  u16TabIdx_MADi_SST_DDR;
            }
            else if (u16IPIdx ==  PQ_IP_MADi_EODiW_Main)
            {
                u16IPTabIdx_Mode = u16TabIdx_MADi_EODiW_DDR;
            }
            else if (u16IPIdx ==  PQ_IP_UC_CTL_Main)
            {
                u16IPTabIdx_Mode = u16TabIdx_UCCTL_DDR;
            }
            else
            {
                continue;
            }
            PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[UC FEATURE]SRC: %u,  uc feature IPIdx:%u, IPIdx:%u, u8TabIdx:%u\n",
                _u16PQSrcType[eWindow], u8Index, u16IPIdx, u16IPTabIdx_Mode);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16IPTabIdx_Mode, u16IPIdx);
#endif
#endif


#if (PQ_UC_CTL == 1)
            if (u16IPIdx == PQ_IP_UC_CTL_Main)
            {
                u16TabIdx_UCCTL = u16IPTabIdx_Mode;
            }
#endif
        }
        if(_gIsEnable8LBMode)
        {
#if (PQ_UC_CTL == 1)
            if(u16TabIdx_UCCTL == PQ_IP_UC_CTL_OFF_Main)
            {
                MApi_XC_Set_OPWriteOffEnableToReg(ENABLE, MAIN_WINDOW);
            }
            else
            {
                MApi_XC_Set_OPWriteOffEnableToReg(DISABLE, MAIN_WINDOW);
            }
#endif
        }
    }
#if(PQ_ENABLE_PIP == 1)
    else // Sub Window
    {
       // Sub wibndow not support UC Feature, Do Nothing
    }
#endif
    if(_gIsEnable8LBMode)
    {
        MDrv_PQBin_Enable8LBMode(pInstance,TRUE);
    }

    return bRet;
}
#endif

static MS_BOOL _MDrv_PQ_GetTableType(void* pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, MS_U16* pu16TabType)
{
    if(pu16TabType == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Null Pointer\n");
        return FALSE;
    }
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            MS_PQBin_IP_Table_Info stTableInfo;
            memset(&stTableInfo, 0, sizeof(MS_PQBin_IP_Table_Info));

            MDrv_PQBin_GetTable(pInstance, MDrv_PQBin_GetPanelIdx(pInstance),
                                u16TabIdx,
                                u16PQIPIdx,
                                &stTableInfo,
                                 &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
            *pu16TabType = stTableInfo.u16TableType;
            PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"TableType = %d, GroupNum = %d, GroupIdx = %d \n",stTableInfo.u16TableType, stTableInfo.u16GroupNum, stTableInfo.u16GroupIdx);
        }
        else
        {
            PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"Not Support\n");
            return FALSE;
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            MS_PQBin_IP_Table_Info stTableInfo;
            memset(&stTableInfo, 0, sizeof(MS_PQBin_IP_Table_Info));

            MDrv_PQBin_GetTable(pInstance, MDrv_PQBin_GetPanelIdx(pInstance),
                                u16TabIdx,
                                u16PQIPIdx,
                                &stTableInfo,
                                 &stPQBinHeaderInfo[PQ_BIN_STD_SUB]);
            *pu16TabType = stTableInfo.u16TableType;
        }
        else
        {
           PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[GameMode]Not Support\n");
            return FALSE;
        }
    }
#endif
    else
    {
        PQ_ASSERT(0);
    }
    return TRUE;
}

#if ((PQ_XRULE_DISABLE_UC == 1)|| (PQ_SUPPORT_GAMEMODE_VER == 1))
MS_BOOL MDrv_PQ_SetGameMode_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bEnable)
{
    if (eWindow >= PQ_MAX_WINDOW)
        return FALSE;

#if (PQ_GRULE_GAMEMODE_VER == 1)
    // m7632 hdmi for game mode
    // since xc fbl and frc not disable when MM
    if(!QM_IsSourceHDMI(_enInputSourceType[eWindow]))
    {
        return FALSE;
    }
#endif

    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"MDrv_PQ_SetGameMode() bEnable: %u \n",bEnable);

#if (PQ_GRULE_GAME_MODE_ENABLE == 0)
    MS_U8  u8RWmappingNum = 0;
#endif
    _bGameModeOn[eWindow] = bEnable;
    if(bEnable)
    {
#if (PQ_GRULE_GAME_MODE_ENABLE == 1)
        MS_U16 u16PQ_GameMode_Idx = PQ_GRule_Lvl_GAME_MODE_On_NonMirror_Main_Ex;
        if(MApi_XC_GetMirrorModeTypeEx(MAIN_WINDOW) != MIRROR_NORMAL)
        {
            u16PQ_GameMode_Idx = PQ_GRule_Lvl_GAME_MODE_On_Mirror_Main_Ex;
        }
        else
        {
            u16PQ_GameMode_Idx = PQ_GRule_Lvl_GAME_MODE_On_NonMirror_Main_Ex;
        }
        _MDrv_PQ_LoadGameModeTable(pInstance, eWindow, u16PQ_GameMode_Idx);
        _MDrv_PQ_EnableGameModeUCPatch(pInstance, eWindow);
#else
            if(_bRWNumIsReset[eWindow])
            {
                _u8RWmappingNum[eWindow] = MApi_XC_GetWRBankMappingNum(eWindow);
                _bRWNumIsReset[eWindow] = FALSE;
            }

            // set the R/W bank mapping
            if (_stMode_Info[eWindow].bInterlace
            && (_stMode_Info[eWindow].u16input_vtotal <= 750)
            && (QM_IsSourceYPbPr(_enInputSourceType[eWindow]) || QM_IsSourceSV(_enInputSourceType[eWindow]) || QM_IsSourceAV(_enInputSourceType[eWindow])))
            {
                u8RWmappingNum = 0x01;
            }
            else
            {
                u8RWmappingNum = _u8RWmappingNum[eWindow];
            }
#endif
    }
    else
    {
#if (PQ_GRULE_GAME_MODE_ENABLE == 1)
        MS_U16 u16Index=0, u16IPIdx=0, u16TabIdx=0;
        MS_U16 u16IPTabIdx_Mode = 0xFFFF;
        MS_U16 u16TabIdx_UCCTL= 0xFFFF;
        ST_PQ_IPInfo stGRuleIPInfo;
        ST_PQ_IPInfo stUpdatedGRuleIPInfo;
#if(ENABLE_PQ_MLOAD == 1)
        MDrv_PQ_Set_MLoadEn(eWindow, TRUE);
#endif
        memset(&stGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));
        memset(&stUpdatedGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));
        stGRuleIPInfo.u16IPIdx= PQ_GRule_GAME_MODE_Main;
        stGRuleIPInfo.u16GruleMaxIPNum = PQ_GRULE_GAME_MODE_IP_NUM_Main;
        stGRuleIPInfo.u16GruleLevelMainNum = PQ_GRULE_GAME_MODE_NUM_Main;
        stGRuleIPInfo.u16GruleLevelNum = PQ_GRULE_GAME_MODE_LVL_NUM_Main;
        MHal_PQ_RemappingGruleIP(pInstance, stGRuleIPInfo, &stUpdatedGRuleIPInfo, TRUE);
        for(u16Index=0; u16Index<stUpdatedGRuleIPInfo.u16GruleMaxIPNum; u16Index++)
        {
            u16IPIdx = MDrv_PQ_GetGRule_IPIndex_Ex_U2(pInstance, eWindow, stUpdatedGRuleIPInfo.u16IPIdx, u16Index, stUpdatedGRuleIPInfo.enPQEngineType);
            // load Ex
            u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, eWindow, u16IPIdx, stUpdatedGRuleIPInfo.enPQEngineType);
            PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[GameMode disable]SRC: %u, Index: %u, IPIdx:%u, u8TabIdx:%u\n",
                _u16PQSrcType[eWindow], u16Index, u16IPIdx, u16TabIdx);
            MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx, u16IPIdx,stUpdatedGRuleIPInfo.enPQEngineType);
        }
        _MDrv_PQ_DisableGameModeUCPatch(pInstance, eWindow);
        _stPQ_StatusEx.bZeroFrameMode[eWindow] = FALSE;
#if(ENABLE_PQ_MLOAD == 1)
        MDrv_PQ_Set_MLoadEn(eWindow, FALSE);
#endif
#else
        // Restore original value
        u8RWmappingNum = _u8RWmappingNum[eWindow];
#endif
    }

#if (PQ_GRULE_GAME_MODE_ENABLE == 0)
    MApi_XC_SetWRBankMappingNum(u8RWmappingNum,eWindow);
#endif
    return TRUE;
}
#else
MS_BOOL MDrv_PQ_SetGameMode_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bEnable)
{
#if PQ_UC_CTL
#if(PQ_GRULE_GAME_MODE_ENABLE == 0)
    static MS_U16 _u16TabIdx_MADi_DFK_Gamemode[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
    static MS_U16 _u16TabIdx_MADi_SST_Gamemode[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
    static MS_U16 _u16TabIdx_MADi_EODiW_Gamemode[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
    static MS_U16 _u16TabIdx_UCNR_Gamemode[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
    static MS_U16 _u16TabIdx_UCDi_Gamemode[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
    static MS_U16 _u16TabIdx_UCCTL_Gamemode[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
    static MS_U16 _u16TabIdx_MADi_Gamemode[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
    static MS_U16 _u16TabIdx_MADi_Mot_Gamemode[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
    static MS_U16 _u16TabIdx_SwDriver_Gamemode[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
    static MS_U16 _u16TabIdx_PostCCS_Gamemode[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
    static MS_U16 _u16TabIdx_DHD_Gamemode[PQ_MAX_WINDOW] = {0xFFFF, 0xFFFF};
#endif
#endif
    if (eWindow >= PQ_MAX_WINDOW)
        return FALSE;
#if (PQ_SUPPORT_GAMEMODE_VER == 2)
    // hdmi /mm for game mode
    if(!(QM_IsSourceHDMI(_enInputSourceType[eWindow]) || QM_IsSourceMultiMedia(_enInputSourceType[eWindow])))
    {
        return FALSE;
    }
#endif
    #if(ENABLE_PQ_MLOAD == 0)
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"*** [Game mode] please enable PQ Mload \n");
    #endif

    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"MDrv_PQ_SetGameMode() bEnable: %u\n", bEnable);

#if (PQ_GRULE_GAME_MODE_ENABLE == 0)
    MS_U8  u8RWmappingNum = 0;
#endif
    _bGameModeOn[eWindow] = bEnable;

    if(bEnable)
    {
#if (PQ_GRULE_GAME_MODE_ENABLE == 0)
        if(_bRWNumIsReset[eWindow])
        {
            _u8RWmappingNum[eWindow] = MApi_XC_GetWRBankMappingNum(eWindow);
            _bRWNumIsReset[eWindow] = FALSE;
        }

        // set the R/W bank mapping
        if (_stMode_Info[eWindow].bInterlace
        && (_stMode_Info[eWindow].u16input_vtotal <= 750)
        && (QM_IsSourceYPbPr(_enInputSourceType[eWindow]) || QM_IsSourceSV(_enInputSourceType[eWindow]) || QM_IsSourceAV(_enInputSourceType[eWindow])))
        {
            u8RWmappingNum = 0x01;
        }
        else
        {
            u8RWmappingNum = _u8RWmappingNum[eWindow];
        }
#endif

#if PQ_UC_CTL
#if(PQ_GRULE_GAME_MODE_ENABLE == 0)
        MDrv_PQ_GetCurrentUCFeature( eWindow,
                                    &_u16TabIdx_MADi_Gamemode[eWindow],
                                    &_u16TabIdx_MADi_Mot_Gamemode[eWindow],
                                    &_u16TabIdx_MADi_DFK_Gamemode[eWindow],
                                    &_u16TabIdx_MADi_SST_Gamemode[eWindow],
                                    &_u16TabIdx_MADi_EODiW_Gamemode[eWindow],
                                    &_u16TabIdx_UCNR_Gamemode[eWindow],
                                    &_u16TabIdx_UCDi_Gamemode[eWindow],
                                    &_u16TabIdx_UCCTL_Gamemode[eWindow],
                                    &_u16TabIdx_SwDriver_Gamemode[eWindow],
                                    &_u16TabIdx_PostCCS_Gamemode[eWindow],
                                    &_u16TabIdx_DHD_Gamemode[eWindow]
                                    );
 #endif
        MDrv_PQ_DisableUCFeature(eWindow);
#endif
    }
    else
    {
#if (PQ_GRULE_GAME_MODE_ENABLE == 0)
        // Restore original value
        u8RWmappingNum = _u8RWmappingNum[eWindow];
#endif

#if PQ_UC_CTL
#if(PQ_GRULE_GAME_MODE_ENABLE == 1)
        MDrv_PQ_EnableUCFeature_U2(pInstance, eWindow);
#else
        MDrv_PQ_SetUCFeature(eWindow,
                             _u16TabIdx_MADi_Gamemode[eWindow],
                             _u16TabIdx_MADi_Mot_Gamemode[eWindow],
                             _u16TabIdx_MADi_DFK_Gamemode[eWindow],
                             _u16TabIdx_MADi_SST_Gamemode[eWindow],
                             _u16TabIdx_MADi_EODiW_Gamemode[eWindow],
                             _u16TabIdx_UCNR_Gamemode[eWindow],
                             _u16TabIdx_UCDi_Gamemode[eWindow],
                             _u16TabIdx_UCCTL_Gamemode[eWindow],
                             _u16TabIdx_SwDriver_Gamemode[eWindow],
                             _u16TabIdx_PostCCS_Gamemode[eWindow],
                             _u16TabIdx_DHD_Gamemode[eWindow]
                             );
#endif
#endif
    }

#if (PQ_GRULE_GAME_MODE_ENABLE == 0)
    MApi_XC_SetWRBankMappingNum(u8RWmappingNum,eWindow);
#endif
    return TRUE;
}
#endif

MS_BOOL SYMBOL_WEAK MDrv_PQ_SetGameMode(PQ_WIN eWindow, MS_BOOL bEnable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SetGameMode PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bEnable = bEnable;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_GAMEMODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}


MS_BOOL MDrv_PQ_GetGameMode_Status_U2(void* pInstance,PQ_WIN eWindow)
{
    if (eWindow >= PQ_MAX_WINDOW)
        return FALSE;
    else
        return _bGameModeOn[eWindow];
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetGameMode_Status(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetGameMode_Status PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_GAMEMODE_STATUS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_SetBypassMode_U2(void* pInstance,PQ_WIN eWindow, MS_BOOL bEnable)
{
    if (eWindow >= PQ_MAX_WINDOW)
        return FALSE;

    _bBypassModeOn[eWindow] = bEnable;
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_SetBypassMode(PQ_WIN eWindow, MS_BOOL bEnable)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SetBypassMode PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bEnable = bEnable;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_BYPASS_MODE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_GetBypassModeStatus_U2(void* pInstance,PQ_WIN eWindow)
{
    if (eWindow >= PQ_MAX_WINDOW)
        return FALSE;
    else
        return _bBypassModeOn[eWindow];
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_GetBypassModeStatus(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetBypassModeStatus PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_BYPASS_MODE_STATUS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_GetVersion_U2(void* pInstance,PQ_WIN eWindow, char* pu8Version)
{
    if(gbPQBinEnable == 1)
    {
        if(PQ_MAIN_WINDOW == eWindow)
        {
            strncpy(pu8Version, stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u8Version, PQ_BIN_VERSION_LEN);
        }
        else
        {
            strncpy(pu8Version, stPQBinHeaderInfo[PQ_BIN_STD_SUB].u8Version, PQ_BIN_VERSION_LEN);
        }

        return TRUE;
    }
    else
    {
        return FALSE;

    }
}

char* SYMBOL_WEAK MDrv_PQ_GetVersion(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetVersion PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u8Ret = gu8Version;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_VERSION, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return NULL;
    }
    else
    {
        return PQArgs.u8Ret;
    }
}
char* MDrv_PQ_Cus_GetVersion_U2(void *pInstance,PQ_WIN eWindow)
{
    if(_bCustomerMainPQEn == 1)
    {
        char* Cus_PQ_Version = NULL;

        if(PQ_MAIN_WINDOW == eWindow)
        {
            Cus_PQ_Version = stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN].u8Version;
        }
        else
        {
            if(_bCustomerSubPQEn == 1)
            {
                Cus_PQ_Version = stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB].u8Version;
            }
        }
        return Cus_PQ_Version;
    }
    else
    {
        return NULL;
    }
}
char* SYMBOL_WEAK MDrv_PQ_Cus_GetVersion(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Cus_GetVersion PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u8Ret = NULL;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_CUS_GET_VERSION, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return NULL;
    }
    else
    {
        return PQArgs.u8Ret;
    }
}


static MS_BOOL _MDrv_PQ_SetGRuleStatus(void *pInstance,PQ_WIN eWindow, MS_U16 u16GruleType, MS_U16 u16PQ_GroupIdx)
{
    PQ_BIN_TYPE enPQBinTypeMain = PQ_BIN_STD_MAIN;
#if (PQ_ENABLE_PIP)
    PQ_BIN_TYPE enPQBinTypeSub = PQ_BIN_STD_SUB;
#endif

    if(gbPQBinEnable)
    {
        MS_U16 i, u16IPIdx = 0, u16TabIdx = 0, u16GroupNum = 0, u16IPNum = 0;

        if(eWindow == PQ_MAIN_WINDOW)
        {
            if(u16GruleType >= stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum)
            {
                if(_bCustomerMainPQEn)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRuleType(%u) is bigger than STD PQ's(%u), so consider it as CUSTOMER PQ's GRule Type\n",
                            u16GruleType, stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum);
                    u16GruleType = u16GruleType - stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum;
                    enPQBinTypeMain = PQ_BIN_CUSTOMER_MAIN;
                    if(u16GruleType >= stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN].u16GRule_RuleNum)
                    {
                        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"CustomerPQ: =ERROR!!!, GruleType =%d, out of %d, =\n", u16GruleType, stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN].u16GRule_RuleNum);
                        return FALSE;
                    }
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"=ERROR!!!, GruleType =%d, out of %d, =\n", u16GruleType, stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum);
                    return FALSE;
                }
            }
            u16GroupNum = MDrv_PQBin_GetGRule_GroupNum(pInstance,u16GruleType, &stPQBinHeaderInfo[enPQBinTypeMain]);
            u16IPNum = MDrv_PQBin_GetGRule_IPNum(pInstance,u16GruleType, &stPQBinHeaderInfo[enPQBinTypeMain]);
        }
#if (PQ_ENABLE_PIP)
        else if(eWindow == PQ_SUB_WINDOW)
        {
            if(_bCustomerSubPQEn)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRuleType(%u) is bigger than STD PQ's(%u), so consider it as CUSTOMER PQ's GRule Type\n",
                        u16GruleType, stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum);
                u16GruleType = u16GruleType - stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum;
                enPQBinTypeSub = PQ_BIN_CUSTOMER_SUB;
                if(u16GruleType >= stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB].u16GRule_RuleNum)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"CustomerPQ: ERROR!!!, GruleType =%d, out of %d, =\n", u16GruleType, stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB].u16GRule_RuleNum);
                    return 0;
                }
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"=ERROR!!!, GruleType =%d, out of %d, =\n", u16GruleType, stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum);
                return FALSE;
            }
            u16GroupNum = MDrv_PQBin_GetGRule_GroupNum(pInstance,u16GruleType, &stPQBinHeaderInfo[enPQBinTypeSub]);
            u16IPNum = MDrv_PQBin_GetGRule_IPNum(pInstance,u16GruleType, &stPQBinHeaderInfo[enPQBinTypeSub]);
        }
#endif

        if ((PQ_BIN_IP_NULL == u16GroupNum) || (PQ_BIN_IP_NULL == u16IPNum))
        {
            return FALSE;
        }

        if(u16PQ_GroupIdx >= u16GroupNum)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"=ERROR!!!, u16PQ_GroupIdx:%d, out of range:%d=\n", u16PQ_GroupIdx, u16GroupNum);
            return FALSE;
        }

        if(((_bCustomerMainPQEn) && (enPQBinTypeMain == PQ_BIN_CUSTOMER_MAIN))
#if (PQ_ENABLE_PIP)
                || ((_bCustomerSubPQEn) && (enPQBinTypeSub == PQ_BIN_CUSTOMER_SUB))
#endif
          )
        {
            for(i = 0; i < u16IPNum; i++)
            {
                u16IPIdx = MDrv_PQ_GetCustomerGRule_IPIndex(eWindow, u16GruleType, i);

                if(u16PQ_GroupIdx == u16GroupNum)  // see it as default
                {
                    u16TabIdx = MDrv_PQ_GetCustomerTableIndex(eWindow, u16TabIdx);
                }
                else
                {
                    u16TabIdx = MDrv_PQ_GetGRule_CustomerTableIndex(eWindow, u16GruleType, u16PQ_GroupIdx, i);
                }
                PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[DC]SRC: %u, DC: %u, DCIPIdx:%u, IPIdx:%u, u16TabIdx:%u\n",
                                   _u16PQSrcType[eWindow], u16PQ_GroupIdx, i, u16IPIdx, u16TabIdx);
                MDrv_PQ_LoadCustomerTable(eWindow, u16TabIdx, u16IPIdx);
            }
        }
        else
        {
            for(i = 0; i < u16IPNum; i++)
            {
                u16IPIdx = MDrv_PQ_GetGRule_IPIndex_U2(pInstance, eWindow, u16GruleType, i);

                if(u16PQ_GroupIdx == u16GroupNum)  // see it as default
                    u16TabIdx = MDrv_PQ_GetTableIndex_U2(pInstance,eWindow, u16TabIdx);
                else
                    u16TabIdx = MDrv_PQ_GetGRule_TableIndex(eWindow, u16GruleType, u16PQ_GroupIdx, i);

                PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[DC]SRC: %u, DC: %u, DCIPIdx:%u, IPIdx:%u, u16TabIdx:%u\n",
                               _u16PQSrcType[eWindow], u16PQ_GroupIdx, i, u16IPIdx, u16TabIdx);
                MDrv_PQ_LoadTable_U2(pInstance,eWindow, u16TabIdx, u16IPIdx);
            }
        }

    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_GRULE_DBG;
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.pValue= &u16PQ_GroupIdx;
    stPQSetVRInfo.u16RuleIndex= u16GruleType;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }
    return TRUE;
    }
    else
    {
       return FALSE;
    }
}
MS_BOOL MDrv_PQ_SetBwdLevel_U2(void* pInstance, PQ_WIN eWindow, PQ_BWD_TYPE enPQBWDType)
{
#if(PQ_GRULE_DYNAMIC_SCBW_ENABLE == 1)
    if(eWindow > PQ_MAX_WINDOW)
    {
        return FALSE;
    }
    MS_BOOL bIsChangeLevel = (_eBWDType[eWindow] < enPQBWDType) ? TRUE:FALSE;
    if(bIsChangeLevel)
    {
        enPQBWDType = _eBWDType[eWindow];
    }
    MS_U16 u16Index = 0, u16IPIdx = 0, u16TabIdx = 0,u16PQ_TableIdx=0;
    ST_PQ_IPInfo stGRuleIPInfo;
    ST_PQ_IPInfo stUpdatedGRuleIPInfo;
    memset(&stGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));
    memset(&stUpdatedGRuleIPInfo, 0, sizeof(ST_PQ_IPInfo));
    if(enPQBWDType == PQ_BWD_OFF)
    {
        stGRuleIPInfo.u16GruleLevel = PQ_GRule_DYNAMIC_SCBW_Off_Main;
    }
    else if(enPQBWDType == PQ_BWD_LOW)
    {
        stGRuleIPInfo.u16GruleLevel = PQ_GRule_DYNAMIC_SCBW_Low_Main;
    }
    else if(enPQBWDType == PQ_BWD_MID)
    {
        stGRuleIPInfo.u16GruleLevel = PQ_GRule_DYNAMIC_SCBW_Middle_Main;
    }
    else if(enPQBWDType == PQ_BWD_HIGH)
    {
        stGRuleIPInfo.u16GruleLevel = PQ_GRule_DYNAMIC_SCBW_High_Main;
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"No such Level type %u\n",enPQBWDType);
        return FALSE;
    }
    stGRuleIPInfo.u16IPIdx= PQ_GRule_DYNAMIC_SCBW_Main;
    stGRuleIPInfo.u16GruleMaxIPNum = PQ_GRULE_DYNAMIC_SCBW_IP_NUM_Main;
    stGRuleIPInfo.u16GruleLevelMainNum = PQ_GRULE_DYNAMIC_SCBW_NUM_Main;
    stGRuleIPInfo.u16GruleLevelNum = PQ_GRULE_DYNAMIC_SCBW_LVL_NUM_Main;
    stGRuleIPInfo.enPQEngineType = PQ_XC0_STD;
    MHal_PQ_RemappingGruleIP(pInstance, stGRuleIPInfo, &stUpdatedGRuleIPInfo, (eWindow == PQ_MAIN_WINDOW)?TRUE:FALSE);

    if(MHal_PQ_Support_GroupIPBIN())
    {
        if(stUpdatedGRuleIPInfo.enPQEngineType == PQ_XC0_STD)
        {
            MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,stUpdatedGRuleIPInfo.u16IPIdx,PQ_BIN_STD_MAIN ,&stUpdatedGRuleIPInfo.u16GruleLevelMainNum, &stUpdatedGRuleIPInfo.u16GruleMaxIPNum ,&stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        }
        else
        {
            MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,stUpdatedGRuleIPInfo.u16IPIdx,PQ_BIN_EXT_MAIN ,&stUpdatedGRuleIPInfo.u16GruleLevelMainNum, &stUpdatedGRuleIPInfo.u16GruleMaxIPNum ,&stPQBinHeaderInfo[PQ_BIN_EXT_MAIN]);
        }
    }
    u16PQ_TableIdx = MDrv_PQ_GetGRule_LevelIndex_Ex_U2(pInstance, eWindow, stUpdatedGRuleIPInfo.u16IPIdx, stUpdatedGRuleIPInfo.u16GruleLevel, stUpdatedGRuleIPInfo.enPQEngineType);
    if(u16PQ_TableIdx > stUpdatedGRuleIPInfo.u16GruleLevelMainNum)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRULE_INDEX_OVERFLOW_ERROR Level Index = %u, Max Level Index = %u",u16PQ_TableIdx,(stUpdatedGRuleIPInfo.u16GruleLevelMainNum-1));
        PQ_ASSERT(0);
        return FALSE;
    }
    for(u16Index = 0; u16Index < stUpdatedGRuleIPInfo.u16GruleMaxIPNum; u16Index++)
    {
        u16IPIdx = MDrv_PQ_GetGRule_IPIndex_Ex_U2(pInstance, eWindow, stUpdatedGRuleIPInfo.u16IPIdx, u16Index, stUpdatedGRuleIPInfo.enPQEngineType);
        if(u16PQ_TableIdx == stUpdatedGRuleIPInfo.u16GruleLevelMainNum)  // see it as default
        {
            u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance,eWindow, u16IPIdx, stUpdatedGRuleIPInfo.enPQEngineType);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetGRule_TableIndex_Ex(eWindow, stUpdatedGRuleIPInfo.u16IPIdx, u16PQ_TableIdx, u16Index, stUpdatedGRuleIPInfo.enPQEngineType);
        }
        MDrv_PQ_LoadTable_Ex_U2(pInstance, eWindow, u16TabIdx, u16IPIdx, stUpdatedGRuleIPInfo.enPQEngineType);
    }
    return TRUE;
#else
    UNUSED(eWindow);
    UNUSED(enPQBWDType);
    return FALSE;
#endif
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_SetBwdLevel(PQ_WIN eWindow, PQ_BWD_TYPE enPQBWDType)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SetBwdLevel PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.enPQBWDType = enPQBWDType;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_BWD_LEVEL, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_SetGRuleStatus_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16GruleType, MS_U16 u16GruleStatus)
{
    MS_U16 u16PQ_CurLvlIdx = 0;
    u16PQ_CurLvlIdx = MDrv_PQ_GetGRule_LevelIndex_U2(pInstance, eWindow, u16GruleType, u16GruleStatus);

    return _MDrv_PQ_SetGRuleStatus(pInstance,eWindow, u16GruleType, u16PQ_CurLvlIdx);
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_SetGRuleStatus(PQ_WIN eWindow, MS_U16 u16GruleType, MS_U16 u16GruleStatus)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SetGRuleStatus PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GruleType = u16GruleType;
    PQArgs.u16GruleStatus = u16GruleStatus;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_GRULE_STATUS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_U16 MDrv_PQ_GetGRule_LvlNum_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16GRuleType)
{
    MS_U16 u16Ret = 0;

    PQ_BIN_TYPE enPQBinTypeMain = PQ_BIN_STD_MAIN;
#if (PQ_ENABLE_PIP)
    PQ_BIN_TYPE enPQBinTypeSub = PQ_BIN_STD_SUB;
#endif

    if(gbPQBinEnable)
    {
        MS_U16 u16PanelIdx = 0;
        if(eWindow == PQ_MAIN_WINDOW)
        {
            if(u16GRuleType >= stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum)
            {
                if(_bCustomerMainPQEn)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"GRuleType(%u) is bigger than STD PQ's(%u), so consider it as CUSTOMER PQ's GRule Type\n",
                                u16GRuleType, stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum);
                    u16GRuleType = u16GRuleType - stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum;
                    enPQBinTypeMain = PQ_BIN_CUSTOMER_MAIN;
                    u16PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    if(u16GRuleType >= stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN].u16GRule_RuleNum)
                    {
                        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"CustomerPQ: =ERROR!!!, GRuleType =%d, out of %d, =\n", u16GRuleType, stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN].u16GRule_RuleNum);
                        return 0;
                    }
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"=ERROR!!!, GRuleType =%d, out of %d, =\n", u16GRuleType, stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum);
                    return 0;
                }
            }
            else
            {
                u16PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
            }

            u16Ret = MDrv_PQBin_GetGRule_LvlNum(pInstance,u16PanelIdx, u16GRuleType, &stPQBinHeaderInfo[enPQBinTypeMain]);
        }
#if (PQ_ENABLE_PIP)
        else if(eWindow == PQ_SUB_WINDOW)
        {
            if(u16GRuleType >= stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum)
            {
                if(_bCustomerSubPQEn)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"GRuleType(%u) is bigger than STD PQ's(%u), so consider it as CUSTOMER PQ's GRule Type\n",
                                u16GRuleType, stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum);
                    u16GRuleType = u16GRuleType - stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum;
                    enPQBinTypeSub = PQ_BIN_CUSTOMER_SUB;
                    u16PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    if(u16GRuleType >= stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB].u16GRule_RuleNum)
                    {
                        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"CustomerPQ: =ERROR!!!, GRuleType =%d, out of %d, =\n", u16GRuleType, stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB].u16GRule_RuleNum);
                        return 0;
                    }
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"=ERROR!!!, GRuleType =%d, out of %d, =\n", u16GRuleType, stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum);
                    return 0;
                }
            }
            else
            {
                u16PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
            }

            u16Ret = MDrv_PQBin_GetGRule_LvlNum(pInstance,u16PanelIdx, u16GRuleType, &stPQBinHeaderInfo[enPQBinTypeSub]);
        }
#endif
        else
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"=Error, =Not support case \n");
        }

        if (PQ_BIN_IP_NULL == u16Ret)
        {
            return 0;
        }
    }
    return u16Ret;
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetGRule_LvlNum(PQ_WIN eWindow, MS_U16 u16GRuleType)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetGRule_LvlNum PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GRuleType = u16GRuleType;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_GRULE_LV1NUM, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetGRule_LvlNum_Ex_U2(void *pInstance,PQ_WIN eWindow, MS_U16 u16GRuleType, PQ_ENGINE_TYPE ePQEngineType)
{
    MS_U16 u16Ret = PQ_BIN_IP_NULL;
    MS_U8 u8PanelIdx = 0;
    PQ_BIN_TYPE enPQBinType = MAX_PQ_BIN_NUM;

    if (eWindow == PQ_MAIN_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_STD_MAIN;
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_EXT_MAIN;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerMainPQEn)
                {
                    u8PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    enPQBinType = PQ_BIN_CUSTOMER_MAIN;
                }
                break;
            case PQ_XC0_UFSC:
                if (_bUFSCPQEn)
                {
                    u8PanelIdx = PQ_BIN_UFSC_PANEL_INDEX;
                    enPQBinType = PQ_BIN_UFSC;
                }
                break;
            default:
                break;
        }
    }
#if (PQ_ENABLE_PIP)
    else if (eWindow == PQ_SUB_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_STD_SUB;
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_EXT_SUB;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerSubPQEn)
                {
                    u8PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    enPQBinType = PQ_BIN_CUSTOMER_SUB;
                }
                break;
            case PQ_XC0_UFSC:
                break;
            default:
                break;
        }

    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }

    if (enPQBinType != MAX_PQ_BIN_NUM)
    {
        if(u16GRuleType >= stPQBinHeaderInfo[enPQBinType].u16GRule_RuleNum)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"[PQ ERROR]\nenPQBinType = %d, u16GruleType = %d out of %d\n", enPQBinType, u16GRuleType, stPQBinHeaderInfo[enPQBinType].u16GRule_RuleNum);
        }
        else
        {
            u16Ret = MDrv_PQBin_GetGRule_LvlNum(pInstance,u8PanelIdx, u16GRuleType, &stPQBinHeaderInfo[enPQBinType]);
        }
    }

    return u16Ret;
}

MS_U16 SYMBOL_WEAK MDrv_PQ_GetGRule_LvlNum_Ex(PQ_WIN eWindow, MS_U16 u16GRuleType, PQ_ENGINE_TYPE ePQEngineType)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetGRule_LvlNum_Ex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GRuleType = u16GRuleType;
    PQArgs.ePQEngineType = ePQEngineType;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_GRULE_LV1NUM_EX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetGRule_GRuleNum_U2(void* pInstance,PQ_WIN eWindow)
{
    MS_U16 u16Ret = 0;

    if(gbPQBinEnable)
    {
        if(eWindow == PQ_MAIN_WINDOW)
        {
            u16Ret = stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum;
        }
#if (PQ_ENABLE_PIP)
        else if(eWindow == PQ_SUB_WINDOW)
        {
            u16Ret = stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum;
        }
        else
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"=Error, =Not support case \n");
        }
#endif
    }

    return u16Ret;
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetGRule_GRuleNum(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetGRule_GRuleNum PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_GRULE_GRULENUM, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetGRule_LevelIndex_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16GRuleLevelIndex)
{
    MS_U16 u16Ret = 0;
    PQ_BIN_TYPE enPQBinTypeMain = PQ_BIN_STD_MAIN;
#if (PQ_ENABLE_PIP)
    PQ_BIN_TYPE enPQBinTypeSub = PQ_BIN_STD_SUB;
#endif
    MS_U16 u16PanelIdx = 0;
    if(eWindow == PQ_MAIN_WINDOW)
    {

        if(gbPQBinEnable)
        {

            if(u16GRuleType >= stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum)
            {
                if(_bCustomerMainPQEn)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"(%s, %d)GRuleType(%u) is bigger than STD PQ's(%u), so consider it as CUSTOMER PQ's GRule Type\n",
                                u16GRuleType, stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum);
                    u16GRuleType = u16GRuleType - stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum;
                    enPQBinTypeMain = PQ_BIN_CUSTOMER_MAIN;
                    u16PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    if(u16GRuleType >= stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN].u16GRule_RuleNum)
                    {
                        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"CustomerPQ: =ERROR!!!, =%d, out of %d, =\n", u16GRuleType, stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN].u16GRule_RuleNum);
                        return 0;
                    }
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"=ERROR!!!, =%d, out of %d, =\n", u16GRuleType, stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16GRule_RuleNum);
                    return 0;
                }
            }
            else
            {
                u16PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
            }

            u16Ret = (MS_U8) MDrv_PQBin_GetGRule_LevelIndex(pInstance,u16PanelIdx,
                     u16GRuleType,
                     u16GRuleLevelIndex,
                     &stPQBinHeaderInfo[enPQBinTypeMain]);
        }
        else
        {
            u16Ret = MDrv_PQ_GetGRule_LevelIndex_(MAIN,pInstance, (MS_U8)u16GRuleType, (MS_U8)u16GRuleLevelIndex);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            if(u16GRuleType >= stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum)
            {
                if(_bCustomerSubPQEn)
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"GRuleType(%u) is bigger than STD PQ's(%u), so consider it as CUSTOMER PQ's GRule Type\n",
                                u16GRuleType, stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum);
                    u16GRuleType = u16GRuleType - stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum;
                    enPQBinTypeSub = PQ_BIN_CUSTOMER_SUB;
                    u16PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    if(u16GRuleType >= stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB].u16GRule_RuleNum)
                    {
                        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"CustomerPQ: =ERROR!!!, =%d, out of %d, =\n", u16GRuleType, stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB].u16GRule_RuleNum);
                        return 0;
                    }
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"=ERROR!!!, =%d, out of %d, =\n", u16GRuleType, stPQBinHeaderInfo[PQ_BIN_STD_SUB].u16GRule_RuleNum);
                    return 0;
                }
            }
            else
            {
                u16PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
            }

            u16Ret = (MS_U8) MDrv_PQBin_GetGRule_LevelIndex(pInstance,u16PanelIdx,
                     u16GRuleType,
                     u16GRuleLevelIndex,
                     &stPQBinHeaderInfo[enPQBinTypeSub]);
        }
        else
        {
            u16Ret = MDrv_PQ_GetGRule_LevelIndex_(SUB,pInstance, (MS_U8)u16GRuleType, (MS_U8)u16GRuleLevelIndex);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }
    return u16Ret;
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetGRule_LevelIndex(PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16GRuleLevelIndex)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetGRule_LevelIndex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GRuleType = u16GRuleType;
    PQArgs.u16GRuleLevelIndex = u16GRuleLevelIndex;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_GRULE_LEVELINDEX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetGRule_LevelIndex_Ex_U2(void *pInstance,PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16GRuleLevelIndex, PQ_ENGINE_TYPE ePQEngineType)
{
    MS_U16 u16Ret = PQ_BIN_IP_NULL;
    MS_U8 u8PanelIdx = 0;
    PQ_BIN_TYPE enPQBinType = MAX_PQ_BIN_NUM;

    if (eWindow == PQ_MAIN_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_STD_MAIN;
                }
                else
                {
                    u16Ret = MDrv_PQ_GetGRule_LevelIndex_(MAIN, pInstance,(MS_U8)u16GRuleType, (MS_U8)u16GRuleLevelIndex);
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_EXT_MAIN;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerMainPQEn)
                {
                    u8PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    enPQBinType = PQ_BIN_CUSTOMER_MAIN;
                }
                break;
            case PQ_XC0_UFSC:
                if (_bUFSCPQEn)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx_UFSC(pInstance);
                    enPQBinType = PQ_BIN_UFSC;
                }
                break;
            case PQ_XC0_TMO:
                if (_bTMOPQEn)
                {
                    u8PanelIdx = MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO);
                    enPQBinType = PQ_BIN_TMO;
                }
                break;
            default:
                break;
        }
    }
#if (PQ_ENABLE_PIP)
    else if (eWindow == PQ_SUB_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_STD_SUB;
                }
                else
                {
                    u16Ret = MDrv_PQ_GetGRule_LevelIndex_(SUB, pInstance,(MS_U8)u16GRuleType, (MS_U8)u16GRuleLevelIndex);
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    u8PanelIdx = MDrv_PQBin_GetPanelIdx(pInstance);
                    enPQBinType = PQ_BIN_EXT_SUB;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerSubPQEn)
                {
                    u8PanelIdx = PQ_BIN_CUSTOMER_PANEL_INDEX;
                    enPQBinType = PQ_BIN_CUSTOMER_SUB;
                }
                break;
            case PQ_XC0_UFSC:
                break;
            default:
                break;
        }

    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }

    if (enPQBinType != MAX_PQ_BIN_NUM)
    {
        if(u16GRuleType >= stPQBinHeaderInfo[enPQBinType].u16GRule_RuleNum)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"enPQBinType = %d, u16GruleType = %d out of %d\n", enPQBinType, u16GRuleType, stPQBinHeaderInfo[enPQBinType].u16GRule_RuleNum);
        }
        else
        {
            u16Ret = MDrv_PQBin_GetGRule_LevelIndex(pInstance,
                                                    u8PanelIdx,
                                                    u16GRuleType,
                                                    u16GRuleLevelIndex,
                                                    &stPQBinHeaderInfo[enPQBinType]);
        }
    }

    return u16Ret;
}

MS_U16 SYMBOL_WEAK MDrv_PQ_GetGRule_LevelIndex_Ex(PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16GRuleLevelIndex, PQ_ENGINE_TYPE ePQEngineType)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetGRule_LevelIndex_Ex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GRuleType = u16GRuleType;
    PQArgs.u16GRuleLevelIndex = u16GRuleLevelIndex;
    PQArgs.ePQEngineType = ePQEngineType;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_GRULE_LEVELINDEX_EX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetGRule_IPIndex_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16GRuleIPIndex)
{
    MS_U16 u16Ret = 0;
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            u16Ret = MDrv_PQBin_GetGRule_IPIndex(pInstance,u16GRuleType,
                                                 u16GRuleIPIndex,
                                                 &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        }
        else
        {
            u16Ret = MDrv_PQ_GetGRule_IPIndex_(MAIN,pInstance, (MS_U8)u16GRuleType, (MS_U8)u16GRuleIPIndex);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            u16Ret = MDrv_PQBin_GetGRule_IPIndex(pInstance,u16GRuleType,
                                                 u16GRuleIPIndex,
                                                 &stPQBinHeaderInfo[PQ_BIN_STD_SUB]);
        }
        else
        {
            u16Ret = MDrv_PQ_GetGRule_IPIndex_(SUB,pInstance, (MS_U8)u16GRuleType, (MS_U8)u16GRuleIPIndex);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }

    return u16Ret;
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetGRule_IPIndex(PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16GRuleIPIndex)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetGRule_IPIndex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GRuleType = u16GRuleType;
    PQArgs.u16GRuleIPIndex = u16GRuleIPIndex;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_GRULE_IPINDEX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetGRule_IPIndex_Ex_U2(void *pInstance,PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16GRuleIPIndex, PQ_ENGINE_TYPE ePQEngineType)
{
    MS_U16 u16Ret = PQ_BIN_IP_NULL;
    PQ_BIN_TYPE enPQBinType = MAX_PQ_BIN_NUM;

    if (eWindow == PQ_MAIN_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                if (gbPQBinEnable)
                {
                    enPQBinType = PQ_BIN_STD_MAIN;
    }
    else
    {
                    u16Ret = MDrv_PQ_GetGRule_IPIndex_(MAIN,pInstance, (MS_U8)u16GRuleType, (MS_U8)u16GRuleIPIndex);
    }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
    {
                    enPQBinType = PQ_BIN_EXT_MAIN;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerMainPQEn)
            {
                    enPQBinType = PQ_BIN_CUSTOMER_MAIN;
            }
            break;
            case PQ_XC0_UFSC:
                if (_bUFSCPQEn)
            {
                    enPQBinType = PQ_BIN_UFSC;
            }
            break;
            case PQ_XC0_TMO:
                if (_bTMOPQEn)
                {
                    enPQBinType = PQ_BIN_TMO;
                }
                break;
            default:
                break;
        }
    }
#if (PQ_ENABLE_PIP)
    else if (eWindow == PQ_SUB_WINDOW)
            {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                if (gbPQBinEnable)
                {
                    enPQBinType = PQ_BIN_STD_SUB;
                }
                else
                {
                    u16Ret = MDrv_PQ_GetGRule_IPIndex_(SUB,pInstance, (MS_U8)u16GRuleType, (MS_U8)u16GRuleIPIndex);
            }
            break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
            {
                    enPQBinType = PQ_BIN_EXT_SUB;
            }
            break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerSubPQEn)
            {
                    enPQBinType = PQ_BIN_CUSTOMER_SUB;
            }
            break;
            case PQ_XC0_UFSC:
                break;
        default:
            break;
    }

}
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }

    if (enPQBinType != MAX_PQ_BIN_NUM)
    {
        u16Ret = MDrv_PQBin_GetGRule_IPIndex(pInstance,
                                            u16GRuleType,
                                            u16GRuleIPIndex,
                                            &stPQBinHeaderInfo[enPQBinType]);
    }

    return u16Ret;
}

MS_U16 SYMBOL_WEAK MDrv_PQ_GetGRule_IPIndex_Ex(PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16GRuleIPIndex, PQ_ENGINE_TYPE ePQEngineType)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetGRule_IPIndex_Ex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GRuleType = u16GRuleType;
    PQArgs.u16GRuleIPIndex = u16GRuleIPIndex;
    PQArgs.ePQEngineType = ePQEngineType;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_GRULE_IPINDEX_EX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetGRule_TableIndex_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16PQ_NRIdx, MS_U16 u16GRuleIPIndex)
{
    MS_U16 u16Ret = 0;
    if(eWindow == PQ_MAIN_WINDOW)
    {
        if(gbPQBinEnable)
        {
            u16Ret = (MS_U8) MDrv_PQBin_GetGRule_TableIndex(pInstance,u16GRuleType,
                     _u16PQSrcType[eWindow],
                     u16PQ_NRIdx,
                     u16GRuleIPIndex,
                     &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        }
        else
        {
            u16Ret = MDrv_PQ_GetGRule_TableIndex_(MAIN,pInstance, (MS_U8)u16GRuleType, _u16PQSrcType[eWindow], (MS_U8)u16PQ_NRIdx, (MS_U8)u16GRuleIPIndex);
        }
    }
#if (PQ_ENABLE_PIP)
    else if(eWindow == PQ_SUB_WINDOW)
    {
        if(gbPQBinEnable)
        {
            u16Ret = (MS_U8) MDrv_PQBin_GetGRule_TableIndex(pInstance,u16GRuleType,
                     _u16PQSrcType[eWindow],
                     u16PQ_NRIdx,
                     u16GRuleIPIndex,
                     &stPQBinHeaderInfo[PQ_BIN_STD_SUB]);
        }
        else
        {
            u16Ret = MDrv_PQ_GetGRule_TableIndex_(SUB,pInstance, (MS_U8)u16GRuleType, _u16PQSrcType[eWindow], (MS_U8)u16PQ_NRIdx, (MS_U8)u16GRuleIPIndex);
        }
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }

    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_GRULE_DBG;
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.pValue= &u16PQ_NRIdx;
    stPQSetVRInfo.u16RuleIndex= u16GRuleType;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }

    return u16Ret;
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetGRule_TableIndex(PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16PQ_NRIdx, MS_U16 u16GRuleIPIndex)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
    }

    stPQ_GetGRule_TableIndex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GRuleType = u16GRuleType;
    PQArgs.u16PQ_NRIdx = u16PQ_NRIdx;
    PQArgs.u16GRuleIPIndex = u16GRuleIPIndex;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_GRULE_TABLEINDEX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetGRule_TableIndex_Ex_U2(void *pInstance,PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16PQ_NRIdx, MS_U16 u16GRuleIPIndex, PQ_ENGINE_TYPE ePQEngineType)
{
    MS_U16 u16Ret = PQ_BIN_IP_NULL, u16SrcIdx = 0;
    PQ_BIN_TYPE enPQBinType = MAX_PQ_BIN_NUM;

    if (eWindow == PQ_MAIN_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                u16SrcIdx = _u16PQSrcType[eWindow];
                if (gbPQBinEnable)
                {
                    enPQBinType = PQ_BIN_STD_MAIN;
                }
                else
                {
                    u16Ret = MDrv_PQ_GetGRule_TableIndex_(MAIN, pInstance,(MS_U8)u16GRuleType, u16SrcIdx, (MS_U8)u16PQ_NRIdx, (MS_U8)u16GRuleIPIndex);
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    u16SrcIdx = _u16PQSrcType[eWindow];
                    enPQBinType = PQ_BIN_EXT_MAIN;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerMainPQEn)
                {
                    u16SrcIdx = _u16PQSrcType[eWindow];
                    enPQBinType = PQ_BIN_CUSTOMER_MAIN;
                }
                break;
            case PQ_XC0_UFSC:
                if (_bUFSCPQEn)
                {
                    u16SrcIdx = _u16PQSrcType_UFSC[eWindow];
                    enPQBinType = PQ_BIN_UFSC;
                }
                break;
            case PQ_XC0_TMO:
                if (_bTMOPQEn)
                {
                    u16SrcIdx = _u16PQSrcType_TMO[eWindow];
                    enPQBinType = PQ_BIN_TMO;
                }
                break;
            default:
                break;
        }
    }
#if (PQ_ENABLE_PIP)
    else if (eWindow == PQ_SUB_WINDOW)
    {
        switch (ePQEngineType)
        {
            case PQ_XC0_STD:
                u16SrcIdx = _u16PQSrcType[eWindow];
                if (gbPQBinEnable)
                {
                    enPQBinType = PQ_BIN_STD_SUB;
                }
                else
                {
                    u16Ret = MDrv_PQ_GetGRule_TableIndex_(SUB,pInstance, (MS_U8)u16GRuleType, u16SrcIdx, (MS_U8)u16PQ_NRIdx, (MS_U8)u16GRuleIPIndex);
                }
                break;
            case PQ_XC0_EXT:
                if (gbPQBinEnable)
                {
                    u16SrcIdx = _u16PQSrcType[eWindow];
                    enPQBinType = PQ_BIN_EXT_SUB;
                }
                break;
            case PQ_XC0_CUSTOMER:
                if (_bCustomerSubPQEn)
                {
                    u16SrcIdx = _u16PQSrcType[eWindow];
                    enPQBinType = PQ_BIN_CUSTOMER_SUB;
                }
                break;
            case PQ_XC0_UFSC:
                break;
            default:
                break;
        }

    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
        PQ_ASSERT(0);
    }

    if (enPQBinType != MAX_PQ_BIN_NUM)
    {
        u16Ret = MDrv_PQBin_GetGRule_TableIndex(pInstance,
                                                u16GRuleType,
                                                u16SrcIdx,
                                                u16PQ_NRIdx,
                                                u16GRuleIPIndex,
                                                &stPQBinHeaderInfo[enPQBinType]);
    }
    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_GRULE_DBG;
    stPQSetVRInfo.enWindow= eWindow;
    stPQSetVRInfo.pValue= &u16PQ_NRIdx;
    stPQSetVRInfo.u16RuleIndex= u16GRuleType;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }
    return u16Ret;
}

MS_U16 SYMBOL_WEAK MDrv_PQ_GetGRule_TableIndex_Ex(PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16PQ_NRIdx, MS_U16 u16GRuleIPIndex, PQ_ENGINE_TYPE ePQEngineType)
{
    if (pu32PQInst == NULL)
    {
    if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
    }

    stPQ_GetGRule_TableIndex_Ex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GRuleType = u16GRuleType;
    PQArgs.u16PQ_NRIdx = u16PQ_NRIdx;
    PQArgs.u16GRuleIPIndex = u16GRuleIPIndex;
    PQArgs.ePQEngineType = ePQEngineType;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_GRULE_TABLEINDEX_EX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_U16 MDrv_PQ_GetCustomerGRule_IPIndex_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16GRuleIPIndex)
{
    if(_bCustomerMainPQEn || _bCustomerSubPQEn)
    {
        MS_U16 u16Ret = 0;
        if(eWindow == PQ_MAIN_WINDOW)
        {
            if(gbPQBinEnable)
            {
                u16Ret = MDrv_PQBin_GetGRule_IPIndex(pInstance,u16GRuleType,
                        u16GRuleIPIndex,
                        &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN]);
            }
        }
#if (PQ_ENABLE_PIP)
        else if(eWindow == PQ_SUB_WINDOW)
        {
            if(gbPQBinEnable)
            {
                u16Ret = MDrv_PQBin_GetGRule_IPIndex(pInstance,u16GRuleType,
                        u16GRuleIPIndex,
                        &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB]);
            }
        }
#endif
        else
        {
            PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"CUSTOMER PQ Not support case\n");
        }

        return u16Ret;
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"CUSTOMER PQ Not support case\n");
        return 0;
    }
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetCustomerGRule_IPIndex(PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16GRuleIPIndex)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetCustomerGRule_IPIndex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GRuleType = u16GRuleType;
    PQArgs.u16GRuleIPIndex = u16GRuleIPIndex;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_CUSTOMERGRULE_IPINDEX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}


MS_U16 MDrv_PQ_GetGRule_CustomerTableIndex_U2(void* pInstance,PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16PQ_NRIdx, MS_U16 u16GRuleIPIndex)
{
    if(_bCustomerMainPQEn || _bCustomerSubPQEn)
    {
        MS_U16 u16Ret = 0;
        if(eWindow == PQ_MAIN_WINDOW)
        {
            if(gbPQBinEnable)
            {
                u16Ret = (MS_U8) MDrv_PQBin_GetGRule_TableIndex(pInstance,u16GRuleType,
                        _u16PQSrcType[eWindow],
                        u16PQ_NRIdx,
                        u16GRuleIPIndex,
                        &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_MAIN]);
            }
            else
            {
                u16Ret = MDrv_PQ_GetGRule_TableIndex_(MAIN,pInstance, (MS_U8)u16GRuleType, _u16PQSrcType[eWindow], (MS_U8)u16PQ_NRIdx, (MS_U8)u16GRuleIPIndex);
            }
        }
#if (PQ_ENABLE_PIP)
        else if(eWindow == PQ_SUB_WINDOW)
        {
            if(gbPQBinEnable)
            {
                u16Ret = (MS_U8) MDrv_PQBin_GetGRule_TableIndex(pInstance,u16GRuleType,
                        _u16PQSrcType[eWindow],
                        u16PQ_NRIdx,
                        u16GRuleIPIndex,
                        &stPQBinHeaderInfo[PQ_BIN_CUSTOMER_SUB]);
            }
            else
            {
                u16Ret = MDrv_PQ_GetGRule_TableIndex_(SUB,pInstance, (MS_U8)u16GRuleType, _u16PQSrcType[eWindow], (MS_U8)u16PQ_NRIdx, (MS_U8)u16GRuleIPIndex);
            }
        }
#endif
        else
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ASSERT!!!");
            PQ_ASSERT(0);
        }
        ST_PQ_SET_VR_INFO stPQSetVRInfo;
        memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
        stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
        stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
        stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_GRULE_DBG;
        stPQSetVRInfo.enWindow= eWindow;
        stPQSetVRInfo.pValue= &u16PQ_NRIdx;
        stPQSetVRInfo.u16RuleIndex= u16GRuleType;
        if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
        }
        return u16Ret;
    }
    else
    {
        return 0;
    }
}
MS_U16 SYMBOL_WEAK MDrv_PQ_GetGRule_CustomerTableIndex(PQ_WIN eWindow, MS_U16 u16GRuleType, MS_U16 u16PQ_NRIdx, MS_U16 u16GRuleIPIndex)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetGRule_CustomerTableIndex PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16GRuleType = u16GRuleType;
    PQArgs.u16PQ_NRIdx = u16PQ_NRIdx;
    PQArgs.u16GRuleIPIndex = u16GRuleIPIndex;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_CUSTOMERGRULE_TABLEINDEX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_BOOL MDrv_PQ_Set_xvYCC_MatrixCoefficient_U2(void* pInstance,
                                            MS_U64 u64Red_x,   MS_U64 u64Red_y,
                                            MS_U64 u64Green_x, MS_U64 u64Green_y,
                                            MS_U64 u64Blue_x,  MS_U64 u64Blue_y,
                                            MS_U64 u64White_x, MS_U64 u64White_y,
                                            PQ_WIN eWindow)
{
    MS_U32 u32idx, s32H_idx,u32V_idx, u32cnt;
    MS_U64 s64Mat[3][ 3] =  { {0},{0},{0}};
    MS_S64 s64sRGBMat[3][3];
    MS_S64 s64RGB3x3[ 3][ 3];
    MS_S32 s32RGB3x3[ 3][ 3];
    MS_U32 __attribute__((__unused__))u32data_invalid[ 9];
    MS_S32 s32fix_value; //format s2.10
    MS_S64 dbUp_value, dbDown_value;
    MS_S64 dbHistogramSum[3];
    MS_S64 dbMaxHistogramSum;

    if (eWindow >= PQ_MAX_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Invalid window type, please check ! \n");
        return FALSE;
    }

    for(u32idx=0; u32idx<9; u32idx++)
    {
        u32data_invalid[ u32idx] = 0;
    }

    // calculation inverse Matrix Mat[3][3]
    // u32data_invalid is used when the inverse of matrix is not existed
    // for (1,1)
    dbUp_value   = (MS_S64)(u64White_y*(-u64Blue_y + u64Green_x*u64Blue_y + u64Green_y - u64Blue_x*u64Green_y) ) ;
    dbDown_value = (MS_S64)(-u64Green_x*u64White_y + u64Blue_x*u64White_y + u64Green_x*u64Blue_y - u64Blue_y*u64White_x + u64Green_y*u64White_x - u64Blue_x*u64Green_y);
    if( dbDown_value ==0)\
    {
        u32data_invalid[ 0] = 1;
    }
    else
    {
        u32data_invalid[ 0] =0;
        s64Mat[ 0][ 0] = do_div(dbUp_value,dbDown_value);
    }

    // for (1,2)
    dbUp_value   = (MS_S64)(u64White_y*(u64Green_x*u64Blue_y -u64Green_x + u64Blue_x - u64Blue_x*u64Green_y)) ;
    dbDown_value = (MS_S64)(-u64Green_x*u64White_y + u64Blue_x*u64White_y + u64Green_x*u64Blue_y - u64Blue_y*u64White_x + u64Green_y*u64White_x - u64Blue_x*u64Green_y);
    if( dbDown_value ==0)
    {
        u32data_invalid[ 1] = 1;
    }
    else
    {
        u32data_invalid[ 1] =0;
        s64Mat[ 0][ 1] = do_div(dbUp_value,dbDown_value);
    }

    // for (1,3)
    dbUp_value   = (MS_S64) ( u64White_y* ( u64Green_x*u64Blue_y - u64Blue_x*u64Green_y) ) ;
    dbDown_value = (MS_S64) ( -u64Green_x*u64White_y + u64Blue_x*u64White_y + u64Green_x*u64Blue_y - u64Blue_y*u64White_x + u64Green_y*u64White_x - u64Blue_x*u64Green_y);
    if( dbDown_value ==0)
    {
        u32data_invalid[ 2] = 1;
    }
    else
    {
        u32data_invalid[ 2] =0;
        s64Mat[ 0][ 2] = do_div(dbUp_value,dbDown_value);
    }

    // for (2,1)
    dbUp_value   = (MS_S64) ( u64White_y* ( u64Red_y - u64Red_y*u64Blue_x - u64Blue_y + u64Red_x*u64Blue_y) ) ;
    dbDown_value = (MS_S64) ( u64Red_y*u64White_x + u64Red_x*u64Blue_y - u64Red_y*u64Blue_x - u64Blue_y*u64White_x + u64Blue_x*u64White_y - u64Red_x*u64White_y) ;
    if( dbDown_value ==0)
    {
        u32data_invalid[ 3] = 1;
    }
    else
    {
        u32data_invalid[ 3] =0;
        s64Mat[ 1][ 0] = do_div(dbUp_value,dbDown_value);
    }

    // for (2,2)
    dbUp_value   = (MS_S64) ( u64White_y* ( u64Red_x*u64Blue_y - u64Red_x + u64Blue_x - u64Red_y*u64Blue_x) ) ;
    dbDown_value = (MS_S64) ( u64Red_y*u64White_x + u64Red_x*u64Blue_y - u64Red_y*u64Blue_x - u64Blue_y*u64White_x + u64Blue_x*u64White_y - u64Red_x*u64White_y) ;
    if( dbDown_value ==0)
    {
        u32data_invalid[ 4] = 1;
    }
    else
    {
        u32data_invalid[ 4] =0;
        s64Mat[ 1][ 1] = do_div(dbUp_value,dbDown_value);
    }

    // for (2,3)
    dbUp_value   = (MS_S64) ( u64White_y* ( u64Red_x*u64Blue_y - u64Red_y*u64Blue_x) ) ;
    dbDown_value = (MS_S64) ( u64Red_y*u64White_x + u64Red_x*u64Blue_y - u64Red_y*u64Blue_x - u64Blue_y*u64White_x + u64Blue_x*u64White_y - u64Red_x*u64White_y) ;
    if( dbDown_value ==0)
    {
        u32data_invalid[ 5] = 1;
    }
    else
    {
        u32data_invalid[ 5] =0;
        s64Mat[ 1][ 2] = do_div(dbUp_value,dbDown_value);
    }

    // for (3,1)
    dbUp_value   = (MS_S64) ( u64White_y* ( u64Red_x*u64Green_y + u64Red_y - u64Green_y -u64Green_x*u64Red_y) ) ;
    dbDown_value = (MS_S64) ( -u64Green_x*u64Red_y + u64Red_y*u64White_x -u64Green_y*u64White_x -u64Red_x*u64White_y + u64Red_x*u64Green_y + u64Green_x*u64White_y) ;
    if( dbDown_value ==0)
    {
        u32data_invalid[ 6] = 1;
    }
    else
    {
        u32data_invalid[ 6] =0;
        s64Mat[ 2][ 0] = do_div(dbUp_value,dbDown_value);
    }

    // for (3,2)
    dbUp_value   = (MS_S64) ( u64White_y* ( -u64Red_x + u64Red_x*u64Green_y + u64Green_x - u64Green_x*u64Red_y) ) ;
    dbDown_value = (MS_S64) ( -u64Green_x*u64Red_y + u64Red_y*u64White_x -u64Green_y*u64White_x -u64Red_x*u64White_y + u64Red_x*u64Green_y + u64Green_x*u64White_y) ;
    if( dbDown_value ==0)
    {
        u32data_invalid[ 7] = 1;
    }
    else
    {
        u32data_invalid[ 7] =0;
        s64Mat[ 2][ 1] = do_div(dbUp_value,dbDown_value);
    }

    // for (3,3)
    dbUp_value   = (MS_S64) ( u64White_y* ( u64Red_x*u64Green_y - u64Green_x*u64Red_y) ) ;
    dbDown_value = (MS_S64) ( -u64Green_x*u64Red_y + u64Red_y*u64White_x -u64Green_y*u64White_x -u64Red_x*u64White_y + u64Red_x*u64Green_y + u64Green_x*u64White_y) ;
    if( dbDown_value ==0)
    {
        u32data_invalid[ 8] = 1;
    }
    else
    {
        u32data_invalid[ 8] =0;
        s64Mat[ 2][ 2] = do_div(dbUp_value,dbDown_value);
    }

    #if 0   // this is used for debugging matrix
    int k = 0, o=0;
    for(k=0;k<3;k++)
    {
        for (o=0;o<3;o++)
        {
            printf("Mat[%d][%d] = %f\n",k,o,flMat[k][o]);
        }
    }
    #endif

    //sRGB Matrix
    s64sRGBMat[0][0]= 412390;//0.412390799;
    s64sRGBMat[0][1]= 357584;//0.357584339;
    s64sRGBMat[0][2]= 180480;//0.180480788;
    s64sRGBMat[1][0]= 212639;//0.212639006;
    s64sRGBMat[1][1]= 715168;//0.715168679;
    s64sRGBMat[1][2]= 72192; //0.072192315;
    s64sRGBMat[2][0]= 19330; //0.019330819;
    s64sRGBMat[2][1]= 119194;//0.11919478;
    s64sRGBMat[2][2]= 950532;//0.950532152;
    //--------------------------------------------------------------------

    // RGB2XYZ * Mat( XYZ2RGB)
    for( u32V_idx =0; u32V_idx<3; u32V_idx++)
    {
        dbHistogramSum[u32V_idx]=0;
        for( s32H_idx=0; s32H_idx<3; s32H_idx++)
        {
            MS_U64 s32Sum=0;
            for( u32cnt=0; u32cnt<3; u32cnt++)
            {
                s32Sum += s64Mat[ u32V_idx][ u32cnt] * s64sRGBMat[ u32cnt][ s32H_idx];
            }

            s64RGB3x3[ u32V_idx][ s32H_idx] = s32Sum;

            //printf("rgb3x3[%ld][%ld] = %f\n",u32V_idx,s32H_idx,flRGB3x3[u32V_idx][s32H_idx]);

            dbHistogramSum[u32V_idx]=dbHistogramSum[u32V_idx]+s32Sum;
        }
    }



    //Normalize
    dbMaxHistogramSum=0;
    for( u32V_idx =0; u32V_idx<3; u32V_idx++)
    {
        if (dbHistogramSum[u32V_idx]>=dbMaxHistogramSum)
        {
            dbMaxHistogramSum=dbHistogramSum[u32V_idx];
        }

    }

    MS_S64 s64_tmp=0;
    //convert to s2.10 format
    for( u32V_idx =0; u32V_idx<3; u32V_idx++)
    {
        for( s32H_idx=0; s32H_idx<3; s32H_idx++)
        {
            s64_tmp = (MS_S64) s64RGB3x3[u32V_idx][s32H_idx];
            do_div(s64_tmp,(dbMaxHistogramSum*ENLARGE_GATE));
            s64RGB3x3[u32V_idx][s32H_idx]=s64_tmp;

            //MS_S64 round_value;
            //if (s64RGB3x3[u32V_idx][s32H_idx]>=0)
            //{
            //    round_value= s64RGB3x3[u32V_idx][s32H_idx] *1024 + 0.5;
            //}
            //else
            //{
            //    round_value= s64RGB3x3[u32V_idx][s32H_idx] *1024 - 0.5;
            //}

            s32fix_value = (MS_S32)(s64RGB3x3[u32V_idx][s32H_idx]);
            MS_U32 abs_fix =(MS_U32) abs(s64RGB3x3[u32V_idx][s32H_idx]);

            if( (abs_fix !=0) && (s32fix_value<0) )
            {
                MS_S32 sbits = 1<<12;
                s32RGB3x3[u32V_idx][ s32H_idx] = sbits + abs_fix ;
            }
            else
            {
                s32RGB3x3[ u32V_idx][ s32H_idx] = s32fix_value;
            }

         //printf("s32RGB3x3[%ld][%ld] = %ld\n",u32V_idx,s32H_idx,s32RGB3x3[u32V_idx][s32H_idx]);

         // convert signed matrix of s2.10 format to 2's complement for negative value
         MS_U32 u32current_value = s32RGB3x3[u32V_idx][s32H_idx] & 0xFFF; // remove carry bit
         if((s32RGB3x3[u32V_idx][s32H_idx] >> 12) == TRUE) // it is negative
         {
            s32RGB3x3[u32V_idx][s32H_idx] = ((~u32current_value)& 0x1FFF )+1;
         }
        }
    }

    Hal_PQ_set_xvycc_matrix_coefficient(pInstance,s32RGB3x3, eWindow == PQ_MAIN_WINDOW ? TRUE : FALSE);
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Set_xvYCC_MatrixCoefficient(MS_U64 u64Red_x,   MS_U64 u64Red_y,
                                            MS_U64 u64Green_x, MS_U64 u64Green_y,
                                            MS_U64 u64Blue_x,  MS_U64 u64Blue_y,
                                            MS_U64 u64White_x, MS_U64 u64White_y,
                                            PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Set_xvYCC_MatrixCoefficient PQArgs;
    PQArgs.u64Red_x = u64Red_x;
    PQArgs.u64Red_y = u64Red_y;
    PQArgs.u64Green_x = u64Green_x;
    PQArgs.u64Green_y = u64Green_y;
    PQArgs.u64Blue_x = u64Blue_x;
    PQArgs.u64Blue_y = u64Blue_y;
    PQArgs.u64White_x = u64White_x;
    PQArgs.u64White_y = u64White_y;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_XVYCC_MATRIXCOEFFICIENT, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_Set_xvYCC_MatrixEnable_U2(void* pInstance,MS_BOOL bEnable, PQ_WIN eWindow)
{
    if (eWindow >= PQ_MAX_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Invalid window type, please check ! \n");
        return FALSE;
    }

    Hal_PQ_set_xvycc_matrix_enable(pInstance,bEnable, eWindow == PQ_MAIN_WINDOW ? TRUE : FALSE);
    return TRUE;
}
MS_BOOL SYMBOL_WEAK MDrv_PQ_Set_xvYCC_MatrixEnable(MS_BOOL bEnable, PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Set_xvYCC_MatrixEnable PQArgs;
    PQArgs.bEnable = bEnable;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_XVYCC_MATRIXENABLE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_U16 MDrv_PQ_Get_DNR_Whole_Reg_U2(void* pInstance,PQ_WIN eWindow)
{
    return Hal_PQ_get_dnr_whole_reg(pInstance, (eWindow == PQ_MAIN_WINDOW));
}

MS_U16 SYMBOL_WEAK MDrv_PQ_Get_DNR_Whole_Reg(PQ_WIN eWindow)
{
#ifdef TRACE_DBG
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;33m\33[m\n");
#endif
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetDnrWholeReg PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u16Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_DNR_WHOLE_REG, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u16Ret;
    }
}

MS_BOOL MDrv_PQ_Up_Layer_Set_Config_U2(void* pInstance,MS_U16 uplayerSetting, PQ_MISC_GROUP group)
{
    switch(group)
    {
        case E_PQ_GROUP_A:
            _upLayerConfig = uplayerSetting;
        break;
        //case E_PQ_GROUP_B: ....
        default:
            return FALSE;
        break;
    }
    return TRUE;
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_Up_Layer_Set_Config(MS_U16 uplayerSetting, PQ_MISC_GROUP group)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_Up_Layer_Set_Config PQArgs;
    PQArgs.uplayerSetting = uplayerSetting;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_UP_LAYER_SET_CONFIG, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_U32 _MDrv_PQ_GET_DEVICE_NUM(void)
{
    return PQ_SUPPORT_DEVICE_NUM;
}
char* MDrv_PQ_Ex_GetVersion_U2(void* pInstance,PQ_WIN eWindow)
{
    if(gbPQBinEnable == 1)
    {
        char* Ex_PQ_Version;

        if(PQ_MAIN_WINDOW == eWindow)
        {
            Ex_PQ_Version = stPQBinHeaderInfo[PQ_BIN_EXT_MAIN].u8Version;
        }
        else
        {
            Ex_PQ_Version = stPQBinHeaderInfo[PQ_BIN_EXT_SUB].u8Version;
        }
        return Ex_PQ_Version;
    }
    else
    {
        return NULL;
    }
}
char* SYMBOL_WEAK MDrv_PQ_Ex_GetVersion(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return NULL;
    }

    stPQ_Ex_GetVersion PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = NULL;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_EX_GET_VERSION, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return NULL;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_U32  MDrv_PQ_GetAdaptiveVersion_U2(void* pInstance,PQ_WIN eWindow)
{
        MS_U32  PQ_Adaptive_Version = PQ_ADAPTIVE_INVALID_VERSION;
#if (PQ_SUPPORT_ADAPTIVE_VERSION)
        PQ_Adaptive_Version = Get_PQ_Adaptive_Version(pInstance);
#else
        PQ_Adaptive_Version = PQ_ADAPTIVE_INVALID_VERSION;
#endif
        return PQ_Adaptive_Version;
}
MS_U32  SYMBOL_WEAK MDrv_PQ_GetAdaptiveVersion(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return 0;
    }

    stPQ_GetAdaptiveVersion PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.bReturnValue = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_ADAPTVIE_VERSION, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}




void MDrv_PQ_LoadUFSCSettings_U2(void *pInstance,PQ_WIN eWindow)
{
    if(_bUFSCPQEn && (eWindow == PQ_MAIN_WINDOW))
    {
        _u16PQSrcType_UFSC[eWindow] = QM_InputSourceToIndex_UFSC(pInstance,eWindow, _enInputSourceType[eWindow]);
        MDrv_PQBin_LoadTableBySrcType(pInstance,_u16PQSrcType_UFSC[eWindow],
                                      PQ_BIN_IP_ALL,
                                      MDrv_PQBin_GetPanelIdx_UFSC(pInstance),
                                      &stPQBinHeaderInfo[PQ_BIN_UFSC],
                                      eWindow);
    }
}

void SYMBOL_WEAK MDrv_PQ_LoadUFSCSettings(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_LoadUFSCSettings PQArgs;
    PQArgs.eWindow = eWindow;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_UFSC_SETTING, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return ;
    }
    else
    {
        return ;
    }
}
#if (ENABLE_PQ_BIN_CF)
void MDrv_PQ_LoadCFSettings_U2(void *pInstance,PQ_WIN eWindow)
{
    if(_bCFMainPQEn && (eWindow == PQ_MAIN_WINDOW))
    {
        _u16PQSrcType_CF[eWindow] = QM_InputSourceToIndex_CF(pInstance,eWindow, _enInputSourceType[eWindow]);
        MDrv_PQBin_LoadTableBySrcType(pInstance,_u16PQSrcType_CF[eWindow],
                                      PQ_BIN_IP_ALL,
                                      PQ_BIN_CF_PANEL_INDEX,
                                      &stPQBinHeaderInfo[PQ_BIN_CF_MAIN],
                                      eWindow);
    }
#if (PQ_ENABLE_PIP)
    else if(_bCFSubPQEn &&(eWindow == PQ_SUB_WINDOW))
    {
        _u16PQSrcType_CF[eWindow] = QM_InputSourceToIndex_CF(pInstance,eWindow, _enInputSourceType[eWindow]);
        MDrv_PQBin_LoadTableBySrcType(pInstance,_u16PQSrcType_CF[eWindow],
                                      PQ_BIN_IP_ALL,
                                      PQ_BIN_CF_PANEL_INDEX,
                                      &stPQBinHeaderInfo[PQ_BIN_CF_SUB],
                                      eWindow);
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"NO CF QMAP!!!");
    }
}

void SYMBOL_WEAK MDrv_PQ_LoadCFSettings(PQ_WIN eWindow)
{
#ifdef TRACE_DBG
        printf("\33[1;33m(%s %d)\33[m\n", __FUNCTION__, __LINE__);
#endif
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return ;
    }

    stPQ_LoadCFSettings PQArgs;
    PQArgs.eWindow = eWindow;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_CF_SETTING, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return ;
    }
    else
    {
        return ;
    }
}
#endif

void SYMBOL_WEAK MDrv_PQ_SetP2pForceToDoCsc(MS_BOOL bEnable)
{
    printf("[%s:%d] bEnable=%d\n", __FUNCTION__, __LINE__, bEnable);
    _bP2pForceToDoCsc = bEnable;
}

void SYMBOL_WEAK MDrv_PQ_SetOutputColorFormat(PQ_COLOR_FORMAT enColorFmt)
{
    printf("[%s:%d] enColorFmt=%d\n", __FUNCTION__, __LINE__, enColorFmt);
    _enOutputColorFmt = enColorFmt;
    //Set Info to VR
    ST_PQ_SET_VR_INFO stPQSetVRInfo;
    memset(&stPQSetVRInfo,0,sizeof(ST_PQ_SET_VR_INFO));
    stPQSetVRInfo.u32Version = ST_PQ_SET_VR_INFO_VERSION;
    stPQSetVRInfo.u32Length = sizeof(ST_PQ_SET_VR_INFO);
    stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_COLOR_FORMAT;
    stPQSetVRInfo.enWindow= PQ_MAIN_WINDOW;
    stPQSetVRInfo.pValue= &_enOutputColorFmt;
    if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
    }

    if(_enOutputColorFmt != E_PQ_COLOR_RGB)
    {
        MS_U8 u8SubSample = 0;
        stPQSetVRInfo.enVRInfoType = E_PQ_VR_INFO_SUB_SAMPLE;
        stPQSetVRInfo.enWindow= PQ_MAIN_WINDOW;
        stPQSetVRInfo.pValue= &u8SubSample;
        if(MDrv_PQ_SetPQInfoToVR(stPQSetVRInfo) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_SetPQInfoToVR Fail\n");
        }
    }
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_GetPQPathStatus(E_PQ_Path_Type ePqPathType, MS_U16 u16Width, MS_U16 u16Height)
{
#ifdef UFO_XC_PQ_PATH
    return MApi_XC_GetPQPathStatus((E_XC_PQ_Path_Type)ePqPathType, u16Width, u16Height);
#else
    return PQ_IS_4K_INPUT(u16Width, u16Height);
#endif
}

void MDrv_PQ_SetPQBinPath_U2(void* pInstance,PQ_BIN_PATH ePqBinPath,MS_U8 u8size,char* b_PQBinFilePath)
{
    PQ_INSTANCE_PRIVATE *psPQInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psPQInstPri);
    PQ_RESOURCE_PRIVATE* pPQResourcePrivate = NULL;
    MDrv_PQ_Get_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
    UtopiaResourceGetPrivate(g_pPQResource[E_PQ_DEVICE_ID_0],(void**)(&pPQResourcePrivate));

    if(b_PQBinFilePath == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ERROR!! NULL BIN PATH!!!!!\n");
    }
    else
    {
        switch(ePqBinPath)
        {
            case E_PQ_BIN_PATH_CUSTOMER:
            {
                memset(pPQResourcePrivate->stPQBin_Path.pm_PQ_CUSTOMER_PATH,0,PQ_FILE_PATH_LENGTH);
                strncpy(pPQResourcePrivate->stPQBin_Path.pm_PQ_CUSTOMER_PATH,b_PQBinFilePath,PQ_FILE_PATH_LENGTH - 1);
                break;
            }
            case E_PQ_BIN_PATH_DEFAULT:
            {
                memset(pPQResourcePrivate->stPQBin_Path.pm_PQ_DEFAULT_PATH,0,PQ_FILE_PATH_LENGTH);
                strncpy(pPQResourcePrivate->stPQBin_Path.pm_PQ_DEFAULT_PATH,b_PQBinFilePath,PQ_FILE_PATH_LENGTH - 1);
                break;
            }
            case E_PQ_BIN_PATH_INI:
            {
                memset(pPQResourcePrivate->stPQBin_Path.pm_PQ_INI_PATH,0,PQ_FILE_PATH_LENGTH);
                strncpy(pPQResourcePrivate->stPQBin_Path.pm_PQ_INI_PATH,b_PQBinFilePath,PQ_FILE_PATH_LENGTH - 1);
                break;
            }
            case E_PQ_BIN_PATH_BANDWIDTH:
            {
                memset(pPQResourcePrivate->stPQBin_Path.pm_PQ_BANDWIDTH_PATH,0,PQ_FILE_PATH_LENGTH);
                strncpy(pPQResourcePrivate->stPQBin_Path.pm_PQ_BANDWIDTH_PATH,b_PQBinFilePath,PQ_FILE_PATH_LENGTH - 1);
                break;
            }
            default:
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"ERROR!! SET WRONG PQ_BIN_PATH TYPE!!!!!\n");
                break;

        }
    }
    MDrv_PQ_Release_Semaphore(pInstance,E_PQ_POOL_ID_INTERNAL);
}
void SYMBOL_WEAK MDrv_PQ_SetPQBinPath(PQ_BIN_PATH ePqBinPath,MS_U8 u8size,char* b_PQBinFilePath)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return ;
        }
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"\33[1;33m[MARCOS]OPEN FINE\33[m\n");
    }

    stPQ_SetPQBinPath PQArgs;
    PQArgs.ePqBinPath = ePqBinPath;
    PQArgs.u8size = u8size;
    PQArgs.b_PQBinFilePath = b_PQBinFilePath;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_PQBIN_PATH, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return ;
    }
}

MS_U32 MDrv_PQ_IsFieldOrderIncorrect_U2(void* pInstance, PQ_WIN eWindow)
{
    //Need Impl
    return FALSE;
    //return Hal_PQ_IsReversedFieldOrder(pInstance);
}

MS_U32 SYMBOL_WEAK MDrv_PQ_IsFieldOrderIncorrect(PQ_WIN eWindow)
{
#ifdef TRACE_DBG
        printf("\33[1;33m(%s %d)\33[m\n", __FUNCTION__, __LINE__);
#endif
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_IsFieldOrderIncorrect PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.u32Ret = 0;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_IS_FIELD_ORDER_INCORRECT, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return 0;
    }
    else
    {
        return PQArgs.u32Ret;
    }
}

void MDrv_PQ_LoadTMOSettings_U2(void* pInstance, PQ_WIN eWindow)
{
#if (ENABLE_PQ_BIN_TMO == 1)
    if(_bTMOPQEn && (eWindow == PQ_MAIN_WINDOW))
    {
        _u16PQSrcType_TMO[eWindow] = QM_InputSourceToIndex_TMO(pInstance, eWindow, _enInputSourceType[eWindow]);
        if(_u16LastPQSrcTypeTMO[eWindow] == _u16PQSrcType_TMO[eWindow])
        {
            PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"Skip loading TMO table,[TMO type = %d, eWindow = %d]\n", _u16PQSrcType_TMO[eWindow], eWindow);
        }
        else
        {
            _u16LastPQSrcTypeTMO[eWindow] = _u16PQSrcType_TMO[eWindow];
            MDrv_PQBin_LoadTableBySrcType(pInstance, _u16PQSrcType_TMO[eWindow],
                                            PQ_BIN_IP_ALL,
                                            MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO),
                                            &stPQBinHeaderInfo[PQ_BIN_TMO],
                                            eWindow);
        }
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;33m[TMO] Do Not Have TMO bin");
        //PQ_ASSERT(0);
    }
#else
    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"\33[1;33m[TMO] Do Not Have TMO bin");
#endif
}

void SYMBOL_WEAK MDrv_PQ_LoadTMOSettings(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return ;
        }
        printf("\33[1;33m[MARCOS](%s %d) OPEN FINE\33[m\n", __FUNCTION__, __LINE__);
    }

    stPQ_LoadTMOSettings PQArgs;
    PQArgs.eWindow = eWindow;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_TMO_SETTINGS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return ;
    }
}

MS_BOOL MDrv_PQ_SetMemoryAddress_U2(void* pInstance, MS_PQ_SET_MEMORY_INFO *pstPQ_Set_Memory_Address)
{
#if (ENABLE_PQ_BIN_TMO == 1)
    MS_BOOL bReturn = FALSE;

    switch(pstPQ_Set_Memory_Address->eType)
    {
        case E_PQ_MEMORY_TMO_VR_REQUEST:
             bReturn = MDrv_PQBin_SetVRAddress(pInstance, pstPQ_Set_Memory_Address->phyAddress ,pstPQ_Set_Memory_Address->u32Size );
            break;
        default:
            return FALSE;
    }
    return bReturn;
#else
    return FALSE;
#endif
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_SetMemoryAddress(MS_PQ_SET_MEMORY_INFO *pstPQ_Set_Memory_Address)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
        printf("\33[1;33m[MARCOS](%s %d) OPEN FINE\33[m\n", __FUNCTION__, __LINE__);
    }

    stPQ_SetMemoryAddress PQArgs;
    PQArgs.pstPQ_Set_Memory_Address = pstPQ_Set_Memory_Address;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_MEMORY_ADDRESS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_LoadTMOModeGRuleTable_U2(void* pInstance, PQ_WIN eWindow, MS_PQ_HDR_GRULE_INDEX  eHDRGRuleLevelIndex, MS_U16 u16Width, MS_U16 u16Height)
{
#if (ENABLE_PQ_BIN_TMO)
    if(eWindow == PQ_SUB_WINDOW)
    {
        return FALSE;
    }
    MS_U16 u16PQ_TMOMode_Idx=0;

    u16PQ_TMOMode_Idx = MHal_PQ_GetTMOModeLevelIndex(pInstance,eWindow,eHDRGRuleLevelIndex,u16Width,u16Height);

    if(u16PQ_TMOMode_Idx == PQ_INVALID_QMAP_INDEX)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"u16PQ_TMOMode_Idx == PQ_INVALID_QMAP_INDEX\n");
        return FALSE;
    }

    MS_U16 u16GroupNum = 0;
    MS_U16 u16IPNum = 0;

    if(MHal_PQ_Support_GroupIPBIN())
    {
        MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,PQ_GRule_TMO_MODE_Main,PQ_BIN_TMO,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_TMO]);
    }
    else
    {
        u16GroupNum = PQ_GRULE_TMO_MODE_NUM_Main;
        u16IPNum = PQ_GRULE_TMO_MODE_IP_NUM_Main;
    }
    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[PQ] GroupNum: %u, IPNum: %u\n", u16GroupNum, u16IPNum);

    if (u16PQ_TMOMode_Idx > u16GroupNum)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRULE_INDEX_OVERFLOW_ERROR Level Index = %u, Max Level Index = %u",u16PQ_TMOMode_Idx,(PQ_GRULE_TMO_MODE_NUM_Main-1));
        PQ_ASSERT(0);
        return FALSE;
    }

#if (PQ_SUPPORT_SETGRULEDATA == 1)
    _MDrv_PQ_SetQmapGRuleLevelIndex(pInstance, eWindow, PQ_BIN_TMO, E_PQ_TMO_GRULE, u16PQ_TMOMode_Idx);
    _MDrv_PQ_SetQMAPTMOGruleData(pInstance, eWindow, E_PQ_TMO_GRULE);
#else
    MS_U8 u8Count = 0;
    MS_U16 u16IPIdx=0, u16TabIdx=0;

    _u16PQSrcType_TMO[eWindow] = QM_InputSourceToIndex_TMO(pInstance, eWindow, _enInputSourceType[eWindow]);

    for(u8Count=0; u8Count<u16IPNum; u8Count++)
    {
        u16IPIdx = MDrv_PQ_GetTMOGRule_IPIndex(pInstance, eWindow, PQ_GRule_TMO_MODE_Main, u8Counti);

        if (u16PQ_TMOMode_Idx == u16GroupNum) // see it as default
        {
            u16TabIdx = MDrv_PQ_GetTMOTableIndex(pInstance, eWindow, u16IPIdx);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetTMOGRule_TableIndex(pInstance, eWindow, PQ_GRule_TMO_MODE_Main, u16PQ_TMOMode_Idx, u8Count);
        }
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[TMOMode]SRC  PQSrcType:%u, u16PQ_HDRMode_Idx: %u, HDRModeIPIdx:%u, IPIdx:%u, u8TabIdx:%u\n",
            _u16PQSrcType_TMO[eWindow], u16PQ_TMOMode_Idx, u8Count, u16IPIdx, u16TabIdx);

        if(_bTMOPQEn)
        {
            MDrv_PQBin_LoadTable(pInstance, MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO),
                                 u16TabIdx,
                                 u16IPIdx,
                                 &stPQBinHeaderInfo[PQ_BIN_TMO],eWindow);
        }
    }
#endif
    return TRUE;
#else
    UNUSED(eWindow);
    UNUSED(eHDRGRuleLevelIndex);
    UNUSED(u16Width);
    UNUSED(u16Height);
    return FALSE;
#endif
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_LoadTMOModeGRuleTable(PQ_WIN eWindow, MS_PQ_HDR_GRULE_INDEX  eHDRGRuleLevelIndex, MS_U16 u16Width, MS_U16 u16Height)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
        printf("\33[1;33m[MARCOS](%s %d) OPEN FINE\33[m\n", __FUNCTION__, __LINE__);
    }

    stPQ_LoadTMOModeGRuleTable PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.eHDRGRuleLevelIndex= eHDRGRuleLevelIndex;
    PQArgs.u16Width= u16Width;
    PQArgs.u16Height= u16Height;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_TMO_MODE_GRULE_TABLE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }

}

MS_BOOL MDrv_PQ_LoadSWDRModeGRuleTable_U2(void* pInstance, PQ_WIN eWindow, MS_PQ_SWDR_GRULE_INDEX eSWDRGRuleLevelIndex)
{
#if ((ENABLE_PQ_BIN_TMO==1)&&(PQ_SWDR_VR==1))
    MS_U8 i;
    MS_U16 u16IPIdx=0, u16TabIdx=0;
    MS_U16 u16PQ_SWDRMode_Idx=0;
    MS_U16 u16GroupNum = 0;
    MS_U16 u16IPNum = 0;

    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"Window[%u] eSWDRGRuleLevelIndex is %u\n",eWindow,eSWDRGRuleLevelIndex);

    if(eWindow == PQ_SUB_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"Unsupport Window[%u]\n",eWindow);
        return FALSE;
    }

    if(  (PQ_DynContr_OFF == MDrv_PQ_GetDLCModeStatus(eWindow))
        &&   (E_PQ_SWDR_OFF == MDrv_PQ_GetSWDRModeStatus(eWindow)) )
    {
        eSWDRGRuleLevelIndex = E_PQ_SWDR_DLC_OFF;
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"Window[%u] eSWDRGRuleLevelIndex is %u\n",eWindow,eSWDRGRuleLevelIndex);
    }

    MDrv_PQ_SetSWDRModeStatus(eWindow , eSWDRGRuleLevelIndex);

    u16PQ_SWDRMode_Idx = MHal_PQ_GetSWDRModeLevelIndex(pInstance,eWindow,eSWDRGRuleLevelIndex);
    if(u16PQ_SWDRMode_Idx == PQ_INVALID_QMAP_INDEX)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"u16PQ_SWDRMode_Idx == PQ_INVALID_QMAP_INDEX\n");
        return FALSE;
    }

    if(MHal_PQ_Support_GroupIPBIN())
    {
        MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,PQ_GRule_SWDR_MODE_Main,PQ_BIN_TMO,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_TMO]);
    }
    else
    {
        u16GroupNum = PQ_GRULE_SWDR_MODE_NUM_Main;
        u16IPNum = PQ_GRULE_SWDR_MODE_IP_NUM_Main;
    }
    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[PQ] GroupNum: %u, IPNum: %u\n", u16GroupNum, u16IPNum);

    if (u16PQ_SWDRMode_Idx > u16GroupNum)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRULE_INDEX_OVERFLOW_ERROR Level Index = %u, Max Level Index = %u",u16PQ_SWDRMode_Idx,(PQ_GRULE_SWDR_MODE_NUM_Main-1));
        PQ_ASSERT(0);
        return FALSE;
    }

    _u16PQSrcType_TMO[eWindow] = QM_InputSourceToIndex_TMO(pInstance, eWindow, _enInputSourceType[eWindow]);

    for(i=0; i<u16IPNum; i++)
    {
        u16IPIdx = MDrv_PQ_GetTMOGRule_IPIndex(pInstance, eWindow, PQ_GRule_SWDR_MODE_Main, i);

        if (u16PQ_SWDRMode_Idx == u16GroupNum) // see it as default
            u16TabIdx = MDrv_PQ_GetTMOTableIndex(pInstance, eWindow, u16IPIdx);
        else
            u16TabIdx = MDrv_PQ_GetTMOGRule_TableIndex(pInstance, eWindow, PQ_GRule_SWDR_MODE_Main, u16PQ_SWDRMode_Idx, i);

        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[SWDRMode]SRC  PQSrcType:%u, u16PQ_SWDRMode_Idx: %u, SWDRModeIPIdx:%u, IPIdx:%u, u8TabIdx:%u\n",
            _u16PQSrcType_TMO[eWindow], u16PQ_SWDRMode_Idx, i, u16IPIdx, u16TabIdx);

        if(_bTMOPQEn)
        {
            MDrv_PQBin_LoadTable(pInstance, MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO),
                                 u16TabIdx,
                                 u16IPIdx,
                                 &stPQBinHeaderInfo[PQ_BIN_TMO],eWindow);
        }
    }
    return TRUE;
#else
    UNUSED(eWindow);
    UNUSED(eSWDRGRuleLevelIndex);
    return FALSE;
#endif
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_LoadSWDRModeGRuleTable(PQ_WIN eWindow, MS_PQ_SWDR_GRULE_INDEX  eSWDRGRuleLevelIndex)
{
#ifdef TRACE_DBG
        printf("\33[1;33m[](%s %d)\33[m\n", __FUNCTION__, __LINE__);
        //return;
#endif
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
        printf("\33[1;33m[](%s %d) OPEN FINE\33[m\n", __FUNCTION__, __LINE__);
    }

    stPQ_LoadSWDRModeGRuleTable PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.eSWDRGRuleLevelIndex= eSWDRGRuleLevelIndex;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_SWDR_MODE_GRULE_TABLE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}


MS_BOOL MDrv_PQ_LoadUCDModeGRuleTable_U2(void* pInstance, PQ_WIN eWindow, ST_PQ_UCD_LEVEL *pstUCDGRuleLevelIndex)
{
#if ((ENABLE_PQ_BIN_TMO==1)&&(PQ_UCD_VR==1))
    MS_U8 u8Index=0;
    MS_U16 u16IPIdx=0, u16TabIdx=0;
    MS_U16 u16PQ_UCDMode_Idx=0;
    MS_PQ_UCD_GRULE_INDEX eUCDGRuleLevelIndex;
    MS_U16 u16GroupNum = 0;
    MS_U16 u16IPNum = 0;

    ST_PQ_UCD_LEVEL stUCD_LEVEL;
    MS_U32 u32CopiedLength = sizeof(ST_PQ_UCD_LEVEL);
    memset(&stUCD_LEVEL, 0, sizeof(ST_PQ_UCD_LEVEL));


    VERSION_COMPATIBLE_CHECK(pstUCDGRuleLevelIndex, stUCD_LEVEL, u32CopiedLength, u32Version, u32Length, ST_UCD_LEVEL_VERSION, E_APIXC_RET_FAIL);
    pstUCDGRuleLevelIndex->u32Version = ST_UCD_LEVEL_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstUCDGRuleLevelIndex->u32Length = u32CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/

    memcpy(&stUCD_LEVEL, pstUCDGRuleLevelIndex, u32CopiedLength);
    eUCDGRuleLevelIndex = stUCD_LEVEL.enUCDLevel;

    if(eWindow == PQ_SUB_WINDOW)
    {
        return FALSE;
    }

    if(E_PQ_UCD_OFF == eUCDGRuleLevelIndex)
    {
        u16PQ_UCDMode_Idx = MDrv_PQ_GetTMOGRule_LevelIndex(pInstance, PQ_GRule_UCD_MODE_Main, PQ_GRule_Lvl_UCD_MODE_OFF_Main);
    }
   else if(E_PQ_UCD_LOW == eUCDGRuleLevelIndex)
    {
        u16PQ_UCDMode_Idx = MDrv_PQ_GetTMOGRule_LevelIndex(pInstance, PQ_GRule_UCD_MODE_Main, PQ_GRule_Lvl_UCD_MODE_LOW_Main);
    }
    else if(E_PQ_UCD_MID == eUCDGRuleLevelIndex)
    {
        u16PQ_UCDMode_Idx = MDrv_PQ_GetTMOGRule_LevelIndex(pInstance, PQ_GRule_UCD_MODE_Main, PQ_GRule_Lvl_UCD_MODE_MID_Main);
    }
    else if(E_PQ_UCD_HIGH == eUCDGRuleLevelIndex)
    {
        u16PQ_UCDMode_Idx = MDrv_PQ_GetTMOGRule_LevelIndex(pInstance, PQ_GRule_UCD_MODE_Main, PQ_GRule_Lvl_UCD_MODE_HIGH_Main);
    }
    else
    {
        return FALSE;
    }

    if(MHal_PQ_Support_GroupIPBIN())
    {
        MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,PQ_GRule_UCD_MODE_Main,PQ_BIN_TMO,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_TMO]);
    }
    else
    {
        u16GroupNum = PQ_GRULE_UCD_MODE_NUM_Main;
        u16IPNum = PQ_GRULE_UCD_MODE_IP_NUM_Main;
    }
    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[PQ] GroupNum: %u, IPNum: %u\n", u16GroupNum, u16IPNum);

    if (u16PQ_UCDMode_Idx > u16GroupNum)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRULE_INDEX_OVERFLOW_ERROR Level Index = %u, Max Level Index = %u",u16PQ_UCDMode_Idx,(PQ_GRULE_UCD_MODE_NUM_Main-1));
        PQ_ASSERT(0);
        return FALSE;
    }

    _u16PQSrcType_TMO[eWindow] = QM_InputSourceToIndex_TMO(pInstance, eWindow, _enInputSourceType[eWindow]);

    for(u8Index=0; u8Index<u16IPNum; u8Index++)
    {
        u16IPIdx = MDrv_PQ_GetTMOGRule_IPIndex(pInstance, eWindow, PQ_GRule_UCD_MODE_Main, u8Index);

        if (u16PQ_UCDMode_Idx == u16GroupNum) // see it as default
        {
            u16TabIdx = MDrv_PQ_GetTMOTableIndex(pInstance, eWindow, u16IPIdx);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetTMOGRule_TableIndex(pInstance, eWindow, PQ_GRule_UCD_MODE_Main, u16PQ_UCDMode_Idx, u8Index);
        }

        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[SWDRMode]SRC  PQSrcType:%u, u16PQ_SWDRMode_Idx: %u, SWDRModeIPIdx:%u, IPIdx:%u, u8TabIdx:%u\n",
            _u16PQSrcType_TMO[eWindow], u16PQ_UCDMode_Idx, u8Index, u16IPIdx, u16TabIdx);

        if(_bTMOPQEn)
        {
            MDrv_PQBin_LoadTable(pInstance, MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO),
                                 u16TabIdx,
                                 u16IPIdx,
                                 &stPQBinHeaderInfo[PQ_BIN_TMO],eWindow);
        }
    }
    return TRUE;
#else
    UNUSED(eWindow);
    UNUSED(pstUCDGRuleLevelIndex);
    return FALSE;
#endif
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_LoadUCDModeGRuleTable(PQ_WIN eWindow, ST_PQ_UCD_LEVEL  *pstUCDGRuleLevel)
{
#ifdef TRACE_DBG
        printf("\33[1;33m[](%s %d)\33[m\n", __FUNCTION__, __LINE__);
        //return;
#endif
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
        printf("\33[1;33m[](%s %d) OPEN FINE\33[m\n", __FUNCTION__, __LINE__);
    }

    stPQ_LoadUCDModeGRuleTable PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.pstUCDGRuleLevelIndex= pstUCDGRuleLevel;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_UCD_MODE_GRULE_TABLE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_SetSWDRModeStatus_U2(void* pInstance, PQ_WIN enWindow , MS_PQ_SWDR_GRULE_INDEX enSWDRModeType)
{
    if (enWindow >= PQ_MAX_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"Unsupport Window[%u]\n",enWindow);
        return FALSE;
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"Window[%u] enSWDRModeType is %u\n",enWindow,enSWDRModeType);
        _enSWDRModeOn[enWindow] = enSWDRModeType;
        return TRUE;
    }
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_SetSWDRModeStatus(PQ_WIN eWindow, MS_PQ_SWDR_GRULE_INDEX enSWDRModeType)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SWDRModeStatus PQArgs;
    PQArgs.enWindow = eWindow;
    PQArgs.enSWDRModeType= enSWDRModeType;

    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_SWDR_MODE_STATUS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

MS_PQ_SWDR_GRULE_INDEX MDrv_PQ_GetSWDRModeStatus_U2(void* pInstance, PQ_WIN enWindow)
{
#if ((ENABLE_PQ_BIN_TMO==1) && (PQ_SWDR_VR==1))
    if (enWindow >= PQ_MAX_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"Unsupport Window[%u]\n",enWindow);
        return E_PQ_SWDR_OFF;
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"Window[%u] _enSWDRModeOn is %u\n",enWindow,_enSWDRModeOn[enWindow]);
        return _enSWDRModeOn[enWindow];
    }
#else
    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"unsupport tmo\n");
    return E_PQ_SWDR_OFF;
#endif
}

MS_PQ_SWDR_GRULE_INDEX SYMBOL_WEAK MDrv_PQ_GetSWDRModeStatus(PQ_WIN eWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return E_PQ_SWDR_DLC_OFF;
    }
    stPQ_SWDRModeStatus PQArgs;
    PQArgs.enWindow = eWindow;
    PQArgs.enSWDRModeType = E_PQ_SWDR_OFF;

    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_SWDR_MODE_STATUS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return E_PQ_SWDR_OFF;
    }
    else
    {
        return PQArgs.enSWDRModeType;
    }
}
MS_BOOL MDrv_PQ_LoadDLCLevelGRuleTable_U2(void* pInstance, PQ_WIN eWindow, EN_PQ_DLC_GRULE_INDEX enDLCGRuleLevelIndex )
{
    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"Window[%u] enDLCGRuleLevelIndex is %u\n",eWindow,enDLCGRuleLevelIndex);
    if( (eWindow == PQ_SUB_WINDOW)
         || (enDLCGRuleLevelIndex >= E_PQ_DLC_MAX) )
    {
        return FALSE;
    }

#if ((ENABLE_PQ_BIN_TMO==1) && (PQ_DLC_VR==1))
    MS_U16 u16PQ_DLCMode_Idx=0;
    MS_U16 u16GroupNum = 0;
    MS_U16 u16IPNum = 0;

    u16PQ_DLCMode_Idx = MHal_PQ_GetDLCSettingModeLevelIndex(pInstance,eWindow,enDLCGRuleLevelIndex);

    if(MHal_PQ_Support_GroupIPBIN())
    {
        MDrv_PQBin_GetGRule_GroupIPNum(pInstance,eWindow,PQ_GRule_DLC_Setting_MODE_Main,PQ_BIN_TMO,&u16GroupNum, &u16IPNum ,&stPQBinHeaderInfo[PQ_BIN_TMO]);
    }
    else
    {
        u16GroupNum = PQ_GRULE_DLC_Setting_MODE_NUM_Main;
        u16IPNum = PQ_GRULE_DLC_Setting_MODE_IP_NUM_Main;
    }
    PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[PQ] GroupNum: %u, IPNum: %u\n", u16GroupNum, u16IPNum);

    if (u16PQ_DLCMode_Idx > u16GroupNum)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"GRULE_INDEX_OVERFLOW_ERROR Level Index = %u, Max Level Index = %u",u16PQ_DLCMode_Idx,(PQ_GRULE_DLC_Setting_MODE_NUM_Main-1));
        PQ_ASSERT(0);
        return FALSE;
    }

#if (PQ_SUPPORT_SETGRULEDATA == 1)
    _MDrv_PQ_SetQmapGRuleLevelIndex(pInstance, eWindow, PQ_BIN_TMO, (EN_PQ_QMAP_TYPE)PQ_GRule_DLC_Setting_MODE_Main, u16PQ_DLCMode_Idx);
    MDrv_PQ_SetQMAPGruleData_U2(pInstance, eWindow, E_PQ_TMO_DLC_GRULE);
#else
    MS_U8 u8Index=0;
    MS_U16 u16IPIdx=0, u16TabIdx=0;

    _u16PQSrcType_TMO[eWindow] = QM_InputSourceToIndex_TMO(pInstance, eWindow, _enInputSourceType[eWindow]);

    for(u8Index=0; u8Index<u16IPNum; u8Index++)
    {
        u16IPIdx = MDrv_PQ_GetTMOGRule_IPIndex(pInstance, eWindow, PQ_GRule_DLC_Setting_MODE_Main, u8Index);

        if (u16PQ_DLCMode_Idx == PQ_GRULE_SWDR_MODE_NUM_Main) // see it as default
        {
            u16TabIdx = MDrv_PQ_GetTMOTableIndex(pInstance, eWindow, u16IPIdx);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetTMOGRule_TableIndex(pInstance, eWindow, PQ_GRule_DLC_Setting_MODE_Main, u16PQ_DLCMode_Idx, u8Index);
        }

        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[DLCMode]SRC  PQSrcType:%u, u16PQ_SWDRMode_Idx: %u, u16PQ_DLCMode_Idx:%u, IPIdx:%u, u8TabIdx:%u\n",
            _u16PQSrcType_TMO[eWindow], u16PQ_DLCMode_Idx, u8Index, u16IPIdx, u16TabIdx);

        if(_bTMOPQEn)
        {
            MDrv_PQBin_LoadTable(pInstance, MDrv_PQBin_GeneralGetPanelIdx(pInstance,PQ_BIN_TMO),
                                 u16TabIdx,
                                 u16IPIdx,
                                 &stPQBinHeaderInfo[PQ_BIN_TMO],eWindow);
        }

    }
#endif

    return TRUE;
#else
    return FALSE;
#endif
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_LoadDLCLevelGRuleTable(PQ_WIN eWindow, EN_PQ_DLC_GRULE_INDEX enDLCGRuleLevelIndex)
{
#ifdef TRACE_DBG
        printf("\33[1;33m[](%s %d)\33[m\n", __FUNCTION__, __LINE__);
        //return;
#endif
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
        printf("\33[1;33m[](%s %d) OPEN FINE\33[m\n", __FUNCTION__, __LINE__);
    }

    stPQ_LoadDLCModeGRuleTable PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.enDLCGRuleLevelIndex= enDLCGRuleLevelIndex;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_LOAD_DLC_MODE_GRULE_TABLE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_Set_Customer_Color_Grule(PQ_WIN enWindow,MS_PQ_COLOR_PICTUREMODE enColorPictureMode)
{
#if (PQ_GRULE_COLOR_ENABLE == 1)
   MS_U16 u16Grulestatus = PQ_GRule_COLOR_Standard_Main_Color;
   switch(enColorPictureMode)
   {
       case PQ_COLOR_PICTUREMODE_ECONOMY:
            u16Grulestatus = PQ_GRule_COLOR_Economy_Main_Color;
       break;
       case PQ_COLOR_PICTUREMODE_DYNAMIC:
            u16Grulestatus = PQ_GRule_COLOR_Dynamic_Main_Color;
       break;
       case PQ_COLOR_PICTUREMODE_STANDARD:
            u16Grulestatus = PQ_GRule_COLOR_Standard_Main_Color;
       break;
       case PQ_COLOR_PICTUREMODE_MOVIE:
            u16Grulestatus = PQ_GRule_COLOR_Movie_Main_Color;
       break;
       case PQ_COLOR_PICTUREMODE_GAME:
            u16Grulestatus = PQ_GRule_COLOR_Game_Main_Color;
       break;
       case PQ_COLOR_PICTUREMODE_PC:
            u16Grulestatus = PQ_GRule_COLOR_PC_Main_Color;
       break;
       case PQ_COLOR_PICTUREMODE_CINEMA:
            u16Grulestatus = PQ_GRule_COLOR_Cinema_Main_Color;
       break;
       case PQ_COLOR_PICTUREMODE_PERSONAL:
            u16Grulestatus = PQ_GRule_COLOR_Personal_Main_Color;
       break;
       default:
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"file:%s  func:%s     Line:%d  Invalid  color picturemode mode, use standard.  please check it! \n",__FILE__,__FUNCTION__,__LINE__);
            u16Grulestatus = PQ_GRule_COLOR_Standard_Main_Color;
       break;

   }
   return _MDrv_PQ_SetGRuleStatus_Ex(pInstance,enWindow, PQ_GRule_COLOR_Main_Color, u16Grulestatus, PQ_XC0_CUSTOMER);
#else
   PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"customer Color Grule not add , please add it first!!\n");
   return FALSE;
#endif
}

#if (ENABLE_PQ_BIN_HSY)
MS_BOOL MDrv_PQ_GetHSYGRule_IP(PQ_WIN enWin __attribute__((unused)), EN_HSY_GRULE_IP enLabel, MS_U32 *pu32GruleIP)
{
    MS_BOOL bReturn = TRUE;

    switch(enLabel)
    {
    case E_HSY_GRULE_IP_PICTUREMODE:
        *pu32GruleIP = GRULE_PICTUREMODE;
        break;

    default:
        bReturn = FALSE;
        break;
    }

    return bReturn;
}

MS_BOOL MDrv_PQ_GetHSYGRule_IDX(PQ_WIN enWin __attribute__((unused)), EN_HSY_GRULE_IDX enIndex, MS_U32 *pu32label)
{
    MS_BOOL bReturn = TRUE;

    if(NULL == pu32label)
        return FALSE;

    switch(enIndex)
    {
    case E_HSY_GRULE_IDX_PICTUREMODE_AUTO:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_AUTO);
        break;

    case E_HSY_GRULE_IDX_PICTUREMODE_GAME:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_GAME);
        break;

    case E_HSY_GRULE_IDX_PICTUREMODE_LIGHTNESS:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_LIGHTNESS);
        break;

    case E_HSY_GRULE_IDX_PICTUREMODE_NATURAL:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_NATURAL);
        break;

    case E_HSY_GRULE_IDX_PICTUREMODE_PC:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_PC);
        break;

    case E_HSY_GRULE_IDX_PICTUREMODE_SOFT:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_SOFT);
        break;

    case E_HSY_GRULE_IDX_PICTUREMODE_SPORTS:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_SPORTS);
        break;

    case E_HSY_GRULE_IDX_PICTUREMODE_STANDARD:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_STANDARD);
        break;

    case E_HSY_GRULE_IDX_PICTUREMODE_USER:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_USER);
        break;

    case E_HSY_GRULE_IDX_PICTUREMODE_VIVID:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_VIVID);
        break;
    case E_HSY_GRULE_IDX_PICTUREMODE_OFF:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_OFF);
        break;

#if(PQ_GRULE_AI_PQ_ENABLE == 1)
    case E_HSY_GRULE_IDX_PICTUREMODE_AI_PQ:
        SET_GRULE_IDX(pu32label, GRULE_IDX_PICTUREMODE_AI_PQ);
        break;
#endif

    default:
        bReturn = FALSE;
        break;
    }

    return bReturn;
}
#endif

EN_PQ_PICTUREMODE_GRULE_IDX _MDrv_PQ_PictureModeIdxMapping(EN_HSY_GRULE_IDX enGRuleTable)
{
    EN_PQ_PICTUREMODE_GRULE_IDX enResult = E_GRULE_IDX_PICTUREMODE_MAX;
    switch(enGRuleTable)
    {
        case E_HSY_GRULE_IDX_PICTUREMODE_AUTO:
            enResult = E_GRULE_IDX_PICTUREMODE_AUTO;
            break;
        case E_HSY_GRULE_IDX_PICTUREMODE_GAME:
            enResult = E_GRULE_IDX_PICTUREMODE_GAME;
            break;
        case E_HSY_GRULE_IDX_PICTUREMODE_SOFT:
            enResult = E_GRULE_IDX_PICTUREMODE_MOVIE;
            break;
        case E_HSY_GRULE_IDX_PICTUREMODE_STANDARD:
            enResult = E_GRULE_IDX_PICTUREMODE_STANDARD;
            break;
        case E_HSY_GRULE_IDX_PICTUREMODE_USER:
            enResult = E_GRULE_IDX_PICTUREMODE_USER;
            break;
        case E_HSY_GRULE_IDX_PICTUREMODE_VIVID:
            enResult = E_GRULE_IDX_PICTUREMODE_VIVID;
            break;
        case E_HSY_GRULE_IDX_PICTUREMODE_PC:
            enResult = E_GRULE_IDX_PICTUREMODE_PC;
            break;
        case E_HSY_GRULE_IDX_PICTUREMODE_OFF:
            enResult = E_GRULE_IDX_PICTUREMODE_OFF;
            break;
        default:
            enResult = E_GRULE_IDX_PICTUREMODE_STANDARD;
            break;
    }
    return enResult;
}

PQ_WIN MDrv_PQ_SCtoPQ_WinMapping(SCALER_WIN enWindow)
{
    PQ_WIN enResult = PQ_MAIN_WINDOW;
    switch(enWindow)
    {
        case MAIN_WINDOW:
            enResult = PQ_MAIN_WINDOW;
            break;
        case SUB_WINDOW:
            enResult = PQ_SUB_WINDOW;
            break;
        case MAX_WINDOW:
            enResult = PQ_MAX_WINDOW;
            break;
        default:
            enResult = PQ_MAX_WINDOW;
            break;
    }
    return enResult;
}


SCALER_WIN MDrv_PQ_PQtoSC_WinMapping(PQ_WIN enWindow)
{
    SCALER_WIN enResult = MAIN_WINDOW;
    switch(enWindow)
    {
        case PQ_MAIN_WINDOW:
            enResult = MAIN_WINDOW;
            break;
        case PQ_SUB_WINDOW:
            enResult = SUB_WINDOW;
            break;
        case PQ_MAX_WINDOW:
            enResult = MAX_WINDOW;
            break;
        default:
            enResult = MAX_WINDOW;
            break;
    }
    return enResult;
}
#if (PQ_SUPPORT_SETGRULEDATA == 0)
#if defined(PQ_GRULE_PIC_MODE_ENABLE) && (PQ_GRULE_PIC_MODE_ENABLE == 1)
static MS_BOOL _MDrv_PQ_LoadPICModeTable(PQ_WIN eWindow, MS_U16 u16PQ_PICMode_Idx)
{
    MS_U16 u16Idx, u16IPIdx=0, u16TabIdx=0;
    MS_U16 u16GRuleLevelIndex=0;

    if(eWindow == PQ_SUB_WINDOW)
    {
        return FALSE;
    }

    u16GRuleLevelIndex = MDrv_PQ_GetGRule_LevelIndex(eWindow, PQ_GRule_PIC_MODE_Main, u16PQ_PICMode_Idx);

    if (u16GRuleLevelIndex > PQ_GRULE_PIC_MODE_NUM_Main)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"[PICMODE]u16GRuleLevelIndex > PQ_GRULE_PIC_MODE_NUM_Main Fail!\n");
        return FALSE;
    }
#if(ENABLE_PQ_MLOAD == 1)
    MDrv_PQ_Set_MLoadEn(eWindow, TRUE);
#endif

    for(u16Idx=0; u16Idx<PQ_GRULE_PIC_MODE_IP_NUM_Main; u16Idx++)
    {
        u16IPIdx = MDrv_PQ_GetGRule_IPIndex(eWindow, PQ_GRule_PIC_MODE_Main, u16Idx);

        if (u16GRuleLevelIndex == PQ_GRULE_PIC_MODE_NUM_Main) // see it as default
        {
            u16TabIdx = MDrv_PQ_GetTableIndex(eWindow, u16IPIdx);
        }
        else
        {
            u16TabIdx = MDrv_PQ_GetGRule_TableIndex(eWindow, PQ_GRule_PIC_MODE_Main, u16GRuleLevelIndex, u16Idx);
        }

        PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[PICMode]SRC: %u, u16PQ_PICMode_Idx: %u, PICModeIPIdx:%u, IPIdx:%u, u8TabIdx:%u\n",
            _u16PQSrcType[eWindow], u16GRuleLevelIndex,u16Idx, u16IPIdx, u16TabIdx);
        MDrv_PQ_LoadTable(eWindow, u16TabIdx, u16IPIdx);
    }
#if(ENABLE_PQ_MLOAD == 1)
    MDrv_PQ_Set_MLoadEn(eWindow, FALSE);
#endif
    return TRUE;
}
#endif
#endif

MS_BOOL MDrv_PQ_SetHSYGRule_U2(void* pInstance, PQ_WIN enWindow, EN_HSY_GRULE_IP enGRuleLabel, EN_HSY_GRULE_IDX enGRuleTable)
{
    MS_BOOL bReturn = FALSE;
    PQ_LEVEL_PRINTF(PQ_DBG_HSY,"=============Set HSY Grule, GruleIP=[%d], GruleIPIdx=[%d]=========== \n", enGRuleLabel, enGRuleTable);

#if (ENABLE_PQ_BIN_HSY == 1)
    MS_U32 u32GRuleIP = 0;
    MS_U16 u16HYSBinID = 0;
    ST_XC_CFD_HSY stHSY;
    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stHSY, 0, sizeof(ST_XC_CFD_HSY));
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));

    if ((enGRuleLabel >= E_HSY_GRULE_IP_MAX) || (pstBinMap == NULL) )
    {
        PQ_LEVEL_PRINTF((PQ_DBG_ERROR|PQ_DBG_HSY),"Set HSY GRule Failed: Invalid HSY GRule IP or pstBinMap == NULL\n");
        bReturn = FALSE;
    }
    else
    {
        MDrv_PQ_GetHSYGRule_IP(enWindow, enGRuleLabel, &u32GRuleIP);

        if (enGRuleTable >= E_HSY_GRULE_IDX_MAX)
        {
            PQ_LEVEL_PRINTF((PQ_DBG_ERROR|PQ_DBG_HSY),"Set HSY GRule Fail: Invalid HSY GRule IDX\n");
            bReturn = FALSE;
        }
        else
        {
            if (QM_InputSourceToIndex_HSY(enWindow, _enInputSourceType[enWindow], _u32PQSrcType_HSY) != FALSE)
            {
                if (MDrv_PQ_GetHSYGRule_IDX(enWindow, enGRuleTable, _u32PQSrcType_HSY) != FALSE)
                {
                    u16HYSBinID = MDrv_HSYBin_GetBinID(E_HSY_BIN_ID_HSY);
                    bReturn = MDrv_HSYBin_GetGRuleParse(pInstance, &pstBinMap[u16HYSBinID], u32GRuleIP, _u32PQSrcType_HSY);
                    if(bReturn)
                    {
                        stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_HSY;
                        stHSY.u8Win = (MS_U8)enWindow;
                        stHSY.u16Length = sizeof(ST_XC_CFD_HSY);
                        stHSY.u32Version = CFD_HSY_VERSION;
                        PQ_MALLOC_MEM(stHSY.pu32PQSrcTypeHSY, (LEN_HSY_CONFIG * sizeof(MS_U32)));
                        if(stHSY.pu32PQSrcTypeHSY)
                        {
                            memcpy(stHSY.pu32PQSrcTypeHSY,_u32PQSrcType_HSY,LEN_HSY_CONFIG*sizeof(MS_U32));
                            stCfdCtrlInfo.pParam = &stHSY;
                            stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_CFD_HSY);
                            MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrlInfo);
                        }
                        PQ_FREE_MEM(stHSY.pu32PQSrcTypeHSY, (LEN_HSY_CONFIG * sizeof(MS_U32)));
                        _enHsyGruleIdx[enGRuleLabel] = enGRuleTable;
                        PQ_LEVEL_PRINTF(PQ_DBG_HSY,"\033[1;32m HSY Rule found! Label:[%x %x]\033[m\n", _u32PQSrcType_HSY[0], _u32PQSrcType_HSY[1]);
                    }
                }
            }
        }
    }
#else

#if (PQ_SUPPORT_SETGRULEDATA == 0)
#if defined(PQ_GRULE_PIC_MODE_ENABLE) && (PQ_GRULE_PIC_MODE_ENABLE == 1)
    EN_PQ_PICTUREMODE_GRULE_IDX enPicModeIdx = _MDrv_PQ_PictureModeIdxMapping(enGRuleTable);
    MS_U16 u16GRuleLevelIndex = (MS_U16)enPicModeIdx;
    if(_MDrv_PQ_LoadPICModeTable(enWindow, u16GRuleLevelIndex)==FALSE)
    {
      PQ_LEVEL_PRINTF(PQ_DBG_GRULE,"[PICMODE]_MDrv_PQ_LoadPICModeTable Fail!\n");
      bReturn = FALSE;
    }
#endif
#endif
#endif
    return bReturn;
}
#if (ENABLE_PQ_BIN_HSY == 1)
MS_BOOL MDrv_PQ_GetHSYGRule_U2(void* pInstance, PQ_WIN enWindow, EN_HSY_GRULE_IP enGRuleLabel, EN_HSY_GRULE_IDX *penGRuleTable)
{
    if(NULL != penGRuleTable)
    {
        *penGRuleTable = _enHsyGruleIdx[enGRuleLabel];
    }
    else
    {
        return FALSE;
    }

    return TRUE;
}
#endif
MS_BOOL SYMBOL_WEAK MDrv_PQ_SetHSYGRule(PQ_WIN eWindow, EN_HSY_GRULE_IP enGRuleLabel, EN_HSY_GRULE_IDX enGRuleTable)
{
#ifdef TRACE_DBG
        printf("\33[1;33m[HSY](%s %d)\33[m\n", __FUNCTION__, __LINE__);
#endif

    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_SetHSYGRule PQArgs;
    PQArgs.eWindow = eWindow;
    PQArgs.enGRuleLabel = enGRuleLabel;
    PQArgs.enGRuleTable = enGRuleTable;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_HSY_GRULE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return TRUE;
    }
    return FALSE;
}
MS_BOOL MDrv_PQ_GetHSYGRule(PQ_WIN enWindow, EN_HSY_GRULE_IP enGRuleLabel, EN_HSY_GRULE_IDX *penGRuleTable)
{
#if (ENABLE_PQ_BIN_HSY == 1)
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetHSYGRule PQArgs;
    PQArgs.eWindow = enWindow;
    PQArgs.enGRuleLabel = enGRuleLabel;
    PQArgs.penGRuleTable = penGRuleTable;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_HSY_GRULE, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return TRUE;
    }
#endif
    return FALSE;
}

MS_BOOL MDrv_PQ_SetHsbBypass_U2(void* pInstance, ST_PQ_SET_HSB_BYPASS *pstSetHsbBypass)
{
    MS_BOOL bRet = FALSE;
#if PQ_VIP_CTL
    ST_PQ_SET_HSB_BYPASS stSetHsbBypass;
    MS_U32 u32ValidLength = 0;

    memset(&stSetHsbBypass, 0, sizeof(stSetHsbBypass));
    // version handling
    if ((pstSetHsbBypass == NULL) || (pstSetHsbBypass->u32Length == 0))
    {
        //We cosider compatible operation form version2 , so reject the info init when version invalid
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetHsbBypass: Null paramter or Wrong u32Length!!\n");

        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Exit\n");
        return bRet;
    }
    // the version control is coming in with version 1
    if (pstSetHsbBypass->u32Version < 1)
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetHsbBypass: please check your u32Version, it should not set to 0!!\n");
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Exit\n");

        return bRet;
    }

    //new AP + old lib, only the length corresponding to old lib has meaning.
    if(pstSetHsbBypass->u32Version > ST_PQ_SET_COLOUR_PRIMARIES_VERSION)
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
#if defined (__aarch64__)
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetHsbBypass: this old version XC lib has only length:%lu driver status!!\n", sizeof(*pstSetHsbBypass));
#else
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetHsbBypass: this old version XC lib has only length:%u driver status!!\n", sizeof(*pstSetHsbBypass));
#endif
        //we still copy the min size of both structure, but AP should take care of it.
        u32ValidLength = sizeof(*pstSetHsbBypass);
    }

    //old AP + new lib, driver shouldn't access to the space which doesn't exist in old structure
    if((pstSetHsbBypass->u32Version < ST_XC_MEMORY_INFO_VERSION) || (pstSetHsbBypass->u32Length < sizeof(*pstSetHsbBypass)))
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetHsbBypass: new version XC lib shouldn't access to the space which doesn't exist in old structure!!\n");
        u32ValidLength = pstSetHsbBypass->u32Length;
    }
    else
    {
        u32ValidLength = sizeof(*pstSetHsbBypass);
    }
    memcpy(&stSetHsbBypass, pstSetHsbBypass, u32ValidLength);
    stSetHsbBypass.u32Length = u32ValidLength;

    switch(stSetHsbBypass.u8HsbIdx)
    {
        case E_PQ_HSB_HUE:
        {
            if(TRUE == stSetHsbBypass.bBypass)
            {
                MDrv_PQ_LoadTable(stSetHsbBypass.enWindow, PQ_IP_VIP_IHC_OFF_Main, PQ_IP_VIP_IHC_Main);
            }
            else
            {
                MDrv_PQ_LoadTable(stSetHsbBypass.enWindow, PQ_IP_VIP_IHC_ON_Main, PQ_IP_VIP_IHC_Main);
            }
        }
        break;
        case E_PQ_HSB_SATURATION:
        {

            if(TRUE == stSetHsbBypass.bBypass)
            {
                MDrv_PQ_LoadTable(stSetHsbBypass.enWindow, PQ_IP_VIP_ICC_OFF_Main, PQ_IP_VIP_ICC_Main);
            }
            else
            {
                MDrv_PQ_LoadTable(stSetHsbBypass.enWindow, PQ_IP_VIP_ICC_ON_Main, PQ_IP_VIP_ICC_Main);
            }
        }
        break;
        case E_PQ_HSB_BRIGHTNESS:
        {

            if(TRUE == stSetHsbBypass.bBypass)
            {
                MDrv_PQ_LoadTable(stSetHsbBypass.enWindow, PQ_IP_VIP_IBC_OFF_Main, PQ_IP_VIP_IBC_Main);
            }
            else
            {
                MDrv_PQ_LoadTable(stSetHsbBypass.enWindow, PQ_IP_VIP_IBC_ON_Main, PQ_IP_VIP_IBC_Main);
            }
        }
        break;
        default:
        break;
    }
    bRet = TRUE;
#else
    UNUSED(pstSetHsbBypass);
    bRet = FALSE;
#endif
    return bRet;
}

MS_BOOL MDrv_PQ_SetHsbBypass(ST_PQ_SET_HSB_BYPASS *pstSetHsbBypass)
{
    stPQ_SetHsbBypass PQArgs;
#ifdef TRACE_DBG
        printf("\33[1;33m[](%s %d)\33[m\n", __FUNCTION__, __LINE__);
        //return;
#endif
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
        printf("\33[1;33m[](%s %d) OPEN FINE\33[m\n", __FUNCTION__, __LINE__);
    }

    PQArgs.pstHsbBypass = pstSetHsbBypass;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_HSB_BYPASS, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_SetColourPrimaries_U2(void* pInstance, ST_PQ_SET_COLOUR_PRIMARIES *pstSetColourPrimaries)
{
    MS_U8 au8ROffset[3];                //from OSD 0~100
    MS_U8 au8GOffset[3];                //from OSD 0~100
    MS_U8 au8BOffset[3];                //from OSD 0~100
    MS_U8 au8COffset[3];                //from OSD 0~100
    MS_U8 au8MOffset[3];                //from OSD 0~100
    MS_U8 au8YOffset[3];                //from OSD 0~100
    MS_U8 u8AddrIdx = 0;
    MS_U8 u8RIdx = 0, u8GIdx = 0, u8BIdx = 0;
    MS_U8 u8Step = 10;         //0~10
    MS_U16 au16R[3][3][3], au16G[3][3][3], au16B[3][3][3];
    MS_BOOL bRet = FALSE;
    MS_U32 u32ValidLength = 0;
    ST_PQ_SET_COLOUR_PRIMARIES stColourPrimaries;

    memset(&stColourPrimaries, 0, sizeof(stColourPrimaries));
    // version handling
    if ((pstSetColourPrimaries == NULL) || (pstSetColourPrimaries->u32Length == 0))
    {
        //We cosider compatible operation form version2 , so reject the info init when version invalid
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetColourPrimaries: Null paramter or Wrong u32Length!!\n");
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Exit\n");
        return bRet;
    }
    // the version control is coming in with version 1
    if (pstSetColourPrimaries->u32Version < 1)
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetColourPrimaries: please check your u32Version, it should not set to 0!!\n");
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "Exit\n");
        return bRet;
    }

    //new AP + old lib, only the length corresponding to old lib has meaning.
    if(pstSetColourPrimaries->u32Version > ST_PQ_SET_COLOUR_PRIMARIES_VERSION)
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
#if defined (__aarch64__)
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetColourPrimaries: this old version XC lib has only length:%lu driver status!!\n",sizeof(*pstSetColourPrimaries));
#else
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetColourPrimaries: this old version XC lib has only length:%u driver status!!\n", sizeof(*pstSetColourPrimaries));
#endif
        //we still copy the min size of both structure, but AP should take care of it.
        u32ValidLength = sizeof(*pstSetColourPrimaries);
    }

    //old AP + new lib, driver shouldn't access to the space which doesn't exist in old structure
    if((pstSetColourPrimaries->u32Version < ST_PQ_SET_COLOUR_PRIMARIES_VERSION) || (pstSetColourPrimaries->u32Length < sizeof(*pstSetColourPrimaries)))
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
        PQ_LEVEL_PRINTF(PQ_DBG_INFO, "MDrv_PQ_SetColourPrimaries: new version XC lib shouldn't access to the space which doesn't exist in old structure!!\n");
        u32ValidLength = pstSetColourPrimaries->u32Length;
    }
    else
    {
        u32ValidLength = sizeof(*pstSetColourPrimaries);
    }
    memcpy(&stColourPrimaries, pstSetColourPrimaries, u32ValidLength);
    stColourPrimaries.u32Length = u32ValidLength;

    if (stColourPrimaries.u32Size < COLOUR_PRIMARY_SIZE)
    {
        return FALSE;
    }

    au8ROffset[0] = stColourPrimaries.pu8Offset[0];
    au8ROffset[1] = stColourPrimaries.pu8Offset[1];
    au8ROffset[2] = stColourPrimaries.pu8Offset[2];

    au8GOffset[0] = stColourPrimaries.pu8Offset[3];
    au8GOffset[1] = stColourPrimaries.pu8Offset[4];
    au8GOffset[2] = stColourPrimaries.pu8Offset[5];

    au8BOffset[0] = stColourPrimaries.pu8Offset[6];
    au8BOffset[1] = stColourPrimaries.pu8Offset[7];
    au8BOffset[2] = stColourPrimaries.pu8Offset[8];

    au8COffset[0] = stColourPrimaries.pu8Offset[9];
    au8COffset[1] = stColourPrimaries.pu8Offset[10];
    au8COffset[2] = stColourPrimaries.pu8Offset[11];

    au8MOffset[0] = stColourPrimaries.pu8Offset[12];
    au8MOffset[1] = stColourPrimaries.pu8Offset[13];
    au8MOffset[2] = stColourPrimaries.pu8Offset[14];

    au8YOffset[0] = stColourPrimaries.pu8Offset[15];
    au8YOffset[1] = stColourPrimaries.pu8Offset[16];
    au8YOffset[2] = stColourPrimaries.pu8Offset[17];

    //Black
    au16R[0][0][0] = 512;
    au16G[0][0][0] = 512;
    au16B[0][0][0] = 512;

    //R(200)
    au16R[2][0][0] = 512+((MS_S16)au8ROffset[0]-50)*u8Step;
    au16G[2][0][0] = 512+((MS_S16)au8ROffset[1]-50)*u8Step;
    au16B[2][0][0] = 512+((MS_S16)au8ROffset[2]-50)*u8Step;
    au16R[1][0][0] = 512+((MS_S16)au8ROffset[0]-50)*u8Step/2;
    au16G[1][0][0] = 512+((MS_S16)au8ROffset[1]-50)*u8Step/2;
    au16B[1][0][0] = 512+((MS_S16)au8ROffset[2]-50)*u8Step/2;
    //G(020)
    au16R[0][2][0] = 512+((MS_S16)au8GOffset[0]-50)*u8Step;
    au16G[0][2][0] = 512+((MS_S16)au8GOffset[1]-50)*u8Step;
    au16B[0][2][0] = 512+((MS_S16)au8GOffset[2]-50)*u8Step;
    au16R[0][1][0] = 512+((MS_S16)au8GOffset[0]-50)*u8Step/2;
    au16G[0][1][0] = 512+((MS_S16)au8GOffset[1]-50)*u8Step/2;
    au16B[0][1][0] = 512+((MS_S16)au8GOffset[2]-50)*u8Step/2;
    //B(002)
    au16R[0][0][2] = 512+((MS_S16)au8BOffset[0]-50)*u8Step;
    au16G[0][0][2] = 512+((MS_S16)au8BOffset[1]-50)*u8Step;
    au16B[0][0][2] = 512+((MS_S16)au8BOffset[2]-50)*u8Step;
    au16R[0][0][1] = 512+((MS_S16)au8BOffset[0]-50)*u8Step/2;
    au16G[0][0][1] = 512+((MS_S16)au8BOffset[1]-50)*u8Step/2;
    au16B[0][0][1] = 512+((MS_S16)au8BOffset[2]-50)*u8Step/2;
    //C(022)
    au16R[0][2][2] = 512+((MS_S16)au8COffset[0]-50)*u8Step;
    au16G[0][2][2] = 512+((MS_S16)au8COffset[1]-50)*u8Step;
    au16B[0][2][2] = 512+((MS_S16)au8COffset[2]-50)*u8Step;
    au16R[0][1][1] = 512+((MS_S16)au8COffset[0]-50)*u8Step/2;
    au16G[0][1][1] = 512+((MS_S16)au8COffset[1]-50)*u8Step/2;
    au16B[0][1][1] = 512+((MS_S16)au8COffset[2]-50)*u8Step/2;
    //M(202)
    au16R[2][0][2] = 512+((MS_S16)au8MOffset[0]-50)*u8Step;
    au16G[2][0][2] = 512+((MS_S16)au8MOffset[1]-50)*u8Step;
    au16B[2][0][2] = 512+((MS_S16)au8MOffset[2]-50)*u8Step;
    au16R[1][0][1] = 512+((MS_S16)au8MOffset[0]-50)*u8Step/2;
    au16G[1][0][1] = 512+((MS_S16)au8MOffset[1]-50)*u8Step/2;
    au16B[1][0][1] = 512+((MS_S16)au8MOffset[2]-50)*u8Step/2;
    //Y(220)
    au16R[2][2][0] = 512+((MS_S16)au8YOffset[0]-50)*u8Step;
    au16G[2][2][0] = 512+((MS_S16)au8YOffset[1]-50)*u8Step;
    au16B[2][2][0] = 512+((MS_S16)au8YOffset[2]-50)*u8Step;
    au16R[1][1][0] = 512+((MS_S16)au8YOffset[0]-50)*u8Step/2;
    au16G[1][1][0] = 512+((MS_S16)au8YOffset[1]-50)*u8Step/2;
    au16B[1][1][0] = 512+((MS_S16)au8YOffset[2]-50)*u8Step/2;
    //W(222)
    au16R[2][2][2] = 512;//+((MS_S16)au8ROffset[0]+(MS_S16)au8GOffset[0]+(MS_S16)au8BOffset[0]+(MS_S16)au8COffset[0]+(MS_S16)au8MOffset[0]+(MS_S16)au8YOffset[0]-50*6)*u8Step/6;
    au16G[2][2][2] = 512;//+((MS_S16)au8ROffset[1]+(MS_S16)au8GOffset[1]+(MS_S16)au8BOffset[1]+(MS_S16)au8COffset[1]+(MS_S16)au8MOffset[1]+(MS_S16)au8YOffset[1]-50*6)*u8Step/6;
    au16B[2][2][2] = 512;//+((MS_S16)au8ROffset[2]+(MS_S16)au8GOffset[2]+(MS_S16)au8BOffset[2]+(MS_S16)au8COffset[2]+(MS_S16)au8MOffset[2]+(MS_S16)au8YOffset[2]-50*6)*u8Step/6;
    au16R[1][1][1] = 512;//+((MS_S16)au8ROffset[0]+(MS_S16)au8GOffset[0]+(MS_S16)au8BOffset[0]+(MS_S16)au8COffset[0]+(MS_S16)au8MOffset[0]+(MS_S16)au8YOffset[0]-50*6)*u8Step/6/2;
    au16G[1][1][1] = 512;//+((MS_S16)au8ROffset[1]+(MS_S16)au8GOffset[1]+(MS_S16)au8BOffset[1]+(MS_S16)au8COffset[1]+(MS_S16)au8MOffset[1]+(MS_S16)au8YOffset[1]-50*6)*u8Step/6/2;
    au16B[1][1][1] = 512;//+((MS_S16)au8ROffset[2]+(MS_S16)au8GOffset[2]+(MS_S16)au8BOffset[2]+(MS_S16)au8COffset[2]+(MS_S16)au8MOffset[2]+(MS_S16)au8YOffset[2]-50*6)*u8Step/6/2;


    //(210)
    au16R[2][1][0] = (au16R[2][0][0]+au16R[2][2][0])/2;
    au16G[2][1][0] = (au16G[2][0][0]+au16G[2][2][0])/2;
    au16B[2][1][0] = (au16B[2][0][0]+au16B[2][2][0])/2;
    //(120)
    au16R[1][2][0] = (au16R[0][2][0]+au16R[2][2][0])/2;
    au16G[1][2][0] = (au16G[0][2][0]+au16G[2][2][0])/2;
    au16B[1][2][0] = (au16B[0][2][0]+au16B[2][2][0])/2;
    //(012)
    au16R[0][1][2] = (au16R[0][0][2]+au16R[0][2][2])/2;
    au16G[0][1][2] = (au16G[0][0][2]+au16G[0][2][2])/2;
    au16B[0][1][2] = (au16B[0][0][2]+au16B[0][2][2])/2;
    //(021)
    au16R[0][2][1] = (au16R[0][2][0]+au16R[0][2][2])/2;
    au16G[0][2][1] = (au16G[0][2][0]+au16G[0][2][2])/2;
    au16B[0][2][1] = (au16B[0][2][0]+au16B[0][2][2])/2;
    //(102)
    au16R[1][0][2] = (au16R[0][0][2]+au16R[2][0][2])/2;
    au16G[1][0][2] = (au16G[0][0][2]+au16G[2][0][2])/2;
    au16B[1][0][2] = (au16B[0][0][2]+au16B[2][0][2])/2;
    //(201)
    au16R[2][0][1] = (au16R[2][0][0]+au16R[2][0][2])/2;
    au16G[2][0][1] = (au16G[2][0][0]+au16G[2][0][2])/2;
    au16B[2][0][1] = (au16B[2][0][0]+au16B[2][0][2])/2;

    //(122)
    au16R[1][2][2] = (au16R[0][2][2]+au16R[2][2][2])/2;
    au16G[1][2][2] = (au16G[0][2][2]+au16G[2][2][2])/2;
    au16B[1][2][2] = (au16B[0][2][2]+au16B[2][2][2])/2;
    //(212)
    au16R[2][1][2] = (au16R[2][0][2]+au16R[2][2][2])/2;
    au16G[2][1][2] = (au16G[2][0][2]+au16G[2][2][2])/2;
    au16B[2][1][2] = (au16B[2][0][2]+au16B[2][2][2])/2;
    //(221)
    au16R[2][2][1] = (au16R[2][2][0]+au16R[2][2][2])/2;
    au16G[2][2][1] = (au16G[2][2][0]+au16G[2][2][2])/2;
    au16B[2][2][1] = (au16B[2][2][0]+au16B[2][2][2])/2;

    //(112)
    au16R[1][1][2] = (au16R[0][1][2]+au16R[2][1][2])/2;
    au16G[1][1][2] = (au16G[0][1][2]+au16G[2][1][2])/2;
    au16B[1][1][2] = (au16B[0][1][2]+au16B[2][1][2])/2;
    //(121)
    au16R[1][2][1] = (au16R[0][2][1]+au16R[2][2][1])/2;
    au16G[1][2][1] = (au16G[0][2][1]+au16G[2][2][1])/2;
    au16B[1][2][1] = (au16B[0][2][1]+au16B[2][2][1])/2;
    //(211)
    au16R[2][1][1] = (au16R[2][0][1]+au16R[2][2][1])/2;
    au16G[2][1][1] = (au16G[2][0][1]+au16G[2][2][1])/2;
    au16B[2][1][1] = (au16B[2][0][1]+au16B[2][2][1])/2;


    for(u8RIdx = 0; u8RIdx < 3; u8RIdx++)
    {
        for(u8GIdx = 0; u8GIdx < 3; u8GIdx++)
        {
            for(u8BIdx = 0; u8BIdx < 3; u8BIdx++)
            {
                MApi_XC_W2BYTEMSK(REG_SC_BK14_01_L + u8AddrIdx, au16R[u8RIdx][u8GIdx][u8BIdx], 0x03FF);
                MApi_XC_W2BYTEMSK(REG_SC_BK14_1C_L + u8AddrIdx, au16G[u8RIdx][u8GIdx][u8BIdx], 0x03FF);
                MApi_XC_W2BYTEMSK(REG_SC_BK14_37_L + u8AddrIdx, au16B[u8RIdx][u8GIdx][u8BIdx], 0x03FF);
                u8AddrIdx = u8AddrIdx+2;
            }
        }
    }

    MApi_XC_W2BYTEMSK(REG_SC_BK14_52_L,BIT(0), BIT(0));

    return TRUE;
}

MS_BOOL MDrv_PQ_SetColourPrimaries(ST_PQ_SET_COLOUR_PRIMARIES *pstSetColourPrimaries)
{
    stPQ_SetColourPrimaries PQArgs;
#ifdef TRACE_DBG
        printf("\33[1;33m[](%s %d)\33[m\n", __FUNCTION__, __LINE__);
        //return;
#endif
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
        printf("\33[1;33m[](%s %d) OPEN FINE\33[m\n", __FUNCTION__, __LINE__);
    }

    PQArgs.pstColourPrimaries = pstSetColourPrimaries;
    PQArgs.bReturnValue       = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_COLOUR_PRIMARIES, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_General_GetVersion_U2(void* pInstance, MS_U8* pu8Version,PQ_BIN_TYPE enPQBinType ,PQ_WIN enWindow)
{
    if (enPQBinType >= MAX_PQ_BIN_NUM)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"enPQBinType >= MAX_PQ_BIN_NUM\n");
        return FALSE;
    }
    if (enWindow >= PQ_MAX_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"enWindow >= PQ_MAX_WINDOW\n");
        return FALSE;
    }
    if (pu8Version == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pu8Version == NULL\n");
        return FALSE;
    }

    memset(pu8Version, 0 ,PQ_BIN_VERSION_LEN*sizeof(MS_U8));

    switch(enPQBinType)
    {
        case PQ_BIN_STD_MAIN:
        {
            if(MDrv_PQ_GetVersion(enWindow) != NULL)
            {
                memcpy(pu8Version, MDrv_PQ_GetVersion(enWindow), PQ_BIN_VERSION_LEN*sizeof(MS_U8));
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_GetVersion == NULL\n");
                return FALSE;
            }
            break;
        }
        case PQ_BIN_EXT_MAIN:
        {
            if(MDrv_PQ_Ex_GetVersion(enWindow) != NULL)
            {
                memcpy(pu8Version, MDrv_PQ_Ex_GetVersion(enWindow), PQ_BIN_VERSION_LEN*sizeof(MS_U8));
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_Ex_GetVersion == NULL\n");
                return FALSE;
            }
            break;
        }
        case PQ_BIN_CUSTOMER_MAIN:
        {
            if (_bCustomerMainPQEn)
            {
                 if(MDrv_PQ_Cus_GetVersion(enWindow) != NULL)
                {
                    memcpy(pu8Version, MDrv_PQ_Cus_GetVersion(enWindow), PQ_BIN_VERSION_LEN*sizeof(MS_U8));
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_Cus_GetVersion == NULL\n");
                    return FALSE;
                }
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"_bCustomerMainPQEn == FASLE\n");
                return FALSE;
            }
            break;
        }
        case PQ_BIN_UFSC:
        {
            if (_bUFSCPQEn)
            {
                memcpy(pu8Version, stPQBinHeaderInfo[enPQBinType].u8Version, PQ_BIN_VERSION_LEN*sizeof(MS_U8));
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"_bUFSCPQEn == FASLE\n");
                return FALSE;
            }
            break;
        }
#if (PQ_ENABLE_PIP ==1)
        case PQ_BIN_STD_SUB:
        {
             if(MDrv_PQ_GetVersion(enWindow) != NULL)
            {
                memcpy(pu8Version, MDrv_PQ_GetVersion(enWindow), PQ_BIN_VERSION_LEN*sizeof(MS_U8));
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_GetVersion == NULL\n");
                return FALSE;
            }
            break;
        }
        case PQ_BIN_EXT_SUB:
        {
             if(MDrv_PQ_Ex_GetVersion(enWindow) != NULL)
            {
                memcpy(pu8Version, MDrv_PQ_Ex_GetVersion(enWindow), PQ_BIN_VERSION_LEN*sizeof(MS_U8));
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_Ex_GetVersion == NULL\n");
                return FALSE;
            }
            break;
        }
        case PQ_BIN_CUSTOMER_SUB:
        {
            if (_bCustomerSubPQEn)
            {
                if(MDrv_PQ_Cus_GetVersion(enWindow) != NULL)
                {
                    memcpy(pu8Version, MDrv_PQ_Cus_GetVersion(enWindow), PQ_BIN_VERSION_LEN*sizeof(MS_U8));
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_Cus_GetVersion == NULL\n");
                    return FALSE;
                }
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"_bCustomerSubPQEn == FASLE\n");
                return FALSE;
            }
            break;
        }
#endif
#if (ENABLE_PQ_BIN_CF == 1)
        case PQ_BIN_CF_MAIN:
        {
            if (_bCFMainPQEn)
            {
                memcpy(pu8Version, stPQBinHeaderInfo[enPQBinType].u8Version, PQ_BIN_VERSION_LEN*sizeof(MS_U8));
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"_bCFMainPQEn == FASLE\n");
                return FALSE;
            }
            break;
        }
#if (PQ_ENABLE_PIP ==1)
        case PQ_BIN_CF_SUB:
        {
            if (_bCFSubPQEn)
            {
                memcpy(pu8Version, stPQBinHeaderInfo[enPQBinType].u8Version, PQ_BIN_VERSION_LEN*sizeof(MS_U8));
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"_bCFSubPQEn == FASLE\n");
                return FALSE;
            }
        }
#endif
#endif
        case PQ_BIN_TMO:
        {
            if (_bTMOPQEn)
            {
                memcpy(pu8Version, stPQBinHeaderInfo[enPQBinType].u8Version, PQ_BIN_VERSION_LEN*sizeof(MS_U8));
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"_bTMOPQEn == FASLE\n");
                return FALSE;
            }
            break;
        }
#if (ENABLE_PQ_BIN_HSY)
        case PQ_BIN_HSY:
        {
            memcpy(pu8Version, stForestMap.stForestHeader.u8Description, PQ_BIN_VERSION_LEN*sizeof(MS_U8));
            break;
        }
#endif
        default:
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"enPQBinType do not Support\n");
            return FALSE;
        }
    }
    PQ_LEVEL_PRINTF(PQ_DBG_INFO,"pu8Version = %s\n",pu8Version);

    return TRUE;
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_General_GetVersion(MS_U8* pu8Version,PQ_BIN_TYPE enPQBinType ,PQ_WIN enWindow)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetVersion_EX stPQArgs;
    stPQArgs.eWindow = enWindow;
    stPQArgs.pu8Ret  = (char*)pu8Version;
    stPQArgs.enPQBinType  = enPQBinType;
    if (UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_VERSION_EX, (void*)&stPQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
MS_BOOL MDrv_PQBin_GetGRule_GroupIPNum(void *pInstance, PQ_WIN eWindow,MS_U16 u16GRuleType,PQ_BIN_TYPE enPQBinType ,MS_U16* u16GroupNum,MS_U16* u16IPNum ,const MS_PQBin_Header_Info* const pPQBinHeader)
{
    MS_BOOL bPQbinEN = FALSE;
    if (eWindow == PQ_MAIN_WINDOW)
    {
        switch (enPQBinType)
        {
            case PQ_BIN_STD_MAIN:
                    bPQbinEN = gbPQBinEnable;
                break;
            case PQ_BIN_EXT_MAIN:
                    bPQbinEN = gbPQBinEnable;
                break;
            case PQ_BIN_CUSTOMER_MAIN:
                    bPQbinEN = _bCustomerMainPQEn;
                break;
            case PQ_BIN_UFSC:
                    bPQbinEN = _bUFSCPQEn;
                break;
            case PQ_BIN_TMO:
                    bPQbinEN = _bTMOPQEn;
                break;
            default:
                break;
        }
    }
#if (PQ_ENABLE_PIP)
    else if (eWindow == PQ_SUB_WINDOW)
    {
        switch (enPQBinType)
        {
            case PQ_BIN_STD_SUB:
                    bPQbinEN = gbPQBinEnable;
                break;
            case PQ_BIN_EXT_SUB:
                    bPQbinEN = gbPQBinEnable;
                break;
            case PQ_BIN_CUSTOMER_SUB:
                    bPQbinEN = _bCustomerSubPQEn;
                break;
            default:
                break;
        }

    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"=PQ_EWINDOW_OVERFLOW!!!\n");
        return FALSE;
    }

    if (bPQbinEN == TRUE)
    {
        *u16GroupNum = MDrv_PQBin_GetGRule_GroupNum(pInstance,u16GRuleType, &stPQBinHeaderInfo[enPQBinType]);
        *u16IPNum = MDrv_PQBin_GetGRule_IPNum(pInstance,u16GRuleType, &stPQBinHeaderInfo[enPQBinType]);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"=PQ_BIN_NOT_ENABLE!!!\n");
        return FALSE;
    }
    return TRUE;
}
MS_BOOL MDrv_PQ_SetMADiVRInfo(ST_PQ_MADI_VR_INFO* pstPQMADiVRInfo)
{
    if(!pstPQMADiVRInfo)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"stPQMADiVRInfo == NULL!\n");
        return FALSE;
    }
    if(!pstPQMADiVRInfo->peDeInterlaceMode)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"peDeInterlaceMode == NULL!\n");
        return FALSE;
    }
    if(!pstPQMADiVRInfo->pu8BitsPerPixel)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pu8BitsPerPixel == NULL!\n");
        return FALSE;
    }
    if(!pstPQMADiVRInfo->pu8CmpBitsPerPixel)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pu8CmpBitsPerPixel == NULL!\n");
        return FALSE;
    }
    if(!pstPQMADiVRInfo->pu8FrameCount)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pu8FrameCount == NULL!\n");
        return FALSE;
    }
    if(!pstPQMADiVRInfo->penFBLevel)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"penFBLevel == NULL!\n");
        return FALSE;
    }
    if(!pstPQMADiVRInfo->pbOPWriteOffEnable)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pbOPWriteOffEnable == NULL!\n");
        return FALSE;
    }
    if(MHal_PQ_SetMADiVRInfo(pstPQMADiVRInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MHal_PQ_SetMADiVRInfo Fail!\n");
        return FALSE;
    }
    return TRUE;
}
MS_BOOL MDrv_PQ_GetIndexAndLoadTable(ST_PQ_GET_IDX_AND_LOAD_TABLE_INFO* pstPQGetIdxAndLoadTblInfo)
{
    MS_U16 u16TabIdx = 0;
    if(!pstPQGetIdxAndLoadTblInfo)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pstPQGetIdxAndLoadTblInfo == NULL!\n");
        return  FALSE;
    }
    u16TabIdx = MDrv_PQ_GetTableIndex_Ex(pstPQGetIdxAndLoadTblInfo->enWindow, pstPQGetIdxAndLoadTblInfo->u16IPIdx, pstPQGetIdxAndLoadTblInfo->enPQEngineType);
    PQ_LEVEL_PRINTF(PQ_DBG_MADI,"u16TabIdx = %d\n",u16TabIdx);
    //when game mode ON , get frame count from game mode grule for ds control
#if (PQ_SUPPORT_GAMEMODE_VER == 2)
    if(MDrv_PQ_GetGameMode_Status(pstPQGetIdxAndLoadTblInfo->enWindow) == TRUE && pstPQGetIdxAndLoadTblInfo->u16IPIdx ==PQ_IP_ZNR_Control_VR_Main_Ex)
    {
        u16TabIdx = MDrv_PQ_GetGRule_TableIndex_Ex(pstPQGetIdxAndLoadTblInfo->enWindow, PQ_GRule_GAME_MODE_Main_Ex, PQ_GRule_Lvl_GAME_MODE_On_NonMirror_FBL_Main_Ex, 15, PQ_XC0_EXT);
    }
#endif
    MDrv_PQ_LoadTable_Ex(pstPQGetIdxAndLoadTblInfo->enWindow, u16TabIdx, pstPQGetIdxAndLoadTblInfo->u16IPIdx, pstPQGetIdxAndLoadTblInfo->enPQEngineType);
    return TRUE;
}
MS_BOOL MDrv_PQ_GetBPPInfoFromMADiVR(ST_PQ_GET_MADI_VR_INFO* pstPQGetMADiVRInfo)
{
#if (PQ_SUPPORT_MADI_VR == 0)
    PQ_LEVEL_PRINTF(PQ_DBG_MADI,"MADI VR Not Support\n");
    return FALSE;
#else
    ST_PQ_GET_IDX_AND_LOAD_TABLE_INFO stPQGetIdxAndLoadTblInfo;
    memset(&stPQGetIdxAndLoadTblInfo,0,sizeof(ST_PQ_GET_IDX_AND_LOAD_TABLE_INFO));
    stPQGetIdxAndLoadTblInfo.u32Version = ST_PQ_GET_IDX_AND_LOAD_TABLE_INFO_VERSION;
    stPQGetIdxAndLoadTblInfo.u32Length = sizeof(ST_PQ_GET_IDX_AND_LOAD_TABLE_INFO);
    stPQGetIdxAndLoadTblInfo.enWindow = pstPQGetMADiVRInfo->enWindow;
    stPQGetIdxAndLoadTblInfo.u16IPIdx = pstPQGetMADiVRInfo->u16IPIdx;
    stPQGetIdxAndLoadTblInfo.enPQEngineType   = PQ_XC0_EXT;
    if(MDrv_PQ_GetIndexAndLoadTable(&stPQGetIdxAndLoadTblInfo) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Get_Idx_And_Load_Table Fail\n");
        return FALSE;
    }
    else
    {
        E_XC_FB_LEVEL enFBLevel = E_XC_FB_LEVEL_FB;
        ST_PQ_MADI_VR_INFO stPQMADiVRInfo;
        MS_BOOL bOPWriteOffEnable = ENABLE; //default Enable
        memset(&stPQMADiVRInfo,0,sizeof(ST_PQ_MADI_VR_INFO));
        stPQMADiVRInfo.u32Version = ST_PQ_MADI_VR_INFO_VERSION;
        stPQMADiVRInfo.u32Length = sizeof(ST_PQ_MADI_VR_INFO);
        stPQMADiVRInfo.peDeInterlaceMode = pstPQGetMADiVRInfo->peDeInterlaceMode;
        stPQMADiVRInfo.pu8BitsPerPixel = pstPQGetMADiVRInfo->pu8BitsPerPixel;
        stPQMADiVRInfo.pu8CmpBitsPerPixel = pstPQGetMADiVRInfo->pu8CmpBitsPerPixel;
        stPQMADiVRInfo.pu8PQDelayCount = &u8PQDelayCount;
        stPQMADiVRInfo.pu8FrameCount = pstPQGetMADiVRInfo->pu8FrameCount;
        stPQMADiVRInfo.penFBLevel = &enFBLevel;
        stPQMADiVRInfo.pbOPWriteOffEnable = &bOPWriteOffEnable;
       if(MDrv_PQ_SetMADiVRInfo(&stPQMADiVRInfo) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Set_MADi_VR_Info Fail\n");
            return FALSE;
        }
        PQ_LEVEL_PRINTF(PQ_DBG_MADI,"pu8BitsPerPixel = %d\n",*(pstPQGetMADiVRInfo->pu8BitsPerPixel));
        PQ_LEVEL_PRINTF(PQ_DBG_MADI,"peDeInterlaceMode = %d\n",*(pstPQGetMADiVRInfo->peDeInterlaceMode));
        PQ_LEVEL_PRINTF(PQ_DBG_MADI,"pu8FrameCount = %d\n",*(pstPQGetMADiVRInfo->pu8FrameCount));
        PQ_LEVEL_PRINTF(PQ_DBG_MADI,"pu8PQDelayCount = %d\n",u8PQDelayCount);
        PQ_LEVEL_PRINTF(PQ_DBG_MADI,"enFBLevel = %d\n",enFBLevel);
        PQ_LEVEL_PRINTF(PQ_DBG_MADI,"bOPWriteOffEnable = %d\n",bOPWriteOffEnable);
        PQ_LEVEL_PRINTF(PQ_DBG_MADI,"pu8CmpBitsPerPixel = %d\n",*(pstPQGetMADiVRInfo->pu8CmpBitsPerPixel));

        MApi_XC_Set_OPWriteOffEnable(bOPWriteOffEnable, MAIN_WINDOW);
        if(!pstPQGetMADiVRInfo->bMemFmt422)
        {
#if (PQ_GRULE_GAME_MODE_ENABLE == 0)
            if(MApi_XC_GetUCEnabled((SCALER_WIN)pstPQGetMADiVRInfo->enWindow))   //if on need disable.
            {
                MDrv_PQ_DisableUCFeature(PQ_MAIN_WINDOW);
            }
#endif
        }
#if PQ_ENABLE_RFBL
        if (TRUE == MApi_XC_IsBlackVideoEnable((SCALER_WIN)pstPQGetMADiVRInfo->enWindow))
        {
            if (pstPQGetMADiVRInfo->enWindow == PQ_MAIN_WINDOW)
            {
#ifdef UFO_XC_FB_LEVEL
               E_XC_FB_LEVEL entmpFBlevel = MApi_XC_Get_FB_Level(MAIN_WINDOW);
               PQ_LEVEL_PRINTF(PQ_DBG_MADI,"enFBLevel = %d entmpFBlevel = %d\n",enFBLevel,entmpFBlevel);
                switch(entmpFBlevel)
                {
                    case E_XC_FB_LEVEL_FB_VSD:
                    case E_XC_FB_LEVEL_FBL_VSD:
                    case E_XC_FB_LEVEL_RFBL_VSD:
                    {
                        if(enFBLevel == E_XC_FB_LEVEL_FB)
                        {
                            enFBLevel = E_XC_FB_LEVEL_FB_VSD;
                        }
                        else if(enFBLevel == E_XC_FB_LEVEL_FBL)
                        {
                            enFBLevel = E_XC_FB_LEVEL_FBL_VSD;
                        }
                        else if(enFBLevel == E_XC_FB_LEVEL_RFBL_DI)
                        {
                            enFBLevel = E_XC_FB_LEVEL_RFBL_VSD;
                        }
                        else
                        {
                            PQ_LEVEL_PRINTF(PQ_DBG_MADI,"Exception enFBLevel = %d\n",enFBLevel);
                        }
                        break;
                    }
                    case E_XC_FB_LEVEL_FB_MVOPDROPLINE:
                    case E_XC_FB_LEVEL_FBL_MVOPDROPLINE:
                    case E_XC_FB_LEVEL_RFBL_MVOPDROPLINE:
                    {
                        if(enFBLevel == E_XC_FB_LEVEL_FB)
                        {
                            enFBLevel = E_XC_FB_LEVEL_FB_MVOPDROPLINE;
                        }
                        else if(enFBLevel == E_XC_FB_LEVEL_FBL)
                        {
                            enFBLevel = E_XC_FB_LEVEL_FBL_MVOPDROPLINE;
                        }
                        else if(enFBLevel == E_XC_FB_LEVEL_RFBL_DI)
                        {
                            enFBLevel = E_XC_FB_LEVEL_RFBL_MVOPDROPLINE;
                        }
                        else
                        {
                            PQ_LEVEL_PRINTF(PQ_DBG_MADI,"Exception enFBLevel = %d\n",enFBLevel);
                        }
                        break;
                    }
                    default:
                    {
                        PQ_LEVEL_PRINTF(PQ_DBG_MADI,"No VSD or MVOPDROPLINE entmpFBlevel = %d\n",entmpFBlevel);
                        break;
                    }
               }
               PQ_LEVEL_PRINTF(PQ_DBG_MADI,"enFBLevel = %d\n",enFBLevel);
               MApi_XC_Set_FB_Level(enFBLevel, MAIN_WINDOW);
#else
               MS_BOOL bFBL = FALSE;
               MS_BOOL bRFBL = FALSE;

               if(enFBLevel == E_XC_FB_LEVEL_RFBL_DI)
               {
                    bFBL = FALSE;
                    bRFBL = TRUE;
               }
               else if(enFBLevel == E_XC_FB_LEVEL_FBL)
               {
                    bFBL = TRUE;
                    bRFBL = FALSE;
               }
               else
               {
                    bFBL = FALSE;
                    bRFBL = FALSE;
               }
               MApi_XC_EnableRequest_FrameBufferLess(bRFBL);
               MApi_XC_EnableFrameBufferLess(bFBL);
#endif
            }
            else
            {
               PQ_LEVEL_PRINTF(PQ_DBG_MADI,"Sub have no FBL mode!\n");
            }
        }
#endif
        if ((QM_GetInputVFreq(pstPQGetMADiVRInfo->enWindow) != 0) && (QM_GetInputVFreq(pstPQGetMADiVRInfo->enWindow) != 1))
        {
            _u16PQDelyaTime = u8PQDelayCount * ((10000 + (QM_GetInputVFreq(pstPQGetMADiVRInfo->enWindow)/2))/QM_GetInputVFreq(pstPQGetMADiVRInfo->enWindow));  // frame duration is (1 second/input V freq)
        }
        else
        {
            _u16PQDelyaTime = u8PQDelayCount * 20; // default keep original rule, use 20ms as frame duration
        }
    return TRUE;
    }
#endif
}

MS_U8 MDrv_PQ_Get420CmpBitPerPixel(void)
{
    return _u8CmpBitPerPixel;
}
MS_BOOL MDrv_PQ_LoadControlPathTable(ST_PQ_GET_MADI_VR_INFO *pstPQMADiInfo)
{
#if PQ_SUPPORT_ZNR
    E_XC_FB_LEVEL enFBLevel = E_XC_FB_LEVEL_FB;
    ST_PQ_GET_IDX_AND_LOAD_TABLE_INFO stPQGetIdxAndLoadTblInfo;
    ST_PQ_GET_MADI_VR_INFO stPQGetMADiVRInfo;
    memset(&stPQGetIdxAndLoadTblInfo,0,sizeof(ST_PQ_GET_IDX_AND_LOAD_TABLE_INFO));
    memset(&stPQGetMADiVRInfo,0,sizeof(ST_PQ_GET_MADI_VR_INFO));

    if(pstPQMADiInfo == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pstPQGetMADiVRInfo is NULL\n");
        return FALSE;
    }

    if(pstPQMADiInfo->enWindow == PQ_MAIN_WINDOW)
    {
        stPQGetIdxAndLoadTblInfo.u32Version = ST_PQ_GET_IDX_AND_LOAD_TABLE_INFO_VERSION;
        stPQGetIdxAndLoadTblInfo.u32Length = sizeof(ST_PQ_GET_IDX_AND_LOAD_TABLE_INFO);
        stPQGetIdxAndLoadTblInfo.enWindow = pstPQMADiInfo->enWindow;
        stPQGetIdxAndLoadTblInfo.u16IPIdx = PQ_IP_ZNR_Default_Main_Ex;
        stPQGetIdxAndLoadTblInfo.enPQEngineType = PQ_XC0_EXT;
        if(MDrv_PQ_GetIndexAndLoadTable(&stPQGetIdxAndLoadTblInfo) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Load IP:%d table Fail\n",PQ_IP_ZNR_Default_Main_Ex);
            return FALSE;
        }

        stPQGetIdxAndLoadTblInfo.u16IPIdx = PQ_IP_ZNR_Control_Main_Ex;
        if(MDrv_PQ_GetIndexAndLoadTable(&stPQGetIdxAndLoadTblInfo) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Load IP:%d table Fail\n",PQ_IP_ZNR_Control_Main_Ex);
            return FALSE;
        }

        stPQGetIdxAndLoadTblInfo.u16IPIdx = PQ_IP_ZNR_Control_Ext_Main_Ex;
        if(MDrv_PQ_GetIndexAndLoadTable(&stPQGetIdxAndLoadTblInfo) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Load IP:%d table Fail\n",PQ_IP_ZNR_Control_Ext_Main_Ex);
            return FALSE;
        }

        stPQGetIdxAndLoadTblInfo.u16IPIdx = PQ_IP_ZNR_SCM_Format_Main_Ex;
        if(MDrv_PQ_GetIndexAndLoadTable(&stPQGetIdxAndLoadTblInfo) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Load IP:%d table Fail\n",PQ_IP_ZNR_SCM_Format_Main_Ex);
            return FALSE;
        }

        stPQGetIdxAndLoadTblInfo.u16IPIdx = PQ_IP_ZNR_SCM_BW_Main_Ex;
        if(MDrv_PQ_GetIndexAndLoadTable(&stPQGetIdxAndLoadTblInfo) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Load IP:%d table Fail\n",PQ_IP_ZNR_SCM_BW_Main_Ex);
            return FALSE;
        }

        stPQGetIdxAndLoadTblInfo.u16IPIdx = PQ_IP_ZNR_UCM_Format_Main_Ex;
        if(MDrv_PQ_GetIndexAndLoadTable(&stPQGetIdxAndLoadTblInfo) == FALSE)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Load IP:%d table Fail\n",PQ_IP_ZNR_UCM_Format_Main_Ex);
            return FALSE;
        }

        if(MHal_PQ_IsSupportZNRControlVR() == TRUE)
        {
            stPQGetMADiVRInfo.u32Version = ST_PQ_GET_MADI_VR_INFO_VERSION;
            stPQGetMADiVRInfo.u32Length= sizeof(ST_PQ_GET_MADI_VR_INFO);
            stPQGetMADiVRInfo.enWindow = pstPQMADiInfo->enWindow;
            stPQGetMADiVRInfo.bMemFmt422 = pstPQMADiInfo->bMemFmt422;
            stPQGetMADiVRInfo.pu8BitsPerPixel = pstPQMADiInfo->pu8BitsPerPixel;
            stPQGetMADiVRInfo.peDeInterlaceMode = pstPQMADiInfo->peDeInterlaceMode;
            stPQGetMADiVRInfo.pu8FrameCount = pstPQMADiInfo->pu8FrameCount;
            stPQGetMADiVRInfo.pu8CmpBitsPerPixel = pstPQMADiInfo->pu8CmpBitsPerPixel;
            stPQGetMADiVRInfo.u16IPIdx = PQ_IP_ZNR_Control_VR_Main_Ex;
            MDrv_PQ_GetBPPInfoFromMADiVR(&stPQGetMADiVRInfo);
        }
        else
        {
            *(pstPQMADiInfo->pu8FrameCount) = MApi_XC_R2BYTEMSK(FRAME_COUNT, 0x1F);
            *(pstPQMADiInfo->pu8BitsPerPixel) = MApi_XC_R2BYTEMSK(BITS_PER_PIXEL, 0xFF);

            if(MApi_XC_R2BYTEMSK(OPM_WRITE_REQ_OFF, BIT(0)) == DISABLE)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_MADI,"MApi_XC_Set_OPWriteOffEnable = DISABLE");
                MApi_XC_Set_OPWriteOffEnable(DISABLE, MAIN_WINDOW);
            }

#if PQ_ENABLE_RFBL
            if(MApi_XC_R2BYTEMSK(IP_BYPASS_ALL, BIT(11)))
            {
                enFBLevel = E_XC_FB_LEVEL_RFBL_DI;
            }

            if (TRUE == MApi_XC_IsBlackVideoEnable((SCALER_WIN)pstPQMADiInfo->enWindow))
            {
                if (pstPQMADiInfo->enWindow == PQ_MAIN_WINDOW)
                {
#ifdef UFO_XC_FB_LEVEL
                    E_XC_FB_LEVEL entmpFBlevel = MApi_XC_Get_FB_Level(MAIN_WINDOW);
                    PQ_LEVEL_PRINTF(PQ_DBG_MADI,"enFBLevel = %d entmpFBlevel = %d\n",enFBLevel,entmpFBlevel);
                    switch(entmpFBlevel)
                    {
                        case E_XC_FB_LEVEL_FB_VSD:
                        case E_XC_FB_LEVEL_FBL_VSD:
                        case E_XC_FB_LEVEL_RFBL_VSD:
                        {
                            if(enFBLevel == E_XC_FB_LEVEL_FB)
                            {
                                enFBLevel = E_XC_FB_LEVEL_FB_VSD;
                            }
                            else if(enFBLevel == E_XC_FB_LEVEL_FBL)
                            {
                                enFBLevel = E_XC_FB_LEVEL_FBL_VSD;
                            }
                            else if(enFBLevel == E_XC_FB_LEVEL_RFBL_DI)
                            {
                                enFBLevel = E_XC_FB_LEVEL_RFBL_VSD;
                            }
                            else
                            {
                                PQ_LEVEL_PRINTF(PQ_DBG_MADI,"Exception enFBLevel = %d\n",enFBLevel);
                            }
                            break;
                        }
                        case E_XC_FB_LEVEL_FB_MVOPDROPLINE:
                        case E_XC_FB_LEVEL_FBL_MVOPDROPLINE:
                        case E_XC_FB_LEVEL_RFBL_MVOPDROPLINE:
                        {
                            if(enFBLevel == E_XC_FB_LEVEL_FB)
                            {
                                enFBLevel = E_XC_FB_LEVEL_FB_MVOPDROPLINE;
                            }
                            else if(enFBLevel == E_XC_FB_LEVEL_FBL)
                            {
                                enFBLevel = E_XC_FB_LEVEL_FBL_MVOPDROPLINE;
                            }
                            else if(enFBLevel == E_XC_FB_LEVEL_RFBL_DI)
                            {
                                enFBLevel = E_XC_FB_LEVEL_RFBL_MVOPDROPLINE;
                            }
                            else
                            {
                                PQ_LEVEL_PRINTF(PQ_DBG_MADI,"Exception enFBLevel = %d\n",enFBLevel);
                            }
                            break;
                        }
                        default:
                        {
                            PQ_LEVEL_PRINTF(PQ_DBG_MADI,"No VSD or MVOPDROPLINE entmpFBlevel = %d\n",entmpFBlevel);
                            break;
                        }
                    }
                    PQ_LEVEL_PRINTF(PQ_DBG_MADI,"enFBLevel = %d\n",enFBLevel);
                    MApi_XC_Set_FB_Level(enFBLevel, MAIN_WINDOW);
#else
                    MS_BOOL bFBL = FALSE;
                    MS_BOOL bRFBL = FALSE;

                    if(enFBLevel == E_XC_FB_LEVEL_RFBL_DI)
                    {
                         bFBL = FALSE;
                         bRFBL = TRUE;
                    }
                    else if(enFBLevel == E_XC_FB_LEVEL_FBL)
                    {
                         bFBL = TRUE;
                         bRFBL = FALSE;
                    }
                    else
                    {
                         bFBL = FALSE;
                         bRFBL = FALSE;
                    }
                    MApi_XC_EnableRequest_FrameBufferLess(bRFBL);
                    MApi_XC_EnableFrameBufferLess(bFBL);
#endif
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_MADI,"sub have no FBL mode!\n");
                }
            }
#endif
        }
    }

    MHal_PQ_SetABFPatch(_stMode_Info[pstPQMADiInfo->enWindow].bInterlace);
#endif

    return TRUE;
}

MS_BOOL _MDrv_PQ_LoadMMDSXruleTable(void* pInstance,ST_PQ_DS_XRULE_INFO* stXCMMDSXruleInfo)
{
#if (PQ_XRULE_MM_DS_ENABLE == 1)
    if(stXCMMDSXruleInfo == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"stXCMMDSXruleInfo == NULL\n");
        return FALSE;
    }
    if(pInstance == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pInstance == NULL\n");
        return FALSE;
    }
    if(MHal_PQ_IsMMDSByPass(stXCMMDSXruleInfo->enWindow))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"PQ MM_DS By PASS\n");
        return FALSE;
    }

    MS_U16 u16IPIdx;
    MS_U16 u16TabIdx = (gbPQBinEnable == FALSE) ? PQ_IP_NULL : PQ_BIN_IP_NULL;
    MS_U8 u8XRuleIdx = 0x00;
    MS_U16 u16XRuleType = 0;

    MS_U8 u8XRuleIP = 0;
    MS_U8 u8XRuleOPStart = 0;
    MS_U8 u8XRuleUCMStart = 0;
    MS_BOOL bCheckGRule = FALSE;
    PQ_ENGINE_TYPE ePQEngineType = PQ_XC0_STD;
    u16XRuleType = (MS_U16)E_XRULE_MM_DS;
    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"V_Size=%tu, H_Size=%tu, u8FrameCount=%d, bInterlace=%d\n",(ptrdiff_t)(stXCMMDSXruleInfo->u32V_Size), (ptrdiff_t)(stXCMMDSXruleInfo->u32H_Size),stXCMMDSXruleInfo->u8FrameCount,stXCMMDSXruleInfo->bInterlace);
    //check input valid
    if(stXCMMDSXruleInfo->enWindow >= PQ_MAX_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"Invalid input:eWindow=%d,V_Size=%tu,H_Size=%tu,u8FrameCount=%d\n",stXCMMDSXruleInfo->enWindow,(ptrdiff_t)(stXCMMDSXruleInfo->u32V_Size),(ptrdiff_t)(stXCMMDSXruleInfo->u32H_Size),stXCMMDSXruleInfo->u8FrameCount);
        return FALSE;
    }
    //check PQbin support Xrule_MM_DS
    if(stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16XRuleNum <= E_XRULE_MM_DS)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"PQbin and header file is mismatch ,Please update PQbin!   u16XRuleNum(PQnin)=%d u16XRuleNum(header)=%d \n",stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16XRuleNum,E_XRULE_MM_DS);
        return FALSE;
    }

    //Set u8XRuleIP
    u8XRuleIP = stXCMMDSXruleInfo->u8FrameCount;
    u8XRuleOPStart = u8XRuleIP +E_DS_XRULE_FRAME_NUM;
    u8XRuleUCMStart = u8XRuleOPStart +E_DS_XRULE_FRAME_NUM;
#if (defined(PQ_MM_DS_RULE_H_SD_I_MAIN_EX) && defined(PQ_MM_DS_RULE_H_FHD_I_MAIN_EX) && defined(PQ_MM_DS_RULE_4K_1K_I_MAIN_EX))
    ePQEngineType = PQ_XC0_EXT;
    u16XRuleType = (MS_U16)E_XRULE_EX_MM_DS;
    if(stXCMMDSXruleInfo->u32H_Size > PQ_4K2K_XRULE_DS_H_MINSIZE) // For H=4096 case
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_H_FHD_P_Main_Ex: PQ_MM_DS_rule_H_FHD_I_Main_Ex;
    }
    else if(stXCMMDSXruleInfo->u32H_Size == PQ_4K2K_XRULE_DS_H_MINSIZE) // 4k
    {
        if(stXCMMDSXruleInfo->u32V_Size == PQ_4K2K_XRULE_DS_V_MINSIZE) // 4k2k
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_4K_P_Main_Ex: PQ_MM_DS_rule_4K_I_Main_Ex;
        }
        else // 4k1k
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_4K_1K_P_Main_Ex: PQ_MM_DS_rule_4K_1K_I_Main_Ex;
        }
    }
    else if((stXCMMDSXruleInfo->u32H_Size > PQ_FHD_XRULE_DS_H_MAXSIZE) && (stXCMMDSXruleInfo->u32H_Size < PQ_4K2K_XRULE_DS_H_MINSIZE)) // H_FHD
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_H_FHD_P_Main_Ex: PQ_MM_DS_rule_H_FHD_I_Main_Ex;
    }
    else if(stXCMMDSXruleInfo->u32H_Size <= PQ_FHD_XRULE_DS_H_MAXSIZE)
    {
        if(stXCMMDSXruleInfo->u32V_Size > PQ_FHD_XRULE_DS_V_MAXSIZE) // H_FHD
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_H_FHD_P_Main_Ex: PQ_MM_DS_rule_H_FHD_I_Main_Ex;
        }
        else if((stXCMMDSXruleInfo->u32V_Size >= PQ_FHD_XRULE_DS_V_MINSIZE) && (stXCMMDSXruleInfo->u32V_Size <= PQ_FHD_XRULE_DS_V_MAXSIZE)) // FHD
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_FHD_P_Main_Ex: PQ_MM_DS_rule_FHD_I_Main_Ex;
        }
        else if((stXCMMDSXruleInfo->u32V_Size >= PQ_HSD_XRULE_DS_V_MINSIZE) && (stXCMMDSXruleInfo->u32V_Size < PQ_FHD_XRULE_DS_V_MINSIZE)) // H_SD
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_H_SD_P_Main_Ex: PQ_MM_DS_rule_H_SD_I_Main_Ex;
        }
        else if((stXCMMDSXruleInfo->u32V_Size >= PQ_SD_XRULE_DS_V_MINSIZE) && (stXCMMDSXruleInfo->u32V_Size < PQ_HSD_XRULE_DS_V_MINSIZE)) // SD
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_SD_P_Main_Ex: PQ_MM_DS_rule_SD_I_Main_Ex;
        }
        else // L_SD
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_L_SD_P_Main_Ex: PQ_MM_DS_rule_L_SD_I_Main_Ex;
        }
    }
#elif defined(PQ_MM_DS_RULE_4K_P_MAIN_EX)
    //Get u8XRuleIdx
    ePQEngineType = PQ_XC0_EXT;
    u16XRuleType = (MS_U16)E_XRULE_EX_MM_DS;
    if(stXCMMDSXruleInfo->u32V_Size >=2160) //4//4k2k
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_4K_P_Main_Ex: PQ_MM_DS_rule_4K_I_Main_Ex;
    }
    else if( (stXCMMDSXruleInfo->u32V_Size < 2160) && (stXCMMDSXruleInfo->u32V_Size >= 720) ) //FHD
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_FHD_P_Main_Ex: PQ_MM_DS_rule_FHD_I_Main_Ex;
    }
    else if( (stXCMMDSXruleInfo->u32V_Size < 720) && (stXCMMDSXruleInfo->u32V_Size >= 480) ) //SD
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_SD_P_Main_Ex: PQ_MM_DS_rule_SD_I_Main_Ex;
    }
    else if( (stXCMMDSXruleInfo->u32V_Size < 480) && (stXCMMDSXruleInfo->u32V_Size >= 0) ) //LSD
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_L_SD_P_Main_Ex: PQ_MM_DS_rule_L_SD_I_Main_Ex;
    }
#elif (defined(PQ_MM_DS_RULE_H_SD_I_MAIN) && defined(PQ_MM_DS_RULE_H_FHD_I_MAIN) && defined(PQ_MM_DS_RULE_4K_1K_I_MAIN))
    if(stXCMMDSXruleInfo->u32H_Size > PQ_4K2K_XRULE_DS_H_MINSIZE) // For H=4096 case
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_H_FHD_P_Main : PQ_MM_DS_rule_H_FHD_I_Main;
    }
    else if(stXCMMDSXruleInfo->u32H_Size == PQ_4K2K_XRULE_DS_H_MINSIZE) // 4k
    {
        if(stXCMMDSXruleInfo->u32V_Size == PQ_4K2K_XRULE_DS_V_MINSIZE) // 4k2k
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_4K_P_Main : PQ_MM_DS_rule_4K_I_Main;
        }
        else // 4k1k
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_4K_1K_P_Main : PQ_MM_DS_rule_4K_1K_I_Main;
        }
    }
    else if((stXCMMDSXruleInfo->u32H_Size > PQ_FHD_XRULE_DS_H_MAXSIZE) && (stXCMMDSXruleInfo->u32H_Size < PQ_4K2K_XRULE_DS_H_MINSIZE)) // H_FHD
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_H_FHD_P_Main : PQ_MM_DS_rule_H_FHD_I_Main;
    }
    else if(stXCMMDSXruleInfo->u32H_Size <= PQ_FHD_XRULE_DS_H_MAXSIZE)
    {
        if(stXCMMDSXruleInfo->u32V_Size > PQ_FHD_XRULE_DS_V_MAXSIZE) // H_FHD
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_H_FHD_P_Main : PQ_MM_DS_rule_H_FHD_I_Main;
        }
        else if((stXCMMDSXruleInfo->u32V_Size >= PQ_FHD_XRULE_DS_V_MINSIZE) && (stXCMMDSXruleInfo->u32V_Size <= PQ_FHD_XRULE_DS_V_MAXSIZE)) // FHD
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_FHD_P_Main : PQ_MM_DS_rule_FHD_I_Main;
        }
        else if((stXCMMDSXruleInfo->u32V_Size >= PQ_HSD_XRULE_DS_V_MINSIZE) && (stXCMMDSXruleInfo->u32V_Size < PQ_FHD_XRULE_DS_V_MINSIZE)) // H_SD
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_H_SD_P_Main : PQ_MM_DS_rule_H_SD_I_Main;
        }
        else if((stXCMMDSXruleInfo->u32V_Size >= PQ_SD_XRULE_DS_V_MINSIZE) && (stXCMMDSXruleInfo->u32V_Size < PQ_HSD_XRULE_DS_V_MINSIZE)) // SD
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_SD_P_Main : PQ_MM_DS_rule_SD_I_Main;
        }
        else // L_SD
        {
            u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
                ? PQ_MM_DS_rule_L_SD_P_Main : PQ_MM_DS_rule_L_SD_I_Main;
        }
    }
#else
    //Get u8XRuleIdx
    if(stXCMMDSXruleInfo->u32V_Size >=2160) //4//4k2k
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_4K_P_Main : PQ_MM_DS_rule_4K_I_Main;
    }
    else if( (stXCMMDSXruleInfo->u32V_Size < 2160) && (stXCMMDSXruleInfo->u32V_Size >= 720) ) //FHD
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_FHD_P_Main : PQ_MM_DS_rule_FHD_I_Main;
    }
    else if( (stXCMMDSXruleInfo->u32V_Size < 720) && (stXCMMDSXruleInfo->u32V_Size >= 480) ) //SD
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_SD_P_Main : PQ_MM_DS_rule_SD_I_Main;
    }
    else if( (stXCMMDSXruleInfo->u32V_Size < 480) && (stXCMMDSXruleInfo->u32V_Size >= 0) ) //LSD
    {
        u8XRuleIdx = ((stXCMMDSXruleInfo->bInterlace == 0)||(stXCMMDSXruleInfo->bForceDIOff == TRUE))
            ? PQ_MM_DS_rule_L_SD_P_Main : PQ_MM_DS_rule_L_SD_I_Main;
    }
#endif
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"MM PQ DS u8XRuleIdx is invalid!!\n");
        return FALSE;
    }
    if((MHal_PQ_MMDSXRuleMapping(pInstance,(PQ_WIN)stXCMMDSXruleInfo->enWindow,&u16XRuleType,&ePQEngineType)) == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MHal_PQ_MMDSXRuleMapping Fail\n");
        return FALSE;
    }
    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"u8XRuleIdx=%u, ", (MS_U16)u8XRuleIdx);
    //Get u16IPIdx/u16TabIdx
#if (defined(PQ_SUPPORT_DS_MODULE_IP_OP))
#if(PQ_SUPPORT_DS_MODULE_IP_OP == 1)
    bCheckGRule = (u8XRuleIP==E_DS_XRULE_FRAME_CHECK_GRULE)?TRUE:FALSE;//the second last.

    //DS_IP
    u16IPIdx  = MDrv_PQ_GetXRuleIPIndex(pInstance, stXCMMDSXruleInfo->enWindow, u16XRuleType, u8XRuleIP, ePQEngineType);
    u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, stXCMMDSXruleInfo->enWindow, u16XRuleType, (MS_U16)u8XRuleIdx, (MS_U16)u8XRuleIP, ePQEngineType);
    if(((gbPQBinEnable == TRUE)  && (u16TabIdx == PQ_BIN_IP_NULL)) ||
       ((gbPQBinEnable == FALSE) && (u16TabIdx == PQ_IP_NULL)))
    {
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, stXCMMDSXruleInfo->enWindow, u16IPIdx, ePQEngineType);
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(d)tabidx=%u\n", u16TabIdx);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(e)tabidx=%u\n", u16TabIdx);
    }
    if(u8XRuleIP!=E_DS_XRULE_FRAME_CLEAN_CMD)
    {
        MDrv_PQBin_DS_GetTable(pInstance,stXCMMDSXruleInfo->enWindow,u16TabIdx,u16IPIdx, ePQEngineType,E_DS_MODULE_IP,bCheckGRule);
    }

    //DS_OP
    u16IPIdx  = MDrv_PQ_GetXRuleIPIndex(pInstance, stXCMMDSXruleInfo->enWindow, u16XRuleType, u8XRuleOPStart, ePQEngineType);
    u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, stXCMMDSXruleInfo->enWindow, u16XRuleType, (MS_U16)u8XRuleIdx, (MS_U16)u8XRuleOPStart, ePQEngineType);
    if(((gbPQBinEnable == TRUE)  && (u16TabIdx == PQ_BIN_IP_NULL)) ||
       ((gbPQBinEnable == FALSE) && (u16TabIdx == PQ_IP_NULL)))
    {
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, stXCMMDSXruleInfo->enWindow, u16IPIdx, ePQEngineType);
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(d)tabidx=%u\n", u16TabIdx);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(e)tabidx=%u\n", u16TabIdx);
    }
    if(u8XRuleIP!=E_DS_XRULE_FRAME_CLEAN_CMD)
    {
        MDrv_PQBin_DS_GetTable(pInstance,stXCMMDSXruleInfo->enWindow,u16TabIdx,u16IPIdx, ePQEngineType,E_DS_MODULE_OP,bCheckGRule);
    }

#if (PQ_SUPPORT_DS_MODULE_UCM)
    //DS_UCM
    u16IPIdx  = MDrv_PQ_GetXRuleIPIndex(pInstance, stXCMMDSXruleInfo->enWindow, u16XRuleType, u8XRuleUCMStart, ePQEngineType);
    u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, stXCMMDSXruleInfo->enWindow, u16XRuleType, (MS_U16)u8XRuleIdx, (MS_U16)u8XRuleUCMStart, ePQEngineType);
    if(((gbPQBinEnable == TRUE)  && (u16TabIdx == PQ_BIN_IP_NULL)) ||
       ((gbPQBinEnable == FALSE) && (u16TabIdx == PQ_IP_NULL)))
    {
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, stXCMMDSXruleInfo->enWindow, u16IPIdx, ePQEngineType);
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(d)tabidx=%u\n", u16TabIdx);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(e)tabidx=%u\n", u16TabIdx);
    }
    if(u8XRuleIP!=E_DS_XRULE_FRAME_CLEAN_CMD)
    {
        MDrv_PQBin_DS_GetTable(pInstance,stXCMMDSXruleInfo->enWindow,u16TabIdx,u16IPIdx, ePQEngineType,E_DS_MODULE_UCMR,bCheckGRule);
    }
#endif
#else
    u16IPIdx  = MDrv_PQ_GetXRuleIPIndex(pInstance, stXCMMDSXruleInfo->enWindow, u16XRuleType, u8XRuleIP, ePQEngineType);
    u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, stXCMMDSXruleInfo->enWindow, u16XRuleType, (MS_U16)u8XRuleIdx, (MS_U16)u8XRuleIP, ePQEngineType);
    if(((gbPQBinEnable == TRUE) && (u16TabIdx == PQ_BIN_IP_NULL)) ||
       ((gbPQBinEnable == FALSE) && (u16TabIdx == PQ_IP_NULL)))
    {
        u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, stXCMMDSXruleInfo->enWindow, u16IPIdx, ePQEngineType);
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(d)tabidx=%u\n", u16TabIdx);
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"(e)tabidx=%u\n", u16TabIdx);
    }
    //Get PQBin table data
    MDrv_PQBin_DS_GetTable(pInstance,stXCMMDSXruleInfo->enWindow, u16TabIdx, u16IPIdx, ePQEngineType, E_DS_MODULE_OP,bCheckGRule);  //Only Main has X rule
#endif
#endif
    return TRUE;
#endif
    return FALSE;
}
static MS_BOOL _MDrv_PQ_LoadSPFDSXruleTable(void* pInstance,ST_PQ_DS_XRULE_INFO* pstXCMMDSXruleInfo)
{
#if (PQ_XRULE_SPF_DS_ENABLE == 1)
    if(gbIsSupportSPFLB == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"gbIsSupportSPFLB == FALSE\n");
        return FALSE;
    }
    if(pstXCMMDSXruleInfo == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"stXCMMDSXruleInfo == NULL\n");
        return FALSE;
    }
    if(pInstance == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pInstance == NULL\n");
        return FALSE;
    }
    MS_U16 u16IPIdx = 0;
    MS_U16 u16TabIdx = (gbPQBinEnable == FALSE) ? PQ_IP_NULL : PQ_BIN_IP_NULL;
    MS_U8 u8XRuleIdx = 0x00;
    EN_XRULE_IP enXRuleType = E_XRULE_SPF_DS;
    MS_U16 u16XRuleIP = 0;
    MS_BOOL bCheckGRule = FALSE;
    PQ_ENGINE_TYPE ePQEngineType = PQ_XC0_STD;
    MS_U16 u16Idx = 0;
    MS_U32 u32SupportZNR = 0;

    MApi_XC_GetChipCaps(E_XC_SUPPORT_ZNR, &u32SupportZNR, sizeof(MS_U32));

    //Error handle
    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"V_Size=%tu, H_Size=%tu, u8FrameCount=%d, bInterlace=%d enHDRType = %d u32XCDSUpdateCondition = %d\n",(ptrdiff_t)(pstXCMMDSXruleInfo->u32V_Size), (ptrdiff_t)(pstXCMMDSXruleInfo->u32H_Size),\
        pstXCMMDSXruleInfo->u8FrameCount,pstXCMMDSXruleInfo->bInterlace,pstXCMMDSXruleInfo->enHDRType,pstXCMMDSXruleInfo->u32XCDSUpdateCondition);
    //check input valid
    if(pstXCMMDSXruleInfo->enWindow != PQ_MAIN_WINDOW)//Only main window support SPFXRule
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"Invalid input:eWindow=%d,V_Size=%tu,H_Size=%tu,u8FrameCount=%d enHDRType = %d u32XCDSUpdateCondition = %d\n",pstXCMMDSXruleInfo->enWindow,(ptrdiff_t)(pstXCMMDSXruleInfo->u32V_Size),\
            (ptrdiff_t)(pstXCMMDSXruleInfo->u32H_Size),pstXCMMDSXruleInfo->u8FrameCount,pstXCMMDSXruleInfo->enHDRType,pstXCMMDSXruleInfo->u32XCDSUpdateCondition);
        return FALSE;
    }
    if(pstXCMMDSXruleInfo->enLoadXruleTableMethod >= E_PQ_LOADXRULETABLE_MAX)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"Invalid input enLoadXruleTableMethod=%d\n",pstXCMMDSXruleInfo->enLoadXruleTableMethod);
        return FALSE;
    }
    //check PQbin support Xrule_SPF_DS
    if(stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16XRuleNum <= E_XRULE_SPF_DS)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"PQbin and header file is mismatch ,Please update PQbin!   u16XRuleNum(PQnin)=%d u16XRuleNum(header)=%d \n",stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16XRuleNum,E_XRULE_SPF_DS);
        return FALSE;
    }

    //u8XRuleIdx
    if(pstXCMMDSXruleInfo->enHDRType == E_XC_HDR_TYPE_DOLBY) // dolby case
    {
        if(pstXCMMDSXruleInfo->u32H_Size > PQ_XRULE_SPF_DS_4K2K_H_MIN)
        {
            u8XRuleIdx = PQ_DS_LineBuffer_Dolby_4K_Main;//Dolby 4K
        }
        else
        {
            u8XRuleIdx = PQ_DS_LineBuffer_Dolby_Below4K_Main;//Dolby bellow 4K
        }
    }
    else if(pstXCMMDSXruleInfo->u32H_Size > PQ_XRULE_SPF_DS_4K2K_H_MIN) // 4k
    {
        u8XRuleIdx = PQ_DS_LineBuffer_4K_Main;
    }
    else //below 4k
    {
        u8XRuleIdx = PQ_DS_LineBuffer_Below4K_Main;
    }
    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"u8XRuleIdx =%d\n",(u8XRuleIdx));

    switch(pstXCMMDSXruleInfo->enLoadXruleTableMethod)
    {
        case E_PQ_LOADXRULETABLE_GENERAL:
        {
            for(u16Idx = 0;u16Idx < PQ_DS_LineBuffer_IP_NUM_Main;u16Idx ++)
            {
                u16XRuleIP = u16Idx;
                u16IPIdx  = MDrv_PQ_GetXRuleIPIndex(pInstance, pstXCMMDSXruleInfo->enWindow, enXRuleType, u16XRuleIP, ePQEngineType);
                u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, pstXCMMDSXruleInfo->enWindow, (MS_U16)enXRuleType, (MS_U16)u8XRuleIdx, (MS_U16)u16XRuleIP, ePQEngineType);
                if(IS_PQ_TABLEIDX_NULL(u16TabIdx))
                {
                    u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, pstXCMMDSXruleInfo->enWindow, u16IPIdx, ePQEngineType);
                    PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"MDrv_PQ_GetXRuleTableIndex retrun NULL TabIdx load back to Main rule, XRuleIdx=%u, tabidx=%u\n", u16XRuleIP, u16TabIdx);
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_PQTAB,"XRule Table Index got, XRuleIP =%u, u16IPIdx =%u, tabidx=%u\n", u16XRuleIP, u16IPIdx, u16TabIdx);
                }
                MDrv_PQ_LoadTable_U2(pInstance, pstXCMMDSXruleInfo->enWindow, u16TabIdx, u16IPIdx);
            }
            break;
        }
        case E_PQ_LOADXRULETABLE_DS:
        {
            //Get u16IPIdx/u16TabIdx
#if (defined(PQ_SUPPORT_DS_MODULE_IP_OP))
#if(PQ_SUPPORT_DS_MODULE_IP_OP == 1)
            for(u16Idx = 0;u16Idx < PQ_DS_LineBuffer_IP_NUM_Main;u16Idx ++)
            {
                //DS_IP
                u16XRuleIP = u16Idx;
                u16IPIdx  = MDrv_PQ_GetXRuleIPIndex(pInstance, pstXCMMDSXruleInfo->enWindow, enXRuleType, u16XRuleIP, ePQEngineType);
                u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, pstXCMMDSXruleInfo->enWindow, (MS_U16)enXRuleType, (MS_U16)u8XRuleIdx, (MS_U16)u16XRuleIP, ePQEngineType);
                if(IS_PQ_TABLEIDX_NULL(u16TabIdx))
                {
                    u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, pstXCMMDSXruleInfo->enWindow, u16IPIdx, ePQEngineType);
                    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"MDrv_PQ_GetXRuleTableIndex retrun NULL TabIdx load back to Main rule, XRuleIdx=%u, tabidx=%u\n", u16XRuleIP, u16TabIdx);
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"XRule Table Index got, XRuleIP =%u, u16IPIdx =%u, tabidx=%u\n", u16XRuleIP, u16IPIdx, u16TabIdx);
                }

                if(u16Idx <PQ_SPF_XRULE_OP_START_INDEX)
                {
                    //IP
                    MDrv_PQBin_DS_GetTable(pInstance,pstXCMMDSXruleInfo->enWindow,u16TabIdx,u16IPIdx, ePQEngineType,E_DS_MODULE_IP,bCheckGRule);
                }
                else if(u16Idx < PQ_SPF_XRULE_UCM_START_INDEX)
                {
                    //OP
                    MDrv_PQBin_DS_GetTable(pInstance,pstXCMMDSXruleInfo->enWindow,u16TabIdx,u16IPIdx, ePQEngineType,E_DS_MODULE_OP,bCheckGRule);
                }
                else
                {
                    //UCM
                    if(u32SupportZNR != 0)
                    {
                        MDrv_PQBin_DS_GetTable(pInstance,pstXCMMDSXruleInfo->enWindow,u16TabIdx,u16IPIdx, ePQEngineType,E_DS_MODULE_UCMR,bCheckGRule);
                    }
                    else //If dont support ZNR, LineBufferShareControl_SC needs to dump in op domain
                    {
                        MDrv_PQBin_DS_GetTable(pInstance,pstXCMMDSXruleInfo->enWindow,u16TabIdx,u16IPIdx, ePQEngineType,E_DS_MODULE_OP,bCheckGRule);
                    }
                }
            }
#else
            for(u16Idx = 0;u16Idx < MDrv_PQ_GetXRuleIPNum(pInstance, pstXCMMDSXruleInfo->enWindow, (MS_U16)enXRuleType);u16Idx++)
            {
                //Set u8XRuleIP
                u16XRuleIP = u16Idx;
                u16IPIdx  = MDrv_PQ_GetXRuleIPIndex(pInstance, pstXCMMDSXruleInfo->enWindow,  (MS_U16)enXRuleType, u16XRuleIP, ePQEngineType);
                u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, pstXCMMDSXruleInfo->enWindow, (MS_U16)enXRuleType, (MS_U16)u8XRuleIdx, (MS_U16)u16XRuleIP, ePQEngineType);
                if(IS_PQ_TABLEIDX_NULL(u16TabIdx))
                {
                    u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, pstXCMMDSXruleInfo->enWindow, u16IPIdx, ePQEngineType);
                    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"MDrv_PQ_GetXRuleTableIndex retrun NULL TabIdx load back to Main rule, XRuleIdx=%u, tabidx=%u\n", u16XRuleIP, u16TabIdx);
                }
                else
                {
                    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"XRule Table Index got, XRuleIP =%u, u16IPIdx =%u, tabidx=%u\n", u16XRuleIP, u16IPIdx, u16TabIdx);
                }
                //Get PQBin table data
                MDrv_PQBin_DS_GetTable(pInstance,pstXCMMDSXruleInfo->enWindow, u16TabIdx, u16IPIdx, ePQEngineType, E_DS_MODULE_OP,bCheckGRule);  //Only Main has X rule
            }
#endif
#endif
            break;
        }
        default:
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"enLoadXruleTableMethod Not support!\n");
            return FALSE;
        }
    }

    return TRUE;
#endif
    return FALSE;
}

static MS_U16 _MDrv_PQ_Get_HVSPXRuleIPIndex(ST_PQ_DS_XRULE_INFO* pstXCMMDSXruleInfo, MS_U32 u32HScalingRatio, MS_U32 u32VScalingRatio)
{
    MS_U16 u16HVSPVSizeCond = MHal_PQ_GetHSVPHSizeCondition();
    if(pstXCMMDSXruleInfo == NULL)
    {
        UNUSED(u32HScalingRatio);
        UNUSED(u32VScalingRatio);
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pstXCMMDSXruleInfo is NULL!! \n");
        return PQ_IP_NULL;
    }

    EN_PQ_HVSP_DS_SETTING_IP enSettingIP = E_PQ_HVSP_DS_1920_HDOWN_VDOWN;
    if(pstXCMMDSXruleInfo->enWindow == PQ_MAIN_WINDOW)
    {
        if((pstXCMMDSXruleInfo->u32H_Size <= u16HVSPVSizeCond) && (u32HScalingRatio < PQ_RATIO_1X))
        {
            if(u32VScalingRatio < PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920_HDOWN_VDOWN;
            }
            else if(u32VScalingRatio == PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920_HDOWN_V1X;
            }
            else
            {
                enSettingIP = E_PQ_HVSP_DS_1920_HDOWN_VUP;
            }
        }
        else if((pstXCMMDSXruleInfo->u32H_Size <= u16HVSPVSizeCond) && (u32HScalingRatio == PQ_RATIO_1X))
        {
            if(u32VScalingRatio < PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920_H1X_VDOWN;
            }
            else if(u32VScalingRatio == PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920_H1X_V1X;
            }
            else
            {
                enSettingIP = E_PQ_HVSP_DS_1920_H1X_VUP;
            }
        }
        else if((pstXCMMDSXruleInfo->u32H_Size <= u16HVSPVSizeCond) && (u32HScalingRatio > PQ_RATIO_1X))
        {
            if(u32VScalingRatio < PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920_HUP_VDOWN;
            }
            else if(u32VScalingRatio == PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920_HUP_V1X;
            }
            else
            {
                enSettingIP = E_PQ_HVSP_DS_1920_HUP_VUP;
            }
        }
        else if((pstXCMMDSXruleInfo->u32H_Size > u16HVSPVSizeCond) && (u32HScalingRatio < PQ_RATIO_1X))
        {
            if(u32VScalingRatio < PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920UP_HDOWN_VDOWN;
            }
            else if(u32VScalingRatio == PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920UP_HDOWN_V1X;
            }
            else
            {
                enSettingIP = E_PQ_HVSP_DS_1920UP_HDOWN_VUP;
            }
        }
        else if((pstXCMMDSXruleInfo->u32H_Size > u16HVSPVSizeCond) && (u32HScalingRatio == PQ_RATIO_1X))
        {
            if(u32VScalingRatio < PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920UP_H1X_VDOWN;
            }
            else if(u32VScalingRatio == PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920UP_H1X_V1X;
            }
            else
            {
                enSettingIP = E_PQ_HVSP_DS_1920UP_H1X_VUP;
            }
        }
        else if((pstXCMMDSXruleInfo->u32H_Size > u16HVSPVSizeCond) && (u32HScalingRatio > PQ_RATIO_1X))
        {
            if(u32VScalingRatio < PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920UP_HUP_VDOWN;
            }
            else if(u32VScalingRatio == PQ_RATIO_1X)
            {
                enSettingIP = E_PQ_HVSP_DS_1920UP_HUP_V1X;
            }
            else
            {
                enSettingIP = E_PQ_HVSP_DS_1920UP_HUP_VUP;
            }
        }
        else
        {
            enSettingIP = E_PQ_HVSP_DS_NUM;
        }
    }

    return MHal_PQ_Get_HVSPXRuleIPIndex(pstXCMMDSXruleInfo->enWindow, enSettingIP);
}

MS_BOOL _MDrv_PQ_LoadHVSPDSXruleTable(void* pInstance,ST_PQ_DS_XRULE_INFO* pstXCMMDSXruleInfo)
{
    PQ_NULL_POINTER(pInstance);
    PQ_NULL_POINTER(pstXCMMDSXruleInfo);

    MS_U16 u16IPIdx = 0;
    MS_U16 u16TabIdx = (gbPQBinEnable == FALSE) ? PQ_IP_NULL : PQ_BIN_IP_NULL;
    MS_U16 u16XRuleIdx = 0;
    MS_U16 u16XRuleType = MHal_PQ_Get_XRuleIPType(pstXCMMDSXruleInfo->enWindow, E_PQ_XRULE_IP_HVSP_DS);
    MS_U16 u16XRuleIP = 0;
    MS_BOOL bCheckGRule = FALSE;
    PQ_ENGINE_TYPE ePQEngineType = PQ_XC0_STD;
    MS_U32 u32HScalingRatio = 0;
    MS_U32 u32VScalingRatio = 0;

    //Error handle
    PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"Window: %tu, CropH:%tu, CropV:%tu, DispH:%tu, DispV:%tu, FrameCount:%u, bInterlace:%u \n",
                    (ptrdiff_t)(pstXCMMDSXruleInfo->enWindow),
                    (ptrdiff_t)(pstXCMMDSXruleInfo->u32CropWin_Width), (ptrdiff_t)(pstXCMMDSXruleInfo->u32CropWin_Height),
                    (ptrdiff_t)(pstXCMMDSXruleInfo->u32DispWin_Width), (ptrdiff_t)(pstXCMMDSXruleInfo->u32DispWin_Height),
                     pstXCMMDSXruleInfo->u8FrameCount,pstXCMMDSXruleInfo->bInterlace);
    //check input valid
    if(pstXCMMDSXruleInfo->enWindow >= PQ_MAX_WINDOW)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"Invalid Window: eWindow:%u \n", pstXCMMDSXruleInfo->enWindow);
        return FALSE;
    }
    //check PQbin support Xrule HVSP DS
    if(stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16XRuleNum <= MHal_PQ_Get_XRuleIPType(pstXCMMDSXruleInfo->enWindow, E_PQ_XRULE_IP_HVSP_DS))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"PQbin and header file is mismatch ,Please update PQbin!   u16XRuleNum(PQnin)=%d u16XRuleNum(header)=%d \n",
                        stPQBinHeaderInfo[PQ_BIN_STD_MAIN].u16XRuleNum, MHal_PQ_Get_XRuleIPType(pstXCMMDSXruleInfo->enWindow, E_PQ_XRULE_IP_HVSP_DS));
        return FALSE;
    }

    if(pstXCMMDSXruleInfo->u32CropWin_Height != 0)
    {
        u32VScalingRatio = ((MS_U32)(pstXCMMDSXruleInfo->u32DispWin_Height) * 1000) / pstXCMMDSXruleInfo->u32CropWin_Height;
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"CropWin_Height from DMS is 0!! \n");
        return FALSE;
    }

    if(pstXCMMDSXruleInfo->u32CropWin_Width != 0)
    {
        u32HScalingRatio = ((MS_U32)(pstXCMMDSXruleInfo->u32DispWin_Width) * 1000) / pstXCMMDSXruleInfo->u32CropWin_Width;
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"CropWin_Width from DMS is 0!! \n");
        return FALSE;
    }

    u16XRuleIdx = _MDrv_PQ_Get_HVSPXRuleIPIndex(pstXCMMDSXruleInfo, u32HScalingRatio, u32VScalingRatio);
    PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"[PQ] CropV:%u, CropH:%u, DispV:%u, DispH:%u, HRatio:%u, VRatio:%u, XRuleIdx:%u \n",
                    pstXCMMDSXruleInfo->u32CropWin_Height, pstXCMMDSXruleInfo->u32CropWin_Width,
                    pstXCMMDSXruleInfo->u32DispWin_Height, pstXCMMDSXruleInfo->u32DispWin_Width,
                    u32HScalingRatio, u32VScalingRatio, u16XRuleIdx);

    if(u16XRuleIdx != PQ_IP_NULL)
    {
        for(u16XRuleIP = 0; u16XRuleIP < MDrv_PQ_GetXRuleIPNum(pInstance, pstXCMMDSXruleInfo->enWindow, u16XRuleType); u16XRuleIP++)
        {
            u16IPIdx  = MDrv_PQ_GetXRuleIPIndex(pInstance, pstXCMMDSXruleInfo->enWindow, u16XRuleType, u16XRuleIP, ePQEngineType);
            u16TabIdx = MDrv_PQ_GetXRuleTableIndex(pInstance, pstXCMMDSXruleInfo->enWindow, u16XRuleType, u16XRuleIdx, u16XRuleIP, ePQEngineType);

            if(u16TabIdx == PQ_BIN_IP_NULL)
            {
                u16TabIdx = MDrv_PQ_GetTableIndex_Ex_U2(pInstance, pstXCMMDSXruleInfo->enWindow, u16IPIdx, ePQEngineType);
                PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"[PQ] TabIdx: %u\n", u16TabIdx);
            }
            else
            {
                PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"[PQ] TableIdx: %u\n", u16TabIdx);
            }
            MDrv_PQBin_DS_GetTable(pInstance,pstXCMMDSXruleInfo->enWindow, u16TabIdx, u16IPIdx, ePQEngineType, E_DS_MODULE_OP,bCheckGRule);
        }
    }
    else
    {
        PQ_LEVEL_PRINTF(PQ_DBG_HVSP,"[PQ] XRuleIdx is 0xFFFF, TableIdx is 0xFFFF!!\n");
        return FALSE;
    }
    return TRUE;
}

static MS_BOOL _MDrv_PQ_SetQMAPXruleData(void* pInstance, PQ_WIN enWindow, EN_PQ_QMAP_TYPE enQmapType)
{
#if (PQ_XRULE_SPF_DS_ENABLE == 1)
    if(gbIsSupportSPFLB == FALSE)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_INFO,"gbIsSupportSPFLB == FALSE\n");
        return FALSE;
    }
    ST_XC_PQ_QMAPDATA stqmapdata;
    MS_U16 u16row =0, u16column = 0;
    MS_PQBin_IP_Table_Info stTableInfo;
    MS_U16 u16IPIdx=0;
    MS_PHYADDR u32Addr;
    MS_U32 u32tabsize = 0;
    MS_U32 u32tabcnt = 0;
    MS_U32 u32tabalign = 0;
    MS_U8 u8BinBankSize = 0;
    EN_XRULE_IP enXRuleType = E_XRULE_NUM;
    memset(&stqmapdata, 0, sizeof(ST_XC_PQ_QMAPDATA));
    //decide Xrule type
    switch(enQmapType)
    {
        case E_PQ_SPF_XRULE:
        {
            enXRuleType = E_XRULE_SPF_DS;
            stqmapdata.u32Quality_InputType_Num = PQ_DS_LineBuffer_NUM_Main;
            stqmapdata.u32Quality_IP_Num = PQ_DS_LineBuffer_IP_NUM_Main;
            stqmapdata.u32Table_IP_Num = XC_PQ_IP_INFORMATION_SIZE;
            break;
        }
        default:
        {
            PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"enQmapType Not support!\n");
            return FALSE;
        }
    }
    if((stqmapdata.u32Quality_InputType_Num == 0)||(stqmapdata.u32Quality_IP_Num ==0)||(stqmapdata.u32Table_IP_Num ==0))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"stqmapdata parameter error!\n");
        return FALSE;
    }
    PQ_MALLOC_MEM(stqmapdata.pu16Quality_Map_Array, (stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
    PQ_MALLOC_MEM(stqmapdata.pu16Table_Map_Array, (stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));

    if((stqmapdata.pu16Quality_Map_Array == NULL) || (stqmapdata.pu16Table_Map_Array == NULL))
    {
        PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
        PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));
        return FALSE;
    }
    //Get Quality map (table idx)
    //All Xrule idx (input type)
    for (u16column = 0; u16column < stqmapdata.u32Quality_InputType_Num; u16column++)
    {
        for (u16row = 0; u16row < stqmapdata.u32Quality_IP_Num; u16row++)
        {
            stqmapdata.pu16Quality_Map_Array[(u16column * stqmapdata.u32Quality_IP_Num +u16row)] = MDrv_PQ_GetXRuleTableIndex(pInstance, enWindow, enXRuleType, u16column,u16row, PQ_XC0_STD);
        }
    }
    //Get table Map array (talbe info)
    for (u16column = 0; u16column < stqmapdata.u32Quality_IP_Num; u16column++)
    {
        u16IPIdx = MDrv_PQ_GetXRuleIPIndex(pInstance, enWindow, enXRuleType, u16column, PQ_XC0_STD);
        MDrv_PQBin_GetTable(pInstance,
                            MDrv_PQBin_GetPanelIdx(pInstance),
                            0,
                            u16IPIdx,
                            &stTableInfo,
                            &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        u8BinBankSize = (MS_U8)((stTableInfo.u16TableType == PQ_TABTYPE_TWOBYTE_BANK)? PQ_BIN_BANK_SIZE_2BYTE: PQ_BIN_BANK_SIZE);
        stqmapdata.pu16Table_Map_Array[(u16column * stqmapdata.u32Table_IP_Num)] = stTableInfo.u16GroupNum;
        stqmapdata.pu16Table_Map_Array[(u16column * stqmapdata.u32Table_IP_Num + 1)] = stTableInfo.u16RegNum;
        if(stqmapdata.u32Table_IP_Num > (MS_U32)E_KDRV_XC_CFD_QMAPDATA_INFO_TABLE_TYPE_ALIENMENT)
        {
            MS_U16 u16PqTabType = (MS_U16)_MDrv_PQ_ConvertPqTabtype(stTableInfo.u16TableType);

            stqmapdata.pu16Table_Map_Array[(u16column * stqmapdata.u32Table_IP_Num + E_KDRV_XC_CFD_QMAPDATA_INFO_TABLE_TYPE_ALIENMENT)] = u16PqTabType;
        }

        stqmapdata.u32Table_Data_Size = stqmapdata.u32Table_Data_Size + ( u8BinBankSize + PQ_BIN_ADDR_SIZE + PQ_BIN_MASK_SIZE + (MS_U32)stTableInfo.u16GroupNum) * stTableInfo.u16RegNum;
    }
    //Would cause Malloc fail!
    if(stqmapdata.u32Table_Data_Size == 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"stqmapdata.u32Table_Data_Size == 0\n");
        return FALSE;
    }
    PQ_MALLOC_MEM(stqmapdata.pu8Table_Data_Array, (stqmapdata.u32Table_Data_Size * sizeof(MS_U8)));

    if(stqmapdata.pu8Table_Data_Array == NULL)
    {
        PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
        PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));

        return FALSE;
    }
    //Get data
    for (u16column = 0; u16column < stqmapdata.u32Quality_IP_Num; u16column++)
    {
        u16IPIdx = MDrv_PQ_GetXRuleIPIndex(pInstance, enWindow, enXRuleType, u16column, PQ_XC0_STD);
        MDrv_PQBin_GetTable(pInstance,
                            MDrv_PQBin_GetPanelIdx(pInstance),
                            0,
                            u16IPIdx,
                            &stTableInfo,
                            &stPQBinHeaderInfo[PQ_BIN_STD_MAIN]);
        u8BinBankSize = (MS_U8)((stTableInfo.u16TableType == PQ_TABTYPE_TWOBYTE_BANK)? PQ_BIN_BANK_SIZE_2BYTE: PQ_BIN_BANK_SIZE);
        u32Addr = stTableInfo.u32TabOffset;
        u32tabsize = ((MS_U32)u8BinBankSize + PQ_BIN_ADDR_SIZE + PQ_BIN_MASK_SIZE + (MS_U32)stTableInfo.u16GroupNum) * stTableInfo.u16RegNum;
        for (u32tabcnt = 0; u32tabcnt < u32tabsize; u32tabcnt++)
        {
            stqmapdata.pu8Table_Data_Array[(u32tabcnt + u32tabalign)] = *((MS_U8 *)(u32Addr + u32tabcnt));
        }
        u32tabalign = u32tabalign + u32tabsize;
    }

    stqmapdata.u32Version = PQ_QMAPDATA_VERSION;
    stqmapdata.u16Length = sizeof(ST_XC_PQ_QMAPDATA);
#if (PQ_QMAPDATA_VERSION >= 2)
    stqmapdata.eQmapType = enQmapType;
#endif

#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION >= 2)
#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_QMAPDATA;
    stCfdCtrlInfo.pParam = &stqmapdata;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_PQ_QMAPDATA);

    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Scaler device not opened!!!!\n");
        }
    }

    if (ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stCfdCtrlInfo))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDRV_XC_IOC_CFDCONCTRL UPDATE GRULE DATA fail!!!!\n");
    }

    if (stCfdCtrlInfo.u16ErrCode != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDRV_XC_IOC_CFDCONCTRL UPDATE GRULE DATA fail, errCode: %d\n", stCfdCtrlInfo.u16ErrCode);
    }
#endif
#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
    XC_CFD_CONTROL_INFO stCfdCtrlInfo;
    memset(&stCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrlInfo.enCtrlType = E_XC_CFD_CTRL_SET_QMAPDATA;
    stCfdCtrlInfo.pParam = &stqmapdata;
    stCfdCtrlInfo.u32ParamLen = sizeof(ST_XC_PQ_QMAPDATA);
    MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrlInfo);
#endif
#endif
#endif

    PQ_FREE_MEM(stqmapdata.pu16Quality_Map_Array,(stqmapdata.u32Quality_InputType_Num * stqmapdata.u32Quality_IP_Num * sizeof(MS_U16)));
    PQ_FREE_MEM(stqmapdata.pu16Table_Map_Array,(stqmapdata.u32Quality_IP_Num * stqmapdata.u32Table_IP_Num * sizeof(MS_U16)));
    PQ_FREE_MEM(stqmapdata.pu8Table_Data_Array,(stqmapdata.u32Table_Data_Size * sizeof(MS_U8)));
    return TRUE;
 #endif
    return FALSE;
}
MS_BOOL MDrv_PQ_SetQMAPXruleData(void* pInstance, PQ_WIN enWindow, EN_PQ_QMAP_TYPE enQmapType)
{
    if(pInstance == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pInstance == NULL\n");
        return FALSE;
    }
#if (PQ_SUPPORT_SETXRULEDATA == 1)
    switch(enQmapType)
    {
        case E_PQ_SPF_XRULE:
        {
#if (PQ_XRULE_SPF_DS_ENABLE == 1)
        if(gbIsSupportSPFLB == TRUE)
        {
            if(_MDrv_PQ_SetQMAPXruleData(pInstance, enWindow, enQmapType) == FALSE)
            {
                PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"_MDrv_PQ_SetQMAPXruleData Fail\n");
                return FALSE;
            }
        }
#endif
            break;
        }
        default :
        {
            PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"Wrong Xrule in SetQMAPGruleData\n");
            break;
        }
    }
    return TRUE;
#else
    PQ_LEVEL_PRINTF(PQ_DBG_SRULE,"not support\n");
    return FALSE;
#endif
}
MS_BOOL MDrv_PQ_SetSPFXruleSetting(ST_KDRV_XC_CFD_SPF_XRULE* pstXCCFDSPFXrule)
{
    MS_BOOL bRet = FALSE;
    if(pstXCCFDSPFXrule == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pstXCCFDSPFXrule == NULL\n");
        return bRet;
    }
    XC_CFD_CONTROL_INFO* pstCfdCtrlInfo = NULL;
    pstCfdCtrlInfo = malloc(sizeof(XC_CFD_CONTROL_INFO));
    if(pstCfdCtrlInfo == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pstCfdCtrlInfo == NULL\n");
        return bRet;
    }
    memset(pstCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    if(pstCfdCtrlInfo == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"pstXCCFDSPFXrule == NULL\n");
        return bRet;
    }
    pstCfdCtrlInfo->enCtrlType = E_XC_CFD_CTRL_SPF_XRULE;
    pstCfdCtrlInfo->pParam = pstXCCFDSPFXrule;
    pstCfdCtrlInfo->u32ParamLen = sizeof(XC_CFD_CONTROL_INFO);
    MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, pstCfdCtrlInfo);
    free(pstCfdCtrlInfo);
    bRet = TRUE;
    return bRet;
}
MS_BOOL MDrv_PQ_SetBWLevel_U2(void* pInstance,ST_PQ_QOS_BW_INFO *pstSetBWInfo)
{
    ST_PQ_QOS_BW_INFO stSetBWInfo;
    MS_U32 u32CopiedLength = sizeof(ST_PQ_QOS_BW_INFO);
    memset(&stSetBWInfo, 0, sizeof(ST_PQ_QOS_BW_INFO));

    VERSION_COMPATIBLE_CHECK(pstSetBWInfo,stSetBWInfo, u32CopiedLength, u32Version, u32Length, ST_PQ_QOS_BW_INFO_VERSION, E_APIXC_RET_FAIL);
    pstSetBWInfo->u32Version = ST_PQ_QOS_BW_INFO_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstSetBWInfo->u32Length = u32CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/

    memcpy(&stSetBWInfo,pstSetBWInfo, u32CopiedLength);

    if(stSetBWInfo.enWindow < PQ_MAX_WINDOW)
    {
        if(stSetBWInfo.enBWDType < PQ_BWD_NUM)
        {
            _eBWDType[stSetBWInfo.enWindow] = stSetBWInfo.enBWDType;
            return MDrv_PQ_SetBwdLevel(stSetBWInfo.enWindow,stSetBWInfo.enBWDType);
        }
        return FALSE;
    }
    else
    {
        return FALSE;
    }
}

MS_BOOL SYMBOL_WEAK MDrv_PQ_SetBWLevel(ST_PQ_QOS_BW_INFO *pstSetBWInfo)
{
    if (pu32PQInst == NULL)
    {
        if(UtopiaOpen(MODULE_PQ , &pu32PQInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"UtopiaOpen PQ failed\n");
            return FALSE;
        }
    }

    stPQ_QOS_BW_INFO PQArgs;
    PQArgs.pstQosBWInfo = pstSetBWInfo;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_SET_BW_LEVEL, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}
MS_BOOL  MDrv_PQ_GetStatusEx_U2(void* pInstance,ST_PQ_StatusEx *pstStatus)
{
    if((NULL == pstStatus) || (pstStatus->u16ApiStatusEX_Length == 0))
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No status existed, please pass  statusEx and length by calling  MDrv_PQ_GetStatusEx first\n");
        return FALSE;
    }
    if(pstStatus->u32ApiStatusEx_Version < ST_PQ_STATUSEX_VERSION)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"MDrv_PQ_GetStatusEx,please please check your u32ApiStatusEx_Version version ,it should set over cur version!!!\n");
        return FALSE;
    }

    memcpy(&_stPQ_StatusEx.stPQ_MuliMedia_Info,&_stMultiMedia_Info,sizeof(MS_PQ_MuliMedia_Info) * PQ_MAX_WINDOW);
    memcpy(&_stPQ_StatusEx.stHDMI_Info,&_stHDMI_Info,sizeof(MS_PQ_Hdmi_Info) * PQ_MAX_WINDOW);

    memcpy((void*)pstStatus, (void*)&_stPQ_StatusEx, sizeof(ST_PQ_StatusEx));
    return TRUE;
}
MS_BOOL  SYMBOL_WEAK MDrv_PQ_GetStatusEx(ST_PQ_StatusEx *pstStatus)
{
    if (pu32PQInst == NULL)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"No instance existed, please get an instance by calling MDrv_PQ_Init() first\n");
        return FALSE;
    }

    stPQ_GetStatusEx PQArgs;
    PQArgs.pstPQStatusEx = pstStatus;
    PQArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32PQInst, E_PQ_CMD_GET_STATUS_EX, (void*)&PQArgs) != 0)
    {
        PQ_LEVEL_PRINTF(PQ_DBG_ERROR,"Obtain PQ engine fail\n");
        return FALSE;
    }
    else
    {
        return PQArgs.bReturnValue;
    }
}

MS_BOOL MDrv_PQ_SetProcfsLevel(EN_PQ_PROCFS_SEL enProcfs)
{
    switch (enProcfs)
    {
        case E_PQ_PROCFS_SEL_ASSERT_PANIC:
        case E_PQ_ASSERT_SEL_ASSERT_DUMPSTACK:
        {
            _u16PQAssertLevel = enProcfs;
            PQ_LEVEL_PRINTF(PQ_DBG_INFO,"EN_PQ_PROCFS_SEL:%d\n", enProcfs);
            break;
        }
        default:
        {
            PQ_LEVEL_PRINTF(PQ_DBG_INFO,"EN_PQ_PROCFS_SEL miss, please check\n");
            break;
        }
    }

    return TRUE;
}
MS_BOOL MDrv_PQ_SetPQInfoToVR(ST_PQ_SET_VR_INFO stPQSetVRInfo)
{
    return MHal_PQ_SetPQInfoToVR(stPQSetVRInfo);
}

MS_BOOL  MDrv_PQ_SetFastPlaybyMemcDbg(MS_BOOL bEnable)
{
    _u16PQProcfsFastPlayByMemc = bEnable;

    return TRUE;
}


